diff --git a/html/user_login.php b/html/user_login.php index bd29135c8..cf3c33aae 100644 --- a/html/user_login.php +++ b/html/user_login.php @@ -103,27 +103,7 @@ if ( $User->isAdmin() == 1) { if (SITE_UPDATE !="") { $file=$_ENV['TMP']."/version_noalyss".domaine; - if (file_exists($file)) { - $date_time=new \DateTime(); - $file_tmstamp=filemtime($file); - - $delta= $date_time->getTimestamp() - $file_tmstamp; - // 172800 = 2 days in second - if ( $delta > 172800 ) { - $update=@file_get_contents(SITE_UPDATE); - $f_file= fopen($file,"w+"); - fwrite($f_file,$update); - fclose($f_file); - } else { - $update=@file_get_contents($file); - } - - } else { - $update=@file_get_contents(SITE_UPDATE); - $f_file= fopen($file,"w+"); - fwrite($f_file,$update); - fclose($f_file); - } + $update=\Noalyss\File_Cache::get_content(SITE_UPDATE,$file,172800); if ($update > $version_noalyss ) { echo '
'; diff --git a/include/constant.php b/include/constant.php index f05eb71f9..364bbdd3b 100644 --- a/include/constant.php +++ b/include/constant.php @@ -395,13 +395,16 @@ function noalyss_class_autoloader($class) "httpinput" => "lib/http_input.class.php", "ismallbutton" => "lib/ibutton.class.php", "inputswitch" => "lib/input_switch.class.php", - "noalyss\mobile" => "class/mobile.class.php", + 'noalyss\mobile' => "class/mobile.class.php", "htmlinput" => "lib/html_input.class.php", - "noalyss\dbg"=>"lib/dbg.php", + 'noalyss\dbg'=>"lib/dbg.php", + 'noalyss\file_cache'=>"lib/file_cache.class.php", "pdfland"=>"class/pdf_land.class.php" ); if (isset ($aClass[$class])) { require_once NOALYSS_INCLUDE . "/" . $aClass[$class]; + }else { + echo "autoloader fails with $class"; } } diff --git a/include/lib/file_cache.class.php b/include/lib/file_cache.class.php new file mode 100644 index 000000000..f09c534bd --- /dev/null +++ b/include/lib/file_cache.class.php @@ -0,0 +1,77 @@ +getTimestamp() - $file_tmstamp; + + // if file too old , refresh it + if ($delta > $time_cache_second) { + $content = file_get_contents($url); + $f_file = fopen($filename, "w+"); + if ( $f_file == false ) { + return $content; + } + fwrite($f_file, $content); + fclose($f_file); + } else { + $content = file_get_contents($filename); + } + } else { + $content = @file_get_contents($url); + $f_file = fopen($filename, "w+"); + if ( $f_file == false ) { + return $content; + } + fwrite($f_file, $content); + fclose($f_file); + + } + return $content; + } +} \ No newline at end of file diff --git a/unit-test/include/lib/file_cacheTest.php b/unit-test/include/lib/file_cacheTest.php new file mode 100644 index 000000000..41cea8fce --- /dev/null +++ b/unit-test/include/lib/file_cacheTest.php @@ -0,0 +1,68 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * + * Author : Dany De Bontridder danydb@noalyss.eu $(DATE) + */ + +/** + * @file + * @brief noalyss + */ + +use PHPUnit\Framework\TestCase; + +require DIRTEST . '/global.php'; + +/** + * @testdox Class Package_RepositoryTest : used for ... + * @backupGlobals enabled + * @coversDefaultClass + */ +class File_CacheTest extends TestCase +{ + + /** + * @testdox test the content and cache it + * @covers \Noalyss\File_Cache::get_content + */ + function testGet_content() + { + $web_xml = $_ENV['TMP'] . "/testcache.xml"; + if ( file_exists($web_xml) ) unlink($web_xml); + + \Noalyss\File_Cache::get_content(NOALYSS_PACKAGE_REPOSITORY."/web.xml",$web_xml,3); + + $this->assertTrue(file_exists($web_xml),"File not loaded"); + $time = filemtime($web_xml); + sleep(4); + clearstatcache(true, $web_xml); + \Noalyss\File_Cache::get_content(NOALYSS_PACKAGE_REPOSITORY."/web.xml",$web_xml,30); + $time2 = filemtime($web_xml); + $this->assertEquals($time, $time2, " testcache.xml not cached"); + + \Noalyss\File_Cache::get_content(NOALYSS_PACKAGE_REPOSITORY."/web.xml",$web_xml,3); + sleep(6); + clearstatcache(true, $web_xml); + $time3 = filemtime($web_xml); + $this->assertNotEquals($time, $time3, " testcache.xml not refreshed"); + } + + +} \ No newline at end of file