aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Widget/Photo_rand.php
blob: cfe026b19260427e605152db740ac3e3af117069 (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
<?php

/**
 *   * Name: Random photo
 *   * Description: Display a random photo
 */

namespace Zotlabs\Widget;

require_once('include/photos.php');

class Photo_rand {

	function widget($arr) {

		$style = false;

		if(array_key_exists('album', $arr) && isset($arr['album']))
			$album = $arr['album'];
		else
			$album = '';

		$channel_id = 0;
		if(array_key_exists('channel_id', $arr) && intval($arr['channel_id']))
			$channel_id = intval($arr['channel_id']);
		if(! $channel_id)
			$channel_id = \App::$profile_uid;
		if(! $channel_id)
			return '';

		$scale = ((array_key_exists('scale',$arr)) ? intval($arr['scale']) : 0);

		$ret = photos_list_photos(array('channel_id' => $channel_id),\App::get_observer(),$album);

		$filtered = array();
		if($ret['success'] && $ret['photos'])
		foreach($ret['photos'] as $p)
			if($p['imgscale'] == $scale)
				$filtered[] = $p['src'];

		if($filtered) {
			$e = mt_rand(0, count($filtered) - 1);
			$url = $filtered[$e];
		}

		if(strpos($url, 'http') !== 0)
			return '';

		if(array_key_exists('style', $arr) && isset($arr['style']))
			$style = $arr['style'];

		// ensure they can't sneak in an eval(js) function

		if(strpos($style,'(') !== false)
			return '';

		$url = zid($url);

		$o = '<div class="widget">';

		$o .= '<img class="zrl" '
			. (($style) ? ' style="' . $style . '"' : '')
			. ' src="' . $url . '" alt="' . t('photo/image') . '">';

		$o .= '</div>';

		return $o;
	}
}