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.

614 lines
21 KiB

13 years ago
  1. <?php
  2. /**
  3. * @file WidgetCommon.php
  4. * This file is part of MOVIM.
  5. *
  6. * @brief The widgets commons methods.
  7. *
  8. * @author Timothée Jaussoin <edhelas@gmail.com>
  9. *
  10. * @date 08 march 2012
  11. *
  12. * Copyright (C)2010 MOVIM Project
  13. *
  14. * See COPYING for licensing information.
  15. */
  16. class WidgetCommon extends WidgetBase {
  17. private function loadTemplate() {
  18. $view = new RainTPL;
  19. $view->configure('tpl_dir', APP_PATH.'widgets/WidgetCommon/');
  20. $view->configure('cache_dir', CACHE_PATH);
  21. $view->configure('tpl_ext', 'tpl');
  22. $view->assign('c', $this);
  23. return $view;
  24. }
  25. protected function printPost($post, $comments = false, $public = false) {
  26. // Initialize the variables
  27. $class = $title = $access = $flagcolor = $group = $c =
  28. $tags = $toolbox = $place = $recycle = '';
  29. if($post->title)
  30. $title = $post->title;
  31. if($this->user->getLogin() == $post->aid) {
  32. $class = 'me ';
  33. if($post->privacy == 1){
  34. $flagcolor='black';
  35. $access .= 'protect black';
  36. }
  37. else{
  38. $flagcolor='orange';
  39. $access .= 'protect orange';
  40. }
  41. $avatar = $post->getContact()->getPhoto('m');
  42. } else
  43. $avatar = $post->getContact()->getPhoto('m');
  44. if(!filter_var($post->jid, FILTER_VALIDATE_EMAIL) && $post->node != '')
  45. $group = '
  46. <span class="group">
  47. <a href="'.Route::urlize('node', array($post->jid, $post->node)).'">'.$post->node.' ('.$post->jid.')</a>
  48. </span>';
  49. elseif($post->jid != $post->aid)
  50. $recycle .= '
  51. <span class="recycle">
  52. <a href="'.Route::urlize('friend', $post->jid).'">'.$post->jid.'</a>
  53. </span>';
  54. if($post->getPlace() != false)
  55. $place .= '
  56. <span class="place">
  57. <a
  58. target="_blank"
  59. href="http://www.openstreetmap.org/?lat='.$post->lat.'&lon='.$post->lon.'&zoom=10"
  60. >'.t('Place').'</a>
  61. </span>';
  62. if(filter_var($post->jid, FILTER_VALIDATE_EMAIL) && $post->jid != '')
  63. $c = '
  64. <span>
  65. <a href="'.Route::urlize('friend', $post->jid).'">'.$post->getContact()->getTrueName().'</a>
  66. </span>';
  67. elseif($post->getContact()->getTrueName() != '' && filter_var($post->aid, FILTER_VALIDATE_EMAIL))
  68. $c = '
  69. <span>
  70. <a href="'.Route::urlize('friend', $post->aid).'">'.$post->getContact()->getTrueName().'</a>
  71. </span>';
  72. elseif($post->aid != '' && filter_var($post->aid, FILTER_VALIDATE_EMAIL))
  73. $c = '
  74. <span>
  75. <a href="'.Route::urlize('friend', $post->aid).'">'.$post->aid.'</a>
  76. </span>';
  77. if($post->links)
  78. $enc = $this->printEnclosures($post->links);
  79. if($post->tags)
  80. $tags = $this->printTags($post->tags);
  81. if(isset($enc) && $enc != '') {
  82. $enc = '
  83. <div class="enclosure">'.
  84. $enc.
  85. '
  86. <div class="clear"></div>
  87. </div>';
  88. } else
  89. $enc = '';
  90. $author = $this->prepareAuthor($post);
  91. $content = prepareString(html_entity_decode($post->content));
  92. if($post->node == 'urn:xmpp:microblog:0')
  93. $comments = $this->printComments($post, $comments, $public);
  94. else
  95. $comments = '';
  96. if($this->user->getLogin() == $post->aid)
  97. $toolbox = $this->getToolbox($post);
  98. $view = $this->loadTemplate();
  99. $view->assign('idhash', md5($post->nodeid));
  100. $view->assign('id', $post->nodeid);
  101. $view->assign('class', $class);
  102. $view->assign('access', $access);
  103. $view->assign('flagtitle', getFlagTitle($flagcolor));
  104. if(filter_var($post->jid, FILTER_VALIDATE_EMAIL))
  105. $view->assign('friend', Route::urlize('friend', $post->jid));
  106. elseif(!filter_var($post->jid, FILTER_VALIDATE_EMAIL) && $post->node != '')
  107. $view->assign('friend', Route::urlize('node', array($post->jid, $post->node)));
  108. else
  109. $view->assign('friend', '#');
  110. $view->assign('avatar', '<img class="avatar" src="'.$avatar.'"/>');
  111. $view->assign('title', $title);
  112. $view->assign('contact', $c);
  113. $view->assign('date', prepareDate(strtotime($post->published)));
  114. $view->assign('content', $content);
  115. $view->assign('tags', $tags);
  116. $view->assign('toolbox', $toolbox);
  117. $view->assign('enc', $enc);
  118. $view->assign('comments', $comments);
  119. $view->assign('place', $place);
  120. $view->assign('recycle', $recycle);
  121. $view->assign('group', $group);
  122. $view->assign('author', $author);
  123. $html = $view->draw('_post', true);
  124. return $html;
  125. }
  126. private function printTags($tags) {
  127. $html = '<br />';
  128. $tags = unserialize($tags);
  129. foreach($tags as $t)
  130. $html .= '<span class="tag">'.$t.'</span>';
  131. return $html;
  132. }
  133. private function printEnclosures($links) {
  134. $enc = '';
  135. $links = unserialize($links);
  136. foreach($links as $l) {
  137. if(isset($l['rel'])
  138. && $l['rel'] == 'enclosure'
  139. && $l['type'] != 'text/html') {
  140. $enc .= '
  141. <a href="'.$l['href'].'" class="imglink" target="_blank">
  142. <img src="'.$l['href'].'"/>
  143. </a>';
  144. } elseif(
  145. isset($l['rel'])
  146. && $l['rel'] == 'alternate'
  147. && isset($l['title'])) {
  148. $enc .= '
  149. <a href="'.$l['href'].'" class="imglink" target="_blank">
  150. '.$l['title'].'
  151. </a>';
  152. } elseif(isset($l['href'])) {
  153. $url = parse_url($l['href']);
  154. if(substr($l['href'], 0, 5) != 'xmpp:') {
  155. if($url['host'] == 'www.youtube.com') {
  156. $enc .= '
  157. <a href="'.$l['href'].'" target="_blank">
  158. <img src="http://img.youtube.com/vi/'.substr($url['query'], 2, 11).'/1.jpg"/>
  159. <img src="http://img.youtube.com/vi/'.substr($url['query'], 2, 11).'/2.jpg"/>
  160. <img src="http://img.youtube.com/vi/'.substr($url['query'], 2, 11).'/3.jpg"/>
  161. </a><br />';
  162. }
  163. if($url['host'] == 'youtu.be') {
  164. $enc .= '
  165. <a href="'.$l['href'].'" target="_blank">
  166. <img src="http://img.youtube.com/vi/'.substr($url['path'], 1, 11).'/1.jpg"/>
  167. <img src="http://img.youtube.com/vi/'.substr($url['path'], 1, 11).'/2.jpg"/>
  168. <img src="http://img.youtube.com/vi/'.substr($url['path'], 1, 11).'/3.jpg"/>
  169. </a><br />';
  170. }
  171. $enc .= '
  172. <a href="'.$l['href'].'" class="imglink" target="_blank">
  173. <img class="icon" src="https://duckduckgo.com/i/'.$url['host'].'.ico"/>'.$url['scheme'].'://'.$url['host'].$url['path'].'
  174. </a><br />';
  175. }
  176. }
  177. }
  178. return $enc;
  179. }
  180. private function prepareAuthor($post) {
  181. $html = $content = '';
  182. if($post->aname != null) {
  183. $content .= ' <span>'.t('by').'</span> '.$post->aname;
  184. }
  185. if($post->aemail != null) {
  186. $content .= ' <span>'.t('email').'</span> '.$post->aemail;
  187. }
  188. if($post->aid != null) {
  189. $content .= ' <span>'.t('jid').'</span> '.$post->aid;
  190. }
  191. if($content .= '')
  192. $html .= '<div class="author">'.$content.'</div>';
  193. return $html;
  194. }
  195. private function getToolbox($post) {
  196. $view = $this->loadTemplate();
  197. $view->assign(
  198. 'privacy_post_orange',
  199. $this->genCallAjax(
  200. 'ajaxPrivacyPost',
  201. "'".$post->nodeid."'",
  202. "'orange'"));
  203. $view->assign(
  204. 'privacy_post_black',
  205. $this->genCallAjax(
  206. 'ajaxPrivacyPost',
  207. "'".$post->nodeid."'",
  208. "'black'"));
  209. $view->assign(
  210. 'delete_post',
  211. $this->genCallAjax(
  212. 'ajaxDeletePost',
  213. "'".$post->jid."'",
  214. "'".$post->node."'",
  215. "'".$post->nodeid."'"));
  216. $html = $view->draw('_post_toolbox', true);
  217. return $html;
  218. }
  219. protected function printComments($post, $comments, $public = false) {
  220. $view = $this->loadTemplate();
  221. $view->assign('post', $post);
  222. $view->assign('comments', $this->prepareComments($comments));
  223. $view->assign('getcomments',
  224. $this->genCallAjax(
  225. 'ajaxGetComments',
  226. "'".$post->commentplace."'",
  227. "'".$post->nodeid."'")
  228. );
  229. $view->assign('publishcomment',
  230. $this->genCallAjax(
  231. 'ajaxPublishComment',
  232. "'".$post->commentplace."'",
  233. "'".$post->nodeid."'",
  234. "encodeURIComponent(document.getElementById('".$post->nodeid."commentcontent').value)")
  235. );
  236. $html = $view->draw('_comments_toolbox', true);
  237. return $html;
  238. }
  239. protected function printMap($posts, $c = null) {
  240. $html = '<div style="height: 13em;" id="postsmap"></div>';
  241. $javascript = '
  242. <script type="text/javascript">
  243. var postsmap = L.map("postsmap").setView([40,0], 2);
  244. L.tileLayer("http://tile.openstreetmap.org/{z}/{x}/{y}.png", {
  245. attribution: "Map data &copy; <a href=\"http://openstreetmap.org\">OpenStreetMap</a> contributors, <a href=\"http://creativecommons.org/licenses/by-sa/2.0/\">CC-BY-SA</a>, Mapnik ©",
  246. maxZoom: 18
  247. }).addTo(postsmap);';
  248. $id = 0;
  249. $posfound = false;
  250. $bound = '';
  251. foreach($posts as $post) {
  252. if($post->getPlace() != false) {
  253. $posfound = true;
  254. $javascript .= "
  255. var marker".$id." = L.marker([".$post->lat.",".$post->lon."]).addTo(postsmap);
  256. marker".$id.".on('click', function() {document.location = '#".md5($post->nodeid)."'});
  257. ";
  258. $bound .= '['.$post->lat.','.$post->lon.'],';
  259. $id++;
  260. }
  261. }
  262. if(isset($c) && $c->loclatitude != '' && $c->loclongitude != '') {
  263. $posfound = true;
  264. $popup = '<img style=\'float: left; margin-right: 1em;\' src=\''.$c->getPhoto('s').'\'/>';
  265. $popup .= '<div style=\'padding: 0.5em;\'>'.$c->getPlace().'<br />'.prepareDate(strtotime($c->loctimestamp)).'</div>';
  266. $popup .= '<div class=\'clear\'></div>';
  267. $javascript .= '
  268. var red = L.icon({
  269. iconUrl: "'.BASE_URI.'/themes/movim/img/marker-icon.png",
  270. iconSize: [25,41], // size of the icon
  271. shadowSize: [50, 64], // size of the shadow
  272. iconAnchor: [13, 41]
  273. });
  274. var marker = L.marker(['.$c->loclatitude.' ,'.$c->loclongitude.'], {icon: red}).addTo(postsmap);
  275. marker.bindPopup("'.$popup.'").openPopup();
  276. ';
  277. $bound .= '['.$c->loclatitude.','.$c->loclongitude.'],';
  278. }
  279. $javascript .= '
  280. postsmap.fitBounds(['.$bound.']);
  281. </script>';
  282. if($posfound)
  283. return $html.$javascript;
  284. else
  285. return '';
  286. }
  287. /*
  288. * @desc Prepare a group of messages
  289. * @param array of messages
  290. * @return generated HTML
  291. */
  292. protected function preparePosts($posts, $public = false) {
  293. if($posts == false || empty($posts)) {
  294. $html = '<div style="padding: 1.5em; text-align: center;">Ain\'t Nobody Here But Us Chickens...</div>';
  295. } else {
  296. $html = '';
  297. $pd = new \modl\PostnDAO();
  298. $comments = $pd->getComments($posts);
  299. foreach($posts as $post) {
  300. // We split the interesting comments for each messages
  301. $i = 0;
  302. $messagecomment = array();
  303. if(isset($comments)) {
  304. foreach($comments as $comment) {
  305. if('urn:xmpp:microblog:0:comments/'.$post->nodeid == $comments[$i]->node) {
  306. array_push($messagecomment, $comment);
  307. unset($comment);
  308. }
  309. $i++;
  310. }
  311. }
  312. $html .= $this->printPost($post, $messagecomment, $public);
  313. }
  314. }
  315. return $html;
  316. }
  317. protected function testIsSet($element)
  318. {
  319. if(isset($element) && $element != '')
  320. return true;
  321. else
  322. return false;
  323. }
  324. protected function prepareComments($comments) {
  325. $tmp = false;
  326. $size = sizeof($comments);
  327. $i = 0;
  328. while($i < $size-1) {
  329. if($comments[$i]->nodeid == $comments[$i+1]->nodeid)
  330. unset($comments[$i]);
  331. $i++;
  332. }
  333. $size = sizeof($comments);
  334. $comcounter = 0;
  335. if($size > 3) {
  336. $tmp = '<div
  337. class="comment"
  338. onclick="
  339. com = this.parentNode.querySelectorAll(\'.comment\');
  340. for(i = 0; i < com.length; i++) { com.item(i).style.display = \'block\';};
  341. this.style.display = \'none\';">
  342. <a class="getcomments icon chat">'.t('Show the older comments').'</a>
  343. </div>';
  344. $comcounter = $size - 3;
  345. }
  346. if($comments) {
  347. foreach($comments as $comment) {
  348. $photo = $comment->getContact()->getPhoto('xs', $comment->aid);
  349. $name = $comment->getContact()->getTrueName();
  350. $tmp .= '
  351. <div class="comment" ';
  352. if($comcounter > 0) {
  353. $tmp .= 'style="display:none;"';
  354. $comcounter--;
  355. }
  356. $tmp .='>
  357. <img class="avatar tiny" src="'.$photo.'">
  358. <span><a href="'.Route::urlize('friend', $comment->aid).'">'.$name.'</a></span>
  359. <span class="date">'.prepareDate(strtotime($comment->published)).'</span><br />
  360. <div class="content tiny">'.prepareString($comment->content).'</div>
  361. </div>';
  362. }
  363. }
  364. return $tmp;
  365. }
  366. protected function prepareSubmitForm($server = '', $node = '') {
  367. $view = $this->loadTemplate();
  368. $view->assign('toggle_position', $this->genCallAjax('ajaxShowPosition', "poss"));
  369. $view->assign('gallery', $this->user->getDir());
  370. $view->assign(
  371. 'publish_item',
  372. $this->genCallAjax(
  373. 'ajaxPublishItem',
  374. "'".$server."'",
  375. "'".$node."'",
  376. "movim_parse_form('postpublish')"));
  377. $view->assign(
  378. 'post_preview',
  379. $this->genCallAjax(
  380. 'ajaxPostPreview',
  381. "document.querySelector('#postpublishcontent').value"));
  382. $html = $view->draw('_submit_form', true);
  383. return $html;
  384. }
  385. function ajaxShowPosition($pos)
  386. {
  387. list($lat,$lon) = explode(',', $pos);
  388. $pos = json_decode(
  389. file_get_contents('http://nominatim.openstreetmap.org/reverse?format=json&lat='.$lat.'&lon='.$lon.'&zoom=27&addressdetails=1')
  390. );
  391. RPC::call('movim_fill', 'postpublishlocation' , (string)$pos->display_name);
  392. RPC::commit();
  393. }
  394. function ajaxPostPreview($content)
  395. {
  396. if($content != '') {
  397. $content = Michelf\Markdown::defaultTransform($content);
  398. RPC::call('movim_fill', 'postpreviewcontent' , $content);
  399. } else
  400. RPC::call('movim_fill', 'postpreviewcontent' , t('No content'));
  401. RPC::commit();
  402. }
  403. function ajaxPublishItem($server, $node, $form)
  404. {
  405. \movim_log($form);
  406. $content = $form['content'];
  407. $title = $form['title'];
  408. list($lat,$lon) = explode(',', $form['latlonpos']);
  409. $pos = json_decode(
  410. file_get_contents('http://nominatim.openstreetmap.org/reverse?format=json&lat='.$lat.'&lon='.$lon.'&zoom=27&addressdetails=1')
  411. );
  412. $geo = array(
  413. 'latitude' => (string)$pos->lat,
  414. 'longitude' => (string)$pos->lon,
  415. 'altitude' => (string)$pos->alt,
  416. 'country' => (string)$pos->address->country,
  417. 'countrycode' => (string)$pos->address->country_code,
  418. 'region' => (string)$pos->address->county,
  419. 'postalcode' => (string)$pos->address->postcode,
  420. 'locality' => (string)$pos->address->city,
  421. 'street' => (string)$pos->address->path,
  422. 'building' => (string)$pos->address->building,
  423. 'text' => (string)$pos->display_name,
  424. 'uri' => ''//'http://www.openstreetmap.org/'.urlencode('?lat='.(string)$pos->lat.'&lon='.(string)$pos->lon.'&zoom=10')
  425. );
  426. if($content != '') {
  427. $content = Michelf\Markdown::defaultTransform($content);
  428. $p = new moxl\PubsubPostPublish();
  429. $p->setFrom($this->user->getLogin())
  430. ->setTo($server)
  431. ->setNode($node)
  432. ->setLocation($geo)
  433. ->setTitle($title)
  434. ->setContentHtml(rawurldecode($content))
  435. ->enableComments()
  436. ->request();
  437. }
  438. }
  439. function onComment($parent) {
  440. $p = new \modl\ContactPostn();
  441. $p->nodeid = $parent;
  442. $pd = new \modl\PostnDAO();
  443. $comments = $pd->getComments($p);
  444. $html = $this->prepareComments($comments);
  445. RPC::call('movim_fill', $parent.'comments', $html);
  446. }
  447. function onNoComment($parent) {
  448. $html = '
  449. <div class="comment">
  450. <a
  451. class="getcomments icon chat" >'.
  452. t('No comments').
  453. '</a>
  454. </div>';
  455. RPC::call('movim_fill', $parent.'comments', $html);
  456. }
  457. function onNoCommentStream($parent) {
  458. $html = '
  459. <div class="comment">
  460. <a
  461. class="getcomments icon chat" >'.
  462. t('No comments stream').
  463. '</a>
  464. </div>';
  465. RPC::call('movim_fill', $parent.'comments', $html);
  466. }
  467. function ajaxGetComments($jid, $id) {
  468. $c = new moxl\MicroblogCommentsGet();
  469. $c->setTo($jid)
  470. ->setId($id)
  471. ->request();
  472. }
  473. function ajaxPublishComment($to, $id, $content) {
  474. if($content != '') {
  475. $p = new moxl\MicroblogCommentPublish();
  476. $p->setTo($to)
  477. ->setFrom($this->user->getLogin())
  478. ->setParentId($id)
  479. ->setContent(htmlspecialchars(rawurldecode($content)))
  480. ->request();
  481. }
  482. }
  483. function ajaxDeletePost($to, $node, $id) {
  484. $p = new moxl\PubsubPostDelete();
  485. $p->setTo($to)
  486. ->setNode($node)
  487. ->setId($id)
  488. ->request();
  489. }
  490. function ajaxPrivacyPost($nodeid, $privacy) {
  491. $pd = new \modl\PrivacyDAO();
  492. $p = $pd->get($nodeid);
  493. if($privacy == 'orange') {
  494. \modl\Privacy::set($nodeid, 0);
  495. } elseif($privacy == 'black') {
  496. \modl\Privacy::set($nodeid, 1);
  497. }
  498. RPC::call('movim_change_class', $nodeid , 'protect '.$privacy, getFlagTitle($privacy));
  499. RPC::commit();
  500. }
  501. }