Example #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.");
     }
 }
Example #2
0
 static function get_user_sites($user_id = FALSE, $detailed_data = FALSE)
 {
     $result = ORM::factory('mhi_site')->where('user_id', $user_id)->find_all();
     $sites = array();
     foreach ($result as $res) {
         if ($detailed_data != FALSE) {
             // Go to the deployment's database and grab some additional details
             $details = Mhi_Site_Model::get_site_details($res->site_domain);
             $res->site_name = $details['site_name'];
             $res->site_tagline = $details['site_tagline'];
         }
         $sites[] = $res;
     }
     return $sites;
 }
 static function mass_update_db($number_to_update, $from_version)
 {
     if (!is_numeric($number_to_update) or !is_numeric($from_version)) {
         // All of these must be numbers so return false if any of them arent
         return false;
     }
     $all_db_versions = Mhi_Site_Model::get_db_versions($number_to_update);
     $i = 0;
     foreach ($all_db_versions as $db => $current_version) {
         if ($i == $number_to_update) {
             break;
         }
         if ($current_version == $from_version) {
             Mhi_Site_Database_Model::update_db($db);
             $i++;
         }
     }
 }
Example #4
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.");
     }
 }
Example #5
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);
 }
Example #6
0
 /**
  * Lists the reports.
  * @param int $page
  */
 function index($page = 1)
 {
     $this->template->content = new View('admin/mhi');
     $this->template->content->title = Kohana::lang('ui_admin.multiple_hosted_instances');
     $this->template->content->domain_name = $_SERVER['HTTP_HOST'];
     // check, has the form been submitted?
     $form_error = FALSE;
     $form_saved = FALSE;
     $form_action = "";
     if ($_POST) {
         $post = Validation::factory($_POST);
         //  Add some filters
         $post->pre_filter('trim', TRUE);
         // Add some rules, the input field, followed by a list of checks, carried out in order
         $post->add_rules('action', 'required', 'alpha', 'length[1,1]');
         $post->add_rules('instance_id.*', 'required', 'numeric');
         if ($post->validate()) {
             if ($post->action == 'a') {
                 // Approve Action
                 foreach ($post->instance_id as $item) {
                     $update = new Mhi_Site_Model($item);
                     if ($update->loaded == true) {
                         $update->site_active = '1';
                         $update->save();
                     }
                 }
                 $form_action = strtoupper(Kohana::lang('ui_admin.approved'));
             } elseif ($post->action == 'u') {
                 // Unapprove Action
                 foreach ($post->instance_id as $item) {
                     $update = new Mhi_Site_Model($item);
                     if ($update->loaded == true) {
                         $update->site_active = '0';
                         $update->save();
                     }
                 }
                 $form_action = strtoupper(Kohana::lang('ui_admin.unapproved'));
             } elseif ($post->action == 'd') {
                 // Delete Action
                 foreach ($post->instance_id as $item) {
                     $update = new Mhi_Site_Model($item);
                     if ($update->loaded == true) {
                         $update->delete();
                     }
                 }
                 $form_action = Kohana::lang('ui_admin.deleted');
             }
             $form_saved = TRUE;
         } else {
             $form_error = TRUE;
         }
     }
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     $this->template->content->form_action = $form_action;
     // Status is the "Show All/Pending/Approved tabs'
     if (!empty($_GET['status'])) {
         $status = strtolower($_GET['status']);
         if ($status == 'a') {
             $filter = 'site_active = 1';
         } elseif ($status == 'p') {
             $filter = 'site_active = 0';
         }
     } else {
         $status = '0';
         $filter = '1=1';
         // Using 1=1 is a way to preserve the "where" statement to reduce code complexity
     }
     $this->template->content->status = $status;
     // Pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => (int) Kohana::config('settings.items_per_page_admin'), 'total_items' => ORM::factory('mhi_site')->where($filter)->count_all()));
     $this->template->content->pagination = $pagination;
     $db = new Database();
     $db->select('mhi_site.*, mhi_users.email, mhi_users.firstname, mhi_users.lastname');
     $db->from('mhi_site');
     $db->join('mhi_users', 'mhi_users.id', 'mhi_site.user_id');
     $db->where($filter);
     $db->orderby('mhi_site.site_dateadd', 'desc');
     $db->limit((int) Kohana::config('settings.items_per_page_admin'), $pagination->sql_offset);
     $instances = $db->get();
     $this->template->content->instances = $instances;
     $this->template->content->total_items = $pagination->total_items;
     // Javascript Header
     $this->template->js = new View('admin/mhi_js');
 }
Example #7
0
 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.");
     }
 }
Example #8
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'];
 }