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