-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgp-api.php
More file actions
138 lines (121 loc) · 4.46 KB
/
gp-api.php
File metadata and controls
138 lines (121 loc) · 4.46 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
<?php
require_once 'util.php';
require_once 'PhpRestClient/SimpleRestClient.php';
class GpAPI {
private $baseurl = null;
private $restclient = null;
private $format = null;
const DEFAULT_API = 'http://api.greenpride.com/Service.svc/';
public static function gphash($str) {
return urlencode(base64_pad(base64_encode(hash('sha512', utf8_encode($str), 1))));
}
public function __construct($baseurl = GpAPI::DEFAULT_API, $format='json') {
$this->baseurl = $baseurl;
$cert_file=null;//Path to cert file
$key_file=null;//Path to private key
$key_password=null;//Private key passphrase
$curl_opts=null;//Array to set additional CURL options or override the default options of the SimpleRestClient
$post_data=null;//Array or string to set POST data
$user_agent = 'mGP';
$this->restclient = new SimpleRestClient($cert_file, $key_file, $key_password, $user_agent, $curl_opts);
$this->format = $format;
}
public function handleError($code) {
echo 'Error: '.$code.'<BR>';
}
public function Hash($string) {
$string = urlencode($string);
$url = $this->baseurl."Hash?format=".$this->format."&Value=$string&URLEncode=True";
return $this->__handle_url($url);
}
public function Authenticate($username, $password, $alreadyHashed=FALSE) {
$hashedpw = $alreadyHashed ? $password : GpAPI::gphash($password);
$url = $this->baseurl."Authenticate?UserName=$username&Password=$hashedpw&format=".$this->format;
return $this->__handle_url($url);
}
public function UserGet($username, $password, $alreadyHashed=FALSE) {
$hashedpw = $alreadyHashed ? $password : GpAPI::gphash($password);
$url = $this->baseurl."UserGet?UserName=$username&Password=$hashedpw&format=".$this->format;
return $this->__handle_url($url);
}
private function __handle_url($url) {
$this->restclient->getWebRequest($url);
if($this->restclient->getStatusCode() == 200) {
return json_decode($this->restclient->getWebResponse(), TRUE);
}
else {
$this->handleError($this->restclient->getStatusCode());
}
return null;
}
public function Posts($tid='',$pid='',$tlimit='') {
$username = $_SESSION['username'];
$pwhash = $_SESSION['pwhash'];
$url = $this->baseurl."Posts?UserName=$username&Password=$pwhash&format=".$this->format;
if(!empty($tid)) {
$url .= '&ThreadID='.$tid;
}
if(!empty($pid)) {
$url .= '&PostID='.$pid;
}
if(!empty($tlimit)) {
$url .= '&ThreadLimit='.$tlimit;
}
return $this->__handle_url($url);
}
public function PostGet($pid) {
$username = $_SESSION['username'];
$pwhash = $_SESSION['pwhash'];
$url = $this->baseurl."PostGet?UserName=$username&Password=$pwhash&format=".$this->format;
if(!empty($pid)) {
$url .= '&PostID='.$pid;
}
return $this->__handle_url($url);
}
public function PostAdd($postid, $subject, $description) {
$description = urlencode($description);
$subject = urlencode($subject);
$username = $_SESSION['username'];
$pwhash = $_SESSION['pwhash'];
$url = $this->baseurl."PostAdd?UserName=$username&Password=$pwhash&format=".$this->format;
$url .= "&Subject=$subject&Description=$description";
if(!empty($postid)) {
$url .= '&ReplyToID='.$postid;
}
return $this->__handle_url($url);
}
public function PostMarkAs($read, $threadid, $postid) {
$username = $_SESSION['username'];
$pwhash = $_SESSION['pwhash'];
$url = $this->baseurl."PostMarkAs?UserName=$username&Password=$pwhash&format=".$this->format;
if($read) {
$url .= '&Read=True';
}
else {
$url .= '&Read=False';
}
if(!empty($threadid)) {
$url .= '&ThreadID='.$threadid;
}
else if(!empty($postid)) {
$url .= '&PostID='.$postid;
}
return $this->__handle_url($url);
}
public function PostCountUnread() {
$username = $_SESSION['username'];
$pwhash = $_SESSION['pwhash'];
$url = $this->baseurl."PostCountUnread?UserName=$username&Password=$pwhash&format=".$this->format;
return $this->__handle_url($url);
}
public function PostNextUnreadID($threadID=0) {
$username = $_SESSION['username'];
$pwhash = $_SESSION['pwhash'];
$url = $this->baseurl."PostNextUnreadID?UserName=$username&Password=$pwhash&format=".$this->format;
if(!empty($threadID)) {
$url .= "&ThreadID=$threadID";
}
return $this->__handle_url($url);
}
}
?>