3 * @file mod/community.php
7 use Friendica\Content\Nav;
8 use Friendica\Content\Pager;
9 use Friendica\Core\ACL;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\PConfig;
13 use Friendica\Core\Renderer;
14 use Friendica\Database\DBA;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Item;
18 function community_init(App $a)
21 unset($_SESSION['theme']);
22 unset($_SESSION['mobile-theme']);
26 function community_content(App $a, $update = 0)
30 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
31 notice(L10n::t('Public access denied.') . EOL);
35 $page_style = Config::get('system', 'community_page_style');
37 if ($page_style == CP_NO_INTERNAL_COMMUNITY) {
38 notice(L10n::t('Access denied.') . EOL);
45 switch ($a->argv[2]) {
47 $accounttype = Contact::ACCOUNT_TYPE_PERSON;
50 $accounttype = Contact::ACCOUNT_TYPE_ORGANISATION;
53 $accounttype = Contact::ACCOUNT_TYPE_NEWS;
56 $accounttype = Contact::ACCOUNT_TYPE_COMMUNITY;
62 $content = $a->argv[1];
64 if (!empty(Config::get('system', 'singleuser'))) {
65 // On single user systems only the global page does make sense
68 // When only the global community is allowed, we use this as default
69 $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
73 if (!in_array($content, ['local', 'global'])) {
74 notice(L10n::t('Community option not available.') . EOL);
78 // Check if we are allowed to display the content to visitors
80 $available = $page_style == CP_USERS_AND_GLOBAL;
83 $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
87 $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
91 notice(L10n::t('Not available.') . EOL);
96 require_once 'include/conversation.php';
101 if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(Config::get('system', 'singleuser'))) {
103 'label' => L10n::t('Local Community'),
104 'url' => 'community/local',
105 'sel' => $content == 'local' ? 'active' : '',
106 'title' => L10n::t('Posts from local users on this server'),
107 'id' => 'community-local-tab',
112 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
114 'label' => L10n::t('Global Community'),
115 'url' => 'community/global',
116 'sel' => $content == 'global' ? 'active' : '',
117 'title' => L10n::t('Posts from users of the whole federated network'),
118 'id' => 'community-global-tab',
123 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
124 $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
126 Nav::setSelected('community');
128 // We need the editor here to be able to reshare an item.
132 'allow_location' => $a->user['allow_location'],
133 'default_location' => $a->user['default-location'],
134 'nickname' => $a->user['nickname'],
135 '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'),
136 'acl' => ACL::getFullSelectorHTML($a->user, true),
138 'visitor' => 'block',
139 'profile_uid' => local_user(),
141 $o .= status_editor($a, $x, 0, true);
145 // check if we serve a mobile device and get the user settings accordingly
147 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 20);
149 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 40);
152 // now that we have the user settings, see if the theme forces
153 // a maximum item number which is lower then the user choice
154 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
155 $itemspage_network = $a->force_max_items;
158 $pager = new Pager($a->query_string, $itemspage_network);
160 $r = community_getitems($pager->getStart(), $pager->getItemsPerPage(), $content, $accounttype);
162 if (!DBA::isResult($r)) {
163 info(L10n::t('No results.') . EOL);
167 $maxpostperauthor = (int) Config::get('system', 'max_author_posts_community_page');
169 if (($maxpostperauthor != 0) && ($content == 'local')) {
171 $previousauthor = "";
176 foreach ($r as $item) {
177 if ($previousauthor == $item["author-link"]) {
182 $previousauthor = $item["author-link"];
184 if (($numposts < $maxpostperauthor) && (count($s) < $pager->getItemsPerPage())) {
188 if (count($s) < $pager->getItemsPerPage()) {
189 $r = community_getitems($pager->getStart() + ($count * $pager->getItemsPerPage()), $pager->getItemsPerPage(), $content, $accounttype);
191 } while ((count($s) < $pager->getItemsPerPage()) && ( ++$count < 50) && (count($r) > 0));
196 $o .= conversation($a, $s, $pager, 'community', $update, false, 'commented', local_user());
199 $o .= $pager->renderMinimal(count($r));
202 $t = Renderer::getMarkupTemplate("community.tpl");
203 return Renderer::replaceMacros($t, [
206 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
207 '$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.")
211 function community_getitems($start, $itemspage, $content, $accounttype)
213 if ($content == 'local') {
214 if (!is_null($accounttype)) {
215 $sql_accounttype = " AND `user`.`account-type` = ?";
216 $values = [$accounttype, $start, $itemspage];
218 $sql_accounttype = "";
219 $values = [$start, $itemspage];
222 $r = DBA::p("SELECT `item`.`uri`, `author`.`url` AS `author-link` FROM `thread`
223 INNER JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
224 INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
225 INNER 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 `owner`.`contact-type` = ?", 0, $accounttype];
234 $condition = ['uid' => 0];
237 $r = Item::selectThreadForUser(0, ['uri'], $condition, ['order' => ['commented' => true], 'limit' => [$start, $itemspage]]);
238 return DBA::toArray($r);
241 // Should never happen