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

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

[Event Cats] Add comments

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