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.

299 lines
9.2KB

  1. <?php
  2. use Friendica\App;
  3. function add_thread($itemid, $onlyshadow = false) {
  4. $items = q("SELECT `uid`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`,
  5. `moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`, `gcontact-id`,
  6. `deleted`, `origin`, `forum_mode`, `mention`, `network`, `author-id`, `owner-id`
  7. FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid));
  8. if (!$items)
  9. return;
  10. $item = $items[0];
  11. $item['iid'] = $itemid;
  12. if (!$onlyshadow) {
  13. $result = dbq("INSERT INTO `thread` (`"
  14. .implode("`, `", array_keys($item))
  15. ."`) VALUES ('"
  16. .implode("', '", array_values($item))
  17. ."')");
  18. logger("Add thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
  19. }
  20. }
  21. /**
  22. * @brief Add a shadow entry for a given item id that is a thread starter
  23. *
  24. * We store every public item entry additionally with the user id "0".
  25. * This is used for the community page and for the search.
  26. * It is planned that in the future we will store public item entries only once.
  27. *
  28. * @param integer $itemid Item ID that should be added
  29. */
  30. function add_shadow_thread($itemid) {
  31. $items = q("SELECT `uid`, `wall`, `private`, `moderated`, `visible`, `contact-id`, `deleted`, `network`
  32. FROM `item` WHERE `id` = %d AND (`parent` = %d OR `parent` = 0) LIMIT 1", intval($itemid), intval($itemid));
  33. if (!dbm::is_result($items)) {
  34. return;
  35. }
  36. $item = $items[0];
  37. // is it already a copy?
  38. if (($itemid == 0) OR ($item['uid'] == 0)) {
  39. return;
  40. }
  41. // Is it a visible public post?
  42. if (!$item["visible"] OR $item["deleted"] OR $item["moderated"] OR $item["private"]) {
  43. return;
  44. }
  45. // is it an entry from a connector? Only add an entry for natively connected networks
  46. if (!in_array($item["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
  47. return;
  48. }
  49. // Only do these checks if the post isn't a wall post
  50. if (!$item["wall"]) {
  51. // Check, if hide-friends is activated - then don't do a shadow entry
  52. $r = q("SELECT `hide-friends` FROM `profile` WHERE `is-default` AND `uid` = %d AND NOT `hide-friends`",
  53. $item['uid']);
  54. if (!dbm::is_result($r)) {
  55. return;
  56. }
  57. // Check if the contact is hidden or blocked
  58. $r = q("SELECT `id` FROM `contact` WHERE NOT `hidden` AND NOT `blocked` AND `id` = %d",
  59. $item['contact-id']);
  60. if (!dbm::is_result($r)) {
  61. return;
  62. }
  63. }
  64. // Only add a shadow, if the profile isn't hidden
  65. $r = q("SELECT `uid` FROM `user` where `uid` = %d AND NOT `hidewall`", $item['uid']);
  66. if (!dbm::is_result($r)) {
  67. return;
  68. }
  69. $item = q("SELECT * FROM `item` WHERE `id` = %d", intval($itemid));
  70. if (count($item) AND ($item[0]["allow_cid"] == '') AND ($item[0]["allow_gid"] == '') AND
  71. ($item[0]["deny_cid"] == '') AND ($item[0]["deny_gid"] == '')) {
  72. $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1",
  73. dbesc($item['uri']));
  74. if (!dbm::is_result($r)) {
  75. // Preparing public shadow (removing user specific data)
  76. require_once("include/items.php");
  77. require_once("include/Contact.php");
  78. unset($item[0]['id']);
  79. $item[0]['uid'] = 0;
  80. $item[0]['origin'] = 0;
  81. $item[0]['wall'] = 0;
  82. $item[0]['contact-id'] = get_contact($item[0]['author-link'], 0);
  83. if (in_array($item[0]['type'], array("net-comment", "wall-comment"))) {
  84. $item[0]['type'] = 'remote-comment';
  85. } elseif ($item[0]['type'] == 'wall') {
  86. $item[0]['type'] = 'remote';
  87. }
  88. $public_shadow = item_store($item[0], false, false, true);
  89. logger("Stored public shadow for thread ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
  90. }
  91. }
  92. }
  93. /**
  94. * @brief Add a shadow entry for a given item id that is a comment
  95. *
  96. * This function does the same like the function above - but for comments
  97. *
  98. * @param integer $itemid Item ID that should be added
  99. */
  100. function add_shadow_entry($itemid) {
  101. $items = q("SELECT * FROM `item` WHERE `id` = %d", intval($itemid));
  102. if (!dbm::is_result($items)) {
  103. return;
  104. }
  105. $item = $items[0];
  106. // Is it a toplevel post?
  107. if ($item['id'] == $item['parent']) {
  108. add_shadow_thread($itemid);
  109. return;
  110. }
  111. // Is this a shadow entry?
  112. if ($item['uid'] == 0)
  113. return;
  114. // Is there a shadow parent?
  115. $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1", dbesc($item['parent-uri']));
  116. if (!dbm::is_result($r))
  117. return;
  118. // Is there already a shadow entry?
  119. $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1", dbesc($item['uri']));
  120. if (dbm::is_result($r))
  121. return;
  122. // Preparing public shadow (removing user specific data)
  123. require_once("include/items.php");
  124. require_once("include/Contact.php");
  125. unset($item['id']);
  126. $item['uid'] = 0;
  127. $item['origin'] = 0;
  128. $item['wall'] = 0;
  129. $item['contact-id'] = get_contact($item['author-link'], 0);
  130. if (in_array($item['type'], array("net-comment", "wall-comment"))) {
  131. $item['type'] = 'remote-comment';
  132. } elseif ($item['type'] == 'wall') {
  133. $item['type'] = 'remote';
  134. }
  135. $public_shadow = item_store($item, false, false, true);
  136. logger("Stored public shadow for comment ".$item['uri']." under id ".$public_shadow, LOGGER_DEBUG);
  137. }
  138. function update_thread_uri($itemuri, $uid) {
  139. $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
  140. if (dbm::is_result($messages))
  141. foreach ($messages as $message)
  142. update_thread($message["id"]);
  143. }
  144. function update_thread($itemid, $setmention = false) {
  145. $items = q("SELECT `uid`, `guid`, `title`, `body`, `created`, `edited`, `commented`, `received`, `changed`, `wall`, `private`, `pubmail`, `moderated`, `visible`, `spam`, `starred`, `bookmark`, `contact-id`, `gcontact-id`,
  146. `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));
  147. if (!dbm::is_result($items))
  148. return;
  149. $item = $items[0];
  150. if ($setmention)
  151. $item["mention"] = 1;
  152. $sql = "";
  153. foreach ($item AS $field => $data)
  154. if (!in_array($field, array("guid", "title", "body", "rendered-html", "rendered-hash"))) {
  155. if ($sql != "")
  156. $sql .= ", ";
  157. $sql .= "`".$field."` = '".dbesc($data)."'";
  158. }
  159. $result = q("UPDATE `thread` SET ".$sql." WHERE `iid` = %d", intval($itemid));
  160. logger("Update thread for item ".$itemid." - guid ".$item["guid"]." - ".print_r($result, true)." ".print_r($item, true), LOGGER_DEBUG);
  161. // Updating a shadow item entry
  162. $items = q("SELECT `id` FROM `item` WHERE `guid` = '%s' AND `uid` = 0 LIMIT 1", dbesc($item["guid"]));
  163. if (!$items)
  164. return;
  165. $result = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `rendered-html` = '%s', `rendered-hash` = '%s' WHERE `id` = %d",
  166. dbesc($item["title"]),
  167. dbesc($item["body"]),
  168. dbesc($item["rendered-html"]),
  169. dbesc($item["rendered-hash"]),
  170. intval($items[0]["id"])
  171. );
  172. logger("Updating public shadow for post ".$items[0]["id"]." - guid ".$item["guid"]." Result: ".print_r($result, true), LOGGER_DEBUG);
  173. }
  174. function delete_thread_uri($itemuri, $uid) {
  175. $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
  176. if(count($messages))
  177. foreach ($messages as $message)
  178. delete_thread($message["id"], $itemuri);
  179. }
  180. function delete_thread($itemid, $itemuri = "") {
  181. $item = q("SELECT `uid` FROM `thread` WHERE `iid` = %d", intval($itemid));
  182. $result = q("DELETE FROM `thread` WHERE `iid` = %d", intval($itemid));
  183. logger("delete_thread: Deleted thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
  184. if ($itemuri != "") {
  185. $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND NOT (`uid` IN (%d, 0))",
  186. dbesc($itemuri),
  187. intval($item["uid"])
  188. );
  189. if (!dbm::is_result($r)) {
  190. $r = q("DELETE FROM `item` WHERE `uri` = '%s' AND `uid` = 0",
  191. dbesc($itemuri)
  192. );
  193. logger("delete_thread: Deleted shadow for item ".$itemuri." - ".print_r($result, true), LOGGER_DEBUG);
  194. }
  195. }
  196. }
  197. function update_threads() {
  198. logger("update_threads: start");
  199. $messages = dba::p("SELECT `id` FROM `item` WHERE `id` = `parent`");
  200. logger("update_threads: fetched messages: ".dba::num_rows($messages));
  201. while ($message = dba::fetch($messages)) {
  202. add_thread($message["id"]);
  203. add_shadow_thread($message["id"]);
  204. }
  205. dba::close($messages);
  206. }
  207. function update_threads_mention() {
  208. $users = q("SELECT `uid`, `nickname` FROM `user` ORDER BY `uid`");
  209. foreach ($users AS $user) {
  210. $self = normalise_link(App::get_baseurl() . '/profile/' . $user['nickname']);
  211. $selfhttps = str_replace("http://", "https://", $self);
  212. $parents = q("SELECT DISTINCT(`parent`) FROM `item` WHERE `uid` = %d AND
  213. ((`owner-link` IN ('%s', '%s')) OR (`author-link` IN ('%s', '%s')))",
  214. $user["uid"], $self, $selfhttps, $self, $selfhttps);
  215. foreach ($parents AS $parent)
  216. q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", $parent["parent"]);
  217. }
  218. }
  219. function update_shadow_copy() {
  220. logger("start");
  221. $messages = dba::p("SELECT `iid` FROM `thread` WHERE `uid` != 0 AND `network` IN ('', ?, ?, ?)
  222. AND `visible` AND NOT `deleted` AND NOT `moderated` AND NOT `private` ORDER BY `created`",
  223. NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS);
  224. logger("fetched messages: ".dba::num_rows($messages));
  225. while ($message = dba::fetch($messages))
  226. add_shadow_thread($message["iid"]);
  227. dba::close($messages);
  228. }