"Fossies" - the Fresh Open Source Software Archive

Member "betterawstats/core/helpers.inc.php" (18 Mar 2008, 12104 Bytes) of package /linux/www/old/betterawstats-1.0.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 /**
    3  * betterawstats - an alternative display for awstats data
    4  *
    5  * @author      Oliver Spiesshofer, support at betterawstats dot com
    6  * @copyright   2008 Oliver Spiesshofer
    7  * @version     1.0
    8  * @link        http://betterawstats.com
    9 
   10  * Based on the GPL AWStats Totals script by:
   11  * Jeroen de Jong <jeroen@telartis.nl>
   12  * copyright   2004-2006 Telartis
   13  * version 1.13 (http://www.telartis.nl/xcms/awstats)
   14  *
   15  * This program is free software; you can redistribute it and/or
   16  * modify it under the terms of the GNU General Public License
   17  * as published by the Free Software Foundation; either version 2
   18  * of the License, or (at your option) any later version.
   19  *
   20  * This program is distributed in the hope that it will be useful,
   21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   23  * GNU General Public License for more details.
   24  *
   25  * You should have received a copy of the GNU General Public License
   26  * along with this program; if not, write to the Free Software
   27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   28  */
   29 
   30 /**
   31  * File contents:
   32  *
   33  * This file contains functions that are used to make checks, format data and do
   34  * other side-functions.
   35  */
   36 
   37 // this file can't be used on its own
   38 if (strpos ($_SERVER['PHP_SELF'], 'helpers.inc.php') !== false) {
   39     die ('This file can not be used on its own!');
   40 }
   41 
   42 /*
   43 * Helper Function: executes a function that is stored in the $BAW_LIB['formats']
   44 * called by final output functions in /modules/
   45 *
   46 * @param    str     $function_str    string of format 'baw_num_format(%s);'
   47 * @param    str     $var    data to be filled in
   48 * @return   result of the function
   49 *
   50 */
   51 function exec_function($function_str=false, $var=false) {
   52     // check if function exists
   53     $out = $var;
   54     if ($function_str && $var !== false) {
   55         $txt = explode('(',$function_str);
   56         $func_name = $txt[0];
   57         if (function_exists($func_name)) {
   58             $out = eval("return " . sprintf($function_str, $var));
   59         } else {
   60             $out = "error finding function $func_name ($function_str, $var)";
   61         }
   62     }
   63     return $out;
   64 }
   65 
   66 /*
   67 * Helper Function: check for various cfg settings to find if they are correct or not
   68 * called by index.php
   69 *
   70 */
   71 function baw_check_config() {
   72     global $BAW_CONF, $BAW_MES;
   73 
   74     // lets check a non-std file, config.php might exist elsewhere
   75     $path = $BAW_CONF['site_path'] . "/modules/render_table.inc.php";
   76     if (!file_exists($path)) {
   77         // lets get an error-free error message
   78         $BAW_MES[58] = '';
   79         $BAW_MES['e'] = 'UTF-8';
   80         include_once('./language/lang_en.inc.php');
   81         $check = substr($_SERVER["SCRIPT_FILENAME"], 0, -10);
   82         echo baw_raise_error('site_path', array($BAW_CONF['site_path'], $check));
   83         exit;
   84     }
   85 
   86     /* this does not work if the folder is protected with htaccess
   87         since the fopen is not authenticated.
   88     $url = $BAW_CONF['site_url']. '/modules/render_table.inc.php';
   89     if (!@fopen($url, "r")) {
   90         include_once('./language/lang_en.inc.php');
   91         $check = 'http://' . $_SERVER["SERVER_NAME"] . substr($_SERVER["PHP_SELF"], 0, -10);
   92         echo baw_raise_error('site_url', array($BAW_CONF['site_url'], $check));
   93         exit;
   94     }
   95 
   96     $url = $BAW_CONF['icons_url']. '/os/linux.png';
   97     if (!@fopen($url, "r")) {
   98         echo baw_raise_error('iconpath', array($BAW_CONF['icons_url']));
   99         exit;
  100     }
  101     */
  102 }
  103 
  104 /*
  105 * Helper Function: debug output with time stamp and memory meter;
  106 * called by all functions
  107 *
  108 * @param    str     $msg_code   the message to be shown either directly or from
  109 *                               language file
  110 * @param    str     $data       data to be filled in
  111 * @param    bol     $always     show always or only when debug more is set?
  112 * @return   debug message
  113 *
  114 */
  115 function baw_debug($msg_code, $data = false, $always = false) {
  116     global $BAW_CONF, $BAW_MES;
  117     $out = '';
  118     if ($BAW_CONF['debug'] || $always) {
  119         if (isset($BAW_MES[$msg_code])) {
  120             if ($data) {
  121                 $out .= sprintf($BAW_MES[$msg_code], $data);
  122             } else {
  123                 $out = $BAW_MES[$msg_code];
  124             }
  125         } else {
  126             $out = $msg_code;
  127         }
  128         $memory = baw_byte_format(memory_get_usage(),4);
  129         $time = baw_ptime();
  130         echo "<div style=\"text-align:right\">$out | Memory used: $memory | Time: $time</div>\n";
  131     }
  132 }
  133 
  134 /*
  135 * Helper Function: returns the time between the start of the script until now
  136 * called by baw_debug() and baw_display_index()
  137 *
  138 * @return   time in seconds
  139 *
  140 */
  141 function baw_ptime() {
  142     $now = baw_mtime();
  143     $overall = $now - START_TIME;
  144     $time_str = baw_num_format($overall, 3). " sec";
  145     return $time_str;
  146 }
  147 
  148 /*
  149 * Helper Function: sets the current time to START_TIME for calculation
  150 * of time spent for script execution.
  151 * called by index.php, baw_ptime()
  152 *
  153 */
  154 function baw_mtime() {
  155     $arr = explode(" ", microtime());
  156     $out = $arr[0] + $arr[1];
  157     return $out;
  158 }
  159 
  160 /*
  161 * Helper Function: format numeber of bytes
  162 *
  163 * @param    int     $number     number of bytes (1024-base)
  164 * @param    int     $decimals   number of decimals to display
  165 * @return   formatted according to config
  166 *
  167 */
  168 function baw_byte_format($number, $decimals = 2) {
  169     global $BAW_CONF;
  170     // kilo, mega, giga, tera, peta, exa, zetta, yotta
  171     $prefix_arr = array('','k','M','G','T','P','E','Z','Y');
  172     $i = 0;
  173     $value = round($number, $decimals);
  174     while ($value > 1024) {
  175         $value /= 1024;
  176         $i++;
  177     }
  178     $result = baw_num_format($value, $decimals);
  179     $result .= ' '.$prefix_arr[$i].'B';
  180     return $result;
  181 }
  182 
  183 /*
  184 * Helper Function: format numbers according to config
  185 *
  186 * @param    int     $number     number
  187 * @param    int     $decimals   number of decimals to display
  188 * @return   formatted according to config
  189 *
  190 */
  191 function baw_num_format($number, $decimals = 0) {
  192     global $BAW_CONF;
  193     if ($number == 0 || empty($number) || !isset($number)) {
  194         $result = '0';
  195     } else {
  196         $result = number_format($number, $decimals, $BAW_CONF['dec_point'], $BAW_CONF['tho_point']);
  197     }
  198     return $result;
  199 }
  200 
  201 /*
  202 * Helper Function: format dates according to config
  203 *
  204 * @param    int     $str     date in the format YYYYMMDDHHMMSS
  205 * @return   formatted date according to config
  206 *
  207 */
  208 function baw_getdate_format($str) {
  209     global $BAW_CONF;
  210     $out = date($BAW_CONF['date_format'], strtotime($str));
  211     return $out;
  212 }
  213 
  214 /*
  215 * Helper Function: format time according to config
  216 *
  217 * @param    int     $str     date in the format YYYYMMDDHHMMSS
  218 * @return   formatted time only according to config
  219 *
  220 */
  221 function baw_time_format($str) {
  222     global $BAW_CONF, $BAW_MES;
  223     if ($str == 0) {
  224         return $BAW_MES[0];
  225     }
  226     $out = date($BAW_CONF['date_time_format'], strtotime(sprintf("%.0f",$str)));
  227     return $out;
  228 }
  229 
  230 /*
  231 * Helper Function: deliver output according to odd/even numbers
  232 *
  233 * @param    int     $num     number to check if odd/even
  234 * @param    str     $even    string to be returned in case $num is even
  235 * @param    str     $odd     string to be returned in case $num is odd
  236 * @return   $odd or $even
  237 *
  238 */
  239 function baw_even($num, $even, $odd) {
  240     if ($num%2 == 0) {
  241         return $even;
  242     } else {
  243         return $odd;
  244     }
  245 }
  246 
  247 /*
  248 * Helper Function: format a number as a percentage
  249 *
  250 * @param    int     $num     number to format
  251 * @return   str     formatted number
  252 *
  253 */
  254 function baw_percent_format($number) {
  255     global $BAW_CONF;
  256     $result = baw_num_format($number, $BAW_CONF['percent_decimals']) . " %";
  257     return $result;
  258 }
  259 
  260 /*
  261 * Helper Function: Slice an array but keep numeric keys
  262 * called only by baw_display_days()
  263 *
  264 * @param    arr     $array     array to be sliced
  265 * @param    int     $day       $day from where to slice
  266 * @return   arr     sliced array
  267 *
  268 */
  269 function baw_cut_date_array($array, $day) {
  270     $output_array = array();
  271     foreach ($array as $key => $value) {
  272         if ($key > $day) {
  273             $output_array[$key] = $value;
  274         }
  275     }
  276     return $output_array;
  277 }
  278 
  279 /*
  280 * Helper Function: sort a multi-dimensional array by key or index
  281 * called by almost all functions in display.inc.php. For performance reasons,
  282 * the key is the preferred method.
  283 *
  284 * @param    arr     $data     array to be sorted
  285 * @param    str     $column   column to be the sorting reference
  286 * @param    con     $order    SORT_DESC or SORT_ASC
  287 * @return   arr     sorted array
  288 *
  289 */
  290 function baw_array_sorting($data, $column, $order = SORT_DESC) {
  291     if (count($data) ==0) {
  292         return $data;
  293     } else if ($column == 'key') {
  294         baw_debug("sorting array by key");
  295         if ($order == SORT_DESC) {
  296             krsort($data);
  297         } else {
  298             ksort($data);
  299         }
  300     } else {
  301         baw_debug("creating sorting array using column $column $order");
  302         foreach ($data as $description => $numbers) {
  303             $sort_col[$description] = @$numbers[$column];
  304         }
  305         baw_debug("sorting array by column $column $order");
  306         array_multisort($sort_col, $order, $data);
  307     }
  308     baw_debug("array sorted");
  309     return $data;
  310 }
  311 
  312 /*
  313 * Helper Function: raise an error for custom events
  314 *
  315 * @param    str     $type     index for error message
  316 * @param    str     $data     error-specific data to fill into the message
  317 * @return   str     error message
  318 *
  319 */
  320 function baw_raise_error($type, $data = array()) {
  321     global $BAW_MES, $BAW_CONF;
  322 
  323     $cd = count($data);
  324     if ($cd == 0) {
  325         $out = $BAW_MES[$type];
  326     } else if ($cd == 1) {
  327         $out = sprintf($BAW_MES[$type], $data[0]);
  328     } else if ($cd == 2) {
  329         $out = sprintf($BAW_MES[$type], $data[0], $data[1]);
  330     } else if ($cd == 3) {
  331         $out = sprintf($BAW_MES[$type], $data[0], $data[1], $data[2]);
  332     }
  333     // this is here in case the error comes so early that display helpers was not included yet.
  334     if ($BAW_CONF['module'] == 'drupal') {
  335         drupal_set_message(t('The Betterawstats engine generated the error below. If it is a configuration error concerning AWStats locations (i.e. the AWstats data directory, library directory, or language direction), these must be set using the') . ' <a href="skins/default/admin/settings/bawstats">' . t('BAW Statistics') . '</a> ' . t('administration pages.'),'error');
  336         drupal_set_message($out,'warning');
  337     } else {
  338         include_once('display_helpers.php');
  339         $out = baw_site_header() . "<div class=\"errorbox\"><h1>{$BAW_MES['baw_error']}</h1>$out</div></body></html>";
  340         return $out;
  341     }
  342 }
  343 
  344 /*
  345 * Helper Function: check if a file is writable
  346 * called by itself and other occasions where config links are displayed
  347 *
  348 * @param    str     $path     path to the file
  349 * @return   bol     true if writable, otherwise false
  350 *
  351 */
  352 function baw_is_writable($path) {
  353     //will work in despite of Windows ACLs bug
  354     //NOTE: use a trailing slash for folders!!!
  355     //see http://bugs.php.net/bug.php?id=27609
  356     //see http://bugs.php.net/bug.php?id=30931
  357     baw_debug('dbg_test_writable', $path);
  358     if ($path{strlen($path)-1}=='/') {// recursively return a temporary file path
  359         return baw_is_writable($path.uniqid(mt_rand()).'.tmp');
  360     } else if (is_dir($path)) {
  361         return baw_is_writable($path.'/'.uniqid(mt_rand()).'.tmp');
  362     }
  363     // check tmp file for read/write capabilities
  364     $rm = file_exists($path);
  365     $f = @fopen($path, 'a');
  366     if ($f===false) {
  367         @fclose($f);
  368         baw_debug('dbg_test_writable_false', $path);
  369         return false;
  370     }
  371     if (!$rm) {
  372         unlink($path);
  373     }
  374     fclose($f);
  375     baw_debug('dbg_test_writable_true', $path);
  376     return true;
  377 }
  378 
  379 /*
  380 * Helper Function: debug helper that gives var_dump as a return instead of a screendump
  381 *
  382 * @param    misc     $var     variable to be dumped
  383 * @return   str      formatted, dumped variable contents
  384 *
  385 */
  386 function baw_var_dump($var) {
  387     ob_start();
  388     var_dump($var);
  389     $dump = ob_get_contents();
  390     ob_end_clean();
  391     return "<pre>$dump</pre>";
  392 }
  393 ?>