protected function isInstalled() { global $missingConfig, $db_config; if (!empty($missingConfig)) { return False; } try { $db = new PtyxisMySQL(); $db->connect($db_config); //get version setting $version = $db->query("SELECT * FROM setting WHERE name = 'version' "); if (!empty($version[0]['value'])) { return True; } } catch (Exception $e) { return False; } }
public function install() { //check if we should be running instal if ($this->isInstalled()) { die; } //get base url $host = $_SERVER['HTTP_HOST']; $uri = $_SERVER['REQUEST_URI']; $this->baseUrl = "http://{$host}" . str_replace('install', '', $uri); $this->data['base_url'] = $this->baseUrl; $this->data['errors'] = null; $this->setLayout('admin/install'); $this->setTemplate('admin/install'); $this->input = $_REQUEST; $this->validation->setRules('db_hostname', 'Hostname', 'required'); $this->validation->setRules('db_username', 'Username', 'required'); $this->validation->setRules('db_password', 'Password', 'required'); $this->validation->setRules('db_database', 'Database', 'required'); $this->validation->setRules('email', 'Email', 'required|valid_email'); $this->validation->setRules('password', 'Password', 'required|matches[passwordconf]'); if (!$this->validation->validate()) { if (!empty($this->input)) { $install = $this->input; $this->set('install', $install); $this->set('errors', $this->validation->getErrors()); } } else { //check the database details $db_config = array('host' => $this->input['db_hostname'], 'user' => $this->input['db_username'], 'password' => $this->input['db_password'], 'database' => $this->input['db_database']); //try to connect $ptyxisMySQL = new PtyxisMySQL(); try { $ptyxisMySQL->connect($db_config); } catch (Exception $e) { $errors[] = array('label' => 'Error', 'error' => 'Could not connect using database details provided'); $this->set('errors', $errors); } //database details worked if (empty($errors)) { //write the config $configContents = '<?php' . "\n"; //database $configContents .= '$db_config = ' . var_export($db_config, True) . ";\n\n"; //base_url //salt $this->salt = uniqid(mt_rand(), true); $kc_config = array('base_url' => $this->baseUrl, 'salt' => $this->salt); $configContents .= '$kc_config = ' . var_export($kc_config, True) . ";"; //write the config try { file_put_contents('app/config/config.php', $configContents); } catch (Exception $e) { $this->set('configContents', $configContents); } //lets check it isn't already installed //get version setting $installed = false; $version = $ptyxisMySQL->query("SELECT * FROM setting WHERE name = 'version' "); if (!empty($version[0]['value'])) { $installed = True; } if (!$installed) { $ptyxisMySQL->connect($db_config); //run install script $installScript = $this->getInstallScript(); if (!$ptyxisMySQL->multiQuery($installScript)) { $errors[] = array('label' => 'Error', 'error' => 'Install script failed.'); $this->set('errors', $errors); } } //if already installed we need a new user cause we have a new salt if ($installed) { $ptyxisMySQL->connect($db_config); $ptyxisMySQL->query('DELETE FROM user'); } //add the user to the database $user = array(); $user['email'] = $this->input['email']; $user['password'] = $this->hash_password($this->input['password']); $ptyxisMySQL->connect($db_config); $user_result = $ptyxisMySQL->queryPrepared('INSERT INTO user (email,password) VALUES(?,?)', 'ss', array($user['email'], $user['password'])); if (!$user_result) { $errors[] = array('label' => 'Error', 'error' => 'Adding user failed.'); $this->set('errors', $errors); } else { $this->set('install_success', True); } } } return $this->data; }