]> git.mxchange.org Git - friendica.git/blob - mod/community.php
Fix Scrutinizer issues in mod [_well_know -> contactgroup]
[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
92         if (Config::get('system', 'comment_public')) {
93                 // check if we serve a mobile device and get the user settings
94                 // accordingly
95                 if ($a->is_mobile) {
96                         $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 20);
97                 } else {
98                         $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 40);
99                 }
100
101                 // now that we have the user settings, see if the theme forces
102                 // a maximum item number which is lower then the user choice
103                 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
104                         $itemspage_network = $a->force_max_items;
105                 }
106
107                 $a->set_pager_itemspage($itemspage_network);
108         }
109
110         $r = community_getitems($a->pager['start'], $a->pager['itemspage'], $content);
111
112         if (!DBM::is_result($r)) {
113                 info(t('No results.') . EOL);
114                 return $o;
115         }
116
117         $maxpostperauthor = (int) Config::get('system', 'max_author_posts_community_page');
118
119         if (($maxpostperauthor != 0) && ($content == 'local')) {
120                 $count = 1;
121                 $previousauthor = "";
122                 $numposts = 0;
123                 $s = array();
124
125                 do {
126                         foreach ($r as $item) {
127                                 if ($previousauthor == $item["author-link"]) {
128                                         ++$numposts;
129                                 } else {
130                                         $numposts = 0;
131                                 }
132                                 $previousauthor = $item["author-link"];
133
134                                 if (($numposts < $maxpostperauthor) && (count($s) < $a->pager['itemspage'])) {
135                                         $s[] = $item;
136                                 }
137                         }
138                         if (count($s) < $a->pager['itemspage']) {
139                                 $r = community_getitems($a->pager['start'] + ($count * $a->pager['itemspage']), $a->pager['itemspage'], $content);
140                         }
141                 } while ((count($s) < $a->pager['itemspage']) && ( ++$count < 50) && (count($r) > 0));
142         } else {
143                 $s = $r;
144         }
145
146         $o .= conversation($a, $s, 'community', $update);
147
148         if (!$update) {
149                 $o .= alt_pager($a, count($r));
150         }
151
152         $t = get_markup_template("community.tpl");
153         return replace_macros($t, array(
154                 '$content' => $o,
155                 '$header' => '',
156                 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
157                 '$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.")
158         ));
159 }
160
161 function community_getitems($start, $itemspage, $content)
162 {
163         if ($content == 'local') {
164                 $r = dba::p("SELECT " . item_fieldlists() . " FROM `thread`
165                         INNER JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
166                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
167                         AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
168                         AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''" .
169                         item_joins() . " AND `contact`.`self`
170                         WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
171                         AND NOT `thread`.`private` AND `thread`.`wall`
172                         ORDER BY `thread`.`received` DESC LIMIT " . intval($start) . ", " . intval($itemspage)
173                 );
174                 return dba::inArray($r);
175         } elseif ($content == 'global') {
176                 $r = dba::p("SELECT " . item_fieldlists() . " FROM `thread`
177                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid` " . item_joins() .
178                                 "WHERE `thread`.`uid` = 0 AND `verb` = ?
179                         ORDER BY `thread`.`created` DESC LIMIT " . intval($start) . ", " . intval($itemspage),
180                         ACTIVITY_POST
181                 );
182                 return dba::inArray($r);
183         }
184
185         // Should never happen
186         return array();
187 }