install.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. ?>
  14. <!DOCTYPE html>
  15. <html lang="fr">
  16. <head>
  17. <meta charset="utf-8">
  18. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  19. <meta name="description" content="">
  20. <meta name="author" content="">
  21. <link rel="icon" type="image/png" href="favicon.png" />
  22. <title>Installateur</title>
  23. <!-- Bootstrap core CSS -->
  24. <link href="css/bootstrap.min.css" rel="stylesheet">
  25. <!-- Font awesome -->
  26. <link rel="stylesheet" href="css/fontawesome-all.min.css">
  27. <!-- Custom styles for this template -->
  28. <link href="css/main.css" rel="stylesheet">
  29. <link href="css/theme.css" rel="stylesheet">
  30. <!-- Plugin css files -->
  31. <?php echo Plugin::callCss("css"); ?>
  32. </head>
  33. <body>
  34. <!-- Fixed navbar -->
  35. <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
  36. <a class="navbar-brand" href="install.php">Installateur</a>
  37. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
  38. <span class="navbar-toggler-icon"></span>
  39. </button>
  40. <div class="collapse navbar-collapse" id="navbarCollapse">
  41. <ul class="navbar-nav mr-auto">
  42. <li class="nav-item active"><a class="nav-link" href="install.php">Installation <span class="sr-only">(current)</span></a></li>
  43. </ul>
  44. </div>
  45. </nav>
  46. <!-- Begin page content -->
  47. <div class="container">
  48. <?php
  49. $entities = array();
  50. foreach(glob(__DIR__.'/connector/*.class.php') as $classFile){
  51. require_once($classFile);
  52. $className = str_replace('.class.php','',basename($classFile));
  53. $entities[$className] = $className::label.' - '.$className::description;
  54. }
  55. //check prerequisite
  56. if(file_exists(__DIR__.'/constant.php')) throw new Exception('Le script est déja installé, pour recommencer l\'installation, supprimez le fichier constant.php');
  57. 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');
  58. if(!file_exists(__DIR__.'/file')) mkdir(__DIR__.'/file',0755,true);
  59. if(!file_exists(__DIR__.'/file/avatar')) mkdir(__DIR__.'/file/avatar',0755,true);
  60. //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>)');
  61. //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>)');
  62. if(isset($_['install'])){
  63. $constantStream = file_get_contents(__DIR__.'/constant-sample.php');
  64. if(!isset($_['host'])) $_['host'] = '';
  65. if(!isset($_['login'])) $_['login'] = '';
  66. if(!isset($_['password'])) $_['password'] = '';
  67. if(!isset($_['database'])) $_['database'] = '';
  68. $cryptKey = base64_encode(time().$_['login'].mt_rand(0,1000));
  69. $constantStream = str_replace(
  70. array("{{BASE_SGBD}}","{{BASE_HOST}}","{{BASE_NAME}}","{{BASE_LOGIN}}","{{BASE_PASSWORD}}","{{ROOT_URL}}","{{CRYPT_KEY}}"),
  71. array($_['entity'],$_['host'],$_['name'],$_['login'],$_['password'],$_['root'],$cryptKey),$constantStream
  72. );
  73. file_put_contents(__DIR__.'/constant.php',$constantStream);
  74. require_once(__DIR__.'/constant.php');
  75. require_once(__ROOT__.'class'.SLASH.'Entity.class.php');
  76. //install entities
  77. Entity::install(__ROOT__.'class');
  78. global $conf,$myUser;
  79. $conf = new Configuration();
  80. $conf->getAll();
  81. //create firm
  82. $firm = new Firm();
  83. $firm->label = 'Établissement';
  84. $firm->description = 'Établissement par défaut';
  85. $firm->save();
  86. //create admin rank
  87. $rank = new Rank();
  88. $rank->label = 'Administrateur';
  89. $rank->description = 'Dispose de tous les accès';
  90. $rank->save();
  91. //create default user
  92. $admin = new User();
  93. $admin->login = 'admin';
  94. $admin->password = User::password_encrypt('admin');
  95. $admin->firstname = 'Administrateur';
  96. $admin->name = 'SYS1';
  97. $admin->superadmin = 1;
  98. $admin->rank = $rank->id;
  99. $admin->state = User::ACTIVE;
  100. $admin->save();
  101. $_SESSION['currentUser'] = serialize($admin);
  102. $myUser = $admin;
  103. $userfirmrank = new UserFirmRank();
  104. $userfirmrank->user = $admin->login;
  105. $userfirmrank->firm = $firm->id;
  106. $userfirmrank->save();
  107. $sections = array();
  108. Plugin::callHook('section',array(&$sections));
  109. foreach($sections as $section=>$description){
  110. $right = new Right();
  111. $right->rank = $rank->id;
  112. $right->section = $section;
  113. $right->read = true;
  114. $right->edit = true;
  115. $right->delete = true;
  116. $right->configure = true;
  117. $right->save();
  118. }
  119. //Activation des plugins par défaut
  120. foreach (array('fr.sys1.factory','fr.sys1.dashboard','fr.sys1.notification','fr.sys1.navigation') as $plugin) {
  121. Plugin::state($plugin,true);
  122. } ?>
  123. <div class="alert alert-success alert-dismissable">
  124. <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
  125. <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>
  126. </div>
  127. <a class="btn btn-primary" href="index.php">Revenir à l'index</a>
  128. <?php
  129. } else {
  130. $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'];
  131. $root = str_replace("/install.php", "", $root );
  132. $parts = explode('?',$root);
  133. $root = array_shift($parts);
  134. ?>
  135. <div class="row">
  136. <form class="col-md-3" action="install.php" method="POST">
  137. <h3>Installation</h3>
  138. <p>Merci de bien vouloir remplir les champs ci-dessous</p>
  139. <label for="entity">Base de donnée</label>
  140. <select class="form-control" name="entity" onchange="window.location='install.php?sgbd='+$(this).val()">
  141. <option value="">-</option>
  142. <?php foreach($entities as $class=>$label): ?>
  143. <option <?php echo (isset($_['sgbd']) && $_['sgbd']==$class ? 'selected="selected"': '') ?> value="<?php echo $class ?>"><?php echo $label; ?></option>
  144. <?php endforeach; ?>
  145. </select><br/>
  146. <?php if(isset($_['sgbd']) && $_['sgbd']!=''):
  147. require_once(__DIR__.'/connector/'.$_['sgbd'].'.class.php');
  148. foreach($_['sgbd']::fields() as $field): ?>
  149. <label for="<?php echo $field['id']; ?>"><?php echo $field['label']; ?></label><br/>
  150. <?php if(!isset($field['comment'])): ?><small><?php echo $field['comment']; ?></small><br/><?php endif; ?>
  151. <input type="text" class="form-control" value="<?php echo $field['default']; ?>" name="<?php echo $field['id']; ?>" id="<?php echo $field['id']; ?>"/><br/>
  152. <?php endforeach; ?>
  153. <label for="root">Adresse web</label><br/>
  154. <input type="text" class="form-control" name="root" id="root" value="<?php echo $root; ?>"/><br/>
  155. <input type="submit" class="btn btn-primary" value="Installer" name="install"><br/><br/>
  156. <?php endif; ?>
  157. </form>
  158. </div>
  159. <?php
  160. }
  161. } catch (Exception $e) { ?>
  162. <div class="alert alert-danger">
  163. <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
  164. <strong>Oops!</strong> <?php echo $e->getMessage().' - '.$e->getFile().' L'.$e->getLine().'<hr/><pre>'.$e->getTraceAsString().'</pre>';
  165. ?>
  166. </div>
  167. <?php
  168. } ?>
  169. </div>
  170. <footer class="footer">
  171. <div class="container">
  172. <span class="text-muted">Installateur by <a href="mailto:valentin.carruesco@sys1.fr">@valentin carruesco<a></span>
  173. </div>
  174. </footer>
  175. <!-- Bootstrap core JavaScript -->
  176. <!-- Placed at the end of the document so the pages load faster -->
  177. <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
  178. <script>window.jQuery || document.write('<script src="js/vendor/jquery.min.js"><\/script>')</script>
  179. <script src="js/vendor/popper.min.js"></script>
  180. <script src="js/bootstrap.min.js"></script>
  181. <script src="js/vendor/mustache.min.js"></script>
  182. <script src="js/plugins.js"></script>
  183. <script src="js/main.js"></script>
  184. <?php echo Plugin::callJs(); ?>
  185. </body>