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