PageRenderTime 64ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jelix-legacy/installer/jInstallChecker.class.php

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