Install.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. class Install {
  3. const DEFAULT_TABLES_PREFIX = 'leed__';
  4. const CONSTANT_FILE = 'constant.php';
  5. public $finished = false;
  6. public $logs = array(
  7. 'errors' => array(),
  8. 'notices' => array()
  9. );
  10. public $options = array(
  11. 'user' => array(
  12. 'login' => "",
  13. 'password' => ""
  14. ),
  15. 'db' => array(
  16. 'mysqlBase' => "",
  17. 'mysqlHost' => "",
  18. 'mysqlLogin' => "",
  19. 'mysqlMdp' => "",
  20. 'mysqlPrefix' => self::DEFAULT_TABLES_PREFIX
  21. )
  22. );
  23. protected $salt;
  24. public function __construct() {
  25. require_once('Logger.class.php');
  26. $this->logger = new Logger('install');
  27. }
  28. protected function overrideDefaultValues($_) {
  29. foreach ($this->options as $type => $options) {
  30. foreach ($options as $var => $defaultValue) {
  31. if (!empty($_[$var])) {
  32. $val = $type === 'db' ? str_replace("'", "\'", $_[$var]) : Functions::secure($_[$var]);
  33. $this->options[$type][$var] = $val;
  34. }
  35. }
  36. }
  37. }
  38. public function launch($_, $installActionName) {
  39. $this->checkFunctionsExists();
  40. if(!isset($_[$installActionName])) {
  41. return false;
  42. }
  43. $this->overrideDefaultValues($_);
  44. $this->checkLoginPassword();
  45. $this->checkdb();
  46. if(!$this->hasErrors()) {
  47. $this->createConstantFile();
  48. require_once('constant.php');
  49. require_once('MysqlEntity.class.php');
  50. class_exists('Update') or require_once('Update.class.php');
  51. Update::ExecutePatch(true);
  52. require_once('Feed.class.php');
  53. require_once('Event.class.php');
  54. require_once('User.class.php');
  55. require_once('Folder.class.php');
  56. require_once('Configuration.class.php');
  57. $this->createConfig();
  58. $this->createUser();
  59. $this->setFinished(true);
  60. $this->logger->destroy();
  61. }
  62. }
  63. public function getDefaultRoot() {
  64. $urlParts = explode('/', $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
  65. array_pop($urlParts);
  66. return implode('/', $urlParts);
  67. }
  68. protected function createConstantFile() {
  69. $constant = "<?php
  70. //Host de Mysql, le plus souvent localhost ou 127.0.0.1
  71. define('MYSQL_HOST','{$this->options['db']['mysqlHost']}');
  72. //Identifiant MySQL
  73. define('MYSQL_LOGIN','{$this->options['db']['mysqlLogin']}');
  74. //mot de passe MySQL
  75. define('MYSQL_MDP','{$this->options['db']['mysqlMdp']}');
  76. //Nom de la base MySQL ou se trouvera leed
  77. define('MYSQL_BDD','{$this->options['db']['mysqlBase']}');
  78. //Prefix des noms des tables leed pour les bases de données uniques
  79. define('MYSQL_PREFIX','{$this->options['db']['mysqlPrefix']}');
  80. ?>";
  81. file_put_contents(self::CONSTANT_FILE, $constant);
  82. if (!is_readable(self::CONSTANT_FILE))
  83. die('"'.self::CONSTANT_FILE.'" not found!');
  84. }
  85. protected function createConfig() {
  86. $configurationManager = new Configuration();
  87. if ($configurationManager->tableExists()) {
  88. $configurationManager->truncate();
  89. }
  90. $configurationManager->create();
  91. $configurationManager->setDefaults();
  92. $this->salt = $configurationManager->get('cryptographicSalt');
  93. }
  94. protected function createUser() {
  95. $userManager = new User();
  96. if ($userManager->tableExists()) {
  97. $userManager->truncate();
  98. }
  99. $userManager->create();
  100. $userManager->add($this->options['user']['login'], $this->options['user']['password'], $this->salt, $this->logger);
  101. $_SESSION['currentUser'] = serialize($userManager->get($this->options['user']['login']));
  102. }
  103. protected function checkLoginPassword() {
  104. if(
  105. empty($this->options['user']['password'])
  106. || empty($this->options['user']['login'])
  107. ) {
  108. $this->logs['errors'][] = _t('INSTALL_ERROR_USERPWD');
  109. return false;
  110. }
  111. return true;
  112. }
  113. protected function checkDb() {
  114. if(
  115. empty($this->options['db']['mysqlHost'])
  116. || empty($this->options['db']['mysqlLogin'])
  117. || empty($this->options['db']['mysqlBase'])
  118. ) {
  119. $this->logs['errors'][] = _t('INSTALL_ERROR_DB_INFOS');
  120. return false;
  121. }
  122. if (!Functions::testDb(
  123. $this->options['db']['mysqlHost'], $this->options['db']['mysqlLogin'], $this->options['db']['mysqlMdp'], $this->options['db']['mysqlBase']
  124. )) {
  125. $this->logs['errors'][] = _t('INSTALL_ERROR_CONNEXION');
  126. } else {
  127. $this->logs['notices'][] = _t('INSTALL_INFO_CONNEXION');
  128. }
  129. return true;
  130. }
  131. public function hasErrors() {
  132. return count($this->logs['errors']) > 0;
  133. }
  134. public function checkFunctionsExists() {
  135. if(!is_writable('./')){
  136. $this->logs['errors'][] = _t('INSTALL_ERROR_RIGHT', array(str_replace(basename(__FILE__),'',__FILE__)));
  137. }else{
  138. $this->logs['notices'][] = _t('INSTALL_INFO_RIGHT');
  139. }
  140. if (!@function_exists('mysqli_connect')){
  141. $this->logs['errors'][] = _t('INSTALL_ERROR_MYSQLICONNECT');
  142. }else{
  143. $this->logs['notices'][] = _t('INSTALL_INFO_MYSQLICONNECT');
  144. }
  145. if (!@function_exists('file_get_contents')){
  146. $this->logs['errors'][] = _t('INSTALL_ERROR_FILEGET');
  147. }else{
  148. $this->logs['notices'][] = _t('INSTALL_INFO_FILEGET');
  149. }
  150. if (!@function_exists('file_put_contents')){
  151. $this->logs['errors'][] = _t('INSTALL_ERROR_FILEPUT');
  152. }else{
  153. $this->logs['notices'][] = _t('INSTALL_INFO_FILEPUT');
  154. }
  155. if (!@function_exists('curl_exec')){
  156. $this->logs['errors'][] = _t('INSTALL_ERROR_CURL');
  157. }else{
  158. $this->logs['notices'][] = _t('INSTALL_INFO_CURL');
  159. }
  160. if (!@function_exists('gd_info')){
  161. $this->logs['errors'][] = _t('INSTALL_ERROR_GD');
  162. }else{
  163. $this->logs['notices'][] = _t('INSTALL_INFO_GD');
  164. }
  165. if (@version_compare(PHP_VERSION, '5.1.0') <= 0){
  166. $this->logs['errors'][] = _t('INSTALL_ERROR_PHPV', array(PHP_VERSION));
  167. }else{
  168. $this->logs['notices'][] = _t('INSTALL_INFO_PHPV', array(PHP_VERSION));
  169. }
  170. if(ini_get('safe_mode') && ini_get('max_execution_time')!=0){
  171. $this->logs['errors'][] = _t('INSTALL_ERROR_SAFEMODE');
  172. }else{
  173. $this->logs['notices'][] = _t('INSTALL_INFO_SAFEMODE');
  174. }
  175. }
  176. public function setFinished($finished) {
  177. $this->finished = $finished;
  178. }
  179. public function getFinished() {
  180. return $this->finished;
  181. }
  182. }