updated@0:40msk 28nov2009
source:code.txt
#!/usr/bin/php5
<?
/*
CLI interface for yandexdisk.
requires php5 && php_curl
Copyright Alexander N Lazutov (ñ) 2009; mailto: web@lazutov.ru ;
This script if free of charge and licensed under BSD license.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//define('DEBUG',true);
if (!isset($argv)) die ("run me from cli, nasial`nik :)\n");
$auth=false;
$cookielist = '.phpyd-cookie';
$uagent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6 YB/3.5.3';
function parseargv(){
global $argv;
$conf=array();
foreach ($argv as $k=>$v)
{
$next=@$argv[$k+1];
if (($v=='-u') OR ($v=='--user')) $conf['user']=$next;
elseif (($v=='-p') OR ($v=='--password'))$conf['pass']=$next;
/*elseif (($v=='-s') OR ($v=='--source')) $conf['source']=$next;
elseif (($v=='-d') OR ($v=='--dest')) $conf['dest']=$next; not implemented*/
elseif (($v=='-f') OR ($v=='--file')) $conf['file']=$next;
elseif (($v=='-pw') OR ($v=='--setpassword')) $conf['pw']=$next;
elseif (($v=='-c') OR ($v=='--config')) $conf['conf']=$next;
}
$conf['act']=$argv[1];
if (!isset($conf['conf'])) $conf['conf']='.phpyd-config';
if ((!isset($conf['user'])) OR (!isset($conf['pass'])))
if (file_exists($conf['conf'])) list ($conf['user'],$conf['pass'])=file($conf['conf'], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
else die("No username/password to use; use --user (-u) and --password(-p) flags or --config(-c)\n");
@file_put_contents($conf['conf'],$conf['user']."\n".$conf['pass']);
return $conf;
}
$conf=parseargv();
login($conf['user'],$conf['pass'],true);
$time=time();
switch ($conf['act']){
case 'send':
$r=upload($conf['file']);
echo $r;
$speed= (string) round (filesize($conf['file'])/1024/1024/(time()-$time),3);
if (isset($conf['pw'])) setpw(trim($r),$conf['pw']);
$time=time()-$time;
echo "\nSpeed: {$speed} MB/s; {$time} sec.\n";
break;
case 'list':
echo "===filelist===\n";
echo implode("\n",flist());
$time=time()-$time;
echo "\n----\n{$time} sec.\n";
break;
case 'get':
fdl($conf['file']);
$speed= (string) round (@filesize('_'.basename($conf['file'],'.html'))/1024/1024/($time-time()),3);
$time=time()-$time;
echo "\nSpeed: {$speed} MB/s; {$time} sec.\n";
break;
case 'setpw':
if (isset($conf['pw'])) setpw($conf['file'],$conf['pw']); else echo "No pwd given";
$time=time()-$time;
echo "\n {$time} sec.\n";
break;
case 'del':
delfile($conf['file']);
$time=time()-$time;
echo "\n {$time} sec.\n";
break;
default: echo "".
"Usage: [get|send|list] -u user -p pwd -f file
get: get file specified in -f or --file
send: send file specified in -f or --file
list: get filelist
FLAGS:
-u, --user username to use, eg lazutov@qwerty.name
-p, -- password, using both -u & -p updates data in config file.
-f, --file file to work with.
-c, --conf file to get username & pwd from. [default: .phpdropboxconfig]
EXAMPLEs:
job.php list -u alazutov -p yourpwd - gets filelist & writes username & password to config. so you may use the script without -u & -p. So:
job.php list
job.php send -f cat.zip
job.php get -f cat.zip - files are saved as _filename in script directory.
TODO: dest & source flags; recursive processing; removing fils from remote space.
";
break;
}
curl_close($ch);
echo "=== Operation completed!\n";
function login ($login, $password,$useoldsession=false){
global $ch,$cookielist,$uagent,$auth;
$auth=false;
//unlink($cookielist);
if ((file_exists($cookielist) && (time()-filemtime($cookielist)>60*60-24*13)) OR (!$useoldsession)) unlink($cookielist);
$ch = curl_init('https://passport.yandex.ru/passport?mode=auth');
$cnt=1;
while (!$auth){
if (!file_exists($cookielist)) {
echo "--- new session\n";
curl_setopt($ch, CURLOPT_URL, 'https://passport.yandex.ru/passport?mode=auth');
curl_setopt($ch, CURLOPT_POSTFIELDS,"login={$login}&passwd={$password}&twoweeks=yes");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $uagent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookielist);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
if (defined('DEBUG')) echo $info['http_code']."\n";
if ($info['http_code'] !== 200) $auth=true; else {$auth=false; unlink($cookielist);}
if (defined('DEBUG')) file_put_contents('r',$result);
} else {$auth=true; echo "--- session cached\n";;}
// check
if ($auth) {
echo "--- checking\n";
curl_setopt($ch, CURLOPT_URL, 'http://narod.yandex.ru/disk/all');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $uagent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookielist);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
if (defined('DEBUG')) echo $info['http_code']."\n";
if ($info['http_code'] !==200) {$auth=false; unlink($cookielist);}
if (defined('DEBUG')) file_put_contents('r2',$result);
}
$cnt++;
if ($cnt>2) break;
}
if (!$auth) {unlink($cookielist); die("ER! Can not login!\n");}
echo "OK! logged in!\n";
}
function upload($filename){
global $ch,$cookielist,$uagent,$auth;
if (!file_exists($filename)) die ('file_nf');
echo "--- file checked!\n";
curl_setopt($ch, CURLOPT_URL, 'http://narod.yandex.ru/disk/getstorage/?rnd='.rand(100000,999999));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookielist);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
if (preg_match('!"url":"(.*)", "hash":"(.*)", "purl":"(.*)"!si', $result, $r)) {
$uploadto= $r[1];
$hash = $r[2];
$getprogress = $r[3];
unset($r);
echo "--- server got!\n";
} else die("ER! Can not parse target to upl!\n");
curl_setopt($ch, CURLOPT_URL, "{$uploadto}?tid={$hash}");
curl_setopt($ch, CURLOPT_REFERER, 'http://narod.yandex.ru/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file'=>'@'.$filename));
$result = curl_exec($ch);
$info = curl_getinfo($ch);
if (defined('DEBUG')) echo $info['http_code']."\n";
if (defined('DEBUG')) file_put_contents('r3',$result);
if ($info['http_code'] != 200) die("ER! Can not upload!\n");
echo "OK! uploaded!\n";
curl_setopt($ch, CURLOPT_URL, "{$getprogress}?tid={$hash}&rnd=".rand(100000,999999));
curl_setopt($ch, CURLOPT_POST, 0);
$result = curl_exec($ch);
//if (defined('DEBUG')) echo $result;
if (!strpos($result,'"status": "done"')) die("ER! Can not parsestatus!\n");
echo "OK! status checked!\n";
$url = 'http://narod.yandex.ru/disk/last/';
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
if (preg_match('!<span class=\'b\-fname\'><a href="(.*)">!', $result, $r)) {
$uploaded = trim($r[1]);
unset($r);
echo "OK! link recieved!\n";
return $uploaded;
} else die("ER! Can not get uploaded url!\n");
return -1;
}
function flist($find=false){
global $ch,$cookielist,$uagent,$auth;
$i=1;
$url='http://narod.yandex.ru/disk/all/';
$flist=array();
while (true){
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
if (preg_match_all('!<span class=\'b\-fname\'><a href="(.*)">!', $result, $r)!==0)
foreach ($r[1] as $v) {
if ($find) { if ($find===$v) return sgetid($result,$find); } else $flist[]=$v;
}
$i++;
if (strpos($result,'/disk/all/page'.$i)===false) break; else {$url='http://narod.yandex.ru/disk/all/page'.$i.'/?sort=cdate%20desc';}
}
if ($find) return false;
if (count($flist)!==0){ echo "OK! list got!\n"; return $flist;}
else die("ER! No files found!\n");
return -1;
}
function sgetid($result,$find){
$result=substr($result,strpos($result,$find));
$result=substr($result,strpos($result,'href="/disk/setdesc/')+strlen('href="/disk/setdesc/'));
return substr($result,0,strpos($result,'"'));
}
function delfile($link){
global $ch,$cookielist,$uagent;
$id=getid($link);
if ($id===false) die('err: can`t get file id');
$qs='action=delete&retpath=%2Fdisk%2Fall%2F&fid='.$id;
curl_setopt($ch, CURLOPT_URL, 'http://narod.yandex.ru/disk/all/');
curl_setopt($ch, CURLOPT_POSTFIELDS,$qs);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $uagent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookielist);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
return true;
}
function setpw($link,$pwd){
global $ch,$cookielist,$uagent;
$id=getid($link);
if ($id===false) die('err: can`t get file id');
$qs='passwd='.$pwd;
curl_setopt($ch, CURLOPT_URL, 'http://narod.yandex.ru/disk/setpasswd/'.$id);
curl_setopt($ch, CURLOPT_POSTFIELDS,$qs);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $uagent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookielist);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
return true;
}
//
function getid($link){
// just a wraper. to be extended
$r=flist($link);
if ($r===false) return false;
return $r;
}
function fdl($link){
global $ch,$cookielist,$uagent,$auth;
curl_setopt($ch,CURLOPT_URL,$link);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_USERAGENT,$uagent);
$result = curl_exec($ch);
if (defined('DEBUG')) file_put_contents('r4',$result);
if (preg_match('/<a class="h\-link" rel="yandex_bar" href="(.*?)"/',$result,$m)) {
$url ="http://narod.ru{$m[1]}";
echo "OK! direct link got!\n";
} else die('fget_nourl');
curl_setopt($ch,CURLOPT_URL,$url);
$fs=fopen ('_'.basename($m[1]),'w');
curl_setopt($ch, CURLOPT_FILE, $fs);
curl_exec($ch);
echo "OK! file saved!\n";
}
?>