]> git.mxchange.org Git - friendica.git/blob - mod/community.php
Update use statement lists with new Friendica\Database\dba class
[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\Database\DBM;
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         if ($a->argc > 1) {
40                 $content = $a->argv[1];
41         } else {
42                 if (!empty(Config::get('system', 'singleuser'))) {
43                         // On single user systems only the global page does make sense
44                         $content = 'global';
45                 } else {
46                         // When only the global community is allowed, we use this as default
47                         $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
48                 }
49         }
50
51         if (!in_array($content, ['local', 'global'])) {
52                 notice(L10n::t('Community option not available.') . EOL);
53                 return;
54         }
55
56         // Check if we are allowed to display the content to visitors
57         if (!local_user()) {
58                 $available = $page_style == CP_USERS_AND_GLOBAL;
59
60                 if (!$available) {
61                         $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
62                 }
63
64                 if (!$available) {
65                         $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
66                 }
67
68                 if (!$available) {
69                         notice(L10n::t('Not available.') . EOL);
70                         return;
71                 }
72         }
73
74         require_once 'include/security.php';
75         require_once 'include/conversation.php';
76
77         if (!$update) {
78                 $tabs = [];
79
80                 if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(Config::get('system', 'singleuser'))) {
81                         $tabs[] = [
82                                 'label' => L10n::t('Local Community'),
83                                 'url' => 'community/local',
84                                 'sel' => $content == 'local' ? 'active' : '',
85                                 'title' => L10n::t('Posts from local users on this server'),
86                                 'id' => 'community-local-tab',
87                                 'accesskey' => 'l'
88                         ];
89                 }
90
91                 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
92                         $tabs[] = [
93                                 'label' => L10n::t('Global Community'),
94                                 'url' => 'community/global',
95                                 'sel' => $content == 'global' ? 'active' : '',
96                                 'title' => L10n::t('Posts from users of the whole federated network'),
97                                 'id' => 'community-global-tab',
98                                 'accesskey' => 'g'
99                         ];
100                 }
101
102                 $tab_tpl = get_markup_template('common_tabs.tpl');
103                 $o .= replace_macros($tab_tpl, ['$tabs' => $tabs]);
104
105                 Nav::setSelected('community');
106
107                 // We need the editor here to be able to reshare an item.
108                 if (local_user()) {
109                         $x = [
110                                 'is_owner' => true,
111                                 'allow_location' => $a->user['allow_location'],
112                                 'default_location' => $a->user['default-location'],
113                                 'nickname' => $a->user['nickname'],
114                                 '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'),
115                                 'acl' => ACL::getFullSelectorHTML($a->user, true),
116                                 'bang' => '',
117                                 'visitor' => 'block',
118                                 'profile_uid' => local_user(),
119                         ];
120                         $o .= status_editor($a, $x, 0, true);
121                 }
122         }
123
124         // check if we serve a mobile device and get the user settings accordingly
125         if ($a->is_mobile) {
126                 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 20);
127         } else {
128                 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 40);
129         }
130
131         // now that we have the user settings, see if the theme forces
132         // a maximum item number which is lower then the user choice
133         if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
134                 $itemspage_network = $a->force_max_items;
135         }
136
137         $a->set_pager_itemspage($itemspage_network);
138
139         $r = community_getitems($a->pager['start'], $a->pager['itemspage'], $content);
140
141         if (!DBM::is_result($r)) {
142                 info(L10n::t('No results.') . EOL);
143                 return $o;
144         }
145
146         $maxpostperauthor = (int) Config::get('system', 'max_author_posts_community_page');
147
148         if (($maxpostperauthor != 0) && ($content == 'local')) {
149                 $count = 1;
150                 $previousauthor = "";
151                 $numposts = 0;
152                 $s = [];
153
154                 do {
155                         foreach ($r as $item) {
156                                 if ($previousauthor == $item["author-link"]) {
157                                         ++$numposts;
158                                 } else {
159                                         $numposts = 0;
160                                 }
161                                 $previousauthor = $item["author-link"];
162
163                                 if (($numposts < $maxpostperauthor) && (count($s) < $a->pager['itemspage'])) {
164                                         $s[] = $item;
165                                 }
166                         }
167                         if (count($s) < $a->pager['itemspage']) {
168                                 $r = community_getitems($a->pager['start'] + ($count * $a->pager['itemspage']), $a->pager['itemspage'], $content);
169                         }
170                 } while ((count($s) < $a->pager['itemspage']) && ( ++$count < 50) && (count($r) > 0));
171         } else {
172                 $s = $r;
173         }
174
175         $o .= conversation($a, $s, 'community', $update, false, 'commented', local_user());
176
177         if (!$update) {
178                 $o .= alt_pager($a, count($r));
179         }
180
181         $t = get_markup_template("community.tpl");
182         return replace_macros($t, [
183                 '$content' => $o,
184                 '$header' => '',
185                 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
186                 '$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.")
187         ]);
188 }
189
190 function community_getitems($start, $itemspage, $content)
191 {
192         if ($content == 'local') {
193                 $r = dba::p("SELECT `item`.`uri`, `author`.`url` AS `author-link` FROM `thread`
194                         INNER JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
195                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
196                         INNER JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
197                         WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
198                         AND NOT `thread`.`private` AND `thread`.`wall` AND `thread`.`origin`
199                         ORDER BY `thread`.`commented` DESC LIMIT " . intval($start) . ", " . intval($itemspage)
200                 );
201                 return dba::inArray($r);
202         } elseif ($content == 'global') {
203                 $r = dba::p("SELECT `uri` FROM `thread`
204                                 INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
205                                 INNER JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
206                                 WHERE `thread`.`uid` = 0 AND NOT `author`.`hidden` AND NOT `author`.`blocked`
207                                 ORDER BY `thread`.`commented` DESC LIMIT " . intval($start) . ", " . intval($itemspage));
208                 return dba::inArray($r);
209         }
210
211         // Should never happen
212         return [];
213 }