parent
							
								
									4c119dcdc7
								
							
						
					
					
						commit
						23cd6ce2a7
					
				@ -1,128 +0,0 @@ | 
				
			||||
<?php | 
				
			||||
 | 
				
			||||
/** | 
				
			||||
 * Cache for storing data.  | 
				
			||||
 * | 
				
			||||
 * @license see /license.txt | 
				
			||||
 * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva | 
				
			||||
 */ | 
				
			||||
class Cache | 
				
			||||
{ | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * Retrieve an item from the cache if item creation date is greater than limit. | 
				
			||||
     * If item does not exists or is stale returns false. | 
				
			||||
     *  | 
				
			||||
     * @param any Identifier for the variable stored | 
				
			||||
     * @param int If this limit is greater than last mod time of value, stale | 
				
			||||
     * @return false|object Value kept in cache | 
				
			||||
     */ | 
				
			||||
    static function get($key, $limit = 0) | 
				
			||||
    { | 
				
			||||
        if (!self::has($key, $limit)) | 
				
			||||
        { | 
				
			||||
            return false; | 
				
			||||
        } | 
				
			||||
        $path = self::path($key); | 
				
			||||
        return file_get_contents($path); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * Returnsn true if the cache has the item and it is not staled. | 
				
			||||
     *  | 
				
			||||
     * @param any Identifier for the variable stored | 
				
			||||
     * @param int If this limit is greater than last mod time of value, stale | 
				
			||||
     * @return boolean | 
				
			||||
     */ | 
				
			||||
    static function has($key, $limit = 0) | 
				
			||||
    { | 
				
			||||
        $path = self::path($key); | 
				
			||||
        if (!is_readable($path)) | 
				
			||||
        { | 
				
			||||
            return false; | 
				
			||||
        } | 
				
			||||
        if ($limit) | 
				
			||||
        { | 
				
			||||
            $mtime = filemtime($path); | 
				
			||||
            if ($mtime < $limit) | 
				
			||||
            { | 
				
			||||
                return false; | 
				
			||||
            } | 
				
			||||
        } | 
				
			||||
        return true; | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * Put something in cache. | 
				
			||||
     *  | 
				
			||||
     * @param any Identifier for the variable to be stored | 
				
			||||
     * @param string Value to be stored | 
				
			||||
     */ | 
				
			||||
    static function put($key, $value) | 
				
			||||
    { | 
				
			||||
        $path = self::path($key); | 
				
			||||
        file_put_contents($path, $value); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * Remove an item from the cache. | 
				
			||||
     *  | 
				
			||||
     * @param any Identifier for the variable to remove | 
				
			||||
     */ | 
				
			||||
    static function remove($key) | 
				
			||||
    { | 
				
			||||
        $path = self::path($key); | 
				
			||||
        if (is_readable($path)) | 
				
			||||
        { | 
				
			||||
            unlink($path); | 
				
			||||
        } | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * Clear the cache. Remove all entries. | 
				
			||||
     */ | 
				
			||||
    static function clear() | 
				
			||||
    { | 
				
			||||
        $dir = self::path(); | 
				
			||||
        $files = scandir($dir); | 
				
			||||
        $files = array_diff($files, array('.', '..')); | 
				
			||||
        foreach ($files as $file) | 
				
			||||
        { | 
				
			||||
            $path = $dir . $file; | 
				
			||||
            unlink($path); | 
				
			||||
        } | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * Returns the file path based on the key. | 
				
			||||
     *  | 
				
			||||
     * @param any Identifier for the variable | 
				
			||||
     * @return string Path of the file where this  | 
				
			||||
     *                variable is/should-be stored | 
				
			||||
     */ | 
				
			||||
    static function path($key = '') | 
				
			||||
    { | 
				
			||||
        return Chamilo::path('archive/temp/cache/' . self::key($key)); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * Returns the internal string key from the external key. | 
				
			||||
     * For internal use. | 
				
			||||
     *  | 
				
			||||
     * @param any Identifier for the variable | 
				
			||||
     * @return string Unique ID generated to identify the variable | 
				
			||||
     */ | 
				
			||||
    static function key($item) | 
				
			||||
    { | 
				
			||||
        if (is_object($item)) | 
				
			||||
        { | 
				
			||||
            $f = array($item, 'get_unique_id'); | 
				
			||||
            if (is_callable($f)) | 
				
			||||
            { | 
				
			||||
                return call_user_func($f); | 
				
			||||
            } | 
				
			||||
        } | 
				
			||||
        $result = (string)$item; | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
} | 
				
			||||
@ -1,154 +0,0 @@ | 
				
			||||
<?php | 
				
			||||
/* For licensing terms, see /license.txt */ | 
				
			||||
/** | 
				
			||||
 *	This class allows to manage the session. Session is stored in the database. | 
				
			||||
 * | 
				
			||||
 *	@package chamilo.library | 
				
			||||
 */ | 
				
			||||
/** | 
				
			||||
 * @todo use the SessionServiceProvider and not a "in house" class | 
				
			||||
 * @package chamilo.library * | 
				
			||||
 */ | 
				
			||||
class SessionHandler | 
				
			||||
{ | 
				
			||||
 | 
				
			||||
	// TODO: Hm, these variables are public. | 
				
			||||
	public $connection; | 
				
			||||
	public $connection_handler; | 
				
			||||
	public $lifetime; | 
				
			||||
	public $session_name; | 
				
			||||
 | 
				
			||||
	public function __construct() { | 
				
			||||
		global $_configuration; | 
				
			||||
 | 
				
			||||
		$this->lifetime = 60; // 60 minutes | 
				
			||||
 | 
				
			||||
		$this->connection = array( | 
				
			||||
			'server' => $_configuration['db_host'], | 
				
			||||
			'login' => $_configuration['db_user'], | 
				
			||||
			'password' => $_configuration['db_password'], | 
				
			||||
			'base' => $_configuration['main_database'] | 
				
			||||
		); | 
				
			||||
 | 
				
			||||
		$this->connection_handler = false; | 
				
			||||
	} | 
				
			||||
 | 
				
			||||
    // @todo use a dbal connection | 
				
			||||
	public function sqlConnect() { | 
				
			||||
 | 
				
			||||
		if (!$this->connection_handler) { | 
				
			||||
			$this->connection_handler = @mysql_connect($this->connection['server'], $this->connection['login'], $this->connection['password'], true); | 
				
			||||
 | 
				
			||||
			// The system has not been designed to use special SQL modes that were introduced since MySQL 5 | 
				
			||||
			@mysql_query("set session sql_mode='';", $this->connection_handler); | 
				
			||||
 | 
				
			||||
			@mysql_select_db($this->connection['base'], $this->connection_handler); | 
				
			||||
 | 
				
			||||
			// Initialization of the database connection encoding to be used. | 
				
			||||
			// The internationalization library should be already initialized. | 
				
			||||
			@mysql_query("SET SESSION character_set_server='utf8';", $this->connection_handler); | 
				
			||||
			@mysql_query("SET SESSION collation_server='utf8_general_ci';", $this->connection_handler); | 
				
			||||
			$system_encoding = api_get_system_encoding(); | 
				
			||||
			if (api_is_utf8($system_encoding)) { | 
				
			||||
				// See Bug #1802: For UTF-8 systems we prefer to use "SET NAMES 'utf8'" statement in order to avoid a bizarre problem with Chinese language. | 
				
			||||
				@mysql_query("SET NAMES 'utf8';", $this->connection_handler); | 
				
			||||
			} else { | 
				
			||||
				@mysql_query("SET CHARACTER SET '" . Database::to_db_encoding($system_encoding) . "';", $this->connection_handler); | 
				
			||||
			} | 
				
			||||
		} | 
				
			||||
 | 
				
			||||
		return $this->connection_handler ? true : false; | 
				
			||||
	} | 
				
			||||
 | 
				
			||||
	public function sqlClose() { | 
				
			||||
 | 
				
			||||
		if ($this->connection_handler) { | 
				
			||||
			mysql_close($this->connection_handler); | 
				
			||||
			$this->connection_handler = false; | 
				
			||||
			return true; | 
				
			||||
		} | 
				
			||||
 | 
				
			||||
		return false; | 
				
			||||
	} | 
				
			||||
 | 
				
			||||
	public function sqlQuery($query, $die_on_error = true) { | 
				
			||||
 | 
				
			||||
		$result = mysql_query($query, $this->connection_handler); | 
				
			||||
 | 
				
			||||
		if ($die_on_error && !$result) { | 
				
			||||
			$this->sqlClose(); | 
				
			||||
			return; | 
				
			||||
		} | 
				
			||||
 | 
				
			||||
		return $result; | 
				
			||||
	} | 
				
			||||
 | 
				
			||||
	public function open($path, $name) { | 
				
			||||
 | 
				
			||||
		$this->session_name = $name; | 
				
			||||
		return true; | 
				
			||||
	} | 
				
			||||
 | 
				
			||||
	public function close() { | 
				
			||||
		return $this->garbage(0) ? true : false; | 
				
			||||
	} | 
				
			||||
 | 
				
			||||
	public function read($sess_id) { | 
				
			||||
 | 
				
			||||
		if ($this->sqlConnect()) { | 
				
			||||
			$result = $this->sqlQuery("SELECT session_value FROM ".$this->connection['base'].".php_session WHERE session_id='$sess_id'"); | 
				
			||||
 | 
				
			||||
			if ($row = mysql_fetch_assoc($result)) { | 
				
			||||
				return $row['session_value']; | 
				
			||||
			} | 
				
			||||
		} | 
				
			||||
 | 
				
			||||
		return ''; | 
				
			||||
	} | 
				
			||||
 | 
				
			||||
	public function write($sess_id, $sess_value) { | 
				
			||||
		$time = time(); | 
				
			||||
 | 
				
			||||
		if ($this->sqlConnect()) { | 
				
			||||
 | 
				
			||||
			$result = $this->sqlQuery("INSERT INTO ".$this->connection['base'].".php_session(session_id,session_name,session_time,session_start,session_value) VALUES('$sess_id','".$this->session_name."','$time','$time','".addslashes($sess_value)."')", false); | 
				
			||||
 | 
				
			||||
			if (!$result) { | 
				
			||||
				$this->sqlQuery("UPDATE ".$this->connection['base'].".php_session SET session_name='".$this->session_name."',session_time='$time',session_value='".addslashes($sess_value)."' WHERE session_id='$sess_id'"); | 
				
			||||
			} | 
				
			||||
 | 
				
			||||
			return true; | 
				
			||||
		} | 
				
			||||
 | 
				
			||||
		return false; | 
				
			||||
	} | 
				
			||||
 | 
				
			||||
	public function destroy($sess_id) { | 
				
			||||
 | 
				
			||||
		if ($this->sqlConnect()) { | 
				
			||||
			$this->sqlQuery("DELETE FROM ".$this->connection['base'].".php_session WHERE session_id='$sess_id'"); | 
				
			||||
			return true; | 
				
			||||
		} | 
				
			||||
 | 
				
			||||
		return false; | 
				
			||||
	} | 
				
			||||
 | 
				
			||||
	public function garbage($lifetime) { | 
				
			||||
 | 
				
			||||
		if ($this->sqlConnect()) { | 
				
			||||
 | 
				
			||||
			$result = $this->sqlQuery("SELECT COUNT(session_id) FROM ".$this->connection['base'].".php_session"); | 
				
			||||
			list($nbr_results) = Database::fetch_row($result); | 
				
			||||
 | 
				
			||||
			if ($nbr_results > 5000) { | 
				
			||||
				$this->sqlQuery("DELETE FROM ".$this->connection['base'].".php_session WHERE session_time<'".strtotime('-'.$this->lifetime.' minutes')."'"); | 
				
			||||
			} | 
				
			||||
 | 
				
			||||
			$this->sqlClose(); | 
				
			||||
 | 
				
			||||
			return true; | 
				
			||||
		} | 
				
			||||
 | 
				
			||||
		return false; | 
				
			||||
	} | 
				
			||||
} | 
				
			||||
@ -1,112 +0,0 @@ | 
				
			||||
<?php | 
				
			||||
/** | 
				
			||||
 * Generated by PHPUnit_SkeletonGenerator on 2012-10-01 at 14:44:08. | 
				
			||||
 */ | 
				
			||||
class CacheTest extends PHPUnit_Framework_TestCase | 
				
			||||
{ | 
				
			||||
    /** | 
				
			||||
     * @var Cache | 
				
			||||
     */ | 
				
			||||
    protected $object; | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * Sets up the fixture, for example, opens a network connection. | 
				
			||||
     * This method is called before a test is executed. | 
				
			||||
     */ | 
				
			||||
    protected function setUp() | 
				
			||||
    { | 
				
			||||
        $this->object = new Cache; | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * Tears down the fixture, for example, closes a network connection. | 
				
			||||
     * This method is called after a test is executed. | 
				
			||||
     */ | 
				
			||||
    protected function tearDown() | 
				
			||||
    { | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers Cache::get | 
				
			||||
     * @todo   Implement testGet(). | 
				
			||||
     */ | 
				
			||||
    public function testGet() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers Cache::has | 
				
			||||
     * @todo   Implement testHas(). | 
				
			||||
     */ | 
				
			||||
    public function testHas() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers Cache::put | 
				
			||||
     * @todo   Implement testPut(). | 
				
			||||
     */ | 
				
			||||
    public function testPut() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers Cache::remove | 
				
			||||
     * @todo   Implement testRemove(). | 
				
			||||
     */ | 
				
			||||
    public function testRemove() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers Cache::clear | 
				
			||||
     * @todo   Implement testClear(). | 
				
			||||
     */ | 
				
			||||
    public function testClear() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers Cache::path | 
				
			||||
     * @todo   Implement testPath(). | 
				
			||||
     */ | 
				
			||||
    public function testPath() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers Cache::key | 
				
			||||
     * @todo   Implement testKey(). | 
				
			||||
     */ | 
				
			||||
    public function testKey() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
} | 
				
			||||
@ -1,136 +0,0 @@ | 
				
			||||
<?php | 
				
			||||
/** | 
				
			||||
 * Generated by PHPUnit_SkeletonGenerator on 2012-10-01 at 14:44:09. | 
				
			||||
 */ | 
				
			||||
class SessionHandlerTest extends PHPUnit_Framework_TestCase | 
				
			||||
{ | 
				
			||||
    /** | 
				
			||||
     * @var SessionHandler | 
				
			||||
     */ | 
				
			||||
    protected $object; | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * Sets up the fixture, for example, opens a network connection. | 
				
			||||
     * This method is called before a test is executed. | 
				
			||||
     */ | 
				
			||||
    protected function setUp() | 
				
			||||
    { | 
				
			||||
        $this->object = new SessionHandler; | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * Tears down the fixture, for example, closes a network connection. | 
				
			||||
     * This method is called after a test is executed. | 
				
			||||
     */ | 
				
			||||
    protected function tearDown() | 
				
			||||
    { | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers SessionHandler::sqlConnect | 
				
			||||
     * @todo   Implement testSqlConnect(). | 
				
			||||
     */ | 
				
			||||
    public function testSqlConnect() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers SessionHandler::sqlClose | 
				
			||||
     * @todo   Implement testSqlClose(). | 
				
			||||
     */ | 
				
			||||
    public function testSqlClose() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers SessionHandler::sqlQuery | 
				
			||||
     * @todo   Implement testSqlQuery(). | 
				
			||||
     */ | 
				
			||||
    public function testSqlQuery() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers SessionHandler::open | 
				
			||||
     * @todo   Implement testOpen(). | 
				
			||||
     */ | 
				
			||||
    public function testOpen() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers SessionHandler::close | 
				
			||||
     * @todo   Implement testClose(). | 
				
			||||
     */ | 
				
			||||
    public function testClose() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers SessionHandler::read | 
				
			||||
     * @todo   Implement testRead(). | 
				
			||||
     */ | 
				
			||||
    public function testRead() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers SessionHandler::write | 
				
			||||
     * @todo   Implement testWrite(). | 
				
			||||
     */ | 
				
			||||
    public function testWrite() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers SessionHandler::destroy | 
				
			||||
     * @todo   Implement testDestroy(). | 
				
			||||
     */ | 
				
			||||
    public function testDestroy() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    /** | 
				
			||||
     * @covers SessionHandler::garbage | 
				
			||||
     * @todo   Implement testGarbage(). | 
				
			||||
     */ | 
				
			||||
    public function testGarbage() | 
				
			||||
    { | 
				
			||||
        // Remove the following lines when you implement this test. | 
				
			||||
        $this->markTestIncomplete( | 
				
			||||
          'This test has not been implemented yet.' | 
				
			||||
        ); | 
				
			||||
    } | 
				
			||||
} | 
				
			||||
					Loading…
					
					
				
		Reference in new issue