0 && $this->getParam('display_errors')) {
echo "\n';
}
switch ($message['type']) {
case MSG_ERR:
echo '
' . $message['message'] . '
';
break;
case MSG_WARNING:
echo '
' . $message['message'] . '
';
break;
case MSG_SUCCESS:
echo '
' . $message['message'] . '
';
break;
case MSG_NOTICE:
default:
echo '
' . $message['message'] . '
';
break;
}
?>
running) {
return false;
}
// If priority is not specified, assume the worst.
if (!$this->logPriorityToString($priority)) {
$this->logMsg(sprintf('Log priority %s not defined. (Message: %s)', $priority, $message), LOG_EMERG, $file, $line);
$priority = LOG_EMERG;
}
// If log file is not specified, create one in the codebase root.
if ($this->getParam('log_directory') === null || !is_dir($this->getParam('log_directory')) || !is_writable($this->getParam('log_directory'))) {
// If log file is not specified, don't log to a file.
$this->setParam(array('log_file_priority' => false));
// We must use trigger_error rather than calling App::logMsg, which might lead to an infinite loop.
trigger_error(sprintf('Codebase error: log directory (%s) not found or writable.', $this->getParam('log_directory')), E_USER_NOTICE);
}
// Make sure to log in the system's locale.
$locale = setlocale(LC_TIME, 0);
setlocale(LC_TIME, 'C');
// Data to be stored for a log event.
$event = array();
$event['date'] = date('Y-m-d H:i:s');
$event['remote ip'] = getRemoteAddr();
if (substr(PHP_OS, 0, 3) != 'WIN') {
$event['pid'] = posix_getpid();
}
$event['type'] = $this->logPriorityToString($priority);
$event['file:line'] = "$file : $line";
$event['message'] = strip_tags(preg_replace('/\s{2,}/', ' ', $message));
$event_str = '[' . join('] [', $event) . ']';
// FILE ACTION
if ($this->getParam('log_file_priority') && $priority <= $this->getParam('log_file_priority')) {
error_log($event_str . "\n", 3, $this->getParam('log_directory') . '/' . $this->getParam('log_filename'));
}
// EMAIL ACTION
if ($this->getParam('log_email_priority') && $priority <= $this->getParam('log_email_priority')) {
$subject = sprintf('[%s %s] %s', getenv('HTTP_HOST'), $event['type'], $message);
$email_msg = sprintf("A %s log event occured on %s\n\n", $event['type'], getenv('HTTP_HOST'));
$headers = "From: codebase@strangecode.com\r\n";
foreach ($event as $k=>$v) {
$email_msg .= sprintf("%-11s%s\n", $k, $v);
}
mail($this->getParam('log_to_email_address'), $subject, $email_msg, $headers, '-f codebase@strangecode.com');
}
// SMS ACTION
if ($this->getParam('log_sms_priority') && $priority <= $this->getParam('log_sms_priority')) {
$subject = sprintf('[%s %s]', getenv('HTTP_HOST'), $priority);
$sms_msg = sprintf('%s:%s %s', basename($file), $line, $event['message']);
$headers = "From: codebase@strangecode.com\r\n";
mail($this->getParam('log_to_sms_address'), $subject, $sms_msg, $headers, '-f codebase@strangecode.com');
}
// SCREEN ACTION
if ($this->getParam('log_screen_priority') && $priority <= $this->getParam('log_screen_priority')) {
echo "[{$event['date']}] [{$event['type']}] [{$event['file:line']}] [{$event['message']}]\n";
}
// Restore original locale.
setlocale(LC_TIME, $locale);
}
/**
* Returns the string representation of a LOG_* integer constant.
*
* @param int $priority The LOG_* integer constant.
*
* @return The string representation of $priority.
*/
function logPriorityToString ($priority) {
$priorities = array(
LOG_EMERG => 'emergency',
LOG_ALERT => 'alert',
LOG_CRIT => 'critical',
LOG_ERR => 'error',
LOG_WARNING => 'warning',
LOG_NOTICE => 'notice',
LOG_INFO => 'info',
LOG_DEBUG => 'debug'
);
if (isset($priorities[$priority])) {
return $priorities[$priority];
} else {
return false;
}
}
/**
* Outputs a fully qualified URL with a query of all the used (ie: not empty)
* keys and values, including optional queries. This allows simple printing of
* links without needing to know which queries to add to it. If cookies are not
* used, the session id will be propogated in the URL.
*
* @global string $carry_queries An array of keys to define which values to
* carry through from the POST or GET.
* $carry_queries = array('qry'); for example.
*
* @param string $url The initial url
* @param mixed $carry_args Additional url arguments to carry in the query,
* or FALSE to prevent carrying queries. Can be any of the following formats:
* -array('key1', key2', key3') <-- to save these keys if in the form data.
* -array('key1'=>'value', key2'='value') <-- to set keys to default values if not present in form data.
* -false <-- To not carry any queries. If URL already has queries those will be retained.
*
* @param mixed $always_include_sid Always add the session id, even if using_trans_sid = true. This is required when
* URL starts with http, since PHP using_trans_sid doesn't do those and also for
* header('Location...') redirections.
*
* @return string url with attached queries and, if not using cookies, the session id
*/
function oHREF($url='', $carry_args=null, $always_include_sid=false)
{
if (!is_a($this, 'App')) {
$this =& App::getInstance();
}
if (!$this->running) {
return false;
}
static $_using_trans_sid;
global $carry_queries;
// Save the trans_sid setting.
if (!isset($_using_trans_sid)) {
$_using_trans_sid = ini_get('session.use_trans_sid');
}
// Initialize the carried queries.
if (!isset($carry_queries['_carry_queries_init'])) {
if (!is_array($carry_queries)) {
$carry_queries = array($carry_queries);
}
$tmp = $carry_queries;
$carry_queries = array();
foreach ($tmp as $key) {
if (!empty($key) && getFormData($key, false)) {
$carry_queries[$key] = getFormData($key);
}
}
$carry_queries['_carry_queries_init'] = true;
}
// Get any additional query arguments to add to the $carry_queries array.
// If FALSE is a function argument, DO NOT carry the queries.
$do_carry_queries = true;
$one_time_carry_queries = array();
if (!is_null($carry_args)) {
if (is_array($carry_args) && !empty($carry_args)) {
foreach ($carry_args as $key=>$arg) {
// Get query from appropriate source.
if (false === $arg) {
$do_carry_queries = false;
} else if (false !== getFormData($arg, false)) {
$one_time_carry_queries[$arg] = getFormData($arg); // Set arg to form data if available.
} else if (!is_numeric($key) && '' != $arg) {
$one_time_carry_queries[$key] = getFormData($key, $arg); // Set to arg to default if specified (overwritten by form data).
}
}
} else if (false !== getFormData($carry_args, false)) {
$one_time_carry_queries[$carry_args] = getFormData($carry_args);
} else if (false === $carry_args) {
$do_carry_queries = false;
}
}
// Get the first delimiter that is needed in the url.
$delim = preg_match('/\?/', $url) ? ini_get('arg_separator.output') : '?';
$q = '';
if ($do_carry_queries) {
// Join the perm and temp carry_queries and filter out the _carry_queries_init element for the final query args.
$query_args = array_diff_assoc(urlEncodeArray(array_merge($carry_queries, $one_time_carry_queries)), array('_carry_queries_init' => true));
foreach ($query_args as $key=>$val) {
// Check value is set and value does not already exist in the url.
if (!preg_match('/[?&]' . preg_quote($key) . '=/', $url)) {
$q .= $delim . $key . '=' . $val;
$delim = ini_get('arg_separator.output');
}
}
}
// Include the necessary SID if the following is true:
// - no cookie in http request OR cookies disabled in App
// - sessions are enabled
// - the link stays on our site
// - transparent SID propogation with session.use_trans_sid is not being used OR url begins with protocol (using_trans_sid has no effect here)
// OR
// - 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)
// AND
// - the SID is not already in the query.
if (
(
(
(
!isset($_COOKIE[session_name()])
|| !$this->getParam('session_use_cookies')
)
&& $this->getParam('enable_session')
&& isMyDomain($url)
&&
(
!$_using_trans_sid
|| preg_match('!^(http|https)://!i', $url)
)
)
|| $always_include_sid
)
&& !preg_match('/[?&]' . preg_quote(session_name()) . '=/', $url)
) {
$url .= $q . $delim . session_name() . '=' . session_id();
return $url;
} else {
$url .= $q;
return $url;
}
}
/**
* Prints a hidden form element with the PHPSESSID when cookies are not used, as well
* as hidden form elements for GET_VARS that might be in use.
*
* @global string $carry_queries An array of keys to define which values to
* carry through from the POST or GET.
* $carry_queries = array('qry'); for example
*
* @param mixed $carry_args Additional url arguments to carry in the query,
* or FALSE to prevent carrying queries. Can be any of the following formats:
* -array('key1', key2', key3') <-- to save these keys if in the form data.
* -array('key1'=>'value', key2'='value') <-- to set keys to default values if not present in form data.
* -false <-- To not carry any queries. If URL already has queries those will be retained.
*/
function printHiddenSession($carry_args=null)
{
if (!is_a($this, 'App')) {
$this =& App::getInstance();
}
if (!$this->running) {
return false;
}
static $_using_trans_sid;
global $carry_queries;
// Save the trans_sid setting.
if (!isset($_using_trans_sid)) {
$_using_trans_sid = ini_get('session.use_trans_sid');
}
// Initialize the carried queries.
if (!isset($carry_queries['_carry_queries_init'])) {
if (!is_array($carry_queries)) {
$carry_queries = array($carry_queries);
}
$tmp = $carry_queries;
$carry_queries = array();
foreach ($tmp as $key) {
if (!empty($key) && getFormData($key, false)) {
$carry_queries[$key] = getFormData($key);
}
}
$carry_queries['_carry_queries_init'] = true;
}
// Get any additional query names to add to the $carry_queries array
// that are found as function arguments.
// If FALSE is a function argument, DO NOT carry the queries.
$do_carry_queries = true;
$one_time_carry_queries = array();
if (!is_null($carry_args)) {
if (is_array($carry_args) && !empty($carry_args)) {
foreach ($carry_args as $key=>$arg) {
// Get query from appropriate source.
if (false === $arg) {
$do_carry_queries = false;
} else if (false !== getFormData($arg, false)) {
$one_time_carry_queries[$arg] = getFormData($arg); // Set arg to form data if available.
} else if (!is_numeric($key) && '' != $arg) {
$one_time_carry_queries[$key] = getFormData($key, $arg); // Set to arg to default if specified (overwritten by form data).
}
}
} else if (false !== getFormData($carry_args, false)) {
$one_time_carry_queries[$carry_args] = getFormData($carry_args);
} else if (false === $carry_args) {
$do_carry_queries = false;
}
}
// For each existing POST value, we create a hidden input to carry it through a form.
if ($do_carry_queries) {
// Join the perm and temp carry_queries and filter out the _carry_queries_init element for the final query args.
$query_args = array_diff_assoc(urlEncodeArray(array_merge($carry_queries, $one_time_carry_queries)), array('_carry_queries_init' => true));
foreach ($query_args as $key=>$val) {
echo '