Files
databap/inc/note.php
2014-10-28 14:57:46 +01:00

129 lines
4.6 KiB
PHP
Executable File

<?php
class Note extends PhpObject
{
private $sCredentials;
private $sNote;
private $sUrl;
private $sCookie;
function __construct($sNote)
{
parent::__construct(__CLASS__, Settings::DEBUG);
$this->setCredentials();
$this->sNote = $sNote;
$this->sUrl = '';
$this->sCookie = '';
}
public function getNote()
{
$sContent = '';
if($this->sNote!='')
{
$this->sUrl = 'http://service.sap.com/sap/support/notes/convert2pdf/000'.$this->sNote.'?sap-language=EN';
$sContent = $this->getPageRecursive();
header('Content-Description: File Transfer');
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="note_'.$this->sNote.'.pdf"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Content-Length: '.strlen($sContent));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
/*
$this->sUrl = 'http://service.sap.com/sap/support/notes/'.$this->sNote;
$this->getPageRecursive();
$sSapPath = substr($this->sUrl, 0, strpos($this->sUrl, '/bc/bsp/sno/ui_entry/entry.htm'));
$sFirstDir = substr($sSapPath, strpos($sSapPath, 'sap('));
$this->sUrl = $sSapPath.'/bc/bsp/sno/ui/main.do'.strstr($this->sUrl, '?param=');
$sDomain = self::getDomain($this->sUrl);
$sDomain = $sDomain.(substr($sDomain, -1)=='/'?'':'/');
$sContent = $this->getPage();
$sContent = preg_replace('`(src|href|action|popup_emptydoc|emptyhoverurl|mimepath|stylepath)(\=|\:)\"(.*)\"`mUi', '$1$2"'.$sDomain.'$3"', $sContent);
$sContent = preg_replace('`(src|href|action|popup_emptydoc|emptyhoverurl|mimepath|stylepath)(\=|\:)\\\'(.*)\\\'`mUi', '$1$2"'.$sDomain.'$3"', $sContent);
$sContent = preg_replace('`url\((.*)\)`mUi', 'url('.$sDomain.'$1)"', $sContent);
$sContent = str_replace('.sap-ag.de//', '.sap-ag.de/', $sContent);
$sContent = str_replace('.sap-ag.de/1x1', '.sap-ag.de/'.$sFirstDir.'/bc/bsp/sno/ui/1x1', $sContent);
$sContent = str_replace("url = '/sap/support/notes/'", "url = '".$sDomain."sap/support/notes/'", $sContent);
//$sContent = str_replace('domainrelaxing:sapUrDomainRelaxing.NONE', 'domainrelaxing:sapUrDomainRelaxing.MAXIMAL', $sContent);
//Add customized javascript function
$sPre = '<script>window.onresize = setHeight;';
$sPost = '</script>';
$sInsert = ' parent.onEndOfProcess();';
$sContent = str_replace($sPre.$sPost, $sPre.$sInsert.$sPost, $sContent);
//Deleting auto resize
//$sContent = str_replace('lv_diff = 210','lv_diff = 0', $sContent);
$sContent = str_replace(array('setHeight();', 'window.onresize = setHeight; '), '', $sContent);
*/
}
else $this->addError('Empty note number');
return $sContent;
}
private function getPage($bHeader=false)
{
$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $this->sUrl);
curl_setopt($oCurl, CURLOPT_HEADER, $bHeader);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($oCurl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//if($bHeader) curl_setopt($oCurl, CURLOPT_FOLLOWLOCATION, true);
if($this->sCookie!='') curl_setopt($oCurl, CURLOPT_COOKIE, $this->sCookie);
curl_setopt($oCurl, CURLOPT_USERPWD, $this->sCredentials);
$sContent = curl_exec($oCurl);
curl_close($oCurl);
return $sContent;
}
private function getPageRecursive()
{
$sHeader = $this->getPage(true);
$asLines = array_filter(explode("\n", $sHeader));
foreach($asLines as $sLine) $asInfo[strtolower(strstr($sLine, ':', true))] = trim(substr(strstr($sLine, ':'), 1));
if(array_key_exists('set-cookie', $asInfo)) $this->parseSetCookie($asInfo['set-cookie']);
$bToBeContinued = array_key_exists('location', $asInfo);
if($bToBeContinued)
{
$this->sUrl = ((substr($asInfo['location'], 0, 4)!='http')?self::getDomain($this->sUrl):'').$asInfo['location'];
return $this->getPageRecursive();
}
else return implode("\n", $asLines);
}
private function parseSetCookie($sCookie)
{
//Store current cookie
$this->sCookie .= $sCookie;
//Parse FIXME
$asCookieInfo = explode('; ', $sCookie);
foreach($asCookieInfo as $sCookieInfo) $asCookie[strstr($sCookieInfo, '=', true)] = substr(strstr($sCookieInfo, '='), 1);
if(array_key_exists('sap-appcontext', $asCookie)) setcookie('sap-appcontext', $asCookie['sap-appcontext'], time()+3600, $asCookie['path']);
}
private function setCredentials()
{
$asLogins = array_keys(Settings::$OSS_ACCOUNTS);
$iIndex = rand(0, count($asLogins)-1);
$this->sCredentials = $asLogins[$iIndex].':'.Settings::$OSS_ACCOUNTS[$asLogins[$iIndex]];
}
private static function getDomain($sUrl)
{
$asUrl = parse_url($sUrl);
return $asUrl['scheme'].'://'.$asUrl['host'];
}
}
?>