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

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

[Event Cats] Replace table by fieldsets in autolog_new.tpl ; begin help

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