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