res_parser = xml_parser_create(); xml_set_object($this->res_parser, $this); xml_set_element_handler($this->res_parser, "TagOpen", "TagClosed"); xml_set_character_data_handler($this->res_parser, "TagData"); $this->str_xml_data = xml_parse($this->res_parser, $str_input_xml); if(!$this->str_xml_data) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->res_parser)), xml_get_current_line_number($this->res_parser))); } xml_parser_free($this->res_parser); return $this->arr_output; } function TagOpen($parser, $name, $attrs) { $tag = array("name"=>$name, "attrs"=>$attrs); array_push($this->arr_output, $tag); } function TagData($parser, $tag_data) { if(trim($tag_data)) { if(isset($this->arr_output[count($this->arr_output)-1]['tag_data'])) { $this->arr_output[count($this->arr_output)-1]['tag_data'] .= $tag_data; } else { $this->arr_output[count($this->arr_output)-1]['tag_data'] = $tag_data; } } } function TagClosed($parser, $name) { $this->arr_output[count($this->arr_output)-2]['children'][] = $this->arr_output[count($this->arr_output)-1]; array_pop($this->arr_output); } } // This function allows us to do an HTTP (or https) POST or GET // Code for this function modified from http://www.faqts.com/knowledge_base/view.phtml/aid/12039/fid/51 function send_to_host($host, $method = 'GET', $path, $data, $port = 80, $transport = 'tcp://', $user_agent) { $method = strtoupper($method); $fp = fsockopen($transport.$host, $port); if ($method == 'GET') { $path .= '?' . $data; } fputs($fp, "$method $path HTTP/1.0\r\n"); fputs($fp, "Host: $host\r\n"); fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n"); if ($method == 'POST') { fputs($fp, "Content-length: " . strlen($data) . "\r\n"); } fputs($fp, "User-Agent: $user_agent\r\n"); fputs($fp, "Connection: close\r\n\r\n"); if ($method == 'POST') { fputs($fp, $data); } while (!feof($fp)) { $buf .= fgets($fp, 128); } fclose($fp); return $buf; } // This function takes a response from Prodigem's REST API and returns only the XML from it function get_rest_xml($buf) { ereg("<\?xml.*<\/rsp>", $buf, $regs); return $regs[0]; } // This function calls into the Prodigem API and returns an XML_Array of the result function invoke_prodigem_rest_api($rest_method, $data_array) { $host = 'ssl4.westserver.net'; $method = 'POST'; $path = '/lerhaupt/prodigem/services/rest.php'; $data = "method=$rest_method"; $port = 443; $transport = 'ssl://'; $user_agent = 'Prodigem Enclosure Puller '.PEP_VERSION; foreach ($data_array as $key => $element) { $data .= "&$key=$element"; } $buf = send_to_host($host, $method, $path, $data, $port, $transport, $user_agent); $xml_rest = get_rest_xml($buf); $xml_object = new XML_Array_Producer(); return $xml_object->Parse($xml_rest); } // This function inspects an XML_Array and prints any errors function print_api_error($xml_array) { if ($xml_array[0]['attrs']['STAT'] == 'ok') { return 0; } else { print("\nThe Prodigem API call to method \"".$xml_array[0]['children'][0]['tag_data']. "\" resulted in the following errors:\n"); $count = 1; foreach($xml_array[0]['children'][1]['children'] as $error_array) { print("$count. ".$error_array['attrs']['MSG']."\n"); $count++; } return 1; } } // This function takes the top level of an XML_Array and returns an associative array function make_associative_array($xml_array_portion) { $return_array = array(); foreach($xml_array_portion AS $sub_array) { if ($sub_array['tag_data']) { $return_array[$sub_array['name']] = $sub_array['tag_data']; } else if ($sub_array['attrs']) { $return_array[$sub_array['name']] = $sub_array['attrs']; } } return $return_array; } // This function gets an RSS 2.0 feed function get_rss_feed(&$xml_rss) { $url_handle = fopen(RSS_URL, 'r'); if ($url_handle) { while (!feof($url_handle)) { $xml_rss .= fgets($url_handle, 4096); } fclose($url_handle); } else { die('Failed to open '.RSS_URL); } } // This function signs into Prodigem function prodigem_sign_in(&$user_id, &$session) { $data_array = array(); $data_array['username'] = PRODIGEM_USERNAME; $data_array['password'] = PRODIGEM_PASSWORD; $xml_array = invoke_prodigem_rest_api('prodigem.auth.startSession', $data_array); if(print_api_error($xml_array)) { die(); } $xml_assoc = make_associative_array($xml_array[0]['children']); $user_id = $xml_assoc['USERID']; $session = $xml_assoc['SESSION']; } // This function gets the users file list as an XML_Array function prodigem_get_user_files($user_id, $session) { $return_array = array(); $data_array = array(); $data_array['userid'] = $user_id; $data_array['session'] = $session; $return_array = invoke_prodigem_rest_api('prodigem.files.getListUser', $data_array); if(print_api_error($return_array)) { die(); } return $return_array; } function prodigem_user_file_exists($user_files, $filename) { foreach($user_files[0]['children'][2]['children'] as $file) { if ($file['attrs']['FILENAME'] == $filename) { return 1; } } return 0; } function prodigem_upload_url($user_id, $session, $url, &$file_id, &$return_filename, &$folder_id, &$folder_name) { $return_array = array(); $data_array = array(); $data_array['userid'] = $user_id; $data_array['session'] = $session; $data_array['url'] = $url; $return_array = invoke_prodigem_rest_api('prodigem.upload.grabURL', $data_array); $return_assoc = make_associative_array($return_array[0]['children']); $file_id = $return_assoc['FILEID']; $return_filename = $return_assoc['FILENAME']; $folder_id = $return_assoc['FOLDERID']; $folder_name = $return_assoc['FOLDERNAME']; return $return_array; } function prodigem_create_torrent($user_id, $session, $title, $link, $description, $enclosure_type, $folder_id, &$torrent_page, &$torrent_url) { $return_array = array(); $data_array = array(); $data_array['userid'] = $user_id; $data_array['session'] = $session; $data_array['folderid'] = $folder_id; $data_array['categoryid'] = TORRENT_CATEGORY_DEFAULT; $data_array['torrenthosted'] = TORRENT_HOST_ON_CREATION; $data_array['torrenttitle'] = TORRENT_TITLE_PREFIX.$title; $data_array['torrentdescription'] = $link."\n".$description; $data_array['torrentprice'] = '0.00'; $data_array['licensetype'] = TORRENT_LICENSE_TYPE; $data_array['licenseattribution'] = TORRENT_LICENSE_ATTRIBUTION; $data_array['licensecommercialuse'] = TORRENT_LICENSE_COMMERCIAL_USE; $data_array['licensemodifications'] = TORRENT_LICENSE_MODIFICATIONS; // Check the enclosure_type if (preg_match('/video/', $enclosure_type)) { $data_array['categoryid'] = TORRENT_CATEGORY_VIDEO; print($data_array['categoryid']."\n"); } else if (preg_match('/audio/', $enclosure_type)) { $data_array['categoryid'] = TORRENT_CATEGORY_AUDIO; print($data_array['categoryid']."\n"); } $return_array = invoke_prodigem_rest_api('prodigem.torrents.createTorrent', $data_array); $return_assoc = make_associative_array($return_array[0]['children']); $torrent_page = $return_assoc['TORRENTPAGE']; $torrent_url = $return_assoc['TORRENTURL']; return $return_array; } ///////////////////////////////////////////////////////////////////////////////////////// // // Program Starts Here // ///////////////////////////////////////////////////////////////////////////////////////// // First we sign into Prodigem $user_id = ''; $session = ''; print("Signing into Prodigem.\n\n"); prodigem_sign_in($user_id, $session); // Then we grab the RSS feed $xml_rss = ''; print("Getting RSS Feed\n\n"); get_rss_feed($xml_rss); // Then we get a list of the current files in the user's Prodigem account $user_files = array(); print("Retrieving your Prodigem file listing.\n\n"); $user_files = prodigem_get_user_files($user_id, $session); // Then we go through each of the items in the RSS feed $rss_object = new XML_Array_Producer(); $rss_array = $rss_object->Parse($xml_rss); $count = 1; foreach($rss_array[0]['children'][0]['children'] as $sub_array) { if ($count > 10) { print("\n\nDone with feed.\n\n"); break; } if ($sub_array['name'] == 'ITEM') { $rss_assoc = make_associative_array($sub_array['children']); print("\nInspecting RSS Item #$count:\n"); if ($rss_assoc['ENCLOSURE']['URL'] && $rss_assoc['ENCLOSURE']['TYPE'] != 'application/x-bittorrent') { // Get the filename print("... Found an enclosure of type \"".$rss_assoc['ENCLOSURE']['TYPE']."\".\n"); $filename = preg_replace('/.*\//', '', $rss_assoc['ENCLOSURE']['URL']); print("... ... Filename is: $filename\n"); print("... ... URL is: ".$rss_assoc['ENCLOSURE']['URL']."\n"); // Check file existance print("... Checking file existance in your Prodigem account.\n"); if (prodigem_user_file_exists($user_files, $filename)) { print("... ... File exists. Skipping.\n"); $count++; continue; } print("... ... File does not appear to exist in your Prodigem account.\n"); // Grab the URL print("... Uploading URL into Prodigem.\n"); $file_id = 0; $return_filename = ''; $folder_id = 0; $folder_name = ''; $upload_array = prodigem_upload_url($user_id, $session, $rss_assoc['ENCLOSURE']['URL'], $file_id, $return_filename, $folder_id, $folder_name); if (print_api_error($upload_array)) { $count++; continue; } else { print("... ... File uploaded successfully.\n"); } // Make a torrent print("... Creating torrent.\n"); $torrent_page = ''; $torrent_url = ''; $torrent_array = prodigem_create_torrent($user_id, $session, $rss_assoc['TITLE'], $rss_assoc['LINK'], $rss_assoc['DESCRIPTION'], $rss_assoc['ENCLOSURE']['TYPE'], $folder_id, $torrent_page, $torrent_url); if (!print_api_error($torrent_array)) { print("... ... Success!!!\n"); print("... ... Torrent page: $torrent_page\n"); print("... ... Torrent URL: $torrent_url\n"); } } $count++; } } ?>