]> git.mxchange.org Git - friendica.git/blob - mod/community.php
Disable editing of public items / Reshare of community items is now possible
[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                 $tabs = [];
61
62                 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) {
63                         $tabs[] = array('label'=>t('Community'),
64                                         'url' => 'community/local',
65                                         'sel' => $content == 'local' ? 'active' : '',
66                                         'title' => t('Posts from local users on this server'),
67                                         'id' => 'community-local-tab',
68                                         'accesskey' => 'l');
69                 }
70
71                 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
72                         $tabs[] = array('label' => t('Global Timeline'),
73                                         'url' => 'community/global',
74                                         'sel' => $content == 'global' ? 'active' : '',
75                                         'title' => t('Posts from users of the federated network'),
76                                         'id'    => 'community-global-tab',
77                                         'accesskey' => 'g');
78                 }
79
80                 $tab_tpl = get_markup_template('common_tabs.tpl');
81                 $o .= replace_macros($tab_tpl, array('$tabs' => $tabs));
82
83                 nav_set_selected('community');
84
85                 // We need the editor here to be able to reshare an item.
86                 if (local_user()) {
87                         $x = array(
88                                 'is_owner' => true,
89                                 'allow_location' => $a->user['allow_location'],
90                                 'default_location' => $a->user['default-location'],
91                                 'nickname' => $a->user['nickname'],
92                                 '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'),
93                                 'acl' => populate_acl($a->user, true),
94                                 'bang' => '',
95                                 'visitor' => 'block',
96                                 'profile_uid' => local_user(),
97                         );
98                         $o .= status_editor($a, $x, 0, true);
99                 }
100         }
101
102         if (Config::get('system', 'comment_public')) {
103                 // check if we serve a mobile device and get the user settings
104                 // accordingly
105                 if ($a->is_mobile) {
106                         $itemspage_network = PConfig::get(local_user(),'system','itemspage_mobile_network', 20);
107                 } else {
108                         $itemspage_network = PConfig::get(local_user(),'system','itemspage_network', 40);
109                 }
110
111                 // now that we have the user settings, see if the theme forces
112                 // a maximum item number which is lower then the user choice
113                 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
114                         $itemspage_network = $a->force_max_items;
115                 }
116
117                 $a->set_pager_itemspage($itemspage_network);
118         }
119
120         $r = community_getitems($a->pager['start'], $a->pager['itemspage'], $content);
121
122         if (!DBM::is_result($r)) {
123                 info(t('No results.') . EOL);
124                 return $o;
125         }
126
127         $maxpostperauthor = Config::get('system','max_author_posts_community_page');
128
129         if (($maxpostperauthor != 0) && ($content == 'local')) {
130                 $count = 1;
131                 $previousauthor = "";
132                 $numposts = 0;
133                 $s = array();
134
135                 do {
136                         foreach ($r AS $row=>$item) {
137                                 if ($previousauthor == $item["author-link"]) {
138                                         ++$numposts;
139                                 } else {
140                                         $numposts = 0;
141                                 }
142                                 $previousauthor = $item["author-link"];
143
144                                 if (($numposts < $maxpostperauthor) && (sizeof($s) < $a->pager['itemspage'])) {
145                                         $s[] = $item;
146                                 }
147                         }
148                         if (sizeof($s) < $a->pager['itemspage']) {
149                                 $r = community_getitems($a->pager['start'] + ($count * $a->pager['itemspage']), $a->pager['itemspage'], $content);
150                         }
151                 } while ((sizeof($s) < $a->pager['itemspage']) && (++$count < 50) && (sizeof($r) > 0));
152         } else {
153                 $s = $r;
154         }
155
156         $o .= conversation($a, $s, 'community', $update);
157
158         if (!$update) {
159                 $o .= alt_pager($a, count($r));
160         }
161
162         $t = get_markup_template("community.tpl");
163         return replace_macros($t, array(
164                 '$content' => $o,
165                 '$header' => '',
166                 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
167                 '$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.")
168         ));
169 }
170
171 function community_getitems($start, $itemspage, $content) {
172         if ($content == 'local') {
173                 $r = dba::p("SELECT ".item_fieldlists()." FROM `thread`
174                         INNER JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
175                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
176                         AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
177                         AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''".
178                         item_joins()." AND `contact`.`self`
179                         WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
180                         AND NOT `thread`.`private` AND `thread`.`wall`
181                         ORDER BY `thread`.`received` DESC LIMIT ".intval($start).", ".intval($itemspage)
182                 );
183                 return dba::inArray($r);
184         } elseif ($content == 'global') {
185                 $r = dba::p("SELECT ".item_fieldlists()." FROM `thread`
186                         INNER JOIN `item` ON `item`.`id` = `thread`.`iid` ".item_joins().
187                         "WHERE `thread`.`uid` = 0 AND `verb` = ?
188                         ORDER BY `thread`.`created` DESC LIMIT ".intval($start).", ".intval($itemspage),
189                         ACTIVITY_POST
190                 );
191                 return dba::inArray($r);
192         }
193
194         // Should never happen
195         return array();
196 }