aboutsummaryrefslogtreecommitdiffstats
path: root/mod/impel.php
blob: e58264ce8d476cb3ea2098ae85cef28fa709c05e (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
<?php /** @file */

// import page design element


function impel_init(&$a) {

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

	if(! local_channel())
		json_return_and_die($ret);

	logger('impel: ' . print_r($_REQUEST,true), LOGGER_DATA);

	$elm = $_REQUEST['element'];
	$x = base64url_decode($elm);
	if(! $x)
		json_return_and_die($ret);

	$j = json_decode($x,true);
	if(! $j)
		json_return_and_die($ret);


	$channel = $a->get_channel();

	$arr = array();
	$is_menu = false;

	// a portable menu has its links rewritten with the local baseurl
	$portable_menu = false;

	switch($j['type']) {
		case 'webpage':
			$arr['item_restrict'] = ITEM_WEBPAGE;
			$namespace = 'WEBPAGE';
			$installed_type = t('webpage');
			break;
		case 'block':
			$arr['item_restrict'] = ITEM_BUILDBLOCK;
			$namespace = 'BUILDBLOCK';
			$installed_type = t('block');
			break;
		case 'layout':
			$arr['item_restrict'] = ITEM_PDL;
			$namespace = 'PDL';
			$installed_type = t('layout');
			break;
		case 'portable-menu':
			$portable_menu = true;
			// fall through
		case 'menu':
			$is_menu = true;
			$installed_type = t('menu');
			break;			
		default:
			logger('mod_impel: unrecognised element type' . print_r($j,true));
			break;
	}
	if($is_menu) {








	}
	else {
		$arr['uid'] = local_channel();
		$arr['aid'] = $channel['channel_account_id'];
		$arr['title'] = $j['title'];
		$arr['body'] = $j['body'];
		$arr['term'] = $j['term'];
		$arr['created'] = datetime_convert('UTC','UTC', $j['created']);
		$arr['edited'] = datetime_convert('UTC','UTC',$j['edited']);
		$arr['owner_xchan'] = get_observer_hash();
		$arr['author_xchan'] = (($j['author_xchan']) ? $j['author_xchan'] : get_observer_hash());
		$arr['mimetype'] = (($j['mimetype']) ? $j['mimetype'] : 'text/bbcode');

		if(! $j['mid'])
			$j['mid'] = item_message_id();

		$arr['mid'] = $arr['parent_mid'] = $j['mid'];


		if($j['pagetitle']) {
			require_once('library/urlify/URLify.php');
			$pagetitle = strtolower(URLify::transliterate($j['pagetitle']));
		}



		// Verify ability to use html or php!!!

	    $execflag = false;

		if($arr['mimetype'] === 'application/x-php') {
			$z = q("select account_id, account_roles, channel_pageflags from account left join channel on channel_account_id = account_id where channel_id = %d limit 1",
				intval(local_channel())
			);

			if($z && (($z[0]['account_roles'] & ACCOUNT_ROLE_ALLOWCODE) || ($z[0]['channel_pageflags'] & PAGE_ALLOWCODE))) {
				$execflag = true;
			}
		}

		$remote_id = 0;

		$z = q("select * from item_id where sid = '%s' and service = '%s' and uid = %d limit 1",
			dbesc($pagetitle),
			dbesc($namespace),
			intval(local_channel())
		);
		$i = q("select id, item_restrict from item where mid = '%s' and uid = %d limit 1",
			dbesc($arr['mid']),
			intval(local_channel())
		);

		if($z && $i) {
			$remote_id = $z[0]['id'];
			$arr['id'] = $i[0]['id'];
			// don't update if it has the same timestamp as the original
			if($arr['edited'] > $i[0]['edited'])
				$x = item_store_update($arr,$execflag);
		}
		else {
			if(($i) && ($i[0]['item_restrict'] & ITEM_DELETED)) {
				// was partially deleted already, finish it off
				q("delete from item where mid = '%s' and uid = %d",
					dbesc($arr['mid']),
					intval(local_channel())
				);
			}
			$x = item_store($arr,$execflag);
		}

		if($x['success']) {
			$item_id = $x['item_id'];
			update_remote_id($channel,$item_id,$arr['item_restrict'],$pagetitle,$namespace,$remote_id,$arr['mid']);
		}
	}

	if($x['success']) {
		$ret['success'] = true;
		info( sprintf( t('%s element installed'), $installed_type)); 
	}
	else {
		notice( sprintf( t('%s element installation failed'), $installed_type)); 
	}

//??? should perhaps return ret? 
	json_return_and_die(true);

}