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