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