Skip to content

Commit 9c9af25

Browse files
authored
Merge pull request #6 from BMSVieira/development
Development
2 parents 139b9e6 + af797a8 commit 9c9af25

File tree

10 files changed

+759
-173
lines changed

10 files changed

+759
-173
lines changed

README.md

Lines changed: 312 additions & 144 deletions
Large diffs are not rendered by default.

ost_wbs/classes/class.department.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ class Department
33
{
44
public function all($parameters)
55
{
6+
// Check Request method
7+
$validRequests = array("GET");
8+
Helper::validRequest($validRequests);
9+
610
// Connect Database
711
$Dbobj = new DBConnection();
812
$mysqli = $Dbobj->getDBConnect();
@@ -11,8 +15,9 @@ public function all($parameters)
1115
// Sorte by Date
1216
case "creationDate":
1317

14-
$startDate = Helper::getFormatedDate($parameters["parameters"][0], "start");
15-
$endDate = Helper::getFormatedDate($parameters["parameters"][0], "end");
18+
// Get Start&End Date
19+
$startDate = $parameters['parameters']['start_date'];
20+
$endDate = $parameters['parameters']['end_date'];
1621

1722
// Query
1823
$getDepartment = $mysqli->query("SELECT * FROM ".TABLE_PREFIX."department WHERE ".TABLE_PREFIX."department.created >= '$startDate' and ".TABLE_PREFIX."department.created <= '$endDate'");
@@ -57,6 +62,10 @@ public function all($parameters)
5762
public function specific($parameters)
5863
{
5964

65+
// Check Request method
66+
$validRequests = array("GET");
67+
Helper::validRequest($validRequests);
68+
6069
// Connect Database
6170
$Dbobj = new DBConnection();
6271
$mysqli = $Dbobj->getDBConnect();

ost_wbs/classes/class.faq.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ class Faq
55

66
public function all($parameters)
77
{
8+
// Check Request method
9+
$validRequests = array("GET");
10+
Helper::validRequest($validRequests);
11+
812
// Connect Database
913
$Dbobj = new DBConnection();
1014
$mysqli = $Dbobj->getDBConnect();
@@ -36,7 +40,7 @@ public function all($parameters)
3640

3741
foreach ($result as $key=>$category) {
3842

39-
if ($result[$key]['faqs'] = $this->specific(['parameters'=>[0=>$category['id']]],TRUE) )
43+
if ($result[$key]['faqs'] = $this->specific(['parameters'=>["id"=>$category['id']]],TRUE) )
4044
{
4145

4246
} else {
@@ -75,10 +79,14 @@ public function all($parameters)
7579

7680
public function specific($parameters,$exception = FALSE)
7781
{
82+
// Check Request method
83+
$validRequests = array("GET");
84+
Helper::validRequest($validRequests);
85+
7886
// Connect Database
7987
$Dbobj = new DBConnection();
8088
$mysqli = $Dbobj->getDBConnect();
81-
$cID = $parameters["parameters"][0];
89+
$cID = $parameters["parameters"]["id"];
8290

8391
// Query
8492
$getFaq = $mysqli->query("SELECT * FROM ".TABLE_PREFIX."faq WHERE category_id = " . $cID . " AND ispublished = 1");

ost_wbs/classes/class.helper.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function checkTicketStatus($ticketstatus)
1717
return true;
1818
}
1919

20-
// Get formated date from string
20+
// Get formated date from string
2121
public function getFormatedDate($fullstring, $condition)
2222
{
2323

@@ -35,6 +35,31 @@ public function getFormatedDate($fullstring, $condition)
3535
}
3636

3737
return $result;
38+
}
3839

40+
// Check if request method is valid
41+
public function validRequest($method){
42+
if(!in_array($_SERVER['REQUEST_METHOD'], $method)){
43+
throw new Exception($_SERVER['REQUEST_METHOD']." is not a valid request method");
44+
}
45+
}
46+
47+
// Check permissions
48+
public function checkPermission(){
49+
if(CANCREATE == 0){ throw new Exception("Error! Your API Key is READ ONLY, it is no allowed to make any action.");}
50+
}
51+
52+
// Get last ID
53+
public function get_last_id($table, $field)
54+
{
55+
// Connect Database
56+
$Dbobj = new DBConnection();
57+
$mysqli = $Dbobj->getDBConnect();
58+
59+
// Get last inserted ID
60+
$getLastId = $mysqli->query("SELECT ".$field." FROM ".TABLE_PREFIX."".$table." ORDER BY ".$field." DESC LIMIT 1");
61+
$printLastId = $getLastId->fetch_object();
62+
63+
return $printLastId->$field;
3964
}
4065
}

ost_wbs/classes/class.key.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ function OAuth($key)
3030
if(!$this->farray["isactive"] || APIKEY_RESTRICT && $this->farray["ipaddr"] != $_SERVER['REMOTE_ADDR'])
3131
throw new Exception("API key not found/active or source IP not authorized");
3232

33+
define('CANCREATE', $this->farray["can_create_tickets"]); // Can create
34+
define('CANEXECUTE', $this->farray["can_exec_cron"]); // Can execute
35+
3336
}
3437

3538
function cancreate()

ost_wbs/classes/class.sla.php

Lines changed: 164 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ class Sla
33
{
44
public function all($parameters)
55
{
6+
// Check Request method
7+
$validRequests = array("GET");
8+
Helper::validRequest($validRequests);
9+
610
// Connect Database
711
$Dbobj = new DBConnection();
812
$mysqli = $Dbobj->getDBConnect();
@@ -11,8 +15,9 @@ public function all($parameters)
1115
// Sorte by Date
1216
case "creationDate":
1317

14-
$startDate = Helper::getFormatedDate($parameters["parameters"][0], "start");
15-
$endDate = Helper::getFormatedDate($parameters["parameters"][0], "end");
18+
// Get Start&End Date
19+
$startDate = $parameters['parameters']['start_date'];
20+
$endDate = $parameters['parameters']['end_date'];
1621

1722
// Query
1823
$getSla = $mysqli->query("SELECT * FROM ".TABLE_PREFIX."sla WHERE ".TABLE_PREFIX."sla.created >= '$startDate' and ".TABLE_PREFIX."sla.created <= '$endDate'");
@@ -59,7 +64,7 @@ public function specific($parameters)
5964
// Connect Database
6065
$Dbobj = new DBConnection();
6166
$mysqli = $Dbobj->getDBConnect();
62-
$uID = $parameters["parameters"][0];
67+
$uID = $parameters["parameters"]["id"];
6368

6469
// set query
6570
$getSla = $mysqli->query("SELECT * FROM ".TABLE_PREFIX."sla WHERE ".TABLE_PREFIX."sla.id = '$uID'");
@@ -93,5 +98,161 @@ public function specific($parameters)
9398
// Return values
9499
return $returnArray;
95100
}
101+
102+
103+
public function add($parameters)
104+
{
105+
106+
// Check Permission
107+
Helper::checkPermission();
108+
109+
// Check Request method
110+
$validRequests = array("POST", "PUT");
111+
Helper::validRequest($validRequests);
112+
113+
// Expected parameters
114+
$expectedParameters = array("name", "flags", "grace_period", "schedule_id", "notes");
115+
116+
// Check if all paremeters are correct
117+
self::checkRequest($parameters, $expectedParameters);
118+
119+
// Check if row already exists
120+
if($this->checkExists('name', $parameters["parameters"]['name'])) { throw new Exception("Item Already exists"); }
121+
122+
// Prepare query
123+
$paramOrder = "";
124+
$valuesOrder = "";
125+
126+
foreach ($parameters["parameters"] as $key => $value) {
127+
128+
// Parameters order
129+
$paramOrder = $paramOrder.",".$key;
130+
// Values order
131+
if(is_numeric($value)) { $valuesOrder = $valuesOrder.",".$value.""; } else { $valuesOrder = $valuesOrder.",'".$value."'";}
132+
}
133+
134+
// Remove first comma
135+
$paramOrder = substr($paramOrder, 1);
136+
$valuesOrder = substr($valuesOrder, 1);
137+
138+
// final Query
139+
$addQuery = "INSERT INTO ".TABLE_PREFIX."sla ";
140+
$addQuery .= "(".$paramOrder.", created, updated)";
141+
$addQuery .= "VALUES(".$valuesOrder.", now(), now())";
142+
143+
// Send query to be executed
144+
return $this->execQuery($addQuery);
145+
146+
}
147+
148+
public function delete($parameters)
149+
{
150+
151+
// Check Permission
152+
Helper::checkPermission();
153+
154+
// Check Request method
155+
$validRequests = array("DELETE");
156+
Helper::validRequest($validRequests);
157+
158+
// Expected parameters
159+
$expectedParameters = array("id");
160+
161+
// Check if all paremeters are correct
162+
self::checkRequest($parameters, $expectedParameters);
163+
164+
// Prepare query
165+
$paramOrder = "";
166+
$valuesOrder = "";
167+
168+
if($this->checkExists('id', $parameters["parameters"]['id']) == 0) { throw new Exception("Item does not exist."); }
169+
170+
foreach ($parameters["parameters"] as $key => $value) {
171+
172+
// Parameters order
173+
$paramOrder = $paramOrder.",".$key;
174+
// Values order
175+
if(is_numeric($value)) { $valuesOrder = $valuesOrder.",".$value.""; } else { $valuesOrder = $valuesOrder.",'".$value."'";}
176+
}
177+
178+
// Remove first comma
179+
$paramOrder = substr($paramOrder, 1);
180+
$valuesOrder = substr($valuesOrder, 1);
181+
182+
// final Query
183+
$addQuery = "DELETE FROM ".TABLE_PREFIX."sla ";
184+
$addQuery .= "WHERE id= ".$valuesOrder;
185+
186+
// Send query to be executed
187+
return $this->execQuery($addQuery);
188+
189+
}
190+
191+
public function checkRequest($parameters, $expectedParameters)
192+
{
193+
194+
// Error array
195+
$errors = array();
196+
197+
// Check if parameters is an array
198+
if(gettype($parameters["parameters"]) == 'array'){
199+
200+
// Check for empty fields
201+
foreach ($expectedParameters as $key => $value) {
202+
if(empty($parameters["parameters"][$value])) {
203+
array_push($errors,"Empty or Incorrect fields were given.");
204+
}
205+
}
206+
207+
// Check for unkown or unexpected fields
208+
foreach ($parameters["parameters"] as $key => $value) {
209+
if (!in_array($key, $expectedParameters)) {
210+
array_push($errors,"Unexpectec fields given.");
211+
}
212+
}
213+
214+
// If no errors, continue
215+
if(count($errors) > 0){
216+
throw new Exception("Empty or Incorrect fields were given, read documentation for more info.");
217+
}
218+
219+
} else {
220+
throw new Exception("Parameters must be an array.");
221+
}
222+
223+
}
224+
225+
private function checkExists($field, $value)
226+
{
227+
228+
// Connect Database
229+
$Dbobj = new DBConnection();
230+
$mysqli = $Dbobj->getDBConnect();
231+
232+
// Check if already exists
233+
$checkExists = $mysqli->query("SELECT * FROM ".TABLE_PREFIX."sla WHERE ".TABLE_PREFIX."sla.".$field." = '".$value."'");
234+
$numRows = $checkExists->num_rows;
235+
236+
return $numRows;
237+
238+
}
239+
240+
private function execQuery($string)
241+
{
242+
// Connect Database
243+
$Dbobj = new DBConnection();
244+
$mysqli = $Dbobj->getDBConnect();
245+
246+
// Check if already exists
247+
$insertRecord = $mysqli->query($string);
248+
249+
if($insertRecord)
250+
{
251+
return "Success! Row 1 affected.";
252+
} else {
253+
throw new Exception("Something went wrong.");
254+
}
255+
}
256+
96257
}
97258
?>

0 commit comments

Comments
 (0)