install/includes/cli-core.php
changeset 857 f3a5a276208c
parent 818 12346be3ec3d
child 924 142a29b583f9
equal deleted inserted replaced
856:0b7ff06aad13 857:f3a5a276208c
    37   
    37   
    38   installer_fail('Enano is already installed. Uninstall it by deleting config.php and creating a blank file called config.new.php.');
    38   installer_fail('Enano is already installed. Uninstall it by deleting config.php and creating a blank file called config.new.php.');
    39 }
    39 }
    40 
    40 
    41 // parse command line args
    41 // parse command line args
    42 foreach ( array('silent', 'driver', 'dbhost', 'dbuser', 'dbpasswd', 'dbname', 'db_prefix', 'user', 'pass', 'email', 'sitename', 'sitedesc', 'copyright', 'urlscheme', 'lang_id', 'scriptpath') as $var )
    42 foreach ( array('silent', 'driver', 'dbhost', 'dbport', 'dbuser', 'dbpasswd', 'dbname', 'db_prefix', 'user', 'pass', 'email', 'sitename', 'sitedesc', 'copyright', 'urlscheme', 'lang_id', 'scriptpath') as $var )
    43 {
    43 {
    44   if ( !isset($$var) )
    44   if ( !isset($$var) )
    45   {
    45   {
    46     $$var = false;
    46     $$var = false;
    47   }
    47   }
    59       $driver = @$argv[++$i];
    59       $driver = @$argv[++$i];
    60       break;
    60       break;
    61     case '--db-host':
    61     case '--db-host':
    62     case '-h':
    62     case '-h':
    63       $dbhost = @$argv[++$i];
    63       $dbhost = @$argv[++$i];
       
    64       break;
       
    65     case '--db-port':
       
    66     case '-o':
       
    67       $dbport = @$argv[++$i];
    64       break;
    68       break;
    65     case '--db-user':
    69     case '--db-user':
    66     case '-u':
    70     case '-u':
    67       $dbuser = @$argv[++$i];
    71       $dbuser = @$argv[++$i];
    68       break;
    72       break;
   124                   [-d database] [-a adminuser] [-w adminpass] [-e email]
   128                   [-d database] [-a adminuser] [-w adminpass] [-e email]
   125 All arguments are optional; missing information will be prompted for.
   129 All arguments are optional; missing information will be prompted for.
   126   -q                Quiet mode (minimal output)
   130   -q                Quiet mode (minimal output)
   127   -b, --db-driver   Database driver (mysql or postgresql)
   131   -b, --db-driver   Database driver (mysql or postgresql)
   128   -h, --db-host     Hostname of database server
   132   -h, --db-host     Hostname of database server
       
   133   -o, --db-port     TCP port on which to connect to database server
   129   -u, --db-user     Username to use on database server
   134   -u, --db-user     Username to use on database server
   130   -p, --db-pass     Password to use on database server
   135   -p, --db-pass     Password to use on database server
   131   -d, --db-name     Name of database
   136   -d, --db-name     Name of database
   132   -a, --admin-user  Administrator username
   137   -a, --admin-user  Administrator username
   133   -w, --admin-pass  Administrator password
   138   -w, --admin-pass  Administrator password
   216 }
   221 }
   217 
   222 
   218 $defaults = array(
   223 $defaults = array(
   219   'driver'  => 'mysql',
   224   'driver'  => 'mysql',
   220   'dbhost'    => 'localhost',
   225   'dbhost'    => 'localhost',
       
   226   'dbport'    => 3306,
   221   'dbuser'    => false,
   227   'dbuser'    => false,
   222   'dbpasswd'  => false,
   228   'dbpasswd'  => false,
   223   'dbname'    => false,
   229   'dbname'    => false,
   224   'db_prefix'    => '',
   230   'db_prefix'    => '',
   225   'user'      => 'admin',
   231   'user'      => 'admin',
   233 );
   239 );
   234 
   240 
   235 $terms = array(
   241 $terms = array(
   236   'driver'  => $lang->get('cli_prompt_driver'),
   242   'driver'  => $lang->get('cli_prompt_driver'),
   237   'dbhost'    => $lang->get('cli_prompt_dbhost'),
   243   'dbhost'    => $lang->get('cli_prompt_dbhost'),
       
   244   'dbport'    => $lang->get('cli_prompt_dbport'),
   238   'dbuser'    => $lang->get('cli_prompt_dbuser'),
   245   'dbuser'    => $lang->get('cli_prompt_dbuser'),
   239   'dbpasswd'  => $lang->get('cli_prompt_dbpasswd'),
   246   'dbpasswd'  => $lang->get('cli_prompt_dbpasswd'),
   240   'dbname'    => $lang->get('cli_prompt_dbname'),
   247   'dbname'    => $lang->get('cli_prompt_dbname'),
   241   'db_prefix'    => $lang->get('cli_prompt_db_prefix'),
   248   'db_prefix'    => $lang->get('cli_prompt_db_prefix'),
   242   'user'      => $lang->get('cli_prompt_user'),
   249   'user'      => $lang->get('cli_prompt_user'),
   247   'copyright' => $lang->get('cli_prompt_copyright'),
   254   'copyright' => $lang->get('cli_prompt_copyright'),
   248   'urlscheme' => $lang->get('cli_prompt_urlscheme'),
   255   'urlscheme' => $lang->get('cli_prompt_urlscheme'),
   249   'scriptpath'=> $lang->get('cli_prompt_scriptpath')
   256   'scriptpath'=> $lang->get('cli_prompt_scriptpath')
   250 );
   257 );
   251 
   258 
   252 foreach ( array('driver', 'dbhost', 'dbuser', 'dbpasswd', 'dbname', 'db_prefix', 'scriptpath', 'user', 'pass', 'email', 'sitename', 'sitedesc', 'copyright', 'urlscheme') as $var )
   259 $defaults['dbport'] = ( strtolower($driver) == 'postgresql' ) ? 5432 : 3306;
       
   260 
       
   261 foreach ( array('driver', 'dbhost', 'dbport', 'dbuser', 'dbpasswd', 'dbname', 'db_prefix', 'scriptpath', 'user', 'pass', 'email', 'sitename', 'sitedesc', 'copyright', 'urlscheme') as $var )
   253 {
   262 {
   254   if ( empty($$var) )
   263   if ( empty($$var) )
   255   {
   264   {
   256     switch($var)
   265     switch($var)
   257     {
   266     {
   258       default:
   267       default:
   259         $$var = cli_prompt($terms[$var], $defaults[$var]);
   268         $$var = cli_prompt($terms[$var], $defaults[$var]);
       
   269         break;
       
   270       case 'driver':
       
   271         $$var = cli_prompt($terms[$var], $defaults[$var]);
       
   272         $defaults['dbport'] = ( strtolower($driver) == 'postgresql' ) ? 5432 : 3306;
   260         break;
   273         break;
   261       case 'pass':
   274       case 'pass':
   262       case 'dbpasswd':
   275       case 'dbpasswd':
   263         if ( @file_exists('/bin/stty') && @is_executable('/bin/stty') )
   276         if ( @file_exists('/bin/stty') && @is_executable('/bin/stty') )
   264         {
   277         {
   293         while ( !preg_match('/^[a-z0-9_]*$/', $$var) )
   306         while ( !preg_match('/^[a-z0-9_]*$/', $$var) )
   294         {
   307         {
   295           $$var = cli_prompt($terms[$var], $defaults[$var]);
   308           $$var = cli_prompt($terms[$var], $defaults[$var]);
   296         }
   309         }
   297         break;
   310         break;
       
   311       case 'dbport':
       
   312         $$var = cli_prompt($terms[$var], strval($defaults[$var]));
       
   313         while ( !preg_match('/^[0-9]*$/', $$var) )
       
   314         {
       
   315           $$var = cli_prompt($terms[$var], $defaults[$var]);
       
   316         }
       
   317         $$var = intval($$var);
       
   318         break;
   298     }
   319     }
   299   }
   320   }
   300 }
   321 }
   301 
   322 
   302 ##
   323 ##
   308 $dbal = new $driver();
   329 $dbal = new $driver();
   309 
   330 
   310 if ( !$silent )
   331 if ( !$silent )
   311   echo parse_shellcolor_string($lang->get('cli_msg_testing_db'));
   332   echo parse_shellcolor_string($lang->get('cli_msg_testing_db'));
   312 
   333 
   313 $result = $dbal->connect(true, $dbhost, $dbuser, $dbpasswd, $dbname);
   334 $result = $dbal->connect(true, $dbhost, $dbuser, $dbpasswd, $dbname, $dbport);
   314 if ( !$result )
   335 if ( !$result )
   315 {
   336 {
   316   if ( !$silent )
   337   if ( !$silent )
   317   {
   338   {
   318     echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
   339     echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
   331 if ( !$silent )
   352 if ( !$silent )
   332 {
   353 {
   333   echo parse_shellcolor_string($lang->get('cli_stage_sysreqs'));
   354   echo parse_shellcolor_string($lang->get('cli_stage_sysreqs'));
   334 }
   355 }
   335 
   356 
   336 $test_failed = false;
   357 $failed = false;
   337 
   358 $warnings = array();
   338 run_test('return version_compare(\'5.2.0\', PHP_VERSION, \'<=\');', $lang->get('sysreqs_req_php5'), $lang->get('sysreqs_req_desc_php5'), true);
   359 
   339 run_test('return function_exists(\'mysql_connect\');', $lang->get('sysreqs_req_mysql'), $lang->get('sysreqs_req_desc_mysql'), true);
   360 // Test: PHP
   340 run_test('return function_exists(\'pg_connect\');', $lang->get('sysreqs_req_postgres'), $lang->get('sysreqs_req_desc_postgres'), true);
   361 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_php') . ': ';
   341 run_test('return @ini_get(\'file_uploads\');', $lang->get('sysreqs_req_uploads'), $lang->get('sysreqs_req_desc_uploads') );
   362 if ( version_compare(PHP_VERSION, '5.2.0', '>=') )
   342 run_test('return config_write_test();', $lang->get('sysreqs_req_config'), $lang->get('sysreqs_req_desc_config') );
   363 {
   343 run_test('return file_exists(\'/usr/bin/convert\');', $lang->get('sysreqs_req_magick'), $lang->get('sysreqs_req_desc_magick'), true);
   364   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
   344 run_test('return is_writable(ENANO_ROOT.\'/cache/\');', $lang->get('sysreqs_req_cachewriteable'), $lang->get('sysreqs_req_desc_cachewriteable'), true);
   365 }
   345 run_test('return is_writable(ENANO_ROOT.\'/files/\');', $lang->get('sysreqs_req_fileswriteable'), $lang->get('sysreqs_req_desc_fileswriteable'), true);
   366 else if ( version_compare(PHP_VERSION, '5.0.0', '>=') )
       
   367 {
       
   368   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_vwarn')) . "\n";
       
   369   $warnings[] = $lang->get('sysreqs_req_help_php', array('php_version' => PHP_VERSION));
       
   370 }
       
   371 else
       
   372 {
       
   373   $failed = true;
       
   374   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
       
   375 }
       
   376 
       
   377 // Test: MySQL
       
   378 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_mysql') . ': ';
       
   379 $req_mysql = function_exists('mysql_connect');
       
   380 if ( $req_mysql )
       
   381 {
       
   382   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
       
   383   $have_dbms = true;
       
   384 }
       
   385 else
       
   386 {
       
   387   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
       
   388 }
       
   389 
       
   390 // Test: PostgreSQL
       
   391 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_postgresql') . ': ';
       
   392 $req_pgsql = function_exists('pg_connect');
       
   393 if ( $req_pgsql )
       
   394 {
       
   395   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
       
   396   $have_dbms = true;
       
   397 }
       
   398 else
       
   399 {
       
   400   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
       
   401 }
       
   402 
       
   403 if ( !$have_dbms )
       
   404   $failed = true;
       
   405 
       
   406 // Test: Safe Mode
       
   407 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_safemode') . ': ';
       
   408 $req_safemode = !intval(@ini_get('safe_mode'));
       
   409 if ( !$req_safemode )
       
   410 {
       
   411   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
       
   412   $warnings[] = $lang->get('sysreqs_req_help_safemode');
       
   413   $failed = true;
       
   414 }
       
   415 else
       
   416 {
       
   417   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
       
   418 }
       
   419 
       
   420 // Test: File uploads
       
   421 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_uploads') . ': ';
       
   422 $req_uploads = intval(@ini_get('file_uploads'));
       
   423 if ( $req_uploads )
       
   424 {
       
   425   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
       
   426 }
       
   427 else
       
   428 {
       
   429   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_warn')) . "\n";
       
   430 }
       
   431 
       
   432 // Test: ctype validation
       
   433 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_ctype') . ': ';
       
   434 $req_ctype = function_exists('ctype_digit');
       
   435 if ( $req_ctype )
       
   436 {
       
   437   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_pass')) . "\n";
       
   438 }
       
   439 else
       
   440 {
       
   441   if ( !$silent ) echo parse_shellcolor_string($lang->get('cli_test_fail')) . "\n";
       
   442   $failed = true;
       
   443 }
       
   444 
       
   445 // Write tests
       
   446 $req_config_w = write_test('config.new.php');
       
   447 $req_htaccess_w = write_test('.htaccess.new');
       
   448 $req_files_w = write_test('files');
       
   449 $req_cache_w = write_test('cache');
       
   450 
       
   451 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_config_writable') . ': ' . parse_shellcolor_string($lang->get($req_config_w ? 'cli_test_pass' : 'cli_test_fail')) . "\n";
       
   452 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_htaccess_writable') . ': ' . parse_shellcolor_string($lang->get($req_htaccess_w ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
       
   453 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_files_writable') . ': ' . parse_shellcolor_string($lang->get($req_files_w ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
       
   454 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_cache_writable') . ': ' . parse_shellcolor_string($lang->get($req_cache_w ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
       
   455 
       
   456 if ( !$req_config_w || !$req_htaccess_w || !$req_files_w || !$req_cache_w )
       
   457   $warnings[] = $lang->get('sysreqs_req_help_writable');
       
   458 
       
   459 if ( !$req_config_w )
       
   460   $failed = true;
       
   461       
       
   462 // Extension test: GD
       
   463 $req_gd = function_exists('imagecreatefrompng') && function_exists('getimagesize') && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled');
       
   464 if ( !$req_gd )
       
   465   $warnings[] = $lang->get('sysreqs_req_help_gd2');
       
   466 
       
   467 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_gd2') . ': ' . parse_shellcolor_string($lang->get($req_gd ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
       
   468 
       
   469 // FS test: ImageMagick
       
   470 $req_imagick = which('convert');
       
   471 if ( !$req_imagick )
       
   472   $warnings[] = $lang->get('sysreqs_req_help_imagemagick');
       
   473 
       
   474 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_imagemagick') . ': ' . parse_shellcolor_string($lang->get($req_imagick ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
       
   475 
       
   476 // Extension test: GMP
       
   477 $req_gmp = function_exists('gmp_init');
       
   478 if ( !$req_gmp )
       
   479   $warnings[] = $lang->get('sysreqs_req_help_gmp');
       
   480 
       
   481 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_gmp') . ': ' . parse_shellcolor_string($lang->get($req_gmp ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
       
   482 
       
   483 // Extension test: Big_Int
       
   484 $req_bigint = function_exists('bi_from_str');
       
   485 if ( !$req_bigint && !$req_gmp )
       
   486   $warnings[] = $lang->get('sysreqs_req_help_bigint');
       
   487 
       
   488 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_bigint') . ': ' . parse_shellcolor_string($lang->get($req_bigint ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
       
   489 
       
   490 // Extension test: BCMath
       
   491 $req_bcmath = function_exists('bcadd');
       
   492 if ( !$req_bcmath && !$req_bigint && !$req_gmp )
       
   493   $warnings[] = $lang->get('sysreqs_req_help_bcmath');
       
   494 
       
   495 if ( !$silent ) echo '  ' . $lang->get('sysreqs_req_bcmath') . ': ' . parse_shellcolor_string($lang->get($req_bcmath ? 'cli_test_pass' : 'cli_test_warn')) . "\n";
       
   496 
       
   497 if ( !empty($warnings) && !$silent )
       
   498 {
       
   499   echo parse_shellcolor_string($lang->get('cli_msg_test_warnings')) . "\n";
       
   500   echo "  " . implode("\n  ", $warnings) . "\n";
       
   501 }
       
   502 
   346 if ( !function_exists('mysql_connect') && !function_exists('pg_connect') )
   503 if ( !function_exists('mysql_connect') && !function_exists('pg_connect') )
   347 {
   504 {
   348   installer_fail($lang->get('cli_err_no_drivers'));
   505   installer_fail($lang->get('cli_err_no_drivers'));
   349 }
   506 }
   350 if ( $test_failed )
   507 if ( $failed )
   351 {
   508 {
   352   installer_fail($lang->get('cli_err_sysreqs_fail'));
   509   installer_fail($lang->get('cli_err_sysreqs_fail'));
   353 }
   510 }
   354 
   511 
   355 ##
   512 ##