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