"Fossies" - the Fresh Open Source Software Archive

Member "betterawstats/includes/bawstats.admin.inc" (18 Mar 2008, 10635 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.admin.inc,v 1.4 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  * Menu callback; presents the bawstats admin settings page.
   34  */
   35 
   36 function bawstats_admin_settings() {
   37   global $BAW_CONF,$BAW_LIB;
   38   $form = array();
   39 
   40   // build the form for the settings
   41 
   42   $form['bawstats_config'] = array(
   43     '#type' => 'fieldset',
   44     '#title' => t('AWstats configuration'),
   45     '#collapsible' => TRUE,
   46     '#collapsed' => FALSE,
   47     '#weight' => -1
   48   );
   49   $form['bawstats_config']['bawstats_data'] = array(
   50     '#type' => 'textfield',
   51     '#size' => 40,
   52     '#maxlength' => 80,
   53     '#title' => t('AWstats data directory'),
   54     '#default_value' => variable_get('bawstats_data',''),
   55     '#description' => t('Set this value to the directory where AWStats saves its database files into. No trailing slash.'),
   56     '#weight' => -3
   57   );
   58   $form['bawstats_config']['bawstats_lib'] = array(
   59     '#type' => 'textfield',
   60     '#title' => t('AWstats library directory'),
   61     '#size' => 40,
   62     '#maxlength' => 80,
   63     '#default_value' => variable_get('bawstats_lib',''),
   64     '#description' => t('Set this value to the directory where the AWStats library files are located (the "lib" directory under the AWstats tree). No trailing slash.'),
   65     '#weight' => -2
   66   );
   67   $form['bawstats_config']['bawstats_lang'] = array(
   68     '#type' => 'textfield',
   69     '#title' => t('AWstats language directory'),
   70     '#size' => 40,
   71     '#maxlength' => 80,
   72     '#default_value' => variable_get('bawstats_lang',''),
   73     '#description' => t('Set this value to the directory where the AWStats language files are located (the "lang" directory under the AWstats tree). No trailing slash.'),
   74     '#weight' => -1
   75   );
   76   $bawstats_path = drupal_get_path('module', 'bawstats');
   77   $form['bawstats_config']['bawstats_icon'] = array(
   78     '#type' => 'textfield',
   79     '#title' => t('AWstats icon directory'),
   80     '#size' => 40,
   81     '#maxlength' => 80,
   82     '#default_value' => variable_get('bawstats_icon','/' . $bawstats_path . '/icon'),
   83     '#description' => t('Set this value to the URL where AWStats icons can be obtained. Note, as this is used for icon URLs, this must be in web file space.  The default is to copy the icons to a directory called "icon" in the toplevel of the BAWstats module directory') . ' (' . $bawstats_path . '/icon).',
   84     '#weight' => 0
   85   );
   86   $form['bawstats_settings'] = array(
   87     '#type' => 'fieldset',
   88     '#title' => t('BAWstats general settings'),
   89     '#collapsible' => TRUE,
   90     '#collapsed' => TRUE,
   91     '#weight' => 0
   92   );
   93   $form['bawstats_settings']['bawstats_page_view'] = array(
   94     '#type' => 'checkbox',
   95     '#title' => t('Split page view'),
   96     '#default_value' => variable_get('bawstats_page_view',0),
   97     '#description' => t('By default statistics for a single month are displayed on a single large page. Selecting this option splits the page into multiple sub-views.'),
   98     '#weight' => 1
   99   );
  100   // turn off display controle if we are not yet configured
  101   //if ($configured==0) $form['bawstats_settings']['bawstats_page_view']['#disabled']=TRUE;
  102 
  103   $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
  104   $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults') );
  105 
  106   if (!empty($_POST) && form_get_errors()) {
  107     drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
  108   }
  109 
  110   $form['#submit'][] = 'bawstats_admin_settings_submit';
  111   $form['#theme'] = 'system_settings_form';
  112 
  113   return $form;
  114 }
  115 
  116 function _bawstats_remove_slash($path) {
  117   if (substr($path,strlen($path)-1,1)=='/') return substr($path,0,strlen($path)-1);
  118   return $path;
  119 }
  120 
  121 function bawstats_admin_settings_validate($form, &$form_state) {
  122   global $BAW_CONF,$BAW_LIB,$BAW_CONF_DIS_DEF;
  123   $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  124 
  125   if ($op!=t('Reset to defaults')) {
  126     // check the key paths
  127 
  128     $settingOK = TRUE;
  129     $data_dir = _bawstats_remove_slash($form_state['values']['bawstats_data']);
  130 
  131     if (is_dir($data_dir)) {
  132       if (!is_readable($data_dir)) {
  133     form_set_error('bawstats_data', t('AWstats data directory "') . $data_dir . t('" is not accessible.'));
  134     $settingOK = FALSE;
  135       }
  136     } else {
  137       form_set_error('bawstats_data', t('AWstats data location "') . $data_dir . t('" is not a directory.'));
  138       $settingOK = FALSE;
  139     }
  140 
  141     if (!$settingOK) variable_del('bawstats_data');  // this is evil doing this in validate, but ensures clean fall-over
  142 
  143     $settingOK = TRUE;
  144     $lib_dir = _bawstats_remove_slash($form_state['values']['bawstats_lib']);
  145 
  146     if (is_dir($lib_dir)) {
  147       if (is_readable($lib_dir)) {
  148     // directory is there and readable, so check it contains the AWstats lib stuff
  149     if (!is_readable($lib_dir.'/domains.pm')) {
  150       form_set_error('bawstats_lib', t('AWstats library directory "') . $lib_dir . t('" does not contain the necessary library files?'));
  151       $settingOK = FALSE;
  152     }
  153       } else {
  154     form_set_error('bawstats_lib', t('AWstats library directory "') . $lib_dir . t('" is not accessible.'));
  155     $settingOK = FALSE;
  156       }
  157     } else {
  158       form_set_error('bawstats_lib', t('AWstats library location "') . $lib_dir . t('" is not a directory.'));
  159       $settingOK = FALSE;
  160     }
  161 
  162     if (!$settingOK) variable_del('bawstats_lib');  // this is evil doing this in validate, but ensures clean fall-over
  163 
  164     $settingOK = TRUE;
  165     $lang_dir = _bawstats_remove_slash($form_state['values']['bawstats_lang']);
  166 
  167     if (is_dir($lang_dir)) {
  168       if (is_readable($lang_dir)) {
  169     // directory is there and readable, so check it contains the AWstats lib stuff
  170     if (!is_readable($lang_dir.'/awstats-en.txt')) {
  171       form_set_error('bawstats_lang', t('AWstats library directory "') . $lang_dir . t('" does not contain the necessary language files?'));
  172       $settingOK = FALSE;
  173     }
  174       } else {
  175     form_set_error('bawstats_lang', t('AWstats language directory "') . $lang_dir . t('" is not accessible.'));
  176     $settingOK = false;
  177       }
  178     } else {
  179       form_set_error('bawstats_lang', t('AWstats language location "') . $lang_dir . t('" is not a directory.'));
  180       $settingOK = FALSE;
  181     }
  182 
  183     if (!$settingOK) variable_del('bawstats_lang');  // this is evil doing this in validate, but ensures clean fall-over
  184   }
  185 }
  186 
  187 
  188 function bawstats_admin_settings_submit($form, &$form_state) {
  189   global $BAW_CONF,$BAW_LIB,$BAW_CONF_DIS_DEF;
  190   $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  191 
  192   // Exclude unnecessary elements.
  193   unset($form_state['values']['submit'], $form_state['values']['reset'], $form_state['values']['form_id'], $form_state['values']['op'], $form_state['values']['form_token']);
  194 
  195   foreach ($form_state['values'] as $key => $value) {
  196     if ($op == t('Reset to defaults')) {
  197       variable_del($key);
  198     }
  199     else {
  200       if (is_array($value) && isset($form_state['values']['array_filter'])) {
  201         $value = array_keys(array_filter($value));
  202       }
  203       // Test the key is correct:
  204 
  205       if ($key == 'bawstats_data') {
  206     // assume validation has taken care of testing for directory/readable
  207         $value = _bawstats_remove_slash($value);
  208     // do some further checking
  209     $count = count(glob($value . "/*.txt"));
  210     if ($count==0) drupal_set_message(t('Warning, the AWstats data directory "') . $value . t('" does not contain any text data files'),'warning');
  211       }
  212 
  213       if ($key == 'bawstats_lib') {
  214     // assume validation has taken care of testing for directory/readable
  215     $value = _bawstats_remove_slash($value);
  216       }
  217 
  218       if ($key == 'bawstats_lang') {
  219     // assume validation has taken care of testing for directory/readable
  220     $value = _bawstats_remove_slash($value);
  221       }
  222 
  223       if ($key == 'bawstats_icon') {
  224     // assume validation has taken care of testing for directory/readable
  225         $value = _bawstats_remove_slash($value);
  226     $testvalue = substr($value,1);
  227     // do some further checking
  228     if (is_dir($testvalue)) {
  229       if (is_readable($testvalue)) {
  230         // directory is there and readable, so check it contains the AWstats lib stuff
  231         if (!is_readable($testvalue.'/browser')) {
  232           drupal_set_message(t('Warning, the AWstats icon path "') . $value . t('" does not appear to contain icon directories.'),'warning');
  233         }
  234       } else {
  235         drupal_set_message(t('Warning, the AWstats icon path "') . $value . t('" is not accessible.'),'warning');
  236       }
  237     } else {
  238       drupal_set_message(t('Warning, the AWstats icon path "') . $value . t('" is not a directory.'),'warning');
  239     }
  240       }
  241 
  242       // set the actual variable
  243       variable_set($key, $value);
  244     }
  245   }
  246 
  247   if ($op == t('Reset to defaults')) {
  248     drupal_set_message(t('The configuration options have been reset to their default values.'));
  249   }
  250   else {
  251     drupal_set_message(t('The configuration options have been saved.'));
  252   }
  253 
  254   // can only get this if configured
  255   if ((strlen($BAW_CONF['path_data'])>0)&&(strlen($BAW_CONF['path_lib'])>0)&&(strlen($BAW_CONF['path_lang'])>0)) {
  256     // create the stat groups var in case they are wanted post configuration!
  257     $statgroups = array();
  258     foreach ($BAW_LIB['item_groups'] as $group => $group_details) {
  259       $statgroups[check_plain($group)] = check_plain($group_details['title']);
  260     }
  261     variable_set('bawstats_groups',$statgroups);
  262 
  263     // similarly with the stat sections
  264     $statsections = array();
  265     foreach ($BAW_CONF_DIS_DEF as $key => $value) {
  266       $statgroups[] = $key;
  267     }
  268     variable_set('bawstats_sections',$statsections);
  269   }
  270 
  271   menu_rebuild();
  272 }