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

Revision 4263, 17.0 KB checked in by LucMorizur, 10 years ago (diff)

[Event Cats] Continue duplication management

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_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_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, $ec_debug;
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  $group_granted_ids = order_by_name(
371    array_from_query("
372      SELECT `arg2`
373        FROM ".EVNTCATS_TABLE."
374        WHERE `code` IS NULL
375          AND `arg1` = '1'
376          AND `arg2` IS NOT NULL
377      ;",
378      'arg2'
379    ),
380    $groups
381  );
382 
383  // users directly granted to duplication
384  $users_granted_direct_ids = order_by_name(
385    array_from_query("
386      SELECT `arg2`
387        FROM ".EVNTCATS_TABLE."
388        WHERE `code` IS NULL
389          AND `arg1` = '2'
390          AND `arg2` IS NOT NULL
391      ;",
392      'arg2'
393    ),
394    $ec_lists['user_ids']
395  );
396 
397  // Calculate users granted to duplication thanks to belonging to a
398  // certain group (in groups, level (friends, family, contacts),
399  // or user status (generic))
400  $user_granted_ids = array();
401
402  $users_granted_thks_gen_ids = array();
403  if (read_ec_conf('duplic_gen') != '0') {
404    $users_granted_thks_gen_ids = order_by_name(
405      array_diff(
406        array_from_query("
407          SELECT `user_id`
408          FROM `".USER_INFOS_TABLE."`
409          WHERE `status` = 'generic';",
410          'user_id'
411        ),
412        $users_granted_direct_ids
413      ),
414      $ec_lists['user_ids']
415    );
416    foreach ($users_granted_thks_gen_ids as $user_id)
417     $user_granted_ids[$user_id] = l10n('user_status_generic');
418  } global $ec_debug; $ec_debug['1'] = $user_granted_ids;
419
420  $users_granted_thks_types_ids = array();
421  $c = intval(read_ec_conf('duplic_type'));
422  if ($c != 0) {
423    $t = array();
424    if (($c & 1) != 0) $t[] = '1';
425    if (($c & 2) != 0) $t[] = '2';
426    if (($c & 4) != 0) $t[] = '4';
427    if (count($t) > 0) {
428      $types = array(
429        '1' => l10n('Level 1'),
430        '2' => l10n('Level 2'),
431        '4' => l10n('Level 4'),
432      );
433      $user_granted_from_type = array();
434      $result = pwg_query("
435        SELECT `user_id`, `level`
436        FROM `".USER_INFOS_TABLE."`
437        WHERE `level` IN (".implode(',', $t).");
438      ");
439      while ($row = mysql_fetch_array($result)) {
440        if (!isset($user_granted_from_type[$row['level']])) {
441          $user_granted_from_type[$row['level']] = array();
442        }
443        $user_granted_from_type[$row['level']][] = $row['user_id'];
444      }
445      $user_granted_by_type_ids = array();
446      foreach ($user_granted_from_type as $type_users)
447       $user_granted_by_type_ids = array_merge(
448        $user_granted_by_type_ids,
449        $type_users
450       );
451      $users_granted_thks_types_ids = order_by_name(
452        array_diff(
453          array_unique($user_granted_by_type_ids),
454          $users_granted_direct_ids
455        ),
456        $ec_lists['user_ids']
457      );
458      foreach ($users_granted_thks_types_ids as $user_id)
459       foreach ($user_granted_from_type as $type_id => $type_users) {
460        if (in_array($user_id, $type_users)) {
461          $user_granted_ids[$user_id]= $types[$type_id];
462          break;
463        }
464       }
465    }
466  } $ec_debug['2'] = $user_granted_ids;
467
468  $users_granted_thks_groups_ids = array();
469  if (count($group_granted_ids) > 0) {
470    $granted_groups = array();
471
472    $result = pwg_query("
473      SELECT `user_id`, `group_id`
474      FROM `".USER_GROUP_TABLE."`
475      WHERE `group_id` IN (".implode(',', $group_granted_ids).");
476    ");
477    while ($row = mysql_fetch_array($result)) {
478      if (!isset($granted_groups[$row['group_id']])) {
479        $granted_groups[$row['group_id']] = array();
480      }
481      $granted_groups[$row['group_id']][] = $row['user_id'];
482    }
483   
484    $user_granted_by_group_ids = array();
485   
486    foreach ($granted_groups as $group_users)
487     $user_granted_by_group_ids = array_merge(
488      $user_granted_by_group_ids,
489      $group_users
490     );
491    $users_granted_thks_groups_ids = order_by_name(
492      array_diff(
493        array_unique($user_granted_by_group_ids),
494        $users_granted_direct_ids
495      ),
496      $ec_lists['user_ids']
497    );
498    foreach ($users_granted_thks_groups_ids as $user_id)
499     foreach ($granted_groups as $group_id => $group_users)
500      if (in_array($user_id, $group_users)) {
501        $user_granted_ids[$user_id]= $groups[$group_id];
502        break;
503      }
504   
505  } $ec_debug['3'] = $user_granted_ids;
506
507  if ($append_tpl) {
508    $user_denied_ids = order_by_name(
509      array_diff(
510        array_keys($ec_lists['user_ids']),
511        $users_granted_thks_gen_ids,
512        $users_granted_thks_types_ids,
513        $users_granted_thks_groups_ids,
514        $users_granted_direct_ids
515      ),
516      $ec_lists['user_ids']
517    );
518   
519    foreach ($user_granted_ids as $u => $g) $template->append(
520      'user_granted_indirects',
521      array(
522        'USER'  => $ec_lists['user_ids'][$u],
523        'GROUP' => $g
524      )
525    );
526   
527    $ec_debug['4'] = $user_granted_ids;
528    $template->assign('all_groups', $groups);
529    $template->assign('group_granted_ids', $group_granted_ids);
530    $template->assign('group_denied_ids',order_by_name(
531      array_diff(array_keys($groups), $group_granted_ids), $groups
532    ));
533    $template->assign('all_users', $ec_lists['user_ids']);
534    $template->assign('users_granted_direct_ids', $users_granted_direct_ids);
535    $template->assign('user_denied_ids', $user_denied_ids);
536  }
537   $user_granted_ids = array_merge(
538    $user_granted_ids,
539    $users_granted_direct_ids
540   );
541  return $user_granted_ids;
542}
543
544/*
545 * dup_allowed($user_id)
546 * returns true if the user_id is allowed to display a duplicate link
547 *
548 * @param
549 *   $user_id : the user_id
550 * @return
551 *   true if the user_id is allowed to display a duplicate link
552 */
553function dup_allowed($user_id) {
554  return true;
555}
556
557?>
Note: See TracBrowser for help on using the repository browser.