"Fossies" - the Fresh Open Source Software Archive

Member "betterawstats/includes/bawstats.stats.inc" (18 Mar 2008, 15618 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) fasm source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2  // $Id: bawstats.stats.inc,v 1.5 2008/03/18 01:07:41 tokyoahead Exp $
    3 
    4 /**
    5  * BAWstats drupal module
    6  *
    7  * BAWstats is a drupal module written in php for integration of
    8  * the BetterAWstats presentation and processing of AWstats data into drupal.
    9  * Copyright (C) 2008  Andrew Gillies (anaru@equivocation.org)
   10  *
   11  * For more information about BetterAWstats see:
   12  * http://betterawstats.com/
   13  *
   14  * This program is free software; you can redistribute it and/or modify
   15  * it under the terms of the GNU General Public License as published by
   16  * the Free Software Foundation; either version 2 of the License, or, at
   17  * your option, any later version.
   18  *
   19  * This program is distributed in the hope that it will be useful, but
   20  * WITHOUT ANY WARRANTY; without even the implied warranty of
   21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   22  * General Public License for more details.
   23  *
   24  * You should have received a copy of the GNU General Public License
   25  * along with this program; if not, write to the Free Software
   26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   27  *
   28  */
   29 
   30 include(drupal_get_path('module', 'bawstats') . '/includes/bawstats.config.inc');
   31 
   32 /**
   33  * Form for the bawstats
   34  */
   35 
   36 function bawstats_view_selection() {
   37   global $BAW_CURR,$BAW_CONF,$BAW_DFILES;
   38   $site_OK = True;
   39   $date_OK = True;
   40 
   41   $site_access = variable_get('bawstats_admin_access', 0);
   42 
   43   // check we are configured...
   44   if ((strlen($BAW_CONF['path_data'])>0)&&(strlen($BAW_CONF['path_lib'])>0)&&(strlen($BAW_CONF['path_lang'])>0)) {
   45 
   46     // recall the site we are looking at from the SESSION
   47     if (isset($_SESSION['bawstats'])) {
   48       $BAW_CURR['site_name'] = $_SESSION['bawstats']['site_name'];
   49     } else {
   50       // its not set in the session, so set it here to the default (and record it in the session for the future)
   51       $oursite = $_SERVER['HTTP_HOST'];
   52       if (strpos(strtolower($oursite),"www.")===0) $oursite = substr($oursite,4);   // remove www. if any
   53       // this can be overriden only in the settings.php file using conf
   54       $BAW_CURR['site_name'] = variable_get('bawstats_defsite',$oursite);
   55       $_SESSION['bawstats']['site_name'] = $BAW_CURR['site_name'];
   56     }
   57 
   58     // record what was requested before checks so we can make a nice message
   59     $rec_year = $BAW_CURR['year'];
   60     $rec_month = $BAW_CURR['month'];
   61 
   62     // sanity check on date data:
   63     if ((!is_numeric($BAW_CURR['month']))||(intval($BAW_CURR['month'])<1)||(intval($BAW_CURR['month'])>12)) {
   64       $site_OK = False;
   65       $date_OK = False;
   66       // drupal_set_message(t('Invalid month %name.', array('%name' => $BAW_CURR['month'])));
   67       $BAW_CURR['month'] = '01';
   68     }
   69     $curyear = date('Y');
   70     if ((!is_numeric($BAW_CURR['year']))||(intval($BAW_CURR['year'])>intval($curyear))) {
   71       $site_OK = False;
   72       $date_OK = False;
   73       //drupal_set_message(t('Invalid year %name.', array('%name' => $BAW_CURR['year'])));
   74       $BAW_CURR['year'] = $curyear;
   75     }
   76 
   77     //    $BAW_CURR['monthyear'] = $BAW_CURR['month'].$BAW_CURR['year'];
   78     $BAW_CURR['yearmonth'] = $BAW_CURR['year'].$BAW_CURR['month'];
   79 
   80     // set key request variables in the session for use in the, only if sane!
   81     if ($site_OK) {
   82       $_SESSION['bawstats']['month'] = $BAW_CURR['month'];
   83       $_SESSION['bawstats']['year']  = $BAW_CURR['year'];
   84     }
   85 
   86     baw_match_files();
   87 
   88     // some simple validation
   89     if (count($BAW_DFILES)>0) {
   90 
   91       // build a 'stats selection' form
   92       $form['bawstats'] = array(
   93         '#type' => 'fieldset',
   94         '#title' => t('Statistics selection'),
   95         '#prefix' => '<div class="container-inline">',
   96         '#suffix' => '</div>'
   97       );
   98 
   99       // check year is in list of options:
  100 
  101       // FIX, this doesn't work - range of years is nolonger filled, as baw_match_files doesn't do it!
  102       if (!isset($BAW_CURR['years'][$BAW_CURR['year']])) {
  103         $site_OK = False;
  104         drupal_set_message(t('No data for year %name.', array('%name' => $BAW_CURR['year'])));
  105         $BAW_CURR['year'] = $curyear;
  106         // re-read data for current year, so that the forms are correctly built
  107     // need new function! baw_read_data();
  108       }
  109 
  110       $options = bawstats_sites_dropdown();
  111 
  112       // make sure stats exist for this site
  113       if ((!isset($options[$BAW_CURR['site_name']])) &&
  114           (!(($BAW_CURR['site_name']=='all_days')||($BAW_CURR['site_name']=='all_months')))) $site_OK = False;
  115 
  116       // only admin has the option of sites
  117       if ($site_access>0) {
  118         if (count($options) == 1 ) {
  119           $BAW_CURR['site_name'] = $options[0];
  120         }
  121         $form['bawstats']['site'] = array(
  122           '#type' => 'select',
  123           '#options' => $options,
  124           '#default_value' => $BAW_CURR['site_name']
  125         );
  126       }
  127 
  128       $options = bawstats_months_dropdown();
  129 
  130       $form['bawstats']['month'] = array(
  131         '#type' => 'select',
  132         '#options' => $options,
  133         '#default_value' => $BAW_CURR['months'][$BAW_CURR['month']]
  134       );
  135 
  136       $form['bawstats']['year'] = array(
  137         '#type' => 'select',
  138         '#options' => $BAW_CURR['years'],
  139         '#default_value' => $BAW_CURR['year']
  140       );
  141 
  142       $form['bawstats']['submit'] = array(
  143         '#type' => 'submit',
  144         '#value' => t('View')
  145       );
  146     } else $site_OK = False;
  147 
  148     if ($site_OK) {
  149       $form['bawstats_results'] = array(
  150         '#value' => bawstats_display()
  151       );
  152     } else {
  153       if ($date_OK) {
  154     $form['bawstats_results'] = array(
  155       '#value' => "No statistics found for site <em>" . $BAW_CURR['site_name'] . "</em> " . $rec_month . "/" . $rec_year
  156     );
  157       } else {
  158     $form['bawstats_results'] = array(
  159       '#value' => "No statistics found for site <em>" . $BAW_CURR['site_name'] . "</em> "
  160     );
  161       }
  162 
  163     }
  164   } else {
  165     $form['bawstats_results'] = array(
  166         '#value' => 'No statistics found.  Please check the <a href="settings/bawstats">configuration</a> of the AWstats data, library and language locations.'
  167       );
  168   }
  169 
  170   return $form;
  171 }
  172 
  173 function bawstats_view_theform($month='-1',$year='-1',$section='') {
  174   global $BAW_CURR, $BAW_CONF_DIS_DEF, $BAW_CONF, $BAW_LIB;
  175 
  176   // get configuration from drupal variables
  177   $BAW_CONF['path_data'] = variable_get('bawstats_data','');
  178   $BAW_CONF['path_lib'] = variable_get('bawstats_lib','');
  179   $BAW_CONF['path_lang'] = variable_get('bawstats_lang','');
  180 
  181   if (is_numeric($year) && is_numeric($month)) {
  182     if (intval($month)>=0) {
  183       //bawstats_view_selection will check validity of month
  184       $BAW_CURR['month'] = $month;
  185     } else {
  186       $BAW_CURR['month'] = date('m');
  187     }
  188 
  189     if (intval($year)>=0) {
  190       //bawstats_view_selection will check validity of year
  191       $BAW_CURR['year'] = $year;
  192     } else {
  193       $BAW_CURR['year'] = date('Y');
  194     }
  195   }
  196 
  197   if (strlen($section)>1) {
  198     // validate sections (ignore all that are not recognised from the default configuration)
  199     $found = False;
  200     // NOTE, must ensure group and section names are different!
  201     // first, is it a group?
  202     foreach ($BAW_LIB['item_groups'] as $group => $group_details) {
  203       if ($section==check_plain($group)) {
  204         $found = True;
  205         break;
  206       }
  207     }
  208     if ($found) $BAW_CURR['drupal_group'] = $section;
  209     else {
  210       // second, is it a section
  211       foreach ($BAW_CONF_DIS_DEF as $key => $value) {
  212         if ($section==$key) {
  213       $found = True;
  214       break;
  215         }
  216       }
  217       if ($found) $BAW_CURR['drupal_section'] = $section;
  218     }
  219   }
  220   return drupal_get_form('bawstats_view_selection');
  221 }
  222 
  223 /**
  224  * set the desired viewing section using the current view
  225  */
  226 
  227 function bawstats_set_section($section='') {
  228   global $BAW_CONF;
  229 
  230   $month = '0';
  231   $year = '0';
  232 
  233   if (isset($_SESSION['bawstats'])) {
  234     if (isset($_SESSION['bawstats']['month'])) $month = $_SESSION['bawstats']['month'];
  235     if (isset($_SESSION['bawstats']['year']))  $year  = $_SESSION['bawstats']['year'];
  236   }
  237 
  238   if ((strlen($section)>1)&&(intval($month)>0)&&(intval($year)>0)) drupal_goto($BAW_CONF['drupal_base'] . '/' . $section . '/' . $month . '/' . $year);
  239   else drupal_goto('admin/bawstats');
  240 }
  241 
  242 /**
  243  * perform the chosen display settings
  244  */
  245 
  246 function bawstats_view_selection_submit($form, &$form_state) {
  247   global $BAW_CURR, $BAW_MES, $BAW_LIB, $BAW_CONF;
  248 
  249   $site_access = variable_get('bawstats_admin_access', 0);
  250 
  251   $form_values = $form_state['values'];
  252 
  253   if (isset($form_values['year'])) {
  254     $year = $form_values['year'];
  255   } else {
  256     $year = date('Y');
  257   }
  258 
  259   if (isset($form_values['month'])) {
  260     $month = array_search($form_values['month'],$BAW_CURR['months']);
  261   } else {
  262     $month = date('m');
  263   }
  264 
  265   if ($site_access>0) {
  266     // ensure we are allowed to change sites
  267     if (isset($form_values['site'])) {
  268       // alter the session site record
  269       $_SESSION['bawstats']['site_name'] = $form_values['site'];
  270     }
  271   }
  272 
  273   if (variable_get('bawstats_page_view',0)==0) $redirect = $BAW_CONF['drupal_base'] . '/all/' . $month . '/' . $year;
  274   else {
  275     $statgroups = variable_get('bawstats_groups',array());
  276     $keys = array_keys($statgroups);
  277     $redirect = $BAW_CONF['drupal_base'] . '/' . $keys[0]  . '/' . $month . '/' . $year;
  278   }
  279   $form_state['redirect'] = $redirect;
  280 }
  281 
  282 /**
  283  * mirror function to betterawstats baw_sites_dropdown() [core/display_helpers.php]
  284  */
  285 
  286 function bawstats_sites_dropdown() {
  287   global $BAW_CURR,$BAW_DFILES, $BAW_MES;
  288   $sites = array();
  289 
  290   $detected = array_keys($BAW_DFILES);
  291   $site_access = variable_get('bawstats_admin_access', 0);
  292 
  293   if (count($detected)>0) {
  294     foreach ($detected as $site) {
  295       $sites[$site] = $site;
  296     }
  297     if ($site_access>0) {
  298       # add the "all" options
  299       $sites['all_months'] = $BAW_MES['all_months'];
  300       $sites['all_days'] = $BAW_MES['all_days'];
  301     }
  302   } // else there is a problem - no sites data!
  303 
  304   return $sites;
  305 }
  306 
  307 /**
  308  * mirror function to betterawstats baw_date_dropdown()
  309  */
  310 
  311 function bawstats_months_dropdown() {
  312   global $BAW_CURR, $BAW_MES;
  313 
  314   $months = array();
  315 
  316   foreach ($BAW_CURR['months'] as $month) {
  317     $months[$month] = $month;
  318   }
  319   return $months;
  320 }
  321 
  322 /**
  323  * perform the display, main calls to the betterawstats library
  324  *
  325  */
  326 
  327 function bawstats_display() {
  328   global $BAW_CURR, $BAW_LIB;
  329 
  330   $out = "<div class=\"contentbox\">\n";
  331 
  332   if (@$BAW_CURR['site_name'] == 'all_months') {
  333     $settings['section'] = "months";
  334     $out .= baw_display_drupal($settings);
  335   } else if (@$BAW_CURR['site_name'] == 'all_days') {
  336     $settings['section'] = "days";
  337     $out .= baw_display_drupal($settings);
  338   } else if (@strlen($BAW_CURR['drupal_section'])>1) {
  339     $settings['section'] = $BAW_CURR['drupal_section'];
  340     $out .= baw_display_drupal($settings);
  341   } else {
  342     if (variable_get('bawstats_page_view',0)>0) {  // sub-pages
  343       if (@strlen($BAW_CURR['drupal_group'])>1) {
  344         $settings['group'] = $BAW_CURR['drupal_group'];
  345       } else {
  346         // as we are in multi-page view, produce the first entry only
  347         $groupkeys = array_keys($BAW_LIB['item_groups']);
  348         $settings['group'] = $groupkeys[0];
  349       }
  350       $out .= baw_display_drupal($settings);
  351     } else $out .= baw_display_drupal();
  352   }
  353 
  354   $out .= "</div>\n";
  355 
  356   return $out;
  357 }
  358 
  359 /**
  360  * mirror function to, and based on, betterawstats baw_display_index()
  361  */
  362 
  363 function baw_display_drupal($settings = array()) {
  364   global $BAW_CONF, $BAW_CONF_DIS, $BAW_CONF_DIS_DEF, $BAW_D, $BAW_MES, $BAW_CURR, $BAW_LIB, $BAW_DFILES;
  365 
  366   // sort the charts according to order in config
  367   $BAW_CONF_DIS = baw_array_sorting($BAW_CONF_DIS, 'order', SORT_ASC);
  368 
  369   // ---------- CONTENT BOX --------------------------------------------------
  370   // create the main content
  371   $content = "<div class=\"baw-content\">\n";
  372   $nodata = 0;
  373 
  374   if (isset($settings['section'])) {
  375     $section = $settings['section'];
  376     $function = "baw_display_$section";
  377     $temp_conf = $BAW_CONF_DIS[$section];
  378     $temp_conf['top_x'] = false;
  379     $temp_conf['chart'] = false;
  380     $temp_conf['table'] = true;
  381     if (($section == 'months') || ($section == 'days')){
  382       $temp_conf['chart'] = true;
  383       $temp_conf['table'] = false;
  384     } else {
  385       $temp_conf['chart'] = false;
  386       $temp_conf['table'] = true;
  387     }
  388     $temp_conf['name'] = $BAW_CONF_DIS_DEF[$section]['name'];
  389     if (function_exists($function)) {
  390       $stats_html .= $function($temp_conf);
  391       if ((strlen($stats_html)> 1) || (!$BAW_CONF['hideempty'])) {
  392     $content .= $stats_html;
  393     $nodata += 1;
  394       }
  395     }
  396     if ($nodata==0) {
  397       $content .= 'No data found for site <em>'.$BAW_CURR['site_name'].'</em> during '.$BAW_CURR['month'].'/'.$BAW_CURR['year'];
  398     }
  399   } else {
  400     $singlegroup = false;
  401     if (isset($settings['group'])) {
  402       $showonly = $settings['group'];
  403       $singlegroup = true;
  404     } else $showonly = '';
  405     $content .= "\n<!-- CONTENT START ++++++++++++++++++++++++++++++++++++++ -->\n";
  406     foreach ($BAW_LIB['item_groups'] as $group => $group_details) {
  407       if (($showonly==$group)||($showonly=='')) {
  408         $group_items = $group_details['members'];
  409     $has_overview = in_array('overview',$group_items);
  410     if ($singlegroup&&(!$has_overview)) {
  411       // pretend to display the overview, as it sets some BAW_CURR variables that may be needed
  412       // by other groups (namely, session)
  413       $sect_data = $BAW_CONF_DIS['overview'];
  414       $sect_data['name'] = 'overview';
  415       $junk = baw_display_overview($sect_data);
  416     }
  417         $menubuttons = '';
  418         $stats_section = '';
  419         $item_id = 0;
  420         $count_members = count($group_details['members']);
  421         $content .= "\n<!-- GROUP $group START ++++++++++++++++++++++++++++++++++++++ -->\n";
  422 
  423         foreach ($group_details['members'] as $item) {
  424           $sect_data = $BAW_CONF_DIS[$item];
  425           $sect_data['name'] = $item;
  426           //if ($item == 'weekdays' && !isset($BAW_CURR['wdays']['count'])) {
  427           //  $setarr = array('collapse' => false, 'chart' => false,'table' => false);
  428           //  baw_display_weekdays($setarr);
  429           //}
  430           $class = 'aws_data';
  431       $function = "baw_display_$item";
  432       $title = $BAW_CONF_DIS_DEF[$item]['name'];
  433           if (($sect_data['show'] == true)&&(function_exists($function))) {
  434         $stats_html = $function($sect_data);
  435         if ((strlen($stats_html)> 1) || (!$BAW_CONF['hideempty'])) {
  436           $stats_section .= "\n<!-- ITEM {$title} START from function {$item}++++++++++++++++++++++++++++++++++++++ -->\n";
  437           $collapse = false;
  438           $name = "{$group}_$item_id";
  439               $stats_section .= "<h3 class=\"$class\" id=\"h2_$name\">$title</h3>\n"
  440                              . "<div class=\"$class\" id=\"box_$name\"$show>\n"
  441                      . $stats_html
  442                              . "</div>";
  443           $stats_section .= "\n<!-- ITEM $title END ++++++++++++++++++++++++++++++++++++++ -->\n";
  444             }
  445           }
  446           $item_id++;
  447         }
  448     if ((strlen($stats_section)> 1) || (!$BAW_CONF['hideempty'])) {
  449       $content .= '<h2>' . $group_details['title'] . '</h2>';
  450       $content .= $stats_section;
  451       $nodata +=1;
  452     }
  453         $content .= "\n<!-- GROUP $group END ++++++++++++++++++++++++++++++++++++++ -->\n";
  454       }
  455     }
  456     $content .= "\n<!-- CONTENT END ++++++++++++++++++++++++++++++++++++++ -->\n";
  457     if ($nodata==0) {
  458       $content .= 'No data found for site <em>'.$BAW_CURR['site_name'].'</em> during '.$BAW_CURR['month'].'/'.$BAW_CURR['year'];
  459     }
  460   }
  461   $content .=  "</div>";
  462   return $content;
  463   //  return filter_xss($content, array('a', 'em', 'strong', 'cite', 'code', 'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'table', 'td', 'tr', 'th', 'div', 'img', 'br', 'h1', 'h2', 'h3'));
  464 }