Code:
<?php
require_once("dlcapi_includes.php");
function debug($str,$force=0){
if(!$force &&!DLC_API_DEBUG)return;
echo "<pre>";
echo htmlentities(print_r($str,true));
echo "</pre>";
}
function dlcInterndataEncode($str){
if($str==NULL)$str="n.A.";
return base64_encode(trim($str));
}
function isDLCValue($arg){
return $arg!=NULL && strlen($arg)>0;
}
function getDLCPasswordString($pwList){
$ret="";
$list=array();
$emtpy=true;
for($i=0; $i<count($pwList);$i++){
if($pwList[$i] && strlen(trim($pwList[$i]))>0){
$empty=false;
array_push($list,'"'.$pwList[$i].'"');
}
}
if($empty||!$list || count($list)==0)return "";
return '{'.implode(', ',$list).'}';
}
function getCCFPasswordString($pwList){
$ret="";
$list=array();
$emtpy=true;
for($i=0; $i<count($pwList);$i++){
if($pwList[$i] && strlen(trim($pwList[$i]))>0){
$empty=false;
array_push($list,$pwList[$i]);
}
}
if($empty||!$list || count($list)==0)return "";
return implode(', ',$list);
}
function createDLCXML($links,$app=NULL,$url=NULL,$version=NULL){
if(!isDLCValue($app))$app=DLC_CONTENT_GENERATOR_NAME;
if(!isDLCValue($url))$url=DLC_CONTENT_GENERATOR_URL;
if(!isDLCValue($version))$version=DLC_API_VERSION;
$header='<dlc>
<header>
<generator>
<app>'.dlcInterndataEncode($app).'</app>
<version>'.dlcInterndataEncode($version).'</version>
<url>'.dlcInterndataEncode($url).'</url>
</generator>
<tribute>
<name>'.dlcInterndataEncode($links["uploader"]).'</name>
</tribute>
<dlcxmlversion>'.dlcInterndataEncode("20_02_2008").'</dlcxmlversion>
</header>';
$content='
<content>';
for($i=0; $i<count($links["packages"]);$i++){
$package=$links["packages"][$i];
$password=getDLCPasswordString($package["passwords"]);
$name=$package["name"];
$comment=$package["comment"];
$category=$package["category"];
$packageLinks=$package["links"];
if(count($package["links"])>0){
$content.='
<package name="'.dlcInterndataEncode($name)."\"";
if(isDLCValue($password))$content.=" passwords=\"".dlcInterndataEncode($password)."\"";
if(isDLCValue($comment))$content.=" comment=\"".dlcInterndataEncode($comment)."\"";
if(isDLCValue($category))$content.=" category=\"".dlcInterndataEncode($category)."\"";
$content.='>';
for( $l=0; $l<count($packageLinks);$l++){
$content.='
<file>
<url>'.dlcInterndataEncode($packageLinks[$l]["url"]).'</url>';
if($packageLinks[$l]["filename"]&&strlen($packageLinks[$l]["filename"])>1){
$content.='
<filename>'.dlcInterndataEncode($packageLinks[$l]["filename"]).'</filename>';
}
if($packageLinks[$l]["size"]&&strlen($packageLinks[$l]["size"])>1){
$content.='
<size>'.dlcInterndataEncode($packageLinks[$l]["size"]).'</size>';
}
$content.='
</file>';
}
$content.='
</package>';
}
}
$content.='
</content>
</dlc>';
return $header.$content;
}
function ccfInternEncode($str){
return utf8_encode($str);
}
function createCCFXML($links){
$del="";
$content='<xml version="1.0" encoding="utf-8">';
$content='';
$content.=$del;
$content.='<CryptLoad>';
$content.=$del;
for($i=0; $i<count($links["packages"]);$i++){
$package=$links["packages"][$i];
$password=getCCFPasswordString($package["passwords"]);
$name=$package["name"];
$comment=($package["comment"]&&strlen($package["comment"])>0)?$package["comment"]:"("."create by DLCAPI".")";
$category=$package["category"];
$packageLinks=$package["links"];
if(count($package["links"])>0){
$content.=' <Package service="" name="'.ccfInternEncode($name).'" url="Directlinks">';
$content.=$del;
$content.=' <Options>';
$content.=$del;
$content.=$comment?' <Kommentar>'.ccfInternEncode($comment).'</Kommentar>':' <Kommentar />';
$content.=$del;
$content.=$password?' <Passwort>'.ccfInternEncode($password).'</Passwort>':' <Passwort />';
$content.=$del;
$content.=' </Options>';
$content.=$del;
for( $l=0; $l<count($packageLinks);$l++){
if(!$packageLinks[$l]["filename"]||strlen(trim($packageLinks[$l]["filename"]))==0){
$info=pathinfo($packageLinks[$l]["url"]);
$packageLinks[$l]["filename"]=$info["basename"];
}
$content.=' <Download Url="'.ccfInternEncode($packageLinks[$l]["url"]).'">';
$content.=$del;
$content.=' <FileSize>'.ccfInternEncode(($packageLinks[$l]["size"]?$packageLinks[$l]["size"]:"0")).'</FileSize>';
$content.=$del;
$content.=' <Url>'.ccfInternEncode($packageLinks[$l]["url"]).'</Url>';
$content.=$del;
$content.=$packageLinks[$l]["filename"]?' <FileName>'.ccfInternEncode($packageLinks[$l]["filename"]).'</FileName>':' <FileName/>';
$content.=$del;
$content.=' </Download>';
$content.=$del;
}
$content.=' </Package>';
$content.=$del;
}
}
$content.='</CryptLoad>';
return $content;
}
function dataToLinklist($links){
$ret=array();
for($i=0; $i<count($links["packages"]);$i++){
$package=$links["packages"][$i];
$packageLinks=$package["links"];
if(count($package["links"])>0){
for( $l=0; $l<count($packageLinks);$l++){
array_push($ret,$packageLinks[$l]["url"]);
}
}
}
return $ret;
}
function getAthCXRedirect($service){
$url=parse_url($service);
$fp = @fsockopen($url["host"], 80);
if(!$fp)return $service;
fputs($fp, "GET ".$url["path"]." HTTP/1.1\r\n");
fputs($fp, "Host: ".$url["host"]."\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Connection: close\r\n\r\n");
while(!feof($fp)) {
$res .= fgets($fp, 1024);
if(strpos($res,"Location:")>0){
$tmp=explode("Location:",$res);
fclose($fp);
return trim($tmp[1]);
break;
}
}
fclose($fp);
return $service;
}
function getBetween($find1, $find2, $string) {
$parse = explode($find1, $string, 2);
$parse = @substr($parse[1], 0, @strpos($parse[1], $find2));
return $parse;
}
function getDLCPair(){
$expires=DLC_KEY_PAIR_EXPIRES_AFTER;
$keyData=@mysql_fetch_array(@mysql_query("SELECT * FROM `".DLC_MYSQL_TABLE."_2`"));
$plainKey=$keyData[1];
$encodedKey=$keyData[2];
$updateTime=$keyData[0];
if(!$plainKey ||!$encodedKey||($updateTime+$expires)<time()){
debug("INFO: Fetch new Key");
srand((float)microtime() * 1000000);
$key= substr(md5(md5(time().rand(0,10000)).rand(0,10000)),0,16);
$encKey=encryptDLCKey($key);
if($encKey==NULL){
debug("ERROR: Could not get new DLC Pair.");
return array($plainKey,$encodedKey);
}
mysql_query('TRUNCATE TABLE `'.DLC_MYSQL_TABLE.'_2`');
$sql = 'INSERT INTO `'.DLC_MYSQL_TABLE.'_2` (`time`, `plain`, `crypt`) VALUES (\''.time().'\', \''.$encKey[0].'\', \''.$encKey[1].'\');';
mysql_query($sql);
return array(trim($encKey[0]),trim($encKey[1]));
}
return array($plainKey,$encodedKey);
}
function callDLCService($service,$key){
$red=getAthCXRedirect($service);
if($red==NULL){
debug("WARNING:Could not resolve ".$service);
return NULL;
}else{
debug("INFO Resolved ".$service." to ".$red);
}
$ret=postRequest($red,"&data=".$key."&lid=".base64_encode(DLC_CONTENT_GENERATOR_NAME_ID."_".DLC_EMAIL."_".DLCCRYPT_MAINSERVICES."_".DLC_KEY_PAIR_EXPIRES_AFTER)."&version=".DLC_API_VERSION);
if($ret==NULL){
debug("WARNING: Could not get Key from ".$red);
return NULL;
}
if(strpos($ret,"</rc>")==false){
debug("WARNING: service not available ".$red);
return NULL;
}
$key=getBetween("<rc>","</rc>",$ret);
$plain=getBetween("<rcp>","</rcp>",$ret);
if($key==NULL|| strlen($key)!=88){
debug("Key ".$ret);
debug("WARNING: OLD CLIENT OR SERVER VERSION");
return NULL;
}
return array($plain,$key);
}
function getServices(){
$services=array();
if(DLCCRYPT_SERVICES_MIRROR_1)array_push($services,DLCCRYPT_SERVICES_MIRROR_1);
if(DLCCRYPT_SERVICES_MIRROR_2)array_push($services,DLCCRYPT_SERVICES_MIRROR_2);
if(DLCCRYPT_SERVICES_MIRROR_3)array_push($services,DLCCRYPT_SERVICES_MIRROR_3);
if(DLCCRYPT_SERVICES_MIRROR_4)array_push($services,DLCCRYPT_SERVICES_MIRROR_4);
return $services;
}
function encryptDLCKey($key){
$services=getServices();
$main=DLCCRYPT_MAINSERVICES;
srand((float)microtime() * 1000000);
shuffle($services);
if($main!=NULL){
$dlcKey=callDLCService($main,$key);
}
if($dlcKey!=NULL)return $dlcKey;
foreach ($services as $service) {
$dlcKey=callDLCService($service,$key);
if($dlcKey!=NULL)break;
}
if($dlcKey!=NULL)return $dlcKey;
return NULL;
}
function encryptDLCXML($xml){
$xml=base64_encode($xml);
$pair=getDLCPair();
debug("INFO Current: ");
debug($pair);
if($pair==NULL)return NULL;
$key=$pair[0];
$KEY=$pair[1];
debug("Keylength: ".strlen($key));
$cp = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'cbc', '');
@mcrypt_generic_init($cp, $key,$key);
$enc = mcrypt_generic($cp, $xml);
@mcrypt_generic_deinit($cp);
@mcrypt_module_close($cp);
$cp = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'cbc', '');
@mcrypt_generic_init($cp, $key,$key);
$enc = mcrypt_generic($cp, $xml);
@mcrypt_generic_deinit($cp);
@mcrypt_module_close($cp);
$enc=base64_encode($enc);
debug("ENC OK");
$cp = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'cbc', '');
@mcrypt_generic_init($cp, $key,$key);
$dec = mdecrypt_generic($cp, base64_decode($enc));
mcrypt_generic_deinit($cp);
mcrypt_module_close($cp);
debug("DEC ".base64_decode($dec));
return $enc.$KEY;
}
function createDLC($links,$app=NULL,$url=NULL,$version=NULL){
$xml=createDLCXML($links,$app,$url,$version);
return encryptDLCXML($xml);
}
function createCCF($links){
if(!defined("CCF_KEY_10"))die("Error: CCF Keyfile is not installed");
$xml=createCCFXML($links);
return encryptCCFXML($xml);
}
function filterString($str){
$ret="";
$allowed = "QWERTZUIOPÜASDFGHJKLÖÄYXCVBNMqwertzuiopasdfghjklyxcvbnm;:,._-&%(){}#~+ 1234567890<>='\"/";
$char="";
for( $i=0; $i<strlen($str);$i++){
if(!(strpos($allowed,( $char=substr($str,$i,1)))===false)){
$ret.=$char;
}
}
return $ret;
}
function getBetweenAll($find1, $find2, $string){
$elements = array();
while($word = getBetween($find1, $find2, $string)){
array_push($elements, $word);
$string = str_replace($find1.$word.$find2, null, $string);
}
return $elements;
}
function encryptCCFXML($xml){
//$xml=chr(0xEF).chr(0xBB).chr(0xBF).$xml;
$xml=utf8_encode($xml);
$keyList=array(CCF_KEY_10,CCF_KEY_08,CCF_KEY_07);
$ivList=array(CCF_IV_10,CCF_IV_08,CCF_IV_07);
$block=MCRYPT_RIJNDAEL_128;
$mode=MCRYPT_MODE_CBC;
$cp = mcrypt_module_open($block, '', $mode, '');
@mcrypt_generic_init($cp, base16Decode($keyList[0]),base16Decode($ivList[0]));
$enc = mcrypt_generic($cp, $xml);
mcrypt_generic_deinit($cp);
mcrypt_module_close($cp);
return $enc;
}
function string2hex($str)
{
if (trim($str)!="")
{
$hex="";
$length=strlen($str);
for ($i=0; $i<$length; $i++)
{
if ($i>0) $bound="-"; else $bound="";
$hex.=$bound.str_pad(dechex(ord($str[$i])), 2, 0, STR_PAD_LEFT)."(".$str[$i].")";
}
return $hex;
}
}
function decryptCCF($dat){
$keyList=array(CCF_KEY_10,CCF_KEY_08,CCF_KEY_07);
$ivList=array(CCF_IV_10,CCF_IV_08,CCF_IV_07);
$vList=array(CCF_ID_10,CCF_ID_08,CCF_ID_07);
$block=MCRYPT_RIJNDAEL_128;
$mode=MCRYPT_MODE_CBC;
$i=0;
$xml="";
while($i<count($keyList)){
$cp = mcrypt_module_open($block, '', $mode, '');
@mcrypt_generic_init($cp, base16Decode($keyList[$i]),base16Decode($ivList[$i]));
$enc = filterString(mdecrypt_generic($cp, $dat));
mcrypt_generic_deinit($cp);
mcrypt_module_close($cp);
if(strpos(strtolower($enc), "cryptload")>0){
$xml=trim($enc);
break;
}
$i++;
}
$ret=array();
$ret["version"]=$vList[$i];
$ret["linknum"]=0;
$ret["packagenum"]=0;
$ret["password"]="";
$ret["comment"]="";
$ret["uploader"]="CCF2DLC";
$ret["packages"]=array();
$ret["string"]=$xml;
$comment=getBetween('Kommentar>','<',$xml);
$pw=getBetween('Passwort>','<',$xml);
$packages=getBetweenAll("Package","/Package",$xml);
for( $i=0; $i<count($packages);$i++){
$name=getBetween('name="','"',$packages[$i]);
$links=getBetweenAll("<Download","/Download",$packages[$i]);
$ret["packages"][$i]=array();
$ret["packages"][$i]["name"]=$name;
$ret["packages"][$i]["links"]=array();
for($ii=0;$ii<count($links);$ii++){
$link=getBetween("<Url>","</Url>",$links[$ii]);
$name=getBetween("<FileName>","</FileName>",$links[$ii]);
$size=getBetween("<FileSize>","</FileSize>",$links[$ii]);
array_push($ret["packages"][$i]["links"],array("url"=>$link,"filename"=>$name,"size"=>$size));
}
$ret["linknum"]+=count($links);
$ret["packagenum"]++;
$ret["packages"][$i]["passwords"]=array($pw);
$ret["packages"][$i]["comment"]=$comment;
$ret["packages"][$i]["category"]="";
$ret["password"].=$pw." ";
$ret["comment"].=$comment." ";
}
$ret["password"]=trim($ret["password"]);
$ret["comment"]=trim($ret["comment"]);
return $ret;
// debug($xml);
}
function createRSDF($links){
if(!defined('RSDF_KEY'))die("Error: RSDF Keyfile is not installed");
$ret="";
$key = base16Decode(RSDF_KEY);
$iv = base16Decode(RSDF_IV);
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CFB, '');
mcrypt_generic_init($td, $key, $iv);
$linkList=dataToLinklist($links);
for( $i=0; $i<count($linkList);$i++){
$ret.=base64_encode(mcrypt_generic($td,$linkList[$i]));
$ret.="\r\n";
}
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$ret= base16Encode($ret);
return $ret;
}
function decryptRSDF($rsdfData){
$key = base16Decode(RSDF_KEY);
$iv = base16Decode(RSDF_IV);
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CFB, '');
mcrypt_generic_init($td, $key, $iv);
$rsdfLinks = base16Decode($rsdfData);
$rsdfLinks = explode("\r\n", $rsdfLinks);
$links = array();
foreach ($rsdfLinks as $link) {
if (strlen(filterString($link)) > 2){
$encoded = base64_decode(filterString($link));
$decoded = mdecrypt_generic($td, $encoded);
if(strlen(trim($decoded))>0){
$links[sizeof($links)] = str_replace("http://anonym.to/?", "", str_replace("CCF: ", "", $decoded));
}
}
}
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$ret=array(array("packagename"=>"rsdf2dlc","links"=>$links,"password"=>"","comment"=>""));
$ret["linknum"]=count($links);
$ret["packagenum"]=1;
$ret["password"]="";
$ret["comment"]="";
return $ret;
}
function base16Decode($arg){
$ret="";
for($i=0;$i<strlen($arg);$i+=2){
$tmp=substr($arg,$i,2);
$int=hexdec($tmp);
$ret.=chr ($int);
}
return $ret;
}
//Encodiert einen String als Base16
function base16Encode($arg){
$ret="";
for($i=0;$i<strlen($arg);$i++){
$tmp=ord($arg[$i]);
$hex=dechex($tmp);
while(strlen($hex)<2)$hex="0".$hex;
$ret.=$hex;
}
return $ret;
}
function checkRapidshareLinks($links){
$linkstring="";
$urls="";
for( $i=0; $i<count($links);$i++){
$linkstring.=$links[$i]."\r\n";
if(!(strpos(trim($links[$i]),"rapidshare")===false)){
$urls.=$links[$i]."\r\n";
}
}
$erg = PostToHost("rapidshare.com", "/cgi-bin/checkfiles.cgi", "http://rapidshare.com/de/checkfiles.html", "urls=".$urls);
$ret=array();
$i=0;
$ret["ok"]=0;
$ret["bad"]=0;
$ret["total"]=0;
$ret["rapidshare"]=0;
$ret["error"]=0;
if(strpos($erg,"invalid")>0)$ret["error"]++;
foreach(explode("\n", $linkstring) as $link) {
$help = explode("/", trim($link));
if(!strlen(trim($link)))continue;
$ret["total"]++;
if(strpos(trim($link),"rapidshare")===false){
array_push($ret,array($i,2,trim($link)));
}else{
$ret["rapidshare"]++;
if(strpos($erg,$help[4]." (". $help[5].") not found") > 0) {
array_push($ret,array($i,0,trim($link)));
$ret["bad"]++;
}else{
array_push($ret,array($i,1,trim($link)));
$ret["ok"]++;
}
}
$i++;
}
return $ret;
}
function PostToHost($host, $path, $referer, $data_to_send) {
$fp = fsockopen($host, 80);
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Referer: $referer\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ". strlen($data_to_send) ."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data_to_send);
while(!feof($fp)) {
$res .= fgets($fp, 128);
}
fclose($fp);
return $res;
}
function urlListToXML($list,$uploader,$packagename,$pw,$comment,$category){
$dlc=array();
$dlc["uploader"]=$uploader;
$dlc["packages"]=array();
$package=array("name"=>$packagename,"passwords"=>array($pw),"comment"=>$comment,"category"=>$category,"links"=>array());
for( $i=0;$i<count($list);$i++){
$url=pathinfo($list[$i]);
$name=$url["basename"];
$link=array("url"=>$list[$i],"filename"=>$name,"size"=>0);
array_push($package["links"],$link);
}
array_push($dlc["packages"],$package);
return $dlc;
}
function createContainerdataModel($uploadername="unknown"){
$ret=array();
$ret["uploader"]=$uploadername;
$ret["packages"]=array();
return $ret;
}
function addFilePackage(&$model,$packagename="package",$passwords=array(),$comment="no comment",$category="various"){
$package=array();
$package["name"]=$packagename;
$package["passwords"]=$passwords;
$package["comment"]=$comment;
$package["category"]=$category;
$package["links"]=array();
array_push($model["packages"],$package);
return count($model["packages"])-1;
}
function addLink(&$model,$packageID,$url,$filename="",$filesize=0){
$link=array("url"=>$url,"filename"=>$filename,"size"=>$filesize);
array_push($model["packages"][$packageID]["links"],$link);
return $link;
}
debug("DLCAPI v. ".DLC_API_VERSION." loaded. Set \$DLC_API_DEBUG=0; to disabled this messages---");
?>