Commits

Ahmed Youssef committed 05cd011

db module

Comments (0)

Files changed (16)

application/config/routes.php

 
 $route['default_controller'] = "main";
 $route['404_override'] = '';
+// Admin:modules
+$route['admin/module/(:any)/(:any)/(:any)']		= "$1/admin/$2/$3";
+$route['admin/module/(:any)/(:any)']		= "$1/admin/$2";
+$route['admin/module/(:any)']				= "$1/admin/index";
+
+
+
 
 /* End of file routes.php */
 /* Location: ./application/config/routes.php */

application/core/MY_Controller.php

         $this->parser->parse('main', $d);
     }
 
+}
 
+class Admin_Controller extends MY_Controller{
+    function __construct() {
+        parent::__construct();
+		if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
+		{
+			redirect('auth', 'refresh');
+		}
+        
+    }
+    function loadpage($page, $data=NULL) {
+        if ($data == NULL) {
+            $data = $this->_getBasicData();
+        }
 
-
-
+        $d['header'] = $this->parser->parse('header', $data, true);
+        $d['pagename'] = $page;
+        $d['controllername'] = 'admin';
+        $d['sidebar']=$this->parser->parse('admin/adminnav',$data, true);
+        $d['showsidebar'] = true;
+        $d['content'] = $this->parser->parse($page, $data, true);
+        $d['footer'] = $this->parser->parse('footer', $data, true);
+        $this->parser->parse('main', $d);
+    }
+    function current_user_is_admin(){
+        return current_user_can("a");
+    }
+    
 }

application/libraries/new file

Empty file added.

application/modules/admin/controllers/admin.php

+<?php
+/* 
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+class Admin extends MY_Controller{
+    function __construct() {
+        parent::__construct();
+		if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
+		{
+			redirect('auth', 'refresh');
+		}
+        
+    }
+
+    function loadpage($page, $data=NULL) {
+        if ($data == NULL) {
+            $data = $this->_getBasicData();
+        }
+
+        $d['header'] = $this->parser->parse('header', $data, true);
+        $d['pagename'] = $page;
+        $d['controllername'] = 'admin';
+        $d['sidebar']=$this->parser->parse('admin/adminnav',$data, true);
+        $d['showsidebar'] = true;
+        $d['content'] = $this->parser->parse($page, $data, true);
+        $d['footer'] = $this->parser->parse('footer', $data, true);
+        $this->parser->parse('main', $d);
+    }
+
+    function index(){
+        redirect("admin/settings");
+    }
+
+    function settings(){
+        $data=$this->_getBasicData();
+        $data['metas']=$this->Cikimeta_model->getAll();
+        $this->loadpage("admin/settings", $data);
+
+    }
+    function edit_settings_done(){
+        $metas=$this->Cikimeta_model->getAll();
+        $udata=array();
+
+        foreach($metas as $m){
+            $key_val=$this->input->post($m->key);
+            if ($key_val!=$m->value){
+                $this->Cikimeta_model->updateMetaByKey($m->key, array('value'=>$key_val));
+            }
+        }
+
+    }
+}
+?>

application/modules/auth/controllers/admin.php

+<?php
+/* 
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+class Admin extends Admin_Controller{
+   
+    function index(){
+        redirect('admin/module/auth/users');
+        
+    }
+    function users(){
+        $data=$this->_getBasicData();
+        $ulist=$this->ion_auth->get_users();
+        $data['users']=$ulist;
+        $this->loadpage("authv/users", $data);
+    }
+    function new_user(){
+		$data['title'] = "Create User";
+
+		//validate form input
+		$this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
+		$this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
+		$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
+		/*
+        $this->form_validation->set_rules('phone1', 'First Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
+		$this->form_validation->set_rules('phone2', 'Second Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
+		$this->form_validation->set_rules('phone3', 'Third Part of Phone', 'required|xss_clean|min_length[4]|max_length[4]');
+		$this->form_validation->set_rules('company', 'Company Name', 'required|xss_clean');
+		*/
+        $this->form_validation->set_rules('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
+		$this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
+
+		if ($this->form_validation->run() == true)
+		{
+			$username = strtolower($this->input->post('first_name')) . ' ' . strtolower($this->input->post('last_name'));
+			$email = $this->input->post('email');
+			$password = $this->input->post('password');
+
+			$additional_data = array('first_name' => $this->input->post('first_name'),
+				'last_name' => $this->input->post('last_name'),
+				'company' => $this->input->post('company'),
+				'phone' => $this->input->post('phone1') . '-' . $this->input->post('phone2') . '-' . $this->input->post('phone3'),
+			);
+		}
+		if ($this->form_validation->run() == true && $this->ion_auth->register($username, $password, $email, $additional_data))
+		{ //check to see if we are creating the user
+			//redirect them back to the admin page
+			$this->session->set_flashdata('message', "User Created");
+			redirect("auth/admin/users", 'refresh');
+		}
+		else
+		{ //display the create user form
+			//set the flash data error message if there is one
+			$data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
+
+			$data['first_name'] = array('name' => 'first_name',
+				'id' => 'first_name',
+				'type' => 'text',
+				'value' => $this->form_validation->set_value('first_name'),
+			);
+			$data['last_name'] = array('name' => 'last_name',
+				'id' => 'last_name',
+				'type' => 'text',
+				'value' => $this->form_validation->set_value('last_name'),
+			);
+			$data['email'] = array('name' => 'email',
+				'id' => 'email',
+				'type' => 'text',
+				'value' => $this->form_validation->set_value('email'),
+			);
+			$data['company'] = array('name' => 'company',
+				'id' => 'company',
+				'type' => 'text',
+				'value' => $this->form_validation->set_value('company'),
+			);
+			$data['phone1'] = array('name' => 'phone1',
+				'id' => 'phone1',
+				'type' => 'text',
+				'value' => $this->form_validation->set_value('phone1'),
+			);
+			$data['phone2'] = array('name' => 'phone2',
+				'id' => 'phone2',
+				'type' => 'text',
+				'value' => $this->form_validation->set_value('phone2'),
+			);
+			$data['phone3'] = array('name' => 'phone3',
+				'id' => 'phone3',
+				'type' => 'text',
+				'value' => $this->form_validation->set_value('phone3'),
+			);
+			$data['password'] = array('name' => 'password',
+				'id' => 'password',
+				'type' => 'password',
+				'value' => $this->form_validation->set_value('password'),
+			);
+			$data['password_confirm'] = array('name' => 'password_confirm',
+				'id' => 'password_confirm',
+				'type' => 'password',
+				'value' => $this->form_validation->set_value('password_confirm'),
+			);
+			$this->loadpage("authv/create_user_adm", $data);
+		}
+    }
+
+    
+}

application/modules/auth/views/authv/create_user_adm.php

+<div class='mainInfo'>
+
+	<h1>Create User</h1>
+	<p>Please enter the users information below.</p>
+	
+	<div id="infoMessage"><?php echo $message;?></div>
+	
+    <?php echo form_open("auth/admin/new_user");?>
+      <p>First Name:<br />
+      <?php echo form_input($first_name);?>
+      </p>
+      
+      <p>Last Name:<br />
+      <?php echo form_input($last_name);?>
+      </p>
+      
+      <p>Company Name:<br />
+      <?php echo form_input($company);?>
+      </p>
+      
+      <p>Email:<br />
+      <?php echo form_input($email);?>
+      </p>
+      
+      <p>Phone:<br />
+      <?php echo form_input($phone1);?>-<?php echo form_input($phone2);?>-<?php echo form_input($phone3);?>
+      </p>
+      
+      <p>Password:<br />
+      <?php echo form_input($password);?>
+      </p>
+      
+      <p>Confirm Password:<br />
+      <?php echo form_input($password_confirm);?>
+      </p>
+      
+      
+      <p><?php echo form_submit('submit', 'Create User');?></p>
+
+      
+    <?php echo form_close();?>
+
+</div>

application/modules/auth/views/authv/index.php

 			</tr>
 		<?php endforeach;?>
 	</table>
-	
+	<!--
 	<p><a href="<?php echo site_url('auth/create_user');?>">Create a new user</a></p>
 	
 	<p><a href="<?php echo site_url('auth/logout'); ?>">Logout</a></p>
-	
+	-->
 </div>

application/modules/auth/views/authv/users.php

+<div id="userslist">
+        <h1>Users</h1>
+        <p>Below is a list of the users.</p>
+        <?php if(isset($message)): ?>
+            <div id="infoMessage"><?php echo $message;?></div>
+        <?php endif;?>
+        <table cellpadding=0 cellspacing=10>
+            <thead>
+                <tr>
+                    <th>First Name</th>
+                    <th>Last Name</th>
+                    <th>Email</th>
+                    <th>Group</th>
+                    <th>Actions</th>
+                </tr>
+            </thead>
+            <tbody>
+            <?php foreach ($users as $user):?>
+                <tr>
+                    <td><?php echo $user->first_name;?></td>
+                    <td><?php echo $user->last_name;?></td>
+                    <td><?php echo $user->email;?></td>
+                    <td><?php echo $user->group_description;?></td>
+                    <td><?php echo ($user->active) ? anchor("auth/deactivate/".$user->id, 'Active') : anchor("auth/activate/". $user->id, 'Inactive');?></td>
+                </tr>
+            <?php endforeach;?>
+            </tbody>
+        </table>
+</div>
+

application/modules/db/controllers/admin.php

+<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+
+
+class Admin extends Admin_Controller {
+    function __construct(){
+        parent::__construct();
+        $this->load->dbutil();
+        
+    }
+    function index(){
+        redirect('admin/module/db/dbops');
+        
+    }
+    
+    function dbops() {
+        $data=$this->_getBasicData();
+        $data['tables']=$this->db->list_tables();
+        $this->loadpage('admin/dbops', $data);
+    }
+
+    function db_op_done() {
+        $optimizetables=$this->input->post("optimizetables");
+        $repairtables=$this->input->post("repairtables");
+        $optimizedb=$this->input->post("optimizedb");
+
+        $reportmsgs=array();
+        if ($optimizedb) {
+            $result = $this->dbutil->optimize_database();
+
+            if ($result !== FALSE) {
+                $report[]="Database optimized.";
+            }
+        }
+        if (count($optimizetables)>0) {
+            $report[]="Optimize: ";
+            foreach($optimizetables as $table) {
+                if ($this->dbutil->optimize_table($table)) {
+                    $report[]="+".$table." optimized.";
+                }
+            }
+        }
+        if (count($repairtables)>0) {
+            $report[]="Repair: ";
+            foreach($repairtables as $table) {
+                if ($this->dbutil->repair_table($table)) {
+                    $report[]="+". $table." repaired.";
+                }
+            }
+
+        }
+
+        $reportstr=implode("<br />", $report);
+        echo $reportstr;
+    }
+    function phpstatus() {
+        $this->loadpage('admin/phpstatus', $this->_getBasicData());
+    }
+    function backup() {
+        $data=$this->_getBasicData();
+        $data['tables']=$this->db->list_tables();
+        $this->loadpage('admin/backup', $data);
+    }
+
+    function get_backup() {
+
+        $tablestobackup=$this->input->post('backuptables');
+
+        //backup.
+        $fname= 'database-' . date('Y-m-d') . '.sql.zip';
+        $prefs = array(
+                'tables'      => $tablestobackup,
+                'filename'    => $fname,
+                'format'      => 'zip',
+                'add_drop'    => TRUE,
+                'add_insert'  => TRUE,
+                'newline'     => "\n"
+        );
+        $backup =& $this->dbutil->backup($prefs);
+        write_file($fname, $backup);
+        force_download($fname, $backup);
+
+    }
+
+
+    function sqlman() {
+
+        $data=$this->_getBasicData();
+        $data['tables']=$this->db->list_tables();
+        $this->loadpage('admin/sqlman', $data);
+    }
+    function do_sql() {
+        $config=array(
+                array(
+                        'field'=>'sql',
+                        'label'=> 'SQL',
+                        'rules' => 'trim|required'
+                ),
+
+        );
+
+        $this->form_validation->set_rules($config);
+        $data=$this->_getBasicData();
+        if ($this->form_validation->run()==FALSE) {
+            $this->loadpage("admin/sqlman", $data);
+        }else{
+           ###NOTE: THIS PART MOSTLY TAKEN FROM FUDFORUM. http://fudforum.org
+           $sql=$this->input->post("sql");
+           $sqlfile = str_replace("\r","", $sql);
+           $sqlfile = explode(";\n", $sqlfile);
+
+           $qs=array();
+           foreach($sqlfile as $q){
+               if ($this->_securesql($q)){
+                   $q=$this->db->query($q);
+                   $qs[]=$q;
+               }
+           }
+
+           $data['qs']=$qs;
+           $data['tables']=$this->db->list_tables();
+           $this->loadpage('admin/sqlman', $data);
+           
+        }
+
+    }
+    function _securesql($sql) {
+        if (preg_match('/[a-zA-Z]/', $sql) and !preg_match('/^(#|--)/', $sql)) {
+            if (preg_match('/^\s*use\s+\w+\s*;?$/i', $sql)) {
+                return FALSE;
+            }
+        }
+        return TRUE;
+    }
+
+    
+    
+}

application/modules/db/views/admin/backup.php

+<?php
+
+$tablenames=array();
+foreach($tables as $t) {
+    $tablenames[$t]=$t;
+}
+
+?>
+<div class="box">
+    <div class="inbox">
+        <?=form_open('admin/get_backup')?>
+        <?=form_fieldset('Backup preferences')?>
+        <div id="option"><?=form_label("Backup tables", "backuptables[]") ?><?= form_dropdown('backuptables[]', $tablenames,$tablenames) ?></div>
+        <div id="option"><?=form_submit('btnsubmit', "Backup")?></div>
+
+        <?=form_fieldset_close()?>
+        <?= form_close()?>
+    </div>
+</div>

application/modules/db/views/admin/dbops.php

+<?php
+
+$tablenames=array();
+foreach($tables as $t) {
+    $tablenames[$t]=$t;
+}
+
+?>
+<div class="box">
+    <div class="postform" id="dbopsrepairopt">
+    <?= form_open('admin/module/db/db_op_done') ?>
+    <?=form_fieldset('DB Operations')?>
+    <div id="option"><?=form_label("Optimize", "optimizetables[]")?><?= form_dropdown('optimizetables[]', $tablenames,$tablenames) ?></div>
+    <div id="option"><?=form_label("Repair", "repairtables[]")?><?= form_dropdown('repairtables[]', $tablenames,$tablenames) ?></div>
+    <div id="option"><?=form_label("Optimize DB", "optimizedb")?><?= form_checkbox("optimizedb", "0", FALSE) ?></div>
+    <div id="option"><?=form_submit('btnsubmit', "Start")?></div>
+
+    <?=form_fieldset_close()?>
+    <?=form_close()?>
+</div>
+</div>

application/modules/db/views/admin/phpstatus.php

+
+<?php
+    phpinfo();
+?>

application/modules/db/views/admin/sqlman.php

+<div class="box">
+<div id="sqlmanform" class="postform">
+
+    <h1>SQL Man</h1>
+<div id="alert">
+    <p>NOTE: this feature IS DANGEROUS and you could destroy all your data. Please only use it to run SELECT and DESCRIBE statements. Anything else may result in data loss.</p>
+</div>
+
+<?php sort($tables);?>
+<?=form_open("admin/module/db/do_sql", array('name'=>'admsql'))?>
+<div id="option">Enter SQL statements (terminate them with semicolons): <select onchange="if(this.selectedIndex!=0) document.admsql.sql.value+=this.options[this.selectedIndex].value;">
+        <?php foreach($tables as $tbl):?>
+            <?='<option value="'.$tbl.'">'.$tbl.'</option>';?>
+        <?php endforeach;?>
+    </select></div>
+
+<div id="option"><?=form_textarea('sql') ?></div>
+<div id="option"><?=form_submit('btnsubmit', 'Execute!')?></div>
+<?=form_close()?>
+
+<?php if (isset($qs)):?>
+    <?php foreach($qs as $q):?>
+
+        <?php if ($q->num_rows() > 0):?>
+
+<h1>SQL Results: </h1>
+<table class="blocktable">
+                <?php
+                $rows=$q->result_array();
+                foreach(array_keys($rows[0]) as $k) {
+                    echo "<th>$k</th>";
+                }
+
+                foreach($rows as $row) {
+                    echo "<tr>";
+                    foreach($row as $k=>$v) {
+                        echo "<td>$v</td>";
+                    }
+                    echo "</tr>";
+                }
+
+                ?>
+</table>
+<h2>Num of rows returned: <?= $q->num_rows() ?></h2>
+        <?php endif;?>
+    <?php endforeach;?>
+<?php endif;?>
+
+</div>
+</div>

application/modules/main/controllers/feed.php

 <?php
 
-require APPPATH."3rdparty/Zend/Feed.php";
-
 class Feed extends MY_Controller {
 
 
     }
 
     function index() {
+        //FIX ME.
+        header("Content-Type: application/rss+xml");
+        
         $data['encoding'] = 'utf-8';
         $data['feed_name'] =  $this->Cikimeta_model->getMetaValue("wiki", "wiki_title");
         $data['feed_url'] = base_url();
         $data['creator_email'] = 'Ahmed Youssef is at xmonader[at]gmail[dot]com';
         $data['posts'] = $this->Pages_model->getLatestPages(10);
 
-        //FIX ME.
-        echo header("Content-Type: application/rss+xml".'?xml version="1.0" encoding="utf-8"?' . '>');
-        
         $this->load->view('rss', $data);
     }
 }

application/modules/main/views/rss.php

+<?php echo '<?xml version="1.0" encoding="utf-8"?' . '?>' . "\n"; ?>
 <rss version="2.0"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
           <link><?php echo site_url('pages/'. $entry->page_id) ?></link>
           <guid><?php echo site_url('topic/view_topic/'. $entry->page_id) ?></guid>
           <description><![CDATA[
-      <?= str_replace('/img/post_resources/', base_url() . 'img/post_resources/', word_limiter($entry->pagecontent, 25)) ?>
+      <?php echo str_replace('/img/post_resources/', base_url() . 'img/post_resources/', word_limiter($entry->pagecontent, 25)) ; ?>
       ]]></description>
       <pubDate><?php echo date ('r', mysql_to_unix($entry->createdat));?></pubDate>
         </item>

application/views/admin/adminnav.php

 <div id="adminnav">
     <ul id="admintasks">
         <li id="admintask"><?=anchor("admin/settings", "Manage Settings")?></li>
-        <li id="admintask"><?=anchor("admin/users", "Manage Users") ?></li>
+        <li id="admintask"><?=anchor("admin/module/auth", "Manage Users") ?></li>
     </ul>
 </div>