Index: /extensions/event_cats/include/ec_conf.inc.php
===================================================================
--- /extensions/event_cats/include/ec_conf.inc.php	(revision 3962)
+++ /extensions/event_cats/include/ec_conf.inc.php	(revision 3962)
@@ -0,0 +1,121 @@
+﻿<?php
+
+// +-----------------------------------------------------------------------+
+// | Piwigo - a PHP based picture gallery                                  |
+// +-----------------------------------------------------------------------+
+// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
+// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
+// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
+// +-----------------------------------------------------------------------+
+// | This program is free software; you can redistribute it and/or modify  |
+// | it under the terms of the GNU General Public License as published by  |
+// | the Free Software Foundation                                          |
+// |                                                                       |
+// | This program is distributed in the hope that it will be useful, but   |
+// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
+// | General Public License for more details.                              |
+// |                                                                       |
+// | You should have received a copy of the GNU General Public License     |
+// | along with this program; if not, write to the Free Software           |
+// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
+// | USA.                                                                  |
+// +-----------------------------------------------------------------------+
+
+/*****************************************************************************
+Parameters ($ec_conf) :
+ _ plugin activated ($ec_conf[0]) yes/no ;
+ _ duplication allowance ($ec_conf[1]) :
+   0 : no account ;
+   1 : all accounts ;
+   2 : no account but those specified ;    \
+   3 : all accounts but those specified.   | could be different finally
+ _ what to do in case an unknown code is used with "autolog" parameter
+   ($ec_conf[2]) :
+   0 : nothing ("goto" home page, index.php (not logged in of course)) ;
+   1 : redirected to "access denied" page ;
+   2 : redirected to an Additional Page precised below.
+ _ the id of the Additional Page which must be used in case of usage of an
+   unknow code ($ec_conf[3]).
+*****************************************************************************/
+
+global $ec_conf;
+
+// $ec_conf initalization / check
+if ($t = mysql_fetch_row(pwg_query("
+ SELECT `value`
+ FROM `".CONFIG_TABLE."`
+ WHERE `param` = 'event_cats';
+"))) {
+  $ec_conf = explode(',' , $t[0]);
+}
+else {
+  $ec_conf = explode(',' , '0,1,0,0');
+  change_ec_conf('activated', 0);
+}
+
+/*
+ * change_ec_conf($c, $v)
+ * updates Event Cats configuration values in the database as well as in
+ * $ec_conf.
+ *
+ * @param
+ *   $c : conf value to update ;
+ *   $v : value to give to $ec_conf[$c].
+ * @return
+ *   true or false whether the change was OK or not
+ */
+function change_ec_conf($c, $v) {
+  global $ec_conf;
+  switch ($c) {
+    case 'activated':
+      $d = 0;
+    break;
+    case 'dup_allow':
+      $d = 1;
+    break;
+    case 'unknown_code':
+      $d = 2;
+    break;
+    case 'unknown_code_ap_id':
+      $d = 3;
+    break;
+    default: return false;
+  }
+  $ec_conf[$d] = $v;
+  return pwg_query("
+   UPDATE `".CONFIG_TABLE."`
+   SET `value` = '".implode(',', $ec_conf)."'
+   WHERE `param` = 'event_cats';
+  ");
+}
+
+/*
+ * read_ec_conf($c)
+ * returns the value in $ec_conf using string keys.
+ *
+ * @param
+ *   $c : conf value to update ;
+ * @return
+ *   $ec_conf value
+ */
+function read_ec_conf($c) {
+  global $ec_conf;
+  switch ($c) {
+    case 'activated':
+      return $d[0];
+    break;
+    case 'dup_allow':
+      return $d[1];
+    break;
+    case 'unknown_code':
+      return $d[2];
+    break;
+    case 'unknown_code_ap_id':
+      return $d[3];
+    break;
+    default: return false;
+  }
+}
+
+?>
Index: /extensions/event_cats/include/evntcats_funcs.inc.php
===================================================================
--- /extensions/event_cats/include/evntcats_funcs.inc.php	(revision 3962)
+++ /extensions/event_cats/include/evntcats_funcs.inc.php	(revision 3962)
@@ -0,0 +1,317 @@
+﻿<?php
+
+// +-----------------------------------------------------------------------+
+// | Piwigo - a PHP based picture gallery                                  |
+// +-----------------------------------------------------------------------+
+// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
+// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
+// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
+// +-----------------------------------------------------------------------+
+// | This program is free software; you can redistribute it and/or modify  |
+// | it under the terms of the GNU General Public License as published by  |
+// | the Free Software Foundation                                          |
+// |                                                                       |
+// | This program is distributed in the hope that it will be useful, but   |
+// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
+// | General Public License for more details.                              |
+// |                                                                       |
+// | You should have received a copy of the GNU General Public License     |
+// | along with this program; if not, write to the Free Software           |
+// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
+// | USA.                                                                  |
+// +-----------------------------------------------------------------------+
+
+// ***************************************************************************
+// ** evntcats_funcs.php : Functions (include) for Piwigo plugin Event Cats **
+// ***************************************************************************
+
+global $conf, $ec_conf;
+
+include_once(EVNTCATS_PATH.'include/ec_conf.inc.php');
+
+/*
+ * build_ec_table()
+ * builds a table showing the content of the <pwg>_event_cats database table,
+ * and a table showing the eventual errors.
+ *
+ * @param
+ *   no parameters passed ; the main material on which works the function, is
+ *   the global array variable $ec_lists.
+ * @return
+ *   (no return value)
+ */
+function build_ec_table() {
+  global $ec_lists, $conf, $ec_ap_ok;
+  
+  $ec_lists['ec_table'] = array();
+  // $ec_lists['ec_codes'] = array(); // Useful ?
+  
+  if (isset($conf['auto_log'])) {
+    // For tests purpose : table not in MySQL, but in $conf['xxx'] vars in
+    // config_local.inc.php
+    
+    foreach ($conf['auto_log'] as $ec_current_code => $ec_entry) {
+      $ec_current_entry = array_push($ec_lists['ec_table'], array(
+       'code'    => $ec_current_code,
+       'user_id' => get_userid($ec_entry[$ec_current_code]),
+       'action'  => 'ec_ok',
+       'arg1'    => NULL,
+       'arg2'    => NULL,
+       'forced'  => 'false',
+      )) - 1; // key($ec_lists['ec_table']) does not work as expected
+      $ec_lists['ec_table'][$ec_current_entry]['id'] = $ec_current_entry;
+      /* $ec_lists['ec_codes'][$ec_current_code] = array(
+       'count' => 1,
+       'id'    => $ec_current_entry
+      ); */
+      if (isset($conf['prior_page'])) {
+        if (array_key_exists($ec_current_code, $conf['prior_page'])) {
+          $ec_lists['ec_table'][$ec_current_entry]['arg2'] =
+           $conf['prior_page'][$ec_current_code];
+          $ec_lists['ec_table'][$ec_current_entry]['forced'] = 'true';
+        }
+      }
+      if (isset($conf['outdated_page'])) {
+        if (array_key_exists($ec_current_code, $conf['outdated_page'])) {
+          $ec_lists['ec_table'][$ec_current_entry]['action'] = 'ec_nok';
+          $ec_lists['ec_table'][$ec_current_entry]['arg2'] =
+           $conf['outdated_page'][$ec_current_code];
+          $ec_lists['ec_table'][$ec_current_entry]['forced'] = 'true';
+        }
+      }
+    }
+    
+    // Inspection of $conf['outdated_page']
+    if (isset($conf['outdated_page'])) {
+      foreach ($conf['outdated_page'] as $ec_current_code => $ec_current_AP) {
+        if (!array_key_exists($ec_current_code, $conf['auto_log'])) {
+          $ec_current_entry = array_push($ec_lists['ec_table'], array(
+           'code'    => $ec_current_code,
+           'user_id' => '',
+           'action'  => 'ec_nok',
+           'arg1'    => NULL,
+           'arg2'    => $ec_current_AP,
+           'forced'  => 'true',
+          )) - 1; // key($ec_lists['ec_table']) problem
+          $ec_lists['ec_table'][$ec_current_entry]['id'] = $ec_current_entry;
+          /* $ec_lists['ec_codes'][$ec_current_code] = array(
+           'id'     => $ec_current_entry,
+           'count'  => 1
+          ); */
+        }
+      }
+    }
+    
+  }
+  else { // in "normal" use ($conf['auto_log'] NOT set), $ec_lists['ec_table']
+         // is built thanks to below code only. A little bit more simple...
+    $q = pwg_query('
+     SELECT * FROM `'.EVNTCATS_TABLE.'`
+     WHERE `code` IS NOT NULL
+     ORDER BY `id`
+    ');
+    while ($r = mysql_fetch_assoc($q)) {
+      $ec_lists['ec_table'][intval($r['id'])] = $r;
+      /* $ec_current_entry = intval($r['id']);
+      $ec_current_code = $r['code'];
+      if (array_key_exists($ec_current_code, $ec_lists['ec_codes'])) {
+        $ec_lists[$ec_current_code]['count']++;
+      }
+      else {
+        $ec_lists['ec_codes'][$ec_current_code] = array(
+         'id'     => $ec_current_entry,
+         'count'  => 1
+        ); */
+    }
+  }
+  
+  // Construction of behaviour
+  
+  /*
+   * ec_inpect()
+   * goes through ec_lists['ec_table'] to check errors on (multiple) entries
+   * using the same code. Cannot be used elsewhere than here in
+   * build_ec_table() .
+   *
+   * @param
+   *   
+   * @return
+   *   (no return value)
+   */
+  function ec_inpect($checked_item, $new_action,
+   $check_ec_nok = false, $check_forced = false) {
+    global $ec_lists;
+    $first = array();
+    $to_correct = array();
+    // $to_correct is needed because following code would not work anywhere :
+    /*
+    foreach ($table as $value) {
+      if ($value == $value_to_check and $value == $problem) {
+        foreach ($table as &$value2) { // key index of $table can be reset
+          if ($value2 == $value_to_check) {
+            $value2 = $better_value;
+          }
+        }
+      }
+    }
+    // It works with WinAmp Server, but not on Apache server of Free (french
+    // Internet provider).
+    */
+    foreach ($ec_lists['ec_table'] as &$ec_entry) { // & is not really needed
+      $ec_current_code = $ec_entry['code'];
+      if (
+       $ec_entry['action'] == 'ec_ok' or
+       ($check_ec_nok and $ec_entry['action'] == 'ec_nok')
+      ) {
+        if (isset($first[$ec_current_code])) {
+          // $first[$ec_current_code] is set <=> code has already been met.
+          // Checked item MUST be equal to the first item (thus all items) for
+          // this code.
+          if (
+           $first[$ec_current_code] != $ec_entry[$checked_item] or
+           ($check_forced and $ec_entry[$checked_item] == 'true')
+          ) {
+            $to_correct[$ec_current_code] = true; // value not used in fact
+          } // but using $ec_current_code as a key makes a smaller table
+        } // if the error comes back several times
+        else {
+          $first[$ec_current_code] = $ec_entry[$checked_item];
+        }
+      }
+    }
+    foreach ($ec_lists['ec_table'] as &$ec_entry) { // & is needed here
+      if (isset($to_correct[$ec_entry['code']])) {
+        $ec_entry['action'] = $new_action;
+      }
+    }
+  }
+
+  // Multiple action params for a single code check
+  ec_inpect('action', 'ec_nok_action_pb', true);
+  
+  // Multiple user_ids for a single code check
+  ec_inpect('user_id', 'ec_nok_userid_pb');
+  
+  // Multiple "forced" params for a single code check
+  ec_inpect('forced', 'ec_ok_forced_pb', true, true);
+  
+  // User id and associated page validities checks
+  foreach ($ec_lists['ec_table'] as &$ec_entry) {
+    
+    // Check if associated user_id exists
+    if (
+     $ec_entry['action'] == 'ec_ok' and
+     !array_key_exists($ec_entry['user_id'], $ec_lists['user_ids'])
+    ) {
+      $ec_entry['action'] = 'ec_nok_userid_miss';
+    }
+    
+    // Check if associated displayed page exists
+    $a = 0;
+    if (!empty($ec_entry['arg1']) and
+     (strpos($ec_entry['action'], 'ec_ok') !== false)) $a++; // Only arg2
+    // is significant if action is ec_nok[_xxx] .
+    if (!empty($ec_entry['arg2'])) $a+= 2;
+    switch ($a) {
+      // case 0 : home, nothing to check
+      
+      case 2: // Additional Page
+        if (
+          $ec_ap_ok and (
+            $ec_entry['action'] == 'ec_ok' or
+            $ec_entry['action'] == 'ec_nok'
+          ) and
+          !array_key_exists($ec_entry['arg2'], $ec_lists['add_pages'])
+        ) {
+          $ec_entry['action'].= '_ap_pb';
+        }
+      break;
+      
+      case 1: // Category
+      case 3: // Image
+        if ($ec_entry['action'] == 'ec_ok') {
+          if (array_key_exists($ec_entry['arg1'], $ec_lists['categories'])) {
+            if ($a == 3) { // case 3: // Image
+              if (!mysql_fetch_row(pwg_query('
+               SELECT *
+               FROM `'.IMAGE_CATEGORY_TABLE.'` 
+               WHERE `category_id` = '.$ec_entry['arg1'].'
+                AND `image_id` = '.$ec_entry['arg2']
+              ))) {
+                $ec_entry['action'].= '_img_pb';
+              }
+            }
+          }
+          else {
+            $ec_entry['action'].= '_cat_pb';
+          }
+        }
+      break;
+    }
+  }
+}
+
+/*
+ * build_ec_lists()
+ * builds the main array variable contaning all informations for the plugin
+ *
+ * @param
+ *   no parameter passed, the main material on which works the function, is
+ *   the global array variable $ec_lists.
+ * @return
+ *   (no return value)
+ */
+function build_ec_lists() {
+  
+  global $ec_ap_ok, $template, $ec_lists;
+  
+  $ec_lists = array();
+
+  // Construction of $ec_lists['add_pages'] array var
+  $ec_lists['add_pages'] = array();
+  if ($ec_ap_ok) {
+    $res = pwg_query('SELECT id,title FROM '.ADD_PAGES_TABLE);
+    while ($r = mysql_fetch_assoc($res)) {
+      $c = (strpos($r['title'], '/user_id=')) ? '/user_id=' : '/group_id=';
+      $a = explode($c ,$r['title']);
+      $ec_lists['add_pages'][$r['id']] = $a[0];
+    }
+  }
+  
+  // Construction of $ec_lists['categories'] array var
+  $c = array();
+  display_select_cat_wrapper('
+     SELECT id,name,uppercats,global_rank
+     FROM '.CATEGORIES_TABLE,
+   $c, 'category_options', true);
+  $ec_lists['categories'] = $template->smarty->_tpl_vars['category_options'];
+  
+  // Construction of $ec_lists['user_ids'] array var
+  $ec_lists['user_ids'] = array();
+  $q = pwg_query('SELECT id,username FROM '.USERS_TABLE. ' WHERE id > 3');
+  while ($r = mysql_fetch_assoc($q)) {
+    $ec_lists['user_ids'][$r['id']] = $r['username'];
+  }
+  
+  // Construction of $ec_lists['ec_table'] array var
+  build_ec_table();
+}
+
+/*
+ * str_from_var($var)
+ * returns a string easing array var informations displaying in Piwigo :
+ *   _ the string return value starts with"<p align="left">" ;
+ *   _ all "TAB" characters (chr(10)) are replaced by "<br>" ;
+ *   _ all spaces are replaced by "&nbsp;".
+ *
+ * @param
+ *   $var : variable to display
+ * @return
+ *   string easy to display in Piwigo
+ */
+function str_from_var($var) {
+  return '<p align="left">'.str_replace(chr(10),'<br>',str_replace(' ','&nbsp;', print_r /* var_dump */ ($var,true))).'</p>';
+}
+
+?>
