" ; * _ all "TAB" characters (chr(10)) are replaced by "
" ; * _ all spaces are replaced by " ". * * @param * $var : variable to display * @return * string easy to display in Piwigo */ function str_from_var($var) { return '

'. str_replace( chr(10),'
', str_replace(' ',' ', print_r /* var_dump */ ($var,true)) ). '

'; } /* * ec_inspect() * goes through ec_lists['ec_table'] to check errors on (multiple) entries * using the same code. Cannot really be used elsewhere than here in * build_ec_table() . * * @param * * @return * (no return value) */ function ec_inspect($checked_item, $new_action, $check_ec_nok = true) { global $ec_lists; $first = array(); $to_correct = array(); /* // $to_correct is needed cause following code would not work everywhere : foreach ($table as $value) { if ($value == $value_to_check and $value == $problem) { foreach ($table as &$value2) { // key index of $table can be reset if ($value2 == $value_to_check) { $value2 = $better_value; } } } } // It works with WinAmp Server, but not on Apache server of Free (french // Internet provider). */ foreach ($ec_lists['ec_table'] as $ec_entry) { $ec_current_code = $ec_entry['code']; if ( $ec_entry['action'] == 'ec_ok' or ($check_ec_nok and $ec_entry['action'] == 'ec_nok') ) { if (isset($first[$ec_current_code])) { // $first[$ec_current_code] is set <=> code has already been met. // Checked item MUST be equal to the first item (thus all items) for // this code. if ( $first[$ec_current_code] != $ec_entry[$checked_item] or ($new_action == '' and $ec_entry[$checked_item] == 'true') ) $to_correct[$ec_current_code] = true; // value not used in fact // but using $ec_current_code as a key makes a smaller table } // if the error comes back several times else $first[$ec_current_code] = $ec_entry[$checked_item]; } } foreach ($ec_lists['ec_table'] as &$ec_entry) { // & is needed here if (isset($to_correct[$ec_entry['code']])) { if ($new_action == '') { if (!pwg_query(" UPDATE `".EVNTCATS_TABLE."` SET `forced` = 'false' WHERE `id` = ".$ec_entry['id'] )) die('Could not fix a "_f_pb"'); $ec_entry['forced'] = 'false'; } else $ec_entry['action'] = $new_action; } } } // +-----------------------------------------------------------------------+ // | Tables building functions | // +-----------------------------------------------------------------------+ /* * build_ec_addp() * builds $ec_lists['add_pages']. * * @param * (no parameter) * @return * (no return value) */ function build_ec_addp() { global $ec_lists; if (defined('ADD_PAGES_TABLE')) { $res = pwg_query("SELECT `id`, `title` FROM `".ADD_PAGES_TABLE."`"); while ($r = mysql_fetch_assoc($res)) { $c = (is_in($r['title'], '/user_id=')) ? '/user_id=' : '/group_id='; $a = explode($c ,$r['title']); $ec_lists['add_pages'][$r['id']] = $a[0]; } } } /* * build_ec_categories($dsp) * builds $ec_lists['categories']. * * @param * whether $ec_lists['categories'] must be of type "cat / sub-cat" or * or "cat - sub-cat". * @return * (no return value) */ function build_ec_categories($dsp) { global $template, $ec_lists; $c = array(); display_select_cat_wrapper(" SELECT `id`, `name`, `uppercats`, `global_rank` FROM `".CATEGORIES_TABLE."` ", $c, 'category_options', $dsp); $ec_lists['categories'] = $template->smarty->_tpl_vars['category_options']; } /* * build_ec_userids() * builds $ec_lists['user_ids']. * * @param * (no parameter) * @return * (no return value) */ function build_ec_userids() { global $ec_lists, $conf; $ec_lists['user_ids'] = simple_hash_from_query(" SELECT ".$conf['user_fields']['id']." AS id, ".$conf['user_fields']['username']." AS username FROM `".USERS_TABLE."` WHERE id > 2;", 'id', 'username' ); } /* * build_ec_table() * builds a table showing the content of the _event_cats database table, * and a table showing the eventual errors. * * @param * no parameters passed ; the main material on which works the function, is * the global array variable $ec_lists. * @return * (no return value) */ function build_ec_table() { global $ec_lists; $q = pwg_query(" SELECT * FROM `".EVNTCATS_TABLE."` WHERE `code` IS NOT NULL ORDER BY `id` "); while ($r = mysql_fetch_assoc($q)) $ec_lists['ec_table'][intval($r['id'])] = $r; // Construction of behaviour // Multiple action params for a single code check ec_inspect('action', 'ec_nok_action_pb'); // Multiple user_ids for a single code check ec_inspect('user_id', 'ec_nok_userid_pb', false); // Multiple "forced" params for a single code check ec_inspect('forced', ''); // User id and associated page validities checks foreach ($ec_lists['ec_table'] as &$ec_entry) { // Check if associated user_id exists if ( is_in($ec_entry['action'], 'ec_ok') and !array_key_exists($ec_entry['user_id'], $ec_lists['user_ids']) ) $ec_entry['action'] = 'ec_nok_userid_miss'; // Check if associated displayed page exists $a = 0; if ( !empty($ec_entry['arg1']) and // (Only arg2 is significant if action is ec_nok[_xxx] .) is_in($ec_entry['action'], 'ec_ok') ) $a++; if (!empty($ec_entry['arg2'])) $a+= 2; switch ($a) { // case 0 : home, nothing to check case 2: // Additional Page if ( defined('ADD_PAGES_TABLE') and ( is_in($ec_entry['action'], 'ec_ok') or $ec_entry['action'] == 'ec_nok' ) and !array_key_exists($ec_entry['arg2'], $ec_lists['add_pages']) ) $ec_entry['action'].= '_ap_pb'; break; case 1: // Category case 3: // Image if (is_in($ec_entry['action'], 'ec_ok')) { if (array_key_exists($ec_entry['arg1'], $ec_lists['categories'])) { if ($a == 3) // case 3: // Image if (!ec_image_exists($ec_entry['arg1'], $ec_entry['arg2'])) $ec_entry['action'].= '_img_pb'; } else $ec_entry['action'].= '_cat_pb'; } break; } } } /* * build_dup_groups() * builds the information telling who is allowed to duplicate * * @param * no parameter passed, the main material on which works the function, is * the global array variable $ec_lists. * @return * (no return value) */ function build_dup_groups() { global $ec_lists; if (count($ec_lists['user_ids']) == 0) build_ec_userids(); // Existing groups (needed later) $ec_lists['groups'] = simple_hash_from_query(" SELECT `id`, `name` FROM `".GROUPS_TABLE."` ORDER BY `name` ASC; ", 'id', 'name' ); // groups granted to duplication $ec_lists['groups_granted_ids'] = order_by_name( array_from_query(" SELECT `arg2` FROM ".EVNTCATS_TABLE." WHERE `code` IS NULL AND `arg1` = 1 AND `arg2` IS NOT NULL ;", 'arg2' ), $ec_lists['groups'] ); // users directly granted to duplication $ec_lists['users_granted_direct_ids'] = order_by_name( array_from_query(" SELECT `arg2` FROM `".EVNTCATS_TABLE."` WHERE `code` IS NULL AND `arg1` = 2 AND `arg2` IS NOT NULL ;", 'arg2' ), $ec_lists['user_ids'] ); // Calculate users granted to duplication thanks to belonging to a // certain group (in groups, level (friends, family, contacts), // or user status (generic)) $types_entry_exists = (($t = mysql_fetch_row(pwg_query(" SELECT `arg2` FROM `".EVNTCATS_TABLE."` WHERE `code` IS NULL AND `arg1` = 3 AND `arg2` IS NOT NULL LIMIT 1; "))) !== false); $ec_lists['types_granted'] = ($types_entry_exists) ? $t[0] : 0; return $types_entry_exists; } /* * build_ec_lists() * builds the main array variable contaning all informations for the plugin * * @param * no parameter passed, the main material on which works the function, is * the global array variable $ec_lists. * @return * (no return value) */ function build_ec_lists() { // Construction of $ec_lists['add_pages'] array var build_ec_addp(); // Construction of $ec_lists['categories'] array var build_ec_categories(true); // Construction of $ec_lists['user_ids'] array var build_ec_userids(); // Construction of $ec_lists['ec_table'] array var build_ec_table(); } // +-----------------------------------------------------------------------+ // | Duplication analysis functions | // +-----------------------------------------------------------------------+ /* * build_dup_arrays($append_tpl = false) * builds arrays telling which accounts are allowed to display a duplicate * account link. Returns an array of all the user ids allowed to duplicate. * * @param * $append_tpl : tells if $template must be appended with built arrays * @return * array of all the user ids allowed to duplicate */ function build_dup_arrays($append_tpl = false) { global $template, $ec_lists, $conf; // A lot of below code has simply been copied from file cat_perm.php . // Many thanks to people who wrote it ! build_dup_groups(); $users_granted_ids = array(); $users_granted_thks_gen_ids = array(); if ($ec_gen_granted = (($ec_lists['types_granted'] & 8) != 0)) { $users_granted_thks_gen_ids = order_by_name( array_diff( array_from_query(" SELECT `user_id` FROM `".USER_INFOS_TABLE."` WHERE `status` = 'generic';", 'user_id' ), $ec_lists['users_granted_direct_ids'] ), $ec_lists['user_ids'] ); foreach ($users_granted_thks_gen_ids as $user_id) $users_granted_ids[$user_id] = l10n('user_status_generic'); } $types = array( '1' => l10n('Level 1'), '2' => l10n('Level 2'), '4' => l10n('Level 4'), ); $types_granted_ids = array(); $users_granted_thks_types_ids = array(); if (($ec_lists['types_granted'] & 7) != 0) { if (($ec_lists['types_granted'] & 1) != 0) $types_granted_ids[] = '1'; if (($ec_lists['types_granted'] & 2) != 0) $types_granted_ids[] = '2'; if (($ec_lists['types_granted'] & 4) != 0) $types_granted_ids[] = '4'; if (count($types_granted_ids) > 0) { $user_granted_from_type = array(); $result = pwg_query(" SELECT `user_id`, `level` FROM `".USER_INFOS_TABLE."` WHERE `level` IN (".implode(',', $types_granted_ids)."); "); while ($row = mysql_fetch_array($result)) { if (!isset($user_granted_from_type[$row['level']])) { $user_granted_from_type[$row['level']] = array(); } $user_granted_from_type[$row['level']][] = $row['user_id']; } $user_granted_by_type_ids = array(); foreach ($user_granted_from_type as $type_users) $user_granted_by_type_ids = array_merge( $user_granted_by_type_ids, $type_users ); $users_granted_thks_types_ids = order_by_name( array_diff( array_unique($user_granted_by_type_ids), $ec_lists['users_granted_direct_ids'] ), $ec_lists['user_ids'] ); foreach ($users_granted_thks_types_ids as $user_id) foreach ($user_granted_from_type as $type_id => $type_users) { if (in_array($user_id, $type_users)) { $users_granted_ids[$user_id]= $types[$type_id]; break; } } } } $users_granted_thks_groups_ids = array(); if (count($ec_lists['groups_granted_ids']) > 0) { $granted_groups = array(); $result = pwg_query(" SELECT `user_id`, `group_id` FROM `".USER_GROUP_TABLE."` WHERE `group_id` IN (".implode(',', $ec_lists['groups_granted_ids'])."); "); while ($row = mysql_fetch_array($result)) { if (!isset($granted_groups[$row['group_id']])) { $granted_groups[$row['group_id']] = array(); } $granted_groups[$row['group_id']][] = $row['user_id']; } $user_granted_by_group_ids = array(); foreach ($granted_groups as $group_users) $user_granted_by_group_ids = array_merge( $user_granted_by_group_ids, $group_users ); $users_granted_thks_groups_ids = order_by_name( array_diff( array_unique($user_granted_by_group_ids), $ec_lists['users_granted_direct_ids'] ), $ec_lists['user_ids'] ); foreach ($users_granted_thks_groups_ids as $user_id) foreach ($granted_groups as $group_id => $group_users) if (in_array($user_id, $group_users)) { $users_granted_ids[$user_id]= $ec_lists['groups'][$group_id]; break; } } if ($append_tpl) { $users_denied_ids = order_by_name( array_diff( array_keys($ec_lists['user_ids']), $users_granted_thks_gen_ids, $users_granted_thks_types_ids, $users_granted_thks_groups_ids, $ec_lists['users_granted_direct_ids'] ), $ec_lists['user_ids'] ); foreach ($users_granted_ids as $u => $g) $template->append( 'user_granted_indirects', array( 'USER' => $ec_lists['user_ids'][$u], 'GROUP' => $g ) ); $template->assign('all_groups', $ec_lists['groups']); $template->assign('groups_granted_ids', $ec_lists['groups_granted_ids']); $template->assign('groups_denied_ids', order_by_name( array_diff( array_keys($ec_lists['groups']), $ec_lists['groups_granted_ids'] ), $ec_lists['groups'] )); $template->assign('ec_gen_granted', $ec_gen_granted); $template->assign('all_types', $types); $template->assign('types_granted_ids', $types_granted_ids); $template->assign('types_denied_ids', order_by_name( array_diff(array_keys($types), $types_granted_ids), $types )); $template->assign('all_users', $ec_lists['user_ids']); $template->assign( 'users_granted_direct_ids', $ec_lists['users_granted_direct_ids'] ); $template->assign('users_denied_ids', $users_denied_ids); } $users_granted_ids = array_merge( array_keys($users_granted_ids), $ec_lists['users_granted_direct_ids'] ); // Returns an array which values are all the user_ids allowed to display a // "duplicate" link. The keys of this array are strange (for direct allowed // users, keys are usernames), but should not used return $users_granted_ids; } /* * dup_allowed($user_id) * returns true if the user_id is allowed to display a duplicate link * * @param * $user_id : the user_id * @return * true if the user_id is allowed to display a duplicate link */ function dup_allowed($user_id) { return in_array($user_id, build_dup_arrays()); } ?>