]> git.mxchange.org Git - friendica.git/blob - src/Model/Item.php
Oh, standards ...
[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         /**
67          * @brief Add a shadow entry for a given item id that is a thread starter
68          *
69          * We store every public item entry additionally with the user id "0".
70          * This is used for the community page and for the search.
71          * It is planned that in the future we will store public item entries only once.
72          *
73          * @param integer $itemid Item ID that should be added
74          */
75         public static function addShadow($itemid)
76         {
77                 $fields = ['uid', 'wall', 'private', 'moderated', 'visible', 'contact-id', 'deleted', 'network', 'author-id', 'owner-id'];
78                 $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
79                 $item = dba::selectFirst('item', $fields, $condition);
80
81                 if (!DBM::is_result($item)) {
82                         return;
83                 }
84
85                 // is it already a copy?
86                 if (($itemid == 0) || ($item['uid'] == 0)) {
87                         return;
88                 }
89
90                 // Is it a visible public post?
91                 if (!$item["visible"] || $item["deleted"] || $item["moderated"] || $item["private"]) {
92                         return;
93                 }
94
95                 // is it an entry from a connector? Only add an entry for natively connected networks
96                 if (!in_array($item["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""])) {
97                         return;
98                 }
99
100                 // Is the public contact configured as hidden?
101                 if (Contact::isHidden($item["owner-id"]) || Contact::isHidden($item["author-id"])) {
102                         return;
103                 }
104
105                 // Only do these checks if the post isn't a wall post
106                 if (!$item["wall"]) {
107                         // Check, if hide-friends is activated - then don't do a shadow entry
108                         if (dba::exists('profile', ['is-default' => true, 'uid' => $item['uid'], 'hide-friends' => true])) {
109                                 return;
110                         }
111
112                         // Check if the contact is hidden or blocked
113                         if (!dba::exists('contact', ['hidden' => false, 'blocked' => false, 'id' => $item['contact-id']])) {
114                                 return;
115                         }
116                 }
117
118                 // Only add a shadow, if the profile isn't hidden
119                 if (dba::exists('user', ['uid' => $item['uid'], 'hidewall' => true])) {
120                         return;
121                 }
122
123                 $item = dba::selectFirst('item', [], ['id' => $itemid]);
124
125                 if (count($item) && ($item["allow_cid"] == '')  && ($item["allow_gid"] == '') &&
126                         ($item["deny_cid"] == '') && ($item["deny_gid"] == '')) {
127
128                         if (!dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
129                                 // Preparing public shadow (removing user specific data)
130                                 require_once("include/items.php");
131
132                                 unset($item['id']);
133                                 $item['uid'] = 0;
134                                 $item['origin'] = 0;
135                                 $item['wall'] = 0;
136                                 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
137
138                                 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
139                                         $item['type'] = 'remote-comment';
140                                 } elseif ($item['type'] == 'wall') {
141                                         $item['type'] = 'remote';
142                                 }
143
144                                 $public_shadow = item_store($item, false, false, true);
145
146                                 logger("Stored public shadow for thread ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);
147                         }
148                 }
149         }
150
151         /**
152          * @brief Add a shadow entry for a given item id that is a comment
153          *
154          * This function does the same like the function above - but for comments
155          *
156          * @param integer $itemid Item ID that should be added
157          */
158         public static function addShadowPost($itemid)
159         {
160                 $item = dba::selectFirst('item', [], ['id' => $itemid]);
161                 if (!DBM::is_result($item)) {
162                         return;
163                 }
164
165                 // Is it a toplevel post?
166                 if ($item['id'] == $item['parent']) {
167                         self::addShadow($itemid);
168                         return;
169                 }
170
171                 // Is this a shadow entry?
172                 if ($item['uid'] == 0)
173                         return;
174
175                 // Is there a shadow parent?
176                 if (!dba::exists('item', ['uri' => $item['parent-uri'], 'uid' => 0])) {
177                         return;
178                 }
179
180                 // Is there already a shadow entry?
181                 if (dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
182                         return;
183                 }
184
185                 // Preparing public shadow (removing user specific data)
186                 require_once("include/items.php");
187
188                 unset($item['id']);
189                 $item['uid'] = 0;
190                 $item['origin'] = 0;
191                 $item['wall'] = 0;
192                 $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
193
194                 if (in_array($item['type'], ["net-comment", "wall-comment"])) {
195                         $item['type'] = 'remote-comment';
196                 } elseif ($item['type'] == 'wall') {
197                         $item['type'] = 'remote';
198                 }
199
200                 $public_shadow = item_store($item, false, false, true);
201
202                 logger("Stored public shadow for comment ".$item['uri']." under id ".$public_shadow, LOGGER_DEBUG);
203         }
204 }