This repository was archived by the owner on Sep 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgett.class.php
More file actions
173 lines (150 loc) · 4.21 KB
/
gett.class.php
File metadata and controls
173 lines (150 loc) · 4.21 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
<?php
set_time_limit(0);
error_reporting(E_ALL);
/**
* ge.tt PHP Library
* Easily interact with ge.tt API via PHP.
* Website: http://www.spanlayer.com
*
* @version 1.0.0
* @author Ahmed Hosny <admin@spanlayer.com>, Jacob Groß / kurtextrem <kurtextrem@gmail.com>
*
* @license This library is Free for everyone to use or modify,
* Keeping the original credits,
* Appending notes and credits for any modifications.
*/
class gett {
/**
* Your ge.tt API Key
*
* @var string
*/
private $APIKey;
/**
* ge.tt Access Token
*
* @var mixed
*/
private $accessToken;
/**
* Loads the "Request" Lib
*
* @param string $APIKey
*/
public function __construct($APIKey, $email, $password) {
session_start();
require_once 'libraries/Requests.php';
Requests::register_autoloader();
$this->APIKey = $APIKey;
$this->auth($email, $password);
}
/**
* REST API Connection to ge.tt
*
* @param string $method
* @param array $postData
* @return mixed
* @throws Exception
*/
public function request($method, $postData = array()) {
$post_data_str = json_encode($postData);
$this->accessToken = $this->accessToken ? $this->accessToken : (isset($_SESSION['gett_accessToken']) ? $_SESSION['gett_accessToken'] : false);
$accessToken = $this->accessToken ? '?accesstoken=' . $this->accessToken : '';
try {
if (is_array($postData) && count($postData) > 0) {
$request = Requests::post('https://open.ge.tt/1/' . $method . $accessToken, array(), $post_data_str);
} else {
$request = Requests::get('https://open.ge.tt/1/' . $method . $accessToken);
}
} catch (Exception $e) {
$this->handleException($e);
}
$response = json_decode($request->body);
try {
if (isset($response->error) || !$request->success)
throw new Exception('<i>Error:</i> <strong>' . $response->error . '</strong>', E_USER_ERROR);
if (is_null($response))
throw new Exception('<strong>Unknown method <i>(' . $method . ')</i> or <span style="color: white; background-color: lightblue">GE.TT</span> API is down!</strong>', E_USER_ERROR);
} catch (Exception $e) {
$this->handleException($e);
}
return $response;
}
/**
* Creates a new share
*
* @param string $title
* @return mixed
*/
public function new_share($title) {
return $this->request('shares/create', array('title' => $title));
}
/**
* Returns data from a share
*
* @param string $title
* @return mixed
*/
public function get_share($title = '') {
if (!empty($title))
$title = '/' . $title;
return $this->request('shares' . $title);
}
/**
* Renames a share
*
* @param string $title
* @return mixed
*/
public function rename_share($oldTitle, $newTitle) {
return $this->request('shares/' . $oldTitle . '/update', array('title' => $newTitle));
}
/**
* Deletes a share
*
* @param string $title
* @return mixed
*/
public function delete_share($title) {
return $this->request('shares/' . $title . '/destroy'); // alternative would be https://open.ge.tt/1/doc/rest#shares/{sharename}/update
}
/**
* Auth process
*
* @param string $email
* @param string $password
* @return bool
* @throws Exception
*/
private function auth($email, $password) {
try {
if (isset($_SESSION['gett_accessToken']) || !empty($this->accessToken))
return true;
// throw new Exception($email . ' is already authed.'); // There's no problem if user is already authed
} catch (Exception $e) {
$this->handleException($e);
}
$user = $this->request('users/login', array(
'apikey' => $this->APIKey,
'email' => $email,
'password' => $password
));
try {
if(!isset($user->accesstoken))
throw new Exception('Authentication failed !', E_USER_ERROR);
$_SESSION['gett_accessToken'] = $user->accesstoken;
$this->accessToken = $user->accesstoken;
} catch (Exception $e) {
$this->handleException($e);
}
return true;
}
/**
* Handles caught exceptions
*
* @param Exception $e
*/
private function handleException($e) {
echo '<div class="gettError"><span style="color: white; background-color: lightblue">GE.TT</span> ' . $e->getMessage() . '<br><code>in <b>' . $e->getFile() . '</b> on line <b>' . $e->getLine() . '</b></code></div>';
}
}