Example #1
0
 /**
  * installs the software
  * @return [type] [description]
  */
 public static function execute()
 {
     $error_msg = '';
     $install = TRUE;
     $TABLE_PREFIX = '';
     ///////////////////////////////////////////////////////
     //check DB connection
     $link = @mysqli_connect(core::request('DB_HOST'), core::request('DB_USER'), core::request('DB_PASS'));
     if (!$link) {
         $error_msg = __('Cannot connect to server') . ' ' . core::request('DB_HOST');
         $install = FALSE;
     }
     if ($link and $install === TRUE) {
         if (core::request('DB_NAME')) {
             //they selected to create the DB
             if (core::request('DB_CREATE')) {
                 @mysqli_query($link, "CREATE DATABASE IF NOT EXISTS `" . core::request('DB_NAME') . "`");
             }
             $dbcheck = @mysqli_select_db($link, core::request('DB_NAME'));
             if (!$dbcheck) {
                 $error_msg .= __('Database name') . ': ' . mysqli_error($link);
                 $install = FALSE;
             }
         } else {
             $error_msg .= '<p>' . __('No database name was given') . '. ' . __('Available databases') . ':</p>';
             $db_list = @mysqli_query($link, 'SHOW DATABASES');
             $error_msg .= '<pre>';
             if (!$db_list) {
                 $error_msg .= __('Invalid query') . ':<br>' . mysqli_error($link);
             } else {
                 while ($row = mysqli_fetch_assoc($db_list)) {
                     $error_msg .= $row['Database'] . '<br>';
                 }
             }
             $error_msg .= '</pre>';
             $install = FALSE;
         }
     }
     //clean prefix
     $TABLE_PREFIX = core::slug(core::request('TABLE_PREFIX'));
     //save DB config/database.php
     if ($install === TRUE) {
         $_POST['TABLE_PREFIX'] = $TABLE_PREFIX;
         $_GET['TABLE_PREFIX'] = $TABLE_PREFIX;
         $search = array('[DB_HOST]', '[DB_USER]', '[DB_PASS]', '[DB_NAME]', '[TABLE_PREFIX]', '[DB_CHARSET]');
         $replace = array(core::request('DB_HOST'), core::request('DB_USER'), core::request('DB_PASS'), core::request('DB_NAME'), $TABLE_PREFIX, core::request('DB_CHARSET'));
         $install = install::replace_file(INSTALLROOT . 'samples/database.php', $search, $replace, APPPATH . 'config/database.php');
         if (!$install === TRUE) {
             $error_msg = __('Problem saving ' . APPPATH . 'config/database.php');
         }
     }
     //install DB
     if ($install === TRUE) {
         //check if has key is posted if not generate
         self::$hash_key = core::request('HASH_KEY') != '' ? core::request('HASH_KEY') : core::generate_password();
         //check if DB was already installed, I use content since is the last table to be created
         $installed = mysqli_num_rows(mysqli_query($link, "SHOW TABLES LIKE '" . $TABLE_PREFIX . "content'")) == 1 ? TRUE : FALSE;
         if ($installed === FALSE) {
             //if was installed do not launch the SQL.
             include INSTALLROOT . 'samples/install.sql' . EXT;
         }
     }
     ///////////////////////////////////////////////////////
     //AUTH config
     if ($install === TRUE) {
         $search = array('[HASH_KEY]', '[COOKIE_SALT]', '[QL_KEY]');
         $replace = array(self::$hash_key, self::$hash_key, self::$hash_key);
         $install = install::replace_file(INSTALLROOT . 'samples/auth.php', $search, $replace, APPPATH . 'config/auth.php');
         if (!$install === TRUE) {
             $error_msg = __('Problem saving ' . APPPATH . 'config/auth.php');
         }
     }
     ///////////////////////////////////////////////////////
     //create robots.txt
     if ($install === TRUE) {
         $search = array('[SITE_URL]', '[SITE_FOLDER]');
         $replace = array(core::request('SITE_URL'), core::request('SITE_FOLDER'));
         $install = install::replace_file(INSTALLROOT . 'samples/robots.txt', $search, $replace, DOCROOT . 'robots.txt');
         if (!$install === TRUE) {
             $error_msg = __('Problem saving ' . DOCROOT . 'robots.txt');
         }
     }
     ///////////////////////////////////////////////////////
     //create htaccess
     if ($install === TRUE) {
         $search = array('[SITE_FOLDER]');
         $replace = array(core::request('SITE_FOLDER'));
         $install = install::replace_file(INSTALLROOT . 'samples/example.htaccess', $search, $replace, DOCROOT . '.htaccess');
         if (!$install === TRUE) {
             $error_msg = __('Problem saving ' . DOCROOT . '.htaccess');
         }
     }
     ///////////////////////////////////////////////////////
     //all good!
     if ($install === TRUE) {
         core::delete(INSTALLROOT . 'install.lock');
         //core::delete(INSTALLROOT);//prevents from performing a new install
     } elseif ($link != FALSE) {
         if ($table_list = mysqli_query($link, "SHOW TABLES LIKE '" . $TABLE_PREFIX . "%'")) {
             while ($row = mysqli_fetch_assoc($table_list)) {
                 mysqli_query($link, "DROP TABLE " . $row[0]);
             }
         }
     }
     self::$error_msg = $error_msg;
     return $install;
 }