PDA

View Full Version : Help needed with MU Remote Upload Script


s_dragons_lair_c
01-01-2010, 07:18 PM
Hi guys, I need help with a Megaupload Remote Upload Script that I'm making.

There are 2 pages, upload.php & process.php.

So it works like this. I can enter many links in a textarea (since MU doesn't allow more than one link to be submitted at once on their site), process.php sends 5 of these links at once to upload.php with the use of Javascript.

upload.php submits the links to MU(with cURL) and also checks the upload status(this too with cURL). If an upload is completed, process.php sends out the next link to upload.php.

process.php


var rslinks = '';
var mulinks = '';
var linklist = '';

function ajaxRequest(){
var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
if (window.ActiveXObject) {
for (var i = 0; i < activexmodes.length; i++) {
try {return new ActiveXObject(activexmodes[i]);}
catch(e){}
}
}

else if (window.XMLHttpRequest) return new XMLHttpRequest();

else return false;
}

function ajaxdata(filehost, urls) {
var http = new ajaxRequest();
var urls_array = urls.split(',');
var filtered_urls = new Array();
for (i = 0; i < urls_array.length; i++) {
if (filehost == 'mu')
if (mulinks.indexOf(urls_array[i]) == -1)
filtered_urls.push(urls_array[i]);
else if (filehost == 'rs')
if (rslinks.indexOf(urls_array[i]) == -1)
filtered_urls.push(urls_array[i]);
}

final_urls = filtered_urls.length >= 5 ? filtered_urls.slice(0, 5).join(',') : filtered_urls.join(',');

<?php
$dir = substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/') + 1);
$url = "http://" . $_SERVER['HTTP_HOST'] . $dir . 'upload.php';
?>

params = 'urls=' + final_urls;
if (filehost == 'mu')
params += '<?php echo '&credu=' . urlencode($user_mu) . '&credp=' . urlencode($pass_mu) . '&description=' . $_POST['mu_description'] . '&youremail=' . $_POST['mu_youremail'] . '&receiveremail=' . $_POST['mu_receiveremail'] . '&password=' . $_POST['mu_password'] . '&multiplerecipients=' . $_POST['mu_multiplerecipients'] . '&filehost=mu'; ?>';
else if (filehost == 'rs')
params += '<?php echo '&credu=' . urlencode($user_rs) . '&credp=' . urlencode($pass_rs) . '&filehost=rs'; ?>';

http.open('POST', '<?php echo $url; ?>', true)
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.setRequestHeader('Content-length', params.length);
http.setRequestHeader('Connection', 'close');

http.onreadystatechange = function() {
if (http.readyState == 4) {
if (http.status == 200 || window.location.href.indexOf('http') == -1) {
var response = http.responseText;
if (response == null || typeof(response) == "undefined" || response == '') {
setTimeout(function(){ajaxdata(filehost, urls)}, 8000);
return;
}
displayupstats(response, filehost);
if (filtered_urls.length >= 1) setTimeout(function(){ajaxdata(filehost, urls)}, 12000);
else return;
}
else alert('An error has occured making the request');
}
}
http.send(params);
}

function displayupstats(response, filehost) {
var updtls_array = response.split("\n");
var details = new Array();
var filename = new Array();
var upstatus;
var progress;

if (filehost == 'mu') {
if (updtls_array != null && typeof(updtls_array) != "undefined") {
for (i = 0; i < updtls_array.length; i++) {
details = updtls_array[i].split(',');
filename = details[0].split('/');
filename = filename[filename.length - 1];
upstatus = details[3];
if (upstatus == '3') upstatus = 'Upload to Megaupload finished';
else if (upstatus == 'Error:') upstatus = 'Retrying...';
progress = details[1];

if ((dtl1 = document.getElementById(filename + '_upstatus')).firstChild != null) {
if (dtl1.firstChild.nodeType == 3) dtl1.firstChild.nodeValue = upstatus;
else dtl1.appendChild(document.createTextNode(upstatus) );
}

if ((dtl2 = document.getElementById(filename + '_upprog')).firstChild != null) {
if (dtl2.firstChild.nodeType == 3) dtl2.firstChild.nodeValue = progress;
else dtl2.appendChild(document.createTextNode(progress) );
}

if (details[2].indexOf('http://www.megaupload.com') != -1) {
if (linklist.indexOf(details[2]) == -1) linklist += details[2] + "\n";
if (mulinks.indexOf(details[0]) == -1) mulinks += details[0] + "\n";

if ((dtl3 = document.getElementById(filename + '_dnlink')).firstChild != null) {
if (dtl3.firstChild.nodeType == 3) dtl3.firstChild.nodeValue = details[2];
else dtl3.appendChild(document.createTextNode(details[2]));
dtl3.setAttribute('href', details[2]);
}
}
}
}
}
else if (filehost == 'rs') {

}
}

upload.php

<?php

if ($_POST['urls'] != '' && $_POST['credu'] != '' && $_POST['credp'] != '') {
if ($_POST['filehost'] == 'rs')
upload2rs($_POST['urls'], $_POST['credu'], $_POST['credp']);
else if ($_POST['filehost'] == 'mu') {
$mu_uppars = array(
'description' => $_POST['description'],
'youremail' => $_POST['youremail'],
'receiveremail' => $_POST['receiveremail'],
'password' => $_POST['password'],
'multiplerecipients' => $_POST['multiplerecipients']
);
upload2mu($_POST['urls'], $_POST['credu'], $_POST['credp'], $mu_uppars);
}
else exit;
}

function upload2mu($mulinks, $user_mu, $pass_mu, $mu_uppars) {

$mulinks = explode(',', trim($mulinks));

$mu_url = 'http://www.megaupload.com/?c=multifetch&s=transferstatus';
$mu_pars = 'login=1&username=' . $user_mu . '&password=' . $pass_mu;

$tobesubmitted = array();
$updtls = array();
$mu_page = postit($mu_url, $mu_pars);

foreach ($mulinks as $mulink) {

if (($startpos = findlink($mu_page, $mulink)) === FALSE && array_search($mulink, $tobesubmitted) === FALSE) {
$tobesubmitted[] = trim($mulink);
continue;
}

$link_details = array ('downloadurl_' => '', 'status_' => '');

$startpos = stripos($mu_page, "(", stripos($mu_page, 'estimated_', $startpos)) + 1;
$endpos = stripos($mu_page, "</", $startpos);
$value = strip_tags(substr($mu_page , $startpos, $endpos - $startpos));
$startpos = $endpos;
$value = trim(str_replace(array('(',')'), '', $value));
$updtls[$mulink][] = $value;

foreach ($link_details as $detail => $value) {
$startpos = stripos($mu_page, ">", stripos($mu_page, $detail, $startpos)) + 1;
$endpos = stripos($mu_page, "</", $startpos);
$value = strip_tags(substr($mu_page , $startpos, $endpos - $startpos));
$startpos = $endpos;
$link_details[$detail] = trim($value);
$updtls[$mulink][] = $value;
}

$startpos = stripos($mu_page, "&cancel=", $startpos);
$endpos = stripos($mu_page, "\"", $startpos);
$up_id = trim(strip_tags(substr($mu_page , $startpos, $endpos - $startpos)));

if ($link_details['status_'] == '3')
postit($mu_url . $up_id, $mu_pars);

else if (is_int(stripos($link_details['status_'], 'Error:'))) {
postit($mu_url . $up_id, $mu_pars);
sleep(12);
postmu(array($mulink), $user_mu, $pass_mu, $mu_uppars);
}
}

if (!empty($tobesubmitted))
postmu($tobesubmitted, $user_mu, $pass_mu, $mu_uppars);

$lastelement = end(array_keys($updtls));

foreach ($updtls as $directlink => $stats) {
echo $directlink;
foreach ($stats as $stat)
echo ',' . $stat;
if ($directlink != $lastelement) echo "\n";
}
}

function postmu($urls_array, $user, $pass, $up_pars) {
$description = $up_pars['description'];
$youremail = $up_pars['youremail'];
$receiveremail = $up_pars['receiveremail'];
$password = $up_pars['password'];
$multiplerecipients = $up_pars['multiplerecipients'];
foreach($urls_array as $url) {
if($url == $postedurl) continue;
$postdata = 'login=1&username=' . $user . '&password=' . $pass;
$mu_login = curl_init();
$options = array (
CURLOPT_URL => 'http://www.megaupload.com',
CURLOPT_COOKIESESSION => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_COOKIEJAR => 'cookie.txt',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 60,
CURLOPT_TIMEOUT => 60
);
curl_setopt_array($mu_login, $options);
curl_exec($mu_login);

$postdata = 'fetchurl=' . trim($url) . '&description=' . $description . '&youremail=' . $youremail . '&receiveremail=' . $receiveremail . '&password=' . $password . '&multiplerecipients=' . $multiplerecipients;
$options = array (
CURLOPT_URL => 'http://www.megaupload.com/?c=multifetch',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120
);
curl_setopt_array($mu_login, $options);
curl_exec($mu_login);
curl_close($mu_login);

$postedurl = $url;
}
}

function postit($url, $postdata) {
$ch = curl_init();

$options = array (
CURLOPT_URL => $url,
CURLOPT_COOKIESESSION => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120
);

curl_setopt_array($ch, $options);
$source_code = curl_exec($ch);
curl_close($ch);
return $source_code;
}

function findlink($request, $val) {
$startpos = 0;
while(1) {
$searchlink = "http://" . $_SERVER['HTTP_HOST'];
$startpos = stripos($request, $searchlink, $startpos);
if ($startpos !== FALSE) {
$endpos = stripos($request, '</', $startpos);
$searchlink = strip_tags(substr($request, $startpos, $endpos - $startpos));
$startpos = $endpos;
for ($dots = strlen($searchlink)-1; stripos($searchlink, '.', $dots) == $dots; $dots--) {}
$searchlink = substr_replace($searchlink, '', ++$dots);
if (substr_count($val, $searchlink)) break;
else continue;
}
else return false;
}
return $startpos;
}

?>

So initially, the urls are passed to ajaxdata in process.php which sends 5 urls to upload.php. upload.php checks the megaupload page to see which urls have already been submitted. The ones which havent been submitted are submitted and the upload status for the already submitted urls are sent back to process.php which displays these stats in the browser. process.php checks each response to see if a url has finished uploading & if it is true for a url then the url is stored in mulinks and the next url is sent to upload.php.


So the problem that I'm facing is that some of the links get submitted twice or sometimes even thrice. It could be that I need to increase the time period between a link submission & the next AJAX request. I tried putting in sleep(8') in the 9th line from the end of upload2mu function in upload.php.
if (!empty($tobesubmitted)) {
postmu($tobesubmitted, $user_mu, $pass_mu, $mu_uppars);
sleep(8);
}
But it didn't work. I really can't think of anything else.

davidj
01-01-2010, 07:59 PM
i would try a callback rather than sleep()

In your postmu() function you need to impliment a callback which wont envoke until postmu has completed

your callback can be used to carry on the process

s_dragons_lair_c
01-01-2010, 08:58 PM
please elaborate

davidj
01-02-2010, 09:12 AM
Just so i can get a measure of your skills did you write the PHP yourself

Do you understand PHP?

s_dragons_lair_c
01-02-2010, 11:47 AM
Yes david, the complete script has been written by me & I understand php very well.

It's just that I couldn't clearly understand what you said.