Index: branches/5.2.x/core/units/helpers/curl_helper.php =================================================================== diff -u -N -r14092 -r14095 --- branches/5.2.x/core/units/helpers/curl_helper.php (.../curl_helper.php) (revision 14092) +++ branches/5.2.x/core/units/helpers/curl_helper.php (.../curl_helper.php) (revision 14095) @@ -1,6 +1,6 @@ debugMode = constOn('DBG_CURL'); + $this->debugMode = kUtil::constOn('DBG_CURL'); } /** * Reset connection settings (not results) after connection was closed * + * @access protected */ - function _resetSettings() + protected function _resetSettings() { $this->timeout = 90; $this->followLocation = false; - $this->postData = ''; + $this->requestMethod = self::REQUEST_METHOD_GET; + $this->requestData = ''; $this->requestHeaders = Array (); $this->options = Array (); } - function setOptions($options_hash) + /** + * Sets CURL options (adds to options set before) + * + * @param Array $options_hash + * @access public + */ + public function setOptions($options_hash) { - $this->options = array_merge_recursive2($this->options, $options_hash); + $this->options = kUtil::array_merge_recursive($this->options, $options_hash); } - function prepareOptions() + /** + * Combines user-defined and default options before setting them to CURL + * + * @access protected + */ + protected function prepareOptions() { if ($this->followLocation && ((defined('SAFE_MODE') && SAFE_MODE) || ini_get('open_basedir'))) { // this won't work with such restrictions, so turn it off @@ -133,7 +186,7 @@ CURLOPT_RETURNTRANSFER => 1, CURLOPT_REFERER => PROTOCOL.SERVER_NAME, CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'], - + // don't verify SSL certificates CURLOPT_SSL_VERIFYPEER => false, CURLOPT_HTTPHEADER => Array ('Expect:'), @@ -144,9 +197,9 @@ } // if we have post data, then POST else use GET method instead - if ($this->postData) { + if ($this->requestMethod == self::REQUEST_METHOD_POST) { $default_options[CURLOPT_POST] = 1; - $default_options[CURLOPT_POSTFIELDS] = $this->postData; + $default_options[CURLOPT_POSTFIELDS] = $this->requestData; } // $default_options[CURLOPT_HEADERFUNCTION] = Array(&$this, 'ParseHeader'); @@ -157,74 +210,134 @@ $this->applyOptions(); } - function applyOptions() + /** + * Sets prepared options to CURL + * + * @access protected + */ + protected function applyOptions() { foreach ($this->options as $option_name => $option_value) { curl_setopt($this->connectionID, $option_name, $option_value); } } - function ParseHeader(&$ch, $header) + /** + * Parses headers from CURL request + * + * @param resource $ch + * @param string $header + * @return int + * @access protected + */ + protected function ParseHeader(&$ch, $header) { $this->responceHeaders[] = $header; + return strlen($header); } /** - * Sets POST data for next query + * Sets request data for next query * - * @param mixed $post_data Array or string + * @param mixed $data Array or string */ - function SetPostData($post_data) + public function SetRequestData($data) { - if (is_array($post_data)) { - $post_data = $this->Application->HttpQuery->_transformArrays($post_data); - + if ( is_array($data) ) { $params_str = ''; - foreach ($post_data as $key => $value) { - $params_str .= $key.'='.urlencode($value).'&'; + $data = $this->Application->HttpQuery->_transformArrays($data); + + foreach ($data as $key => $value) { + $params_str .= $key . '=' . urlencode($value) . '&'; } - $post_data = $params_str; + + $data = $params_str; } - $this->postData = $post_data; + $this->requestData = $data; } - function SetHeaders($headers) + /** + * Sets request data for next query and switches request method to POST + * + * @param mixed $data Array or string + * @access public + */ + public function SetPostData($data) { - $this->requestHeaders = array_merge_recursive2($this->requestHeaders, $headers); + $this->requestMethod = self::REQUEST_METHOD_POST; + $this->SetRequestData($data); } - function SetHeader($name, $value) + /** + * Sets request method to be used in next request + * + * @param int $request_method + */ + public function SetRequestMethod($request_method) { - $this->requestHeaders[$name] = $value; + if ($request_method != self::REQUEST_METHOD_GET || $request_method != self::REQUEST_METHOD_POST) { + throw new Exception('Method "' . __METHOD__ . '": Invalid $request_method parameter value'); + + return ; + } + + $this->requestMethod = $request_method; } /** + * Sets headers to be sent along with next query + * + * @param Array $headers + * @access public + */ + public function SetHeaders($headers) + { + $this->requestHeaders = array_merge($this->requestHeaders, $headers); + } + + /** * Returns compiled header to be used by curl * * @return Array + * @access protected */ - function prepareHeaders() + protected function prepareHeaders() { $ret = Array (); + foreach ($this->requestHeaders as $header_name => $header_value) { - $ret[] = $header_name.': '.$header_value; + $ret[] = is_numeric($header_name) ? $header_value : $header_name . ': ' . $header_value; } + return $ret; } - function Send($url, $close_connection = true, $log_status = null) + /** + * Performs CURL request and returns it's result + * + * @param string $url + * @param bool $close_connection + * @return string + * @access public + */ + public function Send($url, $close_connection = true, $log_status = null) { 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) { - safeDefine('DBG_CURL_LOGFILE', '/curl.log'); + kUtil::safeDefine('DBG_CURL_LOGFILE', '/curl.log'); $this->logFilePointer = fopen(FULL_PATH.DBG_CURL_LOGFILE, 'a'); $user_id = $this->Application->RecallVar('user_id'); @@ -256,13 +369,19 @@ * @return mixed * * @see http://www.php.net/manual/ru/function.curl-getinfo.php + * @access public */ - function getInfo($info_type) + public function getInfo($info_type) { return curl_getinfo($this->connectionID, $info_type); } - function Finalize($close_connection = true) + /** + * Finalizes curl request and saves some data from curl before closing connection + * + * @param int $close_connection + */ + public function Finalize($close_connection = true) { $this->lastErrorCode = curl_errno($this->connectionID); $this->lastErrorMsg = curl_error($this->connectionID); @@ -275,7 +394,12 @@ $this->_resetSettings(); } - function CloseConnection() + /** + * Closes connection to server + * + * @access public + */ + public function CloseConnection() { curl_close($this->connectionID); @@ -287,6 +411,6 @@ } // restore debug mode setting - $this->debugMode = constOn('DBG_CURL'); + $this->debugMode = kUtil::constOn('DBG_CURL'); } } \ No newline at end of file