| 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 | // Keeps file coded in UTF-8 without BOM: é |
|---|
| 26 | |
|---|
| 27 | // *************************************************************************** |
|---|
| 28 | // ** evntcats_admin_funcs.php : Admin functions (include) ** |
|---|
| 29 | // ** for Piwigo plugin Event Cats ** |
|---|
| 30 | // *************************************************************************** |
|---|
| 31 | |
|---|
| 32 | // +-----------------------------------------------------------------------+ |
|---|
| 33 | // | Header | |
|---|
| 34 | // +-----------------------------------------------------------------------+ |
|---|
| 35 | |
|---|
| 36 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
|---|
| 37 | |
|---|
| 38 | global $conf, $page; |
|---|
| 39 | |
|---|
| 40 | // +-----------------------------------------------------------------------+ |
|---|
| 41 | // | Utilities functions | |
|---|
| 42 | // +-----------------------------------------------------------------------+ |
|---|
| 43 | |
|---|
| 44 | /* |
|---|
| 45 | * ec_end1() |
|---|
| 46 | * Process repetitive task when error in database modifying functions. |
|---|
| 47 | * |
|---|
| 48 | * @param |
|---|
| 49 | * $pst : $_POST argument |
|---|
| 50 | * $msg : message |
|---|
| 51 | * @return |
|---|
| 52 | * false as this function is used when there is a problem |
|---|
| 53 | */ |
|---|
| 54 | function ec_end1($pst, $msg) { |
|---|
| 55 | global $page; |
|---|
| 56 | if (isset($_POST[$pst])) |
|---|
| 57 | $page['errors'][] = |
|---|
| 58 | l10n($msg). |
|---|
| 59 | '$_POST[\''.$pst.'\'] = '. |
|---|
| 60 | $_POST[$pst]; |
|---|
| 61 | else |
|---|
| 62 | $page['errors'][] = |
|---|
| 63 | l10n($msg). |
|---|
| 64 | '$_POST[\''.$pst.'\'] unset'; |
|---|
| 65 | return false; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /* |
|---|
| 69 | * ec_end2() |
|---|
| 70 | * Process repetitive task when error in database modifying functions. |
|---|
| 71 | * |
|---|
| 72 | * @param |
|---|
| 73 | * $n : number to display |
|---|
| 74 | * @return |
|---|
| 75 | * false as this function is used when there is a problem |
|---|
| 76 | */ |
|---|
| 77 | function ec_end2($n) { |
|---|
| 78 | global $page; |
|---|
| 79 | my_error(sprintf(l10n('ec_DB_problem'), $n), false); |
|---|
| 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 | */ |
|---|
| 94 | function ec_create_user_OK() { |
|---|
| 95 | global $page, $ec_lists; |
|---|
| 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, and association with user_id, and if |
|---|
| 126 | // needed category or add. p., at the same time |
|---|
| 127 | if ( |
|---|
| 128 | isset($_POST['ec_in_up_newgroup']) and |
|---|
| 129 | isset($_POST['groupname']) and |
|---|
| 130 | $_POST['groupname'] != '' |
|---|
| 131 | ) { |
|---|
| 132 | // Checks if a group named $_POST['groupname'] already exists. |
|---|
| 133 | // If not, creates it : then, t4 is no more false. In any case, t3 gets |
|---|
| 134 | // the id of the group named $_POST['groupname']. |
|---|
| 135 | $t2 = 0; $t3 = false; $t4 = false; |
|---|
| 136 | while ( // The check is executed once at minimum |
|---|
| 137 | !($t3 = pwg_db_fetch_row(pwg_query(" |
|---|
| 138 | SELECT `id` |
|---|
| 139 | FROM `".GROUPS_TABLE."` |
|---|
| 140 | WHERE `name` = '".$_POST['groupname']."'; |
|---|
| 141 | "))) and |
|---|
| 142 | $t2++ == 0 // The check is executed twice at maximum |
|---|
| 143 | ) |
|---|
| 144 | $t4 = pwg_query(" |
|---|
| 145 | INSERT INTO `".GROUPS_TABLE."` (`name`, `is_default`) |
|---|
| 146 | VALUES ('".$_POST['groupname']."', 'false'); |
|---|
| 147 | "); // Cannot be executed twice |
|---|
| 148 | if ($t4) |
|---|
| 149 | $page['infos'][] = |
|---|
| 150 | sprintf(l10n('ec_group_create_OK'), $_POST['groupname']); |
|---|
| 151 | if (!$t3) my_error(sprintf(l10n('ec_group_create_pb'), |
|---|
| 152 | $_POST['groupname']).' (1) ', false); |
|---|
| 153 | if ( |
|---|
| 154 | pwg_query(" |
|---|
| 155 | INSERT INTO `".USER_GROUP_TABLE."` (`user_id`, `group_id`) |
|---|
| 156 | VALUES ('".$ec_user_id."', '".$t3[0]."'); |
|---|
| 157 | ") === false |
|---|
| 158 | ) my_error(sprintf(l10n('ec_group_create_pb'), |
|---|
| 159 | $_POST['groupname']).' (2) ', false); |
|---|
| 160 | else $page['infos'][] = sprintf( |
|---|
| 161 | l10n('ec_group_create_OK2'), |
|---|
| 162 | $_POST['login'], $_POST['groupname'] |
|---|
| 163 | ); |
|---|
| 164 | |
|---|
| 165 | // If a category id has been posted, the newly created group must be |
|---|
| 166 | // allowed to navigate in this category |
|---|
| 167 | // We are in the group creation block, thus the we know this group cannot |
|---|
| 168 | // be associated to any category |
|---|
| 169 | if ( |
|---|
| 170 | isset($_POST['ec_in_up_cat']) and |
|---|
| 171 | array_key_exists($_POST['ec_in_up_cat'], $ec_lists['categories']) |
|---|
| 172 | ) { |
|---|
| 173 | if (pwg_db_num_rows(pwg_query(" |
|---|
| 174 | SELECT `id` |
|---|
| 175 | FROM `".CATEGORIES_TABLE."` |
|---|
| 176 | WHERE `id` = '".$_POST['ec_in_up_cat']."'; |
|---|
| 177 | ")) == 0) return ec_end1('ec_in_up_cat', 'Category doesn\'t exist : '); |
|---|
| 178 | else { |
|---|
| 179 | $private_uppercats = array_from_query(" |
|---|
| 180 | SELECT `id` |
|---|
| 181 | FROM `".CATEGORIES_TABLE."` |
|---|
| 182 | WHERE `id` IN (". |
|---|
| 183 | implode(',', get_uppercat_ids(array($_POST['ec_in_up_cat']))). |
|---|
| 184 | ") |
|---|
| 185 | AND `status` = 'private'; |
|---|
| 186 | ", 'id'); |
|---|
| 187 | $inserts = array(); |
|---|
| 188 | foreach ($private_uppercats as $cat_id) |
|---|
| 189 | $inserts[] = array( |
|---|
| 190 | 'group_id' => $t3[0], |
|---|
| 191 | 'cat_id' => $cat_id |
|---|
| 192 | ); |
|---|
| 193 | mass_inserts(GROUP_ACCESS_TABLE,array('group_id','cat_id'), $inserts); |
|---|
| 194 | if (mysql_errno() == 0) |
|---|
| 195 | $page['infos'][] = sprintf( |
|---|
| 196 | l10n('ec_group_create_OK2'), |
|---|
| 197 | $_POST['groupname'],$ec_lists['categories'][$_POST['ec_in_up_cat']] |
|---|
| 198 | ); |
|---|
| 199 | else my_error(sprintf( |
|---|
| 200 | l10n('ec_assoc_pb'), |
|---|
| 201 | $_POST['groupname'], $ec_lists['categories'][$_POST['ec_in_up_cat']] |
|---|
| 202 | ), false); |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | // If an add. p. id has been posted, the newly created group must be |
|---|
| 207 | // allowed to navigate in this additional page |
|---|
| 208 | if ( |
|---|
| 209 | isset($_POST['ec_in_up_aps']) and |
|---|
| 210 | array_key_exists($_POST['ec_in_up_aps'], $ec_lists['add_pages']) |
|---|
| 211 | ) { |
|---|
| 212 | $granted_groups = array(); |
|---|
| 213 | $title_arr = array_from_query(" |
|---|
| 214 | SELECT `title` |
|---|
| 215 | FROM `".ADD_PAGES_TABLE."` |
|---|
| 216 | WHERE `id` = ".$_POST['ec_in_up_aps']."; |
|---|
| 217 | ", 'title'); |
|---|
| 218 | $t_user = (is_in($title_arr[0], '/user_id=')) ? |
|---|
| 219 | explode('/user_id=', $title_arr[0]) : array($title_arr[0]); |
|---|
| 220 | if (is_in($t_user[0], '/group_id=')) { |
|---|
| 221 | $t_group = explode('/group_id=', $t_user[0]); |
|---|
| 222 | $granted_groups = explode(',', $t_group[1]); |
|---|
| 223 | } |
|---|
| 224 | else $t_group[0] = $t_user[0]; |
|---|
| 225 | if (!in_array($t3[0], $granted_groups)) { |
|---|
| 226 | $granted_groups[] = $t3[0]; |
|---|
| 227 | $t_group[1] = implode(',', $granted_groups); |
|---|
| 228 | $t_user[0] = implode('/group_id=', $t_group); |
|---|
| 229 | if (pwg_query(" |
|---|
| 230 | UPDATE `".ADD_PAGES_TABLE."` |
|---|
| 231 | SET `title` = '".implode('/user_id=', $t_user)."' |
|---|
| 232 | WHERE `id` = ".$_POST['ec_in_up_aps']."; |
|---|
| 233 | ") === false) my_error(sprintf( |
|---|
| 234 | l10n('ec_assoc_pb'), |
|---|
| 235 | $_POST['groupname'], $ec_lists['add_pages'][$_POST['ec_in_up_aps']] |
|---|
| 236 | ), false); |
|---|
| 237 | else $page['infos'][] = sprintf( |
|---|
| 238 | l10n('ec_group_create_OK2'), |
|---|
| 239 | $_POST['groupname'],$ec_lists['add_pages'][$_POST['ec_in_up_aps']] |
|---|
| 240 | ); |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | else { |
|---|
| 245 | // If a category id has been posted, the newly created user must be |
|---|
| 246 | // allowed to navigate in this category, if it is not the case yet |
|---|
| 247 | if ( |
|---|
| 248 | isset($_POST['ec_in_up_cat']) and |
|---|
| 249 | array_key_exists($_POST['ec_in_up_cat'], $ec_lists['categories']) |
|---|
| 250 | ) { |
|---|
| 251 | $private_uppercats = array_from_query(" |
|---|
| 252 | SELECT `id` |
|---|
| 253 | FROM `".CATEGORIES_TABLE."` |
|---|
| 254 | WHERE `id` IN (". |
|---|
| 255 | implode(',', get_uppercat_ids(array($_POST['ec_in_up_cat']))). |
|---|
| 256 | ") |
|---|
| 257 | AND `status` = 'private'; |
|---|
| 258 | ", 'id'); |
|---|
| 259 | // We must not reinsert already existing lines in user_access table |
|---|
| 260 | $granteds = array(); |
|---|
| 261 | foreach ($private_uppercats as $cat_id) |
|---|
| 262 | $granteds[$cat_id] = array(); |
|---|
| 263 | $result = pwg_query(" |
|---|
| 264 | SELECT `user_id`, `cat_id` |
|---|
| 265 | FROM `".USER_ACCESS_TABLE."` |
|---|
| 266 | WHERE `cat_id` IN (".implode(',', $private_uppercats).") |
|---|
| 267 | AND `user_id` = '$ec_user_id'; |
|---|
| 268 | "); |
|---|
| 269 | while ($row = pwg_db_fetch_assoc($result)) |
|---|
| 270 | $granteds[$row['cat_id']][] = $row['user_id']; |
|---|
| 271 | $inserts = array(); |
|---|
| 272 | foreach ($private_uppercats as $cat_id) |
|---|
| 273 | if (!in_array($ec_user_id, $granteds[$cat_id])) |
|---|
| 274 | $inserts[] = array( |
|---|
| 275 | 'user_id' => $ec_user_id, |
|---|
| 276 | 'cat_id' => $cat_id |
|---|
| 277 | ); |
|---|
| 278 | if (count($inserts) != 0) { |
|---|
| 279 | mass_inserts(USER_ACCESS_TABLE, array('user_id','cat_id'), $inserts); |
|---|
| 280 | if (mysql_errno() == 0) |
|---|
| 281 | $page['infos'][] = sprintf( |
|---|
| 282 | l10n('ec_group_create_OK2'), |
|---|
| 283 | $_POST['login'], $ec_lists['categories'][$_POST['ec_in_up_cat']] |
|---|
| 284 | ); |
|---|
| 285 | else my_error(sprintf( |
|---|
| 286 | l10n('ec_assoc_pb'), |
|---|
| 287 | $_POST['login'], $ec_lists['categories'][$_POST['ec_in_up_cat']] |
|---|
| 288 | ), false); |
|---|
| 289 | } |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | // If an add. p. id has been posted, the newly created user should be |
|---|
| 293 | // allowed to navigate in this additional page => give a warning message |
|---|
| 294 | if ( |
|---|
| 295 | isset($_POST['ec_in_up_aps']) and |
|---|
| 296 | array_key_exists($_POST['ec_in_up_aps'], $ec_lists['add_pages']) |
|---|
| 297 | ) $page['errors'][] = sprintf( |
|---|
| 298 | l10n('ec_user_access_AP'), |
|---|
| 299 | $ec_lists['add_pages'][$_POST['ec_in_up_aps']], |
|---|
| 300 | $_POST['login'] |
|---|
| 301 | ); |
|---|
| 302 | } |
|---|
| 303 | return $ec_user_id; |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | // +-----------------------------------------------------------------------+ |
|---|
| 307 | // | Tables building functions | |
|---|
| 308 | // +-----------------------------------------------------------------------+ |
|---|
| 309 | |
|---|
| 310 | /* |
|---|
| 311 | * build_ec_duplicable_codes() |
|---|
| 312 | * |
|---|
| 313 | * |
|---|
| 314 | * @param |
|---|
| 315 | * no parameter passed, the main material on which works the function, is |
|---|
| 316 | * the global array variable $ec_lists. |
|---|
| 317 | * @return |
|---|
| 318 | * (no return value) |
|---|
| 319 | */ |
|---|
| 320 | function build_ec_duplicable_codes() { |
|---|
| 321 | global $ec_lists, $template; |
|---|
| 322 | $ec_lists['duplicable_codes'] = array(); |
|---|
| 323 | $t = array(); |
|---|
| 324 | foreach ($ec_lists['ec_table'] as $ec_entry) { |
|---|
| 325 | if ( |
|---|
| 326 | is_in($ec_entry['action'], 'ec_ok') and |
|---|
| 327 | $ec_entry['forced'] == 'false' |
|---|
| 328 | ) { |
|---|
| 329 | $t[$ec_entry['id']] = $ec_entry['code']; |
|---|
| 330 | $ec_lists['duplicable_codes']['codes'][$ec_entry['code']]['id'] = |
|---|
| 331 | $ec_entry['id']; |
|---|
| 332 | $ec_lists['duplicable_codes']['codes'][$ec_entry['code']]['comment'] = |
|---|
| 333 | $ec_entry['comment']; |
|---|
| 334 | $ec_lists['duplicable_codes']['codes'][$ec_entry['code']]['user_id'] = |
|---|
| 335 | $ec_entry['user_id']; |
|---|
| 336 | } |
|---|
| 337 | } |
|---|
| 338 | foreach ($t as $ec_id => $ec_code) { |
|---|
| 339 | $ec_lists['duplicable_codes']['ids'][$ec_id] = |
|---|
| 340 | $ec_lists['duplicable_codes']['codes'][$ec_code]['id']; |
|---|
| 341 | $ec_lists['duplicable_codes']['comment'][$ec_id] = |
|---|
| 342 | $ec_lists['duplicable_codes']['codes'][$ec_code]['comment']; |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | // Builds a category list displayed a best way |
|---|
| 346 | build_ec_categories(false); |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | // +-----------------------------------------------------------------------+ |
|---|
| 350 | // | Database modifying functions | |
|---|
| 351 | // +-----------------------------------------------------------------------+ |
|---|
| 352 | |
|---|
| 353 | /* |
|---|
| 354 | * ec_create_modify_entry_OK() |
|---|
| 355 | * returns true or false whether the creation of a new entry described by |
|---|
| 356 | * $_POST was OK or not. |
|---|
| 357 | * |
|---|
| 358 | * @param |
|---|
| 359 | * no param |
|---|
| 360 | * @return |
|---|
| 361 | * true if creation was OK ; false if not |
|---|
| 362 | */ |
|---|
| 363 | function ec_create_modify_entry_OK() { |
|---|
| 364 | global $page, $ec_lists; |
|---|
| 365 | |
|---|
| 366 | // $_POST validity checks : action prevented in case of bad arguments |
|---|
| 367 | |
|---|
| 368 | if (!isset($_POST['ec_act1'])) |
|---|
| 369 | return ec_end1('ec_act1', 'Bad argument : '); |
|---|
| 370 | |
|---|
| 371 | if ( |
|---|
| 372 | ($_POST['ec_act1']) != 'toggle_forced' and |
|---|
| 373 | !isset($_POST['ec_input_action']) |
|---|
| 374 | ) return ec_end1('ec_input_action', 'Bad argument : '); |
|---|
| 375 | |
|---|
| 376 | $is_creation = true; |
|---|
| 377 | $ec_user_id = 'NULL'; |
|---|
| 378 | $action = 'ec_ok'; |
|---|
| 379 | $del_other = false; |
|---|
| 380 | $comment = ''; |
|---|
| 381 | switch ($_POST['ec_act1']) { |
|---|
| 382 | |
|---|
| 383 | // This "switch" statement is a little bit tricky... it has been a pain to |
|---|
| 384 | // debug, and I wish to nobody to have to modify it :-\ ! |
|---|
| 385 | // Its principle is that it manages checks for four occurrences of |
|---|
| 386 | // $_POST['ec_act1'] : 'create', 'modify_entry_submit', |
|---|
| 387 | // 'duplicate_entry_submit', and 'toggle_forced'. Some checks are mutual |
|---|
| 388 | // between different occurences, but never all checks of each occurrence |
|---|
| 389 | // of $_POST['ec_act1']. So tests are done with "if" statements to |
|---|
| 390 | // produce "break" statements when needed. |
|---|
| 391 | |
|---|
| 392 | case 'create': |
|---|
| 393 | |
|---|
| 394 | // Stops if given code or user type are incorrect |
|---|
| 395 | if ( |
|---|
| 396 | !isset($_POST['ec_in_up_code']) or |
|---|
| 397 | !preg_match('/^[a-zA-Z0-9_-]{4,32}$/', $_POST['ec_in_up_code']) |
|---|
| 398 | ) return ec_end1('ec_in_up_code', 'Improper code : '); |
|---|
| 399 | else $ec_code = $_POST['ec_in_up_code']; |
|---|
| 400 | |
|---|
| 401 | foreach ($ec_lists['ec_table'] as $ec_entry) |
|---|
| 402 | if ($ec_code == $ec_entry['code']) |
|---|
| 403 | return ec_end1('ec_in_up_code', 'Code already exists : '); |
|---|
| 404 | |
|---|
| 405 | if ( |
|---|
| 406 | !isset($_POST['ec_sel_user']) or ( |
|---|
| 407 | $_POST['ec_sel_user'] != 'new' and |
|---|
| 408 | $_POST['ec_sel_user'] != 'old' |
|---|
| 409 | ) |
|---|
| 410 | ) return ec_end1('ec_sel_user', 'Bad argument : '); |
|---|
| 411 | |
|---|
| 412 | case 'modify_entry_submit': |
|---|
| 413 | |
|---|
| 414 | // First checks for user type and/or value |
|---|
| 415 | if (isset($_POST['ec_sel_user'])) { |
|---|
| 416 | if ($_POST['ec_sel_user'] == 'new') { |
|---|
| 417 | if ( |
|---|
| 418 | !isset($_POST['login']) or |
|---|
| 419 | $_POST['login'] == '' |
|---|
| 420 | ) return ec_end1('login', 'Bad argument : '); |
|---|
| 421 | if (in_array($_POST['login'], $ec_lists['user_ids'])) |
|---|
| 422 | return ec_end1('login', 'User already exists : '); |
|---|
| 423 | } |
|---|
| 424 | elseif ($_POST['ec_sel_user'] == 'old') { |
|---|
| 425 | if (!isset($_POST['ec_in_up_usr_list'])) |
|---|
| 426 | return ec_end1('login', 'Bad argument : '); |
|---|
| 427 | $ec_user_id = $_POST['ec_in_up_usr_list']; |
|---|
| 428 | if (!array_key_exists($ec_user_id, $ec_lists['user_ids'])) |
|---|
| 429 | return ec_end1('ec_in_up_usr_list', 'User doesn\'t exist : '); |
|---|
| 430 | } |
|---|
| 431 | else $action = 'ec_nok'; |
|---|
| 432 | } |
|---|
| 433 | else $action = 'ec_nok'; |
|---|
| 434 | |
|---|
| 435 | if ($_POST['ec_act1'] == 'create') break; |
|---|
| 436 | |
|---|
| 437 | case 'duplicate_entry_submit': |
|---|
| 438 | |
|---|
| 439 | // Checks of entry value validity |
|---|
| 440 | if ( |
|---|
| 441 | !isset($_POST['ec_entry_sel']) or |
|---|
| 442 | !array_key_exists($_POST['ec_entry_sel'], $ec_lists['ec_table']) |
|---|
| 443 | ) return ec_end1( |
|---|
| 444 | 'ec_entry_sel', 'Code doesn\'t exist or non-duplicable code : ' |
|---|
| 445 | ); |
|---|
| 446 | |
|---|
| 447 | // Other checks for user type and value |
|---|
| 448 | if ($_POST['ec_act1'] == 'modify_entry_submit') if ( |
|---|
| 449 | !isset($_POST['ec_sel_user']) or ( |
|---|
| 450 | $_POST['ec_sel_user'] == 'new' or |
|---|
| 451 | $_POST['ec_sel_user'] == 'none' or ( |
|---|
| 452 | $_POST['ec_sel_user'] == 'old' and |
|---|
| 453 | $_POST['ec_in_up_usr_list'] != |
|---|
| 454 | $ec_lists['ec_table'][$_POST['ec_entry_sel']]['user_id'] |
|---|
| 455 | ) or |
|---|
| 456 | isset($_POST['ec_in_up_forced']) |
|---|
| 457 | ) |
|---|
| 458 | ) $del_other = true; |
|---|
| 459 | |
|---|
| 460 | case 'toggle_forced': |
|---|
| 461 | |
|---|
| 462 | // Establish default values for unchanged values |
|---|
| 463 | $ec_code = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['code']; |
|---|
| 464 | if ($action == 'ec_ok' and $ec_user_id == 'NULL') |
|---|
| 465 | $ec_user_id = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['user_id']; |
|---|
| 466 | $arg1 = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['arg1']; |
|---|
| 467 | $arg2 = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['arg2']; |
|---|
| 468 | $comment = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['comment']; |
|---|
| 469 | if (empty($arg1)) $arg1 = 'NULL'; |
|---|
| 470 | if (empty($arg2)) $arg2 = 'NULL'; |
|---|
| 471 | if (empty($ec_user_id)) $ec_user_id = 'NULL'; |
|---|
| 472 | if ($_POST['ec_act1'] == 'toggle_forced') { |
|---|
| 473 | $forced = ( |
|---|
| 474 | $ec_lists['ec_table'][$_POST['ec_entry_sel']]['forced'] == 'true' |
|---|
| 475 | ) ? 'false' : 'true'; |
|---|
| 476 | $del_other = ($forced == 'true'); |
|---|
| 477 | $action = $ec_lists['ec_table'][$_POST['ec_entry_sel']]['action']; |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | if ( |
|---|
| 481 | $_POST['ec_act1'] == 'toggle_forced' or |
|---|
| 482 | $_POST['ec_act1'] == 'modify_entry_submit' |
|---|
| 483 | ) { |
|---|
| 484 | $is_creation = false; |
|---|
| 485 | break; |
|---|
| 486 | } |
|---|
| 487 | |
|---|
| 488 | // Final check for entry value |
|---|
| 489 | build_ec_duplicable_codes(); |
|---|
| 490 | if (!array_key_exists($_POST['ec_entry_sel'], |
|---|
| 491 | $ec_lists['duplicable_codes']['ids']) |
|---|
| 492 | ) return ec_end1( |
|---|
| 493 | 'ec_entry_sel', 'Code doesn\'t exist or non-duplicable code : ' |
|---|
| 494 | ); |
|---|
| 495 | |
|---|
| 496 | break; |
|---|
| 497 | default: ec_end1('ec_act1', 'Bad argument : '); |
|---|
| 498 | } |
|---|
| 499 | // Pfew ! |
|---|
| 500 | |
|---|
| 501 | if ($_POST['ec_act1'] != 'toggle_forced') { |
|---|
| 502 | // Preparation of $arg1, $arg2 |
|---|
| 503 | switch ($_POST['ec_input_action']) { |
|---|
| 504 | case 'add_p': // Additional Page |
|---|
| 505 | if (isset($_POST['ec_in_up_aps'])) $arg2 = $_POST['ec_in_up_aps']; |
|---|
| 506 | else ec_end1('ec_in_up_aps', 'Bad argument : '); |
|---|
| 507 | $arg1 = 'NULL'; |
|---|
| 508 | break; |
|---|
| 509 | case 'cat': // Category |
|---|
| 510 | case 'img': // Image |
|---|
| 511 | if (isset($_POST['ec_in_up_cat'])) { |
|---|
| 512 | $arg1 = $_POST['ec_in_up_cat']; |
|---|
| 513 | if ($_POST['ec_input_action'] == 'img') { |
|---|
| 514 | if (isset($_POST['ec_in_up_img'])) $arg2 = $_POST['ec_in_up_img']; |
|---|
| 515 | else ec_end1('ec_in_up_img', 'Bad argument : '); |
|---|
| 516 | } |
|---|
| 517 | else $arg2 = 'NULL'; |
|---|
| 518 | } |
|---|
| 519 | else ec_end1('ec_in_up_cat', 'Bad argument : '); |
|---|
| 520 | break; |
|---|
| 521 | case 'home': // Home : nothing to do : "arg"s are '' |
|---|
| 522 | case 'refused': // $_POST['ec_sel_user'] unset, nothing to do |
|---|
| 523 | $arg1 = 'NULL'; $arg2 = 'NULL'; |
|---|
| 524 | break; |
|---|
| 525 | default: ec_end1('ec_input_action', 'Bad argument : '); |
|---|
| 526 | } |
|---|
| 527 | |
|---|
| 528 | // Preparation of $forced |
|---|
| 529 | $forced = (isset($_POST['ec_in_up_forced'])) ? 'true' : 'false'; |
|---|
| 530 | if ($_POST['ec_act1'] == 'duplicate_entry_submit' and $forced == 'true') |
|---|
| 531 | return ec_end1('ec_in_up_forced', 'Bad argument : '); |
|---|
| 532 | |
|---|
| 533 | // Preparation of $comment |
|---|
| 534 | $comment = (isset($_POST['ec_in_up_comment'])) ? |
|---|
| 535 | $_POST['ec_in_up_comment'] : $comment; |
|---|
| 536 | |
|---|
| 537 | // User and eventually group creation, if needed |
|---|
| 538 | if ($_POST['ec_act1'] != 'duplicate_entry_submit') |
|---|
| 539 | if (isset($_POST['ec_sel_user']) and $_POST['ec_sel_user'] == 'new') |
|---|
| 540 | if (!($ec_user_id = ec_create_user_OK())) return false; |
|---|
| 541 | } |
|---|
| 542 | |
|---|
| 543 | // Now we have all infos : check that future entry doesn't exist already |
|---|
| 544 | $arg1p = ($arg1 == 'NULL') ? 'IS NULL' : ' = '.$arg1; |
|---|
| 545 | $arg2p = ($arg2 == 'NULL') ? 'IS NULL' : ' = '.$arg2; |
|---|
| 546 | $ec_user_idp = ($ec_user_id == 'NULL') ? 'IS NULL' : ' = '.$ec_user_id; |
|---|
| 547 | if (($t1 = pwg_db_fetch_row(pwg_query(" |
|---|
| 548 | SELECT `id` |
|---|
| 549 | FROM `".EVNTCATS_TABLE."` |
|---|
| 550 | WHERE `code` = '".$ec_code."' |
|---|
| 551 | AND `user_id` ".$ec_user_idp." |
|---|
| 552 | AND `action` = '".$action."' |
|---|
| 553 | AND `arg1` ".$arg1p." |
|---|
| 554 | AND `arg2` ".$arg2p." |
|---|
| 555 | AND `forced` = '".$forced."' |
|---|
| 556 | AND `comment` = '".$comment."' |
|---|
| 557 | "))) !== false) { // print("<pre>$arg1 $arg2<br>$q</pre>"); |
|---|
| 558 | $page['errors'][] = sprintf(l10n('ec_entry_already_exists'), $t1[0]); |
|---|
| 559 | return false; |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | // Delete other entries using the same code, if needed |
|---|
| 563 | if ($del_other) { |
|---|
| 564 | if (( |
|---|
| 565 | $t1 = pwg_db_fetch_row(pwg_query(" |
|---|
| 566 | SELECT `code` |
|---|
| 567 | FROM `".EVNTCATS_TABLE."` |
|---|
| 568 | WHERE `id` = ".$_POST['ec_entry_sel'] |
|---|
| 569 | ))) === false |
|---|
| 570 | ) die('Entry not found in DB ?!'); |
|---|
| 571 | $r = pwg_query(" |
|---|
| 572 | SELECT `id` |
|---|
| 573 | FROM `".EVNTCATS_TABLE."` |
|---|
| 574 | WHERE `code` = '".$t1[0]."' |
|---|
| 575 | AND `action` IS NOT NULL |
|---|
| 576 | AND `id` <> ".$_POST['ec_entry_sel'] |
|---|
| 577 | ); |
|---|
| 578 | while ($t2 = pwg_db_fetch_row($r)) if (!ec_delete_entry_OK($t2[0])) |
|---|
| 579 | return false; |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | // Action ! |
|---|
| 583 | $ret = true; |
|---|
| 584 | if ($is_creation) { |
|---|
| 585 | if ( |
|---|
| 586 | pwg_query(" |
|---|
| 587 | INSERT INTO `".EVNTCATS_TABLE."` ( |
|---|
| 588 | `code`, |
|---|
| 589 | `user_id`, |
|---|
| 590 | `action`, |
|---|
| 591 | `arg1`, `arg2`, `forced`, `comment` |
|---|
| 592 | ) |
|---|
| 593 | VALUES ( |
|---|
| 594 | '".$ec_code."', |
|---|
| 595 | ".$ec_user_id.", |
|---|
| 596 | '".$action."', |
|---|
| 597 | ".$arg1.", ".$arg2.", '".$forced."', '".$comment."' |
|---|
| 598 | ); |
|---|
| 599 | ") === false |
|---|
| 600 | ) { |
|---|
| 601 | my_error(l10n('ec_entry_create_pb'), false); |
|---|
| 602 | $ret = false; |
|---|
| 603 | } |
|---|
| 604 | else { |
|---|
| 605 | $t5 = pwg_db_insert_id(EVNTCATS_TABLE); |
|---|
| 606 | build_ec_lists(); // Don't remember exactly why, but must be done here |
|---|
| 607 | $forced = ($forced == 'false') ? '' : l10n('ec_cnfrm_forced'); |
|---|
| 608 | $page['infos'][] = |
|---|
| 609 | sprintf(l10n('ec_entry_create_OK'), $t5). |
|---|
| 610 | $ec_code.' => '. |
|---|
| 611 | $ec_lists['user_ids'][$ec_user_id].$forced |
|---|
| 612 | ; |
|---|
| 613 | return true; |
|---|
| 614 | } |
|---|
| 615 | } |
|---|
| 616 | else { |
|---|
| 617 | if ( |
|---|
| 618 | pwg_query(" |
|---|
| 619 | UPDATE `".EVNTCATS_TABLE."` |
|---|
| 620 | SET |
|---|
| 621 | `user_id` = ".$ec_user_id.", |
|---|
| 622 | `action` = '".$action."', |
|---|
| 623 | `arg1` = ".$arg1.", |
|---|
| 624 | `arg2` = ".$arg2.", |
|---|
| 625 | `forced` = '".$forced."', |
|---|
| 626 | `comment` = '".$comment."' |
|---|
| 627 | WHERE `id` = '".$_POST['ec_entry_sel']."' |
|---|
| 628 | ") === false |
|---|
| 629 | ) { |
|---|
| 630 | my_error(l10n('ec_entry_create_pb'), false); |
|---|
| 631 | $ret = false; |
|---|
| 632 | } |
|---|
| 633 | else $page['infos'][] = sprintf( |
|---|
| 634 | l10n('ec_entry_modify_OK'), $_POST['ec_entry_sel'] |
|---|
| 635 | ); |
|---|
| 636 | } |
|---|
| 637 | build_ec_lists(); |
|---|
| 638 | return $ret; |
|---|
| 639 | } |
|---|
| 640 | |
|---|
| 641 | /* |
|---|
| 642 | * ec_delete_entry_OK($ec_id) |
|---|
| 643 | * tries to delete an existing entry. |
|---|
| 644 | * |
|---|
| 645 | * @param |
|---|
| 646 | * $ec_id : the entry to be deleted |
|---|
| 647 | * @return |
|---|
| 648 | * true or false whether deleting succeeded. |
|---|
| 649 | */ |
|---|
| 650 | function ec_delete_entry_OK($ec_id) { |
|---|
| 651 | global $page; |
|---|
| 652 | if (count($t = pwg_db_fetch_row(pwg_query(" |
|---|
| 653 | SELECT `code` |
|---|
| 654 | FROM `".EVNTCATS_TABLE."` |
|---|
| 655 | WHERE `id` = $ec_id; |
|---|
| 656 | "))) == 0) { |
|---|
| 657 | $page['errors'][] = sprintf(l10n('ec_entry_dont_exist'), $ec_id); |
|---|
| 658 | return false; |
|---|
| 659 | } |
|---|
| 660 | if (pwg_query(" |
|---|
| 661 | DELETE FROM `".EVNTCATS_TABLE."` |
|---|
| 662 | WHERE `id` = ".$ec_id |
|---|
| 663 | ) === false) { |
|---|
| 664 | my_error(sprintf(l10n('ec_entry_del_nok'), $ec_id), false); |
|---|
| 665 | return false; |
|---|
| 666 | } |
|---|
| 667 | $page['infos'][] = sprintf(l10n('ec_entry_del_ok'), $ec_id, $t[0]); |
|---|
| 668 | return true; |
|---|
| 669 | } |
|---|
| 670 | |
|---|
| 671 | ?> |
|---|