install/includes/cli-core.php
changeset 1227 bdac73ed481e
parent 1183 15957df91ea4
child 1351 a57727e67241
equal deleted inserted replaced
1226:de56132c008d 1227:bdac73ed481e
    16  */
    16  */
    17 
    17 
    18 require(dirname(__FILE__) . '/common.php');
    18 require(dirname(__FILE__) . '/common.php');
    19 if ( !defined('ENANO_CLI') )
    19 if ( !defined('ENANO_CLI') )
    20 {
    20 {
    21   $ui = new Enano_Installer_UI('Enano installation', false);
    21 	$ui = new Enano_Installer_UI('Enano installation', false);
    22   $ui->set_visible_stage($ui->add_stage('Error', true));
    22 	$ui->set_visible_stage($ui->add_stage('Error', true));
    23 
    23 
    24   $ui->step = 'Access denied';  
    24 	$ui->step = 'Access denied';  
    25   $ui->show_header();
    25 	$ui->show_header();
    26   echo '<h2>CLI only</h2>
    26 	echo '<h2>CLI only</h2>
    27         <p>This script must be run from the command line.</p>';
    27 				<p>This script must be run from the command line.</p>';
    28   $ui->show_footer();
    28 	$ui->show_footer();
    29   exit;
    29 	exit;
    30 }
    30 }
    31 
    31 
    32 if ( defined('ENANO_INSTALLED') )
    32 if ( defined('ENANO_INSTALLED') )
    33 {
    33 {
    34   // start up the API to let it error out if something's wrong
    34 	// start up the API to let it error out if something's wrong
    35   require(ENANO_ROOT . '/includes/common.php');
    35 	require(ENANO_ROOT . '/includes/common.php');
    36   
    36 	
    37   installer_fail('Enano is already installed. Uninstall it by deleting config.php and creating a blank file called config.new.php.');
    37 	installer_fail('Enano is already installed. Uninstall it by deleting config.php and creating a blank file called config.new.php.');
    38 }
    38 }
    39 
    39 
    40 // parse command line args
    40 // parse command line args
    41 foreach ( array('silent', 'driver', 'dbhost', 'dbport', 'dbuser', 'dbpasswd', 'dbname', 'db_prefix', 'user', 'pass', 'email', 'sitename', 'sitedesc', 'copyright', 'urlscheme', 'lang_id', 'scriptpath') as $var )
    41 foreach ( array('silent', 'driver', 'dbhost', 'dbport', 'dbuser', 'dbpasswd', 'dbname', 'db_prefix', 'user', 'pass', 'email', 'sitename', 'sitedesc', 'copyright', 'urlscheme', 'lang_id', 'scriptpath') as $var )
    42 {
    42 {
    43   if ( !isset($$var) )
    43 	if ( !isset($$var) )
    44   {
    44 	{
    45     $$var = false;
    45 		$$var = false;
    46   }
    46 	}
    47 }
    47 }
    48 
    48 
    49 for ( $i = 1; $i < count($argv); $i++ )
    49 for ( $i = 1; $i < count($argv); $i++ )
    50 {
    50 {
    51   switch($argv[$i])
    51 	switch($argv[$i])
    52   {
    52 	{
    53     case '-q':
    53 		case '-q':
    54       $silent = true;
    54 			$silent = true;
    55       break;
    55 			break;
    56     case '--db-driver':
    56 		case '--db-driver':
    57     case '-b':
    57 		case '-b':
    58       $driver = @$argv[++$i];
    58 			$driver = @$argv[++$i];
    59       break;
    59 			break;
    60     case '--db-host':
    60 		case '--db-host':
    61     case '-h':
    61 		case '-h':
    62       $dbhost = @$argv[++$i];
    62 			$dbhost = @$argv[++$i];
    63       break;
    63 			break;
    64     case '--db-port':
    64 		case '--db-port':
    65     case '-o':
    65 		case '-o':
    66       $dbport = @$argv[++$i];
    66 			$dbport = @$argv[++$i];
    67       break;
    67 			break;
    68     case '--db-user':
    68 		case '--db-user':
    69     case '-u':
    69 		case '-u':
    70       $dbuser = @$argv[++$i];
    70 			$dbuser = @$argv[++$i];
    71       break;
    71 			break;
    72     case '--db-pass':
    72 		case '--db-pass':
    73     case '-p':
    73 		case '-p':
    74       $dbpasswd = @$argv[++$i];
    74 			$dbpasswd = @$argv[++$i];
    75       break;
    75 			break;
    76     case '--db-name':
    76 		case '--db-name':
    77     case '-d':
    77 		case '-d':
    78       $dbname = @$argv[++$i];
    78 			$dbname = @$argv[++$i];
    79       break;
    79 			break;
    80     case '--table-prefix':
    80 		case '--table-prefix':
    81     case '-t':
    81 		case '-t':
    82       $db_prefix = @$argv[++$i];
    82 			$db_prefix = @$argv[++$i];
    83       break;
    83 			break;
    84     case '--admin-user':
    84 		case '--admin-user':
    85     case '-a':
    85 		case '-a':
    86       $user = @$argv[++$i];
    86 			$user = @$argv[++$i];
    87       break;
    87 			break;
    88     case '--admin-pass':
    88 		case '--admin-pass':
    89     case '-w':
    89 		case '-w':
    90       $pass = @$argv[++$i];
    90 			$pass = @$argv[++$i];
    91       break;
    91 			break;
    92     case '--admin-email':
    92 		case '--admin-email':
    93     case '-e':
    93 		case '-e':
    94       $email = @$argv[++$i];
    94 			$email = @$argv[++$i];
    95       break;
    95 			break;
    96     case '--site-name':
    96 		case '--site-name':
    97     case '-n':
    97 		case '-n':
    98       $sitename = @$argv[++$i];
    98 			$sitename = @$argv[++$i];
    99       break;
    99 			break;
   100     case '--site-desc':
   100 		case '--site-desc':
   101     case '-s':
   101 		case '-s':
   102       $sitedesc = @$argv[++$i];
   102 			$sitedesc = @$argv[++$i];
   103       break;
   103 			break;
   104     case '--copyright':
   104 		case '--copyright':
   105     case '-c':
   105 		case '-c':
   106       $copyright = @$argv[++$i];
   106 			$copyright = @$argv[++$i];
   107       break;
   107 			break;
   108     case '--url-scheme':
   108 		case '--url-scheme':
   109     case '-r':
   109 		case '-r':
   110       $urlscheme_temp = @$argv[++$i];
   110 			$urlscheme_temp = @$argv[++$i];
   111       if ( in_array($urlscheme_temp, array('standard', 'short', 'rewrite', 'tiny')) )
   111 			if ( in_array($urlscheme_temp, array('standard', 'short', 'rewrite', 'tiny')) )
   112         $urlscheme = $urlscheme_temp;
   112 				$urlscheme = $urlscheme_temp;
   113       break;
   113 			break;
   114     case '--language':
   114 		case '--language':
   115     case '-l':
   115 		case '-l':
   116       $lang_id = @$argv[++$i];
   116 			$lang_id = @$argv[++$i];
   117       break;
   117 			break;
   118     case '-i':
   118 		case '-i':
   119     case '--scriptpath':
   119 		case '--scriptpath':
   120       $scriptpath = @$argv[++$i];
   120 			$scriptpath = @$argv[++$i];
   121       break;
   121 			break;
   122     default:
   122 		default:
   123       $vers = installer_enano_version();
   123 			$vers = installer_enano_version();
   124       echo <<<EOF
   124 			echo <<<EOF
   125 Enano CMS v$vers - CLI Installer
   125 Enano CMS v$vers - CLI Installer
   126 Usage: {$argv[0]} [-q] [-b driver] [-h host] [-u username] [-p password]
   126 Usage: {$argv[0]} [-q] [-b driver] [-h host] [-u username] [-p password]
   127                   [-d database] [-a adminuser] [-w adminpass] [-e email]
   127 									[-d database] [-a adminuser] [-w adminpass] [-e email]
   128 All arguments are optional; missing information will be prompted for.
   128 All arguments are optional; missing information will be prompted for.
   129   -q                Quiet mode (minimal output)
   129 	-q                Quiet mode (minimal output)
   130   -b, --db-driver   Database driver (mysql or postgresql)
   130 	-b, --db-driver   Database driver (mysql or postgresql)
   131   -h, --db-host     Hostname of database server
   131 	-h, --db-host     Hostname of database server
   132   -o, --db-port     TCP port on which to connect to database server
   132 	-o, --db-port     TCP port on which to connect to database server
   133   -u, --db-user     Username to use on database server
   133 	-u, --db-user     Username to use on database server
   134   -p, --db-pass     Password to use on database server
   134 	-p, --db-pass     Password to use on database server
   135   -d, --db-name     Name of database
   135 	-d, --db-name     Name of database
   136   -a, --admin-user  Administrator username
   136 	-a, --admin-user  Administrator username
   137   -w, --admin-pass  Administrator password
   137 	-w, --admin-pass  Administrator password
   138   -e, --admin-email Administrator e-mail address
   138 	-e, --admin-email Administrator e-mail address
   139   -n, --site-name   Name of site
   139 	-n, --site-name   Name of site
   140   -s, --site-desc   *SHORT* Description of site
   140 	-s, --site-desc   *SHORT* Description of site
   141   -c, --copyright   Copyright notice shown on pages
   141 	-c, --copyright   Copyright notice shown on pages
   142   -r, --url-scheme  URL scheme (standard, short, rewrite, or tiny)
   142 	-r, --url-scheme  URL scheme (standard, short, rewrite, or tiny)
   143   -l, --language    Language to be used on site and in installer
   143 	-l, --language    Language to be used on site and in installer
   144   -i, --scriptpath  Where Enano is relative to your website root (no trailing
   144 	-i, --scriptpath  Where Enano is relative to your website root (no trailing
   145                     slash)
   145 										slash)
   146 
   146 
   147 
   147 
   148 EOF;
   148 EOF;
   149       exit(1);
   149 			exit(1);
   150       break;
   150 			break;
   151   }
   151 	}
   152 }
   152 }
   153 
   153 
   154 if ( $silent )
   154 if ( $silent )
   155 {
   155 {
   156   define('ENANO_LIBINSTALL_SILENT', '');
   156 	define('ENANO_LIBINSTALL_SILENT', '');
   157 }
   157 }
   158 
   158 
   159 ##
   159 ##
   160 ## PHP VERSION CHECK
   160 ## PHP VERSION CHECK
   161 ##
   161 ##
   162 
   162 
   163 if ( version_compare(PHP_VERSION, '5.0.0', '<' ) )
   163 if ( version_compare(PHP_VERSION, '5.0.0', '<' ) )
   164 {
   164 {
   165   if ( !$silent )
   165 	if ( !$silent )
   166   {
   166 	{
   167     echo "\x1B[1mWelcome to the \x1B[34mEnano\x1B[0m CMS\x1B[1m installation wizard.\x1B[0m\n";
   167 		echo "\x1B[1mWelcome to the \x1B[34mEnano\x1B[0m CMS\x1B[1m installation wizard.\x1B[0m\n";
   168     echo "Installing Enano version \x1B[1m" . installer_enano_version() . "\x1B[0m on PHP " . PHP_VERSION . "\n";
   168 		echo "Installing Enano version \x1B[1m" . installer_enano_version() . "\x1B[0m on PHP " . PHP_VERSION . "\n";
   169   }
   169 	}
   170   installer_fail('Your version of PHP (' . PHP_VERSION . ') doesn\'t meet Enano requirements (5.0.0)');
   170 	installer_fail('Your version of PHP (' . PHP_VERSION . ') doesn\'t meet Enano requirements (5.0.0)');
   171 }
   171 }
   172 
   172 
   173 ##
   173 ##
   174 ## LANGUAGE STARTUP
   174 ## LANGUAGE STARTUP
   175 ##
   175 ##
   180 
   180 
   181 // Determine language ID to use
   181 // Determine language ID to use
   182 $langids = array_keys($languages);
   182 $langids = array_keys($languages);
   183 if ( $silent )
   183 if ( $silent )
   184 {
   184 {
   185   if ( !in_array($lang_id, $langids ) )
   185 	if ( !in_array($lang_id, $langids ) )
   186     $lang_id = $langids[0];
   186 		$lang_id = $langids[0];
   187 }
   187 }
   188 else if ( !in_array($lang_id, $langids) )
   188 else if ( !in_array($lang_id, $langids) )
   189 {
   189 {
   190   echo "\x1B[1mPlease select a language.\x1B[0m\n";
   190 	echo "\x1B[1mPlease select a language.\x1B[0m\n";
   191   echo "\x1B[32mAvailable languages:\x1B[0m\n";
   191 	echo "\x1B[32mAvailable languages:\x1B[0m\n";
   192   foreach ( $languages as $id => $metadata )
   192 	foreach ( $languages as $id => $metadata )
   193   {
   193 	{
   194     $id_spaced = $id;
   194 		$id_spaced = $id;
   195     while ( strlen($id_spaced) < 10 )
   195 		while ( strlen($id_spaced) < 10 )
   196       $id_spaced = "$id_spaced ";
   196 			$id_spaced = "$id_spaced ";
   197     echo "  \x1B[1;34m$id_spaced\x1B[0m {$metadata['name']} ({$metadata['name_eng']})\n";
   197 		echo "  \x1B[1;34m$id_spaced\x1B[0m {$metadata['name']} ({$metadata['name_eng']})\n";
   198   }
   198 	}
   199   while ( !in_array($lang_id, $langids) )
   199 	while ( !in_array($lang_id, $langids) )
   200   {
   200 	{
   201     $lang_id = cli_prompt('Language: ', $langids[0]);
   201 		$lang_id = cli_prompt('Language: ', $langids[0]);
   202   }
   202 	}
   203 }
   203 }
   204 
   204 
   205 // We have a language ID - init language
   205 // We have a language ID - init language
   206 $language_dir = $languages[$lang_id]['dir'];
   206 $language_dir = $languages[$lang_id]['dir'];
   207 
   207 
   213 ## WELCOME MESSAGE
   213 ## WELCOME MESSAGE
   214 ##
   214 ##
   215 
   215 
   216 if ( !$silent )
   216 if ( !$silent )
   217 {
   217 {
   218   echo parse_shellcolor_string($lang->get('cli_welcome_line1'));
   218 	echo parse_shellcolor_string($lang->get('cli_welcome_line1'));
   219   echo parse_shellcolor_string($lang->get('cli_welcome_line2', array('enano_version' => installer_enano_version(), 'php_version' => PHP_VERSION)));
   219 	echo parse_shellcolor_string($lang->get('cli_welcome_line2', array('enano_version' => installer_enano_version(), 'php_version' => PHP_VERSION)));
   220 }
   220 }
   221 
   221 
   222 $defaults = array(
   222 $defaults = array(
   223   'driver'  => 'mysql',
   223 	'driver'  => 'mysql',
   224   'dbhost'    => 'localhost',
   224 	'dbhost'    => 'localhost',
   225   'dbport'    => 3306,
   225 	'dbport'    => 3306,
   226   'dbuser'    => false,
   226 	'dbuser'    => false,
   227   'dbpasswd'  => false,
   227 	'dbpasswd'  => false,
   228   'dbname'    => false,
   228 	'dbname'    => false,
   229   'db_prefix'    => '',
   229 	'db_prefix'    => '',
   230   'user'      => 'admin',
   230 	'user'      => 'admin',
   231   'pass'      => false,
   231 	'pass'      => false,
   232   'email'     => false,
   232 	'email'     => false,
   233   'sitename'  => $lang->get('cli_default_site_name'),
   233 	'sitename'  => $lang->get('cli_default_site_name'),
   234   'sitedesc'  => $lang->get('cli_default_site_desc'),
   234 	'sitedesc'  => $lang->get('cli_default_site_desc'),
   235   'copyright' => $lang->get('cli_default_copyright', array('year' => date('Y'))),
   235 	'copyright' => $lang->get('cli_default_copyright', array('year' => date('Y'))),
   236   'urlscheme' => 'standard',
   236 	'urlscheme' => 'standard',
   237   'scriptpath'=> '/enano'
   237 	'scriptpath'=> '/enano'
   238 );
   238 );
   239 
   239 
   240 $terms = array(
   240 $terms = array(
   241   'driver'  => $lang->get('cli_prompt_driver'),
   241 	'driver'  => $lang->get('cli_prompt_driver'),
   242   'dbhost'    => $lang->get('cli_prompt_dbhost'),
   242 	'dbhost'    => $lang->get('cli_prompt_dbhost'),
   243   'dbport'    => $lang->get('cli_prompt_dbport'),
   243 	'dbport'    => $lang->get('cli_prompt_dbport'),
   244   'dbuser'    => $lang->get('cli_prompt_dbuser'),
   244 	'dbuser'    => $lang->get('cli_prompt_dbuser'),
   245   'dbpasswd'  => $lang->get('cli_prompt_dbpasswd'),
   245 	'dbpasswd'  => $lang->get('cli_prompt_dbpasswd'),
   246   'dbname'    => $lang->get('cli_prompt_dbname'),
   246 	'dbname'    => $lang->get('cli_prompt_dbname'),
   247   'db_prefix'    => $lang->get('cli_prompt_db_prefix'),
   247 	'db_prefix'    => $lang->get('cli_prompt_db_prefix'),
   248   'user'      => $lang->get('cli_prompt_user'),
   248 	'user'      => $lang->get('cli_prompt_user'),
   249   'pass'      => $lang->get('cli_prompt_pass'),
   249 	'pass'      => $lang->get('cli_prompt_pass'),
   250   'email'     => $lang->get('cli_prompt_email'),
   250 	'email'     => $lang->get('cli_prompt_email'),
   251   'sitename'  => $lang->get('cli_prompt_sitename'),
   251 	'sitename'  => $lang->get('cli_prompt_sitename'),
   252   'sitedesc'  => $lang->get('cli_prompt_sitedesc'),
   252 	'sitedesc'  => $lang->get('cli_prompt_sitedesc'),
   253   'copyright' => $lang->get('cli_prompt_copyright'),
   253 	'copyright' => $lang->get('cli_prompt_copyright'),
   254   'urlscheme' => $lang->get('cli_prompt_urlscheme'),
   254 	'urlscheme' => $lang->get('cli_prompt_urlscheme'),
   255   'scriptpath'=> $lang->get('cli_prompt_scriptpath')
   255 	'scriptpath'=> $lang->get('cli_prompt_scriptpath')
   256 );
   256 );
   257 
   257 
   258 $defaults['dbport'] = ( strtolower($driver) == 'postgresql' ) ? 5432 : 3306;
   258 $defaults['dbport'] = ( strtolower($driver) == 'postgresql' ) ? 5432 : 3306;
   259 
   259 
   260 foreach ( array('driver', 'dbhost', 'dbport', 'dbuser', 'dbpasswd', 'dbname', 'db_prefix', 'scriptpath', 'user', 'pass', 'email', 'sitename', 'sitedesc', 'copyright', 'urlscheme') as $var )
   260 foreach ( array('driver', 'dbhost', 'dbport', 'dbuser', 'dbpasswd', 'dbname', 'db_prefix', 'scriptpath', 'user', 'pass', 'email', 'sitename', 'sitedesc', 'copyright', 'urlscheme') as $var )
   261 {
   261 {
   262   if ( empty($$var) )
   262 	if ( empty($$var) )
   263   {
   263 	{
   264     switch($var)
   264 		switch($var)
   265     {
   265 		{
   266       default:
   266 			default:
   267         $$var = cli_prompt($terms[$var], $defaults[$var]);
   267 				$$var = cli_prompt($terms[$var], $defaults[$var]);
   268         break;
   268 				break;
   269       case 'driver':
   269 			case 'driver':
   270         $$var = cli_prompt($terms[$var], $defaults[$var]);
   270 				$$var = cli_prompt($terms[$var], $defaults[$var]);
   271         $defaults['dbport'] = ( strtolower($driver) == 'postgresql' ) ? 5432 : 3306;
   271 				$defaults['dbport'] = ( strtolower($driver) == 'postgresql' ) ? 5432 : 3306;
   272         break;
   272 				break;
   273       case 'pass':
   273 			case 'pass':
   274       case 'dbpasswd':
   274 			case 'dbpasswd':
   275         if ( @file_exists('/bin/stty') && @is_executable('/bin/stty') )
   275 				if ( @file_exists('/bin/stty') && @is_executable('/bin/stty') )
   276         {
   276 				{
   277           exec('/bin/stty -echo');
   277 					exec('/bin/stty -echo');
   278           while ( true )
   278 					while ( true )
   279           {
   279 					{
   280             $$var = cli_prompt($terms[$var], $defaults[$var]);
   280 						$$var = cli_prompt($terms[$var], $defaults[$var]);
   281             echo "\n";
   281 						echo "\n";
   282             $confirm = cli_prompt($lang->get('cli_prompt_confirm'), $defaults[$var]);
   282 						$confirm = cli_prompt($lang->get('cli_prompt_confirm'), $defaults[$var]);
   283             echo "\n";
   283 						echo "\n";
   284             if ( $$var === $confirm )
   284 						if ( $$var === $confirm )
   285               break;
   285 							break;
   286             else
   286 						else
   287               echo parse_shellcolor_string($lang->get('cli_err_pass_no_match'));
   287 							echo parse_shellcolor_string($lang->get('cli_err_pass_no_match'));
   288           }
   288 					}
   289           exec('/bin/stty echo');
   289 					exec('/bin/stty echo');
   290         }
   290 				}
   291         else
   291 				else
   292         {
   292 				{
   293           $$var = cli_prompt("{$terms[$var]} " . $lang->get('cli_msg_echo_warning'), $defaults[$var]);
   293 					$$var = cli_prompt("{$terms[$var]} " . $lang->get('cli_msg_echo_warning'), $defaults[$var]);
   294         }
   294 				}
   295         break;
   295 				break;
   296       case 'urlscheme':
   296 			case 'urlscheme':
   297         $temp = '';
   297 				$temp = '';
   298         while ( !in_array($temp, array('standard', 'short', 'rewrite', 'tiny')) )
   298 				while ( !in_array($temp, array('standard', 'short', 'rewrite', 'tiny')) )
   299         {
   299 				{
   300           $temp = cli_prompt($terms[$var], $defaults[$var]);
   300 					$temp = cli_prompt($terms[$var], $defaults[$var]);
   301         }
   301 				}
   302         $$var = $temp;
   302 				$$var = $temp;
   303         break;
   303 				break;
   304       case 'db_prefix':
   304 			case 'db_prefix':
   305         while ( !preg_match('/^[a-z0-9_]*$/', $$var) )
   305 				while ( !preg_match('/^[a-z0-9_]*$/', $$var) )
   306         {
   306 				{
   307           $$var = cli_prompt($terms[$var], $defaults[$var]);
   307 					$$var = cli_prompt($terms[$var], $defaults[$var]);
   308         }
   308 				}
   309         break;
   309 				break;
   310       case 'dbport':
   310 			case 'dbport':
   311         $$var = cli_prompt($terms[$var], strval($defaults[$var]));
   311 				$$var = cli_prompt($terms[$var], strval($defaults[$var]));
   312         while ( !preg_match('/^[0-9]*$/', $$var) )
   312 				while ( !preg_match('/^[0-9]*$/', $$var) )
   313         {
   313 				{
   314           $$var = cli_prompt($terms[$var], $defaults[$var]);
   314 					$$var = cli_prompt($terms[$var], $defaults[$var]);
   315         }
   315 				}
   316         $$var = intval($$var);
   316 				$$var = intval($$var);
   317         break;
   317 				break;
   318     }
   318 		}
   319   }
   319 	}
   320 }
   320 }
   321 
   321 
   322 ##
   322 ##
   323 ## DB TEST
   323 ## DB TEST
   324 ##
   324 ##
   326 require( ENANO_ROOT . '/includes/dbal.php' );
   326 require( ENANO_ROOT . '/includes/dbal.php' );
   327 require( ENANO_ROOT . '/includes/sql_parse.php' );
   327 require( ENANO_ROOT . '/includes/sql_parse.php' );
   328 $dbal = new $driver();
   328 $dbal = new $driver();
   329 
   329 
   330 if ( !$silent )
   330 if ( !$silent )
   331   echo parse_shellcolor_string($lang->get('cli_msg_testing_db'));
   331 	echo parse_shellcolor_string($lang->get('cli_msg_testing_db'));
   332 
   332 
   333 $result = $dbal->connect(true, $dbhost, $dbuser, $dbpasswd, $dbname, $dbport);
   333 $result = $dbal->connect(true, $dbhost, $dbuser, $dbpasswd, $dbname, $dbport);
   334 if ( !$result )
   334 if ( !$result )
   335 {
   335 {
   336   if ( !$silent )
   336 	if ( !$silent )
   337   {
   337 	{
   338     echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
   338 		echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
   339     echo "[$driver] " . $dbal->sql_error() . "\n";
   339 		echo "[$driver] " . $dbal->sql_error() . "\n";
   340   }
   340 	}
   341   installer_fail($lang->get('cli_err_db_connect_fail'));
   341 	installer_fail($lang->get('cli_err_db_connect_fail'));
   342 }
   342 }
   343 
   343 
   344 if ( !$silent )
   344 if ( !$silent )
   345   echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   345 	echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   346 
   346 
   347 ##
   347 ##
   348 ## SERVER REQUIREMENTS
   348 ## SERVER REQUIREMENTS
   349 ##
   349 ##
   350 
   350 
   351 if ( !$silent )
   351 if ( !$silent )
   352 {
   352 {
   353   echo parse_shellcolor_string($lang->get('cli_stage_sysreqs'));
   353 	echo parse_shellcolor_string($lang->get('cli_stage_sysreqs'));
   354 }
   354 }
   355 
   355 
   356 $failed = false;
   356 $failed = false;
   357 $warnings = array();
   357 $warnings = array();
   358 
   358 
   359 // Test: PHP
   359 // Test: PHP
   360 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_php') . ': ';
   360 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_php') . ': ';
   361 if ( version_compare(PHP_VERSION, '5.2.0', '>=') )
   361 if ( version_compare(PHP_VERSION, '5.2.0', '>=') )
   362 {
   362 {
   363   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   363 	if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   364 }
   364 }
   365 else if ( version_compare(PHP_VERSION, '5.0.0', '>=') )
   365 else if ( version_compare(PHP_VERSION, '5.0.0', '>=') )
   366 {
   366 {
   367   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_vwarn')) . "\n";
   367 	if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_vwarn')) . "\n";
   368   $warnings[] = $lang->get('sysreqs_req_help_php', array('php_version' => PHP_VERSION));
   368 	$warnings[] = $lang->get('sysreqs_req_help_php', array('php_version' => PHP_VERSION));
   369 }
   369 }
   370 else
   370 else
   371 {
   371 {
   372   $failed = true;
   372 	$failed = true;
   373   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
   373 	if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
   374 }
   374 }
   375 
   375 
   376 // Test: MySQL
   376 // Test: MySQL
   377 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_mysql') . ': ';
   377 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_mysql') . ': ';
   378 $req_mysql = function_exists('mysql_connect');
   378 $req_mysql = function_exists('mysql_connect');
   379 if ( $req_mysql )
   379 if ( $req_mysql )
   380 {
   380 {
   381   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   381 	if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   382   $have_dbms = true;
   382 	$have_dbms = true;
   383 }
   383 }
   384 else
   384 else
   385 {
   385 {
   386   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
   386 	if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
   387 }
   387 }
   388 
   388 
   389 // Test: PostgreSQL
   389 // Test: PostgreSQL
   390 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_postgresql') . ': ';
   390 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_postgresql') . ': ';
   391 $req_pgsql = function_exists('pg_connect');
   391 $req_pgsql = function_exists('pg_connect');
   392 if ( $req_pgsql )
   392 if ( $req_pgsql )
   393 {
   393 {
   394   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   394 	if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   395   $have_dbms = true;
   395 	$have_dbms = true;
   396 }
   396 }
   397 else
   397 else
   398 {
   398 {
   399   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
   399 	if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
   400 }
   400 }
   401 
   401 
   402 if ( !$have_dbms )
   402 if ( !$have_dbms )
   403   $failed = true;
   403 	$failed = true;
   404 
   404 
   405 // Test: Safe Mode
   405 // Test: Safe Mode
   406 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_safemode') . ': ';
   406 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_safemode') . ': ';
   407 $req_safemode = !intval(@ini_get('safe_mode'));
   407 $req_safemode = !intval(@ini_get('safe_mode'));
   408 if ( !$req_safemode )
   408 if ( !$req_safemode )
   409 {
   409 {
   410   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
   410 	if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
   411   $warnings[] = $lang->get('sysreqs_req_help_safemode');
   411 	$warnings[] = $lang->get('sysreqs_req_help_safemode');
   412   $failed = true;
   412 	$failed = true;
   413 }
   413 }
   414 else
   414 else
   415 {
   415 {
   416   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   416 	if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   417 }
   417 }
   418 
   418 
   419 // Test: File uploads
   419 // Test: File uploads
   420 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_uploads') . ': ';
   420 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_uploads') . ': ';
   421 $req_uploads = intval(@ini_get('file_uploads'));
   421 $req_uploads = intval(@ini_get('file_uploads'));
   422 if ( $req_uploads )
   422 if ( $req_uploads )
   423 {
   423 {
   424   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   424 	if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   425 }
   425 }
   426 else
   426 else
   427 {
   427 {
   428   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_warn')) . "\n";
   428 	if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_warn')) . "\n";
   429 }
   429 }
   430 
   430 
   431 // Test: ctype validation
   431 // Test: ctype validation
   432 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_ctype') . ': ';
   432 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_ctype') . ': ';
   433 $req_ctype = function_exists('ctype_digit');
   433 $req_ctype = function_exists('ctype_digit');
   434 if ( $req_ctype )
   434 if ( $req_ctype )
   435 {
   435 {
   436   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   436 	if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   437 }
   437 }
   438 else
   438 else
   439 {
   439 {
   440   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
   440 	if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
   441   $failed = true;
   441 	$failed = true;
   442 }
   442 }
   443 
   443 
   444 // Test: crypto
   444 // Test: crypto
   445 $crypto_backend = install_get_crypto_backend();
   445 $crypto_backend = install_get_crypto_backend();
   446 if ( !$silent )
   446 if ( !$silent )
   447 {
   447 {
   448   echo '  ' . $lang->get('sysreqs_req_crypto') . ': ';
   448 	echo '  ' . $lang->get('sysreqs_req_crypto') . ': ';
   449   switch($crypto_backend)
   449 	switch($crypto_backend)
   450   {
   450 	{
   451     case 'bcmath':
   451 		case 'bcmath':
   452       echo parse_shellcolor_string($lang->get('cli_test_warn') . " [<c 0;33>" . $lang->get("sysreqs_req_{$crypto_backend}") . "</c>]") . "\n";
   452 			echo parse_shellcolor_string($lang->get('cli_test_warn') . " [<c 0;33>" . $lang->get("sysreqs_req_{$crypto_backend}") . "</c>]") . "\n";
   453       $warnings[] = $lang->get('sysreqs_req_help_crypto_bcmath');
   453 			$warnings[] = $lang->get('sysreqs_req_help_crypto_bcmath');
   454       break;
   454 			break;
   455     case 'none':
   455 		case 'none':
   456       echo parse_shellcolor_string($lang->get('cli_test_warn') . " [<c 0;31>" . $lang->get("sysreqs_req_notfound") . "</c>]") . "\n";
   456 			echo parse_shellcolor_string($lang->get('cli_test_warn') . " [<c 0;31>" . $lang->get("sysreqs_req_notfound") . "</c>]") . "\n";
   457       $warnings[] = $lang->get('sysreqs_req_help_crypto_none');
   457 			$warnings[] = $lang->get('sysreqs_req_help_crypto_none');
   458       break;
   458 			break;
   459     default:
   459 		default:
   460       echo parse_shellcolor_string($lang->get('cli_test_pass') . " [<c 0;32>" . $lang->get("sysreqs_req_{$crypto_backend}") . "</c>]") . "\n";
   460 			echo parse_shellcolor_string($lang->get('cli_test_pass') . " [<c 0;32>" . $lang->get("sysreqs_req_{$crypto_backend}") . "</c>]") . "\n";
   461       break;
   461 			break;
   462   }
   462 	}
   463 }
   463 }
   464 
   464 
   465 // Write tests
   465 // Write tests
   466 $req_config_w = write_test('config.new.php');
   466 $req_config_w = write_test('config.new.php');
   467 $req_htaccess_w = write_test('.htaccess.new');
   467 $req_htaccess_w = write_test('.htaccess.new');
   472 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_htaccess_writable') . ': ' . parse_shellcolor_string($lang->get($req_htaccess_w ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
   472 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_htaccess_writable') . ': ' . parse_shellcolor_string($lang->get($req_htaccess_w ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
   473 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_files_writable') . ': ' . parse_shellcolor_string($lang->get($req_files_w ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
   473 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_files_writable') . ': ' . parse_shellcolor_string($lang->get($req_files_w ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
   474 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_cache_writable') . ': ' . parse_shellcolor_string($lang->get($req_cache_w ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
   474 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_cache_writable') . ': ' . parse_shellcolor_string($lang->get($req_cache_w ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
   475 
   475 
   476 if ( !$req_config_w || !$req_htaccess_w || !$req_files_w || !$req_cache_w )
   476 if ( !$req_config_w || !$req_htaccess_w || !$req_files_w || !$req_cache_w )
   477   $warnings[] = $lang->get('sysreqs_req_help_writable');
   477 	$warnings[] = $lang->get('sysreqs_req_help_writable');
   478 
   478 
   479 if ( !$req_config_w )
   479 if ( !$req_config_w )
   480   $failed = true;
   480 	$failed = true;
   481       
   481 			
   482 // Extension test: GD
   482 // Extension test: GD
   483 $req_gd = function_exists('imagecreatefrompng') && function_exists('getimagesize') && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled');
   483 $req_gd = function_exists('imagecreatefrompng') && function_exists('getimagesize') && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled');
   484 if ( !$req_gd )
   484 if ( !$req_gd )
   485   $warnings[] = $lang->get('sysreqs_req_help_gd2');
   485 	$warnings[] = $lang->get('sysreqs_req_help_gd2');
   486 
   486 
   487 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_gd2') . ': ' . parse_shellcolor_string($lang->get($req_gd ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
   487 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_gd2') . ': ' . parse_shellcolor_string($lang->get($req_gd ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
   488 
   488 
   489 // FS test: ImageMagick
   489 // FS test: ImageMagick
   490 $req_imagick = which('convert');
   490 $req_imagick = which('convert');
   491 if ( !$req_imagick )
   491 if ( !$req_imagick )
   492   $warnings[] = $lang->get('sysreqs_req_help_imagemagick');
   492 	$warnings[] = $lang->get('sysreqs_req_help_imagemagick');
   493 
   493 
   494 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_imagemagick') . ': ' . parse_shellcolor_string($lang->get($req_imagick ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
   494 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_imagemagick') . ': ' . parse_shellcolor_string($lang->get($req_imagick ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
   495 
   495 
   496 if ( !empty($warnings) && !$silent )
   496 if ( !empty($warnings) && !$silent )
   497 {
   497 {
   498   echo parse_shellcolor_string($lang->get('cli_msg_test_warnings')) . "\n";
   498 	echo parse_shellcolor_string($lang->get('cli_msg_test_warnings')) . "\n";
   499   echo "  " . implode("\n  ", $warnings) . "\n";
   499 	echo "  " . implode("\n  ", $warnings) . "\n";
   500 }
   500 }
   501 
   501 
   502 if ( !function_exists('mysql_connect') && !function_exists('pg_connect') )
   502 if ( !function_exists('mysql_connect') && !function_exists('pg_connect') )
   503 {
   503 {
   504   installer_fail($lang->get('cli_err_no_drivers'));
   504 	installer_fail($lang->get('cli_err_no_drivers'));
   505 }
   505 }
   506 if ( $failed )
   506 if ( $failed )
   507 {
   507 {
   508   installer_fail($lang->get('cli_err_sysreqs_fail'));
   508 	installer_fail($lang->get('cli_err_sysreqs_fail'));
   509 }
   509 }
   510 
   510 
   511 ##
   511 ##
   512 ## STAGE 1 INSTALLATION
   512 ## STAGE 1 INSTALLATION
   513 ##
   513 ##
   514 
   514 
   515 if ( !$silent )
   515 if ( !$silent )
   516 {
   516 {
   517   echo parse_shellcolor_string($lang->get('cli_msg_tests_passed'));
   517 	echo parse_shellcolor_string($lang->get('cli_msg_tests_passed'));
   518   echo parse_shellcolor_string($lang->get('cli_msg_installing_db_stage1'));
   518 	echo parse_shellcolor_string($lang->get('cli_msg_installing_db_stage1'));
   519 }
   519 }
   520 
   520 
   521 // Create the config table
   521 // Create the config table
   522 try
   522 try
   523 {
   523 {
   524   $sql_parser = new SQL_Parser( ENANO_ROOT . "/install/schemas/{$driver}_stage1.sql" );
   524 	$sql_parser = new SQL_Parser( ENANO_ROOT . "/install/schemas/{$driver}_stage1.sql" );
   525 }
   525 }
   526 catch ( Exception $e )
   526 catch ( Exception $e )
   527 {
   527 {
   528   if ( !$silent )
   528 	if ( !$silent )
   529     echo "\n";
   529 		echo "\n";
   530   installer_fail($lang->get('cli_err_schema_load'));
   530 	installer_fail($lang->get('cli_err_schema_load'));
   531 }
   531 }
   532 // Check to see if the config table already exists
   532 // Check to see if the config table already exists
   533 $q = $dbal->sql_query('SELECT config_name, config_value FROM ' . $db_prefix . 'config LIMIT 1;');
   533 $q = $dbal->sql_query('SELECT config_name, config_value FROM ' . $db_prefix . 'config LIMIT 1;');
   534 if ( !$q )
   534 if ( !$q )
   535 {
   535 {
   536   $sql_parser->assign_vars(array(
   536 	$sql_parser->assign_vars(array(
   537       'TABLE_PREFIX' => $db_prefix
   537 			'TABLE_PREFIX' => $db_prefix
   538     ));
   538 		));
   539   $sql = $sql_parser->parse();
   539 	$sql = $sql_parser->parse();
   540   foreach ( $sql as $q )
   540 	foreach ( $sql as $q )
   541   {
   541 	{
   542     if ( !$dbal->sql_query($q) )
   542 		if ( !$dbal->sql_query($q) )
   543     {
   543 		{
   544       if ( !$silent )
   544 			if ( !$silent )
   545         echo "\n";
   545 				echo "\n";
   546       echo "[$driver] " . $dbal->sql_error() . "\n";
   546 			echo "[$driver] " . $dbal->sql_error() . "\n";
   547       installer_fail($lang->get('cli_err_db_query'));
   547 			installer_fail($lang->get('cli_err_db_query'));
   548     }
   548 		}
   549   }
   549 	}
   550 }
   550 }
   551 else
   551 else
   552 {
   552 {
   553   $dbal->free_result();
   553 	$dbal->free_result();
   554   if ( !$dbal->sql_query('DELETE FROM ' . $db_prefix . 'config WHERE config_name = \'install_aes_key\';') )
   554 	if ( !$dbal->sql_query('DELETE FROM ' . $db_prefix . 'config WHERE config_name = \'install_aes_key\';') )
   555   {
   555 	{
   556     if ( !$silent )
   556 		if ( !$silent )
   557       echo "\n";
   557 			echo "\n";
   558     echo "[$driver] " . $dbal->sql_error() . "\n";
   558 		echo "[$driver] " . $dbal->sql_error() . "\n";
   559     installer_fail($lang->get('cli_err_db_query'));
   559 		installer_fail($lang->get('cli_err_db_query'));
   560   }
   560 	}
   561 }
   561 }
   562 
   562 
   563 if ( !$silent )
   563 if ( !$silent )
   564   echo parse_shellcolor_string($lang->get('cli_msg_ok')) . "\n";
   564 	echo parse_shellcolor_string($lang->get('cli_msg_ok')) . "\n";
   565 
   565 
   566 define('table_prefix', $db_prefix);
   566 define('table_prefix', $db_prefix);
   567 
   567 
   568 ##
   568 ##
   569 ## STAGE 2 INSTALLATION
   569 ## STAGE 2 INSTALLATION
   574 
   574 
   575 // Yes, I am predicting the future here. Because I have that kind of power.
   575 // Yes, I am predicting the future here. Because I have that kind of power.
   576 $_SERVER['REMOTE_ADDR'] = ( intval(date('Y')) >= 2011 ) ? '::1' : '127.0.0.1';
   576 $_SERVER['REMOTE_ADDR'] = ( intval(date('Y')) >= 2011 ) ? '::1' : '127.0.0.1';
   577 
   577 
   578 if ( !$silent )
   578 if ( !$silent )
   579   echo parse_shellcolor_string($lang->get('cli_msg_parsing_schema'));
   579 	echo parse_shellcolor_string($lang->get('cli_msg_parsing_schema'));
   580 
   580 
   581 require_once( ENANO_ROOT . '/includes/rijndael.php' );
   581 require_once( ENANO_ROOT . '/includes/rijndael.php' );
   582 require_once( ENANO_ROOT . '/includes/hmac.php' );
   582 require_once( ENANO_ROOT . '/includes/hmac.php' );
   583 
   583 
   584 $aes = AESCrypt::singleton(AES_BITS, AES_BLOCKSIZE);
   584 $aes = AESCrypt::singleton(AES_BITS, AES_BLOCKSIZE);
   589 
   589 
   590 unset($admin_pass_clean); // Security
   590 unset($admin_pass_clean); // Security
   591 
   591 
   592 try
   592 try
   593 {
   593 {
   594   $sql_parser = new SQL_Parser( ENANO_ROOT . "/install/schemas/{$dbdriver}_stage2.sql" );
   594 	$sql_parser = new SQL_Parser( ENANO_ROOT . "/install/schemas/{$dbdriver}_stage2.sql" );
   595 }
   595 }
   596 catch ( Exception $e )
   596 catch ( Exception $e )
   597 {
   597 {
   598   if ( !$silent )
   598 	if ( !$silent )
   599     echo "\n";
   599 		echo "\n";
   600   installer_fail($lang->get('cli_err_schema_load'));
   600 	installer_fail($lang->get('cli_err_schema_load'));
   601 }
   601 }
   602 
   602 
   603 $vars = array(
   603 $vars = array(
   604     'TABLE_PREFIX'         => table_prefix,
   604 		'TABLE_PREFIX'         => table_prefix,
   605     'SITE_NAME'            => $db->escape($sitename),
   605 		'SITE_NAME'            => $db->escape($sitename),
   606     'SITE_DESC'            => $db->escape($sitedesc),
   606 		'SITE_DESC'            => $db->escape($sitedesc),
   607     'COPYRIGHT'            => $db->escape($copyright),
   607 		'COPYRIGHT'            => $db->escape($copyright),
   608     'WIKI_MODE'            => '0',
   608 		'WIKI_MODE'            => '0',
   609     'ENABLE_CACHE'         => ( is_writable( ENANO_ROOT . '/cache/' ) ? '1' : '0' ),
   609 		'ENABLE_CACHE'         => ( is_writable( ENANO_ROOT . '/cache/' ) ? '1' : '0' ),
   610     'VERSION'              => installer_enano_version(),
   610 		'VERSION'              => installer_enano_version(),
   611     'ADMIN_USER'           => $db->escape($user),
   611 		'ADMIN_USER'           => $db->escape($user),
   612     'ADMIN_PASS'           => $admin_pass,
   612 		'ADMIN_PASS'           => $admin_pass,
   613     'ADMIN_PASS_SALT'      => $hmac_secret,
   613 		'ADMIN_PASS_SALT'      => $hmac_secret,
   614     'ADMIN_EMAIL'          => $db->escape($email),
   614 		'ADMIN_EMAIL'          => $db->escape($email),
   615     'REAL_NAME'            => '', // This has always been stubbed.
   615 		'REAL_NAME'            => '', // This has always been stubbed.
   616     'ADMIN_EMBED_PHP'      => strval(AUTH_DISALLOW),
   616 		'ADMIN_EMBED_PHP'      => strval(AUTH_DISALLOW),
   617     'UNIX_TIME'            => strval(time()),
   617 		'UNIX_TIME'            => strval(time()),
   618     'IP_ADDRESS'           => $_SERVER['REMOTE_ADDR']
   618 		'IP_ADDRESS'           => $_SERVER['REMOTE_ADDR']
   619   );
   619 	);
   620 
   620 
   621 $sql_parser->assign_vars($vars);
   621 $sql_parser->assign_vars($vars);
   622 $schema = $sql_parser->parse();
   622 $schema = $sql_parser->parse();
   623 
   623 
   624 if ( !$silent )
   624 if ( !$silent )
   625   echo parse_shellcolor_string($lang->get('cli_msg_ok')) . "\n";
   625 	echo parse_shellcolor_string($lang->get('cli_msg_ok')) . "\n";
   626 
   626 
   627 ##
   627 ##
   628 ## PAYLOAD DELIVERY
   628 ## PAYLOAD DELIVERY
   629 ##
   629 ##
   630 
   630 
   631 if ( !$silent )
   631 if ( !$silent )
   632   echo parse_shellcolor_string($lang->get('cli_msg_installing_db_stage2'));
   632 	echo parse_shellcolor_string($lang->get('cli_msg_installing_db_stage2'));
   633 
   633 
   634 foreach ( $schema as $sql )
   634 foreach ( $schema as $sql )
   635 {
   635 {
   636   if ( !$db->check_query($sql) )
   636 	if ( !$db->check_query($sql) )
   637   {
   637 	{
   638     if ( !$silent )
   638 		if ( !$silent )
   639       echo "\n";
   639 			echo "\n";
   640     installer_fail($lang->get('cli_err_query_sanity_failed'));
   640 		installer_fail($lang->get('cli_err_query_sanity_failed'));
   641   }
   641 	}
   642 }
   642 }
   643 
   643 
   644 foreach ( $schema as $sql )
   644 foreach ( $schema as $sql )
   645 {
   645 {
   646   if ( !$db->sql_query($sql) )
   646 	if ( !$db->sql_query($sql) )
   647   {
   647 	{
   648     if ( !$silent )
   648 		if ( !$silent )
   649       echo "\n";
   649 			echo "\n";
   650     echo "[$dbdriver] " . $db->sql_error() . "\n";
   650 		echo "[$dbdriver] " . $db->sql_error() . "\n";
   651     installer_fail($lang->get('cli_err_db_query'));
   651 		installer_fail($lang->get('cli_err_db_query'));
   652   }
   652 	}
   653 }
   653 }
   654 
   654 
   655 if ( !$silent )
   655 if ( !$silent )
   656   echo parse_shellcolor_string($lang->get('cli_msg_ok')) . "\n";
   656 	echo parse_shellcolor_string($lang->get('cli_msg_ok')) . "\n";
   657 
   657 
   658 ##
   658 ##
   659 ## CONFIG FILE GENERATION
   659 ## CONFIG FILE GENERATION
   660 ##
   660 ##
   661 
   661 
   662 require_once( ENANO_ROOT . '/install/includes/payload.php' );
   662 require_once( ENANO_ROOT . '/install/includes/payload.php' );
   663 require_once( ENANO_ROOT . '/install/includes/libenanoinstallcli.php' );
   663 require_once( ENANO_ROOT . '/install/includes/libenanoinstallcli.php' );
   664 define('scriptPath', $scriptpath);
   664 define('scriptPath', $scriptpath);
   665 $urlscheme = strtr($urlscheme, array(
   665 $urlscheme = strtr($urlscheme, array(
   666   'short' => 'shortened'
   666 	'short' => 'shortened'
   667 ));
   667 ));
   668 $_POST['url_scheme'] =& $urlscheme;
   668 $_POST['url_scheme'] =& $urlscheme;
   669 
   669 
   670 run_installer_stage('writeconfig', 'writing_config', 'stg_write_config', 'install_stg_writeconfig_body');
   670 run_installer_stage('writeconfig', 'writing_config', 'stg_write_config', 'install_stg_writeconfig_body');
   671 
   671 
   672 ##
   672 ##
   673 ## FINAL STAGES
   673 ## FINAL STAGES
   674 ##
   674 ##
   675 
   675 
   676 if ( !$silent )
   676 if ( !$silent )
   677   echo parse_shellcolor_string($lang->get('cli_msg_starting_api'));
   677 	echo parse_shellcolor_string($lang->get('cli_msg_starting_api'));
   678 
   678 
   679 // Start up the Enano API
   679 // Start up the Enano API
   680 $db->close();
   680 $db->close();
   681 @define('ENANO_ALLOW_LOAD_NOLANG', 1);
   681 @define('ENANO_ALLOW_LOAD_NOLANG', 1);
   682 // If this fails, it fails hard.
   682 // If this fails, it fails hard.
   683 require(ENANO_ROOT . '/includes/common.php');
   683 require(ENANO_ROOT . '/includes/common.php');
   684 
   684 
   685 if ( !$silent )
   685 if ( !$silent )
   686   echo parse_shellcolor_string($lang->get('cli_msg_ok')) . "\n";
   686 	echo parse_shellcolor_string($lang->get('cli_msg_ok')) . "\n";
   687 
   687 
   688 $_POST['username'] =& $user;
   688 $_POST['username'] =& $user;
   689 $_POST['default_content_type'] = ( isset($start_with) ) ? $start_with : 'blank';
   689 $_POST['default_content_type'] = ( isset($start_with) ) ? $start_with : 'blank';
   690 
   690 
   691 run_installer_stage('importlang', 'importing_language', 'stg_language_setup', $lang->get('install_stg_importlang_body'));
   691 run_installer_stage('importlang', 'importing_language', 'stg_language_setup', $lang->get('install_stg_importlang_body'));
   695 run_installer_stage('buildindex', 'initting_index', 'stg_build_index', $lang->get('install_stg_buildindex_body'));
   695 run_installer_stage('buildindex', 'initting_index', 'stg_build_index', $lang->get('install_stg_buildindex_body'));
   696 run_installer_stage('renameconfig', 'renaming_config', 'stg_rename_config', $lang->get('install_stg_rename_body', array('mainpage_link' => scriptPath . '/index.php')));
   696 run_installer_stage('renameconfig', 'renaming_config', 'stg_rename_config', $lang->get('install_stg_rename_body', array('mainpage_link' => scriptPath . '/index.php')));
   697 
   697 
   698 if ( !$silent )
   698 if ( !$silent )
   699 {
   699 {
   700   echo parse_shellcolor_string($lang->get('cli_msg_install_success'));
   700 	echo parse_shellcolor_string($lang->get('cli_msg_install_success'));
   701 }
   701 }
   702 
   702 
   703 return true;
   703 return true;
   704 
   704 
   705 ##
   705 ##
   706 ## FUNCTIONS
   706 ## FUNCTIONS
   707 ##
   707 ##
   708 
   708 
   709 function cli_prompt($prompt, $default = false)
   709 function cli_prompt($prompt, $default = false)
   710 {
   710 {
   711   if ( is_string($default) )
   711 	if ( is_string($default) )
   712   {
   712 	{
   713     echo "$prompt [$default]: ";
   713 		echo "$prompt [$default]: ";
   714     $stdin = fopen('php://stdin', 'r');
   714 		$stdin = fopen('php://stdin', 'r');
   715     $input = trim(fgets($stdin, 1024));
   715 		$input = trim(fgets($stdin, 1024));
   716     fclose($stdin);
   716 		fclose($stdin);
   717     if ( empty($input) )
   717 		if ( empty($input) )
   718       return $default;
   718 			return $default;
   719     return $input;
   719 		return $input;
   720   }
   720 	}
   721   else
   721 	else
   722   {
   722 	{
   723     while ( true )
   723 		while ( true )
   724     {
   724 		{
   725       echo "$prompt: ";
   725 			echo "$prompt: ";
   726       $stdin = fopen('php://stdin', 'r');
   726 			$stdin = fopen('php://stdin', 'r');
   727       $input = trim(fgets($stdin, 1024));
   727 			$input = trim(fgets($stdin, 1024));
   728       fclose($stdin);
   728 			fclose($stdin);
   729       if ( !empty($input) )
   729 			if ( !empty($input) )
   730         return $input;
   730 				return $input;
   731     }
   731 		}
   732   }
   732 	}
   733 }
   733 }
   734 
   734 
   735 function run_test($evalme, $test, $description, $warnonly = false)
   735 function run_test($evalme, $test, $description, $warnonly = false)
   736 {
   736 {
   737   global $silent, $test_failed, $lang;
   737 	global $silent, $test_failed, $lang;
   738   if ( !$silent )
   738 	if ( !$silent )
   739     echo "$test: ";
   739 		echo "$test: ";
   740   $result = eval($evalme);
   740 	$result = eval($evalme);
   741   if ( $result )
   741 	if ( $result )
   742   {
   742 	{
   743     if ( !$silent )
   743 		if ( !$silent )
   744       echo parse_shellcolor_string($lang->get('cli_test_pass'));
   744 			echo parse_shellcolor_string($lang->get('cli_test_pass'));
   745   }
   745 	}
   746   else
   746 	else
   747   {
   747 	{
   748     if ( !$silent )
   748 		if ( !$silent )
   749       echo $warnonly ? parse_shellcolor_string($lang->get('cli_test_warn')) : parse_shellcolor_string($lang->get('cli_test_fail'));
   749 			echo $warnonly ? parse_shellcolor_string($lang->get('cli_test_warn')) : parse_shellcolor_string($lang->get('cli_test_fail'));
   750     if ( !$silent )
   750 		if ( !$silent )
   751       echo "\n" . preg_replace('/^/m', '  ', wordwrap(strip_tags($description)));
   751 			echo "\n" . preg_replace('/^/m', '  ', wordwrap(strip_tags($description)));
   752     if ( !$warnonly )
   752 		if ( !$warnonly )
   753       $test_failed = true;
   753 			$test_failed = true;
   754   }
   754 	}
   755   if ( !$silent )
   755 	if ( !$silent )
   756     echo "\n";
   756 		echo "\n";
   757 }
   757 }
   758 
   758 
   759 function installer_fail($message)
   759 function installer_fail($message)
   760 {
   760 {
   761   global $silent;
   761 	global $silent;
   762   if ( $silent )
   762 	if ( $silent )
   763     file_put_contents('php://stderr', "$message\n");
   763 		file_put_contents('php://stderr', "$message\n");
   764   else
   764 	else
   765     echo "\x1B[1;31m" . "Error:\x1B[0;1m $message\x1B[0m\n";
   765 		echo "\x1B[1;31m" . "Error:\x1B[0;1m $message\x1B[0m\n";
   766   exit(1);
   766 	exit(1);
   767 }
   767 }
   768 
   768 
   769 function config_write_test()
   769 function config_write_test()
   770 {
   770 {
   771   if ( !is_writable(ENANO_ROOT.'/config.new.php') )
   771 	if ( !is_writable(ENANO_ROOT.'/config.new.php') )
   772     return false;
   772 		return false;
   773   // We need to actually _open_ the file to make sure it can be written, because sometimes this fails even when is_writable() returns
   773 	// We need to actually _open_ the file to make sure it can be written, because sometimes this fails even when is_writable() returns
   774   // true on Windows/IIS servers. Don't ask me why.
   774 	// true on Windows/IIS servers. Don't ask me why.
   775   $h = @fopen( ENANO_ROOT . '/config.new.php', 'a+' );
   775 	$h = @fopen( ENANO_ROOT . '/config.new.php', 'a+' );
   776   if ( !$h )
   776 	if ( !$h )
   777     return false;
   777 		return false;
   778   fclose($h);
   778 	fclose($h);
   779   return true;
   779 	return true;
   780 }
   780 }
   781 
   781 
   782 function parse_shellcolor_string($str)
   782 function parse_shellcolor_string($str)
   783 {
   783 {
   784   // only compute this once (saves some CPU time)
   784 	// only compute this once (saves some CPU time)
   785   static $do_colors = null;
   785 	static $do_colors = null;
   786   if ( $do_colors === null )
   786 	if ( $do_colors === null )
   787   {
   787 	{
   788     $do_colors = ( isset($_SERVER['TERM']) && $_SERVER['TERM'] != 'dumb' );
   788 		$do_colors = ( isset($_SERVER['TERM']) && $_SERVER['TERM'] != 'dumb' );
   789   }
   789 	}
   790   
   790 	
   791   $expr = '/<c ((?:[0-9]+)(?:;[0-9]+)*)>([\w\W]*?)<\/c>/';
   791 	$expr = '/<c ((?:[0-9]+)(?:;[0-9]+)*)>([\w\W]*?)<\/c>/';
   792   while ( preg_match($expr, $str) )
   792 	while ( preg_match($expr, $str) )
   793     $str = $do_colors ? preg_replace($expr, "\x1B[\\1m\\2\x1B[0m", $str) : preg_replace($expr, "\\2", $str);
   793 		$str = $do_colors ? preg_replace($expr, "\x1B[\\1m\\2\x1B[0m", $str) : preg_replace($expr, "\\2", $str);
   794   
   794 	
   795   return $str;
   795 	return $str;
   796 }
   796 }
   797 
   797