Install.class.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. define('VERSION_NUMBER','1.6');
  71. define('VERSION_NAME','Dev');
  72. //Host de Mysql, le plus souvent localhost ou 127.0.0.1
  73. define('MYSQL_HOST','{$this->options['db']['mysqlHost']}');
  74. //Identifiant MySQL
  75. define('MYSQL_LOGIN','{$this->options['db']['mysqlLogin']}');
  76. //mot de passe MySQL
  77. define('MYSQL_MDP','{$this->options['db']['mysqlMdp']}');
  78. //Nom de la base MySQL ou se trouvera leed
  79. define('MYSQL_BDD','{$this->options['db']['mysqlBase']}');
  80. //Prefix des noms des tables leed pour les bases de données uniques
  81. define('MYSQL_PREFIX','{$this->options['db']['mysqlPrefix']}');
  82. ?>";
  83. file_put_contents(self::CONSTANT_FILE, $constant);
  84. if (!is_readable(self::CONSTANT_FILE))
  85. die('"'.self::CONSTANT_FILE.'" not found!');
  86. }
  87. protected function createConfig() {
  88. $configurationManager = new Configuration();
  89. if ($configurationManager->tableExists()) {
  90. $configurationManager->truncate();
  91. }
  92. $configurationManager->create();
  93. $configurationManager->setDefaults();
  94. $this->salt = $configurationManager->get('cryptographicSalt');
  95. }
  96. protected function createUser() {
  97. $userManager = new User();
  98. if ($userManager->tableExists()) {
  99. $userManager->truncate();
  100. }
  101. $userManager->create();
  102. $userManager->add($this->options['user']['login'], $this->options['user']['password'], $this->salt, $this->logger);
  103. $_SESSION['currentUser'] = serialize($userManager->get($this->options['user']['login']));
  104. }
  105. protected function checkLoginPassword() {
  106. if(
  107. empty($this->options['user']['password'])
  108. || empty($this->options['user']['login'])
  109. ) {
  110. $this->logs['errors'][] = _t('INSTALL_ERROR_USERPWD');
  111. return false;
  112. }
  113. return true;
  114. }
  115. protected function checkDb() {
  116. if(
  117. empty($this->options['db']['mysqlHost'])
  118. || empty($this->options['db']['mysqlLogin'])
  119. || empty($this->options['db']['mysqlBase'])
  120. ) {
  121. $this->logs['errors'][] = _t('INSTALL_ERROR_DB_INFOS');
  122. return false;
  123. }
  124. if (!Functions::testDb(
  125. $this->options['db']['mysqlHost'], $this->options['db']['mysqlLogin'], $this->options['db']['mysqlMdp'], $this->options['db']['mysqlBase']
  126. )) {
  127. $this->logs['errors'][] = _t('INSTALL_ERROR_CONNEXION');
  128. } else {
  129. $this->logs['notices'][] = _t('INSTALL_INFO_CONNEXION');
  130. }
  131. return true;
  132. }
  133. public function hasErrors() {
  134. return count($this->logs['errors']) > 0;
  135. }
  136. public function checkFunctionsExists() {
  137. if(!is_writable('./')){
  138. $this->logs['errors'][] = _t('INSTALL_ERROR_RIGHT', array(str_replace(basename(__FILE__),'',__FILE__)));
  139. }else{
  140. $this->logs['notices'][] = _t('INSTALL_INFO_RIGHT');
  141. }
  142. if (!@function_exists('mysqli_connect')){
  143. $this->logs['errors'][] = _t('INSTALL_ERROR_MYSQLICONNECT');
  144. }else{
  145. $this->logs['notices'][] = _t('INSTALL_INFO_MYSQLICONNECT');
  146. }
  147. if (!@function_exists('file_get_contents')){
  148. $this->logs['errors'][] = _t('INSTALL_ERROR_FILEGET');
  149. }else{
  150. $this->logs['notices'][] = _t('INSTALL_INFO_FILEGET');
  151. }
  152. if (!@function_exists('file_put_contents')){
  153. $this->logs['errors'][] = _t('INSTALL_ERROR_FILEPUT');
  154. }else{
  155. $this->logs['notices'][] = _t('INSTALL_INFO_FILEPUT');
  156. }
  157. if (!@function_exists('curl_exec')){
  158. $this->logs['errors'][] = _t('INSTALL_ERROR_CURL');
  159. }else{
  160. $this->logs['notices'][] = _t('INSTALL_INFO_CURL');
  161. }
  162. if (@version_compare(PHP_VERSION, '5.1.0') <= 0){
  163. $this->logs['errors'][] = _t('INSTALL_ERROR_PHPV', array(PHP_VERSION));
  164. }else{
  165. $this->logs['notices'][] = _t('INSTALL_INFO_PHPV', array(PHP_VERSION));
  166. }
  167. if(ini_get('safe_mode') && ini_get('max_execution_time')!=0){
  168. $this->logs['errors'][] = _t('INSTALL_ERROR_SAFEMODE');
  169. }else{
  170. $this->logs['notices'][] = _t('INSTALL_INFO_SAFEMODE');
  171. }
  172. }
  173. public function setFinished($finished) {
  174. $this->finished = $finished;
  175. }
  176. public function getFinished() {
  177. return $this->finished;
  178. }
  179. }