You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
7.2 KiB

10 years ago
  1. <?php
  2. use Moxl\Xec\Action\Presence\Muc;
  3. use Moxl\Xec\Action\Bookmark\Get;
  4. use Moxl\Xec\Action\Bookmark\Set;
  5. use Moxl\Xec\Action\Presence\Unavailable;
  6. use Respect\Validation\Validator;
  7. class Rooms extends WidgetBase
  8. {
  9. function load()
  10. {
  11. $this->addjs('rooms.js');
  12. $this->addcss('rooms.css');
  13. $this->registerEvent('bookmark_set_handle', 'onBookmark');
  14. $this->registerEvent('presence_muc_handle', 'onConnected');
  15. $this->registerEvent('presence_unavailable_handle', 'onDisconnected');
  16. $this->registerEvent('presence_muc_errorconflict', 'onConflict');
  17. }
  18. function onBookmark()
  19. {
  20. $this->refreshRooms();
  21. RPC::call('MovimTpl.hidePanel');
  22. }
  23. function onConnected()
  24. {
  25. $this->refreshRooms();
  26. }
  27. function onConflict()
  28. {
  29. Notification::append(null, $this->__('chatrooms.conflict'));
  30. }
  31. function onDisconnected()
  32. {
  33. // We reset the Chat view
  34. $c = new Chat();
  35. $c->ajaxGet();
  36. $this->refreshRooms();
  37. Notification::append(null, $this->__('chatrooms.disconnected'));
  38. }
  39. private function refreshRooms()
  40. {
  41. RPC::call('movim_fill', 'rooms_widget', $this->prepareRooms());
  42. RPC::call('Rooms.refresh');
  43. }
  44. /**
  45. * @brief Display the add room form
  46. */
  47. function ajaxAdd()
  48. {
  49. $view = $this->tpl();
  50. $cd = new \Modl\ContactDAO;
  51. $view->assign('username', $this->user->getUser());
  52. Dialog::fill($view->draw('_rooms_add', true));
  53. }
  54. /**
  55. * @brief Display the remove room confirmation
  56. */
  57. function ajaxRemoveConfirm($room)
  58. {
  59. if(!$this->validateRoom($room)) return;
  60. $view = $this->tpl();
  61. $view->assign('room', $room);
  62. Dialog::fill($view->draw('_rooms_remove', true));
  63. }
  64. /**
  65. * @brief Display the room list
  66. */
  67. function ajaxList($room)
  68. {
  69. if(!$this->validateRoom($room)) return;
  70. $view = $this->tpl();
  71. $cd = new \Modl\ContactDAO;
  72. $view->assign('list', $cd->getPresences($room));
  73. Dialog::fill($view->draw('_rooms_list', true), true);
  74. }
  75. /**
  76. * @brief Remove a room
  77. */
  78. function ajaxRemove($room)
  79. {
  80. if(!$this->validateRoom($room)) return;
  81. $cd = new \modl\ConferenceDAO();
  82. $cd->deleteNode($room);
  83. $this->setBookmark();
  84. }
  85. /**
  86. * @brief Join a chatroom
  87. */
  88. function ajaxJoin($room, $nickname = false)
  89. {
  90. if(!$this->validateRoom($room)) return;
  91. $p = new Muc;
  92. $p->setTo($room);
  93. if($nickname == false) {
  94. $s = Session::start();
  95. $nickname = $s->get('username');
  96. }
  97. if($nickname == false || $nickname == null) {
  98. $session = \Sessionx::start();
  99. $nickname = $session->username;
  100. }
  101. $p->setNickname($nickname);
  102. $p->request();
  103. }
  104. /**
  105. * @brief Exit a room
  106. *
  107. * @param string $room
  108. */
  109. function ajaxExit($room)
  110. {
  111. if(!$this->validateRoom($room)) return;
  112. $s = Session::start();
  113. $resource = $s->get('username');
  114. if($resource == null) {
  115. $session = \Sessionx::start();
  116. $resource = $session->username;
  117. }
  118. $pu = new Unavailable;
  119. $pu->setTo($room)
  120. ->setResource($resource)
  121. ->setMuc()
  122. ->request();
  123. }
  124. /**
  125. * @brief Confirm the room add
  126. */
  127. function ajaxChatroomAdd($form)
  128. {
  129. if(!filter_var($form['jid'], FILTER_VALIDATE_EMAIL)) {
  130. Notification::append(null, $this->__('chatrooms.bad_id'));
  131. //Notification::append(null, $this->__('chatrooms.bad_nickname'));
  132. } elseif(trim($form['name']) == '') {
  133. Notification::append(null, $this->__('chatrooms.empty_name'));
  134. } else {
  135. $item = array(
  136. 'type' => 'conference',
  137. 'name' => $form['name'],
  138. 'autojoin' => $form['autojoin'],
  139. 'nick' => $form['nick'],
  140. 'jid' => $form['jid']);
  141. $this->setBookmark($item);
  142. RPC::call('Dialog.clear');
  143. }
  144. }
  145. public function setBookmark($item = false)
  146. {
  147. $arr = array();
  148. if($item) {
  149. array_push($arr, $item);
  150. }
  151. $sd = new \modl\SubscriptionDAO();
  152. $cd = new \modl\ConferenceDAO();
  153. foreach($sd->getSubscribed() as $s) {
  154. array_push($arr,
  155. array(
  156. 'type' => 'subscription',
  157. 'server' => $s->server,
  158. 'title' => $s->title,
  159. 'subid' => $s->subid,
  160. 'tags' => unserialize($s->tags),
  161. 'node' => $s->node));
  162. }
  163. foreach($cd->getAll() as $c) {
  164. array_push($arr,
  165. array(
  166. 'type' => 'conference',
  167. 'name' => htmlentities($c->name),
  168. 'autojoin' => $c->autojoin,
  169. 'nick' => htmlentities($c->nick),
  170. 'jid' => $c->conference));
  171. }
  172. $b = new Set;
  173. $b->setArr($arr)
  174. ->setTo($this->user->getLogin())
  175. ->request();
  176. }
  177. function checkConnected($room, $resource = false)
  178. {
  179. if(!$this->validateRoom($room)) return;
  180. if($resource && !$this->validateResource($resource)) {
  181. Notification::append(null, $this->__('chatrooms.bad_id'));
  182. return;
  183. }
  184. $pd = new \modl\PresenceDAO();
  185. if($resource == false) {
  186. $session = \Sessionx::start();
  187. $resource = $session->user;
  188. }
  189. $presence = $pd->getPresence($room, $resource);
  190. if($presence != null) {
  191. return true;
  192. } else {
  193. return false;
  194. }
  195. }
  196. function prepareRooms()
  197. {
  198. $view = $this->tpl();
  199. $cod = new \modl\ConferenceDAO();
  200. $list = $cod->getAll();
  201. $connected = array();
  202. foreach($list as $key => $room) {
  203. if($this->checkConnected($room->conference, $room->nick)) {
  204. $room->connected = true;
  205. array_push($connected, $room);
  206. unset($list[$key]);
  207. }
  208. }
  209. $list = array_merge($connected, $list);
  210. $view->assign('conferences', $list);
  211. $view->assign('room', $this->get('r'));
  212. return $view->draw('_rooms', true);
  213. }
  214. /**
  215. * @brief Validate the room
  216. *
  217. * @param string $room
  218. */
  219. private function validateRoom($room)
  220. {
  221. $validate_server = Validator::stringType()->noWhitespace()->length(6, 80);
  222. if(!$validate_server->validate($room)) return false;
  223. else return true;
  224. }
  225. /**
  226. * @brief Validate the resource
  227. *
  228. * @param string $resource
  229. */
  230. private function validateResource($resource)
  231. {
  232. $validate_resource = Validator::stringType()->length(2, 40);
  233. if(!$validate_resource->validate($resource)) return false;
  234. else return true;
  235. }
  236. function display()
  237. {
  238. $this->view->assign('list', $this->prepareRooms());
  239. }
  240. }