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