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