Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2012-09-12 Alexey S. Denisov

* main/Flow/HttpRequest.class.php
main/Net/Http/CurlHttpClient.class.php
main/Utils/UrlParamsUtils.class.php:
allowing curlHttpClient upload files and send post/get arrays with any deep lvl

2012-09-06 Igor V. Gulyaev

* main/Base/Hstore.class.php
Expand Down
1 change: 1 addition & 0 deletions doc/Migration1.0-1.1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/onPHP/onphp-framework/wiki/Ru%3A1.0to1.1
6 changes: 6 additions & 0 deletions global.inc.php.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
ONPHP_ROOT_PATH.'meta'.DIRECTORY_SEPARATOR
);

/**
* @deprecated
*/
if (!defined('ONPHP_CURL_CLIENT_OLD_TO_STRING'))
define('ONPHP_CURL_CLIENT_OLD_TO_STRING', false);

define('ONPHP_META_CLASSES', ONPHP_META_PATH.'classes'.DIRECTORY_SEPARATOR);

define(
Expand Down
31 changes: 30 additions & 1 deletion main/Flow/HttpRequest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,27 @@ final class HttpRequest
// reference, not copy
private $session = array();

// uploads
// uploads and downloads (CurlHttpClient)
private $files = array();

// all other sh1t
private $attached = array();

private $headers = array();

/**
* @var HttpMethod
*/
private $method = null;

/**
* @var HttpUrl
*/
private $url = null;

//for CurlHttpClient if you need to send raw CURLOPT_POSTFIELDS
private $body = null;

/**
* @return HttpRequest
**/
Expand Down Expand Up @@ -341,5 +350,25 @@ public function getUrl()
{
return $this->url;
}

public function hasBody()
{
return $this->body !== null;
}

public function getBody()
{
return $this->body;
}

/**
* @param string $body
* @return HttpRequest
*/
public function setBody($body)
{
$this->body = $body;
return $this;
}
}
?>
105 changes: 89 additions & 16 deletions main/Net/Http/CurlHttpClient.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* License, or (at your option) any later version. *
* *
***************************************************************************/
/* $Id: CurlHttpClient.class.php 45 2009-05-08 07:41:33Z lom $ */

/**
* @ingroup Http
Expand All @@ -23,6 +22,10 @@ final class CurlHttpClient implements HttpClient
private $multiRequests = array();
private $multiResponses = array();
private $multiThreadOptions = array();
/**
* @deprecated in the furure will work like this value is false;
*/
private $oldUrlConstructor = ONPHP_CURL_CLIENT_OLD_TO_STRING;

/**
* @return CurlHttpClient
Expand Down Expand Up @@ -148,6 +151,26 @@ public function getMaxFileSize()
return $this->maxFileSize;
}

/**
* @deprecated in the future value always false and method will be removed
* @param bool $oldUrlConstructor
* @return CurlHttpClient
*/
public function setOldUrlConstructor($oldUrlConstructor = false)
{
$this->oldUrlConstructor = ($oldUrlConstructor == true);
return $this;
}

/**
* @deprecated in the future value always false and method will be removed
* @return bool
*/
public function isOldUrlConstructor()
{
return $this->oldUrlConstructor;
}

/**
* @return CurlHttpClient
**/
Expand Down Expand Up @@ -280,9 +303,14 @@ protected function makeHandle(HttpRequest $request, CurlHttpResponse $response)
break;

case HttpMethod::POST:
if ($request->getGet())
$options[CURLOPT_URL] .=
($request->getUrl()->getQuery() ? '&' : '?')
.$this->argumentsToString($request->getGet());

$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] =
$this->argumentsToString($request->getPost());
$options[CURLOPT_POSTFIELDS] = $this->getPostFields($request);

break;

default:
Expand Down Expand Up @@ -337,23 +365,68 @@ protected function makeResponse($handle, CurlHttpResponse $response)
return $this;
}

private function argumentsToString($array)
private function argumentsToString($array, $isFile = false)
{
Assert::isArray($array);
$result = array();

foreach ($array as $key => $value) {
if (is_array($value)) {
foreach ($value as $valueKey => $simpleValue) {
$result[] =
$key.'['.$valueKey.']='.urlencode($simpleValue);
}
if ($this->oldUrlConstructor)
return UrlParamsUtils::toStringOneDeepLvl($array);
else
return UrlParamsUtils::toString($array);
}

private function getPostFields(HttpRequest $request)
{
if ($request->hasBody()) {
return $request->getBody();
} else {
if ($this->oldUrlConstructor) {
return UrlParamsUtils::toStringOneDeepLvl($request->getPost());
} else {
$result[] = $key.'='.urlencode($value);
$fileList = array_map(
array($this, 'fileFilter'),
UrlParamsUtils::toParamsList($request->getFiles())
);
if (empty($fileList)) {
return UrlParamsUtils::toString($request->getPost());
} else {
$postList = UrlParamsUtils::toParamsList($request->getPost());
if (!is_null($atParam = $this->findAtParamInPost($postList)))
throw new NetworkException(
'Security excepion: not allowed send post param '.$atParam
. ' which begins from @ in request which contains files'
);

return array_merge($postList, $fileList);
}
}
}

return implode('&', $result);
}

/**
* Return param name which start with symbol @ or null
* @param array $postList
* @return string|null
*/
private function findAtParamInPost($postList)
{
foreach ($postList as $param)
if (mb_stripos($param, '@') === 0)
return $param;

return null;
}

/**
* using in getPostFields - array_map func
* @param string $value
* @return string
*/
private function fileFilter($value)
{
Assert::isTrue(
is_readable($value) && is_file($value),
'couldn\'t access to file with path: '.$value
);
return '@'.$value;
}
}
?>
79 changes: 79 additions & 0 deletions main/Utils/UrlParamsUtils.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/***************************************************************************
* Copyright (C) 2012 by Alexey S. Denisov *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/

/**
* @ingroup Utils
**/
final class UrlParamsUtils extends StaticFactory
{
/**
* @deprecated to support old convert method in CurlHttpClient
* @param array $array
* @return string
*/
public static function toStringOneDeepLvl($array)
{
Assert::isArray($array);
$result = array();

foreach ($array as $key => $value) {
if (is_array($value)) {
foreach ($value as $valueKey => $simpleValue) {
$result[] =
$key.'['.$valueKey.']='.urlencode($simpleValue);
}
} else {
$result[] = $key.'='.urlencode($value);
}
}

return implode('&', $result);
}

public static function toString($array)
{
$sum = function ($left, $right) {return $left.'='.urlencode($right);};
$params = self::toParamsList($array, true);
return implode('&',
array_map($sum, array_keys($params), $params)
);
}

public static function toParamsList($array, $encodeKey = false)
{
$result = array();

self::argumentsToParams($array, $result, '', $encodeKey);

return $result;
}

private static function argumentsToParams(
$array,
&$result,
$keyPrefix,
$encodeKey = false
) {
foreach ($array as $key => $value) {
$filteredKey = $encodeKey ? urlencode($key) : $key;
$fullKey = $keyPrefix
? ($keyPrefix.'['.$filteredKey.']')
: $filteredKey;

if (is_array($value)) {
self::argumentsToParams($value, $result, $fullKey, $encodeKey);
} else {
$result[$fullKey] = $value;
}
}
}
}
?>
1 change: 1 addition & 0 deletions test/config.inc.php.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@
VoodooDaoWorker::setDefaultHandler('CacheSegmentHandler');

define('__LOCAL_DEBUG__', true);
define('ONPHP_CURL_TEST_URL', 'http://localhost/curlTest.php'); //set here url to test script test/main/data/curlTest/curlTest.php
?>
Loading