includes/common_cli.php
changeset 1227 bdac73ed481e
parent 1202 5d63267470cd
child 1365 a52cac1b190d
equal deleted inserted replaced
1226:de56132c008d 1227:bdac73ed481e
    32 
    32 
    33 $config_file = ( defined('IN_ENANO_INSTALL') && !defined('ENANO_INSTALLED') ) ? '/config.new.php' : '/config.php';
    33 $config_file = ( defined('IN_ENANO_INSTALL') && !defined('ENANO_INSTALLED') ) ? '/config.new.php' : '/config.php';
    34 @include(ENANO_ROOT . $config_file);
    34 @include(ENANO_ROOT . $config_file);
    35 unset($dbuser, $dbpasswd);
    35 unset($dbuser, $dbpasswd);
    36 if ( !isset($dbdriver) )
    36 if ( !isset($dbdriver) )
    37   $dbdriver = 'mysql';
    37 	$dbdriver = 'mysql';
    38 
    38 
    39 $db = new $dbdriver();
    39 $db = new $dbdriver();
    40 $db->connect();
    40 $db->connect();
    41 
    41 
    42 profiler_log('Database connected');
    42 profiler_log('Database connected');
    50 
    50 
    51 // Select and fetch the site configuration
    51 // Select and fetch the site configuration
    52 $e = $db->sql_query('SELECT config_name, config_value FROM '.table_prefix.'config;');
    52 $e = $db->sql_query('SELECT config_name, config_value FROM '.table_prefix.'config;');
    53 if ( !$e )
    53 if ( !$e )
    54 {
    54 {
    55   $db->_die('Some critical configuration information could not be selected.');
    55 	$db->_die('Some critical configuration information could not be selected.');
    56 }
    56 }
    57 // Used in die_semicritical to figure out whether to call getConfig() or not
    57 // Used in die_semicritical to figure out whether to call getConfig() or not
    58 define('ENANO_CONFIG_FETCHED', '');
    58 define('ENANO_CONFIG_FETCHED', '');
    59 
    59 
    60 // Initialize and fetch the site configuration array, which is used to cache the config
    60 // Initialize and fetch the site configuration array, which is used to cache the config
    61 $enano_config = Array();
    61 $enano_config = Array();
    62 while($r = $db->fetchrow())
    62 while($r = $db->fetchrow())
    63 {
    63 {
    64   $enano_config[$r['config_name']] = $r['config_value'];
    64 	$enano_config[$r['config_name']] = $r['config_value'];
    65 }
    65 }
    66 
    66 
    67 $db->free_result();
    67 $db->free_result();
    68 
    68 
    69 profiler_log('Config fetched');
    69 profiler_log('Config fetched');
    70 
    70 
    71 // Now that we have the config, check the Enano version.
    71 // Now that we have the config, check the Enano version.
    72 if ( enano_version(false, true) != $version && !defined('IN_ENANO_UPGRADE') )
    72 if ( enano_version(false, true) != $version && !defined('IN_ENANO_UPGRADE') )
    73 {
    73 {
    74   grinding_halt('Version mismatch', 'Trying to run Enano version '.$version.' on database version '.enano_version().', you might need to upgrade.');
    74 	grinding_halt('Version mismatch', 'Trying to run Enano version '.$version.' on database version '.enano_version().', you might need to upgrade.');
    75 }
    75 }
    76 
    76 
    77 // Set our CDN path
    77 // Set our CDN path
    78 if ( !defined('cdnPath') )
    78 if ( !defined('cdnPath') )
    79   define('cdnPath', getConfig('cdn_path', scriptPath));
    79 	define('cdnPath', getConfig('cdn_path', scriptPath));
    80 
    80 
    81 //
    81 //
    82 // Low level maintenance
    82 // Low level maintenance
    83 //
    83 //
    84 
    84 
    85 // If the AES key size has been changed, bail out and fast
    85 // If the AES key size has been changed, bail out and fast
    86 if ( !getConfig('aes_key_size') )
    86 if ( !getConfig('aes_key_size') )
    87 {
    87 {
    88   setConfig('aes_key_size', AES_BITS);
    88 	setConfig('aes_key_size', AES_BITS);
    89 }
    89 }
    90 else if ( $ks = getConfig('aes_key_size') )
    90 else if ( $ks = getConfig('aes_key_size') )
    91 {
    91 {
    92   if ( intval($ks) != AES_BITS )
    92 	if ( intval($ks) != AES_BITS )
    93   {
    93 	{
    94     grinding_halt('AES key size changed', 'Enano has detected that the AES key size in constants.php has been changed. This change cannot be performed after installation, otherwise the private key would have to be re-generated and all passwords would have to be re-encrypted.' . "\n\n" . 'Please change the key size back to ' . $ks . ' bits and rerun this script.');
    94 		grinding_halt('AES key size changed', 'Enano has detected that the AES key size in constants.php has been changed. This change cannot be performed after installation, otherwise the private key would have to be re-generated and all passwords would have to be re-encrypted.' . "\n\n" . 'Please change the key size back to ' . $ks . ' bits and rerun this script.');
    95   }
    95 	}
    96 }
    96 }
    97 
    97 
    98 // Same for AES block size
    98 // Same for AES block size
    99 if ( !getConfig('aes_block_size') )
    99 if ( !getConfig('aes_block_size') )
   100 {
   100 {
   101   setConfig('aes_block_size', AES_BLOCKSIZE);
   101 	setConfig('aes_block_size', AES_BLOCKSIZE);
   102 }
   102 }
   103 else if ( $ks = getConfig('aes_block_size') )
   103 else if ( $ks = getConfig('aes_block_size') )
   104 {
   104 {
   105   if ( intval($ks) != AES_BLOCKSIZE )
   105 	if ( intval($ks) != AES_BLOCKSIZE )
   106   {
   106 	{
   107     grinding_halt('AES block size changed', "Enano has detected that the AES block size in constants.php has been changed. This change cannot be performed after installation, otherwise all passwords would have to be re-encrypted.\n\nPlease change the block size back to $ks bits and rerun this script.");
   107 		grinding_halt('AES block size changed', "Enano has detected that the AES block size in constants.php has been changed. This change cannot be performed after installation, otherwise all passwords would have to be re-encrypted.\n\nPlease change the block size back to $ks bits and rerun this script.");
   108   }
   108 	}
   109 }
   109 }
   110 
   110 
   111 // Is there no default language?
   111 // Is there no default language?
   112 if ( getConfig('default_language') === false && !defined('IN_ENANO_MIGRATION') )
   112 if ( getConfig('default_language') === false && !defined('IN_ENANO_MIGRATION') )
   113 {
   113 {
   114   $q = $db->sql_query('SELECT lang_id FROM '.table_prefix.'language LIMIT 1;');
   114 	$q = $db->sql_query('SELECT lang_id FROM '.table_prefix.'language LIMIT 1;');
   115   if ( !$q )
   115 	if ( !$q )
   116     $db->_die('common.php - setting default language');
   116 		$db->_die('common.php - setting default language');
   117   if ( $db->numrows() < 1 && !defined('ENANO_ALLOW_LOAD_NOLANG') )
   117 	if ( $db->numrows() < 1 && !defined('ENANO_ALLOW_LOAD_NOLANG') )
   118   {
   118 	{
   119     grinding_halt('No languages', 'No languages are installed on the site, load from web interface for instructions on how to fix this.');
   119 		grinding_halt('No languages', 'No languages are installed on the site, load from web interface for instructions on how to fix this.');
   120   }
   120 	}
   121   $row = $db->fetchrow();
   121 	$row = $db->fetchrow();
   122   setConfig('default_language', $row['lang_id']);
   122 	setConfig('default_language', $row['lang_id']);
   123 }
   123 }
   124 
   124 
   125 profiler_log('Ran checks');
   125 profiler_log('Ran checks');
   126 
   126 
   127 // Init cache
   127 // Init cache
   142 global $plugins;
   142 global $plugins;
   143 
   143 
   144 // Load plugins from common because we can't give plugins full abilities in object context
   144 // Load plugins from common because we can't give plugins full abilities in object context
   145 if ( !defined('ENANO_NO_PLUGINS') )
   145 if ( !defined('ENANO_NO_PLUGINS') )
   146 {
   146 {
   147   foreach ( $plugins->load_list as $f )
   147 	foreach ( $plugins->load_list as $f )
   148   {
   148 	{
   149     if ( file_exists(ENANO_ROOT . '/plugins/' . $f) )
   149 		if ( file_exists(ENANO_ROOT . '/plugins/' . $f) )
   150       include_once ENANO_ROOT . '/plugins/' . $f;
   150 			include_once ENANO_ROOT . '/plugins/' . $f;
   151   }
   151 	}
   152 }
   152 }
   153 
   153 
   154 profiler_log('Loaded plugins');
   154 profiler_log('Loaded plugins');
   155 
   155 
   156 // Three fifths of the Enano API gets the breath of life right here.
   156 // Three fifths of the Enano API gets the breath of life right here.
   168 // Once $paths->init() is called, we could be redirected to the main page, so we don't want
   168 // Once $paths->init() is called, we could be redirected to the main page, so we don't want
   169 // that if the installer's running. Don't just go and define IN_ENANO_INSTALL from your
   169 // that if the installer's running. Don't just go and define IN_ENANO_INSTALL from your
   170 // script though, because that will make the DBAL look in the wrong place for the config file.
   170 // script though, because that will make the DBAL look in the wrong place for the config file.
   171 if ( !defined('IN_ENANO_INSTALL') )
   171 if ( !defined('IN_ENANO_INSTALL') )
   172 {
   172 {
   173   // And here you have it, the de facto way to place a hook. Plugins can place hooks and hook
   173 	// And here you have it, the de facto way to place a hook. Plugins can place hooks and hook
   174   // into other plugins. You just never know.
   174 	// into other plugins. You just never know.
   175   $code = $plugins->setHook('base_classes_initted');
   175 	$code = $plugins->setHook('base_classes_initted');
   176   foreach ( $code as $cmd )
   176 	foreach ( $code as $cmd )
   177   {
   177 	{
   178     eval($cmd);
   178 		eval($cmd);
   179   }
   179 	}
   180   
   180 	
   181   profiler_log('Finished base_classes_initted hook');
   181 	profiler_log('Finished base_classes_initted hook');
   182   
   182 	
   183   // One quick security check...
   183 	// One quick security check...
   184   if ( isset($_SERVER['REMOTE_ADDR']) )
   184 	if ( isset($_SERVER['REMOTE_ADDR']) )
   185   {
   185 	{
   186     grinding_halt('REMOTE_ADDR detected', 'Detected a REMOTE_ADDR, this should not happen in CLI mode.');
   186 		grinding_halt('REMOTE_ADDR detected', 'Detected a REMOTE_ADDR, this should not happen in CLI mode.');
   187   }
   187 	}
   188   $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
   188 	$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
   189 
   189 
   190   // All checks passed! Start the main components up.  
   190 	// All checks passed! Start the main components up.  
   191   $session->start();
   191 	$session->start();
   192   
   192 	
   193   // This is where plugins will want to add pages from 1.1.x on out. You can still add
   193 	// This is where plugins will want to add pages from 1.1.x on out. You can still add
   194   // pages at base_classes_initted but the titles won't be localized. This is because
   194 	// pages at base_classes_initted but the titles won't be localized. This is because
   195   // the session manager has to be started before localization will work in the user's
   195 	// the session manager has to be started before localization will work in the user's
   196   // preferred language.
   196 	// preferred language.
   197   $code = $plugins->setHook('session_started');
   197 	$code = $plugins->setHook('session_started');
   198   foreach ( $code as $cmd )
   198 	foreach ( $code as $cmd )
   199   {
   199 	{
   200     eval($cmd);
   200 		eval($cmd);
   201   }
   201 	}
   202   
   202 	
   203   profiler_log('Ran session_started hook');
   203 	profiler_log('Ran session_started hook');
   204   
   204 	
   205   $paths->init($paths->nslist['System'] . 'API');
   205 	$paths->init($paths->nslist['System'] . 'API');
   206   
   206 	
   207   // We're ready for whatever life throws us now.
   207 	// We're ready for whatever life throws us now.
   208   define('ENANO_MAINSTREAM', '');
   208 	define('ENANO_MAINSTREAM', '');
   209   
   209 	
   210   // If the site is disabled, bail out, unless we're trying to log in or administer the site
   210 	// If the site is disabled, bail out, unless we're trying to log in or administer the site
   211   if(getConfig('site_disabled') == '1' && $session->user_level < USER_LEVEL_ADMIN)
   211 	if(getConfig('site_disabled') == '1' && $session->user_level < USER_LEVEL_ADMIN)
   212   {
   212 	{
   213     if ( $paths->namespace == 'Admin' || ( $paths->namespace == 'Special' && ( $paths->page_id == 'CSS' || $paths->page_id == 'Administration' || $paths->page_id == 'Login' ) ) )
   213 		if ( $paths->namespace == 'Admin' || ( $paths->namespace == 'Special' && ( $paths->page_id == 'CSS' || $paths->page_id == 'Administration' || $paths->page_id == 'Login' ) ) )
   214     {
   214 		{
   215       // do nothing; allow execution to continue
   215 			// do nothing; allow execution to continue
   216     }
   216 		}
   217     else
   217 		else
   218     {
   218 		{
   219       if(!$n = getConfig('site_disabled_notice')) 
   219 			if(!$n = getConfig('site_disabled_notice')) 
   220       {
   220 			{
   221         $n = 'The administrator has disabled the site. Please check back later.';
   221 				$n = 'The administrator has disabled the site. Please check back later.';
   222       }
   222 			}
   223       
   223 			
   224       $text = RenderMan::render($n) . '
   224 			$text = RenderMan::render($n) . '
   225       <div class="info-box">
   225 			<div class="info-box">
   226         If you have an administrative account, you may <a href="'.makeUrlNS('Special', 'Login').'">log in</a> to the site.
   226 				If you have an administrative account, you may <a href="'.makeUrlNS('Special', 'Login').'">log in</a> to the site.
   227       </div>';
   227 			</div>';
   228       $paths->wiki_mode = 0;
   228 			$paths->wiki_mode = 0;
   229       die_semicritical('Site disabled', $text);
   229 			die_semicritical('Site disabled', $text);
   230     }
   230 		}
   231   }
   231 	}
   232   else if ( getConfig('site_disabled') == '1' && $session->user_level >= USER_LEVEL_ADMIN )
   232 	else if ( getConfig('site_disabled') == '1' && $session->user_level >= USER_LEVEL_ADMIN )
   233   {
   233 	{
   234     // If the site is disabled but the user has admin rights, allow browsing
   234 		// If the site is disabled but the user has admin rights, allow browsing
   235     // and stuff, but display the orange box notifying the admin.
   235 		// and stuff, but display the orange box notifying the admin.
   236     $template->site_disabled = true;
   236 		$template->site_disabled = true;
   237   }
   237 	}
   238   
   238 	
   239   // At this point all of Enano is fully initialized and running and you're ready to do whatever you want.
   239 	// At this point all of Enano is fully initialized and running and you're ready to do whatever you want.
   240   $code = $plugins->setHook('common_post');
   240 	$code = $plugins->setHook('common_post');
   241   foreach ( $code as $cmd )
   241 	foreach ( $code as $cmd )
   242   {
   242 	{
   243     eval($cmd);
   243 		eval($cmd);
   244   }
   244 	}
   245   
   245 	
   246   profiler_log('Ran disabled-site checks and common_post');
   246 	profiler_log('Ran disabled-site checks and common_post');
   247   
   247 	
   248   load_rank_data();
   248 	load_rank_data();
   249   
   249 	
   250   profiler_log('Loaded user rank data');
   250 	profiler_log('Loaded user rank data');
   251   
   251 	
   252   if ( isset($_GET['noheaders']) )
   252 	if ( isset($_GET['noheaders']) )
   253     $template->no_headers = true;
   253 		$template->no_headers = true;
   254 }
   254 }
   255 
   255 
   256 profiler_log('common finished');
   256 profiler_log('common finished');
   257 
   257 
   258 // That's the end. Enano should be loaded now :-)
   258 // That's the end. Enano should be loaded now :-)