aboutsummaryrefslogtreecommitdiffstats
path: root/mod/uexport.php
blob: fdd7fcec64d4aaf0d1d757c8ddda1c1e0411f6f5 (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
<?php

function uexport_init(&$a) {

	if(! local_user())
		killme();

	$user = array();
	$r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
		local_user()
	);
	if(count($r)) {
		foreach($r as $rr)
			foreach($rr as $k => $v)
				$user[$k] = $v;

	}
	$contact = array();
	$r = q("SELECT * FROM `contact` WHERE `uid` = %d ",
		intval(local_user())
	);
	if(count($r)) {
		foreach($r as $rr)
			foreach($rr as $k => $v)
				$contact[][$k] = $v;

	}

	$profile = array();
	$r = q("SELECT * FROM `profile` WHERE `uid` = %d ",
		intval(local_user())
	);
	if(count($r)) {
		foreach($r as $rr)
			foreach($rr as $k => $v)
				$profile[][$k] = $v;
	}

	$output = array('user' => $user, 'contact' => $contact, 'profile' => $profile );

	header("Content-type: text/json");
	echo str_replace('\\/','/',json_encode($output));

	$r = q("SELECT count(*) as `total` FROM `item` WHERE `uid` = %d ",
		intval(local_user())
	);
	if(count($r))
		$total = $r[0]['total'];

	// chunk the output to avoid exhausting memory

	for($x = 0; $x < $total; $x += 500) {
		$item = array();
		$r = q("SELECT * FROM `item` WHERE `uid` = %d LIMIT %d, %d",
			intval(local_user()),
			intval($x),
			intval(500)
		);
		if(count($r)) {
			foreach($r as $rr)
				foreach($rr as $k => $v)
					$item[][$k] = $v;
		}

		$output = array('item' => $item);
		echo str_replace('\\/','/',json_encode($output));
	}


	killme();

}