From b27866b3426ba9e8912406619aedf75cc789e0c5 Mon Sep 17 00:00:00 2001 From: Alex Furnica Date: Mon, 2 Apr 2018 12:48:27 +0300 Subject: [PATCH] upgraded core and CertificateAuthority, added BitTorrent --- Extensions/BitTorrent/BitTorrentEncode.php | 90 +++++ .../CertificateAuthority.php} | 14 +- .../CertificateAuthorityConfig.php} | 16 +- .../IQuarkCertificateAuthorityProvider.php} | 8 +- .../Providers/LetsEncrypt.php | 8 +- .../Providers/SelfSignedAuthority.php | 8 +- Quark.php | 376 +++++++++++++----- Scenarios/Generate/Certificate.php | 12 +- 8 files changed, 398 insertions(+), 134 deletions(-) create mode 100644 Extensions/BitTorrent/BitTorrentEncode.php rename Extensions/{SSLAuthority/SSLAuthority.php => CertificateAuthority/CertificateAuthority.php} (85%) rename Extensions/{SSLAuthority/SSLAuthorityConfig.php => CertificateAuthority/CertificateAuthorityConfig.php} (61%) rename Extensions/{SSLAuthority/IQuarkSSLAuthorityProvider.php => CertificateAuthority/IQuarkCertificateAuthorityProvider.php} (78%) rename Extensions/{SSLAuthority => CertificateAuthority}/Providers/LetsEncrypt.php (98%) rename Extensions/{SSLAuthority => CertificateAuthority}/Providers/SelfSignedAuthority.php (92%) diff --git a/Extensions/BitTorrent/BitTorrentEncode.php b/Extensions/BitTorrent/BitTorrentEncode.php new file mode 100644 index 00000000..80294482 --- /dev/null +++ b/Extensions/BitTorrent/BitTorrentEncode.php @@ -0,0 +1,90 @@ +_plain ? QuarkPlainIOProcessor::MIME : self::MIME; + } + + /** + * @param $data + * + * @return string + */ + public function Encode ($data) { + if (is_bool($data)) return 'd' . ($data ? 1 : 0) . 'e'; + if (is_int($data)) return 'd' . $data . 'e'; + if (is_float($data)) return 'd' . ((int)$data) . 'e'; + if (is_string($data)) return strlen($data) . ':' . $data; + + if (QuarkObject::isIterative($data)) { + $out = 'l'; + + foreach ($data as $i => &$item) + $out .= $this->Encode($item); + + unset($i , $item); + + return $out . 'e'; + } + + if (QuarkObject::isAssociative($data)) { + $out = 'd'; + + foreach ($data as $key => &$value) + $out .= $this->Encode($key) . $this->Encode($value); + + unset($key , $value); + + return $out . 'e'; + } + + return null; + } + + /** + * @param $raw + * + * @return mixed + */ + public function Decode ($raw) { + // TODO: Implement Decode() method. + } + + /** + * @param string $raw + * @param bool $fallback + * + * @return mixed + */ + public function Batch ($raw, $fallback) { + return array($raw); + } + + /** + * @return bool + */ + public function ForceInput () { + return false; + } +} \ No newline at end of file diff --git a/Extensions/SSLAuthority/SSLAuthority.php b/Extensions/CertificateAuthority/CertificateAuthority.php similarity index 85% rename from Extensions/SSLAuthority/SSLAuthority.php rename to Extensions/CertificateAuthority/CertificateAuthority.php index 540e82ec..5d93455a 100644 --- a/Extensions/SSLAuthority/SSLAuthority.php +++ b/Extensions/CertificateAuthority/CertificateAuthority.php @@ -1,5 +1,5 @@ &$domain) $certificate->AltName(new QuarkCertificateSAN($domain)); + unset($i, $domain); + return $this->CertificateRequest($certificate); } } \ No newline at end of file diff --git a/Extensions/SSLAuthority/SSLAuthorityConfig.php b/Extensions/CertificateAuthority/CertificateAuthorityConfig.php similarity index 61% rename from Extensions/SSLAuthority/SSLAuthorityConfig.php rename to Extensions/CertificateAuthority/CertificateAuthorityConfig.php index 4c42fc18..ab9560fd 100644 --- a/Extensions/SSLAuthority/SSLAuthorityConfig.php +++ b/Extensions/CertificateAuthority/CertificateAuthorityConfig.php @@ -1,5 +1,5 @@ _provider = $provider; } /** - * @return IQuarkSSLAuthorityProvider + * @return IQuarkCertificateAuthorityProvider */ public function &Provider () { return $this->_provider; @@ -61,6 +61,6 @@ public function ExtensionOptions ($ini) { * @return IQuarkExtension */ public function ExtensionInstance () { - return new SSLAuthority($this->_name); + return new CertificateAuthority($this->_name); } } \ No newline at end of file diff --git a/Extensions/SSLAuthority/IQuarkSSLAuthorityProvider.php b/Extensions/CertificateAuthority/IQuarkCertificateAuthorityProvider.php similarity index 78% rename from Extensions/SSLAuthority/IQuarkSSLAuthorityProvider.php rename to Extensions/CertificateAuthority/IQuarkCertificateAuthorityProvider.php index 6c6f12b4..dc20ef19 100644 --- a/Extensions/SSLAuthority/IQuarkSSLAuthorityProvider.php +++ b/Extensions/CertificateAuthority/IQuarkCertificateAuthorityProvider.php @@ -1,14 +1,14 @@ _localizationDictionary == null) $this->_localizationDictionary = new \stdClass(); - if (QuarkObject::isTraversable($details)) - foreach ($details as $key => $block) { + if (QuarkObject::isTraversable($details)) { + foreach ($details as $key => &$block) { $outKey = $domain . $this->_localizationDetailsDelimiter . $key; $this->_localizationDictionary->$outKey = $block; } + + unset($key, $block); + } } } @@ -2338,7 +2341,7 @@ public function Thread () { $authBasic = 1; } - foreach ($_SERVER as $name => $value) { + foreach ($_SERVER as $name => &$value) { $name = str_replace('CONTENT_', 'HTTP_CONTENT_', $name); $name = str_replace('PHP_AUTH_DIGEST', 'HTTP_AUTHORIZATION', $name, $authDigest); @@ -2352,6 +2355,8 @@ public function Thread () { $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = ($name == 'HTTP_AUTHORIZATION' ? $authType : '') . $value; } + unset($name, $value); + $service->Input()->Method(ucfirst(strtolower($_SERVER['REQUEST_METHOD']))); $service->Input()->Headers($headers); @@ -2381,8 +2386,10 @@ public function Thread () { $service->On(QuarkService::EVENT_OUTPUT_HEADERS, function () use (&$service) { $headers = $service->Output()->SerializeResponseHeadersToArray(); - foreach ($headers as $header) + foreach ($headers as $i => &$header) header($header); + + unset($i, $header, $headers); }); echo $service->Pipeline(); @@ -4077,11 +4084,14 @@ public function ServiceArgs ($flags = false) { $args = array_slice($args, 1); - if (!$flags) + if (!$flags) { foreach ($args as $i => &$arg) if (strlen($arg) != 0 && $arg[0] == '-') unset($args[$i]); + unset($i, $arg); + } + return array_values($args); } @@ -4529,18 +4539,22 @@ public static function Resolve ($uri = '', $base = '', $namespace = '', $postfix $buffer = array(); - foreach ($path as $item) { + foreach ($path as $i => &$item) { if (strlen(trim($item)) == 0) continue; $section = explode('-', trim($item)); $out = array(); - foreach ($section as $elem) + foreach ($section as $j => &$elem) $out[] = ucfirst($elem); + unset($j, $elem); + $buffer[] = implode('', $out); } + unset($i, $item); + $route = $buffer; unset($buffer); $length = sizeof($route); @@ -4908,12 +4922,15 @@ public function Invoke ($method, $args = [], $session = false) { if ($morph) { $processors = $this->_service->Processors($this->_input); - if (is_array($processors)) - foreach ($processors as $processor) { + if (is_array($processors)) { + foreach ($processors as $i => &$processor) { if (!($processor instanceof IQuarkIOProcessor)) continue; if ($processor->MimeType() == $this->_input->ExpectedType()) $selected = $processor; } + + unset($i, $processor, $processors); + } } $this->_filterInput(); @@ -5266,9 +5283,11 @@ public function TemplatedLocalizationDictionaryOf ($key = '', $data = []) { $out = new \stdClass(); $locales = $this->LocalizationDictionaryOf($key); - foreach ($locales as $key => $locale) + foreach ($locales as $i => &$locale) $out->$key = $this->Template($locale, $data); + unset($i, $locale); + return $out; } @@ -5340,9 +5359,10 @@ public static function Walk (&$source, callable $iterator, $key = '', &$parent = $iterator($key, $model, $parent); } else { - foreach ($source as $k => $v) { + foreach ($source as $k => $v) self::Walk($v, $iterator, $key . ($key == '' ? $k : '[' . $k . ']'), $source); - } + + unset($k, $v); } unset($k, $v); @@ -5360,7 +5380,7 @@ public static function TreeBuilder ($paths = [], $prefix = '') { $dirs = array(); $files = array(); - foreach ($paths as $link) { + foreach ($paths as $i => &$link) { $path = explode('/', $link); if (sizeof($path) == 1) $files[] = new QuarkKeyValuePair($prefix . '/' . $link, $link); @@ -5372,12 +5392,18 @@ public static function TreeBuilder ($paths = [], $prefix = '') { } } - foreach ($dirs as $key => $link) + unset($i, $link); + + foreach ($dirs as $key => &$link) $out[$key] = self::TreeBuilder($link, $prefix . '/' . $key); - foreach ($files as $file) + unset($key, $link); + + foreach ($files as $i => &$file) $out[] = $file; + unset($i, $file); + return $out; } @@ -5523,7 +5549,7 @@ public static function is ($class, $interface = '', $silent = false) { $faces = class_implements($class); - foreach ($interface as $face) + foreach ($interface as &$face) if (in_array($face, $faces, true)) return true; return false; @@ -5539,9 +5565,11 @@ public static function Implementations ($interface, callable $filter = null) { $output = array(); $classes = get_declared_classes(); - foreach ($classes as $class) + foreach ($classes as $i => &$class) if (self::is($class, $interface) && ($filter != null ? $filter($class) : true)) $output[] = $class; + unset($i, $class); + return $output; } @@ -5560,13 +5588,17 @@ public static function Uses ($class = '', $trait = '', $parents = true) { $tree = $parents ? class_parents($class) : array(); $tree[] = $class; - foreach ($tree as $node) { + foreach ($tree as $i => &$node) { $uses = class_uses($node); - foreach ($uses as $use) + foreach ($uses as $j => &$use) if ($use == $trait) return true; + + unset($j, $use); } + unset($i, $node); + return false; } @@ -5654,7 +5686,7 @@ public static function ConstByValue ($value) { if (!isset($defined['user']) || !is_array($defined['user'])) return null; - foreach ($defined['user'] as $key => $val) + foreach ($defined['user'] as $key => &$val) if ($val === $value) return $key; return null; @@ -5678,7 +5710,7 @@ public static function ConstValue ($const) { public static function ClassConstByValue ($class, $value) { $defined = self::ClassConstants($class); - foreach ($defined as $key => $const) + foreach ($defined as $key => &$const) if ($const == $value) return $key; return null; @@ -6337,10 +6369,13 @@ public function ResourceList () { * @return QuarkView */ private function _resources ($resources = []) { - if (is_array($resources)) - foreach ($resources as $resource) + if (is_array($resources)) { + foreach ($resources as $i => &$resource) $this->_resource($resource); + unset($i, $resource, $resources); + } + return $this; } @@ -6843,13 +6878,15 @@ public static function TreeMenu ($menu = [], callable $button = null, callable $ if ($node == null) $node = function ($text) { return '
' . $text . '
'; }; - foreach ($menu as $key => $element) { + foreach ($menu as $key => &$element) { if (!is_array($element)) $out .= $button($element->Key(), $element->Value()); else { $out .= '
' . $node($key) . self::TreeMenu($element, $button, $node) . '
'; } + + unset($key, $element); } return $out; @@ -6868,10 +6905,12 @@ public function FieldError (QuarkModel $model = null, $field = '', $template = s $errors = $model->RawValidationErrors(); $out = ''; - foreach ($errors as $error) + foreach ($errors as $i => &$error) if ($error->Key() == $field) $out .= str_replace('{error}', $error->Value()->Of(Quark::CurrentLanguage()), $template); + unset($i, $error); + return $out; } @@ -7917,12 +7956,14 @@ private function _matchOut (&$out, &$outChanged, $state) { private function _matchDocument ($role, $document, $query, $append = null) { $out = true; - foreach ($query as $state => $expected) { + foreach ($query as $state => &$expected) { $hook = str_replace('$', $role, $state); $out &= method_exists($this, $hook) ? $this->$hook($document, $expected, $append) : false; - }; + } + + unset($state, $expected); return $out; } @@ -7959,8 +8000,10 @@ private function _matchTarget ($target, $rule) { private function _aggregate_and ($document, $rule) { $state = true; - foreach ($rule as $item) + foreach ($rule as $i => &$item) $state &= $this->Match($document, $item); + + unset($i, $item); return $state; } @@ -7975,8 +8018,10 @@ private function _aggregate_and ($document, $rule) { private function _aggregate_nand ($document, $rule) { $state = true; - foreach ($rule as $item) + foreach ($rule as $i => &$item) $state &= $this->Match($document, $item); + + unset($i, $item); return !$state; } @@ -7991,8 +8036,10 @@ private function _aggregate_nand ($document, $rule) { private function _aggregate_or ($document, $rule) { $state = false; - foreach ($rule as $item) + foreach ($rule as $i => &$item) $state |= $this->Match($document, $item); + + unset($i, $item); return $state; } @@ -8007,8 +8054,10 @@ private function _aggregate_or ($document, $rule) { private function _aggregate_nor ($document, $rule) { $state = false; - foreach ($rule as $item) + foreach ($rule as $i => &$item) $state |= $this->Match($document, $item); + + unset($i, $item); return !$state; } @@ -8023,8 +8072,10 @@ private function _aggregate_nor ($document, $rule) { private function _aggregate_not ($document, $rule) { $state = true; - foreach ($rule as $item) + foreach ($rule as $i => &$item) $state &= !$this->Match($document, $item); + + unset($i, $item); return $state; } @@ -8061,8 +8112,10 @@ private function _array_nin ($property, $expected) { private function _array_elemMatch ($property, $expected) { $out = false; - foreach ($property as $item) + foreach ($property as $i => &$item) $out |= $this->Match($item, $expected); + + unset($i, $item); return $out; } @@ -8079,7 +8132,7 @@ private function _array_elemMatch ($property, $expected) { private function _array_all ($property, $expected) { $out = true; - foreach ($expected as $item) { + foreach ($expected as $i => &$item) { if (is_scalar($item) || is_null($item)) { $out &= in_array($item, $property); continue; @@ -8090,9 +8143,13 @@ private function _array_all ($property, $expected) { if (isset($item['$elemMatch'])) $query = $item['$elemMatch']; - foreach ($property as $entry) + foreach ($property as $j => &$entry) $out &= $this->Match($entry, $query); + + unset($j, $entry); } + + unset($i, $item); return $out; } @@ -8341,7 +8398,7 @@ private function _slice ($list = [], $options = [], $preserveKeys = null) { /** @noinspection PhpUnusedLocalVariableInspection */ $b = (object)$b; - foreach ($sort as $key => $mode) { + foreach ($sort as $key => &$mode) { $accessor = str_replace('.', '->', str_replace('->', '', $key)); $elem_a = eval('return isset($a->' . $accessor . ') ? $a->' . $accessor . ' : null;'); @@ -8375,6 +8432,8 @@ private function _slice ($list = [], $options = [], $preserveKeys = null) { if ($dir == QuarkModel::SORT_DESC) $res = -$res; break; } + + unset($key, $mode); return $res; }); @@ -10398,15 +10457,20 @@ public function FieldKeys ($runtime = true) { $out = array(); - foreach ($fields as $key => $value) + foreach ($fields as $key => &$value) $out[] = $key; + + unset($key, $value); if ($runtime && $this->_model instanceof IQuarkStrongModelWithRuntimeFields) { $fields = $this->_model->RuntimeFields(); - if (QuarkObject::isAssociative($fields)) - foreach ($fields as $key => $value) + if (QuarkObject::isAssociative($fields)) { + foreach ($fields as $key => &$value) $out[] = $key; + + unset($key, $value); + } } return $out; @@ -10424,16 +10488,21 @@ public function FieldValues ($exclude = [], $runtime = true) { $out = array(); - foreach ($fields as $key => $value) + foreach ($fields as $key => &$value) if (!in_array($key, $exclude)) $out[] = $value; + + unset($key, $value); if ($runtime && $this->_model instanceof IQuarkStrongModelWithRuntimeFields) { $fields = $this->_model->RuntimeFields(); - if (QuarkObject::isAssociative($fields)) - foreach ($fields as $key => $value) + if (QuarkObject::isAssociative($fields)) { + foreach ($fields as $key => &$value) $out[] = $value; + + unset($key, $value); + } } return $out; @@ -10448,9 +10517,11 @@ public function PropertyKeys ($runtime = true) { $out = array(); $fields = $this->FieldKeys($runtime); - foreach ($this->_model as $key => $value) + foreach ($this->_model as $key => &$value) if (!($this->_model instanceof IQuarkStrongModel) || in_array($key, $fields)) $out[] = $key; + + unset($key, $value); return $out; } @@ -10465,9 +10536,11 @@ public function PropertyValues ($exclude = [], $runtime = true) { $out = array(); $fields = $this->FieldKeys($runtime); - foreach ($this->_model as $key => $value) + foreach ($this->_model as $key => &$value) if (!in_array($key, $exclude) && (!($this->_model instanceof IQuarkStrongModel) || in_array($key, $fields))) $out[] = $value; + + unset($key, $value); return $out; } @@ -10576,10 +10649,13 @@ public static function Find (IQuarkModel $model, $criteria = [], $options = [], $raw = self::_provider($model)->Find($model, $criteria, $options); - if ($raw != null) - foreach ($raw as $item) + if ($raw != null) { + foreach ($raw as $i => &$item) $records[] = self::_record($model, $item, $options, $after); + unset($i, $item); + } + if (isset($options[self::OPTION_REVERSE])) $records = array_reverse($records); @@ -11717,7 +11793,7 @@ public static function CollectionOf ($key, $model, $nullable = false) { if (!is_array($key)) return false; - foreach ($key as $item) + foreach ($key as $i => &$item) if (!($item instanceof $model)) return false; return true; @@ -11734,9 +11810,11 @@ public static function Rules ($rules) { $ok = true; - foreach ($rules as $rule) + foreach ($rules as $i => &$rule) $ok = $ok && $rule; + unset($i, $rule); + return $ok; } @@ -11779,9 +11857,11 @@ public static function LocalizedAssert ($rule, $message = '', $field = '') { public static function ValidationErrors ($language = QuarkLanguage::ANY) { $out = array(); - foreach (self:: $_errors as $error) + foreach (self:: $_errors as $i => &$error) $out[] = $error->Value()->Of($language); + unset($i, $error); + return $out; } @@ -12041,12 +12121,14 @@ public function Assert (callable $assert = null, callable $onEmpty = null) { $empty = true; $_empty = null; - foreach ($this->values as $language => $value) { + foreach ($this->values as $language => &$value) { $ok = $assert($value, $language); $out &= $ok === null ? true : $ok; $empty = false; } + unset($language, $value); + if ($empty && $onEmpty != null) { $_empty = $onEmpty(); $_empty = $_empty === null ? true : $_empty; @@ -12531,6 +12613,8 @@ public function Format ($format = '') { $out |= $i == 5 ? $sum >> $offsets[$i] : $sum << $offsets[$i]; } + unset($i, $component, $date); + return $out; } @@ -12752,6 +12836,8 @@ public static function TimezoneList () { foreach ($zones as $i => &$zone) $out[$zone] = QuarkDateInterval::FromSeconds(self::TimezoneOffset($zone)); + unset($i, $zone, $zones); + return $out; } @@ -13121,9 +13207,11 @@ public function Format ($format = '', $sign = false) { public function Modifier () { $out = ''; - foreach (self::$_units as $unit) + foreach (self::$_units as $i => &$unit) $out .= $this->{$unit . 's'} . ' ' . $unit . 's '; + unset($i, $unit); + return $out; } @@ -13156,6 +13244,8 @@ public function Offset ($offset = '') { $seconds += ($modifier[1] != '-' ? 1 : -1) * $interval->Seconds(); } + unset($i, $modifier, $modifiers); + if ($seconds == 0) return $this; $out = self::FromSeconds($this->Seconds() + $seconds); @@ -15189,6 +15279,8 @@ public function Unsubscribe ($channel = '') { foreach ($this->_channels as $i => &$c) if ($channel == $c) unset($this->_channels[$i]); + + unset($i, $c); return $this; } @@ -15458,7 +15550,7 @@ public function &Clients () { * @return bool */ public function Has (QuarkClient $client) { - foreach ($this->_clients as $item) + foreach ($this->_clients as $i => &$item) if ($item->ConnectionURI()->URI() == $client->ConnectionURI()->URI()) return true; return false; @@ -15479,6 +15571,8 @@ public function Broadcast ($data, callable $filter = null) { $ok &= $client->Send($data); } + unset($i, $client); + return $ok; } @@ -15663,10 +15757,13 @@ public function Peer ($uri = null, $unique = true, $loopBack = false) { * @return QuarkClient[]|bool */ public function &Peers ($peers = [], $unique = true, $loopBack = false) { - if (func_num_args() != 0 && is_array($peers)) - foreach ($peers as $peer) + if (func_num_args() != 0 && is_array($peers)) { + foreach ($peers as $i => &$peer) $this->Peer($peer, $unique, $loopBack); + unset($i, $peer); + } + return $this->_peers; } @@ -16284,9 +16381,11 @@ private function __construct () { private function _log ($action = '', $message = '', $args = []) { $append = ''; - foreach ($args as $arg) + foreach ($args as $i => &$arg) $append .= ' * ' . $arg . "\r\n"; + unset($i, $arg); + echo '[', date('Y-m-d H:i:s'), '] [', $action, '] ', $message, "\r\n", $append; } @@ -16339,12 +16438,14 @@ public static function ConnectionURI ($name = '') { $environment = Quark::Environment(); $host = Quark::Config()->StreamHost(); - foreach ($environment as $env) + foreach ($environment as $i => &$env) if ($env instanceof QuarkStreamEnvironment && $env->EnvironmentName() == $name) return $host == '' ? $env->ServerURI()->ConnectionURI() : $env->ServerURI()->ConnectionURI($host); + unset($i, $env); + return null; } @@ -17338,9 +17439,11 @@ public static function FromURI ($uri = '', $local = true) { $out = new self(); - foreach ($url as $key => $value) + foreach ($url as $key => &$value) $out->$key = $value; + unset($key, $value); + if ($rand) $out->port = 0; @@ -17610,17 +17713,16 @@ public static function ParseRoute ($source = '', $decode = true) { $route = explode('/', trim(Quark::NormalizePath(preg_replace('#\.php$#Uis', '', $query), false))); $buffer = array(); - foreach ($route as $component) { + foreach ($route as $i => &$component) { $item = trim($component); if (strlen($item) != 0) $buffer[] = $decode ? urldecode($item) : $item; } - $route = $buffer; - unset($buffer); + unset($i, $component); - return $route; + return $buffer; } /** @@ -17732,7 +17834,7 @@ public function RemoveOption ($key = '') { * @return bool */ public function Equal (QuarkURI $uri) { - foreach ($this as $key => $value) + foreach ($this as $key => &$value) if ($uri->$key != $value) return false; return true; @@ -18445,7 +18547,7 @@ public function Headers ($headers = []) { if (func_num_args() == 1 && is_array($headers)) { $assoc = QuarkObject::isAssociative($headers); - foreach ($headers as $key => $value) { + foreach ($headers as $key => &$value) { if (!$assoc) { $header = explode(': ', $value); $key = $header[0]; @@ -18454,6 +18556,8 @@ public function Headers ($headers = []) { $this->Header($key, $value); } + + unset($key, $value); } return $this->_headers; @@ -19124,9 +19228,11 @@ private function _serializeHeaders ($client, $str) { $this->_headers[self::HEADER_CONTENT_LANGUAGE] = QuarkLanguage::SerializeContentLanguage($this->_languages); } - foreach ($this->_headers as $key => $value) + foreach ($this->_headers as $key => &$value) $headers[] = $key . ': ' . $value; + unset($key, $value); + if ($this->_headerControl != null) { $control = $this->_headerControl; $out = $control($headers); @@ -19233,10 +19339,13 @@ private function _serializePart ($key, $value, $disposition) { * @return QuarkDTO */ private function _unserializeHeaders ($raw) { - if (preg_match_all('#(.*)\: (.*)\n#Uis', $raw . "\r\n", $headers, PREG_SET_ORDER)) + if (preg_match_all('#(.*)\: (.*)\n#Uis', $raw . "\r\n", $headers, PREG_SET_ORDER)) { foreach ($headers as $i => &$header) $this->Header($header[1], $header[2]); + unset($i, $header, $headers); + } + return $this; } @@ -19257,13 +19366,17 @@ private function _unserializeBody ($raw, $batch = false, $reset = false) { $this->_data = QuarkObject::Merge($this->_data, $this->_processor->Decode($chunk)); usleep(QuarkThreadSet::TICK); } + + unset($i, $chunk); } } else { $parts = explode('--' . $this->_boundary, $raw); - foreach ($parts as $part) + foreach ($parts as $i => &$part) $this->_unserializePart($part); + + unset($i, $part); } return $this; @@ -19278,10 +19391,13 @@ private function _unserializePart ($raw) { if (preg_match('#^(.*)\n\s\n(.*)$#Uis', $raw, $found)) { $head = array(); - if (preg_match_all('#(.*)\: (.*)\n#Uis', trim($raw) . "\r\n", $headers, PREG_SET_ORDER)) - foreach ($headers as $header) + if (preg_match_all('#(.*)\: (.*)\n#Uis', trim($raw) . "\r\n", $headers, PREG_SET_ORDER)) { + foreach ($headers as $i => &$header) $head[$header[1]] = trim($header[2]); + unset($i, $header, $headers); + } + if (isset($head[self::HEADER_CONTENT_DISPOSITION])) { $value = $head[self::HEADER_CONTENT_DISPOSITION]; $position = explode(';', $value)[0]; @@ -20144,13 +20260,15 @@ public static function FromCookie ($header = '') { $out = array(); $cookies = array_merge(explode(',', $header), explode(';', $header)); - foreach ($cookies as $raw) { + foreach ($cookies as $i => &$raw) { $cookie = explode('=', trim($raw)); if (sizeof($cookie) == 2) $out[] = new QuarkCookie($cookie[0], $cookie[1]); } + unset($i, $raw, $cookie, $cookies); + return $out; } @@ -20164,7 +20282,7 @@ public static function FromSetCookie ($header = '') { $instance = new QuarkCookie(); - foreach ($cookie as $component) { + foreach ($cookie as $i => &$component) { $item = explode('=', $component); $key = trim($item[0]); @@ -20177,6 +20295,8 @@ public static function FromSetCookie ($header = '') { } } + unset($i, $component, $item); + return $instance; } @@ -20188,9 +20308,11 @@ public static function FromSetCookie ($header = '') { public static function SerializeCookies ($cookies = []) { $out = ''; - foreach ($cookies as $cookie) + foreach ($cookies as $i => &$cookie) $out .= $cookie->name . '=' . $cookie->value . '; '; + unset($i, $cookie); + return substr($out, 0, strlen($out) - 2); } @@ -20201,15 +20323,15 @@ public static function SerializeCookies ($cookies = []) { */ public function Serialize ($full = false) { $out = $this->name . '=' . $this->value; - if (!$full) return $out; - else { - foreach ($this as $field => $value) - if (strlen(trim($value)) != 0 && $field != 'name' && $field != 'value') - $out .= '; ' . $field . '=' . $value; - return $out; - } + foreach ($this as $field => &$value) + if (strlen(trim($value)) != 0 && $field != 'name' && $field != 'value') + $out .= '; ' . $field . '=' . $value; + + unset($key, $value); + + return $out; } } @@ -20330,7 +20452,7 @@ public static function FromAcceptLanguage ($header = '') { $out = array(); $languages = explode(',', $header); - foreach ($languages as $raw) { + foreach ($languages as $i => &$raw) { $language = explode(';', $raw); $loc = explode('-', $language[0]); $q = explode('=', sizeof($language) == 1 ? 'q=1' : $language[1]); @@ -20338,6 +20460,8 @@ public static function FromAcceptLanguage ($header = '') { $out[] = new QuarkLanguage($language[0], array_reverse($q)[0], array_reverse($loc)[0]); } + unset($i, $raw, $language, $loc, $q, $languges); + return $out; } @@ -20350,9 +20474,11 @@ public static function FromContentLanguage ($header = '') { $out = array(); $languages = explode(',', $header); - foreach ($languages as $raw) + foreach ($languages as $i => &$raw) $out[] = new QuarkLanguage(trim($raw)); + unset($i, $raw, $languages); + return $out; } @@ -20369,9 +20495,11 @@ public static function SerializeAcceptLanguage ($languages = []) { /** * @var QuarkLanguage[] $languages */ - foreach ($languages as $language) + foreach ($languages as $i => &$language) $out[] = $language->Name() . ';q=' . $language->Quantity(); + unset($i, $language); + return implode(',', $out); } @@ -20388,9 +20516,11 @@ public static function SerializeContentLanguage ($languages = []) { /** * @var QuarkLanguage[] $languages */ - foreach ($languages as $language) + foreach ($languages as $i => &$language) $out[] = $language->Name(); + unset($i, $language); + return implode(',', $out); } } @@ -20538,24 +20668,28 @@ public static function FromHeader ($header = '') { $out = array(); $types = explode(',', $header); - foreach ($types as $raw) { + foreach ($types as $i => &$raw) { $type = explode(';', trim($raw)); $item = new QuarkMIMEType($type[0]); if (sizeof($type) > 1) { $params = array_slice($type, 1); - foreach ($params as $param) { + foreach ($params as $j => &$param) { $pair = explode('=', trim($param)); if (sizeof($pair) == 2) $item->Param($pair[0], $pair[1]); } + + unset($j, $param, $pair, $params); } $out[] = $item; } + unset($i, $raw, $type, $types); + return $out; } } @@ -21218,20 +21352,26 @@ public static function FromLocation ($location = '', $load = false) { public static function FromFiles ($files) { $output = array(); - foreach ($files as $name => $value) { + foreach ($files as $name => &$value) { $output[$name] = array(); - foreach ($value as $param => $data) { + foreach ($value as $param => &$data) { if (!is_array($data)) { self::_file_populate($output, $name, $param, $data); continue; } - foreach ($data as $k => $v) + foreach ($data as $k => &$v) self::_file_buffer($output[$name], $param, $k, $v); + + unset($k, $v); } + + unset($param, $data); } + unset($name, $value); + return $output; } @@ -21262,8 +21402,10 @@ private static function _file_buffer (&$item, $name, $index, $value) { return; } - foreach ($value as $i => $v) + foreach ($value as $i => &$v) self::_file_buffer($item[$index], $name, $i, $v); + + unset($i, $v); } /** @@ -21326,6 +21468,8 @@ public static function Directory ($location = '', $recursive = false) { $out[] = $item; } + unset($file, $dir, $fs); + return $out; } @@ -22203,11 +22347,13 @@ public function Encode ($data, $meta = true) { } if (QuarkObject::isAssociative($data)) { - foreach ($data as $key => $value) + foreach ($data as $key => &$value) $out .= $value instanceof QuarkXMLNode ? $value->ToXML($this, $key) : '<' . $key . '>' . $this->Encode($value) . ''; + unset($key, $value); + return $out; } @@ -22249,7 +22395,7 @@ public function Decode ($raw, $_meta = true) { $this->_root->Attributes(self::DecodeAttributes($xml[0][2])); } - foreach ($xml as $value) { + foreach ($xml as $k => &$value) { $key = $value[1]; $buffer = null; @@ -22286,6 +22432,8 @@ public function Decode ($raw, $_meta = true) { } } + unset($k, $value, $xml); + return QuarkObject::isIterative($out) ? $out : (object)$out; } @@ -22299,9 +22447,11 @@ public static function DecodeAttributes ($data = '') { $out = array(); - foreach ($attributes as $attribute) + foreach ($attributes as $i => &$attribute) $out[$attribute[1]] = $attribute[2]; + unset($i, $attribute, $attributes); + return (object)$out; } @@ -22498,10 +22648,12 @@ public function ToXML (QuarkXMLIOProcessor $processor, $node = '') { $attributes = ''; $node = $this->_name == '' ? $node : $this->_name; - foreach ($this->_attributes as $key => $value) + foreach ($this->_attributes as $key => &$value) if ($value !== null || ($value === null && $processor->ForceNull())) $attributes .= ' ' . $key . '="'. $value . '"'; + unset($key, $value); + return $this->_single ? ('<' . $node . $attributes . ' />') : ('<' . $node . $attributes . '>' . $processor->Encode($this->_data) . ''); @@ -22664,7 +22816,7 @@ public function Encode ($data) { $out = ''; - foreach ($data as $name => $section) { + foreach ($data as $name => &$section) { if (!is_array($section) && !is_object($section)) { $out .= $name . ' = ' . QuarkObject::Stringify($section) . "\r\n"; continue; @@ -22672,11 +22824,15 @@ public function Encode ($data) { $out .= '[' . $name . ']' . "\r\n"; - foreach ($section as $key => $value) + foreach ($section as $key => &$value) $out .= $key . ' = ' . QuarkObject::Stringify($value) . "\r\n"; + + unset($key, $value); $out .= "\r\n"; } + + unset($name, $section); return $out; } @@ -22706,9 +22862,11 @@ public function Decode ($raw) { $out = array(); - foreach ($ini as $value) + foreach ($ini as $key => &$value) $out[$value[1]] = self::DecodePairs($value[2], $this->_cast); + unset($key, $value, $ini); + return QuarkObject::Merge($out); } @@ -22723,7 +22881,7 @@ public static function DecodePairs ($raw = '', $cast = true) { $out = array(); - foreach ($pairs as $pair) { + foreach ($pairs as $i => &$pair) { $value = trim($pair[2]); if ($cast) { @@ -22739,6 +22897,8 @@ public static function DecodePairs ($raw = '', $cast = true) { $out[trim($pair[1])] = $value; } + + unset($i, $pair, $pairs); return QuarkObject::Merge($out); } @@ -22968,9 +23128,11 @@ public function SigningRequest ($algo = self::ALGO_SHA512, $length = self::DEFAU $data = array(); - foreach ($this as $property => $value) + foreach ($this as $property => &$value) if (in_array($property, self::$_allowed, true)) $data[$property] = $value; + unset($property, $value); + $csr = @openssl_csr_new($data, $this->_key->PrivateKey(false), $config); if (!$csr) return self::_error('SigningRequest: Cannot generate CSR'); @@ -23212,13 +23374,15 @@ public static function FromAltName ($altName = '') { $out = array(); $sans = explode(',', $altName); - foreach ($sans as $item) { + foreach ($sans as $i => &$item) { $san = explode(':', $item); if (sizeof($san) == 2) $out[] = new self($san[1], $san[0]); } + unset($i, $item, $san, $sans); + return $out; } } @@ -24259,7 +24423,7 @@ public function Condition ($condition, $glue = '') { $output = array(); - foreach ($condition as $key => $rule) { + foreach ($condition as $key => &$rule) { $field = $this->Field($key); $value = $this->Value($rule); @@ -24299,6 +24463,8 @@ public function Condition ($condition, $glue = '') { } } + unset($key, $rule); + return ($glue == '' ? ' WHERE ' : '') . implode($glue == '' ? ' AND ' : $glue, $output); } @@ -24313,7 +24479,7 @@ private function _cursor ($options) { if (isset($options[QuarkModel::OPTION_SORT]) && is_array($options[QuarkModel::OPTION_SORT])) { $output .= ' ORDER BY '; - foreach ($options[QuarkModel::OPTION_SORT] as $key => $order) { + foreach ($options[QuarkModel::OPTION_SORT] as $key => &$order) { switch ($order) { case 1: $sort = 'ASC'; break; case -1: $sort = 'DESC'; break; @@ -24323,6 +24489,8 @@ private function _cursor ($options) { $output .= $this->Field($key) . ' ' . $sort . ','; } + unset($key, $order); + $output = trim($output, ','); } @@ -24378,11 +24546,13 @@ public function Insert (IQuarkModel $model, $options = []) { $keys = array(); $values = array(); - foreach ($model as $key => $value) { + foreach ($model as $key => &$value) { $keys[] = $this->Field($key); $values[] = $this->Value($value); } + unset($key, $value); + return $this->Query( $model, $options, @@ -24578,12 +24748,14 @@ public static function MinimizeString ($source = '', $trim = []) { $source = preg_replace('/\s+/', ' ', $source); $source = str_replace(' &$rule) { $source = str_replace(' ' . $rule . ' ', $rule, $source); $source = str_replace(' ' . $rule, $rule, $source); $source = str_replace($rule . ' ', $rule, $source); } + unset($i, $rule); + return trim($source); } diff --git a/Scenarios/Generate/Certificate.php b/Scenarios/Generate/Certificate.php index 5c787127..443e7364 100644 --- a/Scenarios/Generate/Certificate.php +++ b/Scenarios/Generate/Certificate.php @@ -6,9 +6,9 @@ use Quark\Quark; use Quark\QuarkCLIBehavior; -use Quark\Extensions\SSLAuthority\SSLAuthority; -use Quark\Extensions\SSLAuthority\SSLAuthorityConfig; -use Quark\Extensions\SSLAuthority\Providers\LetsEncrypt; +use Quark\Extensions\CertificateAuthority\CertificateAuthority; +use Quark\Extensions\CertificateAuthority\CertificateAuthorityConfig; +use Quark\Extensions\CertificateAuthority\Providers\LetsEncrypt; /** * Class Certificate @@ -28,7 +28,7 @@ class Certificate implements IQuarkTask { * @return mixed */ public function Task ($argc, $argv) { - Quark::Config()->Extension(self::EXTENSION, new SSLAuthorityConfig(new LetsEncrypt($this->HasFlag('staging', 's')))); + Quark::Config()->Extension(self::EXTENSION, new CertificateAuthorityConfig(new LetsEncrypt($this->HasFlag('staging', 's')))); Quark::Config()->ExtensionOptions(self::EXTENSION); $domains = $this->ServiceArg(0); @@ -47,8 +47,8 @@ public function Task ($argc, $argv) { $this->ShellProcessStatus(Quark::LOG_OK, 'Saved to ' . $target . ".\r\n"), $this->ShellProcessStatus(Quark::LOG_FATAL, 'See application log for details.' . "\r\n"), function () use (&$domains, &$target) { - $ssl = new SSLAuthority(self::EXTENSION); - $certificate = $ssl->SignDomains(explode(',', $domains)); + $authority = new CertificateAuthority(self::EXTENSION); + $certificate = $authority->SignDomains(explode(',', $domains)); return $certificate != null && $certificate->SaveTo($target); }