-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsCloud_SDK.php
More file actions
248 lines (197 loc) · 6.95 KB
/
sCloud_SDK.php
File metadata and controls
248 lines (197 loc) · 6.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
sCloud SDK ,uses oauth .02 for authorization and to generate access tokens, then can be used for API calls.
* refer to https://developers.scloud.live/Developer/Documentation/ for more information.
*/
session_start();
///error_reporting(E_ALL);
//skycloud class (Minimal SDK.)
class sCloud {
//SKYCLOUD (sCloud) SDK CLASS.
private $api_key;
private $age;
private $request;//used to hold the request from our application call.
private $app_secret;//app secret that you got from developer.scloud.live when you created an applicaiton on scloud develpers website.
//define some of scloud's error codes later.
function __construct( $api_key,$app_secret, $age,$request ) {
$this->request = $api_key;
$this->api_key = $app_secret;
$this->age = $age;
$this->request = $request;
}
public function getconfig(){
include'Config.php';
//other functions will use these config settings via $this->getconfig();
//if any varables are added to the config.php then they also need to be declared on the json call below so they can be used.
///$makejsonconfog = array('SkycloudClientID'=>$SkycloudClientID, "baseURL"=>$baseURL);
$jsonobj = '{"SkycloudClientID":"'.$SkycloudClientID.'","baseURL":"'.$baseURL.'","authorizeURL":"'.$authorizeURL.'","SkycloudClientSecret":"'.$SkycloudClientSecret.'","Api_url":"'.$apiURLBase.'","token_url":"'.$tokenURL.'"}';
$obj = json_decode($jsonobj);
return $obj;//json_encode($makejsonconfog);//$SkycloudClientID;
}
/////generate a authorzation URL to the sCloud authorization server.
function generateAuthUrl($whatlink){
$SkycloudClientID=$this->getconfig()->SkycloudClientID;
$baseURL=$this->getconfig()->baseURL;
$authorizeURL=$this->getconfig()->authorizeURL;
$_SESSION['state'] = bin2hex(random_bytes(16));
$params = array(
'response_type' => 'code',
'client_id' => $SkycloudClientID,
'redirect_uri' => $baseURL,
'scope' => 'user Images',
'state' => $_SESSION['state']
);
// Redirect the user to Skycloud's authorization page
if($whatlink==1){
$genlink='<a href="'.$authorizeURL.'?'.http_build_query($params).'">Authorize</a>';//authorization link to authorate scloud api with a user account.
}
else if($whatlink==0){
$genlink=$authorizeURL.'?'.http_build_query($params);//authorization link to authorate scloud api with a user account.
}
else{
$genlink="error 0 or 1 must be set";//custom error
}
return $genlink;
//header('Location: '.$authorizeURL.'?'.http_build_query($params));
//die();
}
///Auth token info
function AuthToken(){
if(isset($_SESSION['access_token'])){
$token=$_SESSION['access_token'];
}
else{
$token="No token is set, try to authorize a user.";
}
return $token;
}
//USED TO MAKE API REQUEST TO SKYCLOUD
//ACTUAL API CALL FUNCTION.
function API($url, $post=FALSE, $options=FALSE, $headers=array()) {
//echo 'API-CALL::'.$url;//DEBUG
//connect config varables so we can use them in this function.
$SkycloudClientID=$this->getconfig()->SkycloudClientID;
$baseURL=$this->getconfig()->baseURL;
$authorizeURL=$this->getconfig()->authorizeURL;
$SkycloudClientSecret=$this->getconfig()->SkycloudClientSecret;
$api_url=$this->getconfig()->Api_url;
$token_url=$this->getconfig()->token_url;
//END OF CONFIG VARABLES.
//post values.
if($post){
//user custom post might need to combine with below post aswell
//get POST VALUE
//BUILD FROM USER ARRAY
//HERE WE ARE CHECKING FOR SCLOUD API METHODS BUT ONLY ONES WITH A POST PARAM WILL ACTUALLY REACH THIS SECTION OF CODE.
//cloud_upload method
if($url=='cloud_upload'){
if($options!=FALSE){
//lets check if the mentioned encryption type is a valid scloud encryption type.
if($options=="AES-256"){
}
else{
echo 'Error unrecognised encryption type, please try using AES-256';
exit();
}
if(count($post)==1){
//single file found
$post=array(
'filename' => $post[0],//single file
'encryption_type' => $option
);
}
elseif(count($post)>1){
//multiple files found to lets uploas em all.
//LOOP THROUGH ALL POST THE $POST VAR
$post=array(
'filename' => $post[0],//single file
'encryption_type' => $option
);
}
}
else{
$error=1;//no encrytion type found
echo 'Error no encryption type found, please try using AES-256';
exit();
}
}
//account_create method
if($url=='account_create'){
$post=array(
'email' => $post[0],
'username' => $post[1],
'password' => $post[2],
'fname' => $post[3],
'lname' => $post[4],
'ref_id' => $post[5],
'iso_lang' => $post[6]
);
// print_r($post);
}
}
else{
//THIS IS THE DEFAULT POST PARAMS USIALLY FOR THE OAUTH 2.0 PROTOCOL
//default post
$post=array(
'grant_type' => 'authorization_code',
'client_id' => $SkycloudClientID,
'client_secret' => $SkycloudClientSecret,
'redirect_uri' => $baseURL,
'code' => $_GET['code']
);
}
//URL SWITCHER
if($url=='token'){
if(isset($_SESSION['access_token'])){
//a toekn has already been set delrte this current ne first.
echo 'an access token is already set<br />Token:'.$_SESSION['access_token'].'<br />';
}
else{
//echo 'no previous Token:'.$_SESSION['access_token'].'<br />';
$url=$this->getconfig()->token_url;
}
}
else{
$url=$this->getconfig()->Api_url.$url;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if($post)
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$headers = [
'Accept: application/skycloud.pro+json, application/json',
'User-Agent: https://example-app.com/'
];
if(isset($_SESSION['access_token'])){
//echo 'Authorization: Bearer '.$_SESSION['access_token'];
$headers[] = 'Authorization: Bearer '.$_SESSION['access_token'];
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
if($url==$this->getconfig()->token_url){
////STORE TOKEN INSIDE OUR CLASS so right here
//echo 'poo';
$gettoken=json_decode($response, true);
$_SESSION['access_token'] =$gettoken['access_token'];//store the access token in a session
//END OF SETTING TOKEN IN A SESSION INSIDE OUR CLASS.
}
return json_decode($response, true);
}
////////////////////////////////////////
function Cloud($options) {
//this function will get all user cloud info
//we will use file get contents with GET methods
//But our main option will use curl for post and get methods
//$options;//optons can hold request call..cloud..accounts..security orwhatever
//HOLDSKYCLOUD API ENDPOINTS HERE...
// $url = "https://scloud.live/Apiv1/API.php?api_key=t5hhrhrrhfhv";//default end point - must be https
//$json = file_get_contents($url);
//$skycloud = json_decode($json, true);
//print_r($json_data);
//return $skycloud[cloud][storage_used];
//echo $storage_used;
return $skycloud;
}
}
?>