[SPIP] ~2.1.12 -->2.1.25
[velocampus/web/www.git] / www / ecrire / inc / acces.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2014 *
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
48 // http://doc.spip.org/@creer_uniqid
49 function creer_uniqid() {
50 static $seeded;
51
52 if (!$seeded) {
53 $seed = (double) (microtime() + 1) * time();
54 mt_srand($seed);
55 srand($seed);
56 $seeded = true;
57 }
58
59 $s = mt_rand();
60 if (!$s) $s = rand();
61 return uniqid($s, 1);
62 }
63
64 //
65 // Renouvellement de l'alea utilise pour securiser les scripts dans action/
66 //
67
68 // http://doc.spip.org/@renouvelle_alea
69 function renouvelle_alea() {
70 if (!isset($GLOBALS['meta']['alea_ephemere'])){
71 include_spip('base/abstract_sql');
72 $GLOBALS['meta']['alea_ephemere'] = sql_getfetsel('valeur', 'spip_meta', "nom='alea_ephemere'");
73 }
74 ecrire_meta('alea_ephemere_ancien', @$GLOBALS['meta']['alea_ephemere'], 'non');
75 $GLOBALS['meta']['alea_ephemere'] = md5(creer_uniqid());
76 ecrire_meta('alea_ephemere', $GLOBALS['meta']['alea_ephemere'], 'non');
77 ecrire_meta('alea_ephemere_date', time(), 'non');
78 spip_log("renouvellement de l'alea_ephemere");
79 }
80
81 //
82 // low-security : un ensemble de fonctions pour gerer de l'identification
83 // faible via les URLs (suivi RSS, iCal...)
84 //
85 // http://doc.spip.org/@low_sec
86 function low_sec($id_auteur) {
87 // Pas d'id_auteur : low_sec
88 if (!$id_auteur = intval($id_auteur)) {
89 if (!$low_sec = $GLOBALS['meta']['low_sec']) {
90 ecrire_meta('low_sec', $low_sec = creer_pass_aleatoire());
91 }
92 }
93 else {
94 $low_sec = sql_getfetsel("low_sec", "spip_auteurs", "id_auteur = $id_auteur");
95 if (!$low_sec) {
96 $low_sec = creer_pass_aleatoire();
97 sql_updateq("spip_auteurs", array("low_sec" => $low_sec), "id_auteur = $id_auteur");
98 }
99 }
100 return $low_sec;
101 }
102
103 // Inclure les arguments significatifs pour le hachage
104 // cas particulier du statut pour compatibilite ancien rss/suivi_revisions
105
106 function param_low_sec($op, $args=array(), $lang='', $mime='rss')
107 {
108 $a = $b = '';
109 foreach ($args as $val => $var)
110 if ($var) {
111 if ($val<>'statut') $a .= ':' . $val.'-'.$var;
112 $b .= $val.'='.$var . '&';
113 }
114 $a = substr($a,1);
115 $id = intval(@$GLOBALS['connect_id_auteur']);
116 return $b
117 . "op="
118 . $op
119 . "&id="
120 . $id
121 . "&cle="
122 . afficher_low_sec($id, "$mime $op $a")
123 . (!$a ? '' : "&args=$a")
124 . (!$lang ? '' : "&lang=$lang");
125 }
126
127 // http://doc.spip.org/@afficher_low_sec
128 function afficher_low_sec ($id_auteur, $action='') {
129 return substr(md5($action.low_sec($id_auteur)),0,8);
130 }
131
132 // http://doc.spip.org/@verifier_low_sec
133 function verifier_low_sec ($id_auteur, $cle, $action='') {
134 return ($cle == afficher_low_sec($id_auteur, $action));
135 }
136
137 // http://doc.spip.org/@effacer_low_sec
138 function effacer_low_sec($id_auteur) {
139 if (!$id_auteur = intval($id_auteur)) return; // jamais trop prudent ;)
140 sql_updateq("spip_auteurs", array("low_sec" => ''), "id_auteur = $id_auteur");
141 }
142
143 // http://doc.spip.org/@initialiser_sel
144 function initialiser_sel() {
145 global $htsalt;
146 if (CRYPT_MD5) $htsalt = '$1$'.creer_pass_aleatoire();
147 else return "";
148 }
149
150 // Cette fonction ne sert qu'a la connexion en mode http_auth.non LDAP
151 // Son role est de creer le fichier htpasswd
152 // Voir le plugin "acces restreint"
153 // http://doc.spip.org/@ecrire_acces
154 function ecrire_acces() {
155 $htaccess = _DIR_RESTREINT . _ACCESS_FILE_NAME;
156 $htpasswd = _DIR_TMP . _AUTH_USER_FILE;
157
158 // Cette variable de configuration peut etre posee par un plugin
159 // par exemple acces_restreint ;
160 // si .htaccess existe, outrepasser spip_meta
161 if (($GLOBALS['meta']['creer_htpasswd'] != 'oui')
162 AND !@file_exists($htaccess)) {
163 spip_unlink($htpasswd);
164 spip_unlink($htpasswd."-admin");
165 return;
166 }
167
168 # remarque : ici on laisse passer les "nouveau" de maniere a leur permettre
169 # de devenir redacteur le cas echeant (auth http)... a nettoyer
170 // attention, il faut au prealable se connecter a la base (necessaire car utilise par install)
171
172 if (spip_connect_ldap()) return;
173 $p1 = ''; // login:htpass pour tous
174 $p2 = ''; // login:htpass pour les admins
175 $s = sql_select("login, htpass, statut", "spip_auteurs", sql_in("statut", array('1comite','0minirezo','nouveau')));
176 while ($t = sql_fetch($s)) {
177 if (strlen($t['login']) AND strlen($t['htpass'])) {
178 $p1 .= $t['login'].':'.$t['htpass']."\n";
179 if ($t['statut'] == '0minirezo')
180 $p2 .= $t['login'].':'.$t['htpass']."\n";
181 }
182 }
183 if ($p1) {
184 ecrire_fichier($htpasswd, $p1);
185 ecrire_fichier($htpasswd.'-admin', $p2);
186 spip_log("Ecriture de $htpasswd et $htpasswd-admin");
187 }
188 }
189
190
191 // http://doc.spip.org/@generer_htpass
192 function generer_htpass($pass) {
193 global $htsalt;
194 if (function_exists('crypt'))
195 return crypt($pass, $htsalt);
196 }
197
198 //
199 // Installe ou verifie un .htaccess, y compris sa prise en compte par Apache
200 //
201 // http://doc.spip.org/@verifier_htaccess
202 function verifier_htaccess($rep, $force=false) {
203 $htaccess = rtrim($rep,"/") . "/" . _ACCESS_FILE_NAME;
204 if (((@file_exists($htaccess)) OR defined('_TEST_DIRS')) AND !$force)
205 return true;
206 if ($_SERVER['SERVER_ADMIN'] == 'www@nexenservices.com')
207 return nexen($rep);
208 if ($ht = @fopen($htaccess, "w")) {
209 fputs($ht, "deny from all\n");
210 fclose($ht);
211 @chmod($htaccess, _SPIP_CHMOD & 0666);
212 $t = rtrim($rep,"/") . "/.ok";
213 if ($ht = @fopen($t, "w")) {
214 @fclose($ht);
215 include_spip('inc/distant');
216 $t = substr($t,strlen(_DIR_RACINE));
217 $t = url_de_base() . $t;
218 $ht = recuperer_lapage($t, false, 'HEAD', 0);
219 // htaccess inoperant si on a recupere des entetes HTTP
220 // (ignorer la reussite si connexion par fopen)
221 $ht = !(isset($ht[0]) AND $ht[0]);
222 }
223 }
224 spip_log("Creation de $htaccess " . ($ht ? " reussie" : " manquee"));
225 return $ht;
226 }
227
228 function nexen($rep)
229 {
230 echo "<span style='color: #FF0000'>IMPORTANT : </span>";
231 echo "Votre h&eacute;bergeur est Nexen Services.<br />";
232 echo "La protection du r&eacute;pertoire <i>$rep/</i> doit se faire
233 par l'interm&eacute;diaire de ";
234 echo "<a href=\"http://www.nexenservices.com/Webmestres/index.php\"
235 target=\"_blank\">l'espace webmestres</a>.";
236 echo "Veuillez cr&eacute;er manuellement la protection pour
237 ce r&eacute;pertoire (un couple login/mot de passe est
238 n&eacute;cessaire).<br />";
239 return false;
240 }
241
242
243 // http://doc.spip.org/@gerer_htaccess
244 function gerer_htaccess() {
245 // Cette variable de configuration peut etre posee par un plugin
246 // par exemple acces_restreint
247 $f = ($GLOBALS['meta']['creer_htaccess'] === 'oui');
248 $dirs = sql_allfetsel('extension', 'spip_types_documents');
249 $dirs[] = array('extension' => 'distant');
250 foreach($dirs as $e) {
251 if (is_dir($dir = _DIR_IMG . $e['extension'])) {
252 if ($f)
253 verifier_htaccess($dir);
254 else spip_unlink($dir . '/' . _ACCESS_FILE_NAME);
255 }
256 }
257 return $GLOBALS['meta']['creer_htaccess'];
258 }
259
260 initialiser_sel();
261
262 ?>