install.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. try {
  3. date_default_timezone_set('Europe/Paris');
  4. mb_internal_encoding('UTF-8');
  5. require_once(__DIR__.'/function.php');
  6. spl_autoload_register('app_autoloader');
  7. $_ = array_map('secure_user_vars', array_merge($_POST, $_GET));
  8. require_once('class/Plugin.class.php');
  9. $entityFolder = __DIR__.'/class/';
  10. if(file_exists(__DIR__.DIRECTORY_SEPARATOR.'.git') && !file_exists(__DIR__.DIRECTORY_SEPARATOR.'.git'.DIRECTORY_SEPARATOR.'.htaccess')){
  11. file_put_contents(__DIR__.DIRECTORY_SEPARATOR.'.git'.DIRECTORY_SEPARATOR.'.htaccess', 'deny for all');
  12. }
  13. $custom = array('css' => '','js' => '','action' => '');
  14. //Recherche de custom install dans les plugins
  15. foreach(glob(__DIR__.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'*'.DIRECTORY_SEPARATOR.'app.json') as $app){
  16. $manifest = json_decode(file_get_contents($app),true);
  17. if(!$manifest || !isset($manifest['install'])) continue;
  18. $custom = $manifest['install'];
  19. $custom['plugin'] = 'plugin/'.basename(dirname($app)).'/';
  20. $custom['pluginPath'] = __DIR__.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.basename(dirname($app)).DIRECTORY_SEPARATOR;
  21. }
  22. ?>
  23. <!DOCTYPE html>
  24. <html lang="fr">
  25. <head>
  26. <meta charset="utf-8">
  27. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  28. <meta name="description" content="">
  29. <meta name="author" content="">
  30. <link rel="icon" type="image/png" href="favicon.png" />
  31. <title>Installateur</title>
  32. <!-- Bootstrap core CSS -->
  33. <link href="css/bootstrap.min.css" rel="stylesheet">
  34. <!-- Font awesome -->
  35. <link rel="stylesheet" href="css/fontawesome-all.min.css">
  36. <!-- Custom styles for this template -->
  37. <link href="css/main.css" rel="stylesheet">
  38. <?php if(!empty($custom['css'])): ?>
  39. <link href="<?php echo $custom['plugin'].$custom['css']; ?>" rel="stylesheet">
  40. <?php endif; ?>
  41. </head>
  42. <body>
  43. <!-- Fixed navbar -->
  44. <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
  45. <a class="navbar-brand" href="index.php"><img style="max-height: 40px;" src="img/logo/default-logo.png"> Installation</a>
  46. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
  47. <span class="navbar-toggler-icon"></span>
  48. </button>
  49. </nav>
  50. <!-- Begin page content -->
  51. <div class="container">
  52. <?php
  53. $entities = array();
  54. foreach(glob(__DIR__.'/connector/*.class.php') as $classFile){
  55. require_once($classFile);
  56. $className = str_replace('.class.php','',basename($classFile));
  57. $entities[$className] = $className::label.' - '.$className::description;
  58. }
  59. //check prerequisite
  60. if(file_exists(__DIR__.'/constant.php')) throw new Exception('Le script est déja installé, pour recommencer l\'installation, supprimez le fichier constant.php');
  61. if(!is_writable (__DIR__)) throw new Exception('Le dossier '.__DIR__.' doit être accessible en ecriture, merci de taper la commande linux <br/><code>sudo chown -R www-data:www-data '.__ROOT__.'</code><br/> ou de régler le dossier en écriture via votre client ftp');
  62. if(!file_exists(__DIR__.'/file')) mkdir(__DIR__.'/file',0755,true);
  63. if(!file_exists(__DIR__.'/file/avatar')) mkdir(__DIR__.'/file/avatar',0755,true);
  64. //if(!extension_loaded('gd') || !function_exists('gd_info')) throw new Exception('L\'extension php GD2 est requise, veuillez installer GD2 (sous linux : <code>sudo apt-get install php5-gd && service apache2 restart</code>)');
  65. //if(!in_array('sqlite',PDO::getAvailableDrivers())) throw new Exception('Le driver SQLITE est requis, veuillez installer sqlite3 (sous linux : <code>sudo apt-get install php5-sqlite && service apache2 restart</code>)');
  66. if(isset($_['install'])){
  67. $constantStream = file_get_contents(__DIR__.'/constant-sample.php');
  68. if(!isset($_['host'])) $_['host'] = '';
  69. if(!isset($_['login'])) $_['login'] = '';
  70. if(!isset($_['password'])) $_['password'] = '';
  71. if(!isset($_['database'])) $_['database'] = '';
  72. $cryptKey = base64_encode(time().$_['login'].mt_rand(0,1000));
  73. $constantStream = str_replace(
  74. array("{{BASE_SGBD}}","{{BASE_HOST}}","{{BASE_NAME}}","{{BASE_LOGIN}}","{{BASE_PASSWORD}}","{{ROOT_URL}}","{{CRYPT_KEY}}"),
  75. array($_['entity'],$_['host'],$_['name'],$_['login'],$_['password'],$_['root'],$cryptKey),$constantStream
  76. );
  77. file_put_contents(__DIR__.'/constant.php',$constantStream);
  78. require_once(__DIR__.'/constant.php');
  79. require_once(__ROOT__.'class'.SLASH.'Entity.class.php');
  80. //install entities
  81. Entity::install(__ROOT__.'class');
  82. global $conf,$myUser;
  83. $conf = new Configuration();
  84. $conf->getAll();
  85. //create firm
  86. $firm = new Firm();
  87. $firm->label = 'Établissement';
  88. $firm->description = 'Établissement par défaut';
  89. $firm->save();
  90. //create admin rank
  91. $rank = new Rank();
  92. $rank->label = 'Administrateur';
  93. $rank->description = 'Dispose de tous les accès';
  94. $rank->save();
  95. //create default user
  96. $admin = new User();
  97. $admin->login = 'admin';
  98. $admin->password = User::password_encrypt('admin');
  99. $admin->firstname = 'Administrateur';
  100. $admin->name = 'SYS1';
  101. $admin->superadmin = 1;
  102. $admin->rank = $rank->id;
  103. $admin->state = User::ACTIVE;
  104. $admin->save();
  105. $_SESSION['currentUser'] = serialize($admin);
  106. $myUser = $admin;
  107. $userfirmrank = new UserFirmRank();
  108. $userfirmrank->user = $admin->login;
  109. $userfirmrank->firm = $firm->id;
  110. $userfirmrank->save();
  111. $sections = array();
  112. Plugin::callHook('section',array(&$sections));
  113. foreach($sections as $section=>$description){
  114. $right = new Right();
  115. $right->rank = $rank->id;
  116. $right->section = $section;
  117. $right->read = true;
  118. $right->edit = true;
  119. $right->delete = true;
  120. $right->configure = true;
  121. $right->save();
  122. }
  123. $enablePlugins = array('fr.sys1.factory','fr.sys1.dashboard','fr.sys1.notification','fr.sys1.navigation');
  124. if(!empty($custom['action'])) require_once($custom['pluginPath'].$custom['action']);
  125. //Activation des plugins par défaut
  126. foreach ($enablePlugins as $plugin) {
  127. Plugin::state($plugin,true);
  128. }
  129. $states = Plugin::states();
  130. //Activation des plugins pour les établissements
  131. foreach(Firm::loadAll() as $firm){
  132. foreach ($enablePlugins as $plugin) {
  133. $firms = $states[$plugin];
  134. $key = array_search($firm->id, $firms);
  135. $firms[] = $firm->id;
  136. $states[$plugin] = array_values($firms);
  137. Plugin::states($states);
  138. }
  139. }
  140. ?>
  141. <div class="alert alert-success alert-dismissable">
  142. <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
  143. <strong>Succès!</strong> La base est bien installée, l'utilisateur par défaut est <code>admin:admin</code>, pensez à changer le mot de passe rapidemment. <br>
  144. </div>
  145. <a class="btn btn-primary" href="index.php">Revenir à l'index</a>
  146. <?php
  147. } else {
  148. $root = 'http'.((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')|| $_SERVER['SERVER_PORT'] == 443?'s':'').'://'.$_SERVER['HTTP_HOST'].($_SERVER['SERVER_PORT']==80?'':':'.$_SERVER['SERVER_PORT']).$_SERVER['REQUEST_URI'];
  149. $root = str_replace("/install.php", "", $root );
  150. $parts = explode('?',$root);
  151. $root = array_shift($parts);
  152. ?>
  153. <div class="row">
  154. <form class="col-md-12 mt-3" action="install.php" method="POST">
  155. <h3>Installation</h3>
  156. <p>Merci de bien vouloir remplir les champs ci-dessous</p>
  157. <label for="entity">Base de donnée</label>
  158. <select class="form-control" name="entity" onchange="window.location='install.php?sgbd='+$(this).val()">
  159. <option value="">-</option>
  160. <?php foreach($entities as $class=>$label): ?>
  161. <option <?php echo (isset($_['sgbd']) && $_['sgbd']==$class ? 'selected="selected"': '') ?> value="<?php echo $class ?>"><?php echo $label; ?></option>
  162. <?php endforeach; ?>
  163. </select><br/>
  164. <?php if(isset($_['sgbd']) && $_['sgbd']!=''):
  165. require_once(__DIR__.'/connector/'.$_['sgbd'].'.class.php');
  166. foreach($_['sgbd']::fields() as $field): ?>
  167. <label for="<?php echo $field['id']; ?>"><?php echo $field['label']; ?></label><br/>
  168. <?php if(!isset($field['comment'])): ?><small><?php echo $field['comment']; ?></small><br/><?php endif; ?>
  169. <input type="text" class="form-control" value="<?php echo $field['default']; ?>" name="<?php echo $field['id']; ?>" id="<?php echo $field['id']; ?>"/><br/>
  170. <?php endforeach; ?>
  171. <label for="root">Adresse web</label><br/>
  172. <input type="text" class="form-control" name="root" id="root" value="<?php echo $root; ?>"/><br/>
  173. <input type="submit" class="btn btn-primary" value="Installer" name="install"><br/><br/>
  174. <?php endif; ?>
  175. </form>
  176. </div>
  177. <?php
  178. if(!empty($custom['form'])) require_once($custom['pluginPath'].$custom['form']);
  179. }
  180. } catch (Exception $e) { ?>
  181. <div class="alert alert-danger">
  182. <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
  183. <strong>Oops!</strong> <?php echo $e->getMessage().' - '.$e->getFile().' L'.$e->getLine().'<hr/><pre>'.$e->getTraceAsString().'</pre>';
  184. ?>
  185. </div>
  186. <?php
  187. } ?>
  188. </div>
  189. <!-- Bootstrap core JavaScript -->
  190. <!-- Placed at the end of the document so the pages load faster -->
  191. <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
  192. <script>window.jQuery || document.write('<script src="js/vendor/jquery.min.js"><\/script>')</script>
  193. <script src="js/vendor/popper.min.js"></script>
  194. <script src="js/bootstrap.min.js"></script>
  195. <script src="js/vendor/mustache.min.js"></script>
  196. <script src="js/plugins.js"></script>
  197. <script src="js/main.js"></script>
  198. <?php if(!empty($custom['js'])): ?>
  199. <link href="<?php echo $custom['plugin'].$custom['js']; ?>" rel="stylesheet">
  200. <?php endif; ?>
  201. </body>
  202. </html>