diff -r de56132c008d -r bdac73ed481e plugins/admin/PageGroups.php --- a/plugins/admin/PageGroups.php Sun Mar 28 21:49:26 2010 -0400 +++ b/plugins/admin/PageGroups.php Sun Mar 28 23:10:46 2010 -0400 @@ -13,1040 +13,1040 @@ function page_Admin_PageGroups() { - global $db, $session, $paths, $template, $plugins; // Common objects - global $lang; - if ( $session->auth_level < USER_LEVEL_ADMIN || $session->user_level < USER_LEVEL_ADMIN ) - { - $login_link = makeUrlNS('Special', 'Login/' . $paths->nslist['Special'] . 'Administration', 'level=' . USER_LEVEL_ADMIN, true); - echo '

' . $lang->get('adm_err_not_auth_title') . '

'; - echo '

' . $lang->get('adm_err_not_auth_body', array( 'login_link' => $login_link )) . '

'; - return; - } - - if ( isset($_POST['action']) ) - { - if ( isset($_POST['action']['create']) || isset($_POST['action']['create_stage2']) ) - { - switch ( isset($_POST['action']['create_stage2']) ) - { - case true: - if ( empty($_POST['pg_name']) || empty($_POST['group_type']) ) - { - echo '
' . $lang->get('acppg_err_need_name') . '
'; - return; - } - if ( $_POST['group_type'] == PAGE_GRP_TAGGED && empty($_POST['member_tag']) ) - { - echo '
' . $lang->get('acppg_err_need_tag') . '
'; - return; - } - if ( $_POST['group_type'] == PAGE_GRP_CATLINK && empty($_POST['member_cat']) ) - { - echo '
' . $lang->get('acppg_err_need_cat') . '
'; - return; - } - if ( $_POST['group_type'] == PAGE_GRP_NORMAL && empty($_POST['member_page_0']) ) - { - echo '
' . $lang->get('acppg_err_need_page') . '
'; - return; - } - if ( $_POST['group_type'] == PAGE_GRP_REGEX && empty($_POST['regex']) ) - { - echo '
' . $lang->get('acppg_err_need_regex') . '
'; - return; - } - if ( $_POST['group_type'] != PAGE_GRP_TAGGED && $_POST['group_type'] != PAGE_GRP_CATLINK && $_POST['group_type'] != PAGE_GRP_NORMAL && $_POST['group_type'] != PAGE_GRP_REGEX ) - { - echo '
Umm, you sent an invalid group type. I\'d put a real error message here but this will only be shown if you try to hack the system.
'; - return; - } - // All checks passed, create the group - switch($_POST['group_type']) - { - case PAGE_GRP_TAGGED: - $name = $db->escape($_POST['pg_name']); - $tag = $db->escape($_POST['member_tag']); - $sql = 'INSERT INTO '.table_prefix.'page_groups(pg_type,pg_name,pg_target) VALUES(' . PAGE_GRP_TAGGED . ', \'' . $name . '\', \'' . $tag . '\');'; - $q = $db->sql_query($sql); - if ( !$q ) - $db->_die(); - break; - case PAGE_GRP_CATLINK: - $name = $db->escape($_POST['pg_name']); - $cat = $db->escape($_POST['member_cat']); - $sql = 'INSERT INTO '.table_prefix.'page_groups(pg_type,pg_name,pg_target) VALUES(' . PAGE_GRP_CATLINK . ', \'' . $name . '\', \'' . $cat . '\');'; - $q = $db->sql_query($sql); - if ( !$q ) - $db->_die(); - break; - case PAGE_GRP_NORMAL: - $name = $db->escape($_POST['pg_name']); - $sql = 'INSERT INTO '.table_prefix.'page_groups(pg_type,pg_name) VALUES(' . PAGE_GRP_NORMAL . ', \'' . $name . '\');'; - $q = $db->sql_query($sql); - if ( !$q ) - $db->_die(); - - $ins_id = $db->insert_id(); - - // Page list - $keys = array_keys($_POST); - $arr_pages = array(); - foreach ( $keys as $val ) - { - if ( preg_match('/^member_page_([0-9]+?)$/', $val) && !empty($_POST[$val]) && isPage($_POST[$val]) ) - { - $arr_pages[] = $_POST[$val]; - } - } - $arr_sql = array(); - foreach ( $arr_pages as $page ) - { - list($id, $ns) = RenderMan::strToPageID($page); - $id = sanitize_page_id($id); - $arr_sql[] = '(' . $ins_id . ',\'' . $db->escape($id) . '\', \'' . $ns . '\')'; - } - $sql = 'INSERT INTO '.table_prefix.'page_group_members(pg_id,page_id,namespace) VALUES' . implode(',', $arr_sql) . ';'; - $q = $db->sql_query($sql); - if ( !$q ) - $db->_die(); - break; - case PAGE_GRP_REGEX: - $name = $db->escape($_POST['pg_name']); - $regex = $db->escape($_POST['regex']); - $sql = 'INSERT INTO '.table_prefix.'page_groups(pg_type,pg_name,pg_target) VALUES(' . PAGE_GRP_REGEX . ', \'' . $name . '\', \'' . $regex . '\');'; - $q = $db->sql_query($sql); - if ( !$q ) - $db->_die(); - break; - } - echo '
' . $lang->get('acppg_msg_create_success', array('group_name' => htmlspecialchars($_POST['pg_name']))) . '
'; - break; - } - // A little Javascript magic - ?> - - sql_query('SELECT name,urlname FROM '.table_prefix.'pages WHERE namespace=\'Category\';'); - if ( !$q ) - $db->_die(); - - if ( $db->numrows() < 1 ) - { - $catlist = $lang->get('acppg_err_no_cats'); - } - else - { - $catlist = ''; - } - - echo ''; - - echo '
'; - - echo '
- - - - '; - - // Name - echo ' - - - '; - - // Group type - echo ' - - - '; - - // Titles - echo ' - - '; - - echo ' - '; - - echo ' - '; - - // Submit button - echo ' - - '; - - echo '
' . $lang->get('acppg_th_create') . '
- ' . $lang->get('acppg_field_group_name') . '
- ' . $lang->get('acppg_field_group_name_hint') . ' -
- -
- ' . $lang->get('acppg_field_group_type') . ' - - -
- - ' . $lang->get('acppg_gtype_static') . ' - - - ' . $lang->get('acppg_gtype_tagged') . ' - - - ' . $lang->get('acppg_gtype_catlink') . ' - - - ' . $lang->get('acppg_gtype_regex') . ' - -
-
- ' . $lang->get('acppg_field_member_pages') . '
- ' . $lang->get('acppg_field_member_pages_hint') . ' -
- -
- ' . $lang->get('acppg_field_target_tag') . ' -
-
- ' . $lang->get('acppg_field_target_regex') . '
- ' . $lang->get('acppg_field_target_regex_hint') . ' -
-
-
-
-
-
-
- -
-
- -
- -
- -
-
-
'; - - echo '
'; - - echo ''; - return; - } - else if ( isset($_POST['action']['del']) ) - { - // Confirmation to delete a group (this is really only a stub) - - $delete_id = array_keys($_POST['action']['del']); - $delete_id = intval($delete_id[0]); - - if ( !empty($delete_id) ) - { - echo '
' . "\n"; - echo '' . "\n"; - echo '
' . "\n"; - echo ' ' . "\n"; - echo ' ' . "\n"; - echo ' ' . "\n"; - echo ' ' . "\n"; - echo '
' . $lang->get('acppg_th_delete_confirm') . '
' . $lang->get('acppg_msg_delete_confirm') . '
' . "\n"; - echo ' ' . "\n"; - echo ' ' . "\n"; - echo '
' . "\n"; - echo '' . "\n"; - - return; - } - } - else if ( isset($_POST['action']['del_confirm']) ) - { - $delete_id = intval($_POST['delete_id']); - if ( empty($delete_id) ) - { - echo 'Hack attempt'; - return; - } - // Obtain group name - $q = $db->sql_query('SELECT pg_name FROM '.table_prefix.'page_groups WHERE pg_id=' . $delete_id . ';'); - if ( !$q ) - $db->_die(); - if ( $db->numrows() < 1 ) - { - echo 'Page group dun exist.'; - return; - } - $row = $db->fetchrow(); - $db->free_result(); - $pg_name = $row['pg_name']; - unset($row); - // Delete the group - $q = $db->sql_query('DELETE FROM '.table_prefix.'page_groups WHERE pg_id=' . $delete_id . ';'); - if ( !$q ) - $db->_die(); - $q = $db->sql_query('DELETE FROM '.table_prefix.'page_group_members WHERE pg_id=' . $delete_id . ';'); - if ( !$q ) - $db->_die(); - - $del_msg = $lang->get('acppg_msg_delete_success', array('pg_name' => htmlspecialchars($pg_name))); - echo "
$del_msg
"; - } - else if ( isset($_POST['action']['edit']) && !isset($_POST['action']['noop']) ) - { - if ( isset($_POST['action']['edit_save']) ) - { - } - - if ( isset($_POST['action']['edit']['add_page']) && isset($_GET['src']) && $_GET['src'] == 'ajax' ) - { - $return = array('successful' => false); - - // - // Add the specified page to the group - // - - // Get ID of the group - $edit_id = intval($_POST['pg_id']); - if ( !$edit_id ) - { - $return = array('mode' => 'error', 'text' => 'Hack attempt'); - echo enano_json_encode($return); - return; - } - - // Run some validation - check that page exists and that it's not already in the group - $page = $_POST['new_page']; - if ( empty($page) ) - { - $return = array('mode' => 'error', 'text' => $lang->get('acppg_err_ajaxadd_need_title')); - echo enano_json_encode($return); - return; - } - - /* - // We're gonna allow adding nonexistent pages for now - if ( !isPage($page) ) - { - $return = array('mode' => 'error', 'text' => 'The page you are trying to add (' . htmlspecialchars($page) . ') does not exist.'); - echo enano_json_encode($return); - return; - } - */ - - list($page_id, $namespace) = RenderMan::strToPageID($page); - $page_id = sanitize_page_id($page_id); - - if ( !isset($paths->namespace[$namespace]) ) - { - $return = array('mode' => 'error', 'text' => 'Invalid namespace return from RenderMan::strToPageID()'); - echo enano_json_encode($return); - return; - } - - $q = $db->sql_query('SELECT "x" FROM '.table_prefix.'page_group_members WHERE pg_id=' . $edit_id . ' AND page_id=\'' . $db->escape($page_id) . '\' AND namespace=\'' . $namespace . '\';'); - if ( !$q ) - { - $return = array('mode' => 'error', 'text' => $db->get_error()); - echo enano_json_encode($return); - return; - } - if ( $db->numrows() > 0 ) - { - $return = array('mode' => 'error', 'text' => $lang->get('acppg_err_ajaxadd_already_in')); - echo enano_json_encode($return); - return; - } - - $q = $db->sql_query('INSERT INTO '.table_prefix.'page_group_members(pg_id, page_id, namespace) VALUES(' . $edit_id . ', \'' . $db->escape($page_id) . '\', \'' . $namespace . '\');'); - if ( !$q ) - { - $return = array('mode' => 'error', 'text' => $db->get_error()); - echo enano_json_encode($return); - return; - } - - $title = "($namespace) " . get_page_title($paths->nslist[$namespace] . $page_id); - - $return = array('mode' => 'info', 'text' => $lang->get('acppg_ajaxadd_success'), 'successful' => true, 'title' => $title, 'member_id' => $db->insert_id()); - - echo enano_json_encode($return); - return; - } - - if ( isset($_POST['action']['edit_save']) && isset($_POST['pg_name']) ) - { - $edit_id = $_POST['action']['edit']; - $edit_id = intval($edit_id); - if ( !empty($edit_id) ) - { - // Update group name - $new_name = $_POST['pg_name']; - if ( empty($new_name) ) - { - echo '
' . $lang->get('acppg_err_save_need_name') . '
'; - } - else - { - $q = $db->sql_query('SELECT pg_name FROM '.table_prefix.'page_groups WHERE pg_id=' . $edit_id . ';'); - if ( !$q ) - $db->_die(); - $row = $db->fetchrow(); - $db->free_result(); - if ( $new_name != $row['pg_name'] ) - { - $new_name = $db->escape(trim($new_name)); - $q = $db->sql_query('UPDATE '.table_prefix.'page_groups SET pg_name=\'' . $new_name . '\' WHERE pg_id=' . $edit_id . ';'); - if ( !$q ) - $db->_die(); - else - echo '
' . $lang->get('acppg_msg_save_name_updated') . '
'; - } - if ( $_POST['pg_type'] == PAGE_GRP_TAGGED ) - { - $target = $_POST['pg_target']; - $target = sanitize_tag($target); - if ( empty($target) ) - { - echo '
' . $lang->get('acppg_err_save_need_tag') . '
'; - } - else - { - $target = $db->escape($target); - $q = $db->sql_query('UPDATE '.table_prefix.'page_groups SET pg_target=\'' . $target . '\' WHERE pg_id=' . $edit_id . ';'); - if ( !$q ) - $db->_die(); - else - echo '
' . $lang->get('acppg_msg_save_tag_updated') . '
'; - } - } - else if ( $_POST['pg_type'] == PAGE_GRP_REGEX ) - { - $target = $_POST['pg_target']; - if ( empty($target) ) - { - echo '
' . $lang->get('acppg_err_save_need_regex') . '
'; - } - else - { - $target = $db->escape($target); - $q = $db->sql_query('UPDATE '.table_prefix.'page_groups SET pg_target=\'' . $target . '\' WHERE pg_id=' . $edit_id . ';'); - if ( !$q ) - $db->_die(); - else - echo '
' . $lang->get('acppg_msg_save_regex_updated') . '
'; - } - } - else if ( $_POST['pg_type'] == PAGE_GRP_CATLINK ) - { - $target = $_POST['pg_target']; - if ( empty($target) ) - { - echo '
' . $lang->get('acppg_err_save_bad_category') . '
'; - } - else - { - $target = $db->escape($target); - $q = $db->sql_query('UPDATE '.table_prefix.'page_groups SET pg_target=\'' . $target . '\' WHERE pg_id=' . $edit_id . ';'); - if ( !$q ) - $db->_die(); - else - echo '
' . $lang->get('acppg_msg_save_cat_updated') . '
'; - } - } - } - } - } - else if ( isset($_POST['action']['edit_save']) ) - { - $edit_id = $_POST['action']['edit']; - $edit_id = intval($edit_id); - } - else - { - $edit_id = array_keys($_POST['action']['edit']); - $edit_id = intval($edit_id[0]); - } - - if ( empty($edit_id) ) - { - echo 'Hack attempt'; - return; - } - - if ( isset($_POST['action']['edit_save']['do_rm']) && !isset($_POST['pg_name']) ) - { - $vals = array_keys($_POST['action']['edit_save']['rm']); - $good = array(); - foreach ( $vals as $id ) - { - if ( strval(intval($id)) == $id ) - $good[] = $id; - } - $subquery = ( count($good) > 0 ) ? 'pg_member_id=' . implode(' OR pg_member_id=', $good) : "'foo'='bar'"; - if ( $subquery == "'foo'='bar'" ) - { - echo '
' . $lang->get('acppg_err_save_no_pages') . '
'; - } - else - { - $sql = 'DELETE FROM '.table_prefix."page_group_members WHERE ( $subquery ) AND pg_id=$edit_id;"; - if ( !$db->sql_query($sql) ) - { - $db->_die(); - } - echo '
' . $lang->get('acppg_msg_save_pages_deleted') . '
'; - } - } - - // Fetch information about page group - $q = $db->sql_query('SELECT pg_name, pg_type, pg_target FROM '.table_prefix.'page_groups WHERE pg_id=' . $edit_id . ';'); - if ( !$q ) - $db->_die(); - - if ( $db->numrows() < 1 ) - { - echo 'Bad request - can\'t load page group from database.'; - return; - } - - $row = $db->fetchrow(); - $db->free_result(); - - echo '
'; - echo ''; - echo ''; - echo '
- - - - '; - // Group name - - echo ' - - - '; - - $ajax_page_add = false; - - // This is where the going gets tricky. - // For static groups, we need to have each page listed out with a removal button, and a form to add new pages. - // For category links, we need a select box with each category in it, and - // For tag sets, just a text box to enter a new tag. - - // You can guess which one I dreaded. - - switch ( $row['pg_type'] ) - { - case PAGE_GRP_NORMAL: - - // You have guessed correct. - // *Sits in chair for 10 minutes listening to the radio in an effort to put off writing the code you see below* - - echo ''; - echo '
' . $lang->get('acppg_th_editing_group') . ' ' . htmlspecialchars($row['pg_name']) . '
' . $lang->get('acppg_field_group_name') . '
'; - echo '
'; - echo '
'; - echo ''; - echo '
- - - - '; - - $q = $db->sql_query('SELECT m.pg_member_id,m.page_id,m.namespace FROM '.table_prefix.'page_group_members AS m - LEFT JOIN '.table_prefix.'pages AS p - ON ( p.urlname = m.page_id AND p.namespace = m.namespace ) - WHERE m.pg_id=' . $edit_id . ';'); - - if ( !$q ) - $db->_die(); - - $delim = ceil( $db->numrows($q) / 2 ); - if ( $delim < 5 ) - { - $delim = 0xFFFFFFFE; - // stupid hack. I'm XSSing my own code. - $colspan = '2" id="pg_edit_tackon2me'; - } - else - { - $colspan = "1"; - } - - echo ''; - echo ''; - - // More javascript magic! - ?> - - - - - '; - break; - case PAGE_GRP_REGEX: - echo ' - - - '; - break; - case PAGE_GRP_CATLINK: - - // Build category list - $q = $db->sql_query('SELECT name,urlname FROM '.table_prefix.'pages WHERE namespace=\'Category\';'); - if ( !$q ) - $db->_die(); - - if ( $db->numrows() < 1 ) - { - $catlist = 'There aren\'t any categories on this site.'; - } - else - { - $catlist = ''; - } - - echo ' - - - '; - - break; - } - - if ( $ajax_page_add ) - { - echo ''; - } - else - { - echo ''; - } - - echo '
' . $lang->get('acppg_th_remove_selected') . '
' . $lang->get('acppg_field_remove') . ''; - $i = 0; - - while ( $row = $db->fetchrow($q) ) - { - $i++; - if ( $i == $delim ) - { - echo ''; - } - $page_name = '(' . $row['namespace'] . ') ' . get_page_title($paths->nslist[$row['namespace']] . $row['page_id']); - echo '
'; - } - - echo '
- ' . $lang->get('acppg_field_target_tag') . ' - - -
- ' . $lang->get('acppg_field_target_regex') . '
- ' . $lang->get('acppg_field_target_regex_hint') . ' -
- -
- ' . $lang->get('acppg_field_target_category') . '
- ' . $lang->get('acppg_field_target_category_hint2') . ' -
- ' . $catlist . ' -
- - -
-
'; - echo '
'; - - if ( $ajax_page_add ) - { - // This needs to be outside of the form. - echo '
'; - echo ''; - echo ''; - // Add pages AJAX form - echo ''; - echo ''; - echo '
' . $lang->get('acppg_th_onthefly') . '
' . $lang->get('acppg_field_add_page') . '
' . $lang->get('acppg_field_add_page_hint') . '
'; - } - - return; - } - else if ( isset($_POST['action']['noop']) ) - { - // Do nothing - skip to main form (noop is usually invoked by a cancel button in a form above) - } - else - { - echo '
Invalid format of $_POST[action].
'; - } - } - // No action defined - show default menu - - echo '

' . $lang->get('acppg_heading_main') . '

'; - echo '

' . $lang->get('acppg_hint_intro') . '

'; - - $q = $db->sql_query('SELECT pg_id, pg_type, pg_name, pg_target FROM '.table_prefix.'page_groups;'); - if ( !$q ) - $db->_die(); + global $db, $session, $paths, $template, $plugins; // Common objects + global $lang; + if ( $session->auth_level < USER_LEVEL_ADMIN || $session->user_level < USER_LEVEL_ADMIN ) + { + $login_link = makeUrlNS('Special', 'Login/' . $paths->nslist['Special'] . 'Administration', 'level=' . USER_LEVEL_ADMIN, true); + echo '

' . $lang->get('adm_err_not_auth_title') . '

'; + echo '

' . $lang->get('adm_err_not_auth_body', array( 'login_link' => $login_link )) . '

'; + return; + } + + if ( isset($_POST['action']) ) + { + if ( isset($_POST['action']['create']) || isset($_POST['action']['create_stage2']) ) + { + switch ( isset($_POST['action']['create_stage2']) ) + { + case true: + if ( empty($_POST['pg_name']) || empty($_POST['group_type']) ) + { + echo '
' . $lang->get('acppg_err_need_name') . '
'; + return; + } + if ( $_POST['group_type'] == PAGE_GRP_TAGGED && empty($_POST['member_tag']) ) + { + echo '
' . $lang->get('acppg_err_need_tag') . '
'; + return; + } + if ( $_POST['group_type'] == PAGE_GRP_CATLINK && empty($_POST['member_cat']) ) + { + echo '
' . $lang->get('acppg_err_need_cat') . '
'; + return; + } + if ( $_POST['group_type'] == PAGE_GRP_NORMAL && empty($_POST['member_page_0']) ) + { + echo '
' . $lang->get('acppg_err_need_page') . '
'; + return; + } + if ( $_POST['group_type'] == PAGE_GRP_REGEX && empty($_POST['regex']) ) + { + echo '
' . $lang->get('acppg_err_need_regex') . '
'; + return; + } + if ( $_POST['group_type'] != PAGE_GRP_TAGGED && $_POST['group_type'] != PAGE_GRP_CATLINK && $_POST['group_type'] != PAGE_GRP_NORMAL && $_POST['group_type'] != PAGE_GRP_REGEX ) + { + echo '
Umm, you sent an invalid group type. I\'d put a real error message here but this will only be shown if you try to hack the system.
'; + return; + } + // All checks passed, create the group + switch($_POST['group_type']) + { + case PAGE_GRP_TAGGED: + $name = $db->escape($_POST['pg_name']); + $tag = $db->escape($_POST['member_tag']); + $sql = 'INSERT INTO '.table_prefix.'page_groups(pg_type,pg_name,pg_target) VALUES(' . PAGE_GRP_TAGGED . ', \'' . $name . '\', \'' . $tag . '\');'; + $q = $db->sql_query($sql); + if ( !$q ) + $db->_die(); + break; + case PAGE_GRP_CATLINK: + $name = $db->escape($_POST['pg_name']); + $cat = $db->escape($_POST['member_cat']); + $sql = 'INSERT INTO '.table_prefix.'page_groups(pg_type,pg_name,pg_target) VALUES(' . PAGE_GRP_CATLINK . ', \'' . $name . '\', \'' . $cat . '\');'; + $q = $db->sql_query($sql); + if ( !$q ) + $db->_die(); + break; + case PAGE_GRP_NORMAL: + $name = $db->escape($_POST['pg_name']); + $sql = 'INSERT INTO '.table_prefix.'page_groups(pg_type,pg_name) VALUES(' . PAGE_GRP_NORMAL . ', \'' . $name . '\');'; + $q = $db->sql_query($sql); + if ( !$q ) + $db->_die(); + + $ins_id = $db->insert_id(); + + // Page list + $keys = array_keys($_POST); + $arr_pages = array(); + foreach ( $keys as $val ) + { + if ( preg_match('/^member_page_([0-9]+?)$/', $val) && !empty($_POST[$val]) && isPage($_POST[$val]) ) + { + $arr_pages[] = $_POST[$val]; + } + } + $arr_sql = array(); + foreach ( $arr_pages as $page ) + { + list($id, $ns) = RenderMan::strToPageID($page); + $id = sanitize_page_id($id); + $arr_sql[] = '(' . $ins_id . ',\'' . $db->escape($id) . '\', \'' . $ns . '\')'; + } + $sql = 'INSERT INTO '.table_prefix.'page_group_members(pg_id,page_id,namespace) VALUES' . implode(',', $arr_sql) . ';'; + $q = $db->sql_query($sql); + if ( !$q ) + $db->_die(); + break; + case PAGE_GRP_REGEX: + $name = $db->escape($_POST['pg_name']); + $regex = $db->escape($_POST['regex']); + $sql = 'INSERT INTO '.table_prefix.'page_groups(pg_type,pg_name,pg_target) VALUES(' . PAGE_GRP_REGEX . ', \'' . $name . '\', \'' . $regex . '\');'; + $q = $db->sql_query($sql); + if ( !$q ) + $db->_die(); + break; + } + echo '
' . $lang->get('acppg_msg_create_success', array('group_name' => htmlspecialchars($_POST['pg_name']))) . '
'; + break; + } + // A little Javascript magic + ?> + + sql_query('SELECT name,urlname FROM '.table_prefix.'pages WHERE namespace=\'Category\';'); + if ( !$q ) + $db->_die(); + + if ( $db->numrows() < 1 ) + { + $catlist = $lang->get('acppg_err_no_cats'); + } + else + { + $catlist = ''; + } + + echo ''; + + echo '
'; + + echo '
+ + + + '; + + // Name + echo ' + + + '; + + // Group type + echo ' + + + '; + + // Titles + echo ' + + '; + + echo ' + '; + + echo ' + '; + + // Submit button + echo ' + + '; + + echo '
' . $lang->get('acppg_th_create') . '
+ ' . $lang->get('acppg_field_group_name') . '
+ ' . $lang->get('acppg_field_group_name_hint') . ' +
+ +
+ ' . $lang->get('acppg_field_group_type') . ' + + +
+ + ' . $lang->get('acppg_gtype_static') . ' + + + ' . $lang->get('acppg_gtype_tagged') . ' + + + ' . $lang->get('acppg_gtype_catlink') . ' + + + ' . $lang->get('acppg_gtype_regex') . ' + +
+
+ ' . $lang->get('acppg_field_member_pages') . '
+ ' . $lang->get('acppg_field_member_pages_hint') . ' +
+ +
+ ' . $lang->get('acppg_field_target_tag') . ' +
+
+ ' . $lang->get('acppg_field_target_regex') . '
+ ' . $lang->get('acppg_field_target_regex_hint') . ' +
+
+
+
+
+
+
+ +
+
+ +
+ +
+ +
+
+
'; + + echo '
'; + + echo ''; + return; + } + else if ( isset($_POST['action']['del']) ) + { + // Confirmation to delete a group (this is really only a stub) + + $delete_id = array_keys($_POST['action']['del']); + $delete_id = intval($delete_id[0]); + + if ( !empty($delete_id) ) + { + echo '
' . "\n"; + echo '' . "\n"; + echo '
' . "\n"; + echo ' ' . "\n"; + echo ' ' . "\n"; + echo ' ' . "\n"; + echo ' ' . "\n"; + echo '
' . $lang->get('acppg_th_delete_confirm') . '
' . $lang->get('acppg_msg_delete_confirm') . '
' . "\n"; + echo ' ' . "\n"; + echo ' ' . "\n"; + echo '
' . "\n"; + echo '' . "\n"; + + return; + } + } + else if ( isset($_POST['action']['del_confirm']) ) + { + $delete_id = intval($_POST['delete_id']); + if ( empty($delete_id) ) + { + echo 'Hack attempt'; + return; + } + // Obtain group name + $q = $db->sql_query('SELECT pg_name FROM '.table_prefix.'page_groups WHERE pg_id=' . $delete_id . ';'); + if ( !$q ) + $db->_die(); + if ( $db->numrows() < 1 ) + { + echo 'Page group dun exist.'; + return; + } + $row = $db->fetchrow(); + $db->free_result(); + $pg_name = $row['pg_name']; + unset($row); + // Delete the group + $q = $db->sql_query('DELETE FROM '.table_prefix.'page_groups WHERE pg_id=' . $delete_id . ';'); + if ( !$q ) + $db->_die(); + $q = $db->sql_query('DELETE FROM '.table_prefix.'page_group_members WHERE pg_id=' . $delete_id . ';'); + if ( !$q ) + $db->_die(); + + $del_msg = $lang->get('acppg_msg_delete_success', array('pg_name' => htmlspecialchars($pg_name))); + echo "
$del_msg
"; + } + else if ( isset($_POST['action']['edit']) && !isset($_POST['action']['noop']) ) + { + if ( isset($_POST['action']['edit_save']) ) + { + } + + if ( isset($_POST['action']['edit']['add_page']) && isset($_GET['src']) && $_GET['src'] == 'ajax' ) + { + $return = array('successful' => false); + + // + // Add the specified page to the group + // + + // Get ID of the group + $edit_id = intval($_POST['pg_id']); + if ( !$edit_id ) + { + $return = array('mode' => 'error', 'text' => 'Hack attempt'); + echo enano_json_encode($return); + return; + } + + // Run some validation - check that page exists and that it's not already in the group + $page = $_POST['new_page']; + if ( empty($page) ) + { + $return = array('mode' => 'error', 'text' => $lang->get('acppg_err_ajaxadd_need_title')); + echo enano_json_encode($return); + return; + } + + /* + // We're gonna allow adding nonexistent pages for now + if ( !isPage($page) ) + { + $return = array('mode' => 'error', 'text' => 'The page you are trying to add (' . htmlspecialchars($page) . ') does not exist.'); + echo enano_json_encode($return); + return; + } + */ + + list($page_id, $namespace) = RenderMan::strToPageID($page); + $page_id = sanitize_page_id($page_id); + + if ( !isset($paths->namespace[$namespace]) ) + { + $return = array('mode' => 'error', 'text' => 'Invalid namespace return from RenderMan::strToPageID()'); + echo enano_json_encode($return); + return; + } + + $q = $db->sql_query('SELECT "x" FROM '.table_prefix.'page_group_members WHERE pg_id=' . $edit_id . ' AND page_id=\'' . $db->escape($page_id) . '\' AND namespace=\'' . $namespace . '\';'); + if ( !$q ) + { + $return = array('mode' => 'error', 'text' => $db->get_error()); + echo enano_json_encode($return); + return; + } + if ( $db->numrows() > 0 ) + { + $return = array('mode' => 'error', 'text' => $lang->get('acppg_err_ajaxadd_already_in')); + echo enano_json_encode($return); + return; + } + + $q = $db->sql_query('INSERT INTO '.table_prefix.'page_group_members(pg_id, page_id, namespace) VALUES(' . $edit_id . ', \'' . $db->escape($page_id) . '\', \'' . $namespace . '\');'); + if ( !$q ) + { + $return = array('mode' => 'error', 'text' => $db->get_error()); + echo enano_json_encode($return); + return; + } + + $title = "($namespace) " . get_page_title($paths->nslist[$namespace] . $page_id); + + $return = array('mode' => 'info', 'text' => $lang->get('acppg_ajaxadd_success'), 'successful' => true, 'title' => $title, 'member_id' => $db->insert_id()); + + echo enano_json_encode($return); + return; + } + + if ( isset($_POST['action']['edit_save']) && isset($_POST['pg_name']) ) + { + $edit_id = $_POST['action']['edit']; + $edit_id = intval($edit_id); + if ( !empty($edit_id) ) + { + // Update group name + $new_name = $_POST['pg_name']; + if ( empty($new_name) ) + { + echo '
' . $lang->get('acppg_err_save_need_name') . '
'; + } + else + { + $q = $db->sql_query('SELECT pg_name FROM '.table_prefix.'page_groups WHERE pg_id=' . $edit_id . ';'); + if ( !$q ) + $db->_die(); + $row = $db->fetchrow(); + $db->free_result(); + if ( $new_name != $row['pg_name'] ) + { + $new_name = $db->escape(trim($new_name)); + $q = $db->sql_query('UPDATE '.table_prefix.'page_groups SET pg_name=\'' . $new_name . '\' WHERE pg_id=' . $edit_id . ';'); + if ( !$q ) + $db->_die(); + else + echo '
' . $lang->get('acppg_msg_save_name_updated') . '
'; + } + if ( $_POST['pg_type'] == PAGE_GRP_TAGGED ) + { + $target = $_POST['pg_target']; + $target = sanitize_tag($target); + if ( empty($target) ) + { + echo '
' . $lang->get('acppg_err_save_need_tag') . '
'; + } + else + { + $target = $db->escape($target); + $q = $db->sql_query('UPDATE '.table_prefix.'page_groups SET pg_target=\'' . $target . '\' WHERE pg_id=' . $edit_id . ';'); + if ( !$q ) + $db->_die(); + else + echo '
' . $lang->get('acppg_msg_save_tag_updated') . '
'; + } + } + else if ( $_POST['pg_type'] == PAGE_GRP_REGEX ) + { + $target = $_POST['pg_target']; + if ( empty($target) ) + { + echo '
' . $lang->get('acppg_err_save_need_regex') . '
'; + } + else + { + $target = $db->escape($target); + $q = $db->sql_query('UPDATE '.table_prefix.'page_groups SET pg_target=\'' . $target . '\' WHERE pg_id=' . $edit_id . ';'); + if ( !$q ) + $db->_die(); + else + echo '
' . $lang->get('acppg_msg_save_regex_updated') . '
'; + } + } + else if ( $_POST['pg_type'] == PAGE_GRP_CATLINK ) + { + $target = $_POST['pg_target']; + if ( empty($target) ) + { + echo '
' . $lang->get('acppg_err_save_bad_category') . '
'; + } + else + { + $target = $db->escape($target); + $q = $db->sql_query('UPDATE '.table_prefix.'page_groups SET pg_target=\'' . $target . '\' WHERE pg_id=' . $edit_id . ';'); + if ( !$q ) + $db->_die(); + else + echo '
' . $lang->get('acppg_msg_save_cat_updated') . '
'; + } + } + } + } + } + else if ( isset($_POST['action']['edit_save']) ) + { + $edit_id = $_POST['action']['edit']; + $edit_id = intval($edit_id); + } + else + { + $edit_id = array_keys($_POST['action']['edit']); + $edit_id = intval($edit_id[0]); + } + + if ( empty($edit_id) ) + { + echo 'Hack attempt'; + return; + } + + if ( isset($_POST['action']['edit_save']['do_rm']) && !isset($_POST['pg_name']) ) + { + $vals = array_keys($_POST['action']['edit_save']['rm']); + $good = array(); + foreach ( $vals as $id ) + { + if ( strval(intval($id)) == $id ) + $good[] = $id; + } + $subquery = ( count($good) > 0 ) ? 'pg_member_id=' . implode(' OR pg_member_id=', $good) : "'foo'='bar'"; + if ( $subquery == "'foo'='bar'" ) + { + echo '
' . $lang->get('acppg_err_save_no_pages') . '
'; + } + else + { + $sql = 'DELETE FROM '.table_prefix."page_group_members WHERE ( $subquery ) AND pg_id=$edit_id;"; + if ( !$db->sql_query($sql) ) + { + $db->_die(); + } + echo '
' . $lang->get('acppg_msg_save_pages_deleted') . '
'; + } + } + + // Fetch information about page group + $q = $db->sql_query('SELECT pg_name, pg_type, pg_target FROM '.table_prefix.'page_groups WHERE pg_id=' . $edit_id . ';'); + if ( !$q ) + $db->_die(); + + if ( $db->numrows() < 1 ) + { + echo 'Bad request - can\'t load page group from database.'; + return; + } + + $row = $db->fetchrow(); + $db->free_result(); + + echo '
'; + echo ''; + echo ''; + echo '
+ + + + '; + // Group name + + echo ' + + + '; + + $ajax_page_add = false; + + // This is where the going gets tricky. + // For static groups, we need to have each page listed out with a removal button, and a form to add new pages. + // For category links, we need a select box with each category in it, and + // For tag sets, just a text box to enter a new tag. + + // You can guess which one I dreaded. + + switch ( $row['pg_type'] ) + { + case PAGE_GRP_NORMAL: + + // You have guessed correct. + // *Sits in chair for 10 minutes listening to the radio in an effort to put off writing the code you see below* + + echo ''; + echo '
' . $lang->get('acppg_th_editing_group') . ' ' . htmlspecialchars($row['pg_name']) . '
' . $lang->get('acppg_field_group_name') . '
'; + echo '
'; + echo '
'; + echo ''; + echo '
+ + + + '; + + $q = $db->sql_query('SELECT m.pg_member_id,m.page_id,m.namespace FROM '.table_prefix.'page_group_members AS m + LEFT JOIN '.table_prefix.'pages AS p + ON ( p.urlname = m.page_id AND p.namespace = m.namespace ) + WHERE m.pg_id=' . $edit_id . ';'); + + if ( !$q ) + $db->_die(); + + $delim = ceil( $db->numrows($q) / 2 ); + if ( $delim < 5 ) + { + $delim = 0xFFFFFFFE; + // stupid hack. I'm XSSing my own code. + $colspan = '2" id="pg_edit_tackon2me'; + } + else + { + $colspan = "1"; + } + + echo ''; + echo ''; + + // More javascript magic! + ?> + + + + + '; + break; + case PAGE_GRP_REGEX: + echo ' + + + '; + break; + case PAGE_GRP_CATLINK: + + // Build category list + $q = $db->sql_query('SELECT name,urlname FROM '.table_prefix.'pages WHERE namespace=\'Category\';'); + if ( !$q ) + $db->_die(); + + if ( $db->numrows() < 1 ) + { + $catlist = 'There aren\'t any categories on this site.'; + } + else + { + $catlist = ''; + } + + echo ' + + + '; + + break; + } + + if ( $ajax_page_add ) + { + echo ''; + } + else + { + echo ''; + } + + echo '
' . $lang->get('acppg_th_remove_selected') . '
' . $lang->get('acppg_field_remove') . ''; + $i = 0; + + while ( $row = $db->fetchrow($q) ) + { + $i++; + if ( $i == $delim ) + { + echo ''; + } + $page_name = '(' . $row['namespace'] . ') ' . get_page_title($paths->nslist[$row['namespace']] . $row['page_id']); + echo '
'; + } + + echo '
+ ' . $lang->get('acppg_field_target_tag') . ' + + +
+ ' . $lang->get('acppg_field_target_regex') . '
+ ' . $lang->get('acppg_field_target_regex_hint') . ' +
+ +
+ ' . $lang->get('acppg_field_target_category') . '
+ ' . $lang->get('acppg_field_target_category_hint2') . ' +
+ ' . $catlist . ' +
+ + +
+
'; + echo '
'; + + if ( $ajax_page_add ) + { + // This needs to be outside of the form. + echo '
'; + echo ''; + echo ''; + // Add pages AJAX form + echo ''; + echo ''; + echo '
' . $lang->get('acppg_th_onthefly') . '
' . $lang->get('acppg_field_add_page') . '
' . $lang->get('acppg_field_add_page_hint') . '
'; + } + + return; + } + else if ( isset($_POST['action']['noop']) ) + { + // Do nothing - skip to main form (noop is usually invoked by a cancel button in a form above) + } + else + { + echo '
Invalid format of $_POST[action].
'; + } + } + // No action defined - show default menu + + echo '

' . $lang->get('acppg_heading_main') . '

'; + echo '

' . $lang->get('acppg_hint_intro') . '

'; + + $q = $db->sql_query('SELECT pg_id, pg_type, pg_name, pg_target FROM '.table_prefix.'page_groups;'); + if ( !$q ) + $db->_die(); - echo '
'; - - echo '
- - - - - - - '; - - if ( $row = $db->fetchrow($q) ) - { - do - { - $name = htmlspecialchars($row['pg_name']); - $type = 'Invalid'; - switch ( $row['pg_type'] ) - { - case PAGE_GRP_CATLINK: - $type = $lang->get('acppg_gtype_catlink'); - break; - case PAGE_GRP_TAGGED: - $type = $lang->get('acppg_gtype_tagged'); - break; - case PAGE_GRP_NORMAL: - $type = $lang->get('acppg_gtype_static'); - break; - case PAGE_GRP_REGEX: - $type = $lang->get('acppg_gtype_regex'); - break; - } - $target = ''; - if ( $row['pg_type'] == PAGE_GRP_TAGGED ) - { - $target = $lang->get('acppg_lbl_tag') . ' ' . htmlspecialchars($row['pg_target']); - } - else if ( $row['pg_type'] == PAGE_GRP_CATLINK ) - { - $target = $lang->get('acppg_lbl_category') . ' ' . htmlspecialchars(get_page_title($paths->nslist['Category'] . sanitize_page_id($row['pg_target']))); - } - else if ( $row['pg_type'] == PAGE_GRP_REGEX ) - { - $target = $lang->get('acppg_lbl_regex') . ' ' . htmlspecialchars($row['pg_target']) . ''; - } - $btn_edit = ''; - $btn_del = ''; - echo " - - - - - - "; - } - while ( $row = $db->fetchrow($q) ); - } - else - { - echo ' '; - } - - echo ' - - '; - - echo '
' . $lang->get('acppg_col_group_name') . '' . $lang->get('acppg_col_type') . '' . $lang->get('acppg_col_target') . '' . $lang->get('acppg_col_actions') . '
$name$type$target$btn_edit$btn_del
' . $lang->get('acppg_msg_no_groups') . '
- -
-
'; - - echo '
'; - + echo '
'; + + echo '
+ + + + + + + '; + + if ( $row = $db->fetchrow($q) ) + { + do + { + $name = htmlspecialchars($row['pg_name']); + $type = 'Invalid'; + switch ( $row['pg_type'] ) + { + case PAGE_GRP_CATLINK: + $type = $lang->get('acppg_gtype_catlink'); + break; + case PAGE_GRP_TAGGED: + $type = $lang->get('acppg_gtype_tagged'); + break; + case PAGE_GRP_NORMAL: + $type = $lang->get('acppg_gtype_static'); + break; + case PAGE_GRP_REGEX: + $type = $lang->get('acppg_gtype_regex'); + break; + } + $target = ''; + if ( $row['pg_type'] == PAGE_GRP_TAGGED ) + { + $target = $lang->get('acppg_lbl_tag') . ' ' . htmlspecialchars($row['pg_target']); + } + else if ( $row['pg_type'] == PAGE_GRP_CATLINK ) + { + $target = $lang->get('acppg_lbl_category') . ' ' . htmlspecialchars(get_page_title($paths->nslist['Category'] . sanitize_page_id($row['pg_target']))); + } + else if ( $row['pg_type'] == PAGE_GRP_REGEX ) + { + $target = $lang->get('acppg_lbl_regex') . ' ' . htmlspecialchars($row['pg_target']) . ''; + } + $btn_edit = ''; + $btn_del = ''; + echo " + + + + + + "; + } + while ( $row = $db->fetchrow($q) ); + } + else + { + echo ' '; + } + + echo ' + + '; + + echo '
' . $lang->get('acppg_col_group_name') . '' . $lang->get('acppg_col_type') . '' . $lang->get('acppg_col_target') . '' . $lang->get('acppg_col_actions') . '
$name$type$target$btn_edit$btn_del
' . $lang->get('acppg_msg_no_groups') . '
+ +
+
'; + + echo '
'; + } ?>