[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[362] | 6 | * |
---|
| 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
| 13 | * |
---|
| 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
[1] | 24 | * App.inc.php |
---|
| 25 | * |
---|
| 26 | * Primary application framework class. |
---|
| 27 | * |
---|
| 28 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[136] | 29 | * @version 2.1 |
---|
[1] | 30 | */ |
---|
[42] | 31 | |
---|
[442] | 32 | //ob_start(); |
---|
| 33 | |
---|
[37] | 34 | // Message Types. |
---|
| 35 | define('MSG_ERR', 1); |
---|
| 36 | define('MSG_ERROR', MSG_ERR); |
---|
[1] | 37 | define('MSG_WARNING', 2); |
---|
[37] | 38 | define('MSG_NOTICE', 4); |
---|
| 39 | define('MSG_SUCCESS', 8); |
---|
[119] | 40 | define('MSG_ALL', MSG_SUCCESS | MSG_NOTICE | MSG_WARNING | MSG_ERROR); |
---|
[1] | 41 | |
---|
| 42 | require_once dirname(__FILE__) . '/Utilities.inc.php'; |
---|
| 43 | |
---|
| 44 | class App { |
---|
[42] | 45 | |
---|
[136] | 46 | // Namespace of this application instance. |
---|
[439] | 47 | private $_ns; |
---|
[1] | 48 | |
---|
[136] | 49 | // If $app->start has run successfully. |
---|
[439] | 50 | public $running = false; |
---|
[1] | 51 | |
---|
| 52 | // Instance of database object. |
---|
[439] | 53 | public $db; |
---|
[42] | 54 | |
---|
[20] | 55 | // Array of query arguments will be carried persistently between requests. |
---|
[439] | 56 | private $_carry_queries = array(); |
---|
[1] | 57 | |
---|
[136] | 58 | // Dictionary of global application parameters. |
---|
[439] | 59 | private $_params = array(); |
---|
[1] | 60 | |
---|
| 61 | // Default parameters. |
---|
[439] | 62 | private $_param_defaults = array( |
---|
[1] | 63 | |
---|
| 64 | // Public name and email address for this application. |
---|
| 65 | 'site_name' => null, |
---|
[390] | 66 | 'site_email' => '', // Set to no-reply@HTTP_HOST if not set here. |
---|
[39] | 67 | 'site_url' => '', // URL automatically determined by _SERVER['HTTP_HOST'] if not set here. |
---|
[318] | 68 | 'images_path' => '', // Location for codebase-generated interface widgets (ex: "/admin/i"). |
---|
[1] | 69 | |
---|
[136] | 70 | // The location the user will go if the system doesn't know where else to send them. |
---|
[1] | 71 | 'redirect_home_url' => '/', |
---|
[42] | 72 | |
---|
[136] | 73 | // SSL URL used when redirecting with $app->sslOn(). |
---|
[1] | 74 | 'ssl_domain' => null, |
---|
| 75 | 'ssl_enabled' => false, |
---|
[42] | 76 | |
---|
[20] | 77 | // Character set for page output. Used in the Content-Type header and the HTML <meta content-type> tag. |
---|
[1] | 78 | 'character_set' => 'utf-8', |
---|
| 79 | |
---|
| 80 | // Human-readable format used to display dates. |
---|
| 81 | 'date_format' => 'd M Y', |
---|
[101] | 82 | 'time_format' => 'h:i:s A', |
---|
[1] | 83 | 'sql_date_format' => '%e %b %Y', |
---|
| 84 | 'sql_time_format' => '%k:%i', |
---|
| 85 | |
---|
| 86 | // Use php sessions? |
---|
| 87 | 'enable_session' => false, |
---|
[242] | 88 | 'session_name' => '_session', |
---|
[1] | 89 | 'session_use_cookies' => true, |
---|
[293] | 90 | |
---|
| 91 | // Pass the session-id through URLs if cookies are not enabled? |
---|
| 92 | // Disable this to prevent session ID theft. |
---|
[242] | 93 | 'session_use_trans_sid' => false, |
---|
[42] | 94 | |
---|
[1] | 95 | // Use database? |
---|
| 96 | 'enable_db' => false, |
---|
| 97 | |
---|
| 98 | // Use db-based sessions? |
---|
| 99 | 'enable_db_session_handler' => false, |
---|
[42] | 100 | |
---|
[1] | 101 | // DB passwords should be set as apache environment variables in httpd.conf, readable only by root. |
---|
| 102 | 'db_server' => 'localhost', |
---|
| 103 | 'db_name' => null, |
---|
| 104 | 'db_user' => null, |
---|
| 105 | 'db_pass' => null, |
---|
| 106 | |
---|
| 107 | // Database debugging. |
---|
| 108 | 'db_always_debug' => false, // TRUE = display all SQL queries. |
---|
| 109 | 'db_debug' => false, // TRUE = display db errors. |
---|
| 110 | 'db_die_on_failure' => false, // TRUE = script stops on db error. |
---|
[42] | 111 | |
---|
[1] | 112 | // For classes that require db tables, do we check that a table exists and create if missing? |
---|
[32] | 113 | 'db_create_tables' => true, |
---|
[1] | 114 | |
---|
[136] | 115 | // The level of error reporting. Don't change this to suppress messages, instead use display_errors to control display. |
---|
[1] | 116 | 'error_reporting' => E_ALL, |
---|
| 117 | |
---|
| 118 | // Don't display errors by default; it is preferable to log them to a file. |
---|
| 119 | 'display_errors' => false, |
---|
[42] | 120 | |
---|
[1] | 121 | // Directory in which to store log files. |
---|
[19] | 122 | 'log_directory' => '', |
---|
[1] | 123 | |
---|
| 124 | // PHP error log. |
---|
| 125 | 'php_error_log' => 'php_error_log', |
---|
| 126 | |
---|
| 127 | // General application log. |
---|
[136] | 128 | 'log_filename' => 'app_log', |
---|
[1] | 129 | |
---|
[390] | 130 | // Don't email or SMS duplicate messages that happen more often than this value (in seconds). |
---|
| 131 | 'log_multiple_timeout' => 3600, // Hourly |
---|
[341] | 132 | |
---|
[1] | 133 | // Logging priority can be any of the following, or false to deactivate: |
---|
| 134 | // LOG_EMERG system is unusable |
---|
| 135 | // LOG_ALERT action must be taken immediately |
---|
| 136 | // LOG_CRIT critical conditions |
---|
| 137 | // LOG_ERR error conditions |
---|
| 138 | // LOG_WARNING warning conditions |
---|
| 139 | // LOG_NOTICE normal, but significant, condition |
---|
| 140 | // LOG_INFO informational message |
---|
| 141 | // LOG_DEBUG debug-level message |
---|
[174] | 142 | 'log_file_priority' => LOG_INFO, |
---|
[342] | 143 | 'log_email_priority' => false, |
---|
[1] | 144 | 'log_sms_priority' => false, |
---|
| 145 | 'log_screen_priority' => false, |
---|
[42] | 146 | |
---|
[390] | 147 | // Email address to receive log event emails. Use multiple addresses by separating them with commas. |
---|
[342] | 148 | 'log_to_email_address' => null, |
---|
[42] | 149 | |
---|
[392] | 150 | // SMS Email address to receive log event SMS messages. Use multiple addresses by separating them with commas. |
---|
[1] | 151 | 'log_to_sms_address' => null, |
---|
[42] | 152 | |
---|
[406] | 153 | // Should we avoid logging repeated logMsg() events? You might want to set this false if you need to see more accurate logging, particularly for long-running scripts. |
---|
| 154 | 'log_ignore_repeated_events' => true, |
---|
| 155 | |
---|
[348] | 156 | // Temporary files directory. |
---|
| 157 | 'tmp_dir' => '/tmp', |
---|
[343] | 158 | |
---|
[19] | 159 | // A key for calculating simple cryptographic signatures. Set using as an environment variables in the httpd.conf with 'SetEnv SIGNING_KEY <key>'. |
---|
[136] | 160 | // Existing password hashes rely on the same key/salt being used to compare encryptions. |
---|
| 161 | // Don't change this unless you know existing hashes or signatures will not be affected! |
---|
[1] | 162 | 'signing_key' => 'aae6abd6209d82a691a9f96384a7634a', |
---|
| 163 | ); |
---|
[42] | 164 | |
---|
[1] | 165 | /** |
---|
| 166 | * This method enforces the singleton pattern for this class. Only one application is running at a time. |
---|
| 167 | * |
---|
[136] | 168 | * $param string $namespace Name of this application. |
---|
| 169 | * @return object Reference to the global Cache object. |
---|
[1] | 170 | * @access public |
---|
| 171 | * @static |
---|
| 172 | */ |
---|
[439] | 173 | public static function &getInstance($namespace='') |
---|
[1] | 174 | { |
---|
| 175 | static $instance = null; |
---|
| 176 | |
---|
| 177 | if ($instance === null) { |
---|
[136] | 178 | $instance = new App($namespace); |
---|
[1] | 179 | } |
---|
| 180 | |
---|
| 181 | return $instance; |
---|
| 182 | } |
---|
[42] | 183 | |
---|
[1] | 184 | /** |
---|
| 185 | * Constructor. |
---|
| 186 | */ |
---|
[439] | 187 | public function __construct($namespace='') |
---|
[1] | 188 | { |
---|
[136] | 189 | // Set namespace of application instance. |
---|
[154] | 190 | $this->_ns = $namespace; |
---|
[42] | 191 | |
---|
[1] | 192 | // Initialize default parameters. |
---|
| 193 | $this->_params = array_merge($this->_params, $this->_param_defaults); |
---|
[172] | 194 | |
---|
| 195 | // Begin timing script. |
---|
| 196 | require_once dirname(__FILE__) . '/ScriptTimer.inc.php'; |
---|
| 197 | $this->timer = new ScriptTimer(); |
---|
| 198 | $this->timer->start('_app'); |
---|
[1] | 199 | } |
---|
| 200 | |
---|
| 201 | /** |
---|
| 202 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
| 203 | * |
---|
| 204 | * @access public |
---|
| 205 | * @param array $param Array of parameters (key => val pairs). |
---|
| 206 | */ |
---|
[439] | 207 | public function setParam($param=null) |
---|
[1] | 208 | { |
---|
| 209 | if (isset($param) && is_array($param)) { |
---|
| 210 | // Merge new parameters with old overriding only those passed. |
---|
[136] | 211 | $this->_params = array_merge($this->_params, $param); |
---|
[1] | 212 | } |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | /** |
---|
| 216 | * Return the value of a parameter. |
---|
| 217 | * |
---|
| 218 | * @access public |
---|
| 219 | * @param string $param The key of the parameter to return. |
---|
| 220 | * @return mixed Parameter value, or null if not existing. |
---|
| 221 | */ |
---|
[439] | 222 | public function getParam($param=null) |
---|
[1] | 223 | { |
---|
| 224 | if ($param === null) { |
---|
[136] | 225 | return $this->_params; |
---|
| 226 | } else if (isset($this->_params[$param])) { |
---|
| 227 | return $this->_params[$param]; |
---|
[1] | 228 | } else { |
---|
[19] | 229 | trigger_error(sprintf('Parameter is not set: %s', $param), E_USER_NOTICE); |
---|
[1] | 230 | return null; |
---|
| 231 | } |
---|
| 232 | } |
---|
[42] | 233 | |
---|
[1] | 234 | /** |
---|
| 235 | * Begin running this application. |
---|
| 236 | * |
---|
| 237 | * @access public |
---|
| 238 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 239 | * @since 15 Jul 2005 00:32:21 |
---|
| 240 | */ |
---|
[439] | 241 | public function start() |
---|
[1] | 242 | { |
---|
| 243 | if ($this->running) { |
---|
| 244 | return false; |
---|
| 245 | } |
---|
[42] | 246 | |
---|
[1] | 247 | // Error reporting. |
---|
[442] | 248 | /* |
---|
[1] | 249 | ini_set('error_reporting', $this->getParam('error_reporting')); |
---|
| 250 | ini_set('display_errors', $this->getParam('display_errors')); |
---|
| 251 | ini_set('log_errors', true); |
---|
| 252 | if (is_dir($this->getParam('log_directory')) && is_writable($this->getParam('log_directory'))) { |
---|
| 253 | ini_set('error_log', $this->getParam('log_directory') . '/' . $this->getParam('php_error_log')); |
---|
| 254 | } |
---|
[442] | 255 | */ |
---|
[248] | 256 | // Set character set to use for multi-byte string functions. |
---|
| 257 | mb_internal_encoding($this->getParam('character_set')); |
---|
[249] | 258 | switch (mb_strtolower($this->getParam('character_set'))) { |
---|
| 259 | case 'utf-8' : |
---|
| 260 | mb_language('uni'); |
---|
| 261 | break; |
---|
[42] | 262 | |
---|
[249] | 263 | case 'iso-2022-jp' : |
---|
| 264 | mb_language('ja'); |
---|
| 265 | break; |
---|
| 266 | |
---|
| 267 | case 'iso-8859-1' : |
---|
| 268 | default : |
---|
| 269 | mb_language('en'); |
---|
| 270 | break; |
---|
| 271 | } |
---|
| 272 | |
---|
[1] | 273 | /** |
---|
| 274 | * 1. Start Database. |
---|
| 275 | */ |
---|
[42] | 276 | |
---|
[103] | 277 | if (true === $this->getParam('enable_db')) { |
---|
[442] | 278 | |
---|
[1] | 279 | // DB connection parameters taken from environment variables in the httpd.conf file, readable only by root. |
---|
| 280 | if (!empty($_SERVER['DB_SERVER'])) { |
---|
| 281 | $this->setParam(array('db_server' => $_SERVER['DB_SERVER'])); |
---|
| 282 | } |
---|
| 283 | if (!empty($_SERVER['DB_NAME'])) { |
---|
| 284 | $this->setParam(array('db_name' => $_SERVER['DB_NAME'])); |
---|
| 285 | } |
---|
| 286 | if (!empty($_SERVER['DB_USER'])) { |
---|
| 287 | $this->setParam(array('db_user' => $_SERVER['DB_USER'])); |
---|
| 288 | } |
---|
| 289 | if (!empty($_SERVER['DB_PASS'])) { |
---|
| 290 | $this->setParam(array('db_pass' => $_SERVER['DB_PASS'])); |
---|
| 291 | } |
---|
[42] | 292 | |
---|
[136] | 293 | // There will ever only be one instance of the DB object, and here is where it is instantiated. |
---|
[1] | 294 | require_once dirname(__FILE__) . '/DB.inc.php'; |
---|
| 295 | $this->db =& DB::getInstance(); |
---|
| 296 | $this->db->setParam(array( |
---|
| 297 | 'db_server' => $this->getParam('db_server'), |
---|
| 298 | 'db_name' => $this->getParam('db_name'), |
---|
| 299 | 'db_user' => $this->getParam('db_user'), |
---|
| 300 | 'db_pass' => $this->getParam('db_pass'), |
---|
| 301 | 'db_always_debug' => $this->getParam('db_always_debug'), |
---|
| 302 | 'db_debug' => $this->getParam('db_debug'), |
---|
| 303 | 'db_die_on_failure' => $this->getParam('db_die_on_failure'), |
---|
| 304 | )); |
---|
| 305 | |
---|
| 306 | // Connect to database. |
---|
| 307 | $this->db->connect(); |
---|
| 308 | } |
---|
[42] | 309 | |
---|
| 310 | |
---|
[1] | 311 | /** |
---|
| 312 | * 2. Start PHP session. |
---|
| 313 | */ |
---|
[42] | 314 | |
---|
[1] | 315 | // Skip session for some user agents. |
---|
| 316 | if (preg_match('/Atomz|ApacheBench|Wget/i', getenv('HTTP_USER_AGENT'))) { |
---|
| 317 | $this->setParam(array('enable_session' => false)); |
---|
| 318 | } |
---|
[42] | 319 | |
---|
[433] | 320 | // Skip sessions if disabled or automatically skip if run in a CLI script. |
---|
| 321 | if (true === $this->getParam('enable_session') && !defined('_CLI')) { |
---|
[42] | 322 | |
---|
[373] | 323 | // Session parameters. |
---|
[442] | 324 | /* |
---|
[373] | 325 | ini_set('session.gc_probability', 1); |
---|
| 326 | ini_set('session.gc_divisor', 1000); |
---|
| 327 | ini_set('session.gc_maxlifetime', 43200); // 12 hours |
---|
| 328 | ini_set('session.use_cookies', $this->getParam('session_use_cookies')); |
---|
| 329 | ini_set('session.use_trans_sid', false); |
---|
| 330 | ini_set('session.entropy_file', '/dev/urandom'); |
---|
| 331 | ini_set('session.entropy_length', '512'); |
---|
[410] | 332 | ini_set('session.cookie_httponly', true); |
---|
[442] | 333 | * */ |
---|
[373] | 334 | session_name($this->getParam('session_name')); |
---|
| 335 | |
---|
[1] | 336 | if (true === $this->getParam('enable_db_session_handler') && true === $this->getParam('enable_db')) { |
---|
| 337 | // Database session handling. |
---|
| 338 | require_once dirname(__FILE__) . '/DBSessionHandler.inc.php'; |
---|
| 339 | $db_save_handler = new DBSessionHandler($this->db, array( |
---|
| 340 | 'db_table' => 'session_tbl', |
---|
| 341 | 'create_table' => $this->getParam('db_create_tables'), |
---|
| 342 | )); |
---|
| 343 | } |
---|
[42] | 344 | |
---|
[22] | 345 | // Start the session. |
---|
[442] | 346 | //session_start(); |
---|
[42] | 347 | |
---|
[154] | 348 | if (!isset($_SESSION['_app'][$this->_ns])) { |
---|
[22] | 349 | // Access session data using: $_SESSION['...']. |
---|
| 350 | // Initialize here _after_ session has started. |
---|
[154] | 351 | $_SESSION['_app'][$this->_ns] = array( |
---|
[22] | 352 | 'messages' => array(), |
---|
| 353 | 'boomerang' => array('url'), |
---|
| 354 | ); |
---|
| 355 | } |
---|
[1] | 356 | } |
---|
[42] | 357 | |
---|
| 358 | |
---|
[1] | 359 | /** |
---|
| 360 | * 3. Misc setup. |
---|
| 361 | */ |
---|
| 362 | |
---|
| 363 | // Script URI will be something like http://host.name.tld (no ending slash) |
---|
| 364 | // and is used whenever a URL need be used to the current site. |
---|
| 365 | // Not available on cli scripts obviously. |
---|
[41] | 366 | if (isset($_SERVER['HTTP_HOST']) && '' != $_SERVER['HTTP_HOST'] && '' == $this->getParam('site_url')) { |
---|
[14] | 367 | $this->setParam(array('site_url' => sprintf('%s://%s', ('on' == getenv('HTTPS') ? 'https' : 'http'), getenv('HTTP_HOST')))); |
---|
[1] | 368 | } |
---|
[390] | 369 | |
---|
| 370 | // In case site_email isn't set, use something halfway presentable. |
---|
| 371 | if (isset($_SERVER['HTTP_HOST']) && '' != $_SERVER['HTTP_HOST'] && '' == $this->getParam('site_email')) { |
---|
| 372 | $this->setParam(array('site_email' => sprintf('no-reply@%s', getenv('HTTP_HOST')))); |
---|
| 373 | } |
---|
[1] | 374 | |
---|
| 375 | // A key for calculating simple cryptographic signatures. |
---|
| 376 | if (isset($_SERVER['SIGNING_KEY'])) { |
---|
| 377 | $this->setParam(array('signing_key' => $_SERVER['SIGNING_KEY'])); |
---|
| 378 | } |
---|
[42] | 379 | |
---|
[1] | 380 | // Character set. This should also be printed in the html header template. |
---|
[442] | 381 | //header('Content-type: text/html; charset=' . $this->getParam('character_set')); |
---|
[136] | 382 | |
---|
| 383 | // Set the version of the codebase we're using. |
---|
| 384 | $codebase_version_file = dirname(__FILE__) . '/../docs/version.txt'; |
---|
| 385 | if (is_readable($codebase_version_file)) { |
---|
| 386 | $codebase_version = trim(file_get_contents($codebase_version_file)); |
---|
[144] | 387 | $this->setParam(array('codebase_version' => $codebase_version)); |
---|
[442] | 388 | //header('X-Codebase-Version: ' . $codebase_version); |
---|
[136] | 389 | } |
---|
[42] | 390 | |
---|
[1] | 391 | $this->running = true; |
---|
| 392 | } |
---|
[42] | 393 | |
---|
[1] | 394 | /** |
---|
| 395 | * Stop running this application. |
---|
| 396 | * |
---|
| 397 | * @access public |
---|
| 398 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 399 | * @since 17 Jul 2005 17:20:18 |
---|
| 400 | */ |
---|
[439] | 401 | public function stop() |
---|
[1] | 402 | { |
---|
| 403 | session_write_close(); |
---|
| 404 | $this->running = false; |
---|
[172] | 405 | $num_queries = 0; |
---|
[103] | 406 | if (true === $this->getParam('enable_db')) { |
---|
[172] | 407 | $num_queries = $this->db->numQueries(); |
---|
[103] | 408 | $this->db->close(); |
---|
| 409 | } |
---|
[172] | 410 | $this->timer->stop('_app'); |
---|
[202] | 411 | $this->logMsg(sprintf('Script ended gracefully. Execution time: %s. Number of db queries: %s.', $this->timer->getTime('_app'), $num_queries), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 412 | } |
---|
[42] | 413 | |
---|
| 414 | |
---|
[1] | 415 | /** |
---|
[84] | 416 | * Add a message to the session, which is printed in the header. |
---|
[1] | 417 | * Just a simple way to print messages to the user. |
---|
| 418 | * |
---|
| 419 | * @access public |
---|
| 420 | * |
---|
| 421 | * @param string $message The text description of the message. |
---|
| 422 | * @param int $type The type of message: MSG_NOTICE, |
---|
| 423 | * MSG_SUCCESS, MSG_WARNING, or MSG_ERR. |
---|
| 424 | * @param string $file __FILE__. |
---|
| 425 | * @param string $line __LINE__. |
---|
| 426 | */ |
---|
[439] | 427 | public function raiseMsg($message, $type=MSG_NOTICE, $file=null, $line=null) |
---|
[1] | 428 | { |
---|
[32] | 429 | $message = trim($message); |
---|
[1] | 430 | |
---|
[203] | 431 | if (!$this->running) { |
---|
| 432 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 433 | return false; |
---|
| 434 | } |
---|
[42] | 435 | |
---|
[203] | 436 | if ('' == trim($message)) { |
---|
| 437 | $this->logMsg(sprintf('Raised message is an empty string.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); |
---|
| 438 | return false; |
---|
| 439 | } |
---|
[406] | 440 | |
---|
| 441 | // Avoid duplicate full-stops.. |
---|
| 442 | $message = trim(preg_replace('/\.{2}$/', '.', $message)); |
---|
[203] | 443 | |
---|
[37] | 444 | // Save message in session under unique key to avoid duplicate messages. |
---|
[177] | 445 | $msg_id = md5($type . $message); |
---|
| 446 | if (!isset($_SESSION['_app'][$this->_ns]['messages'][$msg_id])) { |
---|
| 447 | $_SESSION['_app'][$this->_ns]['messages'][$msg_id] = array( |
---|
| 448 | 'type' => $type, |
---|
| 449 | 'message' => $message, |
---|
| 450 | 'file' => $file, |
---|
| 451 | 'line' => $line, |
---|
| 452 | 'count' => (isset($_SESSION['_app'][$this->_ns]['messages'][$msg_id]['count']) ? (1 + $_SESSION['_app'][$this->_ns]['messages'][$msg_id]['count']) : 1) |
---|
| 453 | ); |
---|
| 454 | } |
---|
[442] | 455 | |
---|
[1] | 456 | if (!in_array($type, array(MSG_NOTICE, MSG_SUCCESS, MSG_WARNING, MSG_ERR))) { |
---|
[136] | 457 | $this->logMsg(sprintf('Invalid MSG_* type: %s', $type), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 458 | } |
---|
| 459 | } |
---|
[46] | 460 | |
---|
| 461 | /** |
---|
| 462 | * Returns an array of the raised messages. |
---|
| 463 | * |
---|
| 464 | * @access public |
---|
[334] | 465 | * @return array List of messages in FIFO order. |
---|
[46] | 466 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 467 | * @since 21 Dec 2005 13:09:20 |
---|
| 468 | */ |
---|
[439] | 469 | public function getRaisedMessages() |
---|
[46] | 470 | { |
---|
[136] | 471 | if (!$this->running) { |
---|
| 472 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); |
---|
| 473 | return false; |
---|
[46] | 474 | } |
---|
[442] | 475 | //die($_SESSION['_app'][$this->_ns]['messages']); |
---|
[154] | 476 | return isset($_SESSION['_app'][$this->_ns]['messages']) ? $_SESSION['_app'][$this->_ns]['messages'] : array(); |
---|
[46] | 477 | } |
---|
| 478 | |
---|
[1] | 479 | /** |
---|
[46] | 480 | * Resets the message list. |
---|
| 481 | * |
---|
| 482 | * @access public |
---|
| 483 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 484 | * @since 21 Dec 2005 13:21:54 |
---|
| 485 | */ |
---|
[439] | 486 | public function clearRaisedMessages() |
---|
[46] | 487 | { |
---|
[136] | 488 | if (!$this->running) { |
---|
| 489 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); |
---|
[46] | 490 | return false; |
---|
| 491 | } |
---|
| 492 | |
---|
[154] | 493 | $_SESSION['_app'][$this->_ns]['messages'] = array(); |
---|
[46] | 494 | } |
---|
| 495 | |
---|
| 496 | /** |
---|
[1] | 497 | * Prints the HTML for displaying raised messages. |
---|
| 498 | * |
---|
[413] | 499 | * @param string $above Additional message to print above error messages (e.g. "Oops!"). |
---|
| 500 | * @param string $below Additional message to print below error messages (e.g. "Please fix and resubmit"). |
---|
| 501 | * @param string $print_gotohash_js Print a line of javascript that scrolls the browser window down to view any error messages. |
---|
| 502 | * @param string $hash The #hashtag to scroll to. |
---|
[1] | 503 | * @access public |
---|
| 504 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 505 | * @since 15 Jul 2005 01:39:14 |
---|
| 506 | */ |
---|
[439] | 507 | public function printRaisedMessages($above='', $below='', $print_gotohash_js=false, $hash='sc-msg') |
---|
[1] | 508 | { |
---|
[442] | 509 | |
---|
[136] | 510 | if (!$this->running) { |
---|
| 511 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 512 | return false; |
---|
| 513 | } |
---|
[136] | 514 | |
---|
| 515 | $messages = $this->getRaisedMessages(); |
---|
[167] | 516 | if (!empty($messages)) { |
---|
[163] | 517 | ?><div id="sc-msg" class="sc-msg"><?php |
---|
[413] | 518 | if ('' != $above) { |
---|
| 519 | ?><div class="sc-above"><?php echo oTxt($above); ?></div><?php |
---|
| 520 | } |
---|
[167] | 521 | foreach ($messages as $m) { |
---|
| 522 | if (error_reporting() > 0 && $this->getParam('display_errors') && isset($m['file']) && isset($m['line'])) { |
---|
| 523 | echo "\n<!-- [" . $m['file'] . ' : ' . $m['line'] . '] -->'; |
---|
| 524 | } |
---|
| 525 | switch ($m['type']) { |
---|
| 526 | case MSG_ERR: |
---|
| 527 | echo '<div class="sc-msg-error">' . $m['message'] . '</div>'; |
---|
| 528 | break; |
---|
[42] | 529 | |
---|
[167] | 530 | case MSG_WARNING: |
---|
| 531 | echo '<div class="sc-msg-warning">' . $m['message'] . '</div>'; |
---|
| 532 | break; |
---|
[42] | 533 | |
---|
[167] | 534 | case MSG_SUCCESS: |
---|
| 535 | echo '<div class="sc-msg-success">' . $m['message'] . '</div>'; |
---|
| 536 | break; |
---|
[42] | 537 | |
---|
[167] | 538 | case MSG_NOTICE: |
---|
| 539 | default: |
---|
| 540 | echo '<div class="sc-msg-notice">' . $m['message'] . '</div>'; |
---|
| 541 | break; |
---|
[42] | 542 | |
---|
[167] | 543 | } |
---|
[1] | 544 | } |
---|
[413] | 545 | if ('' != $below) { |
---|
| 546 | ?><div class="sc-below"><?php echo oTxt($below); ?></div><?php |
---|
| 547 | } |
---|
[1] | 548 | ?></div><?php |
---|
[413] | 549 | if ($print_gotohash_js) { |
---|
| 550 | ?> |
---|
| 551 | <script type="text/javascript"> |
---|
| 552 | /* <![CDATA[ */ |
---|
| 553 | window.location.hash = '#<?php echo urlencode($hash); ?>'; |
---|
| 554 | /* ]]> */ |
---|
| 555 | </script> |
---|
| 556 | <?php |
---|
| 557 | } |
---|
[1] | 558 | } |
---|
[136] | 559 | $this->clearRaisedMessages(); |
---|
[1] | 560 | } |
---|
[42] | 561 | |
---|
[1] | 562 | /** |
---|
[44] | 563 | * Logs messages to defined channels: file, email, sms, and screen. Repeated messages are |
---|
[390] | 564 | * not repeated but printed once with count. Log events that match a sendable channel (email or SMS) |
---|
| 565 | * are sent once per 'log_multiple_timeout' setting (to avoid a flood of error emails). |
---|
[1] | 566 | * |
---|
| 567 | * @access public |
---|
| 568 | * @param string $message The text description of the message. |
---|
| 569 | * @param int $priority The type of message priority (in descending order): |
---|
[390] | 570 | * LOG_EMERG 0 system is unusable |
---|
| 571 | * LOG_ALERT 1 action must be taken immediately |
---|
| 572 | * LOG_CRIT 2 critical conditions |
---|
| 573 | * LOG_ERR 3 error conditions |
---|
| 574 | * LOG_WARNING 4 warning conditions |
---|
| 575 | * LOG_NOTICE 5 normal, but significant, condition |
---|
| 576 | * LOG_INFO 6 informational message |
---|
| 577 | * LOG_DEBUG 7 debug-level message |
---|
[1] | 578 | * @param string $file The file where the log event occurs. |
---|
| 579 | * @param string $line The line of the file where the log event occurs. |
---|
| 580 | */ |
---|
[439] | 581 | public function logMsg($message, $priority=LOG_INFO, $file=null, $line=null) |
---|
[1] | 582 | { |
---|
[44] | 583 | static $previous_events = array(); |
---|
| 584 | |
---|
[1] | 585 | // If priority is not specified, assume the worst. |
---|
[136] | 586 | if (!$this->logPriorityToString($priority)) { |
---|
| 587 | $this->logMsg(sprintf('Log priority %s not defined. (Message: %s)', $priority, $message), LOG_EMERG, $file, $line); |
---|
[1] | 588 | $priority = LOG_EMERG; |
---|
| 589 | } |
---|
[42] | 590 | |
---|
[15] | 591 | // If log file is not specified, don't log to a file. |
---|
[136] | 592 | if (!$this->getParam('log_directory') || !$this->getParam('log_filename') || !is_dir($this->getParam('log_directory')) || !is_writable($this->getParam('log_directory'))) { |
---|
| 593 | $this->setParam(array('log_file_priority' => false)); |
---|
| 594 | // We must use trigger_error to report this problem rather than calling $app->logMsg, which might lead to an infinite loop. |
---|
| 595 | trigger_error(sprintf('Codebase error: log directory (%s) not found or writable.', $this->getParam('log_directory')), E_USER_NOTICE); |
---|
[1] | 596 | } |
---|
[390] | 597 | |
---|
| 598 | // Before we get any further, let's see if ANY log events are configured to be reported. |
---|
| 599 | if ((false === $this->getParam('log_file_priority') || $priority > $this->getParam('log_file_priority')) |
---|
| 600 | && (false === $this->getParam('log_email_priority') || $priority > $this->getParam('log_email_priority')) |
---|
| 601 | && (false === $this->getParam('log_sms_priority') || $priority > $this->getParam('log_sms_priority')) |
---|
| 602 | && (false === $this->getParam('log_screen_priority') || $priority > $this->getParam('log_screen_priority'))) { |
---|
| 603 | // This event would not be recorded, skip it entirely. |
---|
| 604 | return false; |
---|
| 605 | } |
---|
[42] | 606 | |
---|
[1] | 607 | // Make sure to log in the system's locale. |
---|
| 608 | $locale = setlocale(LC_TIME, 0); |
---|
| 609 | setlocale(LC_TIME, 'C'); |
---|
[42] | 610 | |
---|
[44] | 611 | // Strip HTML tags except any with more than 7 characters because that's probably not a HTML tag, e.g. <email@address.com>. |
---|
| 612 | preg_match_all('/(<[^>\s]{7,})[^>]*>/', $message, $strip_tags_allow); |
---|
| 613 | $message = strip_tags(preg_replace('/\s+/', ' ', $message), (!empty($strip_tags_allow[1]) ? join('> ', $strip_tags_allow[1]) . '>' : null)); |
---|
| 614 | |
---|
| 615 | // Store this event under a unique key, counting each time it occurs so that it only gets reported a limited number of times. |
---|
| 616 | $msg_id = md5($message . $priority . $file . $line); |
---|
[406] | 617 | if ($this->getParam('log_ignore_repeated_events') && isset($previous_events[$msg_id])) { |
---|
[44] | 618 | $previous_events[$msg_id]++; |
---|
| 619 | if ($previous_events[$msg_id] == 2) { |
---|
[136] | 620 | $this->logMsg(sprintf('%s (Event repeated %s or more times)', $message, $previous_events[$msg_id]), $priority, $file, $line); |
---|
[44] | 621 | } |
---|
| 622 | return false; |
---|
| 623 | } else { |
---|
| 624 | $previous_events[$msg_id] = 1; |
---|
| 625 | } |
---|
[341] | 626 | |
---|
[390] | 627 | // For email and SMS notification types use "lock" files to prevent sending email and SMS notices ad infinitum. |
---|
| 628 | if ((false !== $this->getParam('log_email_priority') && $priority <= $this->getParam('log_email_priority')) |
---|
| 629 | || (false !== $this->getParam('log_sms_priority') && $priority <= $this->getParam('log_sms_priority'))) { |
---|
| 630 | // This event will generate a "send" notification. Prepare lock file. |
---|
| 631 | $site_hash = md5(empty($_SERVER['SERVER_NAME']) ? $_SERVER['SCRIPT_FILENAME'] : $_SERVER['SERVER_NAME']); |
---|
| 632 | $lock_dir = $this->getParam('tmp_dir') . "/codebase_msgs_$site_hash/"; |
---|
| 633 | // Just use the file and line for the msg_id to limit the number of possible messages |
---|
| 634 | // (the message string itself shan't be used as it may contain innumerable combinations). |
---|
| 635 | $lock_file = $lock_dir . md5($file . ':' . $line); |
---|
| 636 | if (!is_dir($lock_dir)) { |
---|
| 637 | mkdir($lock_dir); |
---|
| 638 | } |
---|
| 639 | $send_notifications = true; |
---|
| 640 | if (is_file($lock_file)) { |
---|
| 641 | $msg_last_sent = filectime($lock_file); |
---|
| 642 | // Has this message been sent more recently than the timeout? |
---|
| 643 | if ((time() - $msg_last_sent) <= $this->getParam('log_multiple_timeout')) { |
---|
| 644 | // This message was already sent recently. |
---|
| 645 | $send_notifications = false; |
---|
| 646 | } else { |
---|
| 647 | // Timeout has expired; send notifications again and reset timeout. |
---|
| 648 | touch($lock_file); |
---|
| 649 | } |
---|
[341] | 650 | } else { |
---|
[390] | 651 | touch($lock_file); |
---|
[341] | 652 | } |
---|
| 653 | } |
---|
[44] | 654 | |
---|
[1] | 655 | // Data to be stored for a log event. |
---|
[44] | 656 | $event = array( |
---|
| 657 | 'date' => date('Y-m-d H:i:s'), |
---|
| 658 | 'remote ip' => getRemoteAddr(), |
---|
[414] | 659 | 'pid' => getmypid(), |
---|
[136] | 660 | 'type' => $this->logPriorityToString($priority), |
---|
[44] | 661 | 'file:line' => "$file : $line", |
---|
[247] | 662 | 'url' => mb_substr(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '', 0, 128), |
---|
[44] | 663 | 'message' => $message |
---|
| 664 | ); |
---|
[42] | 665 | |
---|
[1] | 666 | // FILE ACTION |
---|
[390] | 667 | if (false !== $this->getParam('log_file_priority') && $priority <= $this->getParam('log_file_priority')) { |
---|
[44] | 668 | $event_str = '[' . join('] [', $event) . ']'; |
---|
[247] | 669 | error_log(mb_substr($event_str, 0, 1024) . "\n", 3, $this->getParam('log_directory') . '/' . $this->getParam('log_filename')); |
---|
[1] | 670 | } |
---|
[42] | 671 | |
---|
[390] | 672 | // EMAIL ACTION |
---|
| 673 | if (false !== $this->getParam('log_email_priority') && $priority <= $this->getParam('log_email_priority') && $send_notifications) { |
---|
[422] | 674 | $hostname = (isset($_SERVER['HTTP_HOST']) && '' != $_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : php_uname('n'); |
---|
| 675 | $subject = sprintf('[%s %s] %s', $hostname, $event['type'], mb_substr($message, 0, 64)); |
---|
| 676 | $email_msg = sprintf("A %s log event occurred on %s\n\n", $event['type'], $hostname); |
---|
[390] | 677 | $headers = 'From: ' . $this->getParam('site_email'); |
---|
| 678 | foreach ($event as $k=>$v) { |
---|
| 679 | $email_msg .= sprintf("%-11s%s\n", $k, $v); |
---|
[1] | 680 | } |
---|
[390] | 681 | mb_send_mail($this->getParam('log_to_email_address'), $subject, $email_msg, $headers); |
---|
[1] | 682 | } |
---|
[390] | 683 | |
---|
| 684 | // SMS ACTION |
---|
| 685 | if (false !== $this->getParam('log_sms_priority') && $priority <= $this->getParam('log_sms_priority') && $send_notifications) { |
---|
[422] | 686 | $hostname = (isset($_SERVER['HTTP_HOST']) && '' != $_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : php_uname('n'); |
---|
| 687 | $subject = sprintf('[%s %s]', $hostname, $priority); |
---|
[390] | 688 | $sms_msg = sprintf('%s [%s:%s]', mb_substr($event['message'], 0, 64), basename($file), $line); |
---|
| 689 | $headers = 'From: ' . $this->getParam('site_email'); |
---|
| 690 | mb_send_mail($this->getParam('log_to_sms_address'), $subject, $sms_msg, $headers); |
---|
| 691 | } |
---|
[341] | 692 | |
---|
[1] | 693 | // SCREEN ACTION |
---|
[390] | 694 | if (false !== $this->getParam('log_screen_priority') && $priority <= $this->getParam('log_screen_priority')) { |
---|
[189] | 695 | echo "[{$event['type']}] [{$event['message']}]\n"; |
---|
[1] | 696 | } |
---|
[42] | 697 | |
---|
[1] | 698 | // Restore original locale. |
---|
| 699 | setlocale(LC_TIME, $locale); |
---|
[390] | 700 | |
---|
| 701 | return true; |
---|
[1] | 702 | } |
---|
[42] | 703 | |
---|
[1] | 704 | /** |
---|
| 705 | * Returns the string representation of a LOG_* integer constant. |
---|
| 706 | * |
---|
| 707 | * @param int $priority The LOG_* integer constant. |
---|
| 708 | * |
---|
| 709 | * @return The string representation of $priority. |
---|
| 710 | */ |
---|
[439] | 711 | public function logPriorityToString($priority) { |
---|
[1] | 712 | $priorities = array( |
---|
| 713 | LOG_EMERG => 'emergency', |
---|
| 714 | LOG_ALERT => 'alert', |
---|
| 715 | LOG_CRIT => 'critical', |
---|
| 716 | LOG_ERR => 'error', |
---|
| 717 | LOG_WARNING => 'warning', |
---|
| 718 | LOG_NOTICE => 'notice', |
---|
| 719 | LOG_INFO => 'info', |
---|
| 720 | LOG_DEBUG => 'debug' |
---|
| 721 | ); |
---|
| 722 | if (isset($priorities[$priority])) { |
---|
| 723 | return $priorities[$priority]; |
---|
| 724 | } else { |
---|
| 725 | return false; |
---|
| 726 | } |
---|
| 727 | } |
---|
[42] | 728 | |
---|
[1] | 729 | /** |
---|
[334] | 730 | * Forcefully set a query argument even if one currently exists in the request. |
---|
[136] | 731 | * Values in the _carry_queries array will be copied to URLs (via $app->url()) and |
---|
[20] | 732 | * to hidden input values (via printHiddenSession()). |
---|
| 733 | * |
---|
| 734 | * @access public |
---|
[282] | 735 | * @param mixed $query_key The key (or keys, as an array) of the query argument to save. |
---|
| 736 | * @param mixed $val The new value of the argument key. |
---|
| 737 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 738 | * @since 13 Oct 2007 11:34:51 |
---|
| 739 | */ |
---|
[439] | 740 | public function setQuery($query_key, $val) |
---|
[282] | 741 | { |
---|
| 742 | if (!is_array($query_key)) { |
---|
| 743 | $query_key = array($query_key); |
---|
| 744 | } |
---|
| 745 | foreach ($query_key as $k) { |
---|
| 746 | // Set the value of the specified query argument into the _carry_queries array. |
---|
| 747 | $this->_carry_queries[$k] = $val; |
---|
| 748 | } |
---|
| 749 | } |
---|
| 750 | |
---|
| 751 | /** |
---|
| 752 | * Specify which query arguments will be carried persistently between requests. |
---|
| 753 | * Values in the _carry_queries array will be copied to URLs (via $app->url()) and |
---|
| 754 | * to hidden input values (via printHiddenSession()). |
---|
| 755 | * |
---|
| 756 | * @access public |
---|
[259] | 757 | * @param mixed $query_key The key (or keys, as an array) of the query argument to save. |
---|
[170] | 758 | * @param mixed $default If the key is not available, set to this default value. |
---|
[20] | 759 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 760 | * @since 14 Nov 2005 19:24:52 |
---|
| 761 | */ |
---|
[439] | 762 | public function carryQuery($query_key, $default=false) |
---|
[20] | 763 | { |
---|
[259] | 764 | if (!is_array($query_key)) { |
---|
| 765 | $query_key = array($query_key); |
---|
[20] | 766 | } |
---|
[259] | 767 | foreach ($query_key as $k) { |
---|
| 768 | // If not already set, and there is a non-empty value provided in the request... |
---|
| 769 | if (!isset($this->_carry_queries[$k]) && false !== getFormData($k, $default)) { |
---|
| 770 | // Copy the value of the specified query argument into the _carry_queries array. |
---|
| 771 | $this->_carry_queries[$k] = getFormData($k, $default); |
---|
[331] | 772 | $this->logMsg(sprintf('Carrying query: %s => %s', $k, truncate(getDump($this->_carry_queries[$k], true), 128, 'end')), LOG_DEBUG, __FILE__, __LINE__); |
---|
[259] | 773 | } |
---|
| 774 | } |
---|
[20] | 775 | } |
---|
[42] | 776 | |
---|
[20] | 777 | /** |
---|
[259] | 778 | * dropQuery() is the opposite of carryQuery(). The specified value will not appear in |
---|
| 779 | * url()/ohref()/printHiddenSession() modified URLs unless explicitly written in. |
---|
[325] | 780 | * |
---|
[259] | 781 | * @access public |
---|
| 782 | * @param mixed $query_key The key (or keys, as an array) of the query argument to remove. |
---|
[407] | 783 | * @param bool $unset Remove any values set in the request matching the given $query_key. |
---|
[259] | 784 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 785 | * @since 18 Jun 2007 20:57:29 |
---|
| 786 | */ |
---|
[439] | 787 | public function dropQuery($query_key, $unset=false) |
---|
[259] | 788 | { |
---|
| 789 | if (!is_array($query_key)) { |
---|
| 790 | $query_key = array($query_key); |
---|
| 791 | } |
---|
| 792 | foreach ($query_key as $k) { |
---|
[260] | 793 | if (isset($this->_carry_queries[$k])) { |
---|
[259] | 794 | // Remove the value of the specified query argument from the _carry_queries array. |
---|
[325] | 795 | $this->logMsg(sprintf('Dropping carried query: %s => %s', $k, $this->_carry_queries[$k]), LOG_DEBUG, __FILE__, __LINE__); |
---|
[260] | 796 | unset($this->_carry_queries[$k]); |
---|
[259] | 797 | } |
---|
[325] | 798 | if ($unset && isset($_REQUEST[$k])) { |
---|
| 799 | unset($_REQUEST[$k], $_GET[$k], $_POST[$k], $_COOKIE[$k]); |
---|
| 800 | } |
---|
[259] | 801 | } |
---|
| 802 | } |
---|
| 803 | |
---|
| 804 | /** |
---|
[1] | 805 | * Outputs a fully qualified URL with a query of all the used (ie: not empty) |
---|
[42] | 806 | * keys and values, including optional queries. This allows mindless retention |
---|
[32] | 807 | * of query arguments across page requests. If cookies are not |
---|
[325] | 808 | * used, the session id will be propagated in the URL. |
---|
[1] | 809 | * |
---|
[32] | 810 | * @param string $url The initial url |
---|
| 811 | * @param mixed $carry_args Additional url arguments to carry in the query, |
---|
| 812 | * or FALSE to prevent carrying queries. Can be any of the following formats: |
---|
| 813 | * array('key1', key2', key3') <-- to save these keys if in the form data. |
---|
| 814 | * array('key1'=>'value', key2'='value') <-- to set keys to default values if not present in form data. |
---|
| 815 | * false <-- To not carry any queries. If URL already has queries those will be retained. |
---|
[1] | 816 | * |
---|
| 817 | * @param mixed $always_include_sid Always add the session id, even if using_trans_sid = true. This is required when |
---|
| 818 | * URL starts with http, since PHP using_trans_sid doesn't do those and also for |
---|
| 819 | * header('Location...') redirections. |
---|
| 820 | * |
---|
| 821 | * @return string url with attached queries and, if not using cookies, the session id |
---|
| 822 | */ |
---|
[439] | 823 | public function url($url, $carry_args=null, $always_include_sid=false) |
---|
[1] | 824 | { |
---|
[136] | 825 | if (!$this->running) { |
---|
| 826 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 827 | return false; |
---|
| 828 | } |
---|
[42] | 829 | |
---|
[20] | 830 | // Get any provided query arguments to include in the final URL. |
---|
| 831 | // If FALSE is a provided here, DO NOT carry the queries. |
---|
[1] | 832 | $do_carry_queries = true; |
---|
| 833 | $one_time_carry_queries = array(); |
---|
| 834 | if (!is_null($carry_args)) { |
---|
| 835 | if (is_array($carry_args) && !empty($carry_args)) { |
---|
| 836 | foreach ($carry_args as $key=>$arg) { |
---|
| 837 | // Get query from appropriate source. |
---|
| 838 | if (false === $arg) { |
---|
| 839 | $do_carry_queries = false; |
---|
| 840 | } else if (false !== getFormData($arg, false)) { |
---|
| 841 | $one_time_carry_queries[$arg] = getFormData($arg); // Set arg to form data if available. |
---|
| 842 | } else if (!is_numeric($key) && '' != $arg) { |
---|
| 843 | $one_time_carry_queries[$key] = getFormData($key, $arg); // Set to arg to default if specified (overwritten by form data). |
---|
| 844 | } |
---|
| 845 | } |
---|
| 846 | } else if (false !== getFormData($carry_args, false)) { |
---|
| 847 | $one_time_carry_queries[$carry_args] = getFormData($carry_args); |
---|
| 848 | } else if (false === $carry_args) { |
---|
| 849 | $do_carry_queries = false; |
---|
| 850 | } |
---|
| 851 | } |
---|
[42] | 852 | |
---|
[1] | 853 | // Get the first delimiter that is needed in the url. |
---|
[247] | 854 | $delim = mb_strpos($url, '?') !== false ? ini_get('arg_separator.output') : '?'; |
---|
[32] | 855 | |
---|
[1] | 856 | $q = ''; |
---|
| 857 | if ($do_carry_queries) { |
---|
[20] | 858 | // Join the global _carry_queries and local one_time_carry_queries. |
---|
[136] | 859 | $query_args = urlEncodeArray(array_merge($this->_carry_queries, $one_time_carry_queries)); |
---|
[1] | 860 | foreach ($query_args as $key=>$val) { |
---|
| 861 | // Check value is set and value does not already exist in the url. |
---|
| 862 | if (!preg_match('/[?&]' . preg_quote($key) . '=/', $url)) { |
---|
| 863 | $q .= $delim . $key . '=' . $val; |
---|
| 864 | $delim = ini_get('arg_separator.output'); |
---|
| 865 | } |
---|
| 866 | } |
---|
| 867 | } |
---|
[42] | 868 | |
---|
[1] | 869 | // Include the necessary SID if the following is true: |
---|
| 870 | // - no cookie in http request OR cookies disabled in App |
---|
| 871 | // - sessions are enabled |
---|
| 872 | // - the link stays on our site |
---|
[334] | 873 | // - transparent SID propagation with session.use_trans_sid is not being used OR url begins with protocol (using_trans_sid has no effect here) |
---|
[42] | 874 | // OR |
---|
[1] | 875 | // - 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) |
---|
| 876 | // AND |
---|
| 877 | // - the SID is not already in the query. |
---|
| 878 | if ( |
---|
| 879 | ( |
---|
| 880 | ( |
---|
| 881 | ( |
---|
[42] | 882 | !isset($_COOKIE[session_name()]) |
---|
[136] | 883 | || !$this->getParam('session_use_cookies') |
---|
[42] | 884 | ) |
---|
[242] | 885 | && $this->getParam('session_use_trans_sid') |
---|
[136] | 886 | && $this->getParam('enable_session') |
---|
[42] | 887 | && isMyDomain($url) |
---|
[242] | 888 | && ( |
---|
[20] | 889 | !ini_get('session.use_trans_sid') |
---|
[1] | 890 | || preg_match('!^(http|https)://!i', $url) |
---|
| 891 | ) |
---|
[42] | 892 | ) |
---|
[1] | 893 | || $always_include_sid |
---|
| 894 | ) |
---|
| 895 | && !preg_match('/[?&]' . preg_quote(session_name()) . '=/', $url) |
---|
| 896 | ) { |
---|
| 897 | $url .= $q . $delim . session_name() . '=' . session_id(); |
---|
| 898 | return $url; |
---|
| 899 | } else { |
---|
| 900 | $url .= $q; |
---|
| 901 | return $url; |
---|
| 902 | } |
---|
| 903 | } |
---|
[32] | 904 | |
---|
| 905 | /** |
---|
[136] | 906 | * Returns a HTML-friendly URL processed with $app->url and & replaced with & |
---|
[32] | 907 | * |
---|
| 908 | * @access public |
---|
| 909 | * @param string $url Input URL to parse. |
---|
[334] | 910 | * @return string URL passed through $app->url() and then & turned to $amp;. |
---|
[32] | 911 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 912 | * @since 09 Dec 2005 17:58:45 |
---|
| 913 | */ |
---|
[439] | 914 | public function oHREF($url, $carry_args=null, $always_include_sid=false) |
---|
[32] | 915 | { |
---|
[136] | 916 | $url = $this->url($url, $carry_args, $always_include_sid); |
---|
[42] | 917 | |
---|
[32] | 918 | // Replace any & not followed by an html or unicode entity with it's & equivalent. |
---|
| 919 | $url = preg_replace('/&(?![\w\d#]{1,10};)/', '&', $url); |
---|
[42] | 920 | |
---|
[32] | 921 | return $url; |
---|
| 922 | } |
---|
[42] | 923 | |
---|
[1] | 924 | /** |
---|
| 925 | * Prints a hidden form element with the PHPSESSID when cookies are not used, as well |
---|
[42] | 926 | * as hidden form elements for GET_VARS that might be in use. |
---|
[1] | 927 | * |
---|
| 928 | * @param mixed $carry_args Additional url arguments to carry in the query, |
---|
| 929 | * or FALSE to prevent carrying queries. Can be any of the following formats: |
---|
[32] | 930 | * array('key1', key2', key3') <-- to save these keys if in the form data. |
---|
| 931 | * array('key1'=>'value', key2'='value') <-- to set keys to default values if not present in form data. |
---|
| 932 | * false <-- To not carry any queries. If URL already has queries those will be retained. |
---|
[1] | 933 | */ |
---|
[439] | 934 | public function printHiddenSession($carry_args=null) |
---|
[32] | 935 | { |
---|
[136] | 936 | if (!$this->running) { |
---|
| 937 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 938 | return false; |
---|
| 939 | } |
---|
[42] | 940 | |
---|
[20] | 941 | // Get any provided query arguments to include in the final hidden form data. |
---|
| 942 | // If FALSE is a provided here, DO NOT carry the queries. |
---|
[1] | 943 | $do_carry_queries = true; |
---|
| 944 | $one_time_carry_queries = array(); |
---|
| 945 | if (!is_null($carry_args)) { |
---|
| 946 | if (is_array($carry_args) && !empty($carry_args)) { |
---|
| 947 | foreach ($carry_args as $key=>$arg) { |
---|
| 948 | // Get query from appropriate source. |
---|
| 949 | if (false === $arg) { |
---|
| 950 | $do_carry_queries = false; |
---|
| 951 | } else if (false !== getFormData($arg, false)) { |
---|
| 952 | $one_time_carry_queries[$arg] = getFormData($arg); // Set arg to form data if available. |
---|
| 953 | } else if (!is_numeric($key) && '' != $arg) { |
---|
| 954 | $one_time_carry_queries[$key] = getFormData($key, $arg); // Set to arg to default if specified (overwritten by form data). |
---|
| 955 | } |
---|
| 956 | } |
---|
| 957 | } else if (false !== getFormData($carry_args, false)) { |
---|
| 958 | $one_time_carry_queries[$carry_args] = getFormData($carry_args); |
---|
| 959 | } else if (false === $carry_args) { |
---|
| 960 | $do_carry_queries = false; |
---|
| 961 | } |
---|
| 962 | } |
---|
[42] | 963 | |
---|
[313] | 964 | // For each existing request value, we create a hidden input to carry it through a form. |
---|
[1] | 965 | if ($do_carry_queries) { |
---|
[20] | 966 | // Join the global _carry_queries and local one_time_carry_queries. |
---|
| 967 | // urlencode is not used here, not for form data! |
---|
[136] | 968 | $query_args = array_merge($this->_carry_queries, $one_time_carry_queries); |
---|
[1] | 969 | foreach ($query_args as $key=>$val) { |
---|
[136] | 970 | printf('<input type="hidden" name="%s" value="%s" />', $key, $val); |
---|
[1] | 971 | } |
---|
| 972 | } |
---|
[42] | 973 | |
---|
[1] | 974 | // Include the SID if cookies are disabled. |
---|
[20] | 975 | if (!isset($_COOKIE[session_name()]) && !ini_get('session.use_trans_sid')) { |
---|
[136] | 976 | printf('<input type="hidden" name="%s" value="%s" />', session_name(), session_id()); |
---|
[1] | 977 | } |
---|
| 978 | } |
---|
[42] | 979 | |
---|
[1] | 980 | /** |
---|
| 981 | * Uses an http header to redirect the client to the given $url. If sessions are not used |
---|
| 982 | * and the session is not already defined in the given $url, the SID is appended as a URI query. |
---|
| 983 | * As with all header generating functions, make sure this is called before any other output. |
---|
| 984 | * |
---|
| 985 | * @param string $url The URL the client will be redirected to. |
---|
| 986 | * @param mixed $carry_args Additional url arguments to carry in the query, |
---|
| 987 | * or FALSE to prevent carrying queries. Can be any of the following formats: |
---|
| 988 | * -array('key1', key2', key3') <-- to save these keys if in the form data. |
---|
[136] | 989 | * -array('key1' => 'value', key2' => 'value') <-- to set keys to default values if not present in form data. |
---|
[1] | 990 | * -false <-- To not carry any queries. If URL already has queries those will be retained. |
---|
| 991 | * @param bool $always_include_sid Force session id to be added to Location header. |
---|
| 992 | */ |
---|
[439] | 993 | public function dieURL($url, $carry_args=null, $always_include_sid=false) |
---|
[1] | 994 | { |
---|
[136] | 995 | if (!$this->running) { |
---|
| 996 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 997 | return false; |
---|
| 998 | } |
---|
[42] | 999 | |
---|
[1] | 1000 | if ('' == $url) { |
---|
| 1001 | // If URL is not specified, use the redirect_home_url. |
---|
[136] | 1002 | $url = $this->getParam('redirect_home_url'); |
---|
[1] | 1003 | } |
---|
[42] | 1004 | |
---|
[1] | 1005 | if (preg_match('!^/!', $url)) { |
---|
| 1006 | // If relative URL is given, prepend correct local hostname. |
---|
[22] | 1007 | $scheme = 'on' == getenv('HTTPS') ? 'https' : 'http'; |
---|
| 1008 | $host = getenv('HTTP_HOST'); |
---|
| 1009 | $url = sprintf('%s://%s%s', $scheme, $host, $url); |
---|
[1] | 1010 | } |
---|
[22] | 1011 | |
---|
[136] | 1012 | $url = $this->url($url, $carry_args, $always_include_sid); |
---|
[42] | 1013 | |
---|
[202] | 1014 | // Should we send a "303 See Other" header here instead of relying on the 302 sent automatically by PHP? |
---|
[1] | 1015 | header(sprintf('Location: %s', $url)); |
---|
[136] | 1016 | $this->logMsg(sprintf('dieURL: %s', $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
[42] | 1017 | |
---|
[202] | 1018 | // End application. |
---|
[1] | 1019 | // Recommended, although I'm not sure it's necessary: http://cn2.php.net/session_write_close |
---|
[136] | 1020 | $this->stop(); |
---|
[1] | 1021 | die; |
---|
| 1022 | } |
---|
[42] | 1023 | |
---|
[84] | 1024 | /* |
---|
[136] | 1025 | * Redirects a user by calling $app->dieURL(). It will use: |
---|
[84] | 1026 | * 1. the stored boomerang URL, it it exists |
---|
| 1027 | * 2. a specified $default_url, it it exists |
---|
| 1028 | * 3. the referring URL, it it exists. |
---|
| 1029 | * 4. redirect_home_url configuration variable. |
---|
| 1030 | * |
---|
| 1031 | * @access public |
---|
| 1032 | * @param string $id Identifier for this script. |
---|
[136] | 1033 | * @param mixed $carry_args Additional arguments to carry in the URL automatically (see $app->oHREF()). |
---|
[84] | 1034 | * @param string $default_url A default URL if there is not a valid specified boomerang URL. |
---|
[159] | 1035 | * @param bool $queryless_referrer_comparison Exclude the URL query from the refererIsMe() comparison. |
---|
[84] | 1036 | * @return bool False if the session is not running. No return otherwise. |
---|
| 1037 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 1038 | * @since 31 Mar 2006 19:17:00 |
---|
| 1039 | */ |
---|
[439] | 1040 | public function dieBoomerangURL($id=null, $carry_args=null, $default_url=null, $queryless_referrer_comparison=false) |
---|
[1] | 1041 | { |
---|
[136] | 1042 | if (!$this->running) { |
---|
| 1043 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 1044 | return false; |
---|
| 1045 | } |
---|
[42] | 1046 | |
---|
[1] | 1047 | // Get URL from stored boomerang. Allow non specific URL if ID not valid. |
---|
[136] | 1048 | if ($this->validBoomerangURL($id, true)) { |
---|
[154] | 1049 | if (isset($id) && isset($_SESSION['_app'][$this->_ns]['boomerang']['url'][$id])) { |
---|
| 1050 | $url = $_SESSION['_app'][$this->_ns]['boomerang']['url'][$id]; |
---|
[136] | 1051 | $this->logMsg(sprintf('dieBoomerangURL(%s) found: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 1052 | } else { |
---|
[154] | 1053 | $url = end($_SESSION['_app'][$this->_ns]['boomerang']['url']); |
---|
[136] | 1054 | $this->logMsg(sprintf('dieBoomerangURL(%s) using: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 1055 | } |
---|
[22] | 1056 | // Delete stored boomerang. |
---|
[136] | 1057 | $this->deleteBoomerangURL($id); |
---|
[84] | 1058 | } else if (isset($default_url)) { |
---|
| 1059 | $url = $default_url; |
---|
[159] | 1060 | } else if (!refererIsMe(true === $queryless_referrer_comparison)) { |
---|
[1] | 1061 | // Ensure that the redirecting page is not also the referrer. |
---|
| 1062 | $url = getenv('HTTP_REFERER'); |
---|
[136] | 1063 | $this->logMsg(sprintf('dieBoomerangURL(%s) using referrer: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 1064 | } else { |
---|
[22] | 1065 | // If URL is not specified, use the redirect_home_url. |
---|
[136] | 1066 | $url = $this->getParam('redirect_home_url'); |
---|
[203] | 1067 | $this->logMsg(sprintf('dieBoomerangURL(%s) using redirect_home_url: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 1068 | } |
---|
[42] | 1069 | |
---|
[84] | 1070 | // A redirection will never happen immediately twice. |
---|
[1] | 1071 | // Set the time so ensure this doesn't happen. |
---|
[154] | 1072 | $_SESSION['_app'][$this->_ns]['boomerang']['time'] = time(); |
---|
[136] | 1073 | $this->dieURL($url, $carry_args); |
---|
[1] | 1074 | } |
---|
[42] | 1075 | |
---|
[1] | 1076 | /** |
---|
[136] | 1077 | * Set the URL to return to when $app->dieBoomerangURL() is called. |
---|
[1] | 1078 | * |
---|
| 1079 | * @param string $url A fully validated URL. |
---|
| 1080 | * @param bool $id An identification tag for this url. |
---|
| 1081 | * FIXME: url garbage collection? |
---|
| 1082 | */ |
---|
[439] | 1083 | public function setBoomerangURL($url=null, $id=null) |
---|
[1] | 1084 | { |
---|
[136] | 1085 | if (!$this->running) { |
---|
| 1086 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 1087 | return false; |
---|
| 1088 | } |
---|
[84] | 1089 | // A redirection will never happen immediately after setting the boomerangURL. |
---|
[136] | 1090 | // Set the time so ensure this doesn't happen. See $app->validBoomerangURL for more. |
---|
[359] | 1091 | /// FIXME: Why isn't the time set here under setBoomerangURL() and only under dieBoomerangURL()? |
---|
[42] | 1092 | |
---|
[22] | 1093 | if ('' != $url && is_string($url)) { |
---|
[242] | 1094 | // Delete any boomerang request keys in the query string (along with any trailing delimiters after the deletion). |
---|
| 1095 | $url = preg_replace(array('/([&?])boomerang=\w+[&?]?/', '/[&?]$/'), array('$1', ''), $url); |
---|
[42] | 1096 | |
---|
[154] | 1097 | if (isset($_SESSION['_app'][$this->_ns]['boomerang']['url']) && is_array($_SESSION['_app'][$this->_ns]['boomerang']['url']) && !empty($_SESSION['_app'][$this->_ns]['boomerang']['url'])) { |
---|
[1] | 1098 | // If the URL currently exists in the boomerang array, delete. |
---|
[154] | 1099 | while ($existing_key = array_search($url, $_SESSION['_app'][$this->_ns]['boomerang']['url'])) { |
---|
| 1100 | unset($_SESSION['_app'][$this->_ns]['boomerang']['url'][$existing_key]); |
---|
[1] | 1101 | } |
---|
| 1102 | } |
---|
[42] | 1103 | |
---|
[1] | 1104 | if (isset($id)) { |
---|
[154] | 1105 | $_SESSION['_app'][$this->_ns]['boomerang']['url'][$id] = $url; |
---|
[1] | 1106 | } else { |
---|
[154] | 1107 | $_SESSION['_app'][$this->_ns]['boomerang']['url'][] = $url; |
---|
[1] | 1108 | } |
---|
[136] | 1109 | $this->logMsg(sprintf('setBoomerangURL(%s): %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 1110 | return true; |
---|
| 1111 | } else { |
---|
[136] | 1112 | $this->logMsg(sprintf('setBoomerangURL(%s) is empty!', $id, $url), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 1113 | return false; |
---|
| 1114 | } |
---|
| 1115 | } |
---|
[42] | 1116 | |
---|
[1] | 1117 | /** |
---|
[333] | 1118 | * Return the URL set for the specified $id, or an empty string if one isn't set. |
---|
[1] | 1119 | * |
---|
| 1120 | * @param string $id An identification tag for this url. |
---|
| 1121 | */ |
---|
[439] | 1122 | public function getBoomerangURL($id=null) |
---|
[1] | 1123 | { |
---|
[136] | 1124 | if (!$this->running) { |
---|
| 1125 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 1126 | return false; |
---|
| 1127 | } |
---|
[42] | 1128 | |
---|
[1] | 1129 | if (isset($id)) { |
---|
[154] | 1130 | if (isset($_SESSION['_app'][$this->_ns]['boomerang']['url'][$id])) { |
---|
| 1131 | return $_SESSION['_app'][$this->_ns]['boomerang']['url'][$id]; |
---|
[1] | 1132 | } else { |
---|
| 1133 | return ''; |
---|
| 1134 | } |
---|
[154] | 1135 | } else if (is_array($_SESSION['_app'][$this->_ns]['boomerang']['url'])) { |
---|
| 1136 | return end($_SESSION['_app'][$this->_ns]['boomerang']['url']); |
---|
[1] | 1137 | } else { |
---|
| 1138 | return false; |
---|
| 1139 | } |
---|
| 1140 | } |
---|
[42] | 1141 | |
---|
[1] | 1142 | /** |
---|
| 1143 | * Delete the URL set for the specified $id. |
---|
| 1144 | * |
---|
| 1145 | * @param string $id An identification tag for this url. |
---|
| 1146 | */ |
---|
[439] | 1147 | public function deleteBoomerangURL($id=null) |
---|
[1] | 1148 | { |
---|
[136] | 1149 | if (!$this->running) { |
---|
| 1150 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 1151 | return false; |
---|
| 1152 | } |
---|
[42] | 1153 | |
---|
[136] | 1154 | $this->logMsg(sprintf('deleteBoomerangURL(%s): %s', $id, $this->getBoomerangURL($id)), LOG_DEBUG, __FILE__, __LINE__); |
---|
[22] | 1155 | |
---|
[154] | 1156 | if (isset($id) && isset($_SESSION['_app'][$this->_ns]['boomerang']['url'][$id])) { |
---|
| 1157 | unset($_SESSION['_app'][$this->_ns]['boomerang']['url'][$id]); |
---|
| 1158 | } else if (is_array($_SESSION['_app'][$this->_ns]['boomerang']['url'])) { |
---|
| 1159 | array_pop($_SESSION['_app'][$this->_ns]['boomerang']['url']); |
---|
[1] | 1160 | } |
---|
| 1161 | } |
---|
[42] | 1162 | |
---|
[1] | 1163 | /** |
---|
[103] | 1164 | * Check if a valid boomerang URL value has been set. A boomerang URL is considered |
---|
| 1165 | * valid if: 1) it is not empty, 2) it is not the current URL, and 3) has not been accessed within n seconds. |
---|
[1] | 1166 | * |
---|
[103] | 1167 | * @return bool True if it is set and valid, false otherwise. |
---|
[1] | 1168 | */ |
---|
[439] | 1169 | public function validBoomerangURL($id=null, $use_nonspecificboomerang=false) |
---|
[1] | 1170 | { |
---|
[136] | 1171 | if (!$this->running) { |
---|
| 1172 | $this->logMsg(sprintf('Canceled method call %s, application not running.', __FUNCTION__), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 1173 | return false; |
---|
| 1174 | } |
---|
[42] | 1175 | |
---|
[154] | 1176 | if (!isset($_SESSION['_app'][$this->_ns]['boomerang']['url'])) { |
---|
[136] | 1177 | $this->logMsg(sprintf('validBoomerangURL(%s) no boomerang URL set.', $id), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 1178 | return false; |
---|
| 1179 | } |
---|
[42] | 1180 | |
---|
[334] | 1181 | // Time is the time stamp of a boomerangURL redirection, or setting of a boomerangURL. |
---|
[1] | 1182 | // a boomerang redirection will always occur at least several seconds after the last boomerang redirect |
---|
| 1183 | // or a boomerang being set. |
---|
[154] | 1184 | $boomerang_time = isset($_SESSION['_app'][$this->_ns]['boomerang']['time']) ? $_SESSION['_app'][$this->_ns]['boomerang']['time'] : 0; |
---|
[42] | 1185 | |
---|
[22] | 1186 | $url = ''; |
---|
[154] | 1187 | if (isset($id) && isset($_SESSION['_app'][$this->_ns]['boomerang']['url'][$id])) { |
---|
| 1188 | $url = $_SESSION['_app'][$this->_ns]['boomerang']['url'][$id]; |
---|
[1] | 1189 | } else if (!isset($id) || $use_nonspecificboomerang) { |
---|
| 1190 | // Use non specific boomerang if available. |
---|
[154] | 1191 | $url = end($_SESSION['_app'][$this->_ns]['boomerang']['url']); |
---|
[1] | 1192 | } |
---|
[42] | 1193 | |
---|
[136] | 1194 | $this->logMsg(sprintf('validBoomerangURL(%s) testing: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
[22] | 1195 | |
---|
| 1196 | if ('' == $url) { |
---|
[136] | 1197 | $this->logMsg(sprintf('validBoomerangURL(%s) not valid, empty!', $id), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 1198 | return false; |
---|
| 1199 | } |
---|
| 1200 | if ($url == absoluteMe()) { |
---|
| 1201 | // The URL we are directing to is the current page. |
---|
[136] | 1202 | $this->logMsg(sprintf('validBoomerangURL(%s) not valid, same as absoluteMe: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 1203 | return false; |
---|
| 1204 | } |
---|
| 1205 | if ($boomerang_time >= (time() - 2)) { |
---|
[159] | 1206 | // Last boomerang direction was less than 2 seconds ago. |
---|
| 1207 | $this->logMsg(sprintf('validBoomerangURL(%s) not valid, boomerang_time too short: %s seconds', $id, time() - $boomerang_time), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 1208 | return false; |
---|
| 1209 | } |
---|
[42] | 1210 | |
---|
[136] | 1211 | $this->logMsg(sprintf('validBoomerangURL(%s) is valid: %s', $id, $url), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 1212 | return true; |
---|
| 1213 | } |
---|
| 1214 | |
---|
| 1215 | /** |
---|
| 1216 | * Force the user to connect via https (port 443) by redirecting them to |
---|
| 1217 | * the same page but with https. |
---|
| 1218 | */ |
---|
[439] | 1219 | public function sslOn() |
---|
[1] | 1220 | { |
---|
[38] | 1221 | if (function_exists('apache_get_modules')) { |
---|
[42] | 1222 | $modules = apache_get_modules(); |
---|
[38] | 1223 | } else { |
---|
| 1224 | // It's safe to assume we have mod_ssl if we can't determine otherwise. |
---|
| 1225 | $modules = array('mod_ssl'); |
---|
| 1226 | } |
---|
[42] | 1227 | |
---|
[136] | 1228 | if ('' == getenv('HTTPS') && $this->getParam('ssl_enabled') && in_array('mod_ssl', $modules)) { |
---|
| 1229 | $this->raiseMsg(sprintf(_("Secure SSL connection made to %s"), $this->getParam('ssl_domain')), MSG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 1230 | // Always append session because some browsers do not send cookie when crossing to SSL URL. |
---|
[136] | 1231 | $this->dieURL('https://' . $this->getParam('ssl_domain') . getenv('REQUEST_URI'), null, true); |
---|
[1] | 1232 | } |
---|
| 1233 | } |
---|
[42] | 1234 | |
---|
| 1235 | |
---|
[1] | 1236 | /** |
---|
| 1237 | * to enforce the user to connect via http (port 80) by redirecting them to |
---|
| 1238 | * a http version of the current url. |
---|
| 1239 | */ |
---|
[439] | 1240 | public function sslOff() |
---|
[1] | 1241 | { |
---|
[53] | 1242 | if ('' != getenv('HTTPS')) { |
---|
[1] | 1243 | $this->dieURL('http://' . getenv('HTTP_HOST') . getenv('REQUEST_URI'), null, true); |
---|
| 1244 | } |
---|
| 1245 | } |
---|
| 1246 | |
---|
[42] | 1247 | |
---|
[439] | 1248 | } // End. |
---|