Subversion Repositories Applications.framework

Rev

Rev 253 | Rev 282 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
246 jpm 1
<?php
2
class CacheFichier {
3
	/**
4
	 * Available options
5
	 *
6
	 * =====> (string) cache_dir :
7
	 * - Directory where to put the cache files
8
	 *
9
	 * =====> (boolean) file_locking :
10
	 * - Enable / disable file_locking
11
	 * - Can avoid cache corruption under bad circumstances but it doesn't work on multithread
12
	 * webservers and on NFS filesystems for example
13
	 *
14
	 * =====> (boolean) read_control :
15
	 * - Enable / disable read control
16
	 * - If enabled, a control key is embeded in cache file and this key is compared with the one
17
	 * calculated after the reading.
18
	 *
19
	 * =====> (string) read_control_type :
20
	 * - Type of read control (only if read control is enabled). Available values are :
21
	 *   'md5' for a md5 hash control (best but slowest)
22
	 *   'crc32' for a crc32 hash control (lightly less safe but faster, better choice)
23
	 *   'adler32' for an adler32 hash control (excellent choice too, faster than crc32)
24
	 *   'strlen' for a length only test (fastest)
25
	 *
26
	 * =====> (int) hashed_directory_level :
27
	 * - Hashed directory level
28
	 * - Set the hashed directory structure level. 0 means "no hashed directory
29
	 * structure", 1 means "one level of directory", 2 means "two levels"...
30
	 * This option can speed up the cache only when you have many thousands of
31
	 * cache file. Only specific benchs can help you to choose the perfect value
32
	 * for you. Maybe, 1 or 2 is a good start.
33
	 *
34
	 * =====> (int) hashed_directory_umask :
35
	 * - Umask for hashed directory structure
36
	 *
37
	 * =====> (string) file_name_prefix :
38
	 * - prefix for cache files
39
	 * - be really carefull with this option because a too generic value in a system cache dir
40
	 *   (like /tmp) can cause disasters when cleaning the cache
41
	 *
42
	 * =====> (int) cache_file_umask :
43
	 * - Umask for cache files
44
	 *
45
	 * =====> (int) metatadatas_array_max_size :
46
	 * - max size for the metadatas array (don't change this value unless you
47
	 *   know what you are doing)
48
	 *
49
	 * @var array available options
50
	 */
51
	protected $options = array(
52
		'stockage_chemin' => null,
248 jpm 53
		'fichier_verrou' => true,
54
		'controle_lecture' => true,
246 jpm 55
		'controle_lecture_type' => 'crc32',
56
		'dossier_niveau' => 0,
57
		'dossier_umask' => 0700,
58
		'fichier_prefixe' => 'tbf',
59
		'fichier_umask' => 0600,
60
		'metadonnees_max_taille' => 100
61
	);
62
 
63
	/**
64
	 * Array of metadatas (each item is an associative array)
65
	 *
66
	 * @var array
67
	 */
248 jpm 68
	protected $metadonnees = array();
246 jpm 69
 
269 jpm 70
	private $Cache = null;
71
 
246 jpm 72
	/**
73
	 * Constructor
74
	 *
75
	 * @param  array $options associative array of options
76
	 * @throws Zend_Cache_Exception
77
	 * @return void
78
	 */
269 jpm 79
	public function __construct(array $options = array(), Cache $cache) {
80
		$this->Cache = $cache;
248 jpm 81
		$this->setOptions($options);
82
 
246 jpm 83
		if (isset($this->options['prefixe_fichier'])) {
84
			if (!preg_match('~^[a-zA-Z0-9_]+$~D', $this->options['prefixe_fichier'])) {
85
				trigger_error("Préfixe de nom de fichier invalide : doit contenir seulement [a-zA-Z0-9_]", E_USER_WARNING);
86
			}
87
		}
248 jpm 88
		if ($this->options['metadonnees_max_taille'] < 10) {
246 jpm 89
			trigger_error("Taille du tableau des méta-données invalide, elle doit être > 10", E_USER_WARNING);
90
		}
91
		if (isset($options['dossier_umask']) && is_string($options['dossier_umask'])) {
92
			// See #ZF-4422
93
			$this->options['dossier_umask'] = octdec($this->options['dossier_umask']);
94
		}
95
		if (isset($options['fichier_umask']) && is_string($options['fichier_umask'])) {
96
			// See #ZF-4422
97
			$this->options['fichier_umask'] = octdec($this->options['fichier_umask']);
98
		}
99
	}
248 jpm 100
 
101
	private function setOptions($options) {
102
		while (list($nom, $valeur) = each($options)) {
103
			if (!is_string($nom)) {
104
				trigger_error("Nom d'option incorecte : $nom", E_USER_WARNING);
105
			}
106
			$nom = strtolower($nom);
107
			if (array_key_exists($nom, $this->options)) {
108
				$this->options[$nom] = $valeur;
109
			}
110
		}
111
	}
246 jpm 112
 
248 jpm 113
   	public function setEmplacement($emplacement) {
246 jpm 114
		if (!is_dir($emplacement)) {
115
			trigger_error("L'emplacement doit être un dossier.", E_USER_WARNING);
116
		}
117
		if (!is_writable($emplacement)) {
118
			trigger_error("Le dossier de stockage du cache n'est pas accessible en écriture", E_USER_WARNING);
119
		}
120
		$emplacement = rtrim(realpath($emplacement), '\\/').DS;
121
		$this->options['stockage_chemin'] = $emplacement;
122
	}
123
 
124
	/**
125
	 * Test if a cache is available for the given id and (if yes) return it (false else)
126
	 *
127
	 * @param string $id cache id
128
	 * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
129
	 * @return string|false cached datas
130
	 */
131
	public function charger($id, $ne_pas_tester_validiter_du_cache = false) {
132
		$donnees = false;
133
		if ($this->tester($id, $ne_pas_tester_validiter_du_cache)) {
134
			$metadonnees = $this->getMetadonneesFichier($id);
135
			$fichier = $this->getFichierNom($id);
136
			$donnees = $this->getContenuFichier($fichier);
137
			if ($this->options['controle_lecture']) {
138
				$cle_secu_donnees = $this->genererCleSecu($donnees, $this->options['controle_lecture_type']);
139
				$cle_secu_controle = $metadonnees['hash'];
140
				if ($cle_secu_donnees != $cle_secu_controle) {
141
					// Probléme détecté par le contrôle de lecture !
142
					// TODO : loguer le pb de sécu
143
					$this->supprimer($id);
144
					$donnees = false;
145
				}
146
			}
147
		}
148
		return $donnees;
149
	}
150
 
151
	/**
152
	 * Teste si un enregistrement en cache est disponible ou pas (pour l'id passé en paramètre).
153
	 *
154
	 * @param string $id identifiant de cache.
155
	 * @return mixed false (le cache n'est pas disponible) ou timestamp (int) "de dernière modification" de l'enregistrement en cache
156
	 */
157
	public function tester($id) {
158
		clearstatcache();
159
		return $this->testerExistenceCache($id, false);
160
	}
161
 
162
	/**
163
	 * Save some string datas into a cache record
164
	 *
165
	 * Note : $data is always "string" (serialization is done by the
166
	 * core not by the backend)
167
	 *
168
	 * @param  string $data			 Datas to cache
169
	 * @param  string $id			   Cache id
170
	 * @param  array  $tags			 Array of strings, the cache record will be tagged by each string entry
171
	 * @param  int	$specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
172
	 * @return boolean true if no problem
173
	 */
248 jpm 174
	public function sauver($donnees, $id, $tags = array(), $duree_vie_specifique = false) {
246 jpm 175
		clearstatcache();
248 jpm 176
		$fichier = $this->getFichierNom($id);
177
		$chemin = $this->getChemin($id);
178
 
179
		$resultat = true;
180
		if ($this->options['dossier_niveau'] > 0) {
181
			if (!is_writable($chemin)) {
246 jpm 182
				// maybe, we just have to build the directory structure
183
				$this->lancerMkdirEtChmodRecursif($id);
184
			}
248 jpm 185
			if (!is_writable($chemin)) {
186
				$resultat = false;
246 jpm 187
			}
188
		}
248 jpm 189
 
190
		if ($resultat === true) {
191
			if ($this->options['controle_lecture']) {
192
				$cle_secu = $this->genererCleSecu($donnees, $this->options['controle_lecture_type']);
193
			} else {
194
				$cle_secu = '';
195
			}
196
 
197
			$metadonnees = array(
198
				'hash' => $cle_secu,
199
				'mtime' => time(),
269 jpm 200
				'expiration' => $this->Cache->getTimestampExpiration($duree_vie_specifique),
248 jpm 201
				'tags' => $tags
202
			);
203
 
204
			if (! $resultat = $this->setMetadonnees($id, $metadonnees)) {
205
				// TODO : ajouter un log
206
			} else {
207
				$resultat = $this->setContenuFichier($fichier, $donnees);
208
			}
246 jpm 209
		}
248 jpm 210
		return $resultat;
246 jpm 211
	}
212
 
213
	/**
214
	 * Remove a cache record
215
	 *
216
	 * @param  string $id cache id
217
	 * @return boolean true if no problem
218
	 */
219
	public function supprimer($id) {
220
		$fichier = $this->getFichierNom($id);
221
		$suppression_fichier = $this->supprimerFichier($fichier);
222
		$suppression_metadonnees = $this->supprimerMetadonnees($id);
223
		return $suppression_metadonnees && $suppression_fichier;
224
	}
225
 
226
	/**
227
	 * Clean some cache records
228
	 *
229
	 * Available modes are :
230
	 * 'all' (default)  => remove all cache entries ($tags is not used)
231
	 * 'old'			=> remove too old cache entries ($tags is not used)
232
	 * 'matchingTag'	=> remove cache entries matching all given tags
233
	 *					 ($tags can be an array of strings or a single string)
234
	 * 'notMatchingTag' => remove cache entries not matching one of the given tags
235
	 *					 ($tags can be an array of strings or a single string)
236
	 * 'matchingAnyTag' => remove cache entries matching any given tags
237
	 *					 ($tags can be an array of strings or a single string)
238
	 *
239
	 * @param string $mode clean mode
240
	 * @param tags array $tags array of tags
241
	 * @return boolean true if no problem
242
	 */
243
	public function nettoyer($mode = Cache::NETTOYAGE_MODE_TOUS, $tags = array()) {
244
		// We use this protected method to hide the recursive stuff
245
		clearstatcache();
248 jpm 246
		return $this->nettoyerFichiers($this->options['stockage_chemin'], $mode, $tags);
246 jpm 247
	}
248
 
249
	/**
250
	 * Return an array of stored cache ids
251
	 *
252
	 * @return array array of stored cache ids (string)
253
	 */
248 jpm 254
	public function getIds() {
255
		return $this->analyserCache($this->options['stockage_chemin'], 'ids', array());
246 jpm 256
	}
257
 
258
	/**
259
	 * Return an array of stored tags
260
	 *
261
	 * @return array array of stored tags (string)
262
	 */
248 jpm 263
	public function getTags() {
264
		return $this->analyserCache($this->options['stockage_chemin'], 'tags', array());
246 jpm 265
	}
266
 
267
	/**
268
	 * Return an array of stored cache ids which match given tags
269
	 *
270
	 * In case of multiple tags, a logical AND is made between tags
271
	 *
272
	 * @param array $tags array of tags
273
	 * @return array array of matching cache ids (string)
274
	 */
248 jpm 275
	public function getIdsAvecLesTags($tags = array()) {
276
		return $this->analyserCache($this->options['stockage_chemin'], 'matching', $tags);
246 jpm 277
	}
278
 
279
	/**
280
	 * Return an array of stored cache ids which don't match given tags
281
	 *
282
	 * In case of multiple tags, a logical OR is made between tags
283
	 *
284
	 * @param array $tags array of tags
285
	 * @return array array of not matching cache ids (string)
286
	 */
248 jpm 287
	public function getIdsSansLesTags($tags = array()) {
288
		return $this->analyserCache($this->options['stockage_chemin'], 'notMatching', $tags);
246 jpm 289
	}
290
 
291
	/**
292
	 * Return an array of stored cache ids which match any given tags
293
	 *
294
	 * In case of multiple tags, a logical AND is made between tags
295
	 *
296
	 * @param array $tags array of tags
297
	 * @return array array of any matching cache ids (string)
298
	 */
248 jpm 299
	public function getIdsAvecUnTag($tags = array()) {
300
		return $this->analyserCache($this->options['stockage_chemin'], 'matchingAny', $tags);
246 jpm 301
	}
302
 
303
	/**
304
	 * Return the filling percentage of the backend storage
305
	 *
306
	 * @throws Zend_Cache_Exception
307
	 * @return int integer between 0 and 100
308
	 */
248 jpm 309
	public function getPourcentageRemplissage() {
250 jpm 310
		$libre = disk_free_space($this->options['stockage_chemin']);
311
		$total = disk_total_space($this->options['stockage_chemin']);
248 jpm 312
 
313
		$pourcentage = 0;
246 jpm 314
		if ($total == 0) {
248 jpm 315
			trigger_error("Impossible d'utiliser la fonction disk_total_space", E_USER_WARNING);
246 jpm 316
		} else {
248 jpm 317
			$pourcentage = ($libre >= $total) ? 100 : ((int) (100. * ($total - $libre) / $total));
246 jpm 318
		}
248 jpm 319
		return $pourcentage;
246 jpm 320
	}
321
 
322
	/**
323
	 * Return an array of metadatas for the given cache id
324
	 *
325
	 * The array must include these keys :
326
	 * - expire : the expire timestamp
327
	 * - tags : a string array of tags
328
	 * - mtime : timestamp of last modification time
329
	 *
330
	 * @param string $id cache id
331
	 * @return array array of metadatas (false if the cache id is not found)
332
	 */
248 jpm 333
	public function getMetadonnees($id) {
334
		if ($metadonnees = $this->getMetadonneesFichier($id)) {
335
			if (time() > $metadonnees['expiration']) {
336
				$metadonnees = false;
337
			} else {
338
				$metadonnees = array(
339
					'expiration' => $metadonnees['expiration'],
340
					'tags' => $metadonnees['tags'],
341
					'mtime' => $metadonnees['mtime']
342
				);
343
			}
246 jpm 344
		}
248 jpm 345
 
346
		return $metadonnees;
246 jpm 347
	}
348
 
349
	/**
350
	 * Give (if possible) an extra lifetime to the given cache id
351
	 *
352
	 * @param string $id cache id
353
	 * @param int $extraLifetime
354
	 * @return boolean true if ok
355
	 */
248 jpm 356
	public function ajouterSupplementDureeDeVie($id, $supplement_duree_de_vie) {
357
		$augmentation = true;
358
		if ($metadonnees = $this->getMetadonneesFichier($id)) {
359
			if (time() > $metadonnees['expiration']) {
360
				$augmentation = false;
361
			} else {
362
				$metadonnees_nouvelle = array(
363
					'hash' => $metadonnees['hash'],
364
					'mtime' => time(),
365
					'expiration' => $metadonnees['expiration'] + $supplement_duree_de_vie,
366
					'tags' => $metadonnees['tags']
367
				);
368
				$augmentation = $this->setMetadonnees($id, $metadonnees_nouvelle);
369
			}
246 jpm 370
		}
248 jpm 371
		return $augmentation;
246 jpm 372
	}
373
 
374
	/**
375
	 * Get a metadatas record
376
	 *
377
	 * @param  string $id  Cache id
378
	 * @return array|false Associative array of metadatas
379
	 */
380
	protected function getMetadonneesFichier($id) {
381
		$metadonnees = false;
382
		if (isset($this->metadonnees[$id])) {
383
			$metadonnees = $this->metadonnees[$id];
384
		} else {
385
			if ($metadonnees = $this->chargerMetadonnees($id)) {
386
				$this->setMetadonnees($id, $metadonnees, false);
387
			}
388
		}
389
		return $metadonnees;
390
	}
391
 
392
	/**
393
	 * Set a metadatas record
394
	 *
395
	 * @param  string $id		Cache id
396
	 * @param  array  $metadatas Associative array of metadatas
397
	 * @param  boolean $save	 optional pass false to disable saving to file
398
	 * @return boolean True if no problem
399
	 */
400
	protected function setMetadonnees($id, $metadonnees, $sauvegarde = true) {
401
		if (count($this->metadonnees) >= $this->options['metadonnees_max_taille']) {
402
			$n = (int) ($this->options['metadonnees_max_taille'] / 10);
403
			$this->metadonnees = array_slice($this->metadonnees, $n);
404
		}
405
 
406
		$resultat = true;
407
		if ($sauvegarde) {
408
			$resultat = $this->sauverMetadonnees($id, $metadonnees);
409
		}
410
		if ($resultat == true) {
411
			$this->metadonnees[$id] = $metadonnees;
412
		}
413
		return $resultat;
414
	}
415
 
416
	/**
417
	 * Drop a metadata record
418
	 *
419
	 * @param  string $id Cache id
420
	 * @return boolean True if no problem
421
	 */
422
	protected function supprimerMetadonnees($id) {
423
		if (isset($this->metadonnees[$id])) {
424
			unset($this->metadonnees[$id]);
425
		}
426
		$fichier_meta = $this->getNomFichierMeta($id);
427
		return $this->supprimerFichier($fichier_meta);
428
	}
429
 
430
	/**
431
	 * Clear the metadatas array
432
	 *
433
	 * @return void
434
	 */
435
	protected function nettoyerMetadonnees() {
436
		$this->metadonnees = array();
437
	}
438
 
439
	/**
440
	 * Load metadatas from disk
441
	 *
442
	 * @param  string $id Cache id
443
	 * @return array|false Metadatas associative array
444
	 */
445
	protected function chargerMetadonnees($id) {
446
		$fichier = $this->getNomFichierMeta($id);
447
		if ($resultat = $this->getContenuFichier($fichier)) {
448
			$resultat = @unserialize($resultat);
449
		}
450
		return $resultat;
451
	}
452
 
453
	/**
454
	 * Save metadatas to disk
455
	 *
456
	 * @param  string $id		Cache id
457
	 * @param  array  $metadatas Associative array
458
	 * @return boolean True if no problem
459
	 */
460
	protected function sauverMetadonnees($id, $metadonnees) {
461
		$fichier = $this->getNomFichierMeta($id);
462
		$resultat = $this->setContenuFichier($fichier, serialize($metadonnees));
463
		return $resultat;
464
	}
465
 
466
	/**
467
	 * Make and return a file name (with path) for metadatas
468
	 *
469
	 * @param  string $id Cache id
470
	 * @return string Metadatas file name (with path)
471
	 */
472
	protected function getNomFichierMeta($id) {
473
		$chemin = $this->getChemin($id);
253 jpm 474
		$fichier_nom = $this->transformaterIdEnNomFichier('interne-meta---'.$id);
246 jpm 475
		return $chemin.$fichier_nom;
476
	}
477
 
478
	/**
479
	 * Check if the given filename is a metadatas one
480
	 *
481
	 * @param  string $fileName File name
482
	 * @return boolean True if it's a metadatas one
483
	 */
484
	protected function etreFichierMeta($fichier_nom) {
485
		$id = $this->transformerNomFichierEnId($fichier_nom);
253 jpm 486
		return (substr($id, 0, 21) == 'interne-meta---') ? true : false;
246 jpm 487
	}
488
 
489
	/**
490
	 * Remove a file
491
	 *
492
	 * If we can't remove the file (because of locks or any problem), we will touch
493
	 * the file to invalidate it
494
	 *
495
	 * @param  string $file Complete file path
496
	 * @return boolean True if ok
497
	 */
498
	protected function supprimerFichier($fichier) {
499
		$resultat = false;
500
		if (is_file($fichier)) {
501
			if ($resultat = @unlink($fichier)) {
502
				// TODO : ajouter un log
503
			}
504
		}
505
		return $resultat;
506
	}
507
 
508
	/**
509
	 * Clean some cache records (protected method used for recursive stuff)
510
	 *
511
	 * Available modes are :
512
	 * Zend_Cache::CLEANING_MODE_ALL (default)	=> remove all cache entries ($tags is not used)
513
	 * Zend_Cache::CLEANING_MODE_OLD			  => remove too old cache entries ($tags is not used)
514
	 * Zend_Cache::CLEANING_MODE_MATCHING_TAG	 => remove cache entries matching all given tags
515
	 *											   ($tags can be an array of strings or a single string)
516
	 * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
517
	 *											   ($tags can be an array of strings or a single string)
518
	 * Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
519
	 *											   ($tags can be an array of strings or a single string)
520
	 *
521
	 * @param  string $dir  Directory to clean
522
	 * @param  string $mode Clean mode
523
	 * @param  array  $tags Array of tags
524
	 * @throws Zend_Cache_Exception
525
	 * @return boolean True if no problem
526
	 */
527
	protected function nettoyerFichiers($dossier, $mode = Cache::NETTOYAGE_MODE_TOUS, $tags = array()) {
528
		if (!is_dir($dossier)) {
529
			return false;
530
		}
531
		$resultat = true;
532
		$prefixe = $this->options['fichier_prefixe'];
533
		$glob = @glob($dossier.$prefixe.'--*');
534
		if ($glob === false) {
535
			// On some systems it is impossible to distinguish between empty match and an error.
536
			return true;
537
		}
538
		foreach ($glob as $fichier)  {
539
			if (is_file($fichier)) {
540
				$fichier_nom = basename($fichier);
541
				if ($this->etreFichierMeta($fichier_nom)) {
248 jpm 542
					// Pour le mode Cache::NETTOYAGE_MODE_TOUS, nous essayons de tous supprimer même les vieux fichiers méta
246 jpm 543
					if ($mode != Cache::NETTOYAGE_MODE_TOUS) {
544
						continue;
545
					}
546
				}
547
				$id = $this->transformerNomFichierEnId($fichier_nom);
248 jpm 548
				$metadonnees = $this->getMetadonneesFichier($id);
549
				if ($metadonnees === FALSE) {
550
					$metadonnees = array('expiration' => 1, 'tags' => array());
246 jpm 551
				}
552
				switch ($mode) {
248 jpm 553
					case Cache::NETTOYAGE_MODE_TOUS :
554
						if ($resultat_suppression = $this->supprimer($id)) {
555
							// Dans ce cas seulement, nous acception qu'il y ait un problème avec la suppresssion du fichier meta
556
							$resultat_suppression = $this->supprimerFichier($fichier);
246 jpm 557
						}
248 jpm 558
						$resultat = $resultat && $resultat_suppression;
246 jpm 559
						break;
248 jpm 560
					case Cache::NETTOYAGE_MODE_EXPIRATION :
561
						if (time() > $metadonnees['expiration']) {
562
							$resultat = $this->supprimer($id) && $resultat;
246 jpm 563
						}
564
						break;
248 jpm 565
					case Cache::NETTOYAGE_MODE_AVEC_LES_TAGS :
566
						$correspondance = true;
246 jpm 567
						foreach ($tags as $tag) {
248 jpm 568
							if (!in_array($tag, $metadonnees['tags'])) {
569
								$correspondance = false;
246 jpm 570
								break;
571
							}
572
						}
248 jpm 573
						if ($correspondance) {
574
							$resultat = $this->supprimer($id) && $resultat;
246 jpm 575
						}
576
						break;
248 jpm 577
					case Cache::NETTOYAGE_MODE_SANS_LES_TAGS :
578
						$correspondance = false;
246 jpm 579
						foreach ($tags as $tag) {
248 jpm 580
							if (in_array($tag, $metadonnees['tags'])) {
581
								$correspondance = true;
246 jpm 582
								break;
583
							}
584
						}
248 jpm 585
						if (!$correspondance) {
586
							$resultat = $this->supprimer($id) && $resultat;
246 jpm 587
						}
588
						break;
248 jpm 589
					case Cache::NETTOYAGE_MODE_AVEC_UN_TAG :
590
						$correspondance = false;
246 jpm 591
						foreach ($tags as $tag) {
248 jpm 592
							if (in_array($tag, $metadonnees['tags'])) {
593
								$correspondance = true;
246 jpm 594
								break;
595
							}
596
						}
248 jpm 597
						if ($correspondance) {
598
							$resultat = $this->supprimer($id) && $resultat;
246 jpm 599
						}
600
						break;
601
					default:
248 jpm 602
						trigger_error("Mode de nettoyage invalide pour la méthode nettoyer()", E_USER_WARNING);
246 jpm 603
						break;
604
				}
605
			}
248 jpm 606
			if ((is_dir($fichier)) and ($this->options['dossier_niveau'] > 0)) {
607
				// Appel récursif
608
				$resultat = $this->nettoyerFichiers($fichier.DS, $mode, $tags) && $resultat;
609
				if ($mode == Cache::NETTOYAGE_MODE_TOUS) {
610
					// Si mode == Cache::NETTOYAGE_MODE_TOUS, nous essayons de supprimer la structure aussi
611
					@rmdir($fichier);
246 jpm 612
				}
613
			}
614
		}
615
		return $resultat;
616
	}
617
 
248 jpm 618
	protected function analyserCache($dossier, $mode, $tags = array()) {
619
		if (!is_dir($dossier)) {
246 jpm 620
			return false;
621
		}
248 jpm 622
		$resultat = array();
623
		$prefixe = $this->options['fichier_prefixe'];
624
		$glob = @glob($dossier.$prefixe.'--*');
246 jpm 625
		if ($glob === false) {
626
			// On some systems it is impossible to distinguish between empty match and an error.
627
			return array();
628
		}
248 jpm 629
		foreach ($glob as $fichier)  {
630
			if (is_file($fichier)) {
631
				$nom_fichier = basename($fichier);
632
				$id = $this->transformerNomFichierEnId($nom_fichier);
633
				$metadonnees = $this->getMetadonneesFichier($id);
634
				if ($metadonnees === FALSE) {
246 jpm 635
					continue;
636
				}
248 jpm 637
				if (time() > $metadonnees['expiration']) {
246 jpm 638
					continue;
639
				}
640
				switch ($mode) {
641
					case 'ids':
248 jpm 642
						$resultat[] = $id;
246 jpm 643
						break;
644
					case 'tags':
248 jpm 645
						$resultat = array_unique(array_merge($resultat, $metadonnees['tags']));
246 jpm 646
						break;
647
					case 'matching':
248 jpm 648
						$correspondance = true;
246 jpm 649
						foreach ($tags as $tag) {
248 jpm 650
							if (!in_array($tag, $metadonnees['tags'])) {
651
								$correspondance = false;
246 jpm 652
								break;
653
							}
654
						}
248 jpm 655
						if ($correspondance) {
656
							$resultat[] = $id;
246 jpm 657
						}
658
						break;
659
					case 'notMatching':
248 jpm 660
						$correspondance = false;
246 jpm 661
						foreach ($tags as $tag) {
248 jpm 662
							if (in_array($tag, $metadonnees['tags'])) {
663
								$correspondance = true;
246 jpm 664
								break;
665
							}
666
						}
248 jpm 667
						if (!$correspondance) {
668
							$resultat[] = $id;
246 jpm 669
						}
670
						break;
671
					case 'matchingAny':
248 jpm 672
						$correspondance = false;
246 jpm 673
						foreach ($tags as $tag) {
248 jpm 674
							if (in_array($tag, $metadonnees['tags'])) {
675
								$correspondance = true;
246 jpm 676
								break;
677
							}
678
						}
248 jpm 679
						if ($correspondance) {
680
							$resultat[] = $id;
246 jpm 681
						}
682
						break;
683
					default:
248 jpm 684
						trigger_error("Mode invalide pour la méthode analyserCache()", E_USER_WARNING);
246 jpm 685
						break;
686
				}
687
			}
248 jpm 688
			if ((is_dir($fichier)) and ($this->options['dossier_niveau'] > 0)) {
689
				// Appel récursif
690
				$resultat_analyse_recursive = $this->analyserCache($fichier.DS, $mode, $tags);
691
				if ($resultat_analyse_recursive === false) {
692
					// TODO : ajoute un log
246 jpm 693
				} else {
248 jpm 694
					$resultat = array_unique(array_merge($resultat, $resultat_analyse_recursive));
246 jpm 695
				}
696
			}
697
		}
248 jpm 698
		return array_unique($resultat);
246 jpm 699
	}
700
 
701
	/**
702
	 * Make a control key with the string containing datas
703
	 *
704
	 * @param  string $data		Data
705
	 * @param  string $controlType Type of control 'md5', 'crc32' or 'strlen'
706
	 * @throws Zend_Cache_Exception
707
	 * @return string Control key
708
	 */
709
	protected function genererCleSecu($donnees, $type_de_controle) {
710
		switch ($type_de_controle) {
711
		case 'md5':
712
			return md5($donnees);
713
		case 'crc32':
714
			return crc32($donnees);
715
		case 'strlen':
716
			return strlen($donnees);
717
		case 'adler32':
718
			return hash('adler32', $donnees);
719
		default:
720
			trigger_error("Fonction de génération de clé de sécurité introuvable : $type_de_controle", E_USER_WARNING);
721
		}
722
	}
723
 
724
	/**
725
	 * Transform a cache id into a file name and return it
726
	 *
727
	 * @param  string $id Cache id
728
	 * @return string File name
729
	 */
730
	protected function transformaterIdEnNomFichier($id) {
731
		$prefixe = $this->options['fichier_prefixe'];
248 jpm 732
		$resultat = $prefixe.'---'.$id;
246 jpm 733
		return $resultat;
734
	}
735
 
736
	/**
737
	 * Make and return a file name (with path)
738
	 *
739
	 * @param  string $id Cache id
740
	 * @return string File name (with path)
741
	 */
742
	protected function getFichierNom($id) {
743
		$path = $this->getChemin($id);
744
		$fileName = $this->transformaterIdEnNomFichier($id);
745
		return $path . $fileName;
746
	}
747
 
748
	/**
749
	 * Return the complete directory path of a filename (including hashedDirectoryStructure)
750
	 *
751
	 * @param  string $id Cache id
752
	 * @param  boolean $decoupage if true, returns array of directory parts instead of single string
753
	 * @return string Complete directory path
754
	 */
755
	protected function getChemin($id, $decoupage = false) {
756
		$morceaux = array();
757
		$chemin = $this->options['stockage_chemin'];
758
		$prefixe = $this->options['fichier_prefixe'];
759
		if ($this->options['dossier_niveau'] > 0) {
760
			$hash = hash('adler32', $id);
761
			for ($i = 0 ; $i < $this->options['dossier_niveau'] ; $i++) {
248 jpm 762
				$chemin .= $prefixe.'--'.substr($hash, 0, $i + 1).DS;
246 jpm 763
				$morceaux[] = $chemin;
764
			}
765
		}
766
		return ($decoupage) ? 	$morceaux : $chemin;
767
	}
768
 
769
	/**
770
	 * Make the directory strucuture for the given id
771
	 *
772
	 * @param string $id cache id
773
	 * @return boolean true
774
	 */
775
	protected function lancerMkdirEtChmodRecursif($id) {
776
		$resultat = true;
777
		if ($this->options['dossier_niveau'] > 0) {
778
			$chemins = $this->getChemin($id, true);
779
			foreach ($chemins as $chemin) {
780
				if (!is_dir($chemin)) {
781
					@mkdir($chemin, $this->options['dossier_umask']);
782
					@chmod($chemin, $this->options['dossier_umask']); // see #ZF-320 (this line is required in some configurations)
783
				}
784
			}
785
		}
786
		return $resultat;
787
	}
788
 
789
	/**
790
	 * Test if the given cache id is available (and still valid as a cache record)
791
	 *
792
	 * @param  string  $id					 Cache id
793
	 * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
794
	 * @return boolean|mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
795
	 */
796
	protected function testerExistenceCache($id, $ne_pas_tester_validiter_du_cache) {
797
		$resultat = false;
798
		if ($metadonnees = $this->getMetadonnees($id)) {
799
			if ($ne_pas_tester_validiter_du_cache || (time() <= $metadonnees['expiration'])) {
800
				$resultat =  $metadonnees['mtime'];
801
			}
802
		}
803
		return $resultat;
804
	}
805
 
806
	/**
807
	 * Return the file content of the given file
808
	 *
809
	 * @param  string $file File complete path
810
	 * @return string File content (or false if problem)
811
	 */
812
	protected function getContenuFichier($fichier) {
813
		$resultat = false;
814
		if (is_file($fichier)) {
815
			$f = @fopen($fichier, 'rb');
816
			if ($f) {
817
				if ($this->options['fichier_verrou']) @flock($f, LOCK_SH);
818
				$resultat = stream_get_contents($f);
819
				if ($this->options['fichier_verrou']) @flock($f, LOCK_UN);
820
				@fclose($f);
821
			}
822
		}
823
		return $resultat;
824
	}
825
 
826
	/**
827
	 * Put the given string into the given file
828
	 *
829
	 * @param  string $file   File complete path
830
	 * @param  string $string String to put in file
831
	 * @return boolean true if no problem
832
	 */
833
	protected function setContenuFichier($fichier, $chaine) {
834
		$resultat = false;
835
		$f = @fopen($fichier, 'ab+');
836
		if ($f) {
837
			if ($this->options['fichier_verrou']) @flock($f, LOCK_EX);
838
			fseek($f, 0);
839
			ftruncate($f, 0);
840
			$tmp = @fwrite($f, $chaine);
841
			if (!($tmp === FALSE)) {
842
				$resultat = true;
843
			}
844
			@fclose($f);
845
		}
846
		@chmod($fichier, $this->options['fichier_umask']);
847
		return $resultat;
848
	}
849
 
850
	/**
851
	 * Transform a file name into cache id and return it
852
	 *
853
	 * @param  string $fileName File name
854
	 * @return string Cache id
855
	 */
856
	protected function transformerNomFichierEnId($nom_de_fichier) {
857
		$prefixe = $this->options['fichier_prefixe'];
858
		return preg_replace('~^' . $prefixe . '---(.*)$~', '$1', $nom_de_fichier);
859
	}
860
}
861
?>