aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Module/Pubstream.php
blob: 3c8dfa0a500dfb510949679e6f2c9699c1c68daf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
namespace Zotlabs\Module;

use App;
use Zotlabs\Lib\Apps;

require_once('include/conversation.php');
require_once('include/acl_selectors.php');


class Pubstream extends \Zotlabs\Web\Controller {

	function get($update = 0, $load = false) {

		if(local_channel()) {
			if(! Apps::system_app_installed(local_channel(), 'Public Stream')) {
				//Do not display any associated widgets at this point
				App::$pdl = '';
				$papp = Apps::get_papp('Public Stream');
				return Apps::app_render($papp, 'module');
			}
		}

		if((observer_prohibited(true))) {
			return login();
		}

		if(! intval(get_config('system','open_pubstream',1))) {
			if(! get_observer_hash()) {
				return login();
			}
		}

		$site_firehose = ((intval(get_config('system','site_firehose',0))) ? true : false);
		$net_firehose  = ((get_config('system','disable_discover_tab',1)) ? false : true);

		if(! ($site_firehose || $net_firehose)) {
			return '';
		}

		if($net_firehose) {
			$site_firehose = false;
		}

		$mid = ((x($_REQUEST, 'mid')) ? unpack_link_id($_REQUEST['mid']) : '');
		if ($mid === false) {
			notice(t('Malformed message id.') . EOL);
			return;
		}

		$hashtags = ((x($_REQUEST,'tag')) ? $_REQUEST['tag'] : '');
		$item_normal = item_normal();
		$item_normal_update = item_normal_update();
		$net = ((array_key_exists('net',$_REQUEST))    ? escape_tags($_REQUEST['net']) : '');

		$title = replace_macros(get_markup_template("section_title.tpl"),array(
			'$title' => (($hashtags) ? '#' . htmlspecialchars($hashtags, ENT_COMPAT,'UTF-8') : '')
		));

		$o = (($hashtags) ? $title : '');

		if(local_channel() && (! $update)) {

			$channel = \App::get_channel();

			$channel_acl = array(
				'allow_cid' => $channel['channel_allow_cid'],
				'allow_gid' => $channel['channel_allow_gid'],
				'deny_cid'  => $channel['channel_deny_cid'],
				'deny_gid'  => $channel['channel_deny_gid']
			);

			$x = array(
				'is_owner'            => true,
				'allow_location'      => ((intval(get_pconfig($channel['channel_id'],'system','use_browser_location'))) ? '1' : ''),
				'default_location'    => $channel['channel_location'],
				'nickname'            => $channel['channel_address'],
				'lockstate'           => (($channel['channel_allow_cid'] || $channel['channel_allow_gid'] || $channel['channel_deny_cid'] || $channel['channel_deny_gid']) ? 'lock' : 'unlock'),
				'acl'                 => populate_acl($channel_acl,true, \Zotlabs\Lib\PermissionDescription::fromGlobalPermission('view_stream'), get_post_aclDialogDescription(), 'acl_dialog_post'),
				'permissions'         => $channel_acl,
				'bang'                => '',
				'visitor'             => true,
				'profile_uid'         => local_channel(),
				'return_path'         => 'channel/' . $channel['channel_address'],
				'expanded'            => true,
				'editor_autocomplete' => true,
				'bbco_autocomplete'   => 'bbcode',
				'bbcode'              => true,
				'jotnets'             => true,
				'reset'               => t('Reset form')
			);

			$o .= '<div id="jot-popup">';
			$a = '';
			$o .= status_editor($a,$x,false,'Pubstream');
			$o .= '</div>';
		}

		if(! $update && !$load) {

			nav_set_selected(t('Public Stream'));

			if(!$mid)
				$_SESSION['static_loadtime'] = datetime_convert();

			$maxheight = get_config('system','home_divmore_height');
			if(! $maxheight)
				$maxheight = 400;

			$o .= '<div id="live-pubstream"></div>' . "\r\n";
			$o .= "<script> var profile_uid = " . ((intval(local_channel())) ? local_channel() : (-1))
				. "; var profile_page = " . \App::$pager['page']
				. "; divmore_height = " . intval($maxheight) . "; </script>\r\n";

			//if we got a decoded hash we must encode it again before handing to javascript
			$mid = gen_link_id($mid);

			\App::$page['htmlhead'] .= replace_macros(get_markup_template("build_query.tpl"),array(
				'$baseurl' => z_root(),
				'$pgtype'  => 'pubstream',
				'$uid'     => ((local_channel()) ? local_channel() : '0'),
				'$gid'     => '0',
				'$cid'     => '0',
				'$cmin'    => '(-1)',
				'$cmax'    => '(-1)',
				'$star'    => '0',
				'$liked'   => '0',
				'$conv'    => '0',
				'$spam'    => '0',
				'$fh'      => '1',
				'$dm'      => '0',
				'$nouveau' => '0',
				'$wall'    => '0',
				'$list'    => '0',
				'$page'    => ((\App::$pager['page'] != 1) ? \App::$pager['page'] : 1),
				'$search'  => '',
				'$xchan'   => '',
				'$order'   => 'comment',
				'$file'    => '',
				'$cats'    => '',
				'$tags'    => (($hashtags) ? urlencode($hashtags) : ''),
				'$dend'    => '',
				'$mid'     => (($mid) ? urlencode($mid) : ''),
				'$verb'    => '',
				'$net'     => (($net) ? urlencode($net) : ''),
				'$dbegin'  => ''
			));
		}

		if($update && ! $load) {
			// only setup pagination on initial page view
			$pager_sql = '';
		}
		else {
			\App::set_pager_itemspage(10);
			$pager_sql = sprintf(" LIMIT %d OFFSET %d ", intval(\App::$pager['itemspage']), intval(\App::$pager['start']));
		}

		require_once('include/channel.php');
		require_once('include/security.php');

		$sys = get_sys_channel();
		$abook_uids = " and abook.abook_channel = " . intval($sys['channel_id']) . " ";

		if($site_firehose) {
			$uids = " and item.uid in ( " . stream_perms_api_uids(PERMS_PUBLIC) . " ) and item_private = 0  and item_wall = 1 ";
		}
		else {
			$uids = " and item.uid  = " . intval($sys['channel_id']) . " ";
			$sql_extra = item_permissions_sql($sys['channel_id']);
			\App::$data['firehose'] = intval($sys['channel_id']);
		}

		if(get_config('system','public_list_mode'))
			$page_mode = 'list';
		else
			$page_mode = 'client';


		if(x($hashtags)) {
			$sql_extra .= protect_sprintf(term_query('item', $hashtags, TERM_HASHTAG, TERM_COMMUNITYTAG));
		}

		$net_query = (($net) ? " left join xchan on xchan_hash = author_xchan " : '');
		$net_query2 = (($net) ? " and xchan_network = '" . protect_sprintf(dbesc($net)) . "' " : '');

		if($update && $_SESSION['loadtime'])
			$simple_update = " AND (( item_unseen = 1 AND item.changed > '" . datetime_convert('UTC','UTC',$_SESSION['loadtime']) . "' )  OR item.changed > '" . datetime_convert('UTC','UTC',$_SESSION['loadtime']) . "' ) ";

		//logger('update: ' . $update . ' load: ' . $load);

		$items = [];

		if($update) {

			$ordering = get_config('system', 'pubstream_ordering', 'commented');

			if($load) {
				if($mid) {
					$r = q("SELECT parent AS item_id FROM item
						left join abook on item.author_xchan = abook.abook_xchan
						$net_query
						WHERE mid = '%s' $uids $item_normal
						and (abook.abook_blocked = 0 or abook.abook_flags is null)
						$sql_extra $net_query2",
						dbesc($mid)
					);
				}
				else {
					// Fetch a page full of parent items for this page
					$r = dbq("SELECT item.id AS item_id FROM item
						left join abook on ( item.author_xchan = abook.abook_xchan $abook_uids )
						$net_query
						WHERE true $uids and item.item_thread_top = 1 $item_normal
						and (abook.abook_blocked = 0 or abook.abook_flags is null)
						$sql_extra $net_query2
						ORDER BY $ordering DESC $pager_sql "
					);
				}
			}
			elseif($update) {
				if($mid) {
					$r = q("SELECT parent AS item_id FROM item
						left join abook on item.author_xchan = abook.abook_xchan
						$net_query
						WHERE mid = '%s' $uids $item_normal_update $simple_update
						and (abook.abook_blocked = 0 or abook.abook_flags is null)
						$sql_extra $net_query2",
						dbesc($mid)
					);
				}
				else {
					$r = dbq("SELECT parent AS item_id FROM item
						left join abook on item.author_xchan = abook.abook_xchan
						$net_query
						WHERE true $uids $item_normal_update
						$simple_update
						and (abook.abook_blocked = 0 or abook.abook_flags is null)
						$sql_extra $net_query2"
					);
				}
			}

			// Then fetch all the children of the parents that are on this page
			$parents_str = '';

			if($r) {

				$parents_str = ids_to_querystr($r,'item_id');

				$items = dbq("SELECT item.*, item.id AS item_id FROM item
					WHERE true $uids $item_normal
					AND item.parent IN ( $parents_str )
					$sql_extra"
				);

				// use effective_uid param of xchan_query to help sort out comment permission
				// for sys_channel owned items.

				xchan_query($items,true,(($sys) ? local_channel() : 0));
				$items = fetch_post_tags($items,true);
				$items = conv_sort($items,$ordering);
			}

		}

		// fake it
		$mode = (($hashtags) ? 'search' : 'pubstream');

		$o .= conversation($items,$mode,$update,$page_mode);

		if($mid)
			$o .= '<div id="content-complete"></div>';

		if(($items) && (! $update))
			$o .= alt_pager(count($items));

		$_SESSION['loadtime'] = datetime_convert();

		return $o;

	}
}