* @version 1.0 */ // Message Types define('MSG_SUCCESS', 0); define('MSG_NOTICE', 1); define('MSG_WARNING', 2); define('MSG_ERR', 3); // PHP user error style. define('MSG_ERROR', 3); require_once dirname(__FILE__) . '/Utilities.inc.php'; class App { // Name of this application. var $app = '_app_'; // If App::start has run successfully. var $running = false; // Instance of database object. var $db; // Hash of global application parameters. var $_params = array(); // Default parameters. var $_param_defaults = array( // Public name and email address for this application. 'site_name' => null, 'site_email' => null, // The location the user will go if the system doesn't knew where else to send them. 'redirect_home_url' => '/', // SSL URL used when redirecting with App::sslOn(). 'ssl_domain' => null, 'ssl_enabled' => false, // Character set for page output. Set in the Content-Type header and an HTML tag. 'character_set' => 'utf-8', // Human-readable format used to display dates. 'date_format' => 'd M Y', 'sql_date_format' => '%e %b %Y', 'sql_time_format' => '%k:%i', // Use php sessions? 'enable_session' => false, 'session_name' => 'Strangecode', 'session_use_cookies' => true, // Use database? 'enable_db' => false, // Use db-based sessions? 'enable_db_session_handler' => false, // DB passwords should be set as apache environment variables in httpd.conf, readable only by root. 'db_server' => 'localhost', 'db_name' => null, 'db_user' => null, 'db_pass' => null, // Database debugging. 'db_always_debug' => false, // TRUE = display all SQL queries. 'db_debug' => false, // TRUE = display db errors. 'db_die_on_failure' => false, // TRUE = script stops on db error. // For classes that require db tables, do we check that a table exists and create if missing? 'db_create_tables' => null, // The level of error reporting. Don't set this to 0 to suppress messages, instead use display_errors to control display. 'error_reporting' => E_ALL, // Don't display errors by default; it is preferable to log them to a file. 'display_errors' => false, // Directory in which to store log files. 'log_directory' => null, // PHP error log. 'php_error_log' => 'php_error_log', // General application log. 'log_filename' => 'app_error_log', // Logging priority can be any of the following, or false to deactivate: // LOG_EMERG system is unusable // LOG_ALERT action must be taken immediately // LOG_CRIT critical conditions // LOG_ERR error conditions // LOG_WARNING warning conditions // LOG_NOTICE normal, but significant, condition // LOG_INFO informational message // LOG_DEBUG debug-level message 'log_file_priority' => false, 'log_email_priority' => false, 'log_sms_priority' => false, 'log_screen_priority' => false, // Email address to receive log event emails. 'log_to_email_address' => null, // SMS Email address to receive log event SMS messages 'log_to_sms_address' => null, // A key for calculating simple cryptographic signatures. Set using as an environment variables in the httpd.conf with 'SetEnv SIGNING_KEY ' 'signing_key' => 'aae6abd6209d82a691a9f96384a7634a', ); /** * This method enforces the singleton pattern for this class. Only one application is running at a time. * * @return object Reference to the global SessionCache object. * @access public * @static */ function &getInstance($app=null) { static $instance = null; if ($instance === null) { $instance = new App($app); } return $instance; } /** * Constructor. */ function App($app=null) { if (isset($app)) { $this->app .= $app; } if (!isset($_SESSION[$this->app])) { $_SESSION[$this->app] = array(); } // Initialize default parameters. $this->_params = array_merge($this->_params, $this->_param_defaults); } /** * Set (or overwrite existing) parameters by passing an array of new parameters. * * @access public * @param array $param Array of parameters (key => val pairs). */ function setParam($param=null) { if (!isset($this) || !is_a($this, 'App')) { $this =& App::getInstance(); } if (isset($param) && is_array($param)) { // Merge new parameters with old overriding only those passed. $this->_params = array_merge($this->_params, $param); } } /** * Return the value of a parameter. * * @access public * @param string $param The key of the parameter to return. * @return mixed Parameter value, or null if not existing. */ function &getParam($param=null) { if (!isset($this) || !is_a($this, 'App')) { $this =& App::getInstance(); } if ($param === null) { return $this->_params; } else if (isset($this->_params[$param])) { return $this->_params[$param]; } else { App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); return null; } } /** * Begin running this application. * * @access public * @author Quinn Comendant * @since 15 Jul 2005 00:32:21 */ function start() { if ($this->running) { return false; } // Error reporting. ini_set('error_reporting', $this->getParam('error_reporting')); ini_set('display_errors', $this->getParam('display_errors')); ini_set('log_errors', true); if (is_dir($this->getParam('log_directory')) && is_writable($this->getParam('log_directory'))) { ini_set('error_log', $this->getParam('log_directory') . '/' . $this->getParam('php_error_log')); } /** * 1. Start Database. */ if ($this->getParam('enable_db')) { // DB connection parameters taken from environment variables in the httpd.conf file, readable only by root. if (!empty($_SERVER['DB_SERVER'])) { $this->setParam(array('db_server' => $_SERVER['DB_SERVER'])); } if (!empty($_SERVER['DB_NAME'])) { $this->setParam(array('db_name' => $_SERVER['DB_NAME'])); } if (!empty($_SERVER['DB_USER'])) { $this->setParam(array('db_user' => $_SERVER['DB_USER'])); } if (!empty($_SERVER['DB_PASS'])) { $this->setParam(array('db_pass' => $_SERVER['DB_PASS'])); } // The only instance of the DB object. require_once dirname(__FILE__) . '/DB.inc.php'; $this->db =& DB::getInstance(); $this->db->setParam(array( 'db_server' => $this->getParam('db_server'), 'db_name' => $this->getParam('db_name'), 'db_user' => $this->getParam('db_user'), 'db_pass' => $this->getParam('db_pass'), 'db_always_debug' => $this->getParam('db_always_debug'), 'db_debug' => $this->getParam('db_debug'), 'db_die_on_failure' => $this->getParam('db_die_on_failure'), )); // Connect to database. $this->db->connect(); } /** * 2. Start PHP session. */ // Skip session for some user agents. if (preg_match('/Atomz|ApacheBench|Wget/i', getenv('HTTP_USER_AGENT'))) { $this->setParam(array('enable_session' => false)); } if (true === $this->getParam('enable_session')) { // Set the session ID to one provided in GET/POST. This is necessary for linking // between domains and keeping the same session. if ($ses = getFormData($this->getParam('session_name'), false)) { session_id($ses); } if (true === $this->getParam('enable_db_session_handler') && true === $this->getParam('enable_db')) { // Database session handling. require_once dirname(__FILE__) . '/DBSessionHandler.inc.php'; $db_save_handler = new DBSessionHandler($this->db, array( 'db_table' => 'session_tbl', 'create_table' => $this->getParam('db_create_tables'), )); } // Session parameters. ini_set('session.use_cookies', $this->getParam('session_use_cookies')); ini_set('session.use_trans_sid', false); ini_set('session.entropy_file', '/dev/urandom'); ini_set('session.entropy_length', '512'); session_name($this->getParam('session_name')); // Start the session. Access session data using: $_SESSION['...'] session_start(); } /** * 3. Misc setup. */ // Script URI will be something like http://host.name.tld (no ending slash) // and is used whenever a URL need be used to the current site. // Not available on cli scripts obviously. if (isset($_SERVER['SCRIPT_URI']) && '' != $_SERVER['SCRIPT_URI']) { $site_url = parse_url($_SERVER['SCRIPT_URI']); $this->setParam(array('site_url' => sprintf('%s://%s', $site_url['scheme'], $site_url['host']))); } // A key for calculating simple cryptographic signatures. if (isset($_SERVER['SIGNING_KEY'])) { $this->setParam(array('signing_key' => $_SERVER['SIGNING_KEY'])); } // Used as the fifth parameter to mail() to set the return address for sent messages. Requires safe_mode off. if ('' != $this->getParam('site_email') && !$this->getParam('envelope_sender_address')) { $this->setParam(array('envelope_sender_address' => '-f ' . $this->getParam('site_email'))); } // Character set. This should also be printed in the html header template. header('Content-type: text/html; charset=' . $this->getParam('character_set')); $this->running = true; } /** * Stop running this application. * * @access public * @author Quinn Comendant * @since 17 Jul 2005 17:20:18 */ function stop() { session_write_close(); $this->db->close(); restore_include_path(); $this->running = false; } /** * Add a message to the string globalmessage, which is printed in the header. * Just a simple way to print messages to the user. * * @access public * * @param string $message The text description of the message. * @param int $type The type of message: MSG_NOTICE, * MSG_SUCCESS, MSG_WARNING, or MSG_ERR. * @param string $file __FILE__. * @param string $line __LINE__. */ function raiseMsg($message, $type=MSG_NOTICE, $file=null, $line=null) { if (!isset($this) || !is_a($this, 'App')) { $this =& App::getInstance(); } if (!$this->running) { return false; } $_SESSION[$this->app]['messages'][] = array( 'type' => $type, 'message' => $message, 'file' => $file, 'line' => $line ); if (!in_array($type, array(MSG_NOTICE, MSG_SUCCESS, MSG_WARNING, MSG_ERR))) { App::logMsg(sprintf('Invalid MSG_* type: %s', $type), LOG_DEBUG, __FILE__, __LINE__); } } /** * Prints the HTML for displaying raised messages. * * @access public * @author Quinn Comendant * @since 15 Jul 2005 01:39:14 */ function printRaisedMessages() { if (!isset($this) || !is_a($this, 'App')) { $this =& App::getInstance(); } if (!$this->running) { return false; } while (isset($_SESSION[$this->app]['messages']) && $message = array_shift($_SESSION[$this->app]['messages'])) { ?>
0 && $this->getParam('display_errors')) { echo "\n'; } switch ($message['type']) { case MSG_ERR: echo '
' . $message['message'] . '
'; break; case MSG_WARNING: echo '
' . $message['message'] . '
'; break; case MSG_SUCCESS: echo '
' . $message['message'] . '
'; break; case MSG_NOTICE: default: echo '
' . $message['message'] . '
'; break; } ?>
running) { return false; } // If priority is not specified, assume the worst. if (!$this->logPriorityToString($priority)) { $this->logMsg(sprintf('Log priority %s not defined. (Message: %s)', $priority, $message), LOG_EMERG, $file, $line); $priority = LOG_EMERG; } // If log file is not specified, create one in the codebase root. if ($this->getParam('log_directory') === null || !is_dir($this->getParam('log_directory')) || !is_writable($this->getParam('log_directory'))) { // If log file is not specified, don't log to a file. $this->setParam(array('log_file_priority' => false)); // We must use trigger_error rather than calling App::logMsg, which might lead to an infinite loop. trigger_error(sprintf('Codebase error: log directory (%s) not found or writable.', $this->getParam('log_directory')), E_USER_NOTICE); } // Make sure to log in the system's locale. $locale = setlocale(LC_TIME, 0); setlocale(LC_TIME, 'C'); // Data to be stored for a log event. $event = array(); $event['date'] = date('Y-m-d H:i:s'); $event['remote ip'] = getRemoteAddr(); if (substr(PHP_OS, 0, 3) != 'WIN') { $event['pid'] = posix_getpid(); } $event['type'] = $this->logPriorityToString($priority); $event['file:line'] = "$file : $line"; $event['message'] = strip_tags(preg_replace('/\s{2,}/', ' ', $message)); $event_str = '[' . join('] [', $event) . ']'; // FILE ACTION if ($this->getParam('log_file_priority') && $priority <= $this->getParam('log_file_priority')) { error_log($event_str . "\n", 3, $this->getParam('log_directory') . '/' . $this->getParam('log_filename')); } // EMAIL ACTION if ($this->getParam('log_email_priority') && $priority <= $this->getParam('log_email_priority')) { $subject = sprintf('[%s %s] %s', getenv('HTTP_HOST'), $event['type'], $message); $email_msg = sprintf("A %s log event occured on %s\n\n", $event['type'], getenv('HTTP_HOST')); $headers = "From: codebase@strangecode.com\r\n"; foreach ($event as $k=>$v) { $email_msg .= sprintf("%-11s%s\n", $k, $v); } mail($this->getParam('log_to_email_address'), $subject, $email_msg, $headers, '-f codebase@strangecode.com'); } // SMS ACTION if ($this->getParam('log_sms_priority') && $priority <= $this->getParam('log_sms_priority')) { $subject = sprintf('[%s %s]', getenv('HTTP_HOST'), $priority); $sms_msg = sprintf('%s:%s %s', basename($file), $line, $event['message']); $headers = "From: codebase@strangecode.com\r\n"; mail($this->getParam('log_to_sms_address'), $subject, $sms_msg, $headers, '-f codebase@strangecode.com'); } // SCREEN ACTION if ($this->getParam('log_screen_priority') && $priority <= $this->getParam('log_screen_priority')) { echo "[{$event['date']}] [{$event['type']}] [{$event['file:line']}] [{$event['message']}]\n"; } // Restore original locale. setlocale(LC_TIME, $locale); } /** * Returns the string representation of a LOG_* integer constant. * * @param int $priority The LOG_* integer constant. * * @return The string representation of $priority. */ function logPriorityToString ($priority) { $priorities = array( LOG_EMERG => 'emergency', LOG_ALERT => 'alert', LOG_CRIT => 'critical', LOG_ERR => 'error', LOG_WARNING => 'warning', LOG_NOTICE => 'notice', LOG_INFO => 'info', LOG_DEBUG => 'debug' ); if (isset($priorities[$priority])) { return $priorities[$priority]; } else { return false; } } /** * Outputs a fully qualified URL with a query of all the used (ie: not empty) * keys and values, including optional queries. This allows simple printing of * links without needing to know which queries to add to it. If cookies are not * used, the session id will be propogated in the URL. * * @global string $carry_queries An array of keys to define which values to * carry through from the POST or GET. * $carry_queries = array('qry'); for example. * * @param string $url The initial url * @param mixed $carry_args Additional url arguments to carry in the query, * or FALSE to prevent carrying queries. Can be any of the following formats: * -array('key1', key2', key3') <-- to save these keys if in the form data. * -array('key1'=>'value', key2'='value') <-- to set keys to default values if not present in form data. * -false <-- To not carry any queries. If URL already has queries those will be retained. * * @param mixed $always_include_sid Always add the session id, even if using_trans_sid = true. This is required when * URL starts with http, since PHP using_trans_sid doesn't do those and also for * header('Location...') redirections. * * @return string url with attached queries and, if not using cookies, the session id */ function oHREF($url='', $carry_args=null, $always_include_sid=false) { if (!isset($this) || !is_a($this, 'App')) { $this =& App::getInstance(); } if (!$this->running) { return false; } static $_using_trans_sid; global $carry_queries; // Save the trans_sid setting. if (!isset($_using_trans_sid)) { $_using_trans_sid = ini_get('session.use_trans_sid'); } // Initialize the carried queries. if (!isset($carry_queries['_carry_queries_init'])) { if (!is_array($carry_queries)) { $carry_queries = array($carry_queries); } $tmp = $carry_queries; $carry_queries = array(); foreach ($tmp as $key) { if (!empty($key) && getFormData($key, false)) { $carry_queries[$key] = getFormData($key); } } $carry_queries['_carry_queries_init'] = true; } // Get any additional query arguments to add to the $carry_queries array. // If FALSE is a function argument, DO NOT carry the queries. $do_carry_queries = true; $one_time_carry_queries = array(); if (!is_null($carry_args)) { if (is_array($carry_args) && !empty($carry_args)) { foreach ($carry_args as $key=>$arg) { // Get query from appropriate source. if (false === $arg) { $do_carry_queries = false; } else if (false !== getFormData($arg, false)) { $one_time_carry_queries[$arg] = getFormData($arg); // Set arg to form data if available. } else if (!is_numeric($key) && '' != $arg) { $one_time_carry_queries[$key] = getFormData($key, $arg); // Set to arg to default if specified (overwritten by form data). } } } else if (false !== getFormData($carry_args, false)) { $one_time_carry_queries[$carry_args] = getFormData($carry_args); } else if (false === $carry_args) { $do_carry_queries = false; } } // Get the first delimiter that is needed in the url. $delim = preg_match('/\?/', $url) ? ini_get('arg_separator.output') : '?'; $q = ''; if ($do_carry_queries) { // Join the perm and temp carry_queries and filter out the _carry_queries_init element for the final query args. $query_args = array_diff_assoc(urlEncodeArray(array_merge($carry_queries, $one_time_carry_queries)), array('_carry_queries_init' => true)); foreach ($query_args as $key=>$val) { // Check value is set and value does not already exist in the url. if (!preg_match('/[?&]' . preg_quote($key) . '=/', $url)) { $q .= $delim . $key . '=' . $val; $delim = ini_get('arg_separator.output'); } } } // Include the necessary SID if the following is true: // - no cookie in http request OR cookies disabled in App // - sessions are enabled // - the link stays on our site // - transparent SID propogation with session.use_trans_sid is not being used OR url begins with protocol (using_trans_sid has no effect here) // OR // - we must include the SID because we say so (it's used in a context where cookies will not be effective, ie. moving from http to https) // AND // - the SID is not already in the query. if ( ( ( ( !isset($_COOKIE[session_name()]) || !$this->getParam('session_use_cookies') ) && $this->getParam('enable_session') && isMyDomain($url) && ( !$_using_trans_sid || preg_match('!^(http|https)://!i', $url) ) ) || $always_include_sid ) && !preg_match('/[?&]' . preg_quote(session_name()) . '=/', $url) ) { $url .= $q . $delim . session_name() . '=' . session_id(); return $url; } else { $url .= $q; return $url; } } /** * Prints a hidden form element with the PHPSESSID when cookies are not used, as well * as hidden form elements for GET_VARS that might be in use. * * @global string $carry_queries An array of keys to define which values to * carry through from the POST or GET. * $carry_queries = array('qry'); for example * * @param mixed $carry_args Additional url arguments to carry in the query, * or FALSE to prevent carrying queries. Can be any of the following formats: * -array('key1', key2', key3') <-- to save these keys if in the form data. * -array('key1'=>'value', key2'='value') <-- to set keys to default values if not present in form data. * -false <-- To not carry any queries. If URL already has queries those will be retained. */ function printHiddenSession($carry_args=null) { if (!isset($this) || !is_a($this, 'App')) { $this =& App::getInstance(); } if (!$this->running) { return false; } static $_using_trans_sid; global $carry_queries; // Save the trans_sid setting. if (!isset($_using_trans_sid)) { $_using_trans_sid = ini_get('session.use_trans_sid'); } // Initialize the carried queries. if (!isset($carry_queries['_carry_queries_init'])) { if (!is_array($carry_queries)) { $carry_queries = array($carry_queries); } $tmp = $carry_queries; $carry_queries = array(); foreach ($tmp as $key) { if (!empty($key) && getFormData($key, false)) { $carry_queries[$key] = getFormData($key); } } $carry_queries['_carry_queries_init'] = true; } // Get any additional query names to add to the $carry_queries array // that are found as function arguments. // If FALSE is a function argument, DO NOT carry the queries. $do_carry_queries = true; $one_time_carry_queries = array(); if (!is_null($carry_args)) { if (is_array($carry_args) && !empty($carry_args)) { foreach ($carry_args as $key=>$arg) { // Get query from appropriate source. if (false === $arg) { $do_carry_queries = false; } else if (false !== getFormData($arg, false)) { $one_time_carry_queries[$arg] = getFormData($arg); // Set arg to form data if available. } else if (!is_numeric($key) && '' != $arg) { $one_time_carry_queries[$key] = getFormData($key, $arg); // Set to arg to default if specified (overwritten by form data). } } } else if (false !== getFormData($carry_args, false)) { $one_time_carry_queries[$carry_args] = getFormData($carry_args); } else if (false === $carry_args) { $do_carry_queries = false; } } // For each existing POST value, we create a hidden input to carry it through a form. if ($do_carry_queries) { // Join the perm and temp carry_queries and filter out the _carry_queries_init element for the final query args. $query_args = array_diff_assoc(urlEncodeArray(array_merge($carry_queries, $one_time_carry_queries)), array('_carry_queries_init' => true)); foreach ($query_args as $key=>$val) { echo ''; } } // Include the SID if cookies are disabled. if (!isset($_COOKIE[session_name()]) && !$_using_trans_sid) { echo ''; } } /** * Uses an http header to redirect the client to the given $url. If sessions are not used * and the session is not already defined in the given $url, the SID is appended as a URI query. * As with all header generating functions, make sure this is called before any other output. * * @param string $url The URL the client will be redirected to. * @param mixed $carry_args Additional url arguments to carry in the query, * or FALSE to prevent carrying queries. Can be any of the following formats: * -array('key1', key2', key3') <-- to save these keys if in the form data. * -array('key1'=>'value', key2'='value') <-- to set keys to default values if not present in form data. * -false <-- To not carry any queries. If URL already has queries those will be retained. * @param bool $always_include_sid Force session id to be added to Location header. */ function dieURL($url, $carry_args=null, $always_include_sid=false) { if (!isset($this) || !is_a($this, 'App')) { $this =& App::getInstance(); } if (!$this->running) { return false; } if ('' == $url) { // If URL is not specified, use the redirect_home_url. $url = $this->getParam('redirect_home_url'); } if (preg_match('!^/!', $url)) { // If relative URL is given, prepend correct local hostname. $my_url = parse_url($_SERVER['SCRIPT_URI']); $url = sprintf('%s://%s%s', $my_url['scheme'], $my_url['host'], $url); } $url = $this->oHREF($url, $carry_args, $always_include_sid); header(sprintf('Location: %s', $url)); $this->logMsg(sprintf('dieURL: %s', $url), LOG_DEBUG, __FILE__, __LINE__); // End this application. // Recommended, although I'm not sure it's necessary: http://cn2.php.net/session_write_close $this->stop(); die; } /** * Redirects a user by calling the App::dieURL(). It will use: * 1. the stored boomerang URL, it it exists * 2. the referring URL, it it exists. * 3. an empty string, which will force App::dieURL to use the default URL. */ function dieBoomerangURL($id=null, $carry_args=null) { if (!isset($this) || !is_a($this, 'App')) { $this =& App::getInstance(); } if (!$this->running) { return false; } // Get URL from stored boomerang. Allow non specific URL if ID not valid. if ($this->validBoomerangURL($id, true)) { if (isset($id) && isset($_SESSION[$this->app]['boomerang']['url'][$id])) { $url = $_SESSION[$this->app]['boomerang']['url'][$id]; } else { $url = end($_SESSION[$this->app]['boomerang']['url']); } } else if (!refererIsMe() && !preg_match('/admin_common/', getenv('SCRIPT_NAME'))) { // Ensure that the redirecting page is not also the referrer. // admin_common is an alias of 'admin', which confuses this function. Just here for local testing. $url = getenv('HTTP_REFERER'); } else { $url = ''; } $this->logMsg(sprintf('dieBoomerangURL: %s', $url), LOG_DEBUG, __FILE__, __LINE__); // Delete stored boomerang. $this->deleteBoomerangURL($id); // A redirection will never happen immediatly twice. // Set the time so ensure this doesn't happen. $_SESSION[$this->app]['boomerang']['time'] = time(); $this->dieURL($url, $carry_args); } /** * Set the URL to return to when App::dieBoomerangURL() is called. * * @param string $url A fully validated URL. * @param bool $id An identification tag for this url. * FIXME: url garbage collection? */ function setBoomerangURL($url=null, $id=null) { if (!isset($this) || !is_a($this, 'App')) { $this =& App::getInstance(); } if (!$this->running) { return false; } // A redirection will never happen immediatly after setting the boomerangURL. // Set the time so ensure this doesn't happen. See App::validBoomerangURL for more. if (isset($url) && is_string($url)) { // Delete any boomerang request keys in the query string. $url = preg_replace('/boomerang=[\w]+/', '', $url); if (is_array($_SESSION[$this->app]['boomerang']['url']) && !empty($_SESSION[$this->app]['boomerang']['url'])) { // If the URL currently exists in the boomerang array, delete. while ($existing_key = array_search($url, $_SESSION[$this->app]['boomerang']['url'])) { unset($_SESSION[$this->app]['boomerang']['url'][$existing_key]); } } if (isset($id)) { $_SESSION[$this->app]['boomerang']['url'][$id] = $url; } else { $_SESSION[$this->app]['boomerang']['url'][] = $url; } $this->logMsg(sprintf('setBoomerangURL: %s', $url), LOG_DEBUG, __FILE__, __LINE__); return true; } else { return false; } } /** * Return the URL set for the specified $id. * * @param string $id An identification tag for this url. */ function getBoomerangURL($id=null) { if (!isset($this) || !is_a($this, 'App')) { $this =& App::getInstance(); } if (!$this->running) { return false; } if (isset($id)) { if (isset($_SESSION[$this->app]['boomerang']['url'][$id])) { return $_SESSION[$this->app]['boomerang']['url'][$id]; } else { return ''; } } else if (is_array($_SESSION[$this->app]['boomerang']['url'])) { return end($_SESSION[$this->app]['boomerang']['url']); } else { return false; } } /** * Delete the URL set for the specified $id. * * @param string $id An identification tag for this url. */ function deleteBoomerangURL($id=null) { if (!isset($this) || !is_a($this, 'App')) { $this =& App::getInstance(); } if (!$this->running) { return false; } if (isset($id) && isset($_SESSION[$this->app]['boomerang']['url'][$id])) { unset($_SESSION[$this->app]['boomerang']['url'][$id]); } else if (is_array($_SESSION[$this->app]['boomerang']['url'])) { array_pop($_SESSION[$this->app]['boomerang']['url']); } } /** * Check if a valid boomerang URL value has been set. * if it is not the current url, and has not been accessed within n seconds. * * @return bool True if it is set and not the current URL. */ function validBoomerangURL($id=null, $use_nonspecificboomerang=false) { if (!isset($this) || !is_a($this, 'App')) { $this =& App::getInstance(); } if (!$this->running) { return false; } if (!isset($_SESSION[$this->app]['boomerang']['url'])) { return false; } // Time is the timestamp of a boomerangURL redirection, or setting of a boomerangURL. // a boomerang redirection will always occur at least several seconds after the last boomerang redirect // or a boomerang being set. $boomerang_time = isset($_SESSION[$this->app]['boomerang']['time']) ? $_SESSION[$this->app]['boomerang']['time'] : 0; if (isset($id) && isset($_SESSION[$this->app]['boomerang']['url'][$id])) { $url = $_SESSION[$this->app]['boomerang']['url'][$id]; } else if (!isset($id) || $use_nonspecificboomerang) { // Use non specific boomerang if available. $url = end($_SESSION[$this->app]['boomerang']['url']); } $this->logMsg(sprintf('validBoomerangURL testing url: %s', $url), LOG_DEBUG, __FILE__, __LINE__); if (empty($url)) { return false; } if ($url == absoluteMe()) { // The URL we are directing to is the current page. $this->logMsg(sprintf('Boomerang URL not valid, same as absoluteMe: %s', $url), LOG_WARNING, __FILE__, __LINE__); return false; } if ($boomerang_time >= (time() - 2)) { // Last boomerang direction was more than 2 seconds ago. $this->logMsg(sprintf('Boomerang URL not valid, boomerang_time too short: %s', time() - $boomerang_time), LOG_WARNING, __FILE__, __LINE__); return false; } $this->logMsg(sprintf('validBoomerangURL found: %s', $url), LOG_DEBUG, __FILE__, __LINE__); return true; } /** * Force the user to connect via https (port 443) by redirecting them to * the same page but with https. */ function sslOn() { if (!isset($this) || !is_a($this, 'App')) { $this =& App::getInstance(); } if ('on' != getenv('HTTPS') && $this->getParam('ssl_enabled') && preg_match('/mod_ssl/i', getenv('SERVER_SOFTWARE'))) { $this->raiseMsg(sprintf(_("Secure SSL connection made to %s"), $this->getParam('ssl_domain')), MSG_NOTICE, __FILE__, __LINE__); // Always append session because some browsers do not send cookie when crossing to SSL URL. $this->dieURL('https://' . $this->getParam('ssl_domain') . getenv('REQUEST_URI'), null, true); } } /** * to enforce the user to connect via http (port 80) by redirecting them to * a http version of the current url. */ function sslOff() { if ('on' == getenv('HTTPS')) { $this->dieURL('http://' . getenv('HTTP_HOST') . getenv('REQUEST_URI'), null, true); } } } // End. ?>