source: extensions/event_cats/include/evntcats_admin_funcs.inc.php @ 4333

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

[Event Cats] Remove bug types could not be granted to duplication when from blank

File size: 17.4 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 : Admin functions (include)                  **
27// ** for Piwigo plugin Event Cats                                          **
28// ***************************************************************************
29
30// +-----------------------------------------------------------------------+
31// | Header                                                                |
32// +-----------------------------------------------------------------------+
33
34if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
35
36global $conf, $page;
37
38// +-----------------------------------------------------------------------+
39// | Utilities functions                                                   |
40// +-----------------------------------------------------------------------+
41
42/*
43 * ec_end1()
44 * Process repetitive task when error in database modifying functions.
45 *
46 * @param
47 *   $pst : $_POST argument
48 *   $msg : message
49 * @return
50 *   false as this function is used when there is a problem
51 */
52function ec_end1($pst, $msg) {
53  global $page;
54  if (isset($_POST[$pst]))
55   $page['errors'][] =
56    l10n($msg).
57    '$_POST[\''.$pst.'\'] = '.
58    $_POST[$pst];
59  else
60    $page['errors'][] =
61    l10n($msg).
62    '$_POST[\''.$pst.'\'] unset';
63  return false;
64}
65
66/*
67 * ec_end2()
68 * Process repetitive task when error in database modifying functions.
69 *
70 * @param
71 *   $n : number to display
72 * @return
73 *   false as this function is used when there is a problem
74 */
75function ec_end2($n) {
76  global $page;
77  $page['errors'][] =
78   sprintf(l10n('ec_DB_problem'), $n).
79   'MySQL error '.mysql_errno().', "'.mysql_error().'"';
80  return false;
81}
82
83/*
84 * ec_create_user_OK()
85 * Creates new generic user and eventually new group as described in $_POST.
86 * Assumes that the validity of the different indexes of $_POST it uses, have
87 * already been checked.
88 *
89 * @param
90 *   no param needed
91 * @return
92 *   the created user_id or false whether all operations suceeded or not
93 */
94function ec_create_user_OK() {
95  global $page;
96 
97  // This function assumes that the validity of the different indexes of
98  // $_POST it uses, have already been checked.
99 
100  // User creation, as generic
101  $ec_user_id = false;
102  $page['errors'] = register_user(
103   $_POST['login'], $_POST['password'], '', false
104  );
105  if (
106    count($page['errors']) != 0 or
107    !($ec_user_id = get_userid($_POST['login']))
108  ) {
109    array_unshift($page['errors'], l10n('ec_user_create_pb'));
110    return false;
111  }
112  else
113   $page['infos'][] = sprintf(l10n('ec_user_create_OK'), $_POST['login']);
114  if (
115    pwg_query("
116      UPDATE `".USER_INFOS_TABLE."`
117      SET `status` = 'generic'
118      WHERE `user_id` = ".$ec_user_id.";
119    ") !== false
120  )
121   $page['infos'][] = sprintf(l10n('ec_user_generic_OK'), $_POST['login']);
122  else
123   $page['errors'][] = sprintf(l10n('ec_user_generic_pb'), $_POST['login']);
124 
125  // New group creation if required,
126  // and association with user_id at the same time
127  if (
128    isset($_POST['ec_in_up_newgroup']) and
129    isset($_POST['groupname']) and
130    $_POST['groupname'] != ''
131  ) {
132    $t2 = 0; $t3 = false; $t4 = false;
133    while (
134      !($t3 = mysql_fetch_row(pwg_query("
135        SELECT `id`
136        FROM `".GROUPS_TABLE."`
137        WHERE `name` = '".$_POST['groupname']."';
138      "))) and
139      $t2++ == 0
140    ) $t4 = pwg_query("
141        INSERT INTO `".GROUPS_TABLE."` (`name`, `is_default`)
142        VALUES ('".$_POST['groupname']."', 'false');
143      ");
144    if ($t4)
145     $page['infos'][] =
146      sprintf(l10n('ec_group_create_OK'), $_POST['groupname']);
147    if (!$t3)
148     $page['errors'][] =
149      sprintf(l10n('ec_group_create_pb'), $_POST['groupname']).' (1) : '.
150      'MySQL error '.mysql_errno().', "'.mysql_error().'"';
151    if (
152      pwg_query("
153        INSERT INTO `".USER_GROUP_TABLE."` (`user_id`, `group_id`)
154        VALUES ('".$ec_user_id."', '".$t3[0]."');
155      ") === false
156    ) $page['errors'][] =
157      sprintf(l10n('ec_group_create_pb'), $_POST['groupname']).' (2) : '.
158     'MySQL error '.mysql_errno().', "'.mysql_error().'"';
159    else $page['infos'][] = sprintf(
160      l10n('ec_group_create_OK2'),
161      $_POST['login'], $_POST['groupname']
162    );
163  }
164  return $ec_user_id;
165}
166
167// +-----------------------------------------------------------------------+
168// | Tables building functions                                             |
169// +-----------------------------------------------------------------------+
170
171/*
172 * build_ec_duplicable_codes()
173 *
174 *
175 * @param
176 *   no parameter passed, the main material on which works the function, is
177 *   the global array variable $ec_lists.
178 * @return
179 *   (no return value)
180 */
181function build_ec_duplicable_codes() {
182  global $ec_lists, $template;
183  $ec_lists['duplicable_codes'] = array();
184  $t                            = array();
185  foreach ($ec_lists['ec_table'] as $ec_entry) {
186    if (
187      is_in($ec_entry['action'], 'ec_ok') and
188      $ec_entry['forced'] == 'false'
189    ) {
190      $t[$ec_entry['id']] = $ec_entry['code'];
191      $ec_lists['duplicable_codes']['codes'][$ec_entry['code']]['id'] =
192       $ec_entry['id'];
193      $ec_lists['duplicable_codes']['codes'][$ec_entry['code']]['user_id'] =
194       $ec_entry['user_id'];
195    }
196  }
197  foreach ($t as $ec_id => $ec_code) {
198    $ec_lists['duplicable_codes']['ids'][$ec_id] =
199     $ec_lists['duplicable_codes']['codes'][$ec_code]['id'];
200  }
201 
202  // Builds a category list displayed a best way
203  build_ec_categories(false);
204}
205
206// +-----------------------------------------------------------------------+
207// | Database modifying functions                                          |
208// +-----------------------------------------------------------------------+
209
210/*
211 * ec_create_modify_entry_OK()
212 * returns true or false whether the creation of a new entry described by
213 * $_POST was OK or not.
214 *
215 * @param
216 *   no param
217 * @return
218 *   true if creation was OK ; false if not
219 */
220function ec_create_modify_entry_OK() {
221  global $page, $ec_lists, $ec_debug;
222 
223  // $_POST validity checks : action prevented in case of bad arguments
224 
225  if (!isset($_POST['ec_act1']))
226   return ec_end1('ec_act1', 'Bad argument : ');
227 
228  if (
229    ($_POST['ec_act1']) != 'toggle_forced' and
230    !isset($_POST['ec_input_action'])
231  ) return ec_end1('ec_input_action', 'Bad argument : ');
232 
233  $is_creation = true;
234  $ec_user_id = 'NULL';
235  $action = 'ec_ok';
236  $del_other = false;
237  switch ($_POST['ec_act1']) {
238   
239    // This "switch" command is a little bit tricky... it has been a pain to
240    // debug, and I wish to nobody to have to modify it :-\ !
241    // Its principle is that it manages checks for four occurrences of
242    // $_POST['ec_act1'] : 'create', 'modify_entry_submit',
243    // 'duplicate_entry_submit', and 'toggle_forced'. Some checks are mutual
244    // between different occurences, but never all checks of each occurrence
245    // of $_POST['ec_act1']. So tests are done with "if" statements to
246    // produce "break" statements when needed.
247   
248    case 'create':
249     
250      // Stops if given code or user type are incorrect
251      if (
252        !isset($_POST['ec_in_up_code']) or
253        !ereg('^[a-zA-Z0-9_-]{4,32}$', $_POST['ec_in_up_code'])
254      ) return ec_end1('ec_in_up_code', 'Improper code : ');
255      else $ec_code = $_POST['ec_in_up_code'];
256     
257      foreach ($ec_lists['ec_table'] as $ec_entry)
258       if ($ec_code == $ec_entry['code'])
259        return ec_end1('ec_in_up_code', 'Code already exists : ');
260     
261      if (
262        !isset($_POST['ec_sel_user']) or (
263          $_POST['ec_sel_user'] != 'new' and
264          $_POST['ec_sel_user'] != 'old'
265        )
266      ) return ec_end1('ec_sel_user', 'Bad argument : ');
267   
268    case 'modify_entry_submit':
269     
270      // First checks for user type and/or value
271      if (isset($_POST['ec_sel_user'])) {
272        if ($_POST['ec_sel_user'] == 'new') {
273          if (
274            !isset($_POST['login']) or
275            $_POST['login'] == ''
276          ) return ec_end1('login', 'Bad argument : ');
277          if (in_array($_POST['login'], $ec_lists['user_ids']))
278           return ec_end1('login', 'User already exists : ');
279        }
280        elseif ($_POST['ec_sel_user'] == 'old') {
281          if (!isset($_POST['ec_in_up_usr_list']))
282           return ec_end1('login', 'Bad argument : ');
283          $ec_user_id = $_POST['ec_in_up_usr_list'];
284          if (!array_key_exists($ec_user_id, $ec_lists['user_ids']))
285           return ec_end1('ec_in_up_usr_list', 'User doesn\'t exist : ');
286        }
287        else $action = 'ec_nok';
288      }
289      else $action = 'ec_nok';
290     
291    if ($_POST['ec_act1'] == 'create') break;
292     
293    case 'duplicate_entry_submit':
294     
295      // Checks of entry value validity
296      if (
297        !isset($_POST['ec_entry_sel']) or
298        !array_key_exists($_POST['ec_entry_sel'], $ec_lists['ec_table'])
299      ) return ec_end1(
300        'ec_entry_sel', 'Code doesn\'t exist or non-duplicable code : '
301      );
302     
303      // Other checks for user type and value
304      if ($_POST['ec_act1'] == 'modify_entry_submit') if (
305        !isset($_POST['ec_sel_user']) or (
306          $_POST['ec_sel_user'] == 'new' or
307          $_POST['ec_sel_user'] == 'none' or (
308            $_POST['ec_sel_user'] == 'old' and
309            $_POST['ec_in_up_usr_list'] !=
310             $ec_lists['ec_table'][$_POST['ec_entry_sel']]['user_id']
311          )
312        )
313      ) $del_other = true;
314     
315    case 'toggle_forced':
316     
317      // Establish default values for unchanged values
318      $ec_code    = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['code'];
319      if ($action == 'ec_ok' and $ec_user_id == 'NULL')
320       $ec_user_id = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['user_id'];
321      $arg1       = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['arg1'];
322      $arg2       = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['arg2'];
323      if (empty($arg1)) $arg1 = 'NULL';
324      if (empty($arg2)) $arg2 = 'NULL';
325      if (empty($ec_user_id)) $ec_user_id = 'NULL';
326      if ($_POST['ec_act1'] == 'toggle_forced') {
327        $forced = (
328          $ec_lists['ec_table'][$_POST['ec_entry_sel']]['forced'] == 'true'
329        ) ? 'false' : 'true';
330        $del_other = ($forced == 'true');
331        $action     = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['action'];
332      }
333     
334    if (
335      $_POST['ec_act1'] == 'toggle_forced' or
336      $_POST['ec_act1'] == 'modify_entry_submit'
337    ) {
338      $is_creation = false;
339      break;
340    }
341     
342      // Final check for entry value
343      build_ec_duplicable_codes();
344      if (!array_key_exists($_POST['ec_entry_sel'],
345       $ec_lists['duplicable_codes']['ids'])
346      ) return ec_end1(
347        'ec_entry_sel', 'Code doesn\'t exist or non-duplicable code : '
348      );
349     
350    break;
351    default: ec_end1('ec_act1', 'Bad argument : ');
352  }
353  // Pfew !
354 
355  if ($_POST['ec_act1'] != 'toggle_forced') {
356    // Preparation of $arg1, $arg2
357    switch ($_POST['ec_input_action']) {
358      case 'add_p': // Additional Page
359        if (isset($_POST['ec_in_up_aps'])) $arg2 = $_POST['ec_in_up_aps'];
360        else ec_end1('ec_in_up_aps', 'Bad argument : ');
361        $arg1 = 'NULL';
362      break;
363      case 'cat':   // Category
364      case 'img':   // Image
365        if (isset($_POST['ec_in_up_cat'])) {
366          $arg1 = $_POST['ec_in_up_cat'];
367          if ($_POST['ec_input_action'] == 'img') {
368            if (isset($_POST['ec_in_up_img'])) $arg2 = $_POST['ec_in_up_img'];
369            else ec_end1('ec_in_up_img', 'Bad argument : ');
370          }
371          else $arg2 = 'NULL';
372        }
373        else ec_end1('ec_in_up_cat', 'Bad argument : ');
374      break;
375      case 'home':    // Home : nothing to do : "arg"s are ''
376      case 'refused': // $_POST['ec_sel_user'] unset, nothing to do
377        $arg1 = 'NULL'; $arg2 = 'NULL';
378      break;
379      default: ec_end1('ec_input_action', 'Bad argument : ');
380    }
381   
382    // Preparation of $forced
383    $forced = (isset($_POST['ec_in_up_forced'])) ? 'true' : 'false';
384    if ($_POST['ec_act1'] == 'duplicate_entry_submit' and $forced == 'true')
385     return ec_end1('ec_in_up_forced', 'Bad argument : ');
386   
387    // User and eventually group creation, if needed
388    if ($_POST['ec_act1'] != 'duplicate_entry_submit')
389     if (isset($_POST['ec_sel_user']) and $_POST['ec_sel_user'] == 'new')
390      if (!($ec_user_id = ec_create_user_OK())) return false;
391  }
392 
393  // Now we have all infos : check that future entry doesn't exist already
394  $arg1p = ($arg1 == 'NULL') ? 'IS NULL' : ' = '.$arg1;
395  $arg2p = ($arg2 == 'NULL') ? 'IS NULL' : ' = '.$arg2;
396  $ec_user_idp = ($ec_user_id == 'NULL') ? 'IS NULL' : ' = '.$ec_user_id;
397  if (($t1 = mysql_fetch_row(pwg_query(// $q =
398  "
399    SELECT `id`
400    FROM `".EVNTCATS_TABLE."`
401    WHERE `code`    = '".$ec_code."'
402      AND `user_id`    ".$ec_user_idp."
403      AND `action`  = '".$action."'
404      AND `arg1`       ".$arg1p."
405      AND `arg2`       ".$arg2p."
406      AND `forced`  = '".$forced."'
407  "))) !== false) { // print("<pre>$arg1 $arg2<br>$q</pre>");
408    $page['errors'][] = sprintf(l10n('ec_entry_already_exists'), $t1[0]);
409    return false;
410  }
411 
412  // Delete other entries using the same code, if needed
413  if ($del_other) {
414    if ((
415      $t1 = mysql_fetch_row(pwg_query("
416        SELECT `code`
417        FROM `".EVNTCATS_TABLE."`
418        WHERE `id` = ".$_POST['ec_entry_sel']
419      ))) === false
420    ) die('Entry not found in DB ?!');
421    $r = pwg_query("
422      SELECT `id`
423      FROM `".EVNTCATS_TABLE."`
424      WHERE `code` = '".$t1[0]."'
425       AND `id` <> ".$_POST['ec_entry_sel']
426    );
427    while ($t2 = mysql_fetch_row($r)) if (!ec_delete_entry_OK($t2[0]))
428     return false;
429  }
430 
431  $ec_debug[] = array(
432    'del_other' => $del_other,
433    'code' => $ec_code,
434    'user_id' => $ec_user_id,
435    'user_idp' => $ec_user_idp,
436    'action' => $action,
437    'arg1' => $arg1,
438    'arg2' => $arg2,
439    'arg1p' => $arg1p,
440    'arg2p' => $arg2p,
441    'forced' => $forced,
442  );
443 
444  // Action !
445  $ret = true;
446  if ($is_creation) {
447    if (
448      pwg_query("
449        INSERT INTO `".EVNTCATS_TABLE."` (
450          `code`,
451          `user_id`,
452          `action`,
453          `arg1`, `arg2`, `forced`
454        )
455        VALUES (
456          '".$ec_code."',
457           ".$ec_user_id.",
458          '".$action."',
459          ".$arg1.", ".$arg2.", '".$forced."'
460        );
461      ") === false
462    ) {
463      $page['errors'][] =
464       l10n('ec_entry_create_pb').' : '.
465       'MySQL error '.mysql_errno().', "'.mysql_error().'"'
466      ;
467      $ret = false;
468    }
469    else {
470      build_ec_lists(); // Don't remember exactly why, but must be done here
471      $forced = ($forced == 'false') ? '' : l10n('ec_cnfrm_forced');
472      $page['infos'][] =
473       sprintf(l10n('ec_entry_create_OK'), mysql_insert_id()).' : '.
474       $ec_code.' => '.
475       $ec_lists['user_ids'][$ec_user_id].$forced
476      ;
477      return true;
478    }
479  }
480  else {
481    if (
482      pwg_query("
483        UPDATE `".EVNTCATS_TABLE."`
484        SET
485          `user_id` =  ".$ec_user_id.",
486          `action`  = '".$action."',
487          `arg1`    =  ".$arg1.",
488          `arg2`    =  ".$arg2.",
489          `forced`  = '".$forced."'
490        WHERE `id`  = '".$_POST['ec_entry_sel']."'
491      ") === false
492    ) {
493      $page['errors'][] =
494       l10n('ec_entry_create_pb').' : '.
495       'MySQL error '.mysql_errno().', "'.mysql_error().'"'
496      ;
497      $ret = false;
498    }
499    else $page['infos'][] = sprintf(
500      l10n('ec_entry_modify_OK'), $_POST['ec_entry_sel']
501    );
502  }
503  build_ec_lists();
504  return $ret;
505}
506
507/*
508 * ec_delete_entry_OK($ec_id)
509 * tries to delete an existing entry.
510 *
511 * @param
512 *   $ec_id : the entry to be deleted
513 * @return
514 *   true or false whether deleting succeeded.
515 */
516function ec_delete_entry_OK($ec_id) {
517  global $page;
518  if (pwg_query("
519    DELETE FROM `".EVNTCATS_TABLE."`
520    WHERE `id` = ".$ec_id
521  ) === false) {
522    $page['errors'][] =
523     sprintf(l10n('ec_entry_del_nok'),
524     $ec_id).
525     'MySQL error '.mysql_errno().', "'.mysql_error().'"';
526    return false;
527  }
528  else $page['infos'][] = sprintf(l10n('ec_entry_del_ok'), $ec_id);
529  return true;
530}
531
532?>
Note: See TracBrowser for help on using the repository browser.