<commit_before><?php
/**
 * installer.php
 */

defined('IN_WITY') or die('Access denied');

require 'view.php';

/**
 * Installer installs Wity on the server (configuration files and MySQL tables).
 *
 * @package Installer
 * @author <NAME> <<EMAIL>>
 * @version 0.4.0-17-01-2013
 */
class Installer {
	private $THEMES_DIR;
	private $APPS_DIR;
	private $CONFIG_DIR;

	private $EXCLUDED_THEMES = array('system', 'admin-bootstrap');
	private $EXCLUDED_APPS = array('admin');
	private $EXCLUDED_DIRS = array('.', '..');

	private $view;

	/**
	 * Security system
	 *
	 *  if (the lock file exists && the lock file is still valid) || lock file does not exist
	 *      create lock file (again)
	 *      execute control
	 *  else
	 *      return an error message (msg: delete lock file)
	 *
	 */
	public function launch() {
		$this->THEMES_DIR = "themes";
		$this->APPS_DIR = "apps";
		$this->CONFIG_DIR = "system".DS."config";

		$this->view = new View();

		$data = WRequest::getAssoc(array('command', 'installer', 'group'), '', 'POST');

		switch ($data['command']) {
			default:
			case 'START':
				$this->view->render();
				return;

			case 'INIT_INSTALLER':
				if (!class_exists('PDO')) {
					$this->view->error('installer', $data['installer'], 'System failure', 'PDO class cannot be found. This feature has been introduced since PHP5.1+');
					return;
				}
				break;

			case 'FINISH_INSTALLATION':
				// Store the data in config files
				if ($this->installerValidation($data)) {
					set_time_limit(0);

					// Get config data
					$config = WRequest::getAssoc(array('site_name', 'base', 'theme', 'language', 'timezone'), '', 'POST');
					$route = WRequest::getAssoc(array('front_app', 'admin_app'), '', 'POST');
					$database = WRequest::getAssoc(array('dbserver', 'dbport', 'dbuser', 'dbpassword', 'dbname', 'dbprefix'), '', 'POST');
					$user = WRequest::getAssoc(array('nickname', 'password', 'email', 'firstname', 'lastname'), '', 'POST');
					$user['password'] = <PASSWORD>1($user['password']);
					$user['confirm'] = 0;
					$user['access'] = 'all';
					$user['valid'] = 1;

					$database['dbprefix'] = (!empty($database['dbprefix'])) ? $database['dbprefix']."_":"";

					// Create SQL Tables
					$sql_commands = file_get_contents('installer'.DS.'bdd'.DS.'wity.sql');
					$sql_commands = str_replace('prefix_', $database['dbprefix'], $sql_commands); // configure prefix
					$db = $this->getSQLServerConnection($database);
					$db->exec($sql_commands);
					$error = $db->errorInfo();
					if (!is_null($error[0]) && !$error[0]!=0) {
						$this->view->error('installer', $data['installer'], 'Fatal Error', 'Impossible to create the WityCMS tables in the database. Please, import installer/bdd/wity.sql file manually in your database.');
					}

					// Save Database configuration
					WConfig::set('database.server', str_replace('localhost', '127.0.0.1', $database['dbserver']));
					WConfig::set('database.port', $database['dbport']);
					WConfig::set('database.user', $database['dbuser']);
					WConfig::set('database.pw', $database['dbpassword']);
					WConfig::set('database.dbname', $database['dbname']);
					WConfig::set('database.prefix', $database['dbprefix']);
					WConfig::save('database', CONFIG_DIR.'database.php');

					// Create user account
					$placeholder = false;
					if ($this->isFrontApp('user', null, $placeholder)) {
						include APPS_DIR.'user'.DS.'admin'.DS.'model.php';
						$userModel = new UserAdminModel();
						if (!$userModel->createUser($user)) {
							$this->view->error('installer', $data['installer'], 'Fatal Error', 'Impossible to create your administrator account. Please, check your database credentials.');
						}
					} else {
						$this->view->error('installer', $data['installer'], 'Fatal Error', 'The User application required by the system cannot be found. Please, download a complete package of WityCMS.');
					}

					// Save General configuration
					WConfig::set('config.base', trim($config['base'], '/'));
					WConfig::set('config.site_name', $config['site_name']);
					WConfig::set('config.theme', $config['theme']);
					WConfig::set('config.lang', $config['language']);
					WConfig::set('config.timezone', $config['timezone']);
					WConfig::set('config.email', $user['email']);
					WConfig::set('config.debug', false);
					WConfig::save('config', CONFIG_DIR.'config.php');

					// Save Route configuration
					WConfig::set('route.default_front', $route['front_app']);
					WConfig::set('route.default_admin', $route['admin_app']);
					WConfig::save('route', CONFIG_DIR.'route.php');

					// If success, Delete installer directory
					if (file_exists(CONFIG_DIR.'config.php') && file_exists(CONFIG_DIR.'database.php') && file_exists(CONFIG_DIR.'route.php')) {
						$dir = WITY_PATH.'installer';
						$it = new RecursiveDirectoryIterator($dir);
						$files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
						foreach ($files as $file) {
							if ($file->getFilename() === '.' || $file->getFilename() === '..') {
								continue;
							}
							if ($file->isDir()) {
								rmdir($file->getRealPath());
							} else {
								unlink($file->getRealPath());
							}
						}
						rmdir($dir);

						$this->view->success('installer', $data['installer'], 'Congratulations', 'Installation finished !');
					} else {
						$this->view->error('installer', $data['installer'], 'Fatal Error', 'Data submitted cannot be validated. Please, restart the installation and fill in the form again.');
					}
				} else {
					$this->view->error('installer', $data['installer'], 'Fatal Error', 'Data submitted cannot be validated. Please, restart the installation and fill in the form again.');<commit_msg>return on error on installing<commit_after><?php
/**
 * installer.php
 */

defined('IN_WITY') or die('Access denied');

require 'view.php';

/**
 * Installer installs Wity on the server (configuration files and MySQL tables).
 *
 * @package Installer
 * @author <NAME> <<EMAIL>>
 * @version 0.4.0-17-01-2013
 */
class Installer {
	private $THEMES_DIR;
	private $APPS_DIR;
	private $CONFIG_DIR;

	private $EXCLUDED_THEMES = array('system', 'admin-bootstrap');
	private $EXCLUDED_APPS = array('admin');
	private $EXCLUDED_DIRS = array('.', '..');

	private $view;

	/**
	 * Security system
	 *
	 *  if (the lock file exists && the lock file is still valid) || lock file does not exist
	 *      create lock file (again)
	 *      execute control
	 *  else
	 *      return an error message (msg: delete lock file)
	 *
	 */
	public function launch() {
		$this->THEMES_DIR = "themes";
		$this->APPS_DIR = "apps";
		$this->CONFIG_DIR = "system".DS."config";

		$this->view = new View();

		$data = WRequest::getAssoc(array('command', 'installer', 'group'), '', 'POST');

		switch ($data['command']) {
			default:
			case 'START':
				$this->view->render();
				return;

			case 'INIT_INSTALLER':
				if (!class_exists('PDO')) {
					$this->view->error('installer', $data['installer'], 'System failure', 'PDO class cannot be found. This feature has been introduced since PHP5.1+');
					return;
				}
				break;

			case 'FINISH_INSTALLATION':
				// Store the data in config files
				if ($this->installerValidation($data)) {
					set_time_limit(0);

					// Get config data
					$config = WRequest::getAssoc(array('site_name', 'base', 'theme', 'language', 'timezone'), '', 'POST');
					$route = WRequest::getAssoc(array('front_app', 'admin_app'), '', 'POST');
					$database = WRequest::getAssoc(array('dbserver', 'dbport', 'dbuser', 'dbpassword', 'dbname', 'dbprefix'), '', 'POST');
					$user = WRequest::getAssoc(array('nickname', 'password', 'email', 'firstname', 'lastname'), '', 'POST');
					$user['password'] = <PASSWORD>($user['password']);
					$user['confirm'] = 0;
					$user['access'] = 'all';
					$user['valid'] = 1;

					$database['dbprefix'] = (!empty($database['dbprefix'])) ? $database['dbprefix']."_":"";

					// Create SQL Tables
					$sql_commands = file_get_contents('installer'.DS.'bdd'.DS.'wity.sql');
					$sql_commands = str_replace('prefix_', $database['dbprefix'], $sql_commands); // configure prefix
					$db = $this->getSQLServerConnection($database);
					$db->exec($sql_commands);
					$error = $db->errorInfo();
					if (!is_null($error[0]) && !$error[0]!=0) {
						$this->view->error('installer', $data['installer'], 'Fatal Error', 'Impossible to create the WityCMS tables in the database. Please, import installer/bdd/wity.sql file manually in your database.');
						return;
					}

					// Save Database configuration
					WConfig::set('database.server', str_replace('localhost', '127.0.0.1', $database['dbserver']));
					WConfig::set('database.port', $database['dbport']);
					WConfig::set('database.user', $database['dbuser']);
					WConfig::set('database.pw', $database['dbpassword']);
					WConfig::set('database.dbname', $database['dbname']);
					WConfig::set('database.prefix', $database['dbprefix']);
					WConfig::save('database', CONFIG_DIR.'database.php');

					// Create user account
					$placeholder = false;
					if ($this->isFrontApp('user', null, $placeholder)) {
						include APPS_DIR.'user'.DS.'admin'.DS.'model.php';
						$userModel = new UserAdminModel();
						if (!$userModel->createUser($user)) {
							$this->view->error('installer', $data['installer'], 'Fatal Error', 'Impossible to create your administrator account. Please, check your database credentials.');
							return;
						}
					} else {
						$this->view->error('installer', $data['installer'], 'Fatal Error', 'The User application required by the system cannot be found. Please, download a complete package of WityCMS.');
						return;
					}

					// Save General configuration
					WConfig::set('config.base', trim($config['base'], '/'));
					WConfig::set('config.site_name', $config['site_name']);
					WConfig::set('config.theme', $config['theme']);
					WConfig::set('config.lang', $config['language']);
					WConfig::set('config.timezone', $config['timezone']);
					WConfig::set('config.email', $user['email']);
					WConfig::set('config.debug', false);
					WConfig::save('config', CONFIG_DIR.'config.php');

					// Save Route configuration
					WConfig::set('route.default_front', $route['front_app']);
					WConfig::set('route.default_admin', $route['admin_app']);
					WConfig::save('route', CONFIG_DIR.'route.php');

					// If success, Delete installer directory
					if (file_exists(CONFIG_DIR.'config.php') && file_exists(CONFIG_DIR.'database.php') && file_exists(CONFIG_DIR.'route.php')) {
						$dir = WITY_PATH.'installer';
						$it = new RecursiveDirectoryIterator($dir);
						$files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
						foreach ($files as $file) {
							if ($file->getFilename() === '.' || $file->getFilename() === '..') {
								continue;
							}
							if ($file->isDir()) {
								rmdir($file->getRealPath());
							} else {
								unlink($file->getRealPath());
							}
						}
						rmdir($dir);

						$this->view->success('installer', $data['installer'], 'Congratulations', 'Installation finished !');
					} else {
						$this->view->error('installer', $data['installer'], 'Fatal Error', 'Data submitted cannot be validated. Please, restart the installation and fill in the form again.');
						return;
					}
				} else {
					$this->view->error('installer', $data['installer'], 'Fatal Error', 'Data submitted cannot be validated. Please, restart the installation and fill in the form again.');
					return;
{'fraction_non_alphanumeric': 0.18968947322895943, 'fraction_numerical': 0.004411383098347894, 'mean_word_length': 3.886728655959425, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '625737', 'n_tokens_mistral': 4358, 'n_tokens_neox': 3487, 'n_words': 802}