Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Sun, Jan 5, 6:50 PM

in-portal

Index: branches/RC/core/units/general/helpers/curl_helper.php
===================================================================
--- branches/RC/core/units/general/helpers/curl_helper.php (revision 10298)
+++ branches/RC/core/units/general/helpers/curl_helper.php (revision 10299)
@@ -1,220 +1,238 @@
<?php
class kCurlHelper extends kHelper {
/**
* Connection to host
*
* @var resource
*/
var $connectionID = null;
/**
* Pointer to opened log file
*
* @var resource
*/
var $logFilePointer = null;
/**
* Responce waiting timeout
*
* @var int
*/
var $timeout = 90;
/**
* Follow to url, if redirect received insted of document (only works when open_basedir and safe mode is off)
*
* @var bool
*/
var $followLocation = false;
/**
* Last responce received by Curl
*
* @var string
*/
var $lastRespoce = '';
/**
* Last error code
*
* @var int
*/
var $lastErrorCode = 0;
/**
* Last error message
*
* @var string
*/
var $lastErrorMsg = '';
/**
* Most recent HTTP responce code received
*
* @var int
*/
var $lastHTTPCode = 0;
/**
* POST data to be sent using curl
*
* @var string
*/
var $postData = '';
var $requestHeaders = Array ();
var $responceHeaders = Array ();
+ var $options = Array ();
+
/**
* Indicates debug mode status
*
* @var bool
*/
var $debugMode = false;
function kCurlHelper()
{
parent::kHelper();
$this->debugMode = $this->Application->isDebugMode(false) && constOn('DBG_CURL');
}
- function SetOptions()
+ function setOptions($options_hash)
{
- // customizable options
- curl_setopt($this->connectionID, CURLOPT_FOLLOWLOCATION, $this->followLocation ? 1 : 0);
- curl_setopt($this->connectionID, CURLOPT_TIMEOUT, $this->timeout);
-
- // hardcoded options
- curl_setopt($this->connectionID, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($this->connectionID, CURLOPT_REFERER, PROTOCOL.SERVER_NAME);
- curl_setopt($this->connectionID, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
+ $this->options = array_merge_recursive2($this->options, $options_hash);
+ }
+
+ function prepareOptions()
+ {
+ $default_options = Array (
+ // customizable options
+ CURLOPT_FOLLOWLOCATION => $this->followLocation ? 1 : 0,
+ CURLOPT_TIMEOUT => $this->timeout,
+
+ // hardcoded options
+ CURLOPT_RETURNTRANSFER => 1,
+ CURLOPT_REFERER => PROTOCOL.SERVER_NAME,
+ CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'],
+ );
if ($this->requestHeaders) {
- curl_setopt($this->connectionID, CURLOPT_HTTPHEADER, $this->prepareHeaders());
+ $default_options[CURLOPT_HTTPHEADER] = $this->prepareHeaders();
}
// if we have post data, then POST else use GET method instead
if ($this->postData) {
- curl_setopt($this->connectionID, CURLOPT_POST, 1);
- curl_setopt($this->connectionID, CURLOPT_POSTFIELDS, $this->postData);
+ $default_options[CURLOPT_POST] = 1;
+ $default_options[CURLOPT_POSTFIELDS] = $this->postData;
}
-// curl_setopt($this->connectionID, CURLOPT_HEADERFUNCTION, Array(&$this, 'ParseHeader'));
+// $default_options[CURLOPT_HEADERFUNCTION] = Array(&$this, 'ParseHeader');
+ $this->setOptions($default_options);
+ $this->applyOptions();
+ }
+
+ function applyOptions()
+ {
+ foreach ($this->options as $option_name => $option_value) {
+ curl_setopt($this->connectionID, $option_name, $option_value);
+ }
}
function ParseHeader(&$ch, $header)
{
$this->responceHeaders[] = $header;
return strlen($header);
}
/**
* Sets POST data for next query
*
* @param mixed $post_data Array or string
*/
function SetPostData($post_data)
{
if (is_array($post_data)) {
$params_str = '';
foreach ($post_data as $key => $value) {
$params_str .= $key.'='.urlencode($value).'&';
}
$post_data = $params_str;
}
$this->postData = $post_data;
}
function SetHeaders($headers)
{
$this->requestHeaders = array_merge_recursive2($this->requestHeaders, $headers);
}
function SetHeader($name, $value)
{
$this->requestHeaders[$name] = $value;
}
/**
* Returns compiled header to be used by curl
*
* @return Array
*/
function prepareHeaders()
{
$ret = Array ();
foreach ($this->requestHeaders as $header_name => $header_value) {
$ret[] = $header_name.': '.$header_value;
}
return $ret;
}
function Send($url, $close_connection = true)
{
$this->connectionID = curl_init($url);
if ($this->debugMode) {
safeDefine('DBG_CURL_LOGFILE', '/curl.log');
$this->logFilePointer = fopen(FULL_PATH.DBG_CURL_LOGFILE, 'a');
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);
}
$this->responceHeaders = Array ();
- $this->SetOptions();
+ $this->prepareOptions();
$this->lastRespoce = curl_exec($this->connectionID);
$this->Finalize($close_connection);
return $this->lastRespoce;
}
/**
* Returns various info about request made
*
* @param int $info_type
* @return mixed
*
* @see http://www.php.net/manual/ru/function.curl-getinfo.php
*/
function getInfo($info_type)
{
return curl_getinfo($this->connectionID, $info_type);
}
function Finalize($close_connection = true)
{
$this->lastErrorCode = curl_errno($this->connectionID);
$this->lastErrorMsg = curl_error($this->connectionID);
$this->lastHTTPCode = curl_getinfo($this->connectionID, CURLINFO_HTTP_CODE);
if ($this->debugMode) {
fwrite($this->logFilePointer, "\n".$this->lastRespoce);
fclose($this->logFilePointer);
}
$this->postData = '';
$this->requestHeaders = Array ();
if ($close_connection) {
$this->CloseConnection();
}
}
function CloseConnection()
{
curl_close($this->connectionID);
}
}
?>
\ No newline at end of file
Property changes on: branches/RC/core/units/general/helpers/curl_helper.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.1.4.1
\ No newline at end of property
+1.1.4.2
\ No newline at end of property

Event Timeline