3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2016 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
13 if (!defined('_ECRIRE_INC_VERSION')) return;
15 // http://doc.spip.org/@creer_pass_aleatoire
16 function creer_pass_aleatoire($longueur = 8, $sel = "") {
17 $seed = (double) (microtime() +
1) * time();
22 for ($i = 0; $i < $longueur; $i++
) {
26 $s = substr(md5(uniqid($s).$sel), 0, 16);
28 $r = unpack("Cr", pack("H2", $s.$s));
30 if ($x < 10) $x = chr($x +
48);
31 else if ($x < 36) $x = chr($x +
55);
32 else if ($x < 62) $x = chr($x +
61);
33 else if ($x == 63) $x = '/';
38 $pass = preg_replace("@[./]@", "a", $pass);
39 $pass = preg_replace("@[I1l]@", "L", $pass);
40 $pass = preg_replace("@[0O]@", "o", $pass);
45 * Creer un identifiant aleatoire
47 * http://doc.spip.org/@creer_uniqid
51 function creer_uniqid() {
55 $seed = (double) (microtime() +
1) * time();
67 // Renouvellement de l'alea utilise pour securiser les scripts dans action/
70 // http://doc.spip.org/@renouvelle_alea
71 function renouvelle_alea() {
72 if (!isset($GLOBALS['meta']['alea_ephemere'])){
73 include_spip('base/abstract_sql');
74 $GLOBALS['meta']['alea_ephemere'] = sql_getfetsel('valeur', 'spip_meta', "nom='alea_ephemere'");
76 ecrire_meta('alea_ephemere_ancien', @$GLOBALS['meta']['alea_ephemere'], 'non');
77 $GLOBALS['meta']['alea_ephemere'] = md5(creer_uniqid());
78 ecrire_meta('alea_ephemere', $GLOBALS['meta']['alea_ephemere'], 'non');
79 ecrire_meta('alea_ephemere_date', time(), 'non');
80 spip_log("renouvellement de l'alea_ephemere");
84 // low-security : un ensemble de fonctions pour gerer de l'identification
85 // faible via les URLs (suivi RSS, iCal...)
87 // http://doc.spip.org/@low_sec
88 function low_sec($id_auteur) {
89 // Pas d'id_auteur : low_sec
90 if (!$id_auteur = intval($id_auteur)) {
91 if (!$low_sec = $GLOBALS['meta']['low_sec']) {
92 ecrire_meta('low_sec', $low_sec = creer_pass_aleatoire());
96 $low_sec = sql_getfetsel("low_sec", "spip_auteurs", "id_auteur = $id_auteur");
98 $low_sec = creer_pass_aleatoire();
99 sql_updateq("spip_auteurs", array("low_sec" => $low_sec), "id_auteur = $id_auteur");
105 // Inclure les arguments significatifs pour le hachage
106 // cas particulier du statut pour compatibilite ancien rss/suivi_revisions
108 function param_low_sec($op, $args=array(), $lang='', $mime='rss')
111 foreach ($args as $val => $var)
113 if ($val<>'statut') $a .= ':' . $val.'-'.$var;
114 $b .= $val.'='.$var . '&';
117 $id = intval(@$GLOBALS['connect_id_auteur']);
124 . afficher_low_sec($id, "$mime $op $a")
125 . (!$a ?
'' : "&args=$a")
126 . (!$lang ?
'' : "&lang=$lang");
129 // http://doc.spip.org/@afficher_low_sec
130 function afficher_low_sec ($id_auteur, $action='') {
131 return substr(md5($action.low_sec($id_auteur)),0,8);
134 // http://doc.spip.org/@verifier_low_sec
135 function verifier_low_sec ($id_auteur, $cle, $action='') {
136 return ($cle == afficher_low_sec($id_auteur, $action));
139 // http://doc.spip.org/@effacer_low_sec
140 function effacer_low_sec($id_auteur) {
141 if (!$id_auteur = intval($id_auteur)) return; // jamais trop prudent ;)
142 sql_updateq("spip_auteurs", array("low_sec" => ''), "id_auteur = $id_auteur");
145 // http://doc.spip.org/@initialiser_sel
146 function initialiser_sel() {
148 if (CRYPT_MD5
) $htsalt = '$1$'.creer_pass_aleatoire();
152 // Cette fonction ne sert qu'a la connexion en mode http_auth.non LDAP
153 // Son role est de creer le fichier htpasswd
154 // Voir le plugin "acces restreint"
155 // http://doc.spip.org/@ecrire_acces
156 function ecrire_acces() {
157 $htaccess = _DIR_RESTREINT
. _ACCESS_FILE_NAME
;
158 $htpasswd = _DIR_TMP
. _AUTH_USER_FILE
;
160 // Cette variable de configuration peut etre posee par un plugin
161 // par exemple acces_restreint ;
162 // si .htaccess existe, outrepasser spip_meta
163 if (($GLOBALS['meta']['creer_htpasswd'] != 'oui')
164 AND !@file_exists
($htaccess)) {
165 spip_unlink($htpasswd);
166 spip_unlink($htpasswd."-admin");
170 # remarque : ici on laisse passer les "nouveau" de maniere a leur permettre
171 # de devenir redacteur le cas echeant (auth http)... a nettoyer
172 // attention, il faut au prealable se connecter a la base (necessaire car utilise par install)
173 // TODO: factoriser avec auth/spip qui fait deja ce job et generaliser le test spip_connect_ldap()
175 if (spip_connect_ldap()) return;
176 $p1 = ''; // login:htpass pour tous
177 $p2 = ''; // login:htpass pour les admins
178 $s = sql_select("login, htpass, statut", "spip_auteurs", sql_in("statut", array('1comite','0minirezo','nouveau')));
179 while ($t = sql_fetch($s)) {
180 if (strlen($t['login']) AND strlen($t['htpass'])) {
181 $p1 .= $t['login'].':'.$t['htpass']."\n";
182 if ($t['statut'] == '0minirezo')
183 $p2 .= $t['login'].':'.$t['htpass']."\n";
187 ecrire_fichier($htpasswd, $p1);
188 ecrire_fichier($htpasswd.'-admin', $p2);
189 spip_log("Ecriture de $htpasswd et $htpasswd-admin");
194 // http://doc.spip.org/@generer_htpass
195 function generer_htpass($pass) {
197 if (function_exists('crypt'))
198 return crypt($pass, $htsalt);
202 // Installe ou verifie un .htaccess, y compris sa prise en compte par Apache
204 // http://doc.spip.org/@verifier_htaccess
205 function verifier_htaccess($rep, $force=false) {
206 $htaccess = rtrim($rep,"/") . "/" . _ACCESS_FILE_NAME
;
207 if (((@file_exists
($htaccess)) OR defined('_TEST_DIRS')) AND !$force)
209 if ($ht = @fopen
($htaccess, "w")) {
210 fputs($ht, "deny from all\n");
212 @chmod
($htaccess, _SPIP_CHMOD
& 0666);
213 $t = rtrim($rep,"/") . "/.ok";
214 if ($ht = @fopen
($t, "w")) {
216 include_spip('inc/distant');
217 $t = substr($t,strlen(_DIR_RACINE
));
218 $t = url_de_base() . $t;
219 $ht = recuperer_lapage($t, false, 'HEAD', 0);
220 // htaccess inoperant si on a recupere des entetes HTTP
221 // (ignorer la reussite si connexion par fopen)
222 $ht = !(isset($ht[0]) AND $ht[0]);
225 spip_log("Creation de $htaccess " . ($ht ?
" reussie" : " manquee"));
231 // http://doc.spip.org/@gerer_htaccess
232 function gerer_htaccess() {
233 // Cette variable de configuration peut etre posee par un plugin
234 // par exemple acces_restreint
235 $f = ($GLOBALS['meta']['creer_htaccess'] === 'oui');
236 $dirs = sql_allfetsel('extension', 'spip_types_documents');
237 $dirs[] = array('extension' => 'distant');
238 foreach($dirs as $e) {
239 if (is_dir($dir = _DIR_IMG
. $e['extension'])) {
241 verifier_htaccess($dir);
242 else spip_unlink($dir . '/' . _ACCESS_FILE_NAME
);
245 return $GLOBALS['meta']['creer_htaccess'];