/[drupal]/contributions/modules/prodigem/prodigem.inc
ViewVC logotype

Contents of /contributions/modules/prodigem/prodigem.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Thu Oct 6 14:37:45 2005 UTC (4 years, 1 month ago) by breyten
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-4-6, DRUPAL-4-7
File MIME type: text/x-php
Initial import of prodigem module
1 <?php
2 // most of this is taken from PEP: http://prodigem.com/code/pep/pep.txt
3 // This object parses XML and turns it into an array
4 // Code for this object based from http://us2.php.net/xml_parse
5 class prodigemXML_Array_Producer
6 {
7 var $arr_output = array();
8 var $res_parser;
9 var $str_xml_data;
10
11 function Parse($str_input_xml) {
12 $this->res_parser = xml_parser_create();
13 xml_set_object($this->res_parser, $this);
14 xml_set_element_handler($this->res_parser, "TagOpen", "TagClosed");
15 xml_set_character_data_handler($this->res_parser, "TagData");
16 $this->str_xml_data = xml_parse($this->res_parser, $str_input_xml);
17
18 if (!$this->str_xml_data) {
19 die(sprintf("XML error: %s at line %d",
20 xml_error_string(xml_get_error_code($this->res_parser)),
21 xml_get_current_line_number($this->res_parser)));
22 }
23
24 xml_parser_free($this->res_parser);
25 return $this->arr_output;
26 }
27
28 function TagOpen($parser, $name, $attrs) {
29 $tag = array("name"=>$name, "attrs"=>$attrs);
30 array_push($this->arr_output, $tag);
31 }
32
33 function TagData($parser, $tag_data) {
34 if (trim($tag_data)) {
35 if (isset($this->arr_output[count($this->arr_output)-1]['tag_data'])) {
36 $this->arr_output[count($this->arr_output)-1]['tag_data'] .= $tag_data;
37 }
38 else {
39 $this->arr_output[count($this->arr_output)-1]['tag_data'] = $tag_data;
40 }
41 }
42 }
43
44 function TagClosed($parser, $name) {
45 $this->arr_output[count($this->arr_output)-2]['children'][] = $this->arr_output[count($this->arr_output)-1];
46 array_pop($this->arr_output);
47 }
48 }
49
50 // This function takes a response from Prodigem's REST API and returns only the XML from it
51 function _prodigem_get_rest_xml($buf) {
52 ereg("<\?xml.*<\/rsp>", $buf, $regs);
53 return $regs[0];
54 }
55
56 // This function calls into the Prodigem API and returns an XML_Array of the result
57 function prodigem_invoke_rest_api($rest_method, $data_array) {
58 $host = 'ssl4.westserver.net';
59 $method = 'POST';
60 $path = '/lerhaupt/prodigem/services/rest.php';
61 $data = "method=$rest_method";
62 $transport = function_exists('openssl_open') ? 'https://' : 'http://';
63 $port = function_exists('openssl_open') ? 443 : 80;
64 $prodigem_url = $transport . $host .':'. $port . $path;
65 $headers = array(
66 'Content-Type' => 'application/x-www-form-urlencoded'
67 );
68
69 foreach ($data_array as $key => $element) {
70 $data .= "&$key=$element";
71 }
72
73 $response = drupal_http_request($prodigem_url, $headers, $method, $data);
74
75 if (!isset($response->error)) {
76 $xml_rest = _prodigem_get_rest_xml($response->data);
77
78 $xml_object = new prodigemXML_Array_Producer();
79 return $xml_object->Parse($xml_rest);
80 }
81 else {
82 watchdog('http', $response->code .': '. $response->error);
83 }
84 }
85
86 // This function inspects an XML_Array and prints any errors
87 function prodigem_print_api_error($xml_array) {
88 if ($xml_array[0]['attrs']['STAT'] == 'ok') {
89 return 0;
90 }
91 else {
92 $output = "The Prodigem API call to method \"".$xml_array[0]['children'][0]['tag_data']."\" resulted in the following errors:\n";
93 $count = 1;
94 foreach ($xml_array[0]['children'][1]['children'] as $error_array) {
95 $output .= "$count. ".$error_array['attrs']['MSG']."\n";
96 $count++;
97 }
98 watchdog('error', $output, WATCHDOG_ERROR);
99 return 1;
100 }
101 }
102
103 // This function takes the top level of an XML_Array and returns an associative array
104 function make_associative_array($xml_array_portion) {
105 $return_array = array();
106
107 foreach ($xml_array_portion AS $sub_array) {
108 if ($sub_array['tag_data']) {
109 $return_array[$sub_array['name']] = $sub_array['tag_data'];
110 }
111 else if ($sub_array['attrs']) {
112 $return_array[$sub_array['name']] = $sub_array['attrs'];
113 }
114 }
115
116 return $return_array;
117 }
118 // api calls
119
120 // authentication
121
122 /* Starts a Prodigem session
123 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.auth.startSession
124 * @ingroup prodigem
125 */
126 function prodigem_api_start_session($username, $passwd) {
127 $data_array = array();
128 $data_array['username'] = $username;
129 $data_array['password'] = $passwd;
130 $return_array = array();
131 $xml_array = prodigem_invoke_rest_api('prodigem.auth.startSession', $data_array);
132
133 if (!prodigem_print_api_error($xml_array)) {
134 $xml_assoc = make_associative_array($xml_array[0]['children']);
135 $return_array['userid'] = $xml_assoc['USERID'];
136 $return_array['session'] = $xml_assoc['SESSION'];
137 return $return_array;
138 }
139 }
140
141 /* End a Prodigem session
142 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.auth.endSession
143 * @ingroup prodigem
144 */
145 function prodigem_api_end_session($user_id, $session) {
146 $data_array = array();
147 $data_array['userid'] = $user_id;
148 $data_array['session'] = $session;
149 $xml_array = prodigem_invoke_rest_api('prodigem.auth.endSession', $data_array);
150
151 if (!prodigem_print_api_error($xml_array)) {
152 $xml_assoc = make_associative_array($xml_array[0]['children']);
153 return 1;
154 }
155 }
156
157 // file upload
158 // upload_info is assoc. array
159 function _prodigem_api_upload_info($xml_assoc) {
160 $upload_info = array();
161
162 $upload_info['fileid'] = $xml_assoc['FILEID'];
163 $upload_info['filename'] = $xml_assoc['FILENAME'];
164 $upload_info['folderid'] = $xml_assoc['FOLDERID'];
165 $upload_info['foldername'] = $xml_assoc['FOLDERNAME'];
166
167 return $upload_info;
168 }
169
170 /* Upload a file to Prodigem
171 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.upload.postFile
172 * @ingroup prodigem
173 */
174 function prodigem_api_post_file($user_id, $session, $file_contents) {
175 $data_array = array();
176 $data_array['userid'] = $user_id;
177 $data_array['session'] = $session;
178 $data_array['file'] = $file_contents;
179 $xml_array = prodigem_invoke_rest_api('prodigem.upload.postFile', $data_array);
180
181 if (!prodigem_print_api_error($xml_array)) {
182 $xml_assoc = make_associative_array($xml_array[0]['children']);
183 return _prodigem_api_upload_info($xml_assoc);
184 }
185 }
186
187 /* This method can be used to pull content into your Prodigem account from a URL.
188 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.upload.grabURL
189 * @ingroup prodigem
190 */
191 function prodigem_api_grab_url($user_id, $session, $url) {
192 $data_array = array();
193 $data_array['userid'] = $user_id;
194 $data_array['session'] = $session;
195 $data_array['url'] = $url;
196 $xml_array = prodigem_invoke_rest_api('prodigem.upload.grabURL', $data_array);
197
198 if (!prodigem_print_api_error($xml_array)) {
199 $xml_assoc = make_associative_array($xml_array[0]['children']);
200 return _prodigem_api_upload_info($xml_assoc);
201 }
202 }
203
204 // file management
205
206 /* Move a file in your Prodigem account to a different folder.
207 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.files.moveFile
208 * @ingroup prodigem
209 */
210 function prodigem_api_move_file($user_id, $session, $file_id, $folder_id) {
211 $data_array = array();
212 $data_array['userid'] = $user_id;
213 $data_array['session'] = $session;
214 $data_array['fileid'] = $file_id;
215 $data_array['folderid'] = $folder_id;
216 $xml_array = prodigem_invoke_rest_api('prodigem.files.moveFile', $data_array);
217
218 return !prodigem_print_api_error($xml_array);
219 }
220
221
222 /* Delete a file in your Prodigem account.
223 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.files.deleteFile
224 * @ingroup prodigem
225 */
226 function prodigem_api_delete_file($user_id, $session, $file_id) {
227 $data_array = array();
228 $data_array['userid'] = $user_id;
229 $data_array['session'] = $session;
230 $data_array['fileid'] = $file_id;
231 $xml_array = prodigem_invoke_rest_api('prodigem.files.deleteFile', $data_array);
232
233 return !prodigem_print_api_error($xml_array);
234 }
235
236 // This function gets the users file list as an XML_Array
237
238 /* Get a list of files in your Prodigem account.
239 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.files.getListUser
240 * @ingroup prodigem
241 */
242 function prodigem_get_user_files($user_id, $session, $folder_id = 0) {
243 $return_array = array();
244 $response_array = array();
245 $data_array = array();
246 $data_array['userid'] = $user_id;
247 $data_array['session'] = $session;
248 if ($folder_id) {
249 $data_array['folderid'] = $folder_id;
250 }
251 $response_array = prodigem_invoke_rest_api('prodigem.files.getListUser', $data_array);
252
253 if (!prodigem_print_api_error($response_array)) {
254 foreach ($response_array[0]['children'][2]['children'] as $file) {
255 $current_file = array();
256
257 foreach ($file['attrs'] as $key => $val) {
258 $current_file[strtolower($key)] = $val;
259 }
260
261 $return_array[] = $current_file;
262 }
263
264 return $return_array;
265 }
266 }
267
268 // folder management
269
270 /* Create a folder in your Prodigem account.
271 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.folders.createFolder
272 * @ingroup prodigem
273 */
274 function prodigem_api_create_folder($user_id, $session, $folder_name) {
275 $data_array = array();
276 $return_array = array();
277 $data_array['userid'] = $user_id;
278 $data_array['session'] = $session;
279 $data_array['foldername'] = $folder_name;
280 $xml_array = prodigem_invoke_rest_api('prodigem.folders.createFolder', $data_array);
281
282 if (!prodigem_print_api_error($xml_array)) {
283 $xml_assoc = make_associative_array($xml_array[0]['children']);
284 $return_array['folderid'] = $xml_assoc['FOLDERID'];
285 $return_array['foldername'] = $xml_assoc['FOLDERNAME'];
286 return $return_array;
287 }
288 }
289
290 /* Delete a folder in your Prodigem account.
291 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.folders.deleteFolder
292 * @ingroup prodigem
293 */
294 function prodigem_api_delete_folder($user_id, $session, $folder_id) {
295 $data_array = array();
296 $data_array['userid'] = $user_id;
297 $data_array['session'] = $session;
298 $data_array['folderid'] = $folder_id;
299 $xml_array = prodigem_invoke_rest_api('prodigem.folders.deleteFolder', $data_array);
300
301 return !prodigem_print_api_error($xml_array);
302 }
303
304 /* Get a list of folders in your Prodigem account.
305 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.folders.getListUser
306 * @ingroup prodigem
307 */
308 function prodigem_api_get_user_folders($user_id, $session) {
309 $return_array = array();
310 $response_array = array();
311 $data_array = array();
312 $data_array['userid'] = $user_id;
313 $data_array['session'] = $session;
314 $response_array = prodigem_invoke_rest_api('prodigem.folders.getListUser', $data_array);
315
316 if (!prodigem_print_api_error($response_array)) {
317 foreach ($response_array[0]['children'][2]['children'] as $folder) {
318 $current_folder = array();
319
320 foreach ($folder['attrs'] as $key => $val) {
321 $current_folder[strtolower($key)] = $val;
322 }
323
324 $return_array[] = $current_folder;
325 }
326
327 return $return_array;
328 }
329 }
330
331 // torrent management
332
333 /* Create a torrent from the files in a folder.
334 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.torrents.createTorrent
335 * @ingroup prodigem
336 */
337 function prodigem_api_create_torrent($user_id, $session, $folder_id, $category_id, $torrent_hosted, $torrent_tags, $torrent_title, $torrent_description, $torrent_price, $license_type, $license_options, $series_id = 0) {
338 $return_array = array();
339 $data_array = array();
340 $torrent = array();
341 $data_array['userid'] = $user_id;
342 $data_array['session'] = $session;
343 $data_array['folderid'] = $folder_id;
344 $data_array['categoryid'] = $category_id;
345 $data_array['torrenthosted'] = $torrent_hosted;
346 if ($torrent_tags != '') {
347 $data_array['torrenttags'] = $torrent_tags;
348 }
349 $data_array['torrenttitle'] = $torrent_title;
350 $data_array['torrentdescription'] = $torrent_description;
351 $data_array['torrentprice'] = ($torrent_price != '') ? $torrent_price : '0.00';
352 $data_array['licensetype'] = $license_type;
353 foreach ($license_options as $key => $val) {
354 $data_array[$key] = $val;
355 }
356 if ($series_id > 0) {
357 $data_array['seriesid'] = $series_id;
358 }
359
360 $return_array = prodigem_invoke_rest_api('prodigem.torrents.createTorrent', $data_array);
361
362 if (!prodigem_print_api_error($return_array)) {
363 $return_assoc = make_associative_array($return_array[0]['children']);
364 $torrent['page'] = $return_assoc['TORRENTPAGE'];
365 $torrent['url'] = $return_assoc['TORRENTURL'];
366
367 return $torrent;
368 }
369 }
370
371 /* Edit a torrent in your Prodigem account.
372 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.torrents.editTorrent
373 * @ingroup prodigem
374 */
375 function prodigem_api_edit_torrent($user_id, $session, $torrent_id, $category_id, $torrent_hosted, $torrent_tags, $torrent_title, $torrent_description, $series_id = 0) {
376 $return_array = array();
377 $data_array = array();
378 $data_array['userid'] = $user_id;
379 $data_array['session'] = $session;
380 $data_array['torrentid'] = $torrent_id;
381 $data_array['categoryid'] = $category_id;
382 $data_array['torrenthosted'] = $torrent_hosted;
383 if ($torrent_tags != '') {
384 $data_array['torrenttags'] = $torrent_tags;
385 }
386 $data_array['torrenttitle'] = $torrent_title;
387 $data_array['torrentdescription'] = $torrent_description;
388 if ($series_id > 0) {
389 $data_array['seriesid'] = $series_id;
390 }
391
392 $return_array = prodigem_invoke_rest_api('prodigem.torrents.editTorrent', $data_array);
393
394 return !prodigem_print_api_error($return_array);
395 }
396
397 /* Delete a torrent in your Prodigem account.
398 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.torrents.deleteTorrent
399 * @ingroup prodigem
400 */
401 function prodigem_api_delete_torrent($user_id, $session, $torrent_id) {
402 $return_array = array();
403 $data_array = array();
404 $data_array['userid'] = $user_id;
405 $data_array['session'] = $session;
406 $data_array['torrentid'] = $torrent_id;
407
408 $return_array = prodigem_invoke_rest_api('prodigem.torrents.deleteTorrent', $data_array);
409
410 return !prodigem_print_api_error($return_array);
411 }
412
413 /* Orphan a torrent in your Prodigem account.
414 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.torrents.orphanTorrent
415 * @ingroup prodigem
416 */
417 function prodigem_api_orphan_torrent($user_id, $session, $torrent_id) {
418 $return_array = array();
419 $data_array = array();
420 $data_array['userid'] = $user_id;
421 $data_array['session'] = $session;
422 $data_array['torrentid'] = $torrent_id;
423
424 $return_array = prodigem_invoke_rest_api('prodigem.torrents.orphanTorrent', $data_array);
425
426 return !prodigem_print_api_error($return_array);
427 }
428
429 function _prodigem_api_parse_torrents($response_array) {
430 $return_array = array();
431
432 foreach ($response_array[0]['children'][2]['children'] as $torrent) {
433 $current_torrent = array();
434
435 foreach ($torrent['attrs'] as $key => $val) {
436 $current_torrent[strtolower($key)] = $val;
437 }
438
439 $return_array[] = $current_torrent;
440 }
441
442 return $return_array;
443 }
444
445 /* Get a listing of torrents in your account.
446 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.torrents.getListUser
447 * @ingroup prodigem
448 */
449 function prodigem_api_get_user_torrents($user_id, $session, $category_id = 0) {
450 $return_array = array();
451 $data_array = array();
452 $torrents = array();
453 $data_array['userid'] = $user_id;
454 $data_array['session'] = $session;
455 if ($category_id > 0) {
456 $data_array['categoryid'] = $category_id;
457 }
458
459 $return_array = prodigem_invoke_rest_api('prodigem.torrents.getListUser', $data_array);
460
461 if (!prodigem_print_api_error($return_array)) {
462 $torrents = _prodigem_api_parse_torrents($return_array);
463
464 return $torrents;
465 }
466 }
467
468 /* Get a public listing of torrents available on Prodigem.com.
469 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.torrents.getListPublic
470 * @ingroup prodigem
471 */
472 function prodigem_api_get_public_torrents($user_id, $category_id = 0) {
473 $return_array = array();
474 $data_array = array();
475 $torrents = array();
476 $data_array['userid'] = $user_id;
477 if ($category_id > 0) {
478 $data_array['categoryid'] = $category_id;
479 }
480
481 $return_array = prodigem_invoke_rest_api('prodigem.torrents.getListPublic', $data_array);
482
483 if (!prodigem_print_api_error($return_array)) {
484 $torrents = _prodigem_api_parse_torrents($return_array);
485
486 return $torrents;
487 }
488 }
489
490 // licenses
491
492 /* Get a listing of licenses to place your work under.
493 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.licenses.getListPublic
494 * @ingroup prodigem
495 */
496 function prodigem_api_get_licenses($license_type = '', $license_id = 0, $license_attribution = '', $license_commercialuse = '', $license_modifications = '', $license_sample = '') {
497 $return_array = array();
498 $response_array = array();
499 $data_array = array();
500 if ($license_type != '') {
501 $data_array['licensetype'] = $license_type;
502 }
503 if ($license_id > 0) {
504 $data_array['licenseid'] = $license_id;
505 }
506 if ($license_attribution != '') {
507 $data_array['licenseattribution'] = $license_attribution;
508 }
509 if ($license_commercialuse != '') {
510 $data_array['licensecommercialuse'] = $license_commercialuse;
511 }
512 if ($license_modifications != '') {
513 $data_array['licensemodifications'] = $license_modifications;
514 }
515 if ($license_sample != '') {
516 $data_array['licensesample'] = $license_sample;
517 }
518 $response_array = prodigem_invoke_rest_api('prodigem.licenses.getListPublic', $data_array);
519
520 if (!prodigem_print_api_error($response_array)) {
521 foreach ($response_array[0]['children'][2]['children'] as $license_group) {
522 $current_group = $license_group['attrs']['TYPE'];
523 foreach ($license_group['children'] as $license) {
524 $current_license = array();
525 foreach ($license['attrs'] as $key => $value) {
526 $current_license[strtolower($key)] = $value;
527 }
528 $current_license['licensetype'] = $current_group;
529 $return_array[$current_license['licenseid']] = $current_license;
530 }
531 }
532
533 return $return_array;
534 }
535 }
536
537 function prodigem_api_license_get_options($license) {
538 switch ($license['licensetype']) {
539 case 'licenses_cc':
540 return array(
541 'licenseattribution' => $license['licenseattribution'],
542 'licensecommercialuse' => $license['licensecommercialuse'],
543 'licensemodifications' => $license['licensemodifications']
544 );
545 break;
546 case 'licenses_cc_sampling':
547 return array(
548 'licensesample' => $license['licensesample'],
549 );
550 break;
551 default:
552 $lname = $license['licensename'];
553 if (isset($license['licensesubname'])) {
554 $lname .= '/'. $license['licensesubname'];
555 }
556 return array(
557 'licenseothername' => $lname,
558 'licenseotherurl' => $license['licenseurl']
559 );
560 break;
561 }
562 }
563
564 function prodigem_api_licenses2assoc($licenses) {
565 $ret = array();
566
567 foreach ($licenses as $license) {
568 $ret[$license['licenseid']] = $license['licensename'] .'/'. $license['licensesubname'];
569 }
570
571 natsort($ret);
572 return $ret;
573 }
574
575 function prodigem_get_licenses() {
576 if ($cached = cache_get('prodigem_licenses')) {
577 return unserialize($cached);
578 }
579 else {
580 $licenses = prodigem_api_get_licenses();
581 cache_set('prodigem_licenses', serialize($licenses));
582 return $licenses;
583 }
584 }
585
586 // categories
587
588 /* Get a listing of categories for torrents.
589 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.categories.getListPublic
590 * @ingroup prodigem
591 */
592 function prodigem_api_get_categories($category_id = 0) {
593 $return_array = array();
594 $response_array = array();
595 $data_array = array();
596 if ($category_id > 0) {
597 $data_array['categoryid'] = $category_id;
598 }
599 $response_array = prodigem_invoke_rest_api('prodigem.categories.getListPublic', $data_array);
600
601 if (!prodigem_print_api_error($response_array)) {
602 foreach ($response_array[0]['children'][2]['children'] as $category) {
603 $category_attrs = $category['attrs'];
604 $return_array[$category_attrs['CATEGORYID']] = $category_attrs['CATEGORYNAME'];
605 }
606
607 return $return_array;
608 }
609 }
610
611 function prodigem_get_categories($category_id = 0) {
612 if ($category_id) {
613 return prodigem_api_get_categories($category_id);
614 }
615 else {
616 if ($cached = cache_get('prodigem_categories')) {
617 return unserialize($cached);
618 }
619 else {
620 $categories = prodigem_api_get_categories();
621 cache_set('prodigem_categories', serialize($categories));
622 return $categories;
623 }
624 }
625 }
626
627 // series
628
629 /* Get a listing of series in your Prodigem account.
630 * @sa http://torrentocracy.com/mediawiki/index.php/Prodigem.series.getListUser
631 * @ingroup prodigem
632 */
633 function prodigem_api_get_user_series($user_id, $session, $series_id = 0) {
634 $return_array = array();
635 $response_array = array();
636 $data_array = array();
637 $data_array['userid'] = $user_id;
638 $data_array['session'] = $session;
639 if ($series_id > 0) {
640 $data_array['seriesid'] = $series_id;
641 }
642 $response_array = prodigem_invoke_rest_api('prodigem.series.getListUser', $data_array);
643
644 if (!prodigem_print_api_error($response_array)) {
645 foreach ($response_array[0]['children'][2]['children'] as $series) {
646 $current_series = array();
647
648 foreach ($series['attrs'] as $key => $val) {
649 $current_series[strtolower($key)] = $val;
650 }
651
652 $return_array[] = $current_series;
653 }
654
655 return $return_array;
656 }
657 }

  ViewVC Help
Powered by ViewVC 1.1.2