-
Notifications
You must be signed in to change notification settings - Fork 61
/
upload.php
301 lines (259 loc) · 10.3 KB
/
upload.php
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
<?php
/**
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
* The Apereo Foundation licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once(dirname(__FILE__) . "/../config.php");
require (dirname(__FILE__) . "/../" . $xerte_toolkits_site->php_library_path . "user_library.php");
/**
*
* Extension for SimpleXMLElement
* @author Alexandre FERAUD
*
*/
class ExSimpleXMLElement extends SimpleXMLElement
{
/**
* Add CDATA text in a node
* @param string $cdata_text The CDATA value to add
*/
public function addCData($cdata_text)
{
$node= dom_import_simplexml($this);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($cdata_text));
}
/**
* Create a child with CDATA value
* @param string $name The name of the child element to add.
* @param string $cdata_text The CDATA value of the child element.
*/
public function addChildCData($name,$cdata_text)
{
$child = $this->addChild($name);
$child->addCData($cdata_text);
}
/**
* Add SimpleXMLElement code into a SimpleXMLElement
* @param SimpleXMLElement $append
*/
public function appendXML($append)
{
if ($append) {
if (strlen(trim((string) $append))==0) {
$xml = $this->addChild($append->getName());
foreach($append->children() as $child) {
$xml->appendXML($child);
}
} else {
$xml = $this->addChild($append->getName(), (string) $append);
}
foreach($append->attributes() as $n => $v) {
$xml->addAttribute($n, $v);
}
}
}
}
if (!isset($_SESSION['toolkits_logon_username']) && !is_user_admin())
{
_debug("Session is invalid or expired");
die("Session is invalid or expired");
}
//_debug("upload: " . print_r($_POST, true));
// Check for Preview/Publish
$fileupdate = $_POST["fileupdate"];
$filename = $_POST["filename"];
$mode = $fileupdate ? "publish" : "preview";
if ($mode == 'publish')
{
$preview = dirname(dirname(__FILE__)) . '/' . $_POST["preview"];
}
$filename = dirname(dirname(__FILE__)) . '/' . $filename;
$filenamejson = substr($filename, 0, strlen($filename)-3) . "json";
// This code miserably fails if get_magic_quotes_gpc is turned on
// decoding the json doesn't work anymore
$lo_data = $_POST["lo_data"];
if (function_exists('get_magic_quotes_gpc'))
{
if (get_magic_quotes_gpc())
{
$lo_data=stripslashes($_POST["lo_data"]);
}
}
//_debug("upload (lo_data): " . $lo_data);
$relreffedjsonstr = make_refs_local(urldecode($lo_data), $_POST['absmedia']);
//_debug("upload (lo_data, local_refs): " . $relreffedjsonstr);
file_put_contents($filenamejson, print_r($relreffedjsonstr, true));
$relreffedjson = json_decode($relreffedjsonstr);
//_debug("upload: decoded json");
$data = process($relreffedjson);
//_debug("upload: converted to xml");
// save round-robin queue of 10 xml's
for ($i=10; $i>1; $i--)
{
$j = $i-1;
if (file_exists($filename . "." . $j)) {
rename($filename . "." . $j, $filename . "." . $i);
}
}
rename($filename, $filename . ".1");
// save round-robin queue of 10 json's
for ($i=10; $i>1; $i--)
{
$j = $i-1;
if (file_exists($filenamejson . "." . $j)) {
rename($filenamejson . "." . $j, $filenamejson . "." . $i);
}
}
rename($filenamejson, $filenamejson . ".1");
file_put_contents($filename, $data->asXML());
//_debug("upload: saved as xml");
if ($mode == "publish")
{
file_put_contents($preview, $data->asXML());
// Update templatedetails modify date
$sql = "update {$xerte_toolkits_site->database_table_prefix}templatedetails set date_modified=? where template_id=?";
$params = array(date("Y-m-d H:i:s"), $_POST['template_id']);
db_query_one($sql, $params);
update_oai($data);
//_debug("upload: updated table");
}
echo true;
function make_refs_local($json, $media)
{
// replace instances of $media by FileLocation + '
$temp = $json;
//file_put_contents("step0_$mode.txt", print_r($temp, true));
//1a. \" followed by media
$pos = strpos($temp, '\"' . $media);
while ($pos !== false)
{
$pos2 = strpos($temp, '\"', $pos+1);
$temp = substr($temp, 0, $pos) . '\"FileLocation + \'' . substr($temp, $pos + strlen($media) + 2, $pos2 - $pos - strlen($media)-2) . '\'\"' . substr($temp, $pos2+2);
$pos = strpos($temp, '\"' . $media);
}
//file_put_contents("step3a_$mode.txt", print_r($temp, true));
//1b. " followed by media
$pos = strpos($temp, '"' . $media);
while ($pos !== false)
{
$pos2 = strpos($temp, '"', $pos+1);
$temp = substr($temp, 0, $pos) . '"FileLocation + \'' . substr($temp, $pos + strlen($media) + 1, $pos2 - $pos - strlen($media) -1) . '\'"' . substr($temp, $pos2+1);
$pos = strpos($temp, '"' . $media);
}
//1c. FIX '/media'
$temp = str_replace("'/media", "'media", $temp);
return $temp;
}
function process($json, $xml = null) {
//_debug("upload: " . print_r($json, true));
if (isset($json->attributes)) {
foreach ($json->attributes as $key => $val) {
$name = $key; //echo $name;
$value = $val; //echo $value;
//_debug("upload: " . $name . " ->" . $value);
if (is_null($xml)) {
if ($name == 'nodeName') {
//_debug("upload: before new");
$xml = new ExSimpleXMLElement('<'.$value.'/>');
//_debug("upload: after new");
}
else {
//_debug("upload: addAttribute (1)");
$xml->addAttribute($name, $value);
}
}
else {
if ($name == 'nodeName') {
//_debug("upload: addChild");
$xml = $xml->addChild($value);
}
else {
//_debug("upload: addAttribute (2)");
$xml->addAttribute($name, $value);
}
}
}
}
if (isset($json->data)) {
//_debug("upload: Add CDATA step 1");
if (! is_null($xml))
{
//_debug("upload: Add CDATA step 2");
$xml = $xml->addCData($json->data);
}
}
// Do the same for all child nodes
//_debug("upload: processing children");
if (isset($json->children)) {
foreach ($json->children as $key => $val) {
//_debug("upload: process " . $key . " -> " . print_r($val, true));
process($val, $xml);
}
}
return $xml;
}
function is_ajax_request() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
}
function update_oai($data){
global $xerte_toolkits_site;
$oaiPmhAgree = (string)$data->attributes()->oaiPmhAgree;
if ((string)$data->attributes()->targetFolder != "site") {
$category = (string)$data->attributes()->category;
}
else {
$category = (string)$data->attributes()->metaCategory;
}
$level = (string)$data->attributes()->metaEducation;
$user_type = '';
//get access status
$sql = "select access_to_whom from {$xerte_toolkits_site->database_table_prefix}templatedetails where template_id=?";
$rec = db_query_one($sql, array($_POST['template_id']));
$status = $rec["access_to_whom"];
if ($oaiPmhAgree !== "") {
$sql = "select status from {$xerte_toolkits_site->database_table_prefix}oai_publish where template_id=? ORDER BY audith_id DESC LIMIT 1";
$params = array($_POST['template_id']);
$rec = db_query_one($sql, $params);
$last_oaiTable_status = $rec["status"];
//find user type
if (is_user_admin()){
$user_type = "admin";
} else {
$sql = "select role from {$xerte_toolkits_site->database_table_prefix}templaterights where template_id=? AND user_id=?";
$params = array($_POST['template_id'], $_SESSION['toolkits_logon_id']);
$rec = db_query_one($sql, $params);
$user_type = $rec["role"];
}
$query = "insert into {$xerte_toolkits_site->database_table_prefix}oai_publish set template_id=?, login_id=?, user_type=?, status=?";
$params = array($_POST['template_id'], $_SESSION["toolkits_logon_id"], $user_type);
if ($oaiPmhAgree == 'true' and $category !== "" and $level !== "" and $status === "Public") {
//add new row to oai_published to indicate current oai-pmh status
if (is_null($last_oaiTable_status) || $last_oaiTable_status != "published") {
db_query_one($query, array_merge($params, array("published")));
}
} elseif ($oaiPmhAgree == 'true' and ($category == "" or $level == "") and $status === "Public") {
//if the project has never been oai published we don't add it here
if ($last_oaiTable_status != "incomplete" AND !is_null($last_oaiTable_status)){
db_query_one($query, array_merge($params, array("incomplete")));
}
} else {
//if the project has never been oai published we don't add it here
if ($last_oaiTable_status != "deleted" AND !is_null($last_oaiTable_status)){
db_query_one($query, array_merge($params, array("deleted")));
}
}
}
}