]> git.mxchange.org Git - friendica.git/blob - mod/community.php
Merge pull request #4177 from annando/community-singleuser
[friendica.git] / mod / community.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Config;
5 use Friendica\Core\PConfig;
6 use Friendica\Database\DBM;
7
8 function community_init(App $a)
9 {
10         if (!local_user()) {
11                 unset($_SESSION['theme']);
12                 unset($_SESSION['mobile-theme']);
13         }
14 }
15
16 function community_content(App $a, $update = 0)
17 {
18         $o = '';
19
20         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
21                 notice(t('Public access denied.') . EOL);
22                 return;
23         }
24
25         $page_style = Config::get('system', 'community_page_style');
26
27         if ($a->argc > 1) {
28                 $content = $a->argv[1];
29         } else {
30                 if (!empty(Config::get('system','singleuser'))) {
31                         // On single user systems only the global page does make sense
32                         $content = 'global';
33                 } else {
34                         // When only the global community is allowed, we use this as default
35                         $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
36                 }
37         }
38
39         if (!in_array($content, ['local', 'global'])) {
40                 notice(t('Community option not available.') . EOL);
41                 return;
42         }
43
44         // Check if we are allowed to display the content to visitors
45         if (!local_user()) {
46                 $available = $page_style == CP_USERS_AND_GLOBAL;
47
48                 if (!$available) {
49                         $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
50                 }
51
52                 if (!$available) {
53                         $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
54                 }
55
56                 if (!$available) {
57                         notice(t('Not available.') . EOL);
58                         return;
59                 }
60         }
61
62         require_once 'include/bbcode.php';
63         require_once 'include/security.php';
64         require_once 'include/conversation.php';
65
66         if (!$update) {
67                 $tabs = [];
68
69                 if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(Config::get('system','singleuser'))) {
70                         $tabs[] = array(
71                                 'label' => t('Community'),
72                                 'url' => 'community/local',
73                                 'sel' => $content == 'local' ? 'active' : '',
74                                 'title' => t('Posts from local users on this server'),
75                                 'id' => 'community-local-tab',
76                                 'accesskey' => 'l'
77                         );
78                 }
79
80                 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
81                         $tabs[] = array(
82                                 'label' => t('Global Timeline'),
83                                 'url' => 'community/global',
84                                 'sel' => $content == 'global' ? 'active' : '',
85                                 'title' => t('Posts from users of the federated network'),
86                                 'id' => 'community-global-tab',
87                                 'accesskey' => 'g'
88                         );
89                 }
90
91                 $tab_tpl = get_markup_template('common_tabs.tpl');
92                 $o .= replace_macros($tab_tpl, array('$tabs' => $tabs));
93
94                 nav_set_selected('community');
95
96                 // We need the editor here to be able to reshare an item.
97                 if (local_user()) {
98                         $x = array(
99                                 'is_owner' => true,
100                                 'allow_location' => $a->user['allow_location'],
101                                 'default_location' => $a->user['default-location'],
102                                 'nickname' => $a->user['nickname'],
103                                 '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'),
104                                 'acl' => populate_acl($a->user, true),
105                                 'bang' => '',
106                                 'visitor' => 'block',
107                                 'profile_uid' => local_user(),
108                         );
109                         $o .= status_editor($a, $x, 0, true);
110                 }
111         }
112
113         if (Config::get('system', 'comment_public')) {
114                 // check if we serve a mobile device and get the user settings
115                 // accordingly
116                 if ($a->is_mobile) {
117                         $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 20);
118                 } else {
119                         $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 40);
120                 }
121
122                 // now that we have the user settings, see if the theme forces
123                 // a maximum item number which is lower then the user choice
124                 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
125                         $itemspage_network = $a->force_max_items;
126                 }
127
128                 $a->set_pager_itemspage($itemspage_network);
129         }
130
131         $r = community_getitems($a->pager['start'], $a->pager['itemspage'], $content);
132
133         if (!DBM::is_result($r)) {
134                 info(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 = array();
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, array(
175                 '$content' => $o,
176                 '$header' => '',
177                 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
178                 '$global_community_hint' => 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_fieldlists() . " 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                         AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
189                         AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''" .
190                         item_joins() . " AND `contact`.`self`
191                         WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
192                         AND NOT `thread`.`private` AND `thread`.`wall`
193                         ORDER BY `thread`.`received` DESC LIMIT " . intval($start) . ", " . intval($itemspage)
194                 );
195                 return dba::inArray($r);
196         } elseif ($content == 'global') {
197                 $r = dba::p("SELECT " . item_fieldlists() . " FROM `thread`
198                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid` " . item_joins() .
199                                 "WHERE `thread`.`uid` = 0 AND `verb` = ?
200                         ORDER BY `thread`.`created` DESC LIMIT " . intval($start) . ", " . intval($itemspage),
201                         ACTIVITY_POST
202                 );
203                 return dba::inArray($r);
204         }
205
206         // Should never happen
207         return array();
208 }