Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Components\Http\Collection;
|
||||
|
||||
use ArrayIterator;
|
||||
use Iterator;
|
||||
use Xentral\Components\Http\File\FileUpload;
|
||||
|
||||
class FilesCollection implements Iterator
|
||||
{
|
||||
/** @var ArrayIterator $iterator */
|
||||
protected $iterator;
|
||||
|
||||
/**
|
||||
* @param array $files
|
||||
*/
|
||||
public function __construct(array $files = [])
|
||||
{
|
||||
$files = $this->loadFilesArray($files);
|
||||
$this->iterator = new ArrayIterator($files);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all file uploads.
|
||||
*
|
||||
* @return FileUpload[]
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
return $this->iterator->getArrayCopy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if there is a file upload entry with this name.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
return $this->iterator->offsetExists($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an file upload entry.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed|null $default
|
||||
*
|
||||
* @return FileUpload|mixed
|
||||
*/
|
||||
public function get($name, $default = null)
|
||||
{
|
||||
return $this->has($name) ? $this->iterator->offsetGet($name) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if all upload files are valid.
|
||||
*
|
||||
* @return bool true=all upload files are valid
|
||||
*/
|
||||
public function allValid()
|
||||
{
|
||||
foreach ($this->all() as $name => $upload) {
|
||||
if (!$upload->isValid()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if at least one file upload has an error.
|
||||
*
|
||||
* @return bool true=there is at least one error
|
||||
*/
|
||||
public function hasErrors()
|
||||
{
|
||||
foreach ($this->all() as $name => $upload) {
|
||||
if ($upload->hasError()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current element
|
||||
*
|
||||
* @return mixed FileUpload Object, null on failure
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->iterator->current();
|
||||
}
|
||||
|
||||
/**
|
||||
* Move forward to next element
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
$this->iterator->next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the key of the current element
|
||||
*
|
||||
* @return mixed on success, or null on failure.
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return $this->iterator->key();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if current position is valid
|
||||
*
|
||||
* @return boolean true on success or false on failure.
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return $this->iterator->valid();
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewind the Iterator to the first element
|
||||
*
|
||||
* @return void Any returned value is ignored.
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
$this->iterator->rewind();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $fileEntries
|
||||
*
|
||||
* @return bool true=is alternate Array
|
||||
*/
|
||||
protected function isAlternateArray($fileEntries)
|
||||
{
|
||||
if (
|
||||
!isset($fileEntries[array_keys($fileEntries)[0]]['tmp_name'])
|
||||
|| is_array($fileEntries[array_keys($fileEntries)[0]]['tmp_name'])
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $fileEntries
|
||||
*
|
||||
* @return FileUpload[] $files
|
||||
*/
|
||||
protected function loadFilesArray($fileEntries)
|
||||
{
|
||||
if (empty($fileEntries)) {
|
||||
return [];
|
||||
}
|
||||
if ($this->isAlternateArray($fileEntries)) {
|
||||
return $this->convertFilesArray($fileEntries);
|
||||
}
|
||||
|
||||
$files = [];
|
||||
foreach ($fileEntries as $name => $upload) {
|
||||
if (empty($upload['tmp_name'])) {
|
||||
continue;
|
||||
}
|
||||
if (!is_array($upload['tmp_name'])) {
|
||||
$files[$name] = FileUpload::fromFilesArray($upload);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively builds the array with FileUpload instances from alternative array format.
|
||||
*
|
||||
* tested up to 3rd level nesting
|
||||
*
|
||||
* @param array $files
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function convertFilesArray(array $files)
|
||||
{
|
||||
$totalUploads = [];
|
||||
foreach ($files as $name => $file) {
|
||||
$keys = array_keys($file);
|
||||
if (count(array_intersect($keys, ['name', 'tmp_name'])) === 2) {
|
||||
$uploads = [];
|
||||
foreach ($file['tmp_name'] as $k => $v) {
|
||||
$upload = [
|
||||
'tmp_name' => array_key_exists('tmp_name', $file) ? $file['tmp_name'][$k] : null,
|
||||
'name' => array_key_exists('name', $file) ? $file['name'][$k] : null,
|
||||
'type' => array_key_exists('type', $file) ? $file['type'][$k] : null,
|
||||
'size' => array_key_exists('size', $file) ? $file['size'][$k] : null,
|
||||
'error' => array_key_exists('error', $file) ? $file['error'][$k] : null,
|
||||
];
|
||||
if (!empty($upload['tmp_name'])) {
|
||||
$uploads[$k] = FileUpload::fromFilesArray($upload);
|
||||
}
|
||||
}
|
||||
$totalUploads[$name] = $uploads;
|
||||
} else {
|
||||
if (!(isset($file['tmp_name']) && $file['tmp_name'] === '')) {
|
||||
$totalUploads[$name] = $this->convertFilesArray($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $totalUploads;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Components\Http\Collection;
|
||||
|
||||
class ParameterCollection extends ReadonlyParameterCollection
|
||||
{
|
||||
/**
|
||||
* Sets a parameter; Existing parameter value will be overwritten
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function set($name, $value)
|
||||
{
|
||||
$this->params[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets multiple parameters; Existing parameters will be overwritten
|
||||
*
|
||||
* @param array $values
|
||||
*/
|
||||
public function add(array $values)
|
||||
{
|
||||
$this->params = array_merge($this->params, $values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a parameter
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
public function remove($name)
|
||||
{
|
||||
unset($this->params[$name]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Components\Http\Collection;
|
||||
|
||||
class ReadonlyParameterCollection
|
||||
{
|
||||
/** @var array $params */
|
||||
protected $params;
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*/
|
||||
public function __construct(array $params = [])
|
||||
{
|
||||
$this->params = [];
|
||||
foreach ($params as $name => $value) {
|
||||
$this->params[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all parameters and values as associative array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if parameter is available
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
return array_key_exists($name, $this->params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parameter value
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed|null $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name, $default = null)
|
||||
{
|
||||
return isset($this->params[$name]) ? $this->params[$name] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parameter value casted to boolean
|
||||
*
|
||||
* @param string $name
|
||||
* @param bool $default
|
||||
*
|
||||
* @return bool Returns true for "1", "true", "on" and "yes"; otherwise false
|
||||
*/
|
||||
public function getBool($name, $default = false)
|
||||
{
|
||||
return filter_var($this->get($name, $default), FILTER_VALIDATE_BOOLEAN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parameter value casted to integer
|
||||
*
|
||||
* @param string $name
|
||||
* @param int $default
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getInt($name, $default = 0)
|
||||
{
|
||||
return (int)$this->get($name, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string with digits only (0-9)
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $default
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDigits($name, $default = '')
|
||||
{
|
||||
return (string)preg_replace('#[^0-9]#', '', $this->get($name, $default));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string with alphabetic characters only (A-Z and a-z)
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $default
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAlpha($name, $default = '')
|
||||
{
|
||||
return (string)preg_replace('#[^A-Za-z]#', '', $this->get($name, $default));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string with alphanumeric characters only (A-Z, a-z and 0-9)
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $default
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAlphaNum($name, $default = '')
|
||||
{
|
||||
return (string)preg_replace('#[^A-Za-z0-9]#', '', $this->get($name, $default));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string with alphanumeric characters and dashes only (A-Z, a-z, 0-9, Minus and Underscore)
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $default
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAlphaNumWithDashes($name, $default = '')
|
||||
{
|
||||
return (string)preg_replace('#[^A-Za-z0-9_-]#', '', $this->get($name, $default));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Components\Http\Collection;
|
||||
|
||||
class ServerParameter extends ReadonlyParameterCollection
|
||||
{
|
||||
/**
|
||||
* Returns all http request headers
|
||||
*
|
||||
* Emulates php's getallheaders() function
|
||||
* getallheaders is not available in all environments
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getHeaders()
|
||||
{
|
||||
$header = [];
|
||||
|
||||
if (isset($this->params['CONTENT_TYPE'])) {
|
||||
$header['Content-Type'] = $this->params['CONTENT_TYPE'];
|
||||
}
|
||||
|
||||
foreach ($this->params as $name => $value) {
|
||||
if (strpos($name, 'HTTP_') === 0) {
|
||||
$header[$this->transformHeaderName($name)] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// Auth-Header ist bereits gesetzt durch $_SERVER[HTTP_AUTHORIZATION]
|
||||
if (!empty($header['Authorization'])) {
|
||||
return $header;
|
||||
}
|
||||
|
||||
// Basic-Auth
|
||||
if (isset($this->params['PHP_AUTH_USER'])) {
|
||||
$authString = base64_encode($this->params['PHP_AUTH_USER'] . ':' . $this->params['PHP_AUTH_PW']);
|
||||
$header['Authorization'] = sprintf('Basic %s', $authString);
|
||||
}
|
||||
|
||||
// Digest-Auth
|
||||
if (isset($this->params['PHP_AUTH_DIGEST'])) {
|
||||
$header['Authorization'] = sprintf('Digest %s', $this->params['PHP_AUTH_DIGEST']);
|
||||
}
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform header names
|
||||
*
|
||||
* Transforms php $_SERVER formattet header names to
|
||||
* Browser style formatted header names.
|
||||
*
|
||||
* @example Transforms "HTTP_USER_AGENT" to "User-Agent"
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function transformHeaderName($name)
|
||||
{
|
||||
$name = substr($name, 5); // HTTP-Prefix entfernen
|
||||
$name = (string)str_replace('_', ' ', $name);
|
||||
$name = strtolower($name);
|
||||
$name = ucwords($name);
|
||||
|
||||
return str_replace(' ', '-', $name);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user