]> git.mxchange.org Git - friendica.git/blob - src/Model/Item.php
75ed1e2a030d80615d1c3186596078457f79a83f
[friendica.git] / src / Model / Item.php
1 <?php
2
3 /**
4  * @file src/Model/Item.php
5  */
6
7 namespace Friendica\Model;
8
9 use Friendica\Core\Worker;
10 use Friendica\Model\Term;
11 use Friendica\Model\Contact;
12 use Friendica\Database\DBM;
13 use dba;
14
15 require_once 'include/tags.php';
16 require_once 'include/threads.php';
17
18 class Item
19 {
20         /**
21          * @brief Update existing item entries
22          *
23          * @param array $fields The fields that are to be changed
24          * @param array $condition The condition for finding the item entries
25          *
26          * In the future we may have to change permissions as well.
27          * Then we had to add the user id as third parameter.
28          *
29          * A return value of "0" doesn't mean an error - but that 0 rows had been changed.
30          *
31          * @return integer|boolean number of affected rows - or "false" if there was an error
32          */
33         public static function update(array $fields, array $condition)
34         {
35                 if (empty($condition) || empty($fields)) {
36                         return false;
37                 }
38
39                 $success = dba::update('item', $fields, $condition);
40
41                 if (!$success) {
42                         return false;
43                 }
44
45                 $rows = dba::affected_rows();
46
47                 // We cannot simply expand the condition to check for origin entries
48                 // The condition needn't to be a simple array but could be a complex condition.
49                 $items = dba::select('item', ['id', 'origin'], $condition);
50                 while ($item = dba::fetch($items)) {
51                         // We only need to notfiy others when it is an original entry from us
52                         if (!$item['origin']) {
53                                 continue;
54                         }
55
56                         create_tags_from_item($item['id']);
57                         Term::createFromItem($item['id']);
58                         update_thread($item['id']);
59
60                         Worker::add(PRIORITY_HIGH, "Notifier", 'edit_post', $item['id']);
61                 }
62
63                 return $rows;
64         }
65
66         public static function delete(array $condition, $priority = PRIORITY_HIGH)
67         {
68 /*
69                 // locate item to be deleted
70                 $item = dba::selectFirst('item', [], $condition);
71                 if (!DBM::is_result($item)) {
72                         return false;
73                 }
74
75                 if ($item['deleted']) {
76                         return false;
77                 }
78
79                 $owner = $item['uid'];
80
81                 $contact_id = 0;
82
83                 logger('delete item: ' . $item['id'], LOGGER_DEBUG);
84
85                 // clean up categories and tags so they don't end up as orphans
86
87                 $matches = false;
88                 $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
89                 if ($cnt) {
90                         foreach ($matches as $mtch) {
91                                 file_tag_unsave_file($item['uid'], $item['id'], $mtch[1],true);
92                         }
93                 }
94
95                 $matches = false;
96
97                 $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
98                 if ($cnt) {
99                         foreach ($matches as $mtch) {
100                                 file_tag_unsave_file($item['uid'], $item['id'], $mtch[1],false);
101                         }
102                 }
103
104                 //*
105                 // * If item is a link to a photo resource, nuke all the associated photos
106                 // * (visitors will not have photo resources)
107                 // * This only applies to photos uploaded from the photos page. Photos inserted into a post do not
108                 // * generate a resource-id and therefore aren't intimately linked to the item.
109                 // *
110                 if (strlen($item['resource-id'])) {
111                         dba::delete('photo', ['resource-id' => $item['resource-id'], 'uid' => $item['uid']]);
112                 }
113
114                 // If item is a link to an event, nuke the event record.
115                 if (intval($item['event-id'])) {
116                         dba::delete('event', ['id' => $item['event-id'], 'uid' => $item['uid']]);
117                 }
118
119                 // If item has attachments, drop them
120                 foreach (explode(", ", $item['attach']) as $attach) {
121                         preg_match("|attach/(\d+)|", $attach, $matches);
122                         dba::delete('attach', ['id' => $matches[1], 'uid' => $item['uid']]);
123                 }
124
125                 // Set the item to "deleted"
126                 // Don't delete it here, since we have to send delete messages
127                 dba::update('item', ['deleted' => true, 'title' => '', 'body' => '',
128                                         'edited' => datetime_convert(), 'changed' => datetime_convert()],
129                                 ['id' => $item['id']]);
130
131                 create_tags_from_item($item['id']);
132                 Term::createFromItem($item['id']);
133                 delete_thread($item['id'], $item['parent-uri']);
134
135                 // Creating list of parents
136                 $r = q("SELECT `id` FROM `item` WHERE `parent` = %d AND `uid` = %d",
137                         intval($item['id']),
138                         intval($item['uid'])
139                 );
140
141                 $parentid = "";
142
143                 foreach ($r as $row) {
144                         if ($parentid != "") {
145                                 $parentid .= ", ";
146                         }
147
148                         $parentid .= $row["id"];
149                 }
150
151                 // Now delete them
152                 if ($parentid != "") {
153                         q("DELETE FROM `sign` WHERE `iid` IN (%s)", dbesc($parentid));
154                 }
155
156                 // If it's the parent of a comment thread, kill all the kids
157                 if ($item['uri'] == $item['parent-uri']) {
158                         dba::update('item', ['deleted' => true, 'title' => '', 'body' => '',
159                                         'edited' => datetime_convert(), 'changed' => datetime_convert()],
160                                 ['parent-uri' => $item['parent-uri'], 'uid' => $item['uid']]);
161
162                         create_tags_from_itemuri($item['parent-uri'], $item['uid']);
163                         Term::createFromItemURI($item['parent-uri'], $item['uid']);
164                         delete_thread_uri($item['parent-uri'], $item['uid']);
165                         // ignore the result
166                 } else {
167                         // ensure that last-child is set in case the comment that had it just got wiped.
168                         dba::update('item', ['last-child' => false, 'changed' => datetime_convert()],
169                                         ['parent-uri' => $item['parent-uri'], 'uid' => $item['uid']]);
170
171                         // who is the last child now?
172                         $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d ORDER BY `edited` DESC LIMIT 1",
173                                 dbesc($item['parent-uri']),
174                                 intval($item['uid'])
175                         );
176                         if (DBM::is_result($r)) {
177                                 dba::update('item', ['last-child' => true], ['id' => $r[0]['id']]);
178                         }
179                 }
180
181                 // send the notification upstream/downstream
182                 Worker::add(['priority' => $priority, 'dont_fork' => true], "Notifier", "drop", intval($item['id']));
183
184                 return true;
185 */
186         }
187
188         /**
189          * @brief Add a shadow entry for a given item id that is a thread starter
190          *
191          * We store every public item entry additionally with the user id "0".
192          * This is used for the community page and for the search.
193          * It is planned that in the future we will store public item entries only once.
194          *
195          * @param integer $itemid Item ID that should be added
196          */
197         public static function addShadow($itemid)
198         {
199                 $fields = ['uid', 'wall', 'private', 'moderated', 'visible', 'contact-id', 'deleted', 'network', 'author-id', 'owner-id'];
200                 $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
201                 $item = dba::selectFirst('item', $fields, $condition);
202
203                 if (!DBM::is_result($item)) {
204                         return;
205                 }
206
207                 // is it already a copy?
208                 if (($itemid == 0) || ($item['uid'] == 0)) {
209                         return;
210                 }
211
212                 // Is it a visible public post?
213                 if (!$item["visible"] || $item["deleted"] || $item["moderated"] || $item["private"]) {
214                         return;
215                 }
216
217                 // is it an entry from a connector? Only add an entry for natively connected networks
218                 if (!in_array($item["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""])) {
219                         return;
220                 }
221
222                 // Is the public contact configured as hidden?
223                 if (Contact::isHidden($item["owner-id"]) || Contact::isHidden($item["author-id"])) {
224                         return;
225                 }
226
227                 // Only do these checks if the post isn't a wall post
228                 if (!$item["wall"]) {
229                         // Check, if hide-friends is activated - then don't do a shadow entry
230                         if (dba::exists('profile', ['is-default' => true, 'uid' => $item['uid'], 'hide-friends' => true])) {
231                                 return;
232                         }
233
234                         // Check if the contact is hidden or blocked
235                         if (!dba::exists('contact', ['hidden' => false, 'blocked' => false, 'id' => $item['contact-id']])) {
236                                 return;
237                         }
238                 }
239
240                 // Only add a shadow, if the profile isn't hidden
241                 if (dba::exists('user', ['uid' => $item['uid'], 'hidewall' => true])) {
242                         return;
243                 }
244
245                 $item = dba::selectFirst('item', [], ['id' => $itemid]);
246
247                 if (count($item) && ($item["allow_cid"] == '')  && ($item["allow_gid"] == '') &&
248                         ($item["deny_cid"] == '') && ($item["deny_gid"] == '')) {
249
250                         if (!dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
251                                 // Preparing public shadow (removing user specific data)
252                                 require_once("include/items.php");
253
254                                 unset($item['id']);
255                                 $item['uid'] = 0;
256                                 $item['origin'] = 0;
257                                 $item['wall'] = 0;
258                                 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
259
260                                 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
261                                         $item['type'] = 'remote-comment';
262                                 } elseif ($item['type'] == 'wall') {
263                                         $item['type'] = 'remote';
264                                 }
265
266                                 $public_shadow = item_store($item, false, false, true);
267
268                                 logger("Stored public shadow for thread ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
269                         }
270                 }
271         }
272
273         /**
274          * @brief Add a shadow entry for a given item id that is a comment
275          *
276          * This function does the same like the function above - but for comments
277          *
278          * @param integer $itemid Item ID that should be added
279          */
280         public static function addShadowPost($itemid)
281         {
282                 $item = dba::selectFirst('item', [], ['id' => $itemid]);
283                 if (!DBM::is_result($item)) {
284                         return;
285                 }
286
287                 // Is it a toplevel post?
288                 if ($item['id'] == $item['parent']) {
289                         self::addShadow($itemid);
290                         return;
291                 }
292
293                 // Is this a shadow entry?
294                 if ($item['uid'] == 0)
295                         return;
296
297                 // Is there a shadow parent?
298                 if (!dba::exists('item', ['uri' => $item['parent-uri'], 'uid' => 0])) {
299                         return;
300                 }
301
302                 // Is there already a shadow entry?
303                 if (dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
304                         return;
305                 }
306
307                 // Preparing public shadow (removing user specific data)
308                 require_once("include/items.php");
309
310                 unset($item['id']);
311                 $item['uid'] = 0;
312                 $item['origin'] = 0;
313                 $item['wall'] = 0;
314                 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
315
316                 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
317                         $item['type'] = 'remote-comment';
318                 } elseif ($item['type'] == 'wall') {
319                         $item['type'] = 'remote';
320                 }
321
322                 $public_shadow = item_store($item, false, false, true);
323
324                 logger("Stored public shadow for comment ".$item['uri']." under id ".$public_shadow, LOGGER_DEBUG);
325         }
326 }