source: extensions/event_cats/include/evntcats_main_funcs.inc.php @ 4276

Last change on this file since 4276 was 4276, checked in by LucMorizur, 15 years ago

[Event Cats] Duplication management, add table and begin $_POST analysis

File size: 17.3 KB
Line 
1<?php
2
3// +-----------------------------------------------------------------------+
4// | Piwigo - a PHP based picture gallery                                  |
5// +-----------------------------------------------------------------------+
6// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
7// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
8// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
9// +-----------------------------------------------------------------------+
10// | This program is free software; you can redistribute it and/or modify  |
11// | it under the terms of the GNU General Public License as published by  |
12// | the Free Software Foundation                                          |
13// |                                                                       |
14// | This program is distributed in the hope that it will be useful, but   |
15// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
16// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
17// | General Public License for more details.                              |
18// |                                                                       |
19// | You should have received a copy of the GNU General Public License     |
20// | along with this program; if not, write to the Free Software           |
21// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
22// | USA.                                                                  |
23// +-----------------------------------------------------------------------+
24
25// ***************************************************************************
26// ** evntcats_admin_funcs.php : Main functions (include)                   **
27// ** for Piwigo plugin Event Cats                                          **
28// ***************************************************************************
29
30// +-----------------------------------------------------------------------+
31// | Header                                                                |
32// +-----------------------------------------------------------------------+
33
34include_once(EVNTCATS_PATH.'include/ec_conf.inc.php');
35
36if (!isset($ec_lists)) {
37  $ec_lists               = array();
38  $ec_lists['add_pages']  = array();
39  $ec_lists['categories'] = array();
40  $ec_lists['user_ids']   = array();
41  $ec_lists['ec_table']   = array();
42}
43
44// +-----------------------------------------------------------------------+
45// | Utilities functions                                                   |
46// +-----------------------------------------------------------------------+
47
48/*
49 * is_in($haystack, $needle)
50 * returns true or false whether $needle is a string found in string $haystack
51 *
52 * @param
53 *   $haystack : the string in which to search
54 *   $needle   : the string looked for
55 * @return
56 *   true if $needle is found in $haystack ; false if not
57 */
58function is_in($haystack, $needle) {
59  return (strpos($haystack, $needle) !== false);
60}
61
62/*
63 * ec_image_exists($cat, $img)
64 * returns true or false whether the image is associated with the category.
65 *
66 * @param
67 *   $cat : the category
68 *   $img : the image
69 * @return
70 *   treu or false whether the image is associated with the category.
71 */
72function ec_image_exists($cat, $img) {
73  return (mysql_fetch_row(pwg_query("
74    SELECT *
75    FROM `".IMAGE_CATEGORY_TABLE."`
76    WHERE `category_id` = ".$cat."
77     AND `image_id` = ".$img
78  )) !== false);
79}
80
81/*
82 * str_from_var($var)
83 * returns a string easing array var informations displaying in Piwigo :
84 *   _ the string return value starts with"<p align="left">" ;
85 *   _ all "TAB" characters (chr(10)) are replaced by "<br>" ;
86 *   _ all spaces are replaced by "&nbsp;".
87 *
88 * @param
89 *   $var : variable to display
90 * @return
91 *   string easy to display in Piwigo
92 */
93function str_from_var($var) {
94  return
95   '<p align="left">'.
96   str_replace(
97    chr(10),'<br>',
98    str_replace(' ','&nbsp;', print_r /* var_dump */ ($var,true))
99   ).
100   '</p>';
101}
102
103/*
104 * ec_inspect()
105 * goes through ec_lists['ec_table'] to check errors on (multiple) entries
106 * using the same code. Cannot really be used elsewhere than here in
107 * build_ec_table() .
108 *
109 * @param
110 *   
111 * @return
112 *   (no return value)
113 */
114function ec_inspect($checked_item, $new_action, $check_ec_nok = true) {
115  global $ec_lists;
116  $first = array();
117  $to_correct = array();
118 
119  /*
120  // $to_correct is needed cause following code would not work everywhere :
121  foreach ($table as $value) {
122    if ($value == $value_to_check and $value == $problem) {
123      foreach ($table as &$value2) { // key index of $table can be reset
124        if ($value2 == $value_to_check) {
125          $value2 = $better_value;
126        }
127      }
128    }
129  }
130  // It works with WinAmp Server, but not on Apache server of Free (french
131  // Internet provider).
132  */
133 
134  foreach ($ec_lists['ec_table'] as $ec_entry) {
135    $ec_current_code = $ec_entry['code'];
136    if (
137     $ec_entry['action'] == 'ec_ok' or
138     ($check_ec_nok and $ec_entry['action'] == 'ec_nok')
139    ) {
140      if (isset($first[$ec_current_code])) {
141        // $first[$ec_current_code] is set <=> code has already been met.
142        // Checked item MUST be equal to the first item (thus all items) for
143        // this code.
144        if (
145         $first[$ec_current_code] != $ec_entry[$checked_item] or
146         ($new_action == '' and $ec_entry[$checked_item] == 'true')
147        ) $to_correct[$ec_current_code] = true; // value not used in fact
148        // but using $ec_current_code as a key makes a smaller table
149      } // if the error comes back several times
150      else $first[$ec_current_code] = $ec_entry[$checked_item];
151    }
152  }
153  foreach ($ec_lists['ec_table'] as &$ec_entry) { // & is needed here
154    if (isset($to_correct[$ec_entry['code']])) {
155      if ($new_action == '') {
156        if (!pwg_query("
157          UPDATE `".EVNTCATS_AUTOLOG_TABLE."`
158          SET `forced` = 'false'
159          WHERE `id` = ".$ec_entry['id']
160        )) die('Could not fix a "_f_pb"');
161        $ec_entry['forced'] = 'false';
162      }
163      else $ec_entry['action'] = $new_action;
164    }
165  }
166}
167
168// +-----------------------------------------------------------------------+
169// | Tables building functions                                             |
170// +-----------------------------------------------------------------------+
171
172/*
173 * build_ec_addp()
174 * builds $ec_lists['add_pages'].
175 *
176 * @param
177 *   (no parameter)
178 * @return
179 *   (no return value)
180 */
181function build_ec_addp() {
182  global $ec_lists, $ec_ap_ok;
183  if ($ec_ap_ok) {
184    $res = pwg_query("SELECT `id`, `title` FROM `".ADD_PAGES_TABLE."`");
185    while ($r = mysql_fetch_assoc($res)) {
186      $c = (is_in($r['title'], '/user_id=')) ? '/user_id=' : '/group_id=';
187      $a = explode($c ,$r['title']);
188      $ec_lists['add_pages'][$r['id']] = $a[0];
189    }
190  }
191}
192
193/*
194 * build_ec_categories($dsp)
195 * builds $ec_lists['categories'].
196 *
197 * @param
198 *   whether $ec_lists['categories'] must be of type "cat / sub-cat" or
199 *   or "cat <CR LF> - sub-cat".
200 * @return
201 *   (no return value)
202 */
203function build_ec_categories($dsp) {
204  global $template, $ec_lists;
205  $c = array();
206  display_select_cat_wrapper("
207     SELECT `id`, `name`, `uppercats`, `global_rank`
208     FROM `".CATEGORIES_TABLE."`
209   ", $c, 'category_options', $dsp);
210  $ec_lists['categories'] = $template->smarty->_tpl_vars['category_options'];
211}
212
213/*
214 * build_ec_userids()
215 * builds $ec_lists['user_ids'].
216 *
217 * @param
218 *   (no parameter)
219 * @return
220 *   (no return value)
221 */
222function build_ec_userids() {
223  global $ec_lists, $conf;
224  $ec_lists['user_ids'] = simple_hash_from_query("
225    SELECT
226      ".$conf['user_fields']['id']." AS id,
227      ".$conf['user_fields']['username']." AS username
228    FROM `".USERS_TABLE."`
229    WHERE id > 2;",
230    'id', 'username'
231  );
232}
233
234/*
235 * build_ec_table()
236 * builds a table showing the content of the <pwg>_event_cats database table,
237 * and a table showing the eventual errors.
238 *
239 * @param
240 *   no parameters passed ; the main material on which works the function, is
241 *   the global array variable $ec_lists.
242 * @return
243 *   (no return value)
244 */
245function build_ec_table() {
246  global $ec_lists, $ec_ap_ok;
247 
248  $q = pwg_query("
249    SELECT * FROM `".EVNTCATS_AUTOLOG_TABLE."`
250    WHERE `code` IS NOT NULL
251    ORDER BY `id`
252  ");
253  while ($r = mysql_fetch_assoc($q))
254   $ec_lists['ec_table'][intval($r['id'])] = $r;
255 
256  // Construction of behaviour
257 
258  // Multiple action params for a single code check
259  ec_inspect('action', 'ec_nok_action_pb');
260 
261  // Multiple user_ids for a single code check
262  ec_inspect('user_id', 'ec_nok_userid_pb', false);
263 
264  // Multiple "forced" params for a single code check
265  ec_inspect('forced', '');
266 
267  // User id and associated page validities checks
268  foreach ($ec_lists['ec_table'] as &$ec_entry) {
269   
270    // Check if associated user_id exists
271    if (
272     is_in($ec_entry['action'], 'ec_ok') and
273     !array_key_exists($ec_entry['user_id'], $ec_lists['user_ids'])
274    ) $ec_entry['action'] = 'ec_nok_userid_miss';
275   
276    // Check if associated displayed page exists
277    $a = 0;
278    if (
279      !empty($ec_entry['arg1']) and
280      // (Only arg2 is significant if action is ec_nok[_xxx] .)
281      is_in($ec_entry['action'], 'ec_ok')
282    ) $a++;
283    if (!empty($ec_entry['arg2'])) $a+= 2;
284    switch ($a) {
285      // case 0 : home, nothing to check
286     
287      case 2: // Additional Page
288        if (
289          $ec_ap_ok and (
290            is_in($ec_entry['action'], 'ec_ok') or
291            $ec_entry['action'] == 'ec_nok'
292          ) and
293          !array_key_exists($ec_entry['arg2'], $ec_lists['add_pages'])
294        ) $ec_entry['action'].= '_ap_pb';
295      break;
296     
297      case 1: // Category
298      case 3: // Image
299        if (is_in($ec_entry['action'], 'ec_ok')) {
300          if (array_key_exists($ec_entry['arg1'], $ec_lists['categories'])) {
301           if ($a == 3) // case 3: // Image
302            if (!ec_image_exists($ec_entry['arg1'], $ec_entry['arg2']))
303             $ec_entry['action'].= '_img_pb';
304          }
305          else $ec_entry['action'].= '_cat_pb';
306        }
307      break;
308    }
309  }
310}
311
312/*
313 * build_ec_lists()
314 * builds the main array variable contaning all informations for the plugin
315 *
316 * @param
317 *   no parameter passed, the main material on which works the function, is
318 *   the global array variable $ec_lists.
319 * @return
320 *   (no return value)
321 */
322function build_ec_lists() {
323 
324  global $ec_ap_ok, $template, $ec_lists, $conf;
325 
326  // Construction of $ec_lists['add_pages'] array var
327  build_ec_addp();
328 
329  // Construction of $ec_lists['categories'] array var
330  build_ec_categories(true);
331 
332  // Construction of $ec_lists['user_ids'] array var
333  build_ec_userids();
334 
335  // Construction of $ec_lists['ec_table'] array var
336  build_ec_table();
337}
338
339// +-----------------------------------------------------------------------+
340// | Duplication analysis functions                                        |
341// +-----------------------------------------------------------------------+
342
343/*
344 * build_dup_arrays($append_tpl = false)
345 * builds arrays telling which accounts are allowed to display a duplicate
346 * account link. Returns an array of all the user ids allowed to duplicate.
347 *
348 * @param
349 *   $append_tpl : tells if $template must be appended with built arrays
350 * @return
351 *   (no return value)
352 */
353function build_dup_arrays($append_tpl = false) {
354  global $template, $ec_lists, $conf;
355 
356  if (count($ec_lists['user_ids']) == 0) build_ec_userids();
357 
358  // A lot of below code has simply been copied from file cat_perm.php .
359  // Many thanks to people who wrote it !
360 
361  $groups = simple_hash_from_query("
362    SELECT `id`, `name`
363      FROM `".GROUPS_TABLE."`
364      ORDER BY `name` ASC;
365    ",
366    'id', 'name'
367  );
368 
369  // groups granted to duplication
370  $groups_granted_ids = order_by_name(
371    array_from_query("
372      SELECT `val`
373        FROM ".EVNTCATS_DUPLIC_TABLE."
374        WHERE `item` = 'grp'
375      ;",
376      'val'
377    ),
378    $groups
379  );
380 
381  // users directly granted to duplication
382  $users_granted_direct_ids = order_by_name(
383    array_from_query("
384      SELECT `val`
385        FROM ".EVNTCATS_DUPLIC_TABLE."
386        WHERE `item` = 'usr'
387      ;",
388      'val'
389    ),
390    $ec_lists['user_ids']
391  );
392 
393  // Calculate users granted to duplication thanks to belonging to a
394  // certain group (in groups, level (friends, family, contacts),
395  // or user status (generic))
396  $users_granted_ids = array();
397 
398  $users_granted_thks_gen_ids = array();
399  if ($ec_gen_granted = (read_ec_conf('duplic_gen') != '0')) {
400    $users_granted_thks_gen_ids = order_by_name(
401      array_diff(
402        array_from_query("
403          SELECT `user_id`
404          FROM `".USER_INFOS_TABLE."`
405          WHERE `status` = 'generic';",
406          'user_id'
407        ),
408        $users_granted_direct_ids
409      ),
410      $ec_lists['user_ids']
411    );
412    foreach ($users_granted_thks_gen_ids as $user_id)
413     $users_granted_ids[$user_id] = l10n('user_status_generic');
414  }
415
416  $types = array(
417    '1' => l10n('Level 1'),
418    '2' => l10n('Level 2'),
419    '4' => l10n('Level 4'),
420  );
421  $types_granted_ids = array();
422  $users_granted_thks_types_ids = array();
423  $c = intval(read_ec_conf('duplic_type'));
424  if ($c != 0) {
425    if (($c & 1) != 0) $types_granted_ids[] = '1';
426    if (($c & 2) != 0) $types_granted_ids[] = '2';
427    if (($c & 4) != 0) $types_granted_ids[] = '4';
428    if (count($types_granted_ids) > 0) {
429      $user_granted_from_type = array();
430      $result = pwg_query("
431        SELECT `user_id`, `level`
432        FROM `".USER_INFOS_TABLE."`
433        WHERE `level` IN (".implode(',', $types_granted_ids).");
434      ");
435      while ($row = mysql_fetch_array($result)) {
436        if (!isset($user_granted_from_type[$row['level']])) {
437          $user_granted_from_type[$row['level']] = array();
438        }
439        $user_granted_from_type[$row['level']][] = $row['user_id'];
440      }
441      $user_granted_by_type_ids = array();
442      foreach ($user_granted_from_type as $type_users)
443       $user_granted_by_type_ids = array_merge(
444        $user_granted_by_type_ids,
445        $type_users
446       );
447      $users_granted_thks_types_ids = order_by_name(
448        array_diff(
449          array_unique($user_granted_by_type_ids),
450          $users_granted_direct_ids
451        ),
452        $ec_lists['user_ids']
453      );
454      foreach ($users_granted_thks_types_ids as $user_id)
455       foreach ($user_granted_from_type as $type_id => $type_users) {
456        if (in_array($user_id, $type_users)) {
457          $users_granted_ids[$user_id]= $types[$type_id];
458          break;
459        }
460       }
461    }
462  }
463
464  $users_granted_thks_groups_ids = array();
465  if (count($groups_granted_ids) > 0) {
466    $granted_groups = array();
467
468    $result = pwg_query("
469      SELECT `user_id`, `group_id`
470      FROM `".USER_GROUP_TABLE."`
471      WHERE `group_id` IN (".implode(',', $groups_granted_ids).");
472    ");
473    while ($row = mysql_fetch_array($result)) {
474      if (!isset($granted_groups[$row['group_id']])) {
475        $granted_groups[$row['group_id']] = array();
476      }
477      $granted_groups[$row['group_id']][] = $row['user_id'];
478    }
479   
480    $user_granted_by_group_ids = array();
481   
482    foreach ($granted_groups as $group_users)
483     $user_granted_by_group_ids = array_merge(
484      $user_granted_by_group_ids,
485      $group_users
486     );
487    $users_granted_thks_groups_ids = order_by_name(
488      array_diff(
489        array_unique($user_granted_by_group_ids),
490        $users_granted_direct_ids
491      ),
492      $ec_lists['user_ids']
493    );
494    foreach ($users_granted_thks_groups_ids as $user_id)
495     foreach ($granted_groups as $group_id => $group_users)
496      if (in_array($user_id, $group_users)) {
497        $users_granted_ids[$user_id]= $groups[$group_id];
498        break;
499      }
500   
501  }
502
503  if ($append_tpl) {
504    $users_denied_ids = order_by_name(
505      array_diff(
506        array_keys($ec_lists['user_ids']),
507        $users_granted_thks_gen_ids,
508        $users_granted_thks_types_ids,
509        $users_granted_thks_groups_ids,
510        $users_granted_direct_ids
511      ),
512      $ec_lists['user_ids']
513    );
514   
515    foreach ($users_granted_ids as $u => $g) $template->append(
516      'user_granted_indirects',
517      array(
518        'USER'  => $ec_lists['user_ids'][$u],
519        'GROUP' => $g
520      )
521    );
522   
523    $template->assign('all_groups', $groups);
524    $template->assign('groups_granted_ids', $groups_granted_ids);
525    $template->assign('groups_denied_ids', order_by_name(
526      array_diff(array_keys($groups), $groups_granted_ids), $groups
527    ));
528    $template->assign('ec_gen_granted', $ec_gen_granted);
529    $template->assign('all_types', $types);
530    $template->assign('types_granted_ids', $types_granted_ids);
531    $template->assign('types_denied_ids', order_by_name(
532      array_diff(array_keys($types), $types_granted_ids), $types
533    ));
534    $template->assign('all_users', $ec_lists['user_ids']);
535    $template->assign('users_granted_direct_ids', $users_granted_direct_ids);
536    $template->assign('users_denied_ids', $users_denied_ids);
537  }
538  $users_granted_ids = array_merge(
539    array_keys($users_granted_ids),
540    $users_granted_direct_ids
541  );
542 
543  // Returns an array which values are all the user_ids allowed to display a
544  // "duplicate" link. The keys of this array are strange (for direct allowed
545  // users, keys are usernames), but normally not used
546  return $users_granted_ids;
547}
548
549/*
550 * dup_allowed($user_id)
551 * returns true if the user_id is allowed to display a duplicate link
552 *
553 * @param
554 *   $user_id : the user_id
555 * @return
556 *   true if the user_id is allowed to display a duplicate link
557 */
558function dup_allowed($user_id) {
559  return true;
560}
561
562?>
Note: See TracBrowser for help on using the repository browser.