]> git.mxchange.org Git - friendica.git/blob - mod/community.php
Update functions and calls
[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\PConfig;
9 use Friendica\Database\DBM;
10
11 function community_init(App $a)
12 {
13         if (!local_user()) {
14                 unset($_SESSION['theme']);
15                 unset($_SESSION['mobile-theme']);
16         }
17 }
18
19 function community_content(App $a, $update = 0)
20 {
21         $o = '';
22
23         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
24                 notice(t('Public access denied.') . EOL);
25                 return;
26         }
27
28         $page_style = Config::get('system', 'community_page_style');
29
30         if ($a->argc > 1) {
31                 $content = $a->argv[1];
32         } else {
33                 if (!empty(Config::get('system','singleuser'))) {
34                         // On single user systems only the global page does make sense
35                         $content = 'global';
36                 } else {
37                         // When only the global community is allowed, we use this as default
38                         $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
39                 }
40         }
41
42         if (!in_array($content, ['local', 'global'])) {
43                 notice(t('Community option not available.') . EOL);
44                 return;
45         }
46
47         // Check if we are allowed to display the content to visitors
48         if (!local_user()) {
49                 $available = $page_style == CP_USERS_AND_GLOBAL;
50
51                 if (!$available) {
52                         $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
53                 }
54
55                 if (!$available) {
56                         $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
57                 }
58
59                 if (!$available) {
60                         notice(t('Not available.') . EOL);
61                         return;
62                 }
63         }
64
65         require_once 'include/bbcode.php';
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' => t('Community'),
75                                 'url' => 'community/local',
76                                 'sel' => $content == 'local' ? 'active' : '',
77                                 'title' => 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' => t('Global Timeline'),
86                                 'url' => 'community/global',
87                                 'sel' => $content == 'global' ? 'active' : '',
88                                 'title' => t('Posts from users of the 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         if (Config::get('system', 'comment_public')) {
117                 // check if we serve a mobile device and get the user settings
118                 // 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
134         $r = community_getitems($a->pager['start'], $a->pager['itemspage'], $content);
135
136         if (!DBM::is_result($r)) {
137                 info(t('No results.') . EOL);
138                 return $o;
139         }
140
141         $maxpostperauthor = (int) Config::get('system', 'max_author_posts_community_page');
142
143         if (($maxpostperauthor != 0) && ($content == 'local')) {
144                 $count = 1;
145                 $previousauthor = "";
146                 $numposts = 0;
147                 $s = [];
148
149                 do {
150                         foreach ($r as $item) {
151                                 if ($previousauthor == $item["author-link"]) {
152                                         ++$numposts;
153                                 } else {
154                                         $numposts = 0;
155                                 }
156                                 $previousauthor = $item["author-link"];
157
158                                 if (($numposts < $maxpostperauthor) && (count($s) < $a->pager['itemspage'])) {
159                                         $s[] = $item;
160                                 }
161                         }
162                         if (count($s) < $a->pager['itemspage']) {
163                                 $r = community_getitems($a->pager['start'] + ($count * $a->pager['itemspage']), $a->pager['itemspage'], $content);
164                         }
165                 } while ((count($s) < $a->pager['itemspage']) && ( ++$count < 50) && (count($r) > 0));
166         } else {
167                 $s = $r;
168         }
169
170         $o .= conversation($a, $s, 'community', $update);
171
172         if (!$update) {
173                 $o .= alt_pager($a, count($r));
174         }
175
176         $t = get_markup_template("community.tpl");
177         return replace_macros($t, [
178                 '$content' => $o,
179                 '$header' => '',
180                 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
181                 '$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.")
182         ]);
183 }
184
185 function community_getitems($start, $itemspage, $content)
186 {
187         if ($content == 'local') {
188                 $r = dba::p("SELECT " . item_fieldlists() . " FROM `thread`
189                         INNER JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
190                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
191                         AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
192                         AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''" .
193                         item_joins() . " AND `contact`.`self`
194                         WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
195                         AND NOT `thread`.`private` AND `thread`.`wall`
196                         ORDER BY `thread`.`commented` DESC LIMIT " . intval($start) . ", " . intval($itemspage)
197                 );
198                 return dba::inArray($r);
199         } elseif ($content == 'global') {
200                 $r = dba::p("SELECT " . item_fieldlists() . " FROM `thread`
201                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid` " . item_joins() .
202                                 "WHERE `thread`.`uid` = 0 AND `verb` = ?
203                         ORDER BY `thread`.`commented` DESC LIMIT " . intval($start) . ", " . intval($itemspage),
204                         ACTIVITY_POST
205                 );
206                 return dba::inArray($r);
207         }
208
209         // Should never happen
210         return [];
211 }