Skip to content
Permalink
Browse files
Merge pull request #695 from tarasfrompir/patch-26
Update hooks.inc.php
  • Loading branch information
sergejey committed Feb 21, 2020
2 parents 219fabe + 5d0d0cf commit cad6fd0ddfd037e10ed228ad21e3dae6feb2d4cf
Showing 2 changed files with 126 additions and 8 deletions.
@@ -77,14 +77,9 @@ function processSubscriptionsSafe($event_name, $details = '')
if (session_id()) {
$data[session_name()] = session_id();
}
$url = BASE_URL . '/objects/?' . http_build_query($data);
if (is_array($params)) {
foreach ($params as $k => $v) {
$url .= '&' . $k . '=' . urlencode($v);
}
}
$result = getURLBackground($url, 0);
return $result;
$url = BASE_URL . ROOTHTML . 'objects/?';
postURLBackground($url, $data);
return 1;
}

function processSubscriptionByModule($module_name,$event_name, &$details) {
@@ -357,3 +357,126 @@ function setTerminalMML($host = 'localhost', $mml=0) {
SQLUpdate('terminals', $terminal);
return true;
}

function postURLBackground($url, $query = array(), $cache = 0, $username = '', $password = '')
{
//DebMes("URL: ".$url,'debug1');
postURL($url, $query , $cache, $username, $password, true);
}

/**
* Summary of postURL
* @param mixed $url Url
* @param mixed $query query
* @param mixed $cache Cache (default 0)
* @param mixed $username User name (default '')
* @param mixed $password Password (default '')
* @return mixed
*/
function postURL($url, $query = array(), $cache = 0, $username = '', $password = '', $background = false)
{
startMeasure('getURL');
// DebMes($url,'urls');
$filename_part = preg_replace('/\W/is', '_', str_replace('http://', '', $url));
if (strlen($filename_part) > 200) {
$filename_part = substr($filename_part, 0, 200) . md5($filename_part);
}
$cache_file = ROOT . 'cms/cached/urls/' . $filename_part . '.html';

if (!$cache || !is_file($cache_file) || ((time() - filemtime($cache_file)) > $cache)) {
try {

//DebMes('Geturl started for '.$url. ' Source: ' .debug_backtrace()[1]['function'], 'geturl');
startMeasure('curl_prepare');
$ch = curl_init();
@curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // connection timeout
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 45); // operation timeout 45 seconds
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // bad style, I know...
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

if ($background) {
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 50);
}

if ($username != '' || $password != '') {
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
}

$url_parsed = parse_url($url);
$host = $url_parsed['host'];

$use_proxy = false;
if (defined('USE_PROXY') && USE_PROXY != '') {
$use_proxy = true;
}

if ($host == '127.0.0.1' || $host == 'localhost') {
$use_proxy = false;
}

if ($use_proxy && defined('HOME_NETWORK') && HOME_NETWORK != '') {
$p = preg_quote(HOME_NETWORK);
$p = str_replace('\*', '\d+?', $p);
$p = str_replace(',', ' ', $p);
$p = str_replace(' ', ' ', $p);
$p = str_replace(' ', '|', $p);
if (preg_match('/' . $p . '/is', $host)) {
$use_proxy = false;
}
}

if ($use_proxy) {
curl_setopt($ch, CURLOPT_PROXY, USE_PROXY);
if (defined('USE_PROXY_AUTH') && USE_PROXY_AUTH != '') {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, USE_PROXY_AUTH);
}
}

$tmpfname = ROOT . 'cms/cached/cookie.txt';
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfname);
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfname);

endMeasure('curl_prepare');
startMeasure('curl_exec');
$result = curl_exec($ch);
endMeasure('curl_exec');


startMeasure('curl_post');
if (!$background && curl_errno($ch)) {
$errorInfo = curl_error($ch);
$info = curl_getinfo($ch);
$backtrace = debug_backtrace();
$callSource = $backtrace[1]['function'];
DebMes("GetURL to $url (source " . $callSource . ") finished with error: \n" . $errorInfo . "\n" . json_encode($info),'geturl_error');
}
curl_close($ch);
endMeasure('curl_post');


} catch (Exception $e) {
registerError('geturl', $url . ' ' . get_class($e) . ', ' . $e->getMessage());
}

if ($cache > 0) {
CreateDir(ROOT . 'cms/cached/urls');
SaveFile($cache_file, $result);
}
} else {
$result = LoadFile($cache_file);
}


endMeasure('getURL');

return $result;
}

0 comments on commit cad6fd0

Please sign in to comment.