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.

282 lines
7.3 KiB

  1. <?php
  2. use Moxl\Xec\Action\Roster\UpdateItem;
  3. use Moxl\Xec\Action\Vcard\Get;
  4. use Respect\Validation\Validator;
  5. use Moxl\Xec\Action\Pubsub\GetItems;
  6. class Contact extends WidgetCommon
  7. {
  8. function load()
  9. {
  10. $this->registerEvent('roster_updateitem_handle', 'onContactEdited');
  11. $this->registerEvent('vcard_get_handle', 'onVcardReceived');
  12. }
  13. public function onVcardReceived($packet)
  14. {
  15. $contact = $packet->content;
  16. $this->ajaxGetContact($contact->jid);
  17. }
  18. public function onContactEdited($packet)
  19. {
  20. Notification::append(null, $this->__('edit.updated'));
  21. }
  22. function ajaxClear()
  23. {
  24. $html = $this->prepareEmpty();
  25. RPC::call('movim_fill', 'contact_widget', $html);
  26. }
  27. function ajaxGetContact($jid)
  28. {
  29. if(!$this->validateJid($jid)) return;
  30. $html = $this->prepareContact($jid);
  31. $header = $this->prepareHeader($jid);
  32. Header::fill($header);
  33. RPC::call('movim_fill', 'contact_widget', $html);
  34. RPC::call('MovimTpl.showPanel');
  35. }
  36. function ajaxEditSubmit($form)
  37. {
  38. $rd = new UpdateItem;
  39. $rd->setTo(echapJid($form['jid']))
  40. ->setFrom($this->user->getLogin())
  41. ->setName(htmlspecialchars($form['alias']))
  42. ->setGroup(htmlspecialchars($form['group']))
  43. ->request();
  44. }
  45. function ajaxRefreshFeed($jid)
  46. {
  47. if(!$this->validateJid($jid)) return;
  48. $r = new GetItems;
  49. $r->setTo($jid)
  50. ->setNode('urn:xmpp:microblog:0')
  51. ->request();
  52. }
  53. function ajaxRefreshVcard($jid)
  54. {
  55. if(!$this->validateJid($jid)) return;
  56. $r = new Get;
  57. $r->setTo(echapJid($jid))->request();
  58. }
  59. function ajaxEditContact($jid)
  60. {
  61. if(!$this->validateJid($jid)) return;
  62. $rd = new \Modl\RosterLinkDAO();
  63. $groups = $rd->getGroups();
  64. $rl = $rd->get($jid);
  65. $view = $this->tpl();
  66. if(isset($rl)) {
  67. $view->assign('submit',
  68. $this->call(
  69. 'ajaxEditSubmit',
  70. "movim_parse_form('manage')"));
  71. $view->assign('contact', $rl);
  72. $view->assign('groups', $groups);
  73. }
  74. Dialog::fill($view->draw('_contact_edit', true));
  75. }
  76. function ajaxChat($jid)
  77. {
  78. if(!$this->validateJid($jid)) return;
  79. $c = new Chats;
  80. $c->ajaxOpen($jid);
  81. RPC::call('movim_redirect', $this->route('chat', $jid));
  82. }
  83. function ajaxDeleteContact($jid)
  84. {
  85. if(!$this->validateJid($jid)) return;
  86. $view = $this->tpl();
  87. $view->assign('jid', $jid);
  88. Dialog::fill($view->draw('_contact_delete', true));
  89. }
  90. function prepareHeader($jid)
  91. {
  92. if(!$this->validateJid($jid)) return;
  93. $cd = new \Modl\ContactDAO;
  94. $cr = $cd->getRosterItem($jid);
  95. $view = $this->tpl();
  96. $view->assign('jid', echapJS($jid));
  97. if(isset($cr)) {
  98. $view->assign('contactr', $cr);
  99. $view->assign('edit',
  100. $this->call(
  101. 'ajaxEditContact',
  102. "'".echapJS($cr->jid)."'"));
  103. $view->assign('delete',
  104. $this->call(
  105. 'ajaxDeleteContact',
  106. "'".echapJS($cr->jid)."'"));
  107. } else {
  108. $view->assign('contactr', null);
  109. $c = $cd->get($jid);
  110. if(isset($c)) {
  111. $view->assign('contact', $c);
  112. } else {
  113. $view->assign('contact', null);
  114. }
  115. }
  116. return $view->draw('_contact_header', true);
  117. }
  118. function prepareEmpty($jid = null)
  119. {
  120. if($jid == null) {
  121. $cd = new \modl\ContactDAO();
  122. $users = $cd->getAllPublic(0, 10);
  123. if($users != null){
  124. $view = $this->tpl();
  125. $view->assign('users', array_reverse($users));
  126. return $view->draw('_contact_explore', true);
  127. } else {
  128. return '';
  129. }
  130. } else {
  131. $view = $this->tpl();
  132. $view->assign('jid', $jid);
  133. return $view->draw('_contact_empty', true);
  134. }
  135. }
  136. function prepareContact($jid)
  137. {
  138. if(!$this->validateJid($jid)) return;
  139. $cd = new \Modl\ContactDAO;
  140. $c = $cd->get($jid, true);
  141. if($c == null || $c->created == null || $c->isEmpty()) {
  142. $c = new \Modl\Contact;
  143. $c->jid = $jid;
  144. $this->ajaxRefreshVcard($jid);
  145. }
  146. $cr = $cd->getRosterItem($jid);
  147. $view = $this->tpl();
  148. $pd = new \Modl\PostnDAO;
  149. $gallery = $pd->getGallery($jid);
  150. if(isset($c)) {
  151. $view->assign('mood', getMood());
  152. $view->assign('contact', $c);
  153. $view->assign('contactr', $cr);
  154. if( $cr->node != null
  155. && $cr->ver != null
  156. && $cr->node
  157. && $cr->ver) {
  158. $node = $cr->node.'#'.$cr->ver;
  159. $cad = new \Modl\CapsDAO();
  160. $caps = $cad->get($node);
  161. if(
  162. isset($caps)
  163. && $caps->name != ''
  164. && $caps->type != '' ) {
  165. $clienttype = getClientTypes();
  166. $view->assign('caps', $caps);
  167. $view->assign('clienttype', $clienttype);
  168. }
  169. } else {
  170. $view->assign('caps', null);
  171. }
  172. $view->assign('gallery', $gallery);
  173. $view->assign('chat',
  174. $this->call(
  175. 'ajaxChat',
  176. "'".echapJS($c->jid)."'"));
  177. return $view->draw('_contact', true);
  178. } elseif(isset($cr)) {
  179. $view->assign('contact', null);
  180. $view->assign('contactr', $cr);
  181. $view->assign('chat',
  182. $this->call(
  183. 'ajaxChat',
  184. "'".echapJS($cr->jid)."'"));
  185. return $view->draw('_contact', true);
  186. } else {
  187. return $this->prepareEmpty($jid);
  188. }
  189. }
  190. function getLastFM($contact)
  191. {
  192. $uri = str_replace(
  193. ' ',
  194. '%20',
  195. 'http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=80c1aa3abfa9e3d06f404a2e781e38f9&artist='.
  196. $contact->tuneartist.
  197. '&album='.
  198. $contact->tunesource.
  199. '&format=json'
  200. );
  201. $json = json_decode(requestURL($uri, 2));
  202. $img = $json->album->image[2]->{'#text'};
  203. $url = $json->album->url;
  204. return array($img, $url);
  205. }
  206. /**
  207. * @brief Validate the jid
  208. *
  209. * @param string $jid
  210. */
  211. private function validateJid($jid)
  212. {
  213. $validate_jid = Validator::email()->noWhitespace()->length(6, 60);
  214. if(!$validate_jid->validate($jid)) return false;
  215. else return true;
  216. }
  217. function display()
  218. {
  219. $validate_jid = Validator::email()->length(6, 40);
  220. $this->view->assign('jid', false);
  221. if($validate_jid->validate($this->get('f'))) {
  222. $this->view->assign('jid', $this->get('f'));
  223. }
  224. }
  225. }