-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpFunctions.php
More file actions
executable file
·195 lines (185 loc) · 5.01 KB
/
phpFunctions.php
File metadata and controls
executable file
·195 lines (185 loc) · 5.01 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
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
<?php
/* Template function for defining custom procedures
** input parameter is usually data from post request
** returns PHP object
*/
function phpTestPrintout($inp, $conn){
$info = (object)[
'title' => "Test RPC",
'desc' => "Dummy Function"
];
return (object) array_merge( (array) $info, (array) $inp );
}
function getUserData($inp, $conn, $tokenData){
if(isset($inp->userId)){
$sth = $conn->prepare("SELECT * FROM `users` WHERE `id`=:userId");
$sth->bindParam('userId', $inp->userId);
}else{
$sth = $conn->prepare("SELECT * FROM `users` WHERE `name`=:name");
$sth->bindParam('name', $inp->userName);
}
try{
$sth->execute();
$result = $sth->fetch(PDO::FETCH_OBJ);
$sth2 = $conn->prepare('SHOW FIELDS FROM users');
$sth2->execute();
// $fields = $sth2->fetchAll(PDO::FETCH_OBJ);
$ret = [
'OK' => true,
'dataSet' => $inp->dataSet,
'table' => 'users',
'message' => $sth->rowCount()==1 ? 'UserData OK' : 'User not Found!' ,
"data" => $result
// "fileds" => $fields
];
} catch (PDOException $e) {
$ret = [
'OK' => false,
'errorType' => 'DataBase',
'code' => 416,
'message' => "Data Base Error!",
'PDO' => $e,
"userData" => false
];
}
return $ret;
}
//Checking the validity of the username, new or changed one
function validNewUserName($inp,$conn,$tokenData){
if(isset($inp->userId) && isset($inp->oldUserName) && isset($inp->newUserName)){
try{
$sth = $conn->prepare("SELECT count(id) `count` FROM `users` WHERE id!=:userId and name=:newUserName");
$sth->bindParam('userId', $inp->userId);
$sth->bindParam('newUserName', $inp->newUserName);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_OBJ);
$data = $result->count == 0;
if($data)
//$message='Valid new Username';
$message = '';
else
$message="Username is already taken!";
return [
'OK' => true,
'code' => 200,
'message' => $message,
'data' => $data
];
} catch (PDOException $e){
return [
'OK' => false,
'errorType' => 'Some server ERROR!',
'code' => 500,
'PDO' => $e,
'message' => "Internal RPC server error!"
];
}
}else{
return [
'OK' => false,
'errorType' => 'Bad Request!',
'code' => 400,
'message' => "Wrong input parametars!"
];
}
}
// Change password function
function changePassword($inp, $conn, $tokenData){
if(isset($inp->userId)){
if($tokenData->name != $inp->userName || $tokenData->id != $inp->userId ){
return [
'OK' => false,
'errorType' => 'fakeRquest',
'code' => 477,
'message' => "Fake request Error! $tokenData->name != $inp->userName || $tokenData->id != $inp->userId"
];
};
};
$sth = $conn->prepare("SELECT `password` FROM `users` WHERE `id`=:userId");
$sth->bindParam('userId', $inp->userId);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_OBJ);
if( $inp->oldPassword != $result->password ){
return [
'oldp' => $inp->oldPassword,
'dnOld' => $result->password,
'OK' => false,
'errorType' => 'oldPasswordErr',
'code' => 401,
'message' => "Current Password is Incorrect!"
];
}
$sth2 = $conn->prepare("UPDATE `users` SET `password` = :userPassword WHERE `users`.`id` = :userId;");
$sth2->bindParam('userPassword', $inp->newPassword);
$sth2->bindParam('userId', $inp->userId);
try{
$sth2->execute();
$ret = [
'OK' => true,
'message' => "Password changed!" ,
// "data" => $result
// "fileds" => $fields
];
}catch (PDOException $e) {
$ret = [
'OK' => false,
'errorType' => 'DataBase',
'code' => 416,
'message' => "Data Base Error!",
'PDO' => $e,
"userData" => false
];
}
return $ret;
}
// Getting photo list from a folder on the server, just for an example
function photoList()
{
$ok = true;
$dir="./images/slikca";
$array = [];
$sizes = [];
$link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
foreach (new DirectoryIterator($dir) as $fileInfo) {
if ($fileInfo->getExtension() == 'jpg' ) {
$fl ='images/slikca/'.$fileInfo->getFilename();
array_push($array, $link.$fl);
array_push($sizes, getimagesize( $fl ));
};
}
$ret = [
'OK' => true,
'dataSet' => 'photoList',
'data' => $array,
'sizes' => $sizes
];
return ($ret);
}
// photo list with exif data from photos
function photoListExif()
{
$ok = true;
$dir="./images/slikca";
$array = [];
$sizes = [];
$exif = [];
$link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
foreach (new DirectoryIterator($dir) as $fileInfo) {
if ($fileInfo->getExtension() == 'jpg' ) {
$fl ='images/slikca/'.$fileInfo->getFilename();
array_push($array, $link.$fl);
array_push($sizes, getimagesize( $fl ));
array_push($exif, exif_read_data( $fl) );
};
}
$ret = [
'OK' => true,
'dataSet' => 'photoList',
'data' => $array,
'sizes' => $sizes,
'exif' => $exif
];
return ($ret);
}
// place for rpc declarations
?>