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