[SPIP] ~v3.0.20-->v3.0.25
[lhc/web/clavette_www.git] / www / ecrire / inc / acces.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2016 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
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 \***************************************************************************/
12
13 if (!defined('_ECRIRE_INC_VERSION')) return;
14
15 // http://doc.spip.org/@creer_pass_aleatoire
16 function creer_pass_aleatoire($longueur = 8, $sel = "") {
17 $seed = (double) (microtime() + 1) * time();
18 mt_srand($seed);
19 srand($seed);
20 $s = '';
21 $pass = '';
22 for ($i = 0; $i < $longueur; $i++) {
23 if (!$s) {
24 $s = mt_rand();
25 if (!$s) $s = rand();
26 $s = substr(md5(uniqid($s).$sel), 0, 16);
27 }
28 $r = unpack("Cr", pack("H2", $s.$s));
29 $x = $r['r'] & 63;
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 = '/';
34 else $x = '.';
35 $pass .= $x;
36 $s = substr($s, 2);
37 }
38 $pass = preg_replace("@[./]@", "a", $pass);
39 $pass = preg_replace("@[I1l]@", "L", $pass);
40 $pass = preg_replace("@[0O]@", "o", $pass);
41 return $pass;
42 }
43
44 /**
45 * Creer un identifiant aleatoire
46 *
47 * http://doc.spip.org/@creer_uniqid
48 *
49 * @return string
50 */
51 function creer_uniqid() {
52 static $seeded;
53
54 if (!$seeded) {
55 $seed = (double) (microtime() + 1) * time();
56 mt_srand($seed);
57 srand($seed);
58 $seeded = true;
59 }
60
61 $s = mt_rand();
62 if (!$s) $s = rand();
63 return uniqid($s, 1);
64 }
65
66 //
67 // Renouvellement de l'alea utilise pour securiser les scripts dans action/
68 //
69
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'");
75 }
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");
81 }
82
83 //
84 // low-security : un ensemble de fonctions pour gerer de l'identification
85 // faible via les URLs (suivi RSS, iCal...)
86 //
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());
93 }
94 }
95 else {
96 $low_sec = sql_getfetsel("low_sec", "spip_auteurs", "id_auteur = $id_auteur");
97 if (!$low_sec) {
98 $low_sec = creer_pass_aleatoire();
99 sql_updateq("spip_auteurs", array("low_sec" => $low_sec), "id_auteur = $id_auteur");
100 }
101 }
102 return $low_sec;
103 }
104
105 // Inclure les arguments significatifs pour le hachage
106 // cas particulier du statut pour compatibilite ancien rss/suivi_revisions
107
108 function param_low_sec($op, $args=array(), $lang='', $mime='rss')
109 {
110 $a = $b = '';
111 foreach ($args as $val => $var)
112 if ($var) {
113 if ($val<>'statut') $a .= ':' . $val.'-'.$var;
114 $b .= $val.'='.$var . '&';
115 }
116 $a = substr($a,1);
117 $id = intval(@$GLOBALS['connect_id_auteur']);
118 return $b
119 . "op="
120 . $op
121 . "&id="
122 . $id
123 . "&cle="
124 . afficher_low_sec($id, "$mime $op $a")
125 . (!$a ? '' : "&args=$a")
126 . (!$lang ? '' : "&lang=$lang");
127 }
128
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);
132 }
133
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));
137 }
138
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");
143 }
144
145 // http://doc.spip.org/@initialiser_sel
146 function initialiser_sel() {
147 global $htsalt;
148 if (CRYPT_MD5) $htsalt = '$1$'.creer_pass_aleatoire();
149 else return "";
150 }
151
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;
159
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");
167 return;
168 }
169
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()
174
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";
184 }
185 }
186 if ($p1) {
187 ecrire_fichier($htpasswd, $p1);
188 ecrire_fichier($htpasswd.'-admin', $p2);
189 spip_log("Ecriture de $htpasswd et $htpasswd-admin");
190 }
191 }
192
193
194 // http://doc.spip.org/@generer_htpass
195 function generer_htpass($pass) {
196 global $htsalt;
197 if (function_exists('crypt'))
198 return crypt($pass, $htsalt);
199 }
200
201 //
202 // Installe ou verifie un .htaccess, y compris sa prise en compte par Apache
203 //
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)
208 return true;
209 if ($ht = @fopen($htaccess, "w")) {
210 fputs($ht, "deny from all\n");
211 fclose($ht);
212 @chmod($htaccess, _SPIP_CHMOD & 0666);
213 $t = rtrim($rep,"/") . "/.ok";
214 if ($ht = @fopen($t, "w")) {
215 @fclose($ht);
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]);
223 }
224 }
225 spip_log("Creation de $htaccess " . ($ht ? " reussie" : " manquee"));
226 return $ht;
227 }
228
229
230
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'])) {
240 if ($f)
241 verifier_htaccess($dir);
242 else spip_unlink($dir . '/' . _ACCESS_FILE_NAME);
243 }
244 }
245 return $GLOBALS['meta']['creer_htaccess'];
246 }
247
248 initialiser_sel();
249
250 ?>