aboutsummaryrefslogtreecommitdiffstats
path: root/include/apps.php
blob: 2b590332984fcd4bbd1cb55a53f5cbc3d579aa34 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php /** @file */

/**
 * apps
 *
 */

require_once('include/plugin.php');

function get_system_apps() {

	$ret = array();
	$files = glob('app/*.apd');
	if($files) {
		foreach($files as $f) {
			$x = parse_app_description($f);
			if($x) {
				$ret[] = $x;
			}
		}
	}
	$files = glob('addon/*/*.apd');
	if($files) {
		foreach($files as $f) {
			$n = basename($f,'.apd');
			if(plugin_is_installed($n)) {
				$x = parse_app_description($f);
				if($x) {
					$ret[] = $x;
				}
			}
		}
	}

	return $ret;

}


function parse_app_description($f) {
	$ret = array();

	$baseurl = z_root();
	$channel = get_app()->get_channel();
	$address = (($channel) ? $channel['channel_address'] : '');
		
	//future expansion

	$observer = get_app()->get_observer();
	

	$lines = @file($f);
	if($lines) {
		foreach($lines as $x) {
			if(preg_match('/^([a-zA-Z].*?):(.*?)$/ism',$x,$matches)) {
				$ret[$matches[1]] = trim(str_replace(array('$baseurl','$nick'),array($baseurl,$address),$matches[2]));
			}
		}
	}



	if(! $ret['photo'])
		$ret['photo'] = $baseurl . '/' . get_default_profile_photo(80);


	foreach($ret as $k => $v) {
		if(strpos($v,'http') === 0)
			$ret[$k] = zid($v);
	}

	if(array_key_exists('hover',$ret))
		$ret['hover'] = str_replace(array('\'','"'),array('&#39;','&dquot;'),$ret['hover']);

	if(array_key_exists('requires',$ret)) {
		$require = trim(strtolower($ret['requires']));
		switch($require) {
			case 'nologin':
				if(local_user())
					unset($ret);
				break;
			case 'local_user':
				if(! local_user())
					unset($ret);
				break;
			case 'observer':
				if(! $observer)
					unset($ret);
				break;
			default:
				if(! local_user() && feature_enabled(local_user(),$require))
					unset($ret);
				break;

		}
//		logger('require: ' . print_r($ret,true));
	}
	if($ret) {
		translate_system_apps($ret);
		return $ret;
	}
	return false;
}	


function translate_system_apps(&$arr) {
	$apps = array( 'Matrix' => t('Matrix'), 
		'Channel Home' => t('Channel Home'), 
		'Profile' => t('Profile'),
		'Photos' => t('Photos'), 
		'Events' => t('Events'), 
		'Directory' => t('Directory'), 
		'Help' => t('Help')
	);

	if(array_key_exists($arr['name'],$apps))
		$arr['name'] = $apps[$arr['name']];

}


// papp is a portable app

function app_render($papp,$mode = 'view') {

	/**
	 * modes:
	 *    view: normal mode for viewing an app via bbcode from a conversation or page
	 *       provides install/update button if you're logged in locally
	 *    list: normal mode for viewing an app on the app page
	 *       no buttons are shown
	 *    edit: viewing the app page in editing mode provides a delete button
	 */

	$installed = false;

	if(! $papp['photo'])
		$papp['photo'] = z_root() . '/' . get_default_profile_photo(80);
	
	$papp['papp'] = papp_encode($papp);

	foreach($papp as $k => $v) {
		if(strpos($v,'http') === 0 && $k != 'papp')
			$papp[$k] = zid($v);
	}

	if(local_user()) {
		$installed = app_installed(local_user(),$papp);
	}

	$install_action = (($installed) ? t('Update') : t('Install'));

	return replace_macros(get_markup_template('app.tpl'),array(
		'$app' => $papp,
		'$install' => ((local_user() && $mode == 'view') ? $install_action : ''),
		'$delete' => ((local_user() && $installed && $mode == 'edit') ? t('Delete') : '')
	));
}


function app_install($uid,$app) {
	$app['uid'] = $uid;
	if(app_installed($uid,$app))
		$x = app_update($app);
	else
		$x = app_store($app);

	if($x['success'])
		return $x['app_id'];

	return false;
}

function app_destroy($uid,$app) {
	if($uid && $app['guid']) {
		$r = q("delete from app where app_id = '%s' and app_channel = %d limit 1",
			dbesc($app['guid']),
			intval($uid)
		);
	}
}


function app_installed($uid,$app) {

	$r = q("select id from app where app_id = '%s' and app_version = '%s' and app_channel = %d limit 1",
		dbesc((array_key_exists('guid',$app)) ? $app['guid'] : ''), 
		dbesc((array_key_exists('version',$app)) ? $app['version'] : ''), 
		intval($uid)
	);
	return(($r) ? true : false);

}


function app_list($uid) {
	$r = q("select * from app where app_channel = %d order by app_name asc",
		intval($uid)
	);
	return($r);
}


function app_decode($s) {
	$x = base64_decode(str_replace(array('<br />',"\r","\n",' '),array('','','',''),$s));
	return json_decode($x,true);
}


function app_store($arr) {

	// logger('app_store: ' . print_r($arr,true));

	$darray = array();
	$ret = array('success' => false);

	$darray['app_url'] = ((x($arr,'url')) ? $arr['url'] : '');
	$darray['app_channel'] = ((x($arr,'uid')) ? $arr['uid'] : 0);
	if((! $darray['app_url']) || (! $darray['app_channel']))
		return $ret;

	if($arr['photo'] && ! strstr($arr['photo'],z_root())) {
		$x = import_profile_photo($arr['photo'],get_observer_hash(),true);
		$arr['photo'] = $x[1];
	}


	$darray['app_id'] = ((x($arr,'guid')) ? $arr['guid'] : random_string(). '.' . get_app()->get_hostname());
	$darray['app_sig'] = ((x($arr,'sig')) ? $arr['sig'] : '');
	$darray['app_author'] = ((x($arr,'author')) ? $arr['author'] : get_observer_hash());
	$darray['app_name'] = ((x($arr,'name')) ? escape_tags($arr['name']) : t('Unknown'));
	$darray['app_desc'] = ((x($arr,'desc')) ? escape_tags($arr['desc']) : '');
	$darray['app_photo'] = ((x($arr,'photo')) ? $arr['photo'] : z_root() . '/' . get_default_profile_photo(80));
	$darray['app_version'] = ((x($arr,'version')) ? escape_tags($arr['version']) : '');
	$darray['app_addr'] = ((x($arr,'addr')) ? escape_tags($arr['addr']) : '');
	$darray['app_price'] = ((x($arr,'price')) ? escape_tags($arr['price']) : '');
	$darray['app_page'] = ((x($arr,'page')) ? escape_tags($arr['page']) : '');

	$r = q("insert into app ( app_id, app_sig, app_author, app_name, app_desc, app_url, app_photo, app_version, app_channel, app_addr, app_price, app_page ) values ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s' )",
		dbesc($darray['app_id']),
		dbesc($darray['app_sig']),
		dbesc($darray['app_author']),
		dbesc($darray['app_name']),
		dbesc($darray['app_desc']),
		dbesc($darray['app_url']),
		dbesc($darray['app_photo']),
		dbesc($darray['app_version']),
		intval($darray['app_channel']),
		dbesc($darray['app_addr']),
		dbesc($darray['app_price']),
		dbesc($darray['app_page'])
	);
	if($r) {
		$ret['success'] = true;
		$ret['app_id'] = $darray['app_id'];
	}
	return $ret;
}


function app_update($arr) {

	$darray = array();
	$ret = array('success' => false);

	$darray['app_url'] = ((x($arr,'url')) ? $arr['url'] : '');
	$darray['app_channel'] = ((x($arr,'uid')) ? $arr['uid'] : 0);
	$darray['app_id'] = ((x($arr,'guid')) ? $arr['guid'] : 0);
	if((! $darray['app_url']) || (! $darray['app_channel']) || (! $darray['app_id']))
		return $ret;

	if($arr['photo'] && ! strstr($arr['photo'],z_root())) {
		$x = import_profile_photo($arr['photo'],get_observer_hash(),true);
		$arr['photo'] = $x[1];
	}

	$darray['app_sig'] = ((x($arr,'sig')) ? $arr['sig'] : '');
	$darray['app_author'] = ((x($arr,'author')) ? $arr['author'] : get_observer_hash());
	$darray['app_name'] = ((x($arr,'name')) ? escape_tags($arr['name']) : t('Unknown'));
	$darray['app_desc'] = ((x($arr,'desc')) ? escape_tags($arr['desc']) : '');
	$darray['app_photo'] = ((x($arr,'photo')) ? $arr['photo'] : z_root() . '/' . get_default_profile_photo(80));
	$darray['app_version'] = ((x($arr,'version')) ? escape_tags($arr['version']) : '');
	$darray['app_addr'] = ((x($arr,'addr')) ? escape_tags($arr['addr']) : '');
	$darray['app_price'] = ((x($arr,'price')) ? escape_tags($arr['price']) : '');
	$darray['app_page'] = ((x($arr,'page')) ? escape_tags($arr['page']) : '');

	$r = q("update app set app_sig = '%s', app_author = '%s', app_name = '%s', app_desc = '%s', app_url = '%s', app_photo = '%s', app_version = '%s', app_addr = '%s', app_price = '%s', app_page = '%s' where app_id = '%s' and app_channel = %d limit 1",
		dbesc($darray['app_sig']),
		dbesc($darray['app_author']),
		dbesc($darray['app_name']),
		dbesc($darray['app_desc']),
		dbesc($darray['app_url']),
		dbesc($darray['app_photo']),
		dbesc($darray['app_version']),
		dbesc($darray['app_addr']),
		dbesc($darray['app_price']),
		dbesc($darray['app_page']),
		dbesc($darray['app_id']),
		intval($darray['app_channel'])
	);
	if($r) {
		$ret['success'] = true;
		$ret['app_id'] = $darray['app_id'];
	}

	return $ret;

}


function app_encode($app,$embed = false) {

	$ret = array();

	if($app['app_id'])
		$ret['guid'] = $app['app_id'];

	if($app['app_id'])
		$ret['guid'] = $app['app_id'];

	if($app['app_sig'])
		$ret['sig'] = $app['app_sig'];

	if($app['app_author'])
		$ret['author'] = $app['app_author'];

	if($app['app_name'])
		$ret['name'] = $app['app_name'];

	if($app['app_desc'])
		$ret['desc'] = $app['app_desc'];

	if($app['app_url'])
		$ret['url'] = $app['app_url'];

	if($app['app_photo'])
		$ret['photo'] = $app['app_photo'];

	if($app['app_version'])
		$ret['version'] = $app['app_version'];

	if($app['app_addr'])
		$ret['addr'] = $app['app_addr'];

	if($app['app_price'])
		$ret['price'] = $app['app_price'];

	if($app['app_page'])
		$ret['page'] = $app['app_page'];

	if(! $embed)
		return $ret;

	$j = json_encode($ret);
	return '[app]' . chunk_split(base64_encode($j),72,"\n") . '[/app]';

}


function papp_encode($papp) {
	return chunk_split(base64_encode(json_encode($papp)),72,"\n");

}