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