Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

207 lines
5.2 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. <?php
  2. /**
  3. * @file mod/tagger.php
  4. */
  5. use Friendica\App;
  6. use Friendica\Core\Addon;
  7. use Friendica\Core\L10n;
  8. use Friendica\Core\Logger;
  9. use Friendica\Core\System;
  10. use Friendica\Core\Worker;
  11. use Friendica\Database\DBA;
  12. use Friendica\Model\Item;
  13. use Friendica\Util\Strings;
  14. use Friendica\Util\XML;
  15. function tagger_content(App $a) {
  16. if (!local_user() && !remote_user()) {
  17. return;
  18. }
  19. $term = Strings::escapeTags(trim($_GET['term']));
  20. // no commas allowed
  21. $term = str_replace([',',' '],['','_'],$term);
  22. if (!$term) {
  23. return;
  24. }
  25. $item_id = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : 0);
  26. Logger::log('tagger: tag ' . $term . ' item ' . $item_id);
  27. $item = Item::selectFirst([], ['id' => $item_id]);
  28. if (!$item_id || !DBA::isResult($item)) {
  29. Logger::log('tagger: no item ' . $item_id);
  30. return;
  31. }
  32. $owner_uid = $item['uid'];
  33. $owner_nick = '';
  34. $blocktags = 0;
  35. $r = q("select `nickname`,`blocktags` from user where uid = %d limit 1",
  36. intval($owner_uid)
  37. );
  38. if (DBA::isResult($r)) {
  39. $owner_nick = $r[0]['nickname'];
  40. $blocktags = $r[0]['blocktags'];
  41. }
  42. if (local_user() != $owner_uid) {
  43. return;
  44. }
  45. $r = q("select * from contact where self = 1 and uid = %d limit 1",
  46. intval(local_user())
  47. );
  48. if (DBA::isResult($r)) {
  49. $contact = $r[0];
  50. } else {
  51. Logger::log('tagger: no contact_id');
  52. return;
  53. }
  54. $uri = Item::newURI($owner_uid);
  55. $xterm = XML::escape($term);
  56. $post_type = (($item['resource-id']) ? L10n::t('photo') : L10n::t('status'));
  57. $targettype = (($item['resource-id']) ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE );
  58. if ($owner_nick) {
  59. $href = System::baseUrl() . '/display/' . $owner_nick . '/' . $item['id'];
  60. } else {
  61. $href = System::baseUrl() . '/display/' . $item['guid'];
  62. }
  63. $link = XML::escape('<link rel="alternate" type="text/html" href="'. $href . '" />' . "\n");
  64. $body = XML::escape($item['body']);
  65. $target = <<< EOT
  66. <target>
  67. <type>$targettype</type>
  68. <local>1</local>
  69. <id>{$item['uri']}</id>
  70. <link>$link</link>
  71. <title></title>
  72. <content>$body</content>
  73. </target>
  74. EOT;
  75. $tagid = System::baseUrl() . '/search?tag=' . $xterm;
  76. $objtype = ACTIVITY_OBJ_TAGTERM;
  77. $obj = <<< EOT
  78. <object>
  79. <type>$objtype</type>
  80. <local>1</local>
  81. <id>$tagid</id>
  82. <link>$tagid</link>
  83. <title>$xterm</title>
  84. <content>$xterm</content>
  85. </object>
  86. EOT;
  87. $bodyverb = L10n::t('%1$s tagged %2$s\'s %3$s with %4$s');
  88. if (!isset($bodyverb)) {
  89. return;
  90. }
  91. $termlink = html_entity_decode('&#x2317;') . '[url=' . System::baseUrl() . '/search?tag=' . $term . ']'. $term . '[/url]';
  92. $arr = [];
  93. $arr['guid'] = System::createUUID();
  94. $arr['uri'] = $uri;
  95. $arr['uid'] = $owner_uid;
  96. $arr['contact-id'] = $contact['id'];
  97. $arr['wall'] = $item['wall'];
  98. $arr['gravity'] = GRAVITY_COMMENT;
  99. $arr['parent'] = $item['id'];
  100. $arr['parent-uri'] = $item['uri'];
  101. $arr['owner-name'] = $item['author-name'];
  102. $arr['owner-link'] = $item['author-link'];
  103. $arr['owner-avatar'] = $item['author-avatar'];
  104. $arr['author-name'] = $contact['name'];
  105. $arr['author-link'] = $contact['url'];
  106. $arr['author-avatar'] = $contact['thumb'];
  107. $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
  108. $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
  109. $plink = '[url=' . $item['plink'] . ']' . $post_type . '[/url]';
  110. $arr['body'] = sprintf( $bodyverb, $ulink, $alink, $plink, $termlink );
  111. $arr['verb'] = ACTIVITY_TAG;
  112. $arr['target-type'] = $targettype;
  113. $arr['target'] = $target;
  114. $arr['object-type'] = $objtype;
  115. $arr['object'] = $obj;
  116. $arr['private'] = $item['private'];
  117. $arr['allow_cid'] = $item['allow_cid'];
  118. $arr['allow_gid'] = $item['allow_gid'];
  119. $arr['deny_cid'] = $item['deny_cid'];
  120. $arr['deny_gid'] = $item['deny_gid'];
  121. $arr['visible'] = 1;
  122. $arr['unseen'] = 1;
  123. $arr['origin'] = 1;
  124. $post_id = Item::insert($arr);
  125. if (!$item['visible']) {
  126. Item::update(['visible' => true], ['id' => $item['id']]);
  127. }
  128. $term_objtype = ($item['resource-id'] ? TERM_OBJ_PHOTO : TERM_OBJ_POST);
  129. $t = q("SELECT count(tid) as tcount FROM term WHERE oid=%d AND term='%s'",
  130. intval($item['id']),
  131. DBA::escape($term)
  132. );
  133. if (!$blocktags && $t[0]['tcount'] == 0) {
  134. q("INSERT INTO term (oid, otype, type, term, url, uid) VALUE (%d, %d, %d, '%s', '%s', %d)",
  135. intval($item['id']),
  136. $term_objtype,
  137. TERM_HASHTAG,
  138. DBA::escape($term),
  139. '',
  140. intval($owner_uid)
  141. );
  142. }
  143. // if the original post is on this site, update it.
  144. $original_item = Item::selectFirst(['tag', 'id', 'uid'], ['origin' => true, 'uri' => $item['uri']]);
  145. if (DBA::isResult($original_item)) {
  146. $x = q("SELECT `blocktags` FROM `user` WHERE `uid`=%d LIMIT 1",
  147. intval($original_item['uid'])
  148. );
  149. $t = q("SELECT COUNT(`tid`) AS `tcount` FROM `term` WHERE `oid`=%d AND `term`='%s'",
  150. intval($original_item['id']),
  151. DBA::escape($term)
  152. );
  153. if (DBA::isResult($x) && !$x[0]['blocktags'] && $t[0]['tcount'] == 0){
  154. q("INSERT INTO term (`oid`, `otype`, `type`, `term`, `url`, `uid`) VALUE (%d, %d, %d, '%s', '%s', %d)",
  155. intval($original_item['id']),
  156. $term_objtype,
  157. TERM_HASHTAG,
  158. DBA::escape($term),
  159. '',
  160. intval($owner_uid)
  161. );
  162. }
  163. }
  164. $arr['id'] = $post_id;
  165. Addon::callHooks('post_local_end', $arr);
  166. Worker::add(PRIORITY_HIGH, "Notifier", "tag", $post_id);
  167. exit();
  168. }