source: trunk/lib/App.inc.php @ 414

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