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