From 8e44dd147dbf90b9e58f46b2fd7461d1f481631b Mon Sep 17 00:00:00 2001 From: Cameron Date: Wed, 1 Jun 2016 10:41:47 -0700 Subject: [PATCH] CURL init cleanup. --- class2.php | 47 ++++++----- e107_handlers/e_marketplace.php | 8 +- e107_handlers/file_class.php | 143 ++++++++++++++++++-------------- e107_handlers/xml_class.php | 120 +-------------------------- 4 files changed, 113 insertions(+), 205 deletions(-) diff --git a/class2.php b/class2.php index e5d88f743..11d88a69a 100644 --- a/class2.php +++ b/class2.php @@ -407,14 +407,14 @@ e107::getSingleton('e107_traffic'); // We start traffic counting ASAP $sql = e107::getDb(); //TODO - find & replace $sql, $e107->sql $sql->db_SetErrorReporting(FALSE); -$sql->db_Mark_Time('Start: SQL Connect'); +$sql->db_Mark_Time('SQL Connect'); $merror=$sql->db_Connect($mySQLserver, $mySQLuser, $mySQLpassword, $mySQLdefaultdb); // create after the initial connection. //DEPRECATED, BC, call the method only when needed $sql2 = e107::getDb('sql2'); //TODO find & replace all $sql2 calls -$sql->db_Mark_Time('Start: Prefs, misc tables'); +$sql->db_Mark_Time('Prefs, misc tables'); //DEPRECATED, BC, call the method only when needed, $e107->admin_log caught by __get() $admin_log = e107::getAdminLog(); //TODO - find & replace $admin_log, $e107->admin_log @@ -440,7 +440,7 @@ e107_require_once(e_HANDLER.'php_compatibility_handler.php'); // // L: Extract core prefs from the database // -$sql->db_Mark_Time('Start: Extract Core Prefs'); +$sql->db_Mark_Time('Extract Core Prefs'); // TODO - remove it from here, auto-loaded when required e107_require_once(e_HANDLER.'cache_handler.php'); @@ -527,7 +527,7 @@ if(e107::getPref('ssl_enabled') && !deftrue('e_SSL_DISABLE')) // $sql->db_Mark_Time('(Extracting Core Prefs Done)'); -$sql->db_Mark_Time('Start: Init Language and detect changes'); +$sql->db_Mark_Time('Init Language and detect changes'); $lng = e107::getLanguage(); // required for v1.0 BC. $lng->detect(); @@ -622,7 +622,7 @@ if(!empty($pref['redirectsiteurl']) && !empty($pref['siteurl'])) { // to avoid multi-language 'access-denied' issues. //session_start(); see e107::getSession() above e107::getSession(); //init core _SESSION - actually here for reference only, it's done by language handler set() method -$sql->db_Mark_Time('Start: Set User Language Session'); +$sql->db_Mark_Time('Set User Language Session'); e107::getLanguage()->set(); // set e_LANGUAGE, USERLAN, Language Session / Cookies etc. requires $pref; if(varset($pref['multilanguage']) && (e_LANGUAGE != $pref['sitelanguage'])) @@ -635,7 +635,7 @@ if(varset($pref['multilanguage']) && (e_LANGUAGE != $pref['sitelanguage'])) // e107_include_once(e_LANGUAGEDIR.e_LANGUAGE.'/'.e_LANGUAGE.'.php'); // e107_include_once(e_LANGUAGEDIR.e_LANGUAGE.'/'.e_LANGUAGE.'_custom.php'); // v1 Custom language File Path. -$sql->db_Mark_Time('Start: Include Global Core Language Files'); +$sql->db_Mark_Time('Include Global Core Language Files'); if((e_ADMIN_AREA === true) && !empty($pref['adminlanguage'])) { include(e_LANGUAGEDIR.$pref['adminlanguage'].'/'.$pref['adminlanguage'].'.php'); @@ -662,7 +662,7 @@ unset($customLan, $customLan2); $lng->bcDefs(); // defined v1.x definitions for old templates. -$sql->db_Mark_Time('Start: Include Global Plugin Language Files'); +$sql->db_Mark_Time('Include Global Plugin Language Files'); if(isset($pref['lan_global_list'])) { foreach($pref['lan_global_list'] as $path) @@ -673,7 +673,7 @@ if(isset($pref['lan_global_list'])) -$sql->db_Mark_Time('Start: CHAP challenge'); +$sql->db_Mark_Time('CHAP challenge'); $die = (e_AJAX_REQUEST == true) ? false : true; e107::getSession() @@ -684,7 +684,7 @@ unset($die); // // N: misc setups: online user tracking, cache // -$sql->db_Mark_Time('Start: Misc resources. Online user tracking, cache'); +$sql->db_Mark_Time('Misc resources. Online user tracking, cache'); /** @@ -698,15 +698,15 @@ $override = e107::getSingleton('override', true); //TODO - find & replace $overr //DEPRECATED, BC, call the method only when needed, $e107->user_class caught by __get() $e_userclass = e107::getUserClass(); //TODO - find & replace $e_userclass, $e107->user_class -$sql->db_Mark_Time('Start: Init Event Handler'); +$sql->db_Mark_Time('Init Event Handler'); e107::getEvent()->init(); -$sql->db_Mark_Time('Start: Register Core Events'); +$sql->db_Mark_Time('Register Core Events'); e107::getNotify()->registerEvents(); // // O: Start user session // -$sql -> db_Mark_Time('Start: User session'); +$sql -> db_Mark_Time('User session'); init_session(); // Set up a lot of the user-related constants @@ -781,7 +781,7 @@ if (isset($pref['modules']) && $pref['modules']) { } */ -$sql->db_Mark_Time('Start: Load Plugin Modules'); +$sql->db_Mark_Time('Load Plugin Modules'); $js_body_onload = array(); // Initialise this array in case a module wants to add to it @@ -803,7 +803,7 @@ if(isset($pref['e_module_list']) && $pref['e_module_list']) // P: THEME LOADING // -$sql->db_Mark_Time('Start: Load Theme'); +$sql->db_Mark_Time('Load Theme'); //########### Module redefinable functions ############### if (!function_exists('checkvalidtheme')) @@ -886,7 +886,7 @@ if (!function_exists('checkvalidtheme')) // // Q: ALL OTHER SETUP CODE // -$sql->db_Mark_Time('Start: Misc Setup'); +$sql->db_Mark_Time('Misc Setup'); //------------------------------------------------------------------------------------------------------------------------------------// if (!class_exists('e107table', false)) @@ -1017,7 +1017,7 @@ $ns = e107::getRender(); //TODO - find & replace $ns, $e107->ns // EONE-134 - bad e_module could destroy e107 instance $e107 = e107::getInstance(); // Is this needed now? -$sql->db_Mark_Time('Start: IP Handler and Ban Check'); +$sql->db_Mark_Time('IP Handler and Ban Check'); e107::getIPHandler()->ban(); if(varset($pref['force_userupdate']) && USER && !isset($_E107['no_forceuserupdate']) && $_SERVER['QUERY_STRING'] !== 'logout') @@ -1029,7 +1029,7 @@ if(varset($pref['force_userupdate']) && USER && !isset($_E107['no_forceuserupdat } } -$sql->db_Mark_Time('Start: Signup/splash/admin'); +$sql->db_Mark_Time('Signup/splash/admin'); if(($pref['membersonly_enabled'] && !isset($_E107['allow_guest'])) || ($pref['maintainance_flag'] && empty($_E107['cli']) && empty($_E107['no_maintenance']))) @@ -1047,7 +1047,7 @@ if(!isset($_E107['no_prunetmp'])) } -$sql->db_Mark_Time('Start: Login/logout/ban/tz'); +$sql->db_Mark_Time('Login/logout/ban/tz'); if (isset($_POST['userlogin']) || isset($_POST['userlogin_x'])) @@ -1139,7 +1139,7 @@ define('TIMEOFFSET', $e_deltaTime); // ---------------------------------------------------------------------------- -$sql->db_Mark_Time('Start: Find/Load Theme'); +$sql->db_Mark_Time('Find/Load Theme'); if(e_ADMIN_AREA) // Load admin phrases ASAP { @@ -1175,7 +1175,7 @@ if(!defined('THEME')) $theme_pref = varset($pref['sitetheme_pref']); // -------------------------------------------------------------- -$sql->db_Mark_Time('Start: Find/Load Theme-Layout'); // needs to run after checkvalidtheme() (for theme previewing). +$sql->db_Mark_Time('Find/Load Theme-Layout'); // needs to run after checkvalidtheme() (for theme previewing). if(!defined("THEME_LAYOUT")) { @@ -1258,15 +1258,17 @@ if(!defined("THEME_LAYOUT")) // ----------------------------------------------------------------------- -$sql->db_Mark_Time('Start: Get menus'); + if(!isset($_E107['no_menus'])) { + $sql->db_Mark_Time('Init Menus'); e107::getMenu()->init(); } // here we USE the theme if(e_ADMIN_AREA) { + $sql->db_Mark_Time('Loading Admin Theme'); if(file_exists(THEME.'admin_theme.php')&&(strpos(e_SELF.'?'.e_QUERY, $ADMIN_DIRECTORY.'menus.php?configure')===FALSE)) // no admin theme when previewing. { require_once (THEME.'admin_theme.php'); @@ -1278,6 +1280,7 @@ if(e_ADMIN_AREA) } else { + $sql->db_Mark_Time('Loading Site Theme'); require_once (THEME.'theme.php'); if(isset($SC_WRAPPER)) { @@ -1835,7 +1838,7 @@ function init_session() } -$sql->db_Mark_Time('Start: Go online'); +$sql->db_Mark_Time('Go online'); if(!isset($_E107['no_online']) && varset($pref['track_online'])) { e107::getOnline()->goOnline($pref['track_online'], $pref['flood_protect']); diff --git a/e107_handlers/e_marketplace.php b/e107_handlers/e_marketplace.php index 2dec0ec66..4dd9fa044 100644 --- a/e107_handlers/e_marketplace.php +++ b/e107_handlers/e_marketplace.php @@ -392,8 +392,10 @@ abstract class e_marketplace_adapter_abstract $fp = fopen($path.$local_file, 'w'); // media-directory is the root. //$fp1 = fopen(e_TEMP.'/curllog.txt', 'w'); - - $cp = curl_init($remote_url); + + + $cp = e107::getFile()->initCurl($remote_url); + /* $cp = curl_init($remote_url); curl_setopt($cp, CURLOPT_FILE, $fp); //curl_setopt($ch, CURLOPT_VERBOSE, 1); @@ -402,7 +404,7 @@ abstract class e_marketplace_adapter_abstract curl_setopt($cp, CURLOPT_REFERER, e_REQUEST_HTTP); curl_setopt($cp, CURLOPT_HEADER, 0); curl_setopt($cp, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); - curl_setopt($cp, CURLOPT_COOKIEFILE, e_SYSTEM.'cookies.txt'); + curl_setopt($cp, CURLOPT_COOKIEFILE, e_SYSTEM.'cookies.txt');*/ $buffer = curl_exec($cp); diff --git a/e107_handlers/file_class.php b/e107_handlers/file_class.php index 1be5cd49b..7e6f98805 100644 --- a/e107_handlers/file_class.php +++ b/e107_handlers/file_class.php @@ -437,14 +437,18 @@ class e_file } $fp = fopen($path.$local_file, 'w'); // media-directory is the root. - - $cp = curl_init($remote_url); - curl_setopt($cp, CURLOPT_FILE, $fp); + + $cp = $this->initCurl($remote_url); + + /* + $cp = curl_init($remote_url); + curl_setopt($cp, CURLOPT_FILE, $fp); curl_setopt($cp, CURLOPT_REFERER, e_REQUEST_HTTP); curl_setopt($cp, CURLOPT_HEADER, 0); curl_setopt($cp, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); curl_setopt($cp, CURLOPT_COOKIEFILE, e_SYSTEM.'cookies.txt'); curl_setopt($cp, CURLOPT_SSL_VERIFYPEER, FALSE); + */ $buffer = curl_exec($cp); @@ -453,7 +457,76 @@ class e_file return ($buffer) ? true : false; } - + + /** + * @param string $address + * @param array|null $options + */ + function initCurl($address, $options =null) + { + $cu = curl_init(); + + $timeout = (integer) vartrue($options['timeout'], 10); + $timeout = min($timeout, 120); + $timeout = max($timeout, 3); + + $urlData = parse_url($address); + $referer = $urlData['scheme']."://".$urlData['host']; + + if(empty($referer)) + { + $referer = e_REQUEST_HTTP; + } + + curl_setopt($cu, CURLOPT_URL, $address); + curl_setopt($cu, CURLOPT_TIMEOUT, $timeout); + curl_setopt($cu, CURLOPT_RETURNTRANSFER, true); + curl_setopt($cu, CURLOPT_HEADER, 0); + curl_setopt($cu, CURLOPT_REFERER, $referer); + curl_setopt($cu, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($cu, CURLOPT_FOLLOWLOCATION, 0); + curl_setopt($cu, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); + curl_setopt($cu, CURLOPT_COOKIEFILE, e_SYSTEM.'cookies.txt'); + curl_setopt($cu, CURLOPT_COOKIEJAR, e_SYSTEM.'cookies.txt'); + + if(defined('e_CURL_PROXY')) + { + curl_setopt($cu, CURLOPT_PROXY, e_CURL_PROXY); // PROXY details with port + } + + if(defined('e_CURL_PROXYUSERPWD')) + { + curl_setopt($cu, CURLOPT_PROXYUSERPWD, e_CURL_PROXYUSERPWD); // Use if proxy have username and password + } + + if(defined('e_CURL_PROXYTYPE')) + { + curl_setopt($cu, CURLOPT_PROXYTYPE, e_CURL_PROXYTYPE); // If expected to cal + } + + if(!empty($options['post'])) + { + curl_setopt($cu, CURLOPT_POST, true); + // if array -> will encode the data as multipart/form-data, if URL-encoded string - application/x-www-form-urlencoded + curl_setopt($cu, CURLOPT_POSTFIELDS, $options['post']); + } + + if(isset($options['header']) && is_array($options['header'])) + { + curl_setopt($cu, CURLOPT_HTTPHEADER, $options['header']); + } + + if(!file_exists(e_SYSTEM.'cookies.txt')) + { + file_put_contents(e_SYSTEM.'cookies.txt',''); + } + + return $cu; + + } + + + /** * FIXME add POST support * Get Remote contents @@ -468,10 +541,7 @@ class e_file function getRemoteContent($address, $options = array()) { // Could do something like: if ($timeout <= 0) $timeout = $pref['get_remote_timeout']; here - $postData = varset($options['post'], null); - $timeout = (integer) vartrue($options['timeout'], 10); - $timeout = min($timeout, 120); - $timeout = max($timeout, 3); + $fileContents = ''; $this->error = ''; $this->errornum = null; @@ -484,63 +554,12 @@ class e_file if(vartrue($options['decode'], false)) $address = urldecode($address); - $urlData = parse_url($address); - $referer = $urlData['scheme']."://".$urlData['host']; - - if(empty($referer)) - { - $referer = e_REQUEST_HTTP; - } - - - // Keep this in first position. + // Keep this in first position. if (function_exists("curl_init")) // Preferred. { - $cu = curl_init(); - curl_setopt($cu, CURLOPT_URL, $address); - curl_setopt($cu, CURLOPT_RETURNTRANSFER, true); - curl_setopt($cu, CURLOPT_HEADER, 0); - curl_setopt($cu, CURLOPT_TIMEOUT, $timeout); - curl_setopt($cu, CURLOPT_SSL_VERIFYPEER, FALSE); - curl_setopt($cu, CURLOPT_REFERER, $referer); - curl_setopt($cu, CURLOPT_FOLLOWLOCATION, 0); - curl_setopt($cu, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); - curl_setopt($cu, CURLOPT_COOKIEFILE, e_SYSTEM.'cookies.txt'); - curl_setopt($cu, CURLOPT_COOKIEJAR, e_SYSTEM.'cookies.txt'); - if(defined('e_CURL_PROXY')) - { - curl_setopt($cu, CURLOPT_PROXY, e_CURL_PROXY); // PROXY details with port - } + $cu = $this->initCurl($address, $options); - if(defined('e_CURL_PROXYUSERPWD')) - { - curl_setopt($cu, CURLOPT_PROXYUSERPWD, e_CURL_PROXYUSERPWD); // Use if proxy have username and password - } - - if(defined('e_CURL_PROXYTYPE')) - { - curl_setopt($cu, CURLOPT_PROXYTYPE, e_CURL_PROXYTYPE); // If expected to cal - } - - if($postData !== null) - { - curl_setopt($cu, CURLOPT_POST, true); - // if array -> will encode the data as multipart/form-data, if URL-encoded string - application/x-www-form-urlencoded - curl_setopt($cu, CURLOPT_POSTFIELDS, $postData); - $requireCurl = true; - } - if(isset($options['header']) && is_array($options['header'])) - { - curl_setopt($cu, CURLOPT_HTTPHEADER, $options['header']); - $requireCurl = true; - } - - if(!file_exists(e_SYSTEM.'cookies.txt')) - { - file_put_contents(e_SYSTEM.'cookies.txt',''); - } - $fileContents = curl_exec($cu); if (curl_error($cu)) { @@ -555,6 +574,8 @@ class e_file // CURL is required, abort... if($requireCurl == true) return false; + $timeout = 5; + if (function_exists('file_get_contents') && ini_get('allow_url_fopen')) { $old_timeout = e107_ini_set('default_socket_timeout', $timeout); diff --git a/e107_handlers/xml_class.php b/e107_handlers/xml_class.php index cbf209f6b..7dfb98c9f 100644 --- a/e107_handlers/xml_class.php +++ b/e107_handlers/xml_class.php @@ -435,128 +435,10 @@ class xmlClass { $_file = e107::getFile(); $this->xmlFileContents = $_file->getRemoteContent($address, array('timeout' => $timeout, 'post' => $postData)); - $this->error = $_file->error; + $this->error = $_file->getErrorMessage(); return $this->xmlFileContents; - - // ------ MOVED TO FILE HANDLER ------ // - // Could do something like: if ($timeout <= 0) $timeout = $pref['get_remote_timeout']; here - $timeout = min($timeout, 120); - $timeout = max($timeout, 3); - $this->xmlFileContents = ''; - - $mes = e107::getMessage(); - - if($this->_feedUrl) // override option for use when part of the address needs to be encoded. - { - $mes->addDebug("getting Remote File: ".$this->_feedUrl); - } - else - { - $address = str_replace(array("\r", "\n", "\t"), '', $address); // May be paranoia, but streaky thought it might be a good idea - // ... and there shouldn't be unprintable characters in the URL anyway - } - - if($this->urlPrefix !== false) - { - $address = $this->urlPrefix.$address; - } - - - // ... and there shouldn't be unprintable characters in the URL anyway - - - // Keep this in first position. - if (function_exists("curl_init")) // Preferred. - { - $cu = curl_init(); - curl_setopt($cu, CURLOPT_URL, $address); - curl_setopt($cu, CURLOPT_RETURNTRANSFER, true); - curl_setopt($cu, CURLOPT_HEADER, 0); - curl_setopt($cu, CURLOPT_TIMEOUT, $timeout); - curl_setopt($cu, CURLOPT_SSL_VERIFYPEER, FALSE); - curl_setopt($cu, CURLOPT_REFERER, e_REQUEST_HTTP); - curl_setopt($cu, CURLOPT_FOLLOWLOCATION, 0); - curl_setopt($cu, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); - curl_setopt($cu, CURLOPT_COOKIEFILE, e_SYSTEM.'cookies.txt'); - curl_setopt($cu, CURLOPT_COOKIEJAR, e_SYSTEM.'cookies.txt'); - - if(!file_exists(e_SYSTEM.'cookies.txt')) - { - file_put_contents(e_SYSTEM.'cookies.txt',''); - } - - $this->xmlFileContents = curl_exec($cu); - if (curl_error($cu)) - { - $this->error = "Curl error: ".curl_errno($cu).", ".curl_error($cu); - return FALSE; - } - curl_close($cu); - return $this->xmlFileContents; - } - - if (function_exists('file_get_contents') && ini_get('allow_url_fopen')) - { - $old_timeout = e107_ini_set('default_socket_timeout', $timeout); - $address = ($this->_feedUrl) ? $this->_feedUrl : urldecode($address); - - $data = file_get_contents($address); - - // $data = file_get_contents(htmlspecialchars($address)); // buggy - sometimes fails. - if ($old_timeout !== FALSE) - { - e107_ini_set('default_socket_timeout', $old_timeout); - } - if ($data !== FALSE) - { - $this->xmlFileContents = $data; - return $data; - } - $this->error = "File_get_contents(XML) error"; // Fill in more info later - return FALSE; - } - - if (ini_get("allow_url_fopen")) - { - $old_timeout = e107_ini_set('default_socket_timeout', $timeout); - $remote = @fopen($address, "r"); - if (!$remote) - { - $this->error = "fopen: Unable to open remote XML file: ".$address; - return FALSE; - } - } - else - { - $old_timeout = $timeout; - $tmp = parse_url($address); - if (!$remote = fsockopen($tmp['host'], 80, $errno, $errstr, $timeout)) - { - $this->error = "Sockets: Unable to open remote XML file: ".$address; - return FALSE; - } - else - { - socket_set_timeout($remote, $timeout); - fputs($remote, "GET ".urlencode($address)." HTTP/1.0\r\n\r\n"); - } - } - $this->xmlFileContents = ""; - while (!feof($remote)) - { - $this->xmlFileContents .= fgets($remote, 4096); - } - fclose($remote); - if ($old_timeout != $timeout) - { - if ($old_timeout !== FALSE) - { - e107_ini_set('default_socket_timeout', $old_timeout); - } - } - return $this->xmlFileContents; } /**