summaryrefslogtreecommitdiff
path: root/it_url.class
diff options
context:
space:
mode:
authorChristian A. Weber2017-03-16 17:39:50 +0100
committerChristian A. Weber2017-03-16 17:39:50 +0100
commit9177e8f225ceb494a77786ec49187afe0ae6102f (patch)
treebc8314e107318a667867fbdbcd19c702a3b6045b /it_url.class
parenta833ef8465c738d117d2f72dab588f04bc0982ce (diff)
downloaditools-9177e8f225ceb494a77786ec49187afe0ae6102f.tar.gz
itools-9177e8f225ceb494a77786ec49187afe0ae6102f.tar.bz2
itools-9177e8f225ceb494a77786ec49187afe0ae6102f.zip
it_url::get() supports http file upload, clarify params documentation
Diffstat (limited to 'it_url.class')
-rw-r--r--it_url.class15
1 files changed, 11 insertions, 4 deletions
diff --git a/it_url.class b/it_url.class
index e9d3c45..7355463 100644
--- a/it_url.class
+++ b/it_url.class
@@ -100,8 +100,9 @@ function is_reachable($timeout = 5)
* @param $p['totaltimeout'] timeout for the whole function call
* @param $p['maxlength'] maximum length of response
* @param $p['filemtime'] Add HTTP header to only fetch when newer than this, otherwise return true instead of data
- * @param $p['data']: POST data array with key-value pairs
- * @param $p['retries']: Number of retries if download fails, default 1
+ * @param $p['data'] POST data array with key-value pairs
+ * @param $p['files'] [fieldname => filename] of files to upload
+ * @param $p['retries'] Number of retries if download fails, default 1
* @param $p['retrysleep'] Number of seconds to wait before retry, fractions ok
* @return contents of resulting page, considering redirects, excluding headers, or false on error
*/
@@ -123,7 +124,7 @@ function get($p=null, $timeout=5)
else # called statically
$url = new it_url($p['url']);
- if ($url->protocol == 'http' && !$p['curl'])
+ if ($url->protocol == 'http' && !$p['curl'] && !$p['files']) # only curl can do file upload or non-http protocols
$result = $url->request($p);
else
$result = $url->request_curl($p);
@@ -289,6 +290,10 @@ static function curl_opts($p=array())
);
}
+ # file upload
+ foreach ((array)$p['files'] as $field => $filename)
+ $p['data'][$field] = new CURLFile($filename, mime_content_type($filename));
+
if ($p['data'])
$add[CURLOPT_POSTFIELDS] = $p['data'];
@@ -301,6 +306,7 @@ static function curl_opts($p=array())
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_CUSTOMREQUEST => $p['method'] ?: null,
+ CURLOPT_SAFE_UPLOAD => true, # disable special meaning of @value in POST forms (security)
CURLOPT_CAPATH => '/etc/ssl/certs/',
CURLOPT_SSL_VERIFYPEER => !$p['allow_insecure_ssl'],
@@ -316,6 +322,7 @@ static function curl_opts($p=array())
* todo:
* @param $p['filemtime'] Add HTTP header to only fetch when newer than this, otherwise return true instead of data
* @param $p['data'] POST data array with key-value pairs
+ * @param $p['files'] [fieldname => filename] of files to upload
* @param $p['method'] different HTTP method
*/
@@ -769,7 +776,7 @@ static function encode($str)
/**
* Create GET request from params, optionally only using given fields
- * @param $params Array to take values from, usually $_GET
+ * @param $params Array to take values from, usually $_GET. Values of zero length are ignored.
* @param $keys Keys to use; default: all
*/
static function params($params, $keys = null)