aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Module/Pubstream.php
blob: 15e2d8a746125b127620e7ba696643e5033c9a1b (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
<?php
namespace Zotlabs\Module;

require_once('include/conversation.php');


class Pubstream extends \Zotlabs\Web\Controller {

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

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

	
		if(observer_prohibited(true)) {
				return login();
		}
	
		$disable_discover_tab = get_config('system','disable_discover_tab') || get_config('system','disable_discover_tab') === false;
		if($disable_discover_tab)
			return;

		$mid = ((x($_REQUEST,'mid')) ? $_REQUEST['mid'] : '');

		if(strpos($mid,'b64.') === 0)
			$decoded = @base64url_decode(substr($mid,4));
		if($decoded)
			$mid = $decoded;

		$item_normal = item_normal();
		$item_normal_update = item_normal_update();

		$static = ((array_key_exists('static',$_REQUEST)) ? intval($_REQUEST['static']) : 0);

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

			nav_set_selected(t('Public Stream'));

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

			$static  = ((local_channel()) ? channel_manual_conv_update(local_channel()) : 1);
	
			$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 
			if($decoded)
				$mid = 'b64.' . base64url_encode($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'    => '0',
				'$cmax'    => '99',
				'$star'    => '0',
				'$liked'   => '0',
				'$conv'    => '0',
				'$spam'    => '0',
				'$fh'      => '1',
				'$nouveau' => '0',
				'$wall'    => '0',
				'$list'    => '0',
				'$static'  => $static,
				'$page'    => ((\App::$pager['page'] != 1) ? \App::$pager['page'] : 1),
				'$search'  => '',
				'$xchan'   => '',
				'$order'   => 'comment',
				'$file'    => '',
				'$cats'    => '',
				'$tags'    => '',
				'$dend'    => '',
				'$mid'     => $mid,
				'$verb'     => '',
				'$dbegin'  => ''
			));
		}
	
		if($update && ! $load) {
			// only setup pagination on initial page view
			$pager_sql = '';
		}
		else {
			\App::set_pager_itemspage(20);
			$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');
	
		if(get_config('system','site_firehose')) {
			$uids = " and item.uid in ( " . stream_perms_api_uids(PERMS_PUBLIC) . " ) and item_private = 0  and item_wall = 1 ";
		}
		else {
			$sys = get_sys_channel();
			$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';
	
	
		$simple_update = (($update) ? " and item.item_unseen = 1 " : '');
	
		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']) . "' ) ";
		if($load)
			$simple_update = '';

		if($static && $simple_update)
			$simple_update .= " and item_thread_top = 0 and author_xchan = '" . protect_sprintf(get_observer_hash()) . "' ";

		//logger('update: ' . $update . ' load: ' . $load);
	
		if($update) {
	
			$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
						WHERE mid like '%s' $uids $item_normal
						and (abook.abook_blocked = 0 or abook.abook_flags is null)
						$sql_extra3 $sql_extra $sql_nets LIMIT 1",
						dbesc($mid . '%')
					);
				}
				else {
					// Fetch a page full of parent items for this page
					$r = q("SELECT distinct item.id AS item_id, $ordering FROM item
						left join abook on item.author_xchan = abook.abook_xchan
						WHERE true $uids $item_normal
						AND item.parent = item.id
						and (abook.abook_blocked = 0 or abook.abook_flags is null)
						$sql_extra3 $sql_extra $sql_nets
						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
						WHERE mid like '%s' $uids $item_normal_update $simple_update
						and (abook.abook_blocked = 0 or abook.abook_flags is null)
						$sql_extra3 $sql_extra $sql_nets LIMIT 1",
						dbesc($mid . '%')
					);
				}
				else {
					$r = q("SELECT distinct item.id AS item_id, $ordering FROM item
						left join abook on item.author_xchan = abook.abook_xchan
						WHERE true $uids $item_normal_update
						AND item.parent = item.id $simple_update
						and (abook.abook_blocked = 0 or abook.abook_flags is null)
						$sql_extra3 $sql_extra $sql_nets"
					);
				}
				$_SESSION['loadtime'] = datetime_convert();
			}
			// Then fetch all the children of the parents that are on this page
			$parents_str = '';
			$update_unseen = '';
	
			if($r) {
	
				$parents_str = ids_to_querystr($r,'item_id');
	
				$items = q("SELECT item.*, item.id AS item_id FROM item
					WHERE true $uids $item_normal
					AND item.parent IN ( %s )
					$sql_extra ",
					dbesc($parents_str)
				);
	
				xchan_query($items,true,(-1));
				$items = fetch_post_tags($items,true);
				$items = conv_sort($items,$ordering);
			}
			else {
				$items = array();
			}
	
		}
	
		// fake it
		$mode = ('network');
	
		$o .= conversation($items,$mode,$update,$page_mode);

		if($mid)
			$o .= '<div id="content-complete"></div>';
	
		if(($items) && (! $update))
			$o .= alt_pager($a,count($items));

		return $o;
	
	}
}