includes/common_cli.php
changeset 500 455277559782
child 536 218a627eb53e
equal deleted inserted replaced
499:6b7fdd898ba3 500:455277559782
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
       
     5  * Version 1.1.3 (Caoineag alpha 3)
       
     6  * Copyright (C) 2006-2007 Dan Fuhry
       
     7  *
       
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    12  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    13  */
       
    14  
       
    15 /**
       
    16  * The main loader script that initializes everything about Enano in the proper order. This is only loaded
       
    17  * if PHP is running via CLI.
       
    18  * @package Enano
       
    19  * @subpackage Core
       
    20  * @copyright See header block
       
    21  */
       
    22 
       
    23 //
       
    24 // MAIN API INITIALIZATION
       
    25 //
       
    26 
       
    27 // Note to important functions and the template class that we're running via CLI
       
    28 define('ENANO_CLI', 1);
       
    29 
       
    30 // The first thing we need to do is start the database connection. At this point, for all we know, Enano might not
       
    31 // even be installed. If this connection attempt fails and it's because of a missing or corrupt config file, the
       
    32 // user will be redirected (intelligently) to install.php.
       
    33 
       
    34 $config_file = ( defined('IN_ENANO_INSTALL') ) ? '/config.new.php' : '/config.php';
       
    35 @include(ENANO_ROOT . $config_file);
       
    36 unset($dbuser, $dbpasswd);
       
    37 if ( !isset($dbdriver) )
       
    38   $dbdriver = 'mysql';
       
    39 
       
    40 $db = new $dbdriver();
       
    41 $db->connect();
       
    42 
       
    43 profiler_log('Database connected');
       
    44 
       
    45 // The URL separator is the character appended to contentPath + url_title type strings.
       
    46 // If the contentPath has a ? in it, this should be an ampersand; else, it should be a
       
    47 // question mark.
       
    48 $sep = ( strstr(contentPath, '?') ) ? '&' : '?';
       
    49 define('urlSeparator', $sep);
       
    50 unset($sep); // save 10 bytes of memory...
       
    51 
       
    52 // Select and fetch the site configuration
       
    53 $e = $db->sql_query('SELECT config_name, config_value FROM '.table_prefix.'config;');
       
    54 if ( !$e )
       
    55 {
       
    56   $db->_die('Some critical configuration information could not be selected.');
       
    57 }
       
    58 // Used in die_semicritical to figure out whether to call getConfig() or not
       
    59 define('ENANO_CONFIG_FETCHED', '');
       
    60 
       
    61 // Initialize and fetch the site configuration array, which is used to cache the config
       
    62 $enano_config = Array();
       
    63 while($r = $db->fetchrow())
       
    64 {
       
    65   $enano_config[$r['config_name']] = $r['config_value'];
       
    66 }
       
    67 
       
    68 $db->free_result();
       
    69 
       
    70 profiler_log('Config fetched');
       
    71 
       
    72 // Now that we have the config, check the Enano version.
       
    73 if ( enano_version(false, true) != $version && !defined('IN_ENANO_UPGRADE') )
       
    74 {
       
    75   grinding_halt('Version mismatch', 'Trying to run Enano version '.$version.' on database version '.enano_version().', you might need to upgrade.');
       
    76 }
       
    77 
       
    78 //
       
    79 // Low level maintenance
       
    80 //
       
    81 
       
    82 // If the AES key size has been changed, bail out and fast
       
    83 if ( !getConfig('aes_key_size') )
       
    84 {
       
    85   setConfig('aes_key_size', AES_BITS);
       
    86 }
       
    87 else if ( $ks = getConfig('aes_key_size') )
       
    88 {
       
    89   if ( intval($ks) != AES_BITS )
       
    90   {
       
    91     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.');
       
    92   }
       
    93 }
       
    94 
       
    95 // Same for AES block size
       
    96 if ( !getConfig('aes_block_size') )
       
    97 {
       
    98   setConfig('aes_block_size', AES_BLOCKSIZE);
       
    99 }
       
   100 else if ( $ks = getConfig('aes_block_size') )
       
   101 {
       
   102   if ( intval($ks) != AES_BLOCKSIZE )
       
   103   {
       
   104     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.");
       
   105   }
       
   106 }
       
   107 
       
   108 // Is there no default language?
       
   109 if ( getConfig('default_language') === false && !defined('IN_ENANO_MIGRATION') )
       
   110 {
       
   111   $q = $db->sql_query('SELECT lang_id FROM '.table_prefix.'language LIMIT 1;');
       
   112   if ( !$q )
       
   113     $db->_die('common.php - setting default language');
       
   114   if ( $db->numrows() < 1 && !defined('ENANO_ALLOW_LOAD_NOLANG') )
       
   115   {
       
   116     grinding_halt('No languages', 'No languages are installed on the site, load from web interface for instructions on how to fix this.');
       
   117   }
       
   118   $row = $db->fetchrow();
       
   119   setConfig('default_language', $row['lang_id']);
       
   120 }
       
   121 
       
   122 profiler_log('Ran checks');
       
   123 
       
   124 // Load plugin manager
       
   125 $plugins = new pluginLoader();
       
   126 
       
   127 //
       
   128 // Mainstream API boot-up
       
   129 //
       
   130 
       
   131 // Obtain list of plugins
       
   132 $plugins->loadAll();
       
   133 
       
   134 global $plugins;
       
   135 
       
   136 // Load plugins from common because we can't give plugins full abilities in object context
       
   137 foreach ( $plugins->load_list as $f )
       
   138 {
       
   139   if ( file_exists($f) )
       
   140     include_once $f;
       
   141 }
       
   142 
       
   143 profiler_log('Loaded plugins');
       
   144 
       
   145 // Three fifths of the Enano API gets the breath of life right here.
       
   146 $session = new sessionManager();
       
   147 $paths = new pathManager();
       
   148 $template = new template();
       
   149 $email = new EmailEncryptor();
       
   150 
       
   151 profiler_log('Instanciated important singletons');
       
   152 
       
   153 // We've got the five main objects - flick on the switch so if a problem occurs, we can have a "friendly" UI
       
   154 define('ENANO_BASE_CLASSES_INITIALIZED', '');
       
   155 
       
   156 // From here on out, none of this functionality is needed during the installer stage.
       
   157 // Once $paths->init() is called, we could be redirected to the main page, so we don't want
       
   158 // that if the installer's running. Don't just go and define IN_ENANO_INSTALL from your
       
   159 // script though, because that will make the DBAL look in the wrong place for the config file.
       
   160 if ( !defined('IN_ENANO_INSTALL') )
       
   161 {
       
   162   // And here you have it, the de facto way to place a hook. Plugins can place hooks and hook
       
   163   // into other plugins. You just never know.
       
   164   $code = $plugins->setHook('base_classes_initted');
       
   165   foreach ( $code as $cmd )
       
   166   {
       
   167     eval($cmd);
       
   168   }
       
   169   
       
   170   profiler_log('Finished base_classes_initted hook');
       
   171   
       
   172   // For special and administration pages, sometimes there is a "preloader" function that must be run
       
   173   // before the session manager and/or path manager get the init signal. Call it here.  
       
   174   $p = RenderMan::strToPageId($paths->get_pageid_from_url());
       
   175   if( ( $p[1] == 'Admin' || $p[1] == 'Special' ) && function_exists('page_'.$p[1].'_'.$p[0].'_preloader'))
       
   176   {
       
   177     @call_user_func('page_'.$p[1].'_'.$p[0].'_preloader');
       
   178   }
       
   179   
       
   180   profiler_log('Checked for preloader');
       
   181   
       
   182   // One quick security check...
       
   183   if ( isset($_SERVER['REMOTE_ADDR']) )
       
   184   {
       
   185     grinding_halt('REMOTE_ADDR detected', 'Detected a REMOTE_ADDR, this should not happen in CLI mode.');
       
   186   }
       
   187   $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
       
   188 
       
   189   // All checks passed! Start the main components up.  
       
   190   $session->start();
       
   191   
       
   192   // This is where plugins will want to add pages from 1.1.x on out. You can still add
       
   193   // pages at base_classes_initted but the titles won't be localized. This is because
       
   194   // the session manager has to be started before localization will work in the user's
       
   195   // preferred language.
       
   196   $code = $plugins->setHook('session_started');
       
   197   foreach ( $code as $cmd )
       
   198   {
       
   199     eval($cmd);
       
   200   }
       
   201   
       
   202   profiler_log('Ran session_started hook');
       
   203   
       
   204   $paths->init();
       
   205   
       
   206   // We're ready for whatever life throws us now.
       
   207   define('ENANO_MAINSTREAM', '');
       
   208   
       
   209   // If the site is disabled, bail out, unless we're trying to log in or administer the site
       
   210   if(getConfig('site_disabled') == '1' && $session->user_level < USER_LEVEL_ADMIN)
       
   211   {
       
   212     if ( $paths->namespace == 'Admin' || ( $paths->namespace == 'Special' && ( $paths->page_id == 'CSS' || $paths->page_id == 'Administration' || $paths->page_id == 'Login' ) ) )
       
   213     {
       
   214       // do nothing; allow execution to continue
       
   215     }
       
   216     else
       
   217     {
       
   218       if(!$n = getConfig('site_disabled_notice')) 
       
   219       {
       
   220         $n = 'The administrator has disabled the site. Please check back later.';
       
   221       }
       
   222       
       
   223       $text = RenderMan::render($n) . '
       
   224       <div class="info-box">
       
   225         If you have an administrative account, you may <a href="'.makeUrlNS('Special', 'Login').'">log in</a> to the site.
       
   226       </div>';
       
   227       $paths->wiki_mode = 0;
       
   228       die_semicritical('Site disabled', $text);
       
   229     }
       
   230   }
       
   231   else if ( getConfig('site_disabled') == '1' && $session->user_level >= USER_LEVEL_ADMIN )
       
   232   {
       
   233     // If the site is disabled but the user has admin rights, allow browsing
       
   234     // and stuff, but display the orange box notifying the admin.
       
   235     $template->site_disabled = true;
       
   236   }
       
   237   
       
   238   // At this point all of Enano is fully initialized and running and you're ready to do whatever you want.
       
   239   $code = $plugins->setHook('common_post');
       
   240   foreach ( $code as $cmd )
       
   241   {
       
   242     eval($cmd);
       
   243   }
       
   244   
       
   245   profiler_log('Ran disabled-site checks and common_post');
       
   246   
       
   247   if ( isset($_GET['noheaders']) )
       
   248     $template->no_headers = true;
       
   249 }
       
   250 
       
   251 profiler_log('common finished');
       
   252 
       
   253 // That's the end. Enano should be loaded now :-)
       
   254 
       
   255 ?>