<?php
if (class_exists('RvsLibs_Url') === false) {
class RvsLibs_Url
{
/**
* URL-encode according to RFC 1738
*
* @param string $str
* @return Returns a string in which all non-alphanumeric characters except -_. have been replaced with
* a percent (%) sign followed by two hex digits. This is the encoding described in
* » RFC 1738 for protecting literal characters from being interpreted as special URL delimiters
* , and for protecting URLs from being mangled by transmission media with character conversions
* (like some email systems).
*/
public static function rawurlencode($str)
{
if (RvsLibs_System::function_exists('rawurlencode') === true) {
if(RvsLibs_String::strtolower(RvsLibs_String::mb_detect_encoding($str)) !='utf-8') {
return rawurlencode(utf8_encode($str));
} else {
return rawurlencode($str);
}
} else {
$msg = RvsLibs_String::translate(
'Function %FUNCTION is not exists on your server.'
, 'vprintf'
, array('FUNCTION' => 'rawurlencode')
);
SGL::logMessage($msg, PEAR_LOG_ERR);
}
}
/**
* Decodes URL-encoded string
*
* @param string $string
* @return Returns the decoded string.
*/
public static function urldecode($string)
{
if (RvsLibs_System::function_exists('urldecode') === true) {
return urldecode($string);
} else {
$msg = RvsLibs_String::translate(
'Function %FUNCTION is not exists on your server.'
, 'vprintf'
, array('FUNCTION' => 'urldecode')
);
SGL::logMessage($msg, PEAR_LOG_ERR);
}
}
/**
* URL-encodes string
*
* @param string $str
* @return Returns a string in which all non-alphanumeric characters except -_.
* have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.
* It is encoded the same way that the posted data from a WWW form is encoded
* , that is the same way as in application/x-www-form-urlencoded media type.
* This differs from the » RFC 1738 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs.
*/
public static function url_encode($str)
{
if (RvsLibs_System::function_exists('urlencode') === true) {
return urlencode($str);
} else {
$msg = RvsLibs_String::translate(
'Function %FUNCTION is not exists on your server.'
, 'vprintf'
, array('FUNCTION' => 'urlencode')
);
SGL::logMessage($msg, PEAR_LOG_ERR);
}
}
/**
* Decode URL-encoded strings
*
* @param $string $str
* @return Returns the decoded URL, as a string.
* Note: rawurldecode() does not decode plus symbols ('+') into spaces. urldecode() does.
* have problem decode %80%80%80, %c0%bc
*/
public static function rawurldecode($str)
{
///TODO เป็น utf-8 แล้ว มีปัญหา decode %80%80%80, %c0%bc
if (RvsLibs_System::function_exists('rawurldecode') === true) {
return rawurldecode($str);
} else {
$msg = RvsLibs_String::translate(
'Function %FUNCTION is not exists on your server.'
, 'vprintf'
, array('FUNCTION' => 'rawurldecode')
);
SGL::logMessage($msg, PEAR_LOG_ERR);
}
}
public function urlencodeOutPlus($str)
{
$str = RvsLibs_Url::url_encode($str);
// optimize :nipaporn
return str_replace('+' , ' ' , $str);
}
/**
* Connect to URI page
*
* @param <string> $site
* @param <int> $port default = 80
* @param <string> $authorization defaule = null
* @param <bool> $useSSL
* @param <string> $request
*
CURLOPT_SSL_VERIFYHOST value
0: Don’t check the common name (CN) attribute
1: Check that the common name attribute at least exists
2: Check that the common name exists and that it matches the host name of the server
*
* @example Output Socket
*
* Host = www.b3ta.com
* Get = /404
*
* HTTP/1.1 404 Not Found
* Date: Thu, 09 Oct 2014 05:49:44 GMT
* Server: Apache
* X-Powered-By: PHP/5.3.3
* Content-Length: 3343
* Connection: close
* Content-Type: text/html; charset=iso-8859-1
*
* @return <string> $pages
*/
public static function urlConnect($site, $request, $port = 80, $authorization = null, $useSSL = false, $timeout = 30)
{
///Debug SGL::logMessage(null, PEAR_LOG_DEBUG);
$site = preg_replace('/https\:\/\//i', '', $site);
$site = preg_replace('/http\:\/\//i', '', $site);
$aPages = array(
'CONNECT_ERROR' => 0,
'data' => ''
);
if (RvsLibs_System::function_exists("curl_init")) {
SGL::logMessage("connect by curl_init", PEAR_LOG_DEBUG);
$ch = curl_init();
//Set timeout
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
if ($useSSL) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
$aUrl = RvsLibs_Url::getUrlConnect($site, 443, $request);
curl_setopt($ch, CURLOPT_URL, 'https://' . $aUrl['url']);
} else {
$aUrl = RvsLibs_Url::getUrlConnect($site, 80, $request);
curl_setopt($ch, CURLOPT_URL, $aUrl['url']);
}
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($authorization) {
$curlheaders[0] = 'Authorization: WHM ' . $authorization;
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlheaders);
}
if (RvsLibs_System::function_exists("curl_exec") == false) {
SGL::logMessage("cannot call curl_exec", PEAR_LOG_ERR);
}
$aPages['data'] = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$aPages['ERROR_CODE'] = $httpCode;
if (isset($httpCode) && $httpCode != '200') {
$aPages['CONNECT_ERROR'] = 1;
SGL::logMessage( 'Cannot connect to ' . $aUrl['url'] . ' using curl, please make sure ' . $aUrl['url'] . ' resolve to the server correctly.', PEAR_LOG_DEBUG);
}
curl_close($ch);
} elseif (RvsLibs_System::function_exists("fsockopen") ) {
SGL::logMessage("connect by fsockopen", PEAR_LOG_DEBUG);
$aUrl = RvsLibs_Url::getUrlConnect($site, $port, $request);
if ($authorization) {
$reqHeader = "GET " . $aUrl['request'] . " HTTP/1.0\n";
$reqHeader .= "Connection: close\n";
$reqHeader .= "Authorization: WHM $authorization\n\n";
} else {
$reqHeader = "GET " . $aUrl['request'] . " HTTP/1.0\r\n";
$reqHeader .= "Host: " . $aUrl['site'] . "\r\n";
$reqHeader .= "Connection: close\r\n";
$reqHeader .= "\r\n";
}
$socket = fsockopen($aUrl['site'], 80, $errno, $errstr, $timeout);
fputs($socket, $reqHeader);
if (!$socket) {
SGL::logMessage('Socket error Cannot connect to ' . $aUrl['site'] . $aUrl['request'] . ' using fsockopen, please make sure ' . $aUrl['site'] . ' resolve to the server correctly.' , PEAR_LOG_DEBUG);
$aPages['CONNECT_ERROR'] = 1;
$aPages['ERROR_CODE'] = 0;
$aPages['data'] = "$errstr ($errno)";
} else {
$pages = '';
$boolCheckHeader = true;
while (false !== ($next = fgets($socket, 13))) {
//SGL::logMessage("==== line:" . $next, PEAR_LOG_DEBUG);
$pages .= $next;
if (preg_match("/^HTTP\/1\./i", $next, $match) && $boolCheckHeader == true) {
if (preg_match("/301 (.*?)$/i", $next, $match)) {
SGL::logMessage("==== case: 301 line:" . $next, PEAR_LOG_ERR);
$aPages['CONNECT_ERROR'] = 1;
$aPages['ERROR_CODE'] = 301;
$boolCheckHeader = false;
} elseif (preg_match("/401 (.*?)$/i", $next, $match)) {
SGL::logMessage("==== case: 401 line:" . $next, PEAR_LOG_ERR);
$aPages['CONNECT_ERROR'] = 1;
$aPages['ERROR_CODE'] = 401;
$boolCheckHeader = false;
} elseif (preg_match("/404 (.*?)$/i", $next, $match)) {
SGL::logMessage("==== case: 401 line:" . $next, PEAR_LOG_ERR);
$aPages['CONNECT_ERROR'] = 1;
$aPages['ERROR_CODE'] = 404;
$boolCheckHeader = false;
} elseif (!preg_match("/200/i", $next, $match)) {
SGL::logMessage("==== case: NOT 200 line:" . $next, PEAR_LOG_ERR);
$aPages['CONNECT_ERROR'] = 1;
$aPages['ERROR_CODE'] = 0;
$boolCheckHeader = false;
}
// Etc. Else Validate Here...
}
}
}
//cut header $aData[0] = header data
$aData = explode("\r\n\r\n", $pages ,2);
if ($aData[1]){
$aPages['data'] = $aData[1];
} else {
$aPages['data'] = "";
SGL::logMessage('fsocket return empty data', PEAR_LOG_ERR);
}
fclose($socket);
} else {
SGL::logMessage('ERROR : php not compiled with --enable-sockets or curl', PEAR_LOG_ERR);
// return 'ERROR : php is not compiled with --enable-sockets and curl';
$aPages['CONNECT_ERROR'] = 1;
$aPages['ERROR_CODE'] = 0;
$aPages['data'] = 'ERROR : php is not compiled with curl or --enable-sockets';
}
return $aPages;
}
protected static function _curlConnect($site, $request, $port = 80, $authorization = null, $useSSL = false, $timeout = 30)
{
SGL::logMessage("connect by curl_init", PEAR_LOG_DEBUG);
$site = preg_replace('/https\:\/\//i', '', $site);
$site = preg_replace('/http\:\/\//i', '', $site);
$ch = curl_init();
//Set timeout
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
if ($useSSL) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
$aUrl = RvsLibs_Url::getUrlConnect($site, 443, $request);
curl_setopt($ch, CURLOPT_URL, 'https://' . $aUrl['url']);
} else {
$aUrl = RvsLibs_Url::getUrlConnect($site, 80, $request);
curl_setopt($ch, CURLOPT_URL, $aUrl['url']);
}
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($authorization) {
$curlheaders[0] = 'Authorization: WHM ' . $authorization;
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlheaders);
}
if (RvsLibs_System::function_exists("curl_exec") == false) {
SGL::logMessage("cannot call curl_exec", PEAR_LOG_ERR);
}
$aPages['data'] = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$aPages['ERROR_CODE'] = $httpCode;
if (isset($httpCode) && $httpCode != '200') {
$aPages['CONNECT_ERROR'] = 1;
SGL::logMessage( 'Cannot connect to ' . $aUrl['url'] . ' using curl, please make sure ' . $aUrl['url'] . ' resolve to the server correctly.', PEAR_LOG_DEBUG);
}
curl_close($ch);
return $aPages;
}
public static function urlConnectWithoutReplaceHeader($site, $request, $port = 80, $authorization = null, $useSSL = false, $timeout = 30)
{
$aPages = array();
if (RvsLibs_System::function_exists("curl_init")) {
$aPages = RvsLibs_Url::_curlConnect($site, $request, $port, $authorization, $useSSL, $timeout);
} elseif(RvsLibs_System::function_exists("fsockopen")) {
$aPages = RvsLibs_Url::urlConnect($site, $request, $port, $authorization, $useSSL, $timeout);
} else {
SGL::logMessage('ERROR : php not compiled with --enable- curl', PEAR_LOG_ERR);
$aPages['CONNECT_ERROR'] = 1;
return SGL::raiseError('ERROR : php is not compiled with --enable- curl');
}
return $aPages;
}
/**
* POST connect support long URL
*
* @param unknown $url
* @param string $request
* @param unknown $params
* @param number $timeout
* @return string|multitype:string number NULL unknown Ambigous <number, unknown>
*/
public static function postUrlConnect($url, $request = '', $params = array(), $timeout = 30)
{
SGL::logMessage("url:" . $url, PEAR_LOG_DEBUG);
SGL::logMessage("request:" . $request, PEAR_LOG_DEBUG);
$aPages = array();
$aPages['data'] = '';
$aPages['CONNECT_ERROR'] = 0;
if (RvsLibs_System::function_exists("curl_init")) {
SGL::logMessage("connect by curl_init", PEAR_LOG_DEBUG);
$postData = '';
$url = $url . $request;
//create name value pairs seperated by &
foreach($params as $k => $v)
{
$postData .= $k . '='.$v.'&';
}
rtrim($postData, '&');
SGL::logMessage("postData:" . $postData, PEAR_LOG_DEBUG);
$ch = curl_init();
//Set timeout
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
if (RvsLibs_System::function_exists("curl_exec") == false) {
SGL::logMessage("cannot call curl_exec", PEAR_LOG_ERR);
}
$aPages['data'] = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close ($ch);
# detect curl error
#
# 'Cannot curl to ' . $aUrl['url'] . $request . ' , please make sure ' . $aUrl['url'] ' resolve to the server correctly.'
if (!is_string($aPages['data']) || !RvsLibs_String::strlen($aPages['data']) || $aPages['data'] === false || $info['http_code'] != 200) {
$aPages['CONNECT_ERROR'] = 1;
$aPages['ERROR_CODE'] = isset($info['http_code']) ? $info['http_code'] : 0;
SGL::logMessage( 'Cannot connect to ' . $url . ' using curl, please make sure ' . $url . ' resolve to the server correctly.', PEAR_LOG_DEBUG);
}
} elseif(RvsLibs_System::function_exists("fsockopen") ) {
SGL::logMessage("connect by fsockopen", PEAR_LOG_DEBUG);
$site = preg_replace('/https\:\/\//i', '', $url);
$site = preg_replace('/http\:\/\//i', '', $site);
$fp = fsockopen($site, 80, $errno, $errstr, 30);
if (!$fp) {
# not sure validate $errno after fsockopen or after fclose.
# 'Cannot fsockopen to ' . $aUrl['site'] . $aUrl['request'] . ' , please make sure ' . $aUrl['site'] ' resolve to the server correctly.'
$aPages['CONNECT_ERROR'] = 1;
SGL::logMessage('Cannot connect to ' . $site . $request . ' using fsockopen, please make sure ' . $site . ' resolve to the server correctly.' , PEAR_LOG_DEBUG);
} else {
$content = http_build_query($params);
fwrite($fp, "POST $request HTTP/1.0\r\n");
fwrite($fp, "Host: $site\r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");
fwrite($fp, $content);
$page = '';
while (!feof($fp)) {
$page .= fgets($fp, 13);
}
fclose($fp);
$aData = explode("\r\n\r\n", $page ,2);
if ($aData[1]){
$aPages['data'] = $aData[1];
} else {
$aPages['data'] = "";
SGL::logMessage('fsocket return empty data', PEAR_LOG_ERR);
}
SGL::logMessage('fsocket data return:' . $aPages['data'], PEAR_LOG_DEBUG);
}
} else {
SGL::logMessage('ERROR : php not compiled with --enable-sockets or curl', PEAR_LOG_ERR);
return 'ERROR : php is not compiled with --enable-sockets and curl';
}
return $aPages;
}
public static function validateJsonData($page)
{
SGL::logMessage(null, PEAR_LOG_DEBUG);
$resData = '';
if (isset($page['data']) && $page['data'] != '') {
SGL::logMessage('validateJsonData =========', PEAR_LOG_DEBUG);
$resData = json_decode($page['data']);
if (json_decode($page['data']) && is_string($page['data'])) {
SGL::logMessage('validateJsonData case have key data and json_decode key data success', PEAR_LOG_DEBUG);
$resData = json_decode($page['data']);
SGL::logMessage('validateJsonData resData = ' . $resData, PEAR_LOG_DEBUG);
} elseif (is_string($page)) {
SGL::logMessage('validateJsonData case have key data and don json_decode key data success ', PEAR_LOG_DEBUG);
$resData = json_decode($page);
}
} else {
SGL::logMessage('$page ===========>' . $page['data'], PEAR_LOG_DEBUG);
}
return $resData;
}
/**
* ����Ţ port � URL ���١��
*/
public static function getUrlConnect($site, $port, $request)
{
///Debug SGL::logMessage(null, PEAR_LOG_DEBUG);
$request = (preg_match('/^\//', RvsLibs_String::trim($request))) ? $request : '/';
$aUrl = array();
$aSite = RvsLibs_String::dbeExplode('/', $site );
if ( is_array($aSite) && count($aSite) > 1) {
$k = 1;
$requestTxt = '';
while ( $k < count($aSite) ) {
$requestTxt .= "/" . $aSite[$k];
$k++;
}
$aUrl['site'] = $aSite[0];
$aUrl['port'] = $port;
$aUrl['request'] = $requestTxt . $request;
$aUrl['url'] = $aUrl['site'] . ":" . $port . $aUrl['request'];
} else {
$aUrl['site'] = $site;
$aUrl['port'] = $port;
$aUrl['request'] = $request;
$aUrl['url'] = $site . ":" . $port . $request;
}
SGL::logMessage("MODE " . CPMODE . " Full URL::" . $aUrl['url'], PEAR_LOG_DEBUG);
return $aUrl;
}
/**
* return url file manager
* Ex.cpanel is
* cpanel user: tryout in 192.168.1.3
* http://192.168.1.3:2082/frontend/rvskin/filemanager/index.html?dirselect=homedir&domainselect=tryout.siaminterhost.net&dir=%2Fhome%2Ftryout%2F.rvsitebuilder&showhidden=1
*
* @param unknown_type $username
* @return <string> $url
*/
public function getFileManagerUrlToDotRvsiteBuilder($username)
{
// optimize : nipaporn return url and modify CpHandle
$oCp = CpHandle::factory();
if(SGL::isError($oCp) === true) {
return $oCp;
/*
return SGL::raiseError(RvsLibs_String::translate('CpWaring %MESSAGE', 'vprintf', array('MESSAGE' => $oCp->getMessage())));
*/
}
return $oCp->getFileManagerUrlByUser($username);
}
/**
* get remote access key url:apiruk 2/12/2008
* if is reseller return url
* 1. case http://xxxxxxx.xxx:2082 replace to http://xxxxxxx.xxx:2086
* 2. case https://xxxxxx.xxx:2083 replace to https://xxxxxx.xxx:2087
* 3. case http://cpanel.xxxx.com replace to http://whm.xxxxx.xxxx
*
* @param <string> $cpmod
* @return $hashUrl
*/
public static function getHashUrl($cpmod=null)
{
/*modify CpHandle by nipaorn*/
$oCp = CpHandle::factory();
if(SGL::isError($oCp) === true) {
return $oCp;
/*
return SGL::raiseError(RvsLibs_String::translate('CpWaring %MESSAGE', 'vprintf', array('MESSAGE' => $oCp->getMessage())));
*/
}
return $oCp->getHashUrl();
}
public function getaddrbyhost($host, $timeout = 3) {
$query = `nslookup -timeout=$timeout -retry=1 $host`;
if(preg_match('/\nAddress: (.*)\n/', $query, $matches))
return trim($matches[1]);
return $host;
}
}
}
?>
Copyright 2K16 - 2K18 Indonesian Hacker Rulez