install.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /*
  3. */
  4. session_start();
  5. date_default_timezone_set('Europe/Paris');
  6. //TODO cron auto install
  7. // echo "*/1 * * * * root wget http://127.0.0.1/yana-server/action.php?action=crontab -O /dev/null 2>&1" > /etc/cron.d/yana-server
  8. unset($myUser);
  9. error_reporting(E_ALL);
  10. ini_set('display_errors','On');
  11. if(!file_exists(__DIR__.'/constant.php')) copy(__DIR__.'/constant.sample.php', __DIR__.'/constant.php');
  12. require_once(__DIR__.'/constant.php');
  13. function __autoload($class_name) {
  14. include 'classes/'.$class_name . '.class.php';
  15. }
  16. ?>
  17. <!DOCTYPE html>
  18. <html lang="fr">
  19. <head>
  20. <meta charset="utf-8">
  21. <title>Installation</title>
  22. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  23. <meta name="description" content="">
  24. <meta name="author" content="">
  25. <link rel="shortcut icon" type="image/x-icon" href="img/favicon.ico">
  26. <!-- Le styles -->
  27. <link href="templates/default/css/bootstrap.min.css" rel="stylesheet">
  28. <link href="templates/default/css/jquery-ui-1.10.3.custom.css" rel="stylesheet">
  29. <link href="templates/default/css/style.css" rel="stylesheet">
  30. <link href="templates/default/css/bootstrap-responsive.min.css" rel="stylesheet">
  31. <link rel="shortcut icon" href="ico/favicon.png">
  32. </head>
  33. <body>
  34. <div class="navbar navbar-inverse navbar-fixed-top" id="header">
  35. <div class="navbar-inner">
  36. <div class="container">
  37. <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
  38. </button>
  39. <a class="brand" href="index.php"><?php echo PROGRAM_NAME; ?></a>
  40. <div class="nav-collapse collapse">
  41. <ul class="nav">
  42. </ul>
  43. </div><!--/.nav-collapse -->
  44. </div>
  45. </div>
  46. </div>
  47. <div id="body" class="container">
  48. <?php
  49. //On récupère le chemin http de yana
  50. $path_yana = substr($_SERVER['SCRIPT_FILENAME'],0,-11);
  51. if(isset($_POST['install'])){
  52. try{
  53. if(!isset($_POST['password']) || trim($_POST['password'])=='' || !isset($_POST['login']) || trim($_POST['login'])=='' )
  54. throw new Exception("L'identifiant et le mot de passe ne peuvent être vide");
  55. //Supression de l'ancienne base si elle existe
  56. if(file_exists(DB_NAME) && filesize(DB_NAME)>0) throw new Exception("La base ".DB_NAME." existe déjà, pour recommencer l'installation, merci de supprimer le fichier ".DB_NAME." puis de revenir sur cette page");
  57. //Instanciation des managers d'entités
  58. $user = new User();
  59. $configuration = new Configuration();
  60. $right = new Right();
  61. $rank = new Rank();
  62. $section = new Section();
  63. $event = new Event();
  64. $client = new Client();
  65. $device = new Device();
  66. $personnality = new Personality();
  67. if(isset($_POST['url'])){
  68. $const = file_get_contents("constant.php");
  69. file_put_contents('constant.php', (preg_replace("/(define\(\'YANA_URL\'\,\')(.*)('\)\;)/", "$1".$_POST['url']."$3", $const)));
  70. }
  71. //Création des tables SQL
  72. $configuration->create();
  73. $user->create();
  74. $right->create();
  75. $rank->create();
  76. $section->create();
  77. $event->create();
  78. $device->create();
  79. $personnality->create();
  80. $personnality->birth();
  81. $configuration->put('UPDATE_URL','http://update.idleman.fr/yana?callback=?');
  82. $configuration->put('DEFAULT_THEME','default');
  83. $configuration->put('COOKIE_NAME','yana');
  84. $configuration->put('COOKIE_LIFETIME','7');
  85. $configuration->put('VOCAL_ENTITY_NAME','YANA');
  86. $configuration->put('PROGRAM_VERSION','3.0.6');
  87. $configuration->put('HOME_PAGE','index.php');
  88. $configuration->put('VOCAL_SENSITIVITY','0.0');
  89. $configuration->put('YANA_LATITUDE','24.8235817');
  90. $configuration->put('YANA_LONGITUDE','-75.5070352');
  91. //Création du rang admin
  92. $rank = new Rank();
  93. $rank->setLabel('admin');
  94. $rank->save();
  95. //Déclaration des sections du programme
  96. $sections = array('event','vocal','user','plugin','configuration','admin');
  97. //Création des sections déclarées et attribution de tous les droits sur toutes ces sections pour l'admin
  98. foreach($sections as $sectionName){
  99. $s = New Section();
  100. $s->setLabel($sectionName);
  101. $s->save();
  102. $r = New Right();
  103. $r->setSection($s->getId());
  104. $r->setRead('1');
  105. $r->setDelete('1');
  106. $r->setCreate('1');
  107. $r->setUpdate('1');
  108. $r->setRank($rank->getId());
  109. $r->save();
  110. }
  111. $personalities = array('John Travolta','Jeff Buckley','Tom Cruise','John Lennon','Emmet Brown','Geo trouvetou','Luke Skywalker','Mac Gyver','Marty McFly','The Doctor');
  112. $im = $personalities[rand(0,count($personalities)-1)];
  113. list($fn,$n) = explode(' ',$im);
  114. //Creation du premier compte et assignation en admin
  115. $user->setMail($_POST['email']);
  116. $user->setPassword($_POST['password']);
  117. $user->setLogin($_POST['login']);
  118. $user->setFirstName($fn);
  119. $user->setName($n);
  120. $user->setToken(sha1(time().rand(0,1000)));
  121. $user->setState(1);
  122. $user->setRank($rank->getId());
  123. $user->save();
  124. global $myUser;
  125. $myUser = $user;
  126. foreach(array('radioRelay','wireRelay','vocal_infos','speechcommands','profile','room','story','dashboard','dashboard-monitoring') as $plugin):
  127. Plugin::enabled($plugin.'-'.$plugin);
  128. endforeach;
  129. $notices = array();
  130. if(function_exists('curl_init')){
  131. $url="http://idleman.fr/yana/notice.php?code=justavoidspamrequest";
  132. $ch = curl_init();
  133. curl_setopt($ch, CURLOPT_URL, $url);
  134. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  135. curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
  136. $html = curl_exec($ch);
  137. curl_close($ch);
  138. if($html!==false)
  139. $notices = json_decode($html,true);
  140. if(!is_array($notices)) $notices = array();
  141. }
  142. ?>
  143. <div class="alert alert-info">
  144. <button type="button" class="close" data-dismiss="alert">&times;</button>
  145. <strong>Installation terminée: </strong> L'installation est terminée, vous devez supprimer le fichier <code>yana-server/install.php</code> par mesure de sécurité, puis revenir sur <a class="brand" href="index.php">l'accueil</a>.
  146. </div>
  147. <?php foreach($notices as $notice): ?>
  148. <div class="alert alert-<?php echo $notice['type']; ?>">
  149. <button type="button" class="close" data-dismiss="alert">&times;</button>
  150. <strong><?php echo $notice['title']; ?>: </strong> <?php echo $notice['content']; ?>.
  151. </div
  152. <?php endforeach; ?>
  153. <?php }catch(Exception $e){ ?>
  154. <div class="alert alert-error">
  155. <button type="button" class="close" data-dismiss="alert">&times;</button>
  156. <strong>Echec de l'Installation : </strong> <?php echo $e->getMessage(); ?> <a class="brand" href="install.php">Réessayer</a>.
  157. </div>
  158. <?php
  159. }
  160. }else{
  161. ?>
  162. <?php
  163. /*tests*/
  164. $tests = array();
  165. if(!is_writable($path_yana)) $tests['error'][] = "Le dossier <b>".$path_yana."</b> n'est pas accessible en écriture. <br/>Pour résoudre ce problème, merci de taper la commande suivante dans le shell <code>sudo chown -R www-data:www-data ".$path_yana."</code> ";
  166. if(!class_exists('SQLite3')) $tests['error'][] = "Le pré-requis SQLITE3 n'est pas installé. <br/>Pour résoudre ce problème, merci de taper la commande suivante dans le shell <code>sudo apt-get install sqlite3 php-sqlite3</code> ";
  167. $out = exec('whereis gpio',$out);
  168. if($out == ''){
  169. $tests['warning'][] = "La librairie Wiring pi ne semble pas installé sur le rpi, merci de vérifier l'existence du binaire GPIO sur la machine.";
  170. }else{
  171. require_once(__DIR__.'/classes/Gpio.class.php');
  172. $out = trim(str_replace('gpio: ','',$out));
  173. if($out != GPIO::GPIO_DEFAULT_PATH) $tests['warning'][] = "Le chemin de l'executable de wiring pi est à modifier dans classes/Gpio.class.php, remplacer <code>".GPIO::GPIO_DEFAULT_PATH."</code> par <code>".$out."</code>.";
  174. }
  175. if(function_exists('posix_getpwuid')){
  176. $permissions = array('root:www-data'=>'plugins/relay/radioEmission');
  177. foreach($permissions as $key=>$file){
  178. if(file_exists($file)){
  179. list($o,$g) = explode(':',$key);
  180. $owner = posix_getpwuid(fileowner($file));
  181. $group = posix_getgrgid(filegroup($file));
  182. if($owner['name']!=$o || $group['name'] !=$g) $tests['warning'][] = 'Le fichier <strong>'.$path_yana.$file.'</strong> devrait avoir <i>'.$o.'</i> comme proprietaire et <i>'.$g.'</i> comme groupe, <strong>'.$path_yana.$file.'</strong> pourrait ne pas fonctionner comme attendu, pour résoudre le problème, tapez la commande <code>sudo chown root:www-data '.$path_yana.$file.' && sudo chmod +s '.$path_yana.$file.'</code>';
  183. }
  184. }
  185. }else{
  186. $tests['warning'][] = 'Impossible de vérifier les droits sur les fichiers sensibles, librairie posix manquante';
  187. }
  188. foreach($tests as $type=>$messages){
  189. foreach($messages as $message){
  190. echo
  191. '<div class="alert alert-'.$type.'">
  192. <strong>'.$type.': </strong> '.$message.'
  193. </div>';
  194. }
  195. }
  196. if(!isset($tests['error'])){
  197. if(strpos($_SERVER['HTTP_HOST'], ':') !==false){
  198. list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
  199. }else{
  200. $host = $_SERVER['HTTP_HOST'];
  201. $port = 80;
  202. }
  203. $actionUrl = 'http://'.$host.':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
  204. $actionUrl = str_replace("/install.php", "", $actionUrl );
  205. ?>
  206. <div class="alert alert-info">
  207. <button type="button" class="close" data-dismiss="alert">&times;</button>
  208. <strong>Installation: </strong> Vous devez remplir le formulaire ci dessous pour installer l'application.
  209. </div>
  210. <form class="form-horizontal" action="install.php" method="POST">
  211. <div class="control-group">
  212. <label class="control-label" for="inputLogin">Login</label>
  213. <div class="controls">
  214. <input type="text" name="login" id="inputLogin" placeholder="Login">
  215. </div>
  216. </div>
  217. <div class="control-group">
  218. <label class="control-label" for="inputPassword">Password</label>
  219. <div class="controls">
  220. <input type="password" name="password" name="inputPassword" placeholder="Password">
  221. </div>
  222. </div>
  223. <div class="control-group">
  224. <label class="control-label" for="inputEmail">Email</label>
  225. <div class="controls">
  226. <input type="text" name="email" id="inputEmail" placeholder="Email">
  227. </div>
  228. </div>
  229. <div class="control-group">
  230. <label class="control-label" for="inputUrl">Adresse de yana</label>
  231. <div class="controls">
  232. <input type="text" name="url" id="inputUrl" placeholder="http://" value="<?php echo $actionUrl; ?>">
  233. </div>
  234. </div>
  235. <div class="control-group">
  236. <div class="controls">
  237. <button type="submit" name="install" class="btn">Installer</button>
  238. </div>
  239. </div>
  240. </form>
  241. <?php }} ?>
  242. <div class="navbar navbar-inverse navbar-fixed-bottom" id="footer">CC by nc sa <?php echo PROGRAM_NAME ?>
  243. </div>
  244. </div> <!-- /container -->
  245. <!-- Le javascript
  246. ================================================== -->
  247. <script src="templates/default/js/jquery.min.js"></script>
  248. <script src="templates/default/js/bootstrap.min.js"></script>
  249. <script src="templates/default/js/jquery.ui.custom.min.js"></script>
  250. <script src="templates/default/js/jquery.yana.js"></script>
  251. <script src="templates/default/js/script.js"></script>
  252. </body>
  253. </html>