예제 #1
0
 public function create()
 {
     $this->template->header->this_page = 'mhi';
     $this->template->content = new View('mhi_create');
     // Process Form
     if ($_POST) {
         $post = Validation::factory($_POST);
         //Trim whitespaces
         $post->pre_filter('trim');
         $post->add_rules('signup_first_name', 'required', 'alpha_dash');
         $post->add_rules('signup_last_name', 'required', 'alpha_dash');
         $post->add_rules('signup_email', 'required', 'email');
         $post->add_rules('signup_password', 'required');
         $post->add_rules('signup_subdomain', 'required', 'alpha_dash');
         $post->add_rules('signup_instance_name', 'required');
         $post->add_rules('signup_instance_tagline', 'required');
         if ($post->validate()) {
             $mhi_user = new Mhi_User_Model();
             $db_genesis = new db_genesis();
             $mhi_site_database = new Mhi_Site_Database_Model();
             $mhi_site = new Mhi_Site_Model();
             // Create new user
             $user_id = $mhi_user->save_user(array('firstname' => $post->signup_first_name, 'lastname' => $post->signup_last_name, 'email' => $post->signup_email, 'password' => $post->signup_password));
             // Set up DB and Site
             $base_db = $db_genesis->current_db();
             $new_db_name = $base_db . '_' . $post->signup_subdomain;
             // Do some not so graceful validation
             if ($mhi_site_database->db_assigned($new_db_name) || $db_genesis->db_exists($new_db_name)) {
                 throw new Kohana_User_Exception('MHI Site Setup Error', "Database already exists and/or is already assigned in the MHI DB.");
             }
             if ($mhi_site->domain_exists($post->signup_subdomain)) {
                 throw new Kohana_User_Exception('MHI Site Setup Error', "Domain already assigned in MHI DB.");
             }
             // Create site
             $site_id = $mhi_site->save_site(array('user_id' => $user_id, 'site_domain' => $post->signup_subdomain, 'site_privacy' => 1, 'site_active' => 1));
             // Set up database and save details to MHI DB
             $db_genesis->create_db($new_db_name);
             $mhi_site_database->assign_db($new_db_name, $site_id);
             $db_genesis->populate_db($new_db_name, array('username' => $post->signup_email, 'name' => $post->signup_first_name . ' ' . $post->signup_last_name, 'password' => $post->signup_password, 'email' => $post->signup_email), array('site_name' => $post->signup_instance_name, 'site_tagline' => $post->signup_instance_tagline));
         } else {
             throw new Kohana_User_Exception('Validation Error', "Form not validating. Dev tip: Come back later and clean up validation!");
         }
     } else {
         // If the form was never posted, we need to complain about it.
         throw new Kohana_User_Exception('Incomplete Form', "Form not posted.");
     }
 }
 static function update_db($db)
 {
     // Check if the db is even assigned to anyone. This is a requirement.
     if (Mhi_Site_Database_Model::db_assigned($db) == false) {
         return false;
     }
     $mhi_db = Kohana::config('database.default');
     $table_prefix = $mhi_db['table_prefix'];
     $mhi_db_name = $mhi_db['connection']['database'];
     $settings = kohana::config('settings');
     $current_version = $settings['db_version'];
     // Switch to new DB for a moment
     mysql_query('USE ' . $db . ';');
     // START: Everything that happens in the deployment DB happens below
     $settings = ORM::factory('settings', 1);
     $db_version = $settings->db_version;
     $upgrade_to = $db_version + 1;
     // Check if we even need to apply this update
     if ($db_version >= $current_version) {
         mysql_query('USE ' . $mhi_db_name);
         return false;
     }
     // Check if the update script exists
     $upgrade_schema = @file_get_contents('sql/upgrade' . $db_version . '-' . $upgrade_to . '.sql');
     if ($upgrade_schema == false) {
         mysql_query('USE ' . $mhi_db_name);
         return false;
     }
     // If a table prefix is specified, add it to sql
     if ($table_prefix) {
         $find = array('CREATE TABLE IF NOT EXISTS `', 'INSERT INTO `', 'ALTER TABLE `', 'UPDATE `', 'DROP TABLE `');
         $replace = array('CREATE TABLE IF NOT EXISTS `' . $table_prefix . '_', 'INSERT INTO `' . $table_prefix . '_', 'ALTER TABLE `' . $table_prefix . '_', 'UPDATE `' . $table_prefix . '_', 'DROP TABLE `' . $table_prefix . '_');
         $upgrade_schema = str_replace($find, $replace, $upgrade_schema);
     }
     // Split by ; to get the sql statement for creating individual tables.
     $queries = explode(';', $upgrade_schema);
     //Put a custom mysql_query() here in case you want to run something outside of the sql files.
     foreach ($queries as $query) {
         $result = mysql_query($query);
     }
     // END: Everything that happens in the deployment DB happens above
     //Switch back to our db, otherwise we would be running off some other deployments DB and that wouldn't work
     mysql_query('USE ' . $mhi_db_name);
 }
예제 #3
0
 public function create()
 {
     $this->template->header->this_body = '';
     $this->template->content = new View('mhi/mhi_create');
     // Process Form
     if ($_POST) {
         $post = Validation::factory($_POST);
         // Trim whitespaces
         $post->pre_filter('trim');
         $session = Session::instance();
         $mhi_user_id = $session->get('mhi_user_id');
         $blocked_subdomains = Kohana::config('mhi.blocked_subdomains');
         // These rules are only required if we aren't already logged in
         if ($mhi_user_id == FALSE) {
             $post->add_rules('signup_first_name', 'required', 'alpha_dash');
             $post->add_rules('signup_last_name', 'required', 'alpha_dash');
             $post->add_rules('signup_email', 'required', 'email');
             $post->add_rules('signup_password', 'required');
         } else {
             $post->add_rules('verify_password', 'required');
         }
         $post->add_rules('signup_subdomain', 'required', 'alpha_dash');
         $post->add_rules('signup_instance_name', 'required');
         $post->add_rules('signup_instance_tagline', 'required');
         // If we pass validation AND it's not one of the blocked subdomains
         if ($post->validate() and !in_array($post->signup_subdomain, $blocked_subdomains)) {
             $mhi_user = new Mhi_User_Model();
             $db_genesis = new DBGenesis();
             $mhi_site_database = new Mhi_Site_Database_Model();
             $mhi_site = new Mhi_Site_Model();
             // Check passwords if logged in and create user if not
             if ($mhi_user_id != FALSE) {
                 // Get user info
                 $user = $mhi_user->get($mhi_user_id);
                 $salt = Kohana::config('auth.salt_pattern');
                 $verify_password = sha1($post->verify_password . $salt);
                 if ($verify_password != $user->password) {
                     throw new Kohana_User_Exception('Password Match Error', "Passwords do not match. Dev TODO: Come back later and clean up validation!");
                 }
                 $user_id = $mhi_user_id;
                 $email = $user->email;
                 $name = $user->firstname . ' ' . $user->lastname;
                 $password = $post->verify_password;
             } else {
                 // Save new user
                 $user_id = $mhi_user->save_user(array('firstname' => $post->signup_first_name, 'lastname' => $post->signup_last_name, 'email' => $post->signup_email, 'password' => $post->signup_password));
                 $email = $post->signup_email;
                 $name = $post->signup_first_name . ' ' . $post->signup_last_name;
                 $password = $post->signup_password;
             }
             // Set up DB and Site
             $base_db = $db_genesis->current_db();
             $new_db_name = $base_db . '_' . $post->signup_subdomain;
             // Do some not so graceful validation
             if ($mhi_site_database->db_assigned($new_db_name) or $db_genesis->db_exists($new_db_name)) {
                 throw new Kohana_User_Exception('MHI Site Setup Error', "Database already exists and/or is already assigned in the MHI DB.");
             }
             if ($mhi_site->domain_exists($post->signup_subdomain)) {
                 throw new Kohana_User_Exception('MHI Site Setup Error', "Domain already assigned in MHI DB.");
             }
             // Create site
             $site_id = $mhi_site->save_site(array('user_id' => $user_id, 'site_domain' => $post->signup_subdomain, 'site_privacy' => 1, 'site_active' => 1));
             // Set up database and save details to MHI DB
             $db_genesis->create_db($new_db_name);
             $mhi_site_database->assign_db($new_db_name, $site_id);
             $db_genesis->populate_db($new_db_name, array('username' => $email, 'name' => $name, 'password' => $password, 'email' => $email), array('site_name' => $post->signup_instance_name, 'site_tagline' => $post->signup_instance_tagline));
             // Congrats, everything has been set up. Send an email confirmation.
             $settings = kohana::config('settings');
             $new_site_url = 'http://' . $post->signup_subdomain . '.' . $_SERVER['HTTP_HOST'] . Kohana::config('config.site_domain');
             if ($settings['site_email'] != NULL) {
                 $to = $email;
                 $from = $settings['site_email'];
                 $subject = 'You Deployment ' . $settings['site_name'] . ' set up';
                 $message = 'You new site, ' . $post->signup_instance_name . ' has been set up.' . "/n";
                 $message .= 'Admin URL: ' . $new_site_url . '/admin' . "/n";
                 $message .= 'Username: '******'Password: (hidden)' . "/n";
                 email::send($to, $from, $subject, $message, FALSE);
             }
         } else {
             throw new Kohana_User_Exception('Validation Error', "Form not validating. Dev TODO: Come back later and clean up validation!");
         }
     } else {
         // If the form was never posted, we need to complain about it.
         throw new Kohana_User_Exception('Incomplete Form', "Form not posted.");
     }
 }
예제 #4
0
 public function processcreation()
 {
     // Used to populate form fields. Will assign values on error
     $errors = array();
     $form = array('signup_first_name' => '', 'signup_last_name' => '', 'signup_email' => '', 'signup_password' => '', 'signup_subdomain' => '', 'signup_instance_name' => '', 'signup_instance_tagline' => '');
     $form_error = array();
     // Process Form
     if ($_POST) {
         $sfn = isset($_POST['signup_first_name']) ? $_POST['signup_first_name'] : '';
         $sln = isset($_POST['signup_last_name']) ? $_POST['signup_last_name'] : '';
         $sem = isset($_POST['signup_email']) ? $_POST['signup_email'] : '';
         $spw = isset($_POST['signup_password']) ? $_POST['signup_password'] : '';
         $form = array('signup_first_name' => $sfn, 'signup_last_name' => $sln, 'signup_email' => $sem, 'signup_password' => $spw, 'signup_subdomain' => strtolower($_POST['signup_subdomain']), 'signup_instance_name' => $_POST['signup_instance_name'], 'signup_instance_tagline' => $_POST['signup_instance_tagline']);
         $post = Validation::factory($_POST);
         // Trim whitespaces
         $post->pre_filter('trim');
         $session = Session::instance();
         $mhi_user_id = $session->get('mhi_user_id');
         $blocked_subdomains = Kohana::config('mhi.blocked_subdomains');
         // These rules are only required if we aren't already logged in
         if ($mhi_user_id == FALSE) {
             $post->add_rules('signup_first_name', 'required');
             $post->add_rules('signup_last_name', 'required');
             $post->add_rules('signup_email', 'required', 'email');
             $post->add_rules('signup_password', 'required');
         } else {
             $post->add_rules('verify_password', 'required');
         }
         $post->add_rules('signup_subdomain', 'required', 'alpha_dash');
         $post->add_rules('signup_instance_name', 'required');
         $post->add_rules('signup_instance_tagline', 'required');
         // If we pass validation AND it's not one of the blocked subdomains
         if ($post->validate()) {
             $mhi_user = new Mhi_User_Model();
             $db_genesis = new DBGenesis();
             $mhi_site_database = new Mhi_Site_Database_Model();
             $mhi_site = new Mhi_Site_Model();
             // Setup DB name variable
             $base_db = $db_genesis->current_db();
             $new_db_name = $base_db . '_' . strtolower($post->signup_subdomain);
             // Do some graceful validation
             if (!isset($post->signup_tos)) {
                 return array('errors' => $errors, 'form' => $form, 'form_error' => array('signup_tos' => 'You must accept the Website Terms of Use.'));
             }
             if (strlen($post->signup_subdomain) < 4 or strlen($post->signup_subdomain) > 32) {
                 // ERROR: subdomain length falls outside the char length bounds allowed.
                 return array('errors' => $errors, 'form' => $form, 'form_error' => array('signup_subdomain' => 'Subdomain must be between at least 4 characters and no more than 32 characters long. Please try again.'));
             }
             if ($mhi_site->domain_exists($post->signup_subdomain)) {
                 // ERROR: Domain already assigned in MHI DB.
                 return array('errors' => $errors, 'form' => $form, 'form_error' => array('signup_subdomain' => 'This subdomain has already been taken. Please try again.'));
             }
             if ($mhi_site_database->db_assigned($new_db_name) or $db_genesis->db_exists($new_db_name)) {
                 // ERROR: Database already exists and/or is already assigned in the MHI DB
                 return array('errors' => $errors, 'form' => $form, 'form_error' => array('signup_subdomain' => 'This subdomain is not allowed. Please try again.'));
             }
             if (in_array(strtolower($post->signup_subdomain), $blocked_subdomains)) {
                 // ERROR: Blocked Subdomain
                 return array('errors' => $errors, 'form' => $form, 'form_error' => array('signup_subdomain' => 'This subdomain is not allowed. Please try again.'));
             }
             // Check passwords if logged in and create user if not
             if ($mhi_user_id != FALSE) {
                 // Get user info
                 $user = $mhi_user->get($mhi_user_id);
                 $salt = Kohana::config('auth.salt_pattern');
                 $verify_password = sha1($post->verify_password . $salt);
                 if ($verify_password != $user->password) {
                     // ERROR: Passwords do not match.
                     return array('errors' => $errors, 'form' => $form, 'form_error' => array('password' => 'Password doesn\'t match. Please try again.'));
                 }
                 $user_id = $mhi_user_id;
                 $email = $user->email;
                 $name = $user->firstname . ' ' . $user->lastname;
                 $password = $post->verify_password;
             } else {
                 // Save new user
                 $user_id = $mhi_user->save_user(array('firstname' => $post->signup_first_name, 'lastname' => $post->signup_last_name, 'email' => $post->signup_email, 'password' => $post->signup_password));
                 $email = $post->signup_email;
                 $name = $post->signup_first_name . ' ' . $post->signup_last_name;
                 $password = $post->signup_password;
                 // Log new user in
                 $mhi_user_id = $mhi_user->login($email, $password);
                 Mhi_Log_Model::log($mhi_user_id, 6);
             }
             // Set up DB and Site
             // Create site
             $site_id = $mhi_site->save_site(array('user_id' => $user_id, 'site_domain' => strtolower($post->signup_subdomain), 'site_privacy' => 1, 'site_active' => 1));
             // Set up database and save details to MHI DB
             $db_genesis->create_db($new_db_name);
             $mhi_site_database->assign_db($new_db_name, $site_id);
             $db_genesis->populate_db($new_db_name, array('username' => $email, 'name' => $name, 'password' => $password, 'email' => $email), array('site_name' => $post->signup_instance_name, 'site_tagline' => $post->signup_instance_tagline, 'site_domain' => strtolower($post->signup_subdomain)));
             // Congrats, everything has been set up. Send an email confirmation.
             $settings = kohana::config('settings');
             $new_site_url = 'http://' . strtolower($post->signup_subdomain) . '.' . $_SERVER['HTTP_HOST'] . Kohana::config('config.site_domain');
             if ($settings['site_email'] != NULL) {
                 $to = $email;
                 $from = $settings['site_email'];
                 $subject = 'Your deployment at ' . $settings['site_name'];
                 $message = 'Your new site, ' . $post->signup_instance_name . ' has been set up.' . "\n";
                 $message .= 'Admin URL: ' . $new_site_url . 'admin' . "\n";
                 $message .= 'Username: '******'Password: (hidden)' . "\n";
                 email::send($to, $from, $subject, $message, FALSE);
             }
             Mhi_Log_Model::log($user_id, 3, 'Deployment Created: ' . strtolower($post->signup_subdomain));
         } else {
             if (isset($_POST['signup_password'])) {
                 unset($_POST['signup_password']);
             }
             if (isset($_POST['signup_confirm_password'])) {
                 unset($_POST['signup_confirm_password']);
             }
             if (isset($_POST['verify_password'])) {
                 unset($_POST['verify_password']);
             }
             Mhi_Log_Model::log($mhi_user_id, 8, 'Variables: ' . print_r($_POST, true) . ' * ' . print_r($post->errors('form_error_messages'), true));
             throw new Kohana_User_Exception('Validation Error', "Form not validating. Please go back and try again.");
         }
     } else {
         // If the form was never posted, we need to complain about it.
         throw new Kohana_User_Exception('Incomplete Form', "Form not posted.");
     }
     return array('errors' => $errors, 'form' => $form, 'form_error' => $form_error);
 }
예제 #5
0
파일: mhi.php 프로젝트: Nyamai/Ushahidi_Web
 public function create()
 {
     $this->template->header->this_body = '';
     $this->template->content = new View('mhi_create');
     // Process Form
     if ($_POST) {
         $post = Validation::factory($_POST);
         // Trim whitespaces
         $post->pre_filter('trim');
         $session = Session::instance();
         $mhi_user_id = $session->get('mhi_user_id');
         // These rules are only required if we aren't already logged in
         if ($mhi_user_id == FALSE) {
             $post->add_rules('signup_first_name', 'required', 'alpha_dash');
             $post->add_rules('signup_last_name', 'required', 'alpha_dash');
             $post->add_rules('signup_email', 'required', 'email');
             $post->add_rules('signup_password', 'required');
         } else {
             $post->add_rules('verify_password', 'required');
         }
         $post->add_rules('signup_subdomain', 'required', 'alpha_dash');
         $post->add_rules('signup_instance_name', 'required');
         $post->add_rules('signup_instance_tagline', 'required');
         if ($post->validate()) {
             $mhi_user = new Mhi_User_Model();
             $db_genesis = new DBGenesis();
             $mhi_site_database = new Mhi_Site_Database_Model();
             $mhi_site = new Mhi_Site_Model();
             // Check passwords if logged in and create user if not
             if ($mhi_user_id != FALSE) {
                 // Get user info
                 $user = $mhi_user->get($mhi_user_id);
                 $salt = Kohana::config('auth.salt_pattern');
                 $verify_password = sha1($post->verify_password . $salt);
                 if ($verify_password != $user->password) {
                     throw new Kohana_User_Exception('Password Match Error', "Passwords do not match. Dev TODO: Come back later and clean up validation!");
                 }
                 $user_id = $mhi_user_id;
                 $email = $user->email;
                 $name = $user->firstname . ' ' . $user->lastname;
                 $password = $post->verify_password;
             } else {
                 // Save new user
                 $user_id = $mhi_user->save_user(array('firstname' => $post->signup_first_name, 'lastname' => $post->signup_last_name, 'email' => $post->signup_email, 'password' => $post->signup_password));
                 $email = $post->signup_email;
                 $name = $post->signup_first_name . ' ' . $post->signup_last_name;
                 $password = $post->signup_password;
             }
             // Set up DB and Site
             $base_db = $db_genesis->current_db();
             $new_db_name = $base_db . '_' . $post->signup_subdomain;
             // Do some not so graceful validation
             if ($mhi_site_database->db_assigned($new_db_name) or $db_genesis->db_exists($new_db_name)) {
                 throw new Kohana_User_Exception('MHI Site Setup Error', "Database already exists and/or is already assigned in the MHI DB.");
             }
             if ($mhi_site->domain_exists($post->signup_subdomain)) {
                 throw new Kohana_User_Exception('MHI Site Setup Error', "Domain already assigned in MHI DB.");
             }
             // Create site
             $site_id = $mhi_site->save_site(array('user_id' => $user_id, 'site_domain' => $post->signup_subdomain, 'site_privacy' => 1, 'site_active' => 1));
             // Set up database and save details to MHI DB
             $db_genesis->create_db($new_db_name);
             $mhi_site_database->assign_db($new_db_name, $site_id);
             $db_genesis->populate_db($new_db_name, array('username' => $email, 'name' => $name, 'password' => $password, 'email' => $email), array('site_name' => $post->signup_instance_name, 'site_tagline' => $post->signup_instance_tagline));
         } else {
             throw new Kohana_User_Exception('Validation Error', "Form not validating. Dev TODO: Come back later and clean up validation!");
         }
     } else {
         // If the form was never posted, we need to complain about it.
         throw new Kohana_User_Exception('Incomplete Form', "Form not posted.");
     }
 }
예제 #6
0
 function get_db_versions($limit = 100, $only_needs_upgrade = TRUE)
 {
     $mhi_db = Kohana::config('database.default');
     $table_prefix = $mhi_db['table_prefix'];
     $mhi_db_name = $mhi_db['connection']['database'];
     $dbs = Mhi_Site_Database_Model::get_all_db_details();
     // Switch to new DB for a moment
     $array = array();
     $settings = ORM::factory('settings', 1);
     $current_version = $settings->db_version;
     $i = 0;
     foreach ($dbs as $db) {
         if ($i >= $limit) {
             break;
         }
         mysql_query('USE ' . $db . ';');
         // START: Everything that happens in the deployment DB happens below
         $settings = ORM::factory('settings', 1);
         if ($only_needs_upgrade == FALSE or $settings->db_version != $current_version) {
             $array[$db] = $settings->db_version;
             $i++;
         }
     }
     // END: Everything that happens in the deployment DB happens above
     //Switch back to our db, otherwise we would be running off some other deployments DB and that wouldn't work
     mysql_query('USE ' . $mhi_db_name);
     return $array;
 }
예제 #7
0
 /**
  * Lists the reports.
  * @param int $page
  */
 function updatelist()
 {
     $this->template->content = new View('admin/mhi_updatelist');
     $settings = kohana::config('settings');
     if (isset($_POST['mhiupdatedb'])) {
         Mhi_Site_Database_Model::update_db($_POST['db']);
     }
     if (isset($_GET['mhimassupdatedb']) and isset($_GET['from_version'])) {
         Mhi_Site_Database_Model::mass_update_db($_GET['mhimassupdatedb'], $_GET['from_version']);
     }
     $this->template->content->db_versions = Mhi_Site_Model::get_db_versions(100);
     asort($this->template->content->db_versions);
     $this->template->content->current_version = $settings['db_version'];
 }
예제 #8
0
 function get_db_versions()
 {
     $mhi_db = Kohana::config('database.default');
     $table_prefix = $mhi_db['table_prefix'];
     $mhi_db_name = $mhi_db['connection']['database'];
     $dbs = Mhi_Site_Database_Model::get_all_db_details();
     // Switch to new DB for a moment
     $array = array();
     foreach ($dbs as $db) {
         mysql_query('USE ' . $db . ';');
         // START: Everything that happens in the deployment DB happens below
         $settings = ORM::factory('settings', 1);
         $array[$db] = $settings->db_version;
     }
     // END: Everything that happens in the deployment DB happens above
     //Switch back to our db, otherwise we would be running off some other deployments DB and that wouldn't work
     mysql_query('USE ' . $mhi_db_name);
     return $array;
 }