// Work-out if the page parent (if selected) has a seperate template to the default
 $query = "SELECT template FROM " . TABLE_PREFIX . "pages WHERE page_id = '{$parent}'";
 echoh($query . "<br />Module: {$module}<br />");
 $query_parent = $database->query($query);
 if ($query_parent->numRows() > 0) {
     $fetch_parent = $query_parent->fetchRow();
     $template = $fetch_parent['template'];
 } else {
     $template = '';
 }
 // Work out level
 $level = level_count($page_id);
 // Work out root parent
 $root_parent = root_parent($page_id);
 // Work out page trail
 $page_trail = get_page_trail($page_id);
 // Update page with new level and link
 $query = "UPDATE " . TABLE_PREFIX . "pages SET level = '{$level}', root_parent = '{$root_parent}', page_trail = '{$page_trail}', template = '{$template}' WHERE page_id = '{$page_id}'";
 echoh($query . "<br />");
 $database->query($query);
 // Create a new file in the /pages dir
 create_access_file($filename, $page_id, $level);
 /* clean up page order */
 $order = new order(TABLE_PREFIX . 'pages', 'position', 'page_id', 'parent');
 // First clean order
 $order->clean($parent);
 // Get new order for section
 $order = new order(TABLE_PREFIX . 'sections', 'position', 'section_id', 'page_id');
 $position = $order->get_new($parent);
 // Add new record into the sections table
 $query = "INSERT INTO " . TABLE_PREFIX . "sections (page_id,position,module,block) VALUES ('{$page_id}','{$position}', '{$module}','1')";
 public function createPage($title, $parent, $module, $visibility, $admin_groups, $viewing_groups)
 {
     global $database;
     // admin object initialisieren
     require_once WB_PATH . '/framework/class.admin.php';
     require_once WB_PATH . '/framework/functions.php';
     require_once WB_PATH . '/framework/class.order.php';
     $admin = new admin('Pages', 'pages_add', false, false);
     $title = htmlspecialchars($title);
     // sicherstellen, dass Admin in der Admin-Gruppe und in der Betrachter-Gruppe existiert
     if (!in_array(1, $admin_groups)) {
         $admin_groups[] = 1;
     }
     if (!in_array(1, $viewing_groups)) {
         $viewing_groups[] = 1;
     }
     // Leerer Titel?
     if ($title == '' || substr($title, 0, 1) == '.') {
         $this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, kit_error_blank_title));
         return false;
     }
     // pruefen, ob die Seite ueber die erforderlichen Rechte verfuegt
     if (!in_array(1, $admin->get_groups_id())) {
         $admin_perm_ok = false;
         foreach ($admin_groups as $adm_group) {
             if (in_array($adm_group, $admin->get_groups_id())) {
                 $admin_perm_ok = true;
             }
         }
         if ($admin_perm_ok == false) {
             $this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, kit_error_insufficient_permissions));
             return false;
         }
         $admin_perm_ok = false;
         foreach ($viewing_groups as $view_group) {
             if (in_array($view_group, $admin->get_groups_id())) {
                 $admin_perm_ok = true;
             }
         }
         if ($admin_perm_ok == false) {
             $this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, kit_error_insufficient_permissions));
             return false;
         }
     }
     $admin_groups = implode(',', $admin_groups);
     $viewing_groups = implode(',', $viewing_groups);
     // Dateinamen erstellen
     if ($parent == '0') {
         $link = '/' . page_filename($title);
         // Dateinamen 'index' und 'intro' umbenennen um Kollisionen zu vermeiden
         if ($link == '/index' || $link == '/intro') {
             $link .= '_0';
             $filename = WB_PATH . PAGES_DIRECTORY . '/' . page_filename($title) . '_0' . PAGE_EXTENSION;
         } else {
             $filename = WB_PATH . PAGES_DIRECTORY . '/' . page_filename($title) . PAGE_EXTENSION;
         }
     } else {
         $parent_section = '';
         $parent_titles = array_reverse(get_parent_titles($parent));
         foreach ($parent_titles as $parent_title) {
             $parent_section .= page_filename($parent_title) . '/';
         }
         if ($parent_section == '/') {
             $parent_section = '';
         }
         $page_filename = page_filename($title);
         $page_filename = str_replace('_', '-', $page_filename);
         $link = '/' . $parent_section . $page_filename;
         $filename = WB_PATH . PAGES_DIRECTORY . '/' . $parent_section . $page_filename . PAGE_EXTENSION;
         make_dir(WB_PATH . PAGES_DIRECTORY . '/' . $parent_section);
     }
     // prufen, ob bereits eine Datei mit dem gleichen Dateinamen existiert
     $dbPages = new db_wb_pages();
     $where = array();
     $where[db_wb_pages::field_link] = $link;
     $pages = array();
     if (!$dbPages->sqlSelectRecord($where, $pages)) {
         $this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $dbPages->getError()));
         return false;
     }
     if (sizeof($pages) > 0 || file_exists(WB_PATH . PAGES_DIRECTORY . $link . PAGE_EXTENSION) || file_exists(WB_PATH . PAGES_DIRECTORY . $link . '/')) {
         $this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(kit_error_page_exists, $link)));
         return false;
     }
     // include the ordering class
     $order = new order(TABLE_PREFIX . 'pages', 'position', 'page_id', 'parent');
     // clean order
     $order->clean($parent);
     // get the new order
     $position = $order->get_new($parent);
     // Template und Sprache der uebergeordneten Seite ermitteln
     $where = array();
     $where[db_wb_pages::field_page_id] = $parent;
     $pages = array();
     if (!$dbPages->sqlSelectRecord($where, $pages)) {
         $this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $dbPages->getError()));
         return false;
     }
     if (sizeof($pages) > 0) {
         $template = $pages[0][db_wb_pages::field_template];
         $language = $pages[0][db_wb_pages::field_language];
     } else {
         $template = '';
         $language = DEFAULT_LANGUAGE;
     }
     // Neue Seite in Tabelle einfuegen
     $data = array();
     $data[db_wb_pages::field_page_title] = $title;
     $data[db_wb_pages::field_menu_title] = $title;
     $data[db_wb_pages::field_parent] = $parent;
     $data[db_wb_pages::field_template] = $template;
     $data[db_wb_pages::field_target] = '_top';
     $data[db_wb_pages::field_position] = $position;
     $data[db_wb_pages::field_visibility] = $visibility;
     $data[db_wb_pages::field_searching] = 1;
     $data[db_wb_pages::field_menu] = 1;
     $data[db_wb_pages::field_language] = $language;
     $data[db_wb_pages::field_admin_groups] = $admin_groups;
     $data[db_wb_pages::field_viewing_groups] = $viewing_groups;
     $data[db_wb_pages::field_modified_when] = time();
     $data[db_wb_pages::field_modified_by] = $admin->get_user_id();
     $page_id = -1;
     if (!$dbPages->sqlInsertRecord($data, $page_id)) {
         $this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $dbPages->getError()));
         return false;
     }
     // work out the level
     $level = level_count($page_id);
     // work out root parent
     $root_parent = root_parent($page_id);
     // work out page trail
     $page_trail = get_page_trail($page_id);
     $where = array();
     $where[db_wb_pages::field_page_id] = $page_id;
     $data = array();
     $data[db_wb_pages::field_link] = $link;
     $data[db_wb_pages::field_level] = $level;
     $data[db_wb_pages::field_root_parent] = $root_parent;
     $data[db_wb_pages::field_page_trail] = $page_trail;
     if (!$dbPages->sqlUpdateRecord($data, $where)) {
         $this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $dbPages->getError()));
         return false;
     }
     // create a new file in the /pages directory
     create_access_file($filename, $page_id, $level);
     // add position 1 to new page
     $position = 1;
     // add a new record to section table
     $dbSections = new db_wb_sections();
     $data = array();
     $data[db_wb_sections::field_page_id] = $page_id;
     $data[db_wb_sections::field_position] = $position;
     $data[db_wb_sections::field_module] = $module;
     $data[db_wb_sections::field_block] = 1;
     $section_id = -1;
     if (!$dbSections->sqlInsertRecord($data, $section_id)) {
         $this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $dbSections->getError()));
         return false;
     }
     if (file_exists(WB_PATH . '/modules/' . $module . '/add.php')) {
         require WB_PATH . '/modules/' . $module . '/add.php';
     }
     if ($database->is_error()) {
         $this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $database->get_error()));
         return false;
     }
     return $page_id;
 }
function fix_page_trail($parent, $root_parent)
{
    // Get objects and vars from outside this function
    global $admin, $template, $database, $TEXT, $MESSAGE;
    // Get page list from database
    // $database = new database();
    $query = "SELECT page_id FROM " . TABLE_PREFIX . "pages WHERE parent = '{$parent}'";
    $get_pages = $database->query($query);
    // Insert values into main page list
    if ($get_pages->numRows() > 0) {
        while ($page = $get_pages->fetchRow()) {
            // Fix page trail
            $database->query("UPDATE " . TABLE_PREFIX . "pages SET " . ($root_parent != 0 ? "root_parent = '{$root_parent}', " : "") . " page_trail = '" . get_page_trail($page['page_id']) . "' WHERE page_id = '" . $page['page_id'] . "'");
            // Run this query on subs
            fix_page_trail($page['page_id'], $root_parent);
        }
    }
}
function fix_page_trail($parent, $root_parent)
{
    // Get objects and vars from outside this function
    global $admin, $template, $database, $TEXT, $MESSAGE;
    // Get page list from database
    // $database = new database();
    $sql = 'SELECT `page_id` FROM `' . TABLE_PREFIX . 'pages` ' . 'WHERE `parent`=' . (int) $parent;
    // Insert values into main page list
    if ($get_pages = $database->query($sql)) {
        // Insert values into main page list
        while ($page = $get_pages->fetchRow(MYSQLI_ASSOC)) {
            // Fix page trail
            $sql = 'UPDATE `' . TABLE_PREFIX . 'pages` ' . 'SET `page_trail`=\'' . get_page_trail($page['page_id']) . '\' ' . ($root_parent != 0 ? '`root_parent`=' . (int) $root_parent . ', ' : '') . 'WHERE `page_id`=' . (int) $page['page_id'];
            $database->query($sql);
            // Run this query on subs
            fix_page_trail($page['page_id'], $root_parent);
        }
    }
}
function clone_page($title, $parent, $pagetoclone, $copy_title, $visibility)
{
    // Get objects and vars from outside this function
    global $admin, $template, $database, $TEXT, $PCTEXT, $MESSAGE;
    global $page_id, $section_id;
    // Get page list from database
    $query = "SELECT * FROM `" . TABLE_PREFIX . "pages` WHERE `page_id` = " . $pagetoclone;
    $get_page = $database->query($query);
    $is_page = $get_page->fetchRow(MYSQL_ASSOC);
    // Work-out what the link and page filename should be
    if ($parent == '0') {
        $link = '/' . page_filename($title);
        $filename = WB_PATH . PAGES_DIRECTORY . $link . '.php';
    } else {
        $parent_section = '';
        $parent_titles = array_reverse(get_parent_titles($parent));
        foreach ($parent_titles as $parent_title) {
            $parent_section .= page_filename($parent_title) . '/';
        }
        if ($parent_section == '/') {
            $parent_section = '';
        }
        $link = '/' . $parent_section . page_filename($title);
        $filename = WB_PATH . PAGES_DIRECTORY . '/' . $parent_section . page_filename($title) . '.php';
        make_dir(WB_PATH . PAGES_DIRECTORY . '/' . $parent_section);
    }
    // Check if a page with same page filename exists
    $get_same_page = $database->query("SELECT `page_id` FROM `" . TABLE_PREFIX . "pages` WHERE `link` = '{$link}'");
    if ($get_same_page->numRows() > 0 or file_exists(WB_PATH . PAGES_DIRECTORY . $link . '.php') or file_exists(WB_PATH . PAGES_DIRECTORY . $link . '/')) {
        $admin->print_error($MESSAGE['PAGES']['PAGE_EXISTS'], 'tool_clone.php?pagetoclone=' . $pagetoclone);
    }
    // check the title
    if ($copy_title) {
        $page_title = $is_page['page_title'];
    } else {
        $page_title = $title;
    }
    // Include the ordering class
    $order = new order(TABLE_PREFIX . 'pages', 'position', 'page_id', 'parent');
    // First clean order
    $order->clean($parent);
    // Get new order
    $position = $order->get_new($parent);
    // Insert page into pages table
    $template = $is_page['template'];
    $visibility = $visibility;
    $admin_groups = $is_page['admin_groups'];
    $viewing_groups = $is_page['viewing_groups'];
    $query = "INSERT INTO `" . TABLE_PREFIX . "pages` " . "(`page_title`,`menu_title`,`parent`,`template`,`target`,`position`,`visibility`,`searching`,`menu`,`language`,`admin_groups`,`viewing_groups`,`modified_when`,`modified_by`) VALUES ('" . $database->escapeString($page_title) . "','" . $database->escapeString($title) . "','{$parent}','{$template}','_top','{$position}','{$visibility}','1','1','" . DEFAULT_LANGUAGE . "','{$admin_groups}','{$viewing_groups}','" . time() . "','" . $admin->get_user_id() . "')";
    $database->query($query);
    if ($database->is_error()) {
        $admin->print_error($database->get_error());
    }
    // Get the page id
    $page_id = $database->get_one("SELECT LAST_INSERT_ID()");
    // Work out level
    $level = level_count($page_id);
    // Work out root parent
    $root_parent = root_parent($page_id);
    // Work out page trail
    $page_trail = get_page_trail($page_id);
    // Update page with new level and link
    $database->query("UPDATE `" . TABLE_PREFIX . "pages` SET `link` = '{$link}', `level` = '{$level}', `root_parent` = '{$root_parent}', `page_trail` = '{$page_trail}' WHERE `page_id` = '{$page_id}'");
    // Create a new file in the /pages dir
    create_access_file($filename, $page_id, $level);
    // Make new sections, database
    $query = "SELECT * FROM `" . TABLE_PREFIX . "sections` WHERE `page_id` = '{$pagetoclone}'";
    $get_section = $database->query($query);
    while (false != ($is_section = $get_section->fetchRow(MYSQL_ASSOC))) {
        // Add new record into the sections table
        $from_section = $is_section['section_id'];
        $position = $is_section['position'];
        $module = $is_section['module'];
        $block = $is_section['block'];
        $publ_start = $is_section['publ_start'];
        $publ_end = $is_section['publ_end'];
        $database->query("INSERT INTO `" . TABLE_PREFIX . "sections` (`page_id`,`position`,`module`,`block`,`publ_start`,`publ_end`) VALUES ('{$page_id}','{$position}', '{$module}','{$block}','{$publ_start}','{$publ_end}')");
        // Get the section id
        $section_id = $database->get_one("SELECT LAST_INSERT_ID()");
        require WB_PATH . '/modules/' . $module . '/info.php';
        // Include the selected modules add file if it exists
        if (file_exists(WB_PATH . '/modules/' . $module . '/add.php')) {
            require WB_PATH . '/modules/' . $module . '/add.php';
        }
        // copy module settings per section
        $query = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '%mod_" . $module . "%'";
        $res = $database->query($query);
        while ($row = $res->fetchRow()) {
            // there must be a section_id column at least
            if ($database->query("DESCRIBE {$row['0']} section_id")) {
                clone_lines($row[0], $pagetoclone, $page_id, $from_section, $section_id, $database);
            }
        }
        // some manual corrections that can not be automatically detected
        if ($module == 'miniform') {
            // delete the form submissions which are also copied
            $query = "DELETE FROM " . TABLE_PREFIX . "mod_miniform_data WHERE `section_id` = " . $section_id;
            $database->query($query);
        } elseif ($module == 'mpform') {
            // delete the form submissions which are also copied
            $query = "DELETE FROM " . TABLE_PREFIX . "mod_mpform_submissions WHERE `section_id` = " . $section_id;
            $database->query($query);
            // update refererence to result table
            $query = "UPDATE " . TABLE_PREFIX . "mod_mpform_settings SET `tbl_suffix` = " . $section_id . " WHERE `section_id` = " . $section_id;
            $database->query($query);
            // new results table
            $results = TABLE_PREFIX . "mod_mpform_results_" . $section_id;
            $s = "CREATE TABLE `{$results}` ( `session_id` VARCHAR(20) NOT NULL," . ' `started_when` INT NOT NULL DEFAULT \'0\' ,' . ' `submitted_when` INT NOT NULL DEFAULT \'0\' ,' . ' `referer` VARCHAR( 255 ) NOT NULL, ' . ' PRIMARY KEY ( `session_id` ) ' . ' )';
            $database->query($s);
            $query = "SELECT field_id FROM " . TABLE_PREFIX . "mod_mpform_fields WHERE `section_id` = " . $section_id;
            $ids = $database->query($query);
            while ($fid = $ids->fetchRow()) {
                // Insert new column into database
                $s = "ALTER TABLE `{$results}` add `field" . $fid[0] . "` TEXT NOT NULL";
                $database->query($s);
            }
        } elseif ($module == 'form') {
            // delete the form submissions which are also copied
            $query = "DELETE FROM " . TABLE_PREFIX . "mod_form_submissions WHERE `section_id` = " . $section_id;
            $database->query($query);
        } elseif ($module == 'minigallery') {
            // copy images
            $mediaDir = WB_PATH . MEDIA_DIRECTORY;
            $src = $mediaDir . "/minigallery/{$from_section}";
            $dst = $mediaDir . "/minigallery/{$section_id}";
            recurse_copy($src, $dst);
        }
    }
    return $page_id;
}