]> git.mxchange.org Git - friendica.git/blob - mod/community.php
Correct content type
[friendica.git] / mod / community.php
1 <?php
2 /**
3  * @file mod/community.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Nav;
8 use Friendica\Core\ACL;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\PConfig;
12 use Friendica\Database\DBA;
13 use Friendica\Model\Contact;
14
15 function community_init(App $a)
16 {
17         if (!local_user()) {
18                 unset($_SESSION['theme']);
19                 unset($_SESSION['mobile-theme']);
20         }
21 }
22
23 function community_content(App $a, $update = 0)
24 {
25         $o = '';
26
27         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
28                 notice(L10n::t('Public access denied.') . EOL);
29                 return;
30         }
31
32         $page_style = Config::get('system', 'community_page_style');
33
34         if ($page_style == CP_NO_INTERNAL_COMMUNITY) {
35                 notice(L10n::t('Access denied.') . EOL);
36                 return;
37         }
38
39         $accounttype = null;
40
41         if ($a->argc > 2) {
42                 switch ($a->argv[2]) {
43                         case 'person':
44                                 $accounttype = Contact::ACCOUNT_TYPE_PERSON;
45                                 break;
46                         case 'organisation':
47                                 $accounttype = Contact::ACCOUNT_TYPE_ORGANISATION;
48                                 break;
49                         case 'news':
50                                 $accounttype = Contact::ACCOUNT_TYPE_NEWS;
51                                 break;
52                         case 'community':
53                                 $accounttype = Contact::ACCOUNT_TYPE_COMMUNITY;
54                                 break;
55                 }
56         }
57
58         if ($a->argc > 1) {
59                 $content = $a->argv[1];
60         } else {
61                 if (!empty(Config::get('system', 'singleuser'))) {
62                         // On single user systems only the global page does make sense
63                         $content = 'global';
64                 } else {
65                         // When only the global community is allowed, we use this as default
66                         $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
67                 }
68         }
69
70         if (!in_array($content, ['local', 'global'])) {
71                 notice(L10n::t('Community option not available.') . EOL);
72                 return;
73         }
74
75         // Check if we are allowed to display the content to visitors
76         if (!local_user()) {
77                 $available = $page_style == CP_USERS_AND_GLOBAL;
78
79                 if (!$available) {
80                         $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
81                 }
82
83                 if (!$available) {
84                         $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
85                 }
86
87                 if (!$available) {
88                         notice(L10n::t('Not available.') . EOL);
89                         return;
90                 }
91         }
92
93         require_once 'include/security.php';
94         require_once 'include/conversation.php';
95
96         if (!$update) {
97                 $tabs = [];
98
99                 if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(Config::get('system', 'singleuser'))) {
100                         $tabs[] = [
101                                 'label' => L10n::t('Local Community'),
102                                 'url' => 'community/local',
103                                 'sel' => $content == 'local' ? 'active' : '',
104                                 'title' => L10n::t('Posts from local users on this server'),
105                                 'id' => 'community-local-tab',
106                                 'accesskey' => 'l'
107                         ];
108                 }
109
110                 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
111                         $tabs[] = [
112                                 'label' => L10n::t('Global Community'),
113                                 'url' => 'community/global',
114                                 'sel' => $content == 'global' ? 'active' : '',
115                                 'title' => L10n::t('Posts from users of the whole federated network'),
116                                 'id' => 'community-global-tab',
117                                 'accesskey' => 'g'
118                         ];
119                 }
120
121                 $tab_tpl = get_markup_template('common_tabs.tpl');
122                 $o .= replace_macros($tab_tpl, ['$tabs' => $tabs]);
123
124                 Nav::setSelected('community');
125
126                 // We need the editor here to be able to reshare an item.
127                 if (local_user()) {
128                         $x = [
129                                 'is_owner' => true,
130                                 'allow_location' => $a->user['allow_location'],
131                                 'default_location' => $a->user['default-location'],
132                                 'nickname' => $a->user['nickname'],
133                                 'lockstate' => (is_array($a->user) && (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) || strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
134                                 'acl' => ACL::getFullSelectorHTML($a->user, true),
135                                 'bang' => '',
136                                 'visitor' => 'block',
137                                 'profile_uid' => local_user(),
138                         ];
139                         $o .= status_editor($a, $x, 0, true);
140                 }
141         }
142
143         // check if we serve a mobile device and get the user settings accordingly
144         if ($a->is_mobile) {
145                 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 20);
146         } else {
147                 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 40);
148         }
149
150         // now that we have the user settings, see if the theme forces
151         // a maximum item number which is lower then the user choice
152         if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
153                 $itemspage_network = $a->force_max_items;
154         }
155
156         $a->set_pager_itemspage($itemspage_network);
157
158         $r = community_getitems($a->pager['start'], $a->pager['itemspage'], $content, $accounttype);
159
160         if (!DBA::isResult($r)) {
161                 info(L10n::t('No results.') . EOL);
162                 return $o;
163         }
164
165         $maxpostperauthor = (int) Config::get('system', 'max_author_posts_community_page');
166
167         if (($maxpostperauthor != 0) && ($content == 'local')) {
168                 $count = 1;
169                 $previousauthor = "";
170                 $numposts = 0;
171                 $s = [];
172
173                 do {
174                         foreach ($r as $item) {
175                                 if ($previousauthor == $item["author-link"]) {
176                                         ++$numposts;
177                                 } else {
178                                         $numposts = 0;
179                                 }
180                                 $previousauthor = $item["author-link"];
181
182                                 if (($numposts < $maxpostperauthor) && (count($s) < $a->pager['itemspage'])) {
183                                         $s[] = $item;
184                                 }
185                         }
186                         if (count($s) < $a->pager['itemspage']) {
187                                 $r = community_getitems($a->pager['start'] + ($count * $a->pager['itemspage']), $a->pager['itemspage'], $content, $accounttype);
188                         }
189                 } while ((count($s) < $a->pager['itemspage']) && ( ++$count < 50) && (count($r) > 0));
190         } else {
191                 $s = $r;
192         }
193
194         $o .= conversation($a, $s, 'community', $update, false, 'commented', local_user());
195
196         if (!$update) {
197                 $o .= alt_pager($a, count($r));
198         }
199
200         $t = get_markup_template("community.tpl");
201         return replace_macros($t, [
202                 '$content' => $o,
203                 '$header' => '',
204                 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
205                 '$global_community_hint' => L10n::t("This community stream shows all public posts received by this node. They may not reflect the opinions of this node’s users.")
206         ]);
207 }
208
209 function community_getitems($start, $itemspage, $content, $accounttype)
210 {
211         if ($content == 'local') {
212                 if (!is_null($accounttype)) {
213                         $sql_accounttype = " AND `user`.`account-type` = ?";
214                         $values = [$accounttype, $start, $itemspage];
215                 } else {
216                         $sql_accounttype = "";
217                         $values = [$start, $itemspage];
218                 }
219
220                 $r = DBA::p("SELECT `item`.`uri`, `author`.`url` AS `author-link` FROM `thread`
221                         INNER JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
222                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
223                         INNER JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
224                         WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
225                         AND NOT `thread`.`private` AND `thread`.`wall` AND `thread`.`origin` $sql_accounttype
226                         ORDER BY `thread`.`commented` DESC LIMIT ?, ?", $values);
227                 return DBA::toArray($r);
228         } elseif ($content == 'global') {
229                 if (!is_null($accounttype)) {
230                         $sql_accounttype = " AND `owner`.`contact-type` = ?";
231                         $values = [$accounttype, $start, $itemspage];
232                 } else {
233                         $sql_accounttype = "";
234                         $values = [$start, $itemspage];
235                 }
236
237                 $r = DBA::p("SELECT `uri` FROM `thread`
238                                 INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
239                                 INNER JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
240                                 INNER JOIN `contact` AS `owner` ON `owner`.`id`=`item`.`owner-id`
241                                 WHERE `thread`.`uid` = 0 AND NOT `author`.`hidden` AND NOT `author`.`blocked` $sql_accounttype
242                                 ORDER BY `thread`.`commented` DESC LIMIT ?, ?", $values);
243                 return DBA::toArray($r);
244         }
245
246         // Should never happen
247         return [];
248 }