noalyss Version-9
file_cache.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of noalyss.
5 *
6 * noalyss is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * noalyss is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with NOALYSS; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21// Copyright Author Dany De Bontridder danydb@aevalys.eu 2002-2024
22
23/*!\file
24 * \brief
25 */
26/*!
27 *
28 * \class
29 *
30 * \brief
31 */
32
33namespace Noalyss;
34
36{
37 /**
38 * @brief Check if the file exists, if yes then read otherwise take it from the url,
39 * @param $url url to the file to get the content
40 * @param $filename fullpath to the filename of the cache
41 * @param $time_cache_second valid time, if the cache is older , than reload it from the url
42 * @return void
43 */
44 public static function get_content($url, $filename, $time_cache_second)
45 {
46 $content = null;
47 if (file_exists($filename)) {
48 $date_time = new \DateTime();
49 $file_tmstamp = filemtime($filename);
50
51 $delta = $date_time->getTimestamp() - $file_tmstamp;
52
53 // if file too old , refresh it
54 if ($delta > $time_cache_second) {
55 $content = file_get_contents($url);
56 $f_file = fopen($filename, "w+");
57 if ( $f_file == false ) {
58 return $content;
59 }
60 fwrite($f_file, $content);
61 fclose($f_file);
62 } else {
63 $content = file_get_contents($filename);
64 }
65 } else {
66 $content = @file_get_contents($url);
67 $f_file = fopen($filename, "w+");
68 if ( $f_file == false ) {
69 return $content;
70 }
71 fwrite($f_file, $content);
72 fclose($f_file);
73
74 }
75 return $content;
76 }
77}
$url
static get_content($url, $filename, $time_cache_second)
Check if the file exists, if yes then read otherwise take it from the url,.
$content
Definition: xml.php:17