]> git.mxchange.org Git - friendica.git/blob - mod/community.php
It's now in a single file
[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         if (!local_user()) {
10                 unset($_SESSION['theme']);
11                 unset($_SESSION['mobile-theme']);
12         }
13 }
14
15 function community_content(App $a, $update = 0) {
16         $o = '';
17
18         if (Config::get('system','block_public') && !local_user() && !remote_user()) {
19                 notice(t('Public access denied.') . EOL);
20                 return;
21         }
22
23         $page_style = Config::get('system','community_page_style');
24
25         if ($a->argc > 1) {
26                 $content = $a->argv[1];
27         } else {
28                 // When only the global community is allowed, we use this as default
29                 $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
30         }
31
32         if (!in_array($content, ['local', 'global'])) {
33                 notice(t('Community option not available.') . EOL);
34                 return;
35         }
36
37         // Check if we are allowed to display the content to visitors
38         if (!local_user()) {
39                 $available = $page_style == CP_USERS_AND_GLOBAL;
40
41                 if (!$available) {
42                         $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
43                 }
44
45                 if (!$available) {
46                         $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
47                 }
48
49                 if (!$available) {
50                         notice(t('Not available.') . EOL);
51                         return;
52                 }
53         }
54
55         require_once 'include/bbcode.php';
56         require_once 'include/security.php';
57         require_once 'include/conversation.php';
58
59         if (!$update) {
60                 nav_set_selected('community');
61         }
62
63         if (Config::get('system', 'comment_public')) {
64                 // check if we serve a mobile device and get the user settings
65                 // accordingly
66                 if ($a->is_mobile) {
67                         $itemspage_network = PConfig::get(local_user(),'system','itemspage_mobile_network', 20);
68                 } else {
69                         $itemspage_network = PConfig::get(local_user(),'system','itemspage_network', 40);
70                 }
71
72                 // now that we have the user settings, see if the theme forces
73                 // a maximum item number which is lower then the user choice
74                 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
75                         $itemspage_network = $a->force_max_items;
76                 }
77
78                 $a->set_pager_itemspage($itemspage_network);
79         }
80
81         $r = community_getitems($a->pager['start'], $a->pager['itemspage'], $content);
82
83         if (!DBM::is_result($r)) {
84                 info(t('No results.') . EOL);
85                 return $o;
86         }
87
88         $maxpostperauthor = Config::get('system','max_author_posts_community_page');
89
90         if (($maxpostperauthor != 0) && ($content == 'local')) {
91                 $count = 1;
92                 $previousauthor = "";
93                 $numposts = 0;
94                 $s = array();
95
96                 do {
97                         foreach ($r AS $row=>$item) {
98                                 if ($previousauthor == $item["author-link"]) {
99                                         ++$numposts;
100                                 } else {
101                                         $numposts = 0;
102                                 }
103                                 $previousauthor = $item["author-link"];
104
105                                 if (($numposts < $maxpostperauthor) && (sizeof($s) < $a->pager['itemspage'])) {
106                                         $s[] = $item;
107                                 }
108                         }
109                         if (sizeof($s) < $a->pager['itemspage']) {
110                                 $r = community_getitems($a->pager['start'] + ($count * $a->pager['itemspage']), $a->pager['itemspage'], $content);
111                         }
112                 } while ((sizeof($s) < $a->pager['itemspage']) && (++$count < 50) && (sizeof($r) > 0));
113         } else {
114                 $s = $r;
115         }
116
117         $o .= conversation($a, $s, 'community', $update);
118
119         $o .= alt_pager($a, count($r));
120
121         $t = get_markup_template("community.tpl");
122         return replace_macros($t, array(
123                 '$content' => $o,
124                 '$header' => $content == 'global' ? t("Global Timeline") : t("Community"),
125                 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
126                 '$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.")
127         ));
128 }
129
130 function community_getitems($start, $itemspage, $content) {
131         if ($content == 'local') {
132                 $r = dba::p("SELECT ".item_fieldlists()." FROM `thread`
133                         INNER JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
134                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
135                         AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
136                         AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''".
137                         item_joins()." AND `contact`.`self`
138                         WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
139                         AND NOT `thread`.`private` AND `thread`.`wall`
140                         ORDER BY `thread`.`received` DESC LIMIT ".intval($start).", ".intval($itemspage)
141                 );
142                 return dba::inArray($r);
143         } elseif ($content == 'global') {
144                 $r = dba::p("SELECT ".item_fieldlists()." FROM `thread`
145                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid` ".item_joins().
146                         "WHERE `thread`.`uid` = 0 AND `verb` = ?
147                         ORDER BY `thread`.`created` DESC LIMIT ".intval($start).", ".intval($itemspage),
148                         ACTIVITY_POST
149                 );
150                 return dba::inArray($r);
151         }
152
153         // Should never happen
154         return array();
155 }