Skip to content
Extraits de code Groupes Projets
inc_version.php3 28,6 ko
Newer Older
<?php
Yannick Patois's avatar
Yannick Patois a validé

//
// Ce fichier ne sera execute qu'une fois
if (defined("_ECRIRE_INC_VERSION")) return;
define("_ECRIRE_INC_VERSION", "1");

Fil's avatar
Fil a validé

Fil's avatar
Fil a validé
// *********** traiter les variables ************
// Magic quotes : on n'en veut pas sur la base,
// et on nettoie les GET/POST/COOKIE le cas echeant
//

function magic_unquote($table) {
	if (is_array($GLOBALS[$table])) {
Fil's avatar
Fil a validé
		reset($GLOBALS[$table]);
		while (list($key, $val) = each($GLOBALS[$table])) {
			if (is_string($val))
Antoine Pitrou's avatar
Antoine Pitrou a validé
				$GLOBALS[$table][$key] = stripslashes($val);
Fil's avatar
Fil a validé
		}
Fil's avatar
Fil a validé
	}
}

@set_magic_quotes_runtime(0);
$unquote_gpc = @get_magic_quotes_gpc();
Fil's avatar
Fil a validé

if ($unquote_gpc) {
	magic_unquote('HTTP_GET_VARS');
	magic_unquote('HTTP_POST_VARS');
	magic_unquote('HTTP_COOKIE_VARS');
}

//
// Dirty hack contre le register_globals a 'Off' (PHP 4.1.x)
// A remplacer par une gestion propre des variables admissibles ;-)
//

Antoine Pitrou's avatar
Antoine Pitrou a validé
$INSECURE = array();

function feed_globals($table, $insecure = true, $ignore_variables_contexte = false) {
Antoine Pitrou's avatar
Antoine Pitrou a validé
	global $INSECURE;

	// ignorer des cookies qui contiendraient du contexte 
	$is_contexte = array('id_parent'=>1, 'id_rubrique'=>1, 'id_article'=>1, 'id_auteur'=>1,
		'id_breve'=>1, 'id_forum'=>1, 'id_secteur'=>1, 'id_syndic'=>1, 'id_syndic_article'=>1,
		'id_mot'=>1, 'id_groupe'=>1, 'id_document'=>1, 'date'=>1, 'lang'=>1);

Fil's avatar
Fil a validé
	if (is_array($GLOBALS[$table])) {
        reset($GLOBALS[$table]);
        while (list($key, $val) = each($GLOBALS[$table])) {
Fil's avatar
Fil a validé
			if ($ignore_variables_contexte AND isset($is_contexte[$key]))
				unset ($GLOBALS[$key]);
			else
				$GLOBALS[$key] = $val;
Antoine Pitrou's avatar
Antoine Pitrou a validé
			if ($insecure) $INSECURE[$key] = $val;
Fil's avatar
Fil a validé
	}
}

feed_globals('HTTP_COOKIE_VARS', true, true);
Fil's avatar
Fil a validé
feed_globals('HTTP_GET_VARS');
feed_globals('HTTP_POST_VARS');
Antoine Pitrou's avatar
Antoine Pitrou a validé
feed_globals('HTTP_SERVER_VARS', false);
Fil's avatar
Fil a validé


//
// Avec register_globals a Off sous PHP4, il faut utiliser
// la nouvelle variable HTTP_POST_FILES pour les fichiers uploades
// (pas valable sous PHP3...)
//

function feed_post_files($table) {
Antoine Pitrou's avatar
Antoine Pitrou a validé
	global $INSECURE;
Fil's avatar
Fil a validé
	if (is_array($GLOBALS[$table])) {
	        reset($GLOBALS[$table]);
	        while (list($key, $val) = each($GLOBALS[$table])) {
Antoine Pitrou's avatar
Antoine Pitrou a validé
	                $GLOBALS[$key] = $INSECURE[$key] = $val['tmp_name'];
	                $GLOBALS[$key.'_name'] = $INSECURE[$key.'_name'] = $val['name'];
Fil's avatar
Fil a validé
	        }
	}
}

feed_post_files('HTTP_POST_FILES');

Antoine Pitrou's avatar
Antoine Pitrou a validé
//
Antoine Pitrou's avatar
Antoine Pitrou a validé
// 	*** Parametrage par defaut de SPIP ***
//
Fil's avatar
Fil a validé
// Ces parametres d'ordre technique peuvent etre modifies
Antoine Pitrou's avatar
Antoine Pitrou a validé
// dans ecrire/mes_options.php3. Les valeurs specifiees
// dans ce dernier fichier remplaceront automatiquement
// les valeurs ci-dessous.
//
// Pour creer ecrire/mes_options.php3 : recopier simplement
// les lignes ci-dessous, et ajouter le marquage de debut et
// de fin de fichier PHP ("< ?php" et "? >", sans les espaces)
Antoine Pitrou's avatar
Antoine Pitrou a validé
//
Fil's avatar
Fil a validé

// Prefixe des tables dans la base de donnees
// (a modifier pour avoir plusieurs sites SPIP dans une seule base)
$table_prefix = "spip";

// Prefixe et chemin des cookies
// (a modifier pour installer des sites SPIP dans des sous-repertoires)
$cookie_prefix = "spip";
$cookie_path = "";
// Dossier des squelettes
// (a modifier si l'on veut passer rapidement d'un jeu de squelettes a un autre)
$dossier_squelettes = "";

// faut-il autoriser SPIP a compresser les pages a la volee quand le
// navigateur l'accepte (valable pour apache >= 1.3 seulement) ?
$auto_compress = true;

// creation des vignettes avec image magick en ligne de commande : mettre
// le chemin complet '/bin/convert' (Linux) ou '/sw/bin/convert' (fink/Mac OS X)
// Note : preferer GD2 ou le module php imagick s'ils sont disponibles
$convert_command = 'convert';

// faut-il passer les connexions MySQL en mode debug ?
Fil's avatar
Fil a validé
$mysql_debug = false;

// faut-il chronometrer les requetes MySQL ?
$mysql_profile = false;

// faut-il faire des connexions completes rappelant le nom du serveur et de
// la base MySQL ? (utile si vos squelettes appellent d'autres bases MySQL)
$mysql_rappel_connexion = false;
// faut-il afficher en rouge les chaines non traduites ?
$test_i18n = false;
// faut-il souligner en gris, dans ecrire/articles.php3, les espaces insecables ?
$activer_revision_nbsp = false;

Fil's avatar
Fil a validé
// gestion des extras (voir ecrire/inc_extra.php3 pour plus d'informations)
$champs_extra = false;
$champs_extra_proposes = false;

// faut-il ignorer l'authentification par auth http/remote_user ?
// cela permet d'avoir un SPIP sous .htaccess (ignore_remote_user),
// mais aussi de fonctionner sur des serveurs debiles se
// bloquant sur PHP_AUTH_USER=root (ignore_auth_http)
$ignore_auth_http = false;
$ignore_remote_user = false;
Fil's avatar
Fil a validé

// Faut-il afficher les boutons d'admin 'debug cache' et 'debug squelette' ?
$bouton_admin_debug = false;

// Faut-il "invalider" les caches quand on depublie ou modifie un article ?
# en faire une option dans l'interface de configuration ?
# NB: cette option ne concerne que articles,breves,rubriques et site
# car les forums sont toujours invalidants.
$invalider_caches = true;

$spip_server = array (
	'tex' => 'http://math.spip.org/tex.php',
ARNO*'s avatar
ARNO* a validé
	'mathml' => 'http://arno.rezo.net/tex2mathml/latex.php',
	'ortho' => 'http://ortho.spip.net/ortho_serveur.php'
);

// Produire du TeX ou de MathML ?
$traiter_math = 'tex';

Fil's avatar
Fil a validé
/* ATTENTION CES VARIABLES NE FONCTIONNENT PAS ENCORE */
// Extension du fichier du squelette 
$extension_squelette = 'html';
/* / MERCI DE VOTRE ATTENTION */


Antoine Pitrou's avatar
Antoine Pitrou a validé
//
// *** Fin du parametrage ***
Antoine Pitrou's avatar
Antoine Pitrou a validé
//
Fil's avatar
Fil a validé

Fil's avatar
Fil a validé
$flag_ecrire = !@file_exists('./ecrire/inc_version.php3');
Antoine Pitrou's avatar
Antoine Pitrou a validé
if ($flag_ecrire) {
Fil's avatar
Fil a validé
	if (@file_exists('mes_options.php3')) {
Antoine Pitrou's avatar
Antoine Pitrou a validé
		include('mes_options.php3');
Fil's avatar
Fil a validé
	if (@file_exists('ecrire/mes_options.php3')) {
Antoine Pitrou's avatar
Antoine Pitrou a validé
		include('ecrire/mes_options.php3');
// Version courante de SPIP
// Stockee sous forme de nombre decimal afin de faciliter les comparaisons
// (utilise pour les modifs de la base de donnees)

// version de la base

// version de spip
$spip_version_affichee = "1.8 alpha 4 CVS";
// version de spip / tag cvs
if (ereg('Name: v(.*) ','$Name$', $regs)) $spip_version_affichee = $regs[1];


Fil's avatar
 
Fil a validé
// Pas de warnings idiots
error_reporting(E_ALL ^ E_NOTICE);

// ** Securite **
$auteur_session = '';
$connect_statut = '';
$hash_recherche_strict = '';
Antoine Pitrou's avatar
Antoine Pitrou a validé
// (doit etre au moins egale a 3.0.8)
//

$php_version = explode('.', phpversion());
$php_version_maj = intval($php_version[0]);
$php_version_med = intval($php_version[1]);
if (ereg('([0-9]+)', $php_version[2], $match)) $php_version_min = intval($match[1]);

$flag_levenshtein = ($php_version_maj >= 4);
$flag_uniqid2 = ($php_version_maj > 3 OR $php_version_min >= 13);
Antoine Pitrou's avatar
Antoine Pitrou a validé
$flag_get_cfg_var = (@get_cfg_var('error_reporting') != "");
$flag_strtr2 = ($php_version_maj > 3);
Antoine Pitrou's avatar
Antoine Pitrou a validé
$flag_ini_get = (function_exists("ini_get")
	&& (@ini_get('max_execution_time') > 0));	// verifier pas desactivee
$flag_gz = function_exists("gzopen");
$flag_ob = ($flag_ini_get
	&& !ereg("ob_", ini_get('disable_functions'))
	&& function_exists("ob_start"));
$flag_obgz = ($flag_ob && function_exists("ob_gzhandler"));
$flag_pcre = function_exists("preg_replace");
$flag_crypt = function_exists("crypt");
$flag_wordwrap = function_exists("wordwrap");
$flag_apc = function_exists("apc_rm");
$flag_sapi_name = function_exists("php_sapi_name");
$flag_utf8_decode = function_exists("utf8_decode");
$flag_ldap = function_exists("ldap_connect");
$flag_flock = function_exists("flock");
$flag_ImageCreateTrueColor = function_exists("ImageCreateTrueColor");
$flag_ImageCopyResampled = function_exists("ImageCopyResampled");
Antoine Pitrou's avatar
Antoine Pitrou a validé
$flag_ImageGif = function_exists("ImageGif");
$flag_ImageJpeg = function_exists("ImageJpeg");
$flag_ImagePng = function_exists("ImagePng");
Fil's avatar
Fil a validé
$flag_imagick = function_exists("imagick_readimage");	// http://pear.sourceforge.net/en/packages.imagick.php
Antoine Pitrou's avatar
Antoine Pitrou a validé
$flag_multibyte = function_exists("mb_encode_mimeheader");
$flag_iconv = function_exists("iconv");
Fil's avatar
Fil a validé
$flag_strtotime = function_exists("strtotime");
$flag_gd = $flag_ImageGif || $flag_ImageJpeg || $flag_ImagePng;
$flag_revisions = ($flag_pcre AND function_exists("gzcompress"));
Yannick Patois's avatar
Yannick Patois a validé

//
// Appliquer le prefixe cookie
//
function spip_setcookie ($name='', $value='', $expire=0, $path='AUTO', $domain='', $secure='') {
	$name = ereg_replace ('^spip', $GLOBALS['cookie_prefix'], $name);
	if ($path == 'AUTO') $path=$GLOBALS['cookie_path'];
Antoine Pitrou's avatar
Antoine Pitrou a validé

	if ($secure)
		@setcookie ($name, $value, $expire, $path, $domain, $secure);
	else if ($domain)
		@setcookie ($name, $value, $expire, $path, $domain);
	else if ($path)
		@setcookie ($name, $value, $expire, $path);
	else if ($expire)
		@setcookie ($name, $value, $expire);
	else
		@setcookie ($name, $value);
}
Antoine Pitrou's avatar
Antoine Pitrou a validé
if ($cookie_prefix != 'spip') {
	reset ($HTTP_COOKIE_VARS);
	while (list($name,$value) = each($HTTP_COOKIE_VARS)) {
		if (ereg('^spip', $name)) {
			unset($HTTP_COOKIE_VARS[$name]);
			unset($$name);
		}
	}
	reset ($HTTP_COOKIE_VARS);
	while (list($name,$value) = each($HTTP_COOKIE_VARS)) {
		if (ereg('^'.$cookie_prefix, $name)) {
			$spipname = ereg_replace ('^'.$cookie_prefix, 'spip', $name);
			$HTTP_COOKIE_VARS[$spipname] = $INSECURE[$spipname] = $value;
Yannick Patois's avatar
Yannick Patois a validé
//
// Infos sur l'hebergeur
//

$hebergeur = '';
$login_hebergeur = '';
$os_serveur = '';

Antoine Pitrou's avatar
Antoine Pitrou a validé

// Lycos (ex-Multimachin)
if ($HTTP_X_HOST == 'membres.lycos.fr') {
	$hebergeur = 'lycos';
	ereg('^/([^/]*)', $REQUEST_URI, $regs);
	$login_hebergeur = $regs[1];
Yannick Patois's avatar
Yannick Patois a validé
}
// Altern
Antoine Pitrou's avatar
Antoine Pitrou a validé
if (ereg('altern\.com$', $SERVER_NAME)) {
Yannick Patois's avatar
Yannick Patois a validé
	$hebergeur = 'altern';
	ereg('([^.]*\.[^.]*)$', $HTTP_HOST, $regs);
	$login_hebergeur = ereg_replace('[^a-zA-Z0-9]', '_', $regs[1]);
}
// Free
else if (ereg('^/([^/]*)\.free.fr/', $REQUEST_URI, $regs)) {
	$hebergeur = 'free';
	$login_hebergeur = $regs[1];
}
// NexenServices
else if ($SERVER_ADMIN == 'www@nexenservices.com') {
Fil's avatar
Fil a validé
	if (!function_exists('email'))
		include ('mail.inc');
Yannick Patois's avatar
Yannick Patois a validé
	$hebergeur = 'nexenservices';
}
Antoine Pitrou's avatar
Antoine Pitrou a validé
// Online
else if (function_exists('email')) {
	$hebergeur = 'online';
}
Yannick Patois's avatar
Yannick Patois a validé

if (eregi('\(Win', $HTTP_SERVER_VARS['SERVER_SOFTWARE']))
	$os_serveur = 'windows';

// Droits d'acces maximum par defaut
@umask(0);


//
// Infos sur le fichier courant
//

// Compatibilite avec serveurs ne fournissant pas $REQUEST_URI
if (!$REQUEST_URI) {
Yannick Patois's avatar
Yannick Patois a validé
	$REQUEST_URI = $PHP_SELF;
	if (!strpos($REQUEST_URI, '?') && $QUERY_STRING)
		$REQUEST_URI .= '?'.$QUERY_STRING;
}
Fil's avatar
Fil a validé
$dir_ecrire = (ereg("/ecrire/", $GLOBALS['REQUEST_URI'])) ? '' : 'ecrire/';
Yannick Patois's avatar
Yannick Patois a validé

if (!$PATH_TRANSLATED) {
	if ($SCRIPT_FILENAME) $PATH_TRANSLATED = $SCRIPT_FILENAME;
	else if ($DOCUMENT_ROOT && $SCRIPT_URL) $PATH_TRANSLATED = $DOCUMENT_ROOT.$SCRIPT_URL;
}


//
// Gestion des inclusions et infos repertoires
//

$included_files = '';

function include_local($file) {
	if ($GLOBALS['included_files'][$file]) return;
	include($file);
	$GLOBALS['included_files'][$file] = 1;
}

function include_ecrire($file) {
	if (!$GLOBALS['flag_ecrire']) $file = "ecrire/$file";
	if ($GLOBALS['included_files'][$file]) return;
	include($file);
	$GLOBALS['included_files'][$file] = 1;
}

Antoine Pitrou's avatar
Antoine Pitrou a validé

Fil's avatar
Fil a validé
$flag_connect = @file_exists(($flag_ecrire ? "" : "ecrire/")."inc_connect.php3");
Antoine Pitrou's avatar
Antoine Pitrou a validé

function spip_query($query) {
Antoine Pitrou's avatar
Antoine Pitrou a validé
	if ($GLOBALS['flag_connect']) {
Antoine Pitrou's avatar
Antoine Pitrou a validé
		include_ecrire("inc_connect.php3");
		if (!$GLOBALS['db_ok'])
			return;
		if ($GLOBALS['spip_connect_version'] < 0.1) {
			if (!$GLOBALS['flag_ecrire']) {
				$GLOBALS['db_ok'] = false;
				return;
			}
			@Header("Location: upgrade.php3?reinstall=oui");
			exit;
		}
	}
Fil's avatar
Fil a validé
	return spip_query_db($query);
Yannick Patois's avatar
Yannick Patois a validé
//
// Infos de config PHP
//

// cf. liste des sapi_name - http://fr.php.net/php_sapi_name
$php_module = (($flag_sapi_name AND eregi("apache", @php_sapi_name())) OR
	ereg("^Apache.* PHP", $SERVER_SOFTWARE));
$php_cgi = ($flag_sapi_name AND eregi("cgi", @php_sapi_name()));

function http_status($status) {
	global $php_cgi, $REDIRECT_STATUS;

	if ($REDIRECT_STATUS && $REDIRECT_STATUS == $status) return;
	$status_string = array(
		200 => '200 OK',
		304 => '304 Not Modified',
		401 => '401 Unauthorized',
		403 => '403 Forbidden',
		404 => '404 Not Found'
	);
	if ($php_cgi) Header("Status: $status");
	else Header("HTTP/1.0 ".$status_string[$status]);
}
Yannick Patois's avatar
Yannick Patois a validé

function http_last_modified($lastmodified, $expire = 0) {
	$gmoddate = gmdate("D, d M Y H:i:s", $lastmodified);
	if ($GLOBALS['HTTP_IF_MODIFIED_SINCE']) {
		$if_modified_since = ereg_replace(';.*$', '', $GLOBALS['HTTP_IF_MODIFIED_SINCE']);
		$if_modified_since = trim(str_replace('GMT', '', $if_modified_since));
		if ($if_modified_since == $gmoddate) {
			http_status(304);
			$headers_only = true;
		}
	}
	@Header ("Last-Modified: ".$gmoddate." GMT");
	if ($expire) 
		@Header ("Expires: ".gmdate("D, d M Y H:i:s", $expire)." GMT");
	return $headers_only;
}
$flag_upload = (!$flag_get_cfg_var || (get_cfg_var('upload_max_filesize') > 0));
Yannick Patois's avatar
Yannick Patois a validé
function tester_upload() {
Antoine Pitrou's avatar
Antoine Pitrou a validé
	return $GLOBALS['flag_upload'];
Yannick Patois's avatar
Yannick Patois a validé
}


//
// Reglage de l'output buffering : si possible, generer une sortie
// compressee pour economiser de la bande passante
//

if ($auto_compress && $flag_obgz) {
Yannick Patois's avatar
Yannick Patois a validé
	$use_gz = true;

	// si un buffer est deja ouvert, stop
	if (ob_get_contents())
		$use_gz = false;

Antoine Pitrou's avatar
Antoine Pitrou a validé
	// si la compression est deja commencee, stop
	else if (@ini_get("zlib.output_compression") || @ini_get("output_handler"))
		$use_gz = false;

Yannick Patois's avatar
Yannick Patois a validé
	// special bug de proxy
Antoine Pitrou's avatar
Antoine Pitrou a validé
	else if (eregi("NetCache|Hasd_proxy", $HTTP_VIA))
Yannick Patois's avatar
Yannick Patois a validé
		$use_gz = false;
Yannick Patois's avatar
Yannick Patois a validé
	// special bug Netscape Win 4.0x
Antoine Pitrou's avatar
Antoine Pitrou a validé
	else if (eregi("Mozilla/4\.0[^ ].*Win", $HTTP_USER_AGENT))
Fil's avatar
Fil a validé
		$use_gz = false;

Antoine Pitrou's avatar
Antoine Pitrou a validé
	// special bug Apache2x
Antoine Pitrou's avatar
Antoine Pitrou a validé
	else if (eregi("Apache(-[^ ]+)?/2", $SERVER_SOFTWARE))
Yannick Patois's avatar
Yannick Patois a validé
		$use_gz = false;
	else if ($flag_sapi_name && ereg("^apache2", @php_sapi_name()))
Antoine Pitrou's avatar
Antoine Pitrou a validé
		$use_gz = false;
ARNO*'s avatar
ARNO* a validé
		
Yannick Patois's avatar
Yannick Patois a validé
	if ($use_gz) {
		@ob_start("ob_gzhandler");
	}
	@header("Vary: Cookie, Accept-Encoding");
Yannick Patois's avatar
Yannick Patois a validé
}
ARNO*'s avatar
ARNO* a validé
else {
	@header("Vary: Cookie");
}
Yannick Patois's avatar
Yannick Patois a validé

class Link {
	var $file;
	var $vars;
	var $arrays;
	var $s_vars;
	var $t_vars, $t_var_idx, $t_var_cnt;

	//
	// Contructeur : a appeler soit avec l'URL du lien a creer,
	// soit sans parametres, auquel cas l'URL est l'URL courante
	//
	function Link($url = '', $reentrant = false) {
		static $link = '';

		// If root link not defined, create it
		if (!$link && !$reentrant) {
			$link = new Link('', true);
		}

		$this->vars = array();
		$this->s_vars = array();
		$this->t_vars = array();
		$this->t_var_idx = array();
		$this->t_var_cnt = 0;

		// Normal case
		if ($link) {
			$this->s_vars = $link->s_vars;
			$this->t_vars = $link->t_vars;
			$this->t_var_idx = $link->t_var_idx;
			$this->t_var_cnt = $link->t_var_cnt;
			if ($url) {
				$v = split('[\?\&]', $url);
				list(, $this->file) = each($v);
				while (list(, $var) = each($v)) {
					list($name, $value) = split('=', $var, 2);
					$name = urldecode($name);
					$value = urldecode($value);
					if (ereg('^(.*)\[\]$', $name, $regs)) {
						$this->arrays[$regs[1]][] = $value;
					}
					else {
						$this->vars[$name] = $value;
					}
				}
			}
			else {
				$this->file = $link->file;
				$this->vars = $link->vars;
				$this->arrays = $link->arrays;
			}
			return;
		}

		// Special case : create root link

		// If no URL specified, take current one
		if (!$url) {
			$url = $GLOBALS['REQUEST_URI'];
			$url = substr($url, strrpos($url, '/') + 1);
Antoine Pitrou's avatar
Antoine Pitrou a validé
			if (!$url) $url = "./";
Yannick Patois's avatar
Yannick Patois a validé
			if (count($GLOBALS['HTTP_POST_VARS'])) $vars = $GLOBALS['HTTP_POST_VARS'];
		}
		$v = split('[\?\&]', $url);
		list(, $this->file) = each($v);

		// GET variables are read from the original URL
		// (HTTP_GET_VARS may contain additional variables introduced by rewrite-rules)
		if (!$vars) {
			while (list(, $var) = each($v)) {
				list($name, $value) = split('=', $var, 2);
				$name = urldecode($name);
				$value = urldecode($value);
				if (ereg('^(.*)\[\]$', $name, $regs)) {
					$vars[$regs[1]][] = $value;
				}
				else {
					$vars[$name] = $value;
				}
			}
		}

		if (is_array($vars)) {
			reset($vars);
			while (list($name, $value) = each($vars)) {
				$p = substr($name, 0, 2);
				if ($p == 's_') {
					$this->s_vars[$name] = $value;
				}
				else if ($p == 't_') {
					$this->_addTmpHash($name, $value);
				}
				else {
					if (is_array($value))
						$this->arrays[$name] = $value;
					else
						$this->vars[$name] = $value;
				}
			}
		}
	}

	function _addTmpHash($name, $value) {
		if ($i = $this->t_var_idx[$name]) {
			$this->t_vars[--$i] = $value;
		}
		else {
			$this->t_vars[$this->t_var_cnt] = $value;
			$this->t_var_idx[$name] = ++$this->t_var_cnt;
			if ($this->t_var_cnt >= 5) $this->t_var_cnt = 0;
		}
	}

	//
	// Effacer toutes les variables
	//

	function clearVars() {
		$this->vars = '';
		$this->arrays = '';
	}

	//
	// Effacer une variable
	//

	function delVar($name) {
ARNO*'s avatar
ARNO* a validé
		if($this->vars[$name]) unset($this->vars[$name]);
		if($this->arrays[$name]) unset($this->arrays[$name]);
Yannick Patois's avatar
Yannick Patois a validé
	}

	//
	// Ajouter une variable
	// (si aucune valeur n'est specifiee, prend la valeur globale actuelle)
	//

	function addVar($name, $value = '__global__') {
		if ($value == '__global__') $value = $GLOBALS[$name];
		if (is_array($value))
			$this->arrays[$name] = $value;
		else
			$this->vars[$name] = $value;
	}

	function getVar($name) {
		return $this->vars[$name];
	}

Yannick Patois's avatar
Yannick Patois a validé
	//
	// Ajouter une variable de session
	// (variable dont la valeur est transmise d'un lien a l'autre)
	//

	function addSessionVar($name, $value) {
		$this->addVar('s_'.$name, $value);
	}

	function getSessionVar($name) {
		return $this->vars['s_'.$name];
	}

	//
	// Ajouter une variable temporaire
	// (variable dont le nom est arbitrairement long, et dont la valeur
	// est transmise de lien en lien dans la limite de cinq variables)
	//

	function addTmpVar($name, $value) {
		$this->_addTmpHash('t_'.substr(md5($name), 0, 4), $value);
	}

	function getTmpVar($name) {
		if ($i = $this->t_var_idx['t_'.substr(md5($name), 0, 4)]) {
			return $this->t_vars[--$i];
		}
	}

	function getAllVars() {
		if (is_array($this->t_var_idx)) {
			reset($this->t_var_idx);
			while (list($name, $i) = each($this->t_var_idx)) $vars[$name] = $this->t_vars[--$i];
		}
		if (is_array($this->vars)) {
			reset($this->vars);
			while (list($name, $value) = each($this->vars)) $vars[$name] = $value;
		}
		if (is_array($this->s_vars)) {
			reset($this->s_vars);
			while (list($name, $value) = each($this->s_vars)) $vars[$name] = $value;
		}
		return $vars;
	}

	//
	// Recuperer l'URL correspondant au lien
	//

	function getUrl($anchor = '') {
		$url = $this->file;
Yannick Patois's avatar
Yannick Patois a validé
		$query = '';
		$vars = $this->getAllVars();
		if (is_array($vars)) {
			$first = true;
			reset($vars);
			while (list($name, $value) = each($vars)) {
				$query .= (($query) ? '&' : '?').$name.'='.urlencode($value);
			}
		}
		if (is_array($this->arrays)) {
			reset($this->arrays);
			while (list($name, $table) = each($this->arrays)) {
				reset($table);
				while (list(, $value) = each($table)) {
					$query .= (($query) ? '&' : '?').$name.'[]='.urlencode($value);
				}
			}
		}
		if ($anchor) $anchor = '#'.$anchor;
		return $url.$query.$anchor;
	}

	//
	// Recuperer le debut de formulaire correspondant au lien
	// (tag ouvrant + entrees cachees representant les variables)
	//

	function getForm($method = 'get', $anchor = '', $enctype = '') {
Yannick Patois's avatar
Yannick Patois a validé
		if ($anchor) $anchor = '#'.$anchor;
		$form = "<form method='$method' action='".$this->file.$anchor."'";
		if ($enctype) $form .= " enctype='$enctype'";
		$form .= " style='border: 0px; margin: 0px;'>\n";
Yannick Patois's avatar
Yannick Patois a validé
		$vars = $this->getAllVars();
		if (is_array($vars)) {
			reset($vars);
			while (list($name, $value) = each($vars)) {
Fil's avatar
Fil a validé
				$value = ereg_replace('&amp;(#[0-9]+;)', '&\1', htmlspecialchars($value));
				$form .= "<input type=\"hidden\" name=\"$name\" value=\"$value\" />\n";
Yannick Patois's avatar
Yannick Patois a validé
			}
		}
		if (is_array($this->arrays)) {
			reset($this->arrays);
			while (list($name, $table) = each($this->vars)) {
				reset($table);
				while (list(, $value) = each($table)) {
					$value = ereg_replace('&amp;(#[0-9]+;)', '&\1', htmlspecialchars($value));
					$form .= "<input type=\"hidden\" name=\"".$name."[]\" value=\"$value\" />\n";
Yannick Patois's avatar
Yannick Patois a validé
				}
			}
		}
		return $form;
	}

	//
	// Recuperer l'attribut href="<URL>" correspondant au lien
	//

	function getHref($anchor = '') {
		return 'href="'.$this->getUrl($anchor).'"';
Yannick Patois's avatar
Yannick Patois a validé
	}
}

//
// Creer un lien et retourner directement le href="<URL>" correspondant
//

function newLinkHref($url) {
	$link = new Link($url);
	return $link->getHref();
}

function newLinkUrl($url) {
	$link = new Link($url);
	return $link->getUrl();
}

Yannick Patois's avatar
Yannick Patois a validé
//
// Recuperer la valeur d'une variable de session sur la page courante
//

function getSessionVar($name) {
	return $GLOBALS['this_link']->getSessionVar($name);
}

//
// Recuperer la valeur d'une variable temporaire sur la page courante
//

function getTmpVar($name) {
	return $GLOBALS['this_link']->getTmpVar($name);
}

// Lien vers la page demandee et lien nettoye ne contenant que des id_objet
Yannick Patois's avatar
Yannick Patois a validé
$this_link = new Link();

$clean_link->delVar('submit');
$clean_link->delVar('recalcul');
if (count($GLOBALS['HTTP_POST_VARS'])) {
	$clean_link->clearVars();
	$vars = array('id_article', 'coll', 'id_breve', 'id_rubrique', 'id_syndic', 'id_mot', 'id_auteur', 'var_login'); // il en manque probablement ?
	while (list(,$var) = each($vars)) {
		if (isset($$var)) {
			$clean_link->addVar($var, $$var);
			break;
		}
	}
}
Yannick Patois's avatar
Yannick Patois a validé

// URLs avec passage & -> &amp;

function quote_amp ($url) {
	$url = str_replace("&amp;", "&", $url);
	$url = str_replace("&", "&amp;", $url);
//
// Lire un fichier de maniere un peu sure
//


// flock() marche dans ce repertoire <=> j'ai le droit de flock() sur ce fichier
if (LOCK_UN!=3) {
	define ('LOCK_SH', 1);
	define ('LOCK_EX', 2);
	define ('LOCK_UN', 3);
	define ('LOCK_NB', 4);
}
function test_flock ($dir, $fp=false) {
Fil's avatar
Fil a validé
	static $flock = array();
	global $flag_flock;
	if (!$flag_flock)
		return false;

Fil's avatar
Fil a validé
	if (!$dir) $dir = '.';

	// premier appel pour ce $dir ?
	if (!isset($flock[$dir])) {
		// si on me passe un pointeur, je teste et j'ecris le resultat
		if ($fp) {
			if (@flock($fp, LOCK_SH)) {
				@flock($fp, LOCK_UN);
				@touch("$dir/.flock_ok");
			} else
				@unlink("$dir/.flock_ok");
		}
		// si le fichier est la et pas trop vieux -- id est:
		// pas recopie depuis une autre installation ! -- c'est ok.
		$flock[$dir] = (@file_exists("$dir/.flock_ok")
		AND (filemtime("$dir/.flock_ok") > time() - 3600));
Fil's avatar
Fil a validé
	return $flock[$dir];
}

// Si flock ne marche pas dans ce repertoire ou chez cet hebergeur,
// on renvoie OK pour ne pas bloquer
function spip_flock($filehandle, $mode, $fichier) {
	preg_match('|(.*)/([^/]*)$|', $fichier, $match);
	$dir = $match[1];

	if (!test_flock($dir))
		return true;
	else
		return @flock($filehandle, $mode);
}

// options = array(
// 'size' => 1024      # recuperer seulement le debut
// 'phpcheck' => 'oui' # verifier qu'on a bien du php
function lire_fichier ($fichier, &$contenu, $options=false) {
	$contenu = '';
	if (!@file_exists($fichier))
		return false;

	if ($fl = @fopen($fichier, 'r')) {

		// verrou lecture
		while (!spip_flock($fl, LOCK_SH, $fichier));

		if (!$s = $options['size'])
			$s = filesize($fichier);
		$contenu = @fread($fl, $s);

		// liberer le verrou
		spip_flock($fl, LOCK_UN, $fichier);
		@fclose($fl);

		// Verifications
		$ok = (strlen($contenu) == $s);
		if ($options['phpcheck'] == 'oui')
			$ok &= (ereg("[?]>\n?$", $contenu));
		return $ok;
	}
}

ARNO*'s avatar
ARNO* a validé

// Ecrire un fichier de maniere un peu sure
function ecrire_fichier ($fichier, $contenu) {

	// Ecriture dans un fichier temporaire
	// dans le repertoire destination
	preg_match('|(.*)/([^/]*)$|', $fichier, $match);
	$dir = $match[1];
	$fichiertmp = $dir.'/'
	.uniqid(substr(md5($fichier),0,6).'-'
	.@getmypid()).".tmp";
	if ($ft = @fopen($fichiertmp, 'wb')) {
		// on en profite pour tester flock()
		$flock = test_flock($dir, $ft);
		$s = @fputs($ft, $contenu);
		@fclose($ft);
		$ok = (strlen($contenu) == $s);
	}

	if ($ok) {
		// ouvrir et obtenir un verrou sur le fichier destination
		if ($fp = @fopen($fichier, 'a')) {
			if ($flock) while (!spip_flock($fp, LOCK_EX, $fichier));

			// recopier le temporaire
			$ok = @rename($fichiertmp, $fichier);

			// liberer le verrou
			if ($flock) spip_flock($fp, LOCK_UN, $fichier);

			@fclose($fp);
		}
	}


	// en cas d'echec effacer le temporaire
	if (!$ok)
		@unlink($fichiertmp);

	return $ok;


//
// Lire les meta cachees
//
if (!defined('_ECRIRE_INC_META_CACHE') AND !defined('_ECRIRE_INC_META')
AND lire_fichier ($dir_ecrire.'data/inc_meta_cache.php3', $contenu,
array('phpcheck' => 'oui')))
	eval('?'.'>'.$contenu);

if (!defined("_ECRIRE_INC_META_CACHE")) {
	function lire_meta($nom) {
		global $meta;
		return $meta[$nom];
	}
	function lire_meta_maj($nom) {
		global $meta_maj;
		return $meta_maj[$nom];
	}
	define("_ECRIRE_INC_META_CACHE", "1");

// Verifier la conformite d'une ou plusieurs adresses email
function email_valide($adresse) {
	$adresses = explode(',', $adresse);
	if (is_array($adresses)) {
		while (list(, $adresse) = each($adresses)) {
Fil's avatar
Fil a validé
			// nettoyer certains formats
			// "Marie Toto <Marie@toto.com>"
			$adresse = eregi_replace("^[^<>\"]*<([^<>\"]+)>$", "\\1", $adresse);
Antoine Pitrou's avatar
Antoine Pitrou a validé
			// RFC 822
			if (!eregi('^[^()<>@,;:\\"/[:space:]]+(@([-_0-9a-z]+\.)*[-_0-9a-z]+)?$', trim($adresse)))
				return false;
		}
		return true;
	return false;
Antoine Pitrou's avatar
Antoine Pitrou a validé
}
Yannick Patois's avatar
Yannick Patois a validé

Fil's avatar
 
Fil a validé
//
// Traduction des textes de SPIP
Fil's avatar
 
Fil a validé
//
function _T($text, $args = '') {
	include_ecrire('inc_lang.php3');
	$text = traduire_chaine($text, $args);

	if ($GLOBALS['xhtml']) {
		include_ecrire("inc_charsets.php3");
		$text = html2unicode($text);
	}
// chaines en cours de traduction
function _L($text) {
	if ($GLOBALS['test_i18n'])
		return "<span style='color:red;'>$text</span>";
	else
		return $text;
$langue_site = lire_meta('langue_site');
if (!$langue_site) include_ecrire('inc_lang.php3');
Fil's avatar
 
Fil a validé

Fil's avatar
Fil a validé

// Nommage bizarre des tables d'objets
function table_objet($type) {
	if ($type == 'syndic' OR $type == 'forum')
		return $type;
	else
		return $type.'s';