This repository was archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtor.php
More file actions
106 lines (94 loc) · 2.95 KB
/
tor.php
File metadata and controls
106 lines (94 loc) · 2.95 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
<?php
//check if curl installed
if (!function_exists('curl_init'))
throw new TorApiException("lib curl missing");
class TorApi
{
var $api_key = "";
var $api_token = "";
var $api_version = "1";
var $server_url = "";
var $project_domain = "";
public function __construct($api_key, $project_domain) {
$this->api_key = $api_key;
$this->project_domain = $project_domain;
$this->server_url = "http://api.ticketonrails.com";
$this->api_token = md5($project_domain.md5($api_key));
}
public function request($url, $method, $params, $attachment = null) {
$request_url = $this->server_url . "/v" . $this->api_version . $url;
$params["token"] = $this->api_token;
$ch = curl_init();
switch($method) {
case "GET":
curl_setopt($ch, CURLOPT_URL, $request_url."?".http_build_query($params, '', '&'));
break;
case "POST":
if ($attachment) {
$postParams = array();
foreach ($params as $fieldName => $fieldValue) {
$postParams[$fieldName] = $fieldValue;
}
$postParams["attachment"] = "@".$attachment;
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
} else {
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params, '', '&'));
}
break;
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
curl_close($ch);
//check status code or response for errors
if($httpcode >= 400) {
$json = json_decode($result, TRUE);
throw new TorApiException(sprintf("%s", $json["error"]));
}
else {
return json_decode($result, TRUE);
}
}
public function new_ticket($values = null) {
$params = array();
$ticket = array();
$attachment = null;
if($values){
# sending only what is necessary
$ticket_params = array("email", "from_name", "subject", "body", "html", "date", "labels");
foreach ($ticket_params as $param) {
if (array_key_exists($param, $values)) {
$ticket[$param] = $values[$param];
}
}
if (array_key_exists("attachment", $values)) {
$attachment = $values["attachment"];
}
}
$params["ticket"] = json_encode($ticket);
$response = $this->request("/tickets", "POST", $params, $attachment);
return $response;
}
public function get_tickets($page = 1, $limit = 20) {
$params = array(
"page" => $page,
"limit" => $limit
);
$response = $this->request("/tickets", "GET", $params, null);
return $response;
}
public function get_ticket($ticketId) {
$response = $this->request("/tickets/".$ticketId, "GET", array(), null);
return $response;
}
}
class TorApiException extends Exception { }
?>