[ Index ] |
|
Code source de SPIP Agora 1.4 |
1 <?php 2 /***************************************************** 3 * This file is part of Agora, web based content management system. 4 * 5 * Agora is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; version 2 of the License. 8 * 9 * Agora is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details (file "COPYING"). 13 * 14 * Copyright © Arnaud Martin, Antoine Pitrou et Philippe Rivière. 15 * List of authors detailed in "copyright_fr.html" file. 16 * E-mail : agora@sig.premier-ministre.gouv.fr 17 * Web site : http://www.agora.gouv.fr 18 *****************************************************/ 19 // Ce fichier ne sera execute qu'une fois 20 if (defined("_INC_CACHE")) 21 return; 22 23 define("_INC_CACHE", "1"); 24 25 // 26 // Calcul du nom du fichier cache 27 // 28 29 function generer_nom_fichier_cache ($fichier_requete) { 30 global $HTTP_POST_VARS; 31 32 $md_cache = md5($fichier_requete); 33 34 $fichier_cache = ereg_replace('^/+', '', $fichier_requete); 35 $fichier_cache = ereg_replace('\.[a-zA-Z0-9]*', '', $fichier_cache); 36 $fichier_cache = ereg_replace('&[^&]+=([^&]+)', '&\1', $fichier_cache); 37 $fichier_cache = rawurlencode(strtr($fichier_cache, '/&-', '--_')); 38 39 if (strlen($fichier_cache) > 24) 40 $fichier_cache = substr(ereg_replace('([a-zA-Z]{1,3})[^-]*-', '\1-', $fichier_cache), -24); 41 42 if (!$fichier_cache) 43 $fichier_cache = 'INDEX-'; 44 45 if (!empty($HTTP_POST_VARS)) 46 $fichier_cache .= '.' .@getmypid(); 47 48 $fichier_cache .= '.' . substr($md_cache, 1, 6); 49 50 $subdir_cache = substr($md_cache, 0, 1); 51 52 if (creer_repertoire('CACHE', $subdir_cache)) 53 $fichier_cache = "$subdir_cache/$fichier_cache"; 54 55 return $fichier_cache; 56 } 57 58 // 59 // Doit-on recalculer le cache ? 60 // 61 62 function utiliser_cache ($chemin_cache, $delais) { 63 global $HTTP_SERVER_VARS, $HTTP_POST_VARS; 64 global $lastmodified; 65 66 // A priori cache 67 $use_cache = true; 68 69 // Existence du fichier 70 if (!file_exists($chemin_cache)) { 71 if (file_exists($chemin_cache . '.NEW')) { 72 // Deuxieme acces : le fichier est marque comme utilise 73 @rename($chemin_cache . '.NEW', $chemin_cache); 74 } 75 76 // Double verification (cas renommage/suppression entre les deux file_exists) 77 clearstatcache(); 78 $use_cache = file_exists($chemin_cache); 79 } 80 81 // Date de creation du fichier 82 if ($use_cache) { 83 $t = filemtime($chemin_cache); 84 $age = time() - $t; 85 $age_ok = (($age < $delais)AND ($age >= 0)); 86 87 if (!$age_ok) { // fichier cache trop vieux 88 if (timeout(false, false)) // sauf lock hebergeur ou probleme base 89 $use_cache = false; 90 } 91 // Inclusions multiples : derniere modification 92 if ($lastmodified < $t) 93 $lastmodified = $t; 94 } 95 96 // recalcul obligatoire 97 $use_cache &= ($GLOBALS['recalcul'] != 'oui'); 98 $use_cache &= empty($HTTP_POST_VARS); 99 100 // ne jamais recalculer pour les moteurs de recherche, proxies... 101 if ($HTTP_SERVER_VARS['REQUEST_METHOD'] == 'HEAD') 102 $use_cache = true; 103 104 spip_debug (($use_cache ? "cache" 105 : "calcul"). " ($chemin_cache)" . ($age ? " age: $age s (reste " . ($delais - $age). " s)" : '')); 106 return $use_cache; 107 } 108 109 function ecrire_fichier_cache ($fichier, $contenu) { 110 require_once (dirname(__FILE__). "/ecrire/include/bd/inc_service_factory.php"); 111 global $flag_flock; 112 113 $fichier_tmp = $fichier . '_tmp'; 114 $fichier_new = $fichier . '.NEW'; 115 116 // Essayer de poser un verrou pour proteger l'ecriture du fichier 117 $serviceMetier = &recuperer_instance_service(); 118 119 if (!$serviceMetier->getLock($fichier_tmp, 1)) 120 return $fichier_new; 121 122 $ok = true; 123 $f = fopen($fichier_tmp, "wb"); 124 125 if (!$f) 126 $ok = false; 127 else { 128 $r = fwrite($f, $contenu); 129 130 if ($r != strlen($contenu)) 131 $ok = false; 132 if (!fclose($f)) 133 $ok = false; 134 } 135 136 // En cas d'erreur d'ecriture, renvoyer le fichier existant 137 if (!$ok) { 138 $serviceMetier = &recuperer_instance_service(); 139 $serviceMetier->releaseLock($fichier_tmp); 140 clearstatcache(); 141 return file_exists($fichier_new) ? $fichier_new : $fichier; 142 } 143 144 // Finaliser 145 @unlink ($fichier_new); 146 rename($fichier_tmp, $fichier_new); 147 @unlink ($fichier); 148 149 $serviceMetier = &recuperer_instance_service(); 150 $serviceMetier->releaseLock($fichier_tmp); 151 152 if ($GLOBALS['flag_apc']) { 153 apc_rm ($fichier_new); 154 apc_rm ($fichier); 155 } 156 157 return $fichier_new; 158 } 159 160 // 161 // Retourne true si le sous-repertoire peut etre cree, false sinon 162 // 163 164 function creer_repertoire ($base, $subdir) { 165 if (file_exists("$base/.plat")) 166 return false; 167 168 $path = $base . '/' . $subdir; 169 170 if (file_exists($path)) 171 return true; 172 173 @mkdir($path, 0777); 174 @chmod($path, 0777); 175 $ok = false; 176 177 if ($f = @fopen("$path/.test", "w")) { 178 @fputs($f, '<' . '?php $ok = true; ?' . '>'); 179 @fclose ($f); 180 include ("$path/.test"); 181 } 182 183 if (!$ok) { 184 $f = @fopen("$base/.plat", "w"); 185 if ($f) 186 fclose ($f); 187 else { 188 @header ("Location: spip_test_dirs.php"); 189 exit; 190 } 191 } 192 193 return $ok; 194 } 195 196 function purger_repertoire ($dir, $age, $regexp = '') { 197 require_once (dirname(__FILE__). "/ecrire/include/bd/inc_forum_factory.php"); 198 $handle = @opendir($dir); 199 200 if (!$handle) 201 return; 202 203 $t = time(); 204 205 while (($fichier = @readdir($handle)) != '') { 206 // Eviter ".", "..", ".htaccess", etc. 207 if ($fichier[0] == '.') 208 continue; 209 210 if ($regexp AND !ereg($regexp, $fichier)) 211 continue; 212 213 $chemin = "$dir/$fichier"; 214 if (is_file($chemin)) { 215 $d = $t - filemtime($chemin); 216 if ($d > $age OR (ereg('\.NEW$', $fichier)AND $d > 60)) { 217 @unlink ($chemin); 218 $fichier = ereg_replace('\.NEW$', '', $fichier); 219 220 $forumMetier = &recuperer_instance_forum(); 221 $deleteCacheForFichierOK = $forumMetier->deleteCacheForFichier($fichier); 222 if (PEAR::isError($deleteCacheForFichierOK)) { 223 die ($deleteCacheForFichierOK->getMessage()); 224 } 225 } 226 } 227 else if (is_dir($chemin)) { 228 if ($fichier != 'CVS') 229 purger_repertoire($chemin, $age); 230 } 231 } 232 233 closedir ($handle); 234 } 235 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 14:40:03 2007 | par Balluche grâce à PHPXref 0.7 |