Esempio n. 1
0
 function update_weblog_prefs()
 {
     global $DSP, $IN, $DB, $LOG, $LANG, $FNS, $PREFS, $SESS, $LOC;
     if (!$DSP->allowed_group('can_admin_weblogs')) {
         return $DSP->no_access_message();
     }
     // If the $weblog_id variable is present we are editing an
     // existing weblog, otherwise we are creating a new one
     $edit = isset($_POST['weblog_id']) ? TRUE : FALSE;
     $add_rss = isset($_POST['add_rss']) ? TRUE : FALSE;
     unset($_POST['add_rss']);
     $return = $IN->GBL('return') ? TRUE : FALSE;
     unset($_POST['return']);
     unset($_POST['edit_group_prefs']);
     $dupe_id = $IN->GBL('duplicate_weblog_prefs');
     unset($_POST['duplicate_weblog_prefs']);
     // Check for required fields
     $error = array();
     if ($_POST['blog_name'] == '') {
         $error[] = $LANG->line('no_weblog_name');
     }
     if ($_POST['blog_title'] == '') {
         $error[] = $LANG->line('no_weblog_title');
     }
     if (preg_match('/[^a-z0-9\\-\\_]/i', $_POST['blog_name'])) {
         $error[] = $LANG->line('invalid_short_name');
     }
     if (isset($_POST['url_title_prefix']) && $_POST['url_title_prefix'] != '') {
         $_POST['url_title_prefix'] = strtolower(strip_tags($_POST['url_title_prefix']));
         if (!preg_match("/^[\\w\\-]+\$/", $_POST['url_title_prefix'])) {
             $error[] = $LANG->line('invalid_url_title_prefix');
         }
     }
     if (count($error) > 0) {
         $msg = '';
         foreach ($error as $val) {
             $msg .= $val . BR;
         }
         return $DSP->error_message($msg);
     }
     if (isset($_POST['comment_expiration'])) {
         if (!is_numeric($_POST['comment_expiration']) || $_POST['comment_expiration'] == '') {
             $_POST['comment_expiration'] = 0;
         }
     }
     // Is the weblog name taken?
     $sql = "SELECT COUNT(*) AS count FROM exp_weblogs WHERE site_id = '" . $DB->escape_str($PREFS->ini('site_id')) . "' AND blog_name = '" . $DB->escape_str($_POST['blog_name']) . "'";
     if ($edit == TRUE) {
         $sql .= " AND weblog_id != '" . $DB->escape_str($_POST['weblog_id']) . "'";
     }
     $query = $DB->query($sql);
     if ($query->row['count'] > 0) {
         return $DSP->error_message($LANG->line('taken_weblog_name'));
     }
     /** -----------------------------------------
     		/**  Template Error Trapping
     		/** -----------------------------------------*/
     if ($edit == FALSE) {
         $create_templates = $IN->GBL('create_templates');
         $old_group_id = $IN->GBL('old_group_id');
         $group_name = strtolower($IN->GBL('group_name', 'POST'));
         $template_theme = $FNS->filename_security($IN->GBL('template_theme'));
         unset($_POST['create_templates']);
         unset($_POST['old_group_id']);
         unset($_POST['group_name']);
         unset($_POST['template_theme']);
         if ($create_templates != 'no') {
             $LANG->fetch_language_file('templates');
             if (!$DSP->allowed_group('can_admin_templates')) {
                 return $DSP->no_access_message();
             }
             if (!$group_name) {
                 return $DSP->error_message($LANG->line('group_required'));
             }
             if (!preg_match("#^[a-zA-Z0-9_\\-/]+\$#i", $group_name)) {
                 return $DSP->error_message($LANG->line('illegal_characters'));
             }
             $reserved[] = 'act';
             $reserved[] = 'trackback';
             if ($PREFS->ini("forum_is_installed") == 'y' and $PREFS->ini("forum_trigger") != '') {
                 $reserved[] = $PREFS->ini("forum_trigger");
             }
             if (in_array($group_name, $reserved)) {
                 return $DSP->error_message($LANG->line('reserved_name'));
             }
             $query = $DB->query("SELECT COUNT(*) AS count FROM exp_template_groups \n\t\t\t\t\t\t\t\t\t WHERE site_id = '" . $DB->escape_str($PREFS->ini('site_id')) . "' \n\t\t\t\t\t\t\t\t\t AND group_name = '" . $DB->escape_str($group_name) . "'");
             if ($query->row['count'] > 0) {
                 return $DSP->error_message($LANG->line('template_group_taken'));
             }
         }
     }
     /** -----------------------------------------
     		/**  Create Weblog
     		/** -----------------------------------------*/
     // Construct the query based on whether we are updating or inserting
     if (isset($_POST['apply_expiration_to_existing'])) {
         $this->update_comment_expiration($_POST['weblog_id'], $_POST['comment_expiration']);
     }
     unset($_POST['apply_expiration_to_existing']);
     if (isset($_POST['cat_group']) && is_array($_POST['cat_group'])) {
         foreach ($_POST['cat_group'] as $key => $value) {
             unset($_POST['cat_group_' . $key]);
         }
         $_POST['cat_group'] = implode('|', $_POST['cat_group']);
     }
     if ($edit == FALSE) {
         unset($_POST['weblog_id']);
         unset($_POST['clear_versioning_data']);
         $_POST['blog_url'] = $FNS->fetch_site_index();
         $_POST['blog_lang'] = $PREFS->ini('xml_lang');
         $_POST['blog_encoding'] = $PREFS->ini('charset');
         // Assign field group if there is only one
         if (!isset($_POST['field_group']) or isset($_POST['field_group']) && !is_numeric($_POST['field_group'])) {
             $query = $DB->query("SELECT group_id FROM exp_field_groups WHERE site_id = '" . $DB->escape_str($PREFS->ini('site_id')) . "'");
             if ($query->num_rows == 1) {
                 $_POST['field_group'] = $query->row['group_id'];
             }
         }
         // Insert data
         $_POST['site_id'] = $PREFS->ini('site_id');
         // duplicating preferences?
         if ($dupe_id !== FALSE and is_numeric($dupe_id)) {
             $wquery = $DB->query("SELECT * FROM exp_weblogs WHERE weblog_id = '" . $DB->escape_str($dupe_id) . "'");
             if ($wquery->num_rows == 1) {
                 $exceptions = array('weblog_id', 'site_id', 'blog_name', 'blog_title', 'total_entries', 'total_comments', 'total_trackbacks', 'last_entry_date', 'last_comment_date', 'last_trackback_date');
                 foreach ($wquery->row as $key => $val) {
                     // don't duplicate fields that are unique to each weblog
                     if (!in_array($key, $exceptions)) {
                         switch ($key) {
                             // category, field, and status fields should only be duped
                             // if both weblogs are assigned to the same group of each
                             case 'cat_group':
                                 // allow to implicitly set category group to "None"
                                 if (!isset($_POST[$key])) {
                                     $_POST[$key] = $val;
                                 }
                                 break;
                             case 'status_group':
                             case 'field_group':
                                 if (!isset($_POST[$key]) or $_POST[$key] == '') {
                                     $_POST[$key] = $val;
                                 }
                                 break;
                             case 'deft_status':
                                 if (!isset($_POST['status_group']) or $_POST['status_group'] == $wquery->row['status_group']) {
                                     $_POST[$key] = $val;
                                 }
                                 break;
                             case 'search_excerpt':
                                 if (!isset($_POST['field_group']) or $_POST['field_group'] == $wquery->row['field_group']) {
                                     $_POST[$key] = $val;
                                 }
                                 break;
                             case 'deft_category':
                                 if (!isset($_POST['cat_group']) or count(array_diff(explode('|', $_POST['cat_group']), explode('|', $wquery->row['cat_group']))) == 0) {
                                     $_POST[$key] = $val;
                                 }
                                 break;
                             case 'blog_url':
                             case 'comment_url':
                             case 'search_results_url':
                             case 'tb_return_url':
                             case 'ping_return_url':
                             case 'rss_url':
                                 if ($create_templates != 'no') {
                                     if (!isset($old_group_name)) {
                                         $gquery = $DB->query("SELECT group_name FROM exp_template_groups WHERE group_id = '" . $DB->escape_str($old_group_id) . "'");
                                         $old_group_name = $gquery->row['group_name'];
                                     }
                                     $_POST[$key] = str_replace("/{$old_group_name}/", "/{$group_name}/", $val);
                                 } else {
                                     $_POST[$key] = $val;
                                 }
                                 break;
                             default:
                                 $_POST[$key] = $val;
                                 break;
                         }
                     }
                 }
             }
         }
         $sql = $DB->insert_string('exp_weblogs', $_POST);
         $DB->query($sql);
         $insert_id = $DB->insert_id;
         $weblog_id = $insert_id;
         $success_msg = $LANG->line('weblog_created');
         $crumb = $DSP->crumb_item($LANG->line('new_weblog'));
         $LOG->log_action($success_msg . $DSP->nbs(2) . $_POST['blog_title']);
     } else {
         if (isset($_POST['clear_versioning_data'])) {
             $DB->query("DELETE FROM exp_entry_versioning WHERE weblog_id  = '" . $DB->escape_str($_POST['weblog_id']) . "'");
             unset($_POST['clear_versioning_data']);
         }
         $sql = $DB->update_string('exp_weblogs', $_POST, 'weblog_id=' . $DB->escape_str($_POST['weblog_id']));
         $DB->query($sql);
         $weblog_id = $DB->escape_str($_POST['weblog_id']);
         $success_msg = $LANG->line('weblog_updated');
         $crumb = $DSP->crumb_item($LANG->line('update'));
     }
     /** -----------------------------------------
     		/**  Create Templates
     		/** -----------------------------------------*/
     if ($edit == FALSE) {
         if ($create_templates != 'no') {
             $query = $DB->query("SELECT COUNT(*) AS count FROM exp_template_groups WHERE is_user_blog = 'n'");
             $group_order = $query->row['count'] + 1;
             $DB->query($DB->insert_string('exp_template_groups', array('group_id' => '', 'group_name' => $group_name, 'group_order' => $group_order, 'is_site_default' => 'n', 'site_id' => $PREFS->ini('site_id'))));
             $group_id = $DB->insert_id;
             if ($create_templates == 'duplicate') {
                 $query = $DB->query("SELECT group_name FROM exp_template_groups WHERE group_id = '" . $DB->escape_str($old_group_id) . "'");
                 $old_group_name = $query->row['group_name'];
                 $query = $DB->query("SELECT template_name, template_data, template_type, template_notes, cache, refresh, no_auth_bounce, allow_php, php_parse_location FROM exp_templates WHERE group_id = '" . $DB->escape_str($old_group_id) . "'");
                 if ($query->num_rows == 0) {
                     $DB->query($DB->insert_string('exp_templates', array('template_id' => '', 'group_id' => $group_id, 'template_name' => 'index', 'edit_date' => $LOC->now, 'site_id' => $PREFS->ini('site_id'))));
                 } else {
                     $old_blog_name = '';
                     foreach ($query->result as $row) {
                         if ($old_blog_name == '') {
                             if (preg_match_all("/weblog=[\"'](.+?)[\"']/", $row['template_data'], $matches)) {
                                 for ($i = 0; $i < count($matches['1']); $i++) {
                                     if (substr($matches['1'][$i], 0, 1) != '{') {
                                         $old_blog_name = $matches['1'][$i];
                                         break;
                                     }
                                 }
                             }
                         }
                         $temp = str_replace('weblog="' . $old_blog_name . '"', 'weblog="' . $_POST['blog_name'] . '"', $row['template_data']);
                         $temp = str_replace("weblog='" . $old_blog_name . "'", 'weblog="' . $_POST['blog_name'] . '"', $temp);
                         $temp = preg_replace("/{stylesheet=.+?\\/(.+?)}/", "{stylesheet=" . $group_name . "/\\1}", $temp);
                         $temp = preg_replace("#assign_variable:master_weblog_name=\".+?\"#", 'assign_variable:master_weblog_name="' . $_POST['blog_name'] . '"', $temp);
                         $temp = preg_replace("#assign_variable:master_weblog_name=\\'.+?\\'#", "assign_variable:master_weblog_name='" . $_POST['blog_name'] . "'", $temp);
                         $temp = preg_replace('#assign_variable:my_template_group=(\\042|\\047)([^\\1]*?)\\1#', "assign_variable:my_template_group=\\1{$group_name}\\1", $temp);
                         $temp = preg_replace("#" . $old_group_name . "/(.+?)#", $group_name . "/\\1", $temp);
                         $data = array('template_id' => '', 'group_id' => $group_id, 'template_name' => $row['template_name'], 'template_notes' => $row['template_notes'], 'cache' => $row['cache'], 'refresh' => $row['refresh'], 'no_auth_bounce' => $row['no_auth_bounce'], 'php_parse_location' => $row['php_parse_location'], 'allow_php' => $SESS->userdata['group_id'] == 1 ? $row['allow_php'] : 'n', 'template_type' => $row['template_type'], 'template_data' => $temp, 'edit_date' => $LOC->now, 'site_id' => $PREFS->ini('site_id'));
                         $DB->query($DB->insert_string('exp_templates', $data));
                     }
                 }
             } else {
                 $type = 'core';
                 if ($fp = @opendir(PATH_MOD)) {
                     while (false !== ($file = readdir($fp))) {
                         if (strpos($file, '.') === FALSE) {
                             if ($file == 'mailinglist') {
                                 $type = 'full';
                                 break;
                             }
                         }
                     }
                     closedir($fp);
                 }
                 require PATH_THEMES . 'site_themes/' . $template_theme . '/' . $template_theme . '.php';
                 foreach ($template_matrix as $tmpl) {
                     $Q[] = array($tmpl['0'](), "INSERT INTO exp_templates(template_id, group_id, template_name, template_type, template_data, edit_date, site_id) \n\t\t\t\t\t\t\t\t\t\t\t\t\tVALUES ('', '{$group_id}', '" . $DB->escape_str($tmpl['0']) . "', '" . $DB->escape_str($tmpl['1']) . "', '{template}', '" . $LOC->now . "', '" . $DB->escape_str($PREFS->ini('site_id')) . "')");
                 }
                 if ($add_rss == TRUE) {
                     require PATH_THEMES . 'site_themes/rss/rss.php';
                     $Q[] = array(rss_2(), "INSERT INTO exp_templates(template_id, group_id, template_name, template_type, template_data, edit_date, site_id) \n\t\t\t\t\t\t\t\t\t\t\t   VALUES ('', '{$group_id}', 'rss_2.0', 'rss', '{template}', '" . $DB->escape_str($LOC->now) . "', '" . $DB->escape_str($PREFS->ini('site_id')) . "')");
                     $Q[] = array(atom(), "INSERT INTO exp_templates(template_id, group_id, template_name, template_type, template_data, edit_date, site_id) \n\t\t\t\t\t\t\t\t\t\t\t  VALUES ('', '{$group_id}', 'atom', 'rss', '{template}', '" . $DB->escape_str($LOC->now) . "', '" . $DB->escape_str($PREFS->ini('site_id')) . "')");
                 }
                 foreach ($Q as $val) {
                     $temp = $val['0'];
                     $temp = str_replace('weblog="weblog1"', 'weblog="' . $_POST['blog_name'] . '"', $temp);
                     $temp = str_replace("weblog='weblog1'", 'weblog="' . $_POST['blog_name'] . '"', $temp);
                     $temp = str_replace('my_weblog="weblog1"', 'my_weblog="' . $_POST['blog_name'] . '"', $temp);
                     $temp = str_replace("my_weblog='weblog1'", 'my_weblog="' . $_POST['blog_name'] . '"', $temp);
                     $temp = str_replace('weblog="default_site"', 'weblog="' . $_POST['blog_name'] . '"', $temp);
                     $temp = str_replace("weblog='default_site'", 'weblog="' . $_POST['blog_name'] . '"', $temp);
                     $temp = str_replace('my_weblog="default_site"', 'my_weblog="' . $_POST['blog_name'] . '"', $temp);
                     $temp = str_replace("my_weblog='default_site'", 'my_weblog="' . $_POST['blog_name'] . '"', $temp);
                     $temp = str_replace('my_template_group="site"', 'my_template_group="' . $group_name . '"', $temp);
                     $temp = str_replace("my_template_group='site'", 'my_template_group="' . $group_name . '"', $temp);
                     $temp = str_replace("{stylesheet=weblog/weblog_css}", "{stylesheet=" . $group_name . "/site_css}", $temp);
                     $temp = str_replace("{stylesheet=site/site_css}", "{stylesheet=" . $group_name . "/site_css}", $temp);
                     $temp = str_replace('assign_variable:master_weblog_name="weblog1"', 'assign_variable:master_weblog_name="' . $_POST['blog_name'] . '"', $temp);
                     $temp = preg_replace("#weblog/(.+?)#", $group_name . "/\\1", $temp);
                     $temp = addslashes($temp);
                     $sql = str_replace('{template}', $temp, $val['1']);
                     $DB->query($sql);
                 }
             }
         }
     }
     $message = $DSP->qdiv('itemWrapper', $DSP->qspan('success', $success_msg) . NBS . NBS . '<b>' . $_POST['blog_title'] . '</b>');
     if ($edit == FALSE or $return === TRUE) {
         return $this->weblog_overview($message);
     } else {
         return $this->edit_blog_form($message, $weblog_id);
     }
 }
Esempio n. 2
0
    /**
     * Channel preference submission handler
     *
     * This function receives the submitted channel preferences
     * and stores them in the database.
     *
     * @access	public
     * @return	void
     */
    function channel_update()
    {
        if (!$this->cp->allowed_group('can_access_admin') or !$this->cp->allowed_group('can_access_content_prefs')) {
            show_error($this->lang->line('unauthorized_access'));
        }
        $this->lang->loadfile('admin_content');
        unset($_POST['channel_prefs_submit']);
        // submit button
        // If the $channel_id variable is present we are editing an
        // existing channel, otherwise we are creating a new one
        $edit = isset($_POST['channel_id']) ? TRUE : FALSE;
        // Load the layout Library & update the layouts
        $this->load->library('layout');
        $add_rss = isset($_POST['add_rss']) ? TRUE : FALSE;
        unset($_POST['add_rss']);
        $return = $this->input->get_post('return') ? TRUE : FALSE;
        unset($_POST['return']);
        $edit_group_prefs = TRUE;
        if ($this->input->get_post('edit_group_prefs') !== 'y') {
            unset($_POST['cat_group']);
            unset($_POST['status_group']);
            unset($_POST['field_group']);
            $edit_group_prefs = FALSE;
        }
        unset($_POST['edit_group_prefs']);
        $dupe_id = $this->input->get_post('duplicate_channel_prefs');
        unset($_POST['duplicate_channel_prefs']);
        // Check for required fields
        $error = array();
        if (isset($_POST['comment_expiration']) && $_POST['comment_expiration'] == '') {
            $_POST['comment_expiration'] = 0;
        }
        // Template Error Trapping
        if ($edit == FALSE) {
            $this->load->library('security');
            $create_templates = $this->input->get_post('create_templates');
            $old_group_id = $this->input->get_post('old_group_id');
            $group_name = $this->input->post('group_name');
            $template_theme = $this->security->sanitize_filename($this->input->get_post('template_theme'));
            unset($_POST['create_templates']);
            unset($_POST['old_group_id']);
            unset($_POST['group_name']);
            unset($_POST['template_theme']);
            if ($create_templates != 'no') {
                $this->lang->loadfile('design');
                if (!$this->cp->allowed_group('can_admin_templates')) {
                    show_error($this->lang->line('unauthorized_access'));
                }
                if (!$group_name) {
                    show_error($this->lang->line('group_required'));
                }
                if (!preg_match("#^[a-zA-Z0-9_\\-/]+\$#i", $group_name)) {
                    show_error($this->lang->line('illegal_characters'));
                }
                $reserved[] = 'act';
                if ($this->config->item("forum_is_installed") == 'y' and $this->config->item("forum_trigger") != '') {
                    $reserved[] = $this->config->item("forum_trigger");
                }
                if (in_array($group_name, $reserved)) {
                    show_error($this->lang->line('reserved_name'));
                }
                $this->db->where('site_id', $this->config->item('site_id'));
                $this->db->where('group_name', $group_name);
                $count = $this->db->count_all_results('template_groups');
                if ($count > 0) {
                    show_error($this->lang->line('template_group_taken'));
                }
            }
        }
        if ($this->input->post('apply_comment_enabled_to_existing')) {
            if ($this->input->post('comment_system_enabled') == 'y') {
                $this->channel_model->update_comments_allowed($_POST['channel_id'], 'y');
            } elseif ($this->input->post('comment_system_enabled') == 'n') {
                $this->channel_model->update_comments_allowed($_POST['channel_id'], 'n');
            }
        }
        unset($_POST['apply_comment_enabled_to_existing']);
        if (isset($_POST['apply_expiration_to_existing'])) {
            if ($this->input->post('comment_expiration') == 0) {
                $this->channel_model->update_comment_expiration($_POST['channel_id'], $_POST['comment_expiration'], TRUE);
            } else {
                $this->channel_model->update_comment_expiration($_POST['channel_id'], $_POST['comment_expiration'] * 86400);
            }
        }
        unset($_POST['apply_expiration_to_existing']);
        if (isset($_POST['cat_group']) && is_array($_POST['cat_group'])) {
            foreach ($_POST['cat_group'] as $key => $value) {
                unset($_POST['cat_group_' . $key]);
            }
            $_POST['cat_group'] = implode('|', $_POST['cat_group']);
        }
        // Create Channel
        // Construct the query based on whether we are updating or inserting
        if ($edit == FALSE) {
            unset($_POST['channel_id']);
            unset($_POST['clear_versioning_data']);
            $_POST['channel_url'] = $this->functions->fetch_site_index();
            $_POST['channel_lang'] = $this->config->item('xml_lang');
            // Assign field group if there is only one
            if (!isset($_POST['field_group']) or isset($_POST['field_group']) && !is_numeric($_POST['field_group'])) {
                $this->db->select('group_id');
                $this->db->where('site_id', $this->config->item('site_id'));
                $query = $this->db->get('field_groups');
                if ($query->num_rows() == 1) {
                    $_POST['field_group'] = $query->row('group_id');
                }
            }
            // Insert data
            $_POST['site_id'] = $this->config->item('site_id');
            // duplicating preferences?
            if ($dupe_id !== FALSE and is_numeric($dupe_id)) {
                $this->db->where('channel_id', $dupe_id);
                $wquery = $this->db->get('channels');
                if ($wquery->num_rows() == 1) {
                    $exceptions = array('channel_id', 'site_id', 'channel_name', 'channel_title', 'total_entries', 'total_comments', 'last_entry_date', 'last_comment_date');
                    foreach ($wquery->row_array() as $key => $val) {
                        // don't duplicate fields that are unique to each channel
                        if (!in_array($key, $exceptions)) {
                            switch ($key) {
                                // category, field, and status fields should only be duped
                                // if both channels are assigned to the same group of each
                                case 'cat_group':
                                    // allow to implicitly set category group to "None"
                                    if (!isset($_POST[$key])) {
                                        $_POST[$key] = $val;
                                    }
                                    break;
                                case 'status_group':
                                case 'field_group':
                                    if (!isset($_POST[$key])) {
                                        $_POST[$key] = $val;
                                    } elseif ($_POST[$key] == '') {
                                        $_POST[$key] = NULL;
                                    }
                                    break;
                                case 'deft_status':
                                case 'deft_status':
                                    if (!isset($_POST['status_group']) or $_POST['status_group'] == $wquery->row('status_group')) {
                                        $_POST[$key] = $val;
                                    }
                                    break;
                                case 'search_excerpt':
                                    if (!isset($_POST['field_group']) or $_POST['field_group'] == $wquery->row('field_group')) {
                                        $_POST[$key] = $val;
                                    }
                                    break;
                                case 'deft_category':
                                    if (!isset($_POST['cat_group']) or count(array_diff(explode('|', $_POST['cat_group']), explode('|', $wquery->row('cat_group')))) == 0) {
                                        $_POST[$key] = $val;
                                    }
                                    break;
                                case 'blog_url':
                                case 'comment_url':
                                case 'search_results_url':
                                case 'ping_return_url':
                                case 'rss_url':
                                    if ($create_templates != 'no') {
                                        if (!isset($old_group_name)) {
                                            $this->db->select('group_name');
                                            $this->db->where('group_id', $old_group_id);
                                            $gquery = $this->db->get('template_groups');
                                            $old_group_name = $gquery->row('group_name');
                                        }
                                        $_POST[$key] = str_replace("/{$old_group_name}/", "/{$group_name}/", $val);
                                    } else {
                                        $_POST[$key] = $val;
                                    }
                                    break;
                                default:
                                    $_POST[$key] = $val;
                                    break;
                            }
                        }
                    }
                }
            }
            $_POST['default_entry_title'] = !isset($_POST['default_entry_title']) ? '' : $_POST['default_entry_title'];
            $_POST['url_title_prefix'] = !isset($_POST['url_title_prefix']) ? '' : $_POST['url_title_prefix'];
            $this->db->insert('channels', $_POST);
            $insert_id = $this->db->insert_id();
            $channel_id = $insert_id;
            if ($dupe_id !== FALSE and is_numeric($dupe_id) && $edit_group_prefs == FALSE) {
                // Duplicate layouts
                $this->layout->duplicate_layout($dupe_id, $channel_id);
            }
            $success_msg = $this->lang->line('channel_created');
            $this->logger->log_action($success_msg . NBS . NBS . $_POST['channel_title']);
        } else {
            if (isset($_POST['clear_versioning_data'])) {
                $this->db->delete('entry_versioning', array('channel_id' => $_POST['channel_id']));
                unset($_POST['clear_versioning_data']);
            }
            // Only one possible is revisions- enabled or disabled.
            // We treat as installed/not and delete the whole tab.
            $this->layout->sync_layout($_POST, $_POST['channel_id']);
            $sql = $this->db->update_string('exp_channels', $_POST, 'channel_id=' . $this->db->escape_str($_POST['channel_id']));
            $this->db->query($sql);
            $channel_id = $this->db->escape_str($_POST['channel_id']);
            $success_msg = $this->lang->line('channel_updated');
        }
        /** -----------------------------------------
        		/**  Create Templates
        		/** -----------------------------------------*/
        if ($edit == FALSE) {
            if ($create_templates != 'no') {
                $query = $this->db->query("SELECT COUNT(*) AS count FROM exp_template_groups");
                $group_order = $query->row('count') + 1;
                $this->db->insert('template_groups', array('group_name' => $group_name, 'group_order' => $group_order, 'is_site_default' => 'n', 'site_id' => $this->config->item('site_id')));
                $group_id = $this->db->insert_id();
                if ($create_templates == 'duplicate') {
                    $this->db->select('group_name');
                    $this->db->where('group_id', $old_group_id);
                    $query = $this->db->get('template_groups');
                    $old_group_name = $query->row('group_name');
                    $this->db->select('template_name, template_data, template_type, 
										template_notes, cache, refresh, no_auth_bounce, 
										allow_php, php_parse_location');
                    $this->db->where('group_id', $old_group_id);
                    $query = $this->db->get('templates');
                    if ($query->num_rows() == 0) {
                        $this->db->insert('templates', array('group_id' => $group_id, 'template_name' => 'index', 'edit_date' => $this->localize->now, 'site_id' => $this->config->item('site_id')));
                    } else {
                        $old_channel_name = '';
                        foreach ($query->result_array() as $row) {
                            if ($old_channel_name == '') {
                                if (preg_match_all("/channel=[\"'](.+?)[\"']/", $row['template_data'], $matches)) {
                                    for ($i = 0; $i < count($matches['1']); $i++) {
                                        if (substr($matches['1'][$i], 0, 1) != '{') {
                                            $old_channel_name = $matches['1'][$i];
                                            break;
                                        }
                                    }
                                }
                            }
                            $temp = str_replace('channel="' . $old_channel_name . '"', 'channel="' . $_POST['channel_name'] . '"', $row['template_data']);
                            $temp = str_replace("channel='" . $old_channel_name . "'", 'channel="' . $_POST['channel_name'] . '"', $temp);
                            $temp = preg_replace("/{stylesheet=.+?\\/(.+?)}/", "{stylesheet=" . $group_name . "/\\1}", $temp);
                            $temp = preg_replace("#preload_replace:master_channel_name=\".+?\"#", 'preload_replace:master_channel_name="' . $_POST['channel_name'] . '"', $temp);
                            $temp = preg_replace("#preload_replace:master_channel_name=\\'.+?\\'#", "preload_replace:master_channel_name='" . $_POST['channel_name'] . "'", $temp);
                            $temp = preg_replace('#preload_replace:my_template_group=(\\042|\\047)([^\\1]*?)\\1#', "preload_replace:my_template_group=\\1{$group_name}\\1", $temp);
                            $temp = preg_replace("#" . $old_group_name . "/(.+?)#", $group_name . "/\\1", $temp);
                            $data = array('group_id' => $group_id, 'template_name' => $row['template_name'], 'template_notes' => $row['template_notes'], 'cache' => $row['cache'], 'refresh' => $row['refresh'], 'no_auth_bounce' => $row['no_auth_bounce'], 'php_parse_location' => $row['php_parse_location'], 'allow_php' => $this->session->userdata['group_id'] == 1 ? $row['allow_php'] : 'n', 'template_type' => $row['template_type'], 'template_data' => $temp, 'edit_date' => $this->localize->now, 'last_author_id' => 0, 'site_id' => $this->config->item('site_id'));
                            $this->db->insert('templates', $data);
                        }
                    }
                } else {
                    $type = 'core';
                    if ($fp = @opendir(PATH_MOD)) {
                        while (FALSE !== ($file = readdir($fp))) {
                            if (strpos($file, '.') === FALSE) {
                                if ($file == 'mailinglist') {
                                    $type = 'full';
                                    break;
                                }
                            }
                        }
                        closedir($fp);
                    }
                    require PATH_THEMES . 'site_themes/' . $template_theme . '/' . $template_theme . '.php';
                    foreach ($template_matrix as $tmpl) {
                        $Q[] = array($tmpl['0'](), "INSERT INTO exp_templates(group_id, template_name, template_type, template_data, edit_date, site_id)\n\t\t\t\t\t\t\t\t\t\t\t\t\tVALUES ('{$group_id}', '" . $this->db->escape_str($tmpl['0']) . "', '" . $this->db->escape_str($tmpl['1']) . "', '{template}', '" . $this->localize->now . "', '" . $this->db->escape_str($this->config->item('site_id')) . "')");
                    }
                    if ($add_rss == TRUE) {
                        require PATH_THEMES . 'site_themes/rss/rss.php';
                        $Q[] = array(rss_2(), "INSERT INTO exp_templates(group_id, template_name, template_type, template_data, edit_date, site_id)\n\t\t\t\t\t\t\t\t\t\t\t\tVALUES ('{$group_id}', 'rss_2.0', 'feed', '{template}', '" . $this->db->escape_str($this->localize->now) . "', '" . $this->db->escape_str($this->config->item('site_id')) . "')");
                        $Q[] = array(atom(), "INSERT INTO exp_templates(group_id, template_name, template_type, template_data, edit_date, site_id)\n\t\t\t\t\t\t\t\t\t\t\t  VALUES ('{$group_id}', 'atom', 'feed', '{template}', '" . $this->db->escape_str($this->localize->now) . "', '" . $this->db->escape_str($this->config->item('site_id')) . "')");
                    }
                    foreach ($Q as $val) {
                        $temp = $val['0'];
                        $temp = str_replace('channel="channel1"', 'channel="' . $_POST['channel_name'] . '"', $temp);
                        $temp = str_replace("channel='channel1'", 'channel="' . $_POST['channel_name'] . '"', $temp);
                        $temp = str_replace('my_channel="channel1"', 'my_channel="' . $_POST['channel_name'] . '"', $temp);
                        $temp = str_replace("my_channel='channel1'", 'my_channel="' . $_POST['channel_name'] . '"', $temp);
                        $temp = str_replace('channel="default_site"', 'channel="' . $_POST['channel_name'] . '"', $temp);
                        $temp = str_replace("channel='default_site'", 'channel="' . $_POST['channel_name'] . '"', $temp);
                        $temp = str_replace('my_channel="default_site"', 'my_channel="' . $_POST['channel_name'] . '"', $temp);
                        $temp = str_replace("my_channel='default_site'", 'my_channel="' . $_POST['channel_name'] . '"', $temp);
                        $temp = str_replace('my_template_group="site"', 'my_template_group="' . $group_name . '"', $temp);
                        $temp = str_replace("my_template_group='site'", 'my_template_group="' . $group_name . '"', $temp);
                        $temp = str_replace("{stylesheet=channel/channel_css}", "{stylesheet=" . $group_name . "/site_css}", $temp);
                        $temp = str_replace("{stylesheet=site/site_css}", "{stylesheet=" . $group_name . "/site_css}", $temp);
                        $temp = str_replace('preload_replace:master_channel_name="channel1"', 'preload_replace:master_channel_name="' . $_POST['channel_name'] . '"', $temp);
                        $temp = preg_replace("#channel/(.+?)#", $group_name . "/\\1", $temp);
                        $temp = addslashes($temp);
                        $sql = str_replace('{template}', $temp, $val['1']);
                        $this->db->query($sql);
                    }
                }
            }
        }
        $cp_message = $success_msg . NBS . NBS . $_POST['channel_title'];
        $this->session->set_flashdata('message_success', $cp_message);
        if ($edit == FALSE or $return === TRUE) {
            $this->functions->redirect(BASE . AMP . 'C=admin_content' . AMP . 'M=channel_management');
        } else {
            $this->functions->redirect(BASE . AMP . 'C=admin_content' . AMP . 'M=channel_edit&channel_id=' . $channel_id);
        }
    }