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

Last change on this file since 6769 was 4486, checked in by LucMorizur, 14 years ago

[Event Cats] Continue EN translation ; fix bugs 1324 and 1325

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