-
Notifications
You must be signed in to change notification settings - Fork 20
/
scribd.php
298 lines (264 loc) · 8.63 KB
/
scribd.php
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/* Unofficial Scribd PHP Class library */
class Scribd {
public $api_key;
public $secret;
private $url;
public $session_key;
public $my_user_id;
private $error;
public function __construct($api_key, $secret) {
$this->api_key = $api_key;
$this->secret = $secret;
$this->url = "http://api.scribd.com/api?api_key=" . $api_key;
}
/**
* Upload a document from a file
* @param string $file : relative path to file
* @param string $doc_type : PDF, DOC, TXT, PPT, etc.
* @param string $access : public or private. Default is Public.
* @param int $rev_id : id of file to modify
* @return array containing doc_id, access_key, and secret_password if nessesary.
*/
public function upload($file, $doc_type = null, $access = null, $rev_id = null){
$method = "docs.upload";
$params['doc_type'] = $doc_type;
$params['access'] = $access;
$params['file'] = "@".$file;
$result = $this->postRequest($method, $params);
return $result;
}
/**
* Upload a document from a Url
* @param string $url : absolute URL of file
* @param string $doc_type : PDF, DOC, TXT, PPT, etc.
* @param string $access : public or private. Default is Public.
* @return array containing doc_id, access_key, and secret_password if nessesary.
*/
public function uploadFromUrl($url, $doc_type = null, $access = null, $rev_id = null){
$method = "docs.uploadFromUrl";
$params['url'] = $url;
$params['access'] = $access;
$params['rev_id'] = $rev_id;
$params['doc_type'] = $doc_type;
$data_array = $this->postRequest($method, $params);
return $data_array;
}
public function getDownloadUrl($doc_id,$doc_type)
{
$method="docs.getDownloadUrl";
$params['doc_id']=$doc_id;
$params['doc_type']=$doc_type;
$data_array = $this->postRequest($method, $params);
return $data_array;
}
/**
* Get a list of the current users files
* @return array containing doc_id, title, description, access_key, and conversion_status for all documents
*/
public function getList(){
$method = "docs.getList";
$result = $this->postRequest($method, $params);
return $result['resultset'];
}
/**
* Get the current conversion status of a document
* @param int $doc_id : document id
* @return string containing DISPLAYABLE", "DONE", "ERROR", or "PROCESSING" for the current document.
*/
public function getConversionStatus($doc_id){
$method = "docs.getConversionStatus";
$params['doc_id'] = $doc_id;
$result = $this->postRequest($method, $params);
return $result['conversion_status'];
}
/**
* Get settings of a document
* @return array containing doc_id, title , description , access, tags, show_ads, license, access_key, secret_password
*/
public function getSettings($doc_id){
$method = "docs.getSettings";
$params['doc_id'] = $doc_id;
$result = $this->postRequest($method, $params);
return $result;
}
/**
* Change settings of a document
* @param array $doc_ids : document id
* @param string $title : title of document
* @param string $description : description of document
* @param string $access : private, or public
* @param string $license : "by", "by-nc", "by-nc-nd", "by-nc-sa", "by-nd", "by-sa", "c" or "pd"
* @param string $access : private, or public
* @param array $show_ads : default, true, or false
* @param array $tags : list of tags
* @return string containing DISPLAYABLE", "DONE", "ERROR", or "PROCESSING" for the current document.
*/
public function changeSettings($doc_ids, $title = null, $description = null, $access = null, $license = null, $parental_advisory = null, $show_ads = null, $tags = null){
$method = "docs.changeSettings";
$params['doc_ids'] = $doc_ids;
$params['title'] = $title;
$params['description'] = $description;
$params['access'] = $access;
$params['license'] = $license;
$params['show_ads'] = $show_ads;
$params['tags'] = $tags;
$result = $this->postRequest($method, $params);
return $result;
}
/**
* Delete a document
* @param int $doc_id : document id
* @return 1 on success;
*/
public function delete($doc_id){
$method = "docs.delete";
$params['doc_id'] = $doc_id;
$result = $this->postRequest($method, $params);
return $result;
}
/**
* Search the Scribd database
* @param string $query : search query
* @param int $num_results : number of results to return (10 default, 1000 max)
* @param int $num_start : number to start from
* @param string $scope : scope of search, "all" or "user"
* @return array of results, each of which contain doc_id, secret password, access_key, title, and description
*/
public function search($query, $num_results = null, $num_start = null, $scope = null){
$method = "docs.search";
$params['query'] = $query;
$params['num_results'] = $num_results;
$params['num_start'] = $num_start;
$params['scope'] = $scope;
$result = $this->postRequest($method, $params);
return $result['result_set'];
}
/**
* Login as a user
* @param string $username : username of user to log in
* @param string $password : password of user to log in
* @return array containing session_key, name, username, and user_id of the user;
*/
public function login($username, $password){
$method = "user.login";
$params['username'] = $username;
$params['password'] = $password;
$result = $this->postRequest($method, $params);
$this->session_key = $result['session_key'];
return $result;
}
/**
* Sign up a new user
* @param string $username : username of user to create
* @param string $password : password of user to create
* @param string $email : email address of user
* @param string $name : name of user
* @return array containing session_key, name, username, and user_id of the user;
*/
public function signup($username, $password, $email, $name = null){
$method = "user.signup";
$params['username'] = $username;
$params['password'] = $password;
$params['name'] = $name;
$params['email'] = $email;
$result = $this->postRequest($method, $params);
return $result;
}
private function postRequest($method, $params){
//echo "Function Called";
$params['method'] = $method;
$params['session_key'] = $this->session_key;
$params['my_user_id'] = $this->my_user_id;
//echo "First Step";
//print_r($params);
$post_params = array();
foreach ($params as $key => &$val) {
if(!empty($val)){
if (is_array($val)) $val = implode(',', $val);
if($key != 'file' && substr($val, 0, 1) == "@"){
$val = chr(32).$val;
}
$post_params[$key] = $val;
}
}
$secret = $this->secret;
$post_params['api_sig'] = $this->generate_sig($params, $secret);
//echo "<br>";
//print_r($post_params);
$request_url = $this->url;
// echo "<br>";
// echo $request_url;
//echo "Second step";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params );
$xml = curl_exec( $ch );
//echo "<br>";
//print_r(curl_error($ch));
echo "<br>";
$result = simplexml_load_string($xml);
//print_r($result);
//echo "<br> Hurry";
//echo "Fourth Step";
curl_close($ch);
//echo "Fifth Step";
//echo $result['stat'];
//echo "Sixth";
if($result['stat'] == 'fail'){
//This is ineffecient.
//echo "Failed";
$error_array = (array)$result;
$error_array = (array)$error_array;
$error_array = (array)$error_array['error'];
$error_array = $error_array['@attributes'];
$this->error = $error_array['code'];
throw new Exception($error_array['message'], $error_array['code']);
return 0;
}
if($result['stat'] == "ok"){
//echo "Fifth Step";
//This is shifty. Works currently though.
$result = $this->convert_simplexml_to_array($result);
$tresult=implode("",$result);
if(urlencode($tresult) == "%0A%0A" && $this->error == 0){
$result = "1";
return $result;
}else{
return $result;
}
}
}
public static function generate_sig($params_array, $secret) {
$str = '';
ksort($params_array);
// Note: make sure that the signature parameter is not already included in
// $params_array.
foreach ($params_array as $k=>$v) {
$str .= $k . $v;
}
$str = $secret . $str;
return md5($str);
}
public static function convert_simplexml_to_array($sxml) {
$arr = array();
if ($sxml) {
foreach ($sxml as $k => $v) {
if(!empty($arr[$k])){
echo "Entered";
$arr[$k." ".(count($arr) + 1)] = self::convert_simplexml_to_array($v);
}else{
$arr[$k] = self::convert_simplexml_to_array($v);
}
}
}
if (sizeof($arr) > 0) {
return $arr;
} else {
return (string)$sxml;
}
}
}
?>