/** * Set the field values * * @param array $values * @param array $filters * @return \Pop\Form\Form */ public function setFieldValues(array $values = null, $filters = null) { parent::setFieldValues($values, $filters); if ($_POST) { // Check the content directory if (!file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . $this->content_path)) { $this->getElement('content_path')->addValidator(new Validator\NotEqual($this->content_path, $this->i18n->__('The content directory does not exist.'))); } else { $checkDirs = \Phire\Project::checkDirs($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . $this->content_path, true); if (count($checkDirs) > 0) { $this->getElement('content_path')->addValidator(new Validator\NotEqual($this->content_path, $this->i18n->__('The content directory (or subdirectories) are not writable.'))); } } // If not SQLite, check the DB parameters if (strpos($this->db_adapter, 'Sqlite') === false) { $this->getElement('db_name')->addValidator(new Validator\NotEmpty(null, $this->i18n->__('The database name is required.'))); $this->getElement('db_username')->addValidator(new Validator\NotEmpty(null, $this->i18n->__('The database username is required.'))); $this->getElement('db_password')->addValidator(new Validator\NotEmpty(null, $this->i18n->__('The database password is required.'))); $this->getElement('db_host')->addValidator(new Validator\NotEmpty(null, $this->i18n->__('The database host is required.'))); } // Check the database credentials if ($this->isValid()) { $oldError = ini_get('error_reporting'); error_reporting(E_ERROR); $dbCheck = Dbs::check(array('database' => $this->db_name, 'username' => $this->db_username, 'password' => $this->db_password, 'host' => $this->db_host, 'type' => str_replace('\\', '_', $this->db_adapter))); // If there is a DB error if (null != $dbCheck) { $this->getElement('db_adapter')->addValidator(new Validator\NotEqual($this->db_adapter, wordwrap($dbCheck, 50, '<br />'))); } else { // Check the database version if (strpos($this->db_adapter, 'Sqlite') === false) { $adapter = stripos($this->db_adapter, 'Pdo\\') !== false ? str_replace('Pdo\\', '', $this->db_adapter) : $this->db_adapter; $db = Db::factory($adapter, array('database' => $this->db_name, 'username' => $this->db_username, 'password' => $this->db_password, 'host' => $this->db_host, 'type' => strtolower(str_replace('Pdo\\', '', $this->db_adapter)))); $version = $db->adapter()->version(); $version = substr($version, strrpos($version, ' ') + 1); if (strpos($version, '-') !== false) { $version = substr($version, 0, strpos($version, '-')); } if (stripos($this->db_adapter, 'Mysql') !== false) { $dbVerKey = 'Mysql'; } else { if (stripos($this->db_adapter, 'Pgsql') !== false) { $dbVerKey = 'Pgsql'; } else { $dbVerKey = null; } } if (null !== $dbVerKey && version_compare($version, self::$dbVersions[$dbVerKey]) < 0) { $this->getElement('db_adapter')->addValidator(new Validator\NotEqual($this->db_adapter, wordwrap($this->i18n->__('The %1 database version must be %2 or greater. (%3 detected.)', array($dbVerKey, self::$dbVersions[$dbVerKey], $version)), 45, '<br />'))); } } } error_reporting($oldError); } } return $this; }
/** * Install modules method * * @throws \Phire\Exception * @return void */ public function installModules() { try { $path = BASE_PATH . APP_URI; if ($path == '') { $path = '/'; } $modulePath1 = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules'; $modulePath2 = __DIR__ . '/../../../../../module'; $formats = Archive::formats(); $phireCookie = null; foreach ($this->data['new'] as $name => $module) { $ext = substr($module, strrpos($module, '.') + 1); if (array_key_exists($ext, $formats)) { $modPath = file_exists($modulePath1 . '/' . $module) ? $modulePath1 : $modulePath2; if (!is_writable($modPath)) { throw new \Phire\Exception($this->i18n->__('The modules folder is not writable.')); } $archive = new Archive($modPath . '/' . $module); $archive->extract($modPath . '/'); if ((stripos($module, 'gz') || stripos($module, 'bz')) && file_exists($modPath . '/' . $name . '.tar')) { unlink($modPath . '/' . $name . '.tar'); } $dbType = Table\Extensions::getSql()->getDbType(); if ($dbType == \Pop\Db\Sql::SQLITE) { $type = 'sqlite'; } else { if ($dbType == \Pop\Db\Sql::PGSQL) { $type = 'pgsql'; } else { $type = 'mysql'; } } $sqlFile = $modPath . '/' . $name . '/data/' . strtolower($name) . '.' . $type . '.sql'; $cfg = null; $tables = array(); $info = array(); // Check for a config and try to get info out of it if (file_exists($modPath . '/' . $name . '/config') && file_exists($modPath . '/' . $name . '/config/module.php')) { $cfg = file_get_contents($modPath . '/' . $name . '/config/module.php'); if (strpos($cfg, '*/') !== false) { $cfgHeader = substr($cfg, 0, strpos($cfg, '*/')); $cfgHeader = substr($cfgHeader, strpos($cfgHeader, '/*') + 2); $cfgHeaderAry = explode("\n", $cfgHeader); foreach ($cfgHeaderAry as $line) { if (strpos($line, ':')) { $ary = explode(':', $line); if (isset($ary[0]) && isset($ary[1])) { $key = trim(str_replace('*', '', $ary[0])); $value = trim(str_replace('*', '', $ary[1])); $info[$key] = $value; } } } } } if (file_exists($sqlFile)) { // Get any tables required and created by this module $sql = file_get_contents($sqlFile); $tables = array(); $matches = array(); preg_match_all('/^CREATE TABLE(.*)$/mi', $sql, $matches); if (isset($matches[0]) && isset($matches[0][0])) { foreach ($matches[0] as $table) { if (strpos($table, '`') !== false) { $table = substr($table, strpos($table, '`') + 1); $table = substr($table, 0, strpos($table, '`')); } else { if (strpos($table, '"') !== false) { $table = substr($table, strpos($table, '"') + 1); $table = substr($table, 0, strpos($table, '"')); } else { if (strpos($table, "'") !== false) { $table = substr($table, strpos($table, "'") + 1); $table = substr($table, 0, strpos($table, "'")); } else { if (stripos($table, 'EXISTS') !== false) { $table = substr($table, stripos($table, 'EXISTS') + 6); } else { $table = substr($table, stripos($table, 'TABLE') + 5); } if (strpos($table, '(') !== false) { $table = substr($table, 0, strpos($table, '(')); } $table = trim($table); } } } $tables[] = str_replace('[{prefix}]', DB_PREFIX, $table); } } $ext = new Table\Extensions(array('name' => $name, 'file' => $module, 'type' => 1, 'active' => 1, 'assets' => serialize(array('tables' => $tables, 'info' => $info)))); $ext->save(); // If DB is SQLite if (stripos($type, 'Sqlite') !== false) { $dbName = realpath($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/.htphire.sqlite'); $dbUser = null; $dbPassword = null; $dbHost = null; $installFile = $dbName; } else { $dbName = DB_NAME; $dbUser = DB_USER; $dbPassword = DB_PASS; $dbHost = DB_HOST; $installFile = null; } $db = array('database' => $dbName, 'username' => $dbUser, 'password' => $dbPassword, 'host' => $dbHost, 'prefix' => DB_PREFIX, 'type' => DB_INTERFACE == 'Pdo' ? 'Pdo_' . ucfirst(DB_TYPE) : DB_INTERFACE); Dbs::install($dbName, $db, $sqlFile, $installFile, true, false); } else { $ext = new Table\Extensions(array('name' => $name, 'type' => 1, 'active' => 1, 'assets' => serialize(array('tables' => $tables, 'info' => $info)))); $ext->save(); } if (null !== $cfg) { $config = (include $modPath . '/' . $name . '/config/module.php'); if (null !== $config[$name]->install) { $installFunc = $config[$name]->install; $installFunc(); } } if (php_sapi_name() != 'cli') { $cookie = Cookie::getInstance(array('path' => $path)); if (isset($cookie->phire)) { if (null === $phireCookie) { $phireCookie = $cookie->phire; } $i18n = file_exists($modPath . '/' . $name . '/data/assets/i18n'); $modules = (array) $phireCookie->modules; $modules[] = array('name' => $name, 'i18n' => $i18n); $phireCookie->modules = $modules; } } } } if (null !== $phireCookie) { $cookie = Cookie::getInstance(array('path' => $path)); $cookie->set('phire', $phireCookie); } } catch (\Exception $e) { $this->data['error'] = $e->getMessage(); } }
/** * Install config method * * @param mixed $form * @param string $docRoot * @return void */ public function config($form, $docRoot = null) { if (null === $docRoot) { $docRoot = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH; } // Get config file contents $cfgFile = new File($docRoot . '/config.php'); $config = $cfgFile->read(); // Get DB interface and type if (strpos($form->db_adapter, 'Pdo') !== false) { $dbInterface = 'Pdo'; $dbType = strtolower(substr($form->db_adapter, strrpos($form->db_adapter, '\\') + 1)); } else { $dbInterface = html_entity_decode($form->db_adapter, ENT_QUOTES, 'UTF-8'); $dbType = null; } // If DB is SQLite if (strpos($form->db_adapter, 'Sqlite') !== false) { touch($docRoot . $form->content_path . '/.htphire.sqlite'); $relativeDbName = "__DIR__ . '" . $form->content_path . '/.htphire.sqlite'; $dbName = realpath($docRoot . $form->content_path . '/.htphire.sqlite'); $dbUser = null; $dbPassword = null; $dbHost = null; $installFile = $dbName; chmod($dbName, 0777); } else { $relativeDbName = null; $dbName = $form->db_name; $dbUser = $form->db_username; $dbPassword = $form->db_password; $dbHost = $form->db_host; $installFile = null; } $dbPrefix = $form->db_prefix; // Set config values $config = str_replace("define('CONTENT_PATH', '/phire-content');", "define('CONTENT_PATH', '" . $form->content_path . "');", $config); $config = str_replace("define('APP_URI', '/phire');", "define('APP_URI', '" . $form->app_uri . "');", $config); $config = str_replace("define('DB_INTERFACE', '');", "define('DB_INTERFACE', '" . $dbInterface . "');", $config); $config = str_replace("define('DB_TYPE', '');", "define('DB_TYPE', '" . $dbType . "');", $config); $config = str_replace("define('DB_NAME', '');", "define('DB_NAME', " . (null !== $relativeDbName ? $relativeDbName : "'" . $dbName) . "');", $config); $config = str_replace("define('DB_USER', '');", "define('DB_USER', '" . $dbUser . "');", $config); $config = str_replace("define('DB_PASS', '');", "define('DB_PASS', '" . $dbPassword . "');", $config); $config = str_replace("define('DB_HOST', '');", "define('DB_HOST', '" . $dbHost . "');", $config); $config = str_replace("define('DB_PREFIX', '');", "define('DB_PREFIX', '" . $dbPrefix . "');", $config); $this->data['configWritable'] = is_writable($docRoot . '/config.php'); if ($form instanceof \Pop\Form\Form) { // Store the config values in session in case config file is not writable. $sess = Session::getInstance(); $sess->config = serialize(htmlentities($config, ENT_QUOTES, 'UTF-8')); $sess->app_uri = $form->app_uri; } if ($this->data['configWritable']) { $cfgFile->write($config)->save(); } // Install the database $sqlFile = __DIR__ . '/../../../data/phire.' . str_replace(array('pdo\\', 'mysqli'), array('', 'mysql'), strtolower($form->db_adapter)) . '.sql'; $db = array('database' => $dbName, 'username' => $dbUser, 'password' => $dbPassword, 'host' => $dbHost, 'prefix' => $dbPrefix, 'type' => str_replace('\\', '_', $form->db_adapter)); Dbs::install($dbName, $db, $sqlFile, $installFile, true); if (stripos($form->db_adapter, 'Pdo\\') !== false) { $adapter = 'Pdo'; $type = strtolower(substr($form->db_adapter, strpos($form->db_adapter, '\\') + 1)); } else { $adapter = $form->db_adapter; $type = null; } // Set the default system config $db = Db::factory($adapter, array('database' => $dbName, 'username' => $dbUser, 'password' => $dbPassword, 'host' => $dbHost, 'type' => $type)); // Get server info if (isset($_SERVER) && isset($_SERVER['SERVER_SOFTWARE'])) { $server = new Server(); $os = $server->getOs() . ' (' . $server->getDistro() . ')'; $srv = $server->getServer() . ' ' . $server->getServerVersion(); $domain = $_SERVER['HTTP_HOST']; $doc = $_SERVER['DOCUMENT_ROOT']; } else { $os = ''; $srv = ''; $domain = ''; $doc = ''; } // Set the system configuration $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . \Phire\Project::VERSION . "' WHERE setting = 'system_version'"); $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . $db->adapter()->escape($domain) . "' WHERE setting = 'system_domain'"); $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . $db->adapter()->escape($doc) . "' WHERE setting = 'system_document_root'"); $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . $db->adapter()->escape($os) . "' WHERE setting = 'server_operating_system'"); $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . $db->adapter()->escape($srv) . "' WHERE setting = 'server_software'"); $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . $db->adapter()->version() . "' WHERE setting = 'database_version'"); $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . PHP_VERSION . "' WHERE setting = 'php_version'"); $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . date('Y-m-d H:i:s') . "' WHERE setting = 'installed_on'"); $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "config SET value = '" . $db->adapter()->escape($form->language) . "' WHERE setting = 'default_language'"); $db->adapter()->query("UPDATE " . $db->adapter()->escape($dbPrefix) . "user_types SET password_encryption = '" . $db->adapter()->escape((int) $form->password_encryption) . "' WHERE id = 2001"); }
/** * Install the project based on the available config files * * @param string $installFile * @return void */ public static function install($installFile) { // Display instructions to continue $dbTables = array(); self::instructions(); $input = self::cliInput(); if ($input == 'n') { echo I18n::factory()->__('Aborted.') . PHP_EOL . PHP_EOL; exit(0); } // Get the install config. $installDir = realpath(dirname($installFile)); $install = (include $installFile); // Check if a project folder already exists. if (file_exists($install->project->name)) { echo wordwrap(I18n::factory()->__('Project folder exists. This may overwrite any project files you may already have under that project folder.'), 70, PHP_EOL) . PHP_EOL; $input = self::cliInput(); } else { $input = 'y'; } // If 'No', abort if ($input == 'n') { echo I18n::factory()->__('Aborted.') . PHP_EOL . PHP_EOL; exit(0); // Else, continue } else { $db = false; $databases = array(); // Test for a database creds and schema, and ask // to test and install the database. if (isset($install->databases)) { $databases = $install->databases->asArray(); echo I18n::factory()->__('Database credentials and schema detected.') . PHP_EOL; $input = self::cliInput(I18n::factory()->__('Test and install the database(s)?') . ' (Y/N) '); $db = $input == 'n' ? false : true; } // Handle any databases if ($db) { // Get current error reporting setting and set // error reporting to E_ERROR to suppress warnings $oldError = ini_get('error_reporting'); error_reporting(E_ERROR); // Test the databases echo I18n::factory()->__('Testing the database(s)...') . PHP_EOL; foreach ($databases as $dbname => $db) { echo I18n::factory()->__('Testing') . ' \'' . $dbname . '\'...' . PHP_EOL; if (!isset($db['type']) || !isset($db['database'])) { echo I18n::factory()->__('The database type and database name must be set for the database ') . '\'' . $dbname . '\'.' . PHP_EOL . PHP_EOL; exit(0); } $check = Install\Dbs::check($db); if (null !== $check) { echo $check . PHP_EOL . PHP_EOL; exit(0); } else { echo I18n::factory()->__('Database') . ' \'' . $dbname . '\' passed.' . PHP_EOL; echo I18n::factory()->__('Installing database') . ' \'' . $dbname . '\'...' . PHP_EOL; $tables = Install\Dbs::install($dbname, $db, $installDir, $install); if (count($tables) > 0) { $dbTables = array_merge($dbTables, $tables); } } } // Return error reporting to its original state error_reporting($oldError); } // Install base folder and file structure Install\Base::install($install); // Install project files Install\Projects::install($install, $installDir); // Install table class files if (count($dbTables) > 0) { Install\Tables::install($install, $dbTables); } // Install controller class files if (isset($install->controllers)) { Install\Controllers::install($install, $installDir); } // Install form class files if (isset($install->forms)) { Install\Forms::install($install); } // Install model class files if (isset($install->models)) { Install\Models::install($install); } // Create 'bootstrap.php' file Install\Bootstrap::install($install); echo I18n::factory()->__('Project install complete.') . PHP_EOL . PHP_EOL; } }
/** * Install command * * @return void */ protected function install() { if (!is_writable(PH_CLI_ROOT . '/config.php')) { echo ' The configuration file is not writable. Please make it writable before continuing.' . PHP_EOL . PHP_EOL; } else { // Install config file and database $input = array('language' => null, 'db_adapter' => null, 'db_name' => null, 'db_username' => null, 'db_password' => null, 'db_host' => defined('DB_HOST') && DB_HOST != '' ? DB_HOST : 'localhost', 'db_prefix' => defined('DB_PREFIX') && DB_HOST != '' ? DB_PREFIX : 'ph_', 'app_uri' => defined('APP_URI') ? APP_URI : '/phire', 'content_path' => defined('CONTENT_PATH') ? CONTENT_PATH : '/phire-content', 'password_encryption' => 4); $langs = \Pop\I18n\I18n::getLanguages(); $langKeys = array_keys($langs); $langsList = null; $i = 1; foreach ($langs as $key => $value) { $num = $i < 10 ? ' ' . $i : $i; $langsList .= ' ' . $num . ' : [' . $key . '] ' . $value . PHP_EOL; $i++; } $db = array('Mysqli', 'Pdo\\Mysql', 'Pdo\\Pgsql', 'Pdo\\Sqlite', 'Pgsql', 'Sqlite'); echo 'Installation' . PHP_EOL; echo '------------' . PHP_EOL; echo PHP_EOL; echo ' Select Language:' . PHP_EOL . PHP_EOL; echo $langsList . PHP_EOL; echo PHP_EOL; $inputLang = -1; while (!isset($langKeys[$inputLang])) { $inputLang = self::cliInput(' Enter Language # (Enter for English): '); if (empty($inputLang)) { $inputLang = 3; } else { $inputLang--; } } $input['language'] = $langKeys[$inputLang]; echo PHP_EOL . ' Select DB Adapter:' . PHP_EOL . PHP_EOL; foreach ($db as $key => $value) { echo ' ' . ($key + 1) . ' : ' . $value . PHP_EOL; } echo PHP_EOL; $inputDb = -1; while (!isset($db[$inputDb])) { $inputDb = self::cliInput(' Enter DB Adapter #: '); $inputDb--; } $input['db_adapter'] = $db[$inputDb]; if (stripos($input['db_adapter'], 'sqlite') === false) { $input['db_name'] = self::cliInput(' DB Name: '); $input['db_username'] = self::cliInput(' DB Username: '******'db_password'] = self::cliInput(' DB Password: '******' DB Host (Enter for \'' . $input['db_host'] . '\'): '); $input['db_host'] = empty($inputHost) ? 'localhost' : $inputHost; } $inputPrefix = self::cliInput(' DB Prefix (Enter for \'' . $input['db_prefix'] . '\'): '); $input['db_prefix'] = empty($inputPrefix) ? $input['db_prefix'] : $inputPrefix; $inputAppUri = self::cliInput(' Application URI (Enter for \'' . $input['app_uri'] . '\'): '); $input['app_uri'] = empty($inputAppUri) ? $input['app_uri'] : $inputAppUri; $inputContentPath = self::cliInput(' Content Path (Enter for \'' . $input['content_path'] . '\'): '); $input['content_path'] = empty($inputContentPath) ? $input['content_path'] : $inputContentPath; // Check the content directory if (!file_exists(PH_CLI_ROOT . $input['content_path'])) { echo PHP_EOL . ' The content directory does not exist.' . PHP_EOL . PHP_EOL; exit; } else { $checkDirs = Project::checkDirs(PH_CLI_ROOT . $input['content_path'], true); if (count($checkDirs) > 0) { echo PHP_EOL . ' The content directory (or subdirectories) are not writable.' . PHP_EOL . PHP_EOL; exit; } } echo PHP_EOL . ' ...Checking Database...'; if (stripos($input['db_adapter'], 'sqlite') === false) { $oldError = ini_get('error_reporting'); error_reporting(E_ERROR); $dbCheck = \Pop\Project\Install\Dbs::check(array('database' => $input['db_name'], 'username' => $input['db_username'], 'password' => $input['db_password'], 'host' => $input['db_host'], 'type' => str_replace('\\', '_', $input['db_adapter']))); error_reporting($oldError); if (null != $dbCheck) { echo PHP_EOL . PHP_EOL . ' ' . wordwrap($dbCheck, 70, PHP_EOL . ' ') . PHP_EOL . PHP_EOL; echo ' Please try again.' . PHP_EOL . PHP_EOL; exit; } } echo '..OK!' . PHP_EOL . ' ...Installing Database...'; $install = $install = new Model\Install(); $install->config(new \ArrayObject($input, \ArrayObject::ARRAY_AS_PROPS), realpath(PH_CLI_ROOT)); // Install initial user echo 'OK!' . PHP_EOL . PHP_EOL . ' Initial User Setup:' . PHP_EOL . PHP_EOL; $user = array('email' => null, 'username' => null, 'password' => null); $email = ''; $username = ''; $password = ''; $emailValidator = new \Pop\Validator\Email(); $usernameValidator = new \Pop\Validator\AlphaNumeric(); while (!$emailValidator->evaluate($email)) { $email = self::cliInput(' Enter User Email: '); } while (strlen($username) < 4 || !$usernameValidator->evaluate($username)) { $username = self::cliInput(' Enter Username (> 4 characters): '); } while (strlen($password) < 6) { $password = self::cliInput(' Enter Password (> 6 characters): '); } $user['email'] = $email; $user['username'] = $username; $user['password'] = $password; echo PHP_EOL . ' ...Saving Initial User...' . PHP_EOL . PHP_EOL; if (stripos($input['db_adapter'], 'Pdo') !== false) { $dbInterface = 'Pdo'; $dbType = substr($input['db_adapter'], strpos($input['db_adapter'], '\\') + 1); } else { $dbInterface = $input['db_adapter']; $dbType = null; } if (stripos($input['db_adapter'], 'sqlite') !== false) { $input['db_name'] = PH_CLI_ROOT . $input['content_path'] . '/.htphire.sqlite'; chmod(realpath(PH_CLI_ROOT . $input['content_path'] . '/.htphire.sqlite'), 0777); } $db = \Pop\Db\Db::factory($dbInterface, array('type' => $dbType, 'database' => $input['db_name'], 'host' => $input['db_host'], 'username' => $input['db_username'], 'password' => $input['db_password'])); $db->adapter()->query("INSERT INTO " . $input['db_prefix'] . "users (type_id, role_id, username, password, email, verified, failed_attempts, site_ids, created) VALUES (2001, 3001, '" . $user['username'] . "', '" . Model\User::encryptPassword($user['password'], 4) . "', '" . $user['email'] . "', 1, 0, '" . serialize(array(0)) . "', '" . date('Y-m-d H:i:s') . "')"); $db->adapter()->query('UPDATE ' . $input['db_prefix'] . 'config SET value = \'' . $user['email'] . '\' WHERE setting = \'system_email\''); } }