domodoor.plugin.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /*
  3. @name DomoDoor
  4. @author Valentin CARRUESCO <idleman@idleman.fr>
  5. @link http://blog.idleman.fr
  6. @licence CC by nc sa
  7. @version 1.0.0
  8. @type module
  9. @description Plugin de gestion d'une porte bluetooth, accès par code + conditions fixées ans les réglages (cf tuto 22 blog.idleman.fr)
  10. */
  11. if(isset($_GET['argv'])){
  12. $argv = explode('|',$_GET['argv']);
  13. }
  14. if(isset($argv)){
  15. require_once(dirname(__FILE__).'/../../constant.php');
  16. $db = new SQLite3(dirname(__FILE__).'/../../'.DB_NAME);
  17. $execQuery = $db->query('SELECT * FROM '.MYSQL_PREFIX.'plugin_door');
  18. while($queryReturn = $execQuery->fetchArray() ){
  19. $allowed_badges[] = $queryReturn['code'];
  20. $badges[$queryReturn['code']] = $queryReturn;
  21. }
  22. $response = 0;
  23. if(count($argv)>1){
  24. list($me,$badge) = $argv;
  25. $badge = trim(substr($badge,2,4));
  26. $user = 0;
  27. if(in_array($badge, $allowed_badges)){
  28. $response = 1;
  29. $user = $badges[$badge]['user'];
  30. }
  31. $execQuery = $db->exec('INSERT INTO '.MYSQL_PREFIX.'plugin_door_log(time,code,user,success)VALUES("'.time().'","'.$badge.'",'.$user.','.$response.')');
  32. }
  33. $db->close();
  34. echo $response;
  35. exit();
  36. }
  37. require_once(dirname(__FILE__).'/DoorAccess.class.php');
  38. require_once(dirname(__FILE__).'/DoorLog.class.php');
  39. function dash_domodoor_plugin_menu(&$widgets){
  40. $widgets[] = array(
  41. 'uid' => 'dash_domodoor',
  42. 'icon' => 'fa fa-key',
  43. 'label' => 'Porte d\'entréé',
  44. 'background' => '#50597B',
  45. 'color' => '#fffffff',
  46. 'onLoad' => 'action.php?action=domodoor_get_history',
  47. 'onDelete' => 'action.php?action=dash_domodoor_plugin_delete',
  48. );
  49. }
  50. function domodoor_plugin_setting_menu(){
  51. global $_;
  52. echo '<li '.(isset($_['section']) && $_['section']=='domodoor'?'class="active"':'').'><a href="setting.php?section=domodoor"><i class="fa fa-angle-right"></i> Porte domotique</a></li>';
  53. }
  54. function domodoor_plugin_setting_page(){
  55. global $myUser,$_;
  56. if(isset($_['section']) && $_['section']=='domodoor' ){
  57. if($myUser!=false){
  58. $doorAccessManager = new doorAccess();
  59. $accesses = $doorAccessManager->populate();
  60. $current = isset($_['id'])?$doorAccessManager->getById($_['id']): $doorAccessManager;
  61. $users = User::getAllUsers();
  62. ?>
  63. <div class="span9 userBloc">
  64. <h1>Porte bluetooth</h1>
  65. <p>Gestion des accès à la porte</p>
  66. <form action="action.php?action=domodoor_add_domodoor" method="POST">
  67. <fieldset>
  68. <legend><?php echo $description ?></legend>
  69. <div class="left">
  70. <label for="user">Utilisateur</label>
  71. <select id="user" name="user" >
  72. <option value="">-</option>
  73. <?php foreach($users as $user){
  74. echo '<option '.($user->getId()==$current->user?'selected="selected"':'').' value="'.$user->getId().'">'.$user->getFullName().'</option>';
  75. } ?>
  76. </select>
  77. <label for="code">Code bluetooth (4 lettres)</label>
  78. <input type="text" value="<?php echo $current->code; ?>" maxlength="10" name="code" id="code" />
  79. <input type="hidden" name="id" id="id" value="<?php echo $_['id']; ?>">
  80. </div>
  81. <div class="clear"></div>
  82. <br/><button type="submit" class="btn">Enregistrer</button>
  83. </fieldset>
  84. <br/>
  85. </form>
  86. <table class="table table-striped table-bordered table-hover">
  87. <thead>
  88. <tr>
  89. <th colspan="2">Utilisateur</th>
  90. <th>Code</th>
  91. <th></th>
  92. </tr>
  93. </thead>
  94. <?php
  95. $userManager = new User();
  96. foreach($accesses as $access){
  97. $user = $userManager->getById($access->user);
  98. ?>
  99. <tr>
  100. <td><?php echo $user->getGravatarImg(30); ?></td>
  101. <td><?php echo $user->getFullName(); ?></td>
  102. <td><?php echo $access->code; ?></td>
  103. <td><a class="btn" href="action.php?action=domodoor_delete_domodoor&id=<?php echo $access->getId(); ?>"><i class="fa fa-times"></i></a>
  104. <a class="btn" href="setting.php?section=domodoor&id=<?php echo $access->getId(); ?>"><i class="fa fa-pencil"></i></a></td>
  105. </tr>
  106. <?php } ?>
  107. </table>
  108. </div>
  109. <?php }else{ ?>
  110. <div id="main" class="wrapper clearfix">
  111. <article>
  112. <h3>Vous devez être connecté</h3>
  113. </article>
  114. </div>
  115. <?php
  116. }
  117. }
  118. }
  119. function domodoor_action_domodoor(){
  120. global $_,$myUser;
  121. switch($_['action']){
  122. case 'domodoor_add_domodoor':
  123. if(!$myUser->can('door','c')) exit('permission denied');
  124. $doorAccessManager = new DoorAccess();
  125. $doorAccess = new DoorAccess();
  126. $doorAccess = $_['id']!='' ? $doorAccessManager->getById($_['id']) : $doorAccess ;
  127. $doorAccess->user = $_['user'];
  128. $doorAccess->code = str_pad($_['code'],4, "0");
  129. $doorAccess->save();
  130. header('location:setting.php?section=domodoor');
  131. break;
  132. case 'domodoor_get_history':
  133. header('content-type: application/json');
  134. $response['title'] = 'Entrees porte';
  135. $doorAccessManager = new DoorLog();
  136. $morning = mktime (0, 0, 0, date("n") , date("j") , date("Y"));
  137. $entries = $doorAccessManager->loadAll(array('time'=>$morning),'time DESC',10,$operation=">");
  138. $response['content'] ='<ul class="domodoor_history">';
  139. $userManager = new User();
  140. foreach($entries as $entry){
  141. $user = new User();
  142. if($entry->user!=0){
  143. $user = $userManager->getById($entry->user);
  144. }
  145. $response['content'] .='<li title="Code fournis : '.$entry->code.'" class="domodoor_log state-'.$entry->success.'">'.$user->getGravatarImg(50).'<div><h1>'.$user->getFullName().'</h1><h2>'.date('à H:i \l\e d/m ',$entry->time).'<h2></div></li>';
  146. }
  147. $response['content'] .= '</ul>';
  148. echo json_encode($response);
  149. exit(0);
  150. break;
  151. case 'domodoor_delete_domodoor':
  152. if($myUser->can('door','d')){
  153. $doorAccessManager = new DoorAccess();
  154. $doorAccessManager->delete(array('id'=>$_['id']));
  155. header('location:setting.php?section=domodoor');
  156. }
  157. else
  158. {
  159. header('location:setting.php?section=domodoor&error=Vous n\'avez pas le droit de faire ça!');
  160. }
  161. break;
  162. }
  163. }
  164. function domodoor_dashboard(){
  165. global $_,$myUser;
  166. if($myUser->can('door','r')){
  167. ?>
  168. <div class="flatBloc domodoor-bloc">
  169. <h3><i class="fa fa-sign-in"></i> Porte principale</h3>
  170. <div id="dash_domodoor"></div>
  171. </div>
  172. <?php
  173. }
  174. }
  175. Plugin::addCss("/css/style.css");
  176. Plugin::addJs("/js/main.js");
  177. Plugin::addHook("setting_menu", "domodoor_plugin_setting_menu");
  178. Plugin::addHook("setting_bloc", "domodoor_plugin_setting_page");
  179. Plugin::addHook("action_post_case", "domodoor_action_domodoor");
  180. Plugin::addHook("dashboard_pre_column2", "domodoor_dashboard");
  181. Plugin::addHook("widgets", "dash_domodoor_plugin_menu");
  182. ?>