]> git.mxchange.org Git - friendica.git/blob - mod/community.php
Move /profile_photo to Module\Settings\Profile\Photo
[friendica.git] / mod / community.php
1 <?php
2 /**
3  * @file mod/community.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Feature;
8 use Friendica\Content\Nav;
9 use Friendica\Content\Pager;
10 use Friendica\Content\Widget\TrendingTags;
11 use Friendica\Core\ACL;
12 use Friendica\Core\Renderer;
13 use Friendica\Core\Session;
14 use Friendica\Database\DBA;
15 use Friendica\DI;
16 use Friendica\Model\Item;
17 use Friendica\Model\User;
18
19 function community_content(App $a, $update = 0)
20 {
21         $o = '';
22
23         if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
24                 notice(DI::l10n()->t('Public access denied.') . EOL);
25                 return;
26         }
27
28         $page_style = DI::config()->get('system', 'community_page_style');
29
30         if ($page_style == CP_NO_INTERNAL_COMMUNITY) {
31                 notice(DI::l10n()->t('Access denied.') . EOL);
32                 return;
33         }
34
35         $accounttype = null;
36
37         if ($a->argc > 2) {
38                 switch ($a->argv[2]) {
39                         case 'person':
40                                 $accounttype = User::ACCOUNT_TYPE_PERSON;
41                                 break;
42                         case 'organisation':
43                                 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
44                                 break;
45                         case 'news':
46                                 $accounttype = User::ACCOUNT_TYPE_NEWS;
47                                 break;
48                         case 'community':
49                                 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
50                                 break;
51                 }
52         }
53
54         if ($a->argc > 1) {
55                 $content = $a->argv[1];
56         } else {
57                 if (!empty(DI::config()->get('system', 'singleuser'))) {
58                         // On single user systems only the global page does make sense
59                         $content = 'global';
60                 } else {
61                         // When only the global community is allowed, we use this as default
62                         $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
63                 }
64         }
65
66         if (!in_array($content, ['local', 'global'])) {
67                 notice(DI::l10n()->t('Community option not available.') . EOL);
68                 return;
69         }
70
71         // Check if we are allowed to display the content to visitors
72         if (!local_user()) {
73                 $available = $page_style == CP_USERS_AND_GLOBAL;
74
75                 if (!$available) {
76                         $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
77                 }
78
79                 if (!$available) {
80                         $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
81                 }
82
83                 if (!$available) {
84                         notice(DI::l10n()->t('Not available.') . EOL);
85                         return;
86                 }
87         }
88
89         if (!$update) {
90                 $tabs = [];
91
92                 if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(DI::config()->get('system', 'singleuser'))) {
93                         $tabs[] = [
94                                 'label' => DI::l10n()->t('Local Community'),
95                                 'url' => 'community/local',
96                                 'sel' => $content == 'local' ? 'active' : '',
97                                 'title' => DI::l10n()->t('Posts from local users on this server'),
98                                 'id' => 'community-local-tab',
99                                 'accesskey' => 'l'
100                         ];
101                 }
102
103                 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
104                         $tabs[] = [
105                                 'label' => DI::l10n()->t('Global Community'),
106                                 'url' => 'community/global',
107                                 'sel' => $content == 'global' ? 'active' : '',
108                                 'title' => DI::l10n()->t('Posts from users of the whole federated network'),
109                                 'id' => 'community-global-tab',
110                                 'accesskey' => 'g'
111                         ];
112                 }
113
114                 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
115                 $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
116
117                 Nav::setSelected('community');
118
119                 // We need the editor here to be able to reshare an item.
120                 if (local_user()) {
121                         $x = [
122                                 'is_owner' => true,
123                                 'allow_location' => $a->user['allow_location'],
124                                 'default_location' => $a->user['default-location'],
125                                 'nickname' => $a->user['nickname'],
126                                 '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'),
127                                 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
128                                 'bang' => '',
129                                 'visitor' => 'block',
130                                 'profile_uid' => local_user(),
131                         ];
132                         $o .= status_editor($a, $x, 0, true);
133                 }
134         }
135
136         // check if we serve a mobile device and get the user settings accordingly
137         if (DI::mode()->isMobile()) {
138                 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network', 20);
139         } else {
140                 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_network', 40);
141         }
142
143         // now that we have the user settings, see if the theme forces
144         // a maximum item number which is lower then the user choice
145         if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
146                 $itemspage_network = $a->force_max_items;
147         }
148
149         $pager = new Pager(DI::args()->getQueryString(), $itemspage_network);
150
151         $r = community_getitems($pager->getStart(), $pager->getItemsPerPage(), $content, $accounttype);
152
153         if (!DBA::isResult($r)) {
154                 info(DI::l10n()->t('No results.') . EOL);
155                 return $o;
156         }
157
158         $maxpostperauthor = (int) DI::config()->get('system', 'max_author_posts_community_page');
159
160         if (($maxpostperauthor != 0) && ($content == 'local')) {
161                 $count = 1;
162                 $previousauthor = "";
163                 $numposts = 0;
164                 $s = [];
165
166                 do {
167                         foreach ($r as $item) {
168                                 if ($previousauthor == $item["author-link"]) {
169                                         ++$numposts;
170                                 } else {
171                                         $numposts = 0;
172                                 }
173                                 $previousauthor = $item["author-link"];
174
175                                 if (($numposts < $maxpostperauthor) && (count($s) < $pager->getItemsPerPage())) {
176                                         $s[] = $item;
177                                 }
178                         }
179                         if (count($s) < $pager->getItemsPerPage()) {
180                                 $r = community_getitems($pager->getStart() + ($count * $pager->getItemsPerPage()), $pager->getItemsPerPage(), $content, $accounttype);
181                         }
182                 } while ((count($s) < $pager->getItemsPerPage()) && ( ++$count < 50) && (count($r) > 0));
183         } else {
184                 $s = $r;
185         }
186
187         $o .= conversation($a, $s, $pager, 'community', $update, false, 'commented', local_user());
188
189         if (!$update) {
190                 $o .= $pager->renderMinimal(count($r));
191         }
192
193         if (empty(DI::page()['aside'])) {
194                 DI::page()['aside'] = '';
195         }
196
197         if (Feature::isEnabled(local_user(), 'trending_tags')) {
198                 DI::page()['aside'] .= TrendingTags::getHTML($content);
199         }
200
201         $t = Renderer::getMarkupTemplate("community.tpl");
202         return Renderer::replaceMacros($t, [
203                 '$content' => $o,
204                 '$header' => '',
205                 '$show_global_community_hint' => ($content == 'global') && DI::config()->get('system', 'show_global_community_hint'),
206                 '$global_community_hint' => DI::l10n()->t("This community stream shows all public posts received by this node. They may not reflect the opinions of this node’s users.")
207         ]);
208 }
209
210 function community_getitems($start, $itemspage, $content, $accounttype)
211 {
212         if ($content == 'local') {
213                 if (!is_null($accounttype)) {
214                         $sql_accounttype = " AND `user`.`account-type` = ?";
215                         $values = [$accounttype, $start, $itemspage];
216                 } else {
217                         $sql_accounttype = "";
218                         $values = [$start, $itemspage];
219                 }
220
221                 /// @todo Use "unsearchable" here as well (instead of "hidewall")
222                 $r = DBA::p("SELECT `item`.`uri`, `author`.`url` AS `author-link` FROM `thread`
223                         STRAIGHT_JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
224                         STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
225                         STRAIGHT_JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
226                         WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
227                         AND NOT `thread`.`private` AND `thread`.`wall` AND `thread`.`origin` $sql_accounttype
228                         ORDER BY `thread`.`commented` DESC LIMIT ?, ?", $values);
229                 return DBA::toArray($r);
230         } elseif ($content == 'global') {
231                 if (!is_null($accounttype)) {
232                         $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable` AND `owner`.`contact-type` = ?", 0, $accounttype];
233                 } else {
234                         $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable`", 0];
235                 }
236
237                 $r = Item::selectThreadForUser(0, ['uri'], $condition, ['order' => ['commented' => true], 'limit' => [$start, $itemspage]]);
238                 return DBA::toArray($r);
239         }
240
241         // Should never happen
242         return [];
243 }