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.

231 lines
7.4 KiB

  1. <?php
  2. function add_thread($itemid, $onlyshadow = false) {
  3. $items = q("SELECT `uid`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`, `moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`, `gcontact-id`,
  4. `deleted`, `origin`, `forum_mode`, `mention`, `network` FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid));
  5. if (!$items)
  6. return;
  7. $item = $items[0];
  8. $item['iid'] = $itemid;
  9. if (!$onlyshadow) {
  10. $result = dbq("INSERT INTO `thread` (`"
  11. .implode("`, `", array_keys($item))
  12. ."`) VALUES ('"
  13. .implode("', '", array_values($item))
  14. ."')");
  15. logger("add_thread: Add thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
  16. }
  17. // is it already a copy?
  18. if (($itemid == 0) OR ($item['uid'] == 0))
  19. return;
  20. // Is it a visible public post?
  21. if (!$item["visible"] OR $item["deleted"] OR $item["moderated"] OR $item["private"])
  22. return;
  23. // is it an entry from a connector? Only add an entry for natively connected networks
  24. if (!in_array($item["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")))
  25. return;
  26. // Only do these checks if the post isn't a wall post
  27. if (!$item["wall"]) {
  28. // Check, if hide-friends is activated - then don't do a shadow entry
  29. $r = q("SELECT `hide-friends` FROM `profile` WHERE `is-default` AND `uid` = %d AND NOT `hide-friends`",
  30. $item['uid']);
  31. if (!count($r))
  32. return;
  33. // Check if the contact is hidden or blocked
  34. $r = q("SELECT `id` FROM `contact` WHERE NOT `hidden` AND NOT `blocked` AND `id` = %d",
  35. $item['contact-id']);
  36. if (!count($r))
  37. return;
  38. }
  39. // Only add a shadow, if the profile isn't hidden
  40. $r = q("SELECT `uid` FROM `user` where `uid` = %d AND NOT `hidewall`", $item['uid']);
  41. if (!count($r))
  42. return;
  43. $item = q("SELECT * FROM `item` WHERE `id` = %d",
  44. intval($itemid));
  45. if (count($item) AND ($item[0]["allow_cid"] == '') AND ($item[0]["allow_gid"] == '') AND
  46. ($item[0]["deny_cid"] == '') AND ($item[0]["deny_gid"] == '')) {
  47. $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1",
  48. dbesc($item['uri']));
  49. if (!$r) {
  50. // Preparing public shadow (removing user specific data)
  51. require_once("include/items.php");
  52. require_once("include/Contact.php");
  53. unset($item[0]['id']);
  54. $item[0]['uid'] = 0;
  55. $item[0]['contact-id'] = get_contact($item[0]['author-link'], 0);
  56. $public_shadow = item_store($item[0], false, false, true);
  57. logger("add_thread: Stored public shadow for post ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
  58. }
  59. }
  60. }
  61. function add_shadow_entry($item) {
  62. // Is this a shadow entry?
  63. if ($item['uid'] == 0)
  64. return;
  65. // Is there a shadow parent?
  66. $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1", dbesc($item['parent-uri']));
  67. if (!count($r))
  68. return;
  69. // Is there already a shadow entry?
  70. $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1", dbesc($item['uri']));
  71. if (count($r))
  72. return;
  73. // Preparing public shadow (removing user specific data)
  74. require_once("include/items.php");
  75. require_once("include/Contact.php");
  76. unset($item['id']);
  77. $item['uid'] = 0;
  78. $item['contact-id'] = get_contact($item['author-link'], 0);
  79. $public_shadow = item_store($item, false, false, true);
  80. logger("Stored public shadow for comment ".$item['uri']." under id ".$public_shadow, LOGGER_DEBUG);
  81. }
  82. function update_thread_uri($itemuri, $uid) {
  83. $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
  84. if(count($messages))
  85. foreach ($messages as $message)
  86. update_thread($message["id"]);
  87. }
  88. function update_thread($itemid, $setmention = false) {
  89. $items = q("SELECT `uid`, `guid`, `title`, `body`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`, `moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`, `gcontact-id`,
  90. `deleted`, `origin`, `forum_mode`, `network`, `rendered-html`, `rendered-hash` FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid));
  91. if (!$items)
  92. return;
  93. $item = $items[0];
  94. if ($setmention)
  95. $item["mention"] = 1;
  96. $sql = "";
  97. foreach ($item AS $field => $data)
  98. if (!in_array($field, array("guid", "title", "body", "rendered-html", "rendered-hash"))) {
  99. if ($sql != "")
  100. $sql .= ", ";
  101. $sql .= "`".$field."` = '".dbesc($data)."'";
  102. }
  103. $result = q("UPDATE `thread` SET ".$sql." WHERE `iid` = %d", intval($itemid));
  104. logger("Update thread for item ".$itemid." - guid ".$item["guid"]." - ".print_r($result, true)." ".print_r($item, true), LOGGER_DEBUG);
  105. // Updating a shadow item entry
  106. $items = q("SELECT `id` FROM `item` WHERE `guid` = '%s' AND `uid` = 0 LIMIT 1", dbesc($item["guid"]));
  107. if (!$items)
  108. return;
  109. $result = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `rendered-html` = '%s', `rendered-hash` = '%s' WHERE `id` = %d",
  110. dbesc($item["title"]),
  111. dbesc($item["body"]),
  112. dbesc($item["rendered-html"]),
  113. dbesc($item["rendered-hash"]),
  114. intval($items[0]["id"])
  115. );
  116. logger("Updating public shadow for post ".$items[0]["id"]." - guid ".$item["guid"]." Result: ".print_r($result, true), LOGGER_DEBUG);
  117. }
  118. function delete_thread_uri($itemuri, $uid) {
  119. $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
  120. if(count($messages))
  121. foreach ($messages as $message)
  122. delete_thread($message["id"], $itemuri);
  123. }
  124. function delete_thread($itemid, $itemuri = "") {
  125. $item = q("SELECT `uid` FROM `thread` WHERE `iid` = %d", intval($itemid));
  126. $result = q("DELETE FROM `thread` WHERE `iid` = %d", intval($itemid));
  127. logger("delete_thread: Deleted thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
  128. if ($itemuri != "") {
  129. $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND NOT (`uid` IN (%d, 0))",
  130. dbesc($itemuri),
  131. intval($item["uid"])
  132. );
  133. if (!count($r)) {
  134. $r = q("DELETE FROM `item` WHERE `uri` = '%s' AND `uid` = 0)",
  135. dbesc($itemuri)
  136. );
  137. logger("delete_thread: Deleted shadow for item ".$itemuri." - ".print_r($result, true), LOGGER_DEBUG);
  138. }
  139. }
  140. }
  141. function update_threads() {
  142. global $db;
  143. logger("update_threads: start");
  144. $messages = $db->q("SELECT `id` FROM `item` WHERE `id` = `parent`", true);
  145. logger("update_threads: fetched messages: ".count($messages));
  146. while ($message = $db->qfetch())
  147. add_thread($message["id"]);
  148. $db->qclose();
  149. }
  150. function update_threads_mention() {
  151. $a = get_app();
  152. $users = q("SELECT `uid`, `nickname` FROM `user` ORDER BY `uid`");
  153. foreach ($users AS $user) {
  154. $self = normalise_link($a->get_baseurl() . '/profile/' . $user['nickname']);
  155. $selfhttps = str_replace("http://", "https://", $self);
  156. $parents = q("SELECT DISTINCT(`parent`) FROM `item` WHERE `uid` = %d AND
  157. ((`owner-link` IN ('%s', '%s')) OR (`author-link` IN ('%s', '%s')))",
  158. $user["uid"], $self, $selfhttps, $self, $selfhttps);
  159. foreach ($parents AS $parent)
  160. q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", $parent["parent"]);
  161. }
  162. }
  163. function update_shadow_copy() {
  164. global $db;
  165. logger("start");
  166. $messages = $db->q(sprintf("SELECT `iid` FROM `thread` WHERE `uid` != 0 AND `network` IN ('', '%s', '%s', '%s')
  167. AND `visible` AND NOT `deleted` AND NOT `moderated` AND NOT `private` ORDER BY `created`",
  168. NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS), true);
  169. logger("fetched messages: ".count($messages));
  170. while ($message = $db->qfetch())
  171. add_thread($message["iid"], true);
  172. $db->qclose();
  173. }
  174. ?>