/webcore/webcore.web.php
PHP | 2840 lines | 1581 code | 298 blank | 961 comment | 114 complexity | abb435e1948727a2e20de1d52ac286c2 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * @package WebCore
- * @subpackage Web
- *
- *
- *
- * @author Mario Di Vece <mario@unosquare.com>
- * @author Geovanni Perez <geovanni.perez@unosquare.com>
- * @copyright Copyright (c) 2008-2009, Author(s) above
- */
-
- require_once "webcore.php";
- require_once "webcore.imaging.php";
-
- /**
- * All Http handlers must implement this interface.
- * @package WebCore
- * @subpackage Web
- */
- interface IHttpHandler extends IObject
- {
- function handleRequest();
- }
-
- /**
- * Defines the methods required for a session handler.
- * @package WebCore
- * @subpackage Web
- */
- interface ISessionHandler extends ISingleton
- {
- /**
- * @param string $savePath
- * @param string $sessionName
- * @return bool
- */
- public static function open($savePath, $sessionName);
- /**
- * @return bool
- */
- public static function close();
- /**
- * @param string $sessionId
- * @return string
- */
- public static function read($sessionId);
- /**
- * @param string $sessionId
- * @param string $sessionData
- * @return bool
- */
- public static function write($sessionId, $sessionData);
- /**
- * @param string $sessionId
- * @return bool
- */
- public static function destroy($sessionId);
- /**
- * @param int $maxLifetime the number of seconds the session exists for
- * @return bool
- */
- public static function gc($maxLifetime);
- }
-
- /**
- * Cookie management singleton.
- * @todo Create CookieContainer class to match path and domain otherwise deleteCookie does not work!
- * @package WebCore
- * @subpackage Web
- */
- class CookieManager extends SerializableObjectBase implements ISingleton
- {
- /**
- * Represents the instance of the singleton object
- *
- * @var CookieManager
- */
- protected static $__instance = null;
- /**
- * @var KeyedCollection
- */
- protected static $__internalCollection;
-
- /**
- * Creates a new instance of this class.
- */
- protected function __construct()
- {
- self::$__internalCollection = new KeyedCollectionWrapper($_COOKIE, false);
- }
-
- /**
- * Gets a unique cookie key based on it name, path, and domain.
- * @param string $name
- * @param string $path
- * @param string $domain
- */
- protected static function getCookieId($name, $path, $domain)
- {
- return $domain . $path . $name;
- }
-
- /**
- * Sets or registers a cookie value given its identifier and value.
- * @param string $name
- * @param mixed $value
- * @param string $path Specify an empty string to make this cookie valid only for the current folder.
- * @param string $domain Leave empty to specify the cookie is valid for the entire domain.
- * @param string $expires The UNIX string representation of a time. The expiration timestamp is calculated using the strtotime function.
- * @param bool $secure
- * @param bool $httpOnly Prevents access to the cookie on the client-side (i.e. XSS attacks)
- * @return bool Returns true if the cookie was successfully set.
- */
- public function setCookie($name, $value, $path = '/', $domain = '', $expires = '+30 days', $secure = false, $httpOnly = false)
- {
- $cookieId = self::getCookieId($name, $path, $domain);
- return setcookie($cookieId, $value, strtotime($expires), $path, $domain, $secure, $httpOnly);
- }
-
- /**
- * Deletes the cookie given its name, path and domain.
- * Note that the path and domain must match the original intended cookie for this method to not throw an exception.
- * @param string $name
- * @param string $path
- * @param string $domain
- * @return bool Returns true if the cookie was successfully deleted (an expiration time in the past is set).
- */
- public function deleteCookie($name, $path = '/', $domain = '')
- {
- $cookieId = self::getCookieId($name, $path, $domain);
- if (!$this->isCookieSet($name, $path, $domain))
- throw new SystemException(SystemException::EX_INVALIDKEY, "Could not delete cookie with id '$cookieId'. It does not exist.");
- $this->getCookies()->removeItem($cookieId);
- return setcookie($cookieId, '', 1, $path, $domain);
- }
-
- /**
- * Gets the value stored within a cookie.
- * @param string $name The name of the cookie
- * @param string $path The path for which the cookie is valid.
- * @param string $domain The domain for which the cookie is available.
- *
- * @return mixed
- */
- public function getCookieValue($name, $path = '/', $domain = '')
- {
- $cookieId = self::getCookieId($name, $path, $domain);
- return $this->getCookies()->getValue($cookieId);
- }
-
- /**
- * Provides direct access to the cookies collection.
- * Use the provided cookie methods instead for better cookie management.
- * @return KeyedCollection
- */
- public function getCookies()
- {
- return self::$__internalCollection;
- }
-
- /**
- * Determines whether the cokkie with a given name has been set.
- * @return bool
- */
- public function isCookieSet($name, $path = '/', $domain = '')
- {
- $cookieId = self::getCookieId($name, $path, $domain);
- return $this->getCookies()->keyExists($cookieId);
- }
-
- /**
- * Creates a default instance of this class.
- * @return CookieManager
- */
- public static function createInstance()
- {
- return self::getInstance();
- }
-
- /**
- * Gets the singleton instance for this class
- *
- * @return CookieManager
- */
- public static function getInstance()
- {
- if (self::isLoaded() === false)
- self::$__instance = new CookieManager();
-
- return self::$__instance;
- }
-
- /**
- * Determines whether the singleton has loaded its instance
- *
- * @return bool
- */
- public static function isLoaded()
- {
- if (is_null(self::$__instance))
- return false;
-
- return true;
- }
- }
-
-
- /**
- * Provides a database-agnostic session handler.
- * This session ehandler expects a table structure accessible from the data context.
- * WARNING: If the table is not found, it will be automatically created
- * Tabe Name: AppSessions (use setDbTableName to change this before calling the HttpContext::initialize method)
- * Column: SessionId VARCHAR(255), Primary key
- * Column: SessionData Text
- * Column: SessionExpires Int32
- * Standard ISO SQL command
- * CREATE TABLE AppSessions (
- SessionId varchar(255) NOT NULL,
- SessionData text NOT NULL,
- SessionExpires int NOT NULL,
- PRIMARY KEY (SessionId)
- )
- * @package WebCore
- * @subpackage Web
- */
- class DatabaseSessionHandler implements ISessionHandler
- {
-
- const DB_TABLENAME = 'AppSessions';
-
- private static $dbTableName = self::DB_TABLENAME;
-
- /**
- * Gets the database table name on which sessions are stored
- */
- public static function getDbTableName()
- {
- return self::$dbTableName;
- }
-
- /**
- * Sets the database table name on which sessions are stored
- */
- public static function setDbTableName($value)
- {
- self::$dbTableName = $value;
- }
-
- /**
- * Gets whether the instance of this singleton has been loaded.
- * @return bool
- */
- public static function isLoaded()
- {
- return true;
- }
-
- /**
- * Gets the instance of this singleton
- * @return eAcceleratorSessionHandler
- */
- public static function getInstance()
- {
- return new DatabaseSessionHandler();
- }
-
- /**
- * Gets the reflectionclass for this type.
- * @return ReflectionClass
- */
- public function getType()
- {
- return ReflectionClass(__CLASS__);
- }
-
- /**
- * @param string $savePath
- * @param string $sessionName
- * @return bool
- */
- public static function open($savePath, $sessionName)
- {
- $originalLogLevel = LogManager::getLogLevel();
- LogManager::setLogLevel(LogManager::LOG_LEVEL_DISABLED);
-
- $found = false;
- $foundCol = new IndexedCollection();
-
- try
- {
- $sql = "SELECT * FROM information_schema.tables WHERE TABLE_NAME LIKE ?";
- $command = DataContext::getInstance()->getConnection()->createCommand();
- $command->setCommandText($sql);
- $command->addQuotedParam('tableName', self::getDbTableName());
- $foundCol = DataContext::getInstance()->executeQuery($command);
- }
- catch (Exception $ex)
- {
- $exString = "<pre>DatabseSessionHandler Exception\nA default database needs to be configured in the app.settings file.\n"
- . $ex->getLine() . " on '" . $ex->getFile() . "'\n"
- . $ex->getCode() . ": " . $ex->getMessage() . "\nStack Trace:\n" . $ex->getTraceAsString() . "</pre>";
- LogManager::setLogLevel($originalLogLevel);
- HttpResponse::write($exString);
- HttpResponse::end();
- return false;
- }
-
- $found = $foundCol->getCount() > 0;
- if (!$found)
- {
- $quotedTableName = DataContext::getInstance()->getConnection()->quoteIdentifier(self::getDbTableName());
- $sql = "CREATE TABLE $quotedTableName ( SessionId varchar(255) NOT NULL, SessionData text NOT NULL, SessionExpires int NOT NULL, PRIMARY KEY (SessionId) )";
- $command = DataContext::getInstance()->getConnection()->createCommand();
- $command->setCommandText($sql);
- $sessionData = DataContext::getInstance()->executeNonQuery($command);
- }
-
- LogManager::setLogLevel($originalLogLevel);
- return true;
- }
- /**
- * @return bool
- */
- public static function close()
- {
- return true;
- }
- /**
- * @param string $sessionId
- * @return string
- */
- public static function read($sessionId)
- {
- $originalLogLevel = LogManager::getLogLevel();
- LogManager::setLogLevel(LogManager::LOG_LEVEL_DISABLED);
-
- try
- {
- $quotedTableName = DataContext::getInstance()->getConnection()->quoteIdentifier(self::getDbTableName());
- $sql = "SELECT SessionData FROM $quotedTableName WHERE SessionId = ?";
- $command = DataContext::getInstance()->getConnection()->createCommand();
- $command->setCommandText($sql);
- $command->addQuotedParam('sessionId', $sessionId);
- $sessionData = DataContext::getInstance()->executeScalar($command);
-
- if (is_null($sessionData))
- {
- LogManager::setLogLevel($originalLogLevel);
- return '';
- }
- else
- {
- LogManager::setLogLevel($originalLogLevel);
- return base64_decode($sessionData);
- }
- }
- catch (Exception $ex)
- {
- $exString = "<pre>DatabseSessionHandler Exception\nUnable to read session record from database.\n"
- . $ex->getLine() . " on '" . $ex->getFile() . "'\n"
- . $ex->getCode() . ": " . $ex->getMessage() . "\nStack Trace:\n" . $ex->getTraceAsString() . "</pre>";
- LogManager::setLogLevel($originalLogLevel);
- HttpResponse::write($exString);
- HttpResponse::end();
- return null;
- }
-
- }
- /**
- * Writes session data to the database
- * @param string $sessionId
- * @param string $sessionData
- * @return bool
- */
- public static function write($sessionId, $sessionData)
- {
- $originalLogLevel = LogManager::getLogLevel();
- LogManager::setLogLevel(LogManager::LOG_LEVEL_DISABLED);
-
- try
- {
- $quotedTableName = DataContext::getInstance()->getConnection()->quoteIdentifier(self::getDbTableName());
- $sql = "SELECT COUNT(*) AS __COUNT__ FROM $quotedTableName WHERE SessionId = ?";
- $command = DataContext::getInstance()->getConnection()->createCommand();
- $command->setCommandText($sql);
- $command->addQuotedParam('sessionId', $sessionId);
- $foundSession = DataContext::getInstance()->executeScalar($command);
-
- $command = DataContext::getInstance()->getConnection()->createCommand();
- if ($foundSession == '0')
- {
- $sql = "INSERT INTO $quotedTableName (SessionId, SessionData, SessionExpires) VALUES (?, ?, ?)";
- $command->addQuotedParam('sessionId', $sessionId);
- $command->addQuotedParam('sessionData', base64_encode($sessionData));
- $command->addQuotedParam('sessionExpires', time());
- }
- else
- {
- $sql = "UPDATE $quotedTableName SET SessionData = ?, SessionExpires = ? WHERE SessionId = ?";
- $command->addQuotedParam('sessionData', base64_encode($sessionData));
- $command->addQuotedParam('sessionExpires', time());
- $command->addQuotedParam('sessionId', $sessionId);
- }
-
- $command->setCommandText($sql);
- DataContext::getInstance()->executeNonQuery($command);
- }
- catch (Exception $ex)
- {
- $exString = "<pre>DatabseSessionHandler Exception\nUnable to write session record to database.\n"
- . $ex->getLine() . " on '" . $ex->getFile() . "'\n"
- . $ex->getCode() . ": " . $ex->getMessage() . "\nStack Trace:\n" . $ex->getTraceAsString() . "</pre>";
- LogManager::setLogLevel($originalLogLevel);
- HttpResponse::write($exString);
- HttpResponse::end();
- return false;
- }
-
- LogManager::setLogLevel($originalLogLevel);
- return true;
- }
- /**
- * @param string $sessionId
- * @return bool
- */
- public static function destroy($sessionId)
- {
- $originalLogLevel = LogManager::getLogLevel();
- LogManager::setLogLevel(LogManager::LOG_LEVEL_DISABLED);
-
- try
- {
- $quotedTableName = DataContext::getInstance()->getConnection()->quoteIdentifier(self::getDbTableName());
- $sql = "DELETE FROM $quotedTableName WHERE SessionId = ?";
- $command = DataContext::getInstance()->getConnection()->createCommand();
- $command->setCommandText($sql);
- $command->addQuotedParam('sessionId', $sessionId);
- DataContext::getInstance()->executeNonQuery($command);
- }
- catch (Exception $ex)
- {
- $exString = "<pre>DatabseSessionHandler Exception\nUnable to destroy session record.\n"
- . $ex->getLine() . " on '" . $ex->getFile() . "'\n"
- . $ex->getCode() . ": " . $ex->getMessage() . "\nStack Trace:\n" . $ex->getTraceAsString() . "</pre>";
- LogManager::setLogLevel($originalLogLevel);
- HttpResponse::write($exString);
- HttpResponse::end();
- return false;
- }
-
-
- LogManager::setLogLevel($originalLogLevel);
- return true;
- }
- /**
- * @param int $maxLifetime the number of seconds the session exists for
- * @return bool
- */
- public static function gc($maxLifetime)
- {
- $originalLogLevel = LogManager::getLogLevel();
- LogManager::setLogLevel(LogManager::LOG_LEVEL_DISABLED);
-
- try
- {
- $quotedTableName = DataContext::getInstance()->getConnection()->quoteIdentifier(self::getDbTableName());
- $deleteFilter = time() - $maxLifetime;
- $sql = "DELETE FROM $quotedTableName WHERE SessionExpires < ?";
- $command = DataContext::getInstance()->getConnection()->createCommand();
- $command->setCommandText($sql);
- $command->addQuotedParam('sessionExpires', time());
- DataContext::getInstance()->executeNonQuery($command);
- }
- catch (Exception $ex)
- {
- $exString = "<pre>DatabseSessionHandler Exception\nUnable to perform session garbage collection.\n"
- . $ex->getLine() . " on '" . $ex->getFile() . "'\n"
- . $ex->getCode() . ": " . $ex->getMessage() . "\nStack Trace:\n" . $ex->getTraceAsString() . "</pre>";
- LogManager::setLogLevel($originalLogLevel);
- HttpResponse::write($exString);
- HttpResponse::end();
- return false;
- }
-
- LogManager::setLogLevel($originalLogLevel);
- return true;
- }
- }
-
- /**
- * Provides a session handler for eAccelerator-enabled web servers.
- * @package WebCore
- * @subpackage Web
- */
- class eAcceleratorSessionHandler implements ISessionHandler
- {
- protected static $sessionName;
-
- /**
- * Gets whether the instance of this singleton has been loaded.
- * @return bool
- */
- public static function isLoaded()
- {
- return true;
- }
-
- /**
- * Gets the instance of this singleton
- * @return eAcceleratorSessionHandler
- */
- public static function getInstance()
- {
- return new eAcceleratorSessionHandler();
- }
-
- /**
- * Gets the reflectionclass for this type.
- * @return ReflectionClass
- */
- public function getType()
- {
- return ReflectionClass(__CLASS__);
- }
-
- /**
- * @param string $savePath
- * @param string $sessionName
- * @return bool
- */
- public static function open($savePath, $sessionName)
- {
- self::$sessionName = $sessionName;
- return true;
- }
- /**
- * @return bool
- */
- public static function close()
- {
- return true;
- }
- /**
- * @param string $sessionId
- * @return string
- */
- public static function read($sessionId)
- {
- return eaccelerator_get(self::$sessionName . '_' . $sessionId);
- }
- /**
- * @param string $sessionId
- * @param string $sessionData
- * @return bool
- */
- public static function write($sessionId, $sessionData)
- {
- $ttl = session_cache_limiter() * 60;
- if ($ttl <= 0)
- $ttl = 30 * 60;
- return eaccelerator_put(self::$sessionName . '_' . $sessionId, $sessionData, $ttl);
- }
- /**
- * @param string $sessionId
- * @return bool
- */
- public static function destroy($sessionId)
- {
- return eaccelerator_rm(self::$sessionName . '_' . $sessionId);
- }
- /**
- * @param int $maxLifetime the number of seconds the session exists for
- * @return bool
- */
- public static function gc($maxLifetime)
- {
- eaccelerator_gc();
- return true;
- }
- }
-
- /**
- * Singleton class representing the current request's session state
- * The class should not be used on its own but rather accessed through
- * the HttpContext object.
- *
- * @package WebCore
- * @subpackage Web
- */
- class HttpSession extends KeyedCollectionBase implements ISingleton
- {
- protected function __construct()
- {
- $appSettings = Settings::getValue(Settings::SKEY_APPLICATION);
- $handler = isset($appSettings[Settings::KEY_APPLICATION_SESSIONHANDLER]) ? $appSettings[Settings::KEY_APPLICATION_SESSIONHANDLER] : '';
- if ($handler != '')
- {
- session_set_save_handler(array(
- $handler,
- 'open'
- ), array(
- $handler,
- 'close'
- ), array(
- $handler,
- 'read'
- ), array(
- $handler,
- 'write'
- ), array(
- $handler,
- 'destroy'
- ), array(
- $handler,
- 'gc'
- ));
- }
-
- session_start();
- parent::__construct($_SESSION, false);
- }
-
- /**
- * Regenerates the current session Id
- * Returns true is successful, false if failure.
- *
- * @return bool
- */
- public static function regenerateSessionId()
- {
- return session_regenerate_id();
- }
-
- /**
- * gets the current session identifier
- *
- * @return string
- */
- public static function getSessionId()
- {
- return session_id();
- }
-
- /**
- * Represents the instance of the singleton object
- *
- * @var HttpSession
- */
- private static $__instance = null;
-
- /**
- * Gets the singleton instance for this class
- *
- * @return HttpSession
- */
- public static function getInstance()
- {
- if (self::isLoaded() === false)
- self::$__instance = new HttpSession();
-
- return self::$__instance;
- }
-
- /**
- * Determines whether the singleton has loaded its instance
- *
- * @return bool
- */
- public static function isLoaded()
- {
- if (is_null(self::$__instance))
- return false;
-
- return true;
- }
-
- /**
- * Creates a default instance of this class
- *
- * @return HttpSession
- */
- public static function createInstance()
- {
- return self::getInstance();
- }
-
- /**
- * Clears all the session variables
- */
- public function clear()
- {
- session_unset();
- }
-
- /**
- * Destroys the session and all data associated with it. this is equivalent to calling session_destroy()
- */
- public function destroy()
- {
- session_destroy();
- }
-
- /**
- * Registers an object -or array- for in-session persistence.
- * If the key does not exist, it creates it with the passed $object
- * If the key already exists, then $object is set to the contents of the key
- * @example $obj = new StdClass(); HttpContext::getSession()->registerPersistentObject('myKey', $obj); $obj->prop1 = 'myProp1Value';
- * @param string $keyName
- * @param object $object
- * @return int Returns 0 if the key did not exist before. Returns 1 if the key existed before.
- */
- public static function registerPersistentObject($keyName, &$object)
- {
- if (!is_object($object) && !is_array($object))
- throw new SystemException(SystemException::EX_INVALIDPARAMETER, 'Parameter $object must be an instance of an object or an array.');
- if (!is_string($keyName))
- throw new SystemException(SystemException::EX_INVALIDPARAMETER, 'Parameter $keyName must be a string');
-
- $session = self::getInstance();
- if ($session->keyExists($keyName) === false)
- {
- $session->setValue($keyName, $object);
- return 0;
- }
- else
- {
- $object = HttpContext::getSession()->getValue($keyName);
- return 1;
- }
- }
- }
-
- /**
- * The singleton response to output headers
- *
- * @package WebCore
- * @subpackage Web
- */
- class HttpResponse extends ObjectBase implements ISingleton
- {
- /*
- @todo Add all the response codes to constants http://www.krisjordan.com/php-class-for-http-response-status-codes/
- */
- const HTTP_RESPONSE_CODE_404 = '404 Not Found';
- const HTTP_RESPONSE_CODE_403 = '403 Forbidden';
-
- /**
- * Represents the instance of the singleton object
- *
- * @var HttpResponse
- */
- private static $__instance = null;
-
- /**
- * Gets the singleton instance for this class.
- *
- * @return HttpResponse
- */
- public static function getInstance()
- {
- if (self::isLoaded() === false)
- self::$__instance = new HttpResponse();
-
- return self::$__instance;
- }
-
- /**
- * Determines whether the singleton has loaded its instance
- *
- * @return bool
- */
- public static function isLoaded()
- {
- if (is_null(self::$__instance))
- return false;
- return true;
- }
-
- /**
- * Writes content directly to the response's output stream.
- * Use the getMarkupWriter method to output HTML easily.
- */
- public static function write($content)
- {
- echo $content;
- }
-
- /**
- * Writes content directly to the response's output stream, appending both, a carriage return and a line feed character (CRLF).
- * Use the getMarkupWriter method to output HTML easily.
- */
- public static function writeLine($content)
- {
- echo $content . "\r\n";
- }
-
- /**
- * Ends the response immediately.
- * WARNING: If output buffering is on, you must call the outputBufferFlush method in order to produce some output.
- */
- public static function end()
- {
- exit(0);
- }
-
- /**
- * Gets the output markup writer.
- * @return MarkupWriter
- */
- public static function getMarkupWriter()
- {
- return HtmlWriter::getInstance();
- }
-
- /**
- * Redirects to the given URL
- * @param string $url
- * @param array $params
- */
- public static function redirect($url, $params = null)
- {
- $queryString = '';
- if (is_null($params) === false)
- {
- $queryString = '?';
- $paramNames = array_keys($params);
- foreach ($paramNames as $param)
- {
- $queryString .= $param . '=' . urlencode($params[$param]) . '&';
- }
- }
- self::appendHeader('Location', $url . $queryString);
- HttpResponse::end();
- }
-
- /**
- * Turns output buffering on.
- */
- public static function outputBufferStart()
- {
- ob_start();
- }
-
- /**
- * Flushes the output buffer; You must call the outputBufferstart function before you call this method.
- */
- public static function outputBufferFlush()
- {
- ob_end_flush();
- }
-
- /**
- * Gets the length in bytes of the output buffer.
- *
- * @return int
- */
- public static function outputBufferGetLength()
- {
- return ob_get_length();
- }
-
- /**
- * Clears the contents of the output buffer
- *
- */
- public static function clearOutputBuffer()
- {
- ob_end_clean();
- }
-
- /**
- * Returns the contents of the output buffer
- *
- * @return string
- */
- public static function getOutputBuffer()
- {
- return ob_get_contents();
- }
-
- /**
- * Adds a header to the response headers.
- *
- * @param string $headerName
- * @param string $headerValue
- */
- public static function appendHeader($headerName, $headerValue)
- {
- header($headerName . ': ' . $headerValue);
- }
-
- /**
- * Sets the response code for the Current HttpRequest
- *
- * @param string $responseCode. One of the HTTP_RESPONSE_CODE_* prefixes constants
- */
- public static function setResponseCode($responseCode)
- {
- header('HTTP/1.0 ' . $responseCode);
- }
-
- /**
- * Sets a Last-Modified header.
- * use strtotime function to obtain timestamps from ISO-compliant dates.
- * @param int $timestamp
- */
- public static function setLastModified($timestamp)
- {
- $timestampStr = gmdate("D, d M Y H:i:s", $timestamp);
- self::appendHeader('Expires', $timestampStr . ' GMT');
- }
-
- /**
- * Sets the Expires header given an offset in minutes.
- *
- * @param int $minutesFromNow The parameter can be a negative integer.
- */
- public static function setExpires($minutesFromNow = 0)
- {
- $offset = 60 * $minutesFromNow;
- $timestamp = gmdate("D, d M Y H:i:s", time() + $offset);
- self::appendHeader('Expires', $timestamp . ' GMT');
- }
-
- /**
- * Sets the Cache-Control and Expires header to control content cacheability.
- * If minutes is negative a no-cache, must-revalidate header is sent out. Otherwise a max-age value is sent out.
- *
- * @param int $minutes
- */
- public static function setCacheControl($minutes = 0)
- {
- if ($minutes <= 0)
- {
- self::appendHeader('Cache-Control', 'no-cache, must-revalidate');
- self::setExpires(times() - (60 * 24 * 365 * 10));
- }
- else
- {
- $seconds = $minutes * 60;
- self::appendHeader('Cache-Control', 'max-age=' . $seconds . ', must-revalidate');
- self::setExpires(time() + $seconds);
- }
- }
-
- /**
- * Sets the content-type header
- *
- * @param string $mimetype
- */
- public static function setContentType($mimetype = 'application/octet-stream')
- {
- self::appendHeader('Content-Type', $mimetype);
- }
-
- /**
- * Sets the content-disposition header.
- *
- * @param string $disposition Can be attachment or inline
- * @param string $filename If disposition is 'attachment' then you can specify a filename to download.
- */
- public static function setContentDisposition($disposition = 'attachment', $filename = '')
- {
- $headerValue = $disposition;
- if ($filename != '')
- $headerValue .= '; filename="' . $filename . '"';
-
- self::appendHeader('Content-Disposition', $headerValue);
- }
-
- /**
- * Sets the content-length header (in bytes).
- *
- * @param int $length
- */
- public static function setContentLength($length)
- {
- self::appendHeader('Content-Length', $length);
- }
-
- /**
- * Determines whether the headers have already been sent to the client.
- * @return bool
- */
- public static function hasSentHeaders()
- {
- return headers_sent();
- }
- }
-
- /**
- * The singleton request holding references to GET, POST, REQUEST and FILES variables.
- *
- * @package WebCore
- * @subpackage Web
- */
- class HttpRequest extends ObjectBase implements ISingleton
- {
- /**
- * Represents the instance of the singleton object
- *
- * @var HttpRequest
- */
- private static $__instance = null;
- /**
- * Gets a collection representing $_REQUEST variables.
- *
- * @var KeyedCollectionWrapper
- */
- private static $requestItems;
- /**
- * Gets a collection representing $_GET variables
- *
- * @var KeyedCollectionWrapper
- */
- private static $queryStringItems;
- /**
- * Gets a collection representing $_POST variables
- *
- * @var KeyedCollectionWrapper
- */
- private static $postedItems;
- /**
- * Gets a collection representing $_FILES variables
- *
- * @var KeyedCollection
- */
- private static $filesItems;
-
- /**
- * Gets the singleton instance for this class.
- *
- * @return HttpRequest
- */
- public static function getInstance()
- {
- if (self::isLoaded() === false)
- {
- self::$__instance = new HttpRequest();
- self::$requestItems = new KeyedCollectionWrapper($_REQUEST, true);
- self::$queryStringItems = new KeyedCollectionWrapper($_GET, true);
- self::$postedItems = new KeyedCollectionWrapper($_POST, true);
- self::$filesItems = new KeyedCollection();
- // Populate the files collection
- $postedFileKeys = array_keys($_FILES);
- foreach ($postedFileKeys as $fileKey)
- {
- $postedFile = new PostedFile($fileKey);
- self::$filesItems->setValue($fileKey, $postedFile);
- }
- }
- return self::$__instance;
- }
-
- /**
- * Determines whether the singleton has loaded its instance
- *
- * @return bool
- */
- public static function isLoaded()
- {
- if (is_null(self::$__instance))
- return false;
-
- return true;
- }
-
- /**
- * Gets a collection representing $_REQUEST variables (by reference)
- *
- * @return KeyedCollectionWrapper
- */
- public function &getRequestVars()
- {
- $vars = new KeyedCollection();
-
- foreach (self::$requestItems as $key => $value)
- {
- $decodedValue = utf8_decode($value);
- $vars->setValue($key, $decodedValue);
- }
-
- return $vars;
- }
-
- /**
- * Gets a collection representing $_POST variables (by reference)
- *
- * @return KeyedCollectionWrapper
- */
- public function &getPostVars()
- {
- return self::$postedItems;
- }
-
- /**
- * Gets a collection representing $_GET variables (by reference)
- *
- * @return KeyedCollectionWrapper
- */
- public function &getQueryStringVars()
- {
- return self::$queryStringItems;
- }
-
- /**
- * Gets a KeyedCollection containing items of type PostedFile
- * This is an enhanced version of the $_FILES array
- *
- * @return KeyedCollection
- */
- public function &getPostedFiles()
- {
- return self::$filesItems;
- }
- }
-
- /**
- * Wraps an item from the $_FILES array
- *
- * @package WebCore
- * @subpackage Web
- */
- class PostedFile extends ObjectBase
- {
- private $__fileReference;
-
- /**
- * Creates a new instance of this class
- *
- * @param string $filesKey
- */
- public function __construct($filesKey)
- {
- if (array_key_exists($filesKey, $_FILES) === false)
- throw new SystemException(SystemException::EX_INVALIDKEY, '$_FILES key ' . $filesKey . ' does not exist.');
-
- $this->__fileReference =& $_FILES[$filesKey];
- }
-
- /**
- * Gets the filename, without the path
- * Example: myfile.ext
- *
- * @return string
- */
- public function getFileName()
- {
- return basename($this->__fileReference['name']);
- }
-
- /**
- * Gets the filename, without the path or extension
- * Example: myfile
- *
- * @return string
- */
- public function getFileBaseName()
- {
- $fileName = $this->getFileName();
- $pathInfo = pathinfo($fileName);
- return $pathInfo['filename'];
- }
-
- /**
- * Gets the file extension of the uploaded file
- * Example: ext
- *
- * @return string
- */
- public function getFileExtension()
- {
- $fileName = $this->getFileName();
- $pathInfo = pathinfo($fileName);
- return $pathInfo['extension'];
- }
-
- /**
- * Gets the mime-type that the client set when the file was uploaded
- * Example: image/gif
- *
- * @return string
- */
- public function getMimeType()
- {
- return $this->__fileReference['type'];
- }
-
- /**
- * Gets the size, in bytes of the array.
- *
- * @return int
- */
- public function getSize()
- {
- return (int) $this->__fileReference['size'];
- }
-
- /**
- * Gets the full path in the server to the uploaded file
- *
- * @return string
- */
- public function getTempFileName()
- {
- return $this->__fileReference['tmp_name'];
- }
-
- /**
- * Gets whether file was uploaded and can be correcly found in the server.
- *
- * @return bool
- */
- public function isUploaded()
- {
- return is_uploaded_file($this->getTempFileName());
- }
-
- /**
- * Gets the entire contents of the uploaded filed
- *
- * @return string
- */
- public function readAll()
- {
- $pathFileName = $this->getTempFileName();
- return file_get_contents($pathFileName);
- }
-
- /**
- * Gets the error code of the file upload result
- *
- * @return int
- */
- public function getErrorCode()
- {
- return $this->__fileReference['error'];
- }
-
- /**
- * Gets a user-friendly message based on the error code
- *
- * @return string
- */
- public function getErrorMessage()
- {
- $errorCode = $this->getErrorCode();
- switch ($errorCode)
- {
- case UPLOAD_ERR_CANT_WRITE:
- return Resources::getValue(Resources::SRK_UPLOAD_ERR_CANT_WRITE);
- break;
- case UPLOAD_ERR_EXTENSION:
- return Resources::getValue(Resources::SRK_UPLOAD_ERR_EXTENSION);
- break;
- case UPLOAD_ERR_FORM_SIZE:
- return Resources::getValue(Resources::SRK_UPLOAD_ERR_FORM_SIZE);
- break;
- case UPLOAD_ERR_INI_SIZE:
- return Resources::getValue(Resources::SRK_UPLOAD_ERR_INI_SIZE);
- break;
- case UPLOAD_ERR_NO_FILE:
- return Resources::getValue(Resources::SRK_UPLOAD_ERR_NO_FILE);
- break;
- case UPLOAD_ERR_NO_TMP_DIR:
- return Resources::getValue(Resources::SRK_UPLOAD_ERR_NO_TMP_DIR);
- break;
- case UPLOAD_ERR_OK:
- return Resources::getValue(Resources::SRK_UPLOAD_ERR_OK);
- break;
- case UPLOAD_ERR_PARTIAL:
- return Resources::getValue(Resources::SRK_UPLOAD_ERR_PARTIAL);
- break;
- default:
- return Resources::getValue(Resources::SRK_UPLOAD_ERR_CANT_WRITE);
- break;
- }
-
- return StaticResources::getMessagePostedFileErrorCode($this->getErrorCode());
- }
- }
-
- /**
- * Represents a typed, keyed collection wrapping the $_SERVER PHP array
- *
- * @package WebCore
- * @subpackage Web
- */
- class HttpContextInfo extends KeyedCollectionBase implements ISingleton
- {
- const CLIENT_DESKTOP = "desktop";
- const CLIENT_MOBILE = "mobile";
- const CLIENT_IPHONE = "iphone";
- const CLIENT_BB = "blackberry";
- const CLIENT_BOT = "bot";
-
- const INFO_HTTP_USER_AGENT = 'HTTP_USER_AGENT';
- const INFO_REMOTE_ADDR = 'REMOTE_ADDR';
- const INFO_REMOTE_PORT = 'REMOTE_PORT';
- const INFO_HTTP_ACCEPT = 'HTTP_ACCEPT';
- const INFO_HTTP_ACCEPT_ENCODING = 'HTTP_ACCEPT_ENCODING';
- const INFO_HTTP_ACCEPT_LANGUAGE = 'HTTP_ACCEPT_LANGUAGE';
- const INFO_GATEWAY_INTERFACE = 'GATEWAY_INTERFACE';
- const INFO_SERVER_ADDR = 'SERVER_ADDR';
- const INFO_SERVER_PORT = 'SERVER_PORT';
- const INFO_DOCUMENT_ROOT = 'DOCUMENT_ROOT';
- const INFO_SERVER_NAME = 'SERVER_NAME';
- const INFO_SERVER_SOFTWARE = 'SERVER_SOFTWARE';
- const INFO_SERVER_PROTOCOL = 'SERVER_PROTOCOL';
- const INFO_REQUEST_URI = 'REQUEST_URI';
- const INFO_QUERY_STRING = 'QUERY_STRING';
- const INFO_REQUEST_METHOD = 'REQUEST_METHOD';
- const INFO_REQUEST_TIME = 'REQUEST_TIME';
- const INFO_HTTP_HOST = 'HTTP_HOST';
- const INFO_SCRIPT_NAME = 'SCRIPT_NAME';
- const INFO_HTTP_CONNECTION = 'HTTP_CONNECTION';
- const INFO_PHP_SELF = 'PHP_SELF';
- const INFO_SCRIPT_FILENAME = 'SCRIPT_FILENAME';
-
- private static $clientType;
-
- protected function __construct()
- {
- parent::__construct($_SERVER, true);
- }
-
- /**
- * Represents the instance of the singleton object
- *
- * @var HttpContextInfo
- */
- private static $__instance = null;
-
- /**
- * Gets the singleton instance for this class.
- *
- * @return HttpContextInfo
- */
- public static function getInstance()
- {
- if (self::isLoaded() === false)
- {
- self::$__instance = new HttpContextInfo();
- self::$__instance->detectClientType();
- }
- return self::$__instance;
- }
-
- /**
- * Determines whether the singleton has loaded its instance
- *
- * @return bool
- */
- public static function isLoaded()
- {
- if (is_null(self::$__instance))
- return false;
-
- return true;
- }
-
- /**
- * Returns client's type
- *
- * @return string
- */
- public function getClientType()
- {
- return self::$clientType;
- }
-
- /**
- * Detects client type using User-Agent
- *
- */
- public function detectClientType()
- {
- self::$clientType = self::CLIENT_DESKTOP;
- $agent = self::getInstance()->getClientAgent();
-
- $iphoneAgents = array(
- "iPhone",
- "iPod"
- );
-
- foreach ($iphoneAgents as $key)
- {
- if (preg_match("/$key/i", $agent))
- {
- self::$clientType = self::CLIENT_IPHONE;
- return;
- }
- }
-
- $botAgents = array(
- "Teoma",
- "alexa",
- "MSNBot",
- "inktomi",
- "looksmart",
- "Firefly",
- "NationalDirectory",
- "Ask Jeeves",
- "TECNOSEEK",
- "InfoSeek",
- "WebFindBot",
- "girafabot",
- "crawler",
- "www.galaxy.com",
- "Googlebot",
- "Scooter",
- "Slurp",
- "Yammybot",
- "WebBug",
- "Spade",
- "ZyBorg"
- );
-
- foreach ($botAgents as $key)
- {
- if (preg_match("/$key/i", $agent))
- {
- self::$clientType = self::CLIENT_BOT;
- return;
- }
- }
-
- $mobileAgents = array(
- "BlackBerry",
- "Opera Mini",
- "Windows CE",
- "Symbian"
- );
-
- foreach ($mobileAgents as $key)
- {
- if (preg_match("/$key/i", $agent))
- {
- if ($key == "BlackBerry")
- self::$clientType = self::CLIENT_BB;
- else
- self::$clientType = self::CLIENT_MOBILE;
-
- return;
- }
- }
- }
-
- /**
- * Returns an info value from the $_SERVER array
- *
- * @return string
- */
- protected static function getInfoValue($key)
- {
- return (self::getInstance()->keyExists($key) === true) ? self::getInstance()->getValue($key) : "";
- }
-
- /**
- * Gets the client's browser name and version (if set in the request)
- *
- * @return string
- */
- public function getClientAgent()
- {
- return self::getInfoValue(self::INFO_HTTP_USER_AGENT);
- }
-
- /**
- * Returns an array with client agent information such as name and version
- * @return array
- */
- public function getClientAgentInfo()
- {
- return get_browser(null, true);
- }
-
- /**
- * Gets the IP Address of the current request.
- *
- * @return string
- */
- public function getClientIPAddress()
- {
- return self::getInfoValue(self::INFO_REMOTE_ADDR);
- }
- /**
- * Gets the TCP/IP port the client used to send the request
- *
- * @return int
- */
- public function getClientPort()
- {
- return self::getInfoValue(self::INFO_REMOTE_PORT);
-
- }
- /**
- * Gets the comma-delimited mimetypes that the client accepts.
- *
- * @return string
- */
- public function getClientAccept()
- {
- return self::getInfoValue(self::INFO_HTTP_ACCEPT);
-
- }
- /**
- * Gets the comma-delimieted supported stream formats that the client supports.
- * For example, gzip, deflate
- *
- * @return string
- */
- public function getClientAcceptEncoding()
- {
- return self::getInfoValue(self::INFO_HTTP_ACCEPT_ENCODING);
-
- }
- /**
- * Gets the client's language and culture code
- * For example, en-us or es-mx
- * @return string
- */
- public function getClientAcceptLanguage()
- {
- return self::getInfoValue(self::INFO_HTTP_ACCEPT_LANGUAGE);
-
- }
-
- /**
- * Gets the version of the Common Gateway Interface the server is using.
- * For example, CGI/1.1
- *
- * @return string
- */
- public function getServerCGI()
- {
- return self::getInfoValue(self::INFO_GATEWAY_INTERFACE);
-
- }
- /**
- * Gets the server's IP address serving the response
- *
- * @return string
- */
- public function getServerIPAddress()
- {
- return self::getInfoValue(self::INFO_SERVER_ADDR);
- }
- /**
- * Gets the TCP/IP port on which the response is returned by the server
- *
- * @return string
- */
- public function getServerPort()
- {
- return self::getInfoValue(self::INFO_SERVER_PORT);
-
- }
- /**
- * Gets the root path in the server's file system from which all
- * documents are processed and served.
- *
- * @return string
- */
- public function getServerFileSystemWebRootPath()
- {
- return self::getInfoValue(self::INFO_DOCUMENT_ROOT);
-
- }
- /**
- * Gets the name of the server.
- * For example, localhost
- *
- * @return string
- */
- public function getServerAddress()
- {
- return self::getInfoValue(self::INFO_SERVER_NAME);
-
- }
- /**
- * Gets the name and version of the software used to process requests and responses.
- *
- * @return string
- */
- public function getServerSoftware()
- {
- return self::getInfoValue(self::INFO_SERVER_SOFTWARE);
-
- }
- /**
- * Gets the protocol the server is using to process requests and serve response.
- * For example, HTTP/1.1
- *
- * @return string
- */
- public function getServerProtocol()
- {
- return self::getInfoValue(self::INFO_SERVER_PROTOCOL);
-
- }
-
- /**
- * Gets the full Uniform Resource identifier of the request.
- * For example, http://localhost/test/test.php?id=6
- *
- * @return string
- */
- public function getRequestUri()
- {
- return self::getInfoValue(self::INFO_REQUEST_URI);
-
- }
- /**
- * Gets the query string that appears after the script's name
- * For example, id=6
- *
- * @return string
- */
- public function getRequestQueryString()
- {
- return self::getInfoValue(self::INFO_QUERY_STRING);
-
- }
-
- /**
- * Gets the REST method for the request.
- * For example, POST
- *
- * @return string
- */
- public function getRequestMethod()
- {
- return StringHelper::toUpper(self::getInfoValue(self::INFO_REQUEST_METHOD));
-
- }
- /**
- * Gets the timestamp at which the request was made.
- *
- * @return int
- */
- public function getRequestStartTime()
- {
- return self::getInfoValue(self::INFO_REQUEST_TIME);
-
- }
- /**
- * Gets the hostname and port at which the request is directed
- * For example, localhost:88
- *
- * @return string
- */
- public function getRequestHost()
- {
- return self::getInfoValue(self::INFO_HTTP_HOST);
-
- }
- /**
- * Gets the virtual path to the resource at which the request was created.
- * For example, /test/testing.php
- *
- * @return string
- */
- public function getRequestScriptPath()
- {
- return self::getInfoValue(self::INFO_SCRIPT_NAME);
-
- }
-
- /**
- * Gets the connection mode the client requests.
- * For example, Keep-Alive
- *
- * @return string
- */
- public function getRequestConnectionMode()
- {
- return self::getInfoValue(self::INFO_HTTP_CONNECTION);
-
- }
-
- /**
- * Gets the full path to the executing script in the file system.
- * For example: c:\InetPub\wwwroot\test\testing.php
- *
- * @return string
- */
- public function getScriptFileSystemPath()
- {
- return self::getInfoValue(self::INFO_SCRIPT_FILENAME);
-
- }
-
- /**
- * Gets the full virtual path to the executing script.
- * for example: /test/testing.php
- *
- * @return string
- */
- public function getScriptVirtualPath()
- {
- return self::getInfoValue(self::INFO_PHP_SELF);
-
- }
-
- /**
- * Creates a default instance of this class
- *
- * @return HttpContextInfo
- */
- public static function createInstance()
- {
- return self::getInstance();
- }
- }
-
- /**
- * Represents a user navigation entry holding significant request information.
- * @package WebCore
- * @subpackage Web
- */
- class NavigationEntry extends SerializableObjectBase
- {
- protected $postedVars;
- protected $queryVars;
- protected $requestUrl;
- protected $requestTime;
- protected $hadPostedFiles;
- protected $requestMethod;
- protected $scriptVirtualPath;
-
- /**
…
Large files files are truncated, but you can click here to view the full file