common.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. session_name('yana-server');
  3. session_start();
  4. $start=microtime(true);
  5. ini_set('display_errors','1');
  6. error_reporting(E_ALL & ~E_NOTICE);
  7. //Calage de la date
  8. setlocale( LC_ALL , "fr_FR" );
  9. date_default_timezone_set('Europe/Paris');
  10. //Idleman : Active les notice uniquement pour ma config reseau (pour le débug), pour les user il faut la désactiver
  11. //car les notices peuvent gener les reponses json, pour les dev ajoutez votre config dans une même if en dessous.
  12. if($_SERVER["HTTP_HOST"]=='192.168.0.14' && $_SERVER['REMOTE_ADDR']=='192.168.0.69') error_reporting(E_ALL);
  13. mb_internal_encoding('UTF-8');
  14. if(!file_exists(__DIR__ .DIRECTORY_SEPARATOR.'constant.php')) header('location:install.php');
  15. require_once(__DIR__ .DIRECTORY_SEPARATOR.'constant.php');
  16. global $myUser,$conf,$_;
  17. //Récuperation et sécurisation de toutes les variables POST et GET
  18. $_ = array_map('Functions::secure',array_merge($_POST,$_GET));
  19. $error = '';
  20. $versions = json_decode(file_get_contents(__ROOT__.DIRECTORY_SEPARATOR.'db.json'),true);
  21. if(!file_exists(__ROOT__.DIRECTORY_SEPARATOR.DB_NAME) || (file_exists(__ROOT__.DIRECTORY_SEPARATOR.DB_NAME) && filesize(__ROOT__.DIRECTORY_SEPARATOR.DB_NAME)==0)){
  22. file_put_contents(__ROOT__.'/dbversion',$versions[0]['version']);
  23. header('location:install.php');
  24. }else{
  25. if(file_exists(__ROOT__.DIRECTORY_SEPARATOR.'install.php')) $error .= ($error!=''?'<br/>':'').'<strong>Attention: </strong> Par mesure de sécurité, pensez à supprimer le fichier install.php';
  26. }
  27. if(file_exists(__ROOT__.DIRECTORY_SEPARATOR.'db.json')){
  28. if(!file_exists(__ROOT__.DIRECTORY_SEPARATOR.'dbversion')) file_put_contents(__ROOT__.DIRECTORY_SEPARATOR.'dbversion', '0');
  29. $current = file_get_contents(__ROOT__.DIRECTORY_SEPARATOR.'dbversion');
  30. $versions = json_decode(file_get_contents(__ROOT__.DIRECTORY_SEPARATOR.'db.json'),true);
  31. if($current<$versions[0]['version']){
  32. Functions::alterBase($versions,$current);
  33. file_put_contents(__ROOT__.DIRECTORY_SEPARATOR.'dbversion',$versions[0]['version']);
  34. }
  35. }
  36. require_once(__ROOT__.DIRECTORY_SEPARATOR.'RainTPL.php');
  37. $error = (isset($_['error']) && $_['error']!=''?'<strong>Erreur: </strong> '.str_replace('|','<br/><strong>Erreur: </strong> ',(urldecode($_['error']))):false);
  38. $message = (isset($_['notice']) && $_['notice']!=''?'<strong>Message: </strong> '.str_replace('|','<br/><strong>Message: </strong> ',(urldecode($_['notice']))):false);
  39. function __autoload($class_name){
  40. require_once(__ROOT__.DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR.$class_name . '.class.php');
  41. }
  42. if(file_exists(__ROOT__.DIRECTORY_SEPARATOR.'.tool.php')){
  43. require_once(__ROOT__.DIRECTORY_SEPARATOR.'.tool.php');
  44. switch($tool->type){
  45. case 'reset_password':
  46. if($tool->login != null && $tool->password != null){
  47. $userManager = new User();
  48. $usr = $userManager->load(array('login'=>$tool->login));
  49. $usr->setPassword($tool->password);
  50. $usr->save();
  51. unlink(__ROOT__.DIRECTORY_SEPARATOR.'.tool.php');
  52. }
  53. break;
  54. }
  55. }
  56. $myUser = false;
  57. $conf = new Configuration();
  58. $conf->getAll();
  59. //Inclusion des plugins
  60. Plugin::includeAll($conf->get("DEFAULT_THEME"));
  61. $userManager = new User();
  62. if(isset($_SESSION['currentUser'])){
  63. $myUser =unserialize($_SESSION['currentUser']);
  64. }else{
  65. if(AUTO_LOGIN!=''){
  66. $myUser = $userManager->exist(AUTO_LOGIN,'',true);
  67. $_SESSION['currentUser'] = serialize($myUser);
  68. }
  69. }
  70. if(!$myUser && isset($_COOKIE[$conf->get('COOKIE_NAME')])){
  71. $users = User::getAllUsers();
  72. foreach ($users as $user) {
  73. if($user->getCookie() == $_COOKIE[$conf->get('COOKIE_NAME')])
  74. {
  75. $myUser = $user;
  76. $myUser->loadRight();
  77. }
  78. }
  79. }
  80. //Instanciation du template
  81. $tpl = new RainTPL();
  82. //Definition des dossiers de template
  83. raintpl::configure("base_url", null );
  84. raintpl::configure("tpl_dir", './templates/'.$conf->get('DEFAULT_THEME').'/' );
  85. raintpl::configure("cache_dir", './cache/tmp/' );
  86. $view = '';
  87. $rank = new Rank();
  88. if($myUser!=false && $myUser->getRank()!=false){
  89. $rank = $rank->getById($myUser->getRank());
  90. }
  91. function common_listen($command,$text,$confidence,$user){
  92. echo "\n".'diction de la commande : '.$command;
  93. $response = array();
  94. Plugin::callHook("vocal_command", array(&$response,YANA_URL.'/action.php'));
  95. $commands = array();
  96. echo "\n".'Test de comparaison avec '.count($response['commands']).' commandes';
  97. foreach($response['commands'] as $cmd){
  98. if($command != $cmd['command']) continue;
  99. if(!isset($cmd['parameters'])) $cmd['parameters'] = array();
  100. if(isset($cmd['callback'])){
  101. //Catch des commandes pour les plugins en format client v2
  102. echo "\n".'Commande trouvée, execution de la fonction plugin '.$cmd['callback'];
  103. call_user_func($cmd['callback'],$text,$confidence,$cmd['parameters'],$user);
  104. }else{
  105. //Catch des commandes pour les plugins en format client v1
  106. echo "\n".'Commande ancien format trouvée, execution de l\'url '.$cmd['url'].'&token='.$user->getToken();
  107. $result = file_get_contents($cmd['url'].'&token='.$user->getToken());
  108. $result = json_decode($result,true);
  109. if(is_array($result)){
  110. $client=new Client();
  111. $client->connect();
  112. if(is_array($result['responses'])){
  113. foreach($result['responses'] as $resp){
  114. switch($resp['type']){
  115. case 'talk':
  116. $client->talk($resp['sentence']);
  117. break;
  118. case 'sound':
  119. $client->sound($resp['file']);
  120. break;
  121. case 'command':
  122. $client->execute($resp['program']);
  123. break;
  124. }
  125. }
  126. }
  127. $client->disconnect();
  128. }
  129. }
  130. }
  131. }
  132. Plugin::addHook("listen", "common_listen");
  133. $tpl->assign('myUser',$myUser);
  134. $tpl->assign('userManager',$userManager);
  135. $tpl->assign('configurationManager',$conf);
  136. $tpl->assign('error',$error);
  137. $tpl->assign('notice',$message);
  138. $tpl->assign('_',$_);
  139. $tpl->assign('action','');
  140. $tpl->assign('rank',$rank);
  141. ?>