* @author Brian Wong * @author Jeremy Fowler * @package MailZu * * Following functions taken from PhpScheduleIt, * @author Nick Korbel * @version 04-03-07: * formatTime(), formatDate(), formatDateTime(), minutes_to_hours(), getScriptURL(), * do_error_box(), do_message_box(), getNewLink(), getNewPager(), cleanPostVals(), * get_vert_order(), get_value_order(), write_log(), get_day_name(), redirect(), * print_language_pulldown(), html_activate_links() * * Copyright (C) 2005 - 2007 MailZu * License: GPL, see LICENSE */ /** * Base directory of application */ @define('BASE_DIR', dirname(__FILE__) . '/..'); /** * Include configuration file **/ include_once(BASE_DIR . '/config/config.php'); /** * Include Link class */ include_once('Link.class.php'); /** * Include Pager class */ include_once('Pager.class.php'); // Define constants for method getGlobalVar() @define('INORDER',0); @define('GET',1); @define('POST',2); @define('SESSION',3); @define('SERVER',4); @define('FORM',5); /** * Provides functions common to most pages */ class CmnFns { /** * Convert minutes to hours * @param double $time time to convert in minutes * @return string time in 12 hour time */ function formatTime($time) { global $conf; // Set up time array with $timeArray[0]=hour, $timeArray[1]=minute // If time does not contain decimal point // then set time array manually // else explode on the decimal point $hour = intval($time / 60); $min = $time % 60; if ($conf['app']['timeFormat'] == 24) { $a = ''; // AM/PM does not exist if ($hour < 10) $hour = '0' . $hour; } else { $a = ($hour < 12 || $hour == 24) ? translate('am') : translate('pm'); // Set am/pm if ($hour > 12) $hour = $hour - 12; // Take out of 24hr clock if ($hour == 0) $hour = 12; // Don't show 0hr, show 12 am } // Set proper minutes (the same for 12/24 format) if ($min < 10) $min = 0 . $min; // Put into a string and return return $hour . ':' . $min . $a; } /** * Convert ISO8601 date to date format * @param string $date string (yyyy-mm-dd) * @return int timestamp */ function formatDateISO($date) { $time = strtotime($date); return $time; } /** * Convert timestamp to date format * @param string $date timestamp * @param string $format format to put datestamp into * @return string date as $format or as default format */ function formatDate($date, $format = '') { global $dates; if (empty($format)) $format = $dates['general_date']; return strftime($format, $date); } /** * Convert UNIX timestamp to datetime format * @param string $ts MySQL timestamp * @param string $format format to put datestamp into * @return string date/time as $format or as default format */ function formatDateTime($ts, $format = '') { global $conf; global $dates; if (empty($format)) $format = $dates['general_datetime'] . ' ' . (($conf['app']['timeFormat'] ==24) ? '%H' : '%I') . ':%M:%S' . (($conf['app']['timeFormat'] == 24) ? '' : ' %p'); return strftime($format, $ts); } /** * Convert minutes to hours/minutes * @param int $minutes minutes to convert * @return string version of hours and minutes */ function minutes_to_hours($minutes) { if ($minutes == 0) return '0 ' . translate('hours'); $hours = (intval($minutes / 60) != 0) ? intval($minutes / 60) . ' ' . translate('hours') : ''; $min = (intval($minutes % 60) != 0) ? intval($minutes % 60) . ' ' . translate('minutes') : ''; return ($hours . ' ' . $min); } /** * Return the current script URL directory * @param none * @return url url of curent script directory */ function getScriptURL() { global $conf; $uri = $conf['app']['weburi']; return (strrpos($uri, '/') === false) ? $uri : substr($uri, 0, strlen($uri)); } /** * Prints an error message box and kills the app * @param string $msg error message to print * @param string $style inline CSS style definition to apply to box * @param boolean $die whether to kill the app or not */ function do_error_box($msg, $style='', $die = true) { global $conf; echo '
' . $msg . '
'; if ($die) { echo ''; // endMain() in Template echo '

' . $conf['app']['title'] .' v' . $conf['app']['version'] . '

'; // printHTMLFooter() in Template //$t = new Template(); //$t->endMain(); //$t->printHTMLFooter(); die(); } } /** * Prints out a box with notification message * @param string $msg message to print out * @param string $style inline CSS style definition to apply to box */ function do_message_box($msg, $style='') { echo '
' . $msg . '
'; } /** * Returns a reference to a new Link object * Used to make HTML links * @param none * @return Link object */ function getNewLink() { return new Link(); } /** * Returns a reference to a new Pager object * Used to iterate over limited recordesets * @param none * @return Pager object */ function getNewPager() { return new Pager(); } /** * Strip out slahses from POST values * @param none * @return array of cleaned up POST values */ function cleanPostVals() { $return = array(); foreach ($_POST as $key => $val) $return[$key] = stripslashes(trim($val)); return $return; } /** * Strip out slahses from an array of data * @param none * @return array of cleaned up data */ function cleanVals($data) { $return = array(); foreach ($data as $key => $val) $return[$key] = stripslashes($val); return $return; } /** * Verifies vertical order and returns value * @param string $vert value of vertical order * @return string vertical order */ function get_vert_order($get_name = 'vert') { // If no vertical value is specified, use DESC $vert = isset($_GET[$get_name]) ? $_GET[$get_name] : 'DESC'; // Validate vert value, default to DESC if invalid switch($vert) { case 'DESC'; case 'ASC'; break; default : $vert = 'DESC'; break; } return $vert; } /** * Verifies and returns the order to list recordset results by * If none of the values are valid, it will return the 1st element in the array * @param array $orders all valid order names * @return string order of recorset */ function get_value_order($orders = array(), $get_name = 'order') { if (empty($orders)) // Return null if the order array is empty return NULL; // Set default order value // If a value is specifed in GET, use that. Else use the first element in the array $order = isset($_GET[$get_name]) ? $_GET[$get_name] : $orders[0]; if (in_array($order, $orders)) $order = $order; else $order = $orders[0]; return $order; } /** * Opposite of php's nl2br function. * Subs in a newline for all brs * @param string $subject line to make subs on * @return reformatted line */ function br2nl($subject) { return str_replace('
', "\n", $subject); } /** * Writes a log string to the log file specified in config.php * @param string $string log entry to write to file * @param string $userid memeber id of user performing the action * @param string $ip ip address of user performing the action */ function write_log($string, $userid = NULL, $ip = NULL) { global $conf; $delim = "\t"; $file = $conf['app']['logfile']; $values = ''; if (!$conf['app']['use_log']) // Return if we aren't going to log return; if (empty($ip)) $ip = $_SERVER['REMOTE_ADDR']; clearstatcache(); // Clear cached results if (!is_dir(dirname($file))) mkdir(dirname($file), 0777); // Create the directory if (!touch($file)) return; // Return if we cant touch the file if (!$fp = fopen($file, 'a')) return; // Return if the fopen fails flock($fp, LOCK_EX); // Lock file for writing if (!fwrite($fp, '[' . date('D, d M Y H:i:s') . ']' . $delim . $ip . $delim . $userid . $delim . $string . "\r\n")) // Write log entry return; // Return if we cant write to the file flock($fp, LOCK_UN); // Unlock file fclose($fp); } /** * Returns the day name * @param int $day_of_week day of the week * @param int $type how to return the day name (0 = full, 1 = one letter, 2 = two letter, 3 = three letter) */ function get_day_name($day_of_week, $type = 0) { global $days_full; global $days_abbr; global $days_letter; global $days_two; $names = array ( $days_full, $days_letter, $days_two, $days_letter /* array ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'), array ('S', 'M', 'T', 'W', 'T', 'F', 'S'), array ('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'), array ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat') */ ); return $names[$type][$day_of_week]; } /** * Redirects a user to a new location * @param string $location new http location * @param int $time time in seconds to wait before redirect */ function redirect($location, $time = 0, $die = true) { header("Refresh: $time; URL=$location"); if ($die) exit; } /** * Prints out the HTML to choose a language * @param none */ function print_language_pulldown() { global $conf; ?> \1', $str); $str = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)', '\1\2', $str); $str = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})','\1', $str); return $str; } /** * Verifies current page number and returns value * @param integer $page value of current page number * @return integer current page number */ function get_current_page_number($get_name = 'page') { // If no page number is specified, use 0 $page = ( isset($_GET[$get_name]) && is_numeric($_GET[$get_name]) ) ? $_GET[$get_name] : 0; return $page; } /** * Gets the requested mail_id * @param none * @return string mail_id */ function get_mail_id($get_name = 'mail_id') { // If there isnt one set, return NULL $mail_id = (isset($_GET[$get_name])) ? $_GET[$get_name] : NULL; return $mail_id; } /** * Verifies and returns the order to list recordset results by /** * Convert an array to a query string * @param array $array * @param array $exclude_vars to be excluded from the resulting string * @param boolean $url_encode_ampersands */ function array_to_query_string( $array, $exclude_vars=array(), $url_encode_ampersands=true ) { if( ! is_array( $array ) ) return ''; if( ! $array ) return ''; $str = ''; $i=0; foreach( $array as $name => $val ) { if( ! in_array( $name, $exclude_vars ) ) { if( $i>0 ) if( $url_encode_ampersands ) $str .= '&'; else $str .= '&'; $str .= urlencode( $name ) . '=' . urlencode( $val ); $i++; } } return $str; } /** * Generate HTML for multipage links * @param integer $page current page * @param integer $sizeLimit maximum number of messages per page * @param integer $count total number of messages */ function genMultiPagesLinks( $page, $sizeLimit, $count) { global $link; $total_pages = $count / $sizeLimit; $php_self = $_SERVER['PHP_SELF']; if( $page != 0 ) { $query_string = CmnFns::array_to_query_string( $_GET, array( 'page' ) ); $query_string_first = $query_string . '&page=0'; $query_string_previous = $query_string . '&page=' . ($page-1); $pager_html .= $link->getLink($php_self . '?' . $query_string_first, translate('first'), '', '', translate('Go to first page')) . " | "; $pager_html .= $link->getLink($php_self . '?' . $query_string_previous, translate('previous'), '', '', translate('Go to previous page')) . " | "; } else { $pager_html .= translate('first') . " | " . translate('previous') ." | "; } $pager_html .= '  '; // for large search results where we page beyone the first 20 pages, // print elipsis instead of making the pager be super wide. $elipsis_printed = false; for( $i=0; $i<$count; $i+=$sizeLimit ) { $page_num = $i/$sizeLimit; if( $count > $size_limit * 20 && abs( $page_num - $page ) > 10 ) { if( ! $elipsis_printed ) { $pager_html .= '...  '; $elipsis_printed = true; } } else if( $page == $page_num ) { $pager_html .= '' . ($page_num + 1) . ''; $pager_html .= '  '; $elipsis_printed = false; } else { $query_string = CmnFns::array_to_query_string( $_GET, array( 'page' ) ); $query_string .= '&page=' . $page_num; $pager_html .= $link->getLink($php_self . '?' . $query_string, ($page_num+1), '', '', translate('Go to page') . ' ' . ($page_num+1)); $pager_html .= '  '; $elipsis_printed = false; } } if( $page+1 < $total_pages ) { $query_string = CmnFns::array_to_query_string( $_GET, array( 'page' ) ); $query_string_next .= $query_string . '&page=' . ($page+1); $query_string_last .= $query_string . '&page=' . (ceil($total_pages)-1); $pager_html .= ' | ' . $link->getLink($php_self . '?' . $query_string_next, strtolower(translate('Next')), '', '', translate('Go to next page')); $pager_html .= ' | ' . $link->getLink($php_self . '?' . $query_string_last, translate('last'), '', '', translate('Go to last page')); } else { $pager_html .= " | " . strtolower(translate('Next')) . " | " . translate('last'); } return $pager_html; } /** * Generate HTML for search engine * @param $content_type: 'B' (attachment) or 'S' (spam) */ function searchEngine($content_type, $submit_page, $full_search = false) { global $conf; $fields_array = array("f" => translate('From'), "s" => translate('Subject') ); if ((Auth::isMailAdmin() || Auth::isDomainAdmin()) || $conf['app']['allowMailid']) { $fields_array = array_merge(array("m" => "Mail ID"), $fields_array); } if ($full_search) $fields_array = array_merge(array("t" => translate('To')), $fields_array); ?> \n\t\t\t\n\t\t\t\n\t\t\t
 
  $name) { echo "\t\t\t$name: \n"; echo "\t\t\t\n"; echo "\t\t\t\n"; echo ($i % 2) ? "\t\t\t  \n" : "\t\t\t 
 \n"; $i ++; } ?> :
 \n" : "  "; ?> "; ?>  
"; echo "parent.location.href = '" . $location . "';"; echo ""; } } ?>