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.

201 lines
6.6 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. <?php
  2. use Respect\Validation\Validator;
  3. use App\User;
  4. include_once WIDGETS_PATH.'Post/Post.php';
  5. class Blog extends \Movim\Widget\Base
  6. {
  7. public $_paging = 8;
  8. private $_from;
  9. private $_node;
  10. private $_item;
  11. private $_id;
  12. private $_contact;
  13. private $_messages = [];
  14. private $_page;
  15. private $_mode;
  16. private $_tag;
  17. function load()
  18. {
  19. $this->links = [];
  20. if ($this->_view == 'node') {
  21. $this->_from = $this->get('s');
  22. $this->_node = $this->get('n');
  23. if (!$this->validateServerNode($this->_from, $this->_node)) return;
  24. $pd = new \Modl\InfoDAO;
  25. $this->_item = $pd->get($this->_from, $this->_node);
  26. $this->_mode = 'group';
  27. $this->url = $this->route('node', [$this->_from, $this->_node]);
  28. $this->links[] = [
  29. 'rel' => 'alternate',
  30. 'type' => 'application/atom+xml',
  31. 'href' => $this->route('feed', [$this->_from, $this->_node])
  32. ];
  33. if (!$this->get('i')) {
  34. $this->links[] = [
  35. 'rel' => 'alternate',
  36. 'type' => 'application/atom+xml',
  37. 'href' => 'xmpp:' . rawurlencode($this->_from) . '?;node=' . rawurlencode($this->_node)
  38. ];
  39. }
  40. } elseif ($this->_view == 'tag' && $this->validateTag($this->get('t'))) {
  41. $this->_mode = 'tag';
  42. $this->_tag = strtolower($this->get('t'));
  43. $this->title = '#'.$this->_tag;
  44. } else {
  45. $this->_from = $this->get('f');
  46. $this->_contact = App\Contact::find($this->_from);
  47. if (filter_var($this->_from, FILTER_VALIDATE_EMAIL)) {
  48. $this->_node = 'urn:xmpp:microblog:0';
  49. } else {
  50. return;
  51. }
  52. $this->_mode = 'blog';
  53. $this->url = $this->route('blog', $this->_from);
  54. $this->links[] = [
  55. 'rel' => 'alternate',
  56. 'type' => 'application/atom+xml',
  57. 'href' => $this->route('feed', [$this->_from])
  58. ];
  59. if (!$this->get('i')) {
  60. $this->links[] = [
  61. 'rel' => 'alternate',
  62. 'type' => 'application/atom+xml',
  63. 'href' => 'xmpp:' . rawurlencode($this->_from) . '?;node=' . rawurlencode($this->_node)
  64. ];
  65. }
  66. }
  67. $pd = new \Modl\PostnDAO;
  68. if ($this->_id = $this->get('i')) {
  69. if (Validator::stringType()->between('1', '100')->validate($this->_id)) {
  70. if (isset($this->_tag)) {
  71. $this->_messages = $pd->getPublicTag($this->_tag, $this->_id * $this->_paging, $this->_paging + 1);
  72. } else {
  73. $this->_messages = $pd->getNodeUnfiltered($this->_from, $this->_node, $this->_id * $this->_paging, $this->_paging + 1);
  74. }
  75. $this->_page = $this->_id + 1;
  76. } elseif (Validator::stringType()->length(5, 100)->validate($this->_id)) {
  77. $this->_messages[0] = $pd->getPublicItem($this->_from, $this->_node, $this->_id);
  78. if (is_object($this->_messages[0])) {
  79. $this->title = $this->_messages[0]->title;
  80. $description = stripTags($this->_messages[0]->contentcleaned);
  81. if (!empty($description)) {
  82. $this->description = truncate($description, 100);
  83. }
  84. $attachments = $this->_messages[0]->getAttachments();
  85. if ($attachments && array_key_exists('pictures', $attachments)) {
  86. $this->image = urldecode($attachments['pictures'][0]['href']);
  87. }
  88. }
  89. if ($this->_view == 'node') {
  90. $this->url = $this->route('node', [$this->_from, $this->_node, $this->_id]);
  91. } else {
  92. $this->url = $this->route('blog', [$this->_from, $this->_id]);
  93. }
  94. $this->links[] = [
  95. 'rel' => 'alternate',
  96. 'type' => 'application/atom+xml',
  97. 'href' => 'xmpp:'
  98. . rawurlencode($this->_from)
  99. . '?;node='
  100. . rawurlencode($this->_node)
  101. . ';item='
  102. . rawurlencode($this->_id)
  103. ];
  104. }
  105. } else {
  106. $this->_page = 1;
  107. if (isset($this->_tag)) {
  108. $this->_messages = $pd->getPublicTag($this->_tag, 0, $this->_paging + 1);
  109. } else {
  110. $this->_messages = $pd->getNodeUnfiltered($this->_from, $this->_node, 0, $this->_paging + 1);
  111. }
  112. }
  113. if (is_array($this->messages)
  114. && count($this->_messages) == $this->_paging + 1) {
  115. array_pop($this->_messages);
  116. } else {
  117. $this->_page = null;
  118. }
  119. if ($this->_node == 'urn:xmpp:microblog:0') {
  120. $cssurl = User::find($this->_from)->cssurl;
  121. if (isset($cssurl)
  122. && $cssurl != ''
  123. && Validator::url()->validate($cssurl)) {
  124. $this->addrawcss($cssurl);
  125. }
  126. }
  127. }
  128. public function preparePost($p)
  129. {
  130. $pw = new Post;
  131. return $pw->preparePost($p, true, true);
  132. }
  133. public function prepareCard($p)
  134. {
  135. $pw = new Post;
  136. return $pw->preparePost($p, true, ($this->_view != 'tag'), true);
  137. }
  138. function display()
  139. {
  140. $this->view->assign('server', $this->_from);
  141. $this->view->assign('node', $this->_node);
  142. $this->view->assign('item', $this->_item);
  143. $this->view->assign('contact', $this->_contact);
  144. $this->view->assign('mode', $this->_mode);
  145. $this->view->assign('more', $this->_page);
  146. $this->view->assign('posts', $this->_messages);
  147. $this->view->assign('tag', $this->_tag);
  148. }
  149. private function validateServerNode($server, $node)
  150. {
  151. $validate_server = Validator::stringType()->noWhitespace()->length(6, 40);
  152. $validate_node = Validator::stringType()->length(3, 100);
  153. if (!$validate_server->validate($server)
  154. || !$validate_node->validate($node)
  155. ) return false;
  156. else return true;
  157. }
  158. private function validateTag($tag)
  159. {
  160. return Validator::stringType()->notEmpty()->noWhitespace()->validate($tag);
  161. }
  162. function getComments($post)
  163. {
  164. $pd = new \Modl\PostnDAO;
  165. return $pd->getComments($post);
  166. }
  167. }