Subversion Repositories Applications.framework

Rev

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