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