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