Index: core/install.php =================================================================== --- core/install.php (revision 14653) +++ core/install.php (working copy) @@ -390,7 +390,7 @@ $curl_helper->SetRequestData($url_params); $file_data = $curl_helper->Send(GET_LICENSE_URL); - if ( !$curl_helper->isGoodResponceCode() ) { + if ( !$curl_helper->isGoodResponseCode() ) { $this->errorMessage = 'In-Portal servers temporarily unavailable. Please contact In-Portal support personnel directly.'; } elseif (substr($file_data, 0, 5) == 'Error') { Index: core/install/install_schema.sql =================================================================== --- core/install/install_schema.sql (revision 14670) +++ core/install/install_schema.sql (working copy) @@ -1201,4 +1201,32 @@ KEY ExternalUrl (ExternalUrl), KEY RedirectOnIPMatch (RedirectOnIPMatch), KEY Priority (Priority) +); + +CREATE TABLE CurlLog ( + LogId int(11) NOT NULL AUTO_INCREMENT, + Message varchar(255) NOT NULL, + PageUrl varchar(255) NOT NULL, + RequestUrl varchar(255) NOT NULL, + PortalUserId int(11) NOT NULL, + SessionKey int(11) NOT NULL, + IsAdmin tinyint(4) NOT NULL, + PageData text, + RequestData text, + ResponseData text, + RequestDate int(11) DEFAULT NULL, + ResponseDate int(11) DEFAULT NULL, + ResponseHttpCode int(11) NOT NULL, + CurlError varchar(255) NOT NULL, + PRIMARY KEY (LogId), + KEY Message (Message), + KEY PageUrl (PageUrl), + KEY RequestUrl (RequestUrl), + KEY PortalUserId (PortalUserId), + KEY SessionKey (SessionKey), + KEY IsAdmin (IsAdmin), + KEY RequestDate (RequestDate), + KEY ResponseDate (ResponseDate), + KEY ResponseHttpCode (ResponseHttpCode), + KEY CurlError (CurlError) ); \ No newline at end of file Index: core/install/remove_schema.sql =================================================================== --- core/install/remove_schema.sql (revision 14653) +++ core/install/remove_schema.sql (working copy) @@ -69,3 +69,4 @@ DROP TABLE Semaphores; DROP TABLE CachedUrls; DROP TABLE SiteDomains; +DROP TABLE CurlLog; Index: core/install/upgrades.sql =================================================================== --- core/install/upgrades.sql (revision 14670) +++ core/install/upgrades.sql (working copy) @@ -2158,4 +2158,32 @@ ALTER TABLE Agents ADD Timeout INT(10) UNSIGNED NULL AFTER RunTime, ADD LastTimeoutOn int(10) unsigned default NULL AFTER Timeout, - ADD INDEX (Timeout); \ No newline at end of file + ADD INDEX (Timeout); + +CREATE TABLE CurlLog ( + LogId int(11) NOT NULL AUTO_INCREMENT, + Message varchar(255) NOT NULL, + PageUrl varchar(255) NOT NULL, + RequestUrl varchar(255) NOT NULL, + PortalUserId int(11) NOT NULL, + SessionKey int(11) NOT NULL, + IsAdmin tinyint(4) NOT NULL, + PageData text, + RequestData text, + ResponseData text, + RequestDate int(11) DEFAULT NULL, + ResponseDate int(11) DEFAULT NULL, + ResponseHttpCode int(11) NOT NULL, + CurlError varchar(255) NOT NULL, + PRIMARY KEY (LogId), + KEY Message (Message), + KEY PageUrl (PageUrl), + KEY RequestUrl (RequestUrl), + KEY PortalUserId (PortalUserId), + KEY SessionKey (SessionKey), + KEY IsAdmin (IsAdmin), + KEY RequestDate (RequestDate), + KEY ResponseDate (ResponseDate), + KEY ResponseHttpCode (ResponseHttpCode), + KEY CurlError (CurlError) +); Index: core/units/helpers/curl_helper.php =================================================================== --- core/units/helpers/curl_helper.php (revision 14653) +++ core/units/helpers/curl_helper.php (working copy) @@ -21,23 +21,23 @@ const REQUEST_METHOD_POST = 2; /** - * Connection to host + * ID of database record of currently active curl request * - * @var resource + * @var int * @access protected */ - protected $connectionID = null; + protected $logId = 0; /** - * Pointer to opened log file + * Connection to host * * @var resource * @access protected */ - protected $logFilePointer = null; + protected $connectionID = null; /** - * Responce waiting timeout in seconds + * Response waiting timeout in seconds * * @var int * @access public @@ -45,7 +45,7 @@ public $timeout = 90; /** - * Follow to url, if redirect received insted of document (only works when open_basedir and safe mode is off) + * Follow to url, if redirect received instead of document (only works when open_basedir and safe mode is off) * * @var bool * @access public @@ -53,12 +53,12 @@ public $followLocation = false; /** - * Last responce received by Curl + * Last response received by Curl * * @var string * @access public */ - public $lastRespoce = ''; + public $lastResponse = ''; /** * Last error code @@ -77,7 +77,7 @@ public $lastErrorMsg = ''; /** - * Most recent HTTP responce code received + * Most recent HTTP response code received * * @var int * @access public @@ -109,12 +109,12 @@ protected $requestHeaders = Array (); /** - * Responce headers + * Response headers * * @var Array * @access protected */ - protected $responceHeaders = Array (); + protected $responseHeaders = Array (); /** * CURL options @@ -132,6 +132,9 @@ */ public $debugMode = false; + /** + * Creates an instance of kCurlHelper class + */ public function __construct() { parent::__construct(); @@ -236,7 +239,7 @@ */ protected function ParseHeader(&$ch, $header) { - $this->responceHeaders[] = $header; + $this->responseHeaders[] = $header; return strlen($header); } @@ -323,47 +326,61 @@ * * @param string $url * @param bool $close_connection + * @param bool $log_status + * @param string $log_message * @return string * @access public */ - public function Send($url, $close_connection = true, $log_status = null) + public function Send($url, $close_connection = true, $log_status = null, $log_message = '') { - if (isset($log_status)) { + if ( isset($log_status) ) { // override debug mode setting $this->debugMode = $log_status; } - if (($this->requestMethod == self::REQUEST_METHOD_GET) && $this->requestData) { - // add query to url - $url .= (strpos($url, '?') !== false ? '&' : '?') . $this->requestData; - } - $this->connectionID = curl_init($url); - if ($this->debugMode) { - kUtil::safeDefine('DBG_CURL_LOGFILE', '/curl.log'); - $this->logFilePointer = fopen((defined('RESTRICTED') ? RESTRICTED : FULL_PATH) . DBG_CURL_LOGFILE, 'a'); + if ( $this->debugMode ) { + // collect page data + $page_data = Array (); - $user_id = $this->Application->RecallVar('user_id'); - $data = $_SERVER['REMOTE_ADDR'] . ' - ['.adodb_date('D M d H:i:s Y').'] ' . $_SERVER['REQUEST_URI'] . '; user_id: '.$user_id.'; sid: '.$this->Application->GetSID(); - fwrite($this->logFilePointer, "\n\n" . str_repeat('=', strlen($data)) . "\n"); - fwrite($this->logFilePointer, $data); - fwrite($this->logFilePointer, "\n" . str_repeat('=', strlen($data)) . "\n"); + if ( $_GET ) { + $page_data[] = '_GET:' . "\n" . print_r($_GET, true); + } - curl_setopt($this->connectionID, CURLOPT_FILE, $this->logFilePointer); - curl_setopt($this->connectionID, CURLOPT_VERBOSE, true); - curl_setopt($this->connectionID, CURLOPT_STDERR, $this->logFilePointer); - //curl_setopt($this->connectionID, CURLOPT_WRITEHEADER, $this->logFilePointer); + if ( $_POST ) { + $page_data[] = '_POST:' . "\n" . print_r($_POST, true); + } + + if ( $_COOKIE ) { + $page_data[] = '_COOKIE:' . "\n" . print_r($_COOKIE, true); + } + + // create log record + $fields_hash = Array ( + 'Message' => $log_message, + 'PageUrl' => $_SERVER['REQUEST_URI'], + 'RequestUrl' => $url, + 'PortalUserId' => $this->Application->RecallVar('user_id'), + 'SessionKey' => $this->Application->GetSID(), + 'IsAdmin' => $this->Application->isAdminUser ? 1 : 0, + 'PageData' => implode("\n", $page_data), + 'RequestData' => $this->requestData, + 'RequestDate' => adodb_mktime(), + ); + + $this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'CurlLog'); + $this->logId = $this->Conn->getInsertID(); } - $this->responceHeaders = Array (); + $this->responseHeaders = Array (); $this->prepareOptions(); - $this->lastRespoce = curl_exec($this->connectionID); + $this->lastResponse = curl_exec($this->connectionID); $this->Finalize($close_connection); - return $this->lastRespoce; + return $this->lastResponse; } /** @@ -383,7 +400,9 @@ /** * Finalizes curl request and saves some data from curl before closing connection * - * @param int $close_connection + * @param bool $close_connection + * @return void + * @access public */ public function Finalize($close_connection = true) { @@ -391,7 +410,7 @@ $this->lastErrorMsg = curl_error($this->connectionID); $this->lastHTTPCode = curl_getinfo($this->connectionID, CURLINFO_HTTP_CODE); - if ($close_connection) { + if ( $close_connection ) { $this->CloseConnection(); } @@ -407,20 +426,30 @@ { curl_close($this->connectionID); - if ($this->debugMode) { - // only close log after curl resource has been terminated - fwrite($this->logFilePointer, "\n" . 'LastHTTPCode: ' . $this->lastHTTPCode . '; LastError: #' . $this->lastErrorCode . ' (' . $this->lastErrorMsg . ')' . "\n"); - fwrite($this->logFilePointer, 'Respoce:' . "\n" . $this->lastRespoce); - fclose($this->logFilePointer); + if ( $this->debugMode ) { + $fields_hash = Array ( + 'ResponseData' => $this->lastResponse, + 'ResponseDate' => adodb_mktime(), + 'ResponseHttpCode' => $this->lastHTTPCode, + 'CurlError' => $this->lastErrorCode != 0 ? '#' . $this->lastErrorCode . ' (' . $this->lastErrorMsg . ')' : '', + ); + + $this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'CurlLog', 'LogId = ' . $this->logId); } // restore debug mode setting $this->debugMode = kUtil::constOn('DBG_CURL'); } - - function isGoodResponceCode() + + /** + * Checks, that last curl request was successful + * + * @return bool + * @access public + */ + public function isGoodResponseCode() { - if ($this->lastErrorCode != 0) { + if ( $this->lastErrorCode != 0 ) { return false; } Index: tools/debug_sample.php =================================================================== --- tools/debug_sample.php (revision 14653) +++ tools/debug_sample.php (working copy) @@ -19,6 +19,7 @@ // define('DBG_CAPTURE_STATISTICS', 1); // Capture performance statistics // define('DBG_MAX_SQL_TIME', 2); // Maximal allowed sql execution time in seconds, all sqls above this become slow sqls // define('DBG_RESET_ROOT', 1); // Shows "root" user password reset link on Admin Console login screen +// define('DBG_CURL', 1); // Log all curl requests to CurlLog database table $dbg_options = Array ( // !!! DEBUG MODE will be off if IP does not match !!!