Source for file jInstallChecker.class.php

Documentation is available at jInstallChecker.class.php

  1. <?php
  2. /**
  3. * check a jelix installation
  4. *
  5. @package  jelix
  6. @subpackage core
  7. @author   Laurent Jouanneau
  8. @contributor Bastien Jaillot
  9. @contributor Olivier Demah, Brice Tence, Julien Issler
  10. @copyright 2007-2011 Laurent Jouanneau, 2008 Bastien Jaillot, 2009 Olivier Demah, 2010 Brice Tence, 2011 Julien Issler
  11. @link     http://www.jelix.org
  12. @licence  GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  13. @since 1.0b2
  14. */
  15.  
  16. /**
  17.  * check an installation of a jelix application
  18.  * @package  jelix
  19.  * @subpackage core
  20.  * @since 1.0b2
  21.  */
  22. class jInstallCheck {
  23.  
  24.     /**
  25.      * the object responsible of the results output
  26.      * @var jIInstallReporter 
  27.      */
  28.     protected $reporter;
  29.  
  30.     /**
  31.      * @var jInstallerMessageProvider 
  32.      */
  33.     public $messages;
  34.  
  35.     public $nbError = 0;
  36.     public $nbOk = 0;
  37.     public $nbWarning = 0;
  38.     public $nbNotice = 0;
  39.  
  40.     protected $buildProperties;
  41.  
  42.     public $verbose = false;
  43.  
  44.     public $checkForInstallation = false;
  45.  
  46.     function __construct ($reporter$lang=''){
  47.         $this->reporter = $reporter;
  48.         $this->messages = new jInstallerMessageProvider($lang);
  49.     }
  50.  
  51.     protected $otherExtensions = array();
  52.  
  53.     function addExtensionCheck($extension$required{
  54.         $this->otherExtensions[$extension$required;
  55.     }
  56.  
  57.     protected $otherPaths = array();
  58.  
  59.     /**
  60.      * @since 1.2.5
  61.      */
  62.     function addWritablePathCheck($pathOrFileName{
  63.         if (is_array($pathOrFileName))
  64.             $this->otherPaths = array_merge($this->otherPaths$pathOrFileName);
  65.         else
  66.             $this->otherPaths[$pathOrFileName;
  67.     }
  68.  
  69.     protected $databases = array();
  70.     protected $dbRequired = false;
  71.  
  72.     function addDatabaseCheck($databases$required{
  73.         $this->databases = $databases;
  74.         $this->dbRequired = $required;
  75.     }
  76.  
  77.     /**
  78.      * run the ckecking
  79.      */
  80.     function run(){
  81.         $this->nbError = 0;
  82.         $this->nbOk = 0;
  83.         $this->nbWarning = 0;
  84.         $this->nbNotice = 0;
  85.         $this->reporter->start();
  86.         try {
  87.             $this->checkAppPaths();
  88.             $this->loadBuildFile();
  89.             $this->checkPhpExtensions();
  90.             $this->checkPhpSettings();
  91.         }catch(Exception $e){
  92.             $this->error('cannot.continue',$e->getMessage());
  93.         }
  94.         $results array('error'=>$this->nbError'warning'=>$this->nbWarning'ok'=>$this->nbOk,'notice'=>$this->nbNotice);
  95.         $this->reporter->end($results);
  96.     }
  97.  
  98.     protected function error($msg$msgparams=array()$extraMsg=''){
  99.         if($this->reporter)
  100.             $this->reporter->message($this->messages->get($msg$msgparams).$extraMsg'error');
  101.         $this->nbError ++;
  102.     }
  103.  
  104.     protected function ok($msg$msgparams=array()){
  105.         if($this->reporter)
  106.             $this->reporter->message($this->messages->get($msg$msgparams)'ok');
  107.         $this->nbOk ++;
  108.     }
  109.     /**
  110.      * generate a warning
  111.      * @param string $msg  the key of the message to display
  112.      */
  113.     protected function warning($msg$msgparams=array()){
  114.         if($this->reporter)
  115.             $this->reporter->message($this->messages->get($msg$msgparams)'warning');
  116.         $this->nbWarning ++;
  117.     }
  118.  
  119.     protected function notice($msg$msgparams=array()){
  120.         if($this->reporter{
  121.             $this->reporter->message($this->messages->get($msg$msgparams)'notice');
  122.         }
  123.         $this->nbNotice ++;
  124.     }
  125.  
  126.     function checkPhpExtensions(){
  127.         $ok=true;
  128.         if(!version_compare($this->buildProperties['PHP_VERSION_TARGET']phpversion()'<=')){
  129.             $this->error('php.bad.version');
  130.             $notice $this->messages->get('php.version.required'$this->buildProperties['PHP_VERSION_TARGET']);
  131.             $notice.= '. '.$this->messages->get('php.version.current',phpversion());
  132.             $this->reporter->showNotice($notice);
  133.             $ok=false;
  134.         }
  135.         else if ($this->verbose{
  136.             $this->ok('php.ok.version'phpversion());
  137.         }
  138.  
  139.         $extensions array'dom''SPL''SimpleXML''pcre''session',
  140.             'tokenizer''iconv''filter''json');
  141.  
  142.         if($this->buildProperties['ENABLE_PHP_JELIX'== '1')
  143.             $extensions['jelix';
  144.  
  145.         foreach($extensions as $name){
  146.             if(!extension_loaded($name)){
  147.                 $this->error('extension.required.not.installed'$name);
  148.                 $ok=false;
  149.             }
  150.             else if ($this->verbose{
  151.                 $this->ok('extension.required.installed'$name);
  152.             }
  153.         }
  154.  
  155.         if (count($this->databases)) {
  156.             $req ($this->dbRequired?'required':'optional');
  157.             $okdb false;
  158.             if (class_exists('PDO'))
  159.                 $pdodrivers PDO::getAvailableDrivers();
  160.             else
  161.                 $pdodrivers array();
  162.  
  163.             foreach($this->databases as $name){
  164.                 if(!extension_loaded($name&& !in_array($name$pdodrivers)){
  165.                     $this->notice('extension.not.installed'$name);
  166.                 }
  167.                 else {
  168.                     $okdb true;
  169.                     if ($this->verbose)
  170.                         $this->ok('extension.installed'$name);
  171.                 }
  172.             }
  173.             if ($this->dbRequired{
  174.                 if ($okdb{
  175.                     $this->ok('extension.database.ok');
  176.                 }
  177.                 else {
  178.                     $this->error('extension.database.missing');
  179.                     $ok false;
  180.                 }
  181.             }
  182.             else {
  183.                 if ($okdb{
  184.                     $this->ok('extension.database.ok2');
  185.                 }
  186.                 else {
  187.                     $this->notice('extension.database.missing2');
  188.                 }
  189.             }
  190.  
  191.         }
  192.  
  193.         foreach($this->otherExtensions as $name=>$required){
  194.             $req ($required?'required':'optional');
  195.             if(!extension_loaded($name)){
  196.                 if ($required{
  197.                     $this->error('extension.'.$req.'.not.installed'$name);
  198.                     $ok=false;
  199.                 }
  200.                 else {
  201.                     $this->notice('extension.'.$req.'.not.installed'$name);
  202.                 }
  203.             }
  204.             else if ($this->verbose{
  205.                 $this->ok('extension.'.$req.'.installed'$name);
  206.             }
  207.         }
  208.  
  209.         if($ok)
  210.             $this->ok('extensions.required.ok');
  211.  
  212.         return $ok;
  213.     }
  214.     function checkAppPaths(){
  215.         $ok true;
  216.         if(!defined('JELIX_LIB_PATH'|| !jApp::isInit()){
  217.             throw new Exception($this->messages->get('path.core'));
  218.         }
  219.  
  220.         if(!file_exists(jApp::tempBasePath()) || !is_writable(jApp::tempBasePath())){
  221.             $this->error('path.temp');
  222.             $ok=false;
  223.         }
  224.         if(!file_exists(jApp::logPath()) || !is_writable(jApp::logPath())){
  225.             $this->error('path.log');
  226.             $ok=false;
  227.         }
  228.         if(!file_exists(jApp::varPath())){
  229.             $this->error('path.var');
  230.             $ok=false;
  231.         }
  232.         if(!file_exists(jApp::configPath())){
  233.             $this->error('path.config');
  234.             $ok=false;
  235.         }
  236.         elseif ($this->checkForInstallation{
  237.             if (!is_writable(jApp::configPath())) {
  238.                 $this->error('path.config.writable');
  239.                 $ok false;
  240.             }
  241.             if (file_exists(jApp::configPath('profiles.ini.php'))
  242.                 && !is_writable(jApp::configPath('profiles.ini.php'))) {
  243.                 $this->error('path.profiles.writable');
  244.                 $ok false;
  245.             }
  246.             if (file_exists(jApp::configPath('defaultconfig.ini.php'))
  247.                 && !is_writable(jApp::configPath('defaultconfig.ini.php'))) {
  248.                 $this->error('path.defaultconfig.writable');
  249.                 $ok false;
  250.             }
  251.             if (file_exists(jApp::configPath('installer.ini.php'))
  252.                 && !is_writable(jApp::configPath('installer.ini.php'))) {
  253.                 $this->error('path.installer.writable');
  254.                 $ok false;
  255.             }
  256.         }
  257.  
  258.         if(!file_exists(jApp::wwwPath())){
  259.             $this->error('path.www');
  260.             $ok=false;
  261.         }
  262.  
  263.         foreach($this->otherPaths as $path{
  264.             $realPath str_replace(array('app:','lib:','var:''www:')array(jApp::appPath()LIB_PATHjApp::varPath()jApp::wwwPath())$path);
  265.             if (!file_exists($realPath)) {
  266.                 $this->error('path.custom.not.exists'array($path));
  267.                 $ok false;
  268.             }
  269.             else if(!is_writable($realPath)) {
  270.                 $this->error('path.custom.writable'array($path));
  271.                 $ok false;
  272.             }
  273.             else
  274.                 $this->ok('path.custom.ok'array($path));
  275.         }
  276.  
  277.         if($ok)
  278.             $this->ok('paths.ok');
  279.         else
  280.             throw new Exception($this->messages->get('too.critical.error'));
  281.  
  282.         /*if(!isset($GLOBALS['config_file']) ||
  283.            empty($GLOBALS['config_file']) ||
  284.            !file_exists(jApp::configPath($GLOBALS['config_file']))){
  285.             throw new Exception($this->messages->get('config.file'));
  286.         }*/
  287.  
  288.         return $ok;
  289.     }
  290.  
  291.     function loadBuildFile({
  292.         if (!file_exists(JELIX_LIB_PATH.'BUILD')){
  293.             throw new Exception($this->messages->get('build.not.found'));
  294.         else {
  295.             $this->buildProperties = parse_ini_file(JELIX_LIB_PATH.'BUILD');
  296.         }
  297.     }
  298.  
  299.     function checkPhpSettings(){
  300.         $ok true;
  301.         if (file_exists(jApp::configPath("defaultconfig.ini.php")))
  302.             $defaultconfig parse_ini_file(jApp::configPath("defaultconfig.ini.php")true);
  303.         else
  304.             $defaultconfig array();
  305.         if (file_exists(jApp::configPath("index/config.ini.php")))
  306.             $indexconfig parse_ini_file(jApp::configPath("index/config.ini.php")true);
  307.         else
  308.             $indexconfig array();
  309.  
  310.         if(ini_get('magic_quotes_gpc'== 1){
  311.             $this->error('ini.magic_quotes_gpc');
  312.             $ok=false;
  313.         }
  314.  
  315.         if(ini_get('magic_quotes_runtime'== 1){
  316.             $this->error('ini.magic_quotes_runtime');
  317.             $ok=false;
  318.         }
  319.  
  320.         if(ini_get('session.auto_start'== 1){
  321.             $this->error('ini.session.auto_start');
  322.             $ok=false;
  323.         }
  324.  
  325.         if(ini_get('safe_mode'== 1){
  326.             $this->error('ini.safe_mode');
  327.             $ok=false;
  328.         }
  329.  
  330.         if(ini_get('register_globals'== 1){
  331.             $this->warning('ini.register_globals');
  332.             $ok=false;
  333.         }
  334.  
  335.         if(ini_get('asp_tags'== 1){
  336.             $this->notice('ini.asp_tags');
  337.         }
  338.         if($ok){
  339.             $this->ok('ini.ok');
  340.         }
  341.         return $ok;
  342.     }
  343. }

Documentation generated on Wed, 04 Jan 2017 22:55:20 +0100 by phpDocumentor 1.4.3