aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Module/Sitelist.php
blob: 9908aa6517a59d61bd13d888155182d98f06db94 (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
<?php
namespace Zotlabs\Module; /** @file */


class Sitelist extends \Zotlabs\Web\Controller {

	function init() {

		$start = ((isset($_REQUEST['start'])) ? intval($_REQUEST['start']) : 0);
		$limit = ((isset($_REQUEST['limit'])) ? intval($_REQUEST['limit']) : 30);
		$order = ((isset($_REQUEST['order'])) ? $_REQUEST['order'] : 'random');
		$open = ((isset($_REQUEST['open'])) ? intval($_REQUEST['open']) : false);


		$sql_order = " order by site_url ";
		$rand = db_getfunc('rand');
		if($order == 'random')
			$sql_order = " order by $rand ";

		$sql_limit = " LIMIT $limit OFFSET $start ";

		$sql_extra = "";
		if($open)
			$sql_extra = " and site_register = " . intval(REGISTER_OPEN) . " ";

		$realm = get_directory_realm();
		if($realm == DIRECTORY_REALM) {
			$sql_extra .= " and ( site_realm = '" . dbesc($realm) . "' or site_realm = '') ";
		}
		else
			$sql_extra .= " and site_realm = '" . dbesc($realm) . "' ";

		$result = array('success' => false);

		$r = q("select count(site_url) as total from site where site_type = %d and site_dead = 0 $sql_extra ",
			intval(SITE_TYPE_ZOT)
		);

		if($r)
			$result['total'] = intval($r[0]['total']);

		$result['start'] = $start;
		$result['limit'] = $limit;

		$r = q("select * from site where site_type = %d and site_dead = 0 $sql_extra $sql_order $sql_limit",
			intval(SITE_TYPE_ZOT)
		);

		$result['results'] = 0;
		$result['entries'] = array();

		if($r) {
			$result['success'] = true;
			$result['results'] = count($r);

			foreach($r as $rr) {
				$result['entries'][] = array('url' => $rr['site_url']);
			}

		}

		echo json_encode($result);
		killme();


	}
}