forked from pragunbhutani/skadool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
164 lines (106 loc) · 3.71 KB
/
api.php
File metadata and controls
164 lines (106 loc) · 3.71 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
require_once "includes.php";
switch($_GET['method']) {
case 'create' :
$query = "INSERT INTO `visits` (
`visitID`, `contactName`, `contactNumber`, `status`, 'updateTime'
) VALUES (
NULL, '" . mysql_real_escape_string($_GET['name']) . "', '" . mysql_real_escape_string($_GET['number']) . "', 'Unscheduled', " . time() . "
)"
;
mysql_query($query) or die('Error while inserting new row : ' . mysql_error());
$query = "SELECT * FROM visits ORDER BY visitID DESC LIMIT 1";
$go = mysql_query($query) or die('Unable to retrieve data : ' . mysql_error());
$fetch = mysql_fetch_row($go);
$return = Array($fetch[0], $fetch[1], $fetch[2], $fetch[3]);
$results = Array(
'body' => Array(
'id' => $return[0],
'name' => $return[1],
'number' => $return[2],
'status' => $return[3],
)
);
break;
case 'update' :
$query = "UPDATE visits SET status='" . mysql_real_escape_string($_GET['status']) . "', updateTime=" . time() . " WHERE contactNumber='" . mysql_real_escape_string($_GET['number']) . "'";
mysql_query($query) or die('Error while updating row : ' . mysql_error());
$query = "SELECT visitID, contactName, status from visits WHERE contactNumber='" . mysql_real_escape_string($_GET['number']) . "'";
$go = mysql_query($query) or die('Unable to retrieve data : ' . mysql_error());
$fetch = mysql_fetch_row($go);
$return = Array($fetch[0], $fetch[1], $fetch[2]);
$results = Array(
'body' => Array(
'id' => $return[0],
'name' => $return[1],
'status' => $return[2]
)
);
break;
case 'get' :
$query = "SELECT visitID, contactName, status FROM visits WHERE contactNumber='" . mysql_real_escape_string($_GET['number']) . "'";
$go = mysql_query($query) or die('Unable to retrieve data : ' . mysql_error());
$fetch = mysql_fetch_row($go);
$return = Array($fetch[0], $fetch[1], $fetch[2]);
$results = Array(
'body' => Array(
'id' => $return[0],
'name' => $return[1],
'status' => $return[2]
)
);
break;
case 'delete' :
$query = "DELETE FROM visits WHERE visitID='" . mysql_real_escape_string($_GET['id']) . "'";
mysql_query($query) or die('Could not delete row : ' . mysql_error());
$results = Array(
'body' => Array(
'id' => $_GET['id']
)
);
break;
}
switch($_GET['format']) {
case 'json' :
/* Setting up JSON headers */
@header ("content-type: text/json charset=utf-8");
/* Printing the JSON Object */
echo json_encode($results);
break;
case 'xml' :
/* Setting XML header */
@header ("content-type: text/xml charset=utf-8");
/* Initializing the XML Object */
$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('callback');
$xml->writeAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');
$xml->writeAttribute('xsi:noNamespaceSchemaLocation','schema.xsd');
/* Function that converts each array element to an XML node */
function write(XMLWriter $xml, $data){
foreach($data as $key => $value){
if(is_array($value)){
$xml->startElement($key);
write($xml, $value);
$xml->endElement();
continue;
}
$xml->writeElement($key, $value);
}
}
/* Calls previously declared function, passing our results array as parameter */
write($xml, $results);
/* Closing last XML node */
$xml->endElement();
/* Printing the XML */
echo $xml->outputMemory(true);
break;
case 'php' :
/* Setting up PHP headers */
header ("content-type: text/php charset=utf-8");
/* Printing the PHP serialized Object*/
echo serialize($results);
break;
}
?>