-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_resource.php
More file actions
141 lines (123 loc) · 4.69 KB
/
send_resource.php
File metadata and controls
141 lines (123 loc) · 4.69 KB
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
<?php
header ("Content-Type:text/xml");
require_once('./conf.php');
/*------------------------------------------------------------------------*
* © 2010 University of Limerick. All rights reserved. This material may *
* not be reproduced, displayed, modified or distributed without the *
* express prior written permission of the copyright holder. *
*------------------------------------------------------------------------*/
function prepDB($data)
{
//this function prepare string data to be appended to sqlite database.
$tmpdata=stripslashes($data);
$tmpdata=str_replace('\'','\'\'',$tmpdata);
$tmpdata=str_replace('\"','\"\"',$tmpdata);
$pattern = '/<\?xml version.*;?>/i';
$replacement = '';
$tmpdata=preg_replace($pattern, $replacement, $tmpdata);
return $tmpdata;
}
function genID()
{
// this function generates an resource ID that is not found in the Resources Database
$newID=substr(md5(uniqid()), 0,10);
$db = new PDO('sqlite:'.BASE_DB_URL.'locTemp.sqlite');
while (($newID==$id) || ($newID==""))
{
$newID= substr(md5(uniqid()), 0,10);
$result = $db->query("SELECT ResourceID FROM Resources where ResourceID='".$newID."'");
$id=$result->fetchColumn();
};
$db=null;
return $newID;
}
function sendResource($nid, $type, $desc, $data, $metadata)
{
$error_occured=0;
$response='';
try
{
$id=$nid;
if ($id=="NOTSET") $id=genID(); // if Resource ID is not given, generate an ID for the resource;
$db = new PDO('sqlite:'.BASE_DB_URL.'locTemp.sqlite');
$result = $db->query('SELECT ResourceID FROM Resources where ResourceID="'.$id.'"');
// see whether a resource with the given ID already exists in the database
$dbid= trim($result->fetchColumn());
if ($dbid=='') // there is no resource found in the database with given id. so we need to insert data.
{
$statement="INSERT INTO Resources(ResourceID, Type, Description, File) VALUES ('".$id."', '".strtoupper($type)."','".$desc."','".$data."')";
$count = $db->exec($statement);
//print $statement."<br/>";
//process metadata sent with the request.
//$metadata = "attribute1:value1-attribut1:value2-attribut3:value3";
$metaelements = explode("-", $metadata);
foreach($metaelements as $item)
{
$s = explode(":", $item);
$attrib=prepDB(trim($s[0]));
$value=prepDB(trim($s[1]));
$statement="INSERT INTO ResourceMetadata(ResourceID, Attribute, Value) VALUES ('".$id."', '".strtolower($attrib)."','".strtolower($value)."')";
$count = $db->exec($statement);
//print $statement."<br/>";
}
} else // The resource corresponding to the given ID is already there. Therefore we need to update that resource.
{
$count1 = $db->exec("Update Resources set File='".$data."' where ResourceID='".$id."'");
$count2 = $db->exec("Update Resources set Type='".strtoupper($type)."' where ResourceID='".$id."'");
$count3 = $db->exec("Update Resources set Description='".$desc."' where ResourceID='".$id."'");
// Delete all the metadata related with given ResourceID
$count = $db->exec("Delete FROM ResourceMetadata where ResourceID='".$id."'");
//process metadata
//$metadata = "attribute1:value1-attribut1:value2-attribut3:value3";
$metaelements = explode("-", $metadata);
foreach($metaelements as $item)
{
$s = explode(":", $item);
$attrib=prepDB(trim($s[0]));
$value=prepDB(trim($s[1]));
$statement="INSERT INTO ResourceMetadata(ResourceID, Attribute, Value) VALUES ('".$id."', '".strtolower($attrib)."','".strtolower($value)."')";
$count = $db->exec($statement);
//print $statement."<br/>";
}
};
// close the database connection
$db = NULL;
}
catch(PDOException $e)
{
//print 'Exception : '.$e->getMessage();
$response="<error><msg>".$e->getMessage()."</msg></error>";
}
//if ($count>0) $response="<response><msg>Output Accepted</msg></response>";
//else $response="<error><msg> Output was not updated. Ensure job id:".$id." is correct and component:".$com." has been assigned that job.</msg></error>";
$response="<resource><msg>".$id."</msg></resource>";
return $response;
}
$id=$_POST["id"];
$type=$_POST["type"];
$desc=$_POST["desc"];
$metadata=$_POST["metadata"];
$content=$_POST["data"];
$data=prepDB($content);
//print $data;
if($id=="") $id="NOTSET"; //resource id was not found in the request
if($desc=="") $desc="";
//validate post request
if ($data=="")
{
print "<error><msg>data not found in the request</msg></error>";
} else
{
if ($metadata=="")
{
print "<error><msg>metadata not found in the request</msg></error>";
} else
{
if ($type=="")
{
print "<error><msg>resource type not found in the request</msg></error>";
} else echo sendResource($id, $type, $desc, $data, $metadata);
}
}
//print $data;
;?>