<?php
// tab/indent == 4 spaces
// $Date: 2002/04/21 03:08:15 $
// $Revision: 1.2 $
/**
* HTTP Storage class
*
* PHP class, used to serialize and store information to disk.
* This class will be used if storage features are activated
* in the main HTTP Navigator class.
* You do not have to include it if you will not be saving
* info to disk.
*
* @author Keyvan Minoukadeh <keyvan@k1m.com>
* @version 0.1 alpha
*/
class http_storage
{
/**
* Check calling class
*
* Checks is $obj is instance of http_navigator
*
* @param object $obj instance of http_navigator passed by reference
* @return bool
*/
function check_calling_class(&$obj)
{
if (is_object($obj) && (get_class($obj) == 'http_navigator')) {
return true;
} else {
die('STORAGE: object $obj is not instance of http_navigator');
}
}
/**
* Grab file content
*
* Takes filename as argument, returns contents of the file
*
* @param string $filepath full path to file
* @return string contents of $filepath, false on error
*/
function read_file($filepath)
{
if (file_exists($filepath) && is_readable($filepath)) {
$fp = @fopen($filepath, 'r');
if (!$fp)
return false;
flock($fp, LOCK_SH);
$content = fread($fp, filesize($filepath));
flock($fp, LOCK_UN);
fclose($fp);
return $content;
} else {
return false;
}
}
/**
* Write file content
*
* Writes $string to file $filepath
*
* @param string $filepath full path to file
* @param string $string string to write
* @return bool true if write succeeded, false otherwise
*/
function write_file($filepath, $string)
{
$fp = @fopen($filepath, 'w');
if (!$fp)
return false;
flock($fp, LOCK_EX);
$result = @fwrite($fp, $string);
if (!$result)
return false;
flock($fp, LOCK_UN);
fclose($fp);
chmod($filepath, 0600);
return true;
}
/**
* Load files from disk
*
* Will read files, unserialize content, and store in appropriate var
*
* @param object $obj instance of http_navigator passed by reference
* @return bool
*/
function load_files(&$obj)
{
$func_name = 'STORAGE: load_files';
$this->check_calling_class($obj);
if ($obj->store_name == '') {
$obj->_debug("$func_name: Storage name is empty, skipping file load");
return false;
}
$file_cookies = $obj->store_path.'cookies.'.$obj->store_name;
$file_basic_auth = $obj->store_path.'basic_auth.'.$obj->store_name;
///////////////////////
// cookies
if ($obj->store_cookies && file_exists($file_cookies) && is_readable($file_cookies)) {
$content = $this->read_file($file_cookies);
if ($content === false) {
die("$func_name: could not read cookies file");
}
if (($obj->store_cookies == 'encrypt') && (!empty($obj->crypt_key))) {
$content = $this->decrypt($obj, $content);
if ($content === false) {
die("$func_name: problem decrypting cookie file");
}
}
if (trim($content) != '') {
$content = @unserialize($content);
if (is_array($content)) {
$obj->_debug("$func_name: Loading cookies from disk");
$obj->cookie = $content;
} else {
die("$func_name: cookie file appears to be corrupt! Delete and try again");
}
} else {
$obj->_debug("$func_name: Cookie file is empty");
return false;
}
}
//////////////////////
// basic auth
if ($obj->store_basic_auth && file_exists($file_basic_auth) && is_readable($file_basic_auth)) {
$content = $this->read_file($file_basic_auth);
if ($content === false) {
die("$func_name: could not read basic auth file");
}
if (($obj->store_basic_auth == 'encrypt') && (!empty($obj->crypt_key))) {
$content = $this->decrypt($obj, $content);
if ($content === false) {
die("$func_name: problem decrypting basic auth file");
}
}
if (trim($content) != '') {
$content = @unserialize($content);
if (is_array($content)) {
$obj->_debug("$func_name: Loading basic auth from disk");
$obj->basic_auth = $content;
} else {
die("$func_name: basic auth file appears to be corrupt! Delete and try again");
}
} else {
$obj->_debug("$func_name: Basic auth file is empty");
return false;
}
}
return true;
}
/**
* Save files to disk
*
* Will serialize (and encrypt) content, and store on disk
*
* @param object $obj instance of http_navigator passed by reference
* @return bool
*/
function save_files(&$obj)
{
$func_name = 'STORAGE: save_files';
$this->check_calling_class($obj);
if ($obj->store_name == '') {
$obj->_debug("$func_name: Storage name is empty, skipping file save");
return false;
}
$file_cookies = $obj->store_path.'cookies.'.$obj->store_name;
$file_basic_auth = $obj->store_path.'basic_auth.'.$obj->store_name;
///////////////////////
// cookies
if ($obj->store_cookies) {
$content = serialize($obj->cookie);
if (($obj->store_cookies == 'encrypt') && (!empty($obj->crypt_key))) {
$content = $this->encrypt($obj, $content);
if ($content === false) {
die("$func_name: problem encrypting cookie file");
}
}
$result = $this->write_file($file_cookies, $content);
if ($result === false) {
die("$func_name: problem writing cookie file to disk");
}
}
//////////////////////
// basic auth
if ($obj->store_basic_auth) {
$content = serialize($obj->basic_auth);
if (($obj->store_basic_auth == 'encrypt') && (!empty($obj->crypt_key))) {
$content = $this->encrypt($obj, $content);
if ($content === false) {
die("$func_name: problem encrypting basic auth file");
}
}
$result = $this->write_file($file_basic_auth, $content);
if ($result === false) {
die("$func_name: problem writing basic auth file to disk");
}
}
return true;
}
/**
* Delete files
*
* Will delete storage files from disk
*
* @param object $obj instance of http_navigator passed by reference
* @return bool
*/
function del_files(&$obj)
{
$func_name = 'STORAGE: del_files';
$this->check_calling_class($obj);
if ($obj->store_name == '') {
$obj->_debug("$func_name: Storage name is empty, skipping file delete");
return false;
}
$file_cookies = $obj->store_path.'cookies.'.$obj->store_name;
$file_basic_auth = $obj->store_path.'basic_auth.'.$obj->store_name;
if (@unlink($file_cookies)) {
$obj->_debug("$func_name: cookies file deleted");
} else {
$obj->_debug("$func_name: could NOT delete cookies file!");
}
if (@unlink($file_basic_auth)) {
$obj->_debug("$func_name: basic auth file deleted");
} else {
$obj->_debug("$func_name: could NOT delete basic auth file!");
}
return true;
}
/**
* Encrypt
*
* Encrypts string with $obj->crypt_key
*
* @param object $obj instance of http_navigator passed by reference
* @param string $string string to encrypt
* @return string encrypted string
* @access private
*/
function encrypt(&$obj, $string)
{
$func_name = 'STORAGE: _encrypt';
$this->check_calling_class($obj);
if (!extension_loaded('mcrypt')) {
$obj->_debug("$func_name: PHP not built with mcrypt support, cannot use encryption");
return false;
}
$iv = @mcrypt_create_iv(@mcrypt_get_iv_size(MCRYPT_TripleDES, MCRYPT_MODE_ECB), MCRYPT_RAND);
if (!$iv) {
$obj->_debug("$func_name: Problem encrypting");
return false;
}
$encrypted = @mcrypt_encrypt(MCRYPT_TripleDES, $obj->crypt_key, $string, MCRYPT_MODE_ECB, $iv);
if (!$encrypted) {
$obj->_debug("$func_name: Problem encrypting");
return false;
}
return $encrypted;
}
/**
* Decrypt
*
* Decrypt encrypted string with $obj->crypt_key
*
* @param object $obj instance of http_navigator passed by reference
* @param string $string encrypted string to decrypt
* @return string decrypted string
* @access private
*/
function decrypt(&$obj, $string)
{
$func_name = 'STORAGE: _decrypt';
$this->check_calling_class($obj);
if (!extension_loaded('mcrypt')) {
$obj->_debug("$func_name: PHP not built with mcrypt support, cannot use encryption");
return false;
}
$iv = @mcrypt_create_iv(@mcrypt_get_iv_size(MCRYPT_TripleDES, MCRYPT_MODE_ECB), MCRYPT_RAND);
if (!$iv) {
$obj->_debug("$func_name: Problem decrypting");
return false;
}
$decrypted = @mcrypt_decrypt(MCRYPT_TripleDES, $obj->crypt_key, $string, MCRYPT_MODE_ECB, $iv);
if (!$iv) {
$obj->_debug("$func_name: Problem decrypting");
return false;
}
return trim($decrypted);
}
}
?> |