D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
everqlsh
/
www
/
wp-admin
/
user
/
577040
/
Filename :
src.tar
back
Copy
Cache.php 0000644 00000006260 15162130104 0006255 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; use SimplePie\Cache\Base; /** * Used to create cache objects * * This class can be overloaded with {@see SimplePie::set_cache_class()}, * although the preferred way is to create your own handler * via {@see register()} * * @deprecated since SimplePie 1.8.0, use "SimplePie\SimplePie::set_cache()" instead */ class Cache { /** * Cache handler classes * * These receive 3 parameters to their constructor, as documented in * {@see register()} * @var array<string, class-string<Base>> */ protected static $handlers = [ 'mysql' => Cache\MySQL::class, 'memcache' => Cache\Memcache::class, 'memcached' => Cache\Memcached::class, 'redis' => Cache\Redis::class, ]; /** * Don't call the constructor. Please. */ private function __construct() { } /** * Create a new SimplePie\Cache object * * @param string $location URL location (scheme is used to determine handler) * @param string $filename Unique identifier for cache object * @param Base::TYPE_FEED|Base::TYPE_IMAGE $extension 'spi' or 'spc' * @return Base Type of object depends on scheme of `$location` */ public static function get_handler(string $location, string $filename, $extension) { $type = explode(':', $location, 2); $type = $type[0]; if (!empty(self::$handlers[$type])) { $class = self::$handlers[$type]; return new $class($location, $filename, $extension); } return new \SimplePie\Cache\File($location, $filename, $extension); } /** * Create a new SimplePie\Cache object * * @deprecated since SimplePie 1.3.1, use {@see get_handler()} instead * @param string $location * @param string $filename * @param Base::TYPE_FEED|Base::TYPE_IMAGE $extension * @return Base */ public function create(string $location, string $filename, $extension) { trigger_error('Cache::create() has been replaced with Cache::get_handler() since SimplePie 1.3.1, use the registry system instead.', \E_USER_DEPRECATED); return self::get_handler($location, $filename, $extension); } /** * Register a handler * * @param string $type DSN type to register for * @param class-string<Base> $class Name of handler class. Must implement Base * @return void */ public static function register(string $type, $class) { self::$handlers[$type] = $class; } /** * Parse a URL into an array * * @param string $url * @return array<string, mixed> */ public static function parse_URL(string $url) { $parsedUrl = parse_url($url); if ($parsedUrl === false) { return []; } $params = array_merge($parsedUrl, ['extras' => []]); if (isset($params['query'])) { parse_str($params['query'], $params['extras']); } return $params; } } class_alias('SimplePie\Cache', 'SimplePie_Cache'); Caption.php 0000644 00000006033 15162130105 0006646 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; /** * Handles `<media:text>` captions as defined in Media RSS. * * Used by {@see \SimplePie\Enclosure::get_caption()} and {@see \SimplePie\Enclosure::get_captions()} * * This class can be overloaded with {@see \SimplePie\SimplePie::set_caption_class()} */ class Caption { /** * Content type * * @var ?string * @see get_type() */ public $type; /** * Language * * @var ?string * @see get_language() */ public $lang; /** * Start time * * @var ?string * @see get_starttime() */ public $startTime; /** * End time * * @var ?string * @see get_endtime() */ public $endTime; /** * Caption text * * @var ?string * @see get_text() */ public $text; /** * Constructor, used to input the data * * For documentation on all the parameters, see the corresponding * properties and their accessors */ public function __construct( ?string $type = null, ?string $lang = null, ?string $startTime = null, ?string $endTime = null, ?string $text = null ) { $this->type = $type; $this->lang = $lang; $this->startTime = $startTime; $this->endTime = $endTime; $this->text = $text; } /** * String-ified version * * @return string */ public function __toString() { // There is no $this->data here return md5(serialize($this)); } /** * Get the end time * * @return string|null Time in the format 'hh:mm:ss.SSS' */ public function get_endtime() { if ($this->endTime !== null) { return $this->endTime; } return null; } /** * Get the language * * @link http://tools.ietf.org/html/rfc3066 * @return string|null Language code as per RFC 3066 */ public function get_language() { if ($this->lang !== null) { return $this->lang; } return null; } /** * Get the start time * * @return string|null Time in the format 'hh:mm:ss.SSS' */ public function get_starttime() { if ($this->startTime !== null) { return $this->startTime; } return null; } /** * Get the text of the caption * * @return string|null */ public function get_text() { if ($this->text !== null) { return $this->text; } return null; } /** * Get the content type (not MIME type) * * @return string|null Either 'text' or 'html' */ public function get_type() { if ($this->type !== null) { return $this->type; } return null; } } class_alias('SimplePie\Caption', 'SimplePie_Caption'); Item.php 0000644 00000401045 15162130106 0006152 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; /** * Manages all item-related data * * Used by {@see \SimplePie\SimplePie::get_item()} and {@see \SimplePie\SimplePie::get_items()} * * This class can be overloaded with {@see \SimplePie\SimplePie::set_item_class()} */ class Item implements RegistryAware { /** * Parent feed * * @access private * @var \SimplePie\SimplePie */ public $feed; /** * Raw data * * @access private * @var array<string, mixed> */ public $data = []; /** * Registry object * * @see set_registry * @var \SimplePie\Registry */ protected $registry; /** * @var Sanitize|null */ private $sanitize = null; /** * Create a new item object * * This is usually used by {@see \SimplePie\SimplePie::get_items} and * {@see \SimplePie\SimplePie::get_item}. Avoid creating this manually. * * @param \SimplePie\SimplePie $feed Parent feed * @param array<string, mixed> $data Raw data */ public function __construct(\SimplePie\SimplePie $feed, array $data) { $this->feed = $feed; $this->data = $data; } /** * Set the registry handler * * This is usually used by {@see \SimplePie\Registry::create} * * @since 1.3 * @param \SimplePie\Registry $registry * @return void */ public function set_registry(\SimplePie\Registry $registry) { $this->registry = $registry; } /** * Get a string representation of the item * * @return string */ public function __toString() { return md5(serialize($this->data)); } /** * Remove items that link back to this before destroying this object */ public function __destruct() { if (!gc_enabled()) { unset($this->feed); } } /** * Get data for an item-level element * * This method allows you to get access to ANY element/attribute that is a * sub-element of the item/entry tag. * * See {@see \SimplePie\SimplePie::get_feed_tags()} for a description of the return value * * @since 1.0 * @see http://simplepie.org/wiki/faq/supported_xml_namespaces * @param string $namespace The URL of the XML namespace of the elements you're trying to access * @param string $tag Tag name * @return array<array<string, mixed>>|null */ public function get_item_tags(string $namespace, string $tag) { if (isset($this->data['child'][$namespace][$tag])) { return $this->data['child'][$namespace][$tag]; } return null; } /** * Get base URL of the item itself. * Returns `<xml:base>` or feed base URL. * Similar to `Item::get_base()` but can safely be used during initialisation methods * such as `Item::get_links()` (`Item::get_base()` and `Item::get_links()` call each-other) * and is not affected by enclosures. * * @param array<string, mixed> $element * @see get_base */ private function get_own_base(array $element = []): string { if (!empty($element['xml_base_explicit']) && isset($element['xml_base'])) { return $element['xml_base']; } return $this->feed->get_base(); } /** * Get the base URL value. * Uses `<xml:base>`, or item link, or enclosure link, or feed base URL. * * @param array<string, mixed> $element * @return string */ public function get_base(array $element = []) { if (!empty($element['xml_base_explicit']) && isset($element['xml_base'])) { return $element['xml_base']; } $link = $this->get_permalink(); if ($link != null) { return $link; } return $this->feed->get_base($element); } /** * Sanitize feed data * * @access private * @see \SimplePie\SimplePie::sanitize() * @param string $data Data to sanitize * @param int-mask-of<SimplePie::CONSTRUCT_*> $type * @param string $base Base URL to resolve URLs against * @return string Sanitized data */ public function sanitize(string $data, int $type, string $base = '') { // This really returns string|false but changing encoding is uncommon and we are going to deprecate it, so let’s just lie to PHPStan in the interest of cleaner annotations. return $this->feed->sanitize($data, $type, $base); } /** * Get the parent feed * * Note: this may not work as you think for multifeeds! * * @link http://simplepie.org/faq/typical_multifeed_gotchas#missing_data_from_feed * @since 1.0 * @return \SimplePie\SimplePie */ public function get_feed() { return $this->feed; } /** * Get the unique identifier for the item * * This is usually used when writing code to check for new items in a feed. * * Uses `<atom:id>`, `<guid>`, `<dc:identifier>` or the `about` attribute * for RDF. If none of these are supplied (or `$hash` is true), creates an * MD5 hash based on the permalink, title and content. * * @since Beta 2 * @param bool $hash Should we force using a hash instead of the supplied ID? * @param string|false $fn User-supplied function to generate an hash * @return string|null */ public function get_id(bool $hash = false, $fn = 'md5') { if (!$hash) { if ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'id')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'id')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'guid')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, 'identifier')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_10, 'identifier')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif (isset($this->data['attribs'][\SimplePie\SimplePie::NAMESPACE_RDF]['about'])) { return $this->sanitize($this->data['attribs'][\SimplePie\SimplePie::NAMESPACE_RDF]['about'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } } if ($fn === false) { return null; } elseif (!is_callable($fn)) { trigger_error('User-supplied function $fn must be callable', E_USER_WARNING); $fn = 'md5'; } return call_user_func( $fn, $this->get_permalink().$this->get_title().$this->get_content() ); } /** * Get the title of the item * * Uses `<atom:title>`, `<title>` or `<dc:title>` * * @since Beta 2 (previously called `get_item_title` since 0.8) * @return string|null */ public function get_title() { if (!isset($this->data['title'])) { if ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'title')) { $this->data['title'] = $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'title')) { $this->data['title'] = $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_10, 'title')) { $this->data['title'] = $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_090, 'title')) { $this->data['title'] = $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'title')) { $this->data['title'] = $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, 'title')) { $this->data['title'] = $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_10, 'title')) { $this->data['title'] = $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $this->data['title'] = null; } } return $this->data['title']; } /** * Get the content for the item * * Prefers summaries over full content , but will return full content if a * summary does not exist. * * To prefer full content instead, use {@see get_content} * * Uses `<atom:summary>`, `<description>`, `<dc:description>` or * `<itunes:subtitle>` * * @since 0.8 * @param bool $description_only Should we avoid falling back to the content? * @return string|null */ public function get_description(bool $description_only = false) { if (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'summary')) && ($return = $this->sanitize($tags[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$tags[0]['attribs']]), $this->get_base($tags[0])))) { return $return; } elseif (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'summary')) && ($return = $this->sanitize($tags[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$tags[0]['attribs']]), $this->get_base($tags[0])))) { return $return; } elseif (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_10, 'description')) && ($return = $this->sanitize($tags[0]['data'], \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML, $this->get_base($tags[0])))) { return $return; } elseif (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'description')) && ($return = $this->sanitize($tags[0]['data'], \SimplePie\SimplePie::CONSTRUCT_HTML, $this->get_base($tags[0])))) { return $return; } elseif (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, 'description')) && ($return = $this->sanitize($tags[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT))) { return $return; } elseif (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_10, 'description')) && ($return = $this->sanitize($tags[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT))) { return $return; } elseif (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'summary')) && ($return = $this->sanitize($tags[0]['data'], \SimplePie\SimplePie::CONSTRUCT_HTML, $this->get_base($tags[0])))) { return $return; } elseif (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'subtitle')) && ($return = $this->sanitize($tags[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT))) { return $return; } elseif (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_090, 'description')) && ($return = $this->sanitize($tags[0]['data'], \SimplePie\SimplePie::CONSTRUCT_HTML))) { return $return; } elseif (!$description_only) { return $this->get_content(true); } return null; } /** * Get the content for the item * * Prefers full content over summaries, but will return a summary if full * content does not exist. * * To prefer summaries instead, use {@see get_description} * * Uses `<atom:content>` or `<content:encoded>` (RSS 1.0 Content Module) * * @since 1.0 * @param bool $content_only Should we avoid falling back to the description? * @return string|null */ public function get_content(bool $content_only = false) { if (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'content')) && ($return = $this->sanitize($tags[0]['data'], $this->registry->call(Misc::class, 'atom_10_content_construct_type', [$tags[0]['attribs']]), $this->get_base($tags[0])))) { return $return; } elseif (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'content')) && ($return = $this->sanitize($tags[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$tags[0]['attribs']]), $this->get_base($tags[0])))) { return $return; } elseif (($tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_10_MODULES_CONTENT, 'encoded')) && ($return = $this->sanitize($tags[0]['data'], \SimplePie\SimplePie::CONSTRUCT_HTML, $this->get_base($tags[0])))) { return $return; } elseif (!$content_only) { return $this->get_description(true); } return null; } /** * Get the media:thumbnail of the item * * Uses `<media:thumbnail>` * * * @return array{url: string, height?: string, width?: string, time?: string}|null */ public function get_thumbnail() { if (!isset($this->data['thumbnail'])) { if ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'thumbnail')) { $thumbnail = $return[0]['attribs']['']; if (empty($thumbnail['url'])) { $this->data['thumbnail'] = null; } else { $thumbnail['url'] = $this->sanitize($thumbnail['url'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($return[0])); $this->data['thumbnail'] = $thumbnail; } } else { $this->data['thumbnail'] = null; } } return $this->data['thumbnail']; } /** * Get a category for the item * * @since Beta 3 (previously called `get_categories()` since Beta 2) * @param int $key The category that you want to return. Remember that arrays begin with 0, not 1 * @return \SimplePie\Category|null */ public function get_category(int $key = 0) { $categories = $this->get_categories(); if (isset($categories[$key])) { return $categories[$key]; } return null; } /** * Get all categories for the item * * Uses `<atom:category>`, `<category>` or `<dc:subject>` * * @since Beta 3 * @return \SimplePie\Category[]|null List of {@see \SimplePie\Category} objects */ public function get_categories() { $categories = []; $type = 'category'; foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, $type) as $category) { $term = null; $scheme = null; $label = null; if (isset($category['attribs']['']['term'])) { $term = $this->sanitize($category['attribs']['']['term'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($category['attribs']['']['scheme'])) { $scheme = $this->sanitize($category['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($category['attribs']['']['label'])) { $label = $this->sanitize($category['attribs']['']['label'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $categories[] = $this->registry->create(Category::class, [$term, $scheme, $label, $type]); } foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, $type) as $category) { // This is really the label, but keep this as the term also for BC. // Label will also work on retrieving because that falls back to term. $term = $this->sanitize($category['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); if (isset($category['attribs']['']['domain'])) { $scheme = $this->sanitize($category['attribs']['']['domain'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $scheme = null; } $categories[] = $this->registry->create(Category::class, [$term, $scheme, null, $type]); } $type = 'subject'; foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, $type) as $category) { $categories[] = $this->registry->create(Category::class, [$this->sanitize($category['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT), null, null, $type]); } foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_10, $type) as $category) { $categories[] = $this->registry->create(Category::class, [$this->sanitize($category['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT), null, null, $type]); } if (!empty($categories)) { return array_unique($categories); } return null; } /** * Get an author for the item * * @since Beta 2 * @param int $key The author that you want to return. Remember that arrays begin with 0, not 1 * @return \SimplePie\Author|null */ public function get_author(int $key = 0) { $authors = $this->get_authors(); if (isset($authors[$key])) { return $authors[$key]; } return null; } /** * Get a contributor for the item * * @since 1.1 * @param int $key The contrbutor that you want to return. Remember that arrays begin with 0, not 1 * @return \SimplePie\Author|null */ public function get_contributor(int $key = 0) { $contributors = $this->get_contributors(); if (isset($contributors[$key])) { return $contributors[$key]; } return null; } /** * Get all contributors for the item * * Uses `<atom:contributor>` * * @since 1.1 * @return \SimplePie\Author[]|null List of {@see \SimplePie\Author} objects */ public function get_contributors() { $contributors = []; foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'contributor') as $contributor) { $name = null; $uri = null; $email = null; if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['name'][0]['data'])) { $name = $this->sanitize($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['name'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0]['data'])) { $uri = $contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0]; $uri = $this->sanitize($uri['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($uri)); } if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['email'][0]['data'])) { $email = $this->sanitize($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['email'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if ($name !== null || $email !== null || $uri !== null) { $contributors[] = $this->registry->create(Author::class, [$name, $uri, $email]); } } foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'contributor') as $contributor) { $name = null; $url = null; $email = null; if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['name'][0]['data'])) { $name = $this->sanitize($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['name'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0]['data'])) { $url = $contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0]; $url = $this->sanitize($url['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($url)); } if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['email'][0]['data'])) { $email = $this->sanitize($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['email'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if ($name !== null || $email !== null || $url !== null) { $contributors[] = $this->registry->create(Author::class, [$name, $url, $email]); } } if (!empty($contributors)) { return array_unique($contributors); } return null; } /** * Get all authors for the item * * Uses `<atom:author>`, `<author>`, `<dc:creator>` or `<itunes:author>` * * @since Beta 2 * @return \SimplePie\Author[]|null List of {@see \SimplePie\Author} objects */ public function get_authors() { $authors = []; foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'author') as $author) { $name = null; $uri = null; $email = null; if (isset($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['name'][0]['data'])) { $name = $this->sanitize($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['name'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0]['data'])) { $uri = $author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0]; $uri = $this->sanitize($uri['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($uri)); } if (isset($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['email'][0]['data'])) { $email = $this->sanitize($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['email'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if ($name !== null || $email !== null || $uri !== null) { $authors[] = $this->registry->create(Author::class, [$name, $uri, $email]); } } if ($author = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'author')) { $name = null; $url = null; $email = null; if (isset($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['name'][0]['data'])) { $name = $this->sanitize($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['name'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0]['data'])) { $url = $author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0]; $url = $this->sanitize($url['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($url)); } if (isset($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['email'][0]['data'])) { $email = $this->sanitize($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['email'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if ($name !== null || $email !== null || $url !== null) { $authors[] = $this->registry->create(Author::class, [$name, $url, $email]); } } if ($author = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'author')) { $authors[] = $this->registry->create(Author::class, [null, null, $this->sanitize($author[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT)]); } foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, 'creator') as $author) { $authors[] = $this->registry->create(Author::class, [$this->sanitize($author['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT), null, null]); } foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_10, 'creator') as $author) { $authors[] = $this->registry->create(Author::class, [$this->sanitize($author['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT), null, null]); } foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'author') as $author) { $authors[] = $this->registry->create(Author::class, [$this->sanitize($author['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT), null, null]); } if (!empty($authors)) { return array_unique($authors); } elseif (($source = $this->get_source()) && ($authors = $source->get_authors())) { return $authors; } elseif ($authors = $this->feed->get_authors()) { return $authors; } return null; } /** * Get the copyright info for the item * * Uses `<atom:rights>` or `<dc:rights>` * * @since 1.1 * @return string|null */ public function get_copyright() { if ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'rights')) { return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, 'rights')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_10, 'rights')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } return null; } /** * Get the posting date/time for the item * * Uses `<atom:published>`, `<atom:updated>`, `<atom:issued>`, * `<atom:modified>`, `<pubDate>` or `<dc:date>` * * Note: obeys PHP's timezone setting. To get a UTC date/time, use * {@see get_gmdate} * * @since Beta 2 (previously called `get_item_date` since 0.8) * * @param string $date_format Supports any PHP date format from {@see http://php.net/date} (empty for the raw data) * @return ($date_format is 'U' ? ?int : ?string) */ public function get_date(string $date_format = 'j F Y, g:i a') { if (!isset($this->data['date'])) { if ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'published')) { $this->data['date']['raw'] = $return[0]['data']; } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'pubDate')) { $this->data['date']['raw'] = $return[0]['data']; } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, 'date')) { $this->data['date']['raw'] = $return[0]['data']; } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_DC_10, 'date')) { $this->data['date']['raw'] = $return[0]['data']; } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'updated')) { $this->data['date']['raw'] = $return[0]['data']; } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'issued')) { $this->data['date']['raw'] = $return[0]['data']; } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'created')) { $this->data['date']['raw'] = $return[0]['data']; } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'modified')) { $this->data['date']['raw'] = $return[0]['data']; } if (!empty($this->data['date']['raw'])) { $parser = $this->registry->call(Parse\Date::class, 'get'); $this->data['date']['parsed'] = $parser->parse($this->data['date']['raw']) ?: null; } else { $this->data['date'] = null; } } if ($this->data['date']) { switch ($date_format) { case '': return $this->sanitize($this->data['date']['raw'], \SimplePie\SimplePie::CONSTRUCT_TEXT); case 'U': return $this->data['date']['parsed']; default: return date($date_format, $this->data['date']['parsed']); } } return null; } /** * Get the update date/time for the item * * Uses `<atom:updated>` * * Note: obeys PHP's timezone setting. To get a UTC date/time, use * {@see get_gmdate} * * @param string $date_format Supports any PHP date format from {@see http://php.net/date} (empty for the raw data) * @return ($date_format is 'U' ? ?int : ?string) */ public function get_updated_date(string $date_format = 'j F Y, g:i a') { if (!isset($this->data['updated'])) { if ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'updated')) { $this->data['updated']['raw'] = $return[0]['data']; } if (!empty($this->data['updated']['raw'])) { $parser = $this->registry->call(Parse\Date::class, 'get'); $this->data['updated']['parsed'] = $parser->parse($this->data['updated']['raw']) ?: null; } else { $this->data['updated'] = null; } } if ($this->data['updated']) { switch ($date_format) { case '': return $this->sanitize($this->data['updated']['raw'], \SimplePie\SimplePie::CONSTRUCT_TEXT); case 'U': return $this->data['updated']['parsed']; default: return date($date_format, $this->data['updated']['parsed']); } } return null; } /** * Get the localized posting date/time for the item * * Returns the date formatted in the localized language. To display in * languages other than the server's default, you need to change the locale * with {@link http://php.net/setlocale setlocale()}. The available * localizations depend on which ones are installed on your web server. * * @since 1.0 * * @param string $date_format Supports any PHP date format from {@see http://php.net/strftime} (empty for the raw data) * @return string|null|false see `strftime` for when this can return `false` */ public function get_local_date(string $date_format = '%c') { if ($date_format === '') { if (($raw_date = $this->get_date('')) === null) { return null; } return $this->sanitize($raw_date, \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif (($date = $this->get_date('U')) !== null && $date !== false) { return strftime($date_format, $date); } return null; } /** * Get the posting date/time for the item (UTC time) * * @see get_date * @param string $date_format Supports any PHP date format from {@see http://php.net/date} * @return string|null */ public function get_gmdate(string $date_format = 'j F Y, g:i a') { $date = $this->get_date('U'); if ($date === null) { return null; } return gmdate($date_format, $date); } /** * Get the update date/time for the item (UTC time) * * @see get_updated_date * @param string $date_format Supports any PHP date format from {@see http://php.net/date} * @return string|null */ public function get_updated_gmdate(string $date_format = 'j F Y, g:i a') { $date = $this->get_updated_date('U'); if ($date === null) { return null; } return gmdate($date_format, $date); } /** * Get the permalink for the item * * Returns the first link available with a relationship of "alternate". * Identical to {@see get_link()} with key 0 * * @see get_link * @since 0.8 * @return string|null Permalink URL */ public function get_permalink() { $link = $this->get_link(); $enclosure = $this->get_enclosure(0); if ($link !== null) { return $link; } elseif ($enclosure !== null) { return $enclosure->get_link(); } return null; } /** * Get a single link for the item * * @since Beta 3 * @param int $key The link that you want to return. Remember that arrays begin with 0, not 1 * @param string $rel The relationship of the link to return * @return string|null Link URL */ public function get_link(int $key = 0, string $rel = 'alternate') { $links = $this->get_links($rel); if ($links && $links[$key] !== null) { return $links[$key]; } return null; } /** * Get all links for the item * * Uses `<atom:link>`, `<link>` or `<guid>` * * @since Beta 2 * @param string $rel The relationship of links to return * @return array<string>|null Links found for the item (strings) */ public function get_links(string $rel = 'alternate') { if (!isset($this->data['links'])) { $this->data['links'] = []; foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'link') as $link) { if (isset($link['attribs']['']['href'])) { $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate'; $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($link)); } } foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'link') as $link) { if (isset($link['attribs']['']['href'])) { $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate'; $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($link)); } } if ($links = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_10, 'link')) { $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($links[0])); } if ($links = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_090, 'link')) { $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($links[0])); } if ($links = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'link')) { $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($links[0])); } if ($links = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'guid')) { if (!isset($links[0]['attribs']['']['isPermaLink']) || strtolower(trim($links[0]['attribs']['']['isPermaLink'])) === 'true') { $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($links[0])); } } $keys = array_keys($this->data['links']); foreach ($keys as $key) { if ($this->registry->call(Misc::class, 'is_isegment_nz_nc', [$key])) { if (isset($this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key])) { $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key] = array_merge($this->data['links'][$key], $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key]); $this->data['links'][$key] = &$this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key]; } else { $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key] = &$this->data['links'][$key]; } } elseif (substr((string) $key, 0, 41) === \SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY) { $this->data['links'][substr((string) $key, 41)] = &$this->data['links'][$key]; } $this->data['links'][$key] = array_unique($this->data['links'][$key]); } } if (isset($this->data['links'][$rel])) { return $this->data['links'][$rel]; } return null; } /** * Get an enclosure from the item * * Supports the <enclosure> RSS tag, as well as Media RSS and iTunes RSS. * * @since Beta 2 * @todo Add ability to prefer one type of content over another (in a media group). * @param int $key The enclosure that you want to return. Remember that arrays begin with 0, not 1 * @return \SimplePie\Enclosure|null */ public function get_enclosure(int $key = 0) { $enclosures = $this->get_enclosures(); if (isset($enclosures[$key])) { return $enclosures[$key]; } return null; } /** * Get all available enclosures (podcasts, etc.) * * Supports the <enclosure> RSS tag, as well as Media RSS and iTunes RSS. * * At this point, we're pretty much assuming that all enclosures for an item * are the same content. Anything else is too complicated to * properly support. * * @since Beta 2 * @todo Add support for end-user defined sorting of enclosures by type/handler (so we can prefer the faster-loading FLV over MP4). * @todo If an element exists at a level, but its value is empty, we should fall back to the value from the parent (if it exists). * @return \SimplePie\Enclosure[]|null List of \SimplePie\Enclosure items */ public function get_enclosures() { if (!isset($this->data['enclosures'])) { $this->data['enclosures'] = []; // Elements $captions_parent = null; $categories_parent = null; $copyrights_parent = null; $credits_parent = null; $description_parent = null; $duration_parent = null; $hashes_parent = null; $keywords_parent = null; $player_parent = null; $ratings_parent = null; $restrictions_parent = []; $thumbnails_parent = null; $title_parent = null; // Let's do the channel and item-level ones first, and just re-use them if we need to. $parent = $this->get_feed(); // CAPTIONS if ($captions = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'text')) { foreach ($captions as $caption) { $caption_type = null; $caption_lang = null; $caption_startTime = null; $caption_endTime = null; $caption_text = null; if (isset($caption['attribs']['']['type'])) { $caption_type = $this->sanitize($caption['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['lang'])) { $caption_lang = $this->sanitize($caption['attribs']['']['lang'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['start'])) { $caption_startTime = $this->sanitize($caption['attribs']['']['start'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['end'])) { $caption_endTime = $this->sanitize($caption['attribs']['']['end'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['data'])) { $caption_text = $this->sanitize($caption['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $captions_parent[] = $this->registry->create(Caption::class, [$caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text]); } } elseif ($captions = $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'text')) { foreach ($captions as $caption) { $caption_type = null; $caption_lang = null; $caption_startTime = null; $caption_endTime = null; $caption_text = null; if (isset($caption['attribs']['']['type'])) { $caption_type = $this->sanitize($caption['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['lang'])) { $caption_lang = $this->sanitize($caption['attribs']['']['lang'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['start'])) { $caption_startTime = $this->sanitize($caption['attribs']['']['start'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['end'])) { $caption_endTime = $this->sanitize($caption['attribs']['']['end'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['data'])) { $caption_text = $this->sanitize($caption['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $captions_parent[] = $this->registry->create(Caption::class, [$caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text]); } } if (is_array($captions_parent)) { $captions_parent = array_values(array_unique($captions_parent)); } // CATEGORIES foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'category') as $category) { $term = null; $scheme = null; $label = null; if (isset($category['data'])) { $term = $this->sanitize($category['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($category['attribs']['']['scheme'])) { $scheme = $this->sanitize($category['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $scheme = 'http://search.yahoo.com/mrss/category_schema'; } if (isset($category['attribs']['']['label'])) { $label = $this->sanitize($category['attribs']['']['label'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $categories_parent[] = $this->registry->create(Category::class, [$term, $scheme, $label]); } foreach ((array) $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'category') as $category) { $term = null; $scheme = null; $label = null; if (isset($category['data'])) { $term = $this->sanitize($category['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($category['attribs']['']['scheme'])) { $scheme = $this->sanitize($category['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $scheme = 'http://search.yahoo.com/mrss/category_schema'; } if (isset($category['attribs']['']['label'])) { $label = $this->sanitize($category['attribs']['']['label'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $categories_parent[] = $this->registry->create(Category::class, [$term, $scheme, $label]); } foreach ((array) $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'category') as $category) { $term = null; $scheme = 'http://www.itunes.com/dtds/podcast-1.0.dtd'; $label = null; if (isset($category['attribs']['']['text'])) { $label = $this->sanitize($category['attribs']['']['text'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $categories_parent[] = $this->registry->create(Category::class, [$term, $scheme, $label]); if (isset($category['child'][\SimplePie\SimplePie::NAMESPACE_ITUNES]['category'])) { foreach ((array) $category['child'][\SimplePie\SimplePie::NAMESPACE_ITUNES]['category'] as $subcategory) { if (isset($subcategory['attribs']['']['text'])) { $label = $this->sanitize($subcategory['attribs']['']['text'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $categories_parent[] = $this->registry->create(Category::class, [$term, $scheme, $label]); } } } if (is_array($categories_parent)) { $categories_parent = array_values(array_unique($categories_parent)); } // COPYRIGHT if ($copyright = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'copyright')) { $copyright_url = null; $copyright_label = null; if (isset($copyright[0]['attribs']['']['url'])) { $copyright_url = $this->sanitize($copyright[0]['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($copyright[0]['data'])) { $copyright_label = $this->sanitize($copyright[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $copyrights_parent = $this->registry->create(Copyright::class, [$copyright_url, $copyright_label]); } elseif ($copyright = $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'copyright')) { $copyright_url = null; $copyright_label = null; if (isset($copyright[0]['attribs']['']['url'])) { $copyright_url = $this->sanitize($copyright[0]['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($copyright[0]['data'])) { $copyright_label = $this->sanitize($copyright[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $copyrights_parent = $this->registry->create(Copyright::class, [$copyright_url, $copyright_label]); } // CREDITS if ($credits = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'credit')) { foreach ($credits as $credit) { $credit_role = null; $credit_scheme = null; $credit_name = null; if (isset($credit['attribs']['']['role'])) { $credit_role = $this->sanitize($credit['attribs']['']['role'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($credit['attribs']['']['scheme'])) { $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $credit_scheme = 'urn:ebu'; } if (isset($credit['data'])) { $credit_name = $this->sanitize($credit['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $credits_parent[] = $this->registry->create(Credit::class, [$credit_role, $credit_scheme, $credit_name]); } } elseif ($credits = $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'credit')) { foreach ($credits as $credit) { $credit_role = null; $credit_scheme = null; $credit_name = null; if (isset($credit['attribs']['']['role'])) { $credit_role = $this->sanitize($credit['attribs']['']['role'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($credit['attribs']['']['scheme'])) { $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $credit_scheme = 'urn:ebu'; } if (isset($credit['data'])) { $credit_name = $this->sanitize($credit['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $credits_parent[] = $this->registry->create(Credit::class, [$credit_role, $credit_scheme, $credit_name]); } } if (is_array($credits_parent)) { $credits_parent = array_values(array_unique($credits_parent)); } // DESCRIPTION if ($description_parent = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'description')) { if (isset($description_parent[0]['data'])) { $description_parent = $this->sanitize($description_parent[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } } elseif ($description_parent = $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'description')) { if (isset($description_parent[0]['data'])) { $description_parent = $this->sanitize($description_parent[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } } // DURATION $duration_tags = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'duration'); if ($duration_tags !== null) { $seconds = null; $minutes = null; $hours = null; if (isset($duration_tags[0]['data'])) { $temp = explode(':', $this->sanitize($duration_tags[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT)); $seconds = (int) array_pop($temp); if (count($temp) > 0) { $minutes = (int) array_pop($temp); $seconds += $minutes * 60; } if (count($temp) > 0) { $hours = (int) array_pop($temp); $seconds += $hours * 3600; } unset($temp); $duration_parent = $seconds; } } // HASHES if ($hashes_iterator = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'hash')) { foreach ($hashes_iterator as $hash) { $value = null; $algo = null; if (isset($hash['data'])) { $value = $this->sanitize($hash['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($hash['attribs']['']['algo'])) { $algo = $this->sanitize($hash['attribs']['']['algo'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $algo = 'md5'; } $hashes_parent[] = $algo.':'.$value; } } elseif ($hashes_iterator = $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'hash')) { foreach ($hashes_iterator as $hash) { $value = null; $algo = null; if (isset($hash['data'])) { $value = $this->sanitize($hash['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($hash['attribs']['']['algo'])) { $algo = $this->sanitize($hash['attribs']['']['algo'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $algo = 'md5'; } $hashes_parent[] = $algo.':'.$value; } } if (is_array($hashes_parent)) { $hashes_parent = array_values(array_unique($hashes_parent)); } // KEYWORDS if ($keywords = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'keywords')) { if (isset($keywords[0]['data'])) { $temp = explode(',', $this->sanitize($keywords[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT)); foreach ($temp as $word) { $keywords_parent[] = trim($word); } } unset($temp); } elseif ($keywords = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'keywords')) { if (isset($keywords[0]['data'])) { $temp = explode(',', $this->sanitize($keywords[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT)); foreach ($temp as $word) { $keywords_parent[] = trim($word); } } unset($temp); } elseif ($keywords = $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'keywords')) { if (isset($keywords[0]['data'])) { $temp = explode(',', $this->sanitize($keywords[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT)); foreach ($temp as $word) { $keywords_parent[] = trim($word); } } unset($temp); } elseif ($keywords = $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'keywords')) { if (isset($keywords[0]['data'])) { $temp = explode(',', $this->sanitize($keywords[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT)); foreach ($temp as $word) { $keywords_parent[] = trim($word); } } unset($temp); } if (is_array($keywords_parent)) { $keywords_parent = array_values(array_unique($keywords_parent)); } // PLAYER if ($player_parent = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'player')) { if (isset($player_parent[0]['attribs']['']['url'])) { $player_parent = $this->sanitize($player_parent[0]['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($player_parent[0])); } } elseif ($player_parent = $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'player')) { if (isset($player_parent[0]['attribs']['']['url'])) { $player_parent = $this->sanitize($player_parent[0]['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($player_parent[0])); } } // RATINGS if ($ratings = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'rating')) { foreach ($ratings as $rating) { $rating_scheme = null; $rating_value = null; if (isset($rating['attribs']['']['scheme'])) { $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $rating_scheme = 'urn:simple'; } if (isset($rating['data'])) { $rating_value = $this->sanitize($rating['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $ratings_parent[] = $this->registry->create(Rating::class, [$rating_scheme, $rating_value]); } } elseif ($ratings = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'explicit')) { foreach ($ratings as $rating) { $rating_scheme = 'urn:itunes'; $rating_value = null; if (isset($rating['data'])) { $rating_value = $this->sanitize($rating['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $ratings_parent[] = $this->registry->create(Rating::class, [$rating_scheme, $rating_value]); } } elseif ($ratings = $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'rating')) { foreach ($ratings as $rating) { $rating_scheme = null; $rating_value = null; if (isset($rating['attribs']['']['scheme'])) { $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $rating_scheme = 'urn:simple'; } if (isset($rating['data'])) { $rating_value = $this->sanitize($rating['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $ratings_parent[] = $this->registry->create(Rating::class, [$rating_scheme, $rating_value]); } } elseif ($ratings = $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'explicit')) { foreach ($ratings as $rating) { $rating_scheme = 'urn:itunes'; $rating_value = null; if (isset($rating['data'])) { $rating_value = $this->sanitize($rating['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $ratings_parent[] = $this->registry->create(Rating::class, [$rating_scheme, $rating_value]); } } if (is_array($ratings_parent)) { $ratings_parent = array_values(array_unique($ratings_parent)); } // RESTRICTIONS if ($restrictions = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'restriction')) { foreach ($restrictions as $restriction) { $restriction_relationship = null; $restriction_type = null; $restriction_value = null; if (isset($restriction['attribs']['']['relationship'])) { $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($restriction['attribs']['']['type'])) { $restriction_type = $this->sanitize($restriction['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($restriction['data'])) { $restriction_value = $this->sanitize($restriction['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $restrictions_parent[] = $this->registry->create(Restriction::class, [$restriction_relationship, $restriction_type, $restriction_value]); } } elseif ($restrictions = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'block')) { foreach ($restrictions as $restriction) { $restriction_relationship = Restriction::RELATIONSHIP_ALLOW; $restriction_type = null; $restriction_value = 'itunes'; if (isset($restriction['data']) && strtolower($restriction['data']) === 'yes') { $restriction_relationship = Restriction::RELATIONSHIP_DENY; } $restrictions_parent[] = $this->registry->create(Restriction::class, [$restriction_relationship, $restriction_type, $restriction_value]); } } elseif ($restrictions = $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'restriction')) { foreach ($restrictions as $restriction) { $restriction_relationship = null; $restriction_type = null; $restriction_value = null; if (isset($restriction['attribs']['']['relationship'])) { $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($restriction['attribs']['']['type'])) { $restriction_type = $this->sanitize($restriction['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($restriction['data'])) { $restriction_value = $this->sanitize($restriction['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $restrictions_parent[] = $this->registry->create(Restriction::class, [$restriction_relationship, $restriction_type, $restriction_value]); } } elseif ($restrictions = $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'block')) { foreach ($restrictions as $restriction) { $restriction_relationship = Restriction::RELATIONSHIP_ALLOW; $restriction_type = null; $restriction_value = 'itunes'; if (isset($restriction['data']) && strtolower($restriction['data']) === 'yes') { $restriction_relationship = Restriction::RELATIONSHIP_DENY; } $restrictions_parent[] = $this->registry->create(Restriction::class, [$restriction_relationship, $restriction_type, $restriction_value]); } } if (count($restrictions_parent) > 0) { $restrictions_parent = array_values(array_unique($restrictions_parent)); } else { $restrictions_parent = [new \SimplePie\Restriction(Restriction::RELATIONSHIP_ALLOW, null, 'default')]; } // THUMBNAILS if ($thumbnails = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'thumbnail')) { foreach ($thumbnails as $thumbnail) { if (isset($thumbnail['attribs']['']['url'])) { $thumbnails_parent[] = $this->sanitize($thumbnail['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($thumbnail)); } } } elseif ($thumbnails = $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'thumbnail')) { foreach ($thumbnails as $thumbnail) { if (isset($thumbnail['attribs']['']['url'])) { $thumbnails_parent[] = $this->sanitize($thumbnail['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($thumbnail)); } } } // TITLES if ($title_parent = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'title')) { if (isset($title_parent[0]['data'])) { $title_parent = $this->sanitize($title_parent[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } } elseif ($title_parent = $parent->get_channel_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'title')) { if (isset($title_parent[0]['data'])) { $title_parent = $this->sanitize($title_parent[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } } // Clear the memory unset($parent); // Attributes $bitrate = null; $channels = null; $duration = null; $expression = null; $framerate = null; $height = null; $javascript = null; $lang = null; $length = null; $medium = null; $samplingrate = null; $type = null; $url = null; $width = null; // Elements $captions = null; $categories = null; $copyrights = null; $credits = null; $description = null; $hashes = null; $keywords = null; $player = null; $ratings = null; $restrictions = null; $thumbnails = null; $title = null; // If we have media:group tags, loop through them. foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'group') as $group) { if (isset($group['child']) && isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['content'])) { // If we have media:content tags, loop through them. foreach ((array) $group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['content'] as $content) { if (isset($content['attribs']['']['url'])) { // Attributes $bitrate = null; $channels = null; $duration = null; $expression = null; $framerate = null; $height = null; $javascript = null; $lang = null; $length = null; $medium = null; $samplingrate = null; $type = null; $url = null; $width = null; // Elements $captions = null; $categories = null; $copyrights = null; $credits = null; $description = null; $hashes = null; $keywords = null; $player = null; $ratings = null; $restrictions = null; $thumbnails = null; $title = null; // Start checking the attributes of media:content if (isset($content['attribs']['']['bitrate'])) { $bitrate = $this->sanitize($content['attribs']['']['bitrate'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['channels'])) { $channels = $this->sanitize($content['attribs']['']['channels'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['duration'])) { $duration = $this->sanitize($content['attribs']['']['duration'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $duration = $duration_parent; } if (isset($content['attribs']['']['expression'])) { $expression = $this->sanitize($content['attribs']['']['expression'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['framerate'])) { $framerate = $this->sanitize($content['attribs']['']['framerate'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['height'])) { $height = $this->sanitize($content['attribs']['']['height'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['lang'])) { $lang = $this->sanitize($content['attribs']['']['lang'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['fileSize'])) { $length = intval($content['attribs']['']['fileSize']); } if (isset($content['attribs']['']['medium'])) { $medium = $this->sanitize($content['attribs']['']['medium'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['samplingrate'])) { $samplingrate = $this->sanitize($content['attribs']['']['samplingrate'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['type'])) { $type = $this->sanitize($content['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['width'])) { $width = $this->sanitize($content['attribs']['']['width'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $url = $this->sanitize($content['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($content)); // Checking the other optional media: elements. Priority: media:content, media:group, item, channel // CAPTIONS if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['text'])) { foreach ($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['text'] as $caption) { $caption_type = null; $caption_lang = null; $caption_startTime = null; $caption_endTime = null; $caption_text = null; if (isset($caption['attribs']['']['type'])) { $caption_type = $this->sanitize($caption['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['lang'])) { $caption_lang = $this->sanitize($caption['attribs']['']['lang'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['start'])) { $caption_startTime = $this->sanitize($caption['attribs']['']['start'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['end'])) { $caption_endTime = $this->sanitize($caption['attribs']['']['end'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['data'])) { $caption_text = $this->sanitize($caption['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $captions[] = $this->registry->create(Caption::class, [$caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text]); } if (is_array($captions)) { $captions = array_values(array_unique($captions)); } } elseif (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['text'])) { foreach ($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['text'] as $caption) { $caption_type = null; $caption_lang = null; $caption_startTime = null; $caption_endTime = null; $caption_text = null; if (isset($caption['attribs']['']['type'])) { $caption_type = $this->sanitize($caption['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['lang'])) { $caption_lang = $this->sanitize($caption['attribs']['']['lang'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['start'])) { $caption_startTime = $this->sanitize($caption['attribs']['']['start'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['end'])) { $caption_endTime = $this->sanitize($caption['attribs']['']['end'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['data'])) { $caption_text = $this->sanitize($caption['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $captions[] = $this->registry->create(Caption::class, [$caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text]); } if (is_array($captions)) { $captions = array_values(array_unique($captions)); } } else { $captions = $captions_parent; } // CATEGORIES if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['category'])) { foreach ((array) $content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['category'] as $category) { $term = null; $scheme = null; $label = null; if (isset($category['data'])) { $term = $this->sanitize($category['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($category['attribs']['']['scheme'])) { $scheme = $this->sanitize($category['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $scheme = 'http://search.yahoo.com/mrss/category_schema'; } if (isset($category['attribs']['']['label'])) { $label = $this->sanitize($category['attribs']['']['label'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $categories[] = $this->registry->create(Category::class, [$term, $scheme, $label]); } } if (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['category'])) { foreach ((array) $group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['category'] as $category) { $term = null; $scheme = null; $label = null; if (isset($category['data'])) { $term = $this->sanitize($category['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($category['attribs']['']['scheme'])) { $scheme = $this->sanitize($category['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $scheme = 'http://search.yahoo.com/mrss/category_schema'; } if (isset($category['attribs']['']['label'])) { $label = $this->sanitize($category['attribs']['']['label'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $categories[] = $this->registry->create(Category::class, [$term, $scheme, $label]); } } if (is_array($categories) && is_array($categories_parent)) { $categories = array_values(array_unique(array_merge($categories, $categories_parent))); } elseif (is_array($categories)) { $categories = array_values(array_unique($categories)); } elseif (is_array($categories_parent)) { $categories = array_values(array_unique($categories_parent)); } // COPYRIGHTS if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'])) { $copyright_url = null; $copyright_label = null; if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'])) { $copyright_url = $this->sanitize($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'][0]['data'])) { $copyright_label = $this->sanitize($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $copyrights = $this->registry->create(Copyright::class, [$copyright_url, $copyright_label]); } elseif (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'])) { $copyright_url = null; $copyright_label = null; if (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'])) { $copyright_url = $this->sanitize($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'][0]['data'])) { $copyright_label = $this->sanitize($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $copyrights = $this->registry->create(Copyright::class, [$copyright_url, $copyright_label]); } else { $copyrights = $copyrights_parent; } // CREDITS if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['credit'])) { foreach ($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['credit'] as $credit) { $credit_role = null; $credit_scheme = null; $credit_name = null; if (isset($credit['attribs']['']['role'])) { $credit_role = $this->sanitize($credit['attribs']['']['role'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($credit['attribs']['']['scheme'])) { $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $credit_scheme = 'urn:ebu'; } if (isset($credit['data'])) { $credit_name = $this->sanitize($credit['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $credits[] = $this->registry->create(Credit::class, [$credit_role, $credit_scheme, $credit_name]); } if (is_array($credits)) { $credits = array_values(array_unique($credits)); } } elseif (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['credit'])) { foreach ($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['credit'] as $credit) { $credit_role = null; $credit_scheme = null; $credit_name = null; if (isset($credit['attribs']['']['role'])) { $credit_role = $this->sanitize($credit['attribs']['']['role'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($credit['attribs']['']['scheme'])) { $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $credit_scheme = 'urn:ebu'; } if (isset($credit['data'])) { $credit_name = $this->sanitize($credit['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $credits[] = $this->registry->create(Credit::class, [$credit_role, $credit_scheme, $credit_name]); } if (is_array($credits)) { $credits = array_values(array_unique($credits)); } } else { $credits = $credits_parent; } // DESCRIPTION if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['description'])) { $description = $this->sanitize($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['description'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['description'])) { $description = $this->sanitize($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['description'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $description = $description_parent; } // HASHES if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['hash'])) { foreach ($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['hash'] as $hash) { $value = null; $algo = null; if (isset($hash['data'])) { $value = $this->sanitize($hash['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($hash['attribs']['']['algo'])) { $algo = $this->sanitize($hash['attribs']['']['algo'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $algo = 'md5'; } $hashes[] = $algo.':'.$value; } if (is_array($hashes)) { $hashes = array_values(array_unique($hashes)); } } elseif (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['hash'])) { foreach ($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['hash'] as $hash) { $value = null; $algo = null; if (isset($hash['data'])) { $value = $this->sanitize($hash['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($hash['attribs']['']['algo'])) { $algo = $this->sanitize($hash['attribs']['']['algo'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $algo = 'md5'; } $hashes[] = $algo.':'.$value; } if (is_array($hashes)) { $hashes = array_values(array_unique($hashes)); } } else { $hashes = $hashes_parent; } // KEYWORDS if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['keywords'])) { if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['keywords'][0]['data'])) { $temp = explode(',', $this->sanitize($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['keywords'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT)); foreach ($temp as $word) { $keywords[] = trim($word); } unset($temp); } if (is_array($keywords)) { $keywords = array_values(array_unique($keywords)); } } elseif (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['keywords'])) { if (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['keywords'][0]['data'])) { $temp = explode(',', $this->sanitize($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['keywords'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT)); foreach ($temp as $word) { $keywords[] = trim($word); } unset($temp); } if (is_array($keywords)) { $keywords = array_values(array_unique($keywords)); } } else { $keywords = $keywords_parent; } // PLAYER if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['player'])) { $playerElem = $content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['player'][0]; $player = $this->sanitize($playerElem['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($playerElem)); } elseif (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['player'])) { $playerElem = $group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['player'][0]; $player = $this->sanitize($playerElem['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($playerElem)); } else { $player = $player_parent; } // RATINGS if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['rating'])) { foreach ($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['rating'] as $rating) { $rating_scheme = null; $rating_value = null; if (isset($rating['attribs']['']['scheme'])) { $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $rating_scheme = 'urn:simple'; } if (isset($rating['data'])) { $rating_value = $this->sanitize($rating['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $ratings[] = $this->registry->create(Rating::class, [$rating_scheme, $rating_value]); } if (is_array($ratings)) { $ratings = array_values(array_unique($ratings)); } } elseif (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['rating'])) { foreach ($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['rating'] as $rating) { $rating_scheme = null; $rating_value = null; if (isset($rating['attribs']['']['scheme'])) { $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $rating_scheme = 'urn:simple'; } if (isset($rating['data'])) { $rating_value = $this->sanitize($rating['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $ratings[] = $this->registry->create(Rating::class, [$rating_scheme, $rating_value]); } if (is_array($ratings)) { $ratings = array_values(array_unique($ratings)); } } else { $ratings = $ratings_parent; } // RESTRICTIONS if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['restriction'])) { foreach ($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['restriction'] as $restriction) { $restriction_relationship = null; $restriction_type = null; $restriction_value = null; if (isset($restriction['attribs']['']['relationship'])) { $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($restriction['attribs']['']['type'])) { $restriction_type = $this->sanitize($restriction['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($restriction['data'])) { $restriction_value = $this->sanitize($restriction['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $restrictions[] = $this->registry->create(Restriction::class, [$restriction_relationship, $restriction_type, $restriction_value]); } if (is_array($restrictions)) { $restrictions = array_values(array_unique($restrictions)); } } elseif (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['restriction'])) { foreach ($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['restriction'] as $restriction) { $restriction_relationship = null; $restriction_type = null; $restriction_value = null; if (isset($restriction['attribs']['']['relationship'])) { $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($restriction['attribs']['']['type'])) { $restriction_type = $this->sanitize($restriction['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($restriction['data'])) { $restriction_value = $this->sanitize($restriction['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $restrictions[] = $this->registry->create(Restriction::class, [$restriction_relationship, $restriction_type, $restriction_value]); } if (is_array($restrictions)) { $restrictions = array_values(array_unique($restrictions)); } } else { $restrictions = $restrictions_parent; } // THUMBNAILS if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['thumbnail'])) { foreach ($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['thumbnail'] as $thumbnail) { $thumbnails[] = $this->sanitize($thumbnail['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($thumbnail)); } if (is_array($thumbnails)) { $thumbnails = array_values(array_unique($thumbnails)); } } elseif (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['thumbnail'])) { foreach ($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['thumbnail'] as $thumbnail) { $thumbnails[] = $this->sanitize($thumbnail['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($thumbnail)); } if (is_array($thumbnails)) { $thumbnails = array_values(array_unique($thumbnails)); } } else { $thumbnails = $thumbnails_parent; } // TITLES if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['title'])) { $title = $this->sanitize($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['title'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif (isset($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['title'])) { $title = $this->sanitize($group['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['title'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $title = $title_parent; } $this->data['enclosures'][] = $this->registry->create(Enclosure::class, [$url, $type, $length, null, $bitrate, $captions, $categories, $channels, $copyrights, $credits, $description, $duration, $expression, $framerate, $hashes, $height, $keywords, $lang, $medium, $player, $ratings, $restrictions, $samplingrate, $thumbnails, $title, $width]); } } } } // If we have standalone media:content tags, loop through them. if (isset($this->data['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['content'])) { foreach ((array) $this->data['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['content'] as $content) { if (isset($content['attribs']['']['url']) || isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['player'])) { // Attributes $bitrate = null; $channels = null; $duration = null; $expression = null; $framerate = null; $height = null; $javascript = null; $lang = null; $length = null; $medium = null; $samplingrate = null; $type = null; $url = null; $width = null; // Elements $captions = null; $categories = null; $copyrights = null; $credits = null; $description = null; $hashes = null; $keywords = null; $player = null; $ratings = null; $restrictions = null; $thumbnails = null; $title = null; // Start checking the attributes of media:content if (isset($content['attribs']['']['bitrate'])) { $bitrate = $this->sanitize($content['attribs']['']['bitrate'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['channels'])) { $channels = $this->sanitize($content['attribs']['']['channels'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['duration'])) { $duration = $this->sanitize($content['attribs']['']['duration'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $duration = $duration_parent; } if (isset($content['attribs']['']['expression'])) { $expression = $this->sanitize($content['attribs']['']['expression'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['framerate'])) { $framerate = $this->sanitize($content['attribs']['']['framerate'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['height'])) { $height = $this->sanitize($content['attribs']['']['height'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['lang'])) { $lang = $this->sanitize($content['attribs']['']['lang'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['fileSize'])) { $length = intval($content['attribs']['']['fileSize']); } if (isset($content['attribs']['']['medium'])) { $medium = $this->sanitize($content['attribs']['']['medium'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['samplingrate'])) { $samplingrate = $this->sanitize($content['attribs']['']['samplingrate'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['type'])) { $type = $this->sanitize($content['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['width'])) { $width = $this->sanitize($content['attribs']['']['width'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['attribs']['']['url'])) { $url = $this->sanitize($content['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($content)); } // Checking the other optional media: elements. Priority: media:content, media:group, item, channel // CAPTIONS if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['text'])) { foreach ($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['text'] as $caption) { $caption_type = null; $caption_lang = null; $caption_startTime = null; $caption_endTime = null; $caption_text = null; if (isset($caption['attribs']['']['type'])) { $caption_type = $this->sanitize($caption['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['lang'])) { $caption_lang = $this->sanitize($caption['attribs']['']['lang'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['start'])) { $caption_startTime = $this->sanitize($caption['attribs']['']['start'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['attribs']['']['end'])) { $caption_endTime = $this->sanitize($caption['attribs']['']['end'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($caption['data'])) { $caption_text = $this->sanitize($caption['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $captions[] = $this->registry->create(Caption::class, [$caption_type, $caption_lang, $caption_startTime, $caption_endTime, $caption_text]); } if (is_array($captions)) { $captions = array_values(array_unique($captions)); } } else { $captions = $captions_parent; } // CATEGORIES if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['category'])) { foreach ((array) $content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['category'] as $category) { $term = null; $scheme = null; $label = null; if (isset($category['data'])) { $term = $this->sanitize($category['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($category['attribs']['']['scheme'])) { $scheme = $this->sanitize($category['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $scheme = 'http://search.yahoo.com/mrss/category_schema'; } if (isset($category['attribs']['']['label'])) { $label = $this->sanitize($category['attribs']['']['label'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $categories[] = $this->registry->create(Category::class, [$term, $scheme, $label]); } } if (is_array($categories) && is_array($categories_parent)) { $categories = array_values(array_unique(array_merge($categories, $categories_parent))); } elseif (is_array($categories)) { $categories = array_values(array_unique($categories)); } elseif (is_array($categories_parent)) { $categories = array_values(array_unique($categories_parent)); } else { $categories = null; } // COPYRIGHTS if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'])) { $copyright_url = null; $copyright_label = null; if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'])) { $copyright_url = $this->sanitize($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'][0]['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'][0]['data'])) { $copyright_label = $this->sanitize($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['copyright'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $copyrights = $this->registry->create(Copyright::class, [$copyright_url, $copyright_label]); } else { $copyrights = $copyrights_parent; } // CREDITS if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['credit'])) { foreach ($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['credit'] as $credit) { $credit_role = null; $credit_scheme = null; $credit_name = null; if (isset($credit['attribs']['']['role'])) { $credit_role = $this->sanitize($credit['attribs']['']['role'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($credit['attribs']['']['scheme'])) { $credit_scheme = $this->sanitize($credit['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $credit_scheme = 'urn:ebu'; } if (isset($credit['data'])) { $credit_name = $this->sanitize($credit['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $credits[] = $this->registry->create(Credit::class, [$credit_role, $credit_scheme, $credit_name]); } if (is_array($credits)) { $credits = array_values(array_unique($credits)); } } else { $credits = $credits_parent; } // DESCRIPTION if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['description'])) { $description = $this->sanitize($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['description'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $description = $description_parent; } // HASHES if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['hash'])) { foreach ($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['hash'] as $hash) { $value = null; $algo = null; if (isset($hash['data'])) { $value = $this->sanitize($hash['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($hash['attribs']['']['algo'])) { $algo = $this->sanitize($hash['attribs']['']['algo'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $algo = 'md5'; } $hashes[] = $algo.':'.$value; } if (is_array($hashes)) { $hashes = array_values(array_unique($hashes)); } } else { $hashes = $hashes_parent; } // KEYWORDS if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['keywords'])) { if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['keywords'][0]['data'])) { $temp = explode(',', $this->sanitize($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['keywords'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT)); foreach ($temp as $word) { $keywords[] = trim($word); } unset($temp); } if (is_array($keywords)) { $keywords = array_values(array_unique($keywords)); } } else { $keywords = $keywords_parent; } // PLAYER if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['player'])) { if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['player'][0]['attribs']['']['url'])) { $playerElem = $content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['player'][0]; $player = $this->sanitize($playerElem['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($playerElem)); } } else { $player = $player_parent; } // RATINGS if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['rating'])) { foreach ($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['rating'] as $rating) { $rating_scheme = null; $rating_value = null; if (isset($rating['attribs']['']['scheme'])) { $rating_scheme = $this->sanitize($rating['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $rating_scheme = 'urn:simple'; } if (isset($rating['data'])) { $rating_value = $this->sanitize($rating['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $ratings[] = $this->registry->create(Rating::class, [$rating_scheme, $rating_value]); } if (is_array($ratings)) { $ratings = array_values(array_unique($ratings)); } } else { $ratings = $ratings_parent; } // RESTRICTIONS if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['restriction'])) { foreach ($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['restriction'] as $restriction) { $restriction_relationship = null; $restriction_type = null; $restriction_value = null; if (isset($restriction['attribs']['']['relationship'])) { $restriction_relationship = $this->sanitize($restriction['attribs']['']['relationship'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($restriction['attribs']['']['type'])) { $restriction_type = $this->sanitize($restriction['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($restriction['data'])) { $restriction_value = $this->sanitize($restriction['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $restrictions[] = $this->registry->create(Restriction::class, [$restriction_relationship, $restriction_type, $restriction_value]); } if (is_array($restrictions)) { $restrictions = array_values(array_unique($restrictions)); } } else { $restrictions = $restrictions_parent; } // THUMBNAILS if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['thumbnail'])) { foreach ($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['thumbnail'] as $thumbnail) { if (isset($thumbnail['attribs']['']['url'])) { $thumbnails[] = $this->sanitize($thumbnail['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($thumbnail)); } } if (is_array($thumbnails)) { $thumbnails = array_values(array_unique($thumbnails)); } } else { $thumbnails = $thumbnails_parent; } // TITLES if (isset($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['title'])) { $title = $this->sanitize($content['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['title'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $title = $title_parent; } $this->data['enclosures'][] = $this->registry->create(Enclosure::class, [$url, $type, $length, null, $bitrate, $captions, $categories, $channels, $copyrights, $credits, $description, $duration, $expression, $framerate, $hashes, $height, $keywords, $lang, $medium, $player, $ratings, $restrictions, $samplingrate, $thumbnails, $title, $width]); } } } foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'link') as $link) { if (isset($link['attribs']['']['href']) && !empty($link['attribs']['']['rel']) && $link['attribs']['']['rel'] === 'enclosure') { // Attributes $bitrate = null; $channels = null; $duration = null; $expression = null; $framerate = null; $height = null; $javascript = null; $lang = null; $length = null; $medium = null; $samplingrate = null; $type = null; $url = null; $width = null; $url = $this->sanitize($link['attribs']['']['href'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($link)); if (isset($link['attribs']['']['type'])) { $type = $this->sanitize($link['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($link['attribs']['']['length'])) { $length = intval($link['attribs']['']['length']); } if (isset($link['attribs']['']['title'])) { $title = $this->sanitize($link['attribs']['']['title'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $title = $title_parent; } // Since we don't have group or content for these, we'll just pass the '*_parent' variables directly to the constructor $this->data['enclosures'][] = $this->registry->create(Enclosure::class, [$url, $type, $length, null, $bitrate, $captions_parent, $categories_parent, $channels, $copyrights_parent, $credits_parent, $description_parent, $duration_parent, $expression, $framerate, $hashes_parent, $height, $keywords_parent, $lang, $medium, $player_parent, $ratings_parent, $restrictions_parent, $samplingrate, $thumbnails_parent, $title, $width]); } } foreach ((array) $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'link') as $link) { if (isset($link['attribs']['']['href']) && !empty($link['attribs']['']['rel']) && $link['attribs']['']['rel'] === 'enclosure') { // Attributes $bitrate = null; $channels = null; $duration = null; $expression = null; $framerate = null; $height = null; $javascript = null; $lang = null; $length = null; $medium = null; $samplingrate = null; $type = null; $url = null; $width = null; $url = $this->sanitize($link['attribs']['']['href'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($link)); if (isset($link['attribs']['']['type'])) { $type = $this->sanitize($link['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($link['attribs']['']['length'])) { $length = intval($link['attribs']['']['length']); } // Since we don't have group or content for these, we'll just pass the '*_parent' variables directly to the constructor $this->data['enclosures'][] = $this->registry->create(Enclosure::class, [$url, $type, $length, null, $bitrate, $captions_parent, $categories_parent, $channels, $copyrights_parent, $credits_parent, $description_parent, $duration_parent, $expression, $framerate, $hashes_parent, $height, $keywords_parent, $lang, $medium, $player_parent, $ratings_parent, $restrictions_parent, $samplingrate, $thumbnails_parent, $title_parent, $width]); } } foreach ($this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'enclosure') ?? [] as $enclosure) { if (isset($enclosure['attribs']['']['url'])) { // Attributes $bitrate = null; $channels = null; $duration = null; $expression = null; $framerate = null; $height = null; $javascript = null; $lang = null; $length = null; $medium = null; $samplingrate = null; $type = null; $url = null; $width = null; $url = $this->sanitize($enclosure['attribs']['']['url'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_own_base($enclosure)); $url = $this->get_sanitize()->https_url($url); if (isset($enclosure['attribs']['']['type'])) { $type = $this->sanitize($enclosure['attribs']['']['type'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($enclosure['attribs']['']['length'])) { $length = intval($enclosure['attribs']['']['length']); } // Since we don't have group or content for these, we'll just pass the '*_parent' variables directly to the constructor $this->data['enclosures'][] = $this->registry->create(Enclosure::class, [$url, $type, $length, null, $bitrate, $captions_parent, $categories_parent, $channels, $copyrights_parent, $credits_parent, $description_parent, $duration_parent, $expression, $framerate, $hashes_parent, $height, $keywords_parent, $lang, $medium, $player_parent, $ratings_parent, $restrictions_parent, $samplingrate, $thumbnails_parent, $title_parent, $width]); } } if (count($this->data['enclosures']) === 0 && ($url || $type || $length || $bitrate || $captions_parent || $categories_parent || $channels || $copyrights_parent || $credits_parent || $description_parent || $duration_parent || $expression || $framerate || $hashes_parent || $height || $keywords_parent || $lang || $medium || $player_parent || $ratings_parent || $samplingrate || $thumbnails_parent || $title_parent || $width)) { // Since we don't have group or content for these, we'll just pass the '*_parent' variables directly to the constructor $this->data['enclosures'][] = $this->registry->create(Enclosure::class, [$url, $type, $length, null, $bitrate, $captions_parent, $categories_parent, $channels, $copyrights_parent, $credits_parent, $description_parent, $duration_parent, $expression, $framerate, $hashes_parent, $height, $keywords_parent, $lang, $medium, $player_parent, $ratings_parent, $restrictions_parent, $samplingrate, $thumbnails_parent, $title_parent, $width]); } $this->data['enclosures'] = array_values(array_unique($this->data['enclosures'])); } if (!empty($this->data['enclosures'])) { return $this->data['enclosures']; } return null; } /** * Get the latitude coordinates for the item * * Compatible with the W3C WGS84 Basic Geo and GeoRSS specifications * * Uses `<geo:lat>` or `<georss:point>` * * @since 1.0 * @link http://www.w3.org/2003/01/geo/ W3C WGS84 Basic Geo * @link http://www.georss.org/ GeoRSS * @return float|null */ public function get_latitude() { if ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_W3C_BASIC_GEO, 'lat')) { return (float) $return[0]['data']; } elseif (($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match)) { return (float) $match[1]; } return null; } /** * Get the longitude coordinates for the item * * Compatible with the W3C WGS84 Basic Geo and GeoRSS specifications * * Uses `<geo:long>`, `<geo:lon>` or `<georss:point>` * * @since 1.0 * @link http://www.w3.org/2003/01/geo/ W3C WGS84 Basic Geo * @link http://www.georss.org/ GeoRSS * @return float|null */ public function get_longitude() { if ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_W3C_BASIC_GEO, 'long')) { return (float) $return[0]['data']; } elseif ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_W3C_BASIC_GEO, 'lon')) { return (float) $return[0]['data']; } elseif (($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match)) { return (float) $match[2]; } return null; } /** * Get the `<atom:source>` for the item * * @since 1.1 * @return \SimplePie\Source|null */ public function get_source() { if ($return = $this->get_item_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'source')) { return $this->registry->create(Source::class, [$this, $return[0]]); } return null; } public function set_sanitize(Sanitize $sanitize): void { $this->sanitize = $sanitize; } protected function get_sanitize(): Sanitize { if ($this->sanitize === null) { $this->sanitize = new Sanitize(); } return $this->sanitize; } } class_alias('SimplePie\Item', 'SimplePie_Item'); Rating.php 0000644 00000003402 15162130106 0006473 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; /** * Handles `<media:rating>` or `<itunes:explicit>` tags as defined in Media RSS and iTunes RSS respectively * * Used by {@see \SimplePie\Enclosure::get_rating()} and {@see \SimplePie\Enclosure::get_ratings()} * * This class can be overloaded with {@see \SimplePie\SimplePie::set_rating_class()} */ class Rating { /** * Rating scheme * * @var ?string * @see get_scheme() */ public $scheme; /** * Rating value * * @var ?string * @see get_value() */ public $value; /** * Constructor, used to input the data * * For documentation on all the parameters, see the corresponding * properties and their accessors */ public function __construct( ?string $scheme = null, ?string $value = null ) { $this->scheme = $scheme; $this->value = $value; } /** * String-ified version * * @return string */ public function __toString() { // There is no $this->data here return md5(serialize($this)); } /** * Get the organizational scheme for the rating * * @return string|null */ public function get_scheme() { if ($this->scheme !== null) { return $this->scheme; } return null; } /** * Get the value of the rating * * @return string|null */ public function get_value() { if ($this->value !== null) { return $this->value; } return null; } } class_alias('SimplePie\Rating', 'SimplePie_Rating'); Content/Type/Sniffer.php 0000644 00000016736 15162130112 0011211 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Content\Type; use InvalidArgumentException; use SimplePie\File; use SimplePie\HTTP\Response; /** * Content-type sniffing * * Based on the rules in http://tools.ietf.org/html/draft-abarth-mime-sniff-06 * * This is used since we can't always trust Content-Type headers, and is based * upon the HTML5 parsing rules. * * * This class can be overloaded with {@see \SimplePie\SimplePie::set_content_type_sniffer_class()} */ class Sniffer { /** * File object * * @var File|Response */ public $file; /** * Create an instance of the class with the input file * * @param File|Response $file Input file */ public function __construct(/* File */ $file) { if (!is_object($file) || !$file instanceof Response) { // For BC we're asking for `File`, but internally we accept every `Response` implementation throw new InvalidArgumentException(sprintf( '%s(): Argument #1 ($file) must be of type %s', __METHOD__, File::class ), 1); } $this->file = $file; } /** * Get the Content-Type of the specified file * * @return string Actual Content-Type */ public function get_type() { $content_type = $this->file->has_header('content-type') ? $this->file->get_header_line('content-type') : null; $content_encoding = $this->file->has_header('content-encoding') ? $this->file->get_header_line('content-encoding') : null; if ($content_type !== null) { if ($content_encoding === null && ($content_type === 'text/plain' || $content_type === 'text/plain; charset=ISO-8859-1' || $content_type === 'text/plain; charset=iso-8859-1' || $content_type === 'text/plain; charset=UTF-8')) { return $this->text_or_binary(); } if (($pos = strpos($content_type, ';')) !== false) { $official = substr($content_type, 0, $pos); } else { $official = $content_type; } $official = trim(strtolower($official)); if ($official === 'unknown/unknown' || $official === 'application/unknown') { return $this->unknown(); } elseif (substr($official, -4) === '+xml' || $official === 'text/xml' || $official === 'application/xml') { return $official; } elseif (substr($official, 0, 6) === 'image/') { if ($return = $this->image()) { return $return; } return $official; } elseif ($official === 'text/html') { return $this->feed_or_html(); } return $official; } return $this->unknown(); } /** * Sniff text or binary * * @return string Actual Content-Type */ public function text_or_binary() { $body = $this->file->get_body_content(); if (substr($body, 0, 2) === "\xFE\xFF" || substr($body, 0, 2) === "\xFF\xFE" || substr($body, 0, 4) === "\x00\x00\xFE\xFF" || substr($body, 0, 3) === "\xEF\xBB\xBF") { return 'text/plain'; } elseif (preg_match('/[\x00-\x08\x0E-\x1A\x1C-\x1F]/', $body)) { return 'application/octet-stream'; } return 'text/plain'; } /** * Sniff unknown * * @return string Actual Content-Type */ public function unknown() { $body = $this->file->get_body_content(); $ws = strspn($body, "\x09\x0A\x0B\x0C\x0D\x20"); if (strtolower(substr($body, $ws, 14)) === '<!doctype html' || strtolower(substr($body, $ws, 5)) === '<html' || strtolower(substr($body, $ws, 7)) === '<script') { return 'text/html'; } elseif (substr($body, 0, 5) === '%PDF-') { return 'application/pdf'; } elseif (substr($body, 0, 11) === '%!PS-Adobe-') { return 'application/postscript'; } elseif (substr($body, 0, 6) === 'GIF87a' || substr($body, 0, 6) === 'GIF89a') { return 'image/gif'; } elseif (substr($body, 0, 8) === "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") { return 'image/png'; } elseif (substr($body, 0, 3) === "\xFF\xD8\xFF") { return 'image/jpeg'; } elseif (substr($body, 0, 2) === "\x42\x4D") { return 'image/bmp'; } elseif (substr($body, 0, 4) === "\x00\x00\x01\x00") { return 'image/vnd.microsoft.icon'; } return $this->text_or_binary(); } /** * Sniff images * * @return string|false Actual Content-Type */ public function image() { $body = $this->file->get_body_content(); if (substr($body, 0, 6) === 'GIF87a' || substr($body, 0, 6) === 'GIF89a') { return 'image/gif'; } elseif (substr($body, 0, 8) === "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") { return 'image/png'; } elseif (substr($body, 0, 3) === "\xFF\xD8\xFF") { return 'image/jpeg'; } elseif (substr($body, 0, 2) === "\x42\x4D") { return 'image/bmp'; } elseif (substr($body, 0, 4) === "\x00\x00\x01\x00") { return 'image/vnd.microsoft.icon'; } return false; } /** * Sniff HTML * * @return string Actual Content-Type */ public function feed_or_html() { $body = $this->file->get_body_content(); $len = strlen($body); $pos = strspn($body, "\x09\x0A\x0D\x20\xEF\xBB\xBF"); while ($pos < $len) { switch ($body[$pos]) { case "\x09": case "\x0A": case "\x0D": case "\x20": $pos += strspn($body, "\x09\x0A\x0D\x20", $pos); continue 2; case '<': $pos++; break; default: return 'text/html'; } if (substr($body, $pos, 3) === '!--') { $pos += 3; if ($pos < $len && ($pos = strpos($body, '-->', $pos)) !== false) { $pos += 3; } else { return 'text/html'; } } elseif (substr($body, $pos, 1) === '!') { if ($pos < $len && ($pos = strpos($body, '>', $pos)) !== false) { $pos++; } else { return 'text/html'; } } elseif (substr($body, $pos, 1) === '?') { if ($pos < $len && ($pos = strpos($body, '?>', $pos)) !== false) { $pos += 2; } else { return 'text/html'; } } elseif (substr($body, $pos, 3) === 'rss' || substr($body, $pos, 7) === 'rdf:RDF') { return 'application/rss+xml'; } elseif (substr($body, $pos, 4) === 'feed') { return 'application/atom+xml'; } else { return 'text/html'; } } return 'text/html'; } } class_alias('SimplePie\Content\Type\Sniffer', 'SimplePie_Content_Type_Sniffer'); IRI.php 0000644 00000103767 15162130113 0005707 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-FileCopyrightText: 2008 Steve Minutillo // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; /** * IRI parser/serialiser/normaliser * * @property ?string $scheme * @property ?string $userinfo * @property ?string $host * @property ?int $port * @property-write int|string|null $port * @property ?string $authority * @property string $path * @property ?string $query * @property ?string $fragment */ class IRI { /** * Scheme * * @var ?string */ protected $scheme = null; /** * User Information * * @var ?string */ protected $iuserinfo = null; /** * ihost * * @var ?string */ protected $ihost = null; /** * Port * * @var ?int */ protected $port = null; /** * ipath * * @var string */ protected $ipath = ''; /** * iquery * * @var ?string */ protected $iquery = null; /** * ifragment * * @var ?string */ protected $ifragment = null; /** * Normalization database * * Each key is the scheme, each value is an array with each key as the IRI * part and value as the default value for that part. * * @var array<string, array<string, mixed>> */ protected $normalization = [ 'acap' => [ 'port' => 674 ], 'dict' => [ 'port' => 2628 ], 'file' => [ 'ihost' => 'localhost' ], 'http' => [ 'port' => 80, 'ipath' => '/' ], 'https' => [ 'port' => 443, 'ipath' => '/' ], ]; /** * Return the entire IRI when you try and read the object as a string * * @return string */ public function __toString() { return (string) $this->get_iri(); } /** * Overload __set() to provide access via properties * * @param string $name Property name * @param mixed $value Property value * @return void */ public function __set(string $name, $value) { $callable = [$this, 'set_' . $name]; if (is_callable($callable)) { call_user_func($callable, $value); } elseif ( $name === 'iauthority' || $name === 'iuserinfo' || $name === 'ihost' || $name === 'ipath' || $name === 'iquery' || $name === 'ifragment' ) { call_user_func([$this, 'set_' . substr($name, 1)], $value); } } /** * Overload __get() to provide access via properties * * @param string $name Property name * @return mixed */ public function __get(string $name) { // isset() returns false for null, we don't want to do that // Also why we use array_key_exists below instead of isset() $props = get_object_vars($this); if ( $name === 'iri' || $name === 'uri' || $name === 'iauthority' || $name === 'authority' ) { $return = $this->{"get_$name"}(); } elseif (array_key_exists($name, $props)) { $return = $this->$name; } // host -> ihost elseif (array_key_exists($prop = 'i' . $name, $props)) { $name = $prop; $return = $this->$prop; } // ischeme -> scheme elseif (($prop = substr($name, 1)) && array_key_exists($prop, $props)) { $name = $prop; $return = $this->$prop; } else { trigger_error('Undefined property: ' . get_class($this) . '::' . $name, E_USER_NOTICE); $return = null; } if ($return === null && isset($this->scheme, $this->normalization[$this->scheme][$name])) { return $this->normalization[$this->scheme][$name]; } return $return; } /** * Overload __isset() to provide access via properties * * @param string $name Property name * @return bool */ public function __isset(string $name) { return method_exists($this, 'get_' . $name) || isset($this->$name); } /** * Overload __unset() to provide access via properties * * @param string $name Property name * @return void */ public function __unset(string $name) { $callable = [$this, 'set_' . $name]; if (is_callable($callable)) { call_user_func($callable, ''); } } /** * Create a new IRI object, from a specified string * * @param string|null $iri */ public function __construct(?string $iri = null) { $this->set_iri($iri); } /** * Clean up * @return void */ public function __destruct() { $this->set_iri(null, true); $this->set_path(null, true); $this->set_authority(null, true); } /** * Create a new IRI object by resolving a relative IRI * * Returns false if $base is not absolute, otherwise an IRI. * * @param IRI|string $base (Absolute) Base IRI * @param IRI|string $relative Relative IRI * @return IRI|false */ public static function absolutize($base, $relative) { if (!($relative instanceof IRI)) { $relative = new IRI($relative); } if (!$relative->is_valid()) { return false; } elseif ($relative->scheme !== null) { return clone $relative; } else { if (!($base instanceof IRI)) { $base = new IRI($base); } if ($base->scheme !== null && $base->is_valid()) { if ($relative->get_iri() !== '') { if ($relative->iuserinfo !== null || $relative->ihost !== null || $relative->port !== null) { $target = clone $relative; $target->scheme = $base->scheme; } else { $target = new IRI(); $target->scheme = $base->scheme; $target->iuserinfo = $base->iuserinfo; $target->ihost = $base->ihost; $target->port = $base->port; if ($relative->ipath !== '') { if ($relative->ipath[0] === '/') { $target->ipath = $relative->ipath; } elseif (($base->iuserinfo !== null || $base->ihost !== null || $base->port !== null) && $base->ipath === '') { $target->ipath = '/' . $relative->ipath; } elseif (($last_segment = strrpos($base->ipath, '/')) !== false) { $target->ipath = substr($base->ipath, 0, $last_segment + 1) . $relative->ipath; } else { $target->ipath = $relative->ipath; } $target->ipath = $target->remove_dot_segments($target->ipath); $target->iquery = $relative->iquery; } else { $target->ipath = $base->ipath; if ($relative->iquery !== null) { $target->iquery = $relative->iquery; } elseif ($base->iquery !== null) { $target->iquery = $base->iquery; } } $target->ifragment = $relative->ifragment; } } else { $target = clone $base; $target->ifragment = null; } $target->scheme_normalization(); return $target; } return false; } } /** * Parse an IRI into scheme/authority/path/query/fragment segments * * @param string $iri * @return array{ * scheme: string|null, * authority: string|null, * path: string, * query: string|null, * fragment: string|null, * }|false */ protected function parse_iri(string $iri) { $iri = trim($iri, "\x20\x09\x0A\x0C\x0D"); if (preg_match('/^(?:(?P<scheme>[^:\/?#]+):)?(:?\/\/(?P<authority>[^\/?#]*))?(?P<path>[^?#]*)(?:\?(?P<query>[^#]*))?(?:#(?P<fragment>.*))?$/', $iri, $match, \PREG_UNMATCHED_AS_NULL)) { // TODO: Remove once we require PHP ≥ 7.4. $match['query'] = $match['query'] ?? null; $match['fragment'] = $match['fragment'] ?? null; return $match; } // This can occur when a paragraph is accidentally parsed as a URI return false; } /** * Remove dot segments from a path * * @param string $input * @return string */ protected function remove_dot_segments(string $input) { $output = ''; while (strpos($input, './') !== false || strpos($input, '/.') !== false || $input === '.' || $input === '..') { // A: If the input buffer begins with a prefix of "../" or "./", then remove that prefix from the input buffer; otherwise, if (strpos($input, '../') === 0) { $input = substr($input, 3); } elseif (strpos($input, './') === 0) { $input = substr($input, 2); } // B: if the input buffer begins with a prefix of "/./" or "/.", where "." is a complete path segment, then replace that prefix with "/" in the input buffer; otherwise, elseif (strpos($input, '/./') === 0) { $input = substr($input, 2); } elseif ($input === '/.') { $input = '/'; } // C: if the input buffer begins with a prefix of "/../" or "/..", where ".." is a complete path segment, then replace that prefix with "/" in the input buffer and remove the last segment and its preceding "/" (if any) from the output buffer; otherwise, elseif (strpos($input, '/../') === 0) { $input = substr($input, 3); $output = substr_replace($output, '', intval(strrpos($output, '/'))); } elseif ($input === '/..') { $input = '/'; $output = substr_replace($output, '', intval(strrpos($output, '/'))); } // D: if the input buffer consists only of "." or "..", then remove that from the input buffer; otherwise, elseif ($input === '.' || $input === '..') { $input = ''; } // E: move the first path segment in the input buffer to the end of the output buffer, including the initial "/" character (if any) and any subsequent characters up to, but not including, the next "/" character or the end of the input buffer elseif (($pos = strpos($input, '/', 1)) !== false) { $output .= substr($input, 0, $pos); $input = substr_replace($input, '', 0, $pos); } else { $output .= $input; $input = ''; } } return $output . $input; } /** * Replace invalid character with percent encoding * * @param string $string Input string * @param string $extra_chars Valid characters not in iunreserved or * iprivate (this is ASCII-only) * @param bool $iprivate Allow iprivate * @return string */ protected function replace_invalid_with_pct_encoding(string $string, string $extra_chars, bool $iprivate = false) { // Normalize as many pct-encoded sections as possible $string = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', [$this, 'remove_iunreserved_percent_encoded'], $string); \assert(\is_string($string), "For PHPStan: Should not occur, the regex is valid"); // Replace invalid percent characters $string = preg_replace('/%(?![A-Fa-f0-9]{2})/', '%25', $string); \assert(\is_string($string), "For PHPStan: Should not occur, the regex is valid"); // Add unreserved and % to $extra_chars (the latter is safe because all // pct-encoded sections are now valid). $extra_chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~%'; // Now replace any bytes that aren't allowed with their pct-encoded versions $position = 0; $strlen = strlen($string); while (($position += strspn($string, $extra_chars, $position)) < $strlen) { $value = ord($string[$position]); $character = 0; // Start position $start = $position; // By default we are valid $valid = true; // No one byte sequences are valid due to the while. // Two byte sequence: if (($value & 0xE0) === 0xC0) { $character = ($value & 0x1F) << 6; $length = 2; $remaining = 1; } // Three byte sequence: elseif (($value & 0xF0) === 0xE0) { $character = ($value & 0x0F) << 12; $length = 3; $remaining = 2; } // Four byte sequence: elseif (($value & 0xF8) === 0xF0) { $character = ($value & 0x07) << 18; $length = 4; $remaining = 3; } // Invalid byte: else { $valid = false; $length = 1; $remaining = 0; } if ($remaining) { if ($position + $length <= $strlen) { for ($position++; $remaining; $position++) { $value = ord($string[$position]); // Check that the byte is valid, then add it to the character: if (($value & 0xC0) === 0x80) { $character |= ($value & 0x3F) << (--$remaining * 6); } // If it is invalid, count the sequence as invalid and reprocess the current byte: else { $valid = false; $position--; break; } } } else { $position = $strlen - 1; $valid = false; } } // Percent encode anything invalid or not in ucschar if ( // Invalid sequences !$valid // Non-shortest form sequences are invalid || $length > 1 && $character <= 0x7F || $length > 2 && $character <= 0x7FF || $length > 3 && $character <= 0xFFFF // Outside of range of ucschar codepoints // Noncharacters || ($character & 0xFFFE) === 0xFFFE || $character >= 0xFDD0 && $character <= 0xFDEF || ( // Everything else not in ucschar $character > 0xD7FF && $character < 0xF900 || $character < 0xA0 || $character > 0xEFFFD ) && ( // Everything not in iprivate, if it applies !$iprivate || $character < 0xE000 || $character > 0x10FFFD ) ) { // If we were a character, pretend we weren't, but rather an error. if ($valid) { $position--; } for ($j = $start; $j <= $position; $j++) { $string = substr_replace($string, sprintf('%%%02X', ord($string[$j])), $j, 1); $j += 2; $position += 2; $strlen += 2; } } } return $string; } /** * Callback function for preg_replace_callback. * * Removes sequences of percent encoded bytes that represent UTF-8 * encoded characters in iunreserved * * @param array{string} $match PCRE match, a capture group #0 consisting of a sequence of valid percent-encoded bytes * @return string Replacement */ protected function remove_iunreserved_percent_encoded(array $match) { // As we just have valid percent encoded sequences we can just explode // and ignore the first member of the returned array (an empty string). $bytes = explode('%', $match[0]); // Initialize the new string (this is what will be returned) and that // there are no bytes remaining in the current sequence (unsurprising // at the first byte!). $string = ''; $remaining = 0; // these variables will be initialized in the loop but PHPStan is not able to detect it currently $start = 0; $character = 0; $length = 0; $valid = true; // Loop over each and every byte, and set $value to its value for ($i = 1, $len = count($bytes); $i < $len; $i++) { $value = hexdec($bytes[$i]); // If we're the first byte of sequence: if (!$remaining) { // Start position $start = $i; // By default we are valid $valid = true; // One byte sequence: if ($value <= 0x7F) { $character = $value; $length = 1; } // Two byte sequence: elseif (($value & 0xE0) === 0xC0) { $character = ($value & 0x1F) << 6; $length = 2; $remaining = 1; } // Three byte sequence: elseif (($value & 0xF0) === 0xE0) { $character = ($value & 0x0F) << 12; $length = 3; $remaining = 2; } // Four byte sequence: elseif (($value & 0xF8) === 0xF0) { $character = ($value & 0x07) << 18; $length = 4; $remaining = 3; } // Invalid byte: else { $valid = false; $remaining = 0; } } // Continuation byte: else { // Check that the byte is valid, then add it to the character: if (($value & 0xC0) === 0x80) { $remaining--; $character |= ($value & 0x3F) << ($remaining * 6); } // If it is invalid, count the sequence as invalid and reprocess the current byte as the start of a sequence: else { $valid = false; $remaining = 0; $i--; } } // If we've reached the end of the current byte sequence, append it to Unicode::$data if (!$remaining) { // Percent encode anything invalid or not in iunreserved if ( // Invalid sequences !$valid // Non-shortest form sequences are invalid || $length > 1 && $character <= 0x7F || $length > 2 && $character <= 0x7FF || $length > 3 && $character <= 0xFFFF // Outside of range of iunreserved codepoints || $character < 0x2D || $character > 0xEFFFD // Noncharacters || ($character & 0xFFFE) === 0xFFFE || $character >= 0xFDD0 && $character <= 0xFDEF // Everything else not in iunreserved (this is all BMP) || $character === 0x2F || $character > 0x39 && $character < 0x41 || $character > 0x5A && $character < 0x61 || $character > 0x7A && $character < 0x7E || $character > 0x7E && $character < 0xA0 || $character > 0xD7FF && $character < 0xF900 ) { for ($j = $start; $j <= $i; $j++) { $string .= '%' . strtoupper($bytes[$j]); } } else { for ($j = $start; $j <= $i; $j++) { // Cast for PHPStan, this will always be a number between 0 and 0xFF so hexdec will return int. $string .= chr((int) hexdec($bytes[$j])); } } } } // If we have any bytes left over they are invalid (i.e., we are // mid-way through a multi-byte sequence) if ($remaining) { for ($j = $start; $j < $len; $j++) { $string .= '%' . strtoupper($bytes[$j]); } } return $string; } /** * @return void */ protected function scheme_normalization() { if ($this->scheme === null) { return; } if (isset($this->normalization[$this->scheme]['iuserinfo']) && $this->iuserinfo === $this->normalization[$this->scheme]['iuserinfo']) { $this->iuserinfo = null; } if (isset($this->normalization[$this->scheme]['ihost']) && $this->ihost === $this->normalization[$this->scheme]['ihost']) { $this->ihost = null; } if (isset($this->normalization[$this->scheme]['port']) && $this->port === $this->normalization[$this->scheme]['port']) { $this->port = null; } if (isset($this->normalization[$this->scheme]['ipath']) && $this->ipath === $this->normalization[$this->scheme]['ipath']) { $this->ipath = ''; } if (isset($this->normalization[$this->scheme]['iquery']) && $this->iquery === $this->normalization[$this->scheme]['iquery']) { $this->iquery = null; } if (isset($this->normalization[$this->scheme]['ifragment']) && $this->ifragment === $this->normalization[$this->scheme]['ifragment']) { $this->ifragment = null; } } /** * Check if the object represents a valid IRI. This needs to be done on each * call as some things change depending on another part of the IRI. * * @return bool */ public function is_valid() { if ($this->ipath === '') { return true; } $isauthority = $this->iuserinfo !== null || $this->ihost !== null || $this->port !== null; if ($isauthority && $this->ipath[0] === '/') { return true; } if (!$isauthority && (substr($this->ipath, 0, 2) === '//')) { return false; } // Relative urls cannot have a colon in the first path segment (and the // slashes themselves are not included so skip the first character). if (!$this->scheme && !$isauthority && strpos($this->ipath, ':') !== false && strpos($this->ipath, '/', 1) !== false && strpos($this->ipath, ':') < strpos($this->ipath, '/', 1)) { return false; } return true; } /** * Set the entire IRI. Returns true on success, false on failure (if there * are any invalid characters). * * @param string|null $iri * @return bool */ public function set_iri(?string $iri, bool $clear_cache = false) { static $cache; if ($clear_cache) { $cache = null; return false; } if (!$cache) { $cache = []; } if ($iri === null) { return true; } elseif (isset($cache[$iri])) { [ $this->scheme, $this->iuserinfo, $this->ihost, $this->port, $this->ipath, $this->iquery, $this->ifragment, $return ] = $cache[$iri]; return $return; } $parsed = $this->parse_iri((string) $iri); if (!$parsed) { return false; } $return = $this->set_scheme($parsed['scheme']) && $this->set_authority($parsed['authority']) && $this->set_path($parsed['path']) && $this->set_query($parsed['query']) && $this->set_fragment($parsed['fragment']); $cache[$iri] = [ $this->scheme, $this->iuserinfo, $this->ihost, $this->port, $this->ipath, $this->iquery, $this->ifragment, $return ]; return $return; } /** * Set the scheme. Returns true on success, false on failure (if there are * any invalid characters). * * @param string|null $scheme * @return bool */ public function set_scheme(?string $scheme) { if ($scheme === null) { $this->scheme = null; } elseif (!preg_match('/^[A-Za-z][0-9A-Za-z+\-.]*$/', $scheme)) { $this->scheme = null; return false; } else { $this->scheme = strtolower($scheme); } return true; } /** * Set the authority. Returns true on success, false on failure (if there are * any invalid characters). * * @param string|null $authority * @return bool */ public function set_authority(?string $authority, bool $clear_cache = false) { static $cache; if ($clear_cache) { $cache = null; return false; } if (!$cache) { $cache = []; } if ($authority === null) { $this->iuserinfo = null; $this->ihost = null; $this->port = null; return true; } elseif (isset($cache[$authority])) { [ $this->iuserinfo, $this->ihost, $this->port, $return ] = $cache[$authority]; return $return; } $remaining = $authority; if (($iuserinfo_end = strrpos($remaining, '@')) !== false) { // Cast for PHPStan on PHP < 8.0. It does not detect that // the range is not flipped so substr cannot return false. $iuserinfo = (string) substr($remaining, 0, $iuserinfo_end); $remaining = substr($remaining, $iuserinfo_end + 1); } else { $iuserinfo = null; } if (($port_start = strpos($remaining, ':', intval(strpos($remaining, ']')))) !== false) { $port = substr($remaining, $port_start + 1); if ($port === false) { $port = null; } $remaining = substr($remaining, 0, $port_start); } else { $port = null; } $return = $this->set_userinfo($iuserinfo) && $this->set_host($remaining) && $this->set_port($port); $cache[$authority] = [ $this->iuserinfo, $this->ihost, $this->port, $return ]; return $return; } /** * Set the iuserinfo. * * @param string|null $iuserinfo * @return bool */ public function set_userinfo(?string $iuserinfo) { if ($iuserinfo === null) { $this->iuserinfo = null; } else { $this->iuserinfo = $this->replace_invalid_with_pct_encoding($iuserinfo, '!$&\'()*+,;=:'); $this->scheme_normalization(); } return true; } /** * Set the ihost. Returns true on success, false on failure (if there are * any invalid characters). * * @param string|null $ihost * @return bool */ public function set_host(?string $ihost) { if ($ihost === null) { $this->ihost = null; return true; } elseif (substr($ihost, 0, 1) === '[' && substr($ihost, -1) === ']') { if (\SimplePie\Net\IPv6::check_ipv6(substr($ihost, 1, -1))) { $this->ihost = '[' . \SimplePie\Net\IPv6::compress(substr($ihost, 1, -1)) . ']'; } else { $this->ihost = null; return false; } } else { $ihost = $this->replace_invalid_with_pct_encoding($ihost, '!$&\'()*+,;='); // Lowercase, but ignore pct-encoded sections (as they should // remain uppercase). This must be done after the previous step // as that can add unescaped characters. $position = 0; $strlen = strlen($ihost); while (($position += strcspn($ihost, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ%', $position)) < $strlen) { if ($ihost[$position] === '%') { $position += 3; } else { $ihost[$position] = strtolower($ihost[$position]); $position++; } } $this->ihost = $ihost; } $this->scheme_normalization(); return true; } /** * Set the port. Returns true on success, false on failure (if there are * any invalid characters). * * @param string|int|null $port * @return bool */ public function set_port($port) { if ($port === null) { $this->port = null; return true; } elseif (strspn((string) $port, '0123456789') === strlen((string) $port)) { $this->port = (int) $port; $this->scheme_normalization(); return true; } $this->port = null; return false; } /** * Set the ipath. * * @param string|null $ipath * @return bool */ public function set_path(?string $ipath, bool $clear_cache = false) { static $cache; if ($clear_cache) { $cache = null; return false; } if (!$cache) { $cache = []; } $ipath = (string) $ipath; if (isset($cache[$ipath])) { $this->ipath = $cache[$ipath][(int) ($this->scheme !== null)]; } else { $valid = $this->replace_invalid_with_pct_encoding($ipath, '!$&\'()*+,;=@:/'); $removed = $this->remove_dot_segments($valid); $cache[$ipath] = [$valid, $removed]; $this->ipath = ($this->scheme !== null) ? $removed : $valid; } $this->scheme_normalization(); return true; } /** * Set the iquery. * * @param string|null $iquery * @return bool */ public function set_query(?string $iquery) { if ($iquery === null) { $this->iquery = null; } else { $this->iquery = $this->replace_invalid_with_pct_encoding($iquery, '!$&\'()*+,;=:@/?', true); $this->scheme_normalization(); } return true; } /** * Set the ifragment. * * @param string|null $ifragment * @return bool */ public function set_fragment(?string $ifragment) { if ($ifragment === null) { $this->ifragment = null; } else { $this->ifragment = $this->replace_invalid_with_pct_encoding($ifragment, '!$&\'()*+,;=:@/?'); $this->scheme_normalization(); } return true; } /** * Convert an IRI to a URI (or parts thereof) * * @param string $string * @return string */ public function to_uri(string $string) { static $non_ascii; if (!$non_ascii) { $non_ascii = implode('', range("\x80", "\xFF")); } $position = 0; $strlen = strlen($string); while (($position += strcspn($string, $non_ascii, $position)) < $strlen) { $string = substr_replace($string, sprintf('%%%02X', ord($string[$position])), $position, 1); $position += 3; $strlen += 2; } return $string; } /** * Get the complete IRI * * @return string|false */ public function get_iri() { if (!$this->is_valid()) { return false; } $iri = ''; if ($this->scheme !== null) { $iri .= $this->scheme . ':'; } if (($iauthority = $this->get_iauthority()) !== null) { $iri .= '//' . $iauthority; } if ($this->ipath !== '') { $iri .= $this->ipath; } elseif (!empty($this->normalization[$this->scheme]['ipath']) && $iauthority !== null && $iauthority !== '') { $iri .= $this->normalization[$this->scheme]['ipath']; } if ($this->iquery !== null) { $iri .= '?' . $this->iquery; } if ($this->ifragment !== null) { $iri .= '#' . $this->ifragment; } return $iri; } /** * Get the complete URI * * @return string */ public function get_uri() { return $this->to_uri((string) $this->get_iri()); } /** * Get the complete iauthority * * @return ?string */ protected function get_iauthority() { if ($this->iuserinfo !== null || $this->ihost !== null || $this->port !== null) { $iauthority = ''; if ($this->iuserinfo !== null) { $iauthority .= $this->iuserinfo . '@'; } if ($this->ihost !== null) { $iauthority .= $this->ihost; } if ($this->port !== null && $this->port !== 0) { $iauthority .= ':' . $this->port; } return $iauthority; } return null; } /** * Get the complete authority * * @return ?string */ protected function get_authority() { $iauthority = $this->get_iauthority(); if (is_string($iauthority)) { return $this->to_uri($iauthority); } return $iauthority; } } class_alias('SimplePie\IRI', 'SimplePie_IRI'); Parser.php 0000644 00000104064 15162130114 0006510 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; use SimplePie\XML\Declaration\Parser as DeclarationParser; use XMLParser; /** * Parses XML into something sane * * * This class can be overloaded with {@see \SimplePie\SimplePie::set_parser_class()} */ class Parser implements RegistryAware { /** @var int */ public $error_code; /** @var string */ public $error_string; /** @var int */ public $current_line; /** @var int */ public $current_column; /** @var int */ public $current_byte; /** @var string */ public $separator = ' '; /** @var string[] */ public $namespace = ['']; /** @var string[] */ public $element = ['']; /** @var string[] */ public $xml_base = ['']; /** @var bool[] */ public $xml_base_explicit = [false]; /** @var string[] */ public $xml_lang = ['']; /** @var array<string, mixed> */ public $data = []; /** @var array<array<string, mixed>> */ public $datas = [[]]; /** @var int */ public $current_xhtml_construct = -1; /** @var string */ public $encoding; /** @var Registry */ protected $registry; /** * @return void */ public function set_registry(\SimplePie\Registry $registry) { $this->registry = $registry; } /** * @return bool */ public function parse(string &$data, string $encoding, string $url = '') { if (class_exists('DOMXpath') && function_exists('Mf2\parse')) { $doc = new \DOMDocument(); @$doc->loadHTML($data); $xpath = new \DOMXpath($doc); // Check for both h-feed and h-entry, as both a feed with no entries // and a list of entries without an h-feed wrapper are both valid. $query = '//*[contains(concat(" ", @class, " "), " h-feed ") or '. 'contains(concat(" ", @class, " "), " h-entry ")]'; /** @var \DOMNodeList<\DOMElement> $result */ $result = $xpath->query($query); if ($result->length !== 0) { return $this->parse_microformats($data, $url); } } // Use UTF-8 if we get passed US-ASCII, as every US-ASCII character is a UTF-8 character if (strtoupper($encoding) === 'US-ASCII') { $this->encoding = 'UTF-8'; } else { $this->encoding = $encoding; } // Strip BOM: // UTF-32 Big Endian BOM if (substr($data, 0, 4) === "\x00\x00\xFE\xFF") { $data = substr($data, 4); } // UTF-32 Little Endian BOM elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00") { $data = substr($data, 4); } // UTF-16 Big Endian BOM elseif (substr($data, 0, 2) === "\xFE\xFF") { $data = substr($data, 2); } // UTF-16 Little Endian BOM elseif (substr($data, 0, 2) === "\xFF\xFE") { $data = substr($data, 2); } // UTF-8 BOM elseif (substr($data, 0, 3) === "\xEF\xBB\xBF") { $data = substr($data, 3); } if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false) { $declaration = $this->registry->create(DeclarationParser::class, [substr($data, 5, $pos - 5)]); if ($declaration->parse()) { $data = substr($data, $pos + 2); $data = '<?xml version="' . $declaration->version . '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone) ? 'yes' : 'no') . '"?>' . "\n" . self::set_doctype($data); } else { $this->error_string = 'SimplePie bug! Please report this!'; return false; } } else { $data = self::set_doctype($data); } $return = true; static $xml_is_sane = null; if ($xml_is_sane === null) { $parser_check = xml_parser_create(); xml_parse_into_struct($parser_check, '<foo>&</foo>', $values); if (\PHP_VERSION_ID < 80000) { xml_parser_free($parser_check); } $xml_is_sane = isset($values[0]['value']); } // Create the parser if ($xml_is_sane) { $xml = xml_parser_create_ns($this->encoding, $this->separator); xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1); xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0); xml_set_character_data_handler($xml, [$this, 'cdata']); xml_set_element_handler($xml, [$this, 'tag_open'], [$this, 'tag_close']); // Parse! $wrapper = @is_writable(sys_get_temp_dir()) ? 'php://temp' : 'php://memory'; if (($stream = fopen($wrapper, 'r+')) && fwrite($stream, $data) && rewind($stream)) { //Parse by chunks not to use too much memory do { $stream_data = (string) fread($stream, 1048576); if (!xml_parse($xml, $stream_data, feof($stream))) { $this->error_code = xml_get_error_code($xml); $this->error_string = xml_error_string($this->error_code) ?: "Unknown"; $return = false; break; } } while (!feof($stream)); fclose($stream); } else { $return = false; } $this->current_line = xml_get_current_line_number($xml); $this->current_column = xml_get_current_column_number($xml); $this->current_byte = xml_get_current_byte_index($xml); if (\PHP_VERSION_ID < 80000) { xml_parser_free($xml); } return $return; } libxml_clear_errors(); $xml = new \XMLReader(); $xml->xml($data); while (@$xml->read()) { switch ($xml->nodeType) { case \XMLReader::END_ELEMENT: if ($xml->namespaceURI !== '') { $tagName = $xml->namespaceURI . $this->separator . $xml->localName; } else { $tagName = $xml->localName; } $this->tag_close(null, $tagName); break; case \XMLReader::ELEMENT: $empty = $xml->isEmptyElement; if ($xml->namespaceURI !== '') { $tagName = $xml->namespaceURI . $this->separator . $xml->localName; } else { $tagName = $xml->localName; } $attributes = []; while ($xml->moveToNextAttribute()) { if ($xml->namespaceURI !== '') { $attrName = $xml->namespaceURI . $this->separator . $xml->localName; } else { $attrName = $xml->localName; } $attributes[$attrName] = $xml->value; } $this->tag_open(null, $tagName, $attributes); if ($empty) { $this->tag_close(null, $tagName); } break; case \XMLReader::TEXT: case \XMLReader::CDATA: $this->cdata(null, $xml->value); break; } } if ($error = libxml_get_last_error()) { $this->error_code = $error->code; $this->error_string = $error->message; $this->current_line = $error->line; $this->current_column = $error->column; return false; } return true; } /** * @return int */ public function get_error_code() { return $this->error_code; } /** * @return string */ public function get_error_string() { return $this->error_string; } /** * @return int */ public function get_current_line() { return $this->current_line; } /** * @return int */ public function get_current_column() { return $this->current_column; } /** * @return int */ public function get_current_byte() { return $this->current_byte; } /** * @return array<string, mixed> */ public function get_data() { return $this->data; } /** * @param XMLParser|resource|null $parser * @param array<string, string> $attributes * @return void */ public function tag_open($parser, string $tag, array $attributes) { [$this->namespace[], $this->element[]] = $this->split_ns($tag); $attribs = []; foreach ($attributes as $name => $value) { [$attrib_namespace, $attribute] = $this->split_ns($name); $attribs[$attrib_namespace][$attribute] = $value; } if (isset($attribs[\SimplePie\SimplePie::NAMESPACE_XML]['base'])) { $base = $this->registry->call(Misc::class, 'absolutize_url', [$attribs[\SimplePie\SimplePie::NAMESPACE_XML]['base'], end($this->xml_base)]); if ($base !== false) { $this->xml_base[] = $base; $this->xml_base_explicit[] = true; } } else { $this->xml_base[] = end($this->xml_base) ?: ''; $this->xml_base_explicit[] = end($this->xml_base_explicit); } if (isset($attribs[\SimplePie\SimplePie::NAMESPACE_XML]['lang'])) { $this->xml_lang[] = $attribs[\SimplePie\SimplePie::NAMESPACE_XML]['lang']; } else { $this->xml_lang[] = end($this->xml_lang) ?: ''; } if ($this->current_xhtml_construct >= 0) { $this->current_xhtml_construct++; if (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_XHTML) { $this->data['data'] .= '<' . end($this->element); if (isset($attribs[''])) { foreach ($attribs[''] as $name => $value) { $this->data['data'] .= ' ' . $name . '="' . htmlspecialchars($value, ENT_COMPAT, $this->encoding) . '"'; } } $this->data['data'] .= '>'; } } else { $this->datas[] = &$this->data; $this->data = &$this->data['child'][end($this->namespace)][end($this->element)][]; $this->data = ['data' => '', 'attribs' => $attribs, 'xml_base' => end($this->xml_base), 'xml_base_explicit' => end($this->xml_base_explicit), 'xml_lang' => end($this->xml_lang)]; if ((end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_ATOM_03 && in_array(end($this->element), ['title', 'tagline', 'copyright', 'info', 'summary', 'content']) && isset($attribs['']['mode']) && $attribs['']['mode'] === 'xml') || (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_ATOM_10 && in_array(end($this->element), ['rights', 'subtitle', 'summary', 'info', 'title', 'content']) && isset($attribs['']['type']) && $attribs['']['type'] === 'xhtml') || (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_RSS_20 && in_array(end($this->element), ['title'])) || (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_RSS_090 && in_array(end($this->element), ['title'])) || (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_RSS_10 && in_array(end($this->element), ['title']))) { $this->current_xhtml_construct = 0; } } } /** * @param XMLParser|resource|null $parser * @return void */ public function cdata($parser, string $cdata) { if ($this->current_xhtml_construct >= 0) { $this->data['data'] .= htmlspecialchars($cdata, ENT_QUOTES, $this->encoding); } else { $this->data['data'] .= $cdata; } } /** * @param XMLParser|resource|null $parser * @return void */ public function tag_close($parser, string $tag) { if ($this->current_xhtml_construct >= 0) { $this->current_xhtml_construct--; if (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_XHTML && !in_array(end($this->element), ['area', 'base', 'basefont', 'br', 'col', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param'])) { $this->data['data'] .= '</' . end($this->element) . '>'; } } if ($this->current_xhtml_construct === -1) { $this->data = &$this->datas[count($this->datas) - 1]; array_pop($this->datas); } array_pop($this->element); array_pop($this->namespace); array_pop($this->xml_base); array_pop($this->xml_base_explicit); array_pop($this->xml_lang); } /** * @return array{string, string} */ public function split_ns(string $string) { static $cache = []; if (!isset($cache[$string])) { if ($pos = strpos($string, $this->separator)) { static $separator_length; if (!$separator_length) { $separator_length = strlen($this->separator); } $namespace = substr($string, 0, $pos); $local_name = substr($string, $pos + $separator_length); if (strtolower($namespace) === \SimplePie\SimplePie::NAMESPACE_ITUNES) { $namespace = \SimplePie\SimplePie::NAMESPACE_ITUNES; } // Normalize the Media RSS namespaces if ($namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG || $namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG2 || $namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG3 || $namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG4 || $namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG5) { $namespace = \SimplePie\SimplePie::NAMESPACE_MEDIARSS; } $cache[$string] = [$namespace, $local_name]; } else { $cache[$string] = ['', $string]; } } return $cache[$string]; } /** * @param array<string, mixed> $data */ private function parse_hcard(array $data, bool $category = false): string { $name = ''; $link = ''; // Check if h-card is set and pass that information on in the link. if (isset($data['type']) && in_array('h-card', $data['type'])) { if (isset($data['properties']['name'][0])) { $name = $data['properties']['name'][0]; } if (isset($data['properties']['url'][0])) { $link = $data['properties']['url'][0]; if ($name === '') { $name = $link; } else { // can't have commas in categories. $name = str_replace(',', '', $name); } $person_tag = $category ? '<span class="person-tag"></span>' : ''; return '<a class="h-card" href="'.$link.'">'.$person_tag.$name.'</a>'; } } return $data['value'] ?? ''; } /** * @return true */ private function parse_microformats(string &$data, string $url): bool { // For PHPStan, we already check that in call site. \assert(function_exists('Mf2\parse')); \assert(function_exists('Mf2\fetch')); $feed_title = ''; $feed_author = null; $author_cache = []; $items = []; $entries = []; $mf = \Mf2\parse($data, $url); // First look for an h-feed. $h_feed = []; foreach ($mf['items'] as $mf_item) { if (in_array('h-feed', $mf_item['type'])) { $h_feed = $mf_item; break; } // Also look for h-feed or h-entry in the children of each top level item. if (!isset($mf_item['children'][0]['type'])) { continue; } if (in_array('h-feed', $mf_item['children'][0]['type'])) { $h_feed = $mf_item['children'][0]; // In this case the parent of the h-feed may be an h-card, so use it as // the feed_author. if (in_array('h-card', $mf_item['type'])) { $feed_author = $mf_item; } break; } elseif (in_array('h-entry', $mf_item['children'][0]['type'])) { $entries = $mf_item['children']; // In this case the parent of the h-entry list may be an h-card, so use // it as the feed_author. if (in_array('h-card', $mf_item['type'])) { $feed_author = $mf_item; } break; } } if (isset($h_feed['children'])) { $entries = $h_feed['children']; // Also set the feed title and store author from the h-feed if available. if (isset($mf['items'][0]['properties']['name'][0])) { $feed_title = $mf['items'][0]['properties']['name'][0]; } if (isset($mf['items'][0]['properties']['author'][0])) { $feed_author = $mf['items'][0]['properties']['author'][0]; } } elseif (count($entries) === 0) { $entries = $mf['items']; } for ($i = 0; $i < count($entries); $i++) { $entry = $entries[$i]; if (in_array('h-entry', $entry['type'])) { $item = []; $title = ''; $description = ''; if (isset($entry['properties']['url'][0])) { $link = $entry['properties']['url'][0]; if (isset($link['value'])) { $link = $link['value']; } $item['link'] = [['data' => $link]]; } if (isset($entry['properties']['uid'][0])) { $guid = $entry['properties']['uid'][0]; if (isset($guid['value'])) { $guid = $guid['value']; } $item['guid'] = [['data' => $guid]]; } if (isset($entry['properties']['name'][0])) { $title = $entry['properties']['name'][0]; if (isset($title['value'])) { $title = $title['value']; } $item['title'] = [['data' => $title]]; } if (isset($entry['properties']['author'][0]) || isset($feed_author)) { // author is a special case, it can be plain text or an h-card array. // If it's plain text it can also be a url that should be followed to // get the actual h-card. $author = $entry['properties']['author'][0] ?? $feed_author; if (!is_string($author)) { $author = $this->parse_hcard($author); } elseif (strpos($author, 'http') === 0) { if (isset($author_cache[$author])) { $author = $author_cache[$author]; } else { if ($mf = \Mf2\fetch($author)) { foreach ($mf['items'] as $hcard) { // Only interested in an h-card by itself in this case. if (!in_array('h-card', $hcard['type'])) { continue; } // It must have a url property matching what we fetched. if (!isset($hcard['properties']['url']) || !(in_array($author, $hcard['properties']['url']))) { continue; } // Save parse_hcard the trouble of finding the correct url. $hcard['properties']['url'][0] = $author; // Cache this h-card for the next h-entry to check. $author_cache[$author] = $this->parse_hcard($hcard); $author = $author_cache[$author]; break; } } } } $item['author'] = [['data' => $author]]; } if (isset($entry['properties']['photo'][0])) { // If a photo is also in content, don't need to add it again here. $content = ''; if (isset($entry['properties']['content'][0]['html'])) { $content = $entry['properties']['content'][0]['html']; } $photo_list = []; for ($j = 0; $j < count($entry['properties']['photo']); $j++) { $photo = $entry['properties']['photo'][$j]; if (!empty($photo) && strpos($content, $photo) === false) { $photo_list[] = $photo; } } // When there's more than one photo show the first and use a lightbox. // Need a permanent, unique name for the image set, but don't have // anything unique except for the content itself, so use that. $count = count($photo_list); if ($count > 1) { $image_set_id = preg_replace('/[[:^alnum:]]/', '', $photo_list[0]); $description = '<p>'; for ($j = 0; $j < $count; $j++) { $hidden = $j === 0 ? '' : 'class="hidden" '; $description .= '<a href="'.$photo_list[$j].'" '.$hidden. 'data-lightbox="image-set-'.$image_set_id.'">'. '<img src="'.$photo_list[$j].'"></a>'; } $description .= '<br><b>'.$count.' photos</b></p>'; } elseif ($count == 1) { $description = '<p><img src="'.$photo_list[0].'"></p>'; } } if (isset($entry['properties']['content'][0]['html'])) { // e-content['value'] is the same as p-name when they are on the same // element. Use this to replace title with a strip_tags version so // that alt text from images is not included in the title. if ($entry['properties']['content'][0]['value'] === $title) { $title = strip_tags($entry['properties']['content'][0]['html']); $item['title'] = [['data' => $title]]; } $description .= $entry['properties']['content'][0]['html']; if (isset($entry['properties']['in-reply-to'][0])) { $in_reply_to = ''; if (is_string($entry['properties']['in-reply-to'][0])) { $in_reply_to = $entry['properties']['in-reply-to'][0]; } elseif (isset($entry['properties']['in-reply-to'][0]['value'])) { $in_reply_to = $entry['properties']['in-reply-to'][0]['value']; } if ($in_reply_to !== '') { $description .= '<p><span class="in-reply-to"></span> '. '<a href="'.$in_reply_to.'">'.$in_reply_to.'</a><p>'; } } $item['description'] = [['data' => $description]]; } if (isset($entry['properties']['category'])) { $category_csv = ''; // Categories can also contain h-cards. foreach ($entry['properties']['category'] as $category) { if ($category_csv !== '') { $category_csv .= ', '; } if (is_string($category)) { // Can't have commas in categories. $category_csv .= str_replace(',', '', $category); } else { $category_csv .= $this->parse_hcard($category, true); } } $item['category'] = [['data' => $category_csv]]; } if (isset($entry['properties']['published'][0])) { $timestamp = strtotime($entry['properties']['published'][0]); $pub_date = date('F j Y g:ia', $timestamp).' GMT'; $item['pubDate'] = [['data' => $pub_date]]; } // The title and description are set to the empty string to represent // a deleted item (which also makes it an invalid rss item). if (isset($entry['properties']['deleted'][0])) { $item['title'] = [['data' => '']]; $item['description'] = [['data' => '']]; } $items[] = ['child' => ['' => $item]]; } } // Mimic RSS data format when storing microformats. $link = [['data' => $url]]; $image = ''; if (!is_string($feed_author) && isset($feed_author['properties']['photo'][0])) { $image = [['child' => ['' => ['url' => [['data' => $feed_author['properties']['photo'][0]]]]]]]; } // Use the name given for the h-feed, or get the title from the html. if ($feed_title !== '') { $feed_title = [['data' => htmlspecialchars($feed_title)]]; } elseif ($position = strpos($data, '<title>')) { $start = $position < 200 ? 0 : $position - 200; $check = substr($data, $start, 400); $matches = []; if (preg_match('/<title>(.+)<\/title>/', $check, $matches)) { $feed_title = [['data' => htmlspecialchars($matches[1])]]; } } $channel = ['channel' => [['child' => ['' => ['link' => $link, 'image' => $image, 'title' => $feed_title, 'item' => $items]]]]]; $rss = [['attribs' => ['' => ['version' => '2.0']], 'child' => ['' => $channel]]]; $this->data = ['child' => ['' => ['rss' => $rss]]]; return true; } private static function set_doctype(string $data): string { // Strip DOCTYPE except if containing an [internal subset] $data = preg_replace('/^\\s*<!DOCTYPE\\s[^>\\[\\]]*>\s*/', '', $data) ?? $data; // Declare HTML entities only if no remaining DOCTYPE $doctype = preg_match('/^\\s*<!DOCTYPE\\s/', $data) ? '' : self::declare_html_entities(); return $doctype . $data; } private static function declare_html_entities(): string { // This is required because the RSS specification says that entity-encoded // html is allowed, but the xml specification says they must be declared. return '<!DOCTYPE rss [ <!ENTITY nbsp " "> <!ENTITY iexcl "¡"> <!ENTITY cent "¢"> <!ENTITY pound "£"> <!ENTITY curren "¤"> <!ENTITY yen "¥"> <!ENTITY brvbar "¦"> <!ENTITY sect "§"> <!ENTITY uml "¨"> <!ENTITY copy "©"> <!ENTITY ordf "ª"> <!ENTITY laquo "«"> <!ENTITY not "¬"> <!ENTITY shy "­"> <!ENTITY reg "®"> <!ENTITY macr "¯"> <!ENTITY deg "°"> <!ENTITY plusmn "±"> <!ENTITY sup2 "²"> <!ENTITY sup3 "³"> <!ENTITY acute "´"> <!ENTITY micro "µ"> <!ENTITY para "¶"> <!ENTITY middot "·"> <!ENTITY cedil "¸"> <!ENTITY sup1 "¹"> <!ENTITY ordm "º"> <!ENTITY raquo "»"> <!ENTITY frac14 "¼"> <!ENTITY frac12 "½"> <!ENTITY frac34 "¾"> <!ENTITY iquest "¿"> <!ENTITY Agrave "À"> <!ENTITY Aacute "Á"> <!ENTITY Acirc "Â"> <!ENTITY Atilde "Ã"> <!ENTITY Auml "Ä"> <!ENTITY Aring "Å"> <!ENTITY AElig "Æ"> <!ENTITY Ccedil "Ç"> <!ENTITY Egrave "È"> <!ENTITY Eacute "É"> <!ENTITY Ecirc "Ê"> <!ENTITY Euml "Ë"> <!ENTITY Igrave "Ì"> <!ENTITY Iacute "Í"> <!ENTITY Icirc "Î"> <!ENTITY Iuml "Ï"> <!ENTITY ETH "Ð"> <!ENTITY Ntilde "Ñ"> <!ENTITY Ograve "Ò"> <!ENTITY Oacute "Ó"> <!ENTITY Ocirc "Ô"> <!ENTITY Otilde "Õ"> <!ENTITY Ouml "Ö"> <!ENTITY times "×"> <!ENTITY Oslash "Ø"> <!ENTITY Ugrave "Ù"> <!ENTITY Uacute "Ú"> <!ENTITY Ucirc "Û"> <!ENTITY Uuml "Ü"> <!ENTITY Yacute "Ý"> <!ENTITY THORN "Þ"> <!ENTITY szlig "ß"> <!ENTITY agrave "à"> <!ENTITY aacute "á"> <!ENTITY acirc "â"> <!ENTITY atilde "ã"> <!ENTITY auml "ä"> <!ENTITY aring "å"> <!ENTITY aelig "æ"> <!ENTITY ccedil "ç"> <!ENTITY egrave "è"> <!ENTITY eacute "é"> <!ENTITY ecirc "ê"> <!ENTITY euml "ë"> <!ENTITY igrave "ì"> <!ENTITY iacute "í"> <!ENTITY icirc "î"> <!ENTITY iuml "ï"> <!ENTITY eth "ð"> <!ENTITY ntilde "ñ"> <!ENTITY ograve "ò"> <!ENTITY oacute "ó"> <!ENTITY ocirc "ô"> <!ENTITY otilde "õ"> <!ENTITY ouml "ö"> <!ENTITY divide "÷"> <!ENTITY oslash "ø"> <!ENTITY ugrave "ù"> <!ENTITY uacute "ú"> <!ENTITY ucirc "û"> <!ENTITY uuml "ü"> <!ENTITY yacute "ý"> <!ENTITY thorn "þ"> <!ENTITY yuml "ÿ"> <!ENTITY OElig "Œ"> <!ENTITY oelig "œ"> <!ENTITY Scaron "Š"> <!ENTITY scaron "š"> <!ENTITY Yuml "Ÿ"> <!ENTITY fnof "ƒ"> <!ENTITY circ "ˆ"> <!ENTITY tilde "˜"> <!ENTITY Alpha "Α"> <!ENTITY Beta "Β"> <!ENTITY Gamma "Γ"> <!ENTITY Epsilon "Ε"> <!ENTITY Zeta "Ζ"> <!ENTITY Eta "Η"> <!ENTITY Theta "Θ"> <!ENTITY Iota "Ι"> <!ENTITY Kappa "Κ"> <!ENTITY Lambda "Λ"> <!ENTITY Mu "Μ"> <!ENTITY Nu "Ν"> <!ENTITY Xi "Ξ"> <!ENTITY Omicron "Ο"> <!ENTITY Pi "Π"> <!ENTITY Rho "Ρ"> <!ENTITY Sigma "Σ"> <!ENTITY Tau "Τ"> <!ENTITY Upsilon "Υ"> <!ENTITY Phi "Φ"> <!ENTITY Chi "Χ"> <!ENTITY Psi "Ψ"> <!ENTITY Omega "Ω"> <!ENTITY alpha "α"> <!ENTITY beta "β"> <!ENTITY gamma "γ"> <!ENTITY delta "δ"> <!ENTITY epsilon "ε"> <!ENTITY zeta "ζ"> <!ENTITY eta "η"> <!ENTITY theta "θ"> <!ENTITY iota "ι"> <!ENTITY kappa "κ"> <!ENTITY lambda "λ"> <!ENTITY mu "μ"> <!ENTITY nu "ν"> <!ENTITY xi "ξ"> <!ENTITY omicron "ο"> <!ENTITY pi "π"> <!ENTITY rho "ρ"> <!ENTITY sigmaf "ς"> <!ENTITY sigma "σ"> <!ENTITY tau "τ"> <!ENTITY upsilon "υ"> <!ENTITY phi "φ"> <!ENTITY chi "χ"> <!ENTITY psi "ψ"> <!ENTITY omega "ω"> <!ENTITY thetasym "ϑ"> <!ENTITY upsih "ϒ"> <!ENTITY piv "ϖ"> <!ENTITY ensp " "> <!ENTITY emsp " "> <!ENTITY thinsp " "> <!ENTITY zwnj "‌"> <!ENTITY zwj "‍"> <!ENTITY lrm "‎"> <!ENTITY rlm "‏"> <!ENTITY ndash "–"> <!ENTITY mdash "—"> <!ENTITY lsquo "‘"> <!ENTITY rsquo "’"> <!ENTITY sbquo "‚"> <!ENTITY ldquo "“"> <!ENTITY rdquo "”"> <!ENTITY bdquo "„"> <!ENTITY dagger "†"> <!ENTITY Dagger "‡"> <!ENTITY bull "•"> <!ENTITY hellip "…"> <!ENTITY permil "‰"> <!ENTITY prime "′"> <!ENTITY Prime "″"> <!ENTITY lsaquo "‹"> <!ENTITY rsaquo "›"> <!ENTITY oline "‾"> <!ENTITY frasl "⁄"> <!ENTITY euro "€"> <!ENTITY image "ℑ"> <!ENTITY weierp "℘"> <!ENTITY real "ℜ"> <!ENTITY trade "™"> <!ENTITY alefsym "ℵ"> <!ENTITY larr "←"> <!ENTITY uarr "↑"> <!ENTITY rarr "→"> <!ENTITY darr "↓"> <!ENTITY harr "↔"> <!ENTITY crarr "↵"> <!ENTITY lArr "⇐"> <!ENTITY uArr "⇑"> <!ENTITY rArr "⇒"> <!ENTITY dArr "⇓"> <!ENTITY hArr "⇔"> <!ENTITY forall "∀"> <!ENTITY part "∂"> <!ENTITY exist "∃"> <!ENTITY empty "∅"> <!ENTITY nabla "∇"> <!ENTITY isin "∈"> <!ENTITY notin "∉"> <!ENTITY ni "∋"> <!ENTITY prod "∏"> <!ENTITY sum "∑"> <!ENTITY minus "−"> <!ENTITY lowast "∗"> <!ENTITY radic "√"> <!ENTITY prop "∝"> <!ENTITY infin "∞"> <!ENTITY ang "∠"> <!ENTITY and "∧"> <!ENTITY or "∨"> <!ENTITY cap "∩"> <!ENTITY cup "∪"> <!ENTITY int "∫"> <!ENTITY there4 "∴"> <!ENTITY sim "∼"> <!ENTITY cong "≅"> <!ENTITY asymp "≈"> <!ENTITY ne "≠"> <!ENTITY equiv "≡"> <!ENTITY le "≤"> <!ENTITY ge "≥"> <!ENTITY sub "⊂"> <!ENTITY sup "⊃"> <!ENTITY nsub "⊄"> <!ENTITY sube "⊆"> <!ENTITY supe "⊇"> <!ENTITY oplus "⊕"> <!ENTITY otimes "⊗"> <!ENTITY perp "⊥"> <!ENTITY sdot "⋅"> <!ENTITY lceil "⌈"> <!ENTITY rceil "⌉"> <!ENTITY lfloor "⌊"> <!ENTITY rfloor "⌋"> <!ENTITY lang "〈"> <!ENTITY rang "〉"> <!ENTITY loz "◊"> <!ENTITY spades "♠"> <!ENTITY clubs "♣"> <!ENTITY hearts "♥"> <!ENTITY diams "♦"> ]>'; } } class_alias('SimplePie\Parser', 'SimplePie_Parser'); Net/IPv6.php 0000644 00000016324 15162130115 0006570 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Net; /** * Class to validate and to work with IPv6 addresses. * * @copyright 2003-2005 The PHP Group * @license http://www.opensource.org/licenses/bsd-license.php * @link http://pear.php.net/package/Net_IPv6 * @author Alexander Merz <alexander.merz@web.de> * @author elfrink at introweb dot nl * @author Josh Peck <jmp at joshpeck dot org> * @author Sam Sneddon <geoffers@gmail.com> */ class IPv6 { /** * Uncompresses an IPv6 address * * RFC 4291 allows you to compress consecutive zero pieces in an address to * '::'. This method expects a valid IPv6 address and expands the '::' to * the required number of zero pieces. * * Example: FF01::101 -> FF01:0:0:0:0:0:0:101 * ::1 -> 0:0:0:0:0:0:0:1 * * @author Alexander Merz <alexander.merz@web.de> * @author elfrink at introweb dot nl * @author Josh Peck <jmp at joshpeck dot org> * @copyright 2003-2005 The PHP Group * @license http://www.opensource.org/licenses/bsd-license.php * @param string $ip An IPv6 address * @return string The uncompressed IPv6 address */ public static function uncompress(string $ip) { $c1 = -1; $c2 = -1; if (substr_count($ip, '::') === 1) { [$ip1, $ip2] = explode('::', $ip); if ($ip1 === '') { $c1 = -1; } else { $c1 = substr_count($ip1, ':'); } if ($ip2 === '') { $c2 = -1; } else { $c2 = substr_count($ip2, ':'); } if (strpos($ip2, '.') !== false) { $c2++; } // :: if ($c1 === -1 && $c2 === -1) { $ip = '0:0:0:0:0:0:0:0'; } // ::xxx elseif ($c1 === -1) { $fill = str_repeat('0:', 7 - $c2); $ip = str_replace('::', $fill, $ip); } // xxx:: elseif ($c2 === -1) { $fill = str_repeat(':0', 7 - $c1); $ip = str_replace('::', $fill, $ip); } // xxx::xxx else { $fill = ':' . str_repeat('0:', 6 - $c2 - $c1); $ip = str_replace('::', $fill, $ip); } } return $ip; } /** * Compresses an IPv6 address * * RFC 4291 allows you to compress consecutive zero pieces in an address to * '::'. This method expects a valid IPv6 address and compresses consecutive * zero pieces to '::'. * * Example: FF01:0:0:0:0:0:0:101 -> FF01::101 * 0:0:0:0:0:0:0:1 -> ::1 * * @see uncompress() * @param string $ip An IPv6 address * @return string The compressed IPv6 address */ public static function compress(string $ip) { // Prepare the IP to be compressed $ip = self::uncompress($ip); $ip_parts = self::split_v6_v4($ip); // Replace all leading zeros $ip_parts[0] = (string) preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]); // Find bunches of zeros if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE)) { $max = 0; $pos = null; foreach ($matches[0] as $match) { if (strlen($match[0]) > $max) { $max = strlen($match[0]); $pos = $match[1]; } } assert($pos !== null, 'For PHPStan: Since the regex matched, there is at least one match. And because the pattern is non-empty, the loop will always end with $pos ≥ 1.'); $ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max); } if ($ip_parts[1] !== '') { return implode(':', $ip_parts); } return $ip_parts[0]; } /** * Splits an IPv6 address into the IPv6 and IPv4 representation parts * * RFC 4291 allows you to represent the last two parts of an IPv6 address * using the standard IPv4 representation * * Example: 0:0:0:0:0:0:13.1.68.3 * 0:0:0:0:0:FFFF:129.144.52.38 * * @param string $ip An IPv6 address * @return array{string, string} [0] contains the IPv6 represented part, and [1] the IPv4 represented part */ private static function split_v6_v4(string $ip): array { if (strpos($ip, '.') !== false) { $pos = strrpos($ip, ':'); assert($pos !== false, 'For PHPStan: IPv6 address must contain colon, since split_v6_v4 is only ever called after uncompress.'); $ipv6_part = substr($ip, 0, $pos); $ipv4_part = substr($ip, $pos + 1); return [$ipv6_part, $ipv4_part]; } return [$ip, '']; } /** * Checks an IPv6 address * * Checks if the given IP is a valid IPv6 address * * @param string $ip An IPv6 address * @return bool true if $ip is a valid IPv6 address */ public static function check_ipv6(string $ip) { $ip = self::uncompress($ip); [$ipv6, $ipv4] = self::split_v6_v4($ip); $ipv6 = explode(':', $ipv6); $ipv4 = explode('.', $ipv4); if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4) { foreach ($ipv6 as $ipv6_part) { // The section can't be empty if ($ipv6_part === '') { return false; } // Nor can it be over four characters if (strlen($ipv6_part) > 4) { return false; } // Remove leading zeros (this is safe because of the above) $ipv6_part = ltrim($ipv6_part, '0'); if ($ipv6_part === '') { $ipv6_part = '0'; } // Check the value is valid $value = hexdec($ipv6_part); if ($value < 0 || $value > 0xFFFF) { return false; } assert(is_int($value), 'For PHPStan: $value is only float when $ipv6_part > PHP_INT_MAX'); if (dechex($value) !== strtolower($ipv6_part)) { return false; } } if (count($ipv4) === 4) { foreach ($ipv4 as $ipv4_part) { $value = (int) $ipv4_part; if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF) { return false; } } } return true; } return false; } /** * Checks if the given IP is a valid IPv6 address * * @codeCoverageIgnore * @deprecated Use {@see IPv6::check_ipv6()} instead * @see check_ipv6 * @param string $ip An IPv6 address * @return bool true if $ip is a valid IPv6 address */ public static function checkIPv6(string $ip) { return self::check_ipv6($ip); } } class_alias('SimplePie\Net\IPv6', 'SimplePie_Net_IPv6'); Cache/Base.php 0000644 00000003526 15162130117 0007135 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Cache; /** * Base for cache objects * * Classes to be used with {@see \SimplePie\Cache::register()} are expected * to implement this interface. * * @deprecated since SimplePie 1.8.0, use "Psr\SimpleCache\CacheInterface" instead */ interface Base { /** * Feed cache type * * @var string */ public const TYPE_FEED = 'spc'; /** * Image cache type * * @var string */ public const TYPE_IMAGE = 'spi'; /** * Create a new cache object * * @param string $location Location string (from SimplePie::$cache_location) * @param string $name Unique ID for the cache * @param Base::TYPE_FEED|Base::TYPE_IMAGE $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data */ public function __construct(string $location, string $name, $type); /** * Save data to the cache * * @param array<mixed>|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property * @return bool Successfulness */ public function save($data); /** * Retrieve the data saved to the cache * * @return array<mixed> Data for SimplePie::$data */ public function load(); /** * Retrieve the last modified time for the cache * * @return int Timestamp */ public function mtime(); /** * Set the last modified time to the current time * * @return bool Success status */ public function touch(); /** * Remove the cache * * @return bool Success status */ public function unlink(); } class_alias('SimplePie\Cache\Base', 'SimplePie_Cache_Base'); Cache/Psr16.php 0000644 00000006270 15162130120 0007167 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Cache; use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\InvalidArgumentException; use Throwable; /** * Caches data into a PSR-16 cache implementation * * @internal */ final class Psr16 implements DataCache { /** * PSR-16 cache implementation * * @var CacheInterface */ private $cache; /** * PSR-16 cache implementation * * @param CacheInterface $cache */ public function __construct(CacheInterface $cache) { $this->cache = $cache; } /** * Fetches a value from the cache. * * Equivalent to \Psr\SimpleCache\CacheInterface::get() * <code> * public function get(string $key, mixed $default = null): mixed; * </code> * * @param string $key The unique key of this item in the cache. * @param mixed $default Default value to return if the key does not exist. * * @return array|mixed The value of the item from the cache, or $default in case of cache miss. * * @throws InvalidArgumentException&Throwable * MUST be thrown if the $key string is not a legal value. */ public function get_data(string $key, $default = null) { $data = $this->cache->get($key, $default); if (!is_array($data) || $data === $default) { return $default; } return $data; } /** * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. * * Equivalent to \Psr\SimpleCache\CacheInterface::set() * <code> * public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool; * </code> * * @param string $key The key of the item to store. * @param array<mixed> $value The value of the item to store, must be serializable. * @param null|int $ttl Optional. The TTL value of this item. If no value is sent and * the driver supports TTL then the library may set a default value * for it or let the driver take care of that. * * @return bool True on success and false on failure. * * @throws InvalidArgumentException&Throwable * MUST be thrown if the $key string is not a legal value. */ public function set_data(string $key, array $value, ?int $ttl = null): bool { return $this->cache->set($key, $value, $ttl); } /** * Delete an item from the cache by its unique key. * * Equivalent to \Psr\SimpleCache\CacheInterface::delete() * <code> * public function delete(string $key): bool; * </code> * * @param string $key The unique cache key of the item to delete. * * @return bool True if the item was successfully removed. False if there was an error. * * @throws InvalidArgumentException&Throwable * MUST be thrown if the $key string is not a legal value. */ public function delete_data(string $key): bool { return $this->cache->delete($key); } } Cache/DataCache.php 0000644 00000005357 15162130121 0010057 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Cache; use InvalidArgumentException; /** * Subset of PSR-16 Cache client for caching data arrays * * Only get(), set() and delete() methods are used, * but not has(), getMultiple(), setMultiple() or deleteMultiple(). * * The methods names must be different, but should be compatible to the * methods of \Psr\SimpleCache\CacheInterface. * * @internal */ interface DataCache { /** * Fetches a value from the cache. * * Equivalent to \Psr\SimpleCache\CacheInterface::get() * <code> * public function get(string $key, mixed $default = null): mixed; * </code> * * @param string $key The unique key of this item in the cache. * @param mixed $default Default value to return if the key does not exist. * * @return array|mixed The value of the item from the cache, or $default in case of cache miss. * * @throws InvalidArgumentException * MUST be thrown if the $key string is not a legal value. */ public function get_data(string $key, $default = null); /** * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. * * Equivalent to \Psr\SimpleCache\CacheInterface::set() * <code> * public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool; * </code> * * @param string $key The key of the item to store. * @param array<mixed> $value The value of the item to store, must be serializable. * @param null|int $ttl Optional. The TTL value of this item. If no value is sent and * the driver supports TTL then the library may set a default value * for it or let the driver take care of that. * * @return bool True on success and false on failure. * * @throws InvalidArgumentException * MUST be thrown if the $key string is not a legal value. */ public function set_data(string $key, array $value, ?int $ttl = null): bool; /** * Delete an item from the cache by its unique key. * * Equivalent to \Psr\SimpleCache\CacheInterface::delete() * <code> * public function delete(string $key): bool; * </code> * * @param string $key The unique cache key of the item to delete. * * @return bool True if the item was successfully removed. False if there was an error. * * @throws InvalidArgumentException * MUST be thrown if the $key string is not a legal value. */ public function delete_data(string $key): bool; } Cache/Memcached.php 0000644 00000007365 15162130122 0010132 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-FileCopyrightText: 2015 Paul L. McNeely // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Cache; use Memcached as NativeMemcached; /** * Caches data to memcached * * Registered for URLs with the "memcached" protocol * * For example, `memcached://localhost:11211/?timeout=3600&prefix=sp_` will * connect to memcached on `localhost` on port 11211. All tables will be * prefixed with `sp_` and data will expire after 3600 seconds * * @uses Memcached * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead */ class Memcached implements Base { /** * NativeMemcached instance * @var NativeMemcached */ protected $cache; /** * Options * @var array<string, mixed> */ protected $options; /** * Cache name * @var string */ protected $name; /** * Create a new cache object * @param string $location Location string (from SimplePie::$cache_location) * @param string $name Unique ID for the cache * @param Base::TYPE_FEED|Base::TYPE_IMAGE $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data */ public function __construct(string $location, string $name, $type) { $this->options = [ 'host' => '127.0.0.1', 'port' => 11211, 'extras' => [ 'timeout' => 3600, // one hour 'prefix' => 'simplepie_', ], ]; $this->options = array_replace_recursive($this->options, \SimplePie\Cache::parse_URL($location)); $this->name = $this->options['extras']['prefix'] . md5("$name:$type"); $this->cache = new NativeMemcached(); $this->cache->addServer($this->options['host'], (int)$this->options['port']); } /** * Save data to the cache * @param array<mixed>|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property * @return bool Successfulness */ public function save($data) { if ($data instanceof \SimplePie\SimplePie) { $data = $data->data; } return $this->setData(serialize($data)); } /** * Retrieve the data saved to the cache * @return array<mixed>|false Data for SimplePie::$data */ public function load() { $data = $this->cache->get($this->name); if ($data !== false) { return unserialize($data); } return false; } /** * Retrieve the last modified time for the cache * @return int Timestamp */ public function mtime() { $data = $this->cache->get($this->name . '_mtime'); return (int) $data; } /** * Set the last modified time to the current time * @return bool Success status */ public function touch() { $data = $this->cache->get($this->name); return $this->setData($data); } /** * Remove the cache * @return bool Success status */ public function unlink() { return $this->cache->delete($this->name, 0); } /** * Set the last modified time and data to NativeMemcached * @param string|false $data * @return bool Success status */ private function setData($data): bool { if ($data !== false) { $this->cache->set($this->name . '_mtime', time(), (int)$this->options['extras']['timeout']); return $this->cache->set($this->name, $data, (int)$this->options['extras']['timeout']); } return false; } } class_alias('SimplePie\Cache\Memcached', 'SimplePie_Cache_Memcached'); Cache/Memcache.php 0000644 00000007200 15162130123 0007753 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Cache; use Memcache as NativeMemcache; /** * Caches data to memcache * * Registered for URLs with the "memcache" protocol * * For example, `memcache://localhost:11211/?timeout=3600&prefix=sp_` will * connect to memcache on `localhost` on port 11211. All tables will be * prefixed with `sp_` and data will expire after 3600 seconds * * @uses Memcache * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead */ class Memcache implements Base { /** * Memcache instance * * @var NativeMemcache */ protected $cache; /** * Options * * @var array<string, mixed> */ protected $options; /** * Cache name * * @var string */ protected $name; /** * Create a new cache object * * @param string $location Location string (from SimplePie::$cache_location) * @param string $name Unique ID for the cache * @param Base::TYPE_FEED|Base::TYPE_IMAGE $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data */ public function __construct(string $location, string $name, $type) { $this->options = [ 'host' => '127.0.0.1', 'port' => 11211, 'extras' => [ 'timeout' => 3600, // one hour 'prefix' => 'simplepie_', ], ]; $this->options = array_replace_recursive($this->options, \SimplePie\Cache::parse_URL($location)); $this->name = $this->options['extras']['prefix'] . md5("$name:$type"); $this->cache = new NativeMemcache(); $this->cache->addServer($this->options['host'], (int) $this->options['port']); } /** * Save data to the cache * * @param array<mixed>|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property * @return bool Successfulness */ public function save($data) { if ($data instanceof \SimplePie\SimplePie) { $data = $data->data; } return $this->cache->set($this->name, serialize($data), MEMCACHE_COMPRESSED, (int) $this->options['extras']['timeout']); } /** * Retrieve the data saved to the cache * * @return array<mixed>|false Data for SimplePie::$data */ public function load() { $data = $this->cache->get($this->name); if ($data !== false) { return unserialize($data); } return false; } /** * Retrieve the last modified time for the cache * * @return int|false Timestamp */ public function mtime() { $data = $this->cache->get($this->name); if ($data !== false) { // essentially ignore the mtime because Memcache expires on its own return time(); } return false; } /** * Set the last modified time to the current time * * @return bool Success status */ public function touch() { $data = $this->cache->get($this->name); if ($data !== false) { return $this->cache->set($this->name, $data, MEMCACHE_COMPRESSED, (int) $this->options['extras']['timeout']); } return false; } /** * Remove the cache * * @return bool Success status */ public function unlink() { return $this->cache->delete($this->name, 0); } } class_alias('SimplePie\Cache\Memcache', 'SimplePie_Cache_Memcache'); Cache/Redis.php 0000644 00000010321 15162130125 0007317 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-FileCopyrightText: 2015 Jan Kozak <galvani78@gmail.com> // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Cache; use Redis as NativeRedis; /** * Caches data to redis * * Registered for URLs with the "redis" protocol * * For example, `redis://localhost:6379/?timeout=3600&prefix=sp_&dbIndex=0` will * connect to redis on `localhost` on port 6379. All tables will be * prefixed with `simple_primary-` and data will expire after 3600 seconds * * @uses Redis * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead */ class Redis implements Base { /** * Redis instance * * @var NativeRedis */ protected $cache; /** * Options * * @var array<string, mixed> */ protected $options; /** * Cache name * * @var string */ protected $name; /** * Create a new cache object * * @param string $location Location string (from SimplePie::$cache_location) * @param string $name Unique ID for the cache * @param Base::TYPE_FEED|Base::TYPE_IMAGE|array<string, mixed>|null $options Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data */ public function __construct(string $location, string $name, $options = null) { //$this->cache = \flow\simple\cache\Redis::getRedisClientInstance(); $parsed = \SimplePie\Cache::parse_URL($location); $redis = new NativeRedis(); $redis->connect($parsed['host'], $parsed['port']); if (isset($parsed['pass'])) { $redis->auth($parsed['pass']); } if (isset($parsed['path'])) { $redis->select((int)substr($parsed['path'], 1)); } $this->cache = $redis; if (!is_null($options) && is_array($options)) { $this->options = $options; } else { $this->options = [ 'prefix' => 'rss:simple_primary:', 'expire' => 0, ]; } $this->name = $this->options['prefix'] . $name; } /** * @param NativeRedis $cache * @return void */ public function setRedisClient(NativeRedis $cache) { $this->cache = $cache; } /** * Save data to the cache * * @param array<mixed>|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property * @return bool Successfulness */ public function save($data) { if ($data instanceof \SimplePie\SimplePie) { $data = $data->data; } $response = $this->cache->set($this->name, serialize($data)); if ($this->options['expire']) { $this->cache->expire($this->name, $this->options['expire']); } return $response; } /** * Retrieve the data saved to the cache * * @return array<mixed>|false Data for SimplePie::$data */ public function load() { $data = $this->cache->get($this->name); if ($data !== false) { return unserialize($data); } return false; } /** * Retrieve the last modified time for the cache * * @return int|false Timestamp */ public function mtime() { $data = $this->cache->get($this->name); if ($data !== false) { return time(); } return false; } /** * Set the last modified time to the current time * * @return bool Success status */ public function touch() { $data = $this->cache->get($this->name); if ($data !== false) { $return = $this->cache->set($this->name, $data); if ($this->options['expire']) { return $this->cache->expire($this->name, $this->options['expire']); } return $return; } return false; } /** * Remove the cache * * @return bool Success status */ public function unlink() { return $this->cache->set($this->name, null); } } class_alias('SimplePie\Cache\Redis', 'SimplePie_Cache_Redis'); Cache/BaseDataCache.php 0000644 00000007003 15162130127 0010646 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Cache; use InvalidArgumentException; /** * Adapter for deprecated \SimplePie\Cache\Base implementations * * @internal */ final class BaseDataCache implements DataCache { /** * @var Base */ private $cache; public function __construct(Base $cache) { $this->cache = $cache; } /** * Fetches a value from the cache. * * Equivalent to \Psr\SimpleCache\CacheInterface::get() * <code> * public function get(string $key, mixed $default = null): mixed; * </code> * * @param string $key The unique key of this item in the cache. * @param mixed $default Default value to return if the key does not exist. * * @return array|mixed The value of the item from the cache, or $default in case of cache miss. * * @throws InvalidArgumentException * MUST be thrown if the $key string is not a legal value. */ public function get_data(string $key, $default = null) { $data = $this->cache->load(); if (!is_array($data)) { return $default; } // ignore data if internal cache expiration time is not set if (!array_key_exists('__cache_expiration_time', $data)) { return $default; } // ignore data if internal cache expiration time is expired if ($data['__cache_expiration_time'] < time()) { return $default; } // remove internal cache expiration time unset($data['__cache_expiration_time']); return $data; } /** * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. * * Equivalent to \Psr\SimpleCache\CacheInterface::set() * <code> * public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool; * </code> * * @param string $key The key of the item to store. * @param array<mixed> $value The value of the item to store, must be serializable. * @param null|int $ttl Optional. The TTL value of this item. If no value is sent and * the driver supports TTL then the library may set a default value * for it or let the driver take care of that. * * @return bool True on success and false on failure. * * @throws InvalidArgumentException * MUST be thrown if the $key string is not a legal value. */ public function set_data(string $key, array $value, ?int $ttl = null): bool { if ($ttl === null) { $ttl = 3600; } // place internal cache expiration time $value['__cache_expiration_time'] = time() + $ttl; return $this->cache->save($value); } /** * Delete an item from the cache by its unique key. * * Equivalent to \Psr\SimpleCache\CacheInterface::delete() * <code> * public function delete(string $key): bool; * </code> * * @param string $key The unique cache key of the item to delete. * * @return bool True if the item was successfully removed. False if there was an error. * * @throws InvalidArgumentException * MUST be thrown if the $key string is not a legal value. */ public function delete_data(string $key): bool { return $this->cache->unlink(); } } Cache/MySQL.php 0000644 00000032607 15162130131 0007226 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Cache; /** * Caches data to a MySQL database * * Registered for URLs with the "mysql" protocol * * For example, `mysql://root:password@localhost:3306/mydb?prefix=sp_` will * connect to the `mydb` database on `localhost` on port 3306, with the user * `root` and the password `password`. All tables will be prefixed with `sp_` * * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead */ class MySQL extends DB { /** * PDO instance * * @var \PDO|null */ protected $mysql; /** * Options * * @var array<string, mixed> */ protected $options; /** * Cache ID * * @var string */ protected $id; /** * Create a new cache object * * @param string $location Location string (from SimplePie::$cache_location) * @param string $name Unique ID for the cache * @param Base::TYPE_FEED|Base::TYPE_IMAGE $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data */ public function __construct(string $location, string $name, $type) { $this->options = [ 'user' => null, 'pass' => null, 'host' => '127.0.0.1', 'port' => '3306', 'path' => '', 'extras' => [ 'prefix' => '', 'cache_purge_time' => 2592000 ], ]; $this->options = array_replace_recursive($this->options, \SimplePie\Cache::parse_URL($location)); // Path is prefixed with a "/" $this->options['dbname'] = substr($this->options['path'], 1); try { $this->mysql = new \PDO("mysql:dbname={$this->options['dbname']};host={$this->options['host']};port={$this->options['port']}", $this->options['user'], $this->options['pass'], [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']); } catch (\PDOException $e) { $this->mysql = null; return; } $this->id = $name . $type; if (!$query = $this->mysql->query('SHOW TABLES')) { $this->mysql = null; return; } $db = []; while ($row = $query->fetchColumn()) { $db[] = $row; } if (!in_array($this->options['extras']['prefix'] . 'cache_data', $db)) { $query = $this->mysql->exec('CREATE TABLE `' . $this->options['extras']['prefix'] . 'cache_data` (`id` TEXT CHARACTER SET utf8 NOT NULL, `items` SMALLINT NOT NULL DEFAULT 0, `data` BLOB NOT NULL, `mtime` INT UNSIGNED NOT NULL, UNIQUE (`id`(125)))'); if ($query === false) { trigger_error("Can't create " . $this->options['extras']['prefix'] . "cache_data table, check permissions", \E_USER_WARNING); $this->mysql = null; return; } } if (!in_array($this->options['extras']['prefix'] . 'items', $db)) { $query = $this->mysql->exec('CREATE TABLE `' . $this->options['extras']['prefix'] . 'items` (`feed_id` TEXT CHARACTER SET utf8 NOT NULL, `id` TEXT CHARACTER SET utf8 NOT NULL, `data` MEDIUMBLOB NOT NULL, `posted` INT UNSIGNED NOT NULL, INDEX `feed_id` (`feed_id`(125)))'); if ($query === false) { trigger_error("Can't create " . $this->options['extras']['prefix'] . "items table, check permissions", \E_USER_WARNING); $this->mysql = null; return; } } } /** * Save data to the cache * * @param array<string>|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property * @return bool Successfulness */ public function save($data) { if ($this->mysql === null) { return false; } $query = $this->mysql->prepare('DELETE i, cd FROM `' . $this->options['extras']['prefix'] . 'cache_data` cd, ' . '`' . $this->options['extras']['prefix'] . 'items` i ' . 'WHERE cd.id = i.feed_id ' . 'AND cd.mtime < (unix_timestamp() - :purge_time)'); $query->bindValue(':purge_time', $this->options['extras']['cache_purge_time']); if (!$query->execute()) { return false; } if ($data instanceof \SimplePie\SimplePie) { $data = clone $data; $prepared = self::prepare_simplepie_object_for_cache($data); $query = $this->mysql->prepare('SELECT COUNT(*) FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :feed'); $query->bindValue(':feed', $this->id); if ($query->execute()) { if ($query->fetchColumn() > 0) { $items = count($prepared[1]); if ($items) { $sql = 'UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `items` = :items, `data` = :data, `mtime` = :time WHERE `id` = :feed'; $query = $this->mysql->prepare($sql); $query->bindValue(':items', $items); } else { $sql = 'UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `data` = :data, `mtime` = :time WHERE `id` = :feed'; $query = $this->mysql->prepare($sql); } $query->bindValue(':data', $prepared[0]); $query->bindValue(':time', time()); $query->bindValue(':feed', $this->id); if (!$query->execute()) { return false; } } else { $query = $this->mysql->prepare('INSERT INTO `' . $this->options['extras']['prefix'] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(:feed, :count, :data, :time)'); $query->bindValue(':feed', $this->id); $query->bindValue(':count', count($prepared[1])); $query->bindValue(':data', $prepared[0]); $query->bindValue(':time', time()); if (!$query->execute()) { return false; } } $ids = array_keys($prepared[1]); if (!empty($ids)) { foreach ($ids as $id) { $database_ids[] = $this->mysql->quote($id); } $query = $this->mysql->prepare('SELECT `id` FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `id` = ' . implode(' OR `id` = ', $database_ids) . ' AND `feed_id` = :feed'); $query->bindValue(':feed', $this->id); if ($query->execute()) { $existing_ids = []; while ($row = $query->fetchColumn()) { $existing_ids[] = $row; } $new_ids = array_diff($ids, $existing_ids); foreach ($new_ids as $new_id) { if (!($date = $prepared[1][$new_id]->get_date('U'))) { $date = time(); } $query = $this->mysql->prepare('INSERT INTO `' . $this->options['extras']['prefix'] . 'items` (`feed_id`, `id`, `data`, `posted`) VALUES(:feed, :id, :data, :date)'); $query->bindValue(':feed', $this->id); $query->bindValue(':id', $new_id); $query->bindValue(':data', serialize($prepared[1][$new_id]->data)); $query->bindValue(':date', $date); if (!$query->execute()) { return false; } } return true; } } else { return true; } } } else { $query = $this->mysql->prepare('SELECT `id` FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :feed'); $query->bindValue(':feed', $this->id); if ($query->execute()) { if ($query->rowCount() > 0) { $query = $this->mysql->prepare('UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `items` = 0, `data` = :data, `mtime` = :time WHERE `id` = :feed'); $query->bindValue(':data', serialize($data)); $query->bindValue(':time', time()); $query->bindValue(':feed', $this->id); if ($query->execute()) { return true; } } else { $query = $this->mysql->prepare('INSERT INTO `' . $this->options['extras']['prefix'] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(:id, 0, :data, :time)'); $query->bindValue(':id', $this->id); $query->bindValue(':data', serialize($data)); $query->bindValue(':time', time()); if ($query->execute()) { return true; } } } } return false; } /** * Retrieve the data saved to the cache * * @return array<string>|false Data for SimplePie::$data */ public function load() { if ($this->mysql === null) { return false; } $query = $this->mysql->prepare('SELECT `items`, `data` FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id'); $query->bindValue(':id', $this->id); if ($query->execute() && ($row = $query->fetch())) { $data = unserialize($row[1]); if (isset($this->options['items'][0])) { $items = (int) $this->options['items'][0]; } else { $items = (int) $row[0]; } if ($items !== 0) { if (isset($data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['feed'][0])) { $feed = &$data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['feed'][0]; } elseif (isset($data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['feed'][0])) { $feed = &$data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['feed'][0]; } elseif (isset($data['child'][\SimplePie\SimplePie::NAMESPACE_RDF]['RDF'][0])) { $feed = &$data['child'][\SimplePie\SimplePie::NAMESPACE_RDF]['RDF'][0]; } elseif (isset($data['child'][\SimplePie\SimplePie::NAMESPACE_RSS_20]['rss'][0])) { $feed = &$data['child'][\SimplePie\SimplePie::NAMESPACE_RSS_20]['rss'][0]; } else { $feed = null; } if ($feed !== null) { $sql = 'SELECT `data` FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `feed_id` = :feed ORDER BY `posted` DESC'; if ($items > 0) { $sql .= ' LIMIT ' . $items; } $query = $this->mysql->prepare($sql); $query->bindValue(':feed', $this->id); if ($query->execute()) { while ($row = $query->fetchColumn()) { $feed['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['entry'][] = unserialize((string) $row); } } else { return false; } } } return $data; } return false; } /** * Retrieve the last modified time for the cache * * @return int|false Timestamp */ public function mtime() { if ($this->mysql === null) { return false; } $query = $this->mysql->prepare('SELECT `mtime` FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id'); $query->bindValue(':id', $this->id); if ($query->execute() && ($time = $query->fetchColumn())) { return (int) $time; } return false; } /** * Set the last modified time to the current time * * @return bool Success status */ public function touch() { if ($this->mysql === null) { return false; } $query = $this->mysql->prepare('UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `mtime` = :time WHERE `id` = :id'); $query->bindValue(':time', time()); $query->bindValue(':id', $this->id); return $query->execute() && $query->rowCount() > 0; } /** * Remove the cache * * @return bool Success status */ public function unlink() { if ($this->mysql === null) { return false; } $query = $this->mysql->prepare('DELETE FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id'); $query->bindValue(':id', $this->id); $query2 = $this->mysql->prepare('DELETE FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `feed_id` = :id'); $query2->bindValue(':id', $this->id); return $query->execute() && $query2->execute(); } } class_alias('SimplePie\Cache\MySQL', 'SimplePie_Cache_MySQL'); Cache/DB.php 0000644 00000007171 15162130133 0006546 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Cache; use SimplePie\Item; /** * Base class for database-based caches * * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead */ abstract class DB implements Base { /** * Helper for database conversion * * Converts a given {@see SimplePie} object into data to be stored * * @param \SimplePie\SimplePie $data * @return array{string, array<string, Item>} First item is the serialized data for storage, second item is the unique ID for this item */ protected static function prepare_simplepie_object_for_cache(\SimplePie\SimplePie $data) { $items = $data->get_items(); $items_by_id = []; if (!empty($items)) { foreach ($items as $item) { $items_by_id[$item->get_id()] = $item; } if (count($items_by_id) !== count($items)) { $items_by_id = []; foreach ($items as $item) { $items_by_id[$item->get_id(true)] = $item; } } if (isset($data->data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['feed'][0])) { $channel = &$data->data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['feed'][0]; } elseif (isset($data->data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['feed'][0])) { $channel = &$data->data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['feed'][0]; } elseif (isset($data->data['child'][\SimplePie\SimplePie::NAMESPACE_RDF]['RDF'][0])) { $channel = &$data->data['child'][\SimplePie\SimplePie::NAMESPACE_RDF]['RDF'][0]; } elseif (isset($data->data['child'][\SimplePie\SimplePie::NAMESPACE_RSS_20]['rss'][0]['child'][\SimplePie\SimplePie::NAMESPACE_RSS_20]['channel'][0])) { $channel = &$data->data['child'][\SimplePie\SimplePie::NAMESPACE_RSS_20]['rss'][0]['child'][\SimplePie\SimplePie::NAMESPACE_RSS_20]['channel'][0]; } else { $channel = null; } if ($channel !== null) { if (isset($channel['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['entry'])) { unset($channel['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['entry']); } if (isset($channel['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['entry'])) { unset($channel['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['entry']); } if (isset($channel['child'][\SimplePie\SimplePie::NAMESPACE_RSS_10]['item'])) { unset($channel['child'][\SimplePie\SimplePie::NAMESPACE_RSS_10]['item']); } if (isset($channel['child'][\SimplePie\SimplePie::NAMESPACE_RSS_090]['item'])) { unset($channel['child'][\SimplePie\SimplePie::NAMESPACE_RSS_090]['item']); } if (isset($channel['child'][\SimplePie\SimplePie::NAMESPACE_RSS_20]['item'])) { unset($channel['child'][\SimplePie\SimplePie::NAMESPACE_RSS_20]['item']); } } if (isset($data->data['items'])) { unset($data->data['items']); } if (isset($data->data['ordered_items'])) { unset($data->data['ordered_items']); } } return [serialize($data->data), $items_by_id]; } } class_alias('SimplePie\Cache\DB', 'SimplePie_Cache_DB'); Cache/CallableNameFilter.php 0000644 00000002756 15162130135 0011735 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Cache; /** * Creating a cache filename with callables */ final class CallableNameFilter implements NameFilter { /** * @var callable(string): string */ private $callable; /** * @param callable(string): string $callable */ public function __construct(callable $callable) { $this->callable = $callable; } /** * Method to create cache filename with. * * The returning name MUST follow the rules for keys in PSR-16. * * @link https://www.php-fig.org/psr/psr-16/ * * The returning name MUST be a string of at least one character * that uniquely identifies a cached item, MUST only contain the * characters A-Z, a-z, 0-9, _, and . in any order in UTF-8 encoding * and MUST not longer then 64 characters. The following characters * are reserved for future extensions and MUST NOT be used: {}()/\@: * * A provided implementing library MAY support additional characters * and encodings or longer lengths, but MUST support at least that * minimum. * * @param string $name The name for the cache will be most likely an url with query string * * @return string the new cache name */ public function filter(string $name): string { return call_user_func($this->callable, $name); } } Cache/error_log 0000644 00000045426 15162130137 0007476 0 ustar 00 [24-Mar-2026 21:48:17 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\DataCache" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/BaseDataCache.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/BaseDataCache.php on line 17 [24-Mar-2026 21:48:33 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php:15 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php on line 15 [24-Mar-2026 21:51:08 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Cache\DB" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php:21 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php on line 21 [24-Mar-2026 21:51:19 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\NameFilter" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php:13 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php on line 13 [24-Mar-2026 21:51:24 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/DB.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/DB.php on line 17 [24-Mar-2026 21:51:39 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\DataCache" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php:19 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php on line 19 [24-Mar-2026 21:54:14 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcache.php:24 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcache.php on line 24 [24-Mar-2026 21:55:05 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php:25 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php on line 25 [24-Mar-2026 21:55:26 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcached.php:25 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcached.php on line 25 [25-Mar-2026 04:47:33 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\DataCache" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php:19 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php on line 19 [25-Mar-2026 04:47:43 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Cache\DB" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php:21 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php on line 21 [25-Mar-2026 04:50:08 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\NameFilter" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php:13 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php on line 13 [25-Mar-2026 05:27:37 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php:25 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php on line 25 [25-Mar-2026 05:30:52 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php:15 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php on line 15 [25-Mar-2026 06:37:01 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/DB.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/DB.php on line 17 [25-Mar-2026 06:38:27 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcached.php:25 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcached.php on line 25 [25-Mar-2026 06:44:50 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\DataCache" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/BaseDataCache.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/BaseDataCache.php on line 17 [25-Mar-2026 06:58:51 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcache.php:24 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcache.php on line 24 [25-Mar-2026 12:32:14 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\DataCache" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php:19 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php on line 19 [25-Mar-2026 12:32:50 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Cache\DB" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php:21 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php on line 21 [25-Mar-2026 12:33:55 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\NameFilter" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php:13 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php on line 13 [25-Mar-2026 12:34:55 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php:25 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php on line 25 [25-Mar-2026 12:35:59 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php:15 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php on line 15 [26-Mar-2026 09:03:05 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\DataCache" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/BaseDataCache.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/BaseDataCache.php on line 17 [26-Mar-2026 09:03:08 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\NameFilter" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php:13 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php on line 13 [26-Mar-2026 09:03:10 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/DB.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/DB.php on line 17 [26-Mar-2026 09:03:14 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php:15 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php on line 15 [26-Mar-2026 09:03:19 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcache.php:24 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcache.php on line 24 [26-Mar-2026 09:05:44 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\DataCache" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php:19 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php on line 19 [26-Mar-2026 09:10:59 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\DataCache" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/BaseDataCache.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/BaseDataCache.php on line 17 [26-Mar-2026 09:11:19 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php:15 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php on line 15 [26-Mar-2026 09:13:05 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php:25 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php on line 25 [26-Mar-2026 09:16:46 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\NameFilter" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php:13 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php on line 13 [26-Mar-2026 09:27:09 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/DB.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/DB.php on line 17 [26-Mar-2026 10:41:09 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcache.php:24 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcache.php on line 24 [26-Mar-2026 10:41:54 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcached.php:25 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcached.php on line 25 [26-Mar-2026 10:48:55 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Cache\DB" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php:21 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php on line 21 [26-Mar-2026 11:23:12 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcached.php:25 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcached.php on line 25 [26-Mar-2026 11:23:13 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Cache\DB" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php:21 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php on line 21 [26-Mar-2026 11:23:17 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\DataCache" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php:19 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php on line 19 [26-Mar-2026 11:23:18 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php:25 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php on line 25 [26-Mar-2026 15:48:45 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\DataCache" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php:19 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php on line 19 [26-Mar-2026 15:48:55 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Cache\DB" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php:21 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php on line 21 [26-Mar-2026 15:50:30 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\NameFilter" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php:13 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php on line 13 [26-Mar-2026 16:27:30 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php:25 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php on line 25 [26-Mar-2026 16:33:41 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php:15 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php on line 15 [26-Mar-2026 17:39:01 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/DB.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/DB.php on line 17 [26-Mar-2026 17:39:23 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcached.php:25 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcached.php on line 25 [26-Mar-2026 17:45:49 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\DataCache" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/BaseDataCache.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/BaseDataCache.php on line 17 [26-Mar-2026 17:59:55 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcache.php:24 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcache.php on line 24 [26-Mar-2026 22:58:51 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\DataCache" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php:19 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php on line 19 [26-Mar-2026 22:59:06 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Cache\DB" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php:21 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php on line 21 [26-Mar-2026 22:59:51 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\NameFilter" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php:13 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php on line 13 [26-Mar-2026 23:00:54 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php:25 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php on line 25 [26-Mar-2026 23:01:57 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php:15 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php on line 15 [28-Mar-2026 09:14:29 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\NameFilter" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php:13 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/CallableNameFilter.php on line 13 [28-Mar-2026 09:24:35 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcache.php:24 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcache.php on line 24 [28-Mar-2026 09:26:00 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php:15 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/File.php on line 15 [28-Mar-2026 09:26:20 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Cache\DB" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php:21 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/MySQL.php on line 21 [28-Mar-2026 09:37:57 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcached.php:25 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Memcached.php on line 25 [28-Mar-2026 09:38:12 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php:25 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Redis.php on line 25 [28-Mar-2026 09:38:18 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\DataCache" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php:19 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/Psr16.php on line 19 [28-Mar-2026 09:52:08 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\DataCache" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/BaseDataCache.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/BaseDataCache.php on line 17 [28-Mar-2026 11:12:03 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\Cache\Base" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/DB.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/Cache/DB.php on line 17 Cache/File.php 0000644 00000005641 15162130141 0007137 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Cache; /** * Caches data to the filesystem * * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead */ class File implements Base { /** * Location string * * @see SimplePie::$cache_location * @var string */ protected $location; /** * Filename * * @var string */ protected $filename; /** * File extension * * @var string */ protected $extension; /** * File path * * @var string */ protected $name; /** * Create a new cache object * * @param string $location Location string (from SimplePie::$cache_location) * @param string $name Unique ID for the cache * @param Base::TYPE_FEED|Base::TYPE_IMAGE $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data */ public function __construct(string $location, string $name, $type) { $this->location = $location; $this->filename = $name; $this->extension = $type; $this->name = "$this->location/$this->filename.$this->extension"; } /** * Save data to the cache * * @param array<mixed>|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property * @return bool Successfulness */ public function save($data) { if (file_exists($this->name) && is_writable($this->name) || file_exists($this->location) && is_writable($this->location)) { if ($data instanceof \SimplePie\SimplePie) { $data = $data->data; } $data = serialize($data); return (bool) file_put_contents($this->name, $data); } return false; } /** * Retrieve the data saved to the cache * * @return array<mixed>|false Data for SimplePie::$data */ public function load() { if (file_exists($this->name) && is_readable($this->name)) { return unserialize((string) file_get_contents($this->name)); } return false; } /** * Retrieve the last modified time for the cache * * @return int|false Timestamp */ public function mtime() { return @filemtime($this->name); } /** * Set the last modified time to the current time * * @return bool Success status */ public function touch() { return @touch($this->name); } /** * Remove the cache * * @return bool Success status */ public function unlink() { if (file_exists($this->name)) { return unlink($this->name); } return false; } } class_alias('SimplePie\Cache\File', 'SimplePie_Cache_File'); Cache/NameFilter.php 0000644 00000002227 15162130143 0010305 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Cache; /** * Interface for creating a cache filename */ interface NameFilter { /** * Method to create cache filename with. * * The returning name MUST follow the rules for keys in PSR-16. * * @link https://www.php-fig.org/psr/psr-16/ * * The returning name MUST be a string of at least one character * that uniquely identifies a cached item, MUST only contain the * characters A-Z, a-z, 0-9, _, and . in any order in UTF-8 encoding * and MUST not longer then 64 characters. The following characters * are reserved for future extensions and MUST NOT be used: {}()/\@: * * A provided implementing library MAY support additional characters * and encodings or longer lengths, but MUST support at least that * minimum. * * @param string $name The name for the cache will be most likely an url with query string * * @return string the new cache name */ public function filter(string $name): string; } SimplePie.php 0000644 00000372064 15162130145 0007156 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; use InvalidArgumentException; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\UriFactoryInterface; use Psr\SimpleCache\CacheInterface; use SimplePie\Cache\Base; use SimplePie\Cache\BaseDataCache; use SimplePie\Cache\CallableNameFilter; use SimplePie\Cache\DataCache; use SimplePie\Cache\NameFilter; use SimplePie\Cache\Psr16; use SimplePie\Content\Type\Sniffer; use SimplePie\Exception as SimplePieException; use SimplePie\HTTP\Client; use SimplePie\HTTP\ClientException; use SimplePie\HTTP\FileClient; use SimplePie\HTTP\Psr18Client; use SimplePie\HTTP\Response; /** * SimplePie */ class SimplePie { /** * SimplePie Name */ public const NAME = 'SimplePie'; /** * SimplePie Version */ public const VERSION = '1.9.0'; /** * SimplePie Website URL */ public const URL = 'http://simplepie.org'; /** * SimplePie Linkback */ public const LINKBACK = '<a href="' . self::URL . '" title="' . self::NAME . ' ' . self::VERSION . '">' . self::NAME . '</a>'; /** * No Autodiscovery * @see SimplePie::set_autodiscovery_level() */ public const LOCATOR_NONE = 0; /** * Feed Link Element Autodiscovery * @see SimplePie::set_autodiscovery_level() */ public const LOCATOR_AUTODISCOVERY = 1; /** * Local Feed Extension Autodiscovery * @see SimplePie::set_autodiscovery_level() */ public const LOCATOR_LOCAL_EXTENSION = 2; /** * Local Feed Body Autodiscovery * @see SimplePie::set_autodiscovery_level() */ public const LOCATOR_LOCAL_BODY = 4; /** * Remote Feed Extension Autodiscovery * @see SimplePie::set_autodiscovery_level() */ public const LOCATOR_REMOTE_EXTENSION = 8; /** * Remote Feed Body Autodiscovery * @see SimplePie::set_autodiscovery_level() */ public const LOCATOR_REMOTE_BODY = 16; /** * All Feed Autodiscovery * @see SimplePie::set_autodiscovery_level() */ public const LOCATOR_ALL = 31; /** * No known feed type */ public const TYPE_NONE = 0; /** * RSS 0.90 */ public const TYPE_RSS_090 = 1; /** * RSS 0.91 (Netscape) */ public const TYPE_RSS_091_NETSCAPE = 2; /** * RSS 0.91 (Userland) */ public const TYPE_RSS_091_USERLAND = 4; /** * RSS 0.91 (both Netscape and Userland) */ public const TYPE_RSS_091 = 6; /** * RSS 0.92 */ public const TYPE_RSS_092 = 8; /** * RSS 0.93 */ public const TYPE_RSS_093 = 16; /** * RSS 0.94 */ public const TYPE_RSS_094 = 32; /** * RSS 1.0 */ public const TYPE_RSS_10 = 64; /** * RSS 2.0 */ public const TYPE_RSS_20 = 128; /** * RDF-based RSS */ public const TYPE_RSS_RDF = 65; /** * Non-RDF-based RSS (truly intended as syndication format) */ public const TYPE_RSS_SYNDICATION = 190; /** * All RSS */ public const TYPE_RSS_ALL = 255; /** * Atom 0.3 */ public const TYPE_ATOM_03 = 256; /** * Atom 1.0 */ public const TYPE_ATOM_10 = 512; /** * All Atom */ public const TYPE_ATOM_ALL = 768; /** * All feed types */ public const TYPE_ALL = 1023; /** * No construct */ public const CONSTRUCT_NONE = 0; /** * Text construct */ public const CONSTRUCT_TEXT = 1; /** * HTML construct */ public const CONSTRUCT_HTML = 2; /** * XHTML construct */ public const CONSTRUCT_XHTML = 4; /** * base64-encoded construct */ public const CONSTRUCT_BASE64 = 8; /** * IRI construct */ public const CONSTRUCT_IRI = 16; /** * A construct that might be HTML */ public const CONSTRUCT_MAYBE_HTML = 32; /** * All constructs */ public const CONSTRUCT_ALL = 63; /** * Don't change case */ public const SAME_CASE = 1; /** * Change to lowercase */ public const LOWERCASE = 2; /** * Change to uppercase */ public const UPPERCASE = 4; /** * PCRE for HTML attributes */ public const PCRE_HTML_ATTRIBUTE = '((?:[\x09\x0A\x0B\x0C\x0D\x20]+[^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3D\x3E]*(?:[\x09\x0A\x0B\x0C\x0D\x20]*=[\x09\x0A\x0B\x0C\x0D\x20]*(?:"(?:[^"]*)"|\'(?:[^\']*)\'|(?:[^\x09\x0A\x0B\x0C\x0D\x20\x22\x27\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x3E]*)?))?)*)[\x09\x0A\x0B\x0C\x0D\x20]*'; /** * PCRE for XML attributes */ public const PCRE_XML_ATTRIBUTE = '((?:\s+(?:(?:[^\s:]+:)?[^\s:]+)\s*=\s*(?:"(?:[^"]*)"|\'(?:[^\']*)\'))*)\s*'; /** * XML Namespace */ public const NAMESPACE_XML = 'http://www.w3.org/XML/1998/namespace'; /** * Atom 1.0 Namespace */ public const NAMESPACE_ATOM_10 = 'http://www.w3.org/2005/Atom'; /** * Atom 0.3 Namespace */ public const NAMESPACE_ATOM_03 = 'http://purl.org/atom/ns#'; /** * RDF Namespace */ public const NAMESPACE_RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; /** * RSS 0.90 Namespace */ public const NAMESPACE_RSS_090 = 'http://my.netscape.com/rdf/simple/0.9/'; /** * RSS 1.0 Namespace */ public const NAMESPACE_RSS_10 = 'http://purl.org/rss/1.0/'; /** * RSS 1.0 Content Module Namespace */ public const NAMESPACE_RSS_10_MODULES_CONTENT = 'http://purl.org/rss/1.0/modules/content/'; /** * RSS 2.0 Namespace * (Stupid, I know, but I'm certain it will confuse people less with support.) */ public const NAMESPACE_RSS_20 = ''; /** * DC 1.0 Namespace */ public const NAMESPACE_DC_10 = 'http://purl.org/dc/elements/1.0/'; /** * DC 1.1 Namespace */ public const NAMESPACE_DC_11 = 'http://purl.org/dc/elements/1.1/'; /** * W3C Basic Geo (WGS84 lat/long) Vocabulary Namespace */ public const NAMESPACE_W3C_BASIC_GEO = 'http://www.w3.org/2003/01/geo/wgs84_pos#'; /** * GeoRSS Namespace */ public const NAMESPACE_GEORSS = 'http://www.georss.org/georss'; /** * Media RSS Namespace */ public const NAMESPACE_MEDIARSS = 'http://search.yahoo.com/mrss/'; /** * Wrong Media RSS Namespace. Caused by a long-standing typo in the spec. */ public const NAMESPACE_MEDIARSS_WRONG = 'http://search.yahoo.com/mrss'; /** * Wrong Media RSS Namespace #2. New namespace introduced in Media RSS 1.5. */ public const NAMESPACE_MEDIARSS_WRONG2 = 'http://video.search.yahoo.com/mrss'; /** * Wrong Media RSS Namespace #3. A possible typo of the Media RSS 1.5 namespace. */ public const NAMESPACE_MEDIARSS_WRONG3 = 'http://video.search.yahoo.com/mrss/'; /** * Wrong Media RSS Namespace #4. New spec location after the RSS Advisory Board takes it over, but not a valid namespace. */ public const NAMESPACE_MEDIARSS_WRONG4 = 'http://www.rssboard.org/media-rss'; /** * Wrong Media RSS Namespace #5. A possible typo of the RSS Advisory Board URL. */ public const NAMESPACE_MEDIARSS_WRONG5 = 'http://www.rssboard.org/media-rss/'; /** * iTunes RSS Namespace */ public const NAMESPACE_ITUNES = 'http://www.itunes.com/dtds/podcast-1.0.dtd'; /** * XHTML Namespace */ public const NAMESPACE_XHTML = 'http://www.w3.org/1999/xhtml'; /** * IANA Link Relations Registry */ public const IANA_LINK_RELATIONS_REGISTRY = 'http://www.iana.org/assignments/relation/'; /** * No file source */ public const FILE_SOURCE_NONE = 0; /** * Remote file source */ public const FILE_SOURCE_REMOTE = 1; /** * Local file source */ public const FILE_SOURCE_LOCAL = 2; /** * fsockopen() file source */ public const FILE_SOURCE_FSOCKOPEN = 4; /** * cURL file source */ public const FILE_SOURCE_CURL = 8; /** * file_get_contents() file source */ public const FILE_SOURCE_FILE_GET_CONTENTS = 16; /** * @internal Default value of the HTTP Accept header when fetching/locating feeds */ public const DEFAULT_HTTP_ACCEPT_HEADER = 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1'; /** * @var array<string, mixed> Raw data * @access private */ public $data = []; /** * @var string|string[]|null Error string (or array when multiple feeds are initialized) * @access private */ public $error = null; /** * @var int HTTP status code * @see SimplePie::status_code() * @access private */ public $status_code = 0; /** * @var Sanitize instance of Sanitize class * @see SimplePie::set_sanitize_class() * @access private */ public $sanitize; /** * @var string SimplePie Useragent * @see SimplePie::set_useragent() * @access private */ public $useragent = ''; /** * @var string Feed URL * @see SimplePie::set_feed_url() * @access private */ public $feed_url; /** * @var ?string Original feed URL, or new feed URL iff HTTP 301 Moved Permanently * @see SimplePie::subscribe_url() * @access private */ public $permanent_url = null; /** * @var File Instance of File class to use as a feed * @see SimplePie::set_file() */ private $file; /** * @var string|false Raw feed data * @see SimplePie::set_raw_data() * @access private */ public $raw_data; /** * @var int Timeout for fetching remote files * @see SimplePie::set_timeout() * @access private */ public $timeout = 10; /** * @var array<int, mixed> Custom curl options * @see SimplePie::set_curl_options() * @access private */ public $curl_options = []; /** * @var bool Forces fsockopen() to be used for remote files instead * of cURL, even if a new enough version is installed * @see SimplePie::force_fsockopen() * @access private */ public $force_fsockopen = false; /** * @var bool Force the given data/URL to be treated as a feed no matter what * it appears like * @see SimplePie::force_feed() * @access private */ public $force_feed = false; /** * @var bool Enable/Disable Caching * @see SimplePie::enable_cache() * @access private */ private $enable_cache = true; /** * @var DataCache|null * @see SimplePie::set_cache() */ private $cache = null; /** * @var NameFilter * @see SimplePie::set_cache_namefilter() */ private $cache_namefilter; /** * @var bool Force SimplePie to fallback to expired cache, if enabled, * when feed is unavailable. * @see SimplePie::force_cache_fallback() * @access private */ public $force_cache_fallback = false; /** * @var int Cache duration (in seconds) * @see SimplePie::set_cache_duration() * @access private */ public $cache_duration = 3600; /** * @var int Auto-discovery cache duration (in seconds) * @see SimplePie::set_autodiscovery_cache_duration() * @access private */ public $autodiscovery_cache_duration = 604800; // 7 Days. /** * @var string Cache location (relative to executing script) * @see SimplePie::set_cache_location() * @access private */ public $cache_location = './cache'; /** * @var string&(callable(string): string) Function that creates the cache filename * @see SimplePie::set_cache_name_function() * @access private */ public $cache_name_function = 'md5'; /** * @var bool Reorder feed by date descending * @see SimplePie::enable_order_by_date() * @access private */ public $order_by_date = true; /** * @var mixed Force input encoding to be set to the follow value * (false, or anything type-cast to false, disables this feature) * @see SimplePie::set_input_encoding() * @access private */ public $input_encoding = false; /** * @var self::LOCATOR_* Feed Autodiscovery Level * @see SimplePie::set_autodiscovery_level() * @access private */ public $autodiscovery = self::LOCATOR_ALL; /** * Class registry object * * @var Registry */ public $registry; /** * @var int Maximum number of feeds to check with autodiscovery * @see SimplePie::set_max_checked_feeds() * @access private */ public $max_checked_feeds = 10; /** * @var array<Response>|null All the feeds found during the autodiscovery process * @see SimplePie::get_all_discovered_feeds() * @access private */ public $all_discovered_feeds = []; /** * @var string Web-accessible path to the handler_image.php file. * @see SimplePie::set_image_handler() * @access private */ public $image_handler = ''; /** * @var array<string> Stores the URLs when multiple feeds are being initialized. * @see SimplePie::set_feed_url() * @access private */ public $multifeed_url = []; /** * @var array<int, static> Stores SimplePie objects when multiple feeds initialized. * @access private */ public $multifeed_objects = []; /** * @var array<mixed> Stores the get_object_vars() array for use with multifeeds. * @see SimplePie::set_feed_url() * @access private */ public $config_settings = null; /** * @var int Stores the number of items to return per-feed with multifeeds. * @see SimplePie::set_item_limit() * @access private */ public $item_limit = 0; /** * @var bool Stores if last-modified and/or etag headers were sent with the * request when checking a feed. */ public $check_modified = false; /** * @var array<string> Stores the default attributes to be stripped by strip_attributes(). * @see SimplePie::strip_attributes() * @access private */ public $strip_attributes = ['bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc']; /** * @var array<string, array<string, string>> Stores the default attributes to add to different tags by add_attributes(). * @see SimplePie::add_attributes() * @access private */ public $add_attributes = ['audio' => ['preload' => 'none'], 'iframe' => ['sandbox' => 'allow-scripts allow-same-origin'], 'video' => ['preload' => 'none']]; /** * @var array<string> Stores the default tags to be stripped by strip_htmltags(). * @see SimplePie::strip_htmltags() * @access private */ public $strip_htmltags = ['base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style']; /** * @var string[]|string Stores the default attributes to be renamed by rename_attributes(). * @see SimplePie::rename_attributes() * @access private */ public $rename_attributes = []; /** * @var bool Should we throw exceptions, or use the old-style error property? * @access private */ public $enable_exceptions = false; /** * @var Client|null */ private $http_client = null; /** @var bool Whether HTTP client has been injected */ private $http_client_injected = false; /** * The SimplePie class contains feed level data and options * * To use SimplePie, create the SimplePie object with no parameters. You can * then set configuration options using the provided methods. After setting * them, you must initialise the feed using $feed->init(). At that point the * object's methods and properties will be available to you. * * Previously, it was possible to pass in the feed URL along with cache * options directly into the constructor. This has been removed as of 1.3 as * it caused a lot of confusion. * * @since 1.0 Preview Release */ public function __construct() { if (version_compare(PHP_VERSION, '7.2', '<')) { exit('Please upgrade to PHP 7.2 or newer.'); } $this->set_useragent(); $this->set_cache_namefilter(new CallableNameFilter($this->cache_name_function)); // Other objects, instances created here so we can set options on them $this->sanitize = new Sanitize(); $this->registry = new Registry(); if (func_num_args() > 0) { trigger_error('Passing parameters to the constructor is no longer supported. Please use set_feed_url(), set_cache_location(), and set_cache_duration() directly.', \E_USER_DEPRECATED); $args = func_get_args(); switch (count($args)) { case 3: $this->set_cache_duration($args[2]); // no break case 2: $this->set_cache_location($args[1]); // no break case 1: $this->set_feed_url($args[0]); $this->init(); } } } /** * Used for converting object to a string * @return string */ public function __toString() { return md5(serialize($this->data)); } /** * Remove items that link back to this before destroying this object * @return void */ public function __destruct() { if (!gc_enabled()) { if (!empty($this->data['items'])) { foreach ($this->data['items'] as $item) { $item->__destruct(); } unset($item, $this->data['items']); } if (!empty($this->data['ordered_items'])) { foreach ($this->data['ordered_items'] as $item) { $item->__destruct(); } unset($item, $this->data['ordered_items']); } } } /** * Force the given data/URL to be treated as a feed * * This tells SimplePie to ignore the content-type provided by the server. * Be careful when using this option, as it will also disable autodiscovery. * * @since 1.1 * @param bool $enable Force the given data/URL to be treated as a feed * @return void */ public function force_feed(bool $enable = false) { $this->force_feed = $enable; } /** * Set the URL of the feed you want to parse * * This allows you to enter the URL of the feed you want to parse, or the * website you want to try to use auto-discovery on. This takes priority * over any set raw data. * * Deprecated since 1.9.0: You can set multiple feeds to mash together by passing an array instead * of a string for the $url. Remember that with each additional feed comes * additional processing and resources. * * @since 1.0 Preview Release * @see set_raw_data() * @param string|string[] $url This is the URL (or (deprecated) array of URLs) that you want to parse. * @return void */ public function set_feed_url($url) { $this->multifeed_url = []; if (is_array($url)) { trigger_error('Fetching multiple feeds with single SimplePie instance is deprecated since SimplePie 1.9.0, create one SimplePie instance per feed and use SimplePie::merge_items to get a single list of items.', \E_USER_DEPRECATED); foreach ($url as $value) { $this->multifeed_url[] = $this->registry->call(Misc::class, 'fix_protocol', [$value, 1]); } } else { $this->feed_url = $this->registry->call(Misc::class, 'fix_protocol', [$url, 1]); $this->permanent_url = $this->feed_url; } } /** * Set an instance of {@see File} to use as a feed * * @deprecated since SimplePie 1.9.0, use \SimplePie\SimplePie::set_http_client() or \SimplePie\SimplePie::set_raw_data() instead. * * @param File &$file * @return bool True on success, false on failure */ public function set_file(File &$file) { // trigger_error(sprintf('SimplePie\SimplePie::set_file() is deprecated since SimplePie 1.9.0, please use "SimplePie\SimplePie::set_http_client()" or "SimplePie\SimplePie::set_raw_data()" instead.'), \E_USER_DEPRECATED); $this->feed_url = $file->get_final_requested_uri(); $this->permanent_url = $this->feed_url; $this->file = &$file; return true; } /** * Set the raw XML data to parse * * Allows you to use a string of RSS/Atom data instead of a remote feed. * * If you have a feed available as a string in PHP, you can tell SimplePie * to parse that data string instead of a remote feed. Any set feed URL * takes precedence. * * @since 1.0 Beta 3 * @param string $data RSS or Atom data as a string. * @see set_feed_url() * @return void */ public function set_raw_data(string $data) { $this->raw_data = $data; } /** * Set a PSR-18 client and PSR-17 factories * * Allows you to use your own HTTP client implementations. * This will become required with SimplePie 2.0.0. */ final public function set_http_client( ClientInterface $http_client, RequestFactoryInterface $request_factory, UriFactoryInterface $uri_factory ): void { $this->http_client = new Psr18Client($http_client, $request_factory, $uri_factory); } /** * Set the default timeout for fetching remote feeds * * This allows you to change the maximum time the feed's server to respond * and send the feed back. * * @since 1.0 Beta 3 * @param int $timeout The maximum number of seconds to spend waiting to retrieve a feed. * @return void */ public function set_timeout(int $timeout = 10) { if ($this->http_client_injected) { throw new SimplePieException(sprintf( 'Using "%s()" has no effect, because you already provided a HTTP client with "%s::set_http_client()". Configure timeout in your HTTP client instead.', __METHOD__, self::class )); } $this->timeout = (int) $timeout; // Reset a possible existing FileClient, // so a new client with the changed value will be created if (is_object($this->http_client) && $this->http_client instanceof FileClient) { $this->http_client = null; } elseif (is_object($this->http_client)) { // Trigger notice if a PSR-18 client was set trigger_error(sprintf( 'Using "%s()" has no effect, because you already provided a HTTP client with "%s::set_http_client()". Configure the timeout in your HTTP client instead.', __METHOD__, get_class($this) ), \E_USER_NOTICE); } } /** * Set custom curl options * * This allows you to change default curl options * * @since 1.0 Beta 3 * @param array<int, mixed> $curl_options Curl options to add to default settings * @return void */ public function set_curl_options(array $curl_options = []) { if ($this->http_client_injected) { throw new SimplePieException(sprintf( 'Using "%s()" has no effect, because you already provided a HTTP client with "%s::set_http_client()". Configure custom curl options in your HTTP client instead.', __METHOD__, self::class )); } $this->curl_options = $curl_options; // Reset a possible existing FileClient, // so a new client with the changed value will be created if (is_object($this->http_client) && $this->http_client instanceof FileClient) { $this->http_client = null; } elseif (is_object($this->http_client)) { // Trigger notice if a PSR-18 client was set trigger_error(sprintf( 'Using "%s()" has no effect, because you already provided a HTTP client with "%s::set_http_client()". Configure the curl options in your HTTP client instead.', __METHOD__, get_class($this) ), \E_USER_NOTICE); } } /** * Force SimplePie to use fsockopen() instead of cURL * * @since 1.0 Beta 3 * @param bool $enable Force fsockopen() to be used * @return void */ public function force_fsockopen(bool $enable = false) { if ($this->http_client_injected) { throw new SimplePieException(sprintf( 'Using "%s()" has no effect, because you already provided a HTTP client with "%s::set_http_client()". Configure fsockopen in your HTTP client instead.', __METHOD__, self::class )); } $this->force_fsockopen = $enable; // Reset a possible existing FileClient, // so a new client with the changed value will be created if (is_object($this->http_client) && $this->http_client instanceof FileClient) { $this->http_client = null; } elseif (is_object($this->http_client)) { // Trigger notice if a PSR-18 client was set trigger_error(sprintf( 'Using "%s()" has no effect, because you already provided a HTTP client with "%s::set_http_client()". Configure fsockopen in your HTTP client instead.', __METHOD__, get_class($this) ), \E_USER_NOTICE); } } /** * Enable/disable caching in SimplePie. * * This option allows you to disable caching all-together in SimplePie. * However, disabling the cache can lead to longer load times. * * @since 1.0 Preview Release * @param bool $enable Enable caching * @return void */ public function enable_cache(bool $enable = true) { $this->enable_cache = $enable; } /** * Set a PSR-16 implementation as cache * * @param CacheInterface $cache The PSR-16 cache implementation * * @return void */ public function set_cache(CacheInterface $cache) { $this->cache = new Psr16($cache); } /** * SimplePie to continue to fall back to expired cache, if enabled, when * feed is unavailable. * * This tells SimplePie to ignore any file errors and fall back to cache * instead. This only works if caching is enabled and cached content * still exists. * * @deprecated since SimplePie 1.8.0, expired cache will not be used anymore. * * @param bool $enable Force use of cache on fail. * @return void */ public function force_cache_fallback(bool $enable = false) { // @trigger_error(sprintf('SimplePie\SimplePie::force_cache_fallback() is deprecated since SimplePie 1.8.0, expired cache will not be used anymore.'), \E_USER_DEPRECATED); $this->force_cache_fallback = $enable; } /** * Set the length of time (in seconds) that the contents of a feed will be * cached * * @param int $seconds The feed content cache duration * @return void */ public function set_cache_duration(int $seconds = 3600) { $this->cache_duration = $seconds; } /** * Set the length of time (in seconds) that the autodiscovered feed URL will * be cached * * @param int $seconds The autodiscovered feed URL cache duration. * @return void */ public function set_autodiscovery_cache_duration(int $seconds = 604800) { $this->autodiscovery_cache_duration = $seconds; } /** * Set the file system location where the cached files should be stored * * @deprecated since SimplePie 1.8.0, use SimplePie::set_cache() instead. * * @param string $location The file system location. * @return void */ public function set_cache_location(string $location = './cache') { // @trigger_error(sprintf('SimplePie\SimplePie::set_cache_location() is deprecated since SimplePie 1.8.0, please use "SimplePie\SimplePie::set_cache()" instead.'), \E_USER_DEPRECATED); $this->cache_location = $location; } /** * Return the filename (i.e. hash, without path and without extension) of the file to cache a given URL. * * @param string $url The URL of the feed to be cached. * @return string A filename (i.e. hash, without path and without extension). */ public function get_cache_filename(string $url) { // Append custom parameters to the URL to avoid cache pollution in case of multiple calls with different parameters. $url .= $this->force_feed ? '#force_feed' : ''; $options = []; if ($this->timeout != 10) { $options[CURLOPT_TIMEOUT] = $this->timeout; } if ($this->useragent !== Misc::get_default_useragent()) { $options[CURLOPT_USERAGENT] = $this->useragent; } if (!empty($this->curl_options)) { foreach ($this->curl_options as $k => $v) { $options[$k] = $v; } } if (!empty($options)) { ksort($options); $url .= '#' . urlencode(var_export($options, true)); } return $this->cache_namefilter->filter($url); } /** * Set whether feed items should be sorted into reverse chronological order * * @param bool $enable Sort as reverse chronological order. * @return void */ public function enable_order_by_date(bool $enable = true) { $this->order_by_date = $enable; } /** * Set the character encoding used to parse the feed * * This overrides the encoding reported by the feed, however it will fall * back to the normal encoding detection if the override fails * * @param string|false $encoding Character encoding * @return void */ public function set_input_encoding($encoding = false) { if ($encoding) { $this->input_encoding = (string) $encoding; } else { $this->input_encoding = false; } } /** * Set how much feed autodiscovery to do * * @see self::LOCATOR_NONE * @see self::LOCATOR_AUTODISCOVERY * @see self::LOCATOR_LOCAL_EXTENSION * @see self::LOCATOR_LOCAL_BODY * @see self::LOCATOR_REMOTE_EXTENSION * @see self::LOCATOR_REMOTE_BODY * @see self::LOCATOR_ALL * @param self::LOCATOR_* $level Feed Autodiscovery Level (level can be a combination of the above constants, see bitwise OR operator) * @return void */ public function set_autodiscovery_level(int $level = self::LOCATOR_ALL) { $this->autodiscovery = $level; } /** * Get the class registry * * Use this to override SimplePie's default classes * * @return Registry */ public function &get_registry() { return $this->registry; } /** * Set which class SimplePie uses for caching * * @deprecated since SimplePie 1.3, use {@see set_cache()} instead * * @param class-string<Cache> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_cache_class(string $class = Cache::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::set_cache()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Cache::class, $class, true); } /** * Set which class SimplePie uses for auto-discovery * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<Locator> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_locator_class(string $class = Locator::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Locator::class, $class, true); } /** * Set which class SimplePie uses for XML parsing * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<Parser> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_parser_class(string $class = Parser::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Parser::class, $class, true); } /** * Set which class SimplePie uses for remote file fetching * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<File> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_file_class(string $class = File::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(File::class, $class, true); } /** * Set which class SimplePie uses for data sanitization * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<Sanitize> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_sanitize_class(string $class = Sanitize::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Sanitize::class, $class, true); } /** * Set which class SimplePie uses for handling feed items * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<Item> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_item_class(string $class = Item::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Item::class, $class, true); } /** * Set which class SimplePie uses for handling author data * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<Author> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_author_class(string $class = Author::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Author::class, $class, true); } /** * Set which class SimplePie uses for handling category data * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<Category> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_category_class(string $class = Category::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Category::class, $class, true); } /** * Set which class SimplePie uses for feed enclosures * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<Enclosure> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_enclosure_class(string $class = Enclosure::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Enclosure::class, $class, true); } /** * Set which class SimplePie uses for `<media:text>` captions * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<Caption> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_caption_class(string $class = Caption::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Caption::class, $class, true); } /** * Set which class SimplePie uses for `<media:copyright>` * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<Copyright> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_copyright_class(string $class = Copyright::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Copyright::class, $class, true); } /** * Set which class SimplePie uses for `<media:credit>` * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<Credit> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_credit_class(string $class = Credit::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Credit::class, $class, true); } /** * Set which class SimplePie uses for `<media:rating>` * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<Rating> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_rating_class(string $class = Rating::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Rating::class, $class, true); } /** * Set which class SimplePie uses for `<media:restriction>` * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<Restriction> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_restriction_class(string $class = Restriction::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Restriction::class, $class, true); } /** * Set which class SimplePie uses for content-type sniffing * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<Sniffer> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_content_type_sniffer_class(string $class = Sniffer::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Sniffer::class, $class, true); } /** * Set which class SimplePie uses item sources * * @deprecated since SimplePie 1.3, use {@see get_registry()} instead * * @param class-string<Source> $class Name of custom class * * @return bool True on success, false otherwise */ public function set_source_class(string $class = Source::class) { trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.3, please use "SimplePie\SimplePie::get_registry()" instead.', __METHOD__), \E_USER_DEPRECATED); return $this->registry->register(Source::class, $class, true); } /** * Set the user agent string * * @param string $ua New user agent string. * @return void */ public function set_useragent(?string $ua = null) { if ($this->http_client_injected) { throw new SimplePieException(sprintf( 'Using "%s()" has no effect, because you already provided a HTTP client with "%s::set_http_client()". Configure user agent string in your HTTP client instead.', __METHOD__, self::class )); } if ($ua === null) { $ua = Misc::get_default_useragent(); } $this->useragent = (string) $ua; // Reset a possible existing FileClient, // so a new client with the changed value will be created if (is_object($this->http_client) && $this->http_client instanceof FileClient) { $this->http_client = null; } elseif (is_object($this->http_client)) { // Trigger notice if a PSR-18 client was set trigger_error(sprintf( 'Using "%s()" has no effect, because you already provided a HTTP client with "%s::set_http_client()". Configure the useragent in your HTTP client instead.', __METHOD__, get_class($this) ), \E_USER_NOTICE); } } /** * Set a namefilter to modify the cache filename with * * @param NameFilter $filter * * @return void */ public function set_cache_namefilter(NameFilter $filter): void { $this->cache_namefilter = $filter; } /** * Set callback function to create cache filename with * * @deprecated since SimplePie 1.8.0, use {@see set_cache_namefilter()} instead * * @param (string&(callable(string): string))|null $function Callback function * @return void */ public function set_cache_name_function(?string $function = null) { // trigger_error(sprintf('"%s()" is deprecated since SimplePie 1.8.0, please use "SimplePie\SimplePie::set_cache_namefilter()" instead.', __METHOD__), \E_USER_DEPRECATED); if ($function === null) { $function = 'md5'; } $this->cache_name_function = $function; $this->set_cache_namefilter(new CallableNameFilter($this->cache_name_function)); } /** * Set options to make SP as fast as possible * * Forgoes a substantial amount of data sanitization in favor of speed. This * turns SimplePie into a dumb parser of feeds. * * @param bool $set Whether to set them or not * @return void */ public function set_stupidly_fast(bool $set = false) { if ($set) { $this->enable_order_by_date(false); $this->remove_div(false); $this->strip_comments(false); $this->strip_htmltags([]); $this->strip_attributes([]); $this->add_attributes([]); $this->set_image_handler(false); $this->set_https_domains([]); } } /** * Set maximum number of feeds to check with autodiscovery * * @param int $max Maximum number of feeds to check * @return void */ public function set_max_checked_feeds(int $max = 10) { $this->max_checked_feeds = $max; } /** * @return void */ public function remove_div(bool $enable = true) { $this->sanitize->remove_div($enable); } /** * @param string[]|string|false $tags Set a list of tags to strip, or set empty string to use default tags, or false to strip nothing. * @return void */ public function strip_htmltags($tags = '', ?bool $encode = null) { if ($tags === '') { $tags = $this->strip_htmltags; } $this->sanitize->strip_htmltags($tags); if ($encode !== null) { $this->sanitize->encode_instead_of_strip($encode); } } /** * @return void */ public function encode_instead_of_strip(bool $enable = true) { $this->sanitize->encode_instead_of_strip($enable); } /** * @param string[]|string $attribs * @return void */ public function rename_attributes($attribs = '') { if ($attribs === '') { $attribs = $this->rename_attributes; } $this->sanitize->rename_attributes($attribs); } /** * @param string[]|string $attribs * @return void */ public function strip_attributes($attribs = '') { if ($attribs === '') { $attribs = $this->strip_attributes; } $this->sanitize->strip_attributes($attribs); } /** * @param array<string, array<string, string>>|'' $attribs * @return void */ public function add_attributes($attribs = '') { if ($attribs === '') { $attribs = $this->add_attributes; } $this->sanitize->add_attributes($attribs); } /** * Set the output encoding * * Allows you to override SimplePie's output to match that of your webpage. * This is useful for times when your webpages are not being served as * UTF-8. This setting will be obeyed by {@see handle_content_type()}, and * is similar to {@see set_input_encoding()}. * * It should be noted, however, that not all character encodings can support * all characters. If your page is being served as ISO-8859-1 and you try * to display a Japanese feed, you'll likely see garbled characters. * Because of this, it is highly recommended to ensure that your webpages * are served as UTF-8. * * The number of supported character encodings depends on whether your web * host supports {@link http://php.net/mbstring mbstring}, * {@link http://php.net/iconv iconv}, or both. See * {@link http://simplepie.org/wiki/faq/Supported_Character_Encodings} for * more information. * * @param string $encoding * @return void */ public function set_output_encoding(string $encoding = 'UTF-8') { $this->sanitize->set_output_encoding($encoding); } /** * @return void */ public function strip_comments(bool $strip = false) { $this->sanitize->strip_comments($strip); } /** * Set element/attribute key/value pairs of HTML attributes * containing URLs that need to be resolved relative to the feed * * Defaults to |a|@href, |area|@href, |blockquote|@cite, |del|@cite, * |form|@action, |img|@longdesc, |img|@src, |input|@src, |ins|@cite, * |q|@cite * * @since 1.0 * @param array<string, string|string[]>|null $element_attribute Element/attribute key/value pairs, null for default * @return void */ public function set_url_replacements(?array $element_attribute = null) { $this->sanitize->set_url_replacements($element_attribute); } /** * Set the list of domains for which to force HTTPS. * @see Sanitize::set_https_domains() * @param array<string> $domains List of HTTPS domains. Example array('biz', 'example.com', 'example.org', 'www.example.net'). * @return void */ public function set_https_domains(array $domains = []) { $this->sanitize->set_https_domains($domains); } /** * Set the handler to enable the display of cached images. * * @param string|false $page Web-accessible path to the handler_image.php file. * @param string $qs The query string that the value should be passed to. * @return void */ public function set_image_handler($page = false, string $qs = 'i') { if ($page !== false) { $this->sanitize->set_image_handler($page . '?' . $qs . '='); } else { $this->image_handler = ''; } } /** * Set the limit for items returned per-feed with multifeeds * * @param int $limit The maximum number of items to return. * @return void */ public function set_item_limit(int $limit = 0) { $this->item_limit = $limit; } /** * Enable throwing exceptions * * @param bool $enable Should we throw exceptions, or use the old-style error property? * @return void */ public function enable_exceptions(bool $enable = true) { $this->enable_exceptions = $enable; } /** * Initialize the feed object * * This is what makes everything happen. Period. This is where all of the * configuration options get processed, feeds are fetched, cached, and * parsed, and all of that other good stuff. * * @return bool True if successful, false otherwise */ public function init() { // Check absolute bare minimum requirements. if (!extension_loaded('xml') || !extension_loaded('pcre')) { $this->error = 'XML or PCRE extensions not loaded!'; return false; } // Then check the xml extension is sane (i.e., libxml 2.7.x issue on PHP < 5.2.9 and libxml 2.7.0 to 2.7.2 on any version) if we don't have xmlreader. elseif (!extension_loaded('xmlreader')) { static $xml_is_sane = null; if ($xml_is_sane === null) { $parser_check = xml_parser_create(); xml_parse_into_struct($parser_check, '<foo>&</foo>', $values); if (\PHP_VERSION_ID < 80000) { xml_parser_free($parser_check); } $xml_is_sane = isset($values[0]['value']); } if (!$xml_is_sane) { return false; } } // The default sanitize class gets set in the constructor, check if it has // changed. if ($this->registry->get_class(Sanitize::class) !== Sanitize::class) { $this->sanitize = $this->registry->create(Sanitize::class); } if (method_exists($this->sanitize, 'set_registry')) { $this->sanitize->set_registry($this->registry); } // Pass whatever was set with config options over to the sanitizer. // Pass the classes in for legacy support; new classes should use the registry instead $cache = $this->registry->get_class(Cache::class); \assert($cache !== null, 'Cache must be defined'); $this->sanitize->pass_cache_data( $this->enable_cache, $this->cache_location, $this->cache_namefilter, $cache, $this->cache ); $http_client = $this->get_http_client(); if ($http_client instanceof Psr18Client) { $this->sanitize->set_http_client( $http_client->getHttpClient(), $http_client->getRequestFactory(), $http_client->getUriFactory() ); } if (!empty($this->multifeed_url)) { $i = 0; $success = 0; $this->multifeed_objects = []; $this->error = []; foreach ($this->multifeed_url as $url) { $this->multifeed_objects[$i] = clone $this; $this->multifeed_objects[$i]->set_feed_url($url); $single_success = $this->multifeed_objects[$i]->init(); $success |= $single_success; if (!$single_success) { $this->error[$i] = $this->multifeed_objects[$i]->error(); } $i++; } return (bool) $success; } elseif ($this->feed_url === null && $this->raw_data === null) { return false; } $this->error = null; $this->data = []; $this->check_modified = false; $this->multifeed_objects = []; $cache = false; if ($this->feed_url !== null) { $parsed_feed_url = $this->registry->call(Misc::class, 'parse_url', [$this->feed_url]); // Decide whether to enable caching if ($this->enable_cache && $parsed_feed_url['scheme'] !== '') { $cache = $this->get_cache($this->feed_url); } // Fetch the data into $this->raw_data if (($fetched = $this->fetch_data($cache)) === true) { return true; } elseif ($fetched === false) { return false; } [$headers, $sniffed] = $fetched; } // Empty response check if (empty($this->raw_data)) { $this->error = "A feed could not be found at `$this->feed_url`. Empty body."; $this->registry->call(Misc::class, 'error', [$this->error, E_USER_NOTICE, __FILE__, __LINE__]); return false; } // Set up array of possible encodings $encodings = []; // First check to see if input has been overridden. if ($this->input_encoding !== false) { $encodings[] = strtoupper($this->input_encoding); } $application_types = ['application/xml', 'application/xml-dtd', 'application/xml-external-parsed-entity']; $text_types = ['text/xml', 'text/xml-external-parsed-entity']; // RFC 3023 (only applies to sniffed content) if (isset($sniffed)) { if (in_array($sniffed, $application_types) || substr($sniffed, 0, 12) === 'application/' && substr($sniffed, -4) === '+xml') { if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset)) { $encodings[] = strtoupper($charset[1]); } $encodings = array_merge($encodings, $this->registry->call(Misc::class, 'xml_encoding', [$this->raw_data, &$this->registry])); $encodings[] = 'UTF-8'; } elseif (in_array($sniffed, $text_types) || substr($sniffed, 0, 5) === 'text/' && substr($sniffed, -4) === '+xml') { if (isset($headers['content-type']) && preg_match('/;\x20?charset=([^;]*)/i', $headers['content-type'], $charset)) { $encodings[] = strtoupper($charset[1]); } $encodings[] = 'US-ASCII'; } // Text MIME-type default elseif (substr($sniffed, 0, 5) === 'text/') { $encodings[] = 'UTF-8'; } } // Fallback to XML 1.0 Appendix F.1/UTF-8/ISO-8859-1 $encodings = array_merge($encodings, $this->registry->call(Misc::class, 'xml_encoding', [$this->raw_data, &$this->registry])); $encodings[] = 'UTF-8'; $encodings[] = 'ISO-8859-1'; // There's no point in trying an encoding twice $encodings = array_unique($encodings); // Loop through each possible encoding, till we return something, or run out of possibilities foreach ($encodings as $encoding) { // Change the encoding to UTF-8 (as we always use UTF-8 internally) if ($utf8_data = $this->registry->call(Misc::class, 'change_encoding', [$this->raw_data, $encoding, 'UTF-8'])) { // Create new parser $parser = $this->registry->create(Parser::class); // If it's parsed fine if ($parser->parse($utf8_data, 'UTF-8', $this->permanent_url ?? '')) { $this->data = $parser->get_data(); if (!($this->get_type() & ~self::TYPE_NONE)) { $this->error = "A feed could not be found at `$this->feed_url`. This does not appear to be a valid RSS or Atom feed."; $this->registry->call(Misc::class, 'error', [$this->error, E_USER_NOTICE, __FILE__, __LINE__]); return false; } if (isset($headers)) { $this->data['headers'] = $headers; } $this->data['build'] = Misc::get_build(); // Cache the file if caching is enabled $this->data['cache_expiration_time'] = $this->cache_duration + time(); if ($cache && !$cache->set_data($this->get_cache_filename($this->feed_url), $this->data, $this->cache_duration)) { trigger_error("$this->cache_location is not writable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING); } return true; } } } if (isset($parser)) { // We have an error, just set Misc::error to it and quit $this->error = $this->feed_url; $this->error .= sprintf(' is invalid XML, likely due to invalid characters. XML error: %s at line %d, column %d', $parser->get_error_string(), $parser->get_current_line(), $parser->get_current_column()); } else { $this->error = 'The data could not be converted to UTF-8.'; if (!extension_loaded('mbstring') && !extension_loaded('iconv') && !class_exists('\UConverter')) { $this->error .= ' You MUST have either the iconv, mbstring or intl (PHP 5.5+) extension installed and enabled.'; } else { $missingExtensions = []; if (!extension_loaded('iconv')) { $missingExtensions[] = 'iconv'; } if (!extension_loaded('mbstring')) { $missingExtensions[] = 'mbstring'; } if (!class_exists('\UConverter')) { $missingExtensions[] = 'intl (PHP 5.5+)'; } $this->error .= ' Try installing/enabling the ' . implode(' or ', $missingExtensions) . ' extension.'; } } $this->registry->call(Misc::class, 'error', [$this->error, E_USER_NOTICE, __FILE__, __LINE__]); return false; } /** * Fetch the data * * If the data is already cached, attempt to fetch it from there instead * * @param Base|DataCache|false $cache Cache handler, or false to not load from the cache * @return array{array<string, string>, string}|bool Returns true if the data was loaded from the cache, or an array of HTTP headers and sniffed type */ protected function fetch_data(&$cache) { if ($cache instanceof Base) { // @trigger_error(sprintf('Providing $cache as "\SimplePie\Cache\Base" in %s() is deprecated since SimplePie 1.8.0, please provide "\SimplePie\Cache\DataCache" implementation instead.', __METHOD__), \E_USER_DEPRECATED); $cache = new BaseDataCache($cache); } // @phpstan-ignore-next-line Enforce PHPDoc type. if ($cache !== false && !$cache instanceof DataCache) { throw new InvalidArgumentException(sprintf( '%s(): Argument #1 ($cache) must be of type %s|false', __METHOD__, DataCache::class ), 1); } $cacheKey = $this->get_cache_filename($this->feed_url); // If it's enabled, use the cache if ($cache) { // Load the Cache $this->data = $cache->get_data($cacheKey, []); if (!empty($this->data)) { // If the cache is for an outdated build of SimplePie if (!isset($this->data['build']) || $this->data['build'] !== Misc::get_build()) { $cache->delete_data($cacheKey); $this->data = []; } // If we've hit a collision just rerun it with caching disabled elseif (isset($this->data['url']) && $this->data['url'] !== $this->feed_url) { $cache = false; $this->data = []; } // If we've got a non feed_url stored (if the page isn't actually a feed, or is a redirect) use that URL. elseif (isset($this->data['feed_url'])) { // Do not need to do feed autodiscovery yet. if ($this->data['feed_url'] !== $this->data['url']) { $this->set_feed_url($this->data['feed_url']); $this->data['url'] = $this->data['feed_url']; $cache->set_data($this->get_cache_filename($this->feed_url), $this->data, $this->autodiscovery_cache_duration); return $this->init(); } $cache->delete_data($this->get_cache_filename($this->feed_url)); $this->data = []; } // Check if the cache has been updated elseif (!isset($this->data['cache_expiration_time']) || $this->data['cache_expiration_time'] < time()) { // Want to know if we tried to send last-modified and/or etag headers // when requesting this file. (Note that it's up to the file to // support this, but we don't always send the headers either.) $this->check_modified = true; if (isset($this->data['headers']['last-modified']) || isset($this->data['headers']['etag'])) { $headers = [ 'Accept' => SimplePie::DEFAULT_HTTP_ACCEPT_HEADER, ]; if (isset($this->data['headers']['last-modified'])) { $headers['if-modified-since'] = $this->data['headers']['last-modified']; } if (isset($this->data['headers']['etag'])) { $headers['if-none-match'] = $this->data['headers']['etag']; } try { $file = $this->get_http_client()->request(Client::METHOD_GET, $this->feed_url, $headers); $this->status_code = $file->get_status_code(); } catch (ClientException $th) { $this->check_modified = false; $this->status_code = 0; if ($this->force_cache_fallback) { $this->data['cache_expiration_time'] = $this->cache_duration + time(); $cache->set_data($cacheKey, $this->data, $this->cache_duration); return true; } $failedFileReason = $th->getMessage(); } if ($this->status_code === 304) { // Set raw_data to false here too, to signify that the cache // is still valid. $this->raw_data = false; $this->data['cache_expiration_time'] = $this->cache_duration + time(); $cache->set_data($cacheKey, $this->data, $this->cache_duration); return true; } } } // If the cache is still valid, just return true else { $this->raw_data = false; return true; } } // If the cache is empty else { $this->data = []; } } // If we don't already have the file (it'll only exist if we've opened it to check if the cache has been modified), open it. if (!isset($file)) { if ($this->file instanceof File && $this->file->get_final_requested_uri() === $this->feed_url) { $file = &$this->file; } elseif (isset($failedFileReason)) { // Do not try to fetch again if we already failed once. // If the file connection had an error, set SimplePie::error to that and quit $this->error = $failedFileReason; return !empty($this->data); } else { $headers = [ 'Accept' => SimplePie::DEFAULT_HTTP_ACCEPT_HEADER, ]; try { $file = $this->get_http_client()->request(Client::METHOD_GET, $this->feed_url, $headers); } catch (ClientException $th) { // If the file connection has an error, set SimplePie::error to that and quit $this->error = $th->getMessage(); return !empty($this->data); } } } $this->status_code = $file->get_status_code(); // If the file connection has an error, set SimplePie::error to that and quit if (!(!Misc::is_remote_uri($file->get_final_requested_uri()) || ($file->get_status_code() === 200 || $file->get_status_code() > 206 && $file->get_status_code() < 300))) { $this->error = 'Retrieved unsupported status code "' . $this->status_code . '"'; return !empty($this->data); } if (!$this->force_feed) { // Check if the supplied URL is a feed, if it isn't, look for it. $locate = $this->registry->create(Locator::class, [ (!$file instanceof File) ? File::fromResponse($file) : $file, $this->timeout, $this->useragent, $this->max_checked_feeds, $this->force_fsockopen, $this->curl_options ]); $http_client = $this->get_http_client(); if ($http_client instanceof Psr18Client) { $locate->set_http_client( $http_client->getHttpClient(), $http_client->getRequestFactory(), $http_client->getUriFactory() ); } if (!$locate->is_feed($file)) { $copyStatusCode = $file->get_status_code(); $copyContentType = $file->get_header_line('content-type'); try { $microformats = false; if (class_exists('DOMXpath') && function_exists('Mf2\parse')) { $doc = new \DOMDocument(); @$doc->loadHTML($file->get_body_content()); $xpath = new \DOMXpath($doc); // Check for both h-feed and h-entry, as both a feed with no entries // and a list of entries without an h-feed wrapper are both valid. $query = '//*[contains(concat(" ", @class, " "), " h-feed ") or '. 'contains(concat(" ", @class, " "), " h-entry ")]'; /** @var \DOMNodeList<\DOMElement> $result */ $result = $xpath->query($query); $microformats = $result->length !== 0; } // Now also do feed discovery, but if microformats were found don't // overwrite the current value of file. $discovered = $locate->find( $this->autodiscovery, $this->all_discovered_feeds ); if ($microformats) { $hub = $locate->get_rel_link('hub'); $self = $locate->get_rel_link('self'); if ($hub || $self) { $file = $this->store_links($file, $hub, $self); } // Push the current file onto all_discovered feeds so the user can // be shown this as one of the options. if ($this->all_discovered_feeds !== null) { $this->all_discovered_feeds[] = $file; } } else { if ($discovered) { $file = $discovered; } else { // We need to unset this so that if SimplePie::set_file() has // been called that object is untouched unset($file); $this->error = "A feed could not be found at `$this->feed_url`; the status code is `$copyStatusCode` and content-type is `$copyContentType`"; $this->registry->call(Misc::class, 'error', [$this->error, E_USER_NOTICE, __FILE__, __LINE__]); return false; } } } catch (SimplePieException $e) { // We need to unset this so that if SimplePie::set_file() has been called that object is untouched unset($file); // This is usually because DOMDocument doesn't exist $this->error = $e->getMessage(); $this->registry->call(Misc::class, 'error', [$this->error, E_USER_NOTICE, $e->getFile(), $e->getLine()]); return false; } if ($cache) { $this->data = [ 'url' => $this->feed_url, 'feed_url' => $file->get_final_requested_uri(), 'build' => Misc::get_build(), 'cache_expiration_time' => $this->cache_duration + time(), ]; if (!$cache->set_data($cacheKey, $this->data, $this->cache_duration)) { trigger_error("$this->cache_location is not writable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING); } } } $this->feed_url = $file->get_final_requested_uri(); $locate = null; } $this->raw_data = $file->get_body_content(); $this->permanent_url = $file->get_permanent_uri(); $headers = []; foreach ($file->get_headers() as $key => $values) { $headers[$key] = implode(', ', $values); } $sniffer = $this->registry->create(Sniffer::class, [&$file]); $sniffed = $sniffer->get_type(); return [$headers, $sniffed]; } /** * Get the error message for the occurred error * * @return string|string[]|null Error message, or array of messages for multifeeds */ public function error() { return $this->error; } /** * Get the last HTTP status code * * @return int Status code */ public function status_code() { return $this->status_code; } /** * Get the raw XML * * This is the same as the old `$feed->enable_xml_dump(true)`, but returns * the data instead of printing it. * * @return string|false Raw XML data, false if the cache is used */ public function get_raw_data() { return $this->raw_data; } /** * Get the character encoding used for output * * @since Preview Release * @return string */ public function get_encoding() { return $this->sanitize->output_encoding; } /** * Send the content-type header with correct encoding * * This method ensures that the SimplePie-enabled page is being served with * the correct {@link http://www.iana.org/assignments/media-types/ mime-type} * and character encoding HTTP headers (character encoding determined by the * {@see set_output_encoding} config option). * * This won't work properly if any content or whitespace has already been * sent to the browser, because it relies on PHP's * {@link http://php.net/header header()} function, and these are the * circumstances under which the function works. * * Because it's setting these settings for the entire page (as is the nature * of HTTP headers), this should only be used once per page (again, at the * top). * * @param string $mime MIME type to serve the page as * @return void */ public function handle_content_type(string $mime = 'text/html') { if (!headers_sent()) { $header = "Content-type: $mime;"; if ($this->get_encoding()) { $header .= ' charset=' . $this->get_encoding(); } else { $header .= ' charset=UTF-8'; } header($header); } } /** * Get the type of the feed * * This returns a self::TYPE_* constant, which can be tested against * using {@link http://php.net/language.operators.bitwise bitwise operators} * * @since 0.8 (usage changed to using constants in 1.0) * @see self::TYPE_NONE Unknown. * @see self::TYPE_RSS_090 RSS 0.90. * @see self::TYPE_RSS_091_NETSCAPE RSS 0.91 (Netscape). * @see self::TYPE_RSS_091_USERLAND RSS 0.91 (Userland). * @see self::TYPE_RSS_091 RSS 0.91. * @see self::TYPE_RSS_092 RSS 0.92. * @see self::TYPE_RSS_093 RSS 0.93. * @see self::TYPE_RSS_094 RSS 0.94. * @see self::TYPE_RSS_10 RSS 1.0. * @see self::TYPE_RSS_20 RSS 2.0.x. * @see self::TYPE_RSS_RDF RDF-based RSS. * @see self::TYPE_RSS_SYNDICATION Non-RDF-based RSS (truly intended as syndication format). * @see self::TYPE_RSS_ALL Any version of RSS. * @see self::TYPE_ATOM_03 Atom 0.3. * @see self::TYPE_ATOM_10 Atom 1.0. * @see self::TYPE_ATOM_ALL Any version of Atom. * @see self::TYPE_ALL Any known/supported feed type. * @return int-mask-of<self::TYPE_*> constant */ public function get_type() { if (!isset($this->data['type'])) { $this->data['type'] = self::TYPE_ALL; if (isset($this->data['child'][self::NAMESPACE_ATOM_10]['feed'])) { $this->data['type'] &= self::TYPE_ATOM_10; } elseif (isset($this->data['child'][self::NAMESPACE_ATOM_03]['feed'])) { $this->data['type'] &= self::TYPE_ATOM_03; } elseif (isset($this->data['child'][self::NAMESPACE_RDF]['RDF'])) { if (isset($this->data['child'][self::NAMESPACE_RDF]['RDF'][0]['child'][self::NAMESPACE_RSS_10]['channel']) || isset($this->data['child'][self::NAMESPACE_RDF]['RDF'][0]['child'][self::NAMESPACE_RSS_10]['image']) || isset($this->data['child'][self::NAMESPACE_RDF]['RDF'][0]['child'][self::NAMESPACE_RSS_10]['item']) || isset($this->data['child'][self::NAMESPACE_RDF]['RDF'][0]['child'][self::NAMESPACE_RSS_10]['textinput'])) { $this->data['type'] &= self::TYPE_RSS_10; } if (isset($this->data['child'][self::NAMESPACE_RDF]['RDF'][0]['child'][self::NAMESPACE_RSS_090]['channel']) || isset($this->data['child'][self::NAMESPACE_RDF]['RDF'][0]['child'][self::NAMESPACE_RSS_090]['image']) || isset($this->data['child'][self::NAMESPACE_RDF]['RDF'][0]['child'][self::NAMESPACE_RSS_090]['item']) || isset($this->data['child'][self::NAMESPACE_RDF]['RDF'][0]['child'][self::NAMESPACE_RSS_090]['textinput'])) { $this->data['type'] &= self::TYPE_RSS_090; } } elseif (isset($this->data['child'][self::NAMESPACE_RSS_20]['rss'])) { $this->data['type'] &= self::TYPE_RSS_ALL; if (isset($this->data['child'][self::NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version'])) { switch (trim($this->data['child'][self::NAMESPACE_RSS_20]['rss'][0]['attribs']['']['version'])) { case '0.91': $this->data['type'] &= self::TYPE_RSS_091; if (isset($this->data['child'][self::NAMESPACE_RSS_20]['rss'][0]['child'][self::NAMESPACE_RSS_20]['skiphours']['hour'][0]['data'])) { switch (trim($this->data['child'][self::NAMESPACE_RSS_20]['rss'][0]['child'][self::NAMESPACE_RSS_20]['skiphours']['hour'][0]['data'])) { case '0': $this->data['type'] &= self::TYPE_RSS_091_NETSCAPE; break; case '24': $this->data['type'] &= self::TYPE_RSS_091_USERLAND; break; } } break; case '0.92': $this->data['type'] &= self::TYPE_RSS_092; break; case '0.93': $this->data['type'] &= self::TYPE_RSS_093; break; case '0.94': $this->data['type'] &= self::TYPE_RSS_094; break; case '2.0': $this->data['type'] &= self::TYPE_RSS_20; break; } } } else { $this->data['type'] = self::TYPE_NONE; } } return $this->data['type']; } /** * Get the URL for the feed * * When the 'permanent' mode is enabled, returns the original feed URL, * except in the case of an `HTTP 301 Moved Permanently` status response, * in which case the location of the first redirection is returned. * * When the 'permanent' mode is disabled (default), * may or may not be different from the URL passed to {@see set_feed_url()}, * depending on whether auto-discovery was used, and whether there were * any redirects along the way. * * @since Preview Release (previously called `get_feed_url()` since SimplePie 0.8.) * @todo Support <itunes:new-feed-url> * @todo Also, |atom:link|@rel=self * @param bool $permanent Permanent mode to return only the original URL or the first redirection * iff it is a 301 redirection * @return string|null */ public function subscribe_url(bool $permanent = false) { if ($permanent) { if ($this->permanent_url !== null) { // sanitize encodes ampersands which are required when used in a url. return str_replace( '&', '&', $this->sanitize( $this->permanent_url, self::CONSTRUCT_IRI ) ); } } else { if ($this->feed_url !== null) { return str_replace( '&', '&', $this->sanitize( $this->feed_url, self::CONSTRUCT_IRI ) ); } } return null; } /** * Get data for an feed-level element * * This method allows you to get access to ANY element/attribute that is a * sub-element of the opening feed tag. * * The return value is an indexed array of elements matching the given * namespace and tag name. Each element has `attribs`, `data` and `child` * subkeys. For `attribs` and `child`, these contain namespace subkeys. * `attribs` then has one level of associative name => value data (where * `value` is a string) after the namespace. `child` has tag-indexed keys * after the namespace, each member of which is an indexed array matching * this same format. * * For example: * <pre> * // This is probably a bad example because we already support * // <media:content> natively, but it shows you how to parse through * // the nodes. * $group = $item->get_item_tags(\SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'group'); * $content = $group[0]['child'][\SimplePie\SimplePie::NAMESPACE_MEDIARSS]['content']; * $file = $content[0]['attribs']['']['url']; * echo $file; * </pre> * * @since 1.0 * @see http://simplepie.org/wiki/faq/supported_xml_namespaces * @param string $namespace The URL of the XML namespace of the elements you're trying to access * @param string $tag Tag name * @return array<array<string, mixed>>|null */ public function get_feed_tags(string $namespace, string $tag) { $type = $this->get_type(); if ($type & self::TYPE_ATOM_10) { if (isset($this->data['child'][self::NAMESPACE_ATOM_10]['feed'][0]['child'][$namespace][$tag])) { return $this->data['child'][self::NAMESPACE_ATOM_10]['feed'][0]['child'][$namespace][$tag]; } } if ($type & self::TYPE_ATOM_03) { if (isset($this->data['child'][self::NAMESPACE_ATOM_03]['feed'][0]['child'][$namespace][$tag])) { return $this->data['child'][self::NAMESPACE_ATOM_03]['feed'][0]['child'][$namespace][$tag]; } } if ($type & self::TYPE_RSS_RDF) { if (isset($this->data['child'][self::NAMESPACE_RDF]['RDF'][0]['child'][$namespace][$tag])) { return $this->data['child'][self::NAMESPACE_RDF]['RDF'][0]['child'][$namespace][$tag]; } } if ($type & self::TYPE_RSS_SYNDICATION) { if (isset($this->data['child'][self::NAMESPACE_RSS_20]['rss'][0]['child'][$namespace][$tag])) { return $this->data['child'][self::NAMESPACE_RSS_20]['rss'][0]['child'][$namespace][$tag]; } } return null; } /** * Get data for an channel-level element * * This method allows you to get access to ANY element/attribute in the * channel/header section of the feed. * * See {@see SimplePie::get_feed_tags()} for a description of the return value * * @since 1.0 * @see http://simplepie.org/wiki/faq/supported_xml_namespaces * @param string $namespace The URL of the XML namespace of the elements you're trying to access * @param string $tag Tag name * @return array<array<string, mixed>>|null */ public function get_channel_tags(string $namespace, string $tag) { $type = $this->get_type(); if ($type & self::TYPE_ATOM_ALL) { if ($return = $this->get_feed_tags($namespace, $tag)) { return $return; } } if ($type & self::TYPE_RSS_10) { if ($channel = $this->get_feed_tags(self::NAMESPACE_RSS_10, 'channel')) { if (isset($channel[0]['child'][$namespace][$tag])) { return $channel[0]['child'][$namespace][$tag]; } } } if ($type & self::TYPE_RSS_090) { if ($channel = $this->get_feed_tags(self::NAMESPACE_RSS_090, 'channel')) { if (isset($channel[0]['child'][$namespace][$tag])) { return $channel[0]['child'][$namespace][$tag]; } } } if ($type & self::TYPE_RSS_SYNDICATION) { if ($channel = $this->get_feed_tags(self::NAMESPACE_RSS_20, 'channel')) { if (isset($channel[0]['child'][$namespace][$tag])) { return $channel[0]['child'][$namespace][$tag]; } } } return null; } /** * Get data for an channel-level element * * This method allows you to get access to ANY element/attribute in the * image/logo section of the feed. * * See {@see SimplePie::get_feed_tags()} for a description of the return value * * @since 1.0 * @see http://simplepie.org/wiki/faq/supported_xml_namespaces * @param string $namespace The URL of the XML namespace of the elements you're trying to access * @param string $tag Tag name * @return array<array<string, mixed>>|null */ public function get_image_tags(string $namespace, string $tag) { $type = $this->get_type(); if ($type & self::TYPE_RSS_10) { if ($image = $this->get_feed_tags(self::NAMESPACE_RSS_10, 'image')) { if (isset($image[0]['child'][$namespace][$tag])) { return $image[0]['child'][$namespace][$tag]; } } } if ($type & self::TYPE_RSS_090) { if ($image = $this->get_feed_tags(self::NAMESPACE_RSS_090, 'image')) { if (isset($image[0]['child'][$namespace][$tag])) { return $image[0]['child'][$namespace][$tag]; } } } if ($type & self::TYPE_RSS_SYNDICATION) { if ($image = $this->get_channel_tags(self::NAMESPACE_RSS_20, 'image')) { if (isset($image[0]['child'][$namespace][$tag])) { return $image[0]['child'][$namespace][$tag]; } } } return null; } /** * Get the base URL value from the feed * * Uses `<xml:base>` if available, * otherwise uses the first 'self' link or the first 'alternate' link of the feed, * or failing that, the URL of the feed itself. * * @see get_link * @see subscribe_url * * @param array<string, mixed> $element * @return string */ public function get_base(array $element = []) { if (!empty($element['xml_base_explicit']) && isset($element['xml_base'])) { return $element['xml_base']; } if (($link = $this->get_link(0, 'alternate')) !== null) { return $link; } if (($link = $this->get_link(0, 'self')) !== null) { return $link; } return $this->subscribe_url() ?? ''; } /** * Sanitize feed data * * @access private * @see Sanitize::sanitize() * @param string $data Data to sanitize * @param int-mask-of<SimplePie::CONSTRUCT_*> $type * @param string $base Base URL to resolve URLs against * @return string Sanitized data */ public function sanitize(string $data, int $type, string $base = '') { try { // This really returns string|false but changing encoding is uncommon and we are going to deprecate it, so let’s just lie to PHPStan in the interest of cleaner annotations. return $this->sanitize->sanitize($data, $type, $base); } catch (SimplePieException $e) { if (!$this->enable_exceptions) { $this->error = $e->getMessage(); $this->registry->call(Misc::class, 'error', [$this->error, E_USER_WARNING, $e->getFile(), $e->getLine()]); return ''; } throw $e; } } /** * Get the title of the feed * * Uses `<atom:title>`, `<title>` or `<dc:title>` * * @since 1.0 (previously called `get_feed_title` since 0.8) * @return string|null */ public function get_title() { if ($return = $this->get_channel_tags(self::NAMESPACE_ATOM_10, 'title')) { return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_ATOM_03, 'title')) { return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_RSS_10, 'title')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_RSS_090, 'title')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_RSS_20, 'title')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_DC_11, 'title')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_DC_10, 'title')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } return null; } /** * Get a category for the feed * * @since Unknown * @param int $key The category that you want to return. Remember that arrays begin with 0, not 1 * @return Category|null */ public function get_category(int $key = 0) { $categories = $this->get_categories(); if (isset($categories[$key])) { return $categories[$key]; } return null; } /** * Get all categories for the feed * * Uses `<atom:category>`, `<category>` or `<dc:subject>` * * @since Unknown * @return array<Category>|null List of {@see Category} objects */ public function get_categories() { $categories = []; foreach ((array) $this->get_channel_tags(self::NAMESPACE_ATOM_10, 'category') as $category) { $term = null; $scheme = null; $label = null; if (isset($category['attribs']['']['term'])) { $term = $this->sanitize($category['attribs']['']['term'], self::CONSTRUCT_TEXT); } if (isset($category['attribs']['']['scheme'])) { $scheme = $this->sanitize($category['attribs']['']['scheme'], self::CONSTRUCT_TEXT); } if (isset($category['attribs']['']['label'])) { $label = $this->sanitize($category['attribs']['']['label'], self::CONSTRUCT_TEXT); } $categories[] = $this->registry->create(Category::class, [$term, $scheme, $label]); } foreach ((array) $this->get_channel_tags(self::NAMESPACE_RSS_20, 'category') as $category) { // This is really the label, but keep this as the term also for BC. // Label will also work on retrieving because that falls back to term. $term = $this->sanitize($category['data'], self::CONSTRUCT_TEXT); if (isset($category['attribs']['']['domain'])) { $scheme = $this->sanitize($category['attribs']['']['domain'], self::CONSTRUCT_TEXT); } else { $scheme = null; } $categories[] = $this->registry->create(Category::class, [$term, $scheme, null]); } foreach ((array) $this->get_channel_tags(self::NAMESPACE_DC_11, 'subject') as $category) { $categories[] = $this->registry->create(Category::class, [$this->sanitize($category['data'], self::CONSTRUCT_TEXT), null, null]); } foreach ((array) $this->get_channel_tags(self::NAMESPACE_DC_10, 'subject') as $category) { $categories[] = $this->registry->create(Category::class, [$this->sanitize($category['data'], self::CONSTRUCT_TEXT), null, null]); } if (!empty($categories)) { return array_unique($categories); } return null; } /** * Get an author for the feed * * @since 1.1 * @param int $key The author that you want to return. Remember that arrays begin with 0, not 1 * @return Author|null */ public function get_author(int $key = 0) { $authors = $this->get_authors(); if (isset($authors[$key])) { return $authors[$key]; } return null; } /** * Get all authors for the feed * * Uses `<atom:author>`, `<author>`, `<dc:creator>` or `<itunes:author>` * * @since 1.1 * @return array<Author>|null List of {@see Author} objects */ public function get_authors() { $authors = []; foreach ((array) $this->get_channel_tags(self::NAMESPACE_ATOM_10, 'author') as $author) { $name = null; $uri = null; $email = null; if (isset($author['child'][self::NAMESPACE_ATOM_10]['name'][0]['data'])) { $name = $this->sanitize($author['child'][self::NAMESPACE_ATOM_10]['name'][0]['data'], self::CONSTRUCT_TEXT); } if (isset($author['child'][self::NAMESPACE_ATOM_10]['uri'][0]['data'])) { $uri = $author['child'][self::NAMESPACE_ATOM_10]['uri'][0]; $uri = $this->sanitize($uri['data'], self::CONSTRUCT_IRI, $this->get_base($uri)); } if (isset($author['child'][self::NAMESPACE_ATOM_10]['email'][0]['data'])) { $email = $this->sanitize($author['child'][self::NAMESPACE_ATOM_10]['email'][0]['data'], self::CONSTRUCT_TEXT); } if ($name !== null || $email !== null || $uri !== null) { $authors[] = $this->registry->create(Author::class, [$name, $uri, $email]); } } if ($author = $this->get_channel_tags(self::NAMESPACE_ATOM_03, 'author')) { $name = null; $url = null; $email = null; if (isset($author[0]['child'][self::NAMESPACE_ATOM_03]['name'][0]['data'])) { $name = $this->sanitize($author[0]['child'][self::NAMESPACE_ATOM_03]['name'][0]['data'], self::CONSTRUCT_TEXT); } if (isset($author[0]['child'][self::NAMESPACE_ATOM_03]['url'][0]['data'])) { $url = $author[0]['child'][self::NAMESPACE_ATOM_03]['url'][0]; $url = $this->sanitize($url['data'], self::CONSTRUCT_IRI, $this->get_base($url)); } if (isset($author[0]['child'][self::NAMESPACE_ATOM_03]['email'][0]['data'])) { $email = $this->sanitize($author[0]['child'][self::NAMESPACE_ATOM_03]['email'][0]['data'], self::CONSTRUCT_TEXT); } if ($name !== null || $email !== null || $url !== null) { $authors[] = $this->registry->create(Author::class, [$name, $url, $email]); } } foreach ((array) $this->get_channel_tags(self::NAMESPACE_DC_11, 'creator') as $author) { $authors[] = $this->registry->create(Author::class, [$this->sanitize($author['data'], self::CONSTRUCT_TEXT), null, null]); } foreach ((array) $this->get_channel_tags(self::NAMESPACE_DC_10, 'creator') as $author) { $authors[] = $this->registry->create(Author::class, [$this->sanitize($author['data'], self::CONSTRUCT_TEXT), null, null]); } foreach ((array) $this->get_channel_tags(self::NAMESPACE_ITUNES, 'author') as $author) { $authors[] = $this->registry->create(Author::class, [$this->sanitize($author['data'], self::CONSTRUCT_TEXT), null, null]); } if (!empty($authors)) { return array_unique($authors); } return null; } /** * Get a contributor for the feed * * @since 1.1 * @param int $key The contrbutor that you want to return. Remember that arrays begin with 0, not 1 * @return Author|null */ public function get_contributor(int $key = 0) { $contributors = $this->get_contributors(); if (isset($contributors[$key])) { return $contributors[$key]; } return null; } /** * Get all contributors for the feed * * Uses `<atom:contributor>` * * @since 1.1 * @return array<Author>|null List of {@see Author} objects */ public function get_contributors() { $contributors = []; foreach ((array) $this->get_channel_tags(self::NAMESPACE_ATOM_10, 'contributor') as $contributor) { $name = null; $uri = null; $email = null; if (isset($contributor['child'][self::NAMESPACE_ATOM_10]['name'][0]['data'])) { $name = $this->sanitize($contributor['child'][self::NAMESPACE_ATOM_10]['name'][0]['data'], self::CONSTRUCT_TEXT); } if (isset($contributor['child'][self::NAMESPACE_ATOM_10]['uri'][0]['data'])) { $uri = $contributor['child'][self::NAMESPACE_ATOM_10]['uri'][0]; $uri = $this->sanitize($uri['data'], self::CONSTRUCT_IRI, $this->get_base($uri)); } if (isset($contributor['child'][self::NAMESPACE_ATOM_10]['email'][0]['data'])) { $email = $this->sanitize($contributor['child'][self::NAMESPACE_ATOM_10]['email'][0]['data'], self::CONSTRUCT_TEXT); } if ($name !== null || $email !== null || $uri !== null) { $contributors[] = $this->registry->create(Author::class, [$name, $uri, $email]); } } foreach ((array) $this->get_channel_tags(self::NAMESPACE_ATOM_03, 'contributor') as $contributor) { $name = null; $url = null; $email = null; if (isset($contributor['child'][self::NAMESPACE_ATOM_03]['name'][0]['data'])) { $name = $this->sanitize($contributor['child'][self::NAMESPACE_ATOM_03]['name'][0]['data'], self::CONSTRUCT_TEXT); } if (isset($contributor['child'][self::NAMESPACE_ATOM_03]['url'][0]['data'])) { $url = $contributor['child'][self::NAMESPACE_ATOM_03]['url'][0]; $url = $this->sanitize($url['data'], self::CONSTRUCT_IRI, $this->get_base($url)); } if (isset($contributor['child'][self::NAMESPACE_ATOM_03]['email'][0]['data'])) { $email = $this->sanitize($contributor['child'][self::NAMESPACE_ATOM_03]['email'][0]['data'], self::CONSTRUCT_TEXT); } if ($name !== null || $email !== null || $url !== null) { $contributors[] = $this->registry->create(Author::class, [$name, $url, $email]); } } if (!empty($contributors)) { return array_unique($contributors); } return null; } /** * Get a single link for the feed * * @since 1.0 (previously called `get_feed_link` since Preview Release, `get_feed_permalink()` since 0.8) * @param int $key The link that you want to return. Remember that arrays begin with 0, not 1 * @param string $rel The relationship of the link to return * @return string|null Link URL */ public function get_link(int $key = 0, string $rel = 'alternate') { $links = $this->get_links($rel); if (isset($links[$key])) { return $links[$key]; } return null; } /** * Get the permalink for the item * * Returns the first link available with a relationship of "alternate". * Identical to {@see get_link()} with key 0 * * @see get_link * @since 1.0 (previously called `get_feed_link` since Preview Release, `get_feed_permalink()` since 0.8) * @internal Added for parity between the parent-level and the item/entry-level. * @return string|null Link URL */ public function get_permalink() { return $this->get_link(0); } /** * Get all links for the feed * * Uses `<atom:link>` or `<link>` * * @since Beta 2 * @param string $rel The relationship of links to return * @return array<string>|null Links found for the feed (strings) */ public function get_links(string $rel = 'alternate') { if (!isset($this->data['links'])) { $this->data['links'] = []; if ($links = $this->get_channel_tags(self::NAMESPACE_ATOM_10, 'link')) { foreach ($links as $link) { if (isset($link['attribs']['']['href'])) { $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate'; $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], self::CONSTRUCT_IRI, $this->get_base($link)); } } } if ($links = $this->get_channel_tags(self::NAMESPACE_ATOM_03, 'link')) { foreach ($links as $link) { if (isset($link['attribs']['']['href'])) { $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate'; $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], self::CONSTRUCT_IRI, $this->get_base($link)); } } } if ($links = $this->get_channel_tags(self::NAMESPACE_RSS_10, 'link')) { $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], self::CONSTRUCT_IRI, $this->get_base($links[0])); } if ($links = $this->get_channel_tags(self::NAMESPACE_RSS_090, 'link')) { $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], self::CONSTRUCT_IRI, $this->get_base($links[0])); } if ($links = $this->get_channel_tags(self::NAMESPACE_RSS_20, 'link')) { $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], self::CONSTRUCT_IRI, $this->get_base($links[0])); } $keys = array_keys($this->data['links']); foreach ($keys as $key) { if ($this->registry->call(Misc::class, 'is_isegment_nz_nc', [$key])) { if (isset($this->data['links'][self::IANA_LINK_RELATIONS_REGISTRY . $key])) { $this->data['links'][self::IANA_LINK_RELATIONS_REGISTRY . $key] = array_merge($this->data['links'][$key], $this->data['links'][self::IANA_LINK_RELATIONS_REGISTRY . $key]); $this->data['links'][$key] = &$this->data['links'][self::IANA_LINK_RELATIONS_REGISTRY . $key]; } else { $this->data['links'][self::IANA_LINK_RELATIONS_REGISTRY . $key] = &$this->data['links'][$key]; } } elseif (substr($key, 0, 41) === self::IANA_LINK_RELATIONS_REGISTRY) { $this->data['links'][substr($key, 41)] = &$this->data['links'][$key]; } $this->data['links'][$key] = array_unique($this->data['links'][$key]); } } if (isset($this->data['headers']['link'])) { $link_headers = $this->data['headers']['link']; if (is_array($link_headers)) { $link_headers = implode(',', $link_headers); } // https://datatracker.ietf.org/doc/html/rfc8288 if (is_string($link_headers) && preg_match_all('/<(?P<uri>[^>]+)>\s*;\s*rel\s*=\s*(?P<quote>"?)' . preg_quote($rel) . '(?P=quote)\s*(?=,|$)/i', $link_headers, $matches)) { return $matches['uri']; } } if (isset($this->data['links'][$rel])) { return $this->data['links'][$rel]; } return null; } /** * @return ?array<Response> */ public function get_all_discovered_feeds() { return $this->all_discovered_feeds; } /** * Get the content for the item * * Uses `<atom:subtitle>`, `<atom:tagline>`, `<description>`, * `<dc:description>`, `<itunes:summary>` or `<itunes:subtitle>` * * @since 1.0 (previously called `get_feed_description()` since 0.8) * @return string|null */ public function get_description() { if ($return = $this->get_channel_tags(self::NAMESPACE_ATOM_10, 'subtitle')) { return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_ATOM_03, 'tagline')) { return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_RSS_10, 'description')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_RSS_090, 'description')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_RSS_20, 'description')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_DC_11, 'description')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_DC_10, 'description')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_ITUNES, 'summary')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_ITUNES, 'subtitle')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_HTML, $this->get_base($return[0])); } return null; } /** * Get the copyright info for the feed * * Uses `<atom:rights>`, `<atom:copyright>` or `<dc:rights>` * * @since 1.0 (previously called `get_feed_copyright()` since 0.8) * @return string|null */ public function get_copyright() { if ($return = $this->get_channel_tags(self::NAMESPACE_ATOM_10, 'rights')) { return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_ATOM_03, 'copyright')) { return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_RSS_20, 'copyright')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_DC_11, 'rights')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_DC_10, 'rights')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } return null; } /** * Get the language for the feed * * Uses `<language>`, `<dc:language>`, or @xml_lang * * @since 1.0 (previously called `get_feed_language()` since 0.8) * @return string|null */ public function get_language() { if ($return = $this->get_channel_tags(self::NAMESPACE_RSS_20, 'language')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_DC_11, 'language')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_DC_10, 'language')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } elseif (isset($this->data['child'][self::NAMESPACE_ATOM_10]['feed'][0]['xml_lang'])) { return $this->sanitize($this->data['child'][self::NAMESPACE_ATOM_10]['feed'][0]['xml_lang'], self::CONSTRUCT_TEXT); } elseif (isset($this->data['child'][self::NAMESPACE_ATOM_03]['feed'][0]['xml_lang'])) { return $this->sanitize($this->data['child'][self::NAMESPACE_ATOM_03]['feed'][0]['xml_lang'], self::CONSTRUCT_TEXT); } elseif (isset($this->data['child'][self::NAMESPACE_RDF]['RDF'][0]['xml_lang'])) { return $this->sanitize($this->data['child'][self::NAMESPACE_RDF]['RDF'][0]['xml_lang'], self::CONSTRUCT_TEXT); } elseif (isset($this->data['headers']['content-language'])) { return $this->sanitize($this->data['headers']['content-language'], self::CONSTRUCT_TEXT); } return null; } /** * Get the latitude coordinates for the item * * Compatible with the W3C WGS84 Basic Geo and GeoRSS specifications * * Uses `<geo:lat>` or `<georss:point>` * * @since 1.0 * @link http://www.w3.org/2003/01/geo/ W3C WGS84 Basic Geo * @link http://www.georss.org/ GeoRSS * @return float|null */ public function get_latitude() { if ($return = $this->get_channel_tags(self::NAMESPACE_W3C_BASIC_GEO, 'lat')) { return (float) $return[0]['data']; } elseif (($return = $this->get_channel_tags(self::NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match)) { return (float) $match[1]; } return null; } /** * Get the longitude coordinates for the feed * * Compatible with the W3C WGS84 Basic Geo and GeoRSS specifications * * Uses `<geo:long>`, `<geo:lon>` or `<georss:point>` * * @since 1.0 * @link http://www.w3.org/2003/01/geo/ W3C WGS84 Basic Geo * @link http://www.georss.org/ GeoRSS * @return float|null */ public function get_longitude() { if ($return = $this->get_channel_tags(self::NAMESPACE_W3C_BASIC_GEO, 'long')) { return (float) $return[0]['data']; } elseif ($return = $this->get_channel_tags(self::NAMESPACE_W3C_BASIC_GEO, 'lon')) { return (float) $return[0]['data']; } elseif (($return = $this->get_channel_tags(self::NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match)) { return (float) $match[2]; } return null; } /** * Get the feed logo's title * * RSS 0.9.0, 1.0 and 2.0 feeds are allowed to have a "feed logo" title. * * Uses `<image><title>` or `<image><dc:title>` * * @return string|null */ public function get_image_title() { if ($return = $this->get_image_tags(self::NAMESPACE_RSS_10, 'title')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } elseif ($return = $this->get_image_tags(self::NAMESPACE_RSS_090, 'title')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } elseif ($return = $this->get_image_tags(self::NAMESPACE_RSS_20, 'title')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } elseif ($return = $this->get_image_tags(self::NAMESPACE_DC_11, 'title')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } elseif ($return = $this->get_image_tags(self::NAMESPACE_DC_10, 'title')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_TEXT); } return null; } /** * Get the feed logo's URL * * RSS 0.9.0, 2.0, Atom 1.0, and feeds with iTunes RSS tags are allowed to * have a "feed logo" URL. This points directly to the image itself. * * Uses `<itunes:image>`, `<atom:logo>`, `<atom:icon>`, * `<image><title>` or `<image><dc:title>` * * @return string|null */ public function get_image_url() { if ($return = $this->get_channel_tags(self::NAMESPACE_ITUNES, 'image')) { return $this->sanitize($return[0]['attribs']['']['href'], self::CONSTRUCT_IRI); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_ATOM_10, 'logo')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_IRI, $this->get_base($return[0])); } elseif ($return = $this->get_channel_tags(self::NAMESPACE_ATOM_10, 'icon')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_IRI, $this->get_base($return[0])); } elseif ($return = $this->get_image_tags(self::NAMESPACE_RSS_10, 'url')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_IRI, $this->get_base($return[0])); } elseif ($return = $this->get_image_tags(self::NAMESPACE_RSS_090, 'url')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_IRI, $this->get_base($return[0])); } elseif ($return = $this->get_image_tags(self::NAMESPACE_RSS_20, 'url')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_IRI, $this->get_base($return[0])); } return null; } /** * Get the feed logo's link * * RSS 0.9.0, 1.0 and 2.0 feeds are allowed to have a "feed logo" link. This * points to a human-readable page that the image should link to. * * Uses `<itunes:image>`, `<atom:logo>`, `<atom:icon>`, * `<image><title>` or `<image><dc:title>` * * @return string|null */ public function get_image_link() { if ($return = $this->get_image_tags(self::NAMESPACE_RSS_10, 'link')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_IRI, $this->get_base($return[0])); } elseif ($return = $this->get_image_tags(self::NAMESPACE_RSS_090, 'link')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_IRI, $this->get_base($return[0])); } elseif ($return = $this->get_image_tags(self::NAMESPACE_RSS_20, 'link')) { return $this->sanitize($return[0]['data'], self::CONSTRUCT_IRI, $this->get_base($return[0])); } return null; } /** * Get the feed logo's link * * RSS 2.0 feeds are allowed to have a "feed logo" width. * * Uses `<image><width>` or defaults to 88 if no width is specified and * the feed is an RSS 2.0 feed. * * @return int|null */ public function get_image_width() { if ($return = $this->get_image_tags(self::NAMESPACE_RSS_20, 'width')) { return intval($return[0]['data']); } elseif ($this->get_type() & self::TYPE_RSS_SYNDICATION && $this->get_image_tags(self::NAMESPACE_RSS_20, 'url')) { return 88; } return null; } /** * Get the feed logo's height * * RSS 2.0 feeds are allowed to have a "feed logo" height. * * Uses `<image><height>` or defaults to 31 if no height is specified and * the feed is an RSS 2.0 feed. * * @return int|null */ public function get_image_height() { if ($return = $this->get_image_tags(self::NAMESPACE_RSS_20, 'height')) { return intval($return[0]['data']); } elseif ($this->get_type() & self::TYPE_RSS_SYNDICATION && $this->get_image_tags(self::NAMESPACE_RSS_20, 'url')) { return 31; } return null; } /** * Get the number of items in the feed * * This is well-suited for {@link http://php.net/for for()} loops with * {@see get_item()} * * @param int $max Maximum value to return. 0 for no limit * @return int Number of items in the feed */ public function get_item_quantity(int $max = 0) { $qty = count($this->get_items()); if ($max === 0) { return $qty; } return min($qty, $max); } /** * Get a single item from the feed * * This is better suited for {@link http://php.net/for for()} loops, whereas * {@see get_items()} is better suited for * {@link http://php.net/foreach foreach()} loops. * * @see get_item_quantity() * @since Beta 2 * @param int $key The item that you want to return. Remember that arrays begin with 0, not 1 * @return Item|null */ public function get_item(int $key = 0) { $items = $this->get_items(); if (isset($items[$key])) { return $items[$key]; } return null; } /** * Get all items from the feed * * This is better suited for {@link http://php.net/for for()} loops, whereas * {@see get_items()} is better suited for * {@link http://php.net/foreach foreach()} loops. * * @see get_item_quantity * @since Beta 2 * @param int $start Index to start at * @param int $end Number of items to return. 0 for all items after `$start` * @return Item[] List of {@see Item} objects */ public function get_items(int $start = 0, int $end = 0) { if (!isset($this->data['items'])) { if (!empty($this->multifeed_objects)) { $this->data['items'] = SimplePie::merge_items($this->multifeed_objects, $start, $end, $this->item_limit); if (empty($this->data['items'])) { return []; } return $this->data['items']; } $this->data['items'] = []; if ($items = $this->get_feed_tags(self::NAMESPACE_ATOM_10, 'entry')) { $keys = array_keys($items); foreach ($keys as $key) { $this->data['items'][] = $this->make_item($items[$key]); } } if ($items = $this->get_feed_tags(self::NAMESPACE_ATOM_03, 'entry')) { $keys = array_keys($items); foreach ($keys as $key) { $this->data['items'][] = $this->make_item($items[$key]); } } if ($items = $this->get_feed_tags(self::NAMESPACE_RSS_10, 'item')) { $keys = array_keys($items); foreach ($keys as $key) { $this->data['items'][] = $this->make_item($items[$key]); } } if ($items = $this->get_feed_tags(self::NAMESPACE_RSS_090, 'item')) { $keys = array_keys($items); foreach ($keys as $key) { $this->data['items'][] = $this->make_item($items[$key]); } } if ($items = $this->get_channel_tags(self::NAMESPACE_RSS_20, 'item')) { $keys = array_keys($items); foreach ($keys as $key) { $this->data['items'][] = $this->make_item($items[$key]); } } } if (empty($this->data['items'])) { return []; } if ($this->order_by_date) { if (!isset($this->data['ordered_items'])) { $this->data['ordered_items'] = $this->data['items']; usort($this->data['ordered_items'], [get_class($this), 'sort_items']); } $items = $this->data['ordered_items']; } else { $items = $this->data['items']; } // Slice the data as desired if ($end === 0) { return array_slice($items, $start); } return array_slice($items, $start, $end); } /** * Set the favicon handler * * @deprecated Use your own favicon handling instead * @param string|false $page * @return bool */ public function set_favicon_handler($page = false, string $qs = 'i') { trigger_error('Favicon handling has been removed since SimplePie 1.3, please use your own handling', \E_USER_DEPRECATED); return false; } /** * Get the favicon for the current feed * * @deprecated Use your own favicon handling instead * @return string|bool */ public function get_favicon() { trigger_error('Favicon handling has been removed since SimplePie 1.3, please use your own handling', \E_USER_DEPRECATED); if (($url = $this->get_link()) !== null) { return 'https://www.google.com/s2/favicons?domain=' . urlencode($url); } return false; } /** * Magic method handler * * @param string $method Method name * @param array<mixed> $args Arguments to the method * @return mixed */ public function __call(string $method, array $args) { if (strpos($method, 'subscribe_') === 0) { trigger_error('subscribe_*() has been deprecated since SimplePie 1.3, implement the callback yourself', \E_USER_DEPRECATED); return ''; } if ($method === 'enable_xml_dump') { trigger_error('enable_xml_dump() has been deprecated since SimplePie 1.3, use get_raw_data() instead', \E_USER_DEPRECATED); return false; } $class = get_class($this); $trace = debug_backtrace(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection $file = $trace[0]['file'] ?? ''; $line = $trace[0]['line'] ?? ''; throw new SimplePieException("Call to undefined method $class::$method() in $file on line $line"); } /** * Item factory * * @param array<string, mixed> $data */ private function make_item(array $data): Item { $item = $this->registry->create(Item::class, [$this, $data]); $item->set_sanitize($this->sanitize); return $item; } /** * Sorting callback for items * * @access private * @param Item $a * @param Item $b * @return -1|0|1 */ public static function sort_items(Item $a, Item $b) { $a_date = $a->get_date('U'); $b_date = $b->get_date('U'); if ($a_date && $b_date) { return $a_date > $b_date ? -1 : 1; } // Sort items without dates to the top. if ($a_date) { return 1; } if ($b_date) { return -1; } return 0; } /** * Merge items from several feeds into one * * If you're merging multiple feeds together, they need to all have dates * for the items or else SimplePie will refuse to sort them. * * @link http://simplepie.org/wiki/tutorial/sort_multiple_feeds_by_time_and_date#if_feeds_require_separate_per-feed_settings * @param array<SimplePie> $urls List of SimplePie feed objects to merge * @param int $start Starting item * @param int $end Number of items to return * @param int $limit Maximum number of items per feed * @return array<Item> */ public static function merge_items(array $urls, int $start = 0, int $end = 0, int $limit = 0) { if (count($urls) > 0) { $items = []; foreach ($urls as $arg) { if ($arg instanceof SimplePie) { $items = array_merge($items, $arg->get_items(0, $limit)); // @phpstan-ignore-next-line Enforce PHPDoc type. } else { trigger_error('Arguments must be SimplePie objects', E_USER_WARNING); } } usort($items, [get_class($urls[0]), 'sort_items']); if ($end === 0) { return array_slice($items, $start); } return array_slice($items, $start, $end); } trigger_error('Cannot merge zero SimplePie objects', E_USER_WARNING); return []; } /** * Store PubSubHubbub links as headers * * There is no way to find PuSH links in the body of a microformats feed, * so they are added to the headers when found, to be used later by get_links. */ private function store_links(Response $file, ?string $hub, ?string $self): Response { $linkHeaderLine = $file->get_header_line('link'); $linkHeader = $file->get_header('link'); if ($hub && !preg_match('/rel=hub/', $linkHeaderLine)) { $linkHeader[] = '<'.$hub.'>; rel=hub'; } if ($self && !preg_match('/rel=self/', $linkHeaderLine)) { $linkHeader[] = '<'.$self.'>; rel=self'; } if (count($linkHeader) > 0) { $file = $file->with_header('link', $linkHeader); } return $file; } /** * Get a DataCache * * @param string $feed_url Only needed for BC, can be removed in SimplePie 2.0.0 * * @return DataCache */ private function get_cache(string $feed_url = ''): DataCache { if ($this->cache === null) { // @trigger_error(sprintf('Not providing as PSR-16 cache implementation is deprecated since SimplePie 1.8.0, please use "SimplePie\SimplePie::set_cache()".'), \E_USER_DEPRECATED); $cache = $this->registry->call(Cache::class, 'get_handler', [ $this->cache_location, $this->get_cache_filename($feed_url), Base::TYPE_FEED ]); return new BaseDataCache($cache); } return $this->cache; } /** * Get a HTTP client */ private function get_http_client(): Client { if ($this->http_client === null) { $this->http_client = new FileClient( $this->get_registry(), [ 'timeout' => $this->timeout, 'redirects' => 5, 'useragent' => $this->useragent, 'force_fsockopen' => $this->force_fsockopen, 'curl_options' => $this->curl_options, ] ); $this->http_client_injected = true; } return $this->http_client; } } class_alias('SimplePie\SimplePie', 'SimplePie'); Parse/Date.php 0000644 00000062206 15162130151 0007205 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\Parse; /** * Date Parser */ class Date { /** * Input data * * @access protected * @var string */ public $date; /** * List of days, calendar day name => ordinal day number in the week * * @access protected * @var array<string, int<1,7>> */ public $day = [ // English 'mon' => 1, 'monday' => 1, 'tue' => 2, 'tuesday' => 2, 'wed' => 3, 'wednesday' => 3, 'thu' => 4, 'thursday' => 4, 'fri' => 5, 'friday' => 5, 'sat' => 6, 'saturday' => 6, 'sun' => 7, 'sunday' => 7, // Dutch 'maandag' => 1, 'dinsdag' => 2, 'woensdag' => 3, 'donderdag' => 4, 'vrijdag' => 5, 'zaterdag' => 6, 'zondag' => 7, // French 'lundi' => 1, 'mardi' => 2, 'mercredi' => 3, 'jeudi' => 4, 'vendredi' => 5, 'samedi' => 6, 'dimanche' => 7, // German 'montag' => 1, 'mo' => 1, 'dienstag' => 2, 'di' => 2, 'mittwoch' => 3, 'mi' => 3, 'donnerstag' => 4, 'do' => 4, 'freitag' => 5, 'fr' => 5, 'samstag' => 6, 'sa' => 6, 'sonnabend' => 6, // AFAIK no short form for sonnabend 'so' => 7, 'sonntag' => 7, // Italian 'lunedì' => 1, 'martedì' => 2, 'mercoledì' => 3, 'giovedì' => 4, 'venerdì' => 5, 'sabato' => 6, 'domenica' => 7, // Spanish 'lunes' => 1, 'martes' => 2, 'miércoles' => 3, 'jueves' => 4, 'viernes' => 5, 'sábado' => 6, 'domingo' => 7, // Finnish 'maanantai' => 1, 'tiistai' => 2, 'keskiviikko' => 3, 'torstai' => 4, 'perjantai' => 5, 'lauantai' => 6, 'sunnuntai' => 7, // Hungarian 'hétfő' => 1, 'kedd' => 2, 'szerda' => 3, 'csütörtok' => 4, 'péntek' => 5, 'szombat' => 6, 'vasárnap' => 7, // Greek 'Δευ' => 1, 'Τρι' => 2, 'Τετ' => 3, 'Πεμ' => 4, 'Παρ' => 5, 'Σαβ' => 6, 'Κυρ' => 7, // Russian 'Пн.' => 1, 'Вт.' => 2, 'Ср.' => 3, 'Чт.' => 4, 'Пт.' => 5, 'Сб.' => 6, 'Вс.' => 7, ]; /** * List of months, calendar month name => calendar month number * * @access protected * @var array<string, int<1,12>> */ public $month = [ // English 'jan' => 1, 'january' => 1, 'feb' => 2, 'february' => 2, 'mar' => 3, 'march' => 3, 'apr' => 4, 'april' => 4, 'may' => 5, // No long form of May 'jun' => 6, 'june' => 6, 'jul' => 7, 'july' => 7, 'aug' => 8, 'august' => 8, 'sep' => 9, 'september' => 9, 'oct' => 10, 'october' => 10, 'nov' => 11, 'november' => 11, 'dec' => 12, 'december' => 12, // Dutch 'januari' => 1, 'februari' => 2, 'maart' => 3, // 'april' => 4, 'mei' => 5, 'juni' => 6, 'juli' => 7, 'augustus' => 8, // 'september' => 9, 'oktober' => 10, // 'november' => 11, // 'december' => 12, // French 'janvier' => 1, 'février' => 2, 'mars' => 3, 'avril' => 4, 'mai' => 5, 'juin' => 6, 'juillet' => 7, 'août' => 8, 'septembre' => 9, 'octobre' => 10, 'novembre' => 11, 'décembre' => 12, // German 'januar' => 1, // 'jan' => 1, 'februar' => 2, // 'feb' => 2, 'märz' => 3, 'mär' => 3, // 'april' => 4, // 'apr' => 4, // 'mai' => 5, // no short form for may // 'juni' => 6, // 'jun' => 6, // 'juli' => 7, // 'jul' => 7, // 'august' => 8, // 'aug' => 8, // 'september' => 9, // 'sep' => 9, // 'oktober' => 10, 'okt' => 10, // 'november' => 11, // 'nov' => 11, 'dezember' => 12, 'dez' => 12, // Italian 'gennaio' => 1, 'febbraio' => 2, 'marzo' => 3, 'aprile' => 4, 'maggio' => 5, 'giugno' => 6, 'luglio' => 7, 'agosto' => 8, 'settembre' => 9, 'ottobre' => 10, // 'novembre' => 11, 'dicembre' => 12, // Spanish 'enero' => 1, 'febrero' => 2, // 'marzo' => 3, 'abril' => 4, 'mayo' => 5, 'junio' => 6, 'julio' => 7, // 'agosto' => 8, 'septiembre' => 9, 'setiembre' => 9, 'octubre' => 10, 'noviembre' => 11, 'diciembre' => 12, // Finnish 'tammikuu' => 1, 'helmikuu' => 2, 'maaliskuu' => 3, 'huhtikuu' => 4, 'toukokuu' => 5, 'kesäkuu' => 6, 'heinäkuu' => 7, 'elokuu' => 8, 'suuskuu' => 9, 'lokakuu' => 10, 'marras' => 11, 'joulukuu' => 12, // Hungarian 'január' => 1, 'február' => 2, 'március' => 3, 'április' => 4, 'május' => 5, 'június' => 6, 'július' => 7, 'augusztus' => 8, 'szeptember' => 9, 'október' => 10, // 'november' => 11, // 'december' => 12, // Greek 'Ιαν' => 1, 'Φεβ' => 2, 'Μάώ' => 3, 'Μαώ' => 3, 'Απρ' => 4, 'Μάι' => 5, 'Μαϊ' => 5, 'Μαι' => 5, 'Ιούν' => 6, 'Ιον' => 6, 'Ιούλ' => 7, 'Ιολ' => 7, 'Αύγ' => 8, 'Αυγ' => 8, 'Σεπ' => 9, 'Οκτ' => 10, 'Νοέ' => 11, 'Δεκ' => 12, // Russian 'Янв' => 1, 'января' => 1, 'Фев' => 2, 'февраля' => 2, 'Мар' => 3, 'марта' => 3, 'Апр' => 4, 'апреля' => 4, 'Май' => 5, 'мая' => 5, 'Июн' => 6, 'июня' => 6, 'Июл' => 7, 'июля' => 7, 'Авг' => 8, 'августа' => 8, 'Сен' => 9, 'сентября' => 9, 'Окт' => 10, 'октября' => 10, 'Ноя' => 11, 'ноября' => 11, 'Дек' => 12, 'декабря' => 12, ]; /** * List of timezones, abbreviation => offset from UTC * * @access protected * @var array<string, int> */ public $timezone = [ 'ACDT' => 37800, 'ACIT' => 28800, 'ACST' => 34200, 'ACT' => -18000, 'ACWDT' => 35100, 'ACWST' => 31500, 'AEDT' => 39600, 'AEST' => 36000, 'AFT' => 16200, 'AKDT' => -28800, 'AKST' => -32400, 'AMDT' => 18000, 'AMT' => -14400, 'ANAST' => 46800, 'ANAT' => 43200, 'ART' => -10800, 'AZOST' => -3600, 'AZST' => 18000, 'AZT' => 14400, 'BIOT' => 21600, 'BIT' => -43200, 'BOT' => -14400, 'BRST' => -7200, 'BRT' => -10800, 'BST' => 3600, 'BTT' => 21600, 'CAST' => 18000, 'CAT' => 7200, 'CCT' => 23400, 'CDT' => -18000, 'CEDT' => 7200, 'CEST' => 7200, 'CET' => 3600, 'CGST' => -7200, 'CGT' => -10800, 'CHADT' => 49500, 'CHAST' => 45900, 'CIST' => -28800, 'CKT' => -36000, 'CLDT' => -10800, 'CLST' => -14400, 'COT' => -18000, 'CST' => -21600, 'CVT' => -3600, 'CXT' => 25200, 'DAVT' => 25200, 'DTAT' => 36000, 'EADT' => -18000, 'EAST' => -21600, 'EAT' => 10800, 'ECT' => -18000, 'EDT' => -14400, 'EEST' => 10800, 'EET' => 7200, 'EGT' => -3600, 'EKST' => 21600, 'EST' => -18000, 'FJT' => 43200, 'FKDT' => -10800, 'FKST' => -14400, 'FNT' => -7200, 'GALT' => -21600, 'GEDT' => 14400, 'GEST' => 10800, 'GFT' => -10800, 'GILT' => 43200, 'GIT' => -32400, 'GST' => 14400, // 'GST' => -7200, 'GYT' => -14400, 'HAA' => -10800, 'HAC' => -18000, 'HADT' => -32400, 'HAE' => -14400, 'HAP' => -25200, 'HAR' => -21600, 'HAST' => -36000, 'HAT' => -9000, 'HAY' => -28800, 'HKST' => 28800, 'HMT' => 18000, 'HNA' => -14400, 'HNC' => -21600, 'HNE' => -18000, 'HNP' => -28800, 'HNR' => -25200, 'HNT' => -12600, 'HNY' => -32400, 'IRDT' => 16200, 'IRKST' => 32400, 'IRKT' => 28800, 'IRST' => 12600, 'JFDT' => -10800, 'JFST' => -14400, 'JST' => 32400, 'KGST' => 21600, 'KGT' => 18000, 'KOST' => 39600, 'KOVST' => 28800, 'KOVT' => 25200, 'KRAST' => 28800, 'KRAT' => 25200, 'KST' => 32400, 'LHDT' => 39600, 'LHST' => 37800, 'LINT' => 50400, 'LKT' => 21600, 'MAGST' => 43200, 'MAGT' => 39600, 'MAWT' => 21600, 'MDT' => -21600, 'MESZ' => 7200, 'MEZ' => 3600, 'MHT' => 43200, 'MIT' => -34200, 'MNST' => 32400, 'MSDT' => 14400, 'MSST' => 10800, 'MST' => -25200, 'MUT' => 14400, 'MVT' => 18000, 'MYT' => 28800, 'NCT' => 39600, 'NDT' => -9000, 'NFT' => 41400, 'NMIT' => 36000, 'NOVST' => 25200, 'NOVT' => 21600, 'NPT' => 20700, 'NRT' => 43200, 'NST' => -12600, 'NUT' => -39600, 'NZDT' => 46800, 'NZST' => 43200, 'OMSST' => 25200, 'OMST' => 21600, 'PDT' => -25200, 'PET' => -18000, 'PETST' => 46800, 'PETT' => 43200, 'PGT' => 36000, 'PHOT' => 46800, 'PHT' => 28800, 'PKT' => 18000, 'PMDT' => -7200, 'PMST' => -10800, 'PONT' => 39600, 'PST' => -28800, 'PWT' => 32400, 'PYST' => -10800, 'PYT' => -14400, 'RET' => 14400, 'ROTT' => -10800, 'SAMST' => 18000, 'SAMT' => 14400, 'SAST' => 7200, 'SBT' => 39600, 'SCDT' => 46800, 'SCST' => 43200, 'SCT' => 14400, 'SEST' => 3600, 'SGT' => 28800, 'SIT' => 28800, 'SRT' => -10800, 'SST' => -39600, 'SYST' => 10800, 'SYT' => 7200, 'TFT' => 18000, 'THAT' => -36000, 'TJT' => 18000, 'TKT' => -36000, 'TMT' => 18000, 'TOT' => 46800, 'TPT' => 32400, 'TRUT' => 36000, 'TVT' => 43200, 'TWT' => 28800, 'UYST' => -7200, 'UYT' => -10800, 'UZT' => 18000, 'VET' => -14400, 'VLAST' => 39600, 'VLAT' => 36000, 'VOST' => 21600, 'VUT' => 39600, 'WAST' => 7200, 'WAT' => 3600, 'WDT' => 32400, 'WEST' => 3600, 'WFT' => 43200, 'WIB' => 25200, 'WIT' => 32400, 'WITA' => 28800, 'WKST' => 18000, 'WST' => 28800, 'YAKST' => 36000, 'YAKT' => 32400, 'YAPT' => 36000, 'YEKST' => 21600, 'YEKT' => 18000, ]; /** * Cached PCRE for Date::$day * * @access protected * @var string */ public $day_pcre; /** * Cached PCRE for Date::$month * * @access protected * @var string */ public $month_pcre; /** * Array of user-added callback methods * * @access private * @var array<string> */ public $built_in = []; /** * Array of user-added callback methods * * @access private * @var array<callable(string): (int|false)> */ public $user = []; /** * Create new Date object, and set self::day_pcre, * self::month_pcre, and self::built_in * * @access private */ public function __construct() { $this->day_pcre = '(' . implode('|', array_keys($this->day)) . ')'; $this->month_pcre = '(' . implode('|', array_keys($this->month)) . ')'; static $cache; if (!isset($cache[get_class($this)])) { $all_methods = get_class_methods($this); foreach ($all_methods as $method) { if (strtolower(substr($method, 0, 5)) === 'date_') { $cache[get_class($this)][] = $method; } } } foreach ($cache[get_class($this)] as $method) { $this->built_in[] = $method; } } /** * Get the object * * @access public * @return Date */ public static function get() { static $object; if (!$object) { $object = new Date(); } return $object; } /** * Parse a date * * @final * @access public * @param string $date Date to parse * @return int|false Timestamp corresponding to date string, or false on failure */ public function parse(string $date) { foreach ($this->user as $method) { if (($returned = call_user_func($method, $date)) !== false) { return (int) $returned; } } foreach ($this->built_in as $method) { // TODO: we should really check this in constructor but that would require private properties. /** @var callable(string): (int|false) */ $callable = [$this, $method]; if (($returned = call_user_func($callable, $date)) !== false) { return $returned; } } return false; } /** * Add a callback method to parse a date * * @final * @access public * @param callable $callback * @return void */ public function add_callback(callable $callback) { $this->user[] = $callback; } /** * Parse a superset of W3C-DTF (allows hyphens and colons to be omitted, as * well as allowing any of upper or lower case "T", horizontal tabs, or * spaces to be used as the time separator (including more than one)) * * @access protected * @param string $date * @return int|false Timestamp */ public function date_w3cdtf(string $date) { $pcre = <<<'PCRE' / ^ (?P<year>[0-9]{4}) (?: -? (?P<month>[0-9]{2}) (?: -? (?P<day>[0-9]{2}) (?: [Tt\x09\x20]+ (?P<hour>[0-9]{2}) (?: :? (?P<minute>[0-9]{2}) (?: :? (?P<second>[0-9]{2}) (?: . (?P<second_fraction>[0-9]*) )? )? )? (?: (?P<zulu>Z) | (?P<tz_sign>[+\-]) (?P<tz_hour>[0-9]{1,2}) :? (?P<tz_minute>[0-9]{1,2}) ) )? )? )? $ /x PCRE; if (preg_match($pcre, $date, $match)) { // Fill in empty matches and convert to proper types. $year = (int) $match['year']; $month = isset($match['month']) ? (int) $match['month'] : 1; $day = isset($match['day']) ? (int) $match['day'] : 1; $hour = isset($match['hour']) ? (int) $match['hour'] : 0; $minute = isset($match['minute']) ? (int) $match['minute'] : 0; $second = isset($match['second']) ? (int) $match['second'] : 0; $second_fraction = isset($match['second_fraction']) ? ((int) $match['second_fraction']) / (10 ** strlen($match['second_fraction'])) : 0; $tz_sign = ($match['tz_sign'] ?? '') === '-' ? -1 : 1; $tz_hour = isset($match['tz_hour']) ? (int) $match['tz_hour'] : 0; $tz_minute = isset($match['tz_minute']) ? (int) $match['tz_minute'] : 0; // Numeric timezone $timezone = $tz_hour * 3600; $timezone += $tz_minute * 60; $timezone *= $tz_sign; // Convert the number of seconds to an integer, taking decimals into account $second = (int) round($second + $second_fraction); return gmmktime($hour, $minute, $second, $month, $day, $year) - $timezone; } return false; } /** * Remove RFC822 comments * * @access protected * @param string $string Data to strip comments from * @return string Comment stripped string */ public function remove_rfc2822_comments(string $string) { $position = 0; $length = strlen($string); $depth = 0; $output = ''; while ($position < $length && ($pos = strpos($string, '(', $position)) !== false) { $output .= substr($string, $position, $pos - $position); $position = $pos + 1; if ($pos === 0 || $string[$pos - 1] !== '\\') { $depth++; while ($depth && $position < $length) { $position += strcspn($string, '()', $position); if ($string[$position - 1] === '\\') { $position++; continue; } elseif (isset($string[$position])) { switch ($string[$position]) { case '(': $depth++; break; case ')': $depth--; break; } $position++; } else { break; } } } else { $output .= '('; } } $output .= substr($string, $position); return $output; } /** * Parse RFC2822's date format * * @access protected * @param string $date * @return int|false Timestamp */ public function date_rfc2822(string $date) { static $pcre; if (!$pcre) { $wsp = '[\x09\x20]'; $fws = '(?:' . $wsp . '+|' . $wsp . '*(?:\x0D\x0A' . $wsp . '+)+)'; $optional_fws = $fws . '?'; $day_name = $this->day_pcre; $month = $this->month_pcre; $day = '([0-9]{1,2})'; $hour = $minute = $second = '([0-9]{2})'; $year = '([0-9]{2,4})'; $num_zone = '([+\-])([0-9]{2})([0-9]{2})'; $character_zone = '([A-Z]{1,5})'; $zone = '(?:' . $num_zone . '|' . $character_zone . ')'; $pcre = '/(?:' . $optional_fws . $day_name . $optional_fws . ',)?' . $optional_fws . $day . $fws . $month . $fws . $year . $fws . $hour . $optional_fws . ':' . $optional_fws . $minute . '(?:' . $optional_fws . ':' . $optional_fws . $second . ')?' . $fws . $zone . '/i'; } if (preg_match($pcre, $this->remove_rfc2822_comments($date), $match)) { /* Capturing subpatterns: 1: Day name 2: Day 3: Month 4: Year 5: Hour 6: Minute 7: Second 8: Timezone ± 9: Timezone hours 10: Timezone minutes 11: Alphabetic timezone */ $day = (int) $match[2]; // Find the month number $month = $this->month[strtolower($match[3])]; $year = (int) $match[4]; $hour = (int) $match[5]; $minute = (int) $match[6]; // Second is optional, if it is empty set it to zero $second = (int) $match[7]; $tz_sign = $match[8]; $tz_hour = (int) $match[9]; $tz_minute = (int) $match[10]; $tz_code = isset($match[11]) ? strtoupper($match[11]) : ''; // Numeric timezone if ($tz_sign !== '') { $timezone = $tz_hour * 3600; $timezone += $tz_minute * 60; if ($tz_sign === '-') { $timezone = 0 - $timezone; } } // Character timezone elseif (isset($this->timezone[$tz_code])) { $timezone = $this->timezone[$tz_code]; } // Assume everything else to be -0000 else { $timezone = 0; } // Deal with 2/3 digit years if ($year < 50) { $year += 2000; } elseif ($year < 1000) { $year += 1900; } return gmmktime($hour, $minute, $second, $month, $day, $year) - $timezone; } return false; } /** * Parse RFC850's date format * * @access protected * @param string $date * @return int|false Timestamp */ public function date_rfc850(string $date) { static $pcre; if (!$pcre) { $space = '[\x09\x20]+'; $day_name = $this->day_pcre; $month = $this->month_pcre; $day = '([0-9]{1,2})'; $year = $hour = $minute = $second = '([0-9]{2})'; $zone = '([A-Z]{1,5})'; $pcre = '/^' . $day_name . ',' . $space . $day . '-' . $month . '-' . $year . $space . $hour . ':' . $minute . ':' . $second . $space . $zone . '$/i'; } if (preg_match($pcre, $date, $match)) { /* Capturing subpatterns: 1: Day name 2: Day 3: Month 4: Year 5: Hour 6: Minute 7: Second 8: Timezone */ $day = (int) $match[2]; // Month $month = $this->month[strtolower($match[3])]; $year = (int) $match[4]; $hour = (int) $match[5]; $minute = (int) $match[6]; // Second is optional, if it is empty set it to zero $second = (int) $match[7]; $tz_code = strtoupper($match[8]); // Character timezone if (isset($this->timezone[$tz_code])) { $timezone = $this->timezone[$tz_code]; } // Assume everything else to be -0000 else { $timezone = 0; } // Deal with 2 digit year if ($year < 50) { $year += 2000; } else { $year += 1900; } return gmmktime($hour, $minute, $second, $month, $day, $year) - $timezone; } return false; } /** * Parse C99's asctime()'s date format * * @access protected * @param string $date * @return int|false Timestamp */ public function date_asctime(string $date) { static $pcre; if (!$pcre) { $space = '[\x09\x20]+'; $wday_name = $this->day_pcre; $mon_name = $this->month_pcre; $day = '([0-9]{1,2})'; $hour = $sec = $min = '([0-9]{2})'; $year = '([0-9]{4})'; $terminator = '\x0A?\x00?'; $pcre = '/^' . $wday_name . $space . $mon_name . $space . $day . $space . $hour . ':' . $min . ':' . $sec . $space . $year . $terminator . '$/i'; } if (preg_match($pcre, $date, $match)) { /* Capturing subpatterns: 1: Day name 2: Month 3: Day 4: Hour 5: Minute 6: Second 7: Year */ $month = $this->month[strtolower($match[2])]; return gmmktime((int) $match[4], (int) $match[5], (int) $match[6], $month, (int) $match[3], (int) $match[7]); } return false; } /** * Parse dates using strtotime() * * @access protected * @param string $date * @return int|false Timestamp */ public function date_strtotime(string $date) { $strtotime = strtotime($date); if ($strtotime === -1 || $strtotime === false) { return false; } return $strtotime; } } class_alias('SimplePie\Parse\Date', 'SimplePie_Parse_Date'); Enclosure.php 0000644 00000076616 15162130153 0007231 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; /** * Handles everything related to enclosures (including Media RSS and iTunes RSS) * * Used by {@see \SimplePie\Item::get_enclosure()} and {@see \SimplePie\Item::get_enclosures()} * * This class can be overloaded with {@see \SimplePie\SimplePie::set_enclosure_class()} */ class Enclosure { /** * @var ?string * @see get_bitrate() */ public $bitrate; /** * @var Caption[]|null * @see get_captions() */ public $captions; /** * @var Category[]|null * @see get_categories() */ public $categories; /** * @var ?int * @see get_channels() */ public $channels; /** * @var ?Copyright * @see get_copyright() */ public $copyright; /** * @var Credit[]|null * @see get_credits() */ public $credits; /** * @var ?string * @see get_description() */ public $description; /** * @var ?int * @see get_duration() */ public $duration; /** * @var ?string * @see get_expression() */ public $expression; /** * @var ?string * @see get_framerate() */ public $framerate; /** * @var ?string * @see get_handler() */ public $handler; /** * @var string[]|null * @see get_hashes() */ public $hashes; /** * @var ?string * @see get_height() */ public $height; /** * @deprecated * @var null */ public $javascript; /** * @var string[]|null * @see get_keywords() */ public $keywords; /** * @var ?string * @see get_language() */ public $lang; /** * @var ?int * @see get_length() */ public $length; /** * @var ?string * @see get_link() */ public $link; /** * @var ?string * @see get_medium() */ public $medium; /** * @var ?string * @see get_player() */ public $player; /** * @var Rating[]|null * @see get_ratings() */ public $ratings; /** * @var ?Restriction[] * @see get_restrictions() */ public $restrictions; /** * @var ?string * @see get_sampling_rate() */ public $samplingrate; /** * @var string[]|null * @see get_thumbnails() */ public $thumbnails; /** * @var ?string * @see get_title() */ public $title; /** * @var ?string * @see get_type() */ public $type; /** * @var ?string * @see get_width() */ public $width; /** * Constructor, used to input the data * * For documentation on all the parameters, see the corresponding * properties and their accessors * * @uses idn_to_ascii If available, this will convert an IDN * * @param null $javascript * @param Caption[]|null $captions * @param Category[]|null $categories * @param Credit[]|null $credits * @param string[]|null $hashes * @param string[]|null $keywords * @param Rating[]|null $ratings * @param Restriction[]|null $restrictions * @param string[]|null $thumbnails */ public function __construct( ?string $link = null, ?string $type = null, ?int $length = null, $javascript = null, ?string $bitrate = null, ?array $captions = null, ?array $categories = null, ?int $channels = null, ?Copyright $copyright = null, ?array $credits = null, ?string $description = null, ?int $duration = null, ?string $expression = null, ?string $framerate = null, ?array $hashes = null, ?string $height = null, ?array $keywords = null, ?string $lang = null, ?string $medium = null, ?string $player = null, ?array $ratings = null, ?array $restrictions = null, ?string $samplingrate = null, ?array $thumbnails = null, ?string $title = null, ?string $width = null ) { $this->bitrate = $bitrate; $this->captions = $captions; $this->categories = $categories; $this->channels = $channels; $this->copyright = $copyright; $this->credits = $credits; $this->description = $description; $this->duration = $duration; $this->expression = $expression; $this->framerate = $framerate; $this->hashes = $hashes; $this->height = $height; $this->keywords = $keywords; $this->lang = $lang; $this->length = $length; $this->link = $link; $this->medium = $medium; $this->player = $player; $this->ratings = $ratings; $this->restrictions = $restrictions; $this->samplingrate = $samplingrate; $this->thumbnails = $thumbnails; $this->title = $title; $this->type = $type; $this->width = $width; if (function_exists('idn_to_ascii')) { $parsed = \SimplePie\Misc::parse_url($link ?? ''); if ($parsed['authority'] !== '' && !ctype_print($parsed['authority'])) { $authority = (string) \idn_to_ascii($parsed['authority'], \IDNA_NONTRANSITIONAL_TO_ASCII, \INTL_IDNA_VARIANT_UTS46); $this->link = \SimplePie\Misc::compress_parse_url($parsed['scheme'], $authority, $parsed['path'], $parsed['query'], $parsed['fragment']); } } $this->handler = $this->get_handler(); // Needs to load last } /** * String-ified version * * @return string */ public function __toString() { // There is no $this->data here return md5(serialize($this)); } /** * Get the bitrate * * @return string|null */ public function get_bitrate() { if ($this->bitrate !== null) { return $this->bitrate; } return null; } /** * Get a single caption * * @param int $key * @return \SimplePie\Caption|null */ public function get_caption(int $key = 0) { $captions = $this->get_captions(); if (isset($captions[$key])) { return $captions[$key]; } return null; } /** * Get all captions * * @return Caption[]|null */ public function get_captions() { if ($this->captions !== null) { return $this->captions; } return null; } /** * Get a single category * * @param int $key * @return \SimplePie\Category|null */ public function get_category(int $key = 0) { $categories = $this->get_categories(); if (isset($categories[$key])) { return $categories[$key]; } return null; } /** * Get all categories * * @return \SimplePie\Category[]|null */ public function get_categories() { if ($this->categories !== null) { return $this->categories; } return null; } /** * Get the number of audio channels * * @return int|null */ public function get_channels() { if ($this->channels !== null) { return $this->channels; } return null; } /** * Get the copyright information * * @return \SimplePie\Copyright|null */ public function get_copyright() { if ($this->copyright !== null) { return $this->copyright; } return null; } /** * Get a single credit * * @param int $key * @return \SimplePie\Credit|null */ public function get_credit(int $key = 0) { $credits = $this->get_credits(); if (isset($credits[$key])) { return $credits[$key]; } return null; } /** * Get all credits * * @return Credit[]|null */ public function get_credits() { if ($this->credits !== null) { return $this->credits; } return null; } /** * Get the description of the enclosure * * @return string|null */ public function get_description() { if ($this->description !== null) { return $this->description; } return null; } /** * Get the duration of the enclosure * * @param bool $convert Convert seconds into hh:mm:ss * @return string|int|null 'hh:mm:ss' string if `$convert` was specified, otherwise integer (or null if none found) */ public function get_duration(bool $convert = false) { if ($this->duration !== null) { if ($convert) { $time = \SimplePie\Misc::time_hms($this->duration); return $time; } return $this->duration; } return null; } /** * Get the expression * * @return string Probably one of 'sample', 'full', 'nonstop', 'clip'. Defaults to 'full' */ public function get_expression() { if ($this->expression !== null) { return $this->expression; } return 'full'; } /** * Get the file extension * * @return string|null */ public function get_extension() { if ($this->link !== null) { $url = \SimplePie\Misc::parse_url($this->link); if ($url['path'] !== '') { return pathinfo($url['path'], PATHINFO_EXTENSION); } } return null; } /** * Get the framerate (in frames-per-second) * * @return string|null */ public function get_framerate() { if ($this->framerate !== null) { return $this->framerate; } return null; } /** * Get the preferred handler * * @return string|null One of 'flash', 'fmedia', 'quicktime', 'wmedia', 'mp3' */ public function get_handler() { return $this->get_real_type(true); } /** * Get a single hash * * @link http://www.rssboard.org/media-rss#media-hash * @param int $key * @return string|null Hash as per `media:hash`, prefixed with "$algo:" */ public function get_hash(int $key = 0) { $hashes = $this->get_hashes(); if (isset($hashes[$key])) { return $hashes[$key]; } return null; } /** * Get all credits * * @return string[]|null Array of strings, see {@see get_hash()} */ public function get_hashes() { if ($this->hashes !== null) { return $this->hashes; } return null; } /** * Get the height * * @return string|null */ public function get_height() { if ($this->height !== null) { return $this->height; } return null; } /** * Get the language * * @link http://tools.ietf.org/html/rfc3066 * @return string|null Language code as per RFC 3066 */ public function get_language() { if ($this->lang !== null) { return $this->lang; } return null; } /** * Get a single keyword * * @param int $key * @return string|null */ public function get_keyword(int $key = 0) { $keywords = $this->get_keywords(); if (isset($keywords[$key])) { return $keywords[$key]; } return null; } /** * Get all keywords * * @return string[]|null */ public function get_keywords() { if ($this->keywords !== null) { return $this->keywords; } return null; } /** * Get length * * @return ?int Length in bytes */ public function get_length() { if ($this->length !== null) { return $this->length; } return null; } /** * Get the URL * * @return string|null */ public function get_link() { if ($this->link !== null) { return $this->link; } return null; } /** * Get the medium * * @link http://www.rssboard.org/media-rss#media-content * @return string|null Should be one of 'image', 'audio', 'video', 'document', 'executable' */ public function get_medium() { if ($this->medium !== null) { return $this->medium; } return null; } /** * Get the player URL * * Typically the same as {@see get_permalink()} * @return string|null Player URL */ public function get_player() { if ($this->player !== null) { return $this->player; } return null; } /** * Get a single rating * * @param int $key * @return \SimplePie\Rating|null */ public function get_rating(int $key = 0) { $ratings = $this->get_ratings(); if (isset($ratings[$key])) { return $ratings[$key]; } return null; } /** * Get all ratings * * @return Rating[]|null */ public function get_ratings() { if ($this->ratings !== null) { return $this->ratings; } return null; } /** * Get a single restriction * * @param int $key * @return \SimplePie\Restriction|null */ public function get_restriction(int $key = 0) { $restrictions = $this->get_restrictions(); if (isset($restrictions[$key])) { return $restrictions[$key]; } return null; } /** * Get all restrictions * * @return Restriction[]|null */ public function get_restrictions() { if ($this->restrictions !== null) { return $this->restrictions; } return null; } /** * Get the sampling rate (in kHz) * * @return string|null */ public function get_sampling_rate() { if ($this->samplingrate !== null) { return $this->samplingrate; } return null; } /** * Get the file size (in MiB) * * @return float|null File size in mebibytes (1048 bytes) */ public function get_size() { $length = $this->get_length(); if ($length !== null) { return round($length / 1048576, 2); } return null; } /** * Get a single thumbnail * * @param int $key * @return string|null Thumbnail URL */ public function get_thumbnail(int $key = 0) { $thumbnails = $this->get_thumbnails(); if (isset($thumbnails[$key])) { return $thumbnails[$key]; } return null; } /** * Get all thumbnails * * @return string[]|null Array of thumbnail URLs */ public function get_thumbnails() { if ($this->thumbnails !== null) { return $this->thumbnails; } return null; } /** * Get the title * * @return string|null */ public function get_title() { if ($this->title !== null) { return $this->title; } return null; } /** * Get mimetype of the enclosure * * @see get_real_type() * @return string|null MIME type */ public function get_type() { if ($this->type !== null) { return $this->type; } return null; } /** * Get the width * * @return string|null */ public function get_width() { if ($this->width !== null) { return $this->width; } return null; } /** * Embed the enclosure using `<embed>` * * @deprecated Use the second parameter to {@see embed} instead * * @param array<string, mixed>|string $options See first parameter to {@see embed} * @return string HTML string to output */ public function native_embed($options = '') { return $this->embed($options, true); } /** * Embed the enclosure using Javascript * * `$options` is an array or comma-separated key:value string, with the * following properties: * * - `alt` (string): Alternate content for when an end-user does not have * the appropriate handler installed or when a file type is * unsupported. Can be any text or HTML. Defaults to blank. * - `altclass` (string): If a file type is unsupported, the end-user will * see the alt text (above) linked directly to the content. That link * will have this value as its class name. Defaults to blank. * - `audio` (string): This is an image that should be used as a * placeholder for audio files before they're loaded (QuickTime-only). * Can be any relative or absolute URL. Defaults to blank. * - `bgcolor` (string): The background color for the media, if not * already transparent. Defaults to `#ffffff`. * - `height` (integer): The height of the embedded media. Accepts any * numeric pixel value (such as `360`) or `auto`. Defaults to `auto`, * and it is recommended that you use this default. * - `loop` (boolean): Do you want the media to loop when it's done? * Defaults to `false`. * - `mediaplayer` (string): The location of the included * `mediaplayer.swf` file. This allows for the playback of Flash Video * (`.flv`) files, and is the default handler for non-Odeo MP3's. * Defaults to blank. * - `video` (string): This is an image that should be used as a * placeholder for video files before they're loaded (QuickTime-only). * Can be any relative or absolute URL. Defaults to blank. * - `width` (integer): The width of the embedded media. Accepts any * numeric pixel value (such as `480`) or `auto`. Defaults to `auto`, * and it is recommended that you use this default. * - `widescreen` (boolean): Is the enclosure widescreen or standard? * This applies only to video enclosures, and will automatically resize * the content appropriately. Defaults to `false`, implying 4:3 mode. * * Note: Non-widescreen (4:3) mode with `width` and `height` set to `auto` * will default to 480x360 video resolution. Widescreen (16:9) mode with * `width` and `height` set to `auto` will default to 480x270 video resolution. * * @todo If the dimensions for media:content are defined, use them when width/height are set to 'auto'. * @param array<string, mixed>|string $options Comma-separated key:value list, or array * @param bool $native Use `<embed>` * @return string HTML string to output */ public function embed($options = '', bool $native = false) { // Set up defaults $audio = ''; $video = ''; $alt = ''; $altclass = ''; $loop = 'false'; $width = 'auto'; $height = 'auto'; $bgcolor = '#ffffff'; $mediaplayer = ''; $widescreen = false; $handler = $this->get_handler(); $type = $this->get_real_type(); $placeholder = ''; // Process options and reassign values as necessary if (is_array($options)) { extract($options); } else { $options = explode(',', $options); foreach ($options as $option) { $opt = explode(':', $option, 2); if (isset($opt[0], $opt[1])) { $opt[0] = trim($opt[0]); $opt[1] = trim($opt[1]); switch ($opt[0]) { case 'audio': $audio = $opt[1]; break; case 'video': $video = $opt[1]; break; case 'alt': $alt = $opt[1]; break; case 'altclass': $altclass = $opt[1]; break; case 'loop': $loop = $opt[1]; break; case 'width': $width = $opt[1]; break; case 'height': $height = $opt[1]; break; case 'bgcolor': $bgcolor = $opt[1]; break; case 'mediaplayer': $mediaplayer = $opt[1]; break; case 'widescreen': $widescreen = $opt[1]; break; } } } } $mime = explode('/', (string) $type, 2); $mime = $mime[0]; // Process values for 'auto' if ($width === 'auto') { if ($mime === 'video') { if ($height === 'auto') { $width = 480; } elseif ($widescreen) { $width = round((intval($height) / 9) * 16); } else { $width = round((intval($height) / 3) * 4); } } else { $width = '100%'; } } if ($height === 'auto') { if ($mime === 'audio') { $height = 0; } elseif ($mime === 'video') { if ($width === 'auto') { if ($widescreen) { $height = 270; } else { $height = 360; } } elseif ($widescreen) { $height = round((intval($width) / 16) * 9); } else { $height = round((intval($width) / 4) * 3); } } else { $height = 376; } } elseif ($mime === 'audio') { $height = 0; } // Set proper placeholder value if ($mime === 'audio') { $placeholder = $audio; } elseif ($mime === 'video') { $placeholder = $video; } $embed = ''; // Flash if ($handler === 'flash') { if ($native) { $embed .= "<embed src=\"" . $this->get_link() . "\" pluginspage=\"http://adobe.com/go/getflashplayer\" type=\"$type\" quality=\"high\" width=\"$width\" height=\"$height\" bgcolor=\"$bgcolor\" loop=\"$loop\"></embed>"; } else { $embed .= "<script type='text/javascript'>embed_flash('$bgcolor', '$width', '$height', '" . $this->get_link() . "', '$loop', '$type');</script>"; } } // Flash Media Player file types. // Preferred handler for MP3 file types. elseif ($handler === 'fmedia' || ($handler === 'mp3' && $mediaplayer !== '')) { if (is_numeric($height)) { $height += 20; } if ($native) { $embed .= "<embed src=\"$mediaplayer\" pluginspage=\"http://adobe.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" quality=\"high\" width=\"$width\" height=\"$height\" wmode=\"transparent\" flashvars=\"file=" . rawurlencode($this->get_link().'?file_extension=.'.$this->get_extension()) . "&autostart=false&repeat=$loop&showdigits=true&showfsbutton=false\"></embed>"; } else { $embed .= "<script type='text/javascript'>embed_flv('$width', '$height', '" . rawurlencode($this->get_link().'?file_extension=.'.$this->get_extension()) . "', '$placeholder', '$loop', '$mediaplayer');</script>"; } } // QuickTime 7 file types. Need to test with QuickTime 6. // Only handle MP3's if the Flash Media Player is not present. elseif ($handler === 'quicktime' || ($handler === 'mp3' && $mediaplayer === '')) { if (is_numeric($height)) { $height += 16; } if ($native) { if ($placeholder !== '') { $embed .= "<embed type=\"$type\" style=\"cursor:hand; cursor:pointer;\" href=\"" . $this->get_link() . "\" src=\"$placeholder\" width=\"$width\" height=\"$height\" autoplay=\"false\" target=\"myself\" controller=\"false\" loop=\"$loop\" scale=\"aspect\" bgcolor=\"$bgcolor\" pluginspage=\"http://apple.com/quicktime/download/\"></embed>"; } else { $embed .= "<embed type=\"$type\" style=\"cursor:hand; cursor:pointer;\" src=\"" . $this->get_link() . "\" width=\"$width\" height=\"$height\" autoplay=\"false\" target=\"myself\" controller=\"true\" loop=\"$loop\" scale=\"aspect\" bgcolor=\"$bgcolor\" pluginspage=\"http://apple.com/quicktime/download/\"></embed>"; } } else { $embed .= "<script type='text/javascript'>embed_quicktime('$type', '$bgcolor', '$width', '$height', '" . $this->get_link() . "', '$placeholder', '$loop');</script>"; } } // Windows Media elseif ($handler === 'wmedia') { if (is_numeric($height)) { $height += 45; } if ($native) { $embed .= "<embed type=\"application/x-mplayer2\" src=\"" . $this->get_link() . "\" autosize=\"1\" width=\"$width\" height=\"$height\" showcontrols=\"1\" showstatusbar=\"0\" showdisplay=\"0\" autostart=\"0\"></embed>"; } else { $embed .= "<script type='text/javascript'>embed_wmedia('$width', '$height', '" . $this->get_link() . "');</script>"; } } // Everything else else { $embed .= '<a href="' . $this->get_link() . '" class="' . $altclass . '">' . $alt . '</a>'; } return $embed; } /** * Get the real media type * * Often, feeds lie to us, necessitating a bit of deeper inspection. This * converts types to their canonical representations based on the file * extension * * @see get_type() * @param bool $find_handler Internal use only, use {@see get_handler()} instead * @return string|null MIME type */ public function get_real_type(bool $find_handler = false) { // Mime-types by handler. $types_flash = ['application/x-shockwave-flash', 'application/futuresplash']; // Flash $types_fmedia = ['video/flv', 'video/x-flv','flv-application/octet-stream']; // Flash Media Player $types_quicktime = ['audio/3gpp', 'audio/3gpp2', 'audio/aac', 'audio/x-aac', 'audio/aiff', 'audio/x-aiff', 'audio/mid', 'audio/midi', 'audio/x-midi', 'audio/mp4', 'audio/m4a', 'audio/x-m4a', 'audio/wav', 'audio/x-wav', 'video/3gpp', 'video/3gpp2', 'video/m4v', 'video/x-m4v', 'video/mp4', 'video/mpeg', 'video/x-mpeg', 'video/quicktime', 'video/sd-video']; // QuickTime $types_wmedia = ['application/asx', 'application/x-mplayer2', 'audio/x-ms-wma', 'audio/x-ms-wax', 'video/x-ms-asf-plugin', 'video/x-ms-asf', 'video/x-ms-wm', 'video/x-ms-wmv', 'video/x-ms-wvx']; // Windows Media $types_mp3 = ['audio/mp3', 'audio/x-mp3', 'audio/mpeg', 'audio/x-mpeg']; // MP3 $type = $this->get_type(); if ($type !== null) { $type = strtolower($type); } // If we encounter an unsupported mime-type, check the file extension and guess intelligently. if (!in_array($type, array_merge($types_flash, $types_fmedia, $types_quicktime, $types_wmedia, $types_mp3))) { $extension = $this->get_extension(); if ($extension === null) { return null; } switch (strtolower($extension)) { // Audio mime-types case 'aac': case 'adts': $type = 'audio/acc'; break; case 'aif': case 'aifc': case 'aiff': case 'cdda': $type = 'audio/aiff'; break; case 'bwf': $type = 'audio/wav'; break; case 'kar': case 'mid': case 'midi': case 'smf': $type = 'audio/midi'; break; case 'm4a': $type = 'audio/x-m4a'; break; case 'mp3': case 'swa': $type = 'audio/mp3'; break; case 'wav': $type = 'audio/wav'; break; case 'wax': $type = 'audio/x-ms-wax'; break; case 'wma': $type = 'audio/x-ms-wma'; break; case '3gp': case '3gpp': // Video mime-types $type = 'video/3gpp'; break; case '3g2': case '3gp2': $type = 'video/3gpp2'; break; case 'asf': $type = 'video/x-ms-asf'; break; case 'flv': $type = 'video/x-flv'; break; case 'm1a': case 'm1s': case 'm1v': case 'm15': case 'm75': case 'mp2': case 'mpa': case 'mpeg': case 'mpg': case 'mpm': case 'mpv': $type = 'video/mpeg'; break; case 'm4v': $type = 'video/x-m4v'; break; case 'mov': case 'qt': $type = 'video/quicktime'; break; case 'mp4': case 'mpg4': $type = 'video/mp4'; break; case 'sdv': $type = 'video/sd-video'; break; case 'wm': $type = 'video/x-ms-wm'; break; case 'wmv': $type = 'video/x-ms-wmv'; break; case 'wvx': $type = 'video/x-ms-wvx'; break; case 'spl': // Flash mime-types $type = 'application/futuresplash'; break; case 'swf': $type = 'application/x-shockwave-flash'; break; } } if ($find_handler) { if (in_array($type, $types_flash)) { return 'flash'; } elseif (in_array($type, $types_fmedia)) { return 'fmedia'; } elseif (in_array($type, $types_quicktime)) { return 'quicktime'; } elseif (in_array($type, $types_wmedia)) { return 'wmedia'; } elseif (in_array($type, $types_mp3)) { return 'mp3'; } return null; } return $type; } } class_alias('SimplePie\Enclosure', 'SimplePie_Enclosure'); error_log 0000644 00000005040 15162130154 0006456 0 ustar 00 [24-Mar-2026 18:21:58 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php:9 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php on line 9 [25-Mar-2026 00:36:56 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php:9 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php on line 9 [25-Mar-2026 09:41:03 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php:9 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php on line 9 [26-Mar-2026 01:51:36 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php:9 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php on line 9 [26-Mar-2026 09:05:49 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php:9 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php on line 9 [26-Mar-2026 10:59:37 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php:9 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php on line 9 [26-Mar-2026 20:26:23 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php:9 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php on line 9 [28-Mar-2026 09:38:28 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php:9 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php on line 9 [30-Mar-2026 08:59:45 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php:9 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/File.php on line 9 HTTP/Parser.php 0000644 00000035551 15162130160 0007274 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\HTTP; /** * HTTP Response Parser * @template Psr7Compatible of bool */ class Parser { /** * HTTP Version * * @var float */ public $http_version = 0.0; /** * Status code * * @var int */ public $status_code = 0; /** * Reason phrase * * @var string */ public $reason = ''; /** * @var Psr7Compatible whether headers are compatible with PSR-7 format. */ private $psr7Compatible; /** * Key/value pairs of the headers * * @var (Psr7Compatible is true ? array<string, non-empty-array<string>> : array<string, string>) */ public $headers = []; /** * Body of the response * * @var string */ public $body = ''; private const STATE_HTTP_VERSION = 'http_version'; private const STATE_STATUS = 'status'; private const STATE_REASON = 'reason'; private const STATE_NEW_LINE = 'new_line'; private const STATE_BODY = 'body'; private const STATE_NAME = 'name'; private const STATE_VALUE = 'value'; private const STATE_VALUE_CHAR = 'value_char'; private const STATE_QUOTE = 'quote'; private const STATE_QUOTE_ESCAPED = 'quote_escaped'; private const STATE_QUOTE_CHAR = 'quote_char'; private const STATE_CHUNKED = 'chunked'; private const STATE_EMIT = 'emit'; private const STATE_ERROR = false; /** * Current state of the state machine * * @var self::STATE_* */ protected $state = self::STATE_HTTP_VERSION; /** * Input data * * @var string */ protected $data = ''; /** * Input data length (to avoid calling strlen() everytime this is needed) * * @var int */ protected $data_length = 0; /** * Current position of the pointer * * @var int */ protected $position = 0; /** * Name of the header currently being parsed * * @var string */ protected $name = ''; /** * Value of the header currently being parsed * * @var string */ protected $value = ''; /** * Create an instance of the class with the input data * * @param string $data Input data * @param Psr7Compatible $psr7Compatible Whether the data types are in format compatible with PSR-7. */ public function __construct(string $data, bool $psr7Compatible = false) { $this->data = $data; $this->data_length = strlen($this->data); $this->psr7Compatible = $psr7Compatible; } /** * Parse the input data * * @return bool true on success, false on failure */ public function parse() { while ($this->state && $this->state !== self::STATE_EMIT && $this->has_data()) { $state = $this->state; $this->$state(); } $this->data = ''; if ($this->state === self::STATE_EMIT || $this->state === self::STATE_BODY) { return true; } // Reset the parser state. $this->http_version = 0.0; $this->status_code = 0; $this->reason = ''; $this->headers = []; $this->body = ''; return false; } /** * Check whether there is data beyond the pointer * * @return bool true if there is further data, false if not */ protected function has_data() { return (bool) ($this->position < $this->data_length); } /** * See if the next character is LWS * * @return bool true if the next character is LWS, false if not */ protected function is_linear_whitespace() { return (bool) ($this->data[$this->position] === "\x09" || $this->data[$this->position] === "\x20" || ($this->data[$this->position] === "\x0A" && isset($this->data[$this->position + 1]) && ($this->data[$this->position + 1] === "\x09" || $this->data[$this->position + 1] === "\x20"))); } /** * Parse the HTTP version * @return void */ protected function http_version() { if (strpos($this->data, "\x0A") !== false && strtoupper(substr($this->data, 0, 5)) === 'HTTP/') { $len = strspn($this->data, '0123456789.', 5); $http_version = substr($this->data, 5, $len); $this->position += 5 + $len; if (substr_count($http_version, '.') <= 1) { $this->http_version = (float) $http_version; $this->position += strspn($this->data, "\x09\x20", $this->position); $this->state = self::STATE_STATUS; } else { $this->state = self::STATE_ERROR; } } else { $this->state = self::STATE_ERROR; } } /** * Parse the status code * @return void */ protected function status() { if ($len = strspn($this->data, '0123456789', $this->position)) { $this->status_code = (int) substr($this->data, $this->position, $len); $this->position += $len; $this->state = self::STATE_REASON; } else { $this->state = self::STATE_ERROR; } } /** * Parse the reason phrase * @return void */ protected function reason() { $len = strcspn($this->data, "\x0A", $this->position); $this->reason = trim(substr($this->data, $this->position, $len), "\x09\x0D\x20"); $this->position += $len + 1; $this->state = self::STATE_NEW_LINE; } private function add_header(string $name, string $value): void { if ($this->psr7Compatible) { // For PHPStan: should be enforced by template parameter but PHPStan is not smart enough. /** @var array<string, non-empty-array<string>> */ $headers = &$this->headers; $headers[$name][] = $value; } else { // For PHPStan: should be enforced by template parameter but PHPStan is not smart enough. /** @var array<string, string>) */ $headers = &$this->headers; $headers[$name] .= ', ' . $value; } } private function replace_header(string $name, string $value): void { if ($this->psr7Compatible) { // For PHPStan: should be enforced by template parameter but PHPStan is not smart enough. /** @var array<string, non-empty-array<string>> */ $headers = &$this->headers; $headers[$name] = [$value]; } else { // For PHPStan: should be enforced by template parameter but PHPStan is not smart enough. /** @var array<string, string>) */ $headers = &$this->headers; $headers[$name] = $value; } } /** * Deal with a new line, shifting data around as needed * @return void */ protected function new_line() { $this->value = trim($this->value, "\x0D\x20"); if ($this->name !== '' && $this->value !== '') { $this->name = strtolower($this->name); // We should only use the last Content-Type header. c.f. issue #1 if (isset($this->headers[$this->name]) && $this->name !== 'content-type') { $this->add_header($this->name, $this->value); } else { $this->replace_header($this->name, $this->value); } } $this->name = ''; $this->value = ''; if (substr($this->data[$this->position], 0, 2) === "\x0D\x0A") { $this->position += 2; $this->state = self::STATE_BODY; } elseif ($this->data[$this->position] === "\x0A") { $this->position++; $this->state = self::STATE_BODY; } else { $this->state = self::STATE_NAME; } } /** * Parse a header name * @return void */ protected function name() { $len = strcspn($this->data, "\x0A:", $this->position); if (isset($this->data[$this->position + $len])) { if ($this->data[$this->position + $len] === "\x0A") { $this->position += $len; $this->state = self::STATE_NEW_LINE; } else { $this->name = substr($this->data, $this->position, $len); $this->position += $len + 1; $this->state = self::STATE_VALUE; } } else { $this->state = self::STATE_ERROR; } } /** * Parse LWS, replacing consecutive LWS characters with a single space * @return void */ protected function linear_whitespace() { do { if (substr($this->data, $this->position, 2) === "\x0D\x0A") { $this->position += 2; } elseif ($this->data[$this->position] === "\x0A") { $this->position++; } $this->position += strspn($this->data, "\x09\x20", $this->position); } while ($this->has_data() && $this->is_linear_whitespace()); $this->value .= "\x20"; } /** * See what state to move to while within non-quoted header values * @return void */ protected function value() { if ($this->is_linear_whitespace()) { $this->linear_whitespace(); } else { switch ($this->data[$this->position]) { case '"': // Workaround for ETags: we have to include the quotes as // part of the tag. if (strtolower($this->name) === 'etag') { $this->value .= '"'; $this->position++; $this->state = self::STATE_VALUE_CHAR; break; } $this->position++; $this->state = self::STATE_QUOTE; break; case "\x0A": $this->position++; $this->state = self::STATE_NEW_LINE; break; default: $this->state = self::STATE_VALUE_CHAR; break; } } } /** * Parse a header value while outside quotes * @return void */ protected function value_char() { $len = strcspn($this->data, "\x09\x20\x0A\"", $this->position); $this->value .= substr($this->data, $this->position, $len); $this->position += $len; $this->state = self::STATE_VALUE; } /** * See what state to move to while within quoted header values * @return void */ protected function quote() { if ($this->is_linear_whitespace()) { $this->linear_whitespace(); } else { switch ($this->data[$this->position]) { case '"': $this->position++; $this->state = self::STATE_VALUE; break; case "\x0A": $this->position++; $this->state = self::STATE_NEW_LINE; break; case '\\': $this->position++; $this->state = self::STATE_QUOTE_ESCAPED; break; default: $this->state = self::STATE_QUOTE_CHAR; break; } } } /** * Parse a header value while within quotes * @return void */ protected function quote_char() { $len = strcspn($this->data, "\x09\x20\x0A\"\\", $this->position); $this->value .= substr($this->data, $this->position, $len); $this->position += $len; $this->state = self::STATE_VALUE; } /** * Parse an escaped character within quotes * @return void */ protected function quote_escaped() { $this->value .= $this->data[$this->position]; $this->position++; $this->state = self::STATE_QUOTE; } /** * Parse the body * @return void */ protected function body() { $this->body = substr($this->data, $this->position); if (!empty($this->headers['transfer-encoding'])) { unset($this->headers['transfer-encoding']); $this->state = self::STATE_CHUNKED; } else { $this->state = self::STATE_EMIT; } } /** * Parsed a "Transfer-Encoding: chunked" body * @return void */ protected function chunked() { if (!preg_match('/^([0-9a-f]+)[^\r\n]*\r\n/i', trim($this->body))) { $this->state = self::STATE_EMIT; return; } $decoded = ''; $encoded = $this->body; while (true) { $is_chunked = (bool) preg_match('/^([0-9a-f]+)[^\r\n]*\r\n/i', $encoded, $matches); if (!$is_chunked) { // Looks like it's not chunked after all $this->state = self::STATE_EMIT; return; } $length = hexdec(trim($matches[1])); // For PHPStan: this will only be float when larger than PHP_INT_MAX. // But even on 32-bit systems, it would mean 2GiB chunk, which sounds unlikely. \assert(\is_int($length), "Length needs to be shorter than PHP_INT_MAX"); if ($length === 0) { // Ignore trailer headers $this->state = self::STATE_EMIT; $this->body = $decoded; return; } $chunk_length = strlen($matches[0]); $decoded .= substr($encoded, $chunk_length, $length); $encoded = substr($encoded, $chunk_length + $length + 2); // BC for PHP < 8.0: substr() can return bool instead of string $encoded = ($encoded === false) ? '' : $encoded; if (trim($encoded) === '0' || empty($encoded)) { $this->state = self::STATE_EMIT; $this->body = $decoded; return; } } } /** * Prepare headers (take care of proxies headers) * * @param string $headers Raw headers * @param non-negative-int $count Redirection count. Default to 1. * * @return string */ public static function prepareHeaders(string $headers, int $count = 1) { $data = explode("\r\n\r\n", $headers, $count); $data = array_pop($data); if (false !== stripos($data, "HTTP/1.0 200 Connection established\r\n")) { $exploded = explode("\r\n\r\n", $data, 2); $data = end($exploded); } if (false !== stripos($data, "HTTP/1.1 200 Connection established\r\n")) { $exploded = explode("\r\n\r\n", $data, 2); $data = end($exploded); } return $data; } } class_alias('SimplePie\HTTP\Parser', 'SimplePie_HTTP_Parser'); HTTP/FileClient.php 0000644 00000004333 15162130162 0010052 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\HTTP; use InvalidArgumentException; use SimplePie\File; use SimplePie\Misc; use SimplePie\Registry; use Throwable; /** * HTTP Client based on \SimplePie\File * * @internal */ final class FileClient implements Client { /** @var Registry */ private $registry; /** @var array{timeout?: int, redirects?: int, useragent?: string, force_fsockopen?: bool, curl_options?: array<mixed>} */ private $options; /** * @param array{timeout?: int, redirects?: int, useragent?: string, force_fsockopen?: bool, curl_options?: array<mixed>} $options */ public function __construct(Registry $registry, array $options = []) { $this->registry = $registry; $this->options = $options; } /** * send a request and return the response * * @param Client::METHOD_* $method * @param array<string, string> $headers * * @throws ClientException if anything goes wrong requesting the data */ public function request(string $method, string $url, array $headers = []): Response { // @phpstan-ignore-next-line Enforce PHPDoc type. if ($method !== self::METHOD_GET) { throw new InvalidArgumentException(sprintf( '%s(): Argument #1 ($method) only supports method "%s".', __METHOD__, self::METHOD_GET ), 1); } try { $file = $this->registry->create(File::class, [ $url, $this->options['timeout'] ?? 10, $this->options['redirects'] ?? 5, $headers, $this->options['useragent'] ?? Misc::get_default_useragent(), $this->options['force_fsockopen'] ?? false, $this->options['curl_options'] ?? [] ]); } catch (Throwable $th) { throw new ClientException($th->getMessage(), $th->getCode(), $th); } if ($file->error !== null && $file->get_status_code() === 0) { throw new ClientException($file->error); } return $file; } } HTTP/ClientException.php 0000644 00000000515 15162130164 0011131 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\HTTP; use SimplePie\Exception as SimplePieException; /** * Client exception class * * @internal */ final class ClientException extends SimplePieException { } HTTP/Response.php 0000644 00000015435 15162130166 0007643 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-FileCopyrightText: 2014 PHP Framework Interoperability Group // SPDX-License-Identifier: MIT declare(strict_types=1); namespace SimplePie\HTTP; /** * HTTP Response interface * * This interface must be interoperable with Psr\Http\Message\ResponseInterface * @see https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface * * @internal */ interface Response { /** * Return the string representation of the permanent URI of the requested resource * (the first location after a prefix of (only) permanent redirects). * * Depending on which components of the URI are present, the resulting * string is either a full URI or relative reference according to RFC 3986, * Section 4.1. The method concatenates the various components of the URI, * using the appropriate delimiters: * * - If a scheme is present, it MUST be suffixed by ":". * - If an authority is present, it MUST be prefixed by "//". * - The path can be concatenated without delimiters. But there are two * cases where the path has to be adjusted to make the URI reference * valid as PHP does not allow to throw an exception in __toString(): * - If the path is rootless and an authority is present, the path MUST * be prefixed by "/". * - If the path is starting with more than one "/" and no authority is * present, the starting slashes MUST be reduced to one. * - If a query is present, it MUST be prefixed by "?". * - If a fragment is present, it MUST be prefixed by "#". * * @see http://tools.ietf.org/html/rfc3986#section-4.1 */ public function get_permanent_uri(): string; /** * Return the string representation of the final requested URL after following all redirects. * * Depending on which components of the URI are present, the resulting * string is either a full URI or relative reference according to RFC 3986, * Section 4.1. The method concatenates the various components of the URI, * using the appropriate delimiters: * * - If a scheme is present, it MUST be suffixed by ":". * - If an authority is present, it MUST be prefixed by "//". * - The path can be concatenated without delimiters. But there are two * cases where the path has to be adjusted to make the URI reference * valid as PHP does not allow to throw an exception in __toString(): * - If the path is rootless and an authority is present, the path MUST * be prefixed by "/". * - If the path is starting with more than one "/" and no authority is * present, the starting slashes MUST be reduced to one. * - If a query is present, it MUST be prefixed by "?". * - If a fragment is present, it MUST be prefixed by "#". * * @see http://tools.ietf.org/html/rfc3986#section-4.1 */ public function get_final_requested_uri(): string; /** * Gets the response status code. * * The status code is a 3-digit integer result code of the server's attempt * to understand and satisfy the request. * * @return int Status code. */ public function get_status_code(): int; /** * Retrieves all message header values. * * The keys represent the header name as it will be sent over the wire, and * each value is an array of strings associated with the header. * * // Represent the headers as a string * foreach ($message->get_headers() as $name => $values) { * echo $name . ': ' . implode(', ', $values); * } * * // Emit headers iteratively: * foreach ($message->get_headers() as $name => $values) { * foreach ($values as $value) { * header(sprintf('%s: %s', $name, $value), false); * } * } * * @return array<non-empty-array<string>> Returns an associative array of the message's headers. * Each key MUST be a header name, and each value MUST be an array of * strings for that header. */ public function get_headers(): array; /** * Checks if a header exists by the given case-insensitive name. * * @param string $name Case-insensitive header field name. * @return bool Returns true if any header names match the given header * name using a case-insensitive string comparison. Returns false if * no matching header name is found in the message. */ public function has_header(string $name): bool; /** * Retrieves a message header value by the given case-insensitive name. * * This method returns an array of all the header values of the given * case-insensitive header name. * * If the header does not appear in the message, this method MUST return an * empty array. * * @param string $name Case-insensitive header field name. * @return string[] An array of string values as provided for the given * header. If the header does not appear in the message, this method MUST * return an empty array. */ public function get_header(string $name): array; /** * Return an instance with the provided value replacing the specified header. * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return an instance that has the * new and/or updated header and value. * * @param string $name Case-insensitive header field name. * @param string|non-empty-array<string> $value Header value(s). * @return static * @throws \InvalidArgumentException for invalid header names or values. */ public function with_header(string $name, $value); /** * Retrieves a comma-separated string of the values for a single header. * * This method returns all of the header values of the given * case-insensitive header name as a string concatenated together using * a comma. * * NOTE: Not all header values may be appropriately represented using * comma concatenation. For such headers, use getHeader() instead * and supply your own delimiter when concatenating. * * If the header does not appear in the message, this method MUST return * an empty string. * * @param string $name Case-insensitive header field name. * @return string A string of values as provided for the given header * concatenated together using a comma. If the header does not appear in * the message, this method MUST return an empty string. */ public function get_header_line(string $name): string; /** * get the body as string * * @return string */ public function get_body_content(): string; } HTTP/RawTextResponse.php 0000644 00000004027 15162130170 0011150 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\HTTP; /** * HTTP Response for rax text * * This interface must be interoperable with Psr\Http\Message\ResponseInterface * @see https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface * * @internal */ final class RawTextResponse implements Response { /** * @var string */ private $raw_text; /** * @var string */ private $permanent_url; /** * @var array<non-empty-array<string>> */ private $headers = []; /** * @var string */ private $requested_url; public function __construct(string $raw_text, string $filepath) { $this->raw_text = $raw_text; $this->permanent_url = $filepath; $this->requested_url = $filepath; } public function get_permanent_uri(): string { return $this->permanent_url; } public function get_final_requested_uri(): string { return $this->requested_url; } public function get_status_code(): int { return 200; } public function get_headers(): array { return $this->headers; } public function has_header(string $name): bool { return isset($this->headers[strtolower($name)]); } public function get_header(string $name): array { return isset($this->headers[strtolower($name)]) ? $this->headers[$name] : []; } public function with_header(string $name, $value) { $new = clone $this; $newHeader = [ strtolower($name) => (array) $value, ]; $new->headers = $newHeader + $this->headers; return $new; } public function get_header_line(string $name): string { return isset($this->headers[strtolower($name)]) ? implode(", ", $this->headers[$name]) : ''; } public function get_body_content(): string { return $this->raw_text; } } HTTP/error_log 0000644 00000025514 15162130172 0007245 0 ustar 00 [24-Mar-2026 21:48:34 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Exception" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/ClientException.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/ClientException.php on line 17 [24-Mar-2026 21:49:31 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Client" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/FileClient.php:21 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/FileClient.php on line 21 [24-Mar-2026 21:50:07 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/RawTextResponse.php:18 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/RawTextResponse.php on line 18 [24-Mar-2026 21:51:44 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php:20 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php on line 20 [24-Mar-2026 21:55:31 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Client" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php:22 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php on line 22 [25-Mar-2026 04:36:43 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Client" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php:22 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php on line 22 [25-Mar-2026 04:56:26 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php:20 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php on line 20 [25-Mar-2026 05:06:27 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Exception" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/ClientException.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/ClientException.php on line 17 [25-Mar-2026 05:06:49 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/RawTextResponse.php:18 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/RawTextResponse.php on line 18 [25-Mar-2026 06:39:23 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Client" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/FileClient.php:21 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/FileClient.php on line 21 [25-Mar-2026 11:33:59 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Client" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php:22 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php on line 22 [25-Mar-2026 11:34:12 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php:20 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php on line 20 [25-Mar-2026 11:34:52 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Exception" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/ClientException.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/ClientException.php on line 17 [25-Mar-2026 11:35:54 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/RawTextResponse.php:18 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/RawTextResponse.php on line 18 [26-Mar-2026 09:04:30 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Client" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php:22 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php on line 22 [26-Mar-2026 09:05:03 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php:20 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php on line 20 [26-Mar-2026 09:16:37 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Exception" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/ClientException.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/ClientException.php on line 17 [26-Mar-2026 09:16:46 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/RawTextResponse.php:18 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/RawTextResponse.php on line 18 [26-Mar-2026 10:38:22 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Client" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php:22 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php on line 22 [26-Mar-2026 10:38:23 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php:20 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php on line 20 [26-Mar-2026 10:38:25 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/RawTextResponse.php:18 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/RawTextResponse.php on line 18 [26-Mar-2026 10:51:04 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Client" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/FileClient.php:21 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/FileClient.php on line 21 [26-Mar-2026 11:23:35 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Exception" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/ClientException.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/ClientException.php on line 17 [26-Mar-2026 11:23:37 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Client" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/FileClient.php:21 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/FileClient.php on line 21 [26-Mar-2026 15:38:07 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Client" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php:22 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php on line 22 [26-Mar-2026 15:57:31 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php:20 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php on line 20 [26-Mar-2026 16:07:56 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/RawTextResponse.php:18 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/RawTextResponse.php on line 18 [26-Mar-2026 16:08:16 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Exception" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/ClientException.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/ClientException.php on line 17 [26-Mar-2026 17:40:26 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Client" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/FileClient.php:21 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/FileClient.php on line 21 [26-Mar-2026 22:00:23 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Client" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php:22 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php on line 22 [26-Mar-2026 22:00:29 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php:20 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php on line 20 [28-Mar-2026 09:15:05 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/RawTextResponse.php:18 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/RawTextResponse.php on line 18 [28-Mar-2026 09:23:06 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Response" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php:20 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr7Response.php on line 20 [28-Mar-2026 09:26:10 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Client" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/FileClient.php:21 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/FileClient.php on line 21 [28-Mar-2026 09:36:44 UTC] PHP Fatal error: Uncaught Error: Class "SimplePie\Exception" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/ClientException.php:17 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/ClientException.php on line 17 [28-Mar-2026 09:38:27 UTC] PHP Fatal error: Uncaught Error: Interface "SimplePie\HTTP\Client" not found in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php:22 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/SimplePie/src/HTTP/Psr18Client.php on line 22 HTTP/Psr18Client.php 0000644 00000010537 15162130174 0010116 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\HTTP; use InvalidArgumentException; use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\UriFactoryInterface; use Throwable; /** * HTTP Client based on PSR-18 and PSR-17 implementations * * @internal */ final class Psr18Client implements Client { /** * @var ClientInterface */ private $httpClient; /** * @var RequestFactoryInterface */ private $requestFactory; /** * @var UriFactoryInterface */ private $uriFactory; /** * @var int */ private $allowedRedirects = 5; public function __construct(ClientInterface $httpClient, RequestFactoryInterface $requestFactory, UriFactoryInterface $uriFactory) { $this->httpClient = $httpClient; $this->requestFactory = $requestFactory; $this->uriFactory = $uriFactory; } public function getHttpClient(): ClientInterface { return $this->httpClient; } public function getRequestFactory(): RequestFactoryInterface { return $this->requestFactory; } public function getUriFactory(): UriFactoryInterface { return $this->uriFactory; } /** * send a request and return the response * * @param Client::METHOD_* $method * @param string $url * @param array<string,string|string[]> $headers * * @throws ClientException if anything goes wrong requesting the data */ public function request(string $method, string $url, array $headers = []): Response { if ($method !== self::METHOD_GET) { throw new InvalidArgumentException(sprintf( '%s(): Argument #1 ($method) only supports method "%s".', __METHOD__, self::METHOD_GET ), 1); } if (preg_match('/^http(s)?:\/\//i', $url)) { return $this->requestUrl($method, $url, $headers); } return $this->requestLocalFile($url); } /** * @param array<string,string|string[]> $headers */ private function requestUrl(string $method, string $url, array $headers): Response { $permanentUrl = $url; $requestedUrl = $url; $remainingRedirects = $this->allowedRedirects; $request = $this->requestFactory->createRequest( $method, $this->uriFactory->createUri($requestedUrl) ); foreach ($headers as $name => $value) { $request = $request->withHeader($name, $value); } do { $followRedirect = false; try { $response = $this->httpClient->sendRequest($request); } catch (ClientExceptionInterface $th) { throw new ClientException($th->getMessage(), $th->getCode(), $th); } $statusCode = $response->getStatusCode(); // If we have a redirect if (in_array($statusCode, [300, 301, 302, 303, 307]) && $response->hasHeader('Location')) { // Prevent infinity redirect loops if ($remainingRedirects <= 0) { break; } $remainingRedirects--; $followRedirect = true; $requestedUrl = $response->getHeaderLine('Location'); if ($statusCode === 301) { $permanentUrl = $requestedUrl; } $request = $request->withUri($this->uriFactory->createUri($requestedUrl)); } } while ($followRedirect); return new Psr7Response($response, $permanentUrl, $requestedUrl); } private function requestLocalFile(string $path): Response { if (!is_readable($path)) { throw new ClientException(sprintf('file "%s" is not readable', $path)); } try { $raw = file_get_contents($path); } catch (Throwable $th) { throw new ClientException($th->getMessage(), $th->getCode(), $th); } if ($raw === false) { throw new ClientException('file_get_contents() could not read the file', 1); } return new RawTextResponse($raw, $path); } } HTTP/Client.php 0000644 00000001137 15162130176 0007256 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\HTTP; /** * HTTP Client interface * * @internal */ interface Client { public const METHOD_GET = 'GET'; /** * send a request and return the response * * @param Client::METHOD_* $method * @param array<string, string> $headers * * @throws ClientException if anything goes wrong requesting the data */ public function request(string $method, string $url, array $headers = []): Response; } HTTP/Psr7Response.php 0000644 00000004201 15162130200 0010371 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\HTTP; use Psr\Http\Message\ResponseInterface; /** * HTTP Response based on a PSR-7 response * * This interface must be interoperable with Psr\Http\Message\ResponseInterface * @see https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface * * @internal */ final class Psr7Response implements Response { /** * @var ResponseInterface */ private $response; /** * @var string */ private $permanent_url; /** * @var string */ private $requested_url; public function __construct(ResponseInterface $response, string $permanent_url, string $requested_url) { $this->response = $response; $this->permanent_url = $permanent_url; $this->requested_url = $requested_url; } public function get_permanent_uri(): string { return $this->permanent_url; } public function get_final_requested_uri(): string { return $this->requested_url; } public function get_status_code(): int { return $this->response->getStatusCode(); } public function get_headers(): array { // The filtering is probably redundant but let’s make PHPStan happy. return array_filter($this->response->getHeaders(), function (array $header): bool { return count($header) >= 1; }); } public function has_header(string $name): bool { return $this->response->hasHeader($name); } public function with_header(string $name, $value) { return new self($this->response->withHeader($name, $value), $this->permanent_url, $this->requested_url); } public function get_header(string $name): array { return $this->response->getHeader($name); } public function get_header_line(string $name): string { return $this->response->getHeaderLine($name); } public function get_body_content(): string { return $this->response->getBody()->__toString(); } } Core.php 0000644 00000004273 15162130202 0006143 0 ustar 00 <?php /** * SimplePie * * A PHP-Based RSS and Atom Feed Framework. * Takes the hard work out of managing a complete RSS/Atom solution. * * Copyright (c) 2004-2016, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * * Neither the name of the SimplePie Team nor the names of its contributors may be used * to endorse or promote products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * @package SimplePie * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue * @author Ryan Parman * @author Sam Sneddon * @author Ryan McCue * @link http://simplepie.org/ SimplePie * @license http://www.opensource.org/licenses/bsd-license.php BSD License */ /** * SimplePie class. * * Class for backward compatibility. * * @deprecated Use {@see SimplePie} directly * @package SimplePie * @subpackage API */ class SimplePie_Core extends SimplePie { } RegistryAware.php 0000644 00000000677 15162130204 0010051 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; /** * Handles the injection of Registry into other class * * {@see \SimplePie\SimplePie::get_registry()} */ interface RegistryAware { /** * Set the Registry into the class * * @return void */ public function set_registry(Registry $registry); } Copyright.php 0000644 00000003262 15162130206 0007224 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; /** * Manages `<media:copyright>` copyright tags as defined in Media RSS * * Used by {@see \SimplePie\Enclosure::get_copyright()} * * This class can be overloaded with {@see \SimplePie\SimplePie::set_copyright_class()} */ class Copyright { /** * Copyright URL * * @var ?string * @see get_url() */ public $url; /** * Attribution * * @var ?string * @see get_attribution() */ public $label; /** * Constructor, used to input the data * * For documentation on all the parameters, see the corresponding * properties and their accessors */ public function __construct( ?string $url = null, ?string $label = null ) { $this->url = $url; $this->label = $label; } /** * String-ified version * * @return string */ public function __toString() { // There is no $this->data here return md5(serialize($this)); } /** * Get the copyright URL * * @return string|null URL to copyright information */ public function get_url() { if ($this->url !== null) { return $this->url; } return null; } /** * Get the attribution text * * @return string|null */ public function get_attribution() { if ($this->label !== null) { return $this->label; } return null; } } class_alias('SimplePie\Copyright', 'SimplePie_Copyright'); Credit.php 0000644 00000004140 15162130207 0006463 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; /** * Handles `<media:credit>` as defined in Media RSS * * Used by {@see \SimplePie\Enclosure::get_credit()} and {@see \SimplePie\Enclosure::get_credits()} * * This class can be overloaded with {@see \SimplePie\SimplePie::set_credit_class()} */ class Credit { /** * Credited role * * @var ?string * @see get_role() */ public $role; /** * Organizational scheme * * @var ?string * @see get_scheme() */ public $scheme; /** * Credited name * * @var ?string * @see get_name() */ public $name; /** * Constructor, used to input the data * * For documentation on all the parameters, see the corresponding * properties and their accessors */ public function __construct( ?string $role = null, ?string $scheme = null, ?string $name = null ) { $this->role = $role; $this->scheme = $scheme; $this->name = $name; } /** * String-ified version * * @return string */ public function __toString() { // There is no $this->data here return md5(serialize($this)); } /** * Get the role of the person receiving credit * * @return string|null */ public function get_role() { if ($this->role !== null) { return $this->role; } return null; } /** * Get the organizational scheme * * @return string|null */ public function get_scheme() { if ($this->scheme !== null) { return $this->scheme; } return null; } /** * Get the credited person/entity's name * * @return string|null */ public function get_name() { if ($this->name !== null) { return $this->name; } return null; } } class_alias('SimplePie\Credit', 'SimplePie_Credit'); Exception.php 0000644 00000000543 15162130211 0007205 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; use Exception as NativeException; /** * General SimplePie exception class */ class Exception extends NativeException { } class_alias('SimplePie\Exception', 'SimplePie_Exception'); File.php 0000644 00000152330 15162130212 0006131 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_File', false)) { return; } /** * Class ParagonIE_Sodium_File */ class ParagonIE_Sodium_File extends ParagonIE_Sodium_Core_Util { /* PHP's default buffer size is 8192 for fread()/fwrite(). */ const BUFFER_SIZE = 8192; /** * Box a file (rather than a string). Uses less memory than * ParagonIE_Sodium_Compat::crypto_box(), but produces * the same result. * * @param string $inputFile Absolute path to a file on the filesystem * @param string $outputFile Absolute path to a file on the filesystem * @param string $nonce Number to be used only once * @param string $keyPair ECDH secret key and ECDH public key concatenated * * @return bool * @throws SodiumException * @throws TypeError */ public static function box( $inputFile, $outputFile, $nonce, #[\SensitiveParameter] $keyPair ) { /* Type checks: */ if (!is_string($inputFile)) { throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); } if (!is_string($outputFile)) { throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); } if (!is_string($nonce)) { throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); } /* Input validation: */ if (!is_string($keyPair)) { throw new TypeError('Argument 4 must be a string, ' . gettype($keyPair) . ' given.'); } if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { throw new TypeError('Argument 3 must be CRYPTO_BOX_NONCEBYTES bytes'); } if (self::strlen($keyPair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); } /** @var int $size */ $size = filesize($inputFile); if (!is_int($size)) { throw new SodiumException('Could not obtain the file size'); } /** @var resource $ifp */ $ifp = fopen($inputFile, 'rb'); if (!is_resource($ifp)) { throw new SodiumException('Could not open input file for reading'); } /** @var resource $ofp */ $ofp = fopen($outputFile, 'wb'); if (!is_resource($ofp)) { fclose($ifp); throw new SodiumException('Could not open output file for writing'); } $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $keyPair); fclose($ifp); fclose($ofp); return $res; } /** * Open a boxed file (rather than a string). Uses less memory than * ParagonIE_Sodium_Compat::crypto_box_open(), but produces * the same result. * * Warning: Does not protect against TOCTOU attacks. You should * just load the file into memory and use crypto_box_open() if * you are worried about those. * * @param string $inputFile * @param string $outputFile * @param string $nonce * @param string $keypair * @return bool * @throws SodiumException * @throws TypeError */ public static function box_open( $inputFile, $outputFile, $nonce, #[\SensitiveParameter] $keypair ) { /* Type checks: */ if (!is_string($inputFile)) { throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); } if (!is_string($outputFile)) { throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); } if (!is_string($nonce)) { throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); } if (!is_string($keypair)) { throw new TypeError('Argument 4 must be a string, ' . gettype($keypair) . ' given.'); } /* Input validation: */ if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { throw new TypeError('Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes'); } if (self::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); } if (!file_exists($inputFile)) { throw new SodiumException('Input file does not exist'); } /** @var int $size */ $size = filesize($inputFile); if (!is_int($size)) { throw new SodiumException('Could not obtain the file size'); } /** @var resource $ifp */ $ifp = fopen($inputFile, 'rb'); if (!is_resource($ifp)) { throw new SodiumException('Could not open input file for reading'); } /** @var resource $ofp */ $ofp = @fopen($outputFile, 'wb'); if (!is_resource($ofp)) { fclose($ifp); throw new SodiumException('Could not open output file for writing'); } $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $keypair); fclose($ifp); fclose($ofp); try { ParagonIE_Sodium_Compat::memzero($nonce); ParagonIE_Sodium_Compat::memzero($ephKeypair); } catch (SodiumException $ex) { if (isset($ephKeypair)) { unset($ephKeypair); } } return $res; } /** * Seal a file (rather than a string). Uses less memory than * ParagonIE_Sodium_Compat::crypto_box_seal(), but produces * the same result. * * @param string $inputFile Absolute path to a file on the filesystem * @param string $outputFile Absolute path to a file on the filesystem * @param string $publicKey ECDH public key * * @return bool * @throws SodiumException * @throws TypeError */ public static function box_seal( $inputFile, $outputFile, #[\SensitiveParameter] $publicKey ) { /* Type checks: */ if (!is_string($inputFile)) { throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); } if (!is_string($outputFile)) { throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); } if (!is_string($publicKey)) { throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.'); } /* Input validation: */ if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { throw new TypeError('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes'); } if (!file_exists($inputFile)) { throw new SodiumException('Input file does not exist'); } /** @var int $size */ $size = filesize($inputFile); if (!is_int($size)) { throw new SodiumException('Could not obtain the file size'); } /** @var resource $ifp */ $ifp = fopen($inputFile, 'rb'); if (!is_resource($ifp)) { throw new SodiumException('Could not open input file for reading'); } /** @var resource $ofp */ $ofp = @fopen($outputFile, 'wb'); if (!is_resource($ofp)) { fclose($ifp); throw new SodiumException('Could not open output file for writing'); } /** @var string $ephKeypair */ $ephKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair(); /** @var string $msgKeypair */ $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( ParagonIE_Sodium_Compat::crypto_box_secretkey($ephKeypair), $publicKey ); /** @var string $ephemeralPK */ $ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey($ephKeypair); /** @var string $nonce */ $nonce = ParagonIE_Sodium_Compat::crypto_generichash( $ephemeralPK . $publicKey, '', 24 ); /** @var int $firstWrite */ $firstWrite = fwrite( $ofp, $ephemeralPK, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES ); if (!is_int($firstWrite)) { fclose($ifp); fclose($ofp); ParagonIE_Sodium_Compat::memzero($ephKeypair); throw new SodiumException('Could not write to output file'); } if ($firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { ParagonIE_Sodium_Compat::memzero($ephKeypair); fclose($ifp); fclose($ofp); throw new SodiumException('Error writing public key to output file'); } $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $msgKeypair); fclose($ifp); fclose($ofp); try { ParagonIE_Sodium_Compat::memzero($nonce); ParagonIE_Sodium_Compat::memzero($ephKeypair); } catch (SodiumException $ex) { /** @psalm-suppress PossiblyUndefinedVariable */ unset($ephKeypair); } return $res; } /** * Open a sealed file (rather than a string). Uses less memory than * ParagonIE_Sodium_Compat::crypto_box_seal_open(), but produces * the same result. * * Warning: Does not protect against TOCTOU attacks. You should * just load the file into memory and use crypto_box_seal_open() if * you are worried about those. * * @param string $inputFile * @param string $outputFile * @param string $ecdhKeypair * @return bool * @throws SodiumException * @throws TypeError */ public static function box_seal_open( $inputFile, $outputFile, #[\SensitiveParameter] $ecdhKeypair ) { /* Type checks: */ if (!is_string($inputFile)) { throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); } if (!is_string($outputFile)) { throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); } if (!is_string($ecdhKeypair)) { throw new TypeError('Argument 3 must be a string, ' . gettype($ecdhKeypair) . ' given.'); } /* Input validation: */ if (self::strlen($ecdhKeypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { throw new TypeError('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); } $publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair); if (!file_exists($inputFile)) { throw new SodiumException('Input file does not exist'); } /** @var int $size */ $size = filesize($inputFile); if (!is_int($size)) { throw new SodiumException('Could not obtain the file size'); } /** @var resource $ifp */ $ifp = fopen($inputFile, 'rb'); if (!is_resource($ifp)) { throw new SodiumException('Could not open input file for reading'); } /** @var resource $ofp */ $ofp = @fopen($outputFile, 'wb'); if (!is_resource($ofp)) { fclose($ifp); throw new SodiumException('Could not open output file for writing'); } $ephemeralPK = fread($ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES); if (!is_string($ephemeralPK)) { throw new SodiumException('Could not read input file'); } if (self::strlen($ephemeralPK) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { fclose($ifp); fclose($ofp); throw new SodiumException('Could not read public key from sealed file'); } $nonce = ParagonIE_Sodium_Compat::crypto_generichash( $ephemeralPK . $publicKey, '', 24 ); $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( ParagonIE_Sodium_Compat::crypto_box_secretkey($ecdhKeypair), $ephemeralPK ); $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $msgKeypair); fclose($ifp); fclose($ofp); try { ParagonIE_Sodium_Compat::memzero($nonce); ParagonIE_Sodium_Compat::memzero($ephKeypair); } catch (SodiumException $ex) { if (isset($ephKeypair)) { unset($ephKeypair); } } return $res; } /** * Calculate the BLAKE2b hash of a file. * * @param string $filePath Absolute path to a file on the filesystem * @param string|null $key BLAKE2b key * @param int $outputLength Length of hash output * * @return string BLAKE2b hash * @throws SodiumException * @throws TypeError * @psalm-suppress FailedTypeResolution */ public static function generichash( $filePath, #[\SensitiveParameter] $key = '', $outputLength = 32 ) { /* Type checks: */ if (!is_string($filePath)) { throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); } if (!is_string($key)) { if (is_null($key)) { $key = ''; } else { throw new TypeError('Argument 2 must be a string, ' . gettype($key) . ' given.'); } } if (!is_int($outputLength)) { if (!is_numeric($outputLength)) { throw new TypeError('Argument 3 must be an integer, ' . gettype($outputLength) . ' given.'); } $outputLength = (int) $outputLength; } /* Input validation: */ if (!empty($key)) { if (self::strlen($key) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN) { throw new TypeError('Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes'); } if (self::strlen($key) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX) { throw new TypeError('Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes'); } } if ($outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN) { throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN'); } if ($outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX) { throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX'); } if (!file_exists($filePath)) { throw new SodiumException('File does not exist'); } /** @var int $size */ $size = filesize($filePath); if (!is_int($size)) { throw new SodiumException('Could not obtain the file size'); } /** @var resource $fp */ $fp = fopen($filePath, 'rb'); if (!is_resource($fp)) { throw new SodiumException('Could not open input file for reading'); } $ctx = ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outputLength); while ($size > 0) { $blockSize = $size > 64 ? 64 : $size; $read = fread($fp, $blockSize); if (!is_string($read)) { throw new SodiumException('Could not read input file'); } ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $read); $size -= $blockSize; } fclose($fp); return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength); } /** * Encrypt a file (rather than a string). Uses less memory than * ParagonIE_Sodium_Compat::crypto_secretbox(), but produces * the same result. * * @param string $inputFile Absolute path to a file on the filesystem * @param string $outputFile Absolute path to a file on the filesystem * @param string $nonce Number to be used only once * @param string $key Encryption key * * @return bool * @throws SodiumException * @throws TypeError */ public static function secretbox( $inputFile, $outputFile, $nonce, #[\SensitiveParameter] $key ) { /* Type checks: */ if (!is_string($inputFile)) { throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..'); } if (!is_string($outputFile)) { throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); } if (!is_string($nonce)) { throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); } /* Input validation: */ if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); } if (!is_string($key)) { throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); } if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes'); } if (!file_exists($inputFile)) { throw new SodiumException('Input file does not exist'); } /** @var int $size */ $size = filesize($inputFile); if (!is_int($size)) { throw new SodiumException('Could not obtain the file size'); } /** @var resource $ifp */ $ifp = @fopen($inputFile, 'rb'); if (!is_resource($ifp)) { throw new SodiumException('Could not open input file for reading'); } /** @var resource $ofp */ $ofp = fopen($outputFile, 'wb'); if (!is_resource($ofp)) { fclose($ifp); throw new SodiumException('Could not open output file for writing'); } $res = self::secretbox_encrypt($ifp, $ofp, $size, $nonce, $key); fclose($ifp); fclose($ofp); return $res; } /** * Seal a file (rather than a string). Uses less memory than * ParagonIE_Sodium_Compat::crypto_secretbox_open(), but produces * the same result. * * Warning: Does not protect against TOCTOU attacks. You should * just load the file into memory and use crypto_secretbox_open() if * you are worried about those. * * @param string $inputFile * @param string $outputFile * @param string $nonce * @param string $key * @return bool * @throws SodiumException * @throws TypeError */ public static function secretbox_open( $inputFile, $outputFile, $nonce, #[\SensitiveParameter] $key ) { /* Type checks: */ if (!is_string($inputFile)) { throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); } if (!is_string($outputFile)) { throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); } if (!is_string($nonce)) { throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); } if (!is_string($key)) { throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); } /* Input validation: */ if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); } if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes'); } if (!file_exists($inputFile)) { throw new SodiumException('Input file does not exist'); } /** @var int $size */ $size = filesize($inputFile); if (!is_int($size)) { throw new SodiumException('Could not obtain the file size'); } /** @var resource $ifp */ $ifp = fopen($inputFile, 'rb'); if (!is_resource($ifp)) { throw new SodiumException('Could not open input file for reading'); } /** @var resource $ofp */ $ofp = @fopen($outputFile, 'wb'); if (!is_resource($ofp)) { fclose($ifp); throw new SodiumException('Could not open output file for writing'); } $res = self::secretbox_decrypt($ifp, $ofp, $size, $nonce, $key); fclose($ifp); fclose($ofp); try { ParagonIE_Sodium_Compat::memzero($key); } catch (SodiumException $ex) { /** @psalm-suppress PossiblyUndefinedVariable */ unset($key); } return $res; } /** * Sign a file (rather than a string). Uses less memory than * ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces * the same result. * * @param string $filePath Absolute path to a file on the filesystem * @param string $secretKey Secret signing key * * @return string Ed25519 signature * @throws SodiumException * @throws TypeError */ public static function sign( $filePath, #[\SensitiveParameter] $secretKey ) { /* Type checks: */ if (!is_string($filePath)) { throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); } if (!is_string($secretKey)) { throw new TypeError('Argument 2 must be a string, ' . gettype($secretKey) . ' given.'); } /* Input validation: */ if (self::strlen($secretKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES) { throw new TypeError('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes'); } if (PHP_INT_SIZE === 4) { return self::sign_core32($filePath, $secretKey); } if (!file_exists($filePath)) { throw new SodiumException('File does not exist'); } /** @var int $size */ $size = filesize($filePath); if (!is_int($size)) { throw new SodiumException('Could not obtain the file size'); } /** @var resource $fp */ $fp = fopen($filePath, 'rb'); if (!is_resource($fp)) { throw new SodiumException('Could not open input file for reading'); } /** @var string $az */ $az = hash('sha512', self::substr($secretKey, 0, 32), true); $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); $hs = hash_init('sha512'); self::hash_update($hs, self::substr($az, 32, 32)); /** @var resource $hs */ $hs = self::updateHashWithFile($hs, $fp, $size); /** @var string $nonceHash */ $nonceHash = hash_final($hs, true); /** @var string $pk */ $pk = self::substr($secretKey, 32, 32); /** @var string $nonce */ $nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32); /** @var string $sig */ $sig = ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes( ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base($nonce) ); $hs = hash_init('sha512'); self::hash_update($hs, self::substr($sig, 0, 32)); self::hash_update($hs, self::substr($pk, 0, 32)); /** @var resource $hs */ $hs = self::updateHashWithFile($hs, $fp, $size); /** @var string $hramHash */ $hramHash = hash_final($hs, true); /** @var string $hram */ $hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hramHash); /** @var string $sigAfter */ $sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd($hram, $az, $nonce); /** @var string $sig */ $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); try { ParagonIE_Sodium_Compat::memzero($az); } catch (SodiumException $ex) { $az = null; } fclose($fp); return $sig; } /** * Verify a file (rather than a string). Uses less memory than * ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but * produces the same result. * * @param string $sig Ed25519 signature * @param string $filePath Absolute path to a file on the filesystem * @param string $publicKey Signing public key * * @return bool * @throws SodiumException * @throws TypeError * @throws Exception */ public static function verify( $sig, $filePath, $publicKey ) { /* Type checks: */ if (!is_string($sig)) { throw new TypeError('Argument 1 must be a string, ' . gettype($sig) . ' given.'); } if (!is_string($filePath)) { throw new TypeError('Argument 2 must be a string, ' . gettype($filePath) . ' given.'); } if (!is_string($publicKey)) { throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.'); } /* Input validation: */ if (self::strlen($sig) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES) { throw new TypeError('Argument 1 must be CRYPTO_SIGN_BYTES bytes'); } if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES) { throw new TypeError('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes'); } if (self::strlen($sig) < 64) { throw new SodiumException('Signature is too short'); } if (PHP_INT_SIZE === 4) { return self::verify_core32($sig, $filePath, $publicKey); } /* Security checks */ if ( (ParagonIE_Sodium_Core_Ed25519::chrToInt($sig[63]) & 240) && ParagonIE_Sodium_Core_Ed25519::check_S_lt_L(self::substr($sig, 32, 32)) ) { throw new SodiumException('S < L - Invalid signature'); } if (ParagonIE_Sodium_Core_Ed25519::small_order($sig)) { throw new SodiumException('Signature is on too small of an order'); } if ((self::chrToInt($sig[63]) & 224) !== 0) { throw new SodiumException('Invalid signature'); } $d = 0; for ($i = 0; $i < 32; ++$i) { $d |= self::chrToInt($publicKey[$i]); } if ($d === 0) { throw new SodiumException('All zero public key'); } if (!file_exists($filePath)) { throw new SodiumException('File does not exist'); } /** @var int $size */ $size = filesize($filePath); if (!is_int($size)) { throw new SodiumException('Could not obtain the file size'); } /** @var resource $fp */ $fp = fopen($filePath, 'rb'); if (!is_resource($fp)) { throw new SodiumException('Could not open input file for reading'); } /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ $orig = ParagonIE_Sodium_Compat::$fastMult; // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. ParagonIE_Sodium_Compat::$fastMult = true; /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */ $A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime($publicKey); $hs = hash_init('sha512'); self::hash_update($hs, self::substr($sig, 0, 32)); self::hash_update($hs, self::substr($publicKey, 0, 32)); /** @var resource $hs */ $hs = self::updateHashWithFile($hs, $fp, $size); /** @var string $hDigest */ $hDigest = hash_final($hs, true); /** @var string $h */ $h = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32); /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */ $R = ParagonIE_Sodium_Core_Ed25519::ge_double_scalarmult_vartime( $h, $A, self::substr($sig, 32) ); /** @var string $rcheck */ $rcheck = ParagonIE_Sodium_Core_Ed25519::ge_tobytes($R); // Close the file handle fclose($fp); // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. ParagonIE_Sodium_Compat::$fastMult = $orig; return self::verify_32($rcheck, self::substr($sig, 0, 32)); } /** * @param resource $ifp * @param resource $ofp * @param int $mlen * @param string $nonce * @param string $boxKeypair * @return bool * @throws SodiumException * @throws TypeError */ protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) { if (PHP_INT_SIZE === 4) { return self::secretbox_encrypt( $ifp, $ofp, $mlen, $nonce, ParagonIE_Sodium_Crypto32::box_beforenm( ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair), ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair) ) ); } return self::secretbox_encrypt( $ifp, $ofp, $mlen, $nonce, ParagonIE_Sodium_Crypto::box_beforenm( ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair), ParagonIE_Sodium_Crypto::box_publickey($boxKeypair) ) ); } /** * @param resource $ifp * @param resource $ofp * @param int $mlen * @param string $nonce * @param string $boxKeypair * @return bool * @throws SodiumException * @throws TypeError */ protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) { if (PHP_INT_SIZE === 4) { return self::secretbox_decrypt( $ifp, $ofp, $mlen, $nonce, ParagonIE_Sodium_Crypto32::box_beforenm( ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair), ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair) ) ); } return self::secretbox_decrypt( $ifp, $ofp, $mlen, $nonce, ParagonIE_Sodium_Crypto::box_beforenm( ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair), ParagonIE_Sodium_Crypto::box_publickey($boxKeypair) ) ); } /** * Encrypt a file * * @param resource $ifp * @param resource $ofp * @param int $mlen * @param string $nonce * @param string $key * @return bool * @throws SodiumException * @throws TypeError */ protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key) { if (PHP_INT_SIZE === 4) { return self::secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key); } $plaintext = fread($ifp, 32); if (!is_string($plaintext)) { throw new SodiumException('Could not read input file'); } $first32 = self::ftell($ifp); /** @var string $subkey */ $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); /** @var string $realNonce */ $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); /** @var string $block0 */ $block0 = str_repeat("\x00", 32); /** @var int $mlen - Length of the plaintext message */ $mlen0 = $mlen; if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) { $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES; } $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); /** @var string $block0 */ $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( $block0, $realNonce, $subkey ); $state = new ParagonIE_Sodium_Core_Poly1305_State( ParagonIE_Sodium_Core_Util::substr( $block0, 0, ParagonIE_Sodium_Crypto::onetimeauth_poly1305_KEYBYTES ) ); // Pre-write 16 blank bytes for the Poly1305 tag $start = self::ftell($ofp); fwrite($ofp, str_repeat("\x00", 16)); /** @var string $c */ $cBlock = ParagonIE_Sodium_Core_Util::substr( $block0, ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES ); $state->update($cBlock); fwrite($ofp, $cBlock); $mlen -= 32; /** @var int $iter */ $iter = 1; /** @var int $incr */ $incr = self::BUFFER_SIZE >> 6; /* * Set the cursor to the end of the first half-block. All future bytes will * generated from salsa20_xor_ic, starting from 1 (second block). */ fseek($ifp, $first32, SEEK_SET); while ($mlen > 0) { $blockSize = $mlen > self::BUFFER_SIZE ? self::BUFFER_SIZE : $mlen; $plaintext = fread($ifp, $blockSize); if (!is_string($plaintext)) { throw new SodiumException('Could not read input file'); } $cBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( $plaintext, $realNonce, $iter, $subkey ); fwrite($ofp, $cBlock, $blockSize); $state->update($cBlock); $mlen -= $blockSize; $iter += $incr; } try { ParagonIE_Sodium_Compat::memzero($block0); ParagonIE_Sodium_Compat::memzero($subkey); } catch (SodiumException $ex) { $block0 = null; $subkey = null; } $end = self::ftell($ofp); /* * Write the Poly1305 authentication tag that provides integrity * over the ciphertext (encrypt-then-MAC) */ fseek($ofp, $start, SEEK_SET); fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES); fseek($ofp, $end, SEEK_SET); unset($state); return true; } /** * Decrypt a file * * @param resource $ifp * @param resource $ofp * @param int $mlen * @param string $nonce * @param string $key * @return bool * @throws SodiumException * @throws TypeError */ protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key) { if (PHP_INT_SIZE === 4) { return self::secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key); } $tag = fread($ifp, 16); if (!is_string($tag)) { throw new SodiumException('Could not read input file'); } /** @var string $subkey */ $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); /** @var string $realNonce */ $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); /** @var string $block0 */ $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( 64, ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), $subkey ); /* Verify the Poly1305 MAC -before- attempting to decrypt! */ $state = new ParagonIE_Sodium_Core_Poly1305_State(self::substr($block0, 0, 32)); if (!self::onetimeauth_verify($state, $ifp, $tag, $mlen)) { throw new SodiumException('Invalid MAC'); } /* * Set the cursor to the end of the first half-block. All future bytes will * generated from salsa20_xor_ic, starting from 1 (second block). */ $first32 = fread($ifp, 32); if (!is_string($first32)) { throw new SodiumException('Could not read input file'); } $first32len = self::strlen($first32); fwrite( $ofp, self::xorStrings( self::substr($block0, 32, $first32len), self::substr($first32, 0, $first32len) ) ); $mlen -= 32; /** @var int $iter */ $iter = 1; /** @var int $incr */ $incr = self::BUFFER_SIZE >> 6; /* Decrypts ciphertext, writes to output file. */ while ($mlen > 0) { $blockSize = $mlen > self::BUFFER_SIZE ? self::BUFFER_SIZE : $mlen; $ciphertext = fread($ifp, $blockSize); if (!is_string($ciphertext)) { throw new SodiumException('Could not read input file'); } $pBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( $ciphertext, $realNonce, $iter, $subkey ); fwrite($ofp, $pBlock, $blockSize); $mlen -= $blockSize; $iter += $incr; } return true; } /** * @param ParagonIE_Sodium_Core_Poly1305_State $state * @param resource $ifp * @param string $tag * @param int $mlen * @return bool * @throws SodiumException * @throws TypeError */ protected static function onetimeauth_verify( ParagonIE_Sodium_Core_Poly1305_State $state, $ifp, $tag = '', $mlen = 0 ) { /** @var int $pos */ $pos = self::ftell($ifp); /** @var int $iter */ $iter = 1; /** @var int $incr */ $incr = self::BUFFER_SIZE >> 6; while ($mlen > 0) { $blockSize = $mlen > self::BUFFER_SIZE ? self::BUFFER_SIZE : $mlen; $ciphertext = fread($ifp, $blockSize); if (!is_string($ciphertext)) { throw new SodiumException('Could not read input file'); } $state->update($ciphertext); $mlen -= $blockSize; $iter += $incr; } $res = ParagonIE_Sodium_Core_Util::verify_16($tag, $state->finish()); fseek($ifp, $pos, SEEK_SET); return $res; } /** * Update a hash context with the contents of a file, without * loading the entire file into memory. * * @param resource|HashContext $hash * @param resource $fp * @param int $size * @return resource|object Resource on PHP < 7.2, HashContext object on PHP >= 7.2 * @throws SodiumException * @throws TypeError * @psalm-suppress PossiblyInvalidArgument * PHP 7.2 changes from a resource to an object, * which causes Psalm to complain about an error. * @psalm-suppress TypeCoercion * Ditto. */ public static function updateHashWithFile($hash, $fp, $size = 0) { /* Type checks: */ if (PHP_VERSION_ID < 70200) { if (!is_resource($hash)) { throw new TypeError('Argument 1 must be a resource, ' . gettype($hash) . ' given.'); } } else { if (!is_object($hash)) { throw new TypeError('Argument 1 must be an object (PHP 7.2+), ' . gettype($hash) . ' given.'); } } if (!is_resource($fp)) { throw new TypeError('Argument 2 must be a resource, ' . gettype($fp) . ' given.'); } if (!is_int($size)) { throw new TypeError('Argument 3 must be an integer, ' . gettype($size) . ' given.'); } /** @var int $originalPosition */ $originalPosition = self::ftell($fp); // Move file pointer to beginning of file fseek($fp, 0, SEEK_SET); for ($i = 0; $i < $size; $i += self::BUFFER_SIZE) { /** @var string|bool $message */ $message = fread( $fp, ($size - $i) > self::BUFFER_SIZE ? $size - $i : self::BUFFER_SIZE ); if (!is_string($message)) { throw new SodiumException('Unexpected error reading from file.'); } /** @var string $message */ /** @psalm-suppress InvalidArgument */ self::hash_update($hash, $message); } // Reset file pointer's position fseek($fp, $originalPosition, SEEK_SET); return $hash; } /** * Sign a file (rather than a string). Uses less memory than * ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces * the same result. (32-bit) * * @param string $filePath Absolute path to a file on the filesystem * @param string $secretKey Secret signing key * * @return string Ed25519 signature * @throws SodiumException * @throws TypeError */ private static function sign_core32($filePath, $secretKey) { $size = filesize($filePath); if (!is_int($size)) { throw new SodiumException('Could not obtain the file size'); } $fp = fopen($filePath, 'rb'); if (!is_resource($fp)) { throw new SodiumException('Could not open input file for reading'); } /** @var string $az */ $az = hash('sha512', self::substr($secretKey, 0, 32), true); $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); $hs = hash_init('sha512'); self::hash_update($hs, self::substr($az, 32, 32)); /** @var resource $hs */ $hs = self::updateHashWithFile($hs, $fp, $size); $nonceHash = hash_final($hs, true); $pk = self::substr($secretKey, 32, 32); $nonce = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32); $sig = ParagonIE_Sodium_Core32_Ed25519::ge_p3_tobytes( ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base($nonce) ); $hs = hash_init('sha512'); self::hash_update($hs, self::substr($sig, 0, 32)); self::hash_update($hs, self::substr($pk, 0, 32)); /** @var resource $hs */ $hs = self::updateHashWithFile($hs, $fp, $size); $hramHash = hash_final($hs, true); $hram = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hramHash); $sigAfter = ParagonIE_Sodium_Core32_Ed25519::sc_muladd($hram, $az, $nonce); /** @var string $sig */ $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); try { ParagonIE_Sodium_Compat::memzero($az); } catch (SodiumException $ex) { $az = null; } fclose($fp); return $sig; } /** * * Verify a file (rather than a string). Uses less memory than * ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but * produces the same result. (32-bit) * * @param string $sig Ed25519 signature * @param string $filePath Absolute path to a file on the filesystem * @param string $publicKey Signing public key * * @return bool * @throws SodiumException * @throws Exception */ public static function verify_core32($sig, $filePath, $publicKey) { /* Security checks */ if (ParagonIE_Sodium_Core32_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) { throw new SodiumException('S < L - Invalid signature'); } if (ParagonIE_Sodium_Core32_Ed25519::small_order($sig)) { throw new SodiumException('Signature is on too small of an order'); } if ((self::chrToInt($sig[63]) & 224) !== 0) { throw new SodiumException('Invalid signature'); } $d = 0; for ($i = 0; $i < 32; ++$i) { $d |= self::chrToInt($publicKey[$i]); } if ($d === 0) { throw new SodiumException('All zero public key'); } /** @var int|bool $size */ $size = filesize($filePath); if (!is_int($size)) { throw new SodiumException('Could not obtain the file size'); } /** @var int $size */ /** @var resource|bool $fp */ $fp = fopen($filePath, 'rb'); if (!is_resource($fp)) { throw new SodiumException('Could not open input file for reading'); } /** @var resource $fp */ /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ $orig = ParagonIE_Sodium_Compat::$fastMult; // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. ParagonIE_Sodium_Compat::$fastMult = true; /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */ $A = ParagonIE_Sodium_Core32_Ed25519::ge_frombytes_negate_vartime($publicKey); $hs = hash_init('sha512'); self::hash_update($hs, self::substr($sig, 0, 32)); self::hash_update($hs, self::substr($publicKey, 0, 32)); /** @var resource $hs */ $hs = self::updateHashWithFile($hs, $fp, $size); /** @var string $hDigest */ $hDigest = hash_final($hs, true); /** @var string $h */ $h = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32); /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */ $R = ParagonIE_Sodium_Core32_Ed25519::ge_double_scalarmult_vartime( $h, $A, self::substr($sig, 32) ); /** @var string $rcheck */ $rcheck = ParagonIE_Sodium_Core32_Ed25519::ge_tobytes($R); // Close the file handle fclose($fp); // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. ParagonIE_Sodium_Compat::$fastMult = $orig; return self::verify_32($rcheck, self::substr($sig, 0, 32)); } /** * Encrypt a file (32-bit) * * @param resource $ifp * @param resource $ofp * @param int $mlen * @param string $nonce * @param string $key * @return bool * @throws SodiumException * @throws TypeError */ protected static function secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key) { $plaintext = fread($ifp, 32); if (!is_string($plaintext)) { throw new SodiumException('Could not read input file'); } $first32 = self::ftell($ifp); /** @var string $subkey */ $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key); /** @var string $realNonce */ $realNonce = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8); /** @var string $block0 */ $block0 = str_repeat("\x00", 32); /** @var int $mlen - Length of the plaintext message */ $mlen0 = $mlen; if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) { $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES; } $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0); /** @var string $block0 */ $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor( $block0, $realNonce, $subkey ); $state = new ParagonIE_Sodium_Core32_Poly1305_State( ParagonIE_Sodium_Core32_Util::substr( $block0, 0, ParagonIE_Sodium_Crypto::onetimeauth_poly1305_KEYBYTES ) ); // Pre-write 16 blank bytes for the Poly1305 tag $start = self::ftell($ofp); fwrite($ofp, str_repeat("\x00", 16)); /** @var string $c */ $cBlock = ParagonIE_Sodium_Core32_Util::substr( $block0, ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES ); $state->update($cBlock); fwrite($ofp, $cBlock); $mlen -= 32; /** @var int $iter */ $iter = 1; /** @var int $incr */ $incr = self::BUFFER_SIZE >> 6; /* * Set the cursor to the end of the first half-block. All future bytes will * generated from salsa20_xor_ic, starting from 1 (second block). */ fseek($ifp, $first32, SEEK_SET); while ($mlen > 0) { $blockSize = $mlen > self::BUFFER_SIZE ? self::BUFFER_SIZE : $mlen; $plaintext = fread($ifp, $blockSize); if (!is_string($plaintext)) { throw new SodiumException('Could not read input file'); } $cBlock = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic( $plaintext, $realNonce, $iter, $subkey ); fwrite($ofp, $cBlock, $blockSize); $state->update($cBlock); $mlen -= $blockSize; $iter += $incr; } try { ParagonIE_Sodium_Compat::memzero($block0); ParagonIE_Sodium_Compat::memzero($subkey); } catch (SodiumException $ex) { $block0 = null; $subkey = null; } $end = self::ftell($ofp); /* * Write the Poly1305 authentication tag that provides integrity * over the ciphertext (encrypt-then-MAC) */ fseek($ofp, $start, SEEK_SET); fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES); fseek($ofp, $end, SEEK_SET); unset($state); return true; } /** * Decrypt a file (32-bit) * * @param resource $ifp * @param resource $ofp * @param int $mlen * @param string $nonce * @param string $key * @return bool * @throws SodiumException * @throws TypeError */ protected static function secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key) { $tag = fread($ifp, 16); if (!is_string($tag)) { throw new SodiumException('Could not read input file'); } /** @var string $subkey */ $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key); /** @var string $realNonce */ $realNonce = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8); /** @var string $block0 */ $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20( 64, ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8), $subkey ); /* Verify the Poly1305 MAC -before- attempting to decrypt! */ $state = new ParagonIE_Sodium_Core32_Poly1305_State(self::substr($block0, 0, 32)); if (!self::onetimeauth_verify_core32($state, $ifp, $tag, $mlen)) { throw new SodiumException('Invalid MAC'); } /* * Set the cursor to the end of the first half-block. All future bytes will * generated from salsa20_xor_ic, starting from 1 (second block). */ $first32 = fread($ifp, 32); if (!is_string($first32)) { throw new SodiumException('Could not read input file'); } $first32len = self::strlen($first32); fwrite( $ofp, self::xorStrings( self::substr($block0, 32, $first32len), self::substr($first32, 0, $first32len) ) ); $mlen -= 32; /** @var int $iter */ $iter = 1; /** @var int $incr */ $incr = self::BUFFER_SIZE >> 6; /* Decrypts ciphertext, writes to output file. */ while ($mlen > 0) { $blockSize = $mlen > self::BUFFER_SIZE ? self::BUFFER_SIZE : $mlen; $ciphertext = fread($ifp, $blockSize); if (!is_string($ciphertext)) { throw new SodiumException('Could not read input file'); } $pBlock = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic( $ciphertext, $realNonce, $iter, $subkey ); fwrite($ofp, $pBlock, $blockSize); $mlen -= $blockSize; $iter += $incr; } return true; } /** * One-time message authentication for 32-bit systems * * @param ParagonIE_Sodium_Core32_Poly1305_State $state * @param resource $ifp * @param string $tag * @param int $mlen * @return bool * @throws SodiumException * @throws TypeError */ protected static function onetimeauth_verify_core32( ParagonIE_Sodium_Core32_Poly1305_State $state, $ifp, $tag = '', $mlen = 0 ) { /** @var int $pos */ $pos = self::ftell($ifp); while ($mlen > 0) { $blockSize = $mlen > self::BUFFER_SIZE ? self::BUFFER_SIZE : $mlen; $ciphertext = fread($ifp, $blockSize); if (!is_string($ciphertext)) { throw new SodiumException('Could not read input file'); } $state->update($ciphertext); $mlen -= $blockSize; } $res = ParagonIE_Sodium_Core32_Util::verify_16($tag, $state->finish()); fseek($ifp, $pos, SEEK_SET); return $res; } /** * @param resource $resource * @return int * @throws SodiumException */ private static function ftell($resource) { $return = ftell($resource); if (!is_int($return)) { throw new SodiumException('ftell() returned false'); } return (int) $return; } } Restriction.php 0000644 00000004501 15162130214 0007555 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; /** * Handles `<media:restriction>` as defined in Media RSS * * Used by {@see \SimplePie\Enclosure::get_restriction()} and {@see \SimplePie\Enclosure::get_restrictions()} * * This class can be overloaded with {@see \SimplePie\SimplePie::set_restriction_class()} */ class Restriction { public const RELATIONSHIP_ALLOW = 'allow'; public const RELATIONSHIP_DENY = 'deny'; /** * Relationship ('allow'/'deny') * * @var self::RELATIONSHIP_*|null * @see get_relationship() */ public $relationship; /** * Type of restriction * * @var string|null * @see get_type() */ public $type; /** * Restricted values * * @var string|null * @see get_value() */ public $value; /** * Constructor, used to input the data * * For documentation on all the parameters, see the corresponding * properties and their accessors * * @param ?self::RELATIONSHIP_* $relationship */ public function __construct(?string $relationship = null, ?string $type = null, ?string $value = null) { $this->relationship = $relationship; $this->type = $type; $this->value = $value; } /** * String-ified version * * @return string */ public function __toString() { // There is no $this->data here return md5(serialize($this)); } /** * Get the relationship * * @return ?self::RELATIONSHIP_* */ public function get_relationship() { if ($this->relationship !== null) { return $this->relationship; } return null; } /** * Get the type * * @return string|null */ public function get_type() { if ($this->type !== null) { return $this->type; } return null; } /** * Get the list of restricted things * * @return string|null */ public function get_value() { if ($this->value !== null) { return $this->value; } return null; } } class_alias('SimplePie\Restriction', 'SimplePie_Restriction'); XML/Declaration/Parser.php 0000644 00000017100 15162130220 0011365 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie\XML\Declaration; /** * Parses the XML Declaration */ class Parser { /** * XML Version * * @access public * @var string */ public $version = '1.0'; /** * Encoding * * @access public * @var string */ public $encoding = 'UTF-8'; /** * Standalone * * @access public * @var bool */ public $standalone = false; private const STATE_BEFORE_VERSION_NAME = 'before_version_name'; private const STATE_VERSION_NAME = 'version_name'; private const STATE_VERSION_EQUALS = 'version_equals'; private const STATE_VERSION_VALUE = 'version_value'; private const STATE_ENCODING_NAME = 'encoding_name'; private const STATE_EMIT = 'emit'; private const STATE_ENCODING_EQUALS = 'encoding_equals'; private const STATE_STANDALONE_NAME = 'standalone_name'; private const STATE_ENCODING_VALUE = 'encoding_value'; private const STATE_STANDALONE_EQUALS = 'standalone_equals'; private const STATE_STANDALONE_VALUE = 'standalone_value'; private const STATE_ERROR = false; /** * Current state of the state machine * * @access private * @var self::STATE_* */ public $state = self::STATE_BEFORE_VERSION_NAME; /** * Input data * * @access private * @var string */ public $data = ''; /** * Input data length (to avoid calling strlen() everytime this is needed) * * @access private * @var int */ public $data_length = 0; /** * Current position of the pointer * * @var int * @access private */ public $position = 0; /** * Create an instance of the class with the input data * * @access public * @param string $data Input data */ public function __construct(string $data) { $this->data = $data; $this->data_length = strlen($this->data); } /** * Parse the input data * * @access public * @return bool true on success, false on failure */ public function parse(): bool { while ($this->state && $this->state !== self::STATE_EMIT && $this->has_data()) { $state = $this->state; $this->$state(); } $this->data = ''; if ($this->state === self::STATE_EMIT) { return true; } // Reset the parser state. $this->version = '1.0'; $this->encoding = 'UTF-8'; $this->standalone = false; return false; } /** * Check whether there is data beyond the pointer * * @access private * @return bool true if there is further data, false if not */ public function has_data(): bool { return (bool) ($this->position < $this->data_length); } /** * Advance past any whitespace * * @return int Number of whitespace characters passed */ public function skip_whitespace() { $whitespace = strspn($this->data, "\x09\x0A\x0D\x20", $this->position); $this->position += $whitespace; return $whitespace; } /** * Read value * * @return string|false */ public function get_value() { $quote = substr($this->data, $this->position, 1); if ($quote === '"' || $quote === "'") { $this->position++; $len = strcspn($this->data, $quote, $this->position); if ($this->has_data()) { $value = substr($this->data, $this->position, $len); $this->position += $len + 1; return $value; } } return false; } public function before_version_name(): void { if ($this->skip_whitespace()) { $this->state = self::STATE_VERSION_NAME; } else { $this->state = self::STATE_ERROR; } } public function version_name(): void { if (substr($this->data, $this->position, 7) === 'version') { $this->position += 7; $this->skip_whitespace(); $this->state = self::STATE_VERSION_EQUALS; } else { $this->state = self::STATE_ERROR; } } public function version_equals(): void { if (substr($this->data, $this->position, 1) === '=') { $this->position++; $this->skip_whitespace(); $this->state = self::STATE_VERSION_VALUE; } else { $this->state = self::STATE_ERROR; } } public function version_value(): void { if ($version = $this->get_value()) { $this->version = $version; $this->skip_whitespace(); if ($this->has_data()) { $this->state = self::STATE_ENCODING_NAME; } else { $this->state = self::STATE_EMIT; } } else { $this->state = self::STATE_ERROR; } } public function encoding_name(): void { if (substr($this->data, $this->position, 8) === 'encoding') { $this->position += 8; $this->skip_whitespace(); $this->state = self::STATE_ENCODING_EQUALS; } else { $this->state = self::STATE_STANDALONE_NAME; } } public function encoding_equals(): void { if (substr($this->data, $this->position, 1) === '=') { $this->position++; $this->skip_whitespace(); $this->state = self::STATE_ENCODING_VALUE; } else { $this->state = self::STATE_ERROR; } } public function encoding_value(): void { if ($encoding = $this->get_value()) { $this->encoding = $encoding; $this->skip_whitespace(); if ($this->has_data()) { $this->state = self::STATE_STANDALONE_NAME; } else { $this->state = self::STATE_EMIT; } } else { $this->state = self::STATE_ERROR; } } public function standalone_name(): void { if (substr($this->data, $this->position, 10) === 'standalone') { $this->position += 10; $this->skip_whitespace(); $this->state = self::STATE_STANDALONE_EQUALS; } else { $this->state = self::STATE_ERROR; } } public function standalone_equals(): void { if (substr($this->data, $this->position, 1) === '=') { $this->position++; $this->skip_whitespace(); $this->state = self::STATE_STANDALONE_VALUE; } else { $this->state = self::STATE_ERROR; } } public function standalone_value(): void { if ($standalone = $this->get_value()) { switch ($standalone) { case 'yes': $this->standalone = true; break; case 'no': $this->standalone = false; break; default: $this->state = self::STATE_ERROR; return; } $this->skip_whitespace(); if ($this->has_data()) { $this->state = self::STATE_ERROR; } else { $this->state = self::STATE_EMIT; } } else { $this->state = self::STATE_ERROR; } } } class_alias('SimplePie\XML\Declaration\Parser', 'SimplePie_XML_Declaration_Parser'); Gzdecode.php 0000644 00000020500 15162130221 0006767 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; /** * Decode 'gzip' encoded HTTP data * * @link http://www.gzip.org/format.txt * @link https://www.php.net/manual/en/function.gzdecode.php * @deprecated since SimplePie 1.9.0, use `gzdecode` function instead. */ class Gzdecode { /** * Compressed data * * @access private * @var string * @see gzdecode::$data */ public $compressed_data; /** * Size of compressed data * * @access private * @var int */ public $compressed_size; /** * Minimum size of a valid gzip string * * @access private * @var int */ public $min_compressed_size = 18; /** * Current position of pointer * * @access private * @var int */ public $position = 0; /** * Flags (FLG) * * @access private * @var int */ public $flags; /** * Uncompressed data * * @access public * @see gzdecode::$compressed_data * @var string */ public $data; /** * Modified time * * @access public * @var int */ public $MTIME; /** * Extra Flags * * @access public * @var int */ public $XFL; /** * Operating System * * @access public * @var int */ public $OS; /** * Subfield ID 1 * * @access public * @see gzdecode::$extra_field * @see gzdecode::$SI2 * @var string */ public $SI1; /** * Subfield ID 2 * * @access public * @see gzdecode::$extra_field * @see gzdecode::$SI1 * @var string */ public $SI2; /** * Extra field content * * @access public * @see gzdecode::$SI1 * @see gzdecode::$SI2 * @var string */ public $extra_field; /** * Original filename * * @access public * @var string */ public $filename; /** * Human readable comment * * @access public * @var string */ public $comment; /** * Don't allow anything to be set * * @param string $name * @param mixed $value */ public function __set(string $name, $value) { throw new Exception("Cannot write property $name"); } /** * Set the compressed string and related properties * * @param string $data */ public function __construct(string $data) { $this->compressed_data = $data; $this->compressed_size = strlen($data); } /** * Decode the GZIP stream * * @return bool Successfulness */ public function parse() { if ($this->compressed_size >= $this->min_compressed_size) { $len = 0; // Check ID1, ID2, and CM if (substr($this->compressed_data, 0, 3) !== "\x1F\x8B\x08") { return false; } // Get the FLG (FLaGs) $this->flags = ord($this->compressed_data[3]); // FLG bits above (1 << 4) are reserved if ($this->flags > 0x1F) { return false; } // Advance the pointer after the above $this->position += 4; // MTIME $mtime = substr($this->compressed_data, $this->position, 4); // Reverse the string if we're on a big-endian arch because l is the only signed long and is machine endianness if (current((array) unpack('S', "\x00\x01")) === 1) { $mtime = strrev($mtime); } $this->MTIME = current((array) unpack('l', $mtime)); $this->position += 4; // Get the XFL (eXtra FLags) $this->XFL = ord($this->compressed_data[$this->position++]); // Get the OS (Operating System) $this->OS = ord($this->compressed_data[$this->position++]); // Parse the FEXTRA if ($this->flags & 4) { // Read subfield IDs $this->SI1 = $this->compressed_data[$this->position++]; $this->SI2 = $this->compressed_data[$this->position++]; // SI2 set to zero is reserved for future use if ($this->SI2 === "\x00") { return false; } // Get the length of the extra field $len = current((array) unpack('v', substr($this->compressed_data, $this->position, 2))); $this->position += 2; // Check the length of the string is still valid $this->min_compressed_size += $len + 4; if ($this->compressed_size >= $this->min_compressed_size) { // Set the extra field to the given data $this->extra_field = substr($this->compressed_data, $this->position, $len); $this->position += $len; } else { return false; } } // Parse the FNAME if ($this->flags & 8) { // Get the length of the filename $len = strcspn($this->compressed_data, "\x00", $this->position); // Check the length of the string is still valid $this->min_compressed_size += $len + 1; if ($this->compressed_size >= $this->min_compressed_size) { // Set the original filename to the given string $this->filename = substr($this->compressed_data, $this->position, $len); $this->position += $len + 1; } else { return false; } } // Parse the FCOMMENT if ($this->flags & 16) { // Get the length of the comment $len = strcspn($this->compressed_data, "\x00", $this->position); // Check the length of the string is still valid $this->min_compressed_size += $len + 1; if ($this->compressed_size >= $this->min_compressed_size) { // Set the original comment to the given string $this->comment = substr($this->compressed_data, $this->position, $len); $this->position += $len + 1; } else { return false; } } // Parse the FHCRC if ($this->flags & 2) { // Check the length of the string is still valid $this->min_compressed_size += $len + 2; if ($this->compressed_size >= $this->min_compressed_size) { // Read the CRC $crc = current((array) unpack('v', substr($this->compressed_data, $this->position, 2))); // Check the CRC matches if ((crc32(substr($this->compressed_data, 0, $this->position)) & 0xFFFF) === $crc) { $this->position += 2; } else { return false; } } else { return false; } } // Decompress the actual data if (($data = gzinflate(substr($this->compressed_data, $this->position, -8))) === false) { return false; } $this->data = $data; $this->position = $this->compressed_size - 8; // Check CRC of data $crc = current((array) unpack('V', substr($this->compressed_data, $this->position, 4))); $this->position += 4; /*if (extension_loaded('hash') && sprintf('%u', current(unpack('V', hash('crc32b', $this->data)))) !== sprintf('%u', $crc)) { return false; }*/ // Check ISIZE of data $isize = current((array) unpack('V', substr($this->compressed_data, $this->position, 4))); $this->position += 4; if (sprintf('%u', strlen($this->data) & 0xFFFFFFFF) !== sprintf('%u', $isize)) { return false; } // Wow, against all odds, we've actually got a valid gzip string return true; } return false; } } class_alias('SimplePie\Gzdecode', 'SimplePie_gzdecode'); Category.php 0000644 00000004650 15162130221 0007030 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; /** * Manages all category-related data * * Used by {@see \SimplePie\Item::get_category()} and {@see \SimplePie\Item::get_categories()} * * This class can be overloaded with {@see \SimplePie\SimplePie::set_category_class()} */ class Category { /** * Category identifier * * @var string|null * @see get_term */ public $term; /** * Categorization scheme identifier * * @var string|null * @see get_scheme() */ public $scheme; /** * Human readable label * * @var string|null * @see get_label() */ public $label; /** * Category type * * category for <category> * subject for <dc:subject> * * @var string|null * @see get_type() */ public $type; /** * Constructor, used to input the data * * @param string|null $term * @param string|null $scheme * @param string|null $label * @param string|null $type */ public function __construct(?string $term = null, ?string $scheme = null, ?string $label = null, ?string $type = null) { $this->term = $term; $this->scheme = $scheme; $this->label = $label; $this->type = $type; } /** * String-ified version * * @return string */ public function __toString() { // There is no $this->data here return md5(serialize($this)); } /** * Get the category identifier * * @return string|null */ public function get_term() { return $this->term; } /** * Get the categorization scheme identifier * * @return string|null */ public function get_scheme() { return $this->scheme; } /** * Get the human readable label * * @param bool $strict * @return string|null */ public function get_label(bool $strict = false) { if ($this->label === null && $strict !== true) { return $this->get_term(); } return $this->label; } /** * Get the category type * * @return string|null */ public function get_type() { return $this->type; } } class_alias('SimplePie\Category', 'SimplePie_Category'); Misc.php 0000644 00000210245 15162130222 0006146 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; use SimplePie\XML\Declaration\Parser; /** * Miscellaneous utilities */ class Misc { /** @var int|null */ private static $SIMPLEPIE_BUILD = null; /** * @return string */ public static function time_hms(int $seconds) { $time = ''; $hours = floor($seconds / 3600); $remainder = $seconds % 3600; if ($hours > 0) { $time .= $hours.':'; } $minutes = floor($remainder / 60); $seconds = $remainder % 60; if ($minutes < 10 && $hours > 0) { $minutes = '0' . $minutes; } if ($seconds < 10) { $seconds = '0' . $seconds; } $time .= $minutes.':'; $time .= $seconds; return $time; } /** * @return string|false */ public static function absolutize_url(string $relative, string $base) { $iri = \SimplePie\IRI::absolutize(new \SimplePie\IRI($base), $relative); if ($iri === false) { return false; } return $iri->get_uri(); } /** * @internal */ public static function is_remote_uri(string $uri): bool { return preg_match('/^https?:\/\//i', $uri) === 1; } /** * Get a HTML/XML element from a HTML string * * @deprecated since SimplePie 1.3, use DOMDocument instead (parsing HTML with regex is bad!) * @param string $realname Element name (including namespace prefix if applicable) * @param string $string HTML document * @return array<array{tag: string, self_closing: bool, attribs: array<string, array{data: string}>, content?: string}> */ public static function get_element(string $realname, string $string) { // trigger_error(sprintf('Using method "' . __METHOD__ . '" is deprecated since SimplePie 1.3, use "DOMDocument" instead.'), \E_USER_DEPRECATED); $return = []; $name = preg_quote($realname, '/'); if (preg_match_all("/<($name)" . \SimplePie\SimplePie::PCRE_HTML_ATTRIBUTE . "(>(.*)<\/$name>|(\/)?>)/siU", $string, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) { for ($i = 0, $total_matches = count($matches); $i < $total_matches; $i++) { $return[$i]['tag'] = $realname; $return[$i]['full'] = $matches[$i][0][0]; $return[$i]['offset'] = $matches[$i][0][1]; if (strlen($matches[$i][3][0]) <= 2) { $return[$i]['self_closing'] = true; } else { $return[$i]['self_closing'] = false; $return[$i]['content'] = $matches[$i][4][0]; } $return[$i]['attribs'] = []; if (isset($matches[$i][2][0]) && preg_match_all('/[\x09\x0A\x0B\x0C\x0D\x20]+([^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3D\x3E]*)(?:[\x09\x0A\x0B\x0C\x0D\x20]*=[\x09\x0A\x0B\x0C\x0D\x20]*(?:"([^"]*)"|\'([^\']*)\'|([^\x09\x0A\x0B\x0C\x0D\x20\x22\x27\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x3E]*)?))?/', ' ' . $matches[$i][2][0] . ' ', $attribs, PREG_SET_ORDER)) { foreach ($attribs as $attrib) { if (count($attrib) === 2) { $attrib[2] = $attrib[1]; } $return[$i]['attribs'][strtolower($attrib[1])]['data'] = Misc::entities_decode(end($attrib)); } } } } return $return; } /** * @deprecated since SimplePie 1.9.0. If you need it, you can copy the function to your codebase. But you should consider using `DOMDocument` for any DOM wrangling. * @param array{tag: string, self_closing: bool, attribs: array<string, array{data: string}>, content: string} $element * @return string */ public static function element_implode(array $element) { // trigger_error(sprintf('Using method "' . __METHOD__ . '" is deprecated since SimplePie 1.9.'), \E_USER_DEPRECATED); $full = "<{$element['tag']}"; foreach ($element['attribs'] as $key => $value) { $key = strtolower($key); $full .= " $key=\"" . htmlspecialchars($value['data'], ENT_COMPAT, 'UTF-8') . '"'; } if ($element['self_closing']) { $full .= ' />'; } else { $full .= ">{$element['content']}</{$element['tag']}>"; } return $full; } /** * @param string $message * @param int $level * @param string $file * @param int $line * @return string */ public static function error(string $message, int $level, string $file, int $line) { if ((error_reporting() & $level) > 0) { switch ($level) { case E_USER_ERROR: $note = 'PHP Error'; break; case E_USER_WARNING: $note = 'PHP Warning'; break; case E_USER_NOTICE: $note = 'PHP Notice'; break; default: $note = 'Unknown Error'; break; } $log_error = true; if (!function_exists('error_log')) { $log_error = false; } $log_file = @ini_get('error_log'); if (!empty($log_file) && ('syslog' !== $log_file) && !@is_writable($log_file)) { $log_error = false; } if ($log_error) { @error_log("$note: $message in $file on line $line", 0); } } return $message; } /** * @return string */ public static function fix_protocol(string $url, int $http = 1) { $url = Misc::normalize_url($url); $parsed = Misc::parse_url($url); if ($parsed['scheme'] !== '' && $parsed['scheme'] !== 'http' && $parsed['scheme'] !== 'https') { return Misc::fix_protocol(Misc::compress_parse_url('http', $parsed['authority'], $parsed['path'], $parsed['query'], $parsed['fragment']), $http); } if ($parsed['scheme'] === '' && $parsed['authority'] === '' && !file_exists($url)) { return Misc::fix_protocol(Misc::compress_parse_url('http', $parsed['path'], '', $parsed['query'], $parsed['fragment']), $http); } if ($http === 2 && $parsed['scheme'] !== '') { return "feed:$url"; } elseif ($http === 3 && strtolower($parsed['scheme']) === 'http') { return substr_replace($url, 'podcast', 0, 4); } elseif ($http === 4 && strtolower($parsed['scheme']) === 'http') { return substr_replace($url, 'itpc', 0, 4); } return $url; } /** * @deprecated since SimplePie 1.8.0, use PHP native array_replace_recursive() instead. * @param array<mixed> $array1 * @param array<mixed> $array2 * @return array<mixed> */ public static function array_merge_recursive(array $array1, array $array2) { foreach ($array2 as $key => $value) { if (is_array($value)) { $array1[$key] = Misc::array_merge_recursive($array1[$key], $value); } else { $array1[$key] = $value; } } return $array1; } /** * @return array<string, string> */ public static function parse_url(string $url) { $iri = new \SimplePie\IRI($url); return [ 'scheme' => (string) $iri->scheme, 'authority' => (string) $iri->authority, 'path' => (string) $iri->path, 'query' => (string) $iri->query, 'fragment' => (string) $iri->fragment ]; } /** * @return string */ public static function compress_parse_url(string $scheme = '', string $authority = '', string $path = '', string $query = '', ?string $fragment = '') { $iri = new \SimplePie\IRI(''); $iri->scheme = $scheme; $iri->authority = $authority; $iri->path = $path; $iri->query = $query; $iri->fragment = $fragment; return $iri->get_uri(); } /** * @return string */ public static function normalize_url(string $url) { $iri = new \SimplePie\IRI($url); return $iri->get_uri(); } /** * @deprecated since SimplePie 1.9.0. This functionality is part of `IRI` – if you need it standalone, consider copying the function to your codebase. * @param array<int, string> $match * @return string */ public static function percent_encoding_normalization(array $match) { $integer = hexdec($match[1]); if ($integer >= 0x41 && $integer <= 0x5A || $integer >= 0x61 && $integer <= 0x7A || $integer >= 0x30 && $integer <= 0x39 || $integer === 0x2D || $integer === 0x2E || $integer === 0x5F || $integer === 0x7E) { // Cast for PHPStan, the value would only be float when above PHP_INT_MAX, which would not go in this branch. return chr((int) $integer); } return strtoupper($match[0]); } /** * Converts a Windows-1252 encoded string to a UTF-8 encoded string * * @static * @param string $string Windows-1252 encoded string * @return string UTF-8 encoded string */ public static function windows_1252_to_utf8(string $string) { static $convert_table = ["\x80" => "\xE2\x82\xAC", "\x81" => "\xEF\xBF\xBD", "\x82" => "\xE2\x80\x9A", "\x83" => "\xC6\x92", "\x84" => "\xE2\x80\x9E", "\x85" => "\xE2\x80\xA6", "\x86" => "\xE2\x80\xA0", "\x87" => "\xE2\x80\xA1", "\x88" => "\xCB\x86", "\x89" => "\xE2\x80\xB0", "\x8A" => "\xC5\xA0", "\x8B" => "\xE2\x80\xB9", "\x8C" => "\xC5\x92", "\x8D" => "\xEF\xBF\xBD", "\x8E" => "\xC5\xBD", "\x8F" => "\xEF\xBF\xBD", "\x90" => "\xEF\xBF\xBD", "\x91" => "\xE2\x80\x98", "\x92" => "\xE2\x80\x99", "\x93" => "\xE2\x80\x9C", "\x94" => "\xE2\x80\x9D", "\x95" => "\xE2\x80\xA2", "\x96" => "\xE2\x80\x93", "\x97" => "\xE2\x80\x94", "\x98" => "\xCB\x9C", "\x99" => "\xE2\x84\xA2", "\x9A" => "\xC5\xA1", "\x9B" => "\xE2\x80\xBA", "\x9C" => "\xC5\x93", "\x9D" => "\xEF\xBF\xBD", "\x9E" => "\xC5\xBE", "\x9F" => "\xC5\xB8", "\xA0" => "\xC2\xA0", "\xA1" => "\xC2\xA1", "\xA2" => "\xC2\xA2", "\xA3" => "\xC2\xA3", "\xA4" => "\xC2\xA4", "\xA5" => "\xC2\xA5", "\xA6" => "\xC2\xA6", "\xA7" => "\xC2\xA7", "\xA8" => "\xC2\xA8", "\xA9" => "\xC2\xA9", "\xAA" => "\xC2\xAA", "\xAB" => "\xC2\xAB", "\xAC" => "\xC2\xAC", "\xAD" => "\xC2\xAD", "\xAE" => "\xC2\xAE", "\xAF" => "\xC2\xAF", "\xB0" => "\xC2\xB0", "\xB1" => "\xC2\xB1", "\xB2" => "\xC2\xB2", "\xB3" => "\xC2\xB3", "\xB4" => "\xC2\xB4", "\xB5" => "\xC2\xB5", "\xB6" => "\xC2\xB6", "\xB7" => "\xC2\xB7", "\xB8" => "\xC2\xB8", "\xB9" => "\xC2\xB9", "\xBA" => "\xC2\xBA", "\xBB" => "\xC2\xBB", "\xBC" => "\xC2\xBC", "\xBD" => "\xC2\xBD", "\xBE" => "\xC2\xBE", "\xBF" => "\xC2\xBF", "\xC0" => "\xC3\x80", "\xC1" => "\xC3\x81", "\xC2" => "\xC3\x82", "\xC3" => "\xC3\x83", "\xC4" => "\xC3\x84", "\xC5" => "\xC3\x85", "\xC6" => "\xC3\x86", "\xC7" => "\xC3\x87", "\xC8" => "\xC3\x88", "\xC9" => "\xC3\x89", "\xCA" => "\xC3\x8A", "\xCB" => "\xC3\x8B", "\xCC" => "\xC3\x8C", "\xCD" => "\xC3\x8D", "\xCE" => "\xC3\x8E", "\xCF" => "\xC3\x8F", "\xD0" => "\xC3\x90", "\xD1" => "\xC3\x91", "\xD2" => "\xC3\x92", "\xD3" => "\xC3\x93", "\xD4" => "\xC3\x94", "\xD5" => "\xC3\x95", "\xD6" => "\xC3\x96", "\xD7" => "\xC3\x97", "\xD8" => "\xC3\x98", "\xD9" => "\xC3\x99", "\xDA" => "\xC3\x9A", "\xDB" => "\xC3\x9B", "\xDC" => "\xC3\x9C", "\xDD" => "\xC3\x9D", "\xDE" => "\xC3\x9E", "\xDF" => "\xC3\x9F", "\xE0" => "\xC3\xA0", "\xE1" => "\xC3\xA1", "\xE2" => "\xC3\xA2", "\xE3" => "\xC3\xA3", "\xE4" => "\xC3\xA4", "\xE5" => "\xC3\xA5", "\xE6" => "\xC3\xA6", "\xE7" => "\xC3\xA7", "\xE8" => "\xC3\xA8", "\xE9" => "\xC3\xA9", "\xEA" => "\xC3\xAA", "\xEB" => "\xC3\xAB", "\xEC" => "\xC3\xAC", "\xED" => "\xC3\xAD", "\xEE" => "\xC3\xAE", "\xEF" => "\xC3\xAF", "\xF0" => "\xC3\xB0", "\xF1" => "\xC3\xB1", "\xF2" => "\xC3\xB2", "\xF3" => "\xC3\xB3", "\xF4" => "\xC3\xB4", "\xF5" => "\xC3\xB5", "\xF6" => "\xC3\xB6", "\xF7" => "\xC3\xB7", "\xF8" => "\xC3\xB8", "\xF9" => "\xC3\xB9", "\xFA" => "\xC3\xBA", "\xFB" => "\xC3\xBB", "\xFC" => "\xC3\xBC", "\xFD" => "\xC3\xBD", "\xFE" => "\xC3\xBE", "\xFF" => "\xC3\xBF"]; return strtr($string, $convert_table); } /** * Change a string from one encoding to another * * @param string $data Raw data in $input encoding * @param string $input Encoding of $data * @param string $output Encoding you want * @return string|false False if we can't convert it */ public static function change_encoding(string $data, string $input, string $output) { $input = Misc::encoding($input); $output = Misc::encoding($output); // We fail to fail on non US-ASCII bytes if ($input === 'US-ASCII') { static $non_ascii_octets = ''; if (!$non_ascii_octets) { for ($i = 0x80; $i <= 0xFF; $i++) { $non_ascii_octets .= chr($i); } } $data = substr($data, 0, strcspn($data, $non_ascii_octets)); } // This is first, as behaviour of this is completely predictable if ($input === 'windows-1252' && $output === 'UTF-8') { return Misc::windows_1252_to_utf8($data); } // This is second, as behaviour of this varies only with PHP version (the middle part of this expression checks the encoding is supported). elseif (function_exists('mb_convert_encoding') && ($return = Misc::change_encoding_mbstring($data, $input, $output))) { return $return; } // This is third, as behaviour of this varies with OS userland and PHP version elseif (function_exists('iconv') && ($return = Misc::change_encoding_iconv($data, $input, $output))) { return $return; } // This is last, as behaviour of this varies with OS userland and PHP version elseif (class_exists('\UConverter') && ($return = Misc::change_encoding_uconverter($data, $input, $output))) { return $return; } // If we can't do anything, just fail return false; } /** * @return string|false */ protected static function change_encoding_mbstring(string $data, string $input, string $output) { if ($input === 'windows-949') { $input = 'EUC-KR'; } if ($output === 'windows-949') { $output = 'EUC-KR'; } if ($input === 'Windows-31J') { $input = 'SJIS'; } if ($output === 'Windows-31J') { $output = 'SJIS'; } // Check that the encoding is supported if (!in_array($input, mb_list_encodings())) { return false; } if (@mb_convert_encoding("\x80", 'UTF-16BE', $input) === "\x00\x80") { return false; } // Let's do some conversion if ($return = @mb_convert_encoding($data, $output, $input)) { return $return; } return false; } /** * @return string|false */ protected static function change_encoding_iconv(string $data, string $input, string $output) { return @iconv($input, $output, $data); } /** * @return string|false */ protected static function change_encoding_uconverter(string $data, string $input, string $output) { return @\UConverter::transcode($data, $output, $input); } /** * Normalize an encoding name * * This is automatically generated by create.php * * To generate it, run `php create.php` on the command line, and copy the * output to replace this function. * * @param string $charset Character set to standardise * @return string Standardised name */ public static function encoding(string $charset) { // Normalization from UTS #22 // Cast for PHPStan, the regex should not fail. switch (strtolower((string) preg_replace('/(?:[^a-zA-Z0-9]+|([^0-9])0+)/', '\1', $charset))) { case 'adobestandardencoding': case 'csadobestandardencoding': return 'Adobe-Standard-Encoding'; case 'adobesymbolencoding': case 'cshppsmath': return 'Adobe-Symbol-Encoding'; case 'ami1251': case 'amiga1251': return 'Amiga-1251'; case 'ansix31101983': case 'csat5001983': case 'csiso99naplps': case 'isoir99': case 'naplps': return 'ANSI_X3.110-1983'; case 'arabic7': case 'asmo449': case 'csiso89asmo449': case 'iso9036': case 'isoir89': return 'ASMO_449'; case 'big5': case 'csbig5': return 'Big5'; case 'big5hkscs': return 'Big5-HKSCS'; case 'bocu1': case 'csbocu1': return 'BOCU-1'; case 'brf': case 'csbrf': return 'BRF'; case 'bs4730': case 'csiso4unitedkingdom': case 'gb': case 'iso646gb': case 'isoir4': case 'uk': return 'BS_4730'; case 'bsviewdata': case 'csiso47bsviewdata': case 'isoir47': return 'BS_viewdata'; case 'cesu8': case 'cscesu8': return 'CESU-8'; case 'ca': case 'csa71': case 'csaz243419851': case 'csiso121canadian1': case 'iso646ca': case 'isoir121': return 'CSA_Z243.4-1985-1'; case 'csa72': case 'csaz243419852': case 'csiso122canadian2': case 'iso646ca2': case 'isoir122': return 'CSA_Z243.4-1985-2'; case 'csaz24341985gr': case 'csiso123csaz24341985gr': case 'isoir123': return 'CSA_Z243.4-1985-gr'; case 'csiso139csn369103': case 'csn369103': case 'isoir139': return 'CSN_369103'; case 'csdecmcs': case 'dec': case 'decmcs': return 'DEC-MCS'; case 'csiso21german': case 'de': case 'din66003': case 'iso646de': case 'isoir21': return 'DIN_66003'; case 'csdkus': case 'dkus': return 'dk-us'; case 'csiso646danish': case 'dk': case 'ds2089': case 'iso646dk': return 'DS_2089'; case 'csibmebcdicatde': case 'ebcdicatde': return 'EBCDIC-AT-DE'; case 'csebcdicatdea': case 'ebcdicatdea': return 'EBCDIC-AT-DE-A'; case 'csebcdiccafr': case 'ebcdiccafr': return 'EBCDIC-CA-FR'; case 'csebcdicdkno': case 'ebcdicdkno': return 'EBCDIC-DK-NO'; case 'csebcdicdknoa': case 'ebcdicdknoa': return 'EBCDIC-DK-NO-A'; case 'csebcdices': case 'ebcdices': return 'EBCDIC-ES'; case 'csebcdicesa': case 'ebcdicesa': return 'EBCDIC-ES-A'; case 'csebcdicess': case 'ebcdicess': return 'EBCDIC-ES-S'; case 'csebcdicfise': case 'ebcdicfise': return 'EBCDIC-FI-SE'; case 'csebcdicfisea': case 'ebcdicfisea': return 'EBCDIC-FI-SE-A'; case 'csebcdicfr': case 'ebcdicfr': return 'EBCDIC-FR'; case 'csebcdicit': case 'ebcdicit': return 'EBCDIC-IT'; case 'csebcdicpt': case 'ebcdicpt': return 'EBCDIC-PT'; case 'csebcdicuk': case 'ebcdicuk': return 'EBCDIC-UK'; case 'csebcdicus': case 'ebcdicus': return 'EBCDIC-US'; case 'csiso111ecmacyrillic': case 'ecmacyrillic': case 'isoir111': case 'koi8e': return 'ECMA-cyrillic'; case 'csiso17spanish': case 'es': case 'iso646es': case 'isoir17': return 'ES'; case 'csiso85spanish2': case 'es2': case 'iso646es2': case 'isoir85': return 'ES2'; case 'cseucpkdfmtjapanese': case 'eucjp': case 'extendedunixcodepackedformatforjapanese': return 'EUC-JP'; case 'cseucfixwidjapanese': case 'extendedunixcodefixedwidthforjapanese': return 'Extended_UNIX_Code_Fixed_Width_for_Japanese'; case 'gb18030': return 'GB18030'; case 'chinese': case 'cp936': case 'csgb2312': case 'csiso58gb231280': case 'gb2312': case 'gb231280': case 'gbk': case 'isoir58': case 'ms936': case 'windows936': return 'GBK'; case 'cn': case 'csiso57gb1988': case 'gb198880': case 'iso646cn': case 'isoir57': return 'GB_1988-80'; case 'csiso153gost1976874': case 'gost1976874': case 'isoir153': case 'stsev35888': return 'GOST_19768-74'; case 'csiso150': case 'csiso150greekccitt': case 'greekccitt': case 'isoir150': return 'greek-ccitt'; case 'csiso88greek7': case 'greek7': case 'isoir88': return 'greek7'; case 'csiso18greek7old': case 'greek7old': case 'isoir18': return 'greek7-old'; case 'cshpdesktop': case 'hpdesktop': return 'HP-DeskTop'; case 'cshplegal': case 'hplegal': return 'HP-Legal'; case 'cshpmath8': case 'hpmath8': return 'HP-Math8'; case 'cshppifont': case 'hppifont': return 'HP-Pi-font'; case 'cshproman8': case 'hproman8': case 'r8': case 'roman8': return 'hp-roman8'; case 'hzgb2312': return 'HZ-GB-2312'; case 'csibmsymbols': case 'ibmsymbols': return 'IBM-Symbols'; case 'csibmthai': case 'ibmthai': return 'IBM-Thai'; case 'cp37': case 'csibm37': case 'ebcdiccpca': case 'ebcdiccpnl': case 'ebcdiccpus': case 'ebcdiccpwt': case 'ibm37': return 'IBM037'; case 'cp38': case 'csibm38': case 'ebcdicint': case 'ibm38': return 'IBM038'; case 'cp273': case 'csibm273': case 'ibm273': return 'IBM273'; case 'cp274': case 'csibm274': case 'ebcdicbe': case 'ibm274': return 'IBM274'; case 'cp275': case 'csibm275': case 'ebcdicbr': case 'ibm275': return 'IBM275'; case 'csibm277': case 'ebcdiccpdk': case 'ebcdiccpno': case 'ibm277': return 'IBM277'; case 'cp278': case 'csibm278': case 'ebcdiccpfi': case 'ebcdiccpse': case 'ibm278': return 'IBM278'; case 'cp280': case 'csibm280': case 'ebcdiccpit': case 'ibm280': return 'IBM280'; case 'cp281': case 'csibm281': case 'ebcdicjpe': case 'ibm281': return 'IBM281'; case 'cp284': case 'csibm284': case 'ebcdiccpes': case 'ibm284': return 'IBM284'; case 'cp285': case 'csibm285': case 'ebcdiccpgb': case 'ibm285': return 'IBM285'; case 'cp290': case 'csibm290': case 'ebcdicjpkana': case 'ibm290': return 'IBM290'; case 'cp297': case 'csibm297': case 'ebcdiccpfr': case 'ibm297': return 'IBM297'; case 'cp420': case 'csibm420': case 'ebcdiccpar1': case 'ibm420': return 'IBM420'; case 'cp423': case 'csibm423': case 'ebcdiccpgr': case 'ibm423': return 'IBM423'; case 'cp424': case 'csibm424': case 'ebcdiccphe': case 'ibm424': return 'IBM424'; case '437': case 'cp437': case 'cspc8codepage437': case 'ibm437': return 'IBM437'; case 'cp500': case 'csibm500': case 'ebcdiccpbe': case 'ebcdiccpch': case 'ibm500': return 'IBM500'; case 'cp775': case 'cspc775baltic': case 'ibm775': return 'IBM775'; case '850': case 'cp850': case 'cspc850multilingual': case 'ibm850': return 'IBM850'; case '851': case 'cp851': case 'csibm851': case 'ibm851': return 'IBM851'; case '852': case 'cp852': case 'cspcp852': case 'ibm852': return 'IBM852'; case '855': case 'cp855': case 'csibm855': case 'ibm855': return 'IBM855'; case '857': case 'cp857': case 'csibm857': case 'ibm857': return 'IBM857'; case 'ccsid858': case 'cp858': case 'ibm858': case 'pcmultilingual850euro': return 'IBM00858'; case '860': case 'cp860': case 'csibm860': case 'ibm860': return 'IBM860'; case '861': case 'cp861': case 'cpis': case 'csibm861': case 'ibm861': return 'IBM861'; case '862': case 'cp862': case 'cspc862latinhebrew': case 'ibm862': return 'IBM862'; case '863': case 'cp863': case 'csibm863': case 'ibm863': return 'IBM863'; case 'cp864': case 'csibm864': case 'ibm864': return 'IBM864'; case '865': case 'cp865': case 'csibm865': case 'ibm865': return 'IBM865'; case '866': case 'cp866': case 'csibm866': case 'ibm866': return 'IBM866'; case 'cp868': case 'cpar': case 'csibm868': case 'ibm868': return 'IBM868'; case '869': case 'cp869': case 'cpgr': case 'csibm869': case 'ibm869': return 'IBM869'; case 'cp870': case 'csibm870': case 'ebcdiccproece': case 'ebcdiccpyu': case 'ibm870': return 'IBM870'; case 'cp871': case 'csibm871': case 'ebcdiccpis': case 'ibm871': return 'IBM871'; case 'cp880': case 'csibm880': case 'ebcdiccyrillic': case 'ibm880': return 'IBM880'; case 'cp891': case 'csibm891': case 'ibm891': return 'IBM891'; case 'cp903': case 'csibm903': case 'ibm903': return 'IBM903'; case '904': case 'cp904': case 'csibbm904': case 'ibm904': return 'IBM904'; case 'cp905': case 'csibm905': case 'ebcdiccptr': case 'ibm905': return 'IBM905'; case 'cp918': case 'csibm918': case 'ebcdiccpar2': case 'ibm918': return 'IBM918'; case 'ccsid924': case 'cp924': case 'ebcdiclatin9euro': case 'ibm924': return 'IBM00924'; case 'cp1026': case 'csibm1026': case 'ibm1026': return 'IBM1026'; case 'ibm1047': return 'IBM1047'; case 'ccsid1140': case 'cp1140': case 'ebcdicus37euro': case 'ibm1140': return 'IBM01140'; case 'ccsid1141': case 'cp1141': case 'ebcdicde273euro': case 'ibm1141': return 'IBM01141'; case 'ccsid1142': case 'cp1142': case 'ebcdicdk277euro': case 'ebcdicno277euro': case 'ibm1142': return 'IBM01142'; case 'ccsid1143': case 'cp1143': case 'ebcdicfi278euro': case 'ebcdicse278euro': case 'ibm1143': return 'IBM01143'; case 'ccsid1144': case 'cp1144': case 'ebcdicit280euro': case 'ibm1144': return 'IBM01144'; case 'ccsid1145': case 'cp1145': case 'ebcdices284euro': case 'ibm1145': return 'IBM01145'; case 'ccsid1146': case 'cp1146': case 'ebcdicgb285euro': case 'ibm1146': return 'IBM01146'; case 'ccsid1147': case 'cp1147': case 'ebcdicfr297euro': case 'ibm1147': return 'IBM01147'; case 'ccsid1148': case 'cp1148': case 'ebcdicinternational500euro': case 'ibm1148': return 'IBM01148'; case 'ccsid1149': case 'cp1149': case 'ebcdicis871euro': case 'ibm1149': return 'IBM01149'; case 'csiso143iecp271': case 'iecp271': case 'isoir143': return 'IEC_P27-1'; case 'csiso49inis': case 'inis': case 'isoir49': return 'INIS'; case 'csiso50inis8': case 'inis8': case 'isoir50': return 'INIS-8'; case 'csiso51iniscyrillic': case 'iniscyrillic': case 'isoir51': return 'INIS-cyrillic'; case 'csinvariant': case 'invariant': return 'INVARIANT'; case 'iso2022cn': return 'ISO-2022-CN'; case 'iso2022cnext': return 'ISO-2022-CN-EXT'; case 'csiso2022jp': case 'iso2022jp': return 'ISO-2022-JP'; case 'csiso2022jp2': case 'iso2022jp2': return 'ISO-2022-JP-2'; case 'csiso2022kr': case 'iso2022kr': return 'ISO-2022-KR'; case 'cswindows30latin1': case 'iso88591windows30latin1': return 'ISO-8859-1-Windows-3.0-Latin-1'; case 'cswindows31latin1': case 'iso88591windows31latin1': return 'ISO-8859-1-Windows-3.1-Latin-1'; case 'csisolatin2': case 'iso88592': case 'iso885921987': case 'isoir101': case 'l2': case 'latin2': return 'ISO-8859-2'; case 'cswindows31latin2': case 'iso88592windowslatin2': return 'ISO-8859-2-Windows-Latin-2'; case 'csisolatin3': case 'iso88593': case 'iso885931988': case 'isoir109': case 'l3': case 'latin3': return 'ISO-8859-3'; case 'csisolatin4': case 'iso88594': case 'iso885941988': case 'isoir110': case 'l4': case 'latin4': return 'ISO-8859-4'; case 'csisolatincyrillic': case 'cyrillic': case 'iso88595': case 'iso885951988': case 'isoir144': return 'ISO-8859-5'; case 'arabic': case 'asmo708': case 'csisolatinarabic': case 'ecma114': case 'iso88596': case 'iso885961987': case 'isoir127': return 'ISO-8859-6'; case 'csiso88596e': case 'iso88596e': return 'ISO-8859-6-E'; case 'csiso88596i': case 'iso88596i': return 'ISO-8859-6-I'; case 'csisolatingreek': case 'ecma118': case 'elot928': case 'greek': case 'greek8': case 'iso88597': case 'iso885971987': case 'isoir126': return 'ISO-8859-7'; case 'csisolatinhebrew': case 'hebrew': case 'iso88598': case 'iso885981988': case 'isoir138': return 'ISO-8859-8'; case 'csiso88598e': case 'iso88598e': return 'ISO-8859-8-E'; case 'csiso88598i': case 'iso88598i': return 'ISO-8859-8-I'; case 'cswindows31latin5': case 'iso88599windowslatin5': return 'ISO-8859-9-Windows-Latin-5'; case 'csisolatin6': case 'iso885910': case 'iso8859101992': case 'isoir157': case 'l6': case 'latin6': return 'ISO-8859-10'; case 'iso885913': return 'ISO-8859-13'; case 'iso885914': case 'iso8859141998': case 'isoceltic': case 'isoir199': case 'l8': case 'latin8': return 'ISO-8859-14'; case 'iso885915': case 'latin9': return 'ISO-8859-15'; case 'iso885916': case 'iso8859162001': case 'isoir226': case 'l10': case 'latin10': return 'ISO-8859-16'; case 'iso10646j1': return 'ISO-10646-J-1'; case 'csunicode': case 'iso10646ucs2': return 'ISO-10646-UCS-2'; case 'csucs4': case 'iso10646ucs4': return 'ISO-10646-UCS-4'; case 'csunicodeascii': case 'iso10646ucsbasic': return 'ISO-10646-UCS-Basic'; case 'csunicodelatin1': case 'iso10646': case 'iso10646unicodelatin1': return 'ISO-10646-Unicode-Latin1'; case 'csiso10646utf1': case 'iso10646utf1': return 'ISO-10646-UTF-1'; case 'csiso115481': case 'iso115481': case 'isotr115481': return 'ISO-11548-1'; case 'csiso90': case 'isoir90': return 'iso-ir-90'; case 'csunicodeibm1261': case 'isounicodeibm1261': return 'ISO-Unicode-IBM-1261'; case 'csunicodeibm1264': case 'isounicodeibm1264': return 'ISO-Unicode-IBM-1264'; case 'csunicodeibm1265': case 'isounicodeibm1265': return 'ISO-Unicode-IBM-1265'; case 'csunicodeibm1268': case 'isounicodeibm1268': return 'ISO-Unicode-IBM-1268'; case 'csunicodeibm1276': case 'isounicodeibm1276': return 'ISO-Unicode-IBM-1276'; case 'csiso646basic1983': case 'iso646basic1983': case 'ref': return 'ISO_646.basic:1983'; case 'csiso2intlrefversion': case 'irv': case 'iso646irv1983': case 'isoir2': return 'ISO_646.irv:1983'; case 'csiso2033': case 'e13b': case 'iso20331983': case 'isoir98': return 'ISO_2033-1983'; case 'csiso5427cyrillic': case 'iso5427': case 'isoir37': return 'ISO_5427'; case 'iso5427cyrillic1981': case 'iso54271981': case 'isoir54': return 'ISO_5427:1981'; case 'csiso5428greek': case 'iso54281980': case 'isoir55': return 'ISO_5428:1980'; case 'csiso6937add': case 'iso6937225': case 'isoir152': return 'ISO_6937-2-25'; case 'csisotextcomm': case 'iso69372add': case 'isoir142': return 'ISO_6937-2-add'; case 'csiso8859supp': case 'iso8859supp': case 'isoir154': case 'latin125': return 'ISO_8859-supp'; case 'csiso10367box': case 'iso10367box': case 'isoir155': return 'ISO_10367-box'; case 'csiso15italian': case 'iso646it': case 'isoir15': case 'it': return 'IT'; case 'csiso13jisc6220jp': case 'isoir13': case 'jisc62201969': case 'jisc62201969jp': case 'katakana': case 'x2017': return 'JIS_C6220-1969-jp'; case 'csiso14jisc6220ro': case 'iso646jp': case 'isoir14': case 'jisc62201969ro': case 'jp': return 'JIS_C6220-1969-ro'; case 'csiso42jisc62261978': case 'isoir42': case 'jisc62261978': return 'JIS_C6226-1978'; case 'csiso87jisx208': case 'isoir87': case 'jisc62261983': case 'jisx2081983': case 'x208': return 'JIS_C6226-1983'; case 'csiso91jisc62291984a': case 'isoir91': case 'jisc62291984a': case 'jpocra': return 'JIS_C6229-1984-a'; case 'csiso92jisc62991984b': case 'iso646jpocrb': case 'isoir92': case 'jisc62291984b': case 'jpocrb': return 'JIS_C6229-1984-b'; case 'csiso93jis62291984badd': case 'isoir93': case 'jisc62291984badd': case 'jpocrbadd': return 'JIS_C6229-1984-b-add'; case 'csiso94jis62291984hand': case 'isoir94': case 'jisc62291984hand': case 'jpocrhand': return 'JIS_C6229-1984-hand'; case 'csiso95jis62291984handadd': case 'isoir95': case 'jisc62291984handadd': case 'jpocrhandadd': return 'JIS_C6229-1984-hand-add'; case 'csiso96jisc62291984kana': case 'isoir96': case 'jisc62291984kana': return 'JIS_C6229-1984-kana'; case 'csjisencoding': case 'jisencoding': return 'JIS_Encoding'; case 'cshalfwidthkatakana': case 'jisx201': case 'x201': return 'JIS_X0201'; case 'csiso159jisx2121990': case 'isoir159': case 'jisx2121990': case 'x212': return 'JIS_X0212-1990'; case 'csiso141jusib1002': case 'iso646yu': case 'isoir141': case 'js': case 'jusib1002': case 'yu': return 'JUS_I.B1.002'; case 'csiso147macedonian': case 'isoir147': case 'jusib1003mac': case 'macedonian': return 'JUS_I.B1.003-mac'; case 'csiso146serbian': case 'isoir146': case 'jusib1003serb': case 'serbian': return 'JUS_I.B1.003-serb'; case 'koi7switched': return 'KOI7-switched'; case 'cskoi8r': case 'koi8r': return 'KOI8-R'; case 'koi8u': return 'KOI8-U'; case 'csksc5636': case 'iso646kr': case 'ksc5636': return 'KSC5636'; case 'cskz1048': case 'kz1048': case 'rk1048': case 'strk10482002': return 'KZ-1048'; case 'csiso19latingreek': case 'isoir19': case 'latingreek': return 'latin-greek'; case 'csiso27latingreek1': case 'isoir27': case 'latingreek1': return 'Latin-greek-1'; case 'csiso158lap': case 'isoir158': case 'lap': case 'latinlap': return 'latin-lap'; case 'csmacintosh': case 'mac': case 'macintosh': return 'macintosh'; case 'csmicrosoftpublishing': case 'microsoftpublishing': return 'Microsoft-Publishing'; case 'csmnem': case 'mnem': return 'MNEM'; case 'csmnemonic': case 'mnemonic': return 'MNEMONIC'; case 'csiso86hungarian': case 'hu': case 'iso646hu': case 'isoir86': case 'msz77953': return 'MSZ_7795.3'; case 'csnatsdano': case 'isoir91': case 'natsdano': return 'NATS-DANO'; case 'csnatsdanoadd': case 'isoir92': case 'natsdanoadd': return 'NATS-DANO-ADD'; case 'csnatssefi': case 'isoir81': case 'natssefi': return 'NATS-SEFI'; case 'csnatssefiadd': case 'isoir82': case 'natssefiadd': return 'NATS-SEFI-ADD'; case 'csiso151cuba': case 'cuba': case 'iso646cu': case 'isoir151': case 'ncnc1081': return 'NC_NC00-10:81'; case 'csiso69french': case 'fr': case 'iso646fr': case 'isoir69': case 'nfz62010': return 'NF_Z_62-010'; case 'csiso25french': case 'iso646fr1': case 'isoir25': case 'nfz620101973': return 'NF_Z_62-010_(1973)'; case 'csiso60danishnorwegian': case 'csiso60norwegian1': case 'iso646no': case 'isoir60': case 'no': case 'ns45511': return 'NS_4551-1'; case 'csiso61norwegian2': case 'iso646no2': case 'isoir61': case 'no2': case 'ns45512': return 'NS_4551-2'; case 'osdebcdicdf3irv': return 'OSD_EBCDIC_DF03_IRV'; case 'osdebcdicdf41': return 'OSD_EBCDIC_DF04_1'; case 'osdebcdicdf415': return 'OSD_EBCDIC_DF04_15'; case 'cspc8danishnorwegian': case 'pc8danishnorwegian': return 'PC8-Danish-Norwegian'; case 'cspc8turkish': case 'pc8turkish': return 'PC8-Turkish'; case 'csiso16portuguese': case 'iso646pt': case 'isoir16': case 'pt': return 'PT'; case 'csiso84portuguese2': case 'iso646pt2': case 'isoir84': case 'pt2': return 'PT2'; case 'cp154': case 'csptcp154': case 'cyrillicasian': case 'pt154': case 'ptcp154': return 'PTCP154'; case 'scsu': return 'SCSU'; case 'csiso10swedish': case 'fi': case 'iso646fi': case 'iso646se': case 'isoir10': case 'se': case 'sen850200b': return 'SEN_850200_B'; case 'csiso11swedishfornames': case 'iso646se2': case 'isoir11': case 'se2': case 'sen850200c': return 'SEN_850200_C'; case 'csiso102t617bit': case 'isoir102': case 't617bit': return 'T.61-7bit'; case 'csiso103t618bit': case 'isoir103': case 't61': case 't618bit': return 'T.61-8bit'; case 'csiso128t101g2': case 'isoir128': case 't101g2': return 'T.101-G2'; case 'cstscii': case 'tscii': return 'TSCII'; case 'csunicode11': case 'unicode11': return 'UNICODE-1-1'; case 'csunicode11utf7': case 'unicode11utf7': return 'UNICODE-1-1-UTF-7'; case 'csunknown8bit': case 'unknown8bit': return 'UNKNOWN-8BIT'; case 'ansix341968': case 'ansix341986': case 'ascii': case 'cp367': case 'csascii': case 'ibm367': case 'iso646irv1991': case 'iso646us': case 'isoir6': case 'us': case 'usascii': return 'US-ASCII'; case 'csusdk': case 'usdk': return 'us-dk'; case 'utf7': return 'UTF-7'; case 'utf8': return 'UTF-8'; case 'utf16': return 'UTF-16'; case 'utf16be': return 'UTF-16BE'; case 'utf16le': return 'UTF-16LE'; case 'utf32': return 'UTF-32'; case 'utf32be': return 'UTF-32BE'; case 'utf32le': return 'UTF-32LE'; case 'csventurainternational': case 'venturainternational': return 'Ventura-International'; case 'csventuramath': case 'venturamath': return 'Ventura-Math'; case 'csventuraus': case 'venturaus': return 'Ventura-US'; case 'csiso70videotexsupp1': case 'isoir70': case 'videotexsuppl': return 'videotex-suppl'; case 'csviqr': case 'viqr': return 'VIQR'; case 'csviscii': case 'viscii': return 'VISCII'; case 'csshiftjis': case 'cswindows31j': case 'mskanji': case 'shiftjis': case 'windows31j': return 'Windows-31J'; case 'iso885911': case 'tis620': return 'windows-874'; case 'cseuckr': case 'csksc56011987': case 'euckr': case 'isoir149': case 'korean': case 'ksc5601': case 'ksc56011987': case 'ksc56011989': case 'windows949': return 'windows-949'; case 'windows1250': return 'windows-1250'; case 'windows1251': return 'windows-1251'; case 'cp819': case 'csisolatin1': case 'ibm819': case 'iso88591': case 'iso885911987': case 'isoir100': case 'l1': case 'latin1': case 'windows1252': return 'windows-1252'; case 'windows1253': return 'windows-1253'; case 'csisolatin5': case 'iso88599': case 'iso885991989': case 'isoir148': case 'l5': case 'latin5': case 'windows1254': return 'windows-1254'; case 'windows1255': return 'windows-1255'; case 'windows1256': return 'windows-1256'; case 'windows1257': return 'windows-1257'; case 'windows1258': return 'windows-1258'; default: return $charset; } } /** * @return string */ public static function get_curl_version() { if (is_array($curl = curl_version())) { $curl = $curl['version']; } else { $curl = '0'; } return $curl; } /** * Strip HTML comments * * @deprecated since SimplePie 1.9.0. If you need it, you can copy the function to your codebase. But you should consider using `DOMDocument` for any DOM wrangling. * @param string $data Data to strip comments from * @return string Comment stripped string */ public static function strip_comments(string $data) { // trigger_error(sprintf('Using method "' . __METHOD__ . '" is deprecated since SimplePie 1.9.'), \E_USER_DEPRECATED); $output = ''; while (($start = strpos($data, '<!--')) !== false) { $output .= substr($data, 0, $start); if (($end = strpos($data, '-->', $start)) !== false) { $data = substr_replace($data, '', 0, $end + 3); } else { $data = ''; } } return $output . $data; } /** * @return int|false */ public static function parse_date(string $dt) { $parser = \SimplePie\Parse\Date::get(); return $parser->parse($dt); } /** * Decode HTML entities * * @deprecated since SimplePie 1.3, use DOMDocument instead * @param string $data Input data * @return string Output data */ public static function entities_decode(string $data) { // trigger_error(sprintf('Using method "' . __METHOD__ . '" is deprecated since SimplePie 1.3, use "DOMDocument" instead.'), \E_USER_DEPRECATED); $decoder = new \SimplePie_Decode_HTML_Entities($data); return $decoder->parse(); } /** * Remove RFC822 comments * * @deprecated since SimplePie 1.9.0. If you need it, consider copying the function to your codebase. * @param string $string Data to strip comments from * @return string Comment stripped string */ public static function uncomment_rfc822(string $string) { // trigger_error(sprintf('Using method "' . __METHOD__ . '" is deprecated since SimplePie 1.9.'), \E_USER_DEPRECATED); $position = 0; $length = strlen($string); $depth = 0; $output = ''; while ($position < $length && ($pos = strpos($string, '(', $position)) !== false) { $output .= substr($string, $position, $pos - $position); $position = $pos + 1; if ($string[$pos - 1] !== '\\') { $depth++; while ($depth && $position < $length) { $position += strcspn($string, '()', $position); if ($string[$position - 1] === '\\') { $position++; continue; } elseif (isset($string[$position])) { switch ($string[$position]) { case '(': $depth++; break; case ')': $depth--; break; } $position++; } else { break; } } } else { $output .= '('; } } $output .= substr($string, $position); return $output; } /** * @return string */ public static function parse_mime(string $mime) { if (($pos = strpos($mime, ';')) === false) { return trim($mime); } return trim(substr($mime, 0, $pos)); } /** * @param array<string, array<string, string>> $attribs * @return int-mask-of<SimplePie::CONSTRUCT_*> */ public static function atom_03_construct_type(array $attribs) { if (isset($attribs['']['mode']) && strtolower(trim($attribs['']['mode'])) === 'base64') { $mode = \SimplePie\SimplePie::CONSTRUCT_BASE64; } else { $mode = \SimplePie\SimplePie::CONSTRUCT_NONE; } if (isset($attribs['']['type'])) { switch (strtolower(trim($attribs['']['type']))) { case 'text': case 'text/plain': return \SimplePie\SimplePie::CONSTRUCT_TEXT | $mode; case 'html': case 'text/html': return \SimplePie\SimplePie::CONSTRUCT_HTML | $mode; case 'xhtml': case 'application/xhtml+xml': return \SimplePie\SimplePie::CONSTRUCT_XHTML | $mode; default: return \SimplePie\SimplePie::CONSTRUCT_NONE | $mode; } } return \SimplePie\SimplePie::CONSTRUCT_TEXT | $mode; } /** * @param array<string, array<string, string>> $attribs * @return int-mask-of<SimplePie::CONSTRUCT_*> */ public static function atom_10_construct_type(array $attribs) { if (isset($attribs['']['type'])) { switch (strtolower(trim($attribs['']['type']))) { case 'text': return \SimplePie\SimplePie::CONSTRUCT_TEXT; case 'html': return \SimplePie\SimplePie::CONSTRUCT_HTML; case 'xhtml': return \SimplePie\SimplePie::CONSTRUCT_XHTML; default: return \SimplePie\SimplePie::CONSTRUCT_NONE; } } return \SimplePie\SimplePie::CONSTRUCT_TEXT; } /** * @param array<string, array<string, string>> $attribs * @return int-mask-of<SimplePie::CONSTRUCT_*> */ public static function atom_10_content_construct_type(array $attribs) { if (isset($attribs['']['type'])) { $type = strtolower(trim($attribs['']['type'])); switch ($type) { case 'text': return \SimplePie\SimplePie::CONSTRUCT_TEXT; case 'html': return \SimplePie\SimplePie::CONSTRUCT_HTML; case 'xhtml': return \SimplePie\SimplePie::CONSTRUCT_XHTML; } if (in_array(substr($type, -4), ['+xml', '/xml']) || substr($type, 0, 5) === 'text/') { return \SimplePie\SimplePie::CONSTRUCT_NONE; } else { return \SimplePie\SimplePie::CONSTRUCT_BASE64; } } return \SimplePie\SimplePie::CONSTRUCT_TEXT; } /** * @return bool */ public static function is_isegment_nz_nc(string $string) { return (bool) preg_match('/^([A-Za-z0-9\-._~\x{A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}\x{10000}-\x{1FFFD}\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}\x{40000}-\x{4FFFD}\x{50000}-\x{5FFFD}\x{60000}-\x{6FFFD}\x{70000}-\x{7FFFD}\x{80000}-\x{8FFFD}\x{90000}-\x{9FFFD}\x{A0000}-\x{AFFFD}\x{B0000}-\x{BFFFD}\x{C0000}-\x{CFFFD}\x{D0000}-\x{DFFFD}\x{E1000}-\x{EFFFD}!$&\'()*+,;=@]|(%[0-9ABCDEF]{2}))+$/u', $string); } /** * @return string[] */ public static function space_separated_tokens(string $string) { $space_characters = "\x20\x09\x0A\x0B\x0C\x0D"; $string_length = strlen($string); $position = strspn($string, $space_characters); $tokens = []; while ($position < $string_length) { $len = strcspn($string, $space_characters, $position); $tokens[] = substr($string, $position, $len); $position += $len; $position += strspn($string, $space_characters, $position); } return $tokens; } /** * Converts a unicode codepoint to a UTF-8 character * * @static * @param int $codepoint Unicode codepoint * @return string|false UTF-8 character */ public static function codepoint_to_utf8(int $codepoint) { if ($codepoint < 0) { return false; } elseif ($codepoint <= 0x7f) { return chr($codepoint); } elseif ($codepoint <= 0x7ff) { return chr(0xc0 | ($codepoint >> 6)) . chr(0x80 | ($codepoint & 0x3f)); } elseif ($codepoint <= 0xffff) { return chr(0xe0 | ($codepoint >> 12)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f)); } elseif ($codepoint <= 0x10ffff) { return chr(0xf0 | ($codepoint >> 18)) . chr(0x80 | (($codepoint >> 12) & 0x3f)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f)); } // U+FFFD REPLACEMENT CHARACTER return "\xEF\xBF\xBD"; } /** * Similar to parse_str() * * Returns an associative array of name/value pairs, where the value is an * array of values that have used the same name * * @deprecated since SimplePie 1.9.0. If you need it, consider copying the function to your codebase. * @static * @param string $str The input string. * @return array<string, array<string|null>> */ public static function parse_str(string $str) { // trigger_error(sprintf('Using method "' . __METHOD__ . '" is deprecated since SimplePie 1.9.'), \E_USER_DEPRECATED); $return = []; $str = explode('&', $str); foreach ($str as $section) { if (strpos($section, '=') !== false) { [$name, $value] = explode('=', $section, 2); $return[urldecode($name)][] = urldecode($value); } else { $return[urldecode($section)][] = null; } } return $return; } /** * Detect XML encoding, as per XML 1.0 Appendix F.1 * * @todo Add support for EBCDIC * @param string $data XML data * @param \SimplePie\Registry $registry Class registry * @return array<string> Possible encodings */ public static function xml_encoding(string $data, \SimplePie\Registry $registry) { // UTF-32 Big Endian BOM if (substr($data, 0, 4) === "\x00\x00\xFE\xFF") { $encoding[] = 'UTF-32BE'; } // UTF-32 Little Endian BOM elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00") { $encoding[] = 'UTF-32LE'; } // UTF-16 Big Endian BOM elseif (substr($data, 0, 2) === "\xFE\xFF") { $encoding[] = 'UTF-16BE'; } // UTF-16 Little Endian BOM elseif (substr($data, 0, 2) === "\xFF\xFE") { $encoding[] = 'UTF-16LE'; } // UTF-8 BOM elseif (substr($data, 0, 3) === "\xEF\xBB\xBF") { $encoding[] = 'UTF-8'; } // UTF-32 Big Endian Without BOM elseif (substr($data, 0, 20) === "\x00\x00\x00\x3C\x00\x00\x00\x3F\x00\x00\x00\x78\x00\x00\x00\x6D\x00\x00\x00\x6C") { if ($pos = strpos($data, "\x00\x00\x00\x3F\x00\x00\x00\x3E")) { $parser = $registry->create(Parser::class, [Misc::change_encoding(substr($data, 20, $pos - 20), 'UTF-32BE', 'UTF-8')]); if ($parser->parse()) { $encoding[] = $parser->encoding; } } $encoding[] = 'UTF-32BE'; } // UTF-32 Little Endian Without BOM elseif (substr($data, 0, 20) === "\x3C\x00\x00\x00\x3F\x00\x00\x00\x78\x00\x00\x00\x6D\x00\x00\x00\x6C\x00\x00\x00") { if ($pos = strpos($data, "\x3F\x00\x00\x00\x3E\x00\x00\x00")) { $parser = $registry->create(Parser::class, [Misc::change_encoding(substr($data, 20, $pos - 20), 'UTF-32LE', 'UTF-8')]); if ($parser->parse()) { $encoding[] = $parser->encoding; } } $encoding[] = 'UTF-32LE'; } // UTF-16 Big Endian Without BOM elseif (substr($data, 0, 10) === "\x00\x3C\x00\x3F\x00\x78\x00\x6D\x00\x6C") { if ($pos = strpos($data, "\x00\x3F\x00\x3E")) { $parser = $registry->create(Parser::class, [Misc::change_encoding(substr($data, 20, $pos - 10), 'UTF-16BE', 'UTF-8')]); if ($parser->parse()) { $encoding[] = $parser->encoding; } } $encoding[] = 'UTF-16BE'; } // UTF-16 Little Endian Without BOM elseif (substr($data, 0, 10) === "\x3C\x00\x3F\x00\x78\x00\x6D\x00\x6C\x00") { if ($pos = strpos($data, "\x3F\x00\x3E\x00")) { $parser = $registry->create(Parser::class, [Misc::change_encoding(substr($data, 20, $pos - 10), 'UTF-16LE', 'UTF-8')]); if ($parser->parse()) { $encoding[] = $parser->encoding; } } $encoding[] = 'UTF-16LE'; } // US-ASCII (or superset) elseif (substr($data, 0, 5) === "\x3C\x3F\x78\x6D\x6C") { if ($pos = strpos($data, "\x3F\x3E")) { $parser = $registry->create(Parser::class, [substr($data, 5, $pos - 5)]); if ($parser->parse()) { $encoding[] = $parser->encoding; } } $encoding[] = 'UTF-8'; } // Fallback to UTF-8 else { $encoding[] = 'UTF-8'; } return $encoding; } /** * @return void */ public static function output_javascript() { if (function_exists('ob_gzhandler')) { ob_start('ob_gzhandler'); } header('Content-type: text/javascript; charset: UTF-8'); header('Cache-Control: must-revalidate'); header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 604800) . ' GMT'); // 7 days $body = <<<JS function embed_quicktime(type, bgcolor, width, height, link, placeholder, loop) { if (placeholder != '') { document.writeln('<embed type="'+type+'" style="cursor:hand; cursor:pointer;" href="'+link+'" src="'+placeholder+'" width="'+width+'" height="'+height+'" autoplay="false" target="myself" controller="false" loop="'+loop+'" scale="aspect" bgcolor="'+bgcolor+'" pluginspage="http://www.apple.com/quicktime/download/"></embed>'); } else { document.writeln('<embed type="'+type+'" style="cursor:hand; cursor:pointer;" src="'+link+'" width="'+width+'" height="'+height+'" autoplay="false" target="myself" controller="true" loop="'+loop+'" scale="aspect" bgcolor="'+bgcolor+'" pluginspage="http://www.apple.com/quicktime/download/"></embed>'); } } function embed_flash(bgcolor, width, height, link, loop, type) { document.writeln('<embed src="'+link+'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="'+type+'" quality="high" width="'+width+'" height="'+height+'" bgcolor="'+bgcolor+'" loop="'+loop+'"></embed>'); } function embed_flv(width, height, link, placeholder, loop, player) { document.writeln('<embed src="'+player+'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" quality="high" width="'+width+'" height="'+height+'" wmode="transparent" flashvars="file='+link+'&autostart=false&repeat='+loop+'&showdigits=true&showfsbutton=false"></embed>'); } function embed_wmedia(width, height, link) { document.writeln('<embed type="application/x-mplayer2" src="'+link+'" autosize="1" width="'+width+'" height="'+height+'" showcontrols="1" showstatusbar="0" showdisplay="0" autostart="0"></embed>'); } JS; echo $body; } /** * Get the SimplePie build timestamp * * Uses the git index if it exists, otherwise uses the modification time * of the newest file. * * @return int */ public static function get_build() { if (self::$SIMPLEPIE_BUILD !== null) { return self::$SIMPLEPIE_BUILD; } $root = dirname(__FILE__, 2); if (file_exists($root . '/.git/index')) { self::$SIMPLEPIE_BUILD = (int) filemtime($root . '/.git/index'); return self::$SIMPLEPIE_BUILD; } elseif (file_exists($root . '/src')) { $time = 0; foreach (glob($root . '/src/*.php') ?: [] as $file) { if (($mtime = filemtime($file)) > $time) { $time = $mtime; } } self::$SIMPLEPIE_BUILD = $time; return self::$SIMPLEPIE_BUILD; } self::$SIMPLEPIE_BUILD = (int) filemtime(__FILE__); return self::$SIMPLEPIE_BUILD; } /** * Get the default user agent string * * @return string */ public static function get_default_useragent() { return \SimplePie\SimplePie::NAME . '/' . \SimplePie\SimplePie::VERSION . ' (Feed Parser; ' . \SimplePie\SimplePie::URL . '; Allow like Gecko) Build/' . static::get_build(); } /** * Format debugging information * * @return string */ public static function debug(SimplePie &$sp) { $info = 'SimplePie ' . \SimplePie\SimplePie::VERSION . ' Build ' . static::get_build() . "\n"; $info .= 'PHP ' . PHP_VERSION . "\n"; if ($sp->error() !== null) { // TODO: Remove cast with multifeeds. $info .= 'Error occurred: ' . implode(', ', (array) $sp->error()) . "\n"; } else { $info .= "No error found.\n"; } $info .= "Extensions:\n"; $extensions = ['pcre', 'curl', 'zlib', 'mbstring', 'iconv', 'xmlreader', 'xml']; foreach ($extensions as $ext) { if (extension_loaded($ext)) { $info .= " $ext loaded\n"; switch ($ext) { case 'pcre': $info .= ' Version ' . PCRE_VERSION . "\n"; break; case 'curl': $version = (array) curl_version(); $info .= ' Version ' . $version['version'] . "\n"; break; case 'iconv': $info .= ' Version ' . ICONV_VERSION . "\n"; break; case 'xml': $info .= ' Version ' . LIBXML_DOTTED_VERSION . "\n"; break; } } else { $info .= " $ext not loaded\n"; } } return $info; } /** * @return bool */ public static function silence_errors(int $num, string $str) { // No-op return true; } /** * Sanitize a URL by removing HTTP credentials. * @param string $url the URL to sanitize. * @return string the same URL without HTTP credentials. */ public static function url_remove_credentials(string $url) { // Cast for PHPStan: I do not think this can fail. // The regex is valid and there should be no backtracking. // https://github.com/phpstan/phpstan/issues/11547 return (string) preg_replace('#^(https?://)[^/:@]+:[^/:@]+@#i', '$1', $url); } } class_alias('SimplePie\Misc', 'SimplePie_Misc', false); Source.php 0000644 00000056272 15162130222 0006523 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; /** * Handles `<atom:source>` * * Used by {@see \SimplePie\Item::get_source()} * * This class can be overloaded with {@see \SimplePie::set_source_class()} */ class Source implements RegistryAware { /** @var Item */ public $item; /** @var array<string, mixed> */ public $data = []; /** @var Registry */ protected $registry; /** * @param array<string, mixed> $data */ public function __construct(Item $item, array $data) { $this->item = $item; $this->data = $data; } /** * @return void */ public function set_registry(\SimplePie\Registry $registry) { $this->registry = $registry; } /** * @return string */ public function __toString() { return md5(serialize($this->data)); } /** * @param string $namespace * @param string $tag * @return array<array<string, mixed>>|null */ public function get_source_tags(string $namespace, string $tag) { if (isset($this->data['child'][$namespace][$tag])) { return $this->data['child'][$namespace][$tag]; } return null; } /** * @param array<string, mixed> $element * @return string */ public function get_base(array $element = []) { return $this->item->get_base($element); } /** * @param string $data * @param int-mask-of<SimplePie::CONSTRUCT_*> $type * @param string $base * @return string */ public function sanitize(string $data, $type, string $base = '') { return $this->item->sanitize($data, $type, $base); } /** * @return Item */ public function get_item() { return $this->item; } /** * @return string|null */ public function get_title() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'title')) { return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'title')) { return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_10, 'title')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_090, 'title')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'title')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, 'title')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_DC_10, 'title')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } return null; } /** * @param int $key * @return Category|null */ public function get_category(int $key = 0) { $categories = $this->get_categories(); if (isset($categories[$key])) { return $categories[$key]; } return null; } /** * @return array<Category>|null */ public function get_categories() { $categories = []; foreach ((array) $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'category') as $category) { $term = null; $scheme = null; $label = null; if (isset($category['attribs']['']['term'])) { $term = $this->sanitize($category['attribs']['']['term'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($category['attribs']['']['scheme'])) { $scheme = $this->sanitize($category['attribs']['']['scheme'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($category['attribs']['']['label'])) { $label = $this->sanitize($category['attribs']['']['label'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } $categories[] = $this->registry->create(Category::class, [$term, $scheme, $label]); } foreach ((array) $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'category') as $category) { // This is really the label, but keep this as the term also for BC. // Label will also work on retrieving because that falls back to term. $term = $this->sanitize($category['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); if (isset($category['attribs']['']['domain'])) { $scheme = $this->sanitize($category['attribs']['']['domain'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } else { $scheme = null; } $categories[] = $this->registry->create(Category::class, [$term, $scheme, null]); } foreach ((array) $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, 'subject') as $category) { $categories[] = $this->registry->create(Category::class, [$this->sanitize($category['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT), null, null]); } foreach ((array) $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_DC_10, 'subject') as $category) { $categories[] = $this->registry->create(Category::class, [$this->sanitize($category['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT), null, null]); } if (!empty($categories)) { return array_unique($categories); } return null; } /** * @param int $key * @return Author|null */ public function get_author(int $key = 0) { $authors = $this->get_authors(); if (isset($authors[$key])) { return $authors[$key]; } return null; } /** * @return array<Author>|null */ public function get_authors() { $authors = []; foreach ((array) $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'author') as $author) { $name = null; $uri = null; $email = null; if (isset($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['name'][0]['data'])) { $name = $this->sanitize($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['name'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0]['data'])) { $uri = $author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0]; $uri = $this->sanitize($uri['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($uri)); } if (isset($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['email'][0]['data'])) { $email = $this->sanitize($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['email'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if ($name !== null || $email !== null || $uri !== null) { $authors[] = $this->registry->create(Author::class, [$name, $uri, $email]); } } if ($author = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'author')) { $name = null; $url = null; $email = null; if (isset($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['name'][0]['data'])) { $name = $this->sanitize($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['name'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0]['data'])) { $url = $author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0]; $url = $this->sanitize($url['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($url)); } if (isset($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['email'][0]['data'])) { $email = $this->sanitize($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['email'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if ($name !== null || $email !== null || $url !== null) { $authors[] = $this->registry->create(Author::class, [$name, $url, $email]); } } foreach ((array) $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, 'creator') as $author) { $authors[] = $this->registry->create(Author::class, [$this->sanitize($author['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT), null, null]); } foreach ((array) $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_DC_10, 'creator') as $author) { $authors[] = $this->registry->create(Author::class, [$this->sanitize($author['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT), null, null]); } foreach ((array) $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'author') as $author) { $authors[] = $this->registry->create(Author::class, [$this->sanitize($author['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT), null, null]); } if (!empty($authors)) { return array_unique($authors); } return null; } /** * @param int $key * @return Author|null */ public function get_contributor(int $key = 0) { $contributors = $this->get_contributors(); if (isset($contributors[$key])) { return $contributors[$key]; } return null; } /** * @return array<Author>|null */ public function get_contributors() { $contributors = []; foreach ((array) $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'contributor') as $contributor) { $name = null; $uri = null; $email = null; if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['name'][0]['data'])) { $name = $this->sanitize($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['name'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0]['data'])) { $uri = $contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0]; $uri = $this->sanitize($uri['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($uri)); } if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['email'][0]['data'])) { $email = $this->sanitize($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['email'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if ($name !== null || $email !== null || $uri !== null) { $contributors[] = $this->registry->create(Author::class, [$name, $uri, $email]); } } foreach ((array) $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'contributor') as $contributor) { $name = null; $url = null; $email = null; if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['name'][0]['data'])) { $name = $this->sanitize($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['name'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0]['data'])) { $url = $contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0]; $url = $this->sanitize($url['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($url)); } if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['email'][0]['data'])) { $email = $this->sanitize($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['email'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if ($name !== null || $email !== null || $url !== null) { $contributors[] = $this->registry->create(Author::class, [$name, $url, $email]); } } if (!empty($contributors)) { return array_unique($contributors); } return null; } /** * @param int $key * @param string $rel * @return string|null */ public function get_link(int $key = 0, string $rel = 'alternate') { $links = $this->get_links($rel); if (isset($links[$key])) { return $links[$key]; } return null; } /** * Added for parity between the parent-level and the item/entry-level. * * @return string|null */ public function get_permalink() { return $this->get_link(0); } /** * @param string $rel * @return array<string>|null */ public function get_links(string $rel = 'alternate') { if (!isset($this->data['links'])) { $this->data['links'] = []; if ($links = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'link')) { foreach ($links as $link) { if (isset($link['attribs']['']['href'])) { $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate'; $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($link)); } } } if ($links = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'link')) { foreach ($links as $link) { if (isset($link['attribs']['']['href'])) { $link_rel = (isset($link['attribs']['']['rel'])) ? $link['attribs']['']['rel'] : 'alternate'; $this->data['links'][$link_rel][] = $this->sanitize($link['attribs']['']['href'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($link)); } } } if ($links = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_10, 'link')) { $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($links[0])); } if ($links = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_090, 'link')) { $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($links[0])); } if ($links = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'link')) { $this->data['links']['alternate'][] = $this->sanitize($links[0]['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($links[0])); } $keys = array_keys($this->data['links']); foreach ($keys as $key) { $key = (string) $key; if ($this->registry->call(Misc::class, 'is_isegment_nz_nc', [$key])) { if (isset($this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key])) { $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key] = array_merge($this->data['links'][$key], $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key]); $this->data['links'][$key] = &$this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key]; } else { $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key] = &$this->data['links'][$key]; } } elseif (substr($key, 0, 41) === \SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY) { $this->data['links'][substr($key, 41)] = &$this->data['links'][$key]; } $this->data['links'][$key] = array_unique($this->data['links'][$key]); } } if (isset($this->data['links'][$rel])) { return $this->data['links'][$rel]; } return null; } /** * @return string|null */ public function get_description() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'subtitle')) { return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'tagline')) { return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_10, 'description')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_090, 'description')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'description')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, 'description')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_DC_10, 'description')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'summary')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_HTML, $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'subtitle')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_HTML, $this->get_base($return[0])); } return null; } /** * @return string|null */ public function get_copyright() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'rights')) { return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_10_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_03, 'copyright')) { return $this->sanitize($return[0]['data'], $this->registry->call(Misc::class, 'atom_03_construct_type', [$return[0]['attribs']]), $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'copyright')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, 'rights')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_DC_10, 'rights')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } return null; } /** * @return string|null */ public function get_language() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'language')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_DC_11, 'language')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_DC_10, 'language')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } elseif (isset($this->data['xml_lang'])) { return $this->sanitize($this->data['xml_lang'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } return null; } /** * @return float|null */ public function get_latitude() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_W3C_BASIC_GEO, 'lat')) { return (float) $return[0]['data']; } elseif (($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match)) { return (float) $match[1]; } return null; } /** * @return float|null */ public function get_longitude() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_W3C_BASIC_GEO, 'long')) { return (float) $return[0]['data']; } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_W3C_BASIC_GEO, 'lon')) { return (float) $return[0]['data']; } elseif (($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_GEORSS, 'point')) && preg_match('/^((?:-)?[0-9]+(?:\.[0-9]+)) ((?:-)?[0-9]+(?:\.[0-9]+))$/', trim($return[0]['data']), $match)) { return (float) $match[2]; } return null; } /** * @return string|null */ public function get_image_url() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'image')) { return $this->sanitize($return[0]['attribs']['']['href'], \SimplePie\SimplePie::CONSTRUCT_IRI); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'logo')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($return[0])); } elseif ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'icon')) { return $this->sanitize($return[0]['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($return[0])); } return null; } } class_alias('SimplePie\Source', 'SimplePie_Source'); Author.php 0000644 00000003562 15162130223 0006520 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; /** * Manages all author-related data * * Used by {@see Item::get_author()} and {@see SimplePie::get_authors()} * * This class can be overloaded with {@see SimplePie::set_author_class()} */ class Author { /** * Author's name * * @var ?string * @see get_name() */ public $name; /** * Author's link * * @var ?string * @see get_link() */ public $link; /** * Author's email address * * @var ?string * @see get_email() */ public $email; /** * Constructor, used to input the data */ public function __construct( ?string $name = null, ?string $link = null, ?string $email = null ) { $this->name = $name; $this->link = $link; $this->email = $email; } /** * String-ified version * * @return string */ public function __toString() { // There is no $this->data here return md5(serialize($this)); } /** * Author's name * * @return string|null */ public function get_name() { if ($this->name !== null) { return $this->name; } return null; } /** * Author's link * * @return string|null */ public function get_link() { if ($this->link !== null) { return $this->link; } return null; } /** * Author's email address * * @return string|null */ public function get_email() { if ($this->email !== null) { return $this->email; } return null; } } class_alias('SimplePie\Author', 'SimplePie_Author'); Sanitize.php 0000644 00000073221 15162130223 0007043 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; use DOMDocument; use DOMXPath; use InvalidArgumentException; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\UriFactoryInterface; use SimplePie\Cache\Base; use SimplePie\Cache\BaseDataCache; use SimplePie\Cache\CallableNameFilter; use SimplePie\Cache\DataCache; use SimplePie\Cache\NameFilter; use SimplePie\HTTP\Client; use SimplePie\HTTP\ClientException; use SimplePie\HTTP\FileClient; use SimplePie\HTTP\Psr18Client; /** * Used for data cleanup and post-processing * * * This class can be overloaded with {@see \SimplePie\SimplePie::set_sanitize_class()} * * @todo Move to using an actual HTML parser (this will allow tags to be properly stripped, and to switch between HTML and XHTML), this will also make it easier to shorten a string while preserving HTML tags */ class Sanitize implements RegistryAware { // Private vars /** @var string */ public $base = ''; // Options /** @var bool */ public $remove_div = true; /** @var string */ public $image_handler = ''; /** @var string[] */ public $strip_htmltags = ['base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style']; /** @var bool */ public $encode_instead_of_strip = false; /** @var string[] */ public $strip_attributes = ['bgsound', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc']; /** @var string[] */ public $rename_attributes = []; /** @var array<string, array<string, string>> */ public $add_attributes = ['audio' => ['preload' => 'none'], 'iframe' => ['sandbox' => 'allow-scripts allow-same-origin'], 'video' => ['preload' => 'none']]; /** @var bool */ public $strip_comments = false; /** @var string */ public $output_encoding = 'UTF-8'; /** @var bool */ public $enable_cache = true; /** @var string */ public $cache_location = './cache'; /** @var string&(callable(string): string) */ public $cache_name_function = 'md5'; /** * @var NameFilter */ private $cache_namefilter; /** @var int */ public $timeout = 10; /** @var string */ public $useragent = ''; /** @var bool */ public $force_fsockopen = false; /** @var array<string, string|string[]> */ public $replace_url_attributes = []; /** * @var array<int, mixed> Custom curl options * @see SimplePie::set_curl_options() */ private $curl_options = []; /** @var Registry */ public $registry; /** * @var DataCache|null */ private $cache = null; /** * @var int Cache duration (in seconds) */ private $cache_duration = 3600; /** * List of domains for which to force HTTPS. * @see \SimplePie\Sanitize::set_https_domains() * Array is a tree split at DNS levels. Example: * array('biz' => true, 'com' => array('example' => true), 'net' => array('example' => array('www' => true))) * @var true|array<string, true|array<string, true|array<string, array<string, true|array<string, true|array<string, true>>>>>> */ public $https_domains = []; /** * @var Client|null */ private $http_client = null; public function __construct() { // Set defaults $this->set_url_replacements(null); } /** * @return void */ public function remove_div(bool $enable = true) { $this->remove_div = (bool) $enable; } /** * @param string|false $page * @return void */ public function set_image_handler($page = false) { if ($page) { $this->image_handler = (string) $page; } else { $this->image_handler = ''; } } /** * @return void */ public function set_registry(\SimplePie\Registry $registry) { $this->registry = $registry; } /** * @param (string&(callable(string): string))|NameFilter $cache_name_function * @param class-string<Cache> $cache_class * @return void */ public function pass_cache_data(bool $enable_cache = true, string $cache_location = './cache', $cache_name_function = 'md5', string $cache_class = Cache::class, ?DataCache $cache = null) { $this->enable_cache = $enable_cache; if ($cache_location) { $this->cache_location = $cache_location; } // @phpstan-ignore-next-line Enforce PHPDoc type. if (!is_string($cache_name_function) && !$cache_name_function instanceof NameFilter) { throw new InvalidArgumentException(sprintf( '%s(): Argument #3 ($cache_name_function) must be of type %s', __METHOD__, NameFilter::class ), 1); } // BC: $cache_name_function could be a callable as string if (is_string($cache_name_function)) { // trigger_error(sprintf('Providing $cache_name_function as string in "%s()" is deprecated since SimplePie 1.8.0, provide as "%s" instead.', __METHOD__, NameFilter::class), \E_USER_DEPRECATED); $this->cache_name_function = $cache_name_function; $cache_name_function = new CallableNameFilter($cache_name_function); } $this->cache_namefilter = $cache_name_function; if ($cache !== null) { $this->cache = $cache; } } /** * Set a PSR-18 client and PSR-17 factories * * Allows you to use your own HTTP client implementations. */ final public function set_http_client( ClientInterface $http_client, RequestFactoryInterface $request_factory, UriFactoryInterface $uri_factory ): void { $this->http_client = new Psr18Client($http_client, $request_factory, $uri_factory); } /** * @deprecated since SimplePie 1.9.0, use \SimplePie\Sanitize::set_http_client() instead. * @param class-string<File> $file_class * @param array<int, mixed> $curl_options * @return void */ public function pass_file_data(string $file_class = File::class, int $timeout = 10, string $useragent = '', bool $force_fsockopen = false, array $curl_options = []) { // trigger_error(sprintf('SimplePie\Sanitize::pass_file_data() is deprecated since SimplePie 1.9.0, please use "SimplePie\Sanitize::set_http_client()" instead.'), \E_USER_DEPRECATED); if ($timeout) { $this->timeout = $timeout; } if ($useragent) { $this->useragent = $useragent; } if ($force_fsockopen) { $this->force_fsockopen = $force_fsockopen; } $this->curl_options = $curl_options; // Invalidate the registered client. $this->http_client = null; } /** * @param string[]|string|false $tags Set a list of tags to strip, or set empty string to use default tags, or false to strip nothing. * @return void */ public function strip_htmltags($tags = ['base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style']) { if ($tags) { if (is_array($tags)) { $this->strip_htmltags = $tags; } else { $this->strip_htmltags = explode(',', $tags); } } else { $this->strip_htmltags = []; } } /** * @return void */ public function encode_instead_of_strip(bool $encode = false) { $this->encode_instead_of_strip = $encode; } /** * @param string[]|string $attribs * @return void */ public function rename_attributes($attribs = []) { if ($attribs) { if (is_array($attribs)) { $this->rename_attributes = $attribs; } else { $this->rename_attributes = explode(',', $attribs); } } else { $this->rename_attributes = []; } } /** * @param string[]|string $attribs * @return void */ public function strip_attributes($attribs = ['bgsound', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc']) { if ($attribs) { if (is_array($attribs)) { $this->strip_attributes = $attribs; } else { $this->strip_attributes = explode(',', $attribs); } } else { $this->strip_attributes = []; } } /** * @param array<string, array<string, string>> $attribs * @return void */ public function add_attributes(array $attribs = ['audio' => ['preload' => 'none'], 'iframe' => ['sandbox' => 'allow-scripts allow-same-origin'], 'video' => ['preload' => 'none']]) { $this->add_attributes = $attribs; } /** * @return void */ public function strip_comments(bool $strip = false) { $this->strip_comments = $strip; } /** * @return void */ public function set_output_encoding(string $encoding = 'UTF-8') { $this->output_encoding = $encoding; } /** * Set element/attribute key/value pairs of HTML attributes * containing URLs that need to be resolved relative to the feed * * Defaults to |a|@href, |area|@href, |audio|@src, |blockquote|@cite, * |del|@cite, |form|@action, |img|@longdesc, |img|@src, |input|@src, * |ins|@cite, |q|@cite, |source|@src, |video|@src * * @since 1.0 * @param array<string, string|string[]>|null $element_attribute Element/attribute key/value pairs, null for default * @return void */ public function set_url_replacements(?array $element_attribute = null) { if ($element_attribute === null) { $element_attribute = [ 'a' => 'href', 'area' => 'href', 'audio' => 'src', 'blockquote' => 'cite', 'del' => 'cite', 'form' => 'action', 'img' => [ 'longdesc', 'src' ], 'input' => 'src', 'ins' => 'cite', 'q' => 'cite', 'source' => 'src', 'video' => [ 'poster', 'src' ] ]; } $this->replace_url_attributes = $element_attribute; } /** * Set the list of domains for which to force HTTPS. * @see \SimplePie\Misc::https_url() * Example array('biz', 'example.com', 'example.org', 'www.example.net'); * * @param string[] $domains list of domain names ['biz', 'example.com', 'example.org', 'www.example.net'] * * @return void */ public function set_https_domains(array $domains) { $this->https_domains = []; foreach ($domains as $domain) { $domain = trim($domain, ". \t\n\r\0\x0B"); $segments = array_reverse(explode('.', $domain)); /** @var true|array<string, true|array<string, true|array<string, array<string, true|array<string, true|array<string, true>>>>>> */ // Needed for PHPStan. $node = &$this->https_domains; foreach ($segments as $segment) {//Build a tree if ($node === true) { break; } if (!isset($node[$segment])) { $node[$segment] = []; } $node = &$node[$segment]; } $node = true; } } /** * Check if the domain is in the list of forced HTTPS. * * @return bool */ protected function is_https_domain(string $domain) { $domain = trim($domain, '. '); $segments = array_reverse(explode('.', $domain)); $node = &$this->https_domains; foreach ($segments as $segment) {//Explore the tree if (isset($node[$segment])) { $node = &$node[$segment]; } else { break; } } return $node === true; } /** * Force HTTPS for selected Web sites. * * @return string */ public function https_url(string $url) { return ( strtolower(substr($url, 0, 7)) === 'http://' && ($parsed = parse_url($url, PHP_URL_HOST)) !== false // Malformed URL && $parsed !== null // Missing host && $this->is_https_domain($parsed) // Should be forced? ) ? substr_replace($url, 's', 4, 0) // Add the 's' to HTTPS : $url; } /** * @param int-mask-of<SimplePie::CONSTRUCT_*> $type * @param string $base * @return string Sanitized data; false if output encoding is changed to something other than UTF-8 and conversion fails */ public function sanitize(string $data, int $type, string $base = '') { $data = trim($data); if ($data !== '' || $type & \SimplePie\SimplePie::CONSTRUCT_IRI) { if ($type & \SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML) { if (preg_match('/(&(#(x[0-9a-fA-F]+|[0-9]+)|[a-zA-Z0-9]+)|<\/[A-Za-z][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E]*' . \SimplePie\SimplePie::PCRE_HTML_ATTRIBUTE . '>)/', $data)) { $type |= \SimplePie\SimplePie::CONSTRUCT_HTML; } else { $type |= \SimplePie\SimplePie::CONSTRUCT_TEXT; } } if ($type & \SimplePie\SimplePie::CONSTRUCT_BASE64) { $data = base64_decode($data); } if ($type & (\SimplePie\SimplePie::CONSTRUCT_HTML | \SimplePie\SimplePie::CONSTRUCT_XHTML)) { if (!class_exists('DOMDocument')) { throw new \SimplePie\Exception('DOMDocument not found, unable to use sanitizer'); } $document = new \DOMDocument(); $document->encoding = 'UTF-8'; // PHPStan seems to have trouble resolving int-mask because bitwise // operators are used when operators are used when passing this parameter. // https://github.com/phpstan/phpstan/issues/9384 /** @var int-mask-of<SimplePie::CONSTRUCT_*> $type */ $data = $this->preprocess($data, $type); set_error_handler([Misc::class, 'silence_errors']); $document->loadHTML($data); restore_error_handler(); $xpath = new \DOMXPath($document); // Strip comments if ($this->strip_comments) { /** @var \DOMNodeList<\DOMComment> */ $comments = $xpath->query('//comment()'); foreach ($comments as $comment) { $parentNode = $comment->parentNode; assert($parentNode !== null, 'For PHPStan, comment must have a parent'); $parentNode->removeChild($comment); } } // Strip out HTML tags and attributes that might cause various security problems. // Based on recommendations by Mark Pilgrim at: // https://web.archive.org/web/20110902041826/http://diveintomark.org:80/archives/2003/06/12/how_to_consume_rss_safely if ($this->strip_htmltags) { foreach ($this->strip_htmltags as $tag) { $this->strip_tag($tag, $document, $xpath, $type); } } if ($this->rename_attributes) { foreach ($this->rename_attributes as $attrib) { $this->rename_attr($attrib, $xpath); } } if ($this->strip_attributes) { foreach ($this->strip_attributes as $attrib) { $this->strip_attr($attrib, $xpath); } } if ($this->add_attributes) { foreach ($this->add_attributes as $tag => $valuePairs) { $this->add_attr($tag, $valuePairs, $document); } } // Replace relative URLs $this->base = $base; foreach ($this->replace_url_attributes as $element => $attributes) { $this->replace_urls($document, $element, $attributes); } // If image handling (caching, etc.) is enabled, cache and rewrite all the image tags. if ($this->image_handler !== '' && $this->enable_cache) { $images = $document->getElementsByTagName('img'); foreach ($images as $img) { if ($img->hasAttribute('src')) { $image_url = $this->cache_namefilter->filter($img->getAttribute('src')); $cache = $this->get_cache($image_url); if ($cache->get_data($image_url, false)) { $img->setAttribute('src', $this->image_handler . $image_url); } else { try { $file = $this->get_http_client()->request( Client::METHOD_GET, $img->getAttribute('src'), ['X-FORWARDED-FOR' => $_SERVER['REMOTE_ADDR']] ); } catch (ClientException $th) { continue; } if ((!Misc::is_remote_uri($file->get_final_requested_uri()) || ($file->get_status_code() === 200 || $file->get_status_code() > 206 && $file->get_status_code() < 300))) { if ($cache->set_data($image_url, ['headers' => $file->get_headers(), 'body' => $file->get_body_content()], $this->cache_duration)) { $img->setAttribute('src', $this->image_handler . $image_url); } else { trigger_error("$this->cache_location is not writable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING); } } } } } } // Get content node $div = null; if (($item = $document->getElementsByTagName('body')->item(0)) !== null) { $div = $item->firstChild; } // Finally, convert to a HTML string $data = trim((string) $document->saveHTML($div)); if ($this->remove_div) { $data = preg_replace('/^<div' . \SimplePie\SimplePie::PCRE_XML_ATTRIBUTE . '>/', '', $data); // Cast for PHPStan, it is unable to validate a non-literal regex above. $data = preg_replace('/<\/div>$/', '', (string) $data); } else { $data = preg_replace('/^<div' . \SimplePie\SimplePie::PCRE_XML_ATTRIBUTE . '>/', '<div>', $data); } // Cast for PHPStan, it is unable to validate a non-literal regex above. $data = str_replace('</source>', '', (string) $data); } if ($type & \SimplePie\SimplePie::CONSTRUCT_IRI) { $absolute = $this->registry->call(Misc::class, 'absolutize_url', [$data, $base]); if ($absolute !== false) { $data = $absolute; } } if ($type & (\SimplePie\SimplePie::CONSTRUCT_TEXT | \SimplePie\SimplePie::CONSTRUCT_IRI)) { $data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8'); } if ($this->output_encoding !== 'UTF-8') { // This really returns string|false but changing encoding is uncommon and we are going to deprecate it, so let’s just lie to PHPStan in the interest of cleaner annotations. /** @var string */ $data = $this->registry->call(Misc::class, 'change_encoding', [$data, 'UTF-8', $this->output_encoding]); } } return $data; } /** * @param int-mask-of<SimplePie::CONSTRUCT_*> $type * @return string */ protected function preprocess(string $html, int $type) { $ret = ''; $html = preg_replace('%</?(?:html|body)[^>]*?'.'>%is', '', $html); if ($type & ~\SimplePie\SimplePie::CONSTRUCT_XHTML) { // Atom XHTML constructs are wrapped with a div by default // Note: No protection if $html contains a stray </div>! $html = '<div>' . $html . '</div>'; $ret .= '<!DOCTYPE html>'; $content_type = 'text/html'; } else { $ret .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; $content_type = 'application/xhtml+xml'; } $ret .= '<html><head>'; $ret .= '<meta http-equiv="Content-Type" content="' . $content_type . '; charset=utf-8" />'; $ret .= '</head><body>' . $html . '</body></html>'; return $ret; } /** * @param array<string>|string $attributes * @return void */ public function replace_urls(DOMDocument $document, string $tag, $attributes) { if (!is_array($attributes)) { $attributes = [$attributes]; } if (!is_array($this->strip_htmltags) || !in_array($tag, $this->strip_htmltags)) { $elements = $document->getElementsByTagName($tag); foreach ($elements as $element) { foreach ($attributes as $attribute) { if ($element->hasAttribute($attribute)) { $value = $this->registry->call(Misc::class, 'absolutize_url', [$element->getAttribute($attribute), $this->base]); if ($value !== false) { $value = $this->https_url($value); $element->setAttribute($attribute, $value); } } } } } } /** * @param array<int, string> $match * @return string */ public function do_strip_htmltags(array $match) { if ($this->encode_instead_of_strip) { if (isset($match[4]) && !in_array(strtolower($match[1]), ['script', 'style'])) { $match[1] = htmlspecialchars($match[1], ENT_COMPAT, 'UTF-8'); $match[2] = htmlspecialchars($match[2], ENT_COMPAT, 'UTF-8'); return "<$match[1]$match[2]>$match[3]</$match[1]>"; } else { return htmlspecialchars($match[0], ENT_COMPAT, 'UTF-8'); } } elseif (isset($match[4]) && !in_array(strtolower($match[1]), ['script', 'style'])) { return $match[4]; } else { return ''; } } /** * @param int-mask-of<SimplePie::CONSTRUCT_*> $type * @return void */ protected function strip_tag(string $tag, DOMDocument $document, DOMXPath $xpath, int $type) { $elements = $xpath->query('body//' . $tag); if ($elements === false) { throw new \SimplePie\Exception(sprintf( '%s(): Possibly malformed expression, check argument #1 ($tag)', __METHOD__ ), 1); } if ($this->encode_instead_of_strip) { foreach ($elements as $element) { $fragment = $document->createDocumentFragment(); // For elements which aren't script or style, include the tag itself if (!in_array($tag, ['script', 'style'])) { $text = '<' . $tag; if ($element->attributes !== null) { $attrs = []; foreach ($element->attributes as $name => $attr) { $value = $attr->value; // In XHTML, empty values should never exist, so we repeat the value if (empty($value) && ($type & \SimplePie\SimplePie::CONSTRUCT_XHTML)) { $value = $name; } // For HTML, empty is fine elseif (empty($value) && ($type & \SimplePie\SimplePie::CONSTRUCT_HTML)) { $attrs[] = $name; continue; } // Standard attribute text $attrs[] = $name . '="' . $attr->value . '"'; } $text .= ' ' . implode(' ', $attrs); } $text .= '>'; $fragment->appendChild(new \DOMText($text)); } $number = $element->childNodes->length; for ($i = $number; $i > 0; $i--) { if (($child = $element->childNodes->item(0)) !== null) { $fragment->appendChild($child); } } if (!in_array($tag, ['script', 'style'])) { $fragment->appendChild(new \DOMText('</' . $tag . '>')); } if (($parentNode = $element->parentNode) !== null) { $parentNode->replaceChild($fragment, $element); } } return; } elseif (in_array($tag, ['script', 'style'])) { foreach ($elements as $element) { if (($parentNode = $element->parentNode) !== null) { $parentNode->removeChild($element); } } return; } else { foreach ($elements as $element) { $fragment = $document->createDocumentFragment(); $number = $element->childNodes->length; for ($i = $number; $i > 0; $i--) { if (($child = $element->childNodes->item(0)) !== null) { $fragment->appendChild($child); } } if (($parentNode = $element->parentNode) !== null) { $parentNode->replaceChild($fragment, $element); } } } } /** * @return void */ protected function strip_attr(string $attrib, DOMXPath $xpath) { $elements = $xpath->query('//*[@' . $attrib . ']'); if ($elements === false) { throw new \SimplePie\Exception(sprintf( '%s(): Possibly malformed expression, check argument #1 ($attrib)', __METHOD__ ), 1); } /** @var \DOMElement $element */ foreach ($elements as $element) { $element->removeAttribute($attrib); } } /** * @return void */ protected function rename_attr(string $attrib, DOMXPath $xpath) { $elements = $xpath->query('//*[@' . $attrib . ']'); if ($elements === false) { throw new \SimplePie\Exception(sprintf( '%s(): Possibly malformed expression, check argument #1 ($attrib)', __METHOD__ ), 1); } /** @var \DOMElement $element */ foreach ($elements as $element) { $element->setAttribute('data-sanitized-' . $attrib, $element->getAttribute($attrib)); $element->removeAttribute($attrib); } } /** * @param array<string, string> $valuePairs * @return void */ protected function add_attr(string $tag, array $valuePairs, DOMDocument $document) { $elements = $document->getElementsByTagName($tag); /** @var \DOMElement $element */ foreach ($elements as $element) { foreach ($valuePairs as $attrib => $value) { $element->setAttribute($attrib, $value); } } } /** * Get a DataCache * * @param string $image_url Only needed for BC, can be removed in SimplePie 2.0.0 * * @return DataCache */ private function get_cache(string $image_url = ''): DataCache { if ($this->cache === null) { // @trigger_error(sprintf('Not providing as PSR-16 cache implementation is deprecated since SimplePie 1.8.0, please use "SimplePie\SimplePie::set_cache()".'), \E_USER_DEPRECATED); $cache = $this->registry->call(Cache::class, 'get_handler', [ $this->cache_location, $image_url, Base::TYPE_IMAGE ]); return new BaseDataCache($cache); } return $this->cache; } /** * Get a HTTP client */ private function get_http_client(): Client { if ($this->http_client === null) { $this->http_client = new FileClient( $this->registry, [ 'timeout' => $this->timeout, 'redirects' => 5, 'useragent' => $this->useragent, 'force_fsockopen' => $this->force_fsockopen, 'curl_options' => $this->curl_options, ] ); } return $this->http_client; } } class_alias('SimplePie\Sanitize', 'SimplePie_Sanitize'); Locator.php 0000644 00000040473 15162130224 0006664 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; use DomDocument; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\UriFactoryInterface; use SimplePie\HTTP\Client; use SimplePie\HTTP\ClientException; use SimplePie\HTTP\FileClient; use SimplePie\HTTP\Psr18Client; use SimplePie\HTTP\Response; /** * Used for feed auto-discovery * * * This class can be overloaded with {@see \SimplePie\SimplePie::set_locator_class()} */ class Locator implements RegistryAware { /** @var ?string */ public $useragent = null; /** @var int */ public $timeout = 10; /** @var File */ public $file; /** @var string[] */ public $local = []; /** @var string[] */ public $elsewhere = []; /** @var array<mixed> */ public $cached_entities = []; /** @var string */ public $http_base; /** @var string */ public $base; /** @var int */ public $base_location = 0; /** @var int */ public $checked_feeds = 0; /** @var int */ public $max_checked_feeds = 10; /** @var bool */ public $force_fsockopen = false; /** @var array<int, mixed> */ public $curl_options = []; /** @var ?\DomDocument */ public $dom; /** @var ?Registry */ protected $registry; /** * @var Client|null */ private $http_client = null; /** * @param array<int, mixed> $curl_options */ public function __construct(File $file, int $timeout = 10, ?string $useragent = null, int $max_checked_feeds = 10, bool $force_fsockopen = false, array $curl_options = []) { $this->file = $file; $this->useragent = $useragent; $this->timeout = $timeout; $this->max_checked_feeds = $max_checked_feeds; $this->force_fsockopen = $force_fsockopen; $this->curl_options = $curl_options; $body = $this->file->get_body_content(); if (class_exists('DOMDocument') && $body != '') { $this->dom = new \DOMDocument(); set_error_handler([Misc::class, 'silence_errors']); try { $this->dom->loadHTML($body); } catch (\Throwable $ex) { $this->dom = null; } restore_error_handler(); } else { $this->dom = null; } } /** * Set a PSR-18 client and PSR-17 factories * * Allows you to use your own HTTP client implementations. */ final public function set_http_client( ClientInterface $http_client, RequestFactoryInterface $request_factory, UriFactoryInterface $uri_factory ): void { $this->http_client = new Psr18Client($http_client, $request_factory, $uri_factory); } /** * @return void */ public function set_registry(\SimplePie\Registry $registry) { $this->registry = $registry; } /** * @param SimplePie::LOCATOR_* $type * @param array<Response>|null $working * @return Response|null */ public function find(int $type = \SimplePie\SimplePie::LOCATOR_ALL, ?array &$working = null) { assert($this->registry !== null); if ($this->is_feed($this->file)) { return $this->file; } if (Misc::is_remote_uri($this->file->get_final_requested_uri())) { $sniffer = $this->registry->create(Content\Type\Sniffer::class, [$this->file]); if ($sniffer->get_type() !== 'text/html') { return null; } } if ($type & ~\SimplePie\SimplePie::LOCATOR_NONE) { $this->get_base(); } if ($type & \SimplePie\SimplePie::LOCATOR_AUTODISCOVERY && $working = $this->autodiscovery()) { return $working[0]; } if ($type & (\SimplePie\SimplePie::LOCATOR_LOCAL_EXTENSION | \SimplePie\SimplePie::LOCATOR_LOCAL_BODY | \SimplePie\SimplePie::LOCATOR_REMOTE_EXTENSION | \SimplePie\SimplePie::LOCATOR_REMOTE_BODY) && $this->get_links()) { if ($type & \SimplePie\SimplePie::LOCATOR_LOCAL_EXTENSION && $working = $this->extension($this->local)) { return $working[0]; } if ($type & \SimplePie\SimplePie::LOCATOR_LOCAL_BODY && $working = $this->body($this->local)) { return $working[0]; } if ($type & \SimplePie\SimplePie::LOCATOR_REMOTE_EXTENSION && $working = $this->extension($this->elsewhere)) { return $working[0]; } if ($type & \SimplePie\SimplePie::LOCATOR_REMOTE_BODY && $working = $this->body($this->elsewhere)) { return $working[0]; } } return null; } /** * @return bool */ public function is_feed(Response $file, bool $check_html = false) { assert($this->registry !== null); if (Misc::is_remote_uri($file->get_final_requested_uri())) { $sniffer = $this->registry->create(Content\Type\Sniffer::class, [$file]); $sniffed = $sniffer->get_type(); $mime_types = ['application/rss+xml', 'application/rdf+xml', 'text/rdf', 'application/atom+xml', 'text/xml', 'application/xml', 'application/x-rss+xml']; if ($check_html) { $mime_types[] = 'text/html'; } return in_array($sniffed, $mime_types); } elseif (is_file($file->get_final_requested_uri())) { return true; } else { return false; } } /** * @return void */ public function get_base() { assert($this->registry !== null); if ($this->dom === null) { throw new \SimplePie\Exception('DOMDocument not found, unable to use locator'); } $this->http_base = $this->file->get_final_requested_uri(); $this->base = $this->http_base; $elements = $this->dom->getElementsByTagName('base'); foreach ($elements as $element) { if ($element->hasAttribute('href')) { $base = $this->registry->call(Misc::class, 'absolutize_url', [trim($element->getAttribute('href')), $this->http_base]); if ($base === false) { continue; } $this->base = $base; $this->base_location = method_exists($element, 'getLineNo') ? $element->getLineNo() : 0; break; } } } /** * @return array<Response>|null */ public function autodiscovery() { $done = []; $feeds = []; $feeds = array_merge($feeds, $this->search_elements_by_tag('link', $done, $feeds)); $feeds = array_merge($feeds, $this->search_elements_by_tag('a', $done, $feeds)); $feeds = array_merge($feeds, $this->search_elements_by_tag('area', $done, $feeds)); if (!empty($feeds)) { return array_values($feeds); } return null; } /** * @param string[] $done * @param array<string, Response> $feeds * @return array<string, Response> */ protected function search_elements_by_tag(string $name, array &$done, array $feeds) { assert($this->registry !== null); if ($this->dom === null) { throw new \SimplePie\Exception('DOMDocument not found, unable to use locator'); } $links = $this->dom->getElementsByTagName($name); foreach ($links as $link) { if ($this->checked_feeds === $this->max_checked_feeds) { break; } if ($link->hasAttribute('href') && $link->hasAttribute('rel')) { $rel = array_unique($this->registry->call(Misc::class, 'space_separated_tokens', [strtolower($link->getAttribute('rel'))])); $line = method_exists($link, 'getLineNo') ? $link->getLineNo() : 1; if ($this->base_location < $line) { $href = $this->registry->call(Misc::class, 'absolutize_url', [trim($link->getAttribute('href')), $this->base]); } else { $href = $this->registry->call(Misc::class, 'absolutize_url', [trim($link->getAttribute('href')), $this->http_base]); } if ($href === false) { continue; } if (!in_array($href, $done) && in_array('feed', $rel) || (in_array('alternate', $rel) && !in_array('stylesheet', $rel) && $link->hasAttribute('type') && in_array(strtolower($this->registry->call(Misc::class, 'parse_mime', [$link->getAttribute('type')])), ['text/html', 'application/rss+xml', 'application/atom+xml'])) && !isset($feeds[$href])) { $this->checked_feeds++; $headers = [ 'Accept' => SimplePie::DEFAULT_HTTP_ACCEPT_HEADER, ]; try { $feed = $this->get_http_client()->request(Client::METHOD_GET, $href, $headers); if ((!Misc::is_remote_uri($feed->get_final_requested_uri()) || ($feed->get_status_code() === 200 || $feed->get_status_code() > 206 && $feed->get_status_code() < 300)) && $this->is_feed($feed, true)) { $feeds[$href] = $feed; } } catch (ClientException $th) { // Just mark it as done and continue. } } $done[] = $href; } } return $feeds; } /** * @return true|null */ public function get_links() { assert($this->registry !== null); if ($this->dom === null) { throw new \SimplePie\Exception('DOMDocument not found, unable to use locator'); } $links = $this->dom->getElementsByTagName('a'); foreach ($links as $link) { if ($link->hasAttribute('href')) { $href = trim($link->getAttribute('href')); $parsed = $this->registry->call(Misc::class, 'parse_url', [$href]); if ($parsed['scheme'] === '' || preg_match('/^(https?|feed)?$/i', $parsed['scheme'])) { if (method_exists($link, 'getLineNo') && $this->base_location < $link->getLineNo()) { $href = $this->registry->call(Misc::class, 'absolutize_url', [trim($link->getAttribute('href')), $this->base]); } else { $href = $this->registry->call(Misc::class, 'absolutize_url', [trim($link->getAttribute('href')), $this->http_base]); } if ($href === false) { continue; } $current = $this->registry->call(Misc::class, 'parse_url', [$this->file->get_final_requested_uri()]); if ($parsed['authority'] === '' || $parsed['authority'] === $current['authority']) { $this->local[] = $href; } else { $this->elsewhere[] = $href; } } } } $this->local = array_unique($this->local); $this->elsewhere = array_unique($this->elsewhere); if (!empty($this->local) || !empty($this->elsewhere)) { return true; } return null; } /** * Extracts first `link` element with given `rel` attribute inside the `head` element. * * @return string|null */ public function get_rel_link(string $rel) { assert($this->registry !== null); if ($this->dom === null) { throw new \SimplePie\Exception('DOMDocument not found, unable to use '. 'locator'); } if (!class_exists('DOMXpath')) { throw new \SimplePie\Exception('DOMXpath not found, unable to use '. 'get_rel_link'); } $xpath = new \DOMXpath($this->dom); $query = '(//head)[1]/link[@rel and @href]'; /** @var \DOMNodeList<\DOMElement> */ $queryResult = $xpath->query($query); foreach ($queryResult as $link) { $href = trim($link->getAttribute('href')); $parsed = $this->registry->call(Misc::class, 'parse_url', [$href]); if ($parsed['scheme'] === '' || preg_match('/^https?$/i', $parsed['scheme'])) { if (method_exists($link, 'getLineNo') && $this->base_location < $link->getLineNo()) { $href = $this->registry->call( Misc::class, 'absolutize_url', [trim($link->getAttribute('href')), $this->base] ); } else { $href = $this->registry->call( Misc::class, 'absolutize_url', [trim($link->getAttribute('href')), $this->http_base] ); } if ($href === false) { return null; } $rel_values = explode(' ', strtolower($link->getAttribute('rel'))); if (in_array($rel, $rel_values)) { return $href; } } } return null; } /** * @param string[] $array * @return array<Response>|null */ public function extension(array &$array) { foreach ($array as $key => $value) { if ($this->checked_feeds === $this->max_checked_feeds) { break; } $extension = strrchr($value, '.'); if ($extension !== false && in_array(strtolower($extension), ['.rss', '.rdf', '.atom', '.xml'])) { $this->checked_feeds++; $headers = [ 'Accept' => SimplePie::DEFAULT_HTTP_ACCEPT_HEADER, ]; try { $feed = $this->get_http_client()->request(Client::METHOD_GET, $value, $headers); if ((!Misc::is_remote_uri($feed->get_final_requested_uri()) || ($feed->get_status_code() === 200 || $feed->get_status_code() > 206 && $feed->get_status_code() < 300)) && $this->is_feed($feed)) { return [$feed]; } } catch (ClientException $th) { // Just unset and continue. } unset($array[$key]); } } return null; } /** * @param string[] $array * @return array<Response>|null */ public function body(array &$array) { foreach ($array as $key => $value) { if ($this->checked_feeds === $this->max_checked_feeds) { break; } if (preg_match('/(feed|rss|rdf|atom|xml)/i', $value)) { $this->checked_feeds++; $headers = [ 'Accept' => SimplePie::DEFAULT_HTTP_ACCEPT_HEADER, ]; try { $feed = $this->get_http_client()->request(Client::METHOD_GET, $value, $headers); if ((!Misc::is_remote_uri($feed->get_final_requested_uri()) || ($feed->get_status_code() === 200 || $feed->get_status_code() > 206 && $feed->get_status_code() < 300)) && $this->is_feed($feed)) { return [$feed]; } } catch (ClientException $th) { // Just unset and continue. } unset($array[$key]); } } return null; } /** * Get a HTTP client */ private function get_http_client(): Client { assert($this->registry !== null); if ($this->http_client === null) { $options = [ 'timeout' => $this->timeout, 'redirects' => 5, 'force_fsockopen' => $this->force_fsockopen, 'curl_options' => $this->curl_options, ]; if ($this->useragent !== null) { $options['useragent'] = $this->useragent; } return new FileClient( $this->registry, $options ); } return $this->http_client; } } class_alias('SimplePie\Locator', 'SimplePie_Locator', false); Registry.php 0000644 00000017330 15162130225 0007066 0 ustar 00 <?php // SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause declare(strict_types=1); namespace SimplePie; use InvalidArgumentException; use SimplePie\Content\Type\Sniffer; use SimplePie\Parse\Date; use SimplePie\XML\Declaration\Parser as DeclarationParser; /** * Handles creating objects and calling methods * * Access this via {@see \SimplePie\SimplePie::get_registry()} */ class Registry { /** * Default class mapping * * Overriding classes *must* subclass these. * * @var array<class-string, class-string> */ protected $default = [ Cache::class => Cache::class, Locator::class => Locator::class, Parser::class => Parser::class, File::class => File::class, Sanitize::class => Sanitize::class, Item::class => Item::class, Author::class => Author::class, Category::class => Category::class, Enclosure::class => Enclosure::class, Caption::class => Caption::class, Copyright::class => Copyright::class, Credit::class => Credit::class, Rating::class => Rating::class, Restriction::class => Restriction::class, Sniffer::class => Sniffer::class, Source::class => Source::class, Misc::class => Misc::class, DeclarationParser::class => DeclarationParser::class, Date::class => Date::class, ]; /** * Class mapping * * @see register() * @var array<string, class-string> */ protected $classes = []; /** * Legacy classes * * @see register() * @var array<class-string> */ protected $legacy = []; /** * Legacy types * * @see register() * @var array<string, class-string> */ private $legacyTypes = [ 'Cache' => Cache::class, 'Locator' => Locator::class, 'Parser' => Parser::class, 'File' => File::class, 'Sanitize' => Sanitize::class, 'Item' => Item::class, 'Author' => Author::class, 'Category' => Category::class, 'Enclosure' => Enclosure::class, 'Caption' => Caption::class, 'Copyright' => Copyright::class, 'Credit' => Credit::class, 'Rating' => Rating::class, 'Restriction' => Restriction::class, 'Content_Type_Sniffer' => Sniffer::class, 'Source' => Source::class, 'Misc' => Misc::class, 'XML_Declaration_Parser' => DeclarationParser::class, 'Parse_Date' => Date::class, ]; /** * Constructor * * No-op */ public function __construct() { } /** * Register a class * * @param string $type See {@see $default} for names * @param class-string $class Class name, must subclass the corresponding default * @param bool $legacy Whether to enable legacy support for this class * @return bool Successfulness */ public function register(string $type, $class, bool $legacy = false) { if (array_key_exists($type, $this->legacyTypes)) { // trigger_error(sprintf('"%s"(): Using argument #1 ($type) with value "%s" is deprecated since SimplePie 1.8.0, use class-string "%s" instead.', __METHOD__, $type, $this->legacyTypes[$type]), \E_USER_DEPRECATED); $type = $this->legacyTypes[$type]; } if (!array_key_exists($type, $this->default)) { return false; } if (!class_exists($class)) { return false; } /** @var string */ $base_class = $this->default[$type]; if (!is_subclass_of($class, $base_class)) { return false; } $this->classes[$type] = $class; if ($legacy) { $this->legacy[] = $class; } return true; } /** * Get the class registered for a type * * Where possible, use {@see create()} or {@see call()} instead * * @template T * @param class-string<T> $type * @return class-string<T>|null */ public function get_class($type) { if (array_key_exists($type, $this->legacyTypes)) { // trigger_error(sprintf('"%s"(): Using argument #1 ($type) with value "%s" is deprecated since SimplePie 1.8.0, use class-string "%s" instead.', __METHOD__, $type, $this->legacyTypes[$type]), \E_USER_DEPRECATED); $type = $this->legacyTypes[$type]; } if (!array_key_exists($type, $this->default)) { return null; } // For PHPStan: values in $default should be subtypes of keys. /** @var class-string<T> */ $class = $this->default[$type]; if (array_key_exists($type, $this->classes)) { // For PHPStan: values in $classes should be subtypes of keys. /** @var class-string<T> */ $class = $this->classes[$type]; } return $class; } /** * Create a new instance of a given type * * @template T class-string $type * @param class-string<T> $type * @param array<mixed> $parameters Parameters to pass to the constructor * @return T Instance of class */ public function &create($type, array $parameters = []) { $class = $this->get_class($type); if ($class === null) { throw new InvalidArgumentException(sprintf( '%s(): Argument #1 ($type) "%s" not found in class list.', __METHOD__, $type ), 1); } if (!method_exists($class, '__construct')) { $instance = new $class(); } else { $reflector = new \ReflectionClass($class); // For PHPStan: $class is T. /** @var T */ $instance = $reflector->newInstanceArgs($parameters); } if ($instance instanceof RegistryAware) { $instance->set_registry($this); } elseif (method_exists($instance, 'set_registry')) { trigger_error(sprintf('Using the method "set_registry()" without implementing "%s" is deprecated since SimplePie 1.8.0, implement "%s" in "%s".', RegistryAware::class, RegistryAware::class, $class), \E_USER_DEPRECATED); $instance->set_registry($this); } return $instance; } /** * Call a static method for a type * * @param class-string $type * @param string $method * @param array<mixed> $parameters * @return mixed */ public function &call($type, string $method, array $parameters = []) { $class = $this->get_class($type); if ($class === null) { throw new InvalidArgumentException(sprintf( '%s(): Argument #1 ($type) "%s" not found in class list.', __METHOD__, $type ), 1); } if (in_array($class, $this->legacy)) { switch ($type) { case Cache::class: // For backwards compatibility with old non-static // Cache::create() methods in PHP < 8.0. // No longer supported as of PHP 8.0. if ($method === 'get_handler') { // Fixing this PHPStan error breaks CacheTest::testDirectOverrideLegacy() /** @phpstan-ignore argument.type */ $result = @call_user_func_array([$class, 'create'], $parameters); return $result; } break; } } $callable = [$class, $method]; assert(is_callable($callable), 'For PHPstan'); $result = call_user_func_array($callable, $parameters); return $result; } } class_alias('SimplePie\Registry', 'SimplePie_Registry'); PHP52/SplFixedArray.php 0000644 00000011456 15162213652 0010602 0 ustar 00 <?php if (class_exists('SplFixedArray')) { return; } /** * The SplFixedArray class provides the main functionalities of array. The * main differences between a SplFixedArray and a normal PHP array is that * the SplFixedArray is of fixed length and allows only integers within * the range as indexes. The advantage is that it allows a faster array * implementation. */ class SplFixedArray implements Iterator, ArrayAccess, Countable { /** @var array<int, mixed> */ private $internalArray = array(); /** @var int $size */ private $size = 0; /** * SplFixedArray constructor. * @param int $size */ public function __construct($size = 0) { $this->size = $size; $this->internalArray = array(); } /** * @return int */ #[\ReturnTypeWillChange] public function count() { return count($this->internalArray); } /** * @return array */ public function toArray() { ksort($this->internalArray); return (array) $this->internalArray; } /** * @param array $array * @param bool $save_indexes * @return SplFixedArray * @psalm-suppress MixedAssignment */ public static function fromArray(array $array, $save_indexes = true) { $self = new SplFixedArray(count($array)); if($save_indexes) { foreach($array as $key => $value) { $self[(int) $key] = $value; } } else { $i = 0; foreach (array_values($array) as $value) { $self[$i] = $value; $i++; } } return $self; } /** * @return int */ #[\ReturnTypeWillChange] public function getSize() { return $this->size; } /** * @param int $size * @return bool */ #[\ReturnTypeWillChange] public function setSize($size) { $this->size = $size; return true; } /** * @param string|int $index * @return bool */ #[\ReturnTypeWillChange] public function offsetExists($index) { return array_key_exists((int) $index, $this->internalArray); } /** * @param string|int $index * @return mixed */ #[\ReturnTypeWillChange] public function offsetGet($index) { /** @psalm-suppress MixedReturnStatement */ return $this->internalArray[(int) $index]; } /** * @param string|int $index * @param mixed $newval * @psalm-suppress MixedAssignment */ #[\ReturnTypeWillChange] public function offsetSet($index, $newval) { $this->internalArray[(int) $index] = $newval; } /** * @param string|int $index */ #[\ReturnTypeWillChange] public function offsetUnset($index) { unset($this->internalArray[(int) $index]); } /** * Rewind iterator back to the start * @link https://php.net/manual/en/splfixedarray.rewind.php * @return void * @since 5.3.0 */ #[\ReturnTypeWillChange] public function rewind() { reset($this->internalArray); } /** * Return current array entry * @link https://php.net/manual/en/splfixedarray.current.php * @return mixed The current element value. * @since 5.3.0 */ #[\ReturnTypeWillChange] public function current() { /** @psalm-suppress MixedReturnStatement */ return current($this->internalArray); } /** * Return current array index * @return int The current array index. */ #[\ReturnTypeWillChange] public function key() { return key($this->internalArray); } /** * @return void */ #[\ReturnTypeWillChange] public function next() { next($this->internalArray); } /** * Check whether the array contains more elements * @link https://php.net/manual/en/splfixedarray.valid.php * @return bool true if the array contains any more elements, false otherwise. */ #[\ReturnTypeWillChange] public function valid() { if (empty($this->internalArray)) { return false; } $result = next($this->internalArray) !== false; prev($this->internalArray); return $result; } public function __sleep() { return $this->internalArray; } /** * Do nothing. */ public function __wakeup() { // NOP } public function __serialize() { return array_values($this->internalArray); } public function __unserialize(array $data) { $length = count($data); $values = array_values($data); for ($i = 0; $i < $length; ++$i) { $this->internalArray[$i] = $values[$i]; } $this->size = $length; } } Crypto.php 0000644 00000153264 15162213653 0006554 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Crypto', false)) { return; } /** * Class ParagonIE_Sodium_Crypto * * ATTENTION! * * If you are using this library, you should be using * ParagonIE_Sodium_Compat in your code, not this class. */ abstract class ParagonIE_Sodium_Crypto { const aead_chacha20poly1305_KEYBYTES = 32; const aead_chacha20poly1305_NSECBYTES = 0; const aead_chacha20poly1305_NPUBBYTES = 8; const aead_chacha20poly1305_ABYTES = 16; const aead_chacha20poly1305_IETF_KEYBYTES = 32; const aead_chacha20poly1305_IETF_NSECBYTES = 0; const aead_chacha20poly1305_IETF_NPUBBYTES = 12; const aead_chacha20poly1305_IETF_ABYTES = 16; const aead_xchacha20poly1305_IETF_KEYBYTES = 32; const aead_xchacha20poly1305_IETF_NSECBYTES = 0; const aead_xchacha20poly1305_IETF_NPUBBYTES = 24; const aead_xchacha20poly1305_IETF_ABYTES = 16; const box_curve25519xsalsa20poly1305_SEEDBYTES = 32; const box_curve25519xsalsa20poly1305_PUBLICKEYBYTES = 32; const box_curve25519xsalsa20poly1305_SECRETKEYBYTES = 32; const box_curve25519xsalsa20poly1305_BEFORENMBYTES = 32; const box_curve25519xsalsa20poly1305_NONCEBYTES = 24; const box_curve25519xsalsa20poly1305_MACBYTES = 16; const box_curve25519xsalsa20poly1305_BOXZEROBYTES = 16; const box_curve25519xsalsa20poly1305_ZEROBYTES = 32; const onetimeauth_poly1305_BYTES = 16; const onetimeauth_poly1305_KEYBYTES = 32; const secretbox_xsalsa20poly1305_KEYBYTES = 32; const secretbox_xsalsa20poly1305_NONCEBYTES = 24; const secretbox_xsalsa20poly1305_MACBYTES = 16; const secretbox_xsalsa20poly1305_BOXZEROBYTES = 16; const secretbox_xsalsa20poly1305_ZEROBYTES = 32; const secretbox_xchacha20poly1305_KEYBYTES = 32; const secretbox_xchacha20poly1305_NONCEBYTES = 24; const secretbox_xchacha20poly1305_MACBYTES = 16; const secretbox_xchacha20poly1305_BOXZEROBYTES = 16; const secretbox_xchacha20poly1305_ZEROBYTES = 32; const stream_salsa20_KEYBYTES = 32; /** * AEAD Decryption with ChaCha20-Poly1305 * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $ad * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function aead_chacha20poly1305_decrypt( $message = '', $ad = '', $nonce = '', $key = '' ) { /** @var int $len - Length of message (ciphertext + MAC) */ $len = ParagonIE_Sodium_Core_Util::strlen($message); /** @var int $clen - Length of ciphertext */ $clen = $len - self::aead_chacha20poly1305_ABYTES; /** @var int $adlen - Length of associated data */ $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); /** @var string $mac - Message authentication code */ $mac = ParagonIE_Sodium_Core_Util::substr( $message, $clen, self::aead_chacha20poly1305_ABYTES ); /** @var string $ciphertext - The encrypted message (sans MAC) */ $ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 0, $clen); /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( 32, $nonce, $key ); /* Recalculate the Poly1305 authentication tag (MAC): */ $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); try { ParagonIE_Sodium_Compat::memzero($block0); } catch (SodiumException $ex) { $block0 = null; } $state->update($ad); $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); $state->update($ciphertext); $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); $computed_mac = $state->finish(); /* Compare the given MAC with the recalculated MAC: */ if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) { throw new SodiumException('Invalid MAC'); } // Here, we know that the MAC is valid, so we decrypt and return the plaintext return ParagonIE_Sodium_Core_ChaCha20::streamXorIc( $ciphertext, $nonce, $key, ParagonIE_Sodium_Core_Util::store64_le(1) ); } /** * AEAD Encryption with ChaCha20-Poly1305 * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $ad * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function aead_chacha20poly1305_encrypt( $message = '', $ad = '', $nonce = '', $key = '' ) { /** @var int $len - Length of the plaintext message */ $len = ParagonIE_Sodium_Core_Util::strlen($message); /** @var int $adlen - Length of the associated data */ $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( 32, $nonce, $key ); $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); try { ParagonIE_Sodium_Compat::memzero($block0); } catch (SodiumException $ex) { $block0 = null; } /** @var string $ciphertext - Raw encrypted data */ $ciphertext = ParagonIE_Sodium_Core_ChaCha20::streamXorIc( $message, $nonce, $key, ParagonIE_Sodium_Core_Util::store64_le(1) ); $state->update($ad); $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); $state->update($ciphertext); $state->update(ParagonIE_Sodium_Core_Util::store64_le($len)); return $ciphertext . $state->finish(); } /** * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $ad * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function aead_chacha20poly1305_ietf_decrypt( $message = '', $ad = '', $nonce = '', $key = '' ) { /** @var int $adlen - Length of associated data */ $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); /** @var int $len - Length of message (ciphertext + MAC) */ $len = ParagonIE_Sodium_Core_Util::strlen($message); /** @var int $clen - Length of ciphertext */ $clen = $len - self::aead_chacha20poly1305_IETF_ABYTES; /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ $block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream( 32, $nonce, $key ); /** @var string $mac - Message authentication code */ $mac = ParagonIE_Sodium_Core_Util::substr( $message, $len - self::aead_chacha20poly1305_IETF_ABYTES, self::aead_chacha20poly1305_IETF_ABYTES ); /** @var string $ciphertext - The encrypted message (sans MAC) */ $ciphertext = ParagonIE_Sodium_Core_Util::substr( $message, 0, $len - self::aead_chacha20poly1305_IETF_ABYTES ); /* Recalculate the Poly1305 authentication tag (MAC): */ $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); try { ParagonIE_Sodium_Compat::memzero($block0); } catch (SodiumException $ex) { $block0 = null; } $state->update($ad); $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf))); $state->update($ciphertext); $state->update(str_repeat("\x00", (0x10 - $clen) & 0xf)); $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); $computed_mac = $state->finish(); /* Compare the given MAC with the recalculated MAC: */ if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) { throw new SodiumException('Invalid MAC'); } // Here, we know that the MAC is valid, so we decrypt and return the plaintext return ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( $ciphertext, $nonce, $key, ParagonIE_Sodium_Core_Util::store64_le(1) ); } /** * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $ad * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function aead_chacha20poly1305_ietf_encrypt( $message = '', $ad = '', $nonce = '', $key = '' ) { /** @var int $len - Length of the plaintext message */ $len = ParagonIE_Sodium_Core_Util::strlen($message); /** @var int $adlen - Length of the associated data */ $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ $block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream( 32, $nonce, $key ); $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); try { ParagonIE_Sodium_Compat::memzero($block0); } catch (SodiumException $ex) { $block0 = null; } /** @var string $ciphertext - Raw encrypted data */ $ciphertext = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( $message, $nonce, $key, ParagonIE_Sodium_Core_Util::store64_le(1) ); $state->update($ad); $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf))); $state->update($ciphertext); $state->update(str_repeat("\x00", ((0x10 - $len) & 0xf))); $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); $state->update(ParagonIE_Sodium_Core_Util::store64_le($len)); return $ciphertext . $state->finish(); } /** * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $ad * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function aead_xchacha20poly1305_ietf_decrypt( $message = '', $ad = '', $nonce = '', $key = '' ) { $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), $key ); $nonceLast = "\x00\x00\x00\x00" . ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey); } /** * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $ad * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function aead_xchacha20poly1305_ietf_encrypt( $message = '', $ad = '', $nonce = '', $key = '' ) { $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), $key ); $nonceLast = "\x00\x00\x00\x00" . ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey); } /** * HMAC-SHA-512-256 (a.k.a. the leftmost 256 bits of HMAC-SHA-512) * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $key * @return string * @throws TypeError */ public static function auth($message, $key) { return ParagonIE_Sodium_Core_Util::substr( hash_hmac('sha512', $message, $key, true), 0, 32 ); } /** * HMAC-SHA-512-256 validation. Constant-time via hash_equals(). * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $mac * @param string $message * @param string $key * @return bool * @throws SodiumException * @throws TypeError */ public static function auth_verify($mac, $message, $key) { return ParagonIE_Sodium_Core_Util::hashEquals( $mac, self::auth($message, $key) ); } /** * X25519 key exchange followed by XSalsa20Poly1305 symmetric encryption * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $plaintext * @param string $nonce * @param string $keypair * @return string * @throws SodiumException * @throws TypeError */ public static function box($plaintext, $nonce, $keypair) { $c = self::secretbox( $plaintext, $nonce, self::box_beforenm( self::box_secretkey($keypair), self::box_publickey($keypair) ) ); return $c; } /** * X25519-XSalsa20-Poly1305 with one ephemeral X25519 keypair. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $publicKey * @return string * @throws SodiumException * @throws TypeError */ public static function box_seal($message, $publicKey) { /** @var string $ephemeralKeypair */ $ephemeralKeypair = self::box_keypair(); /** @var string $ephemeralSK */ $ephemeralSK = self::box_secretkey($ephemeralKeypair); /** @var string $ephemeralPK */ $ephemeralPK = self::box_publickey($ephemeralKeypair); /** @var string $nonce */ $nonce = self::generichash( $ephemeralPK . $publicKey, '', 24 ); /** @var string $keypair - The combined keypair used in crypto_box() */ $keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey); /** @var string $ciphertext Ciphertext + MAC from crypto_box */ $ciphertext = self::box($message, $nonce, $keypair); try { ParagonIE_Sodium_Compat::memzero($ephemeralKeypair); ParagonIE_Sodium_Compat::memzero($ephemeralSK); ParagonIE_Sodium_Compat::memzero($nonce); } catch (SodiumException $ex) { $ephemeralKeypair = null; $ephemeralSK = null; $nonce = null; } return $ephemeralPK . $ciphertext; } /** * Opens a message encrypted via box_seal(). * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $keypair * @return string * @throws SodiumException * @throws TypeError */ public static function box_seal_open($message, $keypair) { /** @var string $ephemeralPK */ $ephemeralPK = ParagonIE_Sodium_Core_Util::substr($message, 0, 32); /** @var string $ciphertext (ciphertext + MAC) */ $ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 32); /** @var string $secretKey */ $secretKey = self::box_secretkey($keypair); /** @var string $publicKey */ $publicKey = self::box_publickey($keypair); /** @var string $nonce */ $nonce = self::generichash( $ephemeralPK . $publicKey, '', 24 ); /** @var string $keypair */ $keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK); /** @var string $m */ $m = self::box_open($ciphertext, $nonce, $keypair); try { ParagonIE_Sodium_Compat::memzero($secretKey); ParagonIE_Sodium_Compat::memzero($ephemeralPK); ParagonIE_Sodium_Compat::memzero($nonce); } catch (SodiumException $ex) { $secretKey = null; $ephemeralPK = null; $nonce = null; } return $m; } /** * Used by crypto_box() to get the crypto_secretbox() key. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $sk * @param string $pk * @return string * @throws SodiumException * @throws TypeError */ public static function box_beforenm($sk, $pk) { return ParagonIE_Sodium_Core_HSalsa20::hsalsa20( str_repeat("\x00", 16), self::scalarmult($sk, $pk) ); } /** * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @return string * @throws Exception * @throws SodiumException * @throws TypeError */ public static function box_keypair() { $sKey = random_bytes(32); $pKey = self::scalarmult_base($sKey); return $sKey . $pKey; } /** * @param string $seed * @return string * @throws SodiumException * @throws TypeError */ public static function box_seed_keypair($seed) { $sKey = ParagonIE_Sodium_Core_Util::substr( hash('sha512', $seed, true), 0, 32 ); $pKey = self::scalarmult_base($sKey); return $sKey . $pKey; } /** * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $sKey * @param string $pKey * @return string * @throws TypeError */ public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) { return ParagonIE_Sodium_Core_Util::substr($sKey, 0, 32) . ParagonIE_Sodium_Core_Util::substr($pKey, 0, 32); } /** * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $keypair * @return string * @throws RangeException * @throws TypeError */ public static function box_secretkey($keypair) { if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== 64) { throw new RangeException( 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' ); } return ParagonIE_Sodium_Core_Util::substr($keypair, 0, 32); } /** * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $keypair * @return string * @throws RangeException * @throws TypeError */ public static function box_publickey($keypair) { if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { throw new RangeException( 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' ); } return ParagonIE_Sodium_Core_Util::substr($keypair, 32, 32); } /** * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $sKey * @return string * @throws RangeException * @throws SodiumException * @throws TypeError */ public static function box_publickey_from_secretkey($sKey) { if (ParagonIE_Sodium_Core_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) { throw new RangeException( 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.' ); } return self::scalarmult_base($sKey); } /** * Decrypt a message encrypted with box(). * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $ciphertext * @param string $nonce * @param string $keypair * @return string * @throws SodiumException * @throws TypeError */ public static function box_open($ciphertext, $nonce, $keypair) { return self::secretbox_open( $ciphertext, $nonce, self::box_beforenm( self::box_secretkey($keypair), self::box_publickey($keypair) ) ); } /** * Calculate a BLAKE2b hash. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string|null $key * @param int $outlen * @return string * @throws RangeException * @throws SodiumException * @throws TypeError */ public static function generichash($message, $key = '', $outlen = 32) { // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); $k = null; if (!empty($key)) { /** @var SplFixedArray $k */ $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { throw new RangeException('Invalid key size'); } } /** @var SplFixedArray $in */ $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); /** @var SplFixedArray $ctx */ $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outlen); ParagonIE_Sodium_Core_BLAKE2b::update($ctx, $in, $in->count()); /** @var SplFixedArray $out */ $out = new SplFixedArray($outlen); $out = ParagonIE_Sodium_Core_BLAKE2b::finish($ctx, $out); /** @var array<int, int> */ $outArray = $out->toArray(); return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); } /** * Finalize a BLAKE2b hashing context, returning the hash. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $ctx * @param int $outlen * @return string * @throws SodiumException * @throws TypeError */ public static function generichash_final($ctx, $outlen = 32) { if (!is_string($ctx)) { throw new TypeError('Context must be a string'); } $out = new SplFixedArray($outlen); /** @var SplFixedArray $context */ $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); /** @var SplFixedArray $out */ $out = ParagonIE_Sodium_Core_BLAKE2b::finish($context, $out); /** @var array<int, int> */ $outArray = $out->toArray(); return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); } /** * Initialize a hashing context for BLAKE2b. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $key * @param int $outputLength * @return string * @throws RangeException * @throws SodiumException * @throws TypeError */ public static function generichash_init($key = '', $outputLength = 32) { // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); $k = null; if (!empty($key)) { $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { throw new RangeException('Invalid key size'); } } /** @var SplFixedArray $ctx */ $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength); return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx); } /** * Initialize a hashing context for BLAKE2b. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $key * @param int $outputLength * @param string $salt * @param string $personal * @return string * @throws RangeException * @throws SodiumException * @throws TypeError */ public static function generichash_init_salt_personal( $key = '', $outputLength = 32, $salt = '', $personal = '' ) { // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); $k = null; if (!empty($key)) { $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { throw new RangeException('Invalid key size'); } } if (!empty($salt)) { $s = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($salt); } else { $s = null; } if (!empty($salt)) { $p = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($personal); } else { $p = null; } /** @var SplFixedArray $ctx */ $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength, $s, $p); return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx); } /** * Update a hashing context for BLAKE2b with $message * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $ctx * @param string $message * @return string * @throws SodiumException * @throws TypeError */ public static function generichash_update($ctx, $message) { // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); /** @var SplFixedArray $context */ $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); /** @var SplFixedArray $in */ $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); ParagonIE_Sodium_Core_BLAKE2b::update($context, $in, $in->count()); return ParagonIE_Sodium_Core_BLAKE2b::contextToString($context); } /** * Libsodium's crypto_kx(). * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $my_sk * @param string $their_pk * @param string $client_pk * @param string $server_pk * @return string * @throws SodiumException * @throws TypeError */ public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) { return ParagonIE_Sodium_Compat::crypto_generichash( ParagonIE_Sodium_Compat::crypto_scalarmult($my_sk, $their_pk) . $client_pk . $server_pk ); } /** * ECDH over Curve25519 * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $sKey * @param string $pKey * @return string * * @throws SodiumException * @throws TypeError */ public static function scalarmult($sKey, $pKey) { $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey); self::scalarmult_throw_if_zero($q); return $q; } /** * ECDH over Curve25519, using the basepoint. * Used to get a secret key from a public key. * * @param string $secret * @return string * * @throws SodiumException * @throws TypeError */ public static function scalarmult_base($secret) { $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base($secret); self::scalarmult_throw_if_zero($q); return $q; } /** * This throws an Error if a zero public key was passed to the function. * * @param string $q * @return void * @throws SodiumException * @throws TypeError */ protected static function scalarmult_throw_if_zero($q) { $d = 0; for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) { $d |= ParagonIE_Sodium_Core_Util::chrToInt($q[$i]); } /* branch-free variant of === 0 */ if (-(1 & (($d - 1) >> 8))) { throw new SodiumException('Zero public key is not allowed'); } } /** * XSalsa20-Poly1305 authenticated symmetric-key encryption. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $plaintext * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function secretbox($plaintext, $nonce, $key) { /** @var string $subkey */ $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); /** @var string $block0 */ $block0 = str_repeat("\x00", 32); /** @var int $mlen - Length of the plaintext message */ $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); $mlen0 = $mlen; if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) { $mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES; } $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); /** @var string $block0 */ $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( $block0, ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), $subkey ); /** @var string $c */ $c = ParagonIE_Sodium_Core_Util::substr( $block0, self::secretbox_xsalsa20poly1305_ZEROBYTES ); if ($mlen > $mlen0) { $c .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( ParagonIE_Sodium_Core_Util::substr( $plaintext, self::secretbox_xsalsa20poly1305_ZEROBYTES ), ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), 1, $subkey ); } $state = new ParagonIE_Sodium_Core_Poly1305_State( ParagonIE_Sodium_Core_Util::substr( $block0, 0, self::onetimeauth_poly1305_KEYBYTES ) ); try { ParagonIE_Sodium_Compat::memzero($block0); ParagonIE_Sodium_Compat::memzero($subkey); } catch (SodiumException $ex) { $block0 = null; $subkey = null; } $state->update($c); /** @var string $c - MAC || ciphertext */ $c = $state->finish() . $c; unset($state); return $c; } /** * Decrypt a ciphertext generated via secretbox(). * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $ciphertext * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function secretbox_open($ciphertext, $nonce, $key) { /** @var string $mac */ $mac = ParagonIE_Sodium_Core_Util::substr( $ciphertext, 0, self::secretbox_xsalsa20poly1305_MACBYTES ); /** @var string $c */ $c = ParagonIE_Sodium_Core_Util::substr( $ciphertext, self::secretbox_xsalsa20poly1305_MACBYTES ); /** @var int $clen */ $clen = ParagonIE_Sodium_Core_Util::strlen($c); /** @var string $subkey */ $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); /** @var string $block0 */ $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( 64, ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), $subkey ); $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( $mac, $c, ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) ); if (!$verified) { try { ParagonIE_Sodium_Compat::memzero($subkey); } catch (SodiumException $ex) { $subkey = null; } throw new SodiumException('Invalid MAC'); } /** @var string $m - Decrypted message */ $m = ParagonIE_Sodium_Core_Util::xorStrings( ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES), ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES) ); if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) { // We had more than 1 block, so let's continue to decrypt the rest. $m .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( ParagonIE_Sodium_Core_Util::substr( $c, self::secretbox_xsalsa20poly1305_ZEROBYTES ), ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), 1, (string) $subkey ); } return $m; } /** * XChaCha20-Poly1305 authenticated symmetric-key encryption. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $plaintext * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) { /** @var string $subkey */ $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), $key ); $nonceLast = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); /** @var string $block0 */ $block0 = str_repeat("\x00", 32); /** @var int $mlen - Length of the plaintext message */ $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); $mlen0 = $mlen; if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) { $mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES; } $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); /** @var string $block0 */ $block0 = ParagonIE_Sodium_Core_ChaCha20::streamXorIc( $block0, $nonceLast, $subkey ); /** @var string $c */ $c = ParagonIE_Sodium_Core_Util::substr( $block0, self::secretbox_xchacha20poly1305_ZEROBYTES ); if ($mlen > $mlen0) { $c .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( ParagonIE_Sodium_Core_Util::substr( $plaintext, self::secretbox_xchacha20poly1305_ZEROBYTES ), $nonceLast, $subkey, ParagonIE_Sodium_Core_Util::store64_le(1) ); } $state = new ParagonIE_Sodium_Core_Poly1305_State( ParagonIE_Sodium_Core_Util::substr( $block0, 0, self::onetimeauth_poly1305_KEYBYTES ) ); try { ParagonIE_Sodium_Compat::memzero($block0); ParagonIE_Sodium_Compat::memzero($subkey); } catch (SodiumException $ex) { $block0 = null; $subkey = null; } $state->update($c); /** @var string $c - MAC || ciphertext */ $c = $state->finish() . $c; unset($state); return $c; } /** * Decrypt a ciphertext generated via secretbox_xchacha20poly1305(). * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $ciphertext * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) { /** @var string $mac */ $mac = ParagonIE_Sodium_Core_Util::substr( $ciphertext, 0, self::secretbox_xchacha20poly1305_MACBYTES ); /** @var string $c */ $c = ParagonIE_Sodium_Core_Util::substr( $ciphertext, self::secretbox_xchacha20poly1305_MACBYTES ); /** @var int $clen */ $clen = ParagonIE_Sodium_Core_Util::strlen($c); /** @var string $subkey */ $subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20( ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), $key ); /** @var string $block0 */ $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( 64, ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), $subkey ); $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( $mac, $c, ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) ); if (!$verified) { try { ParagonIE_Sodium_Compat::memzero($subkey); } catch (SodiumException $ex) { $subkey = null; } throw new SodiumException('Invalid MAC'); } /** @var string $m - Decrypted message */ $m = ParagonIE_Sodium_Core_Util::xorStrings( ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES), ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES) ); if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) { // We had more than 1 block, so let's continue to decrypt the rest. $m .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( ParagonIE_Sodium_Core_Util::substr( $c, self::secretbox_xchacha20poly1305_ZEROBYTES ), ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), (string) $subkey, ParagonIE_Sodium_Core_Util::store64_le(1) ); } return $m; } /** * @param string $key * @return array<int, string> Returns a state and a header. * @throws Exception * @throws SodiumException */ public static function secretstream_xchacha20poly1305_init_push($key) { # randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES); $out = random_bytes(24); # crypto_core_hchacha20(state->k, out, k, NULL); $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( ParagonIE_Sodium_Core_Util::substr($out, 0, 16), $key ); $state = new ParagonIE_Sodium_Core_SecretStream_State( $subkey, ParagonIE_Sodium_Core_Util::substr($out, 16, 8) . str_repeat("\0", 4) ); # _crypto_secretstream_xchacha20poly1305_counter_reset(state); $state->counterReset(); # memcpy(STATE_INONCE(state), out + crypto_core_hchacha20_INPUTBYTES, # crypto_secretstream_xchacha20poly1305_INONCEBYTES); # memset(state->_pad, 0, sizeof state->_pad); return array( $state->toString(), $out ); } /** * @param string $key * @param string $header * @return string Returns a state. * @throws Exception */ public static function secretstream_xchacha20poly1305_init_pull($key, $header) { # crypto_core_hchacha20(state->k, in, k, NULL); $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( ParagonIE_Sodium_Core_Util::substr($header, 0, 16), $key ); $state = new ParagonIE_Sodium_Core_SecretStream_State( $subkey, ParagonIE_Sodium_Core_Util::substr($header, 16) ); $state->counterReset(); # memcpy(STATE_INONCE(state), in + crypto_core_hchacha20_INPUTBYTES, # crypto_secretstream_xchacha20poly1305_INONCEBYTES); # memset(state->_pad, 0, sizeof state->_pad); # return 0; return $state->toString(); } /** * @param string $state * @param string $msg * @param string $aad * @param int $tag * @return string * @throws SodiumException */ public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0) { $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); # crypto_onetimeauth_poly1305_state poly1305_state; # unsigned char block[64U]; # unsigned char slen[8U]; # unsigned char *c; # unsigned char *mac; $msglen = ParagonIE_Sodium_Core_Util::strlen($msg); $aadlen = ParagonIE_Sodium_Core_Util::strlen($aad); if ((($msglen + 63) >> 6) > 0xfffffffe) { throw new SodiumException( 'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes' ); } # if (outlen_p != NULL) { # *outlen_p = 0U; # } # if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) { # sodium_misuse(); # } # crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k); # crypto_onetimeauth_poly1305_init(&poly1305_state, block); # sodium_memzero(block, sizeof block); $auth = new ParagonIE_Sodium_Core_Poly1305_State( ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey()) ); # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen); $auth->update($aad); # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0, # (0x10 - adlen) & 0xf); $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf))); # memset(block, 0, sizeof block); # block[0] = tag; # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block, # state->nonce, 1U, state->k); $block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( ParagonIE_Sodium_Core_Util::intToChr($tag) . str_repeat("\0", 63), $st->getCombinedNonce(), $st->getKey(), ParagonIE_Sodium_Core_Util::store64_le(1) ); # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block); $auth->update($block); # out[0] = block[0]; $out = $block[0]; # c = out + (sizeof tag); # crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, state->nonce, 2U, state->k); $cipher = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( $msg, $st->getCombinedNonce(), $st->getKey(), ParagonIE_Sodium_Core_Util::store64_le(2) ); # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen); $auth->update($cipher); $out .= $cipher; unset($cipher); # crypto_onetimeauth_poly1305_update # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf); $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf))); # STORE64_LE(slen, (uint64_t) adlen); $slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen); # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); $auth->update($slen); # STORE64_LE(slen, (sizeof block) + mlen); $slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen); # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); $auth->update($slen); # mac = c + mlen; # crypto_onetimeauth_poly1305_final(&poly1305_state, mac); $mac = $auth->finish(); $out .= $mac; # sodium_memzero(&poly1305_state, sizeof poly1305_state); unset($auth); # XOR_BUF(STATE_INONCE(state), mac, # crypto_secretstream_xchacha20poly1305_INONCEBYTES); $st->xorNonce($mac); # sodium_increment(STATE_COUNTER(state), # crypto_secretstream_xchacha20poly1305_COUNTERBYTES); $st->incrementCounter(); // Overwrite by reference: $state = $st->toString(); /** @var bool $rekey */ $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0; # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 || # sodium_is_zero(STATE_COUNTER(state), # crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) { # crypto_secretstream_xchacha20poly1305_rekey(state); # } if ($rekey || $st->needsRekey()) { // DO REKEY self::secretstream_xchacha20poly1305_rekey($state); } # if (outlen_p != NULL) { # *outlen_p = crypto_secretstream_xchacha20poly1305_ABYTES + mlen; # } return $out; } /** * @param string $state * @param string $cipher * @param string $aad * @return bool|array{0: string, 1: int} * @throws SodiumException */ public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '') { $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); $cipherlen = ParagonIE_Sodium_Core_Util::strlen($cipher); # mlen = inlen - crypto_secretstream_xchacha20poly1305_ABYTES; $msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES; $aadlen = ParagonIE_Sodium_Core_Util::strlen($aad); # if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) { # sodium_misuse(); # } if ((($msglen + 63) >> 6) > 0xfffffffe) { throw new SodiumException( 'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes' ); } # crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k); # crypto_onetimeauth_poly1305_init(&poly1305_state, block); # sodium_memzero(block, sizeof block); $auth = new ParagonIE_Sodium_Core_Poly1305_State( ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey()) ); # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen); $auth->update($aad); # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0, # (0x10 - adlen) & 0xf); $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf))); # memset(block, 0, sizeof block); # block[0] = in[0]; # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block, # state->nonce, 1U, state->k); $block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( $cipher[0] . str_repeat("\0", 63), $st->getCombinedNonce(), $st->getKey(), ParagonIE_Sodium_Core_Util::store64_le(1) ); # tag = block[0]; # block[0] = in[0]; # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block); $tag = ParagonIE_Sodium_Core_Util::chrToInt($block[0]); $block[0] = $cipher[0]; $auth->update($block); # c = in + (sizeof tag); # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen); $auth->update(ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen)); # crypto_onetimeauth_poly1305_update # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf); $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf))); # STORE64_LE(slen, (uint64_t) adlen); # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); $slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen); $auth->update($slen); # STORE64_LE(slen, (sizeof block) + mlen); # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); $slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen); $auth->update($slen); # crypto_onetimeauth_poly1305_final(&poly1305_state, mac); # sodium_memzero(&poly1305_state, sizeof poly1305_state); $mac = $auth->finish(); # stored_mac = c + mlen; # if (sodium_memcmp(mac, stored_mac, sizeof mac) != 0) { # sodium_memzero(mac, sizeof mac); # return -1; # } $stored = ParagonIE_Sodium_Core_Util::substr($cipher, $msglen + 1, 16); if (!ParagonIE_Sodium_Core_Util::hashEquals($mac, $stored)) { return false; } # crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, state->nonce, 2U, state->k); $out = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen), $st->getCombinedNonce(), $st->getKey(), ParagonIE_Sodium_Core_Util::store64_le(2) ); # XOR_BUF(STATE_INONCE(state), mac, # crypto_secretstream_xchacha20poly1305_INONCEBYTES); $st->xorNonce($mac); # sodium_increment(STATE_COUNTER(state), # crypto_secretstream_xchacha20poly1305_COUNTERBYTES); $st->incrementCounter(); # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 || # sodium_is_zero(STATE_COUNTER(state), # crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) { # crypto_secretstream_xchacha20poly1305_rekey(state); # } // Overwrite by reference: $state = $st->toString(); /** @var bool $rekey */ $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0; if ($rekey || $st->needsRekey()) { // DO REKEY self::secretstream_xchacha20poly1305_rekey($state); } return array($out, $tag); } /** * @param string $state * @return void * @throws SodiumException */ public static function secretstream_xchacha20poly1305_rekey(&$state) { $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); # unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + # crypto_secretstream_xchacha20poly1305_INONCEBYTES]; # size_t i; # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) { # new_key_and_inonce[i] = state->k[i]; # } $new_key_and_inonce = $st->getKey(); # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) { # new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i] = # STATE_INONCE(state)[i]; # } $new_key_and_inonce .= ParagonIE_Sodium_Core_Util::substR($st->getNonce(), 0, 8); # crypto_stream_chacha20_ietf_xor(new_key_and_inonce, new_key_and_inonce, # sizeof new_key_and_inonce, # state->nonce, state->k); $st->rekey(ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( $new_key_and_inonce, $st->getCombinedNonce(), $st->getKey(), ParagonIE_Sodium_Core_Util::store64_le(0) )); # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) { # state->k[i] = new_key_and_inonce[i]; # } # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) { # STATE_INONCE(state)[i] = # new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i]; # } # _crypto_secretstream_xchacha20poly1305_counter_reset(state); $st->counterReset(); $state = $st->toString(); } /** * Detached Ed25519 signature. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $sk * @return string * @throws SodiumException * @throws TypeError */ public static function sign_detached($message, $sk) { return ParagonIE_Sodium_Core_Ed25519::sign_detached($message, $sk); } /** * Attached Ed25519 signature. (Returns a signed message.) * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $sk * @return string * @throws SodiumException * @throws TypeError */ public static function sign($message, $sk) { return ParagonIE_Sodium_Core_Ed25519::sign($message, $sk); } /** * Opens a signed message. If valid, returns the message. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $signedMessage * @param string $pk * @return string * @throws SodiumException * @throws TypeError */ public static function sign_open($signedMessage, $pk) { return ParagonIE_Sodium_Core_Ed25519::sign_open($signedMessage, $pk); } /** * Verify a detached signature of a given message and public key. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $signature * @param string $message * @param string $pk * @return bool * @throws SodiumException * @throws TypeError */ public static function sign_verify_detached($signature, $message, $pk) { return ParagonIE_Sodium_Core_Ed25519::verify_detached($signature, $message, $pk); } } Compat.php 0000644 00000501525 15162213653 0006514 0 ustar 00 <?php /** * Libsodium compatibility layer * * This is the only class you should be interfacing with, as a user of * sodium_compat. * * If the PHP extension for libsodium is installed, it will always use that * instead of our implementations. You get better performance and stronger * guarantees against side-channels that way. * * However, if your users don't have the PHP extension installed, we offer a * compatible interface here. It will give you the correct results as if the * PHP extension was installed. It won't be as fast, of course. * * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * * * * Until audited, this is probably not safe to use! DANGER WILL ROBINSON * * * * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * */ if (class_exists('ParagonIE_Sodium_Compat', false)) { return; } /** * @api */ class ParagonIE_Sodium_Compat { /** * This parameter prevents the use of the PECL extension. * It should only be used for unit testing. * * @var bool */ public static $disableFallbackForUnitTests = false; /** * Use fast multiplication rather than our constant-time multiplication * implementation. Can be enabled at runtime. Only enable this if you * are absolutely certain that there is no timing leak on your platform. * * @var bool */ public static $fastMult = false; const LIBRARY_MAJOR_VERSION = 9; const LIBRARY_MINOR_VERSION = 1; const LIBRARY_VERSION_MAJOR = 9; const LIBRARY_VERSION_MINOR = 1; const VERSION_STRING = 'polyfill-1.0.8'; // From libsodium const BASE64_VARIANT_ORIGINAL = 1; const BASE64_VARIANT_ORIGINAL_NO_PADDING = 3; const BASE64_VARIANT_URLSAFE = 5; const BASE64_VARIANT_URLSAFE_NO_PADDING = 7; const CRYPTO_AEAD_AES256GCM_KEYBYTES = 32; const CRYPTO_AEAD_AES256GCM_NSECBYTES = 0; const CRYPTO_AEAD_AES256GCM_NPUBBYTES = 12; const CRYPTO_AEAD_AES256GCM_ABYTES = 16; const CRYPTO_AEAD_AEGIS128L_KEYBYTES = 16; const CRYPTO_AEAD_AEGIS128L_NSECBYTES = 0; const CRYPTO_AEAD_AEGIS128L_NPUBBYTES = 16; const CRYPTO_AEAD_AEGIS128L_ABYTES = 32; const CRYPTO_AEAD_AEGIS256_KEYBYTES = 32; const CRYPTO_AEAD_AEGIS256_NSECBYTES = 0; const CRYPTO_AEAD_AEGIS256_NPUBBYTES = 32; const CRYPTO_AEAD_AEGIS256_ABYTES = 32; const CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES = 32; const CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES = 0; const CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES = 8; const CRYPTO_AEAD_CHACHA20POLY1305_ABYTES = 16; const CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES = 32; const CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES = 0; const CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES = 12; const CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES = 16; const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES = 32; const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NSECBYTES = 0; const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES = 24; const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES = 16; const CRYPTO_AUTH_BYTES = 32; const CRYPTO_AUTH_KEYBYTES = 32; const CRYPTO_BOX_SEALBYTES = 16; const CRYPTO_BOX_SECRETKEYBYTES = 32; const CRYPTO_BOX_PUBLICKEYBYTES = 32; const CRYPTO_BOX_KEYPAIRBYTES = 64; const CRYPTO_BOX_MACBYTES = 16; const CRYPTO_BOX_NONCEBYTES = 24; const CRYPTO_BOX_SEEDBYTES = 32; const CRYPTO_CORE_RISTRETTO255_BYTES = 32; const CRYPTO_CORE_RISTRETTO255_SCALARBYTES = 32; const CRYPTO_CORE_RISTRETTO255_HASHBYTES = 64; const CRYPTO_CORE_RISTRETTO255_NONREDUCEDSCALARBYTES = 64; const CRYPTO_KDF_BYTES_MIN = 16; const CRYPTO_KDF_BYTES_MAX = 64; const CRYPTO_KDF_CONTEXTBYTES = 8; const CRYPTO_KDF_KEYBYTES = 32; const CRYPTO_KX_BYTES = 32; const CRYPTO_KX_PRIMITIVE = 'x25519blake2b'; const CRYPTO_KX_SEEDBYTES = 32; const CRYPTO_KX_KEYPAIRBYTES = 64; const CRYPTO_KX_PUBLICKEYBYTES = 32; const CRYPTO_KX_SECRETKEYBYTES = 32; const CRYPTO_KX_SESSIONKEYBYTES = 32; const CRYPTO_GENERICHASH_BYTES = 32; const CRYPTO_GENERICHASH_BYTES_MIN = 16; const CRYPTO_GENERICHASH_BYTES_MAX = 64; const CRYPTO_GENERICHASH_KEYBYTES = 32; const CRYPTO_GENERICHASH_KEYBYTES_MIN = 16; const CRYPTO_GENERICHASH_KEYBYTES_MAX = 64; const CRYPTO_PWHASH_SALTBYTES = 16; const CRYPTO_PWHASH_STRPREFIX = '$argon2id$'; const CRYPTO_PWHASH_ALG_ARGON2I13 = 1; const CRYPTO_PWHASH_ALG_ARGON2ID13 = 2; const CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE = 33554432; const CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE = 4; const CRYPTO_PWHASH_MEMLIMIT_MODERATE = 134217728; const CRYPTO_PWHASH_OPSLIMIT_MODERATE = 6; const CRYPTO_PWHASH_MEMLIMIT_SENSITIVE = 536870912; const CRYPTO_PWHASH_OPSLIMIT_SENSITIVE = 8; const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES = 32; const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_STRPREFIX = '$7$'; const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE = 534288; const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE = 16777216; const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_SENSITIVE = 33554432; const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_SENSITIVE = 1073741824; const CRYPTO_SCALARMULT_BYTES = 32; const CRYPTO_SCALARMULT_SCALARBYTES = 32; const CRYPTO_SCALARMULT_RISTRETTO255_BYTES = 32; const CRYPTO_SCALARMULT_RISTRETTO255_SCALARBYTES = 32; const CRYPTO_SHORTHASH_BYTES = 8; const CRYPTO_SHORTHASH_KEYBYTES = 16; const CRYPTO_SECRETBOX_KEYBYTES = 32; const CRYPTO_SECRETBOX_MACBYTES = 16; const CRYPTO_SECRETBOX_NONCEBYTES = 24; const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES = 17; const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES = 24; const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES = 32; const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH = 0; const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PULL = 1; const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY = 2; const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL = 3; const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX = 0x3fffffff80; const CRYPTO_SIGN_BYTES = 64; const CRYPTO_SIGN_SEEDBYTES = 32; const CRYPTO_SIGN_PUBLICKEYBYTES = 32; const CRYPTO_SIGN_SECRETKEYBYTES = 64; const CRYPTO_SIGN_KEYPAIRBYTES = 96; const CRYPTO_STREAM_KEYBYTES = 32; const CRYPTO_STREAM_NONCEBYTES = 24; const CRYPTO_STREAM_XCHACHA20_KEYBYTES = 32; const CRYPTO_STREAM_XCHACHA20_NONCEBYTES = 24; /** * Add two numbers (little-endian unsigned), storing the value in the first * parameter. * * This mutates $val. * * @param string $val * @param string $addv * @return void * @throws SodiumException */ public static function add( #[\SensitiveParameter] &$val, #[\SensitiveParameter] $addv ) { $val_len = ParagonIE_Sodium_Core_Util::strlen($val); $addv_len = ParagonIE_Sodium_Core_Util::strlen($addv); if ($val_len !== $addv_len) { throw new SodiumException('values must have the same length'); } $A = ParagonIE_Sodium_Core_Util::stringToIntArray($val); $B = ParagonIE_Sodium_Core_Util::stringToIntArray($addv); $c = 0; for ($i = 0; $i < $val_len; $i++) { $c += ($A[$i] + $B[$i]); $A[$i] = ($c & 0xff); $c >>= 8; } $val = ParagonIE_Sodium_Core_Util::intArrayToString($A); } /** * @param string $encoded * @param int $variant * @param string $ignore * @return string * @throws SodiumException */ public static function base642bin( #[\SensitiveParameter] $encoded, $variant, $ignore = '' ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($encoded, 'string', 1); /** @var string $encoded */ $encoded = (string) $encoded; // Just strip before decoding if (!empty($ignore)) { $encoded = str_replace($ignore, '', $encoded); } try { switch ($variant) { case self::BASE64_VARIANT_ORIGINAL: return ParagonIE_Sodium_Core_Base64_Original::decode($encoded, true); case self::BASE64_VARIANT_ORIGINAL_NO_PADDING: return ParagonIE_Sodium_Core_Base64_Original::decodeNoPadding($encoded); case self::BASE64_VARIANT_URLSAFE: return ParagonIE_Sodium_Core_Base64_UrlSafe::decode($encoded, true); case self::BASE64_VARIANT_URLSAFE_NO_PADDING: return ParagonIE_Sodium_Core_Base64_UrlSafe::decodeNoPadding($encoded); default: throw new SodiumException('invalid base64 variant identifier'); } } catch (Exception $ex) { if ($ex instanceof SodiumException) { throw $ex; } throw new SodiumException('invalid base64 string', 0, $ex); } } /** * @param string $decoded * @param int $variant * @return string * @throws SodiumException */ public static function bin2base64( #[\SensitiveParameter] $decoded, $variant ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($decoded, 'string', 1); /** @var string $decoded */ $decoded = (string) $decoded; if (ParagonIE_Sodium_Core_Util::strlen($decoded) === 0) { return ''; } switch ($variant) { case self::BASE64_VARIANT_ORIGINAL: return ParagonIE_Sodium_Core_Base64_Original::encode($decoded); case self::BASE64_VARIANT_ORIGINAL_NO_PADDING: return ParagonIE_Sodium_Core_Base64_Original::encodeUnpadded($decoded); case self::BASE64_VARIANT_URLSAFE: return ParagonIE_Sodium_Core_Base64_UrlSafe::encode($decoded); case self::BASE64_VARIANT_URLSAFE_NO_PADDING: return ParagonIE_Sodium_Core_Base64_UrlSafe::encodeUnpadded($decoded); default: throw new SodiumException('invalid base64 variant identifier'); } } /** * Cache-timing-safe implementation of bin2hex(). * * @param string $string A string (probably raw binary) * @return string A hexadecimal-encoded string * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function bin2hex( #[\SensitiveParameter] $string ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1); if (self::useNewSodiumAPI()) { return (string) sodium_bin2hex($string); } if (self::use_fallback('bin2hex')) { return (string) call_user_func('\\Sodium\\bin2hex', $string); } return ParagonIE_Sodium_Core_Util::bin2hex($string); } /** * Compare two strings, in constant-time. * Compared to memcmp(), compare() is more useful for sorting. * * @param string $left The left operand; must be a string * @param string $right The right operand; must be a string * @return int If < 0 if the left operand is less than the right * If = 0 if both strings are equal * If > 0 if the right operand is less than the left * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function compare( #[\SensitiveParameter] $left, #[\SensitiveParameter] $right ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2); if (self::useNewSodiumAPI()) { return (int) sodium_compare($left, $right); } if (self::use_fallback('compare')) { return (int) call_user_func('\\Sodium\\compare', $left, $right); } return ParagonIE_Sodium_Core_Util::compare($left, $right); } /** * Authenticated Encryption with Associated Data: Decryption * * Algorithm: * AEGIS-128L * * @param string $ciphertext Encrypted message (with MAC appended) * @param string $assocData Authenticated Associated Data (unencrypted) * @param string $nonce Number to be used only Once; must be 32 bytes * @param string $key Encryption key * * @return string The original plaintext message * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ public static function crypto_aead_aegis128l_decrypt( $ciphertext = '', $assocData = '', $nonce = '', #[\SensitiveParameter] $key = '' ) { ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AEGIS128L_NPUBBYTES) { throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS128L_NPUBBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AEGIS128L_KEYBYTES) { throw new SodiumException('Key must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long'); } $ct_length = ParagonIE_Sodium_Core_Util::strlen($ciphertext); if ($ct_length < self::CRYPTO_AEAD_AEGIS128L_ABYTES) { throw new SodiumException('Message must be at least CRYPTO_AEAD_AEGIS128L_ABYTES long'); } $ct = ParagonIE_Sodium_Core_Util::substr( $ciphertext, 0, $ct_length - self::CRYPTO_AEAD_AEGIS128L_ABYTES ); $tag = ParagonIE_Sodium_Core_Util::substr( $ciphertext, $ct_length - self::CRYPTO_AEAD_AEGIS128L_ABYTES, self::CRYPTO_AEAD_AEGIS128L_ABYTES ); return ParagonIE_Sodium_Core_AEGIS128L::decrypt($ct, $tag, $assocData, $key, $nonce); } /** * Authenticated Encryption with Associated Data: Encryption * * Algorithm: * AEGIS-128L * * @param string $plaintext Message to be encrypted * @param string $assocData Authenticated Associated Data (unencrypted) * @param string $nonce Number to be used only Once; must be 32 bytes * @param string $key Encryption key * * @return string Ciphertext with 32-byte authentication tag appended * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_aead_aegis128l_encrypt( #[\SensitiveParameter] $plaintext = '', $assocData = '', $nonce = '', #[\SensitiveParameter] $key = '' ) { ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AEGIS128L_NPUBBYTES) { throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS128L_NPUBBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AEGIS128L_KEYBYTES) { throw new SodiumException('Key must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long'); } list($ct, $tag) = ParagonIE_Sodium_Core_AEGIS128L::encrypt($plaintext, $assocData, $key, $nonce); return $ct . $tag; } /** * Return a secure random key for use with the AEGIS-128L * symmetric AEAD interface. * * @return string * @throws Exception * @throws Error */ public static function crypto_aead_aegis128l_keygen() { return random_bytes(self::CRYPTO_AEAD_AEGIS128L_KEYBYTES); } /** * Authenticated Encryption with Associated Data: Decryption * * Algorithm: * AEGIS-256 * * @param string $ciphertext Encrypted message (with MAC appended) * @param string $assocData Authenticated Associated Data (unencrypted) * @param string $nonce Number to be used only Once; must be 32 bytes * @param string $key Encryption key * * @return string The original plaintext message * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ public static function crypto_aead_aegis256_decrypt( $ciphertext = '', $assocData = '', $nonce = '', #[\SensitiveParameter] $key = '' ) { ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AEGIS256_NPUBBYTES) { throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS256_NPUBBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AEGIS256_KEYBYTES) { throw new SodiumException('Key must be CRYPTO_AEAD_AEGIS256_KEYBYTES long'); } $ct_length = ParagonIE_Sodium_Core_Util::strlen($ciphertext); if ($ct_length < self::CRYPTO_AEAD_AEGIS256_ABYTES) { throw new SodiumException('Message must be at least CRYPTO_AEAD_AEGIS256_ABYTES long'); } $ct = ParagonIE_Sodium_Core_Util::substr( $ciphertext, 0, $ct_length - self::CRYPTO_AEAD_AEGIS256_ABYTES ); $tag = ParagonIE_Sodium_Core_Util::substr( $ciphertext, $ct_length - self::CRYPTO_AEAD_AEGIS256_ABYTES, self::CRYPTO_AEAD_AEGIS256_ABYTES ); return ParagonIE_Sodium_Core_AEGIS256::decrypt($ct, $tag, $assocData, $key, $nonce); } /** * Authenticated Encryption with Associated Data: Encryption * * Algorithm: * AEGIS-256 * * @param string $plaintext Message to be encrypted * @param string $assocData Authenticated Associated Data (unencrypted) * @param string $nonce Number to be used only Once; must be 32 bytes * @param string $key Encryption key * * @return string Ciphertext with 32-byte authentication tag appended * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_aead_aegis256_encrypt( #[\SensitiveParameter] $plaintext = '', $assocData = '', $nonce = '', #[\SensitiveParameter] $key = '' ) { ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AEGIS256_NPUBBYTES) { throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS256_NPUBBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AEGIS256_KEYBYTES) { throw new SodiumException('Key must be CRYPTO_AEAD_AEGIS256_KEYBYTES long'); } list($ct, $tag) = ParagonIE_Sodium_Core_AEGIS256::encrypt($plaintext, $assocData, $key, $nonce); return $ct . $tag; } /** * Return a secure random key for use with the AEGIS-256 * symmetric AEAD interface. * * @return string * @throws Exception * @throws Error */ public static function crypto_aead_aegis256_keygen() { return random_bytes(self::CRYPTO_AEAD_AEGIS256_KEYBYTES); } /** * Is AES-256-GCM even available to use? * * @return bool * @psalm-suppress UndefinedFunction * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ public static function crypto_aead_aes256gcm_is_available() { if (self::useNewSodiumAPI()) { return sodium_crypto_aead_aes256gcm_is_available(); } if (self::use_fallback('crypto_aead_aes256gcm_is_available')) { return call_user_func('\\Sodium\\crypto_aead_aes256gcm_is_available'); } if (PHP_VERSION_ID < 70100) { // OpenSSL doesn't support AEAD before 7.1.0 return false; } if (!extension_loaded('openssl')) { return false; } if (!is_callable('openssl_encrypt') || !is_callable('openssl_decrypt')) { // OpenSSL isn't installed return false; } return (bool) in_array('aes-256-gcm', openssl_get_cipher_methods()); } /** * Authenticated Encryption with Associated Data: Decryption * * Algorithm: * AES-256-GCM * * This mode uses a 64-bit random nonce with a 64-bit counter. * IETF mode uses a 96-bit random nonce with a 32-bit counter. * * @param string $ciphertext Encrypted message (with Poly1305 MAC appended) * @param string $assocData Authenticated Associated Data (unencrypted) * @param string $nonce Number to be used only Once; must be 8 bytes * @param string $key Encryption key * * @return string|bool The original plaintext message * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ public static function crypto_aead_aes256gcm_decrypt( $ciphertext = '', $assocData = '', $nonce = '', #[\SensitiveParameter] $key = '' ) { if (!self::crypto_aead_aes256gcm_is_available()) { throw new SodiumException('AES-256-GCM is not available'); } ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AES256GCM_NPUBBYTES) { throw new SodiumException('Nonce must be CRYPTO_AEAD_AES256GCM_NPUBBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AES256GCM_KEYBYTES) { throw new SodiumException('Key must be CRYPTO_AEAD_AES256GCM_KEYBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_AES256GCM_ABYTES) { throw new SodiumException('Message must be at least CRYPTO_AEAD_AES256GCM_ABYTES long'); } if (!extension_loaded('openssl')) { throw new SodiumException('The OpenSSL extension is not installed'); } if (!is_callable('openssl_decrypt')) { throw new SodiumException('The OpenSSL extension is not installed, or openssl_decrypt() is not available'); } /** @var string $ctext */ $ctext = ParagonIE_Sodium_Core_Util::substr($ciphertext, 0, -self::CRYPTO_AEAD_AES256GCM_ABYTES); /** @var string $authTag */ $authTag = ParagonIE_Sodium_Core_Util::substr($ciphertext, -self::CRYPTO_AEAD_AES256GCM_ABYTES, 16); return openssl_decrypt( $ctext, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $nonce, $authTag, $assocData ); } /** * Authenticated Encryption with Associated Data: Encryption * * Algorithm: * AES-256-GCM * * @param string $plaintext Message to be encrypted * @param string $assocData Authenticated Associated Data (unencrypted) * @param string $nonce Number to be used only Once; must be 8 bytes * @param string $key Encryption key * * @return string Ciphertext with a 16-byte GCM message * authentication code appended * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_aead_aes256gcm_encrypt( #[\SensitiveParameter] $plaintext = '', $assocData = '', $nonce = '', #[\SensitiveParameter] $key = '' ) { if (!self::crypto_aead_aes256gcm_is_available()) { throw new SodiumException('AES-256-GCM is not available'); } ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AES256GCM_NPUBBYTES) { throw new SodiumException('Nonce must be CRYPTO_AEAD_AES256GCM_NPUBBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AES256GCM_KEYBYTES) { throw new SodiumException('Key must be CRYPTO_AEAD_AES256GCM_KEYBYTES long'); } if (!extension_loaded('openssl')) { throw new SodiumException('The OpenSSL extension is not installed'); } if (!is_callable('openssl_encrypt')) { throw new SodiumException('The OpenSSL extension is not installed, or openssl_encrypt() is not available'); } $authTag = ''; $ciphertext = openssl_encrypt( $plaintext, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $nonce, $authTag, $assocData ); return $ciphertext . $authTag; } /** * Return a secure random key for use with the AES-256-GCM * symmetric AEAD interface. * * @return string * @throws Exception * @throws Error */ public static function crypto_aead_aes256gcm_keygen() { return random_bytes(self::CRYPTO_AEAD_AES256GCM_KEYBYTES); } /** * Authenticated Encryption with Associated Data: Decryption * * Algorithm: * ChaCha20-Poly1305 * * This mode uses a 64-bit random nonce with a 64-bit counter. * IETF mode uses a 96-bit random nonce with a 32-bit counter. * * @param string $ciphertext Encrypted message (with Poly1305 MAC appended) * @param string $assocData Authenticated Associated Data (unencrypted) * @param string $nonce Number to be used only Once; must be 8 bytes * @param string $key Encryption key * * @return string The original plaintext message * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ public static function crypto_aead_chacha20poly1305_decrypt( $ciphertext = '', $assocData = '', $nonce = '', #[\SensitiveParameter] $key = '' ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES) { throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) { throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES) { throw new SodiumException('Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_ABYTES long'); } if (self::useNewSodiumAPI()) { /** * @psalm-suppress InvalidReturnStatement * @psalm-suppress FalsableReturnStatement */ return sodium_crypto_aead_chacha20poly1305_decrypt( $ciphertext, $assocData, $nonce, $key ); } if (self::use_fallback('crypto_aead_chacha20poly1305_decrypt')) { return call_user_func( '\\Sodium\\crypto_aead_chacha20poly1305_decrypt', $ciphertext, $assocData, $nonce, $key ); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_decrypt( $ciphertext, $assocData, $nonce, $key ); } return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_decrypt( $ciphertext, $assocData, $nonce, $key ); } /** * Authenticated Encryption with Associated Data * * Algorithm: * ChaCha20-Poly1305 * * This mode uses a 64-bit random nonce with a 64-bit counter. * IETF mode uses a 96-bit random nonce with a 32-bit counter. * * @param string $plaintext Message to be encrypted * @param string $assocData Authenticated Associated Data (unencrypted) * @param string $nonce Number to be used only Once; must be 8 bytes * @param string $key Encryption key * * @return string Ciphertext with a 16-byte Poly1305 message * authentication code appended * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_aead_chacha20poly1305_encrypt( #[\SensitiveParameter] $plaintext = '', $assocData = '', $nonce = '', #[\SensitiveParameter] $key = '' ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES) { throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) { throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES long'); } if (self::useNewSodiumAPI()) { return (string) sodium_crypto_aead_chacha20poly1305_encrypt( $plaintext, $assocData, $nonce, $key ); } if (self::use_fallback('crypto_aead_chacha20poly1305_encrypt')) { return (string) call_user_func( '\\Sodium\\crypto_aead_chacha20poly1305_encrypt', $plaintext, $assocData, $nonce, $key ); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_encrypt( $plaintext, $assocData, $nonce, $key ); } return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_encrypt( $plaintext, $assocData, $nonce, $key ); } /** * Authenticated Encryption with Associated Data: Decryption * * Algorithm: * ChaCha20-Poly1305 * * IETF mode uses a 96-bit random nonce with a 32-bit counter. * Regular mode uses a 64-bit random nonce with a 64-bit counter. * * @param string $ciphertext Encrypted message (with Poly1305 MAC appended) * @param string $assocData Authenticated Associated Data (unencrypted) * @param string $nonce Number to be used only Once; must be 12 bytes * @param string $key Encryption key * * @return string The original plaintext message * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ public static function crypto_aead_chacha20poly1305_ietf_decrypt( $ciphertext = '', $assocData = '', $nonce = '', #[\SensitiveParameter] $key = '' ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES) { throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) { throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES) { throw new SodiumException('Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES long'); } if (self::useNewSodiumAPI()) { /** * @psalm-suppress InvalidReturnStatement * @psalm-suppress FalsableReturnStatement */ return sodium_crypto_aead_chacha20poly1305_ietf_decrypt( $ciphertext, $assocData, $nonce, $key ); } if (self::use_fallback('crypto_aead_chacha20poly1305_ietf_decrypt')) { return call_user_func( '\\Sodium\\crypto_aead_chacha20poly1305_ietf_decrypt', $ciphertext, $assocData, $nonce, $key ); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_ietf_decrypt( $ciphertext, $assocData, $nonce, $key ); } return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_ietf_decrypt( $ciphertext, $assocData, $nonce, $key ); } /** * Return a secure random key for use with the ChaCha20-Poly1305 * symmetric AEAD interface. * * @return string * @throws Exception * @throws Error */ public static function crypto_aead_chacha20poly1305_keygen() { return random_bytes(self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES); } /** * Authenticated Encryption with Associated Data * * Algorithm: * ChaCha20-Poly1305 * * IETF mode uses a 96-bit random nonce with a 32-bit counter. * Regular mode uses a 64-bit random nonce with a 64-bit counter. * * @param string $plaintext Message to be encrypted * @param string $assocData Authenticated Associated Data (unencrypted) * @param string $nonce Number to be used only Once; must be 8 bytes * @param string $key Encryption key * * @return string Ciphertext with a 16-byte Poly1305 message * authentication code appended * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_aead_chacha20poly1305_ietf_encrypt( #[\SensitiveParameter] $plaintext = '', $assocData = '', $nonce = '', #[\SensitiveParameter] $key = '' ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); if (!is_null($assocData)) { ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); } ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES) { throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) { throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long'); } if (self::useNewSodiumAPI()) { return (string) sodium_crypto_aead_chacha20poly1305_ietf_encrypt( $plaintext, $assocData, $nonce, $key ); } if (self::use_fallback('crypto_aead_chacha20poly1305_ietf_encrypt')) { return (string) call_user_func( '\\Sodium\\crypto_aead_chacha20poly1305_ietf_encrypt', $plaintext, $assocData, $nonce, $key ); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_ietf_encrypt( $plaintext, $assocData, $nonce, $key ); } return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_ietf_encrypt( $plaintext, $assocData, $nonce, $key ); } /** * Return a secure random key for use with the ChaCha20-Poly1305 * symmetric AEAD interface. (IETF version) * * @return string * @throws Exception * @throws Error */ public static function crypto_aead_chacha20poly1305_ietf_keygen() { return random_bytes(self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES); } /** * Authenticated Encryption with Associated Data: Decryption * * Algorithm: * XChaCha20-Poly1305 * * This mode uses a 64-bit random nonce with a 64-bit counter. * IETF mode uses a 96-bit random nonce with a 32-bit counter. * * @param string $ciphertext Encrypted message (with Poly1305 MAC appended) * @param string $assocData Authenticated Associated Data (unencrypted) * @param string $nonce Number to be used only Once; must be 8 bytes * @param string $key Encryption key * @param bool $dontFallback Don't fallback to ext/sodium * * @return string|bool The original plaintext message * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_aead_xchacha20poly1305_ietf_decrypt( $ciphertext = '', $assocData = '', $nonce = '', #[\SensitiveParameter] $key = '', $dontFallback = false ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); if (!is_null($assocData)) { ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); } else { $assocData = ''; } ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES) { throw new SodiumException('Nonce must be CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES) { throw new SodiumException('Key must be CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES) { throw new SodiumException('Message must be at least CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES long'); } if (self::useNewSodiumAPI() && !$dontFallback) { if (is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_decrypt')) { return sodium_crypto_aead_xchacha20poly1305_ietf_decrypt( $ciphertext, $assocData, $nonce, $key ); } } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::aead_xchacha20poly1305_ietf_decrypt( $ciphertext, $assocData, $nonce, $key ); } return ParagonIE_Sodium_Crypto::aead_xchacha20poly1305_ietf_decrypt( $ciphertext, $assocData, $nonce, $key ); } /** * Authenticated Encryption with Associated Data * * Algorithm: * XChaCha20-Poly1305 * * This mode uses a 64-bit random nonce with a 64-bit counter. * IETF mode uses a 96-bit random nonce with a 32-bit counter. * * @param string $plaintext Message to be encrypted * @param string $assocData Authenticated Associated Data (unencrypted) * @param string $nonce Number to be used only Once; must be 8 bytes * @param string $key Encryption key * @param bool $dontFallback Don't fallback to ext/sodium * * @return string Ciphertext with a 16-byte Poly1305 message * authentication code appended * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_aead_xchacha20poly1305_ietf_encrypt( #[\SensitiveParameter] $plaintext = '', $assocData = '', $nonce = '', #[\SensitiveParameter] $key = '', $dontFallback = false ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); if (!is_null($assocData)) { ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2); } else { $assocData = ''; } ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES) { throw new SodiumException('Nonce must be CRYPTO_AEAD_XCHACHA20POLY1305_NPUBBYTES long'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES) { throw new SodiumException('Key must be CRYPTO_AEAD_XCHACHA20POLY1305_KEYBYTES long'); } if (self::useNewSodiumAPI() && !$dontFallback) { if (is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_encrypt')) { return sodium_crypto_aead_xchacha20poly1305_ietf_encrypt( $plaintext, $assocData, $nonce, $key ); } } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::aead_xchacha20poly1305_ietf_encrypt( $plaintext, $assocData, $nonce, $key ); } return ParagonIE_Sodium_Crypto::aead_xchacha20poly1305_ietf_encrypt( $plaintext, $assocData, $nonce, $key ); } /** * Return a secure random key for use with the XChaCha20-Poly1305 * symmetric AEAD interface. * * @return string * @throws Exception * @throws Error */ public static function crypto_aead_xchacha20poly1305_ietf_keygen() { return random_bytes(self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES); } /** * Authenticate a message. Uses symmetric-key cryptography. * * Algorithm: * HMAC-SHA512-256. Which is HMAC-SHA-512 truncated to 256 bits. * Not to be confused with HMAC-SHA-512/256 which would use the * SHA-512/256 hash function (uses different initial parameters * but still truncates to 256 bits to sidestep length-extension * attacks). * * @param string $message Message to be authenticated * @param string $key Symmetric authentication key * @return string Message authentication code * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_auth( $message, #[\SensitiveParameter] $key ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AUTH_KEYBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_AUTH_KEYBYTES long.'); } if (self::useNewSodiumAPI()) { return (string) sodium_crypto_auth($message, $key); } if (self::use_fallback('crypto_auth')) { return (string) call_user_func('\\Sodium\\crypto_auth', $message, $key); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::auth($message, $key); } return ParagonIE_Sodium_Crypto::auth($message, $key); } /** * @return string * @throws Exception * @throws Error */ public static function crypto_auth_keygen() { return random_bytes(self::CRYPTO_AUTH_KEYBYTES); } /** * Verify the MAC of a message previously authenticated with crypto_auth. * * @param string $mac Message authentication code * @param string $message Message whose authenticity you are attempting to * verify (with a given MAC and key) * @param string $key Symmetric authentication key * @return bool TRUE if authenticated, FALSE otherwise * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_auth_verify( $mac, $message, #[\SensitiveParameter] $key ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($mac, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($mac) !== self::CRYPTO_AUTH_BYTES) { throw new SodiumException('Argument 1 must be CRYPTO_AUTH_BYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AUTH_KEYBYTES) { throw new SodiumException('Argument 3 must be CRYPTO_AUTH_KEYBYTES long.'); } if (self::useNewSodiumAPI()) { return (bool) sodium_crypto_auth_verify($mac, $message, $key); } if (self::use_fallback('crypto_auth_verify')) { return (bool) call_user_func('\\Sodium\\crypto_auth_verify', $mac, $message, $key); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::auth_verify($mac, $message, $key); } return ParagonIE_Sodium_Crypto::auth_verify($mac, $message, $key); } /** * Authenticated asymmetric-key encryption. Both the sender and recipient * may decrypt messages. * * Algorithm: X25519-XSalsa20-Poly1305. * X25519: Elliptic-Curve Diffie Hellman over Curve25519. * XSalsa20: Extended-nonce variant of salsa20. * Poyl1305: Polynomial MAC for one-time message authentication. * * @param string $plaintext The message to be encrypted * @param string $nonce A Number to only be used Once; must be 24 bytes * @param string $keypair Your secret key and your recipient's public key * @return string Ciphertext with 16-byte Poly1305 MAC * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_box( $plaintext, $nonce, #[\SensitiveParameter] $keypair ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 3); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_BOX_NONCEBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_BOX_NONCEBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) { throw new SodiumException('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES long.'); } if (self::useNewSodiumAPI()) { return (string) sodium_crypto_box($plaintext, $nonce, $keypair); } if (self::use_fallback('crypto_box')) { return (string) call_user_func('\\Sodium\\crypto_box', $plaintext, $nonce, $keypair); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::box($plaintext, $nonce, $keypair); } return ParagonIE_Sodium_Crypto::box($plaintext, $nonce, $keypair); } /** * Anonymous public-key encryption. Only the recipient may decrypt messages. * * Algorithm: X25519-XSalsa20-Poly1305, as with crypto_box. * The sender's X25519 keypair is ephemeral. * Nonce is generated from the BLAKE2b hash of both public keys. * * This provides ciphertext integrity. * * @param string $plaintext Message to be sealed * @param string $publicKey Your recipient's public key * @return string Sealed message that only your recipient can * decrypt * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_box_seal( #[\SensitiveParameter] $plaintext, $publicKey ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_BOX_PUBLICKEYBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.'); } if (self::useNewSodiumAPI()) { return (string) sodium_crypto_box_seal($plaintext, $publicKey); } if (self::use_fallback('crypto_box_seal')) { return (string) call_user_func('\\Sodium\\crypto_box_seal', $plaintext, $publicKey); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::box_seal($plaintext, $publicKey); } return ParagonIE_Sodium_Crypto::box_seal($plaintext, $publicKey); } /** * Opens a message encrypted with crypto_box_seal(). Requires * the recipient's keypair (sk || pk) to decrypt successfully. * * This validates ciphertext integrity. * * @param string $ciphertext Sealed message to be opened * @param string $keypair Your crypto_box keypair * @return string The original plaintext message * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ public static function crypto_box_seal_open( $ciphertext, #[\SensitiveParameter] $keypair ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 2); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_BOX_KEYPAIRBYTES long.'); } if (self::useNewSodiumAPI()) { /** * @psalm-suppress InvalidReturnStatement * @psalm-suppress FalsableReturnStatement */ return sodium_crypto_box_seal_open($ciphertext, $keypair); } if (self::use_fallback('crypto_box_seal_open')) { return call_user_func('\\Sodium\\crypto_box_seal_open', $ciphertext, $keypair); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::box_seal_open($ciphertext, $keypair); } return ParagonIE_Sodium_Crypto::box_seal_open($ciphertext, $keypair); } /** * Generate a new random X25519 keypair. * * @return string A 64-byte string; the first 32 are your secret key, while * the last 32 are your public key. crypto_box_secretkey() * and crypto_box_publickey() exist to separate them so you * don't accidentally get them mixed up! * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_box_keypair() { if (self::useNewSodiumAPI()) { return (string) sodium_crypto_box_keypair(); } if (self::use_fallback('crypto_box_keypair')) { return (string) call_user_func('\\Sodium\\crypto_box_keypair'); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::box_keypair(); } return ParagonIE_Sodium_Crypto::box_keypair(); } /** * Combine two keys into a keypair for use in library methods that expect * a keypair. This doesn't necessarily have to be the same person's keys. * * @param string $secretKey Secret key * @param string $publicKey Public key * @return string Keypair * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_box_keypair_from_secretkey_and_publickey( #[\SensitiveParameter] $secretKey, $publicKey ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) { throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_BOX_PUBLICKEYBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.'); } if (self::useNewSodiumAPI()) { return (string) sodium_crypto_box_keypair_from_secretkey_and_publickey($secretKey, $publicKey); } if (self::use_fallback('crypto_box_keypair_from_secretkey_and_publickey')) { return (string) call_user_func('\\Sodium\\crypto_box_keypair_from_secretkey_and_publickey', $secretKey, $publicKey); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::box_keypair_from_secretkey_and_publickey($secretKey, $publicKey); } return ParagonIE_Sodium_Crypto::box_keypair_from_secretkey_and_publickey($secretKey, $publicKey); } /** * Decrypt a message previously encrypted with crypto_box(). * * @param string $ciphertext Encrypted message * @param string $nonce Number to only be used Once; must be 24 bytes * @param string $keypair Your secret key and the sender's public key * @return string The original plaintext message * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ public static function crypto_box_open( $ciphertext, $nonce, #[\SensitiveParameter] $keypair ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 3); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_BOX_MACBYTES) { throw new SodiumException('Argument 1 must be at least CRYPTO_BOX_MACBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_BOX_NONCEBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_BOX_NONCEBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) { throw new SodiumException('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES long.'); } if (self::useNewSodiumAPI()) { /** * @psalm-suppress InvalidReturnStatement * @psalm-suppress FalsableReturnStatement */ return sodium_crypto_box_open($ciphertext, $nonce, $keypair); } if (self::use_fallback('crypto_box_open')) { return call_user_func('\\Sodium\\crypto_box_open', $ciphertext, $nonce, $keypair); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::box_open($ciphertext, $nonce, $keypair); } return ParagonIE_Sodium_Crypto::box_open($ciphertext, $nonce, $keypair); } /** * Extract the public key from a crypto_box keypair. * * @param string $keypair Keypair containing secret and public key * @return string Your crypto_box public key * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_box_publickey( #[\SensitiveParameter] $keypair ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) { throw new SodiumException('Argument 1 must be CRYPTO_BOX_KEYPAIRBYTES long.'); } if (self::useNewSodiumAPI()) { return (string) sodium_crypto_box_publickey($keypair); } if (self::use_fallback('crypto_box_publickey')) { return (string) call_user_func('\\Sodium\\crypto_box_publickey', $keypair); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::box_publickey($keypair); } return ParagonIE_Sodium_Crypto::box_publickey($keypair); } /** * Calculate the X25519 public key from a given X25519 secret key. * * @param string $secretKey Any X25519 secret key * @return string The corresponding X25519 public key * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_box_publickey_from_secretkey( #[\SensitiveParameter] $secretKey ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) { throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.'); } if (self::useNewSodiumAPI()) { return (string) sodium_crypto_box_publickey_from_secretkey($secretKey); } if (self::use_fallback('crypto_box_publickey_from_secretkey')) { return (string) call_user_func('\\Sodium\\crypto_box_publickey_from_secretkey', $secretKey); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::box_publickey_from_secretkey($secretKey); } return ParagonIE_Sodium_Crypto::box_publickey_from_secretkey($secretKey); } /** * Extract the secret key from a crypto_box keypair. * * @param string $keypair * @return string Your crypto_box secret key * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_box_secretkey( #[\SensitiveParameter] $keypair ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) { throw new SodiumException('Argument 1 must be CRYPTO_BOX_KEYPAIRBYTES long.'); } if (self::useNewSodiumAPI()) { return (string) sodium_crypto_box_secretkey($keypair); } if (self::use_fallback('crypto_box_secretkey')) { return (string) call_user_func('\\Sodium\\crypto_box_secretkey', $keypair); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::box_secretkey($keypair); } return ParagonIE_Sodium_Crypto::box_secretkey($keypair); } /** * Generate an X25519 keypair from a seed. * * @param string $seed * @return string * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress UndefinedFunction */ public static function crypto_box_seed_keypair( #[\SensitiveParameter] $seed ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1); if (self::useNewSodiumAPI()) { return (string) sodium_crypto_box_seed_keypair($seed); } if (self::use_fallback('crypto_box_seed_keypair')) { return (string) call_user_func('\\Sodium\\crypto_box_seed_keypair', $seed); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::box_seed_keypair($seed); } return ParagonIE_Sodium_Crypto::box_seed_keypair($seed); } /** * Calculates a BLAKE2b hash, with an optional key. * * @param string $message The message to be hashed * @param string|null $key If specified, must be a string between 16 * and 64 bytes long * @param int $length Output length in bytes; must be between 16 * and 64 (default = 32) * @return string Raw binary * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_generichash( $message, #[\SensitiveParameter] $key = '', $length = self::CRYPTO_GENERICHASH_BYTES ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); if (is_null($key)) { $key = ''; } ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 3); /* Input validation: */ if (!empty($key)) { if (ParagonIE_Sodium_Core_Util::strlen($key) < self::CRYPTO_GENERICHASH_KEYBYTES_MIN) { throw new SodiumException('Unsupported key size. Must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes long.'); } if (ParagonIE_Sodium_Core_Util::strlen($key) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX) { throw new SodiumException('Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.'); } } if (self::useNewSodiumAPI()) { return (string) sodium_crypto_generichash($message, $key, $length); } if (self::use_fallback('crypto_generichash')) { return (string) call_user_func('\\Sodium\\crypto_generichash', $message, $key, $length); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::generichash($message, $key, $length); } return ParagonIE_Sodium_Crypto::generichash($message, $key, $length); } /** * Get the final BLAKE2b hash output for a given context. * * @param string $ctx BLAKE2 hashing context. Generated by crypto_generichash_init(). * @param int $length Hash output size. * @return string Final BLAKE2b hash. * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress ReferenceConstraintViolation * @psalm-suppress ConflictingReferenceConstraint */ public static function crypto_generichash_final( #[\SensitiveParameter] &$ctx, $length = self::CRYPTO_GENERICHASH_BYTES ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($ctx, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2); if (self::useNewSodiumAPI()) { return sodium_crypto_generichash_final($ctx, $length); } if (self::use_fallback('crypto_generichash_final')) { $func = '\\Sodium\\crypto_generichash_final'; return (string) $func($ctx, $length); } if ($length < 1) { try { self::memzero($ctx); } catch (SodiumException $ex) { unset($ctx); } return ''; } if (PHP_INT_SIZE === 4) { $result = ParagonIE_Sodium_Crypto32::generichash_final($ctx, $length); } else { $result = ParagonIE_Sodium_Crypto::generichash_final($ctx, $length); } try { self::memzero($ctx); } catch (SodiumException $ex) { unset($ctx); } return $result; } /** * Initialize a BLAKE2b hashing context, for use in a streaming interface. * * @param string|null $key If specified must be a string between 16 and 64 bytes * @param int $length The size of the desired hash output * @return string A BLAKE2 hashing context, encoded as a string * (To be 100% compatible with ext/libsodium) * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_generichash_init( #[\SensitiveParameter] $key = '', $length = self::CRYPTO_GENERICHASH_BYTES ) { /* Type checks: */ if (is_null($key)) { $key = ''; } ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2); /* Input validation: */ if (!empty($key)) { if (ParagonIE_Sodium_Core_Util::strlen($key) < self::CRYPTO_GENERICHASH_KEYBYTES_MIN) { throw new SodiumException('Unsupported key size. Must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes long.'); } if (ParagonIE_Sodium_Core_Util::strlen($key) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX) { throw new SodiumException('Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.'); } } if (self::useNewSodiumAPI()) { return sodium_crypto_generichash_init($key, $length); } if (self::use_fallback('crypto_generichash_init')) { return (string) call_user_func('\\Sodium\\crypto_generichash_init', $key, $length); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::generichash_init($key, $length); } return ParagonIE_Sodium_Crypto::generichash_init($key, $length); } /** * Initialize a BLAKE2b hashing context, for use in a streaming interface. * * @param string|null $key If specified must be a string between 16 and 64 bytes * @param int $length The size of the desired hash output * @param string $salt Salt (up to 16 bytes) * @param string $personal Personalization string (up to 16 bytes) * @return string A BLAKE2 hashing context, encoded as a string * (To be 100% compatible with ext/libsodium) * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_generichash_init_salt_personal( #[\SensitiveParameter] $key = '', $length = self::CRYPTO_GENERICHASH_BYTES, $salt = '', $personal = '' ) { /* Type checks: */ if (is_null($key)) { $key = ''; } ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2); ParagonIE_Sodium_Core_Util::declareScalarType($salt, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($personal, 'string', 4); $salt = str_pad($salt, 16, "\0", STR_PAD_RIGHT); $personal = str_pad($personal, 16, "\0", STR_PAD_RIGHT); /* Input validation: */ if (!empty($key)) { /* if (ParagonIE_Sodium_Core_Util::strlen($key) < self::CRYPTO_GENERICHASH_KEYBYTES_MIN) { throw new SodiumException('Unsupported key size. Must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes long.'); } */ if (ParagonIE_Sodium_Core_Util::strlen($key) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX) { throw new SodiumException('Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.'); } } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::generichash_init_salt_personal($key, $length, $salt, $personal); } return ParagonIE_Sodium_Crypto::generichash_init_salt_personal($key, $length, $salt, $personal); } /** * Update a BLAKE2b hashing context with additional data. * * @param string $ctx BLAKE2 hashing context. Generated by crypto_generichash_init(). * $ctx is passed by reference and gets updated in-place. * @param-out string $ctx * @param string $message The message to append to the existing hash state. * @return void * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress ReferenceConstraintViolation */ public static function crypto_generichash_update( #[\SensitiveParameter] &$ctx, $message ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($ctx, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2); if (self::useNewSodiumAPI()) { sodium_crypto_generichash_update($ctx, $message); return; } if (self::use_fallback('crypto_generichash_update')) { $func = '\\Sodium\\crypto_generichash_update'; $func($ctx, $message); return; } if (PHP_INT_SIZE === 4) { $ctx = ParagonIE_Sodium_Crypto32::generichash_update($ctx, $message); } else { $ctx = ParagonIE_Sodium_Crypto::generichash_update($ctx, $message); } } /** * @return string * @throws Exception * @throws Error */ public static function crypto_generichash_keygen() { return random_bytes(self::CRYPTO_GENERICHASH_KEYBYTES); } /** * @param int $subkey_len * @param int $subkey_id * @param string $context * @param string $key * @return string * @throws SodiumException */ public static function crypto_kdf_derive_from_key( $subkey_len, $subkey_id, $context, #[\SensitiveParameter] $key ) { ParagonIE_Sodium_Core_Util::declareScalarType($subkey_len, 'int', 1); ParagonIE_Sodium_Core_Util::declareScalarType($subkey_id, 'int', 2); ParagonIE_Sodium_Core_Util::declareScalarType($context, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); $subkey_id = (int) $subkey_id; $subkey_len = (int) $subkey_len; $context = (string) $context; $key = (string) $key; if ($subkey_len < self::CRYPTO_KDF_BYTES_MIN) { throw new SodiumException('subkey cannot be smaller than SODIUM_CRYPTO_KDF_BYTES_MIN'); } if ($subkey_len > self::CRYPTO_KDF_BYTES_MAX) { throw new SodiumException('subkey cannot be larger than SODIUM_CRYPTO_KDF_BYTES_MAX'); } if ($subkey_id < 0) { throw new SodiumException('subkey_id cannot be negative'); } if (ParagonIE_Sodium_Core_Util::strlen($context) !== self::CRYPTO_KDF_CONTEXTBYTES) { throw new SodiumException('context should be SODIUM_CRYPTO_KDF_CONTEXTBYTES bytes'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_KDF_KEYBYTES) { throw new SodiumException('key should be SODIUM_CRYPTO_KDF_KEYBYTES bytes'); } $salt = ParagonIE_Sodium_Core_Util::store64_le($subkey_id); $state = self::crypto_generichash_init_salt_personal( $key, $subkey_len, $salt, $context ); return self::crypto_generichash_final($state, $subkey_len); } /** * @return string * @throws Exception * @throws Error */ public static function crypto_kdf_keygen() { return random_bytes(self::CRYPTO_KDF_KEYBYTES); } /** * Perform a key exchange, between a designated client and a server. * * Typically, you would designate one machine to be the client and the * other to be the server. The first two keys are what you'd expect for * scalarmult() below, but the latter two public keys don't swap places. * * | ALICE | BOB | * | Client | Server | * |--------------------------------|-------------------------------------| * | shared = crypto_kx( | shared = crypto_kx( | * | alice_sk, | bob_sk, | <- contextual * | bob_pk, | alice_pk, | <- contextual * | alice_pk, | alice_pk, | <----- static * | bob_pk | bob_pk | <----- static * | ) | ) | * * They are used along with the scalarmult product to generate a 256-bit * BLAKE2b hash unique to the client and server keys. * * @param string $my_secret * @param string $their_public * @param string $client_public * @param string $server_public * @param bool $dontFallback * @return string * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_kx( #[\SensitiveParameter] $my_secret, $their_public, $client_public, $server_public, $dontFallback = false ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($my_secret, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($their_public, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($client_public, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($server_public, 'string', 4); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($my_secret) !== self::CRYPTO_BOX_SECRETKEYBYTES) { throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($their_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($client_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) { throw new SodiumException('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($server_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) { throw new SodiumException('Argument 4 must be CRYPTO_BOX_PUBLICKEYBYTES long.'); } if (self::useNewSodiumAPI() && !$dontFallback) { if (is_callable('sodium_crypto_kx')) { return (string) sodium_crypto_kx( $my_secret, $their_public, $client_public, $server_public ); } } if (self::use_fallback('crypto_kx')) { return (string) call_user_func( '\\Sodium\\crypto_kx', $my_secret, $their_public, $client_public, $server_public ); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::keyExchange( $my_secret, $their_public, $client_public, $server_public ); } return ParagonIE_Sodium_Crypto::keyExchange( $my_secret, $their_public, $client_public, $server_public ); } /** * @param string $seed * @return string * @throws SodiumException */ public static function crypto_kx_seed_keypair( #[\SensitiveParameter] $seed ) { ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1); $seed = (string) $seed; if (ParagonIE_Sodium_Core_Util::strlen($seed) !== self::CRYPTO_KX_SEEDBYTES) { throw new SodiumException('seed must be SODIUM_CRYPTO_KX_SEEDBYTES bytes'); } $sk = self::crypto_generichash($seed, '', self::CRYPTO_KX_SECRETKEYBYTES); $pk = self::crypto_scalarmult_base($sk); return $sk . $pk; } /** * @return string * @throws Exception */ public static function crypto_kx_keypair() { $sk = self::randombytes_buf(self::CRYPTO_KX_SECRETKEYBYTES); $pk = self::crypto_scalarmult_base($sk); return $sk . $pk; } /** * @param string $keypair * @param string $serverPublicKey * @return array{0: string, 1: string} * @throws SodiumException */ public static function crypto_kx_client_session_keys( #[\SensitiveParameter] $keypair, $serverPublicKey ) { ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($serverPublicKey, 'string', 2); $keypair = (string) $keypair; $serverPublicKey = (string) $serverPublicKey; if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_KX_KEYPAIRBYTES) { throw new SodiumException('keypair should be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes'); } if (ParagonIE_Sodium_Core_Util::strlen($serverPublicKey) !== self::CRYPTO_KX_PUBLICKEYBYTES) { throw new SodiumException('public keys must be SODIUM_CRYPTO_KX_PUBLICKEYBYTES bytes'); } $sk = self::crypto_kx_secretkey($keypair); $pk = self::crypto_kx_publickey($keypair); $h = self::crypto_generichash_init(null, self::CRYPTO_KX_SESSIONKEYBYTES * 2); self::crypto_generichash_update($h, self::crypto_scalarmult($sk, $serverPublicKey)); self::crypto_generichash_update($h, $pk); self::crypto_generichash_update($h, $serverPublicKey); $sessionKeys = self::crypto_generichash_final($h, self::CRYPTO_KX_SESSIONKEYBYTES * 2); return array( ParagonIE_Sodium_Core_Util::substr( $sessionKeys, 0, self::CRYPTO_KX_SESSIONKEYBYTES ), ParagonIE_Sodium_Core_Util::substr( $sessionKeys, self::CRYPTO_KX_SESSIONKEYBYTES, self::CRYPTO_KX_SESSIONKEYBYTES ) ); } /** * @param string $keypair * @param string $clientPublicKey * @return array{0: string, 1: string} * @throws SodiumException */ public static function crypto_kx_server_session_keys( #[\SensitiveParameter] $keypair, $clientPublicKey ) { ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($clientPublicKey, 'string', 2); $keypair = (string) $keypair; $clientPublicKey = (string) $clientPublicKey; if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_KX_KEYPAIRBYTES) { throw new SodiumException('keypair should be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes'); } if (ParagonIE_Sodium_Core_Util::strlen($clientPublicKey) !== self::CRYPTO_KX_PUBLICKEYBYTES) { throw new SodiumException('public keys must be SODIUM_CRYPTO_KX_PUBLICKEYBYTES bytes'); } $sk = self::crypto_kx_secretkey($keypair); $pk = self::crypto_kx_publickey($keypair); $h = self::crypto_generichash_init(null, self::CRYPTO_KX_SESSIONKEYBYTES * 2); self::crypto_generichash_update($h, self::crypto_scalarmult($sk, $clientPublicKey)); self::crypto_generichash_update($h, $clientPublicKey); self::crypto_generichash_update($h, $pk); $sessionKeys = self::crypto_generichash_final($h, self::CRYPTO_KX_SESSIONKEYBYTES * 2); return array( ParagonIE_Sodium_Core_Util::substr( $sessionKeys, self::CRYPTO_KX_SESSIONKEYBYTES, self::CRYPTO_KX_SESSIONKEYBYTES ), ParagonIE_Sodium_Core_Util::substr( $sessionKeys, 0, self::CRYPTO_KX_SESSIONKEYBYTES ) ); } /** * @param string $kp * @return string * @throws SodiumException */ public static function crypto_kx_secretkey( #[\SensitiveParameter] $kp ) { return ParagonIE_Sodium_Core_Util::substr( $kp, 0, self::CRYPTO_KX_SECRETKEYBYTES ); } /** * @param string $kp * @return string * @throws SodiumException */ public static function crypto_kx_publickey($kp) { return ParagonIE_Sodium_Core_Util::substr( $kp, self::CRYPTO_KX_SECRETKEYBYTES, self::CRYPTO_KX_PUBLICKEYBYTES ); } /** * @param int $outlen * @param string $passwd * @param string $salt * @param int $opslimit * @param int $memlimit * @param int|null $alg * @return string * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_pwhash( $outlen, #[\SensitiveParameter] $passwd, $salt, $opslimit, $memlimit, $alg = null ) { ParagonIE_Sodium_Core_Util::declareScalarType($outlen, 'int', 1); ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($salt, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 4); ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 5); if (self::useNewSodiumAPI()) { if (!is_null($alg)) { ParagonIE_Sodium_Core_Util::declareScalarType($alg, 'int', 6); return sodium_crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit, $alg); } return sodium_crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit); } if (self::use_fallback('crypto_pwhash')) { return (string) call_user_func('\\Sodium\\crypto_pwhash', $outlen, $passwd, $salt, $opslimit, $memlimit); } // This is the best we can do. throw new SodiumException( 'This is not implemented, as it is not possible to implement Argon2i with acceptable performance in pure-PHP' ); } /** * !Exclusive to sodium_compat! * * This returns TRUE if the native crypto_pwhash API is available by libsodium. * This returns FALSE if only sodium_compat is available. * * @return bool */ public static function crypto_pwhash_is_available() { if (self::useNewSodiumAPI()) { return true; } if (self::use_fallback('crypto_pwhash')) { return true; } return false; } /** * @param string $passwd * @param int $opslimit * @param int $memlimit * @return string * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_pwhash_str( #[\SensitiveParameter] $passwd, $opslimit, $memlimit ) { ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2); ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3); if (self::useNewSodiumAPI()) { return sodium_crypto_pwhash_str($passwd, $opslimit, $memlimit); } if (self::use_fallback('crypto_pwhash_str')) { return (string) call_user_func('\\Sodium\\crypto_pwhash_str', $passwd, $opslimit, $memlimit); } // This is the best we can do. throw new SodiumException( 'This is not implemented, as it is not possible to implement Argon2i with acceptable performance in pure-PHP' ); } /** * Do we need to rehash this password? * * @param string $hash * @param int $opslimit * @param int $memlimit * @return bool * @throws SodiumException */ public static function crypto_pwhash_str_needs_rehash( #[\SensitiveParameter] $hash, $opslimit, $memlimit ) { ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2); ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3); // Just grab the first 4 pieces. $pieces = explode('$', (string) $hash); $prefix = implode('$', array_slice($pieces, 0, 4)); // Rebuild the expected header. /** @var int $ops */ $ops = (int) $opslimit; /** @var int $mem */ $mem = (int) $memlimit >> 10; $encoded = self::CRYPTO_PWHASH_STRPREFIX . 'v=19$m=' . $mem . ',t=' . $ops . ',p=1'; // Do they match? If so, we don't need to rehash, so return false. return !ParagonIE_Sodium_Core_Util::hashEquals($encoded, $prefix); } /** * @param string $passwd * @param string $hash * @return bool * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_pwhash_str_verify( #[\SensitiveParameter] $passwd, #[\SensitiveParameter] $hash ) { ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2); if (self::useNewSodiumAPI()) { return (bool) sodium_crypto_pwhash_str_verify($passwd, $hash); } if (self::use_fallback('crypto_pwhash_str_verify')) { return (bool) call_user_func('\\Sodium\\crypto_pwhash_str_verify', $passwd, $hash); } // This is the best we can do. throw new SodiumException( 'This is not implemented, as it is not possible to implement Argon2i with acceptable performance in pure-PHP' ); } /** * @param int $outlen * @param string $passwd * @param string $salt * @param int $opslimit * @param int $memlimit * @return string * @throws SodiumException * @throws TypeError */ public static function crypto_pwhash_scryptsalsa208sha256( $outlen, #[\SensitiveParameter] $passwd, $salt, $opslimit, $memlimit ) { ParagonIE_Sodium_Core_Util::declareScalarType($outlen, 'int', 1); ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($salt, 'string', 3); ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 4); ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 5); if (self::useNewSodiumAPI()) { return (string) sodium_crypto_pwhash_scryptsalsa208sha256( (int) $outlen, (string) $passwd, (string) $salt, (int) $opslimit, (int) $memlimit ); } if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256')) { return (string) call_user_func( '\\Sodium\\crypto_pwhash_scryptsalsa208sha256', (int) $outlen, (string) $passwd, (string) $salt, (int) $opslimit, (int) $memlimit ); } // This is the best we can do. throw new SodiumException( 'This is not implemented, as it is not possible to implement Scrypt with acceptable performance in pure-PHP' ); } /** * !Exclusive to sodium_compat! * * This returns TRUE if the native crypto_pwhash API is available by libsodium. * This returns FALSE if only sodium_compat is available. * * @return bool */ public static function crypto_pwhash_scryptsalsa208sha256_is_available() { if (self::useNewSodiumAPI()) { return true; } if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256')) { return true; } return false; } /** * @param string $passwd * @param int $opslimit * @param int $memlimit * @return string * @throws SodiumException * @throws TypeError */ public static function crypto_pwhash_scryptsalsa208sha256_str( #[\SensitiveParameter] $passwd, $opslimit, $memlimit ) { ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2); ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3); if (self::useNewSodiumAPI()) { return (string) sodium_crypto_pwhash_scryptsalsa208sha256_str( (string) $passwd, (int) $opslimit, (int) $memlimit ); } if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256_str')) { return (string) call_user_func( '\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str', (string) $passwd, (int) $opslimit, (int) $memlimit ); } // This is the best we can do. throw new SodiumException( 'This is not implemented, as it is not possible to implement Scrypt with acceptable performance in pure-PHP' ); } /** * @param string $passwd * @param string $hash * @return bool * @throws SodiumException * @throws TypeError */ public static function crypto_pwhash_scryptsalsa208sha256_str_verify( #[\SensitiveParameter] $passwd, #[\SensitiveParameter] $hash ) { ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2); if (self::useNewSodiumAPI()) { return (bool) sodium_crypto_pwhash_scryptsalsa208sha256_str_verify( (string) $passwd, (string) $hash ); } if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256_str_verify')) { return (bool) call_user_func( '\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str_verify', (string) $passwd, (string) $hash ); } // This is the best we can do. throw new SodiumException( 'This is not implemented, as it is not possible to implement Scrypt with acceptable performance in pure-PHP' ); } /** * Calculate the shared secret between your secret key and your * recipient's public key. * * Algorithm: X25519 (ECDH over Curve25519) * * @param string $secretKey * @param string $publicKey * @return string * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_scalarmult( #[\SensitiveParameter] $secretKey, $publicKey ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) { throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_BOX_PUBLICKEYBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.'); } if (self::useNewSodiumAPI()) { return sodium_crypto_scalarmult($secretKey, $publicKey); } if (self::use_fallback('crypto_scalarmult')) { return (string) call_user_func('\\Sodium\\crypto_scalarmult', $secretKey, $publicKey); } /* Output validation: Forbid all-zero keys */ if (ParagonIE_Sodium_Core_Util::hashEquals($secretKey, str_repeat("\0", self::CRYPTO_BOX_SECRETKEYBYTES))) { throw new SodiumException('Zero secret key is not allowed'); } if (ParagonIE_Sodium_Core_Util::hashEquals($publicKey, str_repeat("\0", self::CRYPTO_BOX_PUBLICKEYBYTES))) { throw new SodiumException('Zero public key is not allowed'); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::scalarmult($secretKey, $publicKey); } return ParagonIE_Sodium_Crypto::scalarmult($secretKey, $publicKey); } /** * Calculate an X25519 public key from an X25519 secret key. * * @param string $secretKey * @return string * @throws SodiumException * @throws TypeError * @psalm-suppress TooFewArguments * @psalm-suppress MixedArgument */ public static function crypto_scalarmult_base( #[\SensitiveParameter] $secretKey ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) { throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.'); } if (self::useNewSodiumAPI()) { return sodium_crypto_scalarmult_base($secretKey); } if (self::use_fallback('crypto_scalarmult_base')) { return (string) call_user_func('\\Sodium\\crypto_scalarmult_base', $secretKey); } if (ParagonIE_Sodium_Core_Util::hashEquals($secretKey, str_repeat("\0", self::CRYPTO_BOX_SECRETKEYBYTES))) { throw new SodiumException('Zero secret key is not allowed'); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::scalarmult_base($secretKey); } return ParagonIE_Sodium_Crypto::scalarmult_base($secretKey); } /** * Authenticated symmetric-key encryption. * * Algorithm: XSalsa20-Poly1305 * * @param string $plaintext The message you're encrypting * @param string $nonce A Number to be used Once; must be 24 bytes * @param string $key Symmetric encryption key * @return string Ciphertext with Poly1305 MAC * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_secretbox( #[\SensitiveParameter] $plaintext, $nonce, #[\SensitiveParameter] $key ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) { throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.'); } if (self::useNewSodiumAPI()) { return sodium_crypto_secretbox($plaintext, $nonce, $key); } if (self::use_fallback('crypto_secretbox')) { return (string) call_user_func('\\Sodium\\crypto_secretbox', $plaintext, $nonce, $key); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::secretbox($plaintext, $nonce, $key); } return ParagonIE_Sodium_Crypto::secretbox($plaintext, $nonce, $key); } /** * Decrypts a message previously encrypted with crypto_secretbox(). * * @param string $ciphertext Ciphertext with Poly1305 MAC * @param string $nonce A Number to be used Once; must be 24 bytes * @param string $key Symmetric encryption key * @return string Original plaintext message * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ public static function crypto_secretbox_open( $ciphertext, $nonce, #[\SensitiveParameter] $key ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) { throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.'); } if (self::useNewSodiumAPI()) { /** * @psalm-suppress InvalidReturnStatement * @psalm-suppress FalsableReturnStatement */ return sodium_crypto_secretbox_open($ciphertext, $nonce, $key); } if (self::use_fallback('crypto_secretbox_open')) { return call_user_func('\\Sodium\\crypto_secretbox_open', $ciphertext, $nonce, $key); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::secretbox_open($ciphertext, $nonce, $key); } return ParagonIE_Sodium_Crypto::secretbox_open($ciphertext, $nonce, $key); } /** * Return a secure random key for use with crypto_secretbox * * @return string * @throws Exception * @throws Error */ public static function crypto_secretbox_keygen() { return random_bytes(self::CRYPTO_SECRETBOX_KEYBYTES); } /** * Authenticated symmetric-key encryption. * * Algorithm: XChaCha20-Poly1305 * * @param string $plaintext The message you're encrypting * @param string $nonce A Number to be used Once; must be 24 bytes * @param string $key Symmetric encryption key * @return string Ciphertext with Poly1305 MAC * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_secretbox_xchacha20poly1305($plaintext, $nonce, $key) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) { throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.'); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::secretbox_xchacha20poly1305($plaintext, $nonce, $key); } return ParagonIE_Sodium_Crypto::secretbox_xchacha20poly1305($plaintext, $nonce, $key); } /** * Decrypts a message previously encrypted with crypto_secretbox_xchacha20poly1305(). * * @param string $ciphertext Ciphertext with Poly1305 MAC * @param string $nonce A Number to be used Once; must be 24 bytes * @param string $key Symmetric encryption key * @return string Original plaintext message * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_secretbox_xchacha20poly1305_open( $ciphertext, $nonce, #[\SensitiveParameter] $key ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) { throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_SECRETBOX_MACBYTES) { throw new SodiumException("Ciphertext must be at least CRYPTO_SECRETBOX_MACBYTES long"); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key); } return ParagonIE_Sodium_Crypto::secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key); } /** * @param string $key * @return array<int, string> Returns a state and a header. * @throws Exception * @throws SodiumException */ public static function crypto_secretstream_xchacha20poly1305_init_push( #[\SensitiveParameter] $key ) { if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_push($key); } return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_init_push($key); } /** * @param string $header * @param string $key * @return string Returns a state. * @throws Exception */ public static function crypto_secretstream_xchacha20poly1305_init_pull( $header, #[\SensitiveParameter] $key ) { if (ParagonIE_Sodium_Core_Util::strlen($header) < self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES) { throw new SodiumException( 'header size should be SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES bytes' ); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_pull($key, $header); } return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_init_pull($key, $header); } /** * @param string $state * @param string $msg * @param string $aad * @param int $tag * @return string * @throws SodiumException */ public static function crypto_secretstream_xchacha20poly1305_push( #[\SensitiveParameter] &$state, #[\SensitiveParameter] $msg, $aad = '', $tag = 0 ) { if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_push( $state, $msg, $aad, $tag ); } return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_push( $state, $msg, $aad, $tag ); } /** * @param string $state * @param string $msg * @param string $aad * @return bool|array{0: string, 1: int} * @throws SodiumException */ public static function crypto_secretstream_xchacha20poly1305_pull( #[\SensitiveParameter] &$state, $msg, $aad = '' ) { if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_pull( $state, $msg, $aad ); } return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_pull( $state, $msg, $aad ); } /** * @return string * @throws Exception */ public static function crypto_secretstream_xchacha20poly1305_keygen() { return random_bytes(self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES); } /** * @param string $state * @return void * @throws SodiumException */ public static function crypto_secretstream_xchacha20poly1305_rekey( #[\SensitiveParameter] &$state ) { if (PHP_INT_SIZE === 4) { ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_rekey($state); } else { ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_rekey($state); } } /** * Calculates a SipHash-2-4 hash of a message for a given key. * * @param string $message Input message * @param string $key SipHash-2-4 key * @return string Hash * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ public static function crypto_shorthash( $message, #[\SensitiveParameter] $key ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SHORTHASH_KEYBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_SHORTHASH_KEYBYTES long.'); } if (self::useNewSodiumAPI()) { return sodium_crypto_shorthash($message, $key); } if (self::use_fallback('crypto_shorthash')) { return (string) call_user_func('\\Sodium\\crypto_shorthash', $message, $key); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Core32_SipHash::sipHash24($message, $key); } return ParagonIE_Sodium_Core_SipHash::sipHash24($message, $key); } /** * Return a secure random key for use with crypto_shorthash * * @return string * @throws Exception * @throws Error */ public static function crypto_shorthash_keygen() { return random_bytes(self::CRYPTO_SHORTHASH_KEYBYTES); } /** * Returns a signed message. You probably want crypto_sign_detached() * instead, which only returns the signature. * * Algorithm: Ed25519 (EdDSA over Curve25519) * * @param string $message Message to be signed. * @param string $secretKey Secret signing key. * @return string Signed message (signature is prefixed). * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ public static function crypto_sign( $message, #[\SensitiveParameter] $secretKey ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long.'); } if (self::useNewSodiumAPI()) { return sodium_crypto_sign($message, $secretKey); } if (self::use_fallback('crypto_sign')) { return (string) call_user_func('\\Sodium\\crypto_sign', $message, $secretKey); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::sign($message, $secretKey); } return ParagonIE_Sodium_Crypto::sign($message, $secretKey); } /** * Validates a signed message then returns the message. * * @param string $signedMessage A signed message * @param string $publicKey A public key * @return string The original message (if the signature is * valid for this public key) * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedInferredReturnType * @psalm-suppress MixedReturnStatement */ public static function crypto_sign_open( $signedMessage, $publicKey ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($signedMessage, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($signedMessage) < self::CRYPTO_SIGN_BYTES) { throw new SodiumException('Argument 1 must be at least CRYPTO_SIGN_BYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_SIGN_PUBLICKEYBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_SIGN_PUBLICKEYBYTES long.'); } if (self::useNewSodiumAPI()) { /** * @psalm-suppress InvalidReturnStatement * @psalm-suppress FalsableReturnStatement */ return sodium_crypto_sign_open($signedMessage, $publicKey); } if (self::use_fallback('crypto_sign_open')) { return call_user_func('\\Sodium\\crypto_sign_open', $signedMessage, $publicKey); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::sign_open($signedMessage, $publicKey); } return ParagonIE_Sodium_Crypto::sign_open($signedMessage, $publicKey); } /** * Generate a new random Ed25519 keypair. * * @return string * @throws SodiumException * @throws TypeError */ public static function crypto_sign_keypair() { if (self::useNewSodiumAPI()) { return sodium_crypto_sign_keypair(); } if (self::use_fallback('crypto_sign_keypair')) { return (string) call_user_func('\\Sodium\\crypto_sign_keypair'); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Core32_Ed25519::keypair(); } return ParagonIE_Sodium_Core_Ed25519::keypair(); } /** * @param string $sk * @param string $pk * @return string * @throws SodiumException */ public static function crypto_sign_keypair_from_secretkey_and_publickey( #[\SensitiveParameter] $sk, $pk ) { ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($pk, 'string', 1); $sk = (string) $sk; $pk = (string) $pk; if (ParagonIE_Sodium_Core_Util::strlen($sk) !== self::CRYPTO_SIGN_SECRETKEYBYTES) { throw new SodiumException('secretkey should be SODIUM_CRYPTO_SIGN_SECRETKEYBYTES bytes'); } if (ParagonIE_Sodium_Core_Util::strlen($pk) !== self::CRYPTO_SIGN_PUBLICKEYBYTES) { throw new SodiumException('publickey should be SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES bytes'); } if (self::useNewSodiumAPI()) { return sodium_crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk); } return $sk . $pk; } /** * Generate an Ed25519 keypair from a seed. * * @param string $seed Input seed * @return string Keypair * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_sign_seed_keypair( #[\SensitiveParameter] $seed ) { ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1); if (self::useNewSodiumAPI()) { return sodium_crypto_sign_seed_keypair($seed); } if (self::use_fallback('crypto_sign_keypair')) { return (string) call_user_func('\\Sodium\\crypto_sign_seed_keypair', $seed); } $publicKey = ''; $secretKey = ''; if (PHP_INT_SIZE === 4) { ParagonIE_Sodium_Core32_Ed25519::seed_keypair($publicKey, $secretKey, $seed); } else { ParagonIE_Sodium_Core_Ed25519::seed_keypair($publicKey, $secretKey, $seed); } return $secretKey . $publicKey; } /** * Extract an Ed25519 public key from an Ed25519 keypair. * * @param string $keypair Keypair * @return string Public key * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_sign_publickey( #[\SensitiveParameter] $keypair ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_SIGN_KEYPAIRBYTES) { throw new SodiumException('Argument 1 must be CRYPTO_SIGN_KEYPAIRBYTES long.'); } if (self::useNewSodiumAPI()) { return sodium_crypto_sign_publickey($keypair); } if (self::use_fallback('crypto_sign_publickey')) { return (string) call_user_func('\\Sodium\\crypto_sign_publickey', $keypair); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Core32_Ed25519::publickey($keypair); } return ParagonIE_Sodium_Core_Ed25519::publickey($keypair); } /** * Calculate an Ed25519 public key from an Ed25519 secret key. * * @param string $secretKey Your Ed25519 secret key * @return string The corresponding Ed25519 public key * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_sign_publickey_from_secretkey( #[\SensitiveParameter] $secretKey ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) { throw new SodiumException('Argument 1 must be CRYPTO_SIGN_SECRETKEYBYTES long.'); } if (self::useNewSodiumAPI()) { return sodium_crypto_sign_publickey_from_secretkey($secretKey); } if (self::use_fallback('crypto_sign_publickey_from_secretkey')) { return (string) call_user_func('\\Sodium\\crypto_sign_publickey_from_secretkey', $secretKey); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Core32_Ed25519::publickey_from_secretkey($secretKey); } return ParagonIE_Sodium_Core_Ed25519::publickey_from_secretkey($secretKey); } /** * Extract an Ed25519 secret key from an Ed25519 keypair. * * @param string $keypair Keypair * @return string Secret key * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_sign_secretkey( #[\SensitiveParameter] $keypair ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_SIGN_KEYPAIRBYTES) { throw new SodiumException('Argument 1 must be CRYPTO_SIGN_KEYPAIRBYTES long.'); } if (self::useNewSodiumAPI()) { return sodium_crypto_sign_secretkey($keypair); } if (self::use_fallback('crypto_sign_secretkey')) { return (string) call_user_func('\\Sodium\\crypto_sign_secretkey', $keypair); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Core32_Ed25519::secretkey($keypair); } return ParagonIE_Sodium_Core_Ed25519::secretkey($keypair); } /** * Calculate the Ed25519 signature of a message and return ONLY the signature. * * Algorithm: Ed25519 (EdDSA over Curve25519) * * @param string $message Message to be signed * @param string $secretKey Secret signing key * @return string Digital signature * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_sign_detached( $message, #[\SensitiveParameter] $secretKey ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long.'); } if (self::useNewSodiumAPI()) { return sodium_crypto_sign_detached($message, $secretKey); } if (self::use_fallback('crypto_sign_detached')) { return (string) call_user_func('\\Sodium\\crypto_sign_detached', $message, $secretKey); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::sign_detached($message, $secretKey); } return ParagonIE_Sodium_Crypto::sign_detached($message, $secretKey); } /** * Verify the Ed25519 signature of a message. * * @param string $signature Digital sginature * @param string $message Message to be verified * @param string $publicKey Public key * @return bool TRUE if this signature is good for this public key; * FALSE otherwise * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_sign_verify_detached($signature, $message, $publicKey) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($signature, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 3); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($signature) !== self::CRYPTO_SIGN_BYTES) { throw new SodiumException('Argument 1 must be CRYPTO_SIGN_BYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_SIGN_PUBLICKEYBYTES) { throw new SodiumException('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES long.'); } if (self::useNewSodiumAPI()) { return sodium_crypto_sign_verify_detached($signature, $message, $publicKey); } if (self::use_fallback('crypto_sign_verify_detached')) { return (bool) call_user_func( '\\Sodium\\crypto_sign_verify_detached', $signature, $message, $publicKey ); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Crypto32::sign_verify_detached($signature, $message, $publicKey); } return ParagonIE_Sodium_Crypto::sign_verify_detached($signature, $message, $publicKey); } /** * Convert an Ed25519 public key to a Curve25519 public key * * @param string $pk * @return string * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_sign_ed25519_pk_to_curve25519($pk) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($pk, 'string', 1); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($pk) < self::CRYPTO_SIGN_PUBLICKEYBYTES) { throw new SodiumException('Argument 1 must be at least CRYPTO_SIGN_PUBLICKEYBYTES long.'); } if (self::useNewSodiumAPI()) { if (is_callable('crypto_sign_ed25519_pk_to_curve25519')) { return (string) sodium_crypto_sign_ed25519_pk_to_curve25519($pk); } } if (self::use_fallback('crypto_sign_ed25519_pk_to_curve25519')) { return (string) call_user_func('\\Sodium\\crypto_sign_ed25519_pk_to_curve25519', $pk); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Core32_Ed25519::pk_to_curve25519($pk); } return ParagonIE_Sodium_Core_Ed25519::pk_to_curve25519($pk); } /** * Convert an Ed25519 secret key to a Curve25519 secret key * * @param string $sk * @return string * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_sign_ed25519_sk_to_curve25519( #[\SensitiveParameter] $sk ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($sk) < self::CRYPTO_SIGN_SEEDBYTES) { throw new SodiumException('Argument 1 must be at least CRYPTO_SIGN_SEEDBYTES long.'); } if (self::useNewSodiumAPI()) { if (is_callable('crypto_sign_ed25519_sk_to_curve25519')) { return sodium_crypto_sign_ed25519_sk_to_curve25519($sk); } } if (self::use_fallback('crypto_sign_ed25519_sk_to_curve25519')) { return (string) call_user_func('\\Sodium\\crypto_sign_ed25519_sk_to_curve25519', $sk); } $h = hash('sha512', ParagonIE_Sodium_Core_Util::substr($sk, 0, 32), true); $h[0] = ParagonIE_Sodium_Core_Util::intToChr( ParagonIE_Sodium_Core_Util::chrToInt($h[0]) & 248 ); $h[31] = ParagonIE_Sodium_Core_Util::intToChr( (ParagonIE_Sodium_Core_Util::chrToInt($h[31]) & 127) | 64 ); return ParagonIE_Sodium_Core_Util::substr($h, 0, 32); } /** * Expand a key and nonce into a keystream of pseudorandom bytes. * * @param int $len Number of bytes desired * @param string $nonce Number to be used Once; must be 24 bytes * @param string $key XSalsa20 key * @return string Pseudorandom stream that can be XORed with messages * to provide encryption (but not authentication; see * Poly1305 or crypto_auth() for that, which is not * optional for security) * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_stream( $len, $nonce, #[\SensitiveParameter] $key ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_NONCEBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_KEYBYTES) { throw new SodiumException('Argument 3 must be CRYPTO_STREAM_KEYBYTES long.'); } if (self::useNewSodiumAPI()) { return sodium_crypto_stream($len, $nonce, $key); } if (self::use_fallback('crypto_stream')) { return (string) call_user_func('\\Sodium\\crypto_stream', $len, $nonce, $key); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Core32_XSalsa20::xsalsa20($len, $nonce, $key); } return ParagonIE_Sodium_Core_XSalsa20::xsalsa20($len, $nonce, $key); } /** * DANGER! UNAUTHENTICATED ENCRYPTION! * * Unless you are following expert advice, do not use this feature. * * Algorithm: XSalsa20 * * This DOES NOT provide ciphertext integrity. * * @param string $message Plaintext message * @param string $nonce Number to be used Once; must be 24 bytes * @param string $key Encryption key * @return string Encrypted text which is vulnerable to chosen- * ciphertext attacks unless you implement some * other mitigation to the ciphertext (i.e. * Encrypt then MAC) * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_stream_xor( #[\SensitiveParameter] $message, $nonce, #[\SensitiveParameter] $key ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_NONCEBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_KEYBYTES) { throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.'); } if (self::useNewSodiumAPI()) { return sodium_crypto_stream_xor($message, $nonce, $key); } if (self::use_fallback('crypto_stream_xor')) { return (string) call_user_func('\\Sodium\\crypto_stream_xor', $message, $nonce, $key); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Core32_XSalsa20::xsalsa20_xor($message, $nonce, $key); } return ParagonIE_Sodium_Core_XSalsa20::xsalsa20_xor($message, $nonce, $key); } /** * Return a secure random key for use with crypto_stream * * @return string * @throws Exception * @throws Error */ public static function crypto_stream_keygen() { return random_bytes(self::CRYPTO_STREAM_KEYBYTES); } /** * Expand a key and nonce into a keystream of pseudorandom bytes. * * @param int $len Number of bytes desired * @param string $nonce Number to be used Once; must be 24 bytes * @param string $key XChaCha20 key * @param bool $dontFallback * @return string Pseudorandom stream that can be XORed with messages * to provide encryption (but not authentication; see * Poly1305 or crypto_auth() for that, which is not * optional for security) * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_stream_xchacha20( $len, $nonce, #[\SensitiveParameter] $key, $dontFallback = false ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_XCHACHA20_NONCEBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_XCHACHA20_NONCEBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_XCHACHA20_KEYBYTES) { throw new SodiumException('Argument 3 must be CRYPTO_STREAM_XCHACHA20_KEYBYTES long.'); } if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_stream_xchacha20($len, $nonce, $key); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Core32_XChaCha20::stream($len, $nonce, $key); } return ParagonIE_Sodium_Core_XChaCha20::stream($len, $nonce, $key); } /** * DANGER! UNAUTHENTICATED ENCRYPTION! * * Unless you are following expert advice, do not use this feature. * * Algorithm: XChaCha20 * * This DOES NOT provide ciphertext integrity. * * @param string $message Plaintext message * @param string $nonce Number to be used Once; must be 24 bytes * @param string $key Encryption key * @return string Encrypted text which is vulnerable to chosen- * ciphertext attacks unless you implement some * other mitigation to the ciphertext (i.e. * Encrypt then MAC) * @param bool $dontFallback * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_stream_xchacha20_xor( #[\SensitiveParameter] $message, $nonce, #[\SensitiveParameter] $key, $dontFallback = false ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_XCHACHA20_NONCEBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_XCHACHA20_NONCEBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_XCHACHA20_KEYBYTES) { throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_XCHACHA20_KEYBYTES long.'); } if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_stream_xchacha20_xor($message, $nonce, $key); } if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Core32_XChaCha20::streamXorIc($message, $nonce, $key); } return ParagonIE_Sodium_Core_XChaCha20::streamXorIc($message, $nonce, $key); } /** * DANGER! UNAUTHENTICATED ENCRYPTION! * * Unless you are following expert advice, do not use this feature. * * Algorithm: XChaCha20 * * This DOES NOT provide ciphertext integrity. * * @param string $message Plaintext message * @param string $nonce Number to be used Once; must be 24 bytes * @param int $counter * @param string $key Encryption key * @return string Encrypted text which is vulnerable to chosen- * ciphertext attacks unless you implement some * other mitigation to the ciphertext (i.e. * Encrypt then MAC) * @param bool $dontFallback * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function crypto_stream_xchacha20_xor_ic( #[\SensitiveParameter] $message, $nonce, $counter, #[\SensitiveParameter] $key, $dontFallback = false ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); ParagonIE_Sodium_Core_Util::declareScalarType($counter, 'int', 3); ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4); /* Input validation: */ if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_XCHACHA20_NONCEBYTES) { throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_XCHACHA20_NONCEBYTES long.'); } if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_XCHACHA20_KEYBYTES) { throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_XCHACHA20_KEYBYTES long.'); } if (is_callable('sodium_crypto_stream_xchacha20_xor_ic') && !$dontFallback) { return sodium_crypto_stream_xchacha20_xor_ic($message, $nonce, $counter, $key); } $ic = ParagonIE_Sodium_Core_Util::store64_le($counter); if (PHP_INT_SIZE === 4) { return ParagonIE_Sodium_Core32_XChaCha20::streamXorIc($message, $nonce, $key, $ic); } return ParagonIE_Sodium_Core_XChaCha20::streamXorIc($message, $nonce, $key, $ic); } /** * Return a secure random key for use with crypto_stream_xchacha20 * * @return string * @throws Exception * @throws Error */ public static function crypto_stream_xchacha20_keygen() { return random_bytes(self::CRYPTO_STREAM_XCHACHA20_KEYBYTES); } /** * Cache-timing-safe implementation of hex2bin(). * * @param string $string Hexadecimal string * @param string $ignore List of characters to ignore; useful for whitespace * @return string Raw binary string * @throws SodiumException * @throws TypeError * @psalm-suppress TooFewArguments * @psalm-suppress MixedArgument */ public static function hex2bin( #[\SensitiveParameter] $string, $ignore = '' ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($ignore, 'string', 2); if (self::useNewSodiumAPI()) { if (is_callable('sodium_hex2bin')) { return (string) sodium_hex2bin($string, $ignore); } } if (self::use_fallback('hex2bin')) { return (string) call_user_func('\\Sodium\\hex2bin', $string, $ignore); } return ParagonIE_Sodium_Core_Util::hex2bin($string, $ignore); } /** * Increase a string (little endian) * * @param string $var * * @return void * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function increment( #[\SensitiveParameter] &$var ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1); if (self::useNewSodiumAPI()) { sodium_increment($var); return; } if (self::use_fallback('increment')) { $func = '\\Sodium\\increment'; $func($var); return; } $len = ParagonIE_Sodium_Core_Util::strlen($var); if ($len < 1) { throw new SodiumException('Argument 1 cannot be empty'); } $c = 1; $copy = ''; for ($i = 0; $i < $len; ++$i) { $c += ParagonIE_Sodium_Core_Util::chrToInt( ParagonIE_Sodium_Core_Util::substr($var, $i, 1) ); $copy .= ParagonIE_Sodium_Core_Util::intToChr($c); $c >>= 8; } $var = $copy; } /** * @param string $str * @return bool * * @throws SodiumException */ public static function is_zero( #[\SensitiveParameter] $str ) { $d = 0; for ($i = 0; $i < 32; ++$i) { $d |= ParagonIE_Sodium_Core_Util::chrToInt($str[$i]); } return ((($d - 1) >> 31) & 1) === 1; } /** * The equivalent to the libsodium minor version we aim to be compatible * with (sans pwhash and memzero). * * @return int */ public static function library_version_major() { if (self::useNewSodiumAPI() && defined('SODIUM_LIBRARY_MAJOR_VERSION')) { return SODIUM_LIBRARY_MAJOR_VERSION; } if (self::use_fallback('library_version_major')) { /** @psalm-suppress UndefinedFunction */ return (int) call_user_func('\\Sodium\\library_version_major'); } return self::LIBRARY_VERSION_MAJOR; } /** * The equivalent to the libsodium minor version we aim to be compatible * with (sans pwhash and memzero). * * @return int */ public static function library_version_minor() { if (self::useNewSodiumAPI() && defined('SODIUM_LIBRARY_MINOR_VERSION')) { return SODIUM_LIBRARY_MINOR_VERSION; } if (self::use_fallback('library_version_minor')) { /** @psalm-suppress UndefinedFunction */ return (int) call_user_func('\\Sodium\\library_version_minor'); } return self::LIBRARY_VERSION_MINOR; } /** * Compare two strings. * * @param string $left * @param string $right * @return int * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument */ public static function memcmp( #[\SensitiveParameter] $left, #[\SensitiveParameter] $right ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2); if (self::useNewSodiumAPI()) { return sodium_memcmp($left, $right); } if (self::use_fallback('memcmp')) { return (int) call_user_func('\\Sodium\\memcmp', $left, $right); } /** @var string $left */ /** @var string $right */ return ParagonIE_Sodium_Core_Util::memcmp($left, $right); } /** * It's actually not possible to zero memory buffers in PHP. You need the * native library for that. * * @param string|null $var * @param-out string|null $var * * @return void * @throws SodiumException (Unless libsodium is installed) * @throws TypeError * @psalm-suppress TooFewArguments */ public static function memzero( #[\SensitiveParameter] &$var ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1); if (self::useNewSodiumAPI()) { /** @psalm-suppress MixedArgument */ sodium_memzero($var); return; } if (self::use_fallback('memzero')) { $func = '\\Sodium\\memzero'; $func($var); if ($var === null) { return; } } // This is the best we can do. throw new SodiumException( 'This is not implemented in sodium_compat, as it is not possible to securely wipe memory from PHP. ' . 'To fix this error, make sure libsodium is installed and the PHP extension is enabled.' ); } /** * @param string $unpadded * @param int $blockSize * @param bool $dontFallback * @return string * @throws SodiumException */ public static function pad( #[\SensitiveParameter] $unpadded, $blockSize, $dontFallback = false ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($unpadded, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2); $unpadded = (string) $unpadded; $blockSize = (int) $blockSize; if (self::useNewSodiumAPI() && !$dontFallback) { return (string) sodium_pad($unpadded, $blockSize); } if ($blockSize <= 0) { throw new SodiumException( 'block size cannot be less than 1' ); } $unpadded_len = ParagonIE_Sodium_Core_Util::strlen($unpadded); $xpadlen = ($blockSize - 1); if (($blockSize & ($blockSize - 1)) === 0) { $xpadlen -= $unpadded_len & ($blockSize - 1); } else { $xpadlen -= $unpadded_len % $blockSize; } $xpadded_len = $unpadded_len + $xpadlen; $padded = str_repeat("\0", $xpadded_len - 1); if ($unpadded_len > 0) { $st = 1; $i = 0; $k = $unpadded_len; for ($j = 0; $j <= $xpadded_len; ++$j) { $i = (int) $i; $k = (int) $k; $st = (int) $st; if ($j >= $unpadded_len) { $padded[$j] = "\0"; } else { $padded[$j] = $unpadded[$j]; } /** @var int $k */ $k -= $st; $st = (int) (~( ( ( ($k >> 48) | ($k >> 32) | ($k >> 16) | $k ) - 1 ) >> 16 ) ) & 1; $i += $st; } } $mask = 0; $tail = $xpadded_len; for ($i = 0; $i < $blockSize; ++$i) { # barrier_mask = (unsigned char) # (((i ^ xpadlen) - 1U) >> ((sizeof(size_t) - 1U) * CHAR_BIT)); $barrier_mask = (($i ^ $xpadlen) -1) >> ((PHP_INT_SIZE << 3) - 1); # tail[-i] = (tail[-i] & mask) | (0x80 & barrier_mask); $padded[$tail - $i] = ParagonIE_Sodium_Core_Util::intToChr( (ParagonIE_Sodium_Core_Util::chrToInt($padded[$tail - $i]) & $mask) | (0x80 & $barrier_mask) ); # mask |= barrier_mask; $mask |= $barrier_mask; } return $padded; } /** * @param string $padded * @param int $blockSize * @param bool $dontFallback * @return string * @throws SodiumException */ public static function unpad( #[\SensitiveParameter] $padded, $blockSize, $dontFallback = false ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($padded, 'string', 1); ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2); $padded = (string) $padded; $blockSize = (int) $blockSize; if (self::useNewSodiumAPI() && !$dontFallback) { return (string) sodium_unpad($padded, $blockSize); } if ($blockSize <= 0) { throw new SodiumException('block size cannot be less than 1'); } $padded_len = ParagonIE_Sodium_Core_Util::strlen($padded); if ($padded_len < $blockSize) { throw new SodiumException('invalid padding'); } # tail = &padded[padded_len - 1U]; $tail = $padded_len - 1; $acc = 0; $valid = 0; $pad_len = 0; $found = 0; for ($i = 0; $i < $blockSize; ++$i) { # c = tail[-i]; $c = ParagonIE_Sodium_Core_Util::chrToInt($padded[$tail - $i]); # is_barrier = # (( (acc - 1U) & (pad_len - 1U) & ((c ^ 0x80) - 1U) ) >> 8) & 1U; $is_barrier = ( ( ($acc - 1) & ($pad_len - 1) & (($c ^ 80) - 1) ) >> 7 ) & 1; $is_barrier &= ~$found; $found |= $is_barrier; # acc |= c; $acc |= $c; # pad_len |= i & (1U + ~is_barrier); $pad_len |= $i & (1 + ~$is_barrier); # valid |= (unsigned char) is_barrier; $valid |= ($is_barrier & 0xff); } # unpadded_len = padded_len - 1U - pad_len; $unpadded_len = $padded_len - 1 - $pad_len; if ($valid !== 1) { throw new SodiumException('invalid padding'); } return ParagonIE_Sodium_Core_Util::substr($padded, 0, $unpadded_len); } /** * Will sodium_compat run fast on the current hardware and PHP configuration? * * @return bool */ public static function polyfill_is_fast() { if (extension_loaded('sodium')) { return true; } if (extension_loaded('libsodium')) { return true; } return PHP_INT_SIZE === 8; } /** * Generate a string of bytes from the kernel's CSPRNG. * Proudly uses /dev/urandom (if getrandom(2) is not available). * * @param int $numBytes * @return string * @throws Exception * @throws TypeError */ public static function randombytes_buf($numBytes) { /* Type checks: */ if (!is_int($numBytes)) { if (is_numeric($numBytes)) { $numBytes = (int) $numBytes; } else { throw new TypeError( 'Argument 1 must be an integer, ' . gettype($numBytes) . ' given.' ); } } /** @var positive-int $numBytes */ if (self::use_fallback('randombytes_buf')) { return (string) call_user_func('\\Sodium\\randombytes_buf', $numBytes); } if ($numBytes < 0) { throw new SodiumException("Number of bytes must be a positive integer"); } return random_bytes($numBytes); } /** * Generate an integer between 0 and $range (non-inclusive). * * @param int $range * @return int * @throws Exception * @throws Error * @throws TypeError */ public static function randombytes_uniform($range) { /* Type checks: */ if (!is_int($range)) { if (is_numeric($range)) { $range = (int) $range; } else { throw new TypeError( 'Argument 1 must be an integer, ' . gettype($range) . ' given.' ); } } if (self::use_fallback('randombytes_uniform')) { return (int) call_user_func('\\Sodium\\randombytes_uniform', $range); } return random_int(0, $range - 1); } /** * Generate a random 16-bit integer. * * @return int * @throws Exception * @throws Error * @throws TypeError */ public static function randombytes_random16() { if (self::use_fallback('randombytes_random16')) { return (int) call_user_func('\\Sodium\\randombytes_random16'); } return random_int(0, 65535); } /** * @param string $p * @param bool $dontFallback * @return bool * @throws SodiumException */ public static function ristretto255_is_valid_point( #[\SensitiveParameter] $p, $dontFallback = false ) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_core_ristretto255_is_valid_point($p); } try { $r = ParagonIE_Sodium_Core_Ristretto255::ristretto255_frombytes($p); return $r['res'] === 0 && ParagonIE_Sodium_Core_Ristretto255::ristretto255_point_is_canonical($p) === 1; } catch (SodiumException $ex) { if ($ex->getMessage() === 'S is not canonical') { return false; } throw $ex; } } /** * @param string $p * @param string $q * @param bool $dontFallback * @return string * @throws SodiumException */ public static function ristretto255_add( #[\SensitiveParameter] $p, #[\SensitiveParameter] $q, $dontFallback = false ) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_core_ristretto255_add($p, $q); } return ParagonIE_Sodium_Core_Ristretto255::ristretto255_add($p, $q); } /** * @param string $p * @param string $q * @param bool $dontFallback * @return string * @throws SodiumException */ public static function ristretto255_sub( #[\SensitiveParameter] $p, #[\SensitiveParameter] $q, $dontFallback = false ) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_core_ristretto255_sub($p, $q); } return ParagonIE_Sodium_Core_Ristretto255::ristretto255_sub($p, $q); } /** * @param string $r * @param bool $dontFallback * @return string * * @throws SodiumException */ public static function ristretto255_from_hash( #[\SensitiveParameter] $r, $dontFallback = false ) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_core_ristretto255_from_hash($r); } return ParagonIE_Sodium_Core_Ristretto255::ristretto255_from_hash($r); } /** * @param bool $dontFallback * @return string * * @throws SodiumException */ public static function ristretto255_random($dontFallback = false) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_core_ristretto255_random(); } return ParagonIE_Sodium_Core_Ristretto255::ristretto255_random(); } /** * @param bool $dontFallback * @return string * * @throws SodiumException */ public static function ristretto255_scalar_random($dontFallback = false) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_core_ristretto255_scalar_random(); } return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_random(); } /** * @param string $s * @param bool $dontFallback * @return string * @throws SodiumException */ public static function ristretto255_scalar_invert( #[\SensitiveParameter] $s, $dontFallback = false ) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_core_ristretto255_scalar_invert($s); } return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_invert($s); } /** * @param string $s * @param bool $dontFallback * @return string * @throws SodiumException */ public static function ristretto255_scalar_negate( #[\SensitiveParameter] $s, $dontFallback = false ) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_core_ristretto255_scalar_negate($s); } return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_negate($s); } /** * @param string $s * @param bool $dontFallback * @return string * @throws SodiumException */ public static function ristretto255_scalar_complement( #[\SensitiveParameter] $s, $dontFallback = false ) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_core_ristretto255_scalar_complement($s); } return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_complement($s); } /** * @param string $x * @param string $y * @param bool $dontFallback * @return string * @throws SodiumException */ public static function ristretto255_scalar_add( #[\SensitiveParameter] $x, #[\SensitiveParameter] $y, $dontFallback = false ) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_core_ristretto255_scalar_add($x, $y); } return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_add($x, $y); } /** * @param string $x * @param string $y * @param bool $dontFallback * @return string * @throws SodiumException */ public static function ristretto255_scalar_sub( #[\SensitiveParameter] $x, #[\SensitiveParameter] $y, $dontFallback = false ) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_core_ristretto255_scalar_sub($x, $y); } return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_sub($x, $y); } /** * @param string $x * @param string $y * @param bool $dontFallback * @return string * @throws SodiumException */ public static function ristretto255_scalar_mul( #[\SensitiveParameter] $x, #[\SensitiveParameter] $y, $dontFallback = false ) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_core_ristretto255_scalar_mul($x, $y); } return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_mul($x, $y); } /** * @param string $n * @param string $p * @param bool $dontFallback * @return string * @throws SodiumException */ public static function scalarmult_ristretto255( #[\SensitiveParameter] $n, #[\SensitiveParameter] $p, $dontFallback = false ) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_scalarmult_ristretto255($n, $p); } return ParagonIE_Sodium_Core_Ristretto255::scalarmult_ristretto255($n, $p); } /** * @param string $n * @param string $p * @param bool $dontFallback * @return string * @throws SodiumException */ public static function scalarmult_ristretto255_base( #[\SensitiveParameter] $n, $dontFallback = false ) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_scalarmult_ristretto255_base($n); } return ParagonIE_Sodium_Core_Ristretto255::scalarmult_ristretto255_base($n); } /** * @param string $s * @param bool $dontFallback * @return string * @throws SodiumException */ public static function ristretto255_scalar_reduce( #[\SensitiveParameter] $s, $dontFallback = false ) { if (self::useNewSodiumAPI() && !$dontFallback) { return sodium_crypto_core_ristretto255_scalar_reduce($s); } return ParagonIE_Sodium_Core_Ristretto255::sc_reduce($s); } /** * Runtime testing method for 32-bit platforms. * * Usage: If runtime_speed_test() returns FALSE, then our 32-bit * implementation is to slow to use safely without risking timeouts. * If this happens, install sodium from PECL to get acceptable * performance. * * @param int $iterations Number of multiplications to attempt * @param int $maxTimeout Milliseconds * @return bool TRUE if we're fast enough, FALSE is not * @throws SodiumException */ public static function runtime_speed_test($iterations, $maxTimeout) { if (self::polyfill_is_fast()) { return true; } /** @var float $end */ $end = 0.0; /** @var float $start */ $start = microtime(true); /** @var ParagonIE_Sodium_Core32_Int64 $a */ $a = ParagonIE_Sodium_Core32_Int64::fromInt(random_int(3, 1 << 16)); for ($i = 0; $i < $iterations; ++$i) { /** @var ParagonIE_Sodium_Core32_Int64 $b */ $b = ParagonIE_Sodium_Core32_Int64::fromInt(random_int(3, 1 << 16)); $a->mulInt64($b); } /** @var float $end */ $end = microtime(true); /** @var int $diff */ $diff = (int) ceil(($end - $start) * 1000); return $diff < $maxTimeout; } /** * Add two numbers (little-endian unsigned), storing the value in the first * parameter. * * This mutates $val. * * @param string $val * @param string $addv * @return void * @throws SodiumException */ public static function sub( #[\SensitiveParameter] &$val, #[\SensitiveParameter] $addv ) { $val_len = ParagonIE_Sodium_Core_Util::strlen($val); $addv_len = ParagonIE_Sodium_Core_Util::strlen($addv); if ($val_len !== $addv_len) { throw new SodiumException('values must have the same length'); } $A = ParagonIE_Sodium_Core_Util::stringToIntArray($val); $B = ParagonIE_Sodium_Core_Util::stringToIntArray($addv); $c = 0; for ($i = 0; $i < $val_len; $i++) { $c = ($A[$i] - $B[$i] - $c); $A[$i] = ($c & 0xff); $c = ($c >> 8) & 1; } $val = ParagonIE_Sodium_Core_Util::intArrayToString($A); } /** * This emulates libsodium's version_string() function, except ours is * prefixed with 'polyfill-'. * * @return string * @psalm-suppress MixedInferredReturnType * @psalm-suppress UndefinedFunction */ public static function version_string() { if (self::useNewSodiumAPI()) { return (string) sodium_version_string(); } if (self::use_fallback('version_string')) { return (string) call_user_func('\\Sodium\\version_string'); } return (string) self::VERSION_STRING; } /** * Should we use the libsodium core function instead? * This is always a good idea, if it's available. (Unless we're in the * middle of running our unit test suite.) * * If ext/libsodium is available, use it. Return TRUE. * Otherwise, we have to use the code provided herein. Return FALSE. * * @param string $sodium_func_name * * @return bool */ protected static function use_fallback($sodium_func_name = '') { static $res = null; if ($res === null) { $res = extension_loaded('libsodium') && PHP_VERSION_ID >= 50300; } if ($res === false) { // No libsodium installed return false; } if (self::$disableFallbackForUnitTests) { // Don't fallback. Use the PHP implementation. return false; } if (!empty($sodium_func_name)) { return is_callable('\\Sodium\\' . $sodium_func_name); } return true; } /** * Libsodium as implemented in PHP 7.2 * and/or ext/sodium (via PECL) * * @ref https://wiki.php.net/rfc/libsodium * @return bool */ protected static function useNewSodiumAPI() { static $res = null; if ($res === null) { $res = PHP_VERSION_ID >= 70000 && extension_loaded('sodium'); } if (self::$disableFallbackForUnitTests) { // Don't fallback. Use the PHP implementation. return false; } return (bool) $res; } } SodiumException.php 0000644 00000000236 15162213655 0010403 0 ustar 00 <?php if (!class_exists('SodiumException', false)) { /** * Class SodiumException */ class SodiumException extends Exception { } } Core32/Curve25519.php 0000644 00000403556 15162213656 0010030 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_Curve25519', false)) { return; } /** * Class ParagonIE_Sodium_Core32_Curve25519 * * Implements Curve25519 core functions * * Based on the ref10 curve25519 code provided by libsodium * * @ref https://github.com/jedisct1/libsodium/blob/master/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.c */ abstract class ParagonIE_Sodium_Core32_Curve25519 extends ParagonIE_Sodium_Core32_Curve25519_H { /** * Get a field element of size 10 with a value of 0 * * @internal You should not use this directly from another application * * @return ParagonIE_Sodium_Core32_Curve25519_Fe * @throws SodiumException * @throws TypeError */ public static function fe_0() { return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array( new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32() ) ); } /** * Get a field element of size 10 with a value of 1 * * @internal You should not use this directly from another application * * @return ParagonIE_Sodium_Core32_Curve25519_Fe * @throws SodiumException * @throws TypeError */ public static function fe_1() { return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array( ParagonIE_Sodium_Core32_Int32::fromInt(1), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32(), new ParagonIE_Sodium_Core32_Int32() ) ); } /** * Add two field elements. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f * @param ParagonIE_Sodium_Core32_Curve25519_Fe $g * @return ParagonIE_Sodium_Core32_Curve25519_Fe * @throws SodiumException * @throws TypeError * @psalm-suppress MixedAssignment * @psalm-suppress MixedMethodCall */ public static function fe_add( ParagonIE_Sodium_Core32_Curve25519_Fe $f, ParagonIE_Sodium_Core32_Curve25519_Fe $g ) { $arr = array(); for ($i = 0; $i < 10; ++$i) { $arr[$i] = $f[$i]->addInt32($g[$i]); } /** @var array<int, ParagonIE_Sodium_Core32_Int32> $arr */ return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($arr); } /** * Constant-time conditional move. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f * @param ParagonIE_Sodium_Core32_Curve25519_Fe $g * @param int $b * @return ParagonIE_Sodium_Core32_Curve25519_Fe * @throws SodiumException * @throws TypeError * @psalm-suppress MixedAssignment * @psalm-suppress MixedMethodCall */ public static function fe_cmov( ParagonIE_Sodium_Core32_Curve25519_Fe $f, ParagonIE_Sodium_Core32_Curve25519_Fe $g, $b = 0 ) { /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h */ $h = array(); for ($i = 0; $i < 10; ++$i) { if (!($f[$i] instanceof ParagonIE_Sodium_Core32_Int32)) { throw new TypeError('Expected Int32'); } if (!($g[$i] instanceof ParagonIE_Sodium_Core32_Int32)) { throw new TypeError('Expected Int32'); } $h[$i] = $f[$i]->xorInt32( $f[$i]->xorInt32($g[$i])->mask($b) ); } /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h */ return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h); } /** * Create a copy of a field element. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f * @return ParagonIE_Sodium_Core32_Curve25519_Fe */ public static function fe_copy(ParagonIE_Sodium_Core32_Curve25519_Fe $f) { $h = clone $f; return $h; } /** * Give: 32-byte string. * Receive: A field element object to use for internal calculations. * * @internal You should not use this directly from another application * * @param string $s * @return ParagonIE_Sodium_Core32_Curve25519_Fe * @throws RangeException * @throws SodiumException * @throws TypeError * @psalm-suppress MixedMethodCall */ public static function fe_frombytes($s) { if (self::strlen($s) !== 32) { throw new RangeException('Expected a 32-byte string.'); } /** @var ParagonIE_Sodium_Core32_Int32 $h0 */ $h0 = ParagonIE_Sodium_Core32_Int32::fromInt( self::load_4($s) ); /** @var ParagonIE_Sodium_Core32_Int32 $h1 */ $h1 = ParagonIE_Sodium_Core32_Int32::fromInt( self::load_3(self::substr($s, 4, 3)) << 6 ); /** @var ParagonIE_Sodium_Core32_Int32 $h2 */ $h2 = ParagonIE_Sodium_Core32_Int32::fromInt( self::load_3(self::substr($s, 7, 3)) << 5 ); /** @var ParagonIE_Sodium_Core32_Int32 $h3 */ $h3 = ParagonIE_Sodium_Core32_Int32::fromInt( self::load_3(self::substr($s, 10, 3)) << 3 ); /** @var ParagonIE_Sodium_Core32_Int32 $h4 */ $h4 = ParagonIE_Sodium_Core32_Int32::fromInt( self::load_3(self::substr($s, 13, 3)) << 2 ); /** @var ParagonIE_Sodium_Core32_Int32 $h5 */ $h5 = ParagonIE_Sodium_Core32_Int32::fromInt( self::load_4(self::substr($s, 16, 4)) ); /** @var ParagonIE_Sodium_Core32_Int32 $h6 */ $h6 = ParagonIE_Sodium_Core32_Int32::fromInt( self::load_3(self::substr($s, 20, 3)) << 7 ); /** @var ParagonIE_Sodium_Core32_Int32 $h7 */ $h7 = ParagonIE_Sodium_Core32_Int32::fromInt( self::load_3(self::substr($s, 23, 3)) << 5 ); /** @var ParagonIE_Sodium_Core32_Int32 $h8 */ $h8 = ParagonIE_Sodium_Core32_Int32::fromInt( self::load_3(self::substr($s, 26, 3)) << 4 ); /** @var ParagonIE_Sodium_Core32_Int32 $h9 */ $h9 = ParagonIE_Sodium_Core32_Int32::fromInt( (self::load_3(self::substr($s, 29, 3)) & 8388607) << 2 ); $carry9 = $h9->addInt(1 << 24)->shiftRight(25); $h0 = $h0->addInt32($carry9->mulInt(19, 5)); $h9 = $h9->subInt32($carry9->shiftLeft(25)); $carry1 = $h1->addInt(1 << 24)->shiftRight(25); $h2 = $h2->addInt32($carry1); $h1 = $h1->subInt32($carry1->shiftLeft(25)); $carry3 = $h3->addInt(1 << 24)->shiftRight(25); $h4 = $h4->addInt32($carry3); $h3 = $h3->subInt32($carry3->shiftLeft(25)); $carry5 = $h5->addInt(1 << 24)->shiftRight(25); $h6 = $h6->addInt32($carry5); $h5 = $h5->subInt32($carry5->shiftLeft(25)); $carry7 = $h7->addInt(1 << 24)->shiftRight(25); $h8 = $h8->addInt32($carry7); $h7 = $h7->subInt32($carry7->shiftLeft(25)); $carry0 = $h0->addInt(1 << 25)->shiftRight(26); $h1 = $h1->addInt32($carry0); $h0 = $h0->subInt32($carry0->shiftLeft(26)); $carry2 = $h2->addInt(1 << 25)->shiftRight(26); $h3 = $h3->addInt32($carry2); $h2 = $h2->subInt32($carry2->shiftLeft(26)); $carry4 = $h4->addInt(1 << 25)->shiftRight(26); $h5 = $h5->addInt32($carry4); $h4 = $h4->subInt32($carry4->shiftLeft(26)); $carry6 = $h6->addInt(1 << 25)->shiftRight(26); $h7 = $h7->addInt32($carry6); $h6 = $h6->subInt32($carry6->shiftLeft(26)); $carry8 = $h8->addInt(1 << 25)->shiftRight(26); $h9 = $h9->addInt32($carry8); $h8 = $h8->subInt32($carry8->shiftLeft(26)); return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array($h0, $h1, $h2,$h3, $h4, $h5, $h6, $h7, $h8, $h9) ); } /** * Convert a field element to a byte string. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $h * @return string * @throws SodiumException * @throws TypeError * @psalm-suppress MixedAssignment * @psalm-suppress MixedMethodCall */ public static function fe_tobytes(ParagonIE_Sodium_Core32_Curve25519_Fe $h) { /** * @var ParagonIE_Sodium_Core32_Int64[] $f * @var ParagonIE_Sodium_Core32_Int64 $q */ $f = array(); for ($i = 0; $i < 10; ++$i) { $f[$i] = $h[$i]->toInt64(); } $q = $f[9]->mulInt(19, 5)->addInt(1 << 14)->shiftRight(25) ->addInt64($f[0])->shiftRight(26) ->addInt64($f[1])->shiftRight(25) ->addInt64($f[2])->shiftRight(26) ->addInt64($f[3])->shiftRight(25) ->addInt64($f[4])->shiftRight(26) ->addInt64($f[5])->shiftRight(25) ->addInt64($f[6])->shiftRight(26) ->addInt64($f[7])->shiftRight(25) ->addInt64($f[8])->shiftRight(26) ->addInt64($f[9])->shiftRight(25); $f[0] = $f[0]->addInt64($q->mulInt(19, 5)); $carry0 = $f[0]->shiftRight(26); $f[1] = $f[1]->addInt64($carry0); $f[0] = $f[0]->subInt64($carry0->shiftLeft(26)); $carry1 = $f[1]->shiftRight(25); $f[2] = $f[2]->addInt64($carry1); $f[1] = $f[1]->subInt64($carry1->shiftLeft(25)); $carry2 = $f[2]->shiftRight(26); $f[3] = $f[3]->addInt64($carry2); $f[2] = $f[2]->subInt64($carry2->shiftLeft(26)); $carry3 = $f[3]->shiftRight(25); $f[4] = $f[4]->addInt64($carry3); $f[3] = $f[3]->subInt64($carry3->shiftLeft(25)); $carry4 = $f[4]->shiftRight(26); $f[5] = $f[5]->addInt64($carry4); $f[4] = $f[4]->subInt64($carry4->shiftLeft(26)); $carry5 = $f[5]->shiftRight(25); $f[6] = $f[6]->addInt64($carry5); $f[5] = $f[5]->subInt64($carry5->shiftLeft(25)); $carry6 = $f[6]->shiftRight(26); $f[7] = $f[7]->addInt64($carry6); $f[6] = $f[6]->subInt64($carry6->shiftLeft(26)); $carry7 = $f[7]->shiftRight(25); $f[8] = $f[8]->addInt64($carry7); $f[7] = $f[7]->subInt64($carry7->shiftLeft(25)); $carry8 = $f[8]->shiftRight(26); $f[9] = $f[9]->addInt64($carry8); $f[8] = $f[8]->subInt64($carry8->shiftLeft(26)); $carry9 = $f[9]->shiftRight(25); $f[9] = $f[9]->subInt64($carry9->shiftLeft(25)); $h0 = $f[0]->toInt32()->toInt(); $h1 = $f[1]->toInt32()->toInt(); $h2 = $f[2]->toInt32()->toInt(); $h3 = $f[3]->toInt32()->toInt(); $h4 = $f[4]->toInt32()->toInt(); $h5 = $f[5]->toInt32()->toInt(); $h6 = $f[6]->toInt32()->toInt(); $h7 = $f[7]->toInt32()->toInt(); $h8 = $f[8]->toInt32()->toInt(); $h9 = $f[9]->toInt32()->toInt(); /** * @var array<int, int> */ $s = array( (int) (($h0 >> 0) & 0xff), (int) (($h0 >> 8) & 0xff), (int) (($h0 >> 16) & 0xff), (int) ((($h0 >> 24) | ($h1 << 2)) & 0xff), (int) (($h1 >> 6) & 0xff), (int) (($h1 >> 14) & 0xff), (int) ((($h1 >> 22) | ($h2 << 3)) & 0xff), (int) (($h2 >> 5) & 0xff), (int) (($h2 >> 13) & 0xff), (int) ((($h2 >> 21) | ($h3 << 5)) & 0xff), (int) (($h3 >> 3) & 0xff), (int) (($h3 >> 11) & 0xff), (int) ((($h3 >> 19) | ($h4 << 6)) & 0xff), (int) (($h4 >> 2) & 0xff), (int) (($h4 >> 10) & 0xff), (int) (($h4 >> 18) & 0xff), (int) (($h5 >> 0) & 0xff), (int) (($h5 >> 8) & 0xff), (int) (($h5 >> 16) & 0xff), (int) ((($h5 >> 24) | ($h6 << 1)) & 0xff), (int) (($h6 >> 7) & 0xff), (int) (($h6 >> 15) & 0xff), (int) ((($h6 >> 23) | ($h7 << 3)) & 0xff), (int) (($h7 >> 5) & 0xff), (int) (($h7 >> 13) & 0xff), (int) ((($h7 >> 21) | ($h8 << 4)) & 0xff), (int) (($h8 >> 4) & 0xff), (int) (($h8 >> 12) & 0xff), (int) ((($h8 >> 20) | ($h9 << 6)) & 0xff), (int) (($h9 >> 2) & 0xff), (int) (($h9 >> 10) & 0xff), (int) (($h9 >> 18) & 0xff) ); return self::intArrayToString($s); } /** * Is a field element negative? (1 = yes, 0 = no. Used in calculations.) * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f * @return int * @throws SodiumException * @throws TypeError */ public static function fe_isnegative(ParagonIE_Sodium_Core32_Curve25519_Fe $f) { $str = self::fe_tobytes($f); return (int) (self::chrToInt($str[0]) & 1); } /** * Returns 0 if this field element results in all NUL bytes. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f * @return bool * @throws SodiumException * @throws TypeError */ public static function fe_isnonzero(ParagonIE_Sodium_Core32_Curve25519_Fe $f) { static $zero; if ($zero === null) { $zero = str_repeat("\x00", 32); } $str = self::fe_tobytes($f); /** @var string $zero */ return !self::verify_32($str, $zero); } /** * Multiply two field elements * * h = f * g * * @internal You should not use this directly from another application * * @security Is multiplication a source of timing leaks? If so, can we do * anything to prevent that from happening? * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f * @param ParagonIE_Sodium_Core32_Curve25519_Fe $g * @return ParagonIE_Sodium_Core32_Curve25519_Fe * @throws SodiumException * @throws TypeError */ public static function fe_mul( ParagonIE_Sodium_Core32_Curve25519_Fe $f, ParagonIE_Sodium_Core32_Curve25519_Fe $g ) { /** * @var ParagonIE_Sodium_Core32_Int32[] $f * @var ParagonIE_Sodium_Core32_Int32[] $g * @var ParagonIE_Sodium_Core32_Int64 $f0 * @var ParagonIE_Sodium_Core32_Int64 $f1 * @var ParagonIE_Sodium_Core32_Int64 $f2 * @var ParagonIE_Sodium_Core32_Int64 $f3 * @var ParagonIE_Sodium_Core32_Int64 $f4 * @var ParagonIE_Sodium_Core32_Int64 $f5 * @var ParagonIE_Sodium_Core32_Int64 $f6 * @var ParagonIE_Sodium_Core32_Int64 $f7 * @var ParagonIE_Sodium_Core32_Int64 $f8 * @var ParagonIE_Sodium_Core32_Int64 $f9 * @var ParagonIE_Sodium_Core32_Int64 $g0 * @var ParagonIE_Sodium_Core32_Int64 $g1 * @var ParagonIE_Sodium_Core32_Int64 $g2 * @var ParagonIE_Sodium_Core32_Int64 $g3 * @var ParagonIE_Sodium_Core32_Int64 $g4 * @var ParagonIE_Sodium_Core32_Int64 $g5 * @var ParagonIE_Sodium_Core32_Int64 $g6 * @var ParagonIE_Sodium_Core32_Int64 $g7 * @var ParagonIE_Sodium_Core32_Int64 $g8 * @var ParagonIE_Sodium_Core32_Int64 $g9 */ $f0 = $f[0]->toInt64(); $f1 = $f[1]->toInt64(); $f2 = $f[2]->toInt64(); $f3 = $f[3]->toInt64(); $f4 = $f[4]->toInt64(); $f5 = $f[5]->toInt64(); $f6 = $f[6]->toInt64(); $f7 = $f[7]->toInt64(); $f8 = $f[8]->toInt64(); $f9 = $f[9]->toInt64(); $g0 = $g[0]->toInt64(); $g1 = $g[1]->toInt64(); $g2 = $g[2]->toInt64(); $g3 = $g[3]->toInt64(); $g4 = $g[4]->toInt64(); $g5 = $g[5]->toInt64(); $g6 = $g[6]->toInt64(); $g7 = $g[7]->toInt64(); $g8 = $g[8]->toInt64(); $g9 = $g[9]->toInt64(); $g1_19 = $g1->mulInt(19, 5); /* 2^4 <= 19 <= 2^5, but we only want 5 bits */ $g2_19 = $g2->mulInt(19, 5); $g3_19 = $g3->mulInt(19, 5); $g4_19 = $g4->mulInt(19, 5); $g5_19 = $g5->mulInt(19, 5); $g6_19 = $g6->mulInt(19, 5); $g7_19 = $g7->mulInt(19, 5); $g8_19 = $g8->mulInt(19, 5); $g9_19 = $g9->mulInt(19, 5); $f1_2 = $f1->shiftLeft(1); $f3_2 = $f3->shiftLeft(1); $f5_2 = $f5->shiftLeft(1); $f7_2 = $f7->shiftLeft(1); $f9_2 = $f9->shiftLeft(1); $f0g0 = $f0->mulInt64($g0, 27); $f0g1 = $f0->mulInt64($g1, 27); $f0g2 = $f0->mulInt64($g2, 27); $f0g3 = $f0->mulInt64($g3, 27); $f0g4 = $f0->mulInt64($g4, 27); $f0g5 = $f0->mulInt64($g5, 27); $f0g6 = $f0->mulInt64($g6, 27); $f0g7 = $f0->mulInt64($g7, 27); $f0g8 = $f0->mulInt64($g8, 27); $f0g9 = $f0->mulInt64($g9, 27); $f1g0 = $f1->mulInt64($g0, 27); $f1g1_2 = $f1_2->mulInt64($g1, 27); $f1g2 = $f1->mulInt64($g2, 27); $f1g3_2 = $f1_2->mulInt64($g3, 27); $f1g4 = $f1->mulInt64($g4, 30); $f1g5_2 = $f1_2->mulInt64($g5, 30); $f1g6 = $f1->mulInt64($g6, 30); $f1g7_2 = $f1_2->mulInt64($g7, 30); $f1g8 = $f1->mulInt64($g8, 30); $f1g9_38 = $g9_19->mulInt64($f1_2, 30); $f2g0 = $f2->mulInt64($g0, 30); $f2g1 = $f2->mulInt64($g1, 29); $f2g2 = $f2->mulInt64($g2, 30); $f2g3 = $f2->mulInt64($g3, 29); $f2g4 = $f2->mulInt64($g4, 30); $f2g5 = $f2->mulInt64($g5, 29); $f2g6 = $f2->mulInt64($g6, 30); $f2g7 = $f2->mulInt64($g7, 29); $f2g8_19 = $g8_19->mulInt64($f2, 30); $f2g9_19 = $g9_19->mulInt64($f2, 30); $f3g0 = $f3->mulInt64($g0, 30); $f3g1_2 = $f3_2->mulInt64($g1, 30); $f3g2 = $f3->mulInt64($g2, 30); $f3g3_2 = $f3_2->mulInt64($g3, 30); $f3g4 = $f3->mulInt64($g4, 30); $f3g5_2 = $f3_2->mulInt64($g5, 30); $f3g6 = $f3->mulInt64($g6, 30); $f3g7_38 = $g7_19->mulInt64($f3_2, 30); $f3g8_19 = $g8_19->mulInt64($f3, 30); $f3g9_38 = $g9_19->mulInt64($f3_2, 30); $f4g0 = $f4->mulInt64($g0, 30); $f4g1 = $f4->mulInt64($g1, 30); $f4g2 = $f4->mulInt64($g2, 30); $f4g3 = $f4->mulInt64($g3, 30); $f4g4 = $f4->mulInt64($g4, 30); $f4g5 = $f4->mulInt64($g5, 30); $f4g6_19 = $g6_19->mulInt64($f4, 30); $f4g7_19 = $g7_19->mulInt64($f4, 30); $f4g8_19 = $g8_19->mulInt64($f4, 30); $f4g9_19 = $g9_19->mulInt64($f4, 30); $f5g0 = $f5->mulInt64($g0, 30); $f5g1_2 = $f5_2->mulInt64($g1, 30); $f5g2 = $f5->mulInt64($g2, 30); $f5g3_2 = $f5_2->mulInt64($g3, 30); $f5g4 = $f5->mulInt64($g4, 30); $f5g5_38 = $g5_19->mulInt64($f5_2, 30); $f5g6_19 = $g6_19->mulInt64($f5, 30); $f5g7_38 = $g7_19->mulInt64($f5_2, 30); $f5g8_19 = $g8_19->mulInt64($f5, 30); $f5g9_38 = $g9_19->mulInt64($f5_2, 30); $f6g0 = $f6->mulInt64($g0, 30); $f6g1 = $f6->mulInt64($g1, 30); $f6g2 = $f6->mulInt64($g2, 30); $f6g3 = $f6->mulInt64($g3, 30); $f6g4_19 = $g4_19->mulInt64($f6, 30); $f6g5_19 = $g5_19->mulInt64($f6, 30); $f6g6_19 = $g6_19->mulInt64($f6, 30); $f6g7_19 = $g7_19->mulInt64($f6, 30); $f6g8_19 = $g8_19->mulInt64($f6, 30); $f6g9_19 = $g9_19->mulInt64($f6, 30); $f7g0 = $f7->mulInt64($g0, 30); $f7g1_2 = $g1->mulInt64($f7_2, 30); $f7g2 = $f7->mulInt64($g2, 30); $f7g3_38 = $g3_19->mulInt64($f7_2, 30); $f7g4_19 = $g4_19->mulInt64($f7, 30); $f7g5_38 = $g5_19->mulInt64($f7_2, 30); $f7g6_19 = $g6_19->mulInt64($f7, 30); $f7g7_38 = $g7_19->mulInt64($f7_2, 30); $f7g8_19 = $g8_19->mulInt64($f7, 30); $f7g9_38 = $g9_19->mulInt64($f7_2, 30); $f8g0 = $f8->mulInt64($g0, 30); $f8g1 = $f8->mulInt64($g1, 29); $f8g2_19 = $g2_19->mulInt64($f8, 30); $f8g3_19 = $g3_19->mulInt64($f8, 30); $f8g4_19 = $g4_19->mulInt64($f8, 30); $f8g5_19 = $g5_19->mulInt64($f8, 30); $f8g6_19 = $g6_19->mulInt64($f8, 30); $f8g7_19 = $g7_19->mulInt64($f8, 30); $f8g8_19 = $g8_19->mulInt64($f8, 30); $f8g9_19 = $g9_19->mulInt64($f8, 30); $f9g0 = $f9->mulInt64($g0, 30); $f9g1_38 = $g1_19->mulInt64($f9_2, 30); $f9g2_19 = $g2_19->mulInt64($f9, 30); $f9g3_38 = $g3_19->mulInt64($f9_2, 30); $f9g4_19 = $g4_19->mulInt64($f9, 30); $f9g5_38 = $g5_19->mulInt64($f9_2, 30); $f9g6_19 = $g6_19->mulInt64($f9, 30); $f9g7_38 = $g7_19->mulInt64($f9_2, 30); $f9g8_19 = $g8_19->mulInt64($f9, 30); $f9g9_38 = $g9_19->mulInt64($f9_2, 30); // $h0 = $f0g0 + $f1g9_38 + $f2g8_19 + $f3g7_38 + $f4g6_19 + $f5g5_38 + $f6g4_19 + $f7g3_38 + $f8g2_19 + $f9g1_38; $h0 = $f0g0->addInt64($f1g9_38)->addInt64($f2g8_19)->addInt64($f3g7_38) ->addInt64($f4g6_19)->addInt64($f5g5_38)->addInt64($f6g4_19) ->addInt64($f7g3_38)->addInt64($f8g2_19)->addInt64($f9g1_38); // $h1 = $f0g1 + $f1g0 + $f2g9_19 + $f3g8_19 + $f4g7_19 + $f5g6_19 + $f6g5_19 + $f7g4_19 + $f8g3_19 + $f9g2_19; $h1 = $f0g1->addInt64($f1g0)->addInt64($f2g9_19)->addInt64($f3g8_19) ->addInt64($f4g7_19)->addInt64($f5g6_19)->addInt64($f6g5_19) ->addInt64($f7g4_19)->addInt64($f8g3_19)->addInt64($f9g2_19); // $h2 = $f0g2 + $f1g1_2 + $f2g0 + $f3g9_38 + $f4g8_19 + $f5g7_38 + $f6g6_19 + $f7g5_38 + $f8g4_19 + $f9g3_38; $h2 = $f0g2->addInt64($f1g1_2)->addInt64($f2g0)->addInt64($f3g9_38) ->addInt64($f4g8_19)->addInt64($f5g7_38)->addInt64($f6g6_19) ->addInt64($f7g5_38)->addInt64($f8g4_19)->addInt64($f9g3_38); // $h3 = $f0g3 + $f1g2 + $f2g1 + $f3g0 + $f4g9_19 + $f5g8_19 + $f6g7_19 + $f7g6_19 + $f8g5_19 + $f9g4_19; $h3 = $f0g3->addInt64($f1g2)->addInt64($f2g1)->addInt64($f3g0) ->addInt64($f4g9_19)->addInt64($f5g8_19)->addInt64($f6g7_19) ->addInt64($f7g6_19)->addInt64($f8g5_19)->addInt64($f9g4_19); // $h4 = $f0g4 + $f1g3_2 + $f2g2 + $f3g1_2 + $f4g0 + $f5g9_38 + $f6g8_19 + $f7g7_38 + $f8g6_19 + $f9g5_38; $h4 = $f0g4->addInt64($f1g3_2)->addInt64($f2g2)->addInt64($f3g1_2) ->addInt64($f4g0)->addInt64($f5g9_38)->addInt64($f6g8_19) ->addInt64($f7g7_38)->addInt64($f8g6_19)->addInt64($f9g5_38); // $h5 = $f0g5 + $f1g4 + $f2g3 + $f3g2 + $f4g1 + $f5g0 + $f6g9_19 + $f7g8_19 + $f8g7_19 + $f9g6_19; $h5 = $f0g5->addInt64($f1g4)->addInt64($f2g3)->addInt64($f3g2) ->addInt64($f4g1)->addInt64($f5g0)->addInt64($f6g9_19) ->addInt64($f7g8_19)->addInt64($f8g7_19)->addInt64($f9g6_19); // $h6 = $f0g6 + $f1g5_2 + $f2g4 + $f3g3_2 + $f4g2 + $f5g1_2 + $f6g0 + $f7g9_38 + $f8g8_19 + $f9g7_38; $h6 = $f0g6->addInt64($f1g5_2)->addInt64($f2g4)->addInt64($f3g3_2) ->addInt64($f4g2)->addInt64($f5g1_2)->addInt64($f6g0) ->addInt64($f7g9_38)->addInt64($f8g8_19)->addInt64($f9g7_38); // $h7 = $f0g7 + $f1g6 + $f2g5 + $f3g4 + $f4g3 + $f5g2 + $f6g1 + $f7g0 + $f8g9_19 + $f9g8_19; $h7 = $f0g7->addInt64($f1g6)->addInt64($f2g5)->addInt64($f3g4) ->addInt64($f4g3)->addInt64($f5g2)->addInt64($f6g1) ->addInt64($f7g0)->addInt64($f8g9_19)->addInt64($f9g8_19); // $h8 = $f0g8 + $f1g7_2 + $f2g6 + $f3g5_2 + $f4g4 + $f5g3_2 + $f6g2 + $f7g1_2 + $f8g0 + $f9g9_38; $h8 = $f0g8->addInt64($f1g7_2)->addInt64($f2g6)->addInt64($f3g5_2) ->addInt64($f4g4)->addInt64($f5g3_2)->addInt64($f6g2) ->addInt64($f7g1_2)->addInt64($f8g0)->addInt64($f9g9_38); // $h9 = $f0g9 + $f1g8 + $f2g7 + $f3g6 + $f4g5 + $f5g4 + $f6g3 + $f7g2 + $f8g1 + $f9g0 ; $h9 = $f0g9->addInt64($f1g8)->addInt64($f2g7)->addInt64($f3g6) ->addInt64($f4g5)->addInt64($f5g4)->addInt64($f6g3) ->addInt64($f7g2)->addInt64($f8g1)->addInt64($f9g0); /** * @var ParagonIE_Sodium_Core32_Int64 $h0 * @var ParagonIE_Sodium_Core32_Int64 $h1 * @var ParagonIE_Sodium_Core32_Int64 $h2 * @var ParagonIE_Sodium_Core32_Int64 $h3 * @var ParagonIE_Sodium_Core32_Int64 $h4 * @var ParagonIE_Sodium_Core32_Int64 $h5 * @var ParagonIE_Sodium_Core32_Int64 $h6 * @var ParagonIE_Sodium_Core32_Int64 $h7 * @var ParagonIE_Sodium_Core32_Int64 $h8 * @var ParagonIE_Sodium_Core32_Int64 $h9 * @var ParagonIE_Sodium_Core32_Int64 $carry0 * @var ParagonIE_Sodium_Core32_Int64 $carry1 * @var ParagonIE_Sodium_Core32_Int64 $carry2 * @var ParagonIE_Sodium_Core32_Int64 $carry3 * @var ParagonIE_Sodium_Core32_Int64 $carry4 * @var ParagonIE_Sodium_Core32_Int64 $carry5 * @var ParagonIE_Sodium_Core32_Int64 $carry6 * @var ParagonIE_Sodium_Core32_Int64 $carry7 * @var ParagonIE_Sodium_Core32_Int64 $carry8 * @var ParagonIE_Sodium_Core32_Int64 $carry9 */ $carry0 = $h0->addInt(1 << 25)->shiftRight(26); $h1 = $h1->addInt64($carry0); $h0 = $h0->subInt64($carry0->shiftLeft(26)); $carry4 = $h4->addInt(1 << 25)->shiftRight(26); $h5 = $h5->addInt64($carry4); $h4 = $h4->subInt64($carry4->shiftLeft(26)); $carry1 = $h1->addInt(1 << 24)->shiftRight(25); $h2 = $h2->addInt64($carry1); $h1 = $h1->subInt64($carry1->shiftLeft(25)); $carry5 = $h5->addInt(1 << 24)->shiftRight(25); $h6 = $h6->addInt64($carry5); $h5 = $h5->subInt64($carry5->shiftLeft(25)); $carry2 = $h2->addInt(1 << 25)->shiftRight(26); $h3 = $h3->addInt64($carry2); $h2 = $h2->subInt64($carry2->shiftLeft(26)); $carry6 = $h6->addInt(1 << 25)->shiftRight(26); $h7 = $h7->addInt64($carry6); $h6 = $h6->subInt64($carry6->shiftLeft(26)); $carry3 = $h3->addInt(1 << 24)->shiftRight(25); $h4 = $h4->addInt64($carry3); $h3 = $h3->subInt64($carry3->shiftLeft(25)); $carry7 = $h7->addInt(1 << 24)->shiftRight(25); $h8 = $h8->addInt64($carry7); $h7 = $h7->subInt64($carry7->shiftLeft(25)); $carry4 = $h4->addInt(1 << 25)->shiftRight(26); $h5 = $h5->addInt64($carry4); $h4 = $h4->subInt64($carry4->shiftLeft(26)); $carry8 = $h8->addInt(1 << 25)->shiftRight(26); $h9 = $h9->addInt64($carry8); $h8 = $h8->subInt64($carry8->shiftLeft(26)); $carry9 = $h9->addInt(1 << 24)->shiftRight(25); $h0 = $h0->addInt64($carry9->mulInt(19, 5)); $h9 = $h9->subInt64($carry9->shiftLeft(25)); $carry0 = $h0->addInt(1 << 25)->shiftRight(26); $h1 = $h1->addInt64($carry0); $h0 = $h0->subInt64($carry0->shiftLeft(26)); return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array( $h0->toInt32(), $h1->toInt32(), $h2->toInt32(), $h3->toInt32(), $h4->toInt32(), $h5->toInt32(), $h6->toInt32(), $h7->toInt32(), $h8->toInt32(), $h9->toInt32() ) ); } /** * Get the negative values for each piece of the field element. * * h = -f * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f * @return ParagonIE_Sodium_Core32_Curve25519_Fe * @psalm-suppress MixedAssignment * @psalm-suppress MixedMethodCall */ public static function fe_neg(ParagonIE_Sodium_Core32_Curve25519_Fe $f) { $h = new ParagonIE_Sodium_Core32_Curve25519_Fe(); for ($i = 0; $i < 10; ++$i) { $h[$i] = $h[$i]->subInt32($f[$i]); } return $h; } /** * Square a field element * * h = f * f * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f * @return ParagonIE_Sodium_Core32_Curve25519_Fe * @throws SodiumException * @throws TypeError * @psalm-suppress MixedMethodCall */ public static function fe_sq(ParagonIE_Sodium_Core32_Curve25519_Fe $f) { $f0 = $f[0]->toInt64(); $f1 = $f[1]->toInt64(); $f2 = $f[2]->toInt64(); $f3 = $f[3]->toInt64(); $f4 = $f[4]->toInt64(); $f5 = $f[5]->toInt64(); $f6 = $f[6]->toInt64(); $f7 = $f[7]->toInt64(); $f8 = $f[8]->toInt64(); $f9 = $f[9]->toInt64(); $f0_2 = $f0->shiftLeft(1); $f1_2 = $f1->shiftLeft(1); $f2_2 = $f2->shiftLeft(1); $f3_2 = $f3->shiftLeft(1); $f4_2 = $f4->shiftLeft(1); $f5_2 = $f5->shiftLeft(1); $f6_2 = $f6->shiftLeft(1); $f7_2 = $f7->shiftLeft(1); $f5_38 = $f5->mulInt(38, 6); $f6_19 = $f6->mulInt(19, 5); $f7_38 = $f7->mulInt(38, 6); $f8_19 = $f8->mulInt(19, 5); $f9_38 = $f9->mulInt(38, 6); $f0f0 = $f0->mulInt64($f0, 28); $f0f1_2 = $f0_2->mulInt64($f1, 28); $f0f2_2 = $f0_2->mulInt64($f2, 28); $f0f3_2 = $f0_2->mulInt64($f3, 28); $f0f4_2 = $f0_2->mulInt64($f4, 28); $f0f5_2 = $f0_2->mulInt64($f5, 28); $f0f6_2 = $f0_2->mulInt64($f6, 28); $f0f7_2 = $f0_2->mulInt64($f7, 28); $f0f8_2 = $f0_2->mulInt64($f8, 28); $f0f9_2 = $f0_2->mulInt64($f9, 28); $f1f1_2 = $f1_2->mulInt64($f1, 28); $f1f2_2 = $f1_2->mulInt64($f2, 28); $f1f3_4 = $f1_2->mulInt64($f3_2, 28); $f1f4_2 = $f1_2->mulInt64($f4, 28); $f1f5_4 = $f1_2->mulInt64($f5_2, 30); $f1f6_2 = $f1_2->mulInt64($f6, 28); $f1f7_4 = $f1_2->mulInt64($f7_2, 28); $f1f8_2 = $f1_2->mulInt64($f8, 28); $f1f9_76 = $f9_38->mulInt64($f1_2, 30); $f2f2 = $f2->mulInt64($f2, 28); $f2f3_2 = $f2_2->mulInt64($f3, 28); $f2f4_2 = $f2_2->mulInt64($f4, 28); $f2f5_2 = $f2_2->mulInt64($f5, 28); $f2f6_2 = $f2_2->mulInt64($f6, 28); $f2f7_2 = $f2_2->mulInt64($f7, 28); $f2f8_38 = $f8_19->mulInt64($f2_2, 30); $f2f9_38 = $f9_38->mulInt64($f2, 30); $f3f3_2 = $f3_2->mulInt64($f3, 28); $f3f4_2 = $f3_2->mulInt64($f4, 28); $f3f5_4 = $f3_2->mulInt64($f5_2, 30); $f3f6_2 = $f3_2->mulInt64($f6, 28); $f3f7_76 = $f7_38->mulInt64($f3_2, 30); $f3f8_38 = $f8_19->mulInt64($f3_2, 30); $f3f9_76 = $f9_38->mulInt64($f3_2, 30); $f4f4 = $f4->mulInt64($f4, 28); $f4f5_2 = $f4_2->mulInt64($f5, 28); $f4f6_38 = $f6_19->mulInt64($f4_2, 30); $f4f7_38 = $f7_38->mulInt64($f4, 30); $f4f8_38 = $f8_19->mulInt64($f4_2, 30); $f4f9_38 = $f9_38->mulInt64($f4, 30); $f5f5_38 = $f5_38->mulInt64($f5, 30); $f5f6_38 = $f6_19->mulInt64($f5_2, 30); $f5f7_76 = $f7_38->mulInt64($f5_2, 30); $f5f8_38 = $f8_19->mulInt64($f5_2, 30); $f5f9_76 = $f9_38->mulInt64($f5_2, 30); $f6f6_19 = $f6_19->mulInt64($f6, 30); $f6f7_38 = $f7_38->mulInt64($f6, 30); $f6f8_38 = $f8_19->mulInt64($f6_2, 30); $f6f9_38 = $f9_38->mulInt64($f6, 30); $f7f7_38 = $f7_38->mulInt64($f7, 28); $f7f8_38 = $f8_19->mulInt64($f7_2, 30); $f7f9_76 = $f9_38->mulInt64($f7_2, 30); $f8f8_19 = $f8_19->mulInt64($f8, 30); $f8f9_38 = $f9_38->mulInt64($f8, 30); $f9f9_38 = $f9_38->mulInt64($f9, 28); $h0 = $f0f0->addInt64($f1f9_76)->addInt64($f2f8_38)->addInt64($f3f7_76)->addInt64($f4f6_38)->addInt64($f5f5_38); $h1 = $f0f1_2->addInt64($f2f9_38)->addInt64($f3f8_38)->addInt64($f4f7_38)->addInt64($f5f6_38); $h2 = $f0f2_2->addInt64($f1f1_2)->addInt64($f3f9_76)->addInt64($f4f8_38)->addInt64($f5f7_76)->addInt64($f6f6_19); $h3 = $f0f3_2->addInt64($f1f2_2)->addInt64($f4f9_38)->addInt64($f5f8_38)->addInt64($f6f7_38); $h4 = $f0f4_2->addInt64($f1f3_4)->addInt64($f2f2)->addInt64($f5f9_76)->addInt64($f6f8_38)->addInt64($f7f7_38); $h5 = $f0f5_2->addInt64($f1f4_2)->addInt64($f2f3_2)->addInt64($f6f9_38)->addInt64($f7f8_38); $h6 = $f0f6_2->addInt64($f1f5_4)->addInt64($f2f4_2)->addInt64($f3f3_2)->addInt64($f7f9_76)->addInt64($f8f8_19); $h7 = $f0f7_2->addInt64($f1f6_2)->addInt64($f2f5_2)->addInt64($f3f4_2)->addInt64($f8f9_38); $h8 = $f0f8_2->addInt64($f1f7_4)->addInt64($f2f6_2)->addInt64($f3f5_4)->addInt64($f4f4)->addInt64($f9f9_38); $h9 = $f0f9_2->addInt64($f1f8_2)->addInt64($f2f7_2)->addInt64($f3f6_2)->addInt64($f4f5_2); /** * @var ParagonIE_Sodium_Core32_Int64 $h0 * @var ParagonIE_Sodium_Core32_Int64 $h1 * @var ParagonIE_Sodium_Core32_Int64 $h2 * @var ParagonIE_Sodium_Core32_Int64 $h3 * @var ParagonIE_Sodium_Core32_Int64 $h4 * @var ParagonIE_Sodium_Core32_Int64 $h5 * @var ParagonIE_Sodium_Core32_Int64 $h6 * @var ParagonIE_Sodium_Core32_Int64 $h7 * @var ParagonIE_Sodium_Core32_Int64 $h8 * @var ParagonIE_Sodium_Core32_Int64 $h9 */ $carry0 = $h0->addInt(1 << 25)->shiftRight(26); $h1 = $h1->addInt64($carry0); $h0 = $h0->subInt64($carry0->shiftLeft(26)); $carry4 = $h4->addInt(1 << 25)->shiftRight(26); $h5 = $h5->addInt64($carry4); $h4 = $h4->subInt64($carry4->shiftLeft(26)); $carry1 = $h1->addInt(1 << 24)->shiftRight(25); $h2 = $h2->addInt64($carry1); $h1 = $h1->subInt64($carry1->shiftLeft(25)); $carry5 = $h5->addInt(1 << 24)->shiftRight(25); $h6 = $h6->addInt64($carry5); $h5 = $h5->subInt64($carry5->shiftLeft(25)); $carry2 = $h2->addInt(1 << 25)->shiftRight(26); $h3 = $h3->addInt64($carry2); $h2 = $h2->subInt64($carry2->shiftLeft(26)); $carry6 = $h6->addInt(1 << 25)->shiftRight(26); $h7 = $h7->addInt64($carry6); $h6 = $h6->subInt64($carry6->shiftLeft(26)); $carry3 = $h3->addInt(1 << 24)->shiftRight(25); $h4 = $h4->addInt64($carry3); $h3 = $h3->subInt64($carry3->shiftLeft(25)); $carry7 = $h7->addInt(1 << 24)->shiftRight(25); $h8 = $h8->addInt64($carry7); $h7 = $h7->subInt64($carry7->shiftLeft(25)); $carry4 = $h4->addInt(1 << 25)->shiftRight(26); $h5 = $h5->addInt64($carry4); $h4 = $h4->subInt64($carry4->shiftLeft(26)); $carry8 = $h8->addInt(1 << 25)->shiftRight(26); $h9 = $h9->addInt64($carry8); $h8 = $h8->subInt64($carry8->shiftLeft(26)); $carry9 = $h9->addInt(1 << 24)->shiftRight(25); $h0 = $h0->addInt64($carry9->mulInt(19, 5)); $h9 = $h9->subInt64($carry9->shiftLeft(25)); $carry0 = $h0->addInt(1 << 25)->shiftRight(26); $h1 = $h1->addInt64($carry0); $h0 = $h0->subInt64($carry0->shiftLeft(26)); return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array( $h0->toInt32(), $h1->toInt32(), $h2->toInt32(), $h3->toInt32(), $h4->toInt32(), $h5->toInt32(), $h6->toInt32(), $h7->toInt32(), $h8->toInt32(), $h9->toInt32() ) ); } /** * Square and double a field element * * h = 2 * f * f * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f * @return ParagonIE_Sodium_Core32_Curve25519_Fe * @throws SodiumException * @throws TypeError * @psalm-suppress MixedMethodCall */ public static function fe_sq2(ParagonIE_Sodium_Core32_Curve25519_Fe $f) { $f0 = $f[0]->toInt64(); $f1 = $f[1]->toInt64(); $f2 = $f[2]->toInt64(); $f3 = $f[3]->toInt64(); $f4 = $f[4]->toInt64(); $f5 = $f[5]->toInt64(); $f6 = $f[6]->toInt64(); $f7 = $f[7]->toInt64(); $f8 = $f[8]->toInt64(); $f9 = $f[9]->toInt64(); $f0_2 = $f0->shiftLeft(1); $f1_2 = $f1->shiftLeft(1); $f2_2 = $f2->shiftLeft(1); $f3_2 = $f3->shiftLeft(1); $f4_2 = $f4->shiftLeft(1); $f5_2 = $f5->shiftLeft(1); $f6_2 = $f6->shiftLeft(1); $f7_2 = $f7->shiftLeft(1); $f5_38 = $f5->mulInt(38, 6); /* 1.959375*2^30 */ $f6_19 = $f6->mulInt(19, 5); /* 1.959375*2^30 */ $f7_38 = $f7->mulInt(38, 6); /* 1.959375*2^30 */ $f8_19 = $f8->mulInt(19, 5); /* 1.959375*2^30 */ $f9_38 = $f9->mulInt(38, 6); /* 1.959375*2^30 */ $f0f0 = $f0->mulInt64($f0, 28); $f0f1_2 = $f0_2->mulInt64($f1, 28); $f0f2_2 = $f0_2->mulInt64($f2, 28); $f0f3_2 = $f0_2->mulInt64($f3, 28); $f0f4_2 = $f0_2->mulInt64($f4, 28); $f0f5_2 = $f0_2->mulInt64($f5, 28); $f0f6_2 = $f0_2->mulInt64($f6, 28); $f0f7_2 = $f0_2->mulInt64($f7, 28); $f0f8_2 = $f0_2->mulInt64($f8, 28); $f0f9_2 = $f0_2->mulInt64($f9, 28); $f1f1_2 = $f1_2->mulInt64($f1, 28); $f1f2_2 = $f1_2->mulInt64($f2, 28); $f1f3_4 = $f1_2->mulInt64($f3_2, 29); $f1f4_2 = $f1_2->mulInt64($f4, 28); $f1f5_4 = $f1_2->mulInt64($f5_2, 29); $f1f6_2 = $f1_2->mulInt64($f6, 28); $f1f7_4 = $f1_2->mulInt64($f7_2, 29); $f1f8_2 = $f1_2->mulInt64($f8, 28); $f1f9_76 = $f9_38->mulInt64($f1_2, 29); $f2f2 = $f2->mulInt64($f2, 28); $f2f3_2 = $f2_2->mulInt64($f3, 28); $f2f4_2 = $f2_2->mulInt64($f4, 28); $f2f5_2 = $f2_2->mulInt64($f5, 28); $f2f6_2 = $f2_2->mulInt64($f6, 28); $f2f7_2 = $f2_2->mulInt64($f7, 28); $f2f8_38 = $f8_19->mulInt64($f2_2, 29); $f2f9_38 = $f9_38->mulInt64($f2, 29); $f3f3_2 = $f3_2->mulInt64($f3, 28); $f3f4_2 = $f3_2->mulInt64($f4, 28); $f3f5_4 = $f3_2->mulInt64($f5_2, 28); $f3f6_2 = $f3_2->mulInt64($f6, 28); $f3f7_76 = $f7_38->mulInt64($f3_2, 29); $f3f8_38 = $f8_19->mulInt64($f3_2, 29); $f3f9_76 = $f9_38->mulInt64($f3_2, 29); $f4f4 = $f4->mulInt64($f4, 28); $f4f5_2 = $f4_2->mulInt64($f5, 28); $f4f6_38 = $f6_19->mulInt64($f4_2, 29); $f4f7_38 = $f7_38->mulInt64($f4, 29); $f4f8_38 = $f8_19->mulInt64($f4_2, 29); $f4f9_38 = $f9_38->mulInt64($f4, 29); $f5f5_38 = $f5_38->mulInt64($f5, 29); $f5f6_38 = $f6_19->mulInt64($f5_2, 29); $f5f7_76 = $f7_38->mulInt64($f5_2, 29); $f5f8_38 = $f8_19->mulInt64($f5_2, 29); $f5f9_76 = $f9_38->mulInt64($f5_2, 29); $f6f6_19 = $f6_19->mulInt64($f6, 29); $f6f7_38 = $f7_38->mulInt64($f6, 29); $f6f8_38 = $f8_19->mulInt64($f6_2, 29); $f6f9_38 = $f9_38->mulInt64($f6, 29); $f7f7_38 = $f7_38->mulInt64($f7, 29); $f7f8_38 = $f8_19->mulInt64($f7_2, 29); $f7f9_76 = $f9_38->mulInt64($f7_2, 29); $f8f8_19 = $f8_19->mulInt64($f8, 29); $f8f9_38 = $f9_38->mulInt64($f8, 29); $f9f9_38 = $f9_38->mulInt64($f9, 29); $h0 = $f0f0->addInt64($f1f9_76)->addInt64($f2f8_38)->addInt64($f3f7_76)->addInt64($f4f6_38)->addInt64($f5f5_38); $h1 = $f0f1_2->addInt64($f2f9_38)->addInt64($f3f8_38)->addInt64($f4f7_38)->addInt64($f5f6_38); $h2 = $f0f2_2->addInt64($f1f1_2)->addInt64($f3f9_76)->addInt64($f4f8_38)->addInt64($f5f7_76)->addInt64($f6f6_19); $h3 = $f0f3_2->addInt64($f1f2_2)->addInt64($f4f9_38)->addInt64($f5f8_38)->addInt64($f6f7_38); $h4 = $f0f4_2->addInt64($f1f3_4)->addInt64($f2f2)->addInt64($f5f9_76)->addInt64($f6f8_38)->addInt64($f7f7_38); $h5 = $f0f5_2->addInt64($f1f4_2)->addInt64($f2f3_2)->addInt64($f6f9_38)->addInt64($f7f8_38); $h6 = $f0f6_2->addInt64($f1f5_4)->addInt64($f2f4_2)->addInt64($f3f3_2)->addInt64($f7f9_76)->addInt64($f8f8_19); $h7 = $f0f7_2->addInt64($f1f6_2)->addInt64($f2f5_2)->addInt64($f3f4_2)->addInt64($f8f9_38); $h8 = $f0f8_2->addInt64($f1f7_4)->addInt64($f2f6_2)->addInt64($f3f5_4)->addInt64($f4f4)->addInt64($f9f9_38); $h9 = $f0f9_2->addInt64($f1f8_2)->addInt64($f2f7_2)->addInt64($f3f6_2)->addInt64($f4f5_2); /** * @var ParagonIE_Sodium_Core32_Int64 $h0 * @var ParagonIE_Sodium_Core32_Int64 $h1 * @var ParagonIE_Sodium_Core32_Int64 $h2 * @var ParagonIE_Sodium_Core32_Int64 $h3 * @var ParagonIE_Sodium_Core32_Int64 $h4 * @var ParagonIE_Sodium_Core32_Int64 $h5 * @var ParagonIE_Sodium_Core32_Int64 $h6 * @var ParagonIE_Sodium_Core32_Int64 $h7 * @var ParagonIE_Sodium_Core32_Int64 $h8 * @var ParagonIE_Sodium_Core32_Int64 $h9 */ $h0 = $h0->shiftLeft(1); $h1 = $h1->shiftLeft(1); $h2 = $h2->shiftLeft(1); $h3 = $h3->shiftLeft(1); $h4 = $h4->shiftLeft(1); $h5 = $h5->shiftLeft(1); $h6 = $h6->shiftLeft(1); $h7 = $h7->shiftLeft(1); $h8 = $h8->shiftLeft(1); $h9 = $h9->shiftLeft(1); $carry0 = $h0->addInt(1 << 25)->shiftRight(26); $h1 = $h1->addInt64($carry0); $h0 = $h0->subInt64($carry0->shiftLeft(26)); $carry4 = $h4->addInt(1 << 25)->shiftRight(26); $h5 = $h5->addInt64($carry4); $h4 = $h4->subInt64($carry4->shiftLeft(26)); $carry1 = $h1->addInt(1 << 24)->shiftRight(25); $h2 = $h2->addInt64($carry1); $h1 = $h1->subInt64($carry1->shiftLeft(25)); $carry5 = $h5->addInt(1 << 24)->shiftRight(25); $h6 = $h6->addInt64($carry5); $h5 = $h5->subInt64($carry5->shiftLeft(25)); $carry2 = $h2->addInt(1 << 25)->shiftRight(26); $h3 = $h3->addInt64($carry2); $h2 = $h2->subInt64($carry2->shiftLeft(26)); $carry6 = $h6->addInt(1 << 25)->shiftRight(26); $h7 = $h7->addInt64($carry6); $h6 = $h6->subInt64($carry6->shiftLeft(26)); $carry3 = $h3->addInt(1 << 24)->shiftRight(25); $h4 = $h4->addInt64($carry3); $h3 = $h3->subInt64($carry3->shiftLeft(25)); $carry7 = $h7->addInt(1 << 24)->shiftRight(25); $h8 = $h8->addInt64($carry7); $h7 = $h7->subInt64($carry7->shiftLeft(25)); $carry4 = $h4->addInt(1 << 25)->shiftRight(26); $h5 = $h5->addInt64($carry4); $h4 = $h4->subInt64($carry4->shiftLeft(26)); $carry8 = $h8->addInt(1 << 25)->shiftRight(26); $h9 = $h9->addInt64($carry8); $h8 = $h8->subInt64($carry8->shiftLeft(26)); $carry9 = $h9->addInt(1 << 24)->shiftRight(25); $h0 = $h0->addInt64($carry9->mulInt(19, 5)); $h9 = $h9->subInt64($carry9->shiftLeft(25)); $carry0 = $h0->addInt(1 << 25)->shiftRight(26); $h1 = $h1->addInt64($carry0); $h0 = $h0->subInt64($carry0->shiftLeft(26)); return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array( $h0->toInt32(), $h1->toInt32(), $h2->toInt32(), $h3->toInt32(), $h4->toInt32(), $h5->toInt32(), $h6->toInt32(), $h7->toInt32(), $h8->toInt32(), $h9->toInt32() ) ); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $Z * @return ParagonIE_Sodium_Core32_Curve25519_Fe * @throws SodiumException * @throws TypeError */ public static function fe_invert(ParagonIE_Sodium_Core32_Curve25519_Fe $Z) { $z = clone $Z; $t0 = self::fe_sq($z); $t1 = self::fe_sq($t0); $t1 = self::fe_sq($t1); $t1 = self::fe_mul($z, $t1); $t0 = self::fe_mul($t0, $t1); $t2 = self::fe_sq($t0); $t1 = self::fe_mul($t1, $t2); $t2 = self::fe_sq($t1); for ($i = 1; $i < 5; ++$i) { $t2 = self::fe_sq($t2); } $t1 = self::fe_mul($t2, $t1); $t2 = self::fe_sq($t1); for ($i = 1; $i < 10; ++$i) { $t2 = self::fe_sq($t2); } $t2 = self::fe_mul($t2, $t1); $t3 = self::fe_sq($t2); for ($i = 1; $i < 20; ++$i) { $t3 = self::fe_sq($t3); } $t2 = self::fe_mul($t3, $t2); $t2 = self::fe_sq($t2); for ($i = 1; $i < 10; ++$i) { $t2 = self::fe_sq($t2); } $t1 = self::fe_mul($t2, $t1); $t2 = self::fe_sq($t1); for ($i = 1; $i < 50; ++$i) { $t2 = self::fe_sq($t2); } $t2 = self::fe_mul($t2, $t1); $t3 = self::fe_sq($t2); for ($i = 1; $i < 100; ++$i) { $t3 = self::fe_sq($t3); } $t2 = self::fe_mul($t3, $t2); $t2 = self::fe_sq($t2); for ($i = 1; $i < 50; ++$i) { $t2 = self::fe_sq($t2); } $t1 = self::fe_mul($t2, $t1); $t1 = self::fe_sq($t1); for ($i = 1; $i < 5; ++$i) { $t1 = self::fe_sq($t1); } return self::fe_mul($t1, $t0); } /** * @internal You should not use this directly from another application * * @ref https://github.com/jedisct1/libsodium/blob/68564326e1e9dc57ef03746f85734232d20ca6fb/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.c#L1054-L1106 * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $z * @return ParagonIE_Sodium_Core32_Curve25519_Fe * @throws SodiumException * @throws TypeError */ public static function fe_pow22523(ParagonIE_Sodium_Core32_Curve25519_Fe $z) { # fe_sq(t0, z); # fe_sq(t1, t0); # fe_sq(t1, t1); # fe_mul(t1, z, t1); # fe_mul(t0, t0, t1); # fe_sq(t0, t0); # fe_mul(t0, t1, t0); # fe_sq(t1, t0); $t0 = self::fe_sq($z); $t1 = self::fe_sq($t0); $t1 = self::fe_sq($t1); $t1 = self::fe_mul($z, $t1); $t0 = self::fe_mul($t0, $t1); $t0 = self::fe_sq($t0); $t0 = self::fe_mul($t1, $t0); $t1 = self::fe_sq($t0); # for (i = 1; i < 5; ++i) { # fe_sq(t1, t1); # } for ($i = 1; $i < 5; ++$i) { $t1 = self::fe_sq($t1); } # fe_mul(t0, t1, t0); # fe_sq(t1, t0); $t0 = self::fe_mul($t1, $t0); $t1 = self::fe_sq($t0); # for (i = 1; i < 10; ++i) { # fe_sq(t1, t1); # } for ($i = 1; $i < 10; ++$i) { $t1 = self::fe_sq($t1); } # fe_mul(t1, t1, t0); # fe_sq(t2, t1); $t1 = self::fe_mul($t1, $t0); $t2 = self::fe_sq($t1); # for (i = 1; i < 20; ++i) { # fe_sq(t2, t2); # } for ($i = 1; $i < 20; ++$i) { $t2 = self::fe_sq($t2); } # fe_mul(t1, t2, t1); # fe_sq(t1, t1); $t1 = self::fe_mul($t2, $t1); $t1 = self::fe_sq($t1); # for (i = 1; i < 10; ++i) { # fe_sq(t1, t1); # } for ($i = 1; $i < 10; ++$i) { $t1 = self::fe_sq($t1); } # fe_mul(t0, t1, t0); # fe_sq(t1, t0); $t0 = self::fe_mul($t1, $t0); $t1 = self::fe_sq($t0); # for (i = 1; i < 50; ++i) { # fe_sq(t1, t1); # } for ($i = 1; $i < 50; ++$i) { $t1 = self::fe_sq($t1); } # fe_mul(t1, t1, t0); # fe_sq(t2, t1); $t1 = self::fe_mul($t1, $t0); $t2 = self::fe_sq($t1); # for (i = 1; i < 100; ++i) { # fe_sq(t2, t2); # } for ($i = 1; $i < 100; ++$i) { $t2 = self::fe_sq($t2); } # fe_mul(t1, t2, t1); # fe_sq(t1, t1); $t1 = self::fe_mul($t2, $t1); $t1 = self::fe_sq($t1); # for (i = 1; i < 50; ++i) { # fe_sq(t1, t1); # } for ($i = 1; $i < 50; ++$i) { $t1 = self::fe_sq($t1); } # fe_mul(t0, t1, t0); # fe_sq(t0, t0); # fe_sq(t0, t0); # fe_mul(out, t0, z); $t0 = self::fe_mul($t1, $t0); $t0 = self::fe_sq($t0); $t0 = self::fe_sq($t0); return self::fe_mul($t0, $z); } /** * Subtract two field elements. * * h = f - g * * Preconditions: * |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. * |g| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. * * Postconditions: * |h| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f * @param ParagonIE_Sodium_Core32_Curve25519_Fe $g * @return ParagonIE_Sodium_Core32_Curve25519_Fe * @throws SodiumException * @throws TypeError * @psalm-suppress MixedMethodCall * @psalm-suppress MixedTypeCoercion */ public static function fe_sub(ParagonIE_Sodium_Core32_Curve25519_Fe $f, ParagonIE_Sodium_Core32_Curve25519_Fe $g) { return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array( $f[0]->subInt32($g[0]), $f[1]->subInt32($g[1]), $f[2]->subInt32($g[2]), $f[3]->subInt32($g[3]), $f[4]->subInt32($g[4]), $f[5]->subInt32($g[5]), $f[6]->subInt32($g[6]), $f[7]->subInt32($g[7]), $f[8]->subInt32($g[8]), $f[9]->subInt32($g[9]) ) ); } /** * Add two group elements. * * r = p + q * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $p * @param ParagonIE_Sodium_Core32_Curve25519_Ge_Cached $q * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 * @throws SodiumException * @throws TypeError */ public static function ge_add( ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $p, ParagonIE_Sodium_Core32_Curve25519_Ge_Cached $q ) { $r = new ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1(); $r->X = self::fe_add($p->Y, $p->X); $r->Y = self::fe_sub($p->Y, $p->X); $r->Z = self::fe_mul($r->X, $q->YplusX); $r->Y = self::fe_mul($r->Y, $q->YminusX); $r->T = self::fe_mul($q->T2d, $p->T); $r->X = self::fe_mul($p->Z, $q->Z); $t0 = self::fe_add($r->X, $r->X); $r->X = self::fe_sub($r->Z, $r->Y); $r->Y = self::fe_add($r->Z, $r->Y); $r->Z = self::fe_add($t0, $r->T); $r->T = self::fe_sub($t0, $r->T); return $r; } /** * @internal You should not use this directly from another application * * @ref https://github.com/jedisct1/libsodium/blob/157c4a80c13b117608aeae12178b2d38825f9f8f/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.c#L1185-L1215 * @param string $a * @return array<int, mixed> * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArrayOffset */ public static function slide($a) { if (self::strlen($a) < 256) { if (self::strlen($a) < 16) { $a = str_pad($a, 256, '0', STR_PAD_RIGHT); } } /** @var array<int, int> $r */ $r = array(); for ($i = 0; $i < 256; ++$i) { $r[$i] = (int) (1 & ( self::chrToInt($a[$i >> 3]) >> ($i & 7) ) ); } for ($i = 0;$i < 256;++$i) { if ($r[$i]) { for ($b = 1;$b <= 6 && $i + $b < 256;++$b) { if ($r[$i + $b]) { if ($r[$i] + ($r[$i + $b] << $b) <= 15) { $r[$i] += $r[$i + $b] << $b; $r[$i + $b] = 0; } elseif ($r[$i] - ($r[$i + $b] << $b) >= -15) { $r[$i] -= $r[$i + $b] << $b; for ($k = $i + $b; $k < 256; ++$k) { if (!$r[$k]) { $r[$k] = 1; break; } $r[$k] = 0; } } else { break; } } } } } return $r; } /** * @internal You should not use this directly from another application * * @param string $s * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P3 * @throws SodiumException * @throws TypeError */ public static function ge_frombytes_negate_vartime($s) { static $d = null; if (!$d) { $d = ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array( ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[0]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[1]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[2]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[3]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[4]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[5]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[6]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[7]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[8]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d[9]) ) ); } /** @var ParagonIE_Sodium_Core32_Curve25519_Fe $d */ # fe_frombytes(h->Y,s); # fe_1(h->Z); $h = new ParagonIE_Sodium_Core32_Curve25519_Ge_P3( self::fe_0(), self::fe_frombytes($s), self::fe_1() ); # fe_sq(u,h->Y); # fe_mul(v,u,d); # fe_sub(u,u,h->Z); /* u = y^2-1 */ # fe_add(v,v,h->Z); /* v = dy^2+1 */ $u = self::fe_sq($h->Y); /** @var ParagonIE_Sodium_Core32_Curve25519_Fe $d */ $v = self::fe_mul($u, $d); $u = self::fe_sub($u, $h->Z); /* u = y^2 - 1 */ $v = self::fe_add($v, $h->Z); /* v = dy^2 + 1 */ # fe_sq(v3,v); # fe_mul(v3,v3,v); /* v3 = v^3 */ # fe_sq(h->X,v3); # fe_mul(h->X,h->X,v); # fe_mul(h->X,h->X,u); /* x = uv^7 */ $v3 = self::fe_sq($v); $v3 = self::fe_mul($v3, $v); /* v3 = v^3 */ $h->X = self::fe_sq($v3); $h->X = self::fe_mul($h->X, $v); $h->X = self::fe_mul($h->X, $u); /* x = uv^7 */ # fe_pow22523(h->X,h->X); /* x = (uv^7)^((q-5)/8) */ # fe_mul(h->X,h->X,v3); # fe_mul(h->X,h->X,u); /* x = uv^3(uv^7)^((q-5)/8) */ $h->X = self::fe_pow22523($h->X); /* x = (uv^7)^((q-5)/8) */ $h->X = self::fe_mul($h->X, $v3); $h->X = self::fe_mul($h->X, $u); /* x = uv^3(uv^7)^((q-5)/8) */ # fe_sq(vxx,h->X); # fe_mul(vxx,vxx,v); # fe_sub(check,vxx,u); /* vx^2-u */ $vxx = self::fe_sq($h->X); $vxx = self::fe_mul($vxx, $v); $check = self::fe_sub($vxx, $u); /* vx^2 - u */ # if (fe_isnonzero(check)) { # fe_add(check,vxx,u); /* vx^2+u */ # if (fe_isnonzero(check)) { # return -1; # } # fe_mul(h->X,h->X,sqrtm1); # } if (self::fe_isnonzero($check)) { $check = self::fe_add($vxx, $u); /* vx^2 + u */ if (self::fe_isnonzero($check)) { throw new RangeException('Internal check failed.'); } $h->X = self::fe_mul( $h->X, ParagonIE_Sodium_Core32_Curve25519_Fe::fromIntArray(self::$sqrtm1) ); } # if (fe_isnegative(h->X) == (s[31] >> 7)) { # fe_neg(h->X,h->X); # } $i = self::chrToInt($s[31]); if (self::fe_isnegative($h->X) === ($i >> 7)) { $h->X = self::fe_neg($h->X); } # fe_mul(h->T,h->X,h->Y); $h->T = self::fe_mul($h->X, $h->Y); return $h; } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 $R * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $p * @param ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp $q * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 * @throws SodiumException * @throws TypeError */ public static function ge_madd( ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 $R, ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $p, ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp $q ) { $r = clone $R; $r->X = self::fe_add($p->Y, $p->X); $r->Y = self::fe_sub($p->Y, $p->X); $r->Z = self::fe_mul($r->X, $q->yplusx); $r->Y = self::fe_mul($r->Y, $q->yminusx); $r->T = self::fe_mul($q->xy2d, $p->T); $t0 = self::fe_add(clone $p->Z, clone $p->Z); $r->X = self::fe_sub($r->Z, $r->Y); $r->Y = self::fe_add($r->Z, $r->Y); $r->Z = self::fe_add($t0, $r->T); $r->T = self::fe_sub($t0, $r->T); return $r; } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 $R * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $p * @param ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp $q * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 * @throws SodiumException * @throws TypeError */ public static function ge_msub( ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 $R, ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $p, ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp $q ) { $r = clone $R; $r->X = self::fe_add($p->Y, $p->X); $r->Y = self::fe_sub($p->Y, $p->X); $r->Z = self::fe_mul($r->X, $q->yminusx); $r->Y = self::fe_mul($r->Y, $q->yplusx); $r->T = self::fe_mul($q->xy2d, $p->T); $t0 = self::fe_add($p->Z, $p->Z); $r->X = self::fe_sub($r->Z, $r->Y); $r->Y = self::fe_add($r->Z, $r->Y); $r->Z = self::fe_sub($t0, $r->T); $r->T = self::fe_add($t0, $r->T); return $r; } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 $p * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P2 * @throws SodiumException * @throws TypeError */ public static function ge_p1p1_to_p2(ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 $p) { $r = new ParagonIE_Sodium_Core32_Curve25519_Ge_P2(); $r->X = self::fe_mul($p->X, $p->T); $r->Y = self::fe_mul($p->Y, $p->Z); $r->Z = self::fe_mul($p->Z, $p->T); return $r; } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 $p * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P3 * @throws SodiumException * @throws TypeError */ public static function ge_p1p1_to_p3(ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 $p) { $r = new ParagonIE_Sodium_Core32_Curve25519_Ge_P3(); $r->X = self::fe_mul($p->X, $p->T); $r->Y = self::fe_mul($p->Y, $p->Z); $r->Z = self::fe_mul($p->Z, $p->T); $r->T = self::fe_mul($p->X, $p->Y); return $r; } /** * @internal You should not use this directly from another application * * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P2 * @throws SodiumException * @throws TypeError */ public static function ge_p2_0() { return new ParagonIE_Sodium_Core32_Curve25519_Ge_P2( self::fe_0(), self::fe_1(), self::fe_1() ); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $p * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 * @throws SodiumException * @throws TypeError */ public static function ge_p2_dbl(ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $p) { $r = new ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1(); $r->X = self::fe_sq($p->X); $r->Z = self::fe_sq($p->Y); $r->T = self::fe_sq2($p->Z); $r->Y = self::fe_add($p->X, $p->Y); $t0 = self::fe_sq($r->Y); $r->Y = self::fe_add($r->Z, $r->X); $r->Z = self::fe_sub($r->Z, $r->X); $r->X = self::fe_sub($t0, $r->Y); $r->T = self::fe_sub($r->T, $r->Z); return $r; } /** * @internal You should not use this directly from another application * * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P3 * @throws SodiumException * @throws TypeError */ public static function ge_p3_0() { return new ParagonIE_Sodium_Core32_Curve25519_Ge_P3( self::fe_0(), self::fe_1(), self::fe_1(), self::fe_0() ); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $p * @return ParagonIE_Sodium_Core32_Curve25519_Ge_Cached * @throws SodiumException * @throws TypeError */ public static function ge_p3_to_cached(ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $p) { static $d2 = null; if ($d2 === null) { $d2 = ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array( ParagonIE_Sodium_Core32_Int32::fromInt(self::$d2[0]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d2[1]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d2[2]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d2[3]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d2[4]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d2[5]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d2[6]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d2[7]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d2[8]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$d2[9]) ) ); } /** @var ParagonIE_Sodium_Core32_Curve25519_Fe $d2 */ $r = new ParagonIE_Sodium_Core32_Curve25519_Ge_Cached(); $r->YplusX = self::fe_add($p->Y, $p->X); $r->YminusX = self::fe_sub($p->Y, $p->X); $r->Z = self::fe_copy($p->Z); $r->T2d = self::fe_mul($p->T, $d2); return $r; } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $p * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P2 */ public static function ge_p3_to_p2(ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $p) { return new ParagonIE_Sodium_Core32_Curve25519_Ge_P2( $p->X, $p->Y, $p->Z ); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $h * @return string * @throws SodiumException * @throws TypeError */ public static function ge_p3_tobytes(ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $h) { $recip = self::fe_invert($h->Z); $x = self::fe_mul($h->X, $recip); $y = self::fe_mul($h->Y, $recip); $s = self::fe_tobytes($y); $s[31] = self::intToChr( self::chrToInt($s[31]) ^ (self::fe_isnegative($x) << 7) ); return $s; } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $p * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 * @throws SodiumException * @throws TypeError */ public static function ge_p3_dbl(ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $p) { $q = self::ge_p3_to_p2($p); return self::ge_p2_dbl($q); } /** * @return ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp * @throws SodiumException * @throws TypeError */ public static function ge_precomp_0() { return new ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp( self::fe_1(), self::fe_1(), self::fe_0() ); } /** * @internal You should not use this directly from another application * * @param int $b * @param int $c * @return int * @psalm-suppress MixedReturnStatement */ public static function equal($b, $c) { $b0 = $b & 0xffff; $b1 = ($b >> 16) & 0xffff; $c0 = $c & 0xffff; $c1 = ($c >> 16) & 0xffff; $d0 = (($b0 ^ $c0) - 1) >> 31; $d1 = (($b1 ^ $c1) - 1) >> 31; return ($d0 & $d1) & 1; } /** * @internal You should not use this directly from another application * * @param string|int $char * @return int (1 = yes, 0 = no) * @throws SodiumException * @throws TypeError */ public static function negative($char) { if (is_int($char)) { return $char < 0 ? 1 : 0; } /** @var string $char */ $x = self::chrToInt(self::substr($char, 0, 1)); return (int) ($x >> 31); } /** * Conditional move * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp $t * @param ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp $u * @param int $b * @return ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp * @throws SodiumException * @throws TypeError */ public static function cmov( ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp $t, ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp $u, $b ) { if (!is_int($b)) { throw new InvalidArgumentException('Expected an integer.'); } return new ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp( self::fe_cmov($t->yplusx, $u->yplusx, $b), self::fe_cmov($t->yminusx, $u->yminusx, $b), self::fe_cmov($t->xy2d, $u->xy2d, $b) ); } /** * @internal You should not use this directly from another application * * @param int $pos * @param int $b * @return ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayOffset * @psalm-suppress MixedArgument */ public static function ge_select($pos = 0, $b = 0) { static $base = null; if ($base === null) { $base = array(); foreach (self::$base as $i => $bas) { for ($j = 0; $j < 8; ++$j) { $base[$i][$j] = new ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp( ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array( ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][0][0]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][0][1]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][0][2]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][0][3]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][0][4]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][0][5]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][0][6]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][0][7]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][0][8]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][0][9]) ) ), ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array( ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][1][0]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][1][1]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][1][2]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][1][3]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][1][4]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][1][5]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][1][6]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][1][7]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][1][8]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][1][9]) ) ), ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array( ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][2][0]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][2][1]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][2][2]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][2][3]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][2][4]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][2][5]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][2][6]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][2][7]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][2][8]), ParagonIE_Sodium_Core32_Int32::fromInt($bas[$j][2][9]) ) ) ); } } } if (!is_int($pos)) { throw new InvalidArgumentException('Position must be an integer'); } if ($pos < 0 || $pos > 31) { throw new RangeException('Position is out of range [0, 31]'); } $bnegative = self::negative($b); $babs = $b - (((-$bnegative) & $b) << 1); $t = self::ge_precomp_0(); for ($i = 0; $i < 8; ++$i) { $t = self::cmov( $t, $base[$pos][$i], -self::equal($babs, $i + 1) ); } $minusT = new ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp( self::fe_copy($t->yminusx), self::fe_copy($t->yplusx), self::fe_neg($t->xy2d) ); return self::cmov($t, $minusT, -$bnegative); } /** * Subtract two group elements. * * r = p - q * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $p * @param ParagonIE_Sodium_Core32_Curve25519_Ge_Cached $q * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 * @throws SodiumException * @throws TypeError */ public static function ge_sub( ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $p, ParagonIE_Sodium_Core32_Curve25519_Ge_Cached $q ) { $r = new ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1(); $r->X = self::fe_add($p->Y, $p->X); $r->Y = self::fe_sub($p->Y, $p->X); $r->Z = self::fe_mul($r->X, $q->YminusX); $r->Y = self::fe_mul($r->Y, $q->YplusX); $r->T = self::fe_mul($q->T2d, $p->T); $r->X = self::fe_mul($p->Z, $q->Z); $t0 = self::fe_add($r->X, $r->X); $r->X = self::fe_sub($r->Z, $r->Y); $r->Y = self::fe_add($r->Z, $r->Y); $r->Z = self::fe_sub($t0, $r->T); $r->T = self::fe_add($t0, $r->T); return $r; } /** * Convert a group element to a byte string. * * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $h * @return string * @throws SodiumException * @throws TypeError */ public static function ge_tobytes(ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $h) { $recip = self::fe_invert($h->Z); $x = self::fe_mul($h->X, $recip); $y = self::fe_mul($h->Y, $recip); $s = self::fe_tobytes($y); $s[31] = self::intToChr( self::chrToInt($s[31]) ^ (self::fe_isnegative($x) << 7) ); return $s; } /** * @internal You should not use this directly from another application * * @param string $a * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A * @param string $b * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P2 * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArrayAccess */ public static function ge_double_scalarmult_vartime( $a, ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A, $b ) { /** @var array<int, ParagonIE_Sodium_Core32_Curve25519_Ge_Cached> $Ai */ $Ai = array(); static $Bi = array(); /** @var array<int, ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp> $Bi */ if (!$Bi) { for ($i = 0; $i < 8; ++$i) { $Bi[$i] = new ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp( ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array( ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][0][0]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][0][1]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][0][2]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][0][3]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][0][4]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][0][5]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][0][6]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][0][7]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][0][8]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][0][9]) ) ), ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array( ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][1][0]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][1][1]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][1][2]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][1][3]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][1][4]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][1][5]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][1][6]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][1][7]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][1][8]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][1][9]) ) ), ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray( array( ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][2][0]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][2][1]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][2][2]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][2][3]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][2][4]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][2][5]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][2][6]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][2][7]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][2][8]), ParagonIE_Sodium_Core32_Int32::fromInt(self::$base2[$i][2][9]) ) ) ); } } for ($i = 0; $i < 8; ++$i) { $Ai[$i] = new ParagonIE_Sodium_Core32_Curve25519_Ge_Cached( self::fe_0(), self::fe_0(), self::fe_0(), self::fe_0() ); } /** @var array<int, ParagonIE_Sodium_Core32_Curve25519_Ge_Cached> $Ai */ # slide(aslide,a); # slide(bslide,b); /** @var array<int, int> $aslide */ $aslide = self::slide($a); /** @var array<int, int> $bslide */ $bslide = self::slide($b); # ge_p3_to_cached(&Ai[0],A); # ge_p3_dbl(&t,A); ge_p1p1_to_p3(&A2,&t); $Ai[0] = self::ge_p3_to_cached($A); $t = self::ge_p3_dbl($A); $A2 = self::ge_p1p1_to_p3($t); # ge_add(&t,&A2,&Ai[0]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[1],&u); # ge_add(&t,&A2,&Ai[1]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[2],&u); # ge_add(&t,&A2,&Ai[2]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[3],&u); # ge_add(&t,&A2,&Ai[3]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[4],&u); # ge_add(&t,&A2,&Ai[4]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[5],&u); # ge_add(&t,&A2,&Ai[5]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[6],&u); # ge_add(&t,&A2,&Ai[6]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[7],&u); for ($i = 0; $i < 7; ++$i) { $t = self::ge_add($A2, $Ai[$i]); $u = self::ge_p1p1_to_p3($t); $Ai[$i + 1] = self::ge_p3_to_cached($u); } # ge_p2_0(r); $r = self::ge_p2_0(); # for (i = 255;i >= 0;--i) { # if (aslide[i] || bslide[i]) break; # } $i = 255; for (; $i >= 0; --$i) { if ($aslide[$i] || $bslide[$i]) { break; } } # for (;i >= 0;--i) { for (; $i >= 0; --$i) { # ge_p2_dbl(&t,r); $t = self::ge_p2_dbl($r); # if (aslide[i] > 0) { if ($aslide[$i] > 0) { # ge_p1p1_to_p3(&u,&t); # ge_add(&t,&u,&Ai[aslide[i]/2]); $u = self::ge_p1p1_to_p3($t); $t = self::ge_add( $u, $Ai[(int) floor($aslide[$i] / 2)] ); # } else if (aslide[i] < 0) { } elseif ($aslide[$i] < 0) { # ge_p1p1_to_p3(&u,&t); # ge_sub(&t,&u,&Ai[(-aslide[i])/2]); $u = self::ge_p1p1_to_p3($t); $t = self::ge_sub( $u, $Ai[(int) floor(-$aslide[$i] / 2)] ); } /** @var array<int, ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp> $Bi */ # if (bslide[i] > 0) { if ($bslide[$i] > 0) { # ge_p1p1_to_p3(&u,&t); # ge_madd(&t,&u,&Bi[bslide[i]/2]); $u = self::ge_p1p1_to_p3($t); /** @var int $index */ $index = (int) floor($bslide[$i] / 2); /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp $thisB */ $thisB = $Bi[$index]; $t = self::ge_madd($t, $u, $thisB); # } else if (bslide[i] < 0) { } elseif ($bslide[$i] < 0) { # ge_p1p1_to_p3(&u,&t); # ge_msub(&t,&u,&Bi[(-bslide[i])/2]); $u = self::ge_p1p1_to_p3($t); /** @var int $index */ $index = (int) floor(-$bslide[$i] / 2); /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp $thisB */ $thisB = $Bi[$index]; $t = self::ge_msub($t, $u, $thisB); } # ge_p1p1_to_p2(r,&t); $r = self::ge_p1p1_to_p2($t); } return $r; } /** * @internal You should not use this directly from another application * * @param string $a * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P3 * @psalm-suppress MixedAssignment * @psalm-suppress MixedOperand * @throws SodiumException * @throws TypeError */ public static function ge_scalarmult_base($a) { /** @var array<int, int> $e */ $e = array(); $r = new ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1(); for ($i = 0; $i < 32; ++$i) { /** @var int $dbl */ $dbl = (int) $i << 1; $e[$dbl] = (int) self::chrToInt($a[$i]) & 15; $e[$dbl + 1] = (int) (self::chrToInt($a[$i]) >> 4) & 15; } /** @var int $carry */ $carry = 0; for ($i = 0; $i < 63; ++$i) { $e[$i] += $carry; $carry = $e[$i] + 8; $carry >>= 4; $e[$i] -= $carry << 4; } /** @var array<int, int> $e */ $e[63] += (int) $carry; $h = self::ge_p3_0(); for ($i = 1; $i < 64; $i += 2) { $t = self::ge_select((int) floor($i / 2), (int) $e[$i]); $r = self::ge_madd($r, $h, $t); $h = self::ge_p1p1_to_p3($r); } $r = self::ge_p3_dbl($h); $s = self::ge_p1p1_to_p2($r); $r = self::ge_p2_dbl($s); $s = self::ge_p1p1_to_p2($r); $r = self::ge_p2_dbl($s); $s = self::ge_p1p1_to_p2($r); $r = self::ge_p2_dbl($s); $h = self::ge_p1p1_to_p3($r); for ($i = 0; $i < 64; $i += 2) { $t = self::ge_select($i >> 1, (int) $e[$i]); $r = self::ge_madd($r, $h, $t); $h = self::ge_p1p1_to_p3($r); } return $h; } /** * Calculates (ab + c) mod l * where l = 2^252 + 27742317777372353535851937790883648493 * * @internal You should not use this directly from another application * * @param string $a * @param string $b * @param string $c * @return string * @throws SodiumException * @throws TypeError */ public static function sc_muladd($a, $b, $c) { $a0 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & self::load_3(self::substr($a, 0, 3))); $a1 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($a, 2, 4)) >> 5)); $a2 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($a, 5, 3)) >> 2)); $a3 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($a, 7, 4)) >> 7)); $a4 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($a, 10, 4)) >> 4)); $a5 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($a, 13, 3)) >> 1)); $a6 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($a, 15, 4)) >> 6)); $a7 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($a, 18, 3)) >> 3)); $a8 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & self::load_3(self::substr($a, 21, 3))); $a9 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($a, 23, 4)) >> 5)); $a10 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($a, 26, 3)) >> 2)); $a11 = ParagonIE_Sodium_Core32_Int64::fromInt(0x1fffffff & (self::load_4(self::substr($a, 28, 4)) >> 7)); $b0 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & self::load_3(self::substr($b, 0, 3))); $b1 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($b, 2, 4)) >> 5)); $b2 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($b, 5, 3)) >> 2)); $b3 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($b, 7, 4)) >> 7)); $b4 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($b, 10, 4)) >> 4)); $b5 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($b, 13, 3)) >> 1)); $b6 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($b, 15, 4)) >> 6)); $b7 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($b, 18, 3)) >> 3)); $b8 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & self::load_3(self::substr($b, 21, 3))); $b9 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($b, 23, 4)) >> 5)); $b10 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($b, 26, 3)) >> 2)); $b11 = ParagonIE_Sodium_Core32_Int64::fromInt(0x1fffffff & (self::load_4(self::substr($b, 28, 4)) >> 7)); $c0 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & self::load_3(self::substr($c, 0, 3))); $c1 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($c, 2, 4)) >> 5)); $c2 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($c, 5, 3)) >> 2)); $c3 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($c, 7, 4)) >> 7)); $c4 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($c, 10, 4)) >> 4)); $c5 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($c, 13, 3)) >> 1)); $c6 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($c, 15, 4)) >> 6)); $c7 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($c, 18, 3)) >> 3)); $c8 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & self::load_3(self::substr($c, 21, 3))); $c9 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($c, 23, 4)) >> 5)); $c10 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($c, 26, 3)) >> 2)); $c11 = ParagonIE_Sodium_Core32_Int64::fromInt(0x1fffffff & (self::load_4(self::substr($c, 28, 4)) >> 7)); /* Can't really avoid the pyramid here: */ /** * @var ParagonIE_Sodium_Core32_Int64 $s0 * @var ParagonIE_Sodium_Core32_Int64 $s1 * @var ParagonIE_Sodium_Core32_Int64 $s2 * @var ParagonIE_Sodium_Core32_Int64 $s3 * @var ParagonIE_Sodium_Core32_Int64 $s4 * @var ParagonIE_Sodium_Core32_Int64 $s5 * @var ParagonIE_Sodium_Core32_Int64 $s6 * @var ParagonIE_Sodium_Core32_Int64 $s7 * @var ParagonIE_Sodium_Core32_Int64 $s8 * @var ParagonIE_Sodium_Core32_Int64 $s9 * @var ParagonIE_Sodium_Core32_Int64 $s10 * @var ParagonIE_Sodium_Core32_Int64 $s11 * @var ParagonIE_Sodium_Core32_Int64 $s12 * @var ParagonIE_Sodium_Core32_Int64 $s13 * @var ParagonIE_Sodium_Core32_Int64 $s14 * @var ParagonIE_Sodium_Core32_Int64 $s15 * @var ParagonIE_Sodium_Core32_Int64 $s16 * @var ParagonIE_Sodium_Core32_Int64 $s17 * @var ParagonIE_Sodium_Core32_Int64 $s18 * @var ParagonIE_Sodium_Core32_Int64 $s19 * @var ParagonIE_Sodium_Core32_Int64 $s20 * @var ParagonIE_Sodium_Core32_Int64 $s21 * @var ParagonIE_Sodium_Core32_Int64 $s22 * @var ParagonIE_Sodium_Core32_Int64 $s23 */ $s0 = $c0->addInt64($a0->mulInt64($b0, 24)); $s1 = $c1->addInt64($a0->mulInt64($b1, 24))->addInt64($a1->mulInt64($b0, 24)); $s2 = $c2->addInt64($a0->mulInt64($b2, 24))->addInt64($a1->mulInt64($b1, 24))->addInt64($a2->mulInt64($b0, 24)); $s3 = $c3->addInt64($a0->mulInt64($b3, 24))->addInt64($a1->mulInt64($b2, 24))->addInt64($a2->mulInt64($b1, 24)) ->addInt64($a3->mulInt64($b0, 24)); $s4 = $c4->addInt64($a0->mulInt64($b4, 24))->addInt64($a1->mulInt64($b3, 24))->addInt64($a2->mulInt64($b2, 24)) ->addInt64($a3->mulInt64($b1, 24))->addInt64($a4->mulInt64($b0, 24)); $s5 = $c5->addInt64($a0->mulInt64($b5, 24))->addInt64($a1->mulInt64($b4, 24))->addInt64($a2->mulInt64($b3, 24)) ->addInt64($a3->mulInt64($b2, 24))->addInt64($a4->mulInt64($b1, 24))->addInt64($a5->mulInt64($b0, 24)); $s6 = $c6->addInt64($a0->mulInt64($b6, 24))->addInt64($a1->mulInt64($b5, 24))->addInt64($a2->mulInt64($b4, 24)) ->addInt64($a3->mulInt64($b3, 24))->addInt64($a4->mulInt64($b2, 24))->addInt64($a5->mulInt64($b1, 24)) ->addInt64($a6->mulInt64($b0, 24)); $s7 = $c7->addInt64($a0->mulInt64($b7, 24))->addInt64($a1->mulInt64($b6, 24))->addInt64($a2->mulInt64($b5, 24)) ->addInt64($a3->mulInt64($b4, 24))->addInt64($a4->mulInt64($b3, 24))->addInt64($a5->mulInt64($b2, 24)) ->addInt64($a6->mulInt64($b1, 24))->addInt64($a7->mulInt64($b0, 24)); $s8 = $c8->addInt64($a0->mulInt64($b8, 24))->addInt64($a1->mulInt64($b7, 24))->addInt64($a2->mulInt64($b6, 24)) ->addInt64($a3->mulInt64($b5, 24))->addInt64($a4->mulInt64($b4, 24))->addInt64($a5->mulInt64($b3, 24)) ->addInt64($a6->mulInt64($b2, 24))->addInt64($a7->mulInt64($b1, 24))->addInt64($a8->mulInt64($b0, 24)); $s9 = $c9->addInt64($a0->mulInt64($b9, 24))->addInt64($a1->mulInt64($b8, 24))->addInt64($a2->mulInt64($b7, 24)) ->addInt64($a3->mulInt64($b6, 24))->addInt64($a4->mulInt64($b5, 24))->addInt64($a5->mulInt64($b4, 24)) ->addInt64($a6->mulInt64($b3, 24))->addInt64($a7->mulInt64($b2, 24))->addInt64($a8->mulInt64($b1, 24)) ->addInt64($a9->mulInt64($b0, 24)); $s10 = $c10->addInt64($a0->mulInt64($b10, 24))->addInt64($a1->mulInt64($b9, 24))->addInt64($a2->mulInt64($b8, 24)) ->addInt64($a3->mulInt64($b7, 24))->addInt64($a4->mulInt64($b6, 24))->addInt64($a5->mulInt64($b5, 24)) ->addInt64($a6->mulInt64($b4, 24))->addInt64($a7->mulInt64($b3, 24))->addInt64($a8->mulInt64($b2, 24)) ->addInt64($a9->mulInt64($b1, 24))->addInt64($a10->mulInt64($b0, 24)); $s11 = $c11->addInt64($a0->mulInt64($b11, 24))->addInt64($a1->mulInt64($b10, 24))->addInt64($a2->mulInt64($b9, 24)) ->addInt64($a3->mulInt64($b8, 24))->addInt64($a4->mulInt64($b7, 24))->addInt64($a5->mulInt64($b6, 24)) ->addInt64($a6->mulInt64($b5, 24))->addInt64($a7->mulInt64($b4, 24))->addInt64($a8->mulInt64($b3, 24)) ->addInt64($a9->mulInt64($b2, 24))->addInt64($a10->mulInt64($b1, 24))->addInt64($a11->mulInt64($b0, 24)); $s12 = $a1->mulInt64($b11, 24)->addInt64($a2->mulInt64($b10, 24))->addInt64($a3->mulInt64($b9, 24)) ->addInt64($a4->mulInt64($b8, 24))->addInt64($a5->mulInt64($b7, 24))->addInt64($a6->mulInt64($b6, 24)) ->addInt64($a7->mulInt64($b5, 24))->addInt64($a8->mulInt64($b4, 24))->addInt64($a9->mulInt64($b3, 24)) ->addInt64($a10->mulInt64($b2, 24))->addInt64($a11->mulInt64($b1, 24)); $s13 = $a2->mulInt64($b11, 24)->addInt64($a3->mulInt64($b10, 24))->addInt64($a4->mulInt64($b9, 24)) ->addInt64($a5->mulInt64($b8, 24))->addInt64($a6->mulInt64($b7, 24))->addInt64($a7->mulInt64($b6, 24)) ->addInt64($a8->mulInt64($b5, 24))->addInt64($a9->mulInt64($b4, 24))->addInt64($a10->mulInt64($b3, 24)) ->addInt64($a11->mulInt64($b2, 24)); $s14 = $a3->mulInt64($b11, 24)->addInt64($a4->mulInt64($b10, 24))->addInt64($a5->mulInt64($b9, 24)) ->addInt64($a6->mulInt64($b8, 24))->addInt64($a7->mulInt64($b7, 24))->addInt64($a8->mulInt64($b6, 24)) ->addInt64($a9->mulInt64($b5, 24))->addInt64($a10->mulInt64($b4, 24))->addInt64($a11->mulInt64($b3, 24)); $s15 = $a4->mulInt64($b11, 24)->addInt64($a5->mulInt64($b10, 24))->addInt64($a6->mulInt64($b9, 24)) ->addInt64($a7->mulInt64($b8, 24))->addInt64($a8->mulInt64($b7, 24))->addInt64($a9->mulInt64($b6, 24)) ->addInt64($a10->mulInt64($b5, 24))->addInt64($a11->mulInt64($b4, 24)); $s16 = $a5->mulInt64($b11, 24)->addInt64($a6->mulInt64($b10, 24))->addInt64($a7->mulInt64($b9, 24)) ->addInt64($a8->mulInt64($b8, 24))->addInt64($a9->mulInt64($b7, 24))->addInt64($a10->mulInt64($b6, 24)) ->addInt64($a11->mulInt64($b5, 24)); $s17 = $a6->mulInt64($b11, 24)->addInt64($a7->mulInt64($b10, 24))->addInt64($a8->mulInt64($b9, 24)) ->addInt64($a9->mulInt64($b8, 24))->addInt64($a10->mulInt64($b7, 24))->addInt64($a11->mulInt64($b6, 24)); $s18 = $a7->mulInt64($b11, 24)->addInt64($a8->mulInt64($b10, 24))->addInt64($a9->mulInt64($b9, 24)) ->addInt64($a10->mulInt64($b8, 24))->addInt64($a11->mulInt64($b7, 24)); $s19 = $a8->mulInt64($b11, 24)->addInt64($a9->mulInt64($b10, 24))->addInt64($a10->mulInt64($b9, 24)) ->addInt64($a11->mulInt64($b8, 24)); $s20 = $a9->mulInt64($b11, 24)->addInt64($a10->mulInt64($b10, 24))->addInt64($a11->mulInt64($b9, 24)); $s21 = $a10->mulInt64($b11, 24)->addInt64($a11->mulInt64($b10, 24)); $s22 = $a11->mulInt64($b11, 24); $s23 = new ParagonIE_Sodium_Core32_Int64(); $carry0 = $s0->addInt(1 << 20)->shiftRight(21); $s1 = $s1->addInt64($carry0); $s0 = $s0->subInt64($carry0->shiftLeft(21)); $carry2 = $s2->addInt(1 << 20)->shiftRight(21); $s3 = $s3->addInt64($carry2); $s2 = $s2->subInt64($carry2->shiftLeft(21)); $carry4 = $s4->addInt(1 << 20)->shiftRight(21); $s5 = $s5->addInt64($carry4); $s4 = $s4->subInt64($carry4->shiftLeft(21)); $carry6 = $s6->addInt(1 << 20)->shiftRight(21); $s7 = $s7->addInt64($carry6); $s6 = $s6->subInt64($carry6->shiftLeft(21)); $carry8 = $s8->addInt(1 << 20)->shiftRight(21); $s9 = $s9->addInt64($carry8); $s8 = $s8->subInt64($carry8->shiftLeft(21)); $carry10 = $s10->addInt(1 << 20)->shiftRight(21); $s11 = $s11->addInt64($carry10); $s10 = $s10->subInt64($carry10->shiftLeft(21)); $carry12 = $s12->addInt(1 << 20)->shiftRight(21); $s13 = $s13->addInt64($carry12); $s12 = $s12->subInt64($carry12->shiftLeft(21)); $carry14 = $s14->addInt(1 << 20)->shiftRight(21); $s15 = $s15->addInt64($carry14); $s14 = $s14->subInt64($carry14->shiftLeft(21)); $carry16 = $s16->addInt(1 << 20)->shiftRight(21); $s17 = $s17->addInt64($carry16); $s16 = $s16->subInt64($carry16->shiftLeft(21)); $carry18 = $s18->addInt(1 << 20)->shiftRight(21); $s19 = $s19->addInt64($carry18); $s18 = $s18->subInt64($carry18->shiftLeft(21)); $carry20 = $s20->addInt(1 << 20)->shiftRight(21); $s21 = $s21->addInt64($carry20); $s20 = $s20->subInt64($carry20->shiftLeft(21)); $carry22 = $s22->addInt(1 << 20)->shiftRight(21); $s23 = $s23->addInt64($carry22); $s22 = $s22->subInt64($carry22->shiftLeft(21)); $carry1 = $s1->addInt(1 << 20)->shiftRight(21); $s2 = $s2->addInt64($carry1); $s1 = $s1->subInt64($carry1->shiftLeft(21)); $carry3 = $s3->addInt(1 << 20)->shiftRight(21); $s4 = $s4->addInt64($carry3); $s3 = $s3->subInt64($carry3->shiftLeft(21)); $carry5 = $s5->addInt(1 << 20)->shiftRight(21); $s6 = $s6->addInt64($carry5); $s5 = $s5->subInt64($carry5->shiftLeft(21)); $carry7 = $s7->addInt(1 << 20)->shiftRight(21); $s8 = $s8->addInt64($carry7); $s7 = $s7->subInt64($carry7->shiftLeft(21)); $carry9 = $s9->addInt(1 << 20)->shiftRight(21); $s10 = $s10->addInt64($carry9); $s9 = $s9->subInt64($carry9->shiftLeft(21)); $carry11 = $s11->addInt(1 << 20)->shiftRight(21); $s12 = $s12->addInt64($carry11); $s11 = $s11->subInt64($carry11->shiftLeft(21)); $carry13 = $s13->addInt(1 << 20)->shiftRight(21); $s14 = $s14->addInt64($carry13); $s13 = $s13->subInt64($carry13->shiftLeft(21)); $carry15 = $s15->addInt(1 << 20)->shiftRight(21); $s16 = $s16->addInt64($carry15); $s15 = $s15->subInt64($carry15->shiftLeft(21)); $carry17 = $s17->addInt(1 << 20)->shiftRight(21); $s18 = $s18->addInt64($carry17); $s17 = $s17->subInt64($carry17->shiftLeft(21)); $carry19 = $s19->addInt(1 << 20)->shiftRight(21); $s20 = $s20->addInt64($carry19); $s19 = $s19->subInt64($carry19->shiftLeft(21)); $carry21 = $s21->addInt(1 << 20)->shiftRight(21); $s22 = $s22->addInt64($carry21); $s21 = $s21->subInt64($carry21->shiftLeft(21)); $s11 = $s11->addInt64($s23->mulInt(666643, 20)); $s12 = $s12->addInt64($s23->mulInt(470296, 19)); $s13 = $s13->addInt64($s23->mulInt(654183, 20)); $s14 = $s14->subInt64($s23->mulInt(997805, 20)); $s15 = $s15->addInt64($s23->mulInt(136657, 18)); $s16 = $s16->subInt64($s23->mulInt(683901, 20)); $s10 = $s10->addInt64($s22->mulInt(666643, 20)); $s11 = $s11->addInt64($s22->mulInt(470296, 19)); $s12 = $s12->addInt64($s22->mulInt(654183, 20)); $s13 = $s13->subInt64($s22->mulInt(997805, 20)); $s14 = $s14->addInt64($s22->mulInt(136657, 18)); $s15 = $s15->subInt64($s22->mulInt(683901, 20)); $s9 = $s9->addInt64($s21->mulInt(666643, 20)); $s10 = $s10->addInt64($s21->mulInt(470296, 19)); $s11 = $s11->addInt64($s21->mulInt(654183, 20)); $s12 = $s12->subInt64($s21->mulInt(997805, 20)); $s13 = $s13->addInt64($s21->mulInt(136657, 18)); $s14 = $s14->subInt64($s21->mulInt(683901, 20)); $s8 = $s8->addInt64($s20->mulInt(666643, 20)); $s9 = $s9->addInt64($s20->mulInt(470296, 19)); $s10 = $s10->addInt64($s20->mulInt(654183, 20)); $s11 = $s11->subInt64($s20->mulInt(997805, 20)); $s12 = $s12->addInt64($s20->mulInt(136657, 18)); $s13 = $s13->subInt64($s20->mulInt(683901, 20)); $s7 = $s7->addInt64($s19->mulInt(666643, 20)); $s8 = $s8->addInt64($s19->mulInt(470296, 19)); $s9 = $s9->addInt64($s19->mulInt(654183, 20)); $s10 = $s10->subInt64($s19->mulInt(997805, 20)); $s11 = $s11->addInt64($s19->mulInt(136657, 18)); $s12 = $s12->subInt64($s19->mulInt(683901, 20)); $s6 = $s6->addInt64($s18->mulInt(666643, 20)); $s7 = $s7->addInt64($s18->mulInt(470296, 19)); $s8 = $s8->addInt64($s18->mulInt(654183, 20)); $s9 = $s9->subInt64($s18->mulInt(997805, 20)); $s10 = $s10->addInt64($s18->mulInt(136657, 18)); $s11 = $s11->subInt64($s18->mulInt(683901, 20)); $carry6 = $s6->addInt(1 << 20)->shiftRight(21); $s7 = $s7->addInt64($carry6); $s6 = $s6->subInt64($carry6->shiftLeft(21)); $carry8 = $s8->addInt(1 << 20)->shiftRight(21); $s9 = $s9->addInt64($carry8); $s8 = $s8->subInt64($carry8->shiftLeft(21)); $carry10 = $s10->addInt(1 << 20)->shiftRight(21); $s11 = $s11->addInt64($carry10); $s10 = $s10->subInt64($carry10->shiftLeft(21)); $carry12 = $s12->addInt(1 << 20)->shiftRight(21); $s13 = $s13->addInt64($carry12); $s12 = $s12->subInt64($carry12->shiftLeft(21)); $carry14 = $s14->addInt(1 << 20)->shiftRight(21); $s15 = $s15->addInt64($carry14); $s14 = $s14->subInt64($carry14->shiftLeft(21)); $carry16 = $s16->addInt(1 << 20)->shiftRight(21); $s17 = $s17->addInt64($carry16); $s16 = $s16->subInt64($carry16->shiftLeft(21)); $carry7 = $s7->addInt(1 << 20)->shiftRight(21); $s8 = $s8->addInt64($carry7); $s7 = $s7->subInt64($carry7->shiftLeft(21)); $carry9 = $s9->addInt(1 << 20)->shiftRight(21); $s10 = $s10->addInt64($carry9); $s9 = $s9->subInt64($carry9->shiftLeft(21)); $carry11 = $s11->addInt(1 << 20)->shiftRight(21); $s12 = $s12->addInt64($carry11); $s11 = $s11->subInt64($carry11->shiftLeft(21)); $carry13 = $s13->addInt(1 << 20)->shiftRight(21); $s14 = $s14->addInt64($carry13); $s13 = $s13->subInt64($carry13->shiftLeft(21)); $carry15 = $s15->addInt(1 << 20)->shiftRight(21); $s16 = $s16->addInt64($carry15); $s15 = $s15->subInt64($carry15->shiftLeft(21)); $s5 = $s5->addInt64($s17->mulInt(666643, 20)); $s6 = $s6->addInt64($s17->mulInt(470296, 19)); $s7 = $s7->addInt64($s17->mulInt(654183, 20)); $s8 = $s8->subInt64($s17->mulInt(997805, 20)); $s9 = $s9->addInt64($s17->mulInt(136657, 18)); $s10 = $s10->subInt64($s17->mulInt(683901, 20)); $s4 = $s4->addInt64($s16->mulInt(666643, 20)); $s5 = $s5->addInt64($s16->mulInt(470296, 19)); $s6 = $s6->addInt64($s16->mulInt(654183, 20)); $s7 = $s7->subInt64($s16->mulInt(997805, 20)); $s8 = $s8->addInt64($s16->mulInt(136657, 18)); $s9 = $s9->subInt64($s16->mulInt(683901, 20)); $s3 = $s3->addInt64($s15->mulInt(666643, 20)); $s4 = $s4->addInt64($s15->mulInt(470296, 19)); $s5 = $s5->addInt64($s15->mulInt(654183, 20)); $s6 = $s6->subInt64($s15->mulInt(997805, 20)); $s7 = $s7->addInt64($s15->mulInt(136657, 18)); $s8 = $s8->subInt64($s15->mulInt(683901, 20)); $s2 = $s2->addInt64($s14->mulInt(666643, 20)); $s3 = $s3->addInt64($s14->mulInt(470296, 19)); $s4 = $s4->addInt64($s14->mulInt(654183, 20)); $s5 = $s5->subInt64($s14->mulInt(997805, 20)); $s6 = $s6->addInt64($s14->mulInt(136657, 18)); $s7 = $s7->subInt64($s14->mulInt(683901, 20)); $s1 = $s1->addInt64($s13->mulInt(666643, 20)); $s2 = $s2->addInt64($s13->mulInt(470296, 19)); $s3 = $s3->addInt64($s13->mulInt(654183, 20)); $s4 = $s4->subInt64($s13->mulInt(997805, 20)); $s5 = $s5->addInt64($s13->mulInt(136657, 18)); $s6 = $s6->subInt64($s13->mulInt(683901, 20)); $s0 = $s0->addInt64($s12->mulInt(666643, 20)); $s1 = $s1->addInt64($s12->mulInt(470296, 19)); $s2 = $s2->addInt64($s12->mulInt(654183, 20)); $s3 = $s3->subInt64($s12->mulInt(997805, 20)); $s4 = $s4->addInt64($s12->mulInt(136657, 18)); $s5 = $s5->subInt64($s12->mulInt(683901, 20)); $s12 = new ParagonIE_Sodium_Core32_Int64(); $carry0 = $s0->addInt(1 << 20)->shiftRight(21); $s1 = $s1->addInt64($carry0); $s0 = $s0->subInt64($carry0->shiftLeft(21)); $carry2 = $s2->addInt(1 << 20)->shiftRight(21); $s3 = $s3->addInt64($carry2); $s2 = $s2->subInt64($carry2->shiftLeft(21)); $carry4 = $s4->addInt(1 << 20)->shiftRight(21); $s5 = $s5->addInt64($carry4); $s4 = $s4->subInt64($carry4->shiftLeft(21)); $carry6 = $s6->addInt(1 << 20)->shiftRight(21); $s7 = $s7->addInt64($carry6); $s6 = $s6->subInt64($carry6->shiftLeft(21)); $carry8 = $s8->addInt(1 << 20)->shiftRight(21); $s9 = $s9->addInt64($carry8); $s8 = $s8->subInt64($carry8->shiftLeft(21)); $carry10 = $s10->addInt(1 << 20)->shiftRight(21); $s11 = $s11->addInt64($carry10); $s10 = $s10->subInt64($carry10->shiftLeft(21)); $carry1 = $s1->addInt(1 << 20)->shiftRight(21); $s2 = $s2->addInt64($carry1); $s1 = $s1->subInt64($carry1->shiftLeft(21)); $carry3 = $s3->addInt(1 << 20)->shiftRight(21); $s4 = $s4->addInt64($carry3); $s3 = $s3->subInt64($carry3->shiftLeft(21)); $carry5 = $s5->addInt(1 << 20)->shiftRight(21); $s6 = $s6->addInt64($carry5); $s5 = $s5->subInt64($carry5->shiftLeft(21)); $carry7 = $s7->addInt(1 << 20)->shiftRight(21); $s8 = $s8->addInt64($carry7); $s7 = $s7->subInt64($carry7->shiftLeft(21)); $carry9 = $s9->addInt(1 << 20)->shiftRight(21); $s10 = $s10->addInt64($carry9); $s9 = $s9->subInt64($carry9->shiftLeft(21)); $carry11 = $s11->addInt(1 << 20)->shiftRight(21); $s12 = $s12->addInt64($carry11); $s11 = $s11->subInt64($carry11->shiftLeft(21)); $s0 = $s0->addInt64($s12->mulInt(666643, 20)); $s1 = $s1->addInt64($s12->mulInt(470296, 19)); $s2 = $s2->addInt64($s12->mulInt(654183, 20)); $s3 = $s3->subInt64($s12->mulInt(997805, 20)); $s4 = $s4->addInt64($s12->mulInt(136657, 18)); $s5 = $s5->subInt64($s12->mulInt(683901, 20)); $s12 = new ParagonIE_Sodium_Core32_Int64(); $carry0 = $s0->shiftRight(21); $s1 = $s1->addInt64($carry0); $s0 = $s0->subInt64($carry0->shiftLeft(21)); $carry1 = $s1->shiftRight(21); $s2 = $s2->addInt64($carry1); $s1 = $s1->subInt64($carry1->shiftLeft(21)); $carry2 = $s2->shiftRight(21); $s3 = $s3->addInt64($carry2); $s2 = $s2->subInt64($carry2->shiftLeft(21)); $carry3 = $s3->shiftRight(21); $s4 = $s4->addInt64($carry3); $s3 = $s3->subInt64($carry3->shiftLeft(21)); $carry4 = $s4->shiftRight(21); $s5 = $s5->addInt64($carry4); $s4 = $s4->subInt64($carry4->shiftLeft(21)); $carry5 = $s5->shiftRight(21); $s6 = $s6->addInt64($carry5); $s5 = $s5->subInt64($carry5->shiftLeft(21)); $carry6 = $s6->shiftRight(21); $s7 = $s7->addInt64($carry6); $s6 = $s6->subInt64($carry6->shiftLeft(21)); $carry7 = $s7->shiftRight(21); $s8 = $s8->addInt64($carry7); $s7 = $s7->subInt64($carry7->shiftLeft(21)); $carry8 = $s8->shiftRight(21); $s9 = $s9->addInt64($carry8); $s8 = $s8->subInt64($carry8->shiftLeft(21)); $carry9 = $s9->shiftRight(21); $s10 = $s10->addInt64($carry9); $s9 = $s9->subInt64($carry9->shiftLeft(21)); $carry10 = $s10->shiftRight(21); $s11 = $s11->addInt64($carry10); $s10 = $s10->subInt64($carry10->shiftLeft(21)); $carry11 = $s11->shiftRight(21); $s12 = $s12->addInt64($carry11); $s11 = $s11->subInt64($carry11->shiftLeft(21)); $s0 = $s0->addInt64($s12->mulInt(666643, 20)); $s1 = $s1->addInt64($s12->mulInt(470296, 19)); $s2 = $s2->addInt64($s12->mulInt(654183, 20)); $s3 = $s3->subInt64($s12->mulInt(997805, 20)); $s4 = $s4->addInt64($s12->mulInt(136657, 18)); $s5 = $s5->subInt64($s12->mulInt(683901, 20)); $carry0 = $s0->shiftRight(21); $s1 = $s1->addInt64($carry0); $s0 = $s0->subInt64($carry0->shiftLeft(21)); $carry1 = $s1->shiftRight(21); $s2 = $s2->addInt64($carry1); $s1 = $s1->subInt64($carry1->shiftLeft(21)); $carry2 = $s2->shiftRight(21); $s3 = $s3->addInt64($carry2); $s2 = $s2->subInt64($carry2->shiftLeft(21)); $carry3 = $s3->shiftRight(21); $s4 = $s4->addInt64($carry3); $s3 = $s3->subInt64($carry3->shiftLeft(21)); $carry4 = $s4->shiftRight(21); $s5 = $s5->addInt64($carry4); $s4 = $s4->subInt64($carry4->shiftLeft(21)); $carry5 = $s5->shiftRight(21); $s6 = $s6->addInt64($carry5); $s5 = $s5->subInt64($carry5->shiftLeft(21)); $carry6 = $s6->shiftRight(21); $s7 = $s7->addInt64($carry6); $s6 = $s6->subInt64($carry6->shiftLeft(21)); $carry7 = $s7->shiftRight(21); $s8 = $s8->addInt64($carry7); $s7 = $s7->subInt64($carry7->shiftLeft(21)); $carry8 = $s10->shiftRight(21); $s9 = $s9->addInt64($carry8); $s8 = $s8->subInt64($carry8->shiftLeft(21)); $carry9 = $s9->shiftRight(21); $s10 = $s10->addInt64($carry9); $s9 = $s9->subInt64($carry9->shiftLeft(21)); $carry10 = $s10->shiftRight(21); $s11 = $s11->addInt64($carry10); $s10 = $s10->subInt64($carry10->shiftLeft(21)); $S0 = $s0->toInt(); $S1 = $s1->toInt(); $S2 = $s2->toInt(); $S3 = $s3->toInt(); $S4 = $s4->toInt(); $S5 = $s5->toInt(); $S6 = $s6->toInt(); $S7 = $s7->toInt(); $S8 = $s8->toInt(); $S9 = $s9->toInt(); $S10 = $s10->toInt(); $S11 = $s11->toInt(); /** * @var array<int, int> */ $arr = array( (int) (0xff & ($S0 >> 0)), (int) (0xff & ($S0 >> 8)), (int) (0xff & (($S0 >> 16) | ($S1 << 5))), (int) (0xff & ($S1 >> 3)), (int) (0xff & ($S1 >> 11)), (int) (0xff & (($S1 >> 19) | ($S2 << 2))), (int) (0xff & ($S2 >> 6)), (int) (0xff & (($S2 >> 14) | ($S3 << 7))), (int) (0xff & ($S3 >> 1)), (int) (0xff & ($S3 >> 9)), (int) (0xff & (($S3 >> 17) | ($S4 << 4))), (int) (0xff & ($S4 >> 4)), (int) (0xff & ($S4 >> 12)), (int) (0xff & (($S4 >> 20) | ($S5 << 1))), (int) (0xff & ($S5 >> 7)), (int) (0xff & (($S5 >> 15) | ($S6 << 6))), (int) (0xff & ($S6 >> 2)), (int) (0xff & ($S6 >> 10)), (int) (0xff & (($S6 >> 18) | ($S7 << 3))), (int) (0xff & ($S7 >> 5)), (int) (0xff & ($S7 >> 13)), (int) (0xff & ($S8 >> 0)), (int) (0xff & ($S8 >> 8)), (int) (0xff & (($S8 >> 16) | ($S9 << 5))), (int) (0xff & ($S9 >> 3)), (int) (0xff & ($S9 >> 11)), (int) (0xff & (($S9 >> 19) | ($S10 << 2))), (int) (0xff & ($S10 >> 6)), (int) (0xff & (($S10 >> 14) | ($S11 << 7))), (int) (0xff & ($S11 >> 1)), (int) (0xff & ($S11 >> 9)), (int) (0xff & ($S11 >> 17)) ); return self::intArrayToString($arr); } /** * @internal You should not use this directly from another application * * @param string $s * @return string * @throws SodiumException * @throws TypeError */ public static function sc_reduce($s) { /** * @var ParagonIE_Sodium_Core32_Int64 $s0 * @var ParagonIE_Sodium_Core32_Int64 $s1 * @var ParagonIE_Sodium_Core32_Int64 $s2 * @var ParagonIE_Sodium_Core32_Int64 $s3 * @var ParagonIE_Sodium_Core32_Int64 $s4 * @var ParagonIE_Sodium_Core32_Int64 $s5 * @var ParagonIE_Sodium_Core32_Int64 $s6 * @var ParagonIE_Sodium_Core32_Int64 $s7 * @var ParagonIE_Sodium_Core32_Int64 $s8 * @var ParagonIE_Sodium_Core32_Int64 $s9 * @var ParagonIE_Sodium_Core32_Int64 $s10 * @var ParagonIE_Sodium_Core32_Int64 $s11 * @var ParagonIE_Sodium_Core32_Int64 $s12 * @var ParagonIE_Sodium_Core32_Int64 $s13 * @var ParagonIE_Sodium_Core32_Int64 $s14 * @var ParagonIE_Sodium_Core32_Int64 $s15 * @var ParagonIE_Sodium_Core32_Int64 $s16 * @var ParagonIE_Sodium_Core32_Int64 $s17 * @var ParagonIE_Sodium_Core32_Int64 $s18 * @var ParagonIE_Sodium_Core32_Int64 $s19 * @var ParagonIE_Sodium_Core32_Int64 $s20 * @var ParagonIE_Sodium_Core32_Int64 $s21 * @var ParagonIE_Sodium_Core32_Int64 $s22 * @var ParagonIE_Sodium_Core32_Int64 $s23 */ $s0 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & self::load_3(self::substr($s, 0, 3))); $s1 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($s, 2, 4)) >> 5)); $s2 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($s, 5, 3)) >> 2)); $s3 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($s, 7, 4)) >> 7)); $s4 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($s, 10, 4)) >> 4)); $s5 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($s, 13, 3)) >> 1)); $s6 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($s, 15, 4)) >> 6)); $s7 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($s, 18, 4)) >> 3)); $s8 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & self::load_3(self::substr($s, 21, 3))); $s9 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($s, 23, 4)) >> 5)); $s10 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($s, 26, 3)) >> 2)); $s11 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($s, 28, 4)) >> 7)); $s12 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($s, 31, 4)) >> 4)); $s13 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($s, 34, 3)) >> 1)); $s14 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($s, 36, 4)) >> 6)); $s15 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($s, 39, 4)) >> 3)); $s16 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & self::load_3(self::substr($s, 42, 3))); $s17 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($s, 44, 4)) >> 5)); $s18 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($s, 47, 3)) >> 2)); $s19 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($s, 49, 4)) >> 7)); $s20 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($s, 52, 4)) >> 4)); $s21 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_3(self::substr($s, 55, 3)) >> 1)); $s22 = ParagonIE_Sodium_Core32_Int64::fromInt(2097151 & (self::load_4(self::substr($s, 57, 4)) >> 6)); $s23 = ParagonIE_Sodium_Core32_Int64::fromInt(0x1fffffff & (self::load_4(self::substr($s, 60, 4)) >> 3)); $s11 = $s11->addInt64($s23->mulInt(666643, 20)); $s12 = $s12->addInt64($s23->mulInt(470296, 19)); $s13 = $s13->addInt64($s23->mulInt(654183, 20)); $s14 = $s14->subInt64($s23->mulInt(997805, 20)); $s15 = $s15->addInt64($s23->mulInt(136657, 18)); $s16 = $s16->subInt64($s23->mulInt(683901, 20)); $s10 = $s10->addInt64($s22->mulInt(666643, 20)); $s11 = $s11->addInt64($s22->mulInt(470296, 19)); $s12 = $s12->addInt64($s22->mulInt(654183, 20)); $s13 = $s13->subInt64($s22->mulInt(997805, 20)); $s14 = $s14->addInt64($s22->mulInt(136657, 18)); $s15 = $s15->subInt64($s22->mulInt(683901, 20)); $s9 = $s9->addInt64($s21->mulInt(666643, 20)); $s10 = $s10->addInt64($s21->mulInt(470296, 19)); $s11 = $s11->addInt64($s21->mulInt(654183, 20)); $s12 = $s12->subInt64($s21->mulInt(997805, 20)); $s13 = $s13->addInt64($s21->mulInt(136657, 18)); $s14 = $s14->subInt64($s21->mulInt(683901, 20)); $s8 = $s8->addInt64($s20->mulInt(666643, 20)); $s9 = $s9->addInt64($s20->mulInt(470296, 19)); $s10 = $s10->addInt64($s20->mulInt(654183, 20)); $s11 = $s11->subInt64($s20->mulInt(997805, 20)); $s12 = $s12->addInt64($s20->mulInt(136657, 18)); $s13 = $s13->subInt64($s20->mulInt(683901, 20)); $s7 = $s7->addInt64($s19->mulInt(666643, 20)); $s8 = $s8->addInt64($s19->mulInt(470296, 19)); $s9 = $s9->addInt64($s19->mulInt(654183, 20)); $s10 = $s10->subInt64($s19->mulInt(997805, 20)); $s11 = $s11->addInt64($s19->mulInt(136657, 18)); $s12 = $s12->subInt64($s19->mulInt(683901, 20)); $s6 = $s6->addInt64($s18->mulInt(666643, 20)); $s7 = $s7->addInt64($s18->mulInt(470296, 19)); $s8 = $s8->addInt64($s18->mulInt(654183, 20)); $s9 = $s9->subInt64($s18->mulInt(997805, 20)); $s10 = $s10->addInt64($s18->mulInt(136657, 18)); $s11 = $s11->subInt64($s18->mulInt(683901, 20)); $carry6 = $s6->addInt(1 << 20)->shiftRight(21); $s7 = $s7->addInt64($carry6); $s6 = $s6->subInt64($carry6->shiftLeft(21)); $carry8 = $s8->addInt(1 << 20)->shiftRight(21); $s9 = $s9->addInt64($carry8); $s8 = $s8->subInt64($carry8->shiftLeft(21)); $carry10 = $s10->addInt(1 << 20)->shiftRight(21); $s11 = $s11->addInt64($carry10); $s10 = $s10->subInt64($carry10->shiftLeft(21)); $carry12 = $s12->addInt(1 << 20)->shiftRight(21); $s13 = $s13->addInt64($carry12); $s12 = $s12->subInt64($carry12->shiftLeft(21)); $carry14 = $s14->addInt(1 << 20)->shiftRight(21); $s15 = $s15->addInt64($carry14); $s14 = $s14->subInt64($carry14->shiftLeft(21)); $carry16 = $s16->addInt(1 << 20)->shiftRight(21); $s17 = $s17->addInt64($carry16); $s16 = $s16->subInt64($carry16->shiftLeft(21)); $carry7 = $s7->addInt(1 << 20)->shiftRight(21); $s8 = $s8->addInt64($carry7); $s7 = $s7->subInt64($carry7->shiftLeft(21)); $carry9 = $s9->addInt(1 << 20)->shiftRight(21); $s10 = $s10->addInt64($carry9); $s9 = $s9->subInt64($carry9->shiftLeft(21)); $carry11 = $s11->addInt(1 << 20)->shiftRight(21); $s12 = $s12->addInt64($carry11); $s11 = $s11->subInt64($carry11->shiftLeft(21)); $carry13 = $s13->addInt(1 << 20)->shiftRight(21); $s14 = $s14->addInt64($carry13); $s13 = $s13->subInt64($carry13->shiftLeft(21)); $carry15 = $s15->addInt(1 << 20)->shiftRight(21); $s16 = $s16->addInt64($carry15); $s15 = $s15->subInt64($carry15->shiftLeft(21)); $s5 = $s5->addInt64($s17->mulInt(666643, 20)); $s6 = $s6->addInt64($s17->mulInt(470296, 19)); $s7 = $s7->addInt64($s17->mulInt(654183, 20)); $s8 = $s8->subInt64($s17->mulInt(997805, 20)); $s9 = $s9->addInt64($s17->mulInt(136657, 18)); $s10 = $s10->subInt64($s17->mulInt(683901, 20)); $s4 = $s4->addInt64($s16->mulInt(666643, 20)); $s5 = $s5->addInt64($s16->mulInt(470296, 19)); $s6 = $s6->addInt64($s16->mulInt(654183, 20)); $s7 = $s7->subInt64($s16->mulInt(997805, 20)); $s8 = $s8->addInt64($s16->mulInt(136657, 18)); $s9 = $s9->subInt64($s16->mulInt(683901, 20)); $s3 = $s3->addInt64($s15->mulInt(666643, 20)); $s4 = $s4->addInt64($s15->mulInt(470296, 19)); $s5 = $s5->addInt64($s15->mulInt(654183, 20)); $s6 = $s6->subInt64($s15->mulInt(997805, 20)); $s7 = $s7->addInt64($s15->mulInt(136657, 18)); $s8 = $s8->subInt64($s15->mulInt(683901, 20)); $s2 = $s2->addInt64($s14->mulInt(666643, 20)); $s3 = $s3->addInt64($s14->mulInt(470296, 19)); $s4 = $s4->addInt64($s14->mulInt(654183, 20)); $s5 = $s5->subInt64($s14->mulInt(997805, 20)); $s6 = $s6->addInt64($s14->mulInt(136657, 18)); $s7 = $s7->subInt64($s14->mulInt(683901, 20)); $s1 = $s1->addInt64($s13->mulInt(666643, 20)); $s2 = $s2->addInt64($s13->mulInt(470296, 19)); $s3 = $s3->addInt64($s13->mulInt(654183, 20)); $s4 = $s4->subInt64($s13->mulInt(997805, 20)); $s5 = $s5->addInt64($s13->mulInt(136657, 18)); $s6 = $s6->subInt64($s13->mulInt(683901, 20)); $s0 = $s0->addInt64($s12->mulInt(666643, 20)); $s1 = $s1->addInt64($s12->mulInt(470296, 19)); $s2 = $s2->addInt64($s12->mulInt(654183, 20)); $s3 = $s3->subInt64($s12->mulInt(997805, 20)); $s4 = $s4->addInt64($s12->mulInt(136657, 18)); $s5 = $s5->subInt64($s12->mulInt(683901, 20)); $s12 = new ParagonIE_Sodium_Core32_Int64(); $carry0 = $s0->addInt(1 << 20)->shiftRight(21); $s1 = $s1->addInt64($carry0); $s0 = $s0->subInt64($carry0->shiftLeft(21)); $carry2 = $s2->addInt(1 << 20)->shiftRight(21); $s3 = $s3->addInt64($carry2); $s2 = $s2->subInt64($carry2->shiftLeft(21)); $carry4 = $s4->addInt(1 << 20)->shiftRight(21); $s5 = $s5->addInt64($carry4); $s4 = $s4->subInt64($carry4->shiftLeft(21)); $carry6 = $s6->addInt(1 << 20)->shiftRight(21); $s7 = $s7->addInt64($carry6); $s6 = $s6->subInt64($carry6->shiftLeft(21)); $carry8 = $s8->addInt(1 << 20)->shiftRight(21); $s9 = $s9->addInt64($carry8); $s8 = $s8->subInt64($carry8->shiftLeft(21)); $carry10 = $s10->addInt(1 << 20)->shiftRight(21); $s11 = $s11->addInt64($carry10); $s10 = $s10->subInt64($carry10->shiftLeft(21)); $carry1 = $s1->addInt(1 << 20)->shiftRight(21); $s2 = $s2->addInt64($carry1); $s1 = $s1->subInt64($carry1->shiftLeft(21)); $carry3 = $s3->addInt(1 << 20)->shiftRight(21); $s4 = $s4->addInt64($carry3); $s3 = $s3->subInt64($carry3->shiftLeft(21)); $carry5 = $s5->addInt(1 << 20)->shiftRight(21); $s6 = $s6->addInt64($carry5); $s5 = $s5->subInt64($carry5->shiftLeft(21)); $carry7 = $s7->addInt(1 << 20)->shiftRight(21); $s8 = $s8->addInt64($carry7); $s7 = $s7->subInt64($carry7->shiftLeft(21)); $carry9 = $s9->addInt(1 << 20)->shiftRight(21); $s10 = $s10->addInt64($carry9); $s9 = $s9->subInt64($carry9->shiftLeft(21)); $carry11 = $s11->addInt(1 << 20)->shiftRight(21); $s12 = $s12->addInt64($carry11); $s11 = $s11->subInt64($carry11->shiftLeft(21)); $s0 = $s0->addInt64($s12->mulInt(666643, 20)); $s1 = $s1->addInt64($s12->mulInt(470296, 19)); $s2 = $s2->addInt64($s12->mulInt(654183, 20)); $s3 = $s3->subInt64($s12->mulInt(997805, 20)); $s4 = $s4->addInt64($s12->mulInt(136657, 18)); $s5 = $s5->subInt64($s12->mulInt(683901, 20)); $s12 = new ParagonIE_Sodium_Core32_Int64(); $carry0 = $s0->shiftRight(21); $s1 = $s1->addInt64($carry0); $s0 = $s0->subInt64($carry0->shiftLeft(21)); $carry1 = $s1->shiftRight(21); $s2 = $s2->addInt64($carry1); $s1 = $s1->subInt64($carry1->shiftLeft(21)); $carry2 = $s2->shiftRight(21); $s3 = $s3->addInt64($carry2); $s2 = $s2->subInt64($carry2->shiftLeft(21)); $carry3 = $s3->shiftRight(21); $s4 = $s4->addInt64($carry3); $s3 = $s3->subInt64($carry3->shiftLeft(21)); $carry4 = $s4->shiftRight(21); $s5 = $s5->addInt64($carry4); $s4 = $s4->subInt64($carry4->shiftLeft(21)); $carry5 = $s5->shiftRight(21); $s6 = $s6->addInt64($carry5); $s5 = $s5->subInt64($carry5->shiftLeft(21)); $carry6 = $s6->shiftRight(21); $s7 = $s7->addInt64($carry6); $s6 = $s6->subInt64($carry6->shiftLeft(21)); $carry7 = $s7->shiftRight(21); $s8 = $s8->addInt64($carry7); $s7 = $s7->subInt64($carry7->shiftLeft(21)); $carry8 = $s8->shiftRight(21); $s9 = $s9->addInt64($carry8); $s8 = $s8->subInt64($carry8->shiftLeft(21)); $carry9 = $s9->shiftRight(21); $s10 = $s10->addInt64($carry9); $s9 = $s9->subInt64($carry9->shiftLeft(21)); $carry10 = $s10->shiftRight(21); $s11 = $s11->addInt64($carry10); $s10 = $s10->subInt64($carry10->shiftLeft(21)); $carry11 = $s11->shiftRight(21); $s12 = $s12->addInt64($carry11); $s11 = $s11->subInt64($carry11->shiftLeft(21)); $s0 = $s0->addInt64($s12->mulInt(666643, 20)); $s1 = $s1->addInt64($s12->mulInt(470296, 19)); $s2 = $s2->addInt64($s12->mulInt(654183, 20)); $s3 = $s3->subInt64($s12->mulInt(997805, 20)); $s4 = $s4->addInt64($s12->mulInt(136657, 18)); $s5 = $s5->subInt64($s12->mulInt(683901, 20)); $carry0 = $s0->shiftRight(21); $s1 = $s1->addInt64($carry0); $s0 = $s0->subInt64($carry0->shiftLeft(21)); $carry1 = $s1->shiftRight(21); $s2 = $s2->addInt64($carry1); $s1 = $s1->subInt64($carry1->shiftLeft(21)); $carry2 = $s2->shiftRight(21); $s3 = $s3->addInt64($carry2); $s2 = $s2->subInt64($carry2->shiftLeft(21)); $carry3 = $s3->shiftRight(21); $s4 = $s4->addInt64($carry3); $s3 = $s3->subInt64($carry3->shiftLeft(21)); $carry4 = $s4->shiftRight(21); $s5 = $s5->addInt64($carry4); $s4 = $s4->subInt64($carry4->shiftLeft(21)); $carry5 = $s5->shiftRight(21); $s6 = $s6->addInt64($carry5); $s5 = $s5->subInt64($carry5->shiftLeft(21)); $carry6 = $s6->shiftRight(21); $s7 = $s7->addInt64($carry6); $s6 = $s6->subInt64($carry6->shiftLeft(21)); $carry7 = $s7->shiftRight(21); $s8 = $s8->addInt64($carry7); $s7 = $s7->subInt64($carry7->shiftLeft(21)); $carry8 = $s8->shiftRight(21); $s9 = $s9->addInt64($carry8); $s8 = $s8->subInt64($carry8->shiftLeft(21)); $carry9 = $s9->shiftRight(21); $s10 = $s10->addInt64($carry9); $s9 = $s9->subInt64($carry9->shiftLeft(21)); $carry10 = $s10->shiftRight(21); $s11 = $s11->addInt64($carry10); $s10 = $s10->subInt64($carry10->shiftLeft(21)); $S0 = $s0->toInt32()->toInt(); $S1 = $s1->toInt32()->toInt(); $S2 = $s2->toInt32()->toInt(); $S3 = $s3->toInt32()->toInt(); $S4 = $s4->toInt32()->toInt(); $S5 = $s5->toInt32()->toInt(); $S6 = $s6->toInt32()->toInt(); $S7 = $s7->toInt32()->toInt(); $S8 = $s8->toInt32()->toInt(); $S9 = $s9->toInt32()->toInt(); $S10 = $s10->toInt32()->toInt(); $S11 = $s11->toInt32()->toInt(); /** * @var array<int, int> */ $arr = array( (int) ($S0 >> 0), (int) ($S0 >> 8), (int) (($S0 >> 16) | ($S1 << 5)), (int) ($S1 >> 3), (int) ($S1 >> 11), (int) (($S1 >> 19) | ($S2 << 2)), (int) ($S2 >> 6), (int) (($S2 >> 14) | ($S3 << 7)), (int) ($S3 >> 1), (int) ($S3 >> 9), (int) (($S3 >> 17) | ($S4 << 4)), (int) ($S4 >> 4), (int) ($S4 >> 12), (int) (($S4 >> 20) | ($S5 << 1)), (int) ($S5 >> 7), (int) (($S5 >> 15) | ($S6 << 6)), (int) ($S6 >> 2), (int) ($S6 >> 10), (int) (($S6 >> 18) | ($S7 << 3)), (int) ($S7 >> 5), (int) ($S7 >> 13), (int) ($S8 >> 0), (int) ($S8 >> 8), (int) (($S8 >> 16) | ($S9 << 5)), (int) ($S9 >> 3), (int) ($S9 >> 11), (int) (($S9 >> 19) | ($S10 << 2)), (int) ($S10 >> 6), (int) (($S10 >> 14) | ($S11 << 7)), (int) ($S11 >> 1), (int) ($S11 >> 9), (int) $S11 >> 17 ); return self::intArrayToString($arr); } /** * multiply by the order of the main subgroup l = 2^252+27742317777372353535851937790883648493 * * @param ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A * @return ParagonIE_Sodium_Core32_Curve25519_Ge_P3 * @throws SodiumException * @throws TypeError */ public static function ge_mul_l(ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A) { $aslide = array( 13, 0, 0, 0, 0, -1, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 0, -5, 0, 0, 0, 0, 0, 0, -3, 0, 0, 0, 0, -13, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, -13, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0, 0, -3, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 3, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ); /** @var array<int, ParagonIE_Sodium_Core32_Curve25519_Ge_Cached> $Ai size 8 */ $Ai = array(); # ge_p3_to_cached(&Ai[0], A); $Ai[0] = self::ge_p3_to_cached($A); # ge_p3_dbl(&t, A); $t = self::ge_p3_dbl($A); # ge_p1p1_to_p3(&A2, &t); $A2 = self::ge_p1p1_to_p3($t); for ($i = 1; $i < 8; ++$i) { # ge_add(&t, &A2, &Ai[0]); $t = self::ge_add($A2, $Ai[$i - 1]); # ge_p1p1_to_p3(&u, &t); $u = self::ge_p1p1_to_p3($t); # ge_p3_to_cached(&Ai[i], &u); $Ai[$i] = self::ge_p3_to_cached($u); } $r = self::ge_p3_0(); for ($i = 252; $i >= 0; --$i) { $t = self::ge_p3_dbl($r); if ($aslide[$i] > 0) { # ge_p1p1_to_p3(&u, &t); $u = self::ge_p1p1_to_p3($t); # ge_add(&t, &u, &Ai[aslide[i] / 2]); $t = self::ge_add($u, $Ai[(int)($aslide[$i] / 2)]); } elseif ($aslide[$i] < 0) { # ge_p1p1_to_p3(&u, &t); $u = self::ge_p1p1_to_p3($t); # ge_sub(&t, &u, &Ai[(-aslide[i]) / 2]); $t = self::ge_sub($u, $Ai[(int)(-$aslide[$i] / 2)]); } } # ge_p1p1_to_p3(r, &t); return self::ge_p1p1_to_p3($t); } } Core32/Ed25519.php 0000644 00000036567 15162213656 0007300 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_Ed25519', false)) { return; } if (!class_exists('ParagonIE_Sodium_Core32_Curve25519')) { require_once dirname(__FILE__) . '/Curve25519.php'; } /** * Class ParagonIE_Sodium_Core32_Ed25519 */ abstract class ParagonIE_Sodium_Core32_Ed25519 extends ParagonIE_Sodium_Core32_Curve25519 { const KEYPAIR_BYTES = 96; const SEED_BYTES = 32; /** * @internal You should not use this directly from another application * * @return string (96 bytes) * @throws Exception * @throws SodiumException * @throws TypeError */ public static function keypair() { $seed = random_bytes(self::SEED_BYTES); $pk = ''; $sk = ''; self::seed_keypair($pk, $sk, $seed); return $sk . $pk; } /** * @internal You should not use this directly from another application * * @param string $pk * @param string $sk * @param string $seed * @return string * @throws SodiumException * @throws TypeError */ public static function seed_keypair(&$pk, &$sk, $seed) { if (self::strlen($seed) !== self::SEED_BYTES) { throw new RangeException('crypto_sign keypair seed must be 32 bytes long'); } /** @var string $pk */ $pk = self::publickey_from_secretkey($seed); $sk = $seed . $pk; return $sk; } /** * @internal You should not use this directly from another application * * @param string $keypair * @return string * @throws TypeError */ public static function secretkey($keypair) { if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { throw new RangeException('crypto_sign keypair must be 96 bytes long'); } return self::substr($keypair, 0, 64); } /** * @internal You should not use this directly from another application * * @param string $keypair * @return string * @throws RangeException * @throws TypeError */ public static function publickey($keypair) { if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { throw new RangeException('crypto_sign keypair must be 96 bytes long'); } return self::substr($keypair, 64, 32); } /** * @internal You should not use this directly from another application * * @param string $sk * @return string * @throws SodiumException * @throws TypeError */ public static function publickey_from_secretkey($sk) { /** @var string $sk */ $sk = hash('sha512', self::substr($sk, 0, 32), true); $sk[0] = self::intToChr( self::chrToInt($sk[0]) & 248 ); $sk[31] = self::intToChr( (self::chrToInt($sk[31]) & 63) | 64 ); return self::sk_to_pk($sk); } /** * @param string $pk * @return string * @throws SodiumException * @throws TypeError */ public static function pk_to_curve25519($pk) { if (self::small_order($pk)) { throw new SodiumException('Public key is on a small order'); } $A = self::ge_frombytes_negate_vartime($pk); $p1 = self::ge_mul_l($A); if (!self::fe_isnonzero($p1->X)) { throw new SodiumException('Unexpected zero result'); } # fe_1(one_minus_y); # fe_sub(one_minus_y, one_minus_y, A.Y); # fe_invert(one_minus_y, one_minus_y); $one_minux_y = self::fe_invert( self::fe_sub( self::fe_1(), $A->Y ) ); # fe_1(x); # fe_add(x, x, A.Y); # fe_mul(x, x, one_minus_y); $x = self::fe_mul( self::fe_add(self::fe_1(), $A->Y), $one_minux_y ); # fe_tobytes(curve25519_pk, x); return self::fe_tobytes($x); } /** * @internal You should not use this directly from another application * * @param string $sk * @return string * @throws SodiumException * @throws TypeError */ public static function sk_to_pk($sk) { return self::ge_p3_tobytes( self::ge_scalarmult_base( self::substr($sk, 0, 32) ) ); } /** * @internal You should not use this directly from another application * * @param string $message * @param string $sk * @return string * @throws SodiumException * @throws TypeError */ public static function sign($message, $sk) { /** @var string $signature */ $signature = self::sign_detached($message, $sk); return $signature . $message; } /** * @internal You should not use this directly from another application * * @param string $message A signed message * @param string $pk Public key * @return string Message (without signature) * @throws SodiumException * @throws TypeError */ public static function sign_open($message, $pk) { /** @var string $signature */ $signature = self::substr($message, 0, 64); /** @var string $message */ $message = self::substr($message, 64); if (self::verify_detached($signature, $message, $pk)) { return $message; } throw new SodiumException('Invalid signature'); } /** * @internal You should not use this directly from another application * * @param string $message * @param string $sk * @return string * @throws SodiumException * @throws TypeError * @psalm-suppress PossiblyInvalidArgument */ public static function sign_detached($message, $sk) { # crypto_hash_sha512(az, sk, 32); $az = hash('sha512', self::substr($sk, 0, 32), true); # az[0] &= 248; # az[31] &= 63; # az[31] |= 64; $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); # crypto_hash_sha512_init(&hs); # crypto_hash_sha512_update(&hs, az + 32, 32); # crypto_hash_sha512_update(&hs, m, mlen); # crypto_hash_sha512_final(&hs, nonce); $hs = hash_init('sha512'); self::hash_update($hs, self::substr($az, 32, 32)); self::hash_update($hs, $message); $nonceHash = hash_final($hs, true); # memmove(sig + 32, sk + 32, 32); $pk = self::substr($sk, 32, 32); # sc_reduce(nonce); # ge_scalarmult_base(&R, nonce); # ge_p3_tobytes(sig, &R); $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32); $sig = self::ge_p3_tobytes( self::ge_scalarmult_base($nonce) ); # crypto_hash_sha512_init(&hs); # crypto_hash_sha512_update(&hs, sig, 64); # crypto_hash_sha512_update(&hs, m, mlen); # crypto_hash_sha512_final(&hs, hram); $hs = hash_init('sha512'); self::hash_update($hs, self::substr($sig, 0, 32)); self::hash_update($hs, self::substr($pk, 0, 32)); self::hash_update($hs, $message); $hramHash = hash_final($hs, true); # sc_reduce(hram); # sc_muladd(sig + 32, hram, az, nonce); $hram = self::sc_reduce($hramHash); $sigAfter = self::sc_muladd($hram, $az, $nonce); $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); try { ParagonIE_Sodium_Compat::memzero($az); } catch (SodiumException $ex) { $az = null; } return $sig; } /** * @internal You should not use this directly from another application * * @param string $sig * @param string $message * @param string $pk * @return bool * @throws SodiumException * @throws TypeError */ public static function verify_detached($sig, $message, $pk) { if (self::strlen($sig) < 64) { throw new SodiumException('Signature is too short'); } if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) { throw new SodiumException('S < L - Invalid signature'); } if (self::small_order($sig)) { throw new SodiumException('Signature is on too small of an order'); } if ((self::chrToInt($sig[63]) & 224) !== 0) { throw new SodiumException('Invalid signature'); } $d = 0; for ($i = 0; $i < 32; ++$i) { $d |= self::chrToInt($pk[$i]); } if ($d === 0) { throw new SodiumException('All zero public key'); } /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ $orig = ParagonIE_Sodium_Compat::$fastMult; // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. ParagonIE_Sodium_Compat::$fastMult = true; /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */ $A = self::ge_frombytes_negate_vartime($pk); /** @var string $hDigest */ $hDigest = hash( 'sha512', self::substr($sig, 0, 32) . self::substr($pk, 0, 32) . $message, true ); /** @var string $h */ $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32); /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */ $R = self::ge_double_scalarmult_vartime( $h, $A, self::substr($sig, 32) ); /** @var string $rcheck */ $rcheck = self::ge_tobytes($R); // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. ParagonIE_Sodium_Compat::$fastMult = $orig; return self::verify_32($rcheck, self::substr($sig, 0, 32)); } /** * @internal You should not use this directly from another application * * @param string $S * @return bool * @throws SodiumException * @throws TypeError */ public static function check_S_lt_L($S) { if (self::strlen($S) < 32) { throw new SodiumException('Signature must be 32 bytes'); } static $L = array( 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 ); /** @var array<int, int> $L */ $c = 0; $n = 1; $i = 32; do { --$i; $x = self::chrToInt($S[$i]); $c |= ( (($x - $L[$i]) >> 8) & $n ); $n &= ( (($x ^ $L[$i]) - 1) >> 8 ); } while ($i !== 0); return $c === 0; } /** * @param string $R * @return bool * @throws SodiumException * @throws TypeError */ public static function small_order($R) { static $blocklist = array( /* 0 (order 4) */ array( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ), /* 1 (order 1) */ array( 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ), /* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ array( 0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05 ), /* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ array( 0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a ), /* p-1 (order 2) */ array( 0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85 ), /* p (order 4) */ array( 0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa ), /* p+1 (order 1) */ array( 0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f ), /* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ array( 0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f ), /* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ array( 0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f ), /* 2p-1 (order 2) */ array( 0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ), /* 2p (order 4) */ array( 0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ), /* 2p+1 (order 1) */ array( 0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ) ); /** @var array<int, array<int, int>> $blocklist */ $countBlocklist = count($blocklist); for ($i = 0; $i < $countBlocklist; ++$i) { $c = 0; for ($j = 0; $j < 32; ++$j) { $c |= self::chrToInt($R[$j]) ^ $blocklist[$i][$j]; } if ($c === 0) { return true; } } return false; } } Core32/SecretStream/State.php 0000644 00000007110 15162213657 0012002 0 ustar 00 <?php /** * Class ParagonIE_Sodium_Core32_SecretStream_State */ class ParagonIE_Sodium_Core32_SecretStream_State { /** @var string $key */ protected $key; /** @var int $counter */ protected $counter; /** @var string $nonce */ protected $nonce; /** @var string $_pad */ protected $_pad; /** * ParagonIE_Sodium_Core32_SecretStream_State constructor. * @param string $key * @param string|null $nonce */ public function __construct($key, $nonce = null) { $this->key = $key; $this->counter = 1; if (is_null($nonce)) { $nonce = str_repeat("\0", 12); } $this->nonce = str_pad($nonce, 12, "\0", STR_PAD_RIGHT);; $this->_pad = str_repeat("\0", 4); } /** * @return self */ public function counterReset() { $this->counter = 1; $this->_pad = str_repeat("\0", 4); return $this; } /** * @return string */ public function getKey() { return $this->key; } /** * @return string */ public function getCounter() { return ParagonIE_Sodium_Core32_Util::store32_le($this->counter); } /** * @return string */ public function getNonce() { if (!is_string($this->nonce)) { $this->nonce = str_repeat("\0", 12); } if (ParagonIE_Sodium_Core32_Util::strlen($this->nonce) !== 12) { $this->nonce = str_pad($this->nonce, 12, "\0", STR_PAD_RIGHT); } return $this->nonce; } /** * @return string */ public function getCombinedNonce() { return $this->getCounter() . ParagonIE_Sodium_Core32_Util::substr($this->getNonce(), 0, 8); } /** * @return self */ public function incrementCounter() { ++$this->counter; return $this; } /** * @return bool */ public function needsRekey() { return ($this->counter & 0xffff) === 0; } /** * @param string $newKeyAndNonce * @return self */ public function rekey($newKeyAndNonce) { $this->key = ParagonIE_Sodium_Core32_Util::substr($newKeyAndNonce, 0, 32); $this->nonce = str_pad( ParagonIE_Sodium_Core32_Util::substr($newKeyAndNonce, 32), 12, "\0", STR_PAD_RIGHT ); return $this; } /** * @param string $str * @return self */ public function xorNonce($str) { $this->nonce = ParagonIE_Sodium_Core32_Util::xorStrings( $this->getNonce(), str_pad( ParagonIE_Sodium_Core32_Util::substr($str, 0, 8), 12, "\0", STR_PAD_RIGHT ) ); return $this; } /** * @param string $string * @return self */ public static function fromString($string) { $state = new ParagonIE_Sodium_Core32_SecretStream_State( ParagonIE_Sodium_Core32_Util::substr($string, 0, 32) ); $state->counter = ParagonIE_Sodium_Core32_Util::load_4( ParagonIE_Sodium_Core32_Util::substr($string, 32, 4) ); $state->nonce = ParagonIE_Sodium_Core32_Util::substr($string, 36, 12); $state->_pad = ParagonIE_Sodium_Core32_Util::substr($string, 48, 8); return $state; } /** * @return string */ public function toString() { return $this->key . $this->getCounter() . $this->nonce . $this->_pad; } } Core32/Int64.php 0000644 00000074704 15162213660 0007234 0 ustar 00 <?php /** * Class ParagonIE_Sodium_Core32_Int64 * * Encapsulates a 64-bit integer. * * These are immutable. It always returns a new instance. */ class ParagonIE_Sodium_Core32_Int64 { /** * @var array<int, int> - four 16-bit integers */ public $limbs = array(0, 0, 0, 0); /** * @var int */ public $overflow = 0; /** * @var bool */ public $unsignedInt = false; /** * ParagonIE_Sodium_Core32_Int64 constructor. * @param array $array * @param bool $unsignedInt */ public function __construct($array = array(0, 0, 0, 0), $unsignedInt = false) { $this->limbs = array( (int) $array[0], (int) $array[1], (int) $array[2], (int) $array[3] ); $this->overflow = 0; $this->unsignedInt = $unsignedInt; } /** * Adds two int64 objects * * @param ParagonIE_Sodium_Core32_Int64 $addend * @return ParagonIE_Sodium_Core32_Int64 */ public function addInt64(ParagonIE_Sodium_Core32_Int64 $addend) { $i0 = $this->limbs[0]; $i1 = $this->limbs[1]; $i2 = $this->limbs[2]; $i3 = $this->limbs[3]; $j0 = $addend->limbs[0]; $j1 = $addend->limbs[1]; $j2 = $addend->limbs[2]; $j3 = $addend->limbs[3]; $r3 = $i3 + ($j3 & 0xffff); $carry = $r3 >> 16; $r2 = $i2 + ($j2 & 0xffff) + $carry; $carry = $r2 >> 16; $r1 = $i1 + ($j1 & 0xffff) + $carry; $carry = $r1 >> 16; $r0 = $i0 + ($j0 & 0xffff) + $carry; $carry = $r0 >> 16; $r0 &= 0xffff; $r1 &= 0xffff; $r2 &= 0xffff; $r3 &= 0xffff; $return = new ParagonIE_Sodium_Core32_Int64( array($r0, $r1, $r2, $r3) ); $return->overflow = $carry; $return->unsignedInt = $this->unsignedInt; return $return; } /** * Adds a normal integer to an int64 object * * @param int $int * @return ParagonIE_Sodium_Core32_Int64 * @throws SodiumException * @throws TypeError */ public function addInt($int) { ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); /** @var int $int */ $int = (int) $int; $i0 = $this->limbs[0]; $i1 = $this->limbs[1]; $i2 = $this->limbs[2]; $i3 = $this->limbs[3]; $r3 = $i3 + ($int & 0xffff); $carry = $r3 >> 16; $r2 = $i2 + (($int >> 16) & 0xffff) + $carry; $carry = $r2 >> 16; $r1 = $i1 + $carry; $carry = $r1 >> 16; $r0 = $i0 + $carry; $carry = $r0 >> 16; $r0 &= 0xffff; $r1 &= 0xffff; $r2 &= 0xffff; $r3 &= 0xffff; $return = new ParagonIE_Sodium_Core32_Int64( array($r0, $r1, $r2, $r3) ); $return->overflow = $carry; $return->unsignedInt = $this->unsignedInt; return $return; } /** * @param int $b * @return int */ public function compareInt($b = 0) { $gt = 0; $eq = 1; $i = 4; $j = 0; while ($i > 0) { --$i; /** @var int $x1 */ $x1 = $this->limbs[$i]; /** @var int $x2 */ $x2 = ($b >> ($j << 4)) & 0xffff; /** int */ $gt |= (($x2 - $x1) >> 8) & $eq; /** int */ $eq &= (($x2 ^ $x1) - 1) >> 8; } return ($gt + $gt - $eq) + 1; } /** * @param int $b * @return bool */ public function isGreaterThan($b = 0) { return $this->compareInt($b) > 0; } /** * @param int $b * @return bool */ public function isLessThanInt($b = 0) { return $this->compareInt($b) < 0; } /** * @param int $hi * @param int $lo * @return ParagonIE_Sodium_Core32_Int64 */ public function mask64($hi = 0, $lo = 0) { /** @var int $a */ $a = ($hi >> 16) & 0xffff; /** @var int $b */ $b = ($hi) & 0xffff; /** @var int $c */ $c = ($lo >> 16) & 0xffff; /** @var int $d */ $d = ($lo & 0xffff); return new ParagonIE_Sodium_Core32_Int64( array( $this->limbs[0] & $a, $this->limbs[1] & $b, $this->limbs[2] & $c, $this->limbs[3] & $d ), $this->unsignedInt ); } /** * @param int $int * @param int $size * @return ParagonIE_Sodium_Core32_Int64 * @throws SodiumException * @throws TypeError * @psalm-suppress MixedAssignment */ public function mulInt($int = 0, $size = 0) { if (ParagonIE_Sodium_Compat::$fastMult) { return $this->mulIntFast($int); } ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); /** @var int $int */ $int = (int) $int; /** @var int $size */ $size = (int) $size; if (!$size) { $size = 63; } $a = clone $this; $return = new ParagonIE_Sodium_Core32_Int64(); $return->unsignedInt = $this->unsignedInt; // Initialize: $ret0 = 0; $ret1 = 0; $ret2 = 0; $ret3 = 0; $a0 = $a->limbs[0]; $a1 = $a->limbs[1]; $a2 = $a->limbs[2]; $a3 = $a->limbs[3]; /** @var int $size */ /** @var int $i */ for ($i = $size; $i >= 0; --$i) { $mask = -($int & 1); $x0 = $a0 & $mask; $x1 = $a1 & $mask; $x2 = $a2 & $mask; $x3 = $a3 & $mask; $ret3 += $x3; $c = $ret3 >> 16; $ret2 += $x2 + $c; $c = $ret2 >> 16; $ret1 += $x1 + $c; $c = $ret1 >> 16; $ret0 += $x0 + $c; $ret0 &= 0xffff; $ret1 &= 0xffff; $ret2 &= 0xffff; $ret3 &= 0xffff; $a3 = $a3 << 1; $x3 = $a3 >> 16; $a2 = ($a2 << 1) | $x3; $x2 = $a2 >> 16; $a1 = ($a1 << 1) | $x2; $x1 = $a1 >> 16; $a0 = ($a0 << 1) | $x1; $a0 &= 0xffff; $a1 &= 0xffff; $a2 &= 0xffff; $a3 &= 0xffff; $int >>= 1; } $return->limbs[0] = $ret0; $return->limbs[1] = $ret1; $return->limbs[2] = $ret2; $return->limbs[3] = $ret3; return $return; } /** * @param ParagonIE_Sodium_Core32_Int64 $A * @param ParagonIE_Sodium_Core32_Int64 $B * @return array<int, ParagonIE_Sodium_Core32_Int64> * @throws SodiumException * @throws TypeError * @psalm-suppress MixedInferredReturnType */ public static function ctSelect( ParagonIE_Sodium_Core32_Int64 $A, ParagonIE_Sodium_Core32_Int64 $B ) { $a = clone $A; $b = clone $B; /** @var int $aNeg */ $aNeg = ($a->limbs[0] >> 15) & 1; /** @var int $bNeg */ $bNeg = ($b->limbs[0] >> 15) & 1; /** @var int $m */ $m = (-($aNeg & $bNeg)) | 1; /** @var int $swap */ $swap = $bNeg & ~$aNeg; /** @var int $d */ $d = -$swap; /* if ($bNeg && !$aNeg) { $a = clone $int; $b = clone $this; } elseif($bNeg && $aNeg) { $a = $this->mulInt(-1); $b = $int->mulInt(-1); } */ $x = $a->xorInt64($b)->mask64($d, $d); return array( $a->xorInt64($x)->mulInt($m), $b->xorInt64($x)->mulInt($m) ); } /** * @param array<int, int> $a * @param array<int, int> $b * @param int $baseLog2 * @return array<int, int> */ public function multiplyLong(array $a, array $b, $baseLog2 = 16) { $a_l = count($a); $b_l = count($b); /** @var array<int, int> $r */ $r = array_fill(0, $a_l + $b_l + 1, 0); $base = 1 << $baseLog2; for ($i = 0; $i < $a_l; ++$i) { $a_i = $a[$i]; for ($j = 0; $j < $a_l; ++$j) { $b_j = $b[$j]; $product = (($a_i * $b_j) + $r[$i + $j]); $carry = (((int) $product >> $baseLog2) & 0xffff); $r[$i + $j] = ((int) $product - (int) ($carry * $base)) & 0xffff; $r[$i + $j + 1] += $carry; } } return array_slice($r, 0, 5); } /** * @param int $int * @return ParagonIE_Sodium_Core32_Int64 */ public function mulIntFast($int) { // Handle negative numbers $aNeg = ($this->limbs[0] >> 15) & 1; $bNeg = ($int >> 31) & 1; $a = array_reverse($this->limbs); $b = array( $int & 0xffff, ($int >> 16) & 0xffff, -$bNeg & 0xffff, -$bNeg & 0xffff ); if ($aNeg) { for ($i = 0; $i < 4; ++$i) { $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; } ++$a[0]; } if ($bNeg) { for ($i = 0; $i < 4; ++$i) { $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; } ++$b[0]; } // Multiply $res = $this->multiplyLong($a, $b); // Re-apply negation to results if ($aNeg !== $bNeg) { for ($i = 0; $i < 4; ++$i) { $res[$i] = (0xffff ^ $res[$i]) & 0xffff; } // Handle integer overflow $c = 1; for ($i = 0; $i < 4; ++$i) { $res[$i] += $c; $c = $res[$i] >> 16; $res[$i] &= 0xffff; } } // Return our values $return = new ParagonIE_Sodium_Core32_Int64(); $return->limbs = array( $res[3] & 0xffff, $res[2] & 0xffff, $res[1] & 0xffff, $res[0] & 0xffff ); if (count($res) > 4) { $return->overflow = $res[4] & 0xffff; } $return->unsignedInt = $this->unsignedInt; return $return; } /** * @param ParagonIE_Sodium_Core32_Int64 $right * @return ParagonIE_Sodium_Core32_Int64 */ public function mulInt64Fast(ParagonIE_Sodium_Core32_Int64 $right) { $aNeg = ($this->limbs[0] >> 15) & 1; $bNeg = ($right->limbs[0] >> 15) & 1; $a = array_reverse($this->limbs); $b = array_reverse($right->limbs); if ($aNeg) { for ($i = 0; $i < 4; ++$i) { $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; } ++$a[0]; } if ($bNeg) { for ($i = 0; $i < 4; ++$i) { $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; } ++$b[0]; } $res = $this->multiplyLong($a, $b); if ($aNeg !== $bNeg) { if ($aNeg !== $bNeg) { for ($i = 0; $i < 4; ++$i) { $res[$i] = ($res[$i] ^ 0xffff) & 0xffff; } $c = 1; for ($i = 0; $i < 4; ++$i) { $res[$i] += $c; $c = $res[$i] >> 16; $res[$i] &= 0xffff; } } } $return = new ParagonIE_Sodium_Core32_Int64(); $return->limbs = array( $res[3] & 0xffff, $res[2] & 0xffff, $res[1] & 0xffff, $res[0] & 0xffff ); if (count($res) > 4) { $return->overflow = $res[4]; } return $return; } /** * @param ParagonIE_Sodium_Core32_Int64 $int * @param int $size * @return ParagonIE_Sodium_Core32_Int64 * @throws SodiumException * @throws TypeError * @psalm-suppress MixedAssignment */ public function mulInt64(ParagonIE_Sodium_Core32_Int64 $int, $size = 0) { if (ParagonIE_Sodium_Compat::$fastMult) { return $this->mulInt64Fast($int); } ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); if (!$size) { $size = 63; } list($a, $b) = self::ctSelect($this, $int); $return = new ParagonIE_Sodium_Core32_Int64(); $return->unsignedInt = $this->unsignedInt; // Initialize: $ret0 = 0; $ret1 = 0; $ret2 = 0; $ret3 = 0; $a0 = $a->limbs[0]; $a1 = $a->limbs[1]; $a2 = $a->limbs[2]; $a3 = $a->limbs[3]; $b0 = $b->limbs[0]; $b1 = $b->limbs[1]; $b2 = $b->limbs[2]; $b3 = $b->limbs[3]; /** @var int $size */ /** @var int $i */ for ($i = (int) $size; $i >= 0; --$i) { $mask = -($b3 & 1); $x0 = $a0 & $mask; $x1 = $a1 & $mask; $x2 = $a2 & $mask; $x3 = $a3 & $mask; $ret3 += $x3; $c = $ret3 >> 16; $ret2 += $x2 + $c; $c = $ret2 >> 16; $ret1 += $x1 + $c; $c = $ret1 >> 16; $ret0 += $x0 + $c; $ret0 &= 0xffff; $ret1 &= 0xffff; $ret2 &= 0xffff; $ret3 &= 0xffff; $a3 = $a3 << 1; $x3 = $a3 >> 16; $a2 = ($a2 << 1) | $x3; $x2 = $a2 >> 16; $a1 = ($a1 << 1) | $x2; $x1 = $a1 >> 16; $a0 = ($a0 << 1) | $x1; $a0 &= 0xffff; $a1 &= 0xffff; $a2 &= 0xffff; $a3 &= 0xffff; $x0 = ($b0 & 1) << 16; $x1 = ($b1 & 1) << 16; $x2 = ($b2 & 1) << 16; $b0 = ($b0 >> 1); $b1 = (($b1 | $x0) >> 1); $b2 = (($b2 | $x1) >> 1); $b3 = (($b3 | $x2) >> 1); $b0 &= 0xffff; $b1 &= 0xffff; $b2 &= 0xffff; $b3 &= 0xffff; } $return->limbs[0] = $ret0; $return->limbs[1] = $ret1; $return->limbs[2] = $ret2; $return->limbs[3] = $ret3; return $return; } /** * OR this 64-bit integer with another. * * @param ParagonIE_Sodium_Core32_Int64 $b * @return ParagonIE_Sodium_Core32_Int64 */ public function orInt64(ParagonIE_Sodium_Core32_Int64 $b) { $return = new ParagonIE_Sodium_Core32_Int64(); $return->unsignedInt = $this->unsignedInt; $return->limbs = array( (int) ($this->limbs[0] | $b->limbs[0]), (int) ($this->limbs[1] | $b->limbs[1]), (int) ($this->limbs[2] | $b->limbs[2]), (int) ($this->limbs[3] | $b->limbs[3]) ); return $return; } /** * @param int $c * @return ParagonIE_Sodium_Core32_Int64 * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArrayAccess */ public function rotateLeft($c = 0) { ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); /** @var int $c */ $c = (int) $c; $return = new ParagonIE_Sodium_Core32_Int64(); $return->unsignedInt = $this->unsignedInt; $c &= 63; if ($c === 0) { // NOP, but we want a copy. $return->limbs = $this->limbs; } else { /** @var array<int, int> $limbs */ $limbs =& $return->limbs; /** @var array<int, int> $myLimbs */ $myLimbs =& $this->limbs; /** @var int $idx_shift */ $idx_shift = ($c >> 4) & 3; /** @var int $sub_shift */ $sub_shift = $c & 15; for ($i = 3; $i >= 0; --$i) { /** @var int $j */ $j = ($i + $idx_shift) & 3; /** @var int $k */ $k = ($i + $idx_shift + 1) & 3; $limbs[$i] = (int) ( ( ((int) ($myLimbs[$j]) << $sub_shift) | ((int) ($myLimbs[$k]) >> (16 - $sub_shift)) ) & 0xffff ); } } return $return; } /** * Rotate to the right * * @param int $c * @return ParagonIE_Sodium_Core32_Int64 * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArrayAccess */ public function rotateRight($c = 0) { ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); /** @var int $c */ $c = (int) $c; /** @var ParagonIE_Sodium_Core32_Int64 $return */ $return = new ParagonIE_Sodium_Core32_Int64(); $return->unsignedInt = $this->unsignedInt; $c &= 63; /** @var int $c */ if ($c === 0) { // NOP, but we want a copy. $return->limbs = $this->limbs; } else { /** @var array<int, int> $limbs */ $limbs =& $return->limbs; /** @var array<int, int> $myLimbs */ $myLimbs =& $this->limbs; /** @var int $idx_shift */ $idx_shift = ($c >> 4) & 3; /** @var int $sub_shift */ $sub_shift = $c & 15; for ($i = 3; $i >= 0; --$i) { /** @var int $j */ $j = ($i - $idx_shift) & 3; /** @var int $k */ $k = ($i - $idx_shift - 1) & 3; $limbs[$i] = (int) ( ( ((int) ($myLimbs[$j]) >> (int) ($sub_shift)) | ((int) ($myLimbs[$k]) << (16 - (int) ($sub_shift))) ) & 0xffff ); } } return $return; } /** * @param int $c * @return ParagonIE_Sodium_Core32_Int64 * @throws SodiumException * @throws TypeError */ public function shiftLeft($c = 0) { ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); /** @var int $c */ $c = (int) $c; $return = new ParagonIE_Sodium_Core32_Int64(); $return->unsignedInt = $this->unsignedInt; $c &= 63; if ($c >= 16) { if ($c >= 48) { $return->limbs = array( $this->limbs[3], 0, 0, 0 ); } elseif ($c >= 32) { $return->limbs = array( $this->limbs[2], $this->limbs[3], 0, 0 ); } else { $return->limbs = array( $this->limbs[1], $this->limbs[2], $this->limbs[3], 0 ); } return $return->shiftLeft($c & 15); } if ($c === 0) { $return->limbs = $this->limbs; } elseif ($c < 0) { /** @var int $c */ return $this->shiftRight(-$c); } else { if (!is_int($c)) { throw new TypeError(); } /** @var int $carry */ $carry = 0; for ($i = 3; $i >= 0; --$i) { /** @var int $tmp */ $tmp = ($this->limbs[$i] << $c) | ($carry & 0xffff); $return->limbs[$i] = (int) ($tmp & 0xffff); /** @var int $carry */ $carry = $tmp >> 16; } } return $return; } /** * @param int $c * @return ParagonIE_Sodium_Core32_Int64 * @throws SodiumException * @throws TypeError */ public function shiftRight($c = 0) { ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); $c = (int) $c; /** @var int $c */ $return = new ParagonIE_Sodium_Core32_Int64(); $return->unsignedInt = $this->unsignedInt; $c &= 63; $negative = -(($this->limbs[0] >> 15) & 1); if ($c >= 16) { if ($c >= 48) { $return->limbs = array( (int) ($negative & 0xffff), (int) ($negative & 0xffff), (int) ($negative & 0xffff), (int) $this->limbs[0] ); } elseif ($c >= 32) { $return->limbs = array( (int) ($negative & 0xffff), (int) ($negative & 0xffff), (int) $this->limbs[0], (int) $this->limbs[1] ); } else { $return->limbs = array( (int) ($negative & 0xffff), (int) $this->limbs[0], (int) $this->limbs[1], (int) $this->limbs[2] ); } return $return->shiftRight($c & 15); } if ($c === 0) { $return->limbs = $this->limbs; } elseif ($c < 0) { return $this->shiftLeft(-$c); } else { if (!is_int($c)) { throw new TypeError(); } /** @var int $carryRight */ $carryRight = ($negative & 0xffff); $mask = (int) (((1 << ($c + 1)) - 1) & 0xffff); for ($i = 0; $i < 4; ++$i) { $return->limbs[$i] = (int) ( (($this->limbs[$i] >> $c) | ($carryRight << (16 - $c))) & 0xffff ); $carryRight = (int) ($this->limbs[$i] & $mask); } } return $return; } /** * Subtract a normal integer from an int64 object. * * @param int $int * @return ParagonIE_Sodium_Core32_Int64 * @throws SodiumException * @throws TypeError */ public function subInt($int) { ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); $int = (int) $int; $return = new ParagonIE_Sodium_Core32_Int64(); $return->unsignedInt = $this->unsignedInt; /** @var int $carry */ $carry = 0; for ($i = 3; $i >= 0; --$i) { /** @var int $tmp */ $tmp = $this->limbs[$i] - (($int >> 16) & 0xffff) + $carry; /** @var int $carry */ $carry = $tmp >> 16; $return->limbs[$i] = (int) ($tmp & 0xffff); } return $return; } /** * The difference between two Int64 objects. * * @param ParagonIE_Sodium_Core32_Int64 $b * @return ParagonIE_Sodium_Core32_Int64 */ public function subInt64(ParagonIE_Sodium_Core32_Int64 $b) { $return = new ParagonIE_Sodium_Core32_Int64(); $return->unsignedInt = $this->unsignedInt; /** @var int $carry */ $carry = 0; for ($i = 3; $i >= 0; --$i) { /** @var int $tmp */ $tmp = $this->limbs[$i] - $b->limbs[$i] + $carry; /** @var int $carry */ $carry = ($tmp >> 16); $return->limbs[$i] = (int) ($tmp & 0xffff); } return $return; } /** * XOR this 64-bit integer with another. * * @param ParagonIE_Sodium_Core32_Int64 $b * @return ParagonIE_Sodium_Core32_Int64 */ public function xorInt64(ParagonIE_Sodium_Core32_Int64 $b) { $return = new ParagonIE_Sodium_Core32_Int64(); $return->unsignedInt = $this->unsignedInt; $return->limbs = array( (int) ($this->limbs[0] ^ $b->limbs[0]), (int) ($this->limbs[1] ^ $b->limbs[1]), (int) ($this->limbs[2] ^ $b->limbs[2]), (int) ($this->limbs[3] ^ $b->limbs[3]) ); return $return; } /** * @param int $low * @param int $high * @return self * @throws SodiumException * @throws TypeError */ public static function fromInts($low, $high) { ParagonIE_Sodium_Core32_Util::declareScalarType($low, 'int', 1); ParagonIE_Sodium_Core32_Util::declareScalarType($high, 'int', 2); $high = (int) $high; $low = (int) $low; return new ParagonIE_Sodium_Core32_Int64( array( (int) (($high >> 16) & 0xffff), (int) ($high & 0xffff), (int) (($low >> 16) & 0xffff), (int) ($low & 0xffff) ) ); } /** * @param int $low * @return self * @throws SodiumException * @throws TypeError */ public static function fromInt($low) { ParagonIE_Sodium_Core32_Util::declareScalarType($low, 'int', 1); $low = (int) $low; return new ParagonIE_Sodium_Core32_Int64( array( 0, 0, (int) (($low >> 16) & 0xffff), (int) ($low & 0xffff) ) ); } /** * @return int */ public function toInt() { return (int) ( (($this->limbs[2] & 0xffff) << 16) | ($this->limbs[3] & 0xffff) ); } /** * @param string $string * @return self * @throws SodiumException * @throws TypeError */ public static function fromString($string) { ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); $string = (string) $string; if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) { throw new RangeException( 'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' ); } $return = new ParagonIE_Sodium_Core32_Int64(); $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff) << 8); $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff); $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff) << 8); $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff); $return->limbs[2] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[4]) & 0xff) << 8); $return->limbs[2] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[5]) & 0xff); $return->limbs[3] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[6]) & 0xff) << 8); $return->limbs[3] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[7]) & 0xff); return $return; } /** * @param string $string * @return self * @throws SodiumException * @throws TypeError */ public static function fromReverseString($string) { ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); $string = (string) $string; if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) { throw new RangeException( 'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' ); } $return = new ParagonIE_Sodium_Core32_Int64(); $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[7]) & 0xff) << 8); $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[6]) & 0xff); $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[5]) & 0xff) << 8); $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[4]) & 0xff); $return->limbs[2] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff) << 8); $return->limbs[2] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff); $return->limbs[3] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff) << 8); $return->limbs[3] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff); return $return; } /** * @return array<int, int> */ public function toArray() { return array( (int) ((($this->limbs[0] & 0xffff) << 16) | ($this->limbs[1] & 0xffff)), (int) ((($this->limbs[2] & 0xffff) << 16) | ($this->limbs[3] & 0xffff)) ); } /** * @return ParagonIE_Sodium_Core32_Int32 */ public function toInt32() { $return = new ParagonIE_Sodium_Core32_Int32(); $return->limbs[0] = (int) ($this->limbs[2]); $return->limbs[1] = (int) ($this->limbs[3]); $return->unsignedInt = $this->unsignedInt; $return->overflow = (int) (ParagonIE_Sodium_Core32_Util::abs($this->limbs[1], 16) & 0xffff); return $return; } /** * @return ParagonIE_Sodium_Core32_Int64 */ public function toInt64() { $return = new ParagonIE_Sodium_Core32_Int64(); $return->limbs[0] = (int) ($this->limbs[0]); $return->limbs[1] = (int) ($this->limbs[1]); $return->limbs[2] = (int) ($this->limbs[2]); $return->limbs[3] = (int) ($this->limbs[3]); $return->unsignedInt = $this->unsignedInt; $return->overflow = ParagonIE_Sodium_Core32_Util::abs($this->overflow); return $return; } /** * @param bool $bool * @return self */ public function setUnsignedInt($bool = false) { $this->unsignedInt = !empty($bool); return $this; } /** * @return string * @throws TypeError */ public function toString() { return ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[2] >> 8) & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff); } /** * @return string * @throws TypeError */ public function toReverseString() { return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[2] >> 8) & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff); } /** * @return string */ public function __toString() { try { return $this->toString(); } catch (TypeError $ex) { // PHP engine can't handle exceptions from __toString() return ''; } } } Core32/X25519.php 0000644 00000025442 15162213660 0007140 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_X25519', false)) { return; } /** * Class ParagonIE_Sodium_Core32_X25519 */ abstract class ParagonIE_Sodium_Core32_X25519 extends ParagonIE_Sodium_Core32_Curve25519 { /** * Alters the objects passed to this method in place. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f * @param ParagonIE_Sodium_Core32_Curve25519_Fe $g * @param int $b * @return void * @throws SodiumException * @throws TypeError * @psalm-suppress MixedMethodCall */ public static function fe_cswap( ParagonIE_Sodium_Core32_Curve25519_Fe $f, ParagonIE_Sodium_Core32_Curve25519_Fe $g, $b = 0 ) { $f0 = (int) $f[0]->toInt(); $f1 = (int) $f[1]->toInt(); $f2 = (int) $f[2]->toInt(); $f3 = (int) $f[3]->toInt(); $f4 = (int) $f[4]->toInt(); $f5 = (int) $f[5]->toInt(); $f6 = (int) $f[6]->toInt(); $f7 = (int) $f[7]->toInt(); $f8 = (int) $f[8]->toInt(); $f9 = (int) $f[9]->toInt(); $g0 = (int) $g[0]->toInt(); $g1 = (int) $g[1]->toInt(); $g2 = (int) $g[2]->toInt(); $g3 = (int) $g[3]->toInt(); $g4 = (int) $g[4]->toInt(); $g5 = (int) $g[5]->toInt(); $g6 = (int) $g[6]->toInt(); $g7 = (int) $g[7]->toInt(); $g8 = (int) $g[8]->toInt(); $g9 = (int) $g[9]->toInt(); $b = -$b; /** @var int $x0 */ $x0 = ($f0 ^ $g0) & $b; /** @var int $x1 */ $x1 = ($f1 ^ $g1) & $b; /** @var int $x2 */ $x2 = ($f2 ^ $g2) & $b; /** @var int $x3 */ $x3 = ($f3 ^ $g3) & $b; /** @var int $x4 */ $x4 = ($f4 ^ $g4) & $b; /** @var int $x5 */ $x5 = ($f5 ^ $g5) & $b; /** @var int $x6 */ $x6 = ($f6 ^ $g6) & $b; /** @var int $x7 */ $x7 = ($f7 ^ $g7) & $b; /** @var int $x8 */ $x8 = ($f8 ^ $g8) & $b; /** @var int $x9 */ $x9 = ($f9 ^ $g9) & $b; $f[0] = ParagonIE_Sodium_Core32_Int32::fromInt($f0 ^ $x0); $f[1] = ParagonIE_Sodium_Core32_Int32::fromInt($f1 ^ $x1); $f[2] = ParagonIE_Sodium_Core32_Int32::fromInt($f2 ^ $x2); $f[3] = ParagonIE_Sodium_Core32_Int32::fromInt($f3 ^ $x3); $f[4] = ParagonIE_Sodium_Core32_Int32::fromInt($f4 ^ $x4); $f[5] = ParagonIE_Sodium_Core32_Int32::fromInt($f5 ^ $x5); $f[6] = ParagonIE_Sodium_Core32_Int32::fromInt($f6 ^ $x6); $f[7] = ParagonIE_Sodium_Core32_Int32::fromInt($f7 ^ $x7); $f[8] = ParagonIE_Sodium_Core32_Int32::fromInt($f8 ^ $x8); $f[9] = ParagonIE_Sodium_Core32_Int32::fromInt($f9 ^ $x9); $g[0] = ParagonIE_Sodium_Core32_Int32::fromInt($g0 ^ $x0); $g[1] = ParagonIE_Sodium_Core32_Int32::fromInt($g1 ^ $x1); $g[2] = ParagonIE_Sodium_Core32_Int32::fromInt($g2 ^ $x2); $g[3] = ParagonIE_Sodium_Core32_Int32::fromInt($g3 ^ $x3); $g[4] = ParagonIE_Sodium_Core32_Int32::fromInt($g4 ^ $x4); $g[5] = ParagonIE_Sodium_Core32_Int32::fromInt($g5 ^ $x5); $g[6] = ParagonIE_Sodium_Core32_Int32::fromInt($g6 ^ $x6); $g[7] = ParagonIE_Sodium_Core32_Int32::fromInt($g7 ^ $x7); $g[8] = ParagonIE_Sodium_Core32_Int32::fromInt($g8 ^ $x8); $g[9] = ParagonIE_Sodium_Core32_Int32::fromInt($g9 ^ $x9); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f * @return ParagonIE_Sodium_Core32_Curve25519_Fe * @throws SodiumException * @throws TypeError * @psalm-suppress MixedAssignment * @psalm-suppress MixedMethodCall */ public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) { /** @var array<int, ParagonIE_Sodium_Core32_Int64> $h */ $h = array(); for ($i = 0; $i < 10; ++$i) { $h[$i] = $f[$i]->toInt64()->mulInt(121666, 17); } $carry9 = $h[9]->addInt(1 << 24)->shiftRight(25); $h[0] = $h[0]->addInt64($carry9->mulInt(19, 5)); $h[9] = $h[9]->subInt64($carry9->shiftLeft(25)); $carry1 = $h[1]->addInt(1 << 24)->shiftRight(25); $h[2] = $h[2]->addInt64($carry1); $h[1] = $h[1]->subInt64($carry1->shiftLeft(25)); $carry3 = $h[3]->addInt(1 << 24)->shiftRight(25); $h[4] = $h[4]->addInt64($carry3); $h[3] = $h[3]->subInt64($carry3->shiftLeft(25)); $carry5 = $h[5]->addInt(1 << 24)->shiftRight(25); $h[6] = $h[6]->addInt64($carry5); $h[5] = $h[5]->subInt64($carry5->shiftLeft(25)); $carry7 = $h[7]->addInt(1 << 24)->shiftRight(25); $h[8] = $h[8]->addInt64($carry7); $h[7] = $h[7]->subInt64($carry7->shiftLeft(25)); $carry0 = $h[0]->addInt(1 << 25)->shiftRight(26); $h[1] = $h[1]->addInt64($carry0); $h[0] = $h[0]->subInt64($carry0->shiftLeft(26)); $carry2 = $h[2]->addInt(1 << 25)->shiftRight(26); $h[3] = $h[3]->addInt64($carry2); $h[2] = $h[2]->subInt64($carry2->shiftLeft(26)); $carry4 = $h[4]->addInt(1 << 25)->shiftRight(26); $h[5] = $h[5]->addInt64($carry4); $h[4] = $h[4]->subInt64($carry4->shiftLeft(26)); $carry6 = $h[6]->addInt(1 << 25)->shiftRight(26); $h[7] = $h[7]->addInt64($carry6); $h[6] = $h[6]->subInt64($carry6->shiftLeft(26)); $carry8 = $h[8]->addInt(1 << 25)->shiftRight(26); $h[9] = $h[9]->addInt64($carry8); $h[8] = $h[8]->subInt64($carry8->shiftLeft(26)); for ($i = 0; $i < 10; ++$i) { $h[$i] = $h[$i]->toInt32(); } /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h2 */ $h2 = $h; return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h2); } /** * @internal You should not use this directly from another application * * Inline comments preceded by # are from libsodium's ref10 code. * * @param string $n * @param string $p * @return string * @throws SodiumException * @throws TypeError */ public static function crypto_scalarmult_curve25519_ref10($n, $p) { # for (i = 0;i < 32;++i) e[i] = n[i]; $e = '' . $n; # e[0] &= 248; $e[0] = self::intToChr( self::chrToInt($e[0]) & 248 ); # e[31] &= 127; # e[31] |= 64; $e[31] = self::intToChr( (self::chrToInt($e[31]) & 127) | 64 ); # fe_frombytes(x1,p); $x1 = self::fe_frombytes($p); # fe_1(x2); $x2 = self::fe_1(); # fe_0(z2); $z2 = self::fe_0(); # fe_copy(x3,x1); $x3 = self::fe_copy($x1); # fe_1(z3); $z3 = self::fe_1(); # swap = 0; /** @var int $swap */ $swap = 0; # for (pos = 254;pos >= 0;--pos) { for ($pos = 254; $pos >= 0; --$pos) { # b = e[pos / 8] >> (pos & 7); /** @var int $b */ $b = self::chrToInt( $e[(int) floor($pos / 8)] ) >> ($pos & 7); # b &= 1; $b &= 1; # swap ^= b; $swap ^= $b; # fe_cswap(x2,x3,swap); self::fe_cswap($x2, $x3, $swap); # fe_cswap(z2,z3,swap); self::fe_cswap($z2, $z3, $swap); # swap = b; /** @var int $swap */ $swap = $b; # fe_sub(tmp0,x3,z3); $tmp0 = self::fe_sub($x3, $z3); # fe_sub(tmp1,x2,z2); $tmp1 = self::fe_sub($x2, $z2); # fe_add(x2,x2,z2); $x2 = self::fe_add($x2, $z2); # fe_add(z2,x3,z3); $z2 = self::fe_add($x3, $z3); # fe_mul(z3,tmp0,x2); $z3 = self::fe_mul($tmp0, $x2); # fe_mul(z2,z2,tmp1); $z2 = self::fe_mul($z2, $tmp1); # fe_sq(tmp0,tmp1); $tmp0 = self::fe_sq($tmp1); # fe_sq(tmp1,x2); $tmp1 = self::fe_sq($x2); # fe_add(x3,z3,z2); $x3 = self::fe_add($z3, $z2); # fe_sub(z2,z3,z2); $z2 = self::fe_sub($z3, $z2); # fe_mul(x2,tmp1,tmp0); $x2 = self::fe_mul($tmp1, $tmp0); # fe_sub(tmp1,tmp1,tmp0); $tmp1 = self::fe_sub($tmp1, $tmp0); # fe_sq(z2,z2); $z2 = self::fe_sq($z2); # fe_mul121666(z3,tmp1); $z3 = self::fe_mul121666($tmp1); # fe_sq(x3,x3); $x3 = self::fe_sq($x3); # fe_add(tmp0,tmp0,z3); $tmp0 = self::fe_add($tmp0, $z3); # fe_mul(z3,x1,z2); $z3 = self::fe_mul($x1, $z2); # fe_mul(z2,tmp1,tmp0); $z2 = self::fe_mul($tmp1, $tmp0); } # fe_cswap(x2,x3,swap); self::fe_cswap($x2, $x3, $swap); # fe_cswap(z2,z3,swap); self::fe_cswap($z2, $z3, $swap); # fe_invert(z2,z2); $z2 = self::fe_invert($z2); # fe_mul(x2,x2,z2); $x2 = self::fe_mul($x2, $z2); # fe_tobytes(q,x2); return (string) self::fe_tobytes($x2); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ * @return ParagonIE_Sodium_Core32_Curve25519_Fe * @throws SodiumException * @throws TypeError */ public static function edwards_to_montgomery( ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY, ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ ) { $tempX = self::fe_add($edwardsZ, $edwardsY); $tempZ = self::fe_sub($edwardsZ, $edwardsY); $tempZ = self::fe_invert($tempZ); return self::fe_mul($tempX, $tempZ); } /** * @internal You should not use this directly from another application * * @param string $n * @return string * @throws SodiumException * @throws TypeError */ public static function crypto_scalarmult_curve25519_ref10_base($n) { # for (i = 0;i < 32;++i) e[i] = n[i]; $e = '' . $n; # e[0] &= 248; $e[0] = self::intToChr( self::chrToInt($e[0]) & 248 ); # e[31] &= 127; # e[31] |= 64; $e[31] = self::intToChr( (self::chrToInt($e[31]) & 127) | 64 ); $A = self::ge_scalarmult_base($e); if ( !($A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) || !($A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) ) { throw new TypeError('Null points encountered'); } $pk = self::edwards_to_montgomery($A->Y, $A->Z); return self::fe_tobytes($pk); } } Core32/Util.php 0000644 00000000321 15162213660 0007225 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_Util', false)) { return; } /** * Class ParagonIE_Sodium_Core_Util */ abstract class ParagonIE_Sodium_Core32_Util extends ParagonIE_Sodium_Core_Util { } Core32/BLAKE2b.php 0000644 00000053464 15162213661 0007373 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) { return; } /** * Class ParagonIE_Sodium_Core_BLAKE2b * * Based on the work of Devi Mandiri in devi/salt. */ abstract class ParagonIE_Sodium_Core32_BLAKE2b extends ParagonIE_Sodium_Core_Util { /** * @var SplFixedArray */ public static $iv; /** * @var array<int, array<int, int>> */ public static $sigma = array( array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3), array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4), array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8), array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13), array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9), array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11), array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10), array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5), array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0), array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3) ); const BLOCKBYTES = 128; const OUTBYTES = 64; const KEYBYTES = 64; /** * Turn two 32-bit integers into a fixed array representing a 64-bit integer. * * @internal You should not use this directly from another application * * @param int $high * @param int $low * @return ParagonIE_Sodium_Core32_Int64 * @throws SodiumException * @throws TypeError */ public static function new64($high, $low) { return ParagonIE_Sodium_Core32_Int64::fromInts($low, $high); } /** * Convert an arbitrary number into an SplFixedArray of two 32-bit integers * that represents a 64-bit integer. * * @internal You should not use this directly from another application * * @param int $num * @return ParagonIE_Sodium_Core32_Int64 * @throws SodiumException * @throws TypeError */ protected static function to64($num) { list($hi, $lo) = self::numericTo64BitInteger($num); return self::new64($hi, $lo); } /** * Adds two 64-bit integers together, returning their sum as a SplFixedArray * containing two 32-bit integers (representing a 64-bit integer). * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Int64 $x * @param ParagonIE_Sodium_Core32_Int64 $y * @return ParagonIE_Sodium_Core32_Int64 */ protected static function add64($x, $y) { return $x->addInt64($y); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Int64 $x * @param ParagonIE_Sodium_Core32_Int64 $y * @param ParagonIE_Sodium_Core32_Int64 $z * @return ParagonIE_Sodium_Core32_Int64 */ public static function add364($x, $y, $z) { return $x->addInt64($y)->addInt64($z); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Int64 $x * @param ParagonIE_Sodium_Core32_Int64 $y * @return ParagonIE_Sodium_Core32_Int64 * @throws TypeError */ public static function xor64(ParagonIE_Sodium_Core32_Int64 $x, ParagonIE_Sodium_Core32_Int64 $y) { return $x->xorInt64($y); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Int64 $x * @param int $c * @return ParagonIE_Sodium_Core32_Int64 * @throws SodiumException * @throws TypeError */ public static function rotr64(ParagonIE_Sodium_Core32_Int64 $x, $c) { return $x->rotateRight($c); } /** * @internal You should not use this directly from another application * * @param SplFixedArray $x * @param int $i * @return ParagonIE_Sodium_Core32_Int64 * @throws SodiumException * @throws TypeError */ public static function load64($x, $i) { /** @var int $l */ $l = (int) ($x[$i]) | ((int) ($x[$i+1]) << 8) | ((int) ($x[$i+2]) << 16) | ((int) ($x[$i+3]) << 24); /** @var int $h */ $h = (int) ($x[$i+4]) | ((int) ($x[$i+5]) << 8) | ((int) ($x[$i+6]) << 16) | ((int) ($x[$i+7]) << 24); return self::new64($h, $l); } /** * @internal You should not use this directly from another application * * @param SplFixedArray $x * @param int $i * @param ParagonIE_Sodium_Core32_Int64 $u * @return void * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedAssignment * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment * @psalm-suppress MixedArrayOffset */ public static function store64(SplFixedArray $x, $i, ParagonIE_Sodium_Core32_Int64 $u) { $v = clone $u; $maxLength = $x->getSize() - 1; for ($j = 0; $j < 8; ++$j) { $k = 3 - ($j >> 1); $x[$i] = $v->limbs[$k] & 0xff; if (++$i > $maxLength) { return; } $v->limbs[$k] >>= 8; } } /** * This just sets the $iv static variable. * * @internal You should not use this directly from another application * * @return void * @throws SodiumException * @throws TypeError */ public static function pseudoConstructor() { static $called = false; if ($called) { return; } self::$iv = new SplFixedArray(8); self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908); self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b); self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b); self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1); self::$iv[4] = self::new64(0x510e527f, 0xade682d1); self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f); self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b); self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179); $called = true; } /** * Returns a fresh BLAKE2 context. * * @internal You should not use this directly from another application * * @return SplFixedArray * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedAssignment * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment * @psalm-suppress MixedArrayOffset * @throws SodiumException * @throws TypeError */ protected static function context() { $ctx = new SplFixedArray(6); $ctx[0] = new SplFixedArray(8); // h $ctx[1] = new SplFixedArray(2); // t $ctx[2] = new SplFixedArray(2); // f $ctx[3] = new SplFixedArray(256); // buf $ctx[4] = 0; // buflen $ctx[5] = 0; // last_node (uint8_t) for ($i = 8; $i--;) { $ctx[0][$i] = self::$iv[$i]; } for ($i = 256; $i--;) { $ctx[3][$i] = 0; } $zero = self::new64(0, 0); $ctx[1][0] = $zero; $ctx[1][1] = $zero; $ctx[2][0] = $zero; $ctx[2][1] = $zero; return $ctx; } /** * @internal You should not use this directly from another application * * @param SplFixedArray $ctx * @param SplFixedArray $buf * @return void * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment * @psalm-suppress MixedAssignment */ protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) { $m = new SplFixedArray(16); $v = new SplFixedArray(16); for ($i = 16; $i--;) { $m[$i] = self::load64($buf, $i << 3); } for ($i = 8; $i--;) { $v[$i] = $ctx[0][$i]; } $v[ 8] = self::$iv[0]; $v[ 9] = self::$iv[1]; $v[10] = self::$iv[2]; $v[11] = self::$iv[3]; $v[12] = self::xor64($ctx[1][0], self::$iv[4]); $v[13] = self::xor64($ctx[1][1], self::$iv[5]); $v[14] = self::xor64($ctx[2][0], self::$iv[6]); $v[15] = self::xor64($ctx[2][1], self::$iv[7]); for ($r = 0; $r < 12; ++$r) { $v = self::G($r, 0, 0, 4, 8, 12, $v, $m); $v = self::G($r, 1, 1, 5, 9, 13, $v, $m); $v = self::G($r, 2, 2, 6, 10, 14, $v, $m); $v = self::G($r, 3, 3, 7, 11, 15, $v, $m); $v = self::G($r, 4, 0, 5, 10, 15, $v, $m); $v = self::G($r, 5, 1, 6, 11, 12, $v, $m); $v = self::G($r, 6, 2, 7, 8, 13, $v, $m); $v = self::G($r, 7, 3, 4, 9, 14, $v, $m); } for ($i = 8; $i--;) { $ctx[0][$i] = self::xor64( $ctx[0][$i], self::xor64($v[$i], $v[$i+8]) ); } } /** * @internal You should not use this directly from another application * * @param int $r * @param int $i * @param int $a * @param int $b * @param int $c * @param int $d * @param SplFixedArray $v * @param SplFixedArray $m * @return SplFixedArray * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedArrayOffset */ public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) { $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]); $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); $v[$c] = self::add64($v[$c], $v[$d]); $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24); $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]); $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16); $v[$c] = self::add64($v[$c], $v[$d]); $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63); return $v; } /** * @internal You should not use this directly from another application * * @param SplFixedArray $ctx * @param int $inc * @return void * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment */ public static function increment_counter($ctx, $inc) { if ($inc < 0) { throw new SodiumException('Increasing by a negative number makes no sense.'); } $t = self::to64($inc); # S->t is $ctx[1] in our implementation # S->t[0] = ( uint64_t )( t >> 0 ); $ctx[1][0] = self::add64($ctx[1][0], $t); # S->t[1] += ( S->t[0] < inc ); if (!($ctx[1][0] instanceof ParagonIE_Sodium_Core32_Int64)) { throw new TypeError('Not an int64'); } /** @var ParagonIE_Sodium_Core32_Int64 $c*/ $c = $ctx[1][0]; if ($c->isLessThanInt($inc)) { $ctx[1][1] = self::add64($ctx[1][1], self::to64(1)); } } /** * @internal You should not use this directly from another application * * @param SplFixedArray $ctx * @param SplFixedArray $p * @param int $plen * @return void * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedAssignment * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment * @psalm-suppress MixedArrayOffset * @psalm-suppress MixedMethodCall * @psalm-suppress MixedOperand */ public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) { self::pseudoConstructor(); $offset = 0; while ($plen > 0) { $left = $ctx[4]; $fill = 256 - $left; if ($plen > $fill) { # memcpy( S->buf + left, in, fill ); /* Fill buffer */ for ($i = $fill; $i--;) { $ctx[3][$i + $left] = $p[$i + $offset]; } # S->buflen += fill; $ctx[4] += $fill; # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); self::increment_counter($ctx, 128); # blake2b_compress( S, S->buf ); /* Compress */ self::compress($ctx, $ctx[3]); # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */ for ($i = 128; $i--;) { $ctx[3][$i] = $ctx[3][$i + 128]; } # S->buflen -= BLAKE2B_BLOCKBYTES; $ctx[4] -= 128; # in += fill; $offset += $fill; # inlen -= fill; $plen -= $fill; } else { for ($i = $plen; $i--;) { $ctx[3][$i + $left] = $p[$i + $offset]; } $ctx[4] += $plen; $offset += $plen; $plen -= $plen; } } } /** * @internal You should not use this directly from another application * * @param SplFixedArray $ctx * @param SplFixedArray $out * @return SplFixedArray * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedAssignment * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment * @psalm-suppress MixedArrayOffset * @psalm-suppress MixedMethodCall * @psalm-suppress MixedOperand */ public static function finish(SplFixedArray $ctx, SplFixedArray $out) { self::pseudoConstructor(); if ($ctx[4] > 128) { self::increment_counter($ctx, 128); self::compress($ctx, $ctx[3]); $ctx[4] -= 128; if ($ctx[4] > 128) { throw new SodiumException('Failed to assert that buflen <= 128 bytes'); } for ($i = $ctx[4]; $i--;) { $ctx[3][$i] = $ctx[3][$i + 128]; } } self::increment_counter($ctx, $ctx[4]); $ctx[2][0] = self::new64(0xffffffff, 0xffffffff); for ($i = 256 - $ctx[4]; $i--;) { /** @var int $i */ $ctx[3][$i + $ctx[4]] = 0; } self::compress($ctx, $ctx[3]); $i = (int) (($out->getSize() - 1) / 8); for (; $i >= 0; --$i) { self::store64($out, $i << 3, $ctx[0][$i]); } return $out; } /** * @internal You should not use this directly from another application * * @param SplFixedArray|null $key * @param int $outlen * @param SplFixedArray|null $salt * @param SplFixedArray|null $personal * @return SplFixedArray * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedAssignment * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment * @psalm-suppress MixedMethodCall */ public static function init( $key = null, $outlen = 64, $salt = null, $personal = null ) { self::pseudoConstructor(); $klen = 0; if ($key !== null) { if (count($key) > 64) { throw new SodiumException('Invalid key size'); } $klen = count($key); } if ($outlen > 64) { throw new SodiumException('Invalid output size'); } $ctx = self::context(); $p = new SplFixedArray(64); // Zero our param buffer... for ($i = 64; --$i;) { $p[$i] = 0; } $p[0] = $outlen; // digest_length $p[1] = $klen; // key_length $p[2] = 1; // fanout $p[3] = 1; // depth if ($salt instanceof SplFixedArray) { // salt: [32] through [47] for ($i = 0; $i < 16; ++$i) { $p[32 + $i] = (int) $salt[$i]; } } if ($personal instanceof SplFixedArray) { // personal: [48] through [63] for ($i = 0; $i < 16; ++$i) { $p[48 + $i] = (int) $personal[$i]; } } $ctx[0][0] = self::xor64( $ctx[0][0], self::load64($p, 0) ); if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) { // We need to do what blake2b_init_param() does: for ($i = 1; $i < 8; ++$i) { $ctx[0][$i] = self::xor64( $ctx[0][$i], self::load64($p, $i << 3) ); } } if ($klen > 0 && $key instanceof SplFixedArray) { $block = new SplFixedArray(128); for ($i = 128; $i--;) { $block[$i] = 0; } for ($i = $klen; $i--;) { $block[$i] = $key[$i]; } self::update($ctx, $block, 128); $ctx[4] = 128; } return $ctx; } /** * Convert a string into an SplFixedArray of integers * * @internal You should not use this directly from another application * * @param string $str * @return SplFixedArray * @psalm-suppress MixedArgumentTypeCoercion */ public static function stringToSplFixedArray($str = '') { $values = unpack('C*', $str); return SplFixedArray::fromArray(array_values($values)); } /** * Convert an SplFixedArray of integers into a string * * @internal You should not use this directly from another application * * @param SplFixedArray $a * @return string */ public static function SplFixedArrayToString(SplFixedArray $a) { /** * @var array<int, string|int> */ $arr = $a->toArray(); $c = $a->count(); array_unshift($arr, str_repeat('C', $c)); return (string) (call_user_func_array('pack', $arr)); } /** * @internal You should not use this directly from another application * * @param SplFixedArray $ctx * @return string * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment * @psalm-suppress MixedMethodCall */ public static function contextToString(SplFixedArray $ctx) { $str = ''; /** @var array<int, ParagonIE_Sodium_Core32_Int64> $ctxA */ $ctxA = $ctx[0]->toArray(); # uint64_t h[8]; for ($i = 0; $i < 8; ++$i) { if (!($ctxA[$i] instanceof ParagonIE_Sodium_Core32_Int64)) { throw new TypeError('Not an instance of Int64'); } /** @var ParagonIE_Sodium_Core32_Int64 $ctxAi */ $ctxAi = $ctxA[$i]; $str .= $ctxAi->toReverseString(); } # uint64_t t[2]; # uint64_t f[2]; for ($i = 1; $i < 3; ++$i) { /** @var array<int, ParagonIE_Sodium_Core32_Int64> $ctxA */ $ctxA = $ctx[$i]->toArray(); /** @var ParagonIE_Sodium_Core32_Int64 $ctxA1 */ $ctxA1 = $ctxA[0]; /** @var ParagonIE_Sodium_Core32_Int64 $ctxA2 */ $ctxA2 = $ctxA[1]; $str .= $ctxA1->toReverseString(); $str .= $ctxA2->toReverseString(); } # uint8_t buf[2 * 128]; $str .= self::SplFixedArrayToString($ctx[3]); /** @var int $ctx4 */ $ctx4 = $ctx[4]; # size_t buflen; $str .= implode('', array( self::intToChr($ctx4 & 0xff), self::intToChr(($ctx4 >> 8) & 0xff), self::intToChr(($ctx4 >> 16) & 0xff), self::intToChr(($ctx4 >> 24) & 0xff), "\x00\x00\x00\x00" /* self::intToChr(($ctx4 >> 32) & 0xff), self::intToChr(($ctx4 >> 40) & 0xff), self::intToChr(($ctx4 >> 48) & 0xff), self::intToChr(($ctx4 >> 56) & 0xff) */ )); # uint8_t last_node; return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23); } /** * Creates an SplFixedArray containing other SplFixedArray elements, from * a string (compatible with \Sodium\crypto_generichash_{init, update, final}) * * @internal You should not use this directly from another application * * @param string $string * @return SplFixedArray * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment */ public static function stringToContext($string) { $ctx = self::context(); # uint64_t h[8]; for ($i = 0; $i < 8; ++$i) { $ctx[0][$i] = ParagonIE_Sodium_Core32_Int64::fromReverseString( self::substr($string, (($i << 3) + 0), 8) ); } # uint64_t t[2]; # uint64_t f[2]; for ($i = 1; $i < 3; ++$i) { $ctx[$i][1] = ParagonIE_Sodium_Core32_Int64::fromReverseString( self::substr($string, 72 + (($i - 1) << 4), 8) ); $ctx[$i][0] = ParagonIE_Sodium_Core32_Int64::fromReverseString( self::substr($string, 64 + (($i - 1) << 4), 8) ); } # uint8_t buf[2 * 128]; $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256)); # uint8_t buf[2 * 128]; $int = 0; for ($i = 0; $i < 8; ++$i) { $int |= self::chrToInt($string[352 + $i]) << ($i << 3); } $ctx[4] = $int; return $ctx; } } Core32/SipHash.php 0000644 00000014725 15162213661 0007665 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_SipHash', false)) { return; } /** * Class ParagonIE_SodiumCompat_Core32_SipHash * * Only uses 32-bit arithmetic, while the original SipHash used 64-bit integers */ class ParagonIE_Sodium_Core32_SipHash extends ParagonIE_Sodium_Core32_Util { /** * @internal You should not use this directly from another application * * @param array<int, ParagonIE_Sodium_Core32_Int64> $v * @return array<int, ParagonIE_Sodium_Core32_Int64> */ public static function sipRound(array $v) { # v0 += v1; $v[0] = $v[0]->addInt64($v[1]); # v1 = ROTL(v1, 13); $v[1] = $v[1]->rotateLeft(13); # v1 ^= v0; $v[1] = $v[1]->xorInt64($v[0]); # v0=ROTL(v0,32); $v[0] = $v[0]->rotateLeft(32); # v2 += v3; $v[2] = $v[2]->addInt64($v[3]); # v3=ROTL(v3,16); $v[3] = $v[3]->rotateLeft(16); # v3 ^= v2; $v[3] = $v[3]->xorInt64($v[2]); # v0 += v3; $v[0] = $v[0]->addInt64($v[3]); # v3=ROTL(v3,21); $v[3] = $v[3]->rotateLeft(21); # v3 ^= v0; $v[3] = $v[3]->xorInt64($v[0]); # v2 += v1; $v[2] = $v[2]->addInt64($v[1]); # v1=ROTL(v1,17); $v[1] = $v[1]->rotateLeft(17); # v1 ^= v2; $v[1] = $v[1]->xorInt64($v[2]); # v2=ROTL(v2,32) $v[2] = $v[2]->rotateLeft(32); return $v; } /** * @internal You should not use this directly from another application * * @param string $in * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function sipHash24($in, $key) { $inlen = self::strlen($in); # /* "somepseudorandomlygeneratedbytes" */ # u64 v0 = 0x736f6d6570736575ULL; # u64 v1 = 0x646f72616e646f6dULL; # u64 v2 = 0x6c7967656e657261ULL; # u64 v3 = 0x7465646279746573ULL; $v = array( new ParagonIE_Sodium_Core32_Int64( array(0x736f, 0x6d65, 0x7073, 0x6575) ), new ParagonIE_Sodium_Core32_Int64( array(0x646f, 0x7261, 0x6e64, 0x6f6d) ), new ParagonIE_Sodium_Core32_Int64( array(0x6c79, 0x6765, 0x6e65, 0x7261) ), new ParagonIE_Sodium_Core32_Int64( array(0x7465, 0x6462, 0x7974, 0x6573) ) ); # u64 k0 = LOAD64_LE( k ); # u64 k1 = LOAD64_LE( k + 8 ); $k = array( ParagonIE_Sodium_Core32_Int64::fromReverseString( self::substr($key, 0, 8) ), ParagonIE_Sodium_Core32_Int64::fromReverseString( self::substr($key, 8, 8) ) ); # b = ( ( u64 )inlen ) << 56; $b = new ParagonIE_Sodium_Core32_Int64( array(($inlen << 8) & 0xffff, 0, 0, 0) ); # v3 ^= k1; $v[3] = $v[3]->xorInt64($k[1]); # v2 ^= k0; $v[2] = $v[2]->xorInt64($k[0]); # v1 ^= k1; $v[1] = $v[1]->xorInt64($k[1]); # v0 ^= k0; $v[0] = $v[0]->xorInt64($k[0]); $left = $inlen; # for ( ; in != end; in += 8 ) while ($left >= 8) { # m = LOAD64_LE( in ); $m = ParagonIE_Sodium_Core32_Int64::fromReverseString( self::substr($in, 0, 8) ); # v3 ^= m; $v[3] = $v[3]->xorInt64($m); # SIPROUND; # SIPROUND; $v = self::sipRound($v); $v = self::sipRound($v); # v0 ^= m; $v[0] = $v[0]->xorInt64($m); $in = self::substr($in, 8); $left -= 8; } # switch( left ) # { # case 7: b |= ( ( u64 )in[ 6] ) << 48; # case 6: b |= ( ( u64 )in[ 5] ) << 40; # case 5: b |= ( ( u64 )in[ 4] ) << 32; # case 4: b |= ( ( u64 )in[ 3] ) << 24; # case 3: b |= ( ( u64 )in[ 2] ) << 16; # case 2: b |= ( ( u64 )in[ 1] ) << 8; # case 1: b |= ( ( u64 )in[ 0] ); break; # case 0: break; # } switch ($left) { case 7: $b = $b->orInt64( ParagonIE_Sodium_Core32_Int64::fromInts( 0, self::chrToInt($in[6]) << 16 ) ); case 6: $b = $b->orInt64( ParagonIE_Sodium_Core32_Int64::fromInts( 0, self::chrToInt($in[5]) << 8 ) ); case 5: $b = $b->orInt64( ParagonIE_Sodium_Core32_Int64::fromInts( 0, self::chrToInt($in[4]) ) ); case 4: $b = $b->orInt64( ParagonIE_Sodium_Core32_Int64::fromInts( self::chrToInt($in[3]) << 24, 0 ) ); case 3: $b = $b->orInt64( ParagonIE_Sodium_Core32_Int64::fromInts( self::chrToInt($in[2]) << 16, 0 ) ); case 2: $b = $b->orInt64( ParagonIE_Sodium_Core32_Int64::fromInts( self::chrToInt($in[1]) << 8, 0 ) ); case 1: $b = $b->orInt64( ParagonIE_Sodium_Core32_Int64::fromInts( self::chrToInt($in[0]), 0 ) ); case 0: break; } # v3 ^= b; $v[3] = $v[3]->xorInt64($b); # SIPROUND; # SIPROUND; $v = self::sipRound($v); $v = self::sipRound($v); # v0 ^= b; $v[0] = $v[0]->xorInt64($b); // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation # v2 ^= 0xff; $v[2]->limbs[3] ^= 0xff; # SIPROUND; # SIPROUND; # SIPROUND; # SIPROUND; $v = self::sipRound($v); $v = self::sipRound($v); $v = self::sipRound($v); $v = self::sipRound($v); # b = v0 ^ v1 ^ v2 ^ v3; # STORE64_LE( out, b ); return $v[0] ->xorInt64($v[1]) ->xorInt64($v[2]) ->xorInt64($v[3]) ->toReverseString(); } } Core32/error_log 0000644 00000104337 15162213662 0007532 0 ustar 00 [24-Mar-2026 21:58:01 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php on line 12 [24-Mar-2026 21:58:06 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php on line 10 [24-Mar-2026 21:58:57 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [24-Mar-2026 21:59:01 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php on line 10 [24-Mar-2026 21:59:12 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php on line 10 [24-Mar-2026 22:00:19 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [24-Mar-2026 22:00:42 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php on line 10 [24-Mar-2026 22:03:38 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php on line 12 [24-Mar-2026 22:04:04 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php on line 10 [24-Mar-2026 22:04:45 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php on line 10 [24-Mar-2026 22:04:45 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php on line 10 [24-Mar-2026 22:05:01 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php on line 10 [24-Mar-2026 22:05:25 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php on line 10 [25-Mar-2026 02:03:47 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [25-Mar-2026 02:04:01 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php on line 10 [25-Mar-2026 02:04:43 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php on line 10 [25-Mar-2026 02:06:52 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [25-Mar-2026 02:07:49 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php on line 10 [25-Mar-2026 02:08:51 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php on line 10 [25-Mar-2026 02:13:03 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php on line 10 [25-Mar-2026 02:14:04 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php on line 10 [25-Mar-2026 02:15:09 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php on line 10 [25-Mar-2026 02:16:11 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php on line 10 [25-Mar-2026 02:29:53 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php on line 12 [25-Mar-2026 03:47:03 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php on line 10 [25-Mar-2026 03:47:21 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php on line 12 [25-Mar-2026 12:49:29 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [25-Mar-2026 12:49:36 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php on line 10 [25-Mar-2026 12:49:58 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php on line 10 [25-Mar-2026 12:51:02 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [25-Mar-2026 12:52:07 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php on line 10 [25-Mar-2026 12:53:11 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php on line 10 [25-Mar-2026 12:54:17 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php on line 10 [25-Mar-2026 12:55:22 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php on line 10 [25-Mar-2026 12:56:25 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php on line 10 [25-Mar-2026 12:57:30 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php on line 10 [25-Mar-2026 12:58:35 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php on line 12 [25-Mar-2026 12:59:39 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php on line 10 [25-Mar-2026 13:00:41 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php on line 12 [25-Mar-2026 13:10:45 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [25-Mar-2026 13:11:21 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php on line 10 [25-Mar-2026 13:12:25 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php on line 10 [25-Mar-2026 13:13:29 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php on line 10 [26-Mar-2026 07:30:51 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php on line 12 [26-Mar-2026 07:30:52 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php on line 10 [26-Mar-2026 07:30:54 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [26-Mar-2026 07:30:56 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [26-Mar-2026 07:30:57 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php on line 10 [26-Mar-2026 07:30:59 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php on line 10 [26-Mar-2026 07:31:04 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php on line 10 [26-Mar-2026 07:31:05 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php on line 10 [26-Mar-2026 07:31:07 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php on line 12 [26-Mar-2026 07:31:08 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php on line 10 [26-Mar-2026 07:31:10 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php on line 10 [26-Mar-2026 07:31:12 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php on line 10 [26-Mar-2026 07:31:13 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php on line 10 [26-Mar-2026 09:07:58 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [26-Mar-2026 09:08:03 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php on line 10 [26-Mar-2026 09:08:39 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php on line 10 [26-Mar-2026 09:09:35 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php on line 10 [26-Mar-2026 09:10:06 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php on line 10 [26-Mar-2026 09:10:16 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php on line 10 [26-Mar-2026 09:10:42 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php on line 10 [26-Mar-2026 09:11:04 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php on line 10 [26-Mar-2026 09:12:11 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [26-Mar-2026 10:43:50 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php on line 10 [26-Mar-2026 10:45:08 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php on line 12 [26-Mar-2026 10:48:03 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php on line 12 [26-Mar-2026 10:50:19 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php on line 10 [26-Mar-2026 13:02:50 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [26-Mar-2026 13:03:38 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php on line 10 [26-Mar-2026 13:04:43 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php on line 10 [26-Mar-2026 13:06:48 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [26-Mar-2026 13:07:42 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php on line 10 [26-Mar-2026 13:11:58 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php on line 10 [26-Mar-2026 13:13:01 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php on line 10 [26-Mar-2026 13:14:06 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php on line 10 [26-Mar-2026 13:15:10 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php on line 10 [26-Mar-2026 13:16:17 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php on line 10 [26-Mar-2026 13:31:39 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php on line 12 [26-Mar-2026 14:46:55 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php on line 10 [26-Mar-2026 14:47:12 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php on line 12 [26-Mar-2026 23:18:32 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [26-Mar-2026 23:19:13 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php on line 10 [26-Mar-2026 23:20:14 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php on line 10 [26-Mar-2026 23:21:16 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [26-Mar-2026 23:22:21 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php on line 10 [26-Mar-2026 23:23:27 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php on line 10 [26-Mar-2026 23:24:35 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php on line 10 [26-Mar-2026 23:25:34 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php on line 10 [26-Mar-2026 23:26:35 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php on line 10 [26-Mar-2026 23:27:42 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php on line 10 [26-Mar-2026 23:28:43 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php on line 12 [26-Mar-2026 23:29:46 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php on line 10 [26-Mar-2026 23:30:51 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php on line 12 [28-Mar-2026 09:19:09 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [28-Mar-2026 09:19:14 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HSalsa20.php on line 10 [28-Mar-2026 09:20:39 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519.php on line 16 [28-Mar-2026 09:21:05 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/HChaCha20.php on line 10 [28-Mar-2026 09:24:19 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/SipHash.php on line 12 [28-Mar-2026 09:31:22 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Util.php on line 10 [28-Mar-2026 09:32:54 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Salsa20.php on line 10 [28-Mar-2026 09:33:06 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305.php on line 10 [28-Mar-2026 09:34:05 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20.php on line 10 [28-Mar-2026 09:34:30 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XSalsa20.php on line 10 [28-Mar-2026 09:34:49 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/BLAKE2b.php on line 12 [28-Mar-2026 09:35:04 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/XChaCha20.php on line 10 [28-Mar-2026 10:12:50 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/X25519.php on line 10 Core32/XChaCha20.php 0000644 00000004626 15162213663 0007730 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_XChaCha20', false)) { return; } /** * Class ParagonIE_Sodium_Core32_XChaCha20 */ class ParagonIE_Sodium_Core32_XChaCha20 extends ParagonIE_Sodium_Core32_HChaCha20 { /** * @internal You should not use this directly from another application * * @param int $len * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function stream($len = 64, $nonce = '', $key = '') { if (self::strlen($nonce) !== 24) { throw new SodiumException('Nonce must be 24 bytes long'); } return self::encryptBytes( new ParagonIE_Sodium_Core32_ChaCha20_Ctx( self::hChaCha20( self::substr($nonce, 0, 16), $key ), self::substr($nonce, 16, 8) ), str_repeat("\x00", $len) ); } /** * @internal You should not use this directly from another application * * @param string $message * @param string $nonce * @param string $key * @param string $ic * @return string * @throws SodiumException * @throws TypeError */ public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') { if (self::strlen($nonce) !== 24) { throw new SodiumException('Nonce must be 24 bytes long'); } return self::encryptBytes( new ParagonIE_Sodium_Core32_ChaCha20_Ctx( self::hChaCha20(self::substr($nonce, 0, 16), $key), self::substr($nonce, 16, 8), $ic ), $message ); } /** * @internal You should not use this directly from another application * * @param string $message * @param string $nonce * @param string $key * @param string $ic * @return string * @throws SodiumException * @throws TypeError */ public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') { return self::encryptBytes( new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx( self::hChaCha20(self::substr($nonce, 0, 16), $key), "\x00\x00\x00\x00" . self::substr($nonce, 16, 8), $ic ), $message ); } } Core32/Int32.php 0000644 00000060004 15162213663 0007216 0 ustar 00 <?php /** * Class ParagonIE_Sodium_Core32_Int32 * * Encapsulates a 32-bit integer. * * These are immutable. It always returns a new instance. */ class ParagonIE_Sodium_Core32_Int32 { /** * @var array<int, int> - two 16-bit integers * * 0 is the higher 16 bits * 1 is the lower 16 bits */ public $limbs = array(0, 0); /** * @var int */ public $overflow = 0; /** * @var bool */ public $unsignedInt = false; /** * ParagonIE_Sodium_Core32_Int32 constructor. * @param array $array * @param bool $unsignedInt */ public function __construct($array = array(0, 0), $unsignedInt = false) { $this->limbs = array( (int) $array[0], (int) $array[1] ); $this->overflow = 0; $this->unsignedInt = $unsignedInt; } /** * Adds two int32 objects * * @param ParagonIE_Sodium_Core32_Int32 $addend * @return ParagonIE_Sodium_Core32_Int32 */ public function addInt32(ParagonIE_Sodium_Core32_Int32 $addend) { $i0 = $this->limbs[0]; $i1 = $this->limbs[1]; $j0 = $addend->limbs[0]; $j1 = $addend->limbs[1]; $r1 = $i1 + ($j1 & 0xffff); $carry = $r1 >> 16; $r0 = $i0 + ($j0 & 0xffff) + $carry; $carry = $r0 >> 16; $r0 &= 0xffff; $r1 &= 0xffff; $return = new ParagonIE_Sodium_Core32_Int32( array($r0, $r1) ); $return->overflow = $carry; $return->unsignedInt = $this->unsignedInt; return $return; } /** * Adds a normal integer to an int32 object * * @param int $int * @return ParagonIE_Sodium_Core32_Int32 * @throws SodiumException * @throws TypeError */ public function addInt($int) { ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); /** @var int $int */ $int = (int) $int; $int = (int) $int; $i0 = $this->limbs[0]; $i1 = $this->limbs[1]; $r1 = $i1 + ($int & 0xffff); $carry = $r1 >> 16; $r0 = $i0 + (($int >> 16) & 0xffff) + $carry; $carry = $r0 >> 16; $r0 &= 0xffff; $r1 &= 0xffff; $return = new ParagonIE_Sodium_Core32_Int32( array($r0, $r1) ); $return->overflow = $carry; $return->unsignedInt = $this->unsignedInt; return $return; } /** * @param int $b * @return int */ public function compareInt($b = 0) { $gt = 0; $eq = 1; $i = 2; $j = 0; while ($i > 0) { --$i; /** @var int $x1 */ $x1 = $this->limbs[$i]; /** @var int $x2 */ $x2 = ($b >> ($j << 4)) & 0xffff; /** @var int $gt */ $gt |= (($x2 - $x1) >> 8) & $eq; /** @var int $eq */ $eq &= (($x2 ^ $x1) - 1) >> 8; } return ($gt + $gt - $eq) + 1; } /** * @param int $m * @return ParagonIE_Sodium_Core32_Int32 */ public function mask($m = 0) { /** @var int $hi */ $hi = ((int) $m >> 16); $hi &= 0xffff; /** @var int $lo */ $lo = ((int) $m) & 0xffff; return new ParagonIE_Sodium_Core32_Int32( array( (int) ($this->limbs[0] & $hi), (int) ($this->limbs[1] & $lo) ), $this->unsignedInt ); } /** * @param array<int, int> $a * @param array<int, int> $b * @param int $baseLog2 * @return array<int, int> */ public function multiplyLong(array $a, array $b, $baseLog2 = 16) { $a_l = count($a); $b_l = count($b); /** @var array<int, int> $r */ $r = array_fill(0, $a_l + $b_l + 1, 0); $base = 1 << $baseLog2; for ($i = 0; $i < $a_l; ++$i) { $a_i = $a[$i]; for ($j = 0; $j < $a_l; ++$j) { $b_j = $b[$j]; $product = ($a_i * $b_j) + $r[$i + $j]; $carry = ((int) $product >> $baseLog2 & 0xffff); $r[$i + $j] = ((int) $product - (int) ($carry * $base)) & 0xffff; $r[$i + $j + 1] += $carry; } } return array_slice($r, 0, 5); } /** * @param int $int * @return ParagonIE_Sodium_Core32_Int32 */ public function mulIntFast($int) { // Handle negative numbers $aNeg = ($this->limbs[0] >> 15) & 1; $bNeg = ($int >> 31) & 1; $a = array_reverse($this->limbs); $b = array( $int & 0xffff, ($int >> 16) & 0xffff ); if ($aNeg) { for ($i = 0; $i < 2; ++$i) { $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; } ++$a[0]; } if ($bNeg) { for ($i = 0; $i < 2; ++$i) { $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; } ++$b[0]; } // Multiply $res = $this->multiplyLong($a, $b); // Re-apply negation to results if ($aNeg !== $bNeg) { for ($i = 0; $i < 2; ++$i) { $res[$i] = (0xffff ^ $res[$i]) & 0xffff; } // Handle integer overflow $c = 1; for ($i = 0; $i < 2; ++$i) { $res[$i] += $c; $c = $res[$i] >> 16; $res[$i] &= 0xffff; } } // Return our values $return = new ParagonIE_Sodium_Core32_Int32(); $return->limbs = array( $res[1] & 0xffff, $res[0] & 0xffff ); if (count($res) > 2) { $return->overflow = $res[2] & 0xffff; } $return->unsignedInt = $this->unsignedInt; return $return; } /** * @param ParagonIE_Sodium_Core32_Int32 $right * @return ParagonIE_Sodium_Core32_Int32 */ public function mulInt32Fast(ParagonIE_Sodium_Core32_Int32 $right) { $aNeg = ($this->limbs[0] >> 15) & 1; $bNeg = ($right->limbs[0] >> 15) & 1; $a = array_reverse($this->limbs); $b = array_reverse($right->limbs); if ($aNeg) { for ($i = 0; $i < 2; ++$i) { $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; } ++$a[0]; } if ($bNeg) { for ($i = 0; $i < 2; ++$i) { $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; } ++$b[0]; } $res = $this->multiplyLong($a, $b); if ($aNeg !== $bNeg) { if ($aNeg !== $bNeg) { for ($i = 0; $i < 2; ++$i) { $res[$i] = ($res[$i] ^ 0xffff) & 0xffff; } $c = 1; for ($i = 0; $i < 2; ++$i) { $res[$i] += $c; $c = $res[$i] >> 16; $res[$i] &= 0xffff; } } } $return = new ParagonIE_Sodium_Core32_Int32(); $return->limbs = array( $res[1] & 0xffff, $res[0] & 0xffff ); if (count($res) > 2) { $return->overflow = $res[2]; } return $return; } /** * @param int $int * @param int $size * @return ParagonIE_Sodium_Core32_Int32 * @throws SodiumException * @throws TypeError */ public function mulInt($int = 0, $size = 0) { ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); if (ParagonIE_Sodium_Compat::$fastMult) { return $this->mulIntFast((int) $int); } /** @var int $int */ $int = (int) $int; /** @var int $size */ $size = (int) $size; if (!$size) { $size = 31; } /** @var int $size */ $a = clone $this; $return = new ParagonIE_Sodium_Core32_Int32(); $return->unsignedInt = $this->unsignedInt; // Initialize: $ret0 = 0; $ret1 = 0; $a0 = $a->limbs[0]; $a1 = $a->limbs[1]; /** @var int $size */ /** @var int $i */ for ($i = $size; $i >= 0; --$i) { $m = (int) (-($int & 1)); $x0 = $a0 & $m; $x1 = $a1 & $m; $ret1 += $x1; $c = $ret1 >> 16; $ret0 += $x0 + $c; $ret0 &= 0xffff; $ret1 &= 0xffff; $a1 = ($a1 << 1); $x1 = $a1 >> 16; $a0 = ($a0 << 1) | $x1; $a0 &= 0xffff; $a1 &= 0xffff; $int >>= 1; } $return->limbs[0] = $ret0; $return->limbs[1] = $ret1; return $return; } /** * @param ParagonIE_Sodium_Core32_Int32 $int * @param int $size * @return ParagonIE_Sodium_Core32_Int32 * @throws SodiumException * @throws TypeError */ public function mulInt32(ParagonIE_Sodium_Core32_Int32 $int, $size = 0) { ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); if (ParagonIE_Sodium_Compat::$fastMult) { return $this->mulInt32Fast($int); } if (!$size) { $size = 31; } /** @var int $size */ $a = clone $this; $b = clone $int; $return = new ParagonIE_Sodium_Core32_Int32(); $return->unsignedInt = $this->unsignedInt; // Initialize: $ret0 = 0; $ret1 = 0; $a0 = $a->limbs[0]; $a1 = $a->limbs[1]; $b0 = $b->limbs[0]; $b1 = $b->limbs[1]; /** @var int $size */ /** @var int $i */ for ($i = $size; $i >= 0; --$i) { $m = (int) (-($b1 & 1)); $x0 = $a0 & $m; $x1 = $a1 & $m; $ret1 += $x1; $c = $ret1 >> 16; $ret0 += $x0 + $c; $ret0 &= 0xffff; $ret1 &= 0xffff; $a1 = ($a1 << 1); $x1 = $a1 >> 16; $a0 = ($a0 << 1) | $x1; $a0 &= 0xffff; $a1 &= 0xffff; $x0 = ($b0 & 1) << 16; $b0 = ($b0 >> 1); $b1 = (($b1 | $x0) >> 1); $b0 &= 0xffff; $b1 &= 0xffff; } $return->limbs[0] = $ret0; $return->limbs[1] = $ret1; return $return; } /** * OR this 32-bit integer with another. * * @param ParagonIE_Sodium_Core32_Int32 $b * @return ParagonIE_Sodium_Core32_Int32 */ public function orInt32(ParagonIE_Sodium_Core32_Int32 $b) { $return = new ParagonIE_Sodium_Core32_Int32(); $return->unsignedInt = $this->unsignedInt; $return->limbs = array( (int) ($this->limbs[0] | $b->limbs[0]), (int) ($this->limbs[1] | $b->limbs[1]) ); /** @var int overflow */ $return->overflow = $this->overflow | $b->overflow; return $return; } /** * @param int $b * @return bool */ public function isGreaterThan($b = 0) { return $this->compareInt($b) > 0; } /** * @param int $b * @return bool */ public function isLessThanInt($b = 0) { return $this->compareInt($b) < 0; } /** * @param int $c * @return ParagonIE_Sodium_Core32_Int32 * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArrayAccess */ public function rotateLeft($c = 0) { ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); /** @var int $c */ $c = (int) $c; $return = new ParagonIE_Sodium_Core32_Int32(); $return->unsignedInt = $this->unsignedInt; $c &= 31; if ($c === 0) { // NOP, but we want a copy. $return->limbs = $this->limbs; } else { /** @var int $c */ /** @var int $idx_shift */ $idx_shift = ($c >> 4) & 1; /** @var int $sub_shift */ $sub_shift = $c & 15; /** @var array<int, int> $limbs */ $limbs =& $return->limbs; /** @var array<int, int> $myLimbs */ $myLimbs =& $this->limbs; for ($i = 1; $i >= 0; --$i) { /** @var int $j */ $j = ($i + $idx_shift) & 1; /** @var int $k */ $k = ($i + $idx_shift + 1) & 1; $limbs[$i] = (int) ( ( ((int) ($myLimbs[$j]) << $sub_shift) | ((int) ($myLimbs[$k]) >> (16 - $sub_shift)) ) & 0xffff ); } } return $return; } /** * Rotate to the right * * @param int $c * @return ParagonIE_Sodium_Core32_Int32 * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArrayAccess */ public function rotateRight($c = 0) { ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); /** @var int $c */ $c = (int) $c; $return = new ParagonIE_Sodium_Core32_Int32(); $return->unsignedInt = $this->unsignedInt; $c &= 31; /** @var int $c */ if ($c === 0) { // NOP, but we want a copy. $return->limbs = $this->limbs; } else { /** @var int $c */ /** @var int $idx_shift */ $idx_shift = ($c >> 4) & 1; /** @var int $sub_shift */ $sub_shift = $c & 15; /** @var array<int, int> $limbs */ $limbs =& $return->limbs; /** @var array<int, int> $myLimbs */ $myLimbs =& $this->limbs; for ($i = 1; $i >= 0; --$i) { /** @var int $j */ $j = ($i - $idx_shift) & 1; /** @var int $k */ $k = ($i - $idx_shift - 1) & 1; $limbs[$i] = (int) ( ( ((int) ($myLimbs[$j]) >> (int) ($sub_shift)) | ((int) ($myLimbs[$k]) << (16 - (int) ($sub_shift))) ) & 0xffff ); } } return $return; } /** * @param bool $bool * @return self */ public function setUnsignedInt($bool = false) { $this->unsignedInt = !empty($bool); return $this; } /** * @param int $c * @return ParagonIE_Sodium_Core32_Int32 * @throws SodiumException * @throws TypeError */ public function shiftLeft($c = 0) { ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); /** @var int $c */ $c = (int) $c; $return = new ParagonIE_Sodium_Core32_Int32(); $return->unsignedInt = $this->unsignedInt; $c &= 63; /** @var int $c */ if ($c === 0) { $return->limbs = $this->limbs; } elseif ($c < 0) { /** @var int $c */ return $this->shiftRight(-$c); } else { /** @var int $c */ /** @var int $tmp */ $tmp = $this->limbs[1] << $c; $return->limbs[1] = (int)($tmp & 0xffff); /** @var int $carry */ $carry = $tmp >> 16; /** @var int $tmp */ $tmp = ($this->limbs[0] << $c) | ($carry & 0xffff); $return->limbs[0] = (int) ($tmp & 0xffff); } return $return; } /** * @param int $c * @return ParagonIE_Sodium_Core32_Int32 * @throws SodiumException * @throws TypeError * @psalm-suppress MixedAssignment * @psalm-suppress MixedOperand */ public function shiftRight($c = 0) { ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); /** @var int $c */ $c = (int) $c; $return = new ParagonIE_Sodium_Core32_Int32(); $return->unsignedInt = $this->unsignedInt; $c &= 63; /** @var int $c */ if ($c >= 16) { $return->limbs = array( (int) ($this->overflow & 0xffff), (int) ($this->limbs[0]) ); $return->overflow = $this->overflow >> 16; return $return->shiftRight($c & 15); } if ($c === 0) { $return->limbs = $this->limbs; } elseif ($c < 0) { /** @var int $c */ return $this->shiftLeft(-$c); } else { if (!is_int($c)) { throw new TypeError(); } /** @var int $c */ // $return->limbs[0] = (int) (($this->limbs[0] >> $c) & 0xffff); $carryLeft = (int) ($this->overflow & ((1 << ($c + 1)) - 1)); $return->limbs[0] = (int) ((($this->limbs[0] >> $c) | ($carryLeft << (16 - $c))) & 0xffff); $carryRight = (int) ($this->limbs[0] & ((1 << ($c + 1)) - 1)); $return->limbs[1] = (int) ((($this->limbs[1] >> $c) | ($carryRight << (16 - $c))) & 0xffff); $return->overflow >>= $c; } return $return; } /** * Subtract a normal integer from an int32 object. * * @param int $int * @return ParagonIE_Sodium_Core32_Int32 * @throws SodiumException * @throws TypeError */ public function subInt($int) { ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); /** @var int $int */ $int = (int) $int; $return = new ParagonIE_Sodium_Core32_Int32(); $return->unsignedInt = $this->unsignedInt; /** @var int $tmp */ $tmp = $this->limbs[1] - ($int & 0xffff); /** @var int $carry */ $carry = $tmp >> 16; $return->limbs[1] = (int) ($tmp & 0xffff); /** @var int $tmp */ $tmp = $this->limbs[0] - (($int >> 16) & 0xffff) + $carry; $return->limbs[0] = (int) ($tmp & 0xffff); return $return; } /** * Subtract two int32 objects from each other * * @param ParagonIE_Sodium_Core32_Int32 $b * @return ParagonIE_Sodium_Core32_Int32 */ public function subInt32(ParagonIE_Sodium_Core32_Int32 $b) { $return = new ParagonIE_Sodium_Core32_Int32(); $return->unsignedInt = $this->unsignedInt; /** @var int $tmp */ $tmp = $this->limbs[1] - ($b->limbs[1] & 0xffff); /** @var int $carry */ $carry = $tmp >> 16; $return->limbs[1] = (int) ($tmp & 0xffff); /** @var int $tmp */ $tmp = $this->limbs[0] - ($b->limbs[0] & 0xffff) + $carry; $return->limbs[0] = (int) ($tmp & 0xffff); return $return; } /** * XOR this 32-bit integer with another. * * @param ParagonIE_Sodium_Core32_Int32 $b * @return ParagonIE_Sodium_Core32_Int32 */ public function xorInt32(ParagonIE_Sodium_Core32_Int32 $b) { $return = new ParagonIE_Sodium_Core32_Int32(); $return->unsignedInt = $this->unsignedInt; $return->limbs = array( (int) ($this->limbs[0] ^ $b->limbs[0]), (int) ($this->limbs[1] ^ $b->limbs[1]) ); return $return; } /** * @param int $signed * @return self * @throws SodiumException * @throws TypeError */ public static function fromInt($signed) { ParagonIE_Sodium_Core32_Util::declareScalarType($signed, 'int', 1);; /** @var int $signed */ $signed = (int) $signed; return new ParagonIE_Sodium_Core32_Int32( array( (int) (($signed >> 16) & 0xffff), (int) ($signed & 0xffff) ) ); } /** * @param string $string * @return self * @throws SodiumException * @throws TypeError */ public static function fromString($string) { ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); $string = (string) $string; if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) { throw new RangeException( 'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' ); } $return = new ParagonIE_Sodium_Core32_Int32(); $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff) << 8); $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff); $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff) << 8); $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff); return $return; } /** * @param string $string * @return self * @throws SodiumException * @throws TypeError */ public static function fromReverseString($string) { ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); $string = (string) $string; if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) { throw new RangeException( 'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' ); } $return = new ParagonIE_Sodium_Core32_Int32(); $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff) << 8); $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff); $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff) << 8); $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff); return $return; } /** * @return array<int, int> */ public function toArray() { return array((int) ($this->limbs[0] << 16 | $this->limbs[1])); } /** * @return string * @throws TypeError */ public function toString() { return ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff); } /** * @return int */ public function toInt() { return (int) ( (($this->limbs[0] & 0xffff) << 16) | ($this->limbs[1] & 0xffff) ); } /** * @return ParagonIE_Sodium_Core32_Int32 */ public function toInt32() { $return = new ParagonIE_Sodium_Core32_Int32(); $return->limbs[0] = (int) ($this->limbs[0] & 0xffff); $return->limbs[1] = (int) ($this->limbs[1] & 0xffff); $return->unsignedInt = $this->unsignedInt; $return->overflow = (int) ($this->overflow & 0x7fffffff); return $return; } /** * @return ParagonIE_Sodium_Core32_Int64 */ public function toInt64() { $return = new ParagonIE_Sodium_Core32_Int64(); $return->unsignedInt = $this->unsignedInt; if ($this->unsignedInt) { $return->limbs[0] += (($this->overflow >> 16) & 0xffff); $return->limbs[1] += (($this->overflow) & 0xffff); } else { $neg = -(($this->limbs[0] >> 15) & 1); $return->limbs[0] = (int)($neg & 0xffff); $return->limbs[1] = (int)($neg & 0xffff); } $return->limbs[2] = (int) ($this->limbs[0] & 0xffff); $return->limbs[3] = (int) ($this->limbs[1] & 0xffff); return $return; } /** * @return string * @throws TypeError */ public function toReverseString() { return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff); } /** * @return string */ public function __toString() { try { return $this->toString(); } catch (TypeError $ex) { // PHP engine can't handle exceptions from __toString() return ''; } } } Core32/Curve25519/error_log 0000644 00000003614 15162213664 0011222 0 ustar 00 [24-Mar-2026 23:44:46 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519/H.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519/H.php on line 12 [25-Mar-2026 02:17:16 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519/H.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519/H.php on line 12 [25-Mar-2026 12:27:01 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519/H.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519/H.php on line 12 [26-Mar-2026 09:15:22 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519/H.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519/H.php on line 12 [26-Mar-2026 13:17:17 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519/H.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519/H.php on line 12 [26-Mar-2026 22:49:07 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519/H.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Curve25519/H.php on line 12 Core32/Curve25519/Ge/Precomp.php 0000644 00000002775 15162213665 0011766 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp', false)) { return; } /** * Class ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp */ class ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp { /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $yplusx; /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $yminusx; /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $xy2d; /** * ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp constructor. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe $yplusx * @param ParagonIE_Sodium_Core32_Curve25519_Fe $yminusx * @param ParagonIE_Sodium_Core32_Curve25519_Fe $xy2d * @throws SodiumException * @throws TypeError */ public function __construct( ParagonIE_Sodium_Core32_Curve25519_Fe $yplusx = null, ParagonIE_Sodium_Core32_Curve25519_Fe $yminusx = null, ParagonIE_Sodium_Core32_Curve25519_Fe $xy2d = null ) { if ($yplusx === null) { $yplusx = ParagonIE_Sodium_Core32_Curve25519::fe_0(); } $this->yplusx = $yplusx; if ($yminusx === null) { $yminusx = ParagonIE_Sodium_Core32_Curve25519::fe_0(); } $this->yminusx = $yminusx; if ($xy2d === null) { $xy2d = ParagonIE_Sodium_Core32_Curve25519::fe_0(); } $this->xy2d = $xy2d; } } Core32/Curve25519/Ge/Cached.php 0000644 00000003415 15162213665 0011520 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_Curve25519_Ge_Cached', false)) { return; } /** * Class ParagonIE_Sodium_Core32_Curve25519_Ge_Cached */ class ParagonIE_Sodium_Core32_Curve25519_Ge_Cached { /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $YplusX; /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $YminusX; /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $Z; /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $T2d; /** * ParagonIE_Sodium_Core32_Curve25519_Ge_Cached constructor. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $YplusX * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $YminusX * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $Z * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $T2d */ public function __construct( ParagonIE_Sodium_Core32_Curve25519_Fe $YplusX = null, ParagonIE_Sodium_Core32_Curve25519_Fe $YminusX = null, ParagonIE_Sodium_Core32_Curve25519_Fe $Z = null, ParagonIE_Sodium_Core32_Curve25519_Fe $T2d = null ) { if ($YplusX === null) { $YplusX = new ParagonIE_Sodium_Core32_Curve25519_Fe(); } $this->YplusX = $YplusX; if ($YminusX === null) { $YminusX = new ParagonIE_Sodium_Core32_Curve25519_Fe(); } $this->YminusX = $YminusX; if ($Z === null) { $Z = new ParagonIE_Sodium_Core32_Curve25519_Fe(); } $this->Z = $Z; if ($T2d === null) { $T2d = new ParagonIE_Sodium_Core32_Curve25519_Fe(); } $this->T2d = $T2d; } } Core32/Curve25519/Ge/P1p1.php 0000644 00000003344 15162213665 0011073 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1', false)) { return; } /** * Class ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 */ class ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 { /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $X; /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $Y; /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $Z; /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $T; /** * ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 constructor. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $x * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $y * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $z * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $t * * @throws SodiumException * @throws TypeError */ public function __construct( ParagonIE_Sodium_Core32_Curve25519_Fe $x = null, ParagonIE_Sodium_Core32_Curve25519_Fe $y = null, ParagonIE_Sodium_Core32_Curve25519_Fe $z = null, ParagonIE_Sodium_Core32_Curve25519_Fe $t = null ) { if ($x === null) { $x = ParagonIE_Sodium_Core32_Curve25519::fe_0(); } $this->X = $x; if ($y === null) { $y = ParagonIE_Sodium_Core32_Curve25519::fe_0(); } $this->Y = $y; if ($z === null) { $z = ParagonIE_Sodium_Core32_Curve25519::fe_0(); } $this->Z = $z; if ($t === null) { $t = ParagonIE_Sodium_Core32_Curve25519::fe_0(); } $this->T = $t; } } Core32/Curve25519/Ge/P3.php 0000644 00000003242 15162213666 0010632 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_Curve25519_Ge_P3', false)) { return; } /** * Class ParagonIE_Sodium_Core32_Curve25519_Ge_P3 */ class ParagonIE_Sodium_Core32_Curve25519_Ge_P3 { /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $X; /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $Y; /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $Z; /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $T; /** * ParagonIE_Sodium_Core32_Curve25519_Ge_P3 constructor. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $x * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $y * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $z * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $t */ public function __construct( ParagonIE_Sodium_Core32_Curve25519_Fe $x = null, ParagonIE_Sodium_Core32_Curve25519_Fe $y = null, ParagonIE_Sodium_Core32_Curve25519_Fe $z = null, ParagonIE_Sodium_Core32_Curve25519_Fe $t = null ) { if ($x === null) { $x = new ParagonIE_Sodium_Core32_Curve25519_Fe(); } $this->X = $x; if ($y === null) { $y = new ParagonIE_Sodium_Core32_Curve25519_Fe(); } $this->Y = $y; if ($z === null) { $z = new ParagonIE_Sodium_Core32_Curve25519_Fe(); } $this->Z = $z; if ($t === null) { $t = new ParagonIE_Sodium_Core32_Curve25519_Fe(); } $this->T = $t; } } Core32/Curve25519/Ge/P2.php 0000644 00000002541 15162213666 0010632 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_Curve25519_Ge_P2', false)) { return; } /** * Class ParagonIE_Sodium_Core32_Curve25519_Ge_P2 */ class ParagonIE_Sodium_Core32_Curve25519_Ge_P2 { /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $X; /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $Y; /** * @var ParagonIE_Sodium_Core32_Curve25519_Fe */ public $Z; /** * ParagonIE_Sodium_Core32_Curve25519_Ge_P2 constructor. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $x * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $y * @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $z */ public function __construct( ParagonIE_Sodium_Core32_Curve25519_Fe $x = null, ParagonIE_Sodium_Core32_Curve25519_Fe $y = null, ParagonIE_Sodium_Core32_Curve25519_Fe $z = null ) { if ($x === null) { $x = new ParagonIE_Sodium_Core32_Curve25519_Fe(); } $this->X = $x; if ($y === null) { $y = new ParagonIE_Sodium_Core32_Curve25519_Fe(); } $this->Y = $y; if ($z === null) { $z = new ParagonIE_Sodium_Core32_Curve25519_Fe(); } $this->Z = $z; } } Core32/Curve25519/H.php 0000644 00000324375 15162213667 0010222 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_Curve25519_H', false)) { return; } /** * Class ParagonIE_Sodium_Core32_Curve25519_H * * This just contains the constants in the ref10/base.h file */ class ParagonIE_Sodium_Core32_Curve25519_H extends ParagonIE_Sodium_Core32_Util { /** * See: libsodium's crypto_core/curve25519/ref10/base.h * * @var array<int, array<int, array<int, array<int, int>>>> Basically, int[32][8][3][10] */ protected static $base = array( array( array( array(25967493, -14356035, 29566456, 3660896, -12694345, 4014787, 27544626, -11754271, -6079156, 2047605), array(-12545711, 934262, -2722910, 3049990, -727428, 9406986, 12720692, 5043384, 19500929, -15469378), array(-8738181, 4489570, 9688441, -14785194, 10184609, -12363380, 29287919, 11864899, -24514362, -4438546), ), array( array(-12815894, -12976347, -21581243, 11784320, -25355658, -2750717, -11717903, -3814571, -358445, -10211303), array(-21703237, 6903825, 27185491, 6451973, -29577724, -9554005, -15616551, 11189268, -26829678, -5319081), array(26966642, 11152617, 32442495, 15396054, 14353839, -12752335, -3128826, -9541118, -15472047, -4166697), ), array( array(15636291, -9688557, 24204773, -7912398, 616977, -16685262, 27787600, -14772189, 28944400, -1550024), array(16568933, 4717097, -11556148, -1102322, 15682896, -11807043, 16354577, -11775962, 7689662, 11199574), array(30464156, -5976125, -11779434, -15670865, 23220365, 15915852, 7512774, 10017326, -17749093, -9920357), ), array( array(-17036878, 13921892, 10945806, -6033431, 27105052, -16084379, -28926210, 15006023, 3284568, -6276540), array(23599295, -8306047, -11193664, -7687416, 13236774, 10506355, 7464579, 9656445, 13059162, 10374397), array(7798556, 16710257, 3033922, 2874086, 28997861, 2835604, 32406664, -3839045, -641708, -101325), ), array( array(10861363, 11473154, 27284546, 1981175, -30064349, 12577861, 32867885, 14515107, -15438304, 10819380), array(4708026, 6336745, 20377586, 9066809, -11272109, 6594696, -25653668, 12483688, -12668491, 5581306), array(19563160, 16186464, -29386857, 4097519, 10237984, -4348115, 28542350, 13850243, -23678021, -15815942), ), array( array(-15371964, -12862754, 32573250, 4720197, -26436522, 5875511, -19188627, -15224819, -9818940, -12085777), array(-8549212, 109983, 15149363, 2178705, 22900618, 4543417, 3044240, -15689887, 1762328, 14866737), array(-18199695, -15951423, -10473290, 1707278, -17185920, 3916101, -28236412, 3959421, 27914454, 4383652), ), array( array(5153746, 9909285, 1723747, -2777874, 30523605, 5516873, 19480852, 5230134, -23952439, -15175766), array(-30269007, -3463509, 7665486, 10083793, 28475525, 1649722, 20654025, 16520125, 30598449, 7715701), array(28881845, 14381568, 9657904, 3680757, -20181635, 7843316, -31400660, 1370708, 29794553, -1409300), ), array( array(14499471, -2729599, -33191113, -4254652, 28494862, 14271267, 30290735, 10876454, -33154098, 2381726), array(-7195431, -2655363, -14730155, 462251, -27724326, 3941372, -6236617, 3696005, -32300832, 15351955), array(27431194, 8222322, 16448760, -3907995, -18707002, 11938355, -32961401, -2970515, 29551813, 10109425), ), ), array( array( array(-13657040, -13155431, -31283750, 11777098, 21447386, 6519384, -2378284, -1627556, 10092783, -4764171), array(27939166, 14210322, 4677035, 16277044, -22964462, -12398139, -32508754, 12005538, -17810127, 12803510), array(17228999, -15661624, -1233527, 300140, -1224870, -11714777, 30364213, -9038194, 18016357, 4397660), ), array( array(-10958843, -7690207, 4776341, -14954238, 27850028, -15602212, -26619106, 14544525, -17477504, 982639), array(29253598, 15796703, -2863982, -9908884, 10057023, 3163536, 7332899, -4120128, -21047696, 9934963), array(5793303, 16271923, -24131614, -10116404, 29188560, 1206517, -14747930, 4559895, -30123922, -10897950), ), array( array(-27643952, -11493006, 16282657, -11036493, 28414021, -15012264, 24191034, 4541697, -13338309, 5500568), array(12650548, -1497113, 9052871, 11355358, -17680037, -8400164, -17430592, 12264343, 10874051, 13524335), array(25556948, -3045990, 714651, 2510400, 23394682, -10415330, 33119038, 5080568, -22528059, 5376628), ), array( array(-26088264, -4011052, -17013699, -3537628, -6726793, 1920897, -22321305, -9447443, 4535768, 1569007), array(-2255422, 14606630, -21692440, -8039818, 28430649, 8775819, -30494562, 3044290, 31848280, 12543772), array(-22028579, 2943893, -31857513, 6777306, 13784462, -4292203, -27377195, -2062731, 7718482, 14474653), ), array( array(2385315, 2454213, -22631320, 46603, -4437935, -15680415, 656965, -7236665, 24316168, -5253567), array(13741529, 10911568, -33233417, -8603737, -20177830, -1033297, 33040651, -13424532, -20729456, 8321686), array(21060490, -2212744, 15712757, -4336099, 1639040, 10656336, 23845965, -11874838, -9984458, 608372), ), array( array(-13672732, -15087586, -10889693, -7557059, -6036909, 11305547, 1123968, -6780577, 27229399, 23887), array(-23244140, -294205, -11744728, 14712571, -29465699, -2029617, 12797024, -6440308, -1633405, 16678954), array(-29500620, 4770662, -16054387, 14001338, 7830047, 9564805, -1508144, -4795045, -17169265, 4904953), ), array( array(24059557, 14617003, 19037157, -15039908, 19766093, -14906429, 5169211, 16191880, 2128236, -4326833), array(-16981152, 4124966, -8540610, -10653797, 30336522, -14105247, -29806336, 916033, -6882542, -2986532), array(-22630907, 12419372, -7134229, -7473371, -16478904, 16739175, 285431, 2763829, 15736322, 4143876), ), array( array(2379352, 11839345, -4110402, -5988665, 11274298, 794957, 212801, -14594663, 23527084, -16458268), array(33431127, -11130478, -17838966, -15626900, 8909499, 8376530, -32625340, 4087881, -15188911, -14416214), array(1767683, 7197987, -13205226, -2022635, -13091350, 448826, 5799055, 4357868, -4774191, -16323038), ), ), array( array( array(6721966, 13833823, -23523388, -1551314, 26354293, -11863321, 23365147, -3949732, 7390890, 2759800), array(4409041, 2052381, 23373853, 10530217, 7676779, -12885954, 21302353, -4264057, 1244380, -12919645), array(-4421239, 7169619, 4982368, -2957590, 30256825, -2777540, 14086413, 9208236, 15886429, 16489664), ), array( array(1996075, 10375649, 14346367, 13311202, -6874135, -16438411, -13693198, 398369, -30606455, -712933), array(-25307465, 9795880, -2777414, 14878809, -33531835, 14780363, 13348553, 12076947, -30836462, 5113182), array(-17770784, 11797796, 31950843, 13929123, -25888302, 12288344, -30341101, -7336386, 13847711, 5387222), ), array( array(-18582163, -3416217, 17824843, -2340966, 22744343, -10442611, 8763061, 3617786, -19600662, 10370991), array(20246567, -14369378, 22358229, -543712, 18507283, -10413996, 14554437, -8746092, 32232924, 16763880), array(9648505, 10094563, 26416693, 14745928, -30374318, -6472621, 11094161, 15689506, 3140038, -16510092), ), array( array(-16160072, 5472695, 31895588, 4744994, 8823515, 10365685, -27224800, 9448613, -28774454, 366295), array(19153450, 11523972, -11096490, -6503142, -24647631, 5420647, 28344573, 8041113, 719605, 11671788), array(8678025, 2694440, -6808014, 2517372, 4964326, 11152271, -15432916, -15266516, 27000813, -10195553), ), array( array(-15157904, 7134312, 8639287, -2814877, -7235688, 10421742, 564065, 5336097, 6750977, -14521026), array(11836410, -3979488, 26297894, 16080799, 23455045, 15735944, 1695823, -8819122, 8169720, 16220347), array(-18115838, 8653647, 17578566, -6092619, -8025777, -16012763, -11144307, -2627664, -5990708, -14166033), ), array( array(-23308498, -10968312, 15213228, -10081214, -30853605, -11050004, 27884329, 2847284, 2655861, 1738395), array(-27537433, -14253021, -25336301, -8002780, -9370762, 8129821, 21651608, -3239336, -19087449, -11005278), array(1533110, 3437855, 23735889, 459276, 29970501, 11335377, 26030092, 5821408, 10478196, 8544890), ), array( array(32173121, -16129311, 24896207, 3921497, 22579056, -3410854, 19270449, 12217473, 17789017, -3395995), array(-30552961, -2228401, -15578829, -10147201, 13243889, 517024, 15479401, -3853233, 30460520, 1052596), array(-11614875, 13323618, 32618793, 8175907, -15230173, 12596687, 27491595, -4612359, 3179268, -9478891), ), array( array(31947069, -14366651, -4640583, -15339921, -15125977, -6039709, -14756777, -16411740, 19072640, -9511060), array(11685058, 11822410, 3158003, -13952594, 33402194, -4165066, 5977896, -5215017, 473099, 5040608), array(-20290863, 8198642, -27410132, 11602123, 1290375, -2799760, 28326862, 1721092, -19558642, -3131606), ), ), array( array( array(7881532, 10687937, 7578723, 7738378, -18951012, -2553952, 21820786, 8076149, -27868496, 11538389), array(-19935666, 3899861, 18283497, -6801568, -15728660, -11249211, 8754525, 7446702, -5676054, 5797016), array(-11295600, -3793569, -15782110, -7964573, 12708869, -8456199, 2014099, -9050574, -2369172, -5877341), ), array( array(-22472376, -11568741, -27682020, 1146375, 18956691, 16640559, 1192730, -3714199, 15123619, 10811505), array(14352098, -3419715, -18942044, 10822655, 32750596, 4699007, -70363, 15776356, -28886779, -11974553), array(-28241164, -8072475, -4978962, -5315317, 29416931, 1847569, -20654173, -16484855, 4714547, -9600655), ), array( array(15200332, 8368572, 19679101, 15970074, -31872674, 1959451, 24611599, -4543832, -11745876, 12340220), array(12876937, -10480056, 33134381, 6590940, -6307776, 14872440, 9613953, 8241152, 15370987, 9608631), array(-4143277, -12014408, 8446281, -391603, 4407738, 13629032, -7724868, 15866074, -28210621, -8814099), ), array( array(26660628, -15677655, 8393734, 358047, -7401291, 992988, -23904233, 858697, 20571223, 8420556), array(14620715, 13067227, -15447274, 8264467, 14106269, 15080814, 33531827, 12516406, -21574435, -12476749), array(236881, 10476226, 57258, -14677024, 6472998, 2466984, 17258519, 7256740, 8791136, 15069930), ), array( array(1276410, -9371918, 22949635, -16322807, -23493039, -5702186, 14711875, 4874229, -30663140, -2331391), array(5855666, 4990204, -13711848, 7294284, -7804282, 1924647, -1423175, -7912378, -33069337, 9234253), array(20590503, -9018988, 31529744, -7352666, -2706834, 10650548, 31559055, -11609587, 18979186, 13396066), ), array( array(24474287, 4968103, 22267082, 4407354, 24063882, -8325180, -18816887, 13594782, 33514650, 7021958), array(-11566906, -6565505, -21365085, 15928892, -26158305, 4315421, -25948728, -3916677, -21480480, 12868082), array(-28635013, 13504661, 19988037, -2132761, 21078225, 6443208, -21446107, 2244500, -12455797, -8089383), ), array( array(-30595528, 13793479, -5852820, 319136, -25723172, -6263899, 33086546, 8957937, -15233648, 5540521), array(-11630176, -11503902, -8119500, -7643073, 2620056, 1022908, -23710744, -1568984, -16128528, -14962807), array(23152971, 775386, 27395463, 14006635, -9701118, 4649512, 1689819, 892185, -11513277, -15205948), ), array( array(9770129, 9586738, 26496094, 4324120, 1556511, -3550024, 27453819, 4763127, -19179614, 5867134), array(-32765025, 1927590, 31726409, -4753295, 23962434, -16019500, 27846559, 5931263, -29749703, -16108455), array(27461885, -2977536, 22380810, 1815854, -23033753, -3031938, 7283490, -15148073, -19526700, 7734629), ), ), array( array( array(-8010264, -9590817, -11120403, 6196038, 29344158, -13430885, 7585295, -3176626, 18549497, 15302069), array(-32658337, -6171222, -7672793, -11051681, 6258878, 13504381, 10458790, -6418461, -8872242, 8424746), array(24687205, 8613276, -30667046, -3233545, 1863892, -1830544, 19206234, 7134917, -11284482, -828919), ), array( array(11334899, -9218022, 8025293, 12707519, 17523892, -10476071, 10243738, -14685461, -5066034, 16498837), array(8911542, 6887158, -9584260, -6958590, 11145641, -9543680, 17303925, -14124238, 6536641, 10543906), array(-28946384, 15479763, -17466835, 568876, -1497683, 11223454, -2669190, -16625574, -27235709, 8876771), ), array( array(-25742899, -12566864, -15649966, -846607, -33026686, -796288, -33481822, 15824474, -604426, -9039817), array(10330056, 70051, 7957388, -9002667, 9764902, 15609756, 27698697, -4890037, 1657394, 3084098), array(10477963, -7470260, 12119566, -13250805, 29016247, -5365589, 31280319, 14396151, -30233575, 15272409), ), array( array(-12288309, 3169463, 28813183, 16658753, 25116432, -5630466, -25173957, -12636138, -25014757, 1950504), array(-26180358, 9489187, 11053416, -14746161, -31053720, 5825630, -8384306, -8767532, 15341279, 8373727), array(28685821, 7759505, -14378516, -12002860, -31971820, 4079242, 298136, -10232602, -2878207, 15190420), ), array( array(-32932876, 13806336, -14337485, -15794431, -24004620, 10940928, 8669718, 2742393, -26033313, -6875003), array(-1580388, -11729417, -25979658, -11445023, -17411874, -10912854, 9291594, -16247779, -12154742, 6048605), array(-30305315, 14843444, 1539301, 11864366, 20201677, 1900163, 13934231, 5128323, 11213262, 9168384), ), array( array(-26280513, 11007847, 19408960, -940758, -18592965, -4328580, -5088060, -11105150, 20470157, -16398701), array(-23136053, 9282192, 14855179, -15390078, -7362815, -14408560, -22783952, 14461608, 14042978, 5230683), array(29969567, -2741594, -16711867, -8552442, 9175486, -2468974, 21556951, 3506042, -5933891, -12449708), ), array( array(-3144746, 8744661, 19704003, 4581278, -20430686, 6830683, -21284170, 8971513, -28539189, 15326563), array(-19464629, 10110288, -17262528, -3503892, -23500387, 1355669, -15523050, 15300988, -20514118, 9168260), array(-5353335, 4488613, -23803248, 16314347, 7780487, -15638939, -28948358, 9601605, 33087103, -9011387), ), array( array(-19443170, -15512900, -20797467, -12445323, -29824447, 10229461, -27444329, -15000531, -5996870, 15664672), array(23294591, -16632613, -22650781, -8470978, 27844204, 11461195, 13099750, -2460356, 18151676, 13417686), array(-24722913, -4176517, -31150679, 5988919, -26858785, 6685065, 1661597, -12551441, 15271676, -15452665), ), ), array( array( array(11433042, -13228665, 8239631, -5279517, -1985436, -725718, -18698764, 2167544, -6921301, -13440182), array(-31436171, 15575146, 30436815, 12192228, -22463353, 9395379, -9917708, -8638997, 12215110, 12028277), array(14098400, 6555944, 23007258, 5757252, -15427832, -12950502, 30123440, 4617780, -16900089, -655628), ), array( array(-4026201, -15240835, 11893168, 13718664, -14809462, 1847385, -15819999, 10154009, 23973261, -12684474), array(-26531820, -3695990, -1908898, 2534301, -31870557, -16550355, 18341390, -11419951, 32013174, -10103539), array(-25479301, 10876443, -11771086, -14625140, -12369567, 1838104, 21911214, 6354752, 4425632, -837822), ), array( array(-10433389, -14612966, 22229858, -3091047, -13191166, 776729, -17415375, -12020462, 4725005, 14044970), array(19268650, -7304421, 1555349, 8692754, -21474059, -9910664, 6347390, -1411784, -19522291, -16109756), array(-24864089, 12986008, -10898878, -5558584, -11312371, -148526, 19541418, 8180106, 9282262, 10282508), ), array( array(-26205082, 4428547, -8661196, -13194263, 4098402, -14165257, 15522535, 8372215, 5542595, -10702683), array(-10562541, 14895633, 26814552, -16673850, -17480754, -2489360, -2781891, 6993761, -18093885, 10114655), array(-20107055, -929418, 31422704, 10427861, -7110749, 6150669, -29091755, -11529146, 25953725, -106158), ), array( array(-4234397, -8039292, -9119125, 3046000, 2101609, -12607294, 19390020, 6094296, -3315279, 12831125), array(-15998678, 7578152, 5310217, 14408357, -33548620, -224739, 31575954, 6326196, 7381791, -2421839), array(-20902779, 3296811, 24736065, -16328389, 18374254, 7318640, 6295303, 8082724, -15362489, 12339664), ), array( array(27724736, 2291157, 6088201, -14184798, 1792727, 5857634, 13848414, 15768922, 25091167, 14856294), array(-18866652, 8331043, 24373479, 8541013, -701998, -9269457, 12927300, -12695493, -22182473, -9012899), array(-11423429, -5421590, 11632845, 3405020, 30536730, -11674039, -27260765, 13866390, 30146206, 9142070), ), array( array(3924129, -15307516, -13817122, -10054960, 12291820, -668366, -27702774, 9326384, -8237858, 4171294), array(-15921940, 16037937, 6713787, 16606682, -21612135, 2790944, 26396185, 3731949, 345228, -5462949), array(-21327538, 13448259, 25284571, 1143661, 20614966, -8849387, 2031539, -12391231, -16253183, -13582083), ), array( array(31016211, -16722429, 26371392, -14451233, -5027349, 14854137, 17477601, 3842657, 28012650, -16405420), array(-5075835, 9368966, -8562079, -4600902, -15249953, 6970560, -9189873, 16292057, -8867157, 3507940), array(29439664, 3537914, 23333589, 6997794, -17555561, -11018068, -15209202, -15051267, -9164929, 6580396), ), ), array( array( array(-12185861, -7679788, 16438269, 10826160, -8696817, -6235611, 17860444, -9273846, -2095802, 9304567), array(20714564, -4336911, 29088195, 7406487, 11426967, -5095705, 14792667, -14608617, 5289421, -477127), array(-16665533, -10650790, -6160345, -13305760, 9192020, -1802462, 17271490, 12349094, 26939669, -3752294), ), array( array(-12889898, 9373458, 31595848, 16374215, 21471720, 13221525, -27283495, -12348559, -3698806, 117887), array(22263325, -6560050, 3984570, -11174646, -15114008, -566785, 28311253, 5358056, -23319780, 541964), array(16259219, 3261970, 2309254, -15534474, -16885711, -4581916, 24134070, -16705829, -13337066, -13552195), ), array( array(9378160, -13140186, -22845982, -12745264, 28198281, -7244098, -2399684, -717351, 690426, 14876244), array(24977353, -314384, -8223969, -13465086, 28432343, -1176353, -13068804, -12297348, -22380984, 6618999), array(-1538174, 11685646, 12944378, 13682314, -24389511, -14413193, 8044829, -13817328, 32239829, -5652762), ), array( array(-18603066, 4762990, -926250, 8885304, -28412480, -3187315, 9781647, -10350059, 32779359, 5095274), array(-33008130, -5214506, -32264887, -3685216, 9460461, -9327423, -24601656, 14506724, 21639561, -2630236), array(-16400943, -13112215, 25239338, 15531969, 3987758, -4499318, -1289502, -6863535, 17874574, 558605), ), array( array(-13600129, 10240081, 9171883, 16131053, -20869254, 9599700, 33499487, 5080151, 2085892, 5119761), array(-22205145, -2519528, -16381601, 414691, -25019550, 2170430, 30634760, -8363614, -31999993, -5759884), array(-6845704, 15791202, 8550074, -1312654, 29928809, -12092256, 27534430, -7192145, -22351378, 12961482), ), array( array(-24492060, -9570771, 10368194, 11582341, -23397293, -2245287, 16533930, 8206996, -30194652, -5159638), array(-11121496, -3382234, 2307366, 6362031, -135455, 8868177, -16835630, 7031275, 7589640, 8945490), array(-32152748, 8917967, 6661220, -11677616, -1192060, -15793393, 7251489, -11182180, 24099109, -14456170), ), array( array(5019558, -7907470, 4244127, -14714356, -26933272, 6453165, -19118182, -13289025, -6231896, -10280736), array(10853594, 10721687, 26480089, 5861829, -22995819, 1972175, -1866647, -10557898, -3363451, -6441124), array(-17002408, 5906790, 221599, -6563147, 7828208, -13248918, 24362661, -2008168, -13866408, 7421392), ), array( array(8139927, -6546497, 32257646, -5890546, 30375719, 1886181, -21175108, 15441252, 28826358, -4123029), array(6267086, 9695052, 7709135, -16603597, -32869068, -1886135, 14795160, -7840124, 13746021, -1742048), array(28584902, 7787108, -6732942, -15050729, 22846041, -7571236, -3181936, -363524, 4771362, -8419958), ), ), array( array( array(24949256, 6376279, -27466481, -8174608, -18646154, -9930606, 33543569, -12141695, 3569627, 11342593), array(26514989, 4740088, 27912651, 3697550, 19331575, -11472339, 6809886, 4608608, 7325975, -14801071), array(-11618399, -14554430, -24321212, 7655128, -1369274, 5214312, -27400540, 10258390, -17646694, -8186692), ), array( array(11431204, 15823007, 26570245, 14329124, 18029990, 4796082, -31446179, 15580664, 9280358, -3973687), array(-160783, -10326257, -22855316, -4304997, -20861367, -13621002, -32810901, -11181622, -15545091, 4387441), array(-20799378, 12194512, 3937617, -5805892, -27154820, 9340370, -24513992, 8548137, 20617071, -7482001), ), array( array(-938825, -3930586, -8714311, 16124718, 24603125, -6225393, -13775352, -11875822, 24345683, 10325460), array(-19855277, -1568885, -22202708, 8714034, 14007766, 6928528, 16318175, -1010689, 4766743, 3552007), array(-21751364, -16730916, 1351763, -803421, -4009670, 3950935, 3217514, 14481909, 10988822, -3994762), ), array( array(15564307, -14311570, 3101243, 5684148, 30446780, -8051356, 12677127, -6505343, -8295852, 13296005), array(-9442290, 6624296, -30298964, -11913677, -4670981, -2057379, 31521204, 9614054, -30000824, 12074674), array(4771191, -135239, 14290749, -13089852, 27992298, 14998318, -1413936, -1556716, 29832613, -16391035), ), array( array(7064884, -7541174, -19161962, -5067537, -18891269, -2912736, 25825242, 5293297, -27122660, 13101590), array(-2298563, 2439670, -7466610, 1719965, -27267541, -16328445, 32512469, -5317593, -30356070, -4190957), array(-30006540, 10162316, -33180176, 3981723, -16482138, -13070044, 14413974, 9515896, 19568978, 9628812), ), array( array(33053803, 199357, 15894591, 1583059, 27380243, -4580435, -17838894, -6106839, -6291786, 3437740), array(-18978877, 3884493, 19469877, 12726490, 15913552, 13614290, -22961733, 70104, 7463304, 4176122), array(-27124001, 10659917, 11482427, -16070381, 12771467, -6635117, -32719404, -5322751, 24216882, 5944158), ), array( array(8894125, 7450974, -2664149, -9765752, -28080517, -12389115, 19345746, 14680796, 11632993, 5847885), array(26942781, -2315317, 9129564, -4906607, 26024105, 11769399, -11518837, 6367194, -9727230, 4782140), array(19916461, -4828410, -22910704, -11414391, 25606324, -5972441, 33253853, 8220911, 6358847, -1873857), ), array( array(801428, -2081702, 16569428, 11065167, 29875704, 96627, 7908388, -4480480, -13538503, 1387155), array(19646058, 5720633, -11416706, 12814209, 11607948, 12749789, 14147075, 15156355, -21866831, 11835260), array(19299512, 1155910, 28703737, 14890794, 2925026, 7269399, 26121523, 15467869, -26560550, 5052483), ), ), array( array( array(-3017432, 10058206, 1980837, 3964243, 22160966, 12322533, -6431123, -12618185, 12228557, -7003677), array(32944382, 14922211, -22844894, 5188528, 21913450, -8719943, 4001465, 13238564, -6114803, 8653815), array(22865569, -4652735, 27603668, -12545395, 14348958, 8234005, 24808405, 5719875, 28483275, 2841751), ), array( array(-16420968, -1113305, -327719, -12107856, 21886282, -15552774, -1887966, -315658, 19932058, -12739203), array(-11656086, 10087521, -8864888, -5536143, -19278573, -3055912, 3999228, 13239134, -4777469, -13910208), array(1382174, -11694719, 17266790, 9194690, -13324356, 9720081, 20403944, 11284705, -14013818, 3093230), ), array( array(16650921, -11037932, -1064178, 1570629, -8329746, 7352753, -302424, 16271225, -24049421, -6691850), array(-21911077, -5927941, -4611316, -5560156, -31744103, -10785293, 24123614, 15193618, -21652117, -16739389), array(-9935934, -4289447, -25279823, 4372842, 2087473, 10399484, 31870908, 14690798, 17361620, 11864968), ), array( array(-11307610, 6210372, 13206574, 5806320, -29017692, -13967200, -12331205, -7486601, -25578460, -16240689), array(14668462, -12270235, 26039039, 15305210, 25515617, 4542480, 10453892, 6577524, 9145645, -6443880), array(5974874, 3053895, -9433049, -10385191, -31865124, 3225009, -7972642, 3936128, -5652273, -3050304), ), array( array(30625386, -4729400, -25555961, -12792866, -20484575, 7695099, 17097188, -16303496, -27999779, 1803632), array(-3553091, 9865099, -5228566, 4272701, -5673832, -16689700, 14911344, 12196514, -21405489, 7047412), array(20093277, 9920966, -11138194, -5343857, 13161587, 12044805, -32856851, 4124601, -32343828, -10257566), ), array( array(-20788824, 14084654, -13531713, 7842147, 19119038, -13822605, 4752377, -8714640, -21679658, 2288038), array(-26819236, -3283715, 29965059, 3039786, -14473765, 2540457, 29457502, 14625692, -24819617, 12570232), array(-1063558, -11551823, 16920318, 12494842, 1278292, -5869109, -21159943, -3498680, -11974704, 4724943), ), array( array(17960970, -11775534, -4140968, -9702530, -8876562, -1410617, -12907383, -8659932, -29576300, 1903856), array(23134274, -14279132, -10681997, -1611936, 20684485, 15770816, -12989750, 3190296, 26955097, 14109738), array(15308788, 5320727, -30113809, -14318877, 22902008, 7767164, 29425325, -11277562, 31960942, 11934971), ), array( array(-27395711, 8435796, 4109644, 12222639, -24627868, 14818669, 20638173, 4875028, 10491392, 1379718), array(-13159415, 9197841, 3875503, -8936108, -1383712, -5879801, 33518459, 16176658, 21432314, 12180697), array(-11787308, 11500838, 13787581, -13832590, -22430679, 10140205, 1465425, 12689540, -10301319, -13872883), ), ), array( array( array(5414091, -15386041, -21007664, 9643570, 12834970, 1186149, -2622916, -1342231, 26128231, 6032912), array(-26337395, -13766162, 32496025, -13653919, 17847801, -12669156, 3604025, 8316894, -25875034, -10437358), array(3296484, 6223048, 24680646, -12246460, -23052020, 5903205, -8862297, -4639164, 12376617, 3188849), ), array( array(29190488, -14659046, 27549113, -1183516, 3520066, -10697301, 32049515, -7309113, -16109234, -9852307), array(-14744486, -9309156, 735818, -598978, -20407687, -5057904, 25246078, -15795669, 18640741, -960977), array(-6928835, -16430795, 10361374, 5642961, 4910474, 12345252, -31638386, -494430, 10530747, 1053335), ), array( array(-29265967, -14186805, -13538216, -12117373, -19457059, -10655384, -31462369, -2948985, 24018831, 15026644), array(-22592535, -3145277, -2289276, 5953843, -13440189, 9425631, 25310643, 13003497, -2314791, -15145616), array(-27419985, -603321, -8043984, -1669117, -26092265, 13987819, -27297622, 187899, -23166419, -2531735), ), array( array(-21744398, -13810475, 1844840, 5021428, -10434399, -15911473, 9716667, 16266922, -5070217, 726099), array(29370922, -6053998, 7334071, -15342259, 9385287, 2247707, -13661962, -4839461, 30007388, -15823341), array(-936379, 16086691, 23751945, -543318, -1167538, -5189036, 9137109, 730663, 9835848, 4555336), ), array( array(-23376435, 1410446, -22253753, -12899614, 30867635, 15826977, 17693930, 544696, -11985298, 12422646), array(31117226, -12215734, -13502838, 6561947, -9876867, -12757670, -5118685, -4096706, 29120153, 13924425), array(-17400879, -14233209, 19675799, -2734756, -11006962, -5858820, -9383939, -11317700, 7240931, -237388), ), array( array(-31361739, -11346780, -15007447, -5856218, -22453340, -12152771, 1222336, 4389483, 3293637, -15551743), array(-16684801, -14444245, 11038544, 11054958, -13801175, -3338533, -24319580, 7733547, 12796905, -6335822), array(-8759414, -10817836, -25418864, 10783769, -30615557, -9746811, -28253339, 3647836, 3222231, -11160462), ), array( array(18606113, 1693100, -25448386, -15170272, 4112353, 10045021, 23603893, -2048234, -7550776, 2484985), array(9255317, -3131197, -12156162, -1004256, 13098013, -9214866, 16377220, -2102812, -19802075, -3034702), array(-22729289, 7496160, -5742199, 11329249, 19991973, -3347502, -31718148, 9936966, -30097688, -10618797), ), array( array(21878590, -5001297, 4338336, 13643897, -3036865, 13160960, 19708896, 5415497, -7360503, -4109293), array(27736861, 10103576, 12500508, 8502413, -3413016, -9633558, 10436918, -1550276, -23659143, -8132100), array(19492550, -12104365, -29681976, -852630, -3208171, 12403437, 30066266, 8367329, 13243957, 8709688), ), ), array( array( array(12015105, 2801261, 28198131, 10151021, 24818120, -4743133, -11194191, -5645734, 5150968, 7274186), array(2831366, -12492146, 1478975, 6122054, 23825128, -12733586, 31097299, 6083058, 31021603, -9793610), array(-2529932, -2229646, 445613, 10720828, -13849527, -11505937, -23507731, 16354465, 15067285, -14147707), ), array( array(7840942, 14037873, -33364863, 15934016, -728213, -3642706, 21403988, 1057586, -19379462, -12403220), array(915865, -16469274, 15608285, -8789130, -24357026, 6060030, -17371319, 8410997, -7220461, 16527025), array(32922597, -556987, 20336074, -16184568, 10903705, -5384487, 16957574, 52992, 23834301, 6588044), ), array( array(32752030, 11232950, 3381995, -8714866, 22652988, -10744103, 17159699, 16689107, -20314580, -1305992), array(-4689649, 9166776, -25710296, -10847306, 11576752, 12733943, 7924251, -2752281, 1976123, -7249027), array(21251222, 16309901, -2983015, -6783122, 30810597, 12967303, 156041, -3371252, 12331345, -8237197), ), array( array(8651614, -4477032, -16085636, -4996994, 13002507, 2950805, 29054427, -5106970, 10008136, -4667901), array(31486080, 15114593, -14261250, 12951354, 14369431, -7387845, 16347321, -13662089, 8684155, -10532952), array(19443825, 11385320, 24468943, -9659068, -23919258, 2187569, -26263207, -6086921, 31316348, 14219878), ), array( array(-28594490, 1193785, 32245219, 11392485, 31092169, 15722801, 27146014, 6992409, 29126555, 9207390), array(32382935, 1110093, 18477781, 11028262, -27411763, -7548111, -4980517, 10843782, -7957600, -14435730), array(2814918, 7836403, 27519878, -7868156, -20894015, -11553689, -21494559, 8550130, 28346258, 1994730), ), array( array(-19578299, 8085545, -14000519, -3948622, 2785838, -16231307, -19516951, 7174894, 22628102, 8115180), array(-30405132, 955511, -11133838, -15078069, -32447087, -13278079, -25651578, 3317160, -9943017, 930272), array(-15303681, -6833769, 28856490, 1357446, 23421993, 1057177, 24091212, -1388970, -22765376, -10650715), ), array( array(-22751231, -5303997, -12907607, -12768866, -15811511, -7797053, -14839018, -16554220, -1867018, 8398970), array(-31969310, 2106403, -4736360, 1362501, 12813763, 16200670, 22981545, -6291273, 18009408, -15772772), array(-17220923, -9545221, -27784654, 14166835, 29815394, 7444469, 29551787, -3727419, 19288549, 1325865), ), array( array(15100157, -15835752, -23923978, -1005098, -26450192, 15509408, 12376730, -3479146, 33166107, -8042750), array(20909231, 13023121, -9209752, 16251778, -5778415, -8094914, 12412151, 10018715, 2213263, -13878373), array(32529814, -11074689, 30361439, -16689753, -9135940, 1513226, 22922121, 6382134, -5766928, 8371348), ), ), array( array( array(9923462, 11271500, 12616794, 3544722, -29998368, -1721626, 12891687, -8193132, -26442943, 10486144), array(-22597207, -7012665, 8587003, -8257861, 4084309, -12970062, 361726, 2610596, -23921530, -11455195), array(5408411, -1136691, -4969122, 10561668, 24145918, 14240566, 31319731, -4235541, 19985175, -3436086), ), array( array(-13994457, 16616821, 14549246, 3341099, 32155958, 13648976, -17577068, 8849297, 65030, 8370684), array(-8320926, -12049626, 31204563, 5839400, -20627288, -1057277, -19442942, 6922164, 12743482, -9800518), array(-2361371, 12678785, 28815050, 4759974, -23893047, 4884717, 23783145, 11038569, 18800704, 255233), ), array( array(-5269658, -1773886, 13957886, 7990715, 23132995, 728773, 13393847, 9066957, 19258688, -14753793), array(-2936654, -10827535, -10432089, 14516793, -3640786, 4372541, -31934921, 2209390, -1524053, 2055794), array(580882, 16705327, 5468415, -2683018, -30926419, -14696000, -7203346, -8994389, -30021019, 7394435), ), array( array(23838809, 1822728, -15738443, 15242727, 8318092, -3733104, -21672180, -3492205, -4821741, 14799921), array(13345610, 9759151, 3371034, -16137791, 16353039, 8577942, 31129804, 13496856, -9056018, 7402518), array(2286874, -4435931, -20042458, -2008336, -13696227, 5038122, 11006906, -15760352, 8205061, 1607563), ), array( array(14414086, -8002132, 3331830, -3208217, 22249151, -5594188, 18364661, -2906958, 30019587, -9029278), array(-27688051, 1585953, -10775053, 931069, -29120221, -11002319, -14410829, 12029093, 9944378, 8024), array(4368715, -3709630, 29874200, -15022983, -20230386, -11410704, -16114594, -999085, -8142388, 5640030), ), array( array(10299610, 13746483, 11661824, 16234854, 7630238, 5998374, 9809887, -16694564, 15219798, -14327783), array(27425505, -5719081, 3055006, 10660664, 23458024, 595578, -15398605, -1173195, -18342183, 9742717), array(6744077, 2427284, 26042789, 2720740, -847906, 1118974, 32324614, 7406442, 12420155, 1994844), ), array( array(14012521, -5024720, -18384453, -9578469, -26485342, -3936439, -13033478, -10909803, 24319929, -6446333), array(16412690, -4507367, 10772641, 15929391, -17068788, -4658621, 10555945, -10484049, -30102368, -4739048), array(22397382, -7767684, -9293161, -12792868, 17166287, -9755136, -27333065, 6199366, 21880021, -12250760), ), array( array(-4283307, 5368523, -31117018, 8163389, -30323063, 3209128, 16557151, 8890729, 8840445, 4957760), array(-15447727, 709327, -6919446, -10870178, -29777922, 6522332, -21720181, 12130072, -14796503, 5005757), array(-2114751, -14308128, 23019042, 15765735, -25269683, 6002752, 10183197, -13239326, -16395286, -2176112), ), ), array( array( array(-19025756, 1632005, 13466291, -7995100, -23640451, 16573537, -32013908, -3057104, 22208662, 2000468), array(3065073, -1412761, -25598674, -361432, -17683065, -5703415, -8164212, 11248527, -3691214, -7414184), array(10379208, -6045554, 8877319, 1473647, -29291284, -12507580, 16690915, 2553332, -3132688, 16400289), ), array( array(15716668, 1254266, -18472690, 7446274, -8448918, 6344164, -22097271, -7285580, 26894937, 9132066), array(24158887, 12938817, 11085297, -8177598, -28063478, -4457083, -30576463, 64452, -6817084, -2692882), array(13488534, 7794716, 22236231, 5989356, 25426474, -12578208, 2350710, -3418511, -4688006, 2364226), ), array( array(16335052, 9132434, 25640582, 6678888, 1725628, 8517937, -11807024, -11697457, 15445875, -7798101), array(29004207, -7867081, 28661402, -640412, -12794003, -7943086, 31863255, -4135540, -278050, -15759279), array(-6122061, -14866665, -28614905, 14569919, -10857999, -3591829, 10343412, -6976290, -29828287, -10815811), ), array( array(27081650, 3463984, 14099042, -4517604, 1616303, -6205604, 29542636, 15372179, 17293797, 960709), array(20263915, 11434237, -5765435, 11236810, 13505955, -10857102, -16111345, 6493122, -19384511, 7639714), array(-2830798, -14839232, 25403038, -8215196, -8317012, -16173699, 18006287, -16043750, 29994677, -15808121), ), array( array(9769828, 5202651, -24157398, -13631392, -28051003, -11561624, -24613141, -13860782, -31184575, 709464), array(12286395, 13076066, -21775189, -1176622, -25003198, 4057652, -32018128, -8890874, 16102007, 13205847), array(13733362, 5599946, 10557076, 3195751, -5557991, 8536970, -25540170, 8525972, 10151379, 10394400), ), array( array(4024660, -16137551, 22436262, 12276534, -9099015, -2686099, 19698229, 11743039, -33302334, 8934414), array(-15879800, -4525240, -8580747, -2934061, 14634845, -698278, -9449077, 3137094, -11536886, 11721158), array(17555939, -5013938, 8268606, 2331751, -22738815, 9761013, 9319229, 8835153, -9205489, -1280045), ), array( array(-461409, -7830014, 20614118, 16688288, -7514766, -4807119, 22300304, 505429, 6108462, -6183415), array(-5070281, 12367917, -30663534, 3234473, 32617080, -8422642, 29880583, -13483331, -26898490, -7867459), array(-31975283, 5726539, 26934134, 10237677, -3173717, -605053, 24199304, 3795095, 7592688, -14992079), ), array( array(21594432, -14964228, 17466408, -4077222, 32537084, 2739898, 6407723, 12018833, -28256052, 4298412), array(-20650503, -11961496, -27236275, 570498, 3767144, -1717540, 13891942, -1569194, 13717174, 10805743), array(-14676630, -15644296, 15287174, 11927123, 24177847, -8175568, -796431, 14860609, -26938930, -5863836), ), ), array( array( array(12962541, 5311799, -10060768, 11658280, 18855286, -7954201, 13286263, -12808704, -4381056, 9882022), array(18512079, 11319350, -20123124, 15090309, 18818594, 5271736, -22727904, 3666879, -23967430, -3299429), array(-6789020, -3146043, 16192429, 13241070, 15898607, -14206114, -10084880, -6661110, -2403099, 5276065), ), array( array(30169808, -5317648, 26306206, -11750859, 27814964, 7069267, 7152851, 3684982, 1449224, 13082861), array(10342826, 3098505, 2119311, 193222, 25702612, 12233820, 23697382, 15056736, -21016438, -8202000), array(-33150110, 3261608, 22745853, 7948688, 19370557, -15177665, -26171976, 6482814, -10300080, -11060101), ), array( array(32869458, -5408545, 25609743, 15678670, -10687769, -15471071, 26112421, 2521008, -22664288, 6904815), array(29506923, 4457497, 3377935, -9796444, -30510046, 12935080, 1561737, 3841096, -29003639, -6657642), array(10340844, -6630377, -18656632, -2278430, 12621151, -13339055, 30878497, -11824370, -25584551, 5181966), ), array( array(25940115, -12658025, 17324188, -10307374, -8671468, 15029094, 24396252, -16450922, -2322852, -12388574), array(-21765684, 9916823, -1300409, 4079498, -1028346, 11909559, 1782390, 12641087, 20603771, -6561742), array(-18882287, -11673380, 24849422, 11501709, 13161720, -4768874, 1925523, 11914390, 4662781, 7820689), ), array( array(12241050, -425982, 8132691, 9393934, 32846760, -1599620, 29749456, 12172924, 16136752, 15264020), array(-10349955, -14680563, -8211979, 2330220, -17662549, -14545780, 10658213, 6671822, 19012087, 3772772), array(3753511, -3421066, 10617074, 2028709, 14841030, -6721664, 28718732, -15762884, 20527771, 12988982), ), array( array(-14822485, -5797269, -3707987, 12689773, -898983, -10914866, -24183046, -10564943, 3299665, -12424953), array(-16777703, -15253301, -9642417, 4978983, 3308785, 8755439, 6943197, 6461331, -25583147, 8991218), array(-17226263, 1816362, -1673288, -6086439, 31783888, -8175991, -32948145, 7417950, -30242287, 1507265), ), array( array(29692663, 6829891, -10498800, 4334896, 20945975, -11906496, -28887608, 8209391, 14606362, -10647073), array(-3481570, 8707081, 32188102, 5672294, 22096700, 1711240, -33020695, 9761487, 4170404, -2085325), array(-11587470, 14855945, -4127778, -1531857, -26649089, 15084046, 22186522, 16002000, -14276837, -8400798), ), array( array(-4811456, 13761029, -31703877, -2483919, -3312471, 7869047, -7113572, -9620092, 13240845, 10965870), array(-7742563, -8256762, -14768334, -13656260, -23232383, 12387166, 4498947, 14147411, 29514390, 4302863), array(-13413405, -12407859, 20757302, -13801832, 14785143, 8976368, -5061276, -2144373, 17846988, -13971927), ), ), array( array( array(-2244452, -754728, -4597030, -1066309, -6247172, 1455299, -21647728, -9214789, -5222701, 12650267), array(-9906797, -16070310, 21134160, 12198166, -27064575, 708126, 387813, 13770293, -19134326, 10958663), array(22470984, 12369526, 23446014, -5441109, -21520802, -9698723, -11772496, -11574455, -25083830, 4271862), ), array( array(-25169565, -10053642, -19909332, 15361595, -5984358, 2159192, 75375, -4278529, -32526221, 8469673), array(15854970, 4148314, -8893890, 7259002, 11666551, 13824734, -30531198, 2697372, 24154791, -9460943), array(15446137, -15806644, 29759747, 14019369, 30811221, -9610191, -31582008, 12840104, 24913809, 9815020), ), array( array(-4709286, -5614269, -31841498, -12288893, -14443537, 10799414, -9103676, 13438769, 18735128, 9466238), array(11933045, 9281483, 5081055, -5183824, -2628162, -4905629, -7727821, -10896103, -22728655, 16199064), array(14576810, 379472, -26786533, -8317236, -29426508, -10812974, -102766, 1876699, 30801119, 2164795), ), array( array(15995086, 3199873, 13672555, 13712240, -19378835, -4647646, -13081610, -15496269, -13492807, 1268052), array(-10290614, -3659039, -3286592, 10948818, 23037027, 3794475, -3470338, -12600221, -17055369, 3565904), array(29210088, -9419337, -5919792, -4952785, 10834811, -13327726, -16512102, -10820713, -27162222, -14030531), ), array( array(-13161890, 15508588, 16663704, -8156150, -28349942, 9019123, -29183421, -3769423, 2244111, -14001979), array(-5152875, -3800936, -9306475, -6071583, 16243069, 14684434, -25673088, -16180800, 13491506, 4641841), array(10813417, 643330, -19188515, -728916, 30292062, -16600078, 27548447, -7721242, 14476989, -12767431), ), array( array(10292079, 9984945, 6481436, 8279905, -7251514, 7032743, 27282937, -1644259, -27912810, 12651324), array(-31185513, -813383, 22271204, 11835308, 10201545, 15351028, 17099662, 3988035, 21721536, -3148940), array(10202177, -6545839, -31373232, -9574638, -32150642, -8119683, -12906320, 3852694, 13216206, 14842320), ), array( array(-15815640, -10601066, -6538952, -7258995, -6984659, -6581778, -31500847, 13765824, -27434397, 9900184), array(14465505, -13833331, -32133984, -14738873, -27443187, 12990492, 33046193, 15796406, -7051866, -8040114), array(30924417, -8279620, 6359016, -12816335, 16508377, 9071735, -25488601, 15413635, 9524356, -7018878), ), array( array(12274201, -13175547, 32627641, -1785326, 6736625, 13267305, 5237659, -5109483, 15663516, 4035784), array(-2951309, 8903985, 17349946, 601635, -16432815, -4612556, -13732739, -15889334, -22258478, 4659091), array(-16916263, -4952973, -30393711, -15158821, 20774812, 15897498, 5736189, 15026997, -2178256, -13455585), ), ), array( array( array(-8858980, -2219056, 28571666, -10155518, -474467, -10105698, -3801496, 278095, 23440562, -290208), array(10226241, -5928702, 15139956, 120818, -14867693, 5218603, 32937275, 11551483, -16571960, -7442864), array(17932739, -12437276, -24039557, 10749060, 11316803, 7535897, 22503767, 5561594, -3646624, 3898661), ), array( array(7749907, -969567, -16339731, -16464, -25018111, 15122143, -1573531, 7152530, 21831162, 1245233), array(26958459, -14658026, 4314586, 8346991, -5677764, 11960072, -32589295, -620035, -30402091, -16716212), array(-12165896, 9166947, 33491384, 13673479, 29787085, 13096535, 6280834, 14587357, -22338025, 13987525), ), array( array(-24349909, 7778775, 21116000, 15572597, -4833266, -5357778, -4300898, -5124639, -7469781, -2858068), array(9681908, -6737123, -31951644, 13591838, -6883821, 386950, 31622781, 6439245, -14581012, 4091397), array(-8426427, 1470727, -28109679, -1596990, 3978627, -5123623, -19622683, 12092163, 29077877, -14741988), ), array( array(5269168, -6859726, -13230211, -8020715, 25932563, 1763552, -5606110, -5505881, -20017847, 2357889), array(32264008, -15407652, -5387735, -1160093, -2091322, -3946900, 23104804, -12869908, 5727338, 189038), array(14609123, -8954470, -6000566, -16622781, -14577387, -7743898, -26745169, 10942115, -25888931, -14884697), ), array( array(20513500, 5557931, -15604613, 7829531, 26413943, -2019404, -21378968, 7471781, 13913677, -5137875), array(-25574376, 11967826, 29233242, 12948236, -6754465, 4713227, -8940970, 14059180, 12878652, 8511905), array(-25656801, 3393631, -2955415, -7075526, -2250709, 9366908, -30223418, 6812974, 5568676, -3127656), ), array( array(11630004, 12144454, 2116339, 13606037, 27378885, 15676917, -17408753, -13504373, -14395196, 8070818), array(27117696, -10007378, -31282771, -5570088, 1127282, 12772488, -29845906, 10483306, -11552749, -1028714), array(10637467, -5688064, 5674781, 1072708, -26343588, -6982302, -1683975, 9177853, -27493162, 15431203), ), array( array(20525145, 10892566, -12742472, 12779443, -29493034, 16150075, -28240519, 14943142, -15056790, -7935931), array(-30024462, 5626926, -551567, -9981087, 753598, 11981191, 25244767, -3239766, -3356550, 9594024), array(-23752644, 2636870, -5163910, -10103818, 585134, 7877383, 11345683, -6492290, 13352335, -10977084), ), array( array(-1931799, -5407458, 3304649, -12884869, 17015806, -4877091, -29783850, -7752482, -13215537, -319204), array(20239939, 6607058, 6203985, 3483793, -18386976, -779229, -20723742, 15077870, -22750759, 14523817), array(27406042, -6041657, 27423596, -4497394, 4996214, 10002360, -28842031, -4545494, -30172742, -4805667), ), ), array( array( array(11374242, 12660715, 17861383, -12540833, 10935568, 1099227, -13886076, -9091740, -27727044, 11358504), array(-12730809, 10311867, 1510375, 10778093, -2119455, -9145702, 32676003, 11149336, -26123651, 4985768), array(-19096303, 341147, -6197485, -239033, 15756973, -8796662, -983043, 13794114, -19414307, -15621255), ), array( array(6490081, 11940286, 25495923, -7726360, 8668373, -8751316, 3367603, 6970005, -1691065, -9004790), array(1656497, 13457317, 15370807, 6364910, 13605745, 8362338, -19174622, -5475723, -16796596, -5031438), array(-22273315, -13524424, -64685, -4334223, -18605636, -10921968, -20571065, -7007978, -99853, -10237333), ), array( array(17747465, 10039260, 19368299, -4050591, -20630635, -16041286, 31992683, -15857976, -29260363, -5511971), array(31932027, -4986141, -19612382, 16366580, 22023614, 88450, 11371999, -3744247, 4882242, -10626905), array(29796507, 37186, 19818052, 10115756, -11829032, 3352736, 18551198, 3272828, -5190932, -4162409), ), array( array(12501286, 4044383, -8612957, -13392385, -32430052, 5136599, -19230378, -3529697, 330070, -3659409), array(6384877, 2899513, 17807477, 7663917, -2358888, 12363165, 25366522, -8573892, -271295, 12071499), array(-8365515, -4042521, 25133448, -4517355, -6211027, 2265927, -32769618, 1936675, -5159697, 3829363), ), array( array(28425966, -5835433, -577090, -4697198, -14217555, 6870930, 7921550, -6567787, 26333140, 14267664), array(-11067219, 11871231, 27385719, -10559544, -4585914, -11189312, 10004786, -8709488, -21761224, 8930324), array(-21197785, -16396035, 25654216, -1725397, 12282012, 11008919, 1541940, 4757911, -26491501, -16408940), ), array( array(13537262, -7759490, -20604840, 10961927, -5922820, -13218065, -13156584, 6217254, -15943699, 13814990), array(-17422573, 15157790, 18705543, 29619, 24409717, -260476, 27361681, 9257833, -1956526, -1776914), array(-25045300, -10191966, 15366585, 15166509, -13105086, 8423556, -29171540, 12361135, -18685978, 4578290), ), array( array(24579768, 3711570, 1342322, -11180126, -27005135, 14124956, -22544529, 14074919, 21964432, 8235257), array(-6528613, -2411497, 9442966, -5925588, 12025640, -1487420, -2981514, -1669206, 13006806, 2355433), array(-16304899, -13605259, -6632427, -5142349, 16974359, -10911083, 27202044, 1719366, 1141648, -12796236), ), array( array(-12863944, -13219986, -8318266, -11018091, -6810145, -4843894, 13475066, -3133972, 32674895, 13715045), array(11423335, -5468059, 32344216, 8962751, 24989809, 9241752, -13265253, 16086212, -28740881, -15642093), array(-1409668, 12530728, -6368726, 10847387, 19531186, -14132160, -11709148, 7791794, -27245943, 4383347), ), ), array( array( array(-28970898, 5271447, -1266009, -9736989, -12455236, 16732599, -4862407, -4906449, 27193557, 6245191), array(-15193956, 5362278, -1783893, 2695834, 4960227, 12840725, 23061898, 3260492, 22510453, 8577507), array(-12632451, 11257346, -32692994, 13548177, -721004, 10879011, 31168030, 13952092, -29571492, -3635906), ), array( array(3877321, -9572739, 32416692, 5405324, -11004407, -13656635, 3759769, 11935320, 5611860, 8164018), array(-16275802, 14667797, 15906460, 12155291, -22111149, -9039718, 32003002, -8832289, 5773085, -8422109), array(-23788118, -8254300, 1950875, 8937633, 18686727, 16459170, -905725, 12376320, 31632953, 190926), ), array( array(-24593607, -16138885, -8423991, 13378746, 14162407, 6901328, -8288749, 4508564, -25341555, -3627528), array(8884438, -5884009, 6023974, 10104341, -6881569, -4941533, 18722941, -14786005, -1672488, 827625), array(-32720583, -16289296, -32503547, 7101210, 13354605, 2659080, -1800575, -14108036, -24878478, 1541286), ), array( array(2901347, -1117687, 3880376, -10059388, -17620940, -3612781, -21802117, -3567481, 20456845, -1885033), array(27019610, 12299467, -13658288, -1603234, -12861660, -4861471, -19540150, -5016058, 29439641, 15138866), array(21536104, -6626420, -32447818, -10690208, -22408077, 5175814, -5420040, -16361163, 7779328, 109896), ), array( array(30279744, 14648750, -8044871, 6425558, 13639621, -743509, 28698390, 12180118, 23177719, -554075), array(26572847, 3405927, -31701700, 12890905, -19265668, 5335866, -6493768, 2378492, 4439158, -13279347), array(-22716706, 3489070, -9225266, -332753, 18875722, -1140095, 14819434, -12731527, -17717757, -5461437), ), array( array(-5056483, 16566551, 15953661, 3767752, -10436499, 15627060, -820954, 2177225, 8550082, -15114165), array(-18473302, 16596775, -381660, 15663611, 22860960, 15585581, -27844109, -3582739, -23260460, -8428588), array(-32480551, 15707275, -8205912, -5652081, 29464558, 2713815, -22725137, 15860482, -21902570, 1494193), ), array( array(-19562091, -14087393, -25583872, -9299552, 13127842, 759709, 21923482, 16529112, 8742704, 12967017), array(-28464899, 1553205, 32536856, -10473729, -24691605, -406174, -8914625, -2933896, -29903758, 15553883), array(21877909, 3230008, 9881174, 10539357, -4797115, 2841332, 11543572, 14513274, 19375923, -12647961), ), array( array(8832269, -14495485, 13253511, 5137575, 5037871, 4078777, 24880818, -6222716, 2862653, 9455043), array(29306751, 5123106, 20245049, -14149889, 9592566, 8447059, -2077124, -2990080, 15511449, 4789663), array(-20679756, 7004547, 8824831, -9434977, -4045704, -3750736, -5754762, 108893, 23513200, 16652362), ), ), array( array( array(-33256173, 4144782, -4476029, -6579123, 10770039, -7155542, -6650416, -12936300, -18319198, 10212860), array(2756081, 8598110, 7383731, -6859892, 22312759, -1105012, 21179801, 2600940, -9988298, -12506466), array(-24645692, 13317462, -30449259, -15653928, 21365574, -10869657, 11344424, 864440, -2499677, -16710063), ), array( array(-26432803, 6148329, -17184412, -14474154, 18782929, -275997, -22561534, 211300, 2719757, 4940997), array(-1323882, 3911313, -6948744, 14759765, -30027150, 7851207, 21690126, 8518463, 26699843, 5276295), array(-13149873, -6429067, 9396249, 365013, 24703301, -10488939, 1321586, 149635, -15452774, 7159369), ), array( array(9987780, -3404759, 17507962, 9505530, 9731535, -2165514, 22356009, 8312176, 22477218, -8403385), array(18155857, -16504990, 19744716, 9006923, 15154154, -10538976, 24256460, -4864995, -22548173, 9334109), array(2986088, -4911893, 10776628, -3473844, 10620590, -7083203, -21413845, 14253545, -22587149, 536906), ), array( array(4377756, 8115836, 24567078, 15495314, 11625074, 13064599, 7390551, 10589625, 10838060, -15420424), array(-19342404, 867880, 9277171, -3218459, -14431572, -1986443, 19295826, -15796950, 6378260, 699185), array(7895026, 4057113, -7081772, -13077756, -17886831, -323126, -716039, 15693155, -5045064, -13373962), ), array( array(-7737563, -5869402, -14566319, -7406919, 11385654, 13201616, 31730678, -10962840, -3918636, -9669325), array(10188286, -15770834, -7336361, 13427543, 22223443, 14896287, 30743455, 7116568, -21786507, 5427593), array(696102, 13206899, 27047647, -10632082, 15285305, -9853179, 10798490, -4578720, 19236243, 12477404), ), array( array(-11229439, 11243796, -17054270, -8040865, -788228, -8167967, -3897669, 11180504, -23169516, 7733644), array(17800790, -14036179, -27000429, -11766671, 23887827, 3149671, 23466177, -10538171, 10322027, 15313801), array(26246234, 11968874, 32263343, -5468728, 6830755, -13323031, -15794704, -101982, -24449242, 10890804), ), array( array(-31365647, 10271363, -12660625, -6267268, 16690207, -13062544, -14982212, 16484931, 25180797, -5334884), array(-586574, 10376444, -32586414, -11286356, 19801893, 10997610, 2276632, 9482883, 316878, 13820577), array(-9882808, -4510367, -2115506, 16457136, -11100081, 11674996, 30756178, -7515054, 30696930, -3712849), ), array( array(32988917, -9603412, 12499366, 7910787, -10617257, -11931514, -7342816, -9985397, -32349517, 7392473), array(-8855661, 15927861, 9866406, -3649411, -2396914, -16655781, -30409476, -9134995, 25112947, -2926644), array(-2504044, -436966, 25621774, -5678772, 15085042, -5479877, -24884878, -13526194, 5537438, -13914319), ), ), array( array( array(-11225584, 2320285, -9584280, 10149187, -33444663, 5808648, -14876251, -1729667, 31234590, 6090599), array(-9633316, 116426, 26083934, 2897444, -6364437, -2688086, 609721, 15878753, -6970405, -9034768), array(-27757857, 247744, -15194774, -9002551, 23288161, -10011936, -23869595, 6503646, 20650474, 1804084), ), array( array(-27589786, 15456424, 8972517, 8469608, 15640622, 4439847, 3121995, -10329713, 27842616, -202328), array(-15306973, 2839644, 22530074, 10026331, 4602058, 5048462, 28248656, 5031932, -11375082, 12714369), array(20807691, -7270825, 29286141, 11421711, -27876523, -13868230, -21227475, 1035546, -19733229, 12796920), ), array( array(12076899, -14301286, -8785001, -11848922, -25012791, 16400684, -17591495, -12899438, 3480665, -15182815), array(-32361549, 5457597, 28548107, 7833186, 7303070, -11953545, -24363064, -15921875, -33374054, 2771025), array(-21389266, 421932, 26597266, 6860826, 22486084, -6737172, -17137485, -4210226, -24552282, 15673397), ), array( array(-20184622, 2338216, 19788685, -9620956, -4001265, -8740893, -20271184, 4733254, 3727144, -12934448), array(6120119, 814863, -11794402, -622716, 6812205, -15747771, 2019594, 7975683, 31123697, -10958981), array(30069250, -11435332, 30434654, 2958439, 18399564, -976289, 12296869, 9204260, -16432438, 9648165), ), array( array(32705432, -1550977, 30705658, 7451065, -11805606, 9631813, 3305266, 5248604, -26008332, -11377501), array(17219865, 2375039, -31570947, -5575615, -19459679, 9219903, 294711, 15298639, 2662509, -16297073), array(-1172927, -7558695, -4366770, -4287744, -21346413, -8434326, 32087529, -1222777, 32247248, -14389861), ), array( array(14312628, 1221556, 17395390, -8700143, -4945741, -8684635, -28197744, -9637817, -16027623, -13378845), array(-1428825, -9678990, -9235681, 6549687, -7383069, -468664, 23046502, 9803137, 17597934, 2346211), array(18510800, 15337574, 26171504, 981392, -22241552, 7827556, -23491134, -11323352, 3059833, -11782870), ), array( array(10141598, 6082907, 17829293, -1947643, 9830092, 13613136, -25556636, -5544586, -33502212, 3592096), array(33114168, -15889352, -26525686, -13343397, 33076705, 8716171, 1151462, 1521897, -982665, -6837803), array(-32939165, -4255815, 23947181, -324178, -33072974, -12305637, -16637686, 3891704, 26353178, 693168), ), array( array(30374239, 1595580, -16884039, 13186931, 4600344, 406904, 9585294, -400668, 31375464, 14369965), array(-14370654, -7772529, 1510301, 6434173, -18784789, -6262728, 32732230, -13108839, 17901441, 16011505), array(18171223, -11934626, -12500402, 15197122, -11038147, -15230035, -19172240, -16046376, 8764035, 12309598), ), ), array( array( array(5975908, -5243188, -19459362, -9681747, -11541277, 14015782, -23665757, 1228319, 17544096, -10593782), array(5811932, -1715293, 3442887, -2269310, -18367348, -8359541, -18044043, -15410127, -5565381, 12348900), array(-31399660, 11407555, 25755363, 6891399, -3256938, 14872274, -24849353, 8141295, -10632534, -585479), ), array( array(-12675304, 694026, -5076145, 13300344, 14015258, -14451394, -9698672, -11329050, 30944593, 1130208), array(8247766, -6710942, -26562381, -7709309, -14401939, -14648910, 4652152, 2488540, 23550156, -271232), array(17294316, -3788438, 7026748, 15626851, 22990044, 113481, 2267737, -5908146, -408818, -137719), ), array( array(16091085, -16253926, 18599252, 7340678, 2137637, -1221657, -3364161, 14550936, 3260525, -7166271), array(-4910104, -13332887, 18550887, 10864893, -16459325, -7291596, -23028869, -13204905, -12748722, 2701326), array(-8574695, 16099415, 4629974, -16340524, -20786213, -6005432, -10018363, 9276971, 11329923, 1862132), ), array( array(14763076, -15903608, -30918270, 3689867, 3511892, 10313526, -21951088, 12219231, -9037963, -940300), array(8894987, -3446094, 6150753, 3013931, 301220, 15693451, -31981216, -2909717, -15438168, 11595570), array(15214962, 3537601, -26238722, -14058872, 4418657, -15230761, 13947276, 10730794, -13489462, -4363670), ), array( array(-2538306, 7682793, 32759013, 263109, -29984731, -7955452, -22332124, -10188635, 977108, 699994), array(-12466472, 4195084, -9211532, 550904, -15565337, 12917920, 19118110, -439841, -30534533, -14337913), array(31788461, -14507657, 4799989, 7372237, 8808585, -14747943, 9408237, -10051775, 12493932, -5409317), ), array( array(-25680606, 5260744, -19235809, -6284470, -3695942, 16566087, 27218280, 2607121, 29375955, 6024730), array(842132, -2794693, -4763381, -8722815, 26332018, -12405641, 11831880, 6985184, -9940361, 2854096), array(-4847262, -7969331, 2516242, -5847713, 9695691, -7221186, 16512645, 960770, 12121869, 16648078), ), array( array(-15218652, 14667096, -13336229, 2013717, 30598287, -464137, -31504922, -7882064, 20237806, 2838411), array(-19288047, 4453152, 15298546, -16178388, 22115043, -15972604, 12544294, -13470457, 1068881, -12499905), array(-9558883, -16518835, 33238498, 13506958, 30505848, -1114596, -8486907, -2630053, 12521378, 4845654), ), array( array(-28198521, 10744108, -2958380, 10199664, 7759311, -13088600, 3409348, -873400, -6482306, -12885870), array(-23561822, 6230156, -20382013, 10655314, -24040585, -11621172, 10477734, -1240216, -3113227, 13974498), array(12966261, 15550616, -32038948, -1615346, 21025980, -629444, 5642325, 7188737, 18895762, 12629579), ), ), array( array( array(14741879, -14946887, 22177208, -11721237, 1279741, 8058600, 11758140, 789443, 32195181, 3895677), array(10758205, 15755439, -4509950, 9243698, -4879422, 6879879, -2204575, -3566119, -8982069, 4429647), array(-2453894, 15725973, -20436342, -10410672, -5803908, -11040220, -7135870, -11642895, 18047436, -15281743), ), array( array(-25173001, -11307165, 29759956, 11776784, -22262383, -15820455, 10993114, -12850837, -17620701, -9408468), array(21987233, 700364, -24505048, 14972008, -7774265, -5718395, 32155026, 2581431, -29958985, 8773375), array(-25568350, 454463, -13211935, 16126715, 25240068, 8594567, 20656846, 12017935, -7874389, -13920155), ), array( array(6028182, 6263078, -31011806, -11301710, -818919, 2461772, -31841174, -5468042, -1721788, -2776725), array(-12278994, 16624277, 987579, -5922598, 32908203, 1248608, 7719845, -4166698, 28408820, 6816612), array(-10358094, -8237829, 19549651, -12169222, 22082623, 16147817, 20613181, 13982702, -10339570, 5067943), ), array( array(-30505967, -3821767, 12074681, 13582412, -19877972, 2443951, -19719286, 12746132, 5331210, -10105944), array(30528811, 3601899, -1957090, 4619785, -27361822, -15436388, 24180793, -12570394, 27679908, -1648928), array(9402404, -13957065, 32834043, 10838634, -26580150, -13237195, 26653274, -8685565, 22611444, -12715406), ), array( array(22190590, 1118029, 22736441, 15130463, -30460692, -5991321, 19189625, -4648942, 4854859, 6622139), array(-8310738, -2953450, -8262579, -3388049, -10401731, -271929, 13424426, -3567227, 26404409, 13001963), array(-31241838, -15415700, -2994250, 8939346, 11562230, -12840670, -26064365, -11621720, -15405155, 11020693), ), array( array(1866042, -7949489, -7898649, -10301010, 12483315, 13477547, 3175636, -12424163, 28761762, 1406734), array(-448555, -1777666, 13018551, 3194501, -9580420, -11161737, 24760585, -4347088, 25577411, -13378680), array(-24290378, 4759345, -690653, -1852816, 2066747, 10693769, -29595790, 9884936, -9368926, 4745410), ), array( array(-9141284, 6049714, -19531061, -4341411, -31260798, 9944276, -15462008, -11311852, 10931924, -11931931), array(-16561513, 14112680, -8012645, 4817318, -8040464, -11414606, -22853429, 10856641, -20470770, 13434654), array(22759489, -10073434, -16766264, -1871422, 13637442, -10168091, 1765144, -12654326, 28445307, -5364710), ), array( array(29875063, 12493613, 2795536, -3786330, 1710620, 15181182, -10195717, -8788675, 9074234, 1167180), array(-26205683, 11014233, -9842651, -2635485, -26908120, 7532294, -18716888, -9535498, 3843903, 9367684), array(-10969595, -6403711, 9591134, 9582310, 11349256, 108879, 16235123, 8601684, -139197, 4242895), ), ), array( array( array(22092954, -13191123, -2042793, -11968512, 32186753, -11517388, -6574341, 2470660, -27417366, 16625501), array(-11057722, 3042016, 13770083, -9257922, 584236, -544855, -7770857, 2602725, -27351616, 14247413), array(6314175, -10264892, -32772502, 15957557, -10157730, 168750, -8618807, 14290061, 27108877, -1180880), ), array( array(-8586597, -7170966, 13241782, 10960156, -32991015, -13794596, 33547976, -11058889, -27148451, 981874), array(22833440, 9293594, -32649448, -13618667, -9136966, 14756819, -22928859, -13970780, -10479804, -16197962), array(-7768587, 3326786, -28111797, 10783824, 19178761, 14905060, 22680049, 13906969, -15933690, 3797899), ), array( array(21721356, -4212746, -12206123, 9310182, -3882239, -13653110, 23740224, -2709232, 20491983, -8042152), array(9209270, -15135055, -13256557, -6167798, -731016, 15289673, 25947805, 15286587, 30997318, -6703063), array(7392032, 16618386, 23946583, -8039892, -13265164, -1533858, -14197445, -2321576, 17649998, -250080), ), array( array(-9301088, -14193827, 30609526, -3049543, -25175069, -1283752, -15241566, -9525724, -2233253, 7662146), array(-17558673, 1763594, -33114336, 15908610, -30040870, -12174295, 7335080, -8472199, -3174674, 3440183), array(-19889700, -5977008, -24111293, -9688870, 10799743, -16571957, 40450, -4431835, 4862400, 1133), ), array( array(-32856209, -7873957, -5422389, 14860950, -16319031, 7956142, 7258061, 311861, -30594991, -7379421), array(-3773428, -1565936, 28985340, 7499440, 24445838, 9325937, 29727763, 16527196, 18278453, 15405622), array(-4381906, 8508652, -19898366, -3674424, -5984453, 15149970, -13313598, 843523, -21875062, 13626197), ), array( array(2281448, -13487055, -10915418, -2609910, 1879358, 16164207, -10783882, 3953792, 13340839, 15928663), array(31727126, -7179855, -18437503, -8283652, 2875793, -16390330, -25269894, -7014826, -23452306, 5964753), array(4100420, -5959452, -17179337, 6017714, -18705837, 12227141, -26684835, 11344144, 2538215, -7570755), ), array( array(-9433605, 6123113, 11159803, -2156608, 30016280, 14966241, -20474983, 1485421, -629256, -15958862), array(-26804558, 4260919, 11851389, 9658551, -32017107, 16367492, -20205425, -13191288, 11659922, -11115118), array(26180396, 10015009, -30844224, -8581293, 5418197, 9480663, 2231568, -10170080, 33100372, -1306171), ), array( array(15121113, -5201871, -10389905, 15427821, -27509937, -15992507, 21670947, 4486675, -5931810, -14466380), array(16166486, -9483733, -11104130, 6023908, -31926798, -1364923, 2340060, -16254968, -10735770, -10039824), array(28042865, -3557089, -12126526, 12259706, -3717498, -6945899, 6766453, -8689599, 18036436, 5803270), ), ), array( array( array(-817581, 6763912, 11803561, 1585585, 10958447, -2671165, 23855391, 4598332, -6159431, -14117438), array(-31031306, -14256194, 17332029, -2383520, 31312682, -5967183, 696309, 50292, -20095739, 11763584), array(-594563, -2514283, -32234153, 12643980, 12650761, 14811489, 665117, -12613632, -19773211, -10713562), ), array( array(30464590, -11262872, -4127476, -12734478, 19835327, -7105613, -24396175, 2075773, -17020157, 992471), array(18357185, -6994433, 7766382, 16342475, -29324918, 411174, 14578841, 8080033, -11574335, -10601610), array(19598397, 10334610, 12555054, 2555664, 18821899, -10339780, 21873263, 16014234, 26224780, 16452269), ), array( array(-30223925, 5145196, 5944548, 16385966, 3976735, 2009897, -11377804, -7618186, -20533829, 3698650), array(14187449, 3448569, -10636236, -10810935, -22663880, -3433596, 7268410, -10890444, 27394301, 12015369), array(19695761, 16087646, 28032085, 12999827, 6817792, 11427614, 20244189, -1312777, -13259127, -3402461), ), array( array(30860103, 12735208, -1888245, -4699734, -16974906, 2256940, -8166013, 12298312, -8550524, -10393462), array(-5719826, -11245325, -1910649, 15569035, 26642876, -7587760, -5789354, -15118654, -4976164, 12651793), array(-2848395, 9953421, 11531313, -5282879, 26895123, -12697089, -13118820, -16517902, 9768698, -2533218), ), array( array(-24719459, 1894651, -287698, -4704085, 15348719, -8156530, 32767513, 12765450, 4940095, 10678226), array(18860224, 15980149, -18987240, -1562570, -26233012, -11071856, -7843882, 13944024, -24372348, 16582019), array(-15504260, 4970268, -29893044, 4175593, -20993212, -2199756, -11704054, 15444560, -11003761, 7989037), ), array( array(31490452, 5568061, -2412803, 2182383, -32336847, 4531686, -32078269, 6200206, -19686113, -14800171), array(-17308668, -15879940, -31522777, -2831, -32887382, 16375549, 8680158, -16371713, 28550068, -6857132), array(-28126887, -5688091, 16837845, -1820458, -6850681, 12700016, -30039981, 4364038, 1155602, 5988841), ), array( array(21890435, -13272907, -12624011, 12154349, -7831873, 15300496, 23148983, -4470481, 24618407, 8283181), array(-33136107, -10512751, 9975416, 6841041, -31559793, 16356536, 3070187, -7025928, 1466169, 10740210), array(-1509399, -15488185, -13503385, -10655916, 32799044, 909394, -13938903, -5779719, -32164649, -15327040), ), array( array(3960823, -14267803, -28026090, -15918051, -19404858, 13146868, 15567327, 951507, -3260321, -573935), array(24740841, 5052253, -30094131, 8961361, 25877428, 6165135, -24368180, 14397372, -7380369, -6144105), array(-28888365, 3510803, -28103278, -1158478, -11238128, -10631454, -15441463, -14453128, -1625486, -6494814), ), ), array( array( array(793299, -9230478, 8836302, -6235707, -27360908, -2369593, 33152843, -4885251, -9906200, -621852), array(5666233, 525582, 20782575, -8038419, -24538499, 14657740, 16099374, 1468826, -6171428, -15186581), array(-4859255, -3779343, -2917758, -6748019, 7778750, 11688288, -30404353, -9871238, -1558923, -9863646), ), array( array(10896332, -7719704, 824275, 472601, -19460308, 3009587, 25248958, 14783338, -30581476, -15757844), array(10566929, 12612572, -31944212, 11118703, -12633376, 12362879, 21752402, 8822496, 24003793, 14264025), array(27713862, -7355973, -11008240, 9227530, 27050101, 2504721, 23886875, -13117525, 13958495, -5732453), ), array( array(-23481610, 4867226, -27247128, 3900521, 29838369, -8212291, -31889399, -10041781, 7340521, -15410068), array(4646514, -8011124, -22766023, -11532654, 23184553, 8566613, 31366726, -1381061, -15066784, -10375192), array(-17270517, 12723032, -16993061, 14878794, 21619651, -6197576, 27584817, 3093888, -8843694, 3849921), ), array( array(-9064912, 2103172, 25561640, -15125738, -5239824, 9582958, 32477045, -9017955, 5002294, -15550259), array(-12057553, -11177906, 21115585, -13365155, 8808712, -12030708, 16489530, 13378448, -25845716, 12741426), array(-5946367, 10645103, -30911586, 15390284, -3286982, -7118677, 24306472, 15852464, 28834118, -7646072), ), array( array(-17335748, -9107057, -24531279, 9434953, -8472084, -583362, -13090771, 455841, 20461858, 5491305), array(13669248, -16095482, -12481974, -10203039, -14569770, -11893198, -24995986, 11293807, -28588204, -9421832), array(28497928, 6272777, -33022994, 14470570, 8906179, -1225630, 18504674, -14165166, 29867745, -8795943), ), array( array(-16207023, 13517196, -27799630, -13697798, 24009064, -6373891, -6367600, -13175392, 22853429, -4012011), array(24191378, 16712145, -13931797, 15217831, 14542237, 1646131, 18603514, -11037887, 12876623, -2112447), array(17902668, 4518229, -411702, -2829247, 26878217, 5258055, -12860753, 608397, 16031844, 3723494), ), array( array(-28632773, 12763728, -20446446, 7577504, 33001348, -13017745, 17558842, -7872890, 23896954, -4314245), array(-20005381, -12011952, 31520464, 605201, 2543521, 5991821, -2945064, 7229064, -9919646, -8826859), array(28816045, 298879, -28165016, -15920938, 19000928, -1665890, -12680833, -2949325, -18051778, -2082915), ), array( array(16000882, -344896, 3493092, -11447198, -29504595, -13159789, 12577740, 16041268, -19715240, 7847707), array(10151868, 10572098, 27312476, 7922682, 14825339, 4723128, -32855931, -6519018, -10020567, 3852848), array(-11430470, 15697596, -21121557, -4420647, 5386314, 15063598, 16514493, -15932110, 29330899, -15076224), ), ), array( array( array(-25499735, -4378794, -15222908, -6901211, 16615731, 2051784, 3303702, 15490, -27548796, 12314391), array(15683520, -6003043, 18109120, -9980648, 15337968, -5997823, -16717435, 15921866, 16103996, -3731215), array(-23169824, -10781249, 13588192, -1628807, -3798557, -1074929, -19273607, 5402699, -29815713, -9841101), ), array( array(23190676, 2384583, -32714340, 3462154, -29903655, -1529132, -11266856, 8911517, -25205859, 2739713), array(21374101, -3554250, -33524649, 9874411, 15377179, 11831242, -33529904, 6134907, 4931255, 11987849), array(-7732, -2978858, -16223486, 7277597, 105524, -322051, -31480539, 13861388, -30076310, 10117930), ), array( array(-29501170, -10744872, -26163768, 13051539, -25625564, 5089643, -6325503, 6704079, 12890019, 15728940), array(-21972360, -11771379, -951059, -4418840, 14704840, 2695116, 903376, -10428139, 12885167, 8311031), array(-17516482, 5352194, 10384213, -13811658, 7506451, 13453191, 26423267, 4384730, 1888765, -5435404), ), array( array(-25817338, -3107312, -13494599, -3182506, 30896459, -13921729, -32251644, -12707869, -19464434, -3340243), array(-23607977, -2665774, -526091, 4651136, 5765089, 4618330, 6092245, 14845197, 17151279, -9854116), array(-24830458, -12733720, -15165978, 10367250, -29530908, -265356, 22825805, -7087279, -16866484, 16176525), ), array( array(-23583256, 6564961, 20063689, 3798228, -4740178, 7359225, 2006182, -10363426, -28746253, -10197509), array(-10626600, -4486402, -13320562, -5125317, 3432136, -6393229, 23632037, -1940610, 32808310, 1099883), array(15030977, 5768825, -27451236, -2887299, -6427378, -15361371, -15277896, -6809350, 2051441, -15225865), ), array( array(-3362323, -7239372, 7517890, 9824992, 23555850, 295369, 5148398, -14154188, -22686354, 16633660), array(4577086, -16752288, 13249841, -15304328, 19958763, -14537274, 18559670, -10759549, 8402478, -9864273), array(-28406330, -1051581, -26790155, -907698, -17212414, -11030789, 9453451, -14980072, 17983010, 9967138), ), array( array(-25762494, 6524722, 26585488, 9969270, 24709298, 1220360, -1677990, 7806337, 17507396, 3651560), array(-10420457, -4118111, 14584639, 15971087, -15768321, 8861010, 26556809, -5574557, -18553322, -11357135), array(2839101, 14284142, 4029895, 3472686, 14402957, 12689363, -26642121, 8459447, -5605463, -7621941), ), array( array(-4839289, -3535444, 9744961, 2871048, 25113978, 3187018, -25110813, -849066, 17258084, -7977739), array(18164541, -10595176, -17154882, -1542417, 19237078, -9745295, 23357533, -15217008, 26908270, 12150756), array(-30264870, -7647865, 5112249, -7036672, -1499807, -6974257, 43168, -5537701, -32302074, 16215819), ), ), array( array( array(-6898905, 9824394, -12304779, -4401089, -31397141, -6276835, 32574489, 12532905, -7503072, -8675347), array(-27343522, -16515468, -27151524, -10722951, 946346, 16291093, 254968, 7168080, 21676107, -1943028), array(21260961, -8424752, -16831886, -11920822, -23677961, 3968121, -3651949, -6215466, -3556191, -7913075), ), array( array(16544754, 13250366, -16804428, 15546242, -4583003, 12757258, -2462308, -8680336, -18907032, -9662799), array(-2415239, -15577728, 18312303, 4964443, -15272530, -12653564, 26820651, 16690659, 25459437, -4564609), array(-25144690, 11425020, 28423002, -11020557, -6144921, -15826224, 9142795, -2391602, -6432418, -1644817), ), array( array(-23104652, 6253476, 16964147, -3768872, -25113972, -12296437, -27457225, -16344658, 6335692, 7249989), array(-30333227, 13979675, 7503222, -12368314, -11956721, -4621693, -30272269, 2682242, 25993170, -12478523), array(4364628, 5930691, 32304656, -10044554, -8054781, 15091131, 22857016, -10598955, 31820368, 15075278), ), array( array(31879134, -8918693, 17258761, 90626, -8041836, -4917709, 24162788, -9650886, -17970238, 12833045), array(19073683, 14851414, -24403169, -11860168, 7625278, 11091125, -19619190, 2074449, -9413939, 14905377), array(24483667, -11935567, -2518866, -11547418, -1553130, 15355506, -25282080, 9253129, 27628530, -7555480), ), array( array(17597607, 8340603, 19355617, 552187, 26198470, -3176583, 4593324, -9157582, -14110875, 15297016), array(510886, 14337390, -31785257, 16638632, 6328095, 2713355, -20217417, -11864220, 8683221, 2921426), array(18606791, 11874196, 27155355, -5281482, -24031742, 6265446, -25178240, -1278924, 4674690, 13890525), ), array( array(13609624, 13069022, -27372361, -13055908, 24360586, 9592974, 14977157, 9835105, 4389687, 288396), array(9922506, -519394, 13613107, 5883594, -18758345, -434263, -12304062, 8317628, 23388070, 16052080), array(12720016, 11937594, -31970060, -5028689, 26900120, 8561328, -20155687, -11632979, -14754271, -10812892), ), array( array(15961858, 14150409, 26716931, -665832, -22794328, 13603569, 11829573, 7467844, -28822128, 929275), array(11038231, -11582396, -27310482, -7316562, -10498527, -16307831, -23479533, -9371869, -21393143, 2465074), array(20017163, -4323226, 27915242, 1529148, 12396362, 15675764, 13817261, -9658066, 2463391, -4622140), ), array( array(-16358878, -12663911, -12065183, 4996454, -1256422, 1073572, 9583558, 12851107, 4003896, 12673717), array(-1731589, -15155870, -3262930, 16143082, 19294135, 13385325, 14741514, -9103726, 7903886, 2348101), array(24536016, -16515207, 12715592, -3862155, 1511293, 10047386, -3842346, -7129159, -28377538, 10048127), ), ), array( array( array(-12622226, -6204820, 30718825, 2591312, -10617028, 12192840, 18873298, -7297090, -32297756, 15221632), array(-26478122, -11103864, 11546244, -1852483, 9180880, 7656409, -21343950, 2095755, 29769758, 6593415), array(-31994208, -2907461, 4176912, 3264766, 12538965, -868111, 26312345, -6118678, 30958054, 8292160), ), array( array(31429822, -13959116, 29173532, 15632448, 12174511, -2760094, 32808831, 3977186, 26143136, -3148876), array(22648901, 1402143, -22799984, 13746059, 7936347, 365344, -8668633, -1674433, -3758243, -2304625), array(-15491917, 8012313, -2514730, -12702462, -23965846, -10254029, -1612713, -1535569, -16664475, 8194478), ), array( array(27338066, -7507420, -7414224, 10140405, -19026427, -6589889, 27277191, 8855376, 28572286, 3005164), array(26287124, 4821776, 25476601, -4145903, -3764513, -15788984, -18008582, 1182479, -26094821, -13079595), array(-7171154, 3178080, 23970071, 6201893, -17195577, -4489192, -21876275, -13982627, 32208683, -1198248), ), array( array(-16657702, 2817643, -10286362, 14811298, 6024667, 13349505, -27315504, -10497842, -27672585, -11539858), array(15941029, -9405932, -21367050, 8062055, 31876073, -238629, -15278393, -1444429, 15397331, -4130193), array(8934485, -13485467, -23286397, -13423241, -32446090, 14047986, 31170398, -1441021, -27505566, 15087184), ), array( array(-18357243, -2156491, 24524913, -16677868, 15520427, -6360776, -15502406, 11461896, 16788528, -5868942), array(-1947386, 16013773, 21750665, 3714552, -17401782, -16055433, -3770287, -10323320, 31322514, -11615635), array(21426655, -5650218, -13648287, -5347537, -28812189, -4920970, -18275391, -14621414, 13040862, -12112948), ), array( array(11293895, 12478086, -27136401, 15083750, -29307421, 14748872, 14555558, -13417103, 1613711, 4896935), array(-25894883, 15323294, -8489791, -8057900, 25967126, -13425460, 2825960, -4897045, -23971776, -11267415), array(-15924766, -5229880, -17443532, 6410664, 3622847, 10243618, 20615400, 12405433, -23753030, -8436416), ), array( array(-7091295, 12556208, -20191352, 9025187, -17072479, 4333801, 4378436, 2432030, 23097949, -566018), array(4565804, -16025654, 20084412, -7842817, 1724999, 189254, 24767264, 10103221, -18512313, 2424778), array(366633, -11976806, 8173090, -6890119, 30788634, 5745705, -7168678, 1344109, -3642553, 12412659), ), array( array(-24001791, 7690286, 14929416, -168257, -32210835, -13412986, 24162697, -15326504, -3141501, 11179385), array(18289522, -14724954, 8056945, 16430056, -21729724, 7842514, -6001441, -1486897, -18684645, -11443503), array(476239, 6601091, -6152790, -9723375, 17503545, -4863900, 27672959, 13403813, 11052904, 5219329), ), ), array( array( array(20678546, -8375738, -32671898, 8849123, -5009758, 14574752, 31186971, -3973730, 9014762, -8579056), array(-13644050, -10350239, -15962508, 5075808, -1514661, -11534600, -33102500, 9160280, 8473550, -3256838), array(24900749, 14435722, 17209120, -15292541, -22592275, 9878983, -7689309, -16335821, -24568481, 11788948), ), array( array(-3118155, -11395194, -13802089, 14797441, 9652448, -6845904, -20037437, 10410733, -24568470, -1458691), array(-15659161, 16736706, -22467150, 10215878, -9097177, 7563911, 11871841, -12505194, -18513325, 8464118), array(-23400612, 8348507, -14585951, -861714, -3950205, -6373419, 14325289, 8628612, 33313881, -8370517), ), array( array(-20186973, -4967935, 22367356, 5271547, -1097117, -4788838, -24805667, -10236854, -8940735, -5818269), array(-6948785, -1795212, -32625683, -16021179, 32635414, -7374245, 15989197, -12838188, 28358192, -4253904), array(-23561781, -2799059, -32351682, -1661963, -9147719, 10429267, -16637684, 4072016, -5351664, 5596589), ), array( array(-28236598, -3390048, 12312896, 6213178, 3117142, 16078565, 29266239, 2557221, 1768301, 15373193), array(-7243358, -3246960, -4593467, -7553353, -127927, -912245, -1090902, -4504991, -24660491, 3442910), array(-30210571, 5124043, 14181784, 8197961, 18964734, -11939093, 22597931, 7176455, -18585478, 13365930), ), array( array(-7877390, -1499958, 8324673, 4690079, 6261860, 890446, 24538107, -8570186, -9689599, -3031667), array(25008904, -10771599, -4305031, -9638010, 16265036, 15721635, 683793, -11823784, 15723479, -15163481), array(-9660625, 12374379, -27006999, -7026148, -7724114, -12314514, 11879682, 5400171, 519526, -1235876), ), array( array(22258397, -16332233, -7869817, 14613016, -22520255, -2950923, -20353881, 7315967, 16648397, 7605640), array(-8081308, -8464597, -8223311, 9719710, 19259459, -15348212, 23994942, -5281555, -9468848, 4763278), array(-21699244, 9220969, -15730624, 1084137, -25476107, -2852390, 31088447, -7764523, -11356529, 728112), ), array( array(26047220, -11751471, -6900323, -16521798, 24092068, 9158119, -4273545, -12555558, -29365436, -5498272), array(17510331, -322857, 5854289, 8403524, 17133918, -3112612, -28111007, 12327945, 10750447, 10014012), array(-10312768, 3936952, 9156313, -8897683, 16498692, -994647, -27481051, -666732, 3424691, 7540221), ), array( array(30322361, -6964110, 11361005, -4143317, 7433304, 4989748, -7071422, -16317219, -9244265, 15258046), array(13054562, -2779497, 19155474, 469045, -12482797, 4566042, 5631406, 2711395, 1062915, -5136345), array(-19240248, -11254599, -29509029, -7499965, -5835763, 13005411, -6066489, 12194497, 32960380, 1459310), ), ), array( array( array(19852034, 7027924, 23669353, 10020366, 8586503, -6657907, 394197, -6101885, 18638003, -11174937), array(31395534, 15098109, 26581030, 8030562, -16527914, -5007134, 9012486, -7584354, -6643087, -5442636), array(-9192165, -2347377, -1997099, 4529534, 25766844, 607986, -13222, 9677543, -32294889, -6456008), ), array( array(-2444496, -149937, 29348902, 8186665, 1873760, 12489863, -30934579, -7839692, -7852844, -8138429), array(-15236356, -15433509, 7766470, 746860, 26346930, -10221762, -27333451, 10754588, -9431476, 5203576), array(31834314, 14135496, -770007, 5159118, 20917671, -16768096, -7467973, -7337524, 31809243, 7347066), ), array( array(-9606723, -11874240, 20414459, 13033986, 13716524, -11691881, 19797970, -12211255, 15192876, -2087490), array(-12663563, -2181719, 1168162, -3804809, 26747877, -14138091, 10609330, 12694420, 33473243, -13382104), array(33184999, 11180355, 15832085, -11385430, -1633671, 225884, 15089336, -11023903, -6135662, 14480053), ), array( array(31308717, -5619998, 31030840, -1897099, 15674547, -6582883, 5496208, 13685227, 27595050, 8737275), array(-20318852, -15150239, 10933843, -16178022, 8335352, -7546022, -31008351, -12610604, 26498114, 66511), array(22644454, -8761729, -16671776, 4884562, -3105614, -13559366, 30540766, -4286747, -13327787, -7515095), ), array( array(-28017847, 9834845, 18617207, -2681312, -3401956, -13307506, 8205540, 13585437, -17127465, 15115439), array(23711543, -672915, 31206561, -8362711, 6164647, -9709987, -33535882, -1426096, 8236921, 16492939), array(-23910559, -13515526, -26299483, -4503841, 25005590, -7687270, 19574902, 10071562, 6708380, -6222424), ), array( array(2101391, -4930054, 19702731, 2367575, -15427167, 1047675, 5301017, 9328700, 29955601, -11678310), array(3096359, 9271816, -21620864, -15521844, -14847996, -7592937, -25892142, -12635595, -9917575, 6216608), array(-32615849, 338663, -25195611, 2510422, -29213566, -13820213, 24822830, -6146567, -26767480, 7525079), ), array( array(-23066649, -13985623, 16133487, -7896178, -3389565, 778788, -910336, -2782495, -19386633, 11994101), array(21691500, -13624626, -641331, -14367021, 3285881, -3483596, -25064666, 9718258, -7477437, 13381418), array(18445390, -4202236, 14979846, 11622458, -1727110, -3582980, 23111648, -6375247, 28535282, 15779576), ), array( array(30098053, 3089662, -9234387, 16662135, -21306940, 11308411, -14068454, 12021730, 9955285, -16303356), array(9734894, -14576830, -7473633, -9138735, 2060392, 11313496, -18426029, 9924399, 20194861, 13380996), array(-26378102, -7965207, -22167821, 15789297, -18055342, -6168792, -1984914, 15707771, 26342023, 10146099), ), ), array( array( array(-26016874, -219943, 21339191, -41388, 19745256, -2878700, -29637280, 2227040, 21612326, -545728), array(-13077387, 1184228, 23562814, -5970442, -20351244, -6348714, 25764461, 12243797, -20856566, 11649658), array(-10031494, 11262626, 27384172, 2271902, 26947504, -15997771, 39944, 6114064, 33514190, 2333242), ), array( array(-21433588, -12421821, 8119782, 7219913, -21830522, -9016134, -6679750, -12670638, 24350578, -13450001), array(-4116307, -11271533, -23886186, 4843615, -30088339, 690623, -31536088, -10406836, 8317860, 12352766), array(18200138, -14475911, -33087759, -2696619, -23702521, -9102511, -23552096, -2287550, 20712163, 6719373), ), array( array(26656208, 6075253, -7858556, 1886072, -28344043, 4262326, 11117530, -3763210, 26224235, -3297458), array(-17168938, -14854097, -3395676, -16369877, -19954045, 14050420, 21728352, 9493610, 18620611, -16428628), array(-13323321, 13325349, 11432106, 5964811, 18609221, 6062965, -5269471, -9725556, -30701573, -16479657), ), array( array(-23860538, -11233159, 26961357, 1640861, -32413112, -16737940, 12248509, -5240639, 13735342, 1934062), array(25089769, 6742589, 17081145, -13406266, 21909293, -16067981, -15136294, -3765346, -21277997, 5473616), array(31883677, -7961101, 1083432, -11572403, 22828471, 13290673, -7125085, 12469656, 29111212, -5451014), ), array( array(24244947, -15050407, -26262976, 2791540, -14997599, 16666678, 24367466, 6388839, -10295587, 452383), array(-25640782, -3417841, 5217916, 16224624, 19987036, -4082269, -24236251, -5915248, 15766062, 8407814), array(-20406999, 13990231, 15495425, 16395525, 5377168, 15166495, -8917023, -4388953, -8067909, 2276718), ), array( array(30157918, 12924066, -17712050, 9245753, 19895028, 3368142, -23827587, 5096219, 22740376, -7303417), array(2041139, -14256350, 7783687, 13876377, -25946985, -13352459, 24051124, 13742383, -15637599, 13295222), array(33338237, -8505733, 12532113, 7977527, 9106186, -1715251, -17720195, -4612972, -4451357, -14669444), ), array( array(-20045281, 5454097, -14346548, 6447146, 28862071, 1883651, -2469266, -4141880, 7770569, 9620597), array(23208068, 7979712, 33071466, 8149229, 1758231, -10834995, 30945528, -1694323, -33502340, -14767970), array(1439958, -16270480, -1079989, -793782, 4625402, 10647766, -5043801, 1220118, 30494170, -11440799), ), array( array(-5037580, -13028295, -2970559, -3061767, 15640974, -6701666, -26739026, 926050, -1684339, -13333647), array(13908495, -3549272, 30919928, -6273825, -21521863, 7989039, 9021034, 9078865, 3353509, 4033511), array(-29663431, -15113610, 32259991, -344482, 24295849, -12912123, 23161163, 8839127, 27485041, 7356032), ), ), array( array( array(9661027, 705443, 11980065, -5370154, -1628543, 14661173, -6346142, 2625015, 28431036, -16771834), array(-23839233, -8311415, -25945511, 7480958, -17681669, -8354183, -22545972, 14150565, 15970762, 4099461), array(29262576, 16756590, 26350592, -8793563, 8529671, -11208050, 13617293, -9937143, 11465739, 8317062), ), array( array(-25493081, -6962928, 32500200, -9419051, -23038724, -2302222, 14898637, 3848455, 20969334, -5157516), array(-20384450, -14347713, -18336405, 13884722, -33039454, 2842114, -21610826, -3649888, 11177095, 14989547), array(-24496721, -11716016, 16959896, 2278463, 12066309, 10137771, 13515641, 2581286, -28487508, 9930240), ), array( array(-17751622, -2097826, 16544300, -13009300, -15914807, -14949081, 18345767, -13403753, 16291481, -5314038), array(-33229194, 2553288, 32678213, 9875984, 8534129, 6889387, -9676774, 6957617, 4368891, 9788741), array(16660756, 7281060, -10830758, 12911820, 20108584, -8101676, -21722536, -8613148, 16250552, -11111103), ), array( array(-19765507, 2390526, -16551031, 14161980, 1905286, 6414907, 4689584, 10604807, -30190403, 4782747), array(-1354539, 14736941, -7367442, -13292886, 7710542, -14155590, -9981571, 4383045, 22546403, 437323), array(31665577, -12180464, -16186830, 1491339, -18368625, 3294682, 27343084, 2786261, -30633590, -14097016), ), array( array(-14467279, -683715, -33374107, 7448552, 19294360, 14334329, -19690631, 2355319, -19284671, -6114373), array(15121312, -15796162, 6377020, -6031361, -10798111, -12957845, 18952177, 15496498, -29380133, 11754228), array(-2637277, -13483075, 8488727, -14303896, 12728761, -1622493, 7141596, 11724556, 22761615, -10134141), ), array( array(16918416, 11729663, -18083579, 3022987, -31015732, -13339659, -28741185, -12227393, 32851222, 11717399), array(11166634, 7338049, -6722523, 4531520, -29468672, -7302055, 31474879, 3483633, -1193175, -4030831), array(-185635, 9921305, 31456609, -13536438, -12013818, 13348923, 33142652, 6546660, -19985279, -3948376), ), array( array(-32460596, 11266712, -11197107, -7899103, 31703694, 3855903, -8537131, -12833048, -30772034, -15486313), array(-18006477, 12709068, 3991746, -6479188, -21491523, -10550425, -31135347, -16049879, 10928917, 3011958), array(-6957757, -15594337, 31696059, 334240, 29576716, 14796075, -30831056, -12805180, 18008031, 10258577), ), array( array(-22448644, 15655569, 7018479, -4410003, -30314266, -1201591, -1853465, 1367120, 25127874, 6671743), array(29701166, -14373934, -10878120, 9279288, -17568, 13127210, 21382910, 11042292, 25838796, 4642684), array(-20430234, 14955537, -24126347, 8124619, -5369288, -5990470, 30468147, -13900640, 18423289, 4177476), ), ) ); /** * See: libsodium's crypto_core/curve25519/ref10/base2.h * * @var array<int, array<int, array<int, int>>> basically int[8][3] */ protected static $base2 = array( array( array(25967493, -14356035, 29566456, 3660896, -12694345, 4014787, 27544626, -11754271, -6079156, 2047605), array(-12545711, 934262, -2722910, 3049990, -727428, 9406986, 12720692, 5043384, 19500929, -15469378), array(-8738181, 4489570, 9688441, -14785194, 10184609, -12363380, 29287919, 11864899, -24514362, -4438546), ), array( array(15636291, -9688557, 24204773, -7912398, 616977, -16685262, 27787600, -14772189, 28944400, -1550024), array(16568933, 4717097, -11556148, -1102322, 15682896, -11807043, 16354577, -11775962, 7689662, 11199574), array(30464156, -5976125, -11779434, -15670865, 23220365, 15915852, 7512774, 10017326, -17749093, -9920357), ), array( array(10861363, 11473154, 27284546, 1981175, -30064349, 12577861, 32867885, 14515107, -15438304, 10819380), array(4708026, 6336745, 20377586, 9066809, -11272109, 6594696, -25653668, 12483688, -12668491, 5581306), array(19563160, 16186464, -29386857, 4097519, 10237984, -4348115, 28542350, 13850243, -23678021, -15815942), ), array( array(5153746, 9909285, 1723747, -2777874, 30523605, 5516873, 19480852, 5230134, -23952439, -15175766), array(-30269007, -3463509, 7665486, 10083793, 28475525, 1649722, 20654025, 16520125, 30598449, 7715701), array(28881845, 14381568, 9657904, 3680757, -20181635, 7843316, -31400660, 1370708, 29794553, -1409300), ), array( array(-22518993, -6692182, 14201702, -8745502, -23510406, 8844726, 18474211, -1361450, -13062696, 13821877), array(-6455177, -7839871, 3374702, -4740862, -27098617, -10571707, 31655028, -7212327, 18853322, -14220951), array(4566830, -12963868, -28974889, -12240689, -7602672, -2830569, -8514358, -10431137, 2207753, -3209784), ), array( array(-25154831, -4185821, 29681144, 7868801, -6854661, -9423865, -12437364, -663000, -31111463, -16132436), array(25576264, -2703214, 7349804, -11814844, 16472782, 9300885, 3844789, 15725684, 171356, 6466918), array(23103977, 13316479, 9739013, -16149481, 817875, -15038942, 8965339, -14088058, -30714912, 16193877), ), array( array(-33521811, 3180713, -2394130, 14003687, -16903474, -16270840, 17238398, 4729455, -18074513, 9256800), array(-25182317, -4174131, 32336398, 5036987, -21236817, 11360617, 22616405, 9761698, -19827198, 630305), array(-13720693, 2639453, -24237460, -7406481, 9494427, -5774029, -6554551, -15960994, -2449256, -14291300), ), array( array(-3151181, -5046075, 9282714, 6866145, -31907062, -863023, -18940575, 15033784, 25105118, -7894876), array(-24326370, 15950226, -31801215, -14592823, -11662737, -5090925, 1573892, -2625887, 2198790, -15804619), array(-3099351, 10324967, -2241613, 7453183, -5446979, -2735503, -13812022, -16236442, -32461234, -12290683), ) ); /** * 37095705934669439343138083508754565189542113879843219016388785533085940283555 * * @var array<int, int> */ protected static $d = array( -10913610, 13857413, -15372611, 6949391, 114729, -8787816, -6275908, -3247719, -18696448, -12055116 ); /** * 2 * d = 16295367250680780974490674513165176452449235426866156013048779062215315747161 * * @var array<int, int> */ protected static $d2 = array( -21827239, -5839606, -30745221, 13898782, 229458, 15978800, -12551817, -6495438, 29715968, 9444199 ); /** * sqrt(-1) * * @var array<int, int> */ protected static $sqrtm1 = array( -32595792, -7943725, 9377950, 3500415, 12389472, -272473, -25146209, -2005654, 326686, 11406482 ); } Core32/Curve25519/Fe.php 0000644 00000012572 15162213667 0010356 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_Curve25519_Fe', false)) { return; } /** * Class ParagonIE_Sodium_Core32_Curve25519_Fe * * This represents a Field Element */ class ParagonIE_Sodium_Core32_Curve25519_Fe implements ArrayAccess { /** * @var array<int, ParagonIE_Sodium_Core32_Int32> */ protected $container = array(); /** * @var int */ protected $size = 10; /** * @internal You should not use this directly from another application * * @param array<int, ParagonIE_Sodium_Core32_Int32> $array * @param bool $save_indexes * @return self * @throws SodiumException * @throws TypeError */ public static function fromArray($array, $save_indexes = null) { $count = count($array); if ($save_indexes) { $keys = array_keys($array); } else { $keys = range(0, $count - 1); } $array = array_values($array); $obj = new ParagonIE_Sodium_Core32_Curve25519_Fe(); if ($save_indexes) { for ($i = 0; $i < $count; ++$i) { $array[$i]->overflow = 0; $obj->offsetSet($keys[$i], $array[$i]); } } else { for ($i = 0; $i < $count; ++$i) { if (!($array[$i] instanceof ParagonIE_Sodium_Core32_Int32)) { throw new TypeError('Expected ParagonIE_Sodium_Core32_Int32'); } $array[$i]->overflow = 0; $obj->offsetSet($i, $array[$i]); } } return $obj; } /** * @internal You should not use this directly from another application * * @param array<int, int> $array * @param bool $save_indexes * @return self * @throws SodiumException * @throws TypeError */ public static function fromIntArray($array, $save_indexes = null) { $count = count($array); if ($save_indexes) { $keys = array_keys($array); } else { $keys = range(0, $count - 1); } $array = array_values($array); $set = array(); /** @var int $i */ /** @var int $v */ foreach ($array as $i => $v) { $set[$i] = ParagonIE_Sodium_Core32_Int32::fromInt($v); } $obj = new ParagonIE_Sodium_Core32_Curve25519_Fe(); if ($save_indexes) { for ($i = 0; $i < $count; ++$i) { $set[$i]->overflow = 0; $obj->offsetSet($keys[$i], $set[$i]); } } else { for ($i = 0; $i < $count; ++$i) { $set[$i]->overflow = 0; $obj->offsetSet($i, $set[$i]); } } return $obj; } /** * @internal You should not use this directly from another application * * @param mixed $offset * @param mixed $value * @return void * @throws SodiumException * @throws TypeError */ #[ReturnTypeWillChange] public function offsetSet($offset, $value) { if (!($value instanceof ParagonIE_Sodium_Core32_Int32)) { throw new InvalidArgumentException('Expected an instance of ParagonIE_Sodium_Core32_Int32'); } if (is_null($offset)) { $this->container[] = $value; } else { ParagonIE_Sodium_Core32_Util::declareScalarType($offset, 'int', 1); $this->container[(int) $offset] = $value; } } /** * @internal You should not use this directly from another application * * @param mixed $offset * @return bool * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] public function offsetExists($offset) { return isset($this->container[$offset]); } /** * @internal You should not use this directly from another application * * @param mixed $offset * @return void * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] public function offsetUnset($offset) { unset($this->container[$offset]); } /** * @internal You should not use this directly from another application * * @param mixed $offset * @return ParagonIE_Sodium_Core32_Int32 * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] public function offsetGet($offset) { if (!isset($this->container[$offset])) { $this->container[(int) $offset] = new ParagonIE_Sodium_Core32_Int32(); } /** @var ParagonIE_Sodium_Core32_Int32 $get */ $get = $this->container[$offset]; return $get; } /** * @internal You should not use this directly from another application * * @return array */ public function __debugInfo() { if (empty($this->container)) { return array(); } $c = array( (int) ($this->container[0]->toInt()), (int) ($this->container[1]->toInt()), (int) ($this->container[2]->toInt()), (int) ($this->container[3]->toInt()), (int) ($this->container[4]->toInt()), (int) ($this->container[5]->toInt()), (int) ($this->container[6]->toInt()), (int) ($this->container[7]->toInt()), (int) ($this->container[8]->toInt()), (int) ($this->container[9]->toInt()) ); return array(implode(', ', $c)); } } Core32/Curve25519/README.md 0000644 00000000332 15162213670 0010553 0 ustar 00 # Curve25519 Data Structures These are PHP implementation of the [structs used in the ref10 curve25519 code](https://github.com/jedisct1/libsodium/blob/master/src/libsodium/include/sodium/private/curve25519_ref10.h). Core32/ChaCha20.php 0000644 00000034257 15162213670 0007601 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_ChaCha20', false)) { return; } /** * Class ParagonIE_Sodium_Core32_ChaCha20 */ class ParagonIE_Sodium_Core32_ChaCha20 extends ParagonIE_Sodium_Core32_Util { /** * The ChaCha20 quarter round function. Works on four 32-bit integers. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_Int32 $a * @param ParagonIE_Sodium_Core32_Int32 $b * @param ParagonIE_Sodium_Core32_Int32 $c * @param ParagonIE_Sodium_Core32_Int32 $d * @return array<int, ParagonIE_Sodium_Core32_Int32> * @throws SodiumException * @throws TypeError */ protected static function quarterRound( ParagonIE_Sodium_Core32_Int32 $a, ParagonIE_Sodium_Core32_Int32 $b, ParagonIE_Sodium_Core32_Int32 $c, ParagonIE_Sodium_Core32_Int32 $d ) { /** @var ParagonIE_Sodium_Core32_Int32 $a */ /** @var ParagonIE_Sodium_Core32_Int32 $b */ /** @var ParagonIE_Sodium_Core32_Int32 $c */ /** @var ParagonIE_Sodium_Core32_Int32 $d */ # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); $a = $a->addInt32($b); $d = $d->xorInt32($a)->rotateLeft(16); # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); $c = $c->addInt32($d); $b = $b->xorInt32($c)->rotateLeft(12); # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); $a = $a->addInt32($b); $d = $d->xorInt32($a)->rotateLeft(8); # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); $c = $c->addInt32($d); $b = $b->xorInt32($c)->rotateLeft(7); return array($a, $b, $c, $d); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx * @param string $message * * @return string * @throws SodiumException * @throws TypeError */ public static function encryptBytes( ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx, $message = '' ) { $bytes = self::strlen($message); /** @var ParagonIE_Sodium_Core32_Int32 $x0 */ /** @var ParagonIE_Sodium_Core32_Int32 $x1 */ /** @var ParagonIE_Sodium_Core32_Int32 $x2 */ /** @var ParagonIE_Sodium_Core32_Int32 $x3 */ /** @var ParagonIE_Sodium_Core32_Int32 $x4 */ /** @var ParagonIE_Sodium_Core32_Int32 $x5 */ /** @var ParagonIE_Sodium_Core32_Int32 $x6 */ /** @var ParagonIE_Sodium_Core32_Int32 $x7 */ /** @var ParagonIE_Sodium_Core32_Int32 $x8 */ /** @var ParagonIE_Sodium_Core32_Int32 $x9 */ /** @var ParagonIE_Sodium_Core32_Int32 $x10 */ /** @var ParagonIE_Sodium_Core32_Int32 $x11 */ /** @var ParagonIE_Sodium_Core32_Int32 $x12 */ /** @var ParagonIE_Sodium_Core32_Int32 $x13 */ /** @var ParagonIE_Sodium_Core32_Int32 $x14 */ /** @var ParagonIE_Sodium_Core32_Int32 $x15 */ /* j0 = ctx->input[0]; j1 = ctx->input[1]; j2 = ctx->input[2]; j3 = ctx->input[3]; j4 = ctx->input[4]; j5 = ctx->input[5]; j6 = ctx->input[6]; j7 = ctx->input[7]; j8 = ctx->input[8]; j9 = ctx->input[9]; j10 = ctx->input[10]; j11 = ctx->input[11]; j12 = ctx->input[12]; j13 = ctx->input[13]; j14 = ctx->input[14]; j15 = ctx->input[15]; */ /** @var ParagonIE_Sodium_Core32_Int32 $j0 */ $j0 = $ctx[0]; /** @var ParagonIE_Sodium_Core32_Int32 $j1 */ $j1 = $ctx[1]; /** @var ParagonIE_Sodium_Core32_Int32 $j2 */ $j2 = $ctx[2]; /** @var ParagonIE_Sodium_Core32_Int32 $j3 */ $j3 = $ctx[3]; /** @var ParagonIE_Sodium_Core32_Int32 $j4 */ $j4 = $ctx[4]; /** @var ParagonIE_Sodium_Core32_Int32 $j5 */ $j5 = $ctx[5]; /** @var ParagonIE_Sodium_Core32_Int32 $j6 */ $j6 = $ctx[6]; /** @var ParagonIE_Sodium_Core32_Int32 $j7 */ $j7 = $ctx[7]; /** @var ParagonIE_Sodium_Core32_Int32 $j8 */ $j8 = $ctx[8]; /** @var ParagonIE_Sodium_Core32_Int32 $j9 */ $j9 = $ctx[9]; /** @var ParagonIE_Sodium_Core32_Int32 $j10 */ $j10 = $ctx[10]; /** @var ParagonIE_Sodium_Core32_Int32 $j11 */ $j11 = $ctx[11]; /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ $j12 = $ctx[12]; /** @var ParagonIE_Sodium_Core32_Int32 $j13 */ $j13 = $ctx[13]; /** @var ParagonIE_Sodium_Core32_Int32 $j14 */ $j14 = $ctx[14]; /** @var ParagonIE_Sodium_Core32_Int32 $j15 */ $j15 = $ctx[15]; $c = ''; for (;;) { if ($bytes < 64) { $message .= str_repeat("\x00", 64 - $bytes); } $x0 = clone $j0; $x1 = clone $j1; $x2 = clone $j2; $x3 = clone $j3; $x4 = clone $j4; $x5 = clone $j5; $x6 = clone $j6; $x7 = clone $j7; $x8 = clone $j8; $x9 = clone $j9; $x10 = clone $j10; $x11 = clone $j11; $x12 = clone $j12; $x13 = clone $j13; $x14 = clone $j14; $x15 = clone $j15; # for (i = 20; i > 0; i -= 2) { for ($i = 20; $i > 0; $i -= 2) { # QUARTERROUND( x0, x4, x8, x12) list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); # QUARTERROUND( x1, x5, x9, x13) list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); # QUARTERROUND( x2, x6, x10, x14) list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); # QUARTERROUND( x3, x7, x11, x15) list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); # QUARTERROUND( x0, x5, x10, x15) list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); # QUARTERROUND( x1, x6, x11, x12) list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); # QUARTERROUND( x2, x7, x8, x13) list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); # QUARTERROUND( x3, x4, x9, x14) list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); } /* x0 = PLUS(x0, j0); x1 = PLUS(x1, j1); x2 = PLUS(x2, j2); x3 = PLUS(x3, j3); x4 = PLUS(x4, j4); x5 = PLUS(x5, j5); x6 = PLUS(x6, j6); x7 = PLUS(x7, j7); x8 = PLUS(x8, j8); x9 = PLUS(x9, j9); x10 = PLUS(x10, j10); x11 = PLUS(x11, j11); x12 = PLUS(x12, j12); x13 = PLUS(x13, j13); x14 = PLUS(x14, j14); x15 = PLUS(x15, j15); */ $x0 = $x0->addInt32($j0); $x1 = $x1->addInt32($j1); $x2 = $x2->addInt32($j2); $x3 = $x3->addInt32($j3); $x4 = $x4->addInt32($j4); $x5 = $x5->addInt32($j5); $x6 = $x6->addInt32($j6); $x7 = $x7->addInt32($j7); $x8 = $x8->addInt32($j8); $x9 = $x9->addInt32($j9); $x10 = $x10->addInt32($j10); $x11 = $x11->addInt32($j11); $x12 = $x12->addInt32($j12); $x13 = $x13->addInt32($j13); $x14 = $x14->addInt32($j14); $x15 = $x15->addInt32($j15); /* x0 = XOR(x0, LOAD32_LE(m + 0)); x1 = XOR(x1, LOAD32_LE(m + 4)); x2 = XOR(x2, LOAD32_LE(m + 8)); x3 = XOR(x3, LOAD32_LE(m + 12)); x4 = XOR(x4, LOAD32_LE(m + 16)); x5 = XOR(x5, LOAD32_LE(m + 20)); x6 = XOR(x6, LOAD32_LE(m + 24)); x7 = XOR(x7, LOAD32_LE(m + 28)); x8 = XOR(x8, LOAD32_LE(m + 32)); x9 = XOR(x9, LOAD32_LE(m + 36)); x10 = XOR(x10, LOAD32_LE(m + 40)); x11 = XOR(x11, LOAD32_LE(m + 44)); x12 = XOR(x12, LOAD32_LE(m + 48)); x13 = XOR(x13, LOAD32_LE(m + 52)); x14 = XOR(x14, LOAD32_LE(m + 56)); x15 = XOR(x15, LOAD32_LE(m + 60)); */ $x0 = $x0->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4))); $x1 = $x1->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 4, 4))); $x2 = $x2->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 8, 4))); $x3 = $x3->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4))); $x4 = $x4->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 16, 4))); $x5 = $x5->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 20, 4))); $x6 = $x6->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 24, 4))); $x7 = $x7->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 28, 4))); $x8 = $x8->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 32, 4))); $x9 = $x9->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 36, 4))); $x10 = $x10->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 40, 4))); $x11 = $x11->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 44, 4))); $x12 = $x12->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 48, 4))); $x13 = $x13->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 52, 4))); $x14 = $x14->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 56, 4))); $x15 = $x15->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 60, 4))); /* j12 = PLUSONE(j12); if (!j12) { j13 = PLUSONE(j13); } */ /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ $j12 = $j12->addInt(1); if ($j12->limbs[0] === 0 && $j12->limbs[1] === 0) { $j13 = $j13->addInt(1); } /* STORE32_LE(c + 0, x0); STORE32_LE(c + 4, x1); STORE32_LE(c + 8, x2); STORE32_LE(c + 12, x3); STORE32_LE(c + 16, x4); STORE32_LE(c + 20, x5); STORE32_LE(c + 24, x6); STORE32_LE(c + 28, x7); STORE32_LE(c + 32, x8); STORE32_LE(c + 36, x9); STORE32_LE(c + 40, x10); STORE32_LE(c + 44, x11); STORE32_LE(c + 48, x12); STORE32_LE(c + 52, x13); STORE32_LE(c + 56, x14); STORE32_LE(c + 60, x15); */ $block = $x0->toReverseString() . $x1->toReverseString() . $x2->toReverseString() . $x3->toReverseString() . $x4->toReverseString() . $x5->toReverseString() . $x6->toReverseString() . $x7->toReverseString() . $x8->toReverseString() . $x9->toReverseString() . $x10->toReverseString() . $x11->toReverseString() . $x12->toReverseString() . $x13->toReverseString() . $x14->toReverseString() . $x15->toReverseString(); /* Partial block */ if ($bytes < 64) { $c .= self::substr($block, 0, $bytes); break; } /* Full block */ $c .= $block; $bytes -= 64; if ($bytes <= 0) { break; } $message = self::substr($message, 64); } /* end for(;;) loop */ $ctx[12] = $j12; $ctx[13] = $j13; return $c; } /** * @internal You should not use this directly from another application * * @param int $len * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function stream($len = 64, $nonce = '', $key = '') { return self::encryptBytes( new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce), str_repeat("\x00", $len) ); } /** * @internal You should not use this directly from another application * * @param int $len * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function ietfStream($len, $nonce = '', $key = '') { return self::encryptBytes( new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce), str_repeat("\x00", $len) ); } /** * @internal You should not use this directly from another application * * @param string $message * @param string $nonce * @param string $key * @param string $ic * @return string * @throws SodiumException * @throws TypeError */ public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') { return self::encryptBytes( new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic), $message ); } /** * @internal You should not use this directly from another application * * @param string $message * @param string $nonce * @param string $key * @param string $ic * @return string * @throws SodiumException * @throws TypeError */ public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') { return self::encryptBytes( new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic), $message ); } } Core32/ChaCha20/error_log 0000644 00000007570 15162213671 0011004 0 ustar 00 [24-Mar-2026 23:43:45 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php on line 10 [24-Mar-2026 23:44:46 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20_Ctx" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/IetfCtx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/IetfCtx.php on line 10 [25-Mar-2026 02:33:53 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php on line 10 [25-Mar-2026 02:34:44 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20_Ctx" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/IetfCtx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/IetfCtx.php on line 10 [25-Mar-2026 14:40:26 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php on line 10 [25-Mar-2026 14:41:06 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20_Ctx" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/IetfCtx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/IetfCtx.php on line 10 [26-Mar-2026 09:09:56 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php on line 10 [26-Mar-2026 09:15:32 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20_Ctx" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/IetfCtx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/IetfCtx.php on line 10 [26-Mar-2026 13:33:44 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php on line 10 [26-Mar-2026 13:34:41 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20_Ctx" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/IetfCtx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/IetfCtx.php on line 10 [28-Mar-2026 09:24:51 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php on line 10 [28-Mar-2026 09:36:01 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_ChaCha20_Ctx" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/IetfCtx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/ChaCha20/IetfCtx.php on line 10 Core32/ChaCha20/Ctx.php 0000644 00000011450 15162213672 0010327 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_ChaCha20_Ctx', false)) { return; } /** * Class ParagonIE_Sodium_Core32_ChaCha20_Ctx */ class ParagonIE_Sodium_Core32_ChaCha20_Ctx extends ParagonIE_Sodium_Core32_Util implements ArrayAccess { /** * @var SplFixedArray internally, <int, ParagonIE_Sodium_Core32_Int32> */ protected $container; /** * ParagonIE_Sodium_Core_ChaCha20_Ctx constructor. * * @internal You should not use this directly from another application * * @param string $key ChaCha20 key. * @param string $iv Initialization Vector (a.k.a. nonce). * @param string $counter The initial counter value. * Defaults to 8 0x00 bytes. * @throws InvalidArgumentException * @throws SodiumException * @throws TypeError */ public function __construct($key = '', $iv = '', $counter = '') { if (self::strlen($key) !== 32) { throw new InvalidArgumentException('ChaCha20 expects a 256-bit key.'); } if (self::strlen($iv) !== 8) { throw new InvalidArgumentException('ChaCha20 expects a 64-bit nonce.'); } $this->container = new SplFixedArray(16); /* "expand 32-byte k" as per ChaCha20 spec */ $this->container[0] = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); $this->container[1] = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); $this->container[2] = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); $this->container[3] = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); $this->container[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)); $this->container[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4)); $this->container[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4)); $this->container[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)); $this->container[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)); $this->container[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)); $this->container[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)); $this->container[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)); if (empty($counter)) { $this->container[12] = new ParagonIE_Sodium_Core32_Int32(); $this->container[13] = new ParagonIE_Sodium_Core32_Int32(); } else { $this->container[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($counter, 0, 4)); $this->container[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($counter, 4, 4)); } $this->container[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 0, 4)); $this->container[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 4, 4)); } /** * @internal You should not use this directly from another application * * @param int $offset * @param int|ParagonIE_Sodium_Core32_Int32 $value * @return void */ #[ReturnTypeWillChange] public function offsetSet($offset, $value) { if (!is_int($offset)) { throw new InvalidArgumentException('Expected an integer'); } if ($value instanceof ParagonIE_Sodium_Core32_Int32) { /* } elseif (is_int($value)) { $value = ParagonIE_Sodium_Core32_Int32::fromInt($value); */ } else { throw new InvalidArgumentException('Expected an integer'); } $this->container[$offset] = $value; } /** * @internal You should not use this directly from another application * * @param int $offset * @return bool * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] public function offsetExists($offset) { return isset($this->container[$offset]); } /** * @internal You should not use this directly from another application * * @param int $offset * @return void * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] public function offsetUnset($offset) { unset($this->container[$offset]); } /** * @internal You should not use this directly from another application * * @param int $offset * @return mixed|null * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] public function offsetGet($offset) { return isset($this->container[$offset]) ? $this->container[$offset] : null; } } Core32/ChaCha20/IetfCtx.php 0000644 00000002737 15162213673 0011150 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_ChaCha20_IetfCtx', false)) { return; } /** * Class ParagonIE_Sodium_Core32_ChaCha20_IetfCtx */ class ParagonIE_Sodium_Core32_ChaCha20_IetfCtx extends ParagonIE_Sodium_Core32_ChaCha20_Ctx { /** * ParagonIE_Sodium_Core_ChaCha20_IetfCtx constructor. * * @internal You should not use this directly from another application * * @param string $key ChaCha20 key. * @param string $iv Initialization Vector (a.k.a. nonce). * @param string $counter The initial counter value. * Defaults to 4 0x00 bytes. * @throws InvalidArgumentException * @throws SodiumException * @throws TypeError */ public function __construct($key = '', $iv = '', $counter = '') { if (self::strlen($iv) !== 12) { throw new InvalidArgumentException('ChaCha20 expects a 96-bit nonce in IETF mode.'); } parent::__construct($key, self::substr($iv, 0, 8), $counter); if (!empty($counter)) { $this->container[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($counter, 0, 4)); } $this->container[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 0, 4)); $this->container[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 4, 4)); $this->container[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 8, 4)); } } Core32/HChaCha20.php 0000644 00000012261 15162213673 0007703 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_HChaCha20', false)) { return; } /** * Class ParagonIE_Sodium_Core_HChaCha20 */ class ParagonIE_Sodium_Core32_HChaCha20 extends ParagonIE_Sodium_Core32_ChaCha20 { /** * @param string $in * @param string $key * @param string|null $c * @return string * @throws SodiumException * @throws TypeError */ public static function hChaCha20($in = '', $key = '', $c = null) { $ctx = array(); if ($c === null) { $ctx[0] = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); $ctx[1] = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); $ctx[2] = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); $ctx[3] = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); } else { $ctx[0] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); $ctx[1] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); $ctx[2] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); $ctx[3] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); } $ctx[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)); $ctx[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4)); $ctx[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4)); $ctx[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)); $ctx[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)); $ctx[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)); $ctx[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)); $ctx[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)); $ctx[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); $ctx[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); $ctx[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); $ctx[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); return self::hChaCha20Bytes($ctx); } /** * @param array $ctx * @return string * @throws SodiumException * @throws TypeError */ protected static function hChaCha20Bytes(array $ctx) { /** @var ParagonIE_Sodium_Core32_Int32 $x0 */ $x0 = $ctx[0]; /** @var ParagonIE_Sodium_Core32_Int32 $x1 */ $x1 = $ctx[1]; /** @var ParagonIE_Sodium_Core32_Int32 $x2 */ $x2 = $ctx[2]; /** @var ParagonIE_Sodium_Core32_Int32 $x3 */ $x3 = $ctx[3]; /** @var ParagonIE_Sodium_Core32_Int32 $x4 */ $x4 = $ctx[4]; /** @var ParagonIE_Sodium_Core32_Int32 $x5 */ $x5 = $ctx[5]; /** @var ParagonIE_Sodium_Core32_Int32 $x6 */ $x6 = $ctx[6]; /** @var ParagonIE_Sodium_Core32_Int32 $x7 */ $x7 = $ctx[7]; /** @var ParagonIE_Sodium_Core32_Int32 $x8 */ $x8 = $ctx[8]; /** @var ParagonIE_Sodium_Core32_Int32 $x9 */ $x9 = $ctx[9]; /** @var ParagonIE_Sodium_Core32_Int32 $x10 */ $x10 = $ctx[10]; /** @var ParagonIE_Sodium_Core32_Int32 $x11 */ $x11 = $ctx[11]; /** @var ParagonIE_Sodium_Core32_Int32 $x12 */ $x12 = $ctx[12]; /** @var ParagonIE_Sodium_Core32_Int32 $x13 */ $x13 = $ctx[13]; /** @var ParagonIE_Sodium_Core32_Int32 $x14 */ $x14 = $ctx[14]; /** @var ParagonIE_Sodium_Core32_Int32 $x15 */ $x15 = $ctx[15]; for ($i = 0; $i < 10; ++$i) { # QUARTERROUND( x0, x4, x8, x12) list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); # QUARTERROUND( x1, x5, x9, x13) list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); # QUARTERROUND( x2, x6, x10, x14) list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); # QUARTERROUND( x3, x7, x11, x15) list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); # QUARTERROUND( x0, x5, x10, x15) list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); # QUARTERROUND( x1, x6, x11, x12) list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); # QUARTERROUND( x2, x7, x8, x13) list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); # QUARTERROUND( x3, x4, x9, x14) list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); } return $x0->toReverseString() . $x1->toReverseString() . $x2->toReverseString() . $x3->toReverseString() . $x12->toReverseString() . $x13->toReverseString() . $x14->toReverseString() . $x15->toReverseString(); } } Core32/HSalsa20.php 0000644 00000015435 15162213674 0007646 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_HSalsa20', false)) { return; } /** * Class ParagonIE_Sodium_Core32_HSalsa20 */ abstract class ParagonIE_Sodium_Core32_HSalsa20 extends ParagonIE_Sodium_Core32_Salsa20 { /** * Calculate an hsalsa20 hash of a single block * * HSalsa20 doesn't have a counter and will never be used for more than * one block (used to derive a subkey for xsalsa20). * * @internal You should not use this directly from another application * * @param string $in * @param string $k * @param string|null $c * @return string * @throws SodiumException * @throws TypeError */ public static function hsalsa20($in, $k, $c = null) { /** * @var ParagonIE_Sodium_Core32_Int32 $x0 * @var ParagonIE_Sodium_Core32_Int32 $x1 * @var ParagonIE_Sodium_Core32_Int32 $x2 * @var ParagonIE_Sodium_Core32_Int32 $x3 * @var ParagonIE_Sodium_Core32_Int32 $x4 * @var ParagonIE_Sodium_Core32_Int32 $x5 * @var ParagonIE_Sodium_Core32_Int32 $x6 * @var ParagonIE_Sodium_Core32_Int32 $x7 * @var ParagonIE_Sodium_Core32_Int32 $x8 * @var ParagonIE_Sodium_Core32_Int32 $x9 * @var ParagonIE_Sodium_Core32_Int32 $x10 * @var ParagonIE_Sodium_Core32_Int32 $x11 * @var ParagonIE_Sodium_Core32_Int32 $x12 * @var ParagonIE_Sodium_Core32_Int32 $x13 * @var ParagonIE_Sodium_Core32_Int32 $x14 * @var ParagonIE_Sodium_Core32_Int32 $x15 * @var ParagonIE_Sodium_Core32_Int32 $j0 * @var ParagonIE_Sodium_Core32_Int32 $j1 * @var ParagonIE_Sodium_Core32_Int32 $j2 * @var ParagonIE_Sodium_Core32_Int32 $j3 * @var ParagonIE_Sodium_Core32_Int32 $j4 * @var ParagonIE_Sodium_Core32_Int32 $j5 * @var ParagonIE_Sodium_Core32_Int32 $j6 * @var ParagonIE_Sodium_Core32_Int32 $j7 * @var ParagonIE_Sodium_Core32_Int32 $j8 * @var ParagonIE_Sodium_Core32_Int32 $j9 * @var ParagonIE_Sodium_Core32_Int32 $j10 * @var ParagonIE_Sodium_Core32_Int32 $j11 * @var ParagonIE_Sodium_Core32_Int32 $j12 * @var ParagonIE_Sodium_Core32_Int32 $j13 * @var ParagonIE_Sodium_Core32_Int32 $j14 * @var ParagonIE_Sodium_Core32_Int32 $j15 */ if (self::strlen($k) < 32) { throw new RangeException('Key must be 32 bytes long'); } if ($c === null) { $x0 = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); $x5 = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); $x10 = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); $x15 = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); } else { $x0 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); $x5 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); $x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); $x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); } $x1 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 0, 4)); $x2 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 4, 4)); $x3 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 8, 4)); $x4 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 12, 4)); $x6 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); $x7 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); $x8 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); $x9 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); $x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 16, 4)); $x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 20, 4)); $x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 24, 4)); $x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 28, 4)); for ($i = self::ROUNDS; $i > 0; $i -= 2) { $x4 = $x4->xorInt32($x0->addInt32($x12)->rotateLeft(7)); $x8 = $x8->xorInt32($x4->addInt32($x0)->rotateLeft(9)); $x12 = $x12->xorInt32($x8->addInt32($x4)->rotateLeft(13)); $x0 = $x0->xorInt32($x12->addInt32($x8)->rotateLeft(18)); $x9 = $x9->xorInt32($x5->addInt32($x1)->rotateLeft(7)); $x13 = $x13->xorInt32($x9->addInt32($x5)->rotateLeft(9)); $x1 = $x1->xorInt32($x13->addInt32($x9)->rotateLeft(13)); $x5 = $x5->xorInt32($x1->addInt32($x13)->rotateLeft(18)); $x14 = $x14->xorInt32($x10->addInt32($x6)->rotateLeft(7)); $x2 = $x2->xorInt32($x14->addInt32($x10)->rotateLeft(9)); $x6 = $x6->xorInt32($x2->addInt32($x14)->rotateLeft(13)); $x10 = $x10->xorInt32($x6->addInt32($x2)->rotateLeft(18)); $x3 = $x3->xorInt32($x15->addInt32($x11)->rotateLeft(7)); $x7 = $x7->xorInt32($x3->addInt32($x15)->rotateLeft(9)); $x11 = $x11->xorInt32($x7->addInt32($x3)->rotateLeft(13)); $x15 = $x15->xorInt32($x11->addInt32($x7)->rotateLeft(18)); $x1 = $x1->xorInt32($x0->addInt32($x3)->rotateLeft(7)); $x2 = $x2->xorInt32($x1->addInt32($x0)->rotateLeft(9)); $x3 = $x3->xorInt32($x2->addInt32($x1)->rotateLeft(13)); $x0 = $x0->xorInt32($x3->addInt32($x2)->rotateLeft(18)); $x6 = $x6->xorInt32($x5->addInt32($x4)->rotateLeft(7)); $x7 = $x7->xorInt32($x6->addInt32($x5)->rotateLeft(9)); $x4 = $x4->xorInt32($x7->addInt32($x6)->rotateLeft(13)); $x5 = $x5->xorInt32($x4->addInt32($x7)->rotateLeft(18)); $x11 = $x11->xorInt32($x10->addInt32($x9)->rotateLeft(7)); $x8 = $x8->xorInt32($x11->addInt32($x10)->rotateLeft(9)); $x9 = $x9->xorInt32($x8->addInt32($x11)->rotateLeft(13)); $x10 = $x10->xorInt32($x9->addInt32($x8)->rotateLeft(18)); $x12 = $x12->xorInt32($x15->addInt32($x14)->rotateLeft(7)); $x13 = $x13->xorInt32($x12->addInt32($x15)->rotateLeft(9)); $x14 = $x14->xorInt32($x13->addInt32($x12)->rotateLeft(13)); $x15 = $x15->xorInt32($x14->addInt32($x13)->rotateLeft(18)); } return $x0->toReverseString() . $x5->toReverseString() . $x10->toReverseString() . $x15->toReverseString() . $x6->toReverseString() . $x7->toReverseString() . $x8->toReverseString() . $x9->toReverseString(); } } Core32/Salsa20.php 0000644 00000026362 15162213674 0007537 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_Salsa20', false)) { return; } /** * Class ParagonIE_Sodium_Core32_Salsa20 */ abstract class ParagonIE_Sodium_Core32_Salsa20 extends ParagonIE_Sodium_Core32_Util { const ROUNDS = 20; /** * Calculate an salsa20 hash of a single block * * @internal You should not use this directly from another application * * @param string $in * @param string $k * @param string|null $c * @return string * @throws SodiumException * @throws TypeError */ public static function core_salsa20($in, $k, $c = null) { /** * @var ParagonIE_Sodium_Core32_Int32 $x0 * @var ParagonIE_Sodium_Core32_Int32 $x1 * @var ParagonIE_Sodium_Core32_Int32 $x2 * @var ParagonIE_Sodium_Core32_Int32 $x3 * @var ParagonIE_Sodium_Core32_Int32 $x4 * @var ParagonIE_Sodium_Core32_Int32 $x5 * @var ParagonIE_Sodium_Core32_Int32 $x6 * @var ParagonIE_Sodium_Core32_Int32 $x7 * @var ParagonIE_Sodium_Core32_Int32 $x8 * @var ParagonIE_Sodium_Core32_Int32 $x9 * @var ParagonIE_Sodium_Core32_Int32 $x10 * @var ParagonIE_Sodium_Core32_Int32 $x11 * @var ParagonIE_Sodium_Core32_Int32 $x12 * @var ParagonIE_Sodium_Core32_Int32 $x13 * @var ParagonIE_Sodium_Core32_Int32 $x14 * @var ParagonIE_Sodium_Core32_Int32 $x15 * @var ParagonIE_Sodium_Core32_Int32 $j0 * @var ParagonIE_Sodium_Core32_Int32 $j1 * @var ParagonIE_Sodium_Core32_Int32 $j2 * @var ParagonIE_Sodium_Core32_Int32 $j3 * @var ParagonIE_Sodium_Core32_Int32 $j4 * @var ParagonIE_Sodium_Core32_Int32 $j5 * @var ParagonIE_Sodium_Core32_Int32 $j6 * @var ParagonIE_Sodium_Core32_Int32 $j7 * @var ParagonIE_Sodium_Core32_Int32 $j8 * @var ParagonIE_Sodium_Core32_Int32 $j9 * @var ParagonIE_Sodium_Core32_Int32 $j10 * @var ParagonIE_Sodium_Core32_Int32 $j11 * @var ParagonIE_Sodium_Core32_Int32 $j12 * @var ParagonIE_Sodium_Core32_Int32 $j13 * @var ParagonIE_Sodium_Core32_Int32 $j14 * @var ParagonIE_Sodium_Core32_Int32 $j15 */ if (self::strlen($k) < 32) { throw new RangeException('Key must be 32 bytes long'); } if ($c === null) { $x0 = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); $x5 = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); $x10 = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); $x15 = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); } else { $x0 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); $x5 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); $x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); $x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); } $x1 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 0, 4)); $x2 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 4, 4)); $x3 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 8, 4)); $x4 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 12, 4)); $x6 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); $x7 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); $x8 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); $x9 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); $x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 16, 4)); $x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 20, 4)); $x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 24, 4)); $x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 28, 4)); $j0 = clone $x0; $j1 = clone $x1; $j2 = clone $x2; $j3 = clone $x3; $j4 = clone $x4; $j5 = clone $x5; $j6 = clone $x6; $j7 = clone $x7; $j8 = clone $x8; $j9 = clone $x9; $j10 = clone $x10; $j11 = clone $x11; $j12 = clone $x12; $j13 = clone $x13; $j14 = clone $x14; $j15 = clone $x15; for ($i = self::ROUNDS; $i > 0; $i -= 2) { $x4 = $x4->xorInt32($x0->addInt32($x12)->rotateLeft(7)); $x8 = $x8->xorInt32($x4->addInt32($x0)->rotateLeft(9)); $x12 = $x12->xorInt32($x8->addInt32($x4)->rotateLeft(13)); $x0 = $x0->xorInt32($x12->addInt32($x8)->rotateLeft(18)); $x9 = $x9->xorInt32($x5->addInt32($x1)->rotateLeft(7)); $x13 = $x13->xorInt32($x9->addInt32($x5)->rotateLeft(9)); $x1 = $x1->xorInt32($x13->addInt32($x9)->rotateLeft(13)); $x5 = $x5->xorInt32($x1->addInt32($x13)->rotateLeft(18)); $x14 = $x14->xorInt32($x10->addInt32($x6)->rotateLeft(7)); $x2 = $x2->xorInt32($x14->addInt32($x10)->rotateLeft(9)); $x6 = $x6->xorInt32($x2->addInt32($x14)->rotateLeft(13)); $x10 = $x10->xorInt32($x6->addInt32($x2)->rotateLeft(18)); $x3 = $x3->xorInt32($x15->addInt32($x11)->rotateLeft(7)); $x7 = $x7->xorInt32($x3->addInt32($x15)->rotateLeft(9)); $x11 = $x11->xorInt32($x7->addInt32($x3)->rotateLeft(13)); $x15 = $x15->xorInt32($x11->addInt32($x7)->rotateLeft(18)); $x1 = $x1->xorInt32($x0->addInt32($x3)->rotateLeft(7)); $x2 = $x2->xorInt32($x1->addInt32($x0)->rotateLeft(9)); $x3 = $x3->xorInt32($x2->addInt32($x1)->rotateLeft(13)); $x0 = $x0->xorInt32($x3->addInt32($x2)->rotateLeft(18)); $x6 = $x6->xorInt32($x5->addInt32($x4)->rotateLeft(7)); $x7 = $x7->xorInt32($x6->addInt32($x5)->rotateLeft(9)); $x4 = $x4->xorInt32($x7->addInt32($x6)->rotateLeft(13)); $x5 = $x5->xorInt32($x4->addInt32($x7)->rotateLeft(18)); $x11 = $x11->xorInt32($x10->addInt32($x9)->rotateLeft(7)); $x8 = $x8->xorInt32($x11->addInt32($x10)->rotateLeft(9)); $x9 = $x9->xorInt32($x8->addInt32($x11)->rotateLeft(13)); $x10 = $x10->xorInt32($x9->addInt32($x8)->rotateLeft(18)); $x12 = $x12->xorInt32($x15->addInt32($x14)->rotateLeft(7)); $x13 = $x13->xorInt32($x12->addInt32($x15)->rotateLeft(9)); $x14 = $x14->xorInt32($x13->addInt32($x12)->rotateLeft(13)); $x15 = $x15->xorInt32($x14->addInt32($x13)->rotateLeft(18)); } $x0 = $x0->addInt32($j0); $x1 = $x1->addInt32($j1); $x2 = $x2->addInt32($j2); $x3 = $x3->addInt32($j3); $x4 = $x4->addInt32($j4); $x5 = $x5->addInt32($j5); $x6 = $x6->addInt32($j6); $x7 = $x7->addInt32($j7); $x8 = $x8->addInt32($j8); $x9 = $x9->addInt32($j9); $x10 = $x10->addInt32($j10); $x11 = $x11->addInt32($j11); $x12 = $x12->addInt32($j12); $x13 = $x13->addInt32($j13); $x14 = $x14->addInt32($j14); $x15 = $x15->addInt32($j15); return $x0->toReverseString() . $x1->toReverseString() . $x2->toReverseString() . $x3->toReverseString() . $x4->toReverseString() . $x5->toReverseString() . $x6->toReverseString() . $x7->toReverseString() . $x8->toReverseString() . $x9->toReverseString() . $x10->toReverseString() . $x11->toReverseString() . $x12->toReverseString() . $x13->toReverseString() . $x14->toReverseString() . $x15->toReverseString(); } /** * @internal You should not use this directly from another application * * @param int $len * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function salsa20($len, $nonce, $key) { if (self::strlen($key) !== 32) { throw new RangeException('Key must be 32 bytes long'); } $kcopy = '' . $key; $in = self::substr($nonce, 0, 8) . str_repeat("\0", 8); $c = ''; while ($len >= 64) { $c .= self::core_salsa20($in, $kcopy, null); $u = 1; // Internal counter. for ($i = 8; $i < 16; ++$i) { $u += self::chrToInt($in[$i]); $in[$i] = self::intToChr($u & 0xff); $u >>= 8; } $len -= 64; } if ($len > 0) { $c .= self::substr( self::core_salsa20($in, $kcopy, null), 0, $len ); } try { ParagonIE_Sodium_Compat::memzero($kcopy); } catch (SodiumException $ex) { $kcopy = null; } return $c; } /** * @internal You should not use this directly from another application * * @param string $m * @param string $n * @param int $ic * @param string $k * @return string * @throws SodiumException * @throws TypeError */ public static function salsa20_xor_ic($m, $n, $ic, $k) { $mlen = self::strlen($m); if ($mlen < 1) { return ''; } $kcopy = self::substr($k, 0, 32); $in = self::substr($n, 0, 8); // Initialize the counter $in .= ParagonIE_Sodium_Core32_Util::store64_le($ic); $c = ''; while ($mlen >= 64) { $block = self::core_salsa20($in, $kcopy, null); $c .= self::xorStrings( self::substr($m, 0, 64), self::substr($block, 0, 64) ); $u = 1; for ($i = 8; $i < 16; ++$i) { $u += self::chrToInt($in[$i]); $in[$i] = self::intToChr($u & 0xff); $u >>= 8; } $mlen -= 64; $m = self::substr($m, 64); } if ($mlen) { $block = self::core_salsa20($in, $kcopy, null); $c .= self::xorStrings( self::substr($m, 0, $mlen), self::substr($block, 0, $mlen) ); } try { ParagonIE_Sodium_Compat::memzero($block); ParagonIE_Sodium_Compat::memzero($kcopy); } catch (SodiumException $ex) { $block = null; $kcopy = null; } return $c; } /** * @internal You should not use this directly from another application * * @param string $message * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function salsa20_xor($message, $nonce, $key) { return self::xorStrings( $message, self::salsa20( self::strlen($message), $nonce, $key ) ); } } Core32/Poly1305/error_log 0000644 00000003644 15162213676 0010772 0 ustar 00 [24-Mar-2026 23:41:51 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305/State.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305/State.php on line 10 [25-Mar-2026 03:07:05 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305/State.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305/State.php on line 10 [25-Mar-2026 15:06:54 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305/State.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305/State.php on line 10 [26-Mar-2026 09:09:36 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305/State.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305/State.php on line 10 [26-Mar-2026 14:06:57 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305/State.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305/State.php on line 10 [28-Mar-2026 09:25:59 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core32_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305/State.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core32/Poly1305/State.php on line 10 Core32/Poly1305/State.php 0000644 00000037135 15162213676 0010650 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_Poly1305_State', false)) { return; } /** * Class ParagonIE_Sodium_Core32_Poly1305_State */ class ParagonIE_Sodium_Core32_Poly1305_State extends ParagonIE_Sodium_Core32_Util { /** * @var array<int, int> */ protected $buffer = array(); /** * @var bool */ protected $final = false; /** * @var array<int, ParagonIE_Sodium_Core32_Int32> */ public $h; /** * @var int */ protected $leftover = 0; /** * @var array<int, ParagonIE_Sodium_Core32_Int32> */ public $r; /** * @var array<int, ParagonIE_Sodium_Core32_Int64> */ public $pad; /** * ParagonIE_Sodium_Core32_Poly1305_State constructor. * * @internal You should not use this directly from another application * * @param string $key * @throws InvalidArgumentException * @throws SodiumException * @throws TypeError */ public function __construct($key = '') { if (self::strlen($key) < 32) { throw new InvalidArgumentException( 'Poly1305 requires a 32-byte key' ); } /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ $this->r = array( // st->r[0] = ... ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)) ->setUnsignedInt(true) ->mask(0x3ffffff), // st->r[1] = ... ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 3, 4)) ->setUnsignedInt(true) ->shiftRight(2) ->mask(0x3ffff03), // st->r[2] = ... ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 6, 4)) ->setUnsignedInt(true) ->shiftRight(4) ->mask(0x3ffc0ff), // st->r[3] = ... ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 9, 4)) ->setUnsignedInt(true) ->shiftRight(6) ->mask(0x3f03fff), // st->r[4] = ... ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)) ->setUnsignedInt(true) ->shiftRight(8) ->mask(0x00fffff) ); /* h = 0 */ $this->h = array( new ParagonIE_Sodium_Core32_Int32(array(0, 0), true), new ParagonIE_Sodium_Core32_Int32(array(0, 0), true), new ParagonIE_Sodium_Core32_Int32(array(0, 0), true), new ParagonIE_Sodium_Core32_Int32(array(0, 0), true), new ParagonIE_Sodium_Core32_Int32(array(0, 0), true) ); /* save pad for later */ $this->pad = array( ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)) ->setUnsignedInt(true)->toInt64(), ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)) ->setUnsignedInt(true)->toInt64(), ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)) ->setUnsignedInt(true)->toInt64(), ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)) ->setUnsignedInt(true)->toInt64(), ); $this->leftover = 0; $this->final = false; } /** * @internal You should not use this directly from another application * * @param string $message * @return self * @throws SodiumException * @throws TypeError */ public function update($message = '') { $bytes = self::strlen($message); /* handle leftover */ if ($this->leftover) { /** @var int $want */ $want = ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE - $this->leftover; if ($want > $bytes) { $want = $bytes; } for ($i = 0; $i < $want; ++$i) { $mi = self::chrToInt($message[$i]); $this->buffer[$this->leftover + $i] = $mi; } // We snip off the leftmost bytes. $message = self::substr($message, $want); $bytes = self::strlen($message); $this->leftover += $want; if ($this->leftover < ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { // We still don't have enough to run $this->blocks() return $this; } $this->blocks( self::intArrayToString($this->buffer), ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE ); $this->leftover = 0; } /* process full blocks */ if ($bytes >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { /** @var int $want */ $want = $bytes & ~(ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE - 1); if ($want >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { /** @var string $block */ $block = self::substr($message, 0, $want); if (self::strlen($block) >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { $this->blocks($block, $want); $message = self::substr($message, $want); $bytes = self::strlen($message); } } } /* store leftover */ if ($bytes) { for ($i = 0; $i < $bytes; ++$i) { $mi = self::chrToInt($message[$i]); $this->buffer[$this->leftover + $i] = $mi; } $this->leftover = (int) $this->leftover + $bytes; } return $this; } /** * @internal You should not use this directly from another application * * @param string $message * @param int $bytes * @return self * @throws SodiumException * @throws TypeError */ public function blocks($message, $bytes) { if (self::strlen($message) < 16) { $message = str_pad($message, 16, "\x00", STR_PAD_RIGHT); } $hibit = ParagonIE_Sodium_Core32_Int32::fromInt((int) ($this->final ? 0 : 1 << 24)); /* 1 << 128 */ $hibit->setUnsignedInt(true); $zero = new ParagonIE_Sodium_Core32_Int64(array(0, 0, 0, 0), true); /** * @var ParagonIE_Sodium_Core32_Int64 $d0 * @var ParagonIE_Sodium_Core32_Int64 $d1 * @var ParagonIE_Sodium_Core32_Int64 $d2 * @var ParagonIE_Sodium_Core32_Int64 $d3 * @var ParagonIE_Sodium_Core32_Int64 $d4 * @var ParagonIE_Sodium_Core32_Int64 $r0 * @var ParagonIE_Sodium_Core32_Int64 $r1 * @var ParagonIE_Sodium_Core32_Int64 $r2 * @var ParagonIE_Sodium_Core32_Int64 $r3 * @var ParagonIE_Sodium_Core32_Int64 $r4 * * @var ParagonIE_Sodium_Core32_Int32 $h0 * @var ParagonIE_Sodium_Core32_Int32 $h1 * @var ParagonIE_Sodium_Core32_Int32 $h2 * @var ParagonIE_Sodium_Core32_Int32 $h3 * @var ParagonIE_Sodium_Core32_Int32 $h4 */ $r0 = $this->r[0]->toInt64(); $r1 = $this->r[1]->toInt64(); $r2 = $this->r[2]->toInt64(); $r3 = $this->r[3]->toInt64(); $r4 = $this->r[4]->toInt64(); $s1 = $r1->toInt64()->mulInt(5, 3); $s2 = $r2->toInt64()->mulInt(5, 3); $s3 = $r3->toInt64()->mulInt(5, 3); $s4 = $r4->toInt64()->mulInt(5, 3); $h0 = $this->h[0]; $h1 = $this->h[1]; $h2 = $this->h[2]; $h3 = $this->h[3]; $h4 = $this->h[4]; while ($bytes >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { /* h += m[i] */ $h0 = $h0->addInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4)) ->mask(0x3ffffff) )->toInt64(); $h1 = $h1->addInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 3, 4)) ->shiftRight(2) ->mask(0x3ffffff) )->toInt64(); $h2 = $h2->addInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 6, 4)) ->shiftRight(4) ->mask(0x3ffffff) )->toInt64(); $h3 = $h3->addInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 9, 4)) ->shiftRight(6) ->mask(0x3ffffff) )->toInt64(); $h4 = $h4->addInt32( ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4)) ->shiftRight(8) ->orInt32($hibit) )->toInt64(); /* h *= r */ $d0 = $zero ->addInt64($h0->mulInt64($r0, 27)) ->addInt64($s4->mulInt64($h1, 27)) ->addInt64($s3->mulInt64($h2, 27)) ->addInt64($s2->mulInt64($h3, 27)) ->addInt64($s1->mulInt64($h4, 27)); $d1 = $zero ->addInt64($h0->mulInt64($r1, 27)) ->addInt64($h1->mulInt64($r0, 27)) ->addInt64($s4->mulInt64($h2, 27)) ->addInt64($s3->mulInt64($h3, 27)) ->addInt64($s2->mulInt64($h4, 27)); $d2 = $zero ->addInt64($h0->mulInt64($r2, 27)) ->addInt64($h1->mulInt64($r1, 27)) ->addInt64($h2->mulInt64($r0, 27)) ->addInt64($s4->mulInt64($h3, 27)) ->addInt64($s3->mulInt64($h4, 27)); $d3 = $zero ->addInt64($h0->mulInt64($r3, 27)) ->addInt64($h1->mulInt64($r2, 27)) ->addInt64($h2->mulInt64($r1, 27)) ->addInt64($h3->mulInt64($r0, 27)) ->addInt64($s4->mulInt64($h4, 27)); $d4 = $zero ->addInt64($h0->mulInt64($r4, 27)) ->addInt64($h1->mulInt64($r3, 27)) ->addInt64($h2->mulInt64($r2, 27)) ->addInt64($h3->mulInt64($r1, 27)) ->addInt64($h4->mulInt64($r0, 27)); /* (partial) h %= p */ $c = $d0->shiftRight(26); $h0 = $d0->toInt32()->mask(0x3ffffff); $d1 = $d1->addInt64($c); $c = $d1->shiftRight(26); $h1 = $d1->toInt32()->mask(0x3ffffff); $d2 = $d2->addInt64($c); $c = $d2->shiftRight(26); $h2 = $d2->toInt32()->mask(0x3ffffff); $d3 = $d3->addInt64($c); $c = $d3->shiftRight(26); $h3 = $d3->toInt32()->mask(0x3ffffff); $d4 = $d4->addInt64($c); $c = $d4->shiftRight(26); $h4 = $d4->toInt32()->mask(0x3ffffff); $h0 = $h0->addInt32($c->toInt32()->mulInt(5, 3)); $c = $h0->shiftRight(26); $h0 = $h0->mask(0x3ffffff); $h1 = $h1->addInt32($c); // Chop off the left 32 bytes. $message = self::substr( $message, ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE ); $bytes -= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE; } /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h */ $this->h = array($h0, $h1, $h2, $h3, $h4); return $this; } /** * @internal You should not use this directly from another application * * @return string * @throws SodiumException * @throws TypeError */ public function finish() { /* process the remaining block */ if ($this->leftover) { $i = $this->leftover; $this->buffer[$i++] = 1; for (; $i < ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE; ++$i) { $this->buffer[$i] = 0; } $this->final = true; $this->blocks( self::substr( self::intArrayToString($this->buffer), 0, ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE ), $b = ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE ); } /** * @var ParagonIE_Sodium_Core32_Int32 $f * @var ParagonIE_Sodium_Core32_Int32 $g0 * @var ParagonIE_Sodium_Core32_Int32 $g1 * @var ParagonIE_Sodium_Core32_Int32 $g2 * @var ParagonIE_Sodium_Core32_Int32 $g3 * @var ParagonIE_Sodium_Core32_Int32 $g4 * @var ParagonIE_Sodium_Core32_Int32 $h0 * @var ParagonIE_Sodium_Core32_Int32 $h1 * @var ParagonIE_Sodium_Core32_Int32 $h2 * @var ParagonIE_Sodium_Core32_Int32 $h3 * @var ParagonIE_Sodium_Core32_Int32 $h4 */ $h0 = $this->h[0]; $h1 = $this->h[1]; $h2 = $this->h[2]; $h3 = $this->h[3]; $h4 = $this->h[4]; $c = $h1->shiftRight(26); # $c = $h1 >> 26; $h1 = $h1->mask(0x3ffffff); # $h1 &= 0x3ffffff; $h2 = $h2->addInt32($c); # $h2 += $c; $c = $h2->shiftRight(26); # $c = $h2 >> 26; $h2 = $h2->mask(0x3ffffff); # $h2 &= 0x3ffffff; $h3 = $h3->addInt32($c); # $h3 += $c; $c = $h3->shiftRight(26); # $c = $h3 >> 26; $h3 = $h3->mask(0x3ffffff); # $h3 &= 0x3ffffff; $h4 = $h4->addInt32($c); # $h4 += $c; $c = $h4->shiftRight(26); # $c = $h4 >> 26; $h4 = $h4->mask(0x3ffffff); # $h4 &= 0x3ffffff; $h0 = $h0->addInt32($c->mulInt(5, 3)); # $h0 += self::mul($c, 5); $c = $h0->shiftRight(26); # $c = $h0 >> 26; $h0 = $h0->mask(0x3ffffff); # $h0 &= 0x3ffffff; $h1 = $h1->addInt32($c); # $h1 += $c; /* compute h + -p */ $g0 = $h0->addInt(5); $c = $g0->shiftRight(26); $g0 = $g0->mask(0x3ffffff); $g1 = $h1->addInt32($c); $c = $g1->shiftRight(26); $g1 = $g1->mask(0x3ffffff); $g2 = $h2->addInt32($c); $c = $g2->shiftRight(26); $g2 = $g2->mask(0x3ffffff); $g3 = $h3->addInt32($c); $c = $g3->shiftRight(26); $g3 = $g3->mask(0x3ffffff); $g4 = $h4->addInt32($c)->subInt(1 << 26); # $mask = ($g4 >> 31) - 1; /* select h if h < p, or h + -p if h >= p */ $mask = (int) (($g4->toInt() >> 31) + 1); $g0 = $g0->mask($mask); $g1 = $g1->mask($mask); $g2 = $g2->mask($mask); $g3 = $g3->mask($mask); $g4 = $g4->mask($mask); /** @var int $mask */ $mask = ~$mask; $h0 = $h0->mask($mask)->orInt32($g0); $h1 = $h1->mask($mask)->orInt32($g1); $h2 = $h2->mask($mask)->orInt32($g2); $h3 = $h3->mask($mask)->orInt32($g3); $h4 = $h4->mask($mask)->orInt32($g4); /* h = h % (2^128) */ $h0 = $h0->orInt32($h1->shiftLeft(26)); $h1 = $h1->shiftRight(6)->orInt32($h2->shiftLeft(20)); $h2 = $h2->shiftRight(12)->orInt32($h3->shiftLeft(14)); $h3 = $h3->shiftRight(18)->orInt32($h4->shiftLeft(8)); /* mac = (h + pad) % (2^128) */ $f = $h0->toInt64()->addInt64($this->pad[0]); $h0 = $f->toInt32(); $f = $h1->toInt64()->addInt64($this->pad[1])->addInt($h0->overflow); $h1 = $f->toInt32(); $f = $h2->toInt64()->addInt64($this->pad[2])->addInt($h1->overflow); $h2 = $f->toInt32(); $f = $h3->toInt64()->addInt64($this->pad[3])->addInt($h2->overflow); $h3 = $f->toInt32(); return $h0->toReverseString() . $h1->toReverseString() . $h2->toReverseString() . $h3->toReverseString(); } } Core32/Poly1305.php 0000644 00000003062 15162213677 0007561 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_Poly1305', false)) { return; } /** * Class ParagonIE_Sodium_Core32_Poly1305 */ abstract class ParagonIE_Sodium_Core32_Poly1305 extends ParagonIE_Sodium_Core32_Util { const BLOCK_SIZE = 16; /** * @internal You should not use this directly from another application * * @param string $m * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function onetimeauth($m, $key) { if (self::strlen($key) < 32) { throw new InvalidArgumentException( 'Key must be 32 bytes long.' ); } $state = new ParagonIE_Sodium_Core32_Poly1305_State( self::substr($key, 0, 32) ); return $state ->update($m) ->finish(); } /** * @internal You should not use this directly from another application * * @param string $mac * @param string $m * @param string $key * @return bool * @throws SodiumException * @throws TypeError */ public static function onetimeauth_verify($mac, $m, $key) { if (self::strlen($key) < 32) { throw new InvalidArgumentException( 'Key must be 32 bytes long.' ); } $state = new ParagonIE_Sodium_Core32_Poly1305_State( self::substr($key, 0, 32) ); $calc = $state ->update($m) ->finish(); return self::verify_16($calc, $mac); } } Core32/XSalsa20.php 0000644 00000002543 15162213700 0007650 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core32_XSalsa20', false)) { return; } /** * Class ParagonIE_Sodium_Core32_XSalsa20 */ abstract class ParagonIE_Sodium_Core32_XSalsa20 extends ParagonIE_Sodium_Core32_HSalsa20 { /** * Expand a key and nonce into an xsalsa20 keystream. * * @internal You should not use this directly from another application * * @param int $len * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function xsalsa20($len, $nonce, $key) { $ret = self::salsa20( $len, self::substr($nonce, 16, 8), self::hsalsa20($nonce, $key) ); return $ret; } /** * Encrypt a string with XSalsa20. Doesn't provide integrity. * * @internal You should not use this directly from another application * * @param string $message * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function xsalsa20_xor($message, $nonce, $key) { return self::xorStrings( $message, self::xsalsa20( self::strlen($message), $nonce, $key ) ); } } Core/Curve25519.php 0000644 00000430431 15162213701 0007642 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_Curve25519', false)) { return; } /** * Class ParagonIE_Sodium_Core_Curve25519 * * Implements Curve25519 core functions * * Based on the ref10 curve25519 code provided by libsodium * * @ref https://github.com/jedisct1/libsodium/blob/master/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.c */ abstract class ParagonIE_Sodium_Core_Curve25519 extends ParagonIE_Sodium_Core_Curve25519_H { /** * Get a field element of size 10 with a value of 0 * * @internal You should not use this directly from another application * * @return ParagonIE_Sodium_Core_Curve25519_Fe */ public static function fe_0() { return new ParagonIE_Sodium_Core_Curve25519_Fe(); } /** * Get a field element of size 10 with a value of 1 * * @internal You should not use this directly from another application * * @return ParagonIE_Sodium_Core_Curve25519_Fe */ public static function fe_1() { $fe = new ParagonIE_Sodium_Core_Curve25519_Fe(); $fe->e0 = 1; return $fe; } /** * Add two field elements. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @param ParagonIE_Sodium_Core_Curve25519_Fe $g * @return ParagonIE_Sodium_Core_Curve25519_Fe * @psalm-suppress MixedAssignment * @psalm-suppress MixedOperand */ public static function fe_add( ParagonIE_Sodium_Core_Curve25519_Fe $f, ParagonIE_Sodium_Core_Curve25519_Fe $g ) { return new ParagonIE_Sodium_Core_Curve25519_Fe( (int)($f->e0 + $g->e0), (int)($f->e1 + $g->e1), (int)($f->e2 + $g->e2), (int)($f->e3 + $g->e3), (int)($f->e4 + $g->e4), (int)($f->e5 + $g->e5), (int)($f->e6 + $g->e6), (int)($f->e7 + $g->e7), (int)($f->e8 + $g->e8), (int)($f->e9 + $g->e9) ); } /** * Constant-time conditional move. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @param ParagonIE_Sodium_Core_Curve25519_Fe $g * @param int $b * @return ParagonIE_Sodium_Core_Curve25519_Fe * @psalm-suppress MixedAssignment */ public static function fe_cmov( ParagonIE_Sodium_Core_Curve25519_Fe $f, ParagonIE_Sodium_Core_Curve25519_Fe $g, $b = 0 ) { $h = new ParagonIE_Sodium_Core_Curve25519_Fe(); $b *= -1; $x = (($f->e0 ^ $g->e0) & $b); $h->e0 = $f->e0 ^ $x; $x = (($f->e1 ^ $g->e1) & $b); $h->e1 = $f->e1 ^ $x; $x = (($f->e2 ^ $g->e2) & $b); $h->e2 = $f->e2 ^ $x; $x = (($f->e3 ^ $g->e3) & $b); $h->e3 = $f->e3 ^ $x; $x = (($f->e4 ^ $g->e4) & $b); $h->e4 = $f->e4 ^ $x; $x = (($f->e5 ^ $g->e5) & $b); $h->e5 = $f->e5 ^ $x; $x = (($f->e6 ^ $g->e6) & $b); $h->e6 = $f->e6 ^ $x; $x = (($f->e7 ^ $g->e7) & $b); $h->e7 = $f->e7 ^ $x; $x = (($f->e8 ^ $g->e8) & $b); $h->e8 = $f->e8 ^ $x; $x = (($f->e9 ^ $g->e9) & $b); $h->e9 = $f->e9 ^ $x; return $h; } /** * Create a copy of a field element. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @return ParagonIE_Sodium_Core_Curve25519_Fe */ public static function fe_copy(ParagonIE_Sodium_Core_Curve25519_Fe $f) { return clone $f; } /** * Give: 32-byte string. * Receive: A field element object to use for internal calculations. * * @internal You should not use this directly from another application * * @param string $s * @return ParagonIE_Sodium_Core_Curve25519_Fe * @throws RangeException * @throws TypeError */ public static function fe_frombytes($s) { if (self::strlen($s) !== 32) { throw new RangeException('Expected a 32-byte string.'); } $h0 = self::load_4($s); $h1 = self::load_3(self::substr($s, 4, 3)) << 6; $h2 = self::load_3(self::substr($s, 7, 3)) << 5; $h3 = self::load_3(self::substr($s, 10, 3)) << 3; $h4 = self::load_3(self::substr($s, 13, 3)) << 2; $h5 = self::load_4(self::substr($s, 16, 4)); $h6 = self::load_3(self::substr($s, 20, 3)) << 7; $h7 = self::load_3(self::substr($s, 23, 3)) << 5; $h8 = self::load_3(self::substr($s, 26, 3)) << 4; $h9 = (self::load_3(self::substr($s, 29, 3)) & 8388607) << 2; $carry9 = ($h9 + (1 << 24)) >> 25; $h0 += self::mul($carry9, 19, 5); $h9 -= $carry9 << 25; $carry1 = ($h1 + (1 << 24)) >> 25; $h2 += $carry1; $h1 -= $carry1 << 25; $carry3 = ($h3 + (1 << 24)) >> 25; $h4 += $carry3; $h3 -= $carry3 << 25; $carry5 = ($h5 + (1 << 24)) >> 25; $h6 += $carry5; $h5 -= $carry5 << 25; $carry7 = ($h7 + (1 << 24)) >> 25; $h8 += $carry7; $h7 -= $carry7 << 25; $carry0 = ($h0 + (1 << 25)) >> 26; $h1 += $carry0; $h0 -= $carry0 << 26; $carry2 = ($h2 + (1 << 25)) >> 26; $h3 += $carry2; $h2 -= $carry2 << 26; $carry4 = ($h4 + (1 << 25)) >> 26; $h5 += $carry4; $h4 -= $carry4 << 26; $carry6 = ($h6 + (1 << 25)) >> 26; $h7 += $carry6; $h6 -= $carry6 << 26; $carry8 = ($h8 + (1 << 25)) >> 26; $h9 += $carry8; $h8 -= $carry8 << 26; return new ParagonIE_Sodium_Core_Curve25519_Fe( (int) $h0, (int) $h1, (int) $h2, (int) $h3, (int) $h4, (int) $h5, (int) $h6, (int) $h7, (int) $h8, (int) $h9 ); } /** * Convert a field element to a byte string. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $h * @return string */ public static function fe_tobytes(ParagonIE_Sodium_Core_Curve25519_Fe $h) { $h0 = (int) $h->e0; $h1 = (int) $h->e1; $h2 = (int) $h->e2; $h3 = (int) $h->e3; $h4 = (int) $h->e4; $h5 = (int) $h->e5; $h6 = (int) $h->e6; $h7 = (int) $h->e7; $h8 = (int) $h->e8; $h9 = (int) $h->e9; $q = (self::mul($h9, 19, 5) + (1 << 24)) >> 25; $q = ($h0 + $q) >> 26; $q = ($h1 + $q) >> 25; $q = ($h2 + $q) >> 26; $q = ($h3 + $q) >> 25; $q = ($h4 + $q) >> 26; $q = ($h5 + $q) >> 25; $q = ($h6 + $q) >> 26; $q = ($h7 + $q) >> 25; $q = ($h8 + $q) >> 26; $q = ($h9 + $q) >> 25; $h0 += self::mul($q, 19, 5); $carry0 = $h0 >> 26; $h1 += $carry0; $h0 -= $carry0 << 26; $carry1 = $h1 >> 25; $h2 += $carry1; $h1 -= $carry1 << 25; $carry2 = $h2 >> 26; $h3 += $carry2; $h2 -= $carry2 << 26; $carry3 = $h3 >> 25; $h4 += $carry3; $h3 -= $carry3 << 25; $carry4 = $h4 >> 26; $h5 += $carry4; $h4 -= $carry4 << 26; $carry5 = $h5 >> 25; $h6 += $carry5; $h5 -= $carry5 << 25; $carry6 = $h6 >> 26; $h7 += $carry6; $h6 -= $carry6 << 26; $carry7 = $h7 >> 25; $h8 += $carry7; $h7 -= $carry7 << 25; $carry8 = $h8 >> 26; $h9 += $carry8; $h8 -= $carry8 << 26; $carry9 = $h9 >> 25; $h9 -= $carry9 << 25; /** * @var array<int, int> */ $s = array( (int) (($h0 >> 0) & 0xff), (int) (($h0 >> 8) & 0xff), (int) (($h0 >> 16) & 0xff), (int) ((($h0 >> 24) | ($h1 << 2)) & 0xff), (int) (($h1 >> 6) & 0xff), (int) (($h1 >> 14) & 0xff), (int) ((($h1 >> 22) | ($h2 << 3)) & 0xff), (int) (($h2 >> 5) & 0xff), (int) (($h2 >> 13) & 0xff), (int) ((($h2 >> 21) | ($h3 << 5)) & 0xff), (int) (($h3 >> 3) & 0xff), (int) (($h3 >> 11) & 0xff), (int) ((($h3 >> 19) | ($h4 << 6)) & 0xff), (int) (($h4 >> 2) & 0xff), (int) (($h4 >> 10) & 0xff), (int) (($h4 >> 18) & 0xff), (int) (($h5 >> 0) & 0xff), (int) (($h5 >> 8) & 0xff), (int) (($h5 >> 16) & 0xff), (int) ((($h5 >> 24) | ($h6 << 1)) & 0xff), (int) (($h6 >> 7) & 0xff), (int) (($h6 >> 15) & 0xff), (int) ((($h6 >> 23) | ($h7 << 3)) & 0xff), (int) (($h7 >> 5) & 0xff), (int) (($h7 >> 13) & 0xff), (int) ((($h7 >> 21) | ($h8 << 4)) & 0xff), (int) (($h8 >> 4) & 0xff), (int) (($h8 >> 12) & 0xff), (int) ((($h8 >> 20) | ($h9 << 6)) & 0xff), (int) (($h9 >> 2) & 0xff), (int) (($h9 >> 10) & 0xff), (int) (($h9 >> 18) & 0xff) ); return self::intArrayToString($s); } /** * Is a field element negative? (1 = yes, 0 = no. Used in calculations.) * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @return int * @throws SodiumException * @throws TypeError */ public static function fe_isnegative(ParagonIE_Sodium_Core_Curve25519_Fe $f) { $str = self::fe_tobytes($f); return (int) (self::chrToInt($str[0]) & 1); } /** * Returns 0 if this field element results in all NUL bytes. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @return bool * @throws SodiumException * @throws TypeError */ public static function fe_isnonzero(ParagonIE_Sodium_Core_Curve25519_Fe $f) { static $zero; if ($zero === null) { $zero = str_repeat("\x00", 32); } /** @var string $zero */ /** @var string $str */ $str = self::fe_tobytes($f); return !self::verify_32($str, (string) $zero); } /** * Multiply two field elements * * h = f * g * * @internal You should not use this directly from another application * * @security Is multiplication a source of timing leaks? If so, can we do * anything to prevent that from happening? * * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @param ParagonIE_Sodium_Core_Curve25519_Fe $g * @return ParagonIE_Sodium_Core_Curve25519_Fe */ public static function fe_mul( ParagonIE_Sodium_Core_Curve25519_Fe $f, ParagonIE_Sodium_Core_Curve25519_Fe $g ) { // Ensure limbs aren't oversized. $f = self::fe_normalize($f); $g = self::fe_normalize($g); $f0 = $f->e0; $f1 = $f->e1; $f2 = $f->e2; $f3 = $f->e3; $f4 = $f->e4; $f5 = $f->e5; $f6 = $f->e6; $f7 = $f->e7; $f8 = $f->e8; $f9 = $f->e9; $g0 = $g->e0; $g1 = $g->e1; $g2 = $g->e2; $g3 = $g->e3; $g4 = $g->e4; $g5 = $g->e5; $g6 = $g->e6; $g7 = $g->e7; $g8 = $g->e8; $g9 = $g->e9; $g1_19 = self::mul($g1, 19, 5); $g2_19 = self::mul($g2, 19, 5); $g3_19 = self::mul($g3, 19, 5); $g4_19 = self::mul($g4, 19, 5); $g5_19 = self::mul($g5, 19, 5); $g6_19 = self::mul($g6, 19, 5); $g7_19 = self::mul($g7, 19, 5); $g8_19 = self::mul($g8, 19, 5); $g9_19 = self::mul($g9, 19, 5); $f1_2 = $f1 << 1; $f3_2 = $f3 << 1; $f5_2 = $f5 << 1; $f7_2 = $f7 << 1; $f9_2 = $f9 << 1; $f0g0 = self::mul($f0, $g0, 26); $f0g1 = self::mul($f0, $g1, 25); $f0g2 = self::mul($f0, $g2, 26); $f0g3 = self::mul($f0, $g3, 25); $f0g4 = self::mul($f0, $g4, 26); $f0g5 = self::mul($f0, $g5, 25); $f0g6 = self::mul($f0, $g6, 26); $f0g7 = self::mul($f0, $g7, 25); $f0g8 = self::mul($f0, $g8, 26); $f0g9 = self::mul($f0, $g9, 26); $f1g0 = self::mul($f1, $g0, 26); $f1g1_2 = self::mul($f1_2, $g1, 25); $f1g2 = self::mul($f1, $g2, 26); $f1g3_2 = self::mul($f1_2, $g3, 25); $f1g4 = self::mul($f1, $g4, 26); $f1g5_2 = self::mul($f1_2, $g5, 25); $f1g6 = self::mul($f1, $g6, 26); $f1g7_2 = self::mul($f1_2, $g7, 25); $f1g8 = self::mul($f1, $g8, 26); $f1g9_38 = self::mul($g9_19, $f1_2, 26); $f2g0 = self::mul($f2, $g0, 26); $f2g1 = self::mul($f2, $g1, 25); $f2g2 = self::mul($f2, $g2, 26); $f2g3 = self::mul($f2, $g3, 25); $f2g4 = self::mul($f2, $g4, 26); $f2g5 = self::mul($f2, $g5, 25); $f2g6 = self::mul($f2, $g6, 26); $f2g7 = self::mul($f2, $g7, 25); $f2g8_19 = self::mul($g8_19, $f2, 26); $f2g9_19 = self::mul($g9_19, $f2, 26); $f3g0 = self::mul($f3, $g0, 26); $f3g1_2 = self::mul($f3_2, $g1, 25); $f3g2 = self::mul($f3, $g2, 26); $f3g3_2 = self::mul($f3_2, $g3, 25); $f3g4 = self::mul($f3, $g4, 26); $f3g5_2 = self::mul($f3_2, $g5, 25); $f3g6 = self::mul($f3, $g6, 26); $f3g7_38 = self::mul($g7_19, $f3_2, 26); $f3g8_19 = self::mul($g8_19, $f3, 25); $f3g9_38 = self::mul($g9_19, $f3_2, 26); $f4g0 = self::mul($f4, $g0, 26); $f4g1 = self::mul($f4, $g1, 25); $f4g2 = self::mul($f4, $g2, 26); $f4g3 = self::mul($f4, $g3, 25); $f4g4 = self::mul($f4, $g4, 26); $f4g5 = self::mul($f4, $g5, 25); $f4g6_19 = self::mul($g6_19, $f4, 26); $f4g7_19 = self::mul($g7_19, $f4, 26); $f4g8_19 = self::mul($g8_19, $f4, 26); $f4g9_19 = self::mul($g9_19, $f4, 26); $f5g0 = self::mul($f5, $g0, 26); $f5g1_2 = self::mul($f5_2, $g1, 25); $f5g2 = self::mul($f5, $g2, 26); $f5g3_2 = self::mul($f5_2, $g3, 25); $f5g4 = self::mul($f5, $g4, 26); $f5g5_38 = self::mul($g5_19, $f5_2, 26); $f5g6_19 = self::mul($g6_19, $f5, 25); $f5g7_38 = self::mul($g7_19, $f5_2, 26); $f5g8_19 = self::mul($g8_19, $f5, 25); $f5g9_38 = self::mul($g9_19, $f5_2, 26); $f6g0 = self::mul($f6, $g0, 26); $f6g1 = self::mul($f6, $g1, 25); $f6g2 = self::mul($f6, $g2, 26); $f6g3 = self::mul($f6, $g3, 25); $f6g4_19 = self::mul($g4_19, $f6, 26); $f6g5_19 = self::mul($g5_19, $f6, 26); $f6g6_19 = self::mul($g6_19, $f6, 26); $f6g7_19 = self::mul($g7_19, $f6, 26); $f6g8_19 = self::mul($g8_19, $f6, 26); $f6g9_19 = self::mul($g9_19, $f6, 26); $f7g0 = self::mul($f7, $g0, 26); $f7g1_2 = self::mul($f7_2, $g1, 25); $f7g2 = self::mul($f7, $g2, 26); $f7g3_38 = self::mul($g3_19, $f7_2, 26); $f7g4_19 = self::mul($g4_19, $f7, 26); $f7g5_38 = self::mul($g5_19, $f7_2, 26); $f7g6_19 = self::mul($g6_19, $f7, 25); $f7g7_38 = self::mul($g7_19, $f7_2, 26); $f7g8_19 = self::mul($g8_19, $f7, 25); $f7g9_38 = self::mul($g9_19,$f7_2, 26); $f8g0 = self::mul($f8, $g0, 26); $f8g1 = self::mul($f8, $g1, 25); $f8g2_19 = self::mul($g2_19, $f8, 26); $f8g3_19 = self::mul($g3_19, $f8, 26); $f8g4_19 = self::mul($g4_19, $f8, 26); $f8g5_19 = self::mul($g5_19, $f8, 26); $f8g6_19 = self::mul($g6_19, $f8, 26); $f8g7_19 = self::mul($g7_19, $f8, 26); $f8g8_19 = self::mul($g8_19, $f8, 26); $f8g9_19 = self::mul($g9_19, $f8, 26); $f9g0 = self::mul($f9, $g0, 26); $f9g1_38 = self::mul($g1_19, $f9_2, 26); $f9g2_19 = self::mul($g2_19, $f9, 25); $f9g3_38 = self::mul($g3_19, $f9_2, 26); $f9g4_19 = self::mul($g4_19, $f9, 25); $f9g5_38 = self::mul($g5_19, $f9_2, 26); $f9g6_19 = self::mul($g6_19, $f9, 25); $f9g7_38 = self::mul($g7_19, $f9_2, 26); $f9g8_19 = self::mul($g8_19, $f9, 25); $f9g9_38 = self::mul($g9_19, $f9_2, 26); $h0 = $f0g0 + $f1g9_38 + $f2g8_19 + $f3g7_38 + $f4g6_19 + $f5g5_38 + $f6g4_19 + $f7g3_38 + $f8g2_19 + $f9g1_38; $h1 = $f0g1 + $f1g0 + $f2g9_19 + $f3g8_19 + $f4g7_19 + $f5g6_19 + $f6g5_19 + $f7g4_19 + $f8g3_19 + $f9g2_19; $h2 = $f0g2 + $f1g1_2 + $f2g0 + $f3g9_38 + $f4g8_19 + $f5g7_38 + $f6g6_19 + $f7g5_38 + $f8g4_19 + $f9g3_38; $h3 = $f0g3 + $f1g2 + $f2g1 + $f3g0 + $f4g9_19 + $f5g8_19 + $f6g7_19 + $f7g6_19 + $f8g5_19 + $f9g4_19; $h4 = $f0g4 + $f1g3_2 + $f2g2 + $f3g1_2 + $f4g0 + $f5g9_38 + $f6g8_19 + $f7g7_38 + $f8g6_19 + $f9g5_38; $h5 = $f0g5 + $f1g4 + $f2g3 + $f3g2 + $f4g1 + $f5g0 + $f6g9_19 + $f7g8_19 + $f8g7_19 + $f9g6_19; $h6 = $f0g6 + $f1g5_2 + $f2g4 + $f3g3_2 + $f4g2 + $f5g1_2 + $f6g0 + $f7g9_38 + $f8g8_19 + $f9g7_38; $h7 = $f0g7 + $f1g6 + $f2g5 + $f3g4 + $f4g3 + $f5g2 + $f6g1 + $f7g0 + $f8g9_19 + $f9g8_19; $h8 = $f0g8 + $f1g7_2 + $f2g6 + $f3g5_2 + $f4g4 + $f5g3_2 + $f6g2 + $f7g1_2 + $f8g0 + $f9g9_38; $h9 = $f0g9 + $f1g8 + $f2g7 + $f3g6 + $f4g5 + $f5g4 + $f6g3 + $f7g2 + $f8g1 + $f9g0 ; $carry0 = ($h0 + (1 << 25)) >> 26; $h1 += $carry0; $h0 -= $carry0 << 26; $carry4 = ($h4 + (1 << 25)) >> 26; $h5 += $carry4; $h4 -= $carry4 << 26; $carry1 = ($h1 + (1 << 24)) >> 25; $h2 += $carry1; $h1 -= $carry1 << 25; $carry5 = ($h5 + (1 << 24)) >> 25; $h6 += $carry5; $h5 -= $carry5 << 25; $carry2 = ($h2 + (1 << 25)) >> 26; $h3 += $carry2; $h2 -= $carry2 << 26; $carry6 = ($h6 + (1 << 25)) >> 26; $h7 += $carry6; $h6 -= $carry6 << 26; $carry3 = ($h3 + (1 << 24)) >> 25; $h4 += $carry3; $h3 -= $carry3 << 25; $carry7 = ($h7 + (1 << 24)) >> 25; $h8 += $carry7; $h7 -= $carry7 << 25; $carry4 = ($h4 + (1 << 25)) >> 26; $h5 += $carry4; $h4 -= $carry4 << 26; $carry8 = ($h8 + (1 << 25)) >> 26; $h9 += $carry8; $h8 -= $carry8 << 26; $carry9 = ($h9 + (1 << 24)) >> 25; $h0 += self::mul($carry9, 19, 5); $h9 -= $carry9 << 25; $carry0 = ($h0 + (1 << 25)) >> 26; $h1 += $carry0; $h0 -= $carry0 << 26; return self::fe_normalize( new ParagonIE_Sodium_Core_Curve25519_Fe( (int) $h0, (int) $h1, (int) $h2, (int) $h3, (int) $h4, (int) $h5, (int) $h6, (int) $h7, (int) $h8, (int) $h9 ) ); } /** * Get the negative values for each piece of the field element. * * h = -f * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @return ParagonIE_Sodium_Core_Curve25519_Fe * @psalm-suppress MixedAssignment */ public static function fe_neg(ParagonIE_Sodium_Core_Curve25519_Fe $f) { return self::fe_normalize( new ParagonIE_Sodium_Core_Curve25519_Fe( -$f->e0, -$f->e1, -$f->e2, -$f->e3, -$f->e4, -$f->e5, -$f->e6, -$f->e7, -$f->e8, -$f->e9 ) ); } /** * Square a field element * * h = f * f * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @return ParagonIE_Sodium_Core_Curve25519_Fe */ public static function fe_sq(ParagonIE_Sodium_Core_Curve25519_Fe $f) { $f = self::fe_normalize($f); $f0 = (int) $f->e0; $f1 = (int) $f->e1; $f2 = (int) $f->e2; $f3 = (int) $f->e3; $f4 = (int) $f->e4; $f5 = (int) $f->e5; $f6 = (int) $f->e6; $f7 = (int) $f->e7; $f8 = (int) $f->e8; $f9 = (int) $f->e9; $f0_2 = $f0 << 1; $f1_2 = $f1 << 1; $f2_2 = $f2 << 1; $f3_2 = $f3 << 1; $f4_2 = $f4 << 1; $f5_2 = $f5 << 1; $f6_2 = $f6 << 1; $f7_2 = $f7 << 1; $f5_38 = self::mul($f5, 38, 6); $f6_19 = self::mul($f6, 19, 5); $f7_38 = self::mul($f7, 38, 6); $f8_19 = self::mul($f8, 19, 5); $f9_38 = self::mul($f9, 38, 6); $f0f0 = self::mul($f0, $f0, 26); $f0f1_2 = self::mul($f0_2, $f1, 26); $f0f2_2 = self::mul($f0_2, $f2, 26); $f0f3_2 = self::mul($f0_2, $f3, 26); $f0f4_2 = self::mul($f0_2, $f4, 26); $f0f5_2 = self::mul($f0_2, $f5, 26); $f0f6_2 = self::mul($f0_2, $f6, 26); $f0f7_2 = self::mul($f0_2, $f7, 26); $f0f8_2 = self::mul($f0_2, $f8, 26); $f0f9_2 = self::mul($f0_2, $f9, 26); $f1f1_2 = self::mul($f1_2, $f1, 26); $f1f2_2 = self::mul($f1_2, $f2, 26); $f1f3_4 = self::mul($f1_2, $f3_2, 26); $f1f4_2 = self::mul($f1_2, $f4, 26); $f1f5_4 = self::mul($f1_2, $f5_2, 26); $f1f6_2 = self::mul($f1_2, $f6, 26); $f1f7_4 = self::mul($f1_2, $f7_2, 26); $f1f8_2 = self::mul($f1_2, $f8, 26); $f1f9_76 = self::mul($f9_38, $f1_2, 27); $f2f2 = self::mul($f2, $f2, 27); $f2f3_2 = self::mul($f2_2, $f3, 27); $f2f4_2 = self::mul($f2_2, $f4, 27); $f2f5_2 = self::mul($f2_2, $f5, 27); $f2f6_2 = self::mul($f2_2, $f6, 27); $f2f7_2 = self::mul($f2_2, $f7, 27); $f2f8_38 = self::mul($f8_19, $f2_2, 27); $f2f9_38 = self::mul($f9_38, $f2, 26); $f3f3_2 = self::mul($f3_2, $f3, 26); $f3f4_2 = self::mul($f3_2, $f4, 26); $f3f5_4 = self::mul($f3_2, $f5_2, 26); $f3f6_2 = self::mul($f3_2, $f6, 26); $f3f7_76 = self::mul($f7_38, $f3_2, 26); $f3f8_38 = self::mul($f8_19, $f3_2, 26); $f3f9_76 = self::mul($f9_38, $f3_2, 26); $f4f4 = self::mul($f4, $f4, 26); $f4f5_2 = self::mul($f4_2, $f5, 26); $f4f6_38 = self::mul($f6_19, $f4_2, 27); $f4f7_38 = self::mul($f7_38, $f4, 26); $f4f8_38 = self::mul($f8_19, $f4_2, 27); $f4f9_38 = self::mul($f9_38, $f4, 26); $f5f5_38 = self::mul($f5_38, $f5, 26); $f5f6_38 = self::mul($f6_19, $f5_2, 26); $f5f7_76 = self::mul($f7_38, $f5_2, 26); $f5f8_38 = self::mul($f8_19, $f5_2, 26); $f5f9_76 = self::mul($f9_38, $f5_2, 26); $f6f6_19 = self::mul($f6_19, $f6, 26); $f6f7_38 = self::mul($f7_38, $f6, 26); $f6f8_38 = self::mul($f8_19, $f6_2, 27); $f6f9_38 = self::mul($f9_38, $f6, 26); $f7f7_38 = self::mul($f7_38, $f7, 26); $f7f8_38 = self::mul($f8_19, $f7_2, 26); $f7f9_76 = self::mul($f9_38, $f7_2, 26); $f8f8_19 = self::mul($f8_19, $f8, 26); $f8f9_38 = self::mul($f9_38, $f8, 26); $f9f9_38 = self::mul($f9_38, $f9, 26); $h0 = $f0f0 + $f1f9_76 + $f2f8_38 + $f3f7_76 + $f4f6_38 + $f5f5_38; $h1 = $f0f1_2 + $f2f9_38 + $f3f8_38 + $f4f7_38 + $f5f6_38; $h2 = $f0f2_2 + $f1f1_2 + $f3f9_76 + $f4f8_38 + $f5f7_76 + $f6f6_19; $h3 = $f0f3_2 + $f1f2_2 + $f4f9_38 + $f5f8_38 + $f6f7_38; $h4 = $f0f4_2 + $f1f3_4 + $f2f2 + $f5f9_76 + $f6f8_38 + $f7f7_38; $h5 = $f0f5_2 + $f1f4_2 + $f2f3_2 + $f6f9_38 + $f7f8_38; $h6 = $f0f6_2 + $f1f5_4 + $f2f4_2 + $f3f3_2 + $f7f9_76 + $f8f8_19; $h7 = $f0f7_2 + $f1f6_2 + $f2f5_2 + $f3f4_2 + $f8f9_38; $h8 = $f0f8_2 + $f1f7_4 + $f2f6_2 + $f3f5_4 + $f4f4 + $f9f9_38; $h9 = $f0f9_2 + $f1f8_2 + $f2f7_2 + $f3f6_2 + $f4f5_2; $carry0 = ($h0 + (1 << 25)) >> 26; $h1 += $carry0; $h0 -= $carry0 << 26; $carry4 = ($h4 + (1 << 25)) >> 26; $h5 += $carry4; $h4 -= $carry4 << 26; $carry1 = ($h1 + (1 << 24)) >> 25; $h2 += $carry1; $h1 -= $carry1 << 25; $carry5 = ($h5 + (1 << 24)) >> 25; $h6 += $carry5; $h5 -= $carry5 << 25; $carry2 = ($h2 + (1 << 25)) >> 26; $h3 += $carry2; $h2 -= $carry2 << 26; $carry6 = ($h6 + (1 << 25)) >> 26; $h7 += $carry6; $h6 -= $carry6 << 26; $carry3 = ($h3 + (1 << 24)) >> 25; $h4 += $carry3; $h3 -= $carry3 << 25; $carry7 = ($h7 + (1 << 24)) >> 25; $h8 += $carry7; $h7 -= $carry7 << 25; $carry4 = ($h4 + (1 << 25)) >> 26; $h5 += $carry4; $h4 -= $carry4 << 26; $carry8 = ($h8 + (1 << 25)) >> 26; $h9 += $carry8; $h8 -= $carry8 << 26; $carry9 = ($h9 + (1 << 24)) >> 25; $h0 += self::mul($carry9, 19, 5); $h9 -= $carry9 << 25; $carry0 = ($h0 + (1 << 25)) >> 26; $h1 += $carry0; $h0 -= $carry0 << 26; return self::fe_normalize( new ParagonIE_Sodium_Core_Curve25519_Fe( (int) $h0, (int) $h1, (int) $h2, (int) $h3, (int) $h4, (int) $h5, (int) $h6, (int) $h7, (int) $h8, (int) $h9 ) ); } /** * Square and double a field element * * h = 2 * f * f * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @return ParagonIE_Sodium_Core_Curve25519_Fe */ public static function fe_sq2(ParagonIE_Sodium_Core_Curve25519_Fe $f) { $f = self::fe_normalize($f); $f0 = (int) $f->e0; $f1 = (int) $f->e1; $f2 = (int) $f->e2; $f3 = (int) $f->e3; $f4 = (int) $f->e4; $f5 = (int) $f->e5; $f6 = (int) $f->e6; $f7 = (int) $f->e7; $f8 = (int) $f->e8; $f9 = (int) $f->e9; $f0_2 = $f0 << 1; $f1_2 = $f1 << 1; $f2_2 = $f2 << 1; $f3_2 = $f3 << 1; $f4_2 = $f4 << 1; $f5_2 = $f5 << 1; $f6_2 = $f6 << 1; $f7_2 = $f7 << 1; $f5_38 = self::mul($f5, 38, 6); /* 1.959375*2^30 */ $f6_19 = self::mul($f6, 19, 5); /* 1.959375*2^30 */ $f7_38 = self::mul($f7, 38, 6); /* 1.959375*2^30 */ $f8_19 = self::mul($f8, 19, 5); /* 1.959375*2^30 */ $f9_38 = self::mul($f9, 38, 6); /* 1.959375*2^30 */ $f0f0 = self::mul($f0, $f0, 24); $f0f1_2 = self::mul($f0_2, $f1, 24); $f0f2_2 = self::mul($f0_2, $f2, 24); $f0f3_2 = self::mul($f0_2, $f3, 24); $f0f4_2 = self::mul($f0_2, $f4, 24); $f0f5_2 = self::mul($f0_2, $f5, 24); $f0f6_2 = self::mul($f0_2, $f6, 24); $f0f7_2 = self::mul($f0_2, $f7, 24); $f0f8_2 = self::mul($f0_2, $f8, 24); $f0f9_2 = self::mul($f0_2, $f9, 24); $f1f1_2 = self::mul($f1_2, $f1, 24); $f1f2_2 = self::mul($f1_2, $f2, 24); $f1f3_4 = self::mul($f1_2, $f3_2, 24); $f1f4_2 = self::mul($f1_2, $f4, 24); $f1f5_4 = self::mul($f1_2, $f5_2, 24); $f1f6_2 = self::mul($f1_2, $f6, 24); $f1f7_4 = self::mul($f1_2, $f7_2, 24); $f1f8_2 = self::mul($f1_2, $f8, 24); $f1f9_76 = self::mul($f9_38, $f1_2, 24); $f2f2 = self::mul($f2, $f2, 24); $f2f3_2 = self::mul($f2_2, $f3, 24); $f2f4_2 = self::mul($f2_2, $f4, 24); $f2f5_2 = self::mul($f2_2, $f5, 24); $f2f6_2 = self::mul($f2_2, $f6, 24); $f2f7_2 = self::mul($f2_2, $f7, 24); $f2f8_38 = self::mul($f8_19, $f2_2, 25); $f2f9_38 = self::mul($f9_38, $f2, 24); $f3f3_2 = self::mul($f3_2, $f3, 24); $f3f4_2 = self::mul($f3_2, $f4, 24); $f3f5_4 = self::mul($f3_2, $f5_2, 24); $f3f6_2 = self::mul($f3_2, $f6, 24); $f3f7_76 = self::mul($f7_38, $f3_2, 24); $f3f8_38 = self::mul($f8_19, $f3_2, 24); $f3f9_76 = self::mul($f9_38, $f3_2, 24); $f4f4 = self::mul($f4, $f4, 24); $f4f5_2 = self::mul($f4_2, $f5, 24); $f4f6_38 = self::mul($f6_19, $f4_2, 25); $f4f7_38 = self::mul($f7_38, $f4, 24); $f4f8_38 = self::mul($f8_19, $f4_2, 25); $f4f9_38 = self::mul($f9_38, $f4, 24); $f5f5_38 = self::mul($f5_38, $f5, 24); $f5f6_38 = self::mul($f6_19, $f5_2, 24); $f5f7_76 = self::mul($f7_38, $f5_2, 24); $f5f8_38 = self::mul($f8_19, $f5_2, 24); $f5f9_76 = self::mul($f9_38, $f5_2, 24); $f6f6_19 = self::mul($f6_19, $f6, 24); $f6f7_38 = self::mul($f7_38, $f6, 24); $f6f8_38 = self::mul($f8_19, $f6_2, 25); $f6f9_38 = self::mul($f9_38, $f6, 24); $f7f7_38 = self::mul($f7_38, $f7, 24); $f7f8_38 = self::mul($f8_19, $f7_2, 24); $f7f9_76 = self::mul($f9_38, $f7_2, 24); $f8f8_19 = self::mul($f8_19, $f8, 24); $f8f9_38 = self::mul($f9_38, $f8, 24); $f9f9_38 = self::mul($f9_38, $f9, 24); $h0 = (int) ($f0f0 + $f1f9_76 + $f2f8_38 + $f3f7_76 + $f4f6_38 + $f5f5_38) << 1; $h1 = (int) ($f0f1_2 + $f2f9_38 + $f3f8_38 + $f4f7_38 + $f5f6_38) << 1; $h2 = (int) ($f0f2_2 + $f1f1_2 + $f3f9_76 + $f4f8_38 + $f5f7_76 + $f6f6_19) << 1; $h3 = (int) ($f0f3_2 + $f1f2_2 + $f4f9_38 + $f5f8_38 + $f6f7_38) << 1; $h4 = (int) ($f0f4_2 + $f1f3_4 + $f2f2 + $f5f9_76 + $f6f8_38 + $f7f7_38) << 1; $h5 = (int) ($f0f5_2 + $f1f4_2 + $f2f3_2 + $f6f9_38 + $f7f8_38) << 1; $h6 = (int) ($f0f6_2 + $f1f5_4 + $f2f4_2 + $f3f3_2 + $f7f9_76 + $f8f8_19) << 1; $h7 = (int) ($f0f7_2 + $f1f6_2 + $f2f5_2 + $f3f4_2 + $f8f9_38) << 1; $h8 = (int) ($f0f8_2 + $f1f7_4 + $f2f6_2 + $f3f5_4 + $f4f4 + $f9f9_38) << 1; $h9 = (int) ($f0f9_2 + $f1f8_2 + $f2f7_2 + $f3f6_2 + $f4f5_2) << 1; $carry0 = ($h0 + (1 << 25)) >> 26; $h1 += $carry0; $h0 -= $carry0 << 26; $carry4 = ($h4 + (1 << 25)) >> 26; $h5 += $carry4; $h4 -= $carry4 << 26; $carry1 = ($h1 + (1 << 24)) >> 25; $h2 += $carry1; $h1 -= $carry1 << 25; $carry5 = ($h5 + (1 << 24)) >> 25; $h6 += $carry5; $h5 -= $carry5 << 25; $carry2 = ($h2 + (1 << 25)) >> 26; $h3 += $carry2; $h2 -= $carry2 << 26; $carry6 = ($h6 + (1 << 25)) >> 26; $h7 += $carry6; $h6 -= $carry6 << 26; $carry3 = ($h3 + (1 << 24)) >> 25; $h4 += $carry3; $h3 -= $carry3 << 25; $carry7 = ($h7 + (1 << 24)) >> 25; $h8 += $carry7; $h7 -= $carry7 << 25; $carry4 = ($h4 + (1 << 25)) >> 26; $h5 += $carry4; $h4 -= $carry4 << 26; $carry8 = ($h8 + (1 << 25)) >> 26; $h9 += $carry8; $h8 -= $carry8 << 26; $carry9 = ($h9 + (1 << 24)) >> 25; $h0 += self::mul($carry9, 19, 5); $h9 -= $carry9 << 25; $carry0 = ($h0 + (1 << 25)) >> 26; $h1 += $carry0; $h0 -= $carry0 << 26; return self::fe_normalize( new ParagonIE_Sodium_Core_Curve25519_Fe( (int) $h0, (int) $h1, (int) $h2, (int) $h3, (int) $h4, (int) $h5, (int) $h6, (int) $h7, (int) $h8, (int) $h9 ) ); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $Z * @return ParagonIE_Sodium_Core_Curve25519_Fe */ public static function fe_invert(ParagonIE_Sodium_Core_Curve25519_Fe $Z) { $z = clone $Z; $t0 = self::fe_sq($z); $t1 = self::fe_sq($t0); $t1 = self::fe_sq($t1); $t1 = self::fe_mul($z, $t1); $t0 = self::fe_mul($t0, $t1); $t2 = self::fe_sq($t0); $t1 = self::fe_mul($t1, $t2); $t2 = self::fe_sq($t1); for ($i = 1; $i < 5; ++$i) { $t2 = self::fe_sq($t2); } $t1 = self::fe_mul($t2, $t1); $t2 = self::fe_sq($t1); for ($i = 1; $i < 10; ++$i) { $t2 = self::fe_sq($t2); } $t2 = self::fe_mul($t2, $t1); $t3 = self::fe_sq($t2); for ($i = 1; $i < 20; ++$i) { $t3 = self::fe_sq($t3); } $t2 = self::fe_mul($t3, $t2); $t2 = self::fe_sq($t2); for ($i = 1; $i < 10; ++$i) { $t2 = self::fe_sq($t2); } $t1 = self::fe_mul($t2, $t1); $t2 = self::fe_sq($t1); for ($i = 1; $i < 50; ++$i) { $t2 = self::fe_sq($t2); } $t2 = self::fe_mul($t2, $t1); $t3 = self::fe_sq($t2); for ($i = 1; $i < 100; ++$i) { $t3 = self::fe_sq($t3); } $t2 = self::fe_mul($t3, $t2); $t2 = self::fe_sq($t2); for ($i = 1; $i < 50; ++$i) { $t2 = self::fe_sq($t2); } $t1 = self::fe_mul($t2, $t1); $t1 = self::fe_sq($t1); for ($i = 1; $i < 5; ++$i) { $t1 = self::fe_sq($t1); } return self::fe_mul($t1, $t0); } /** * @internal You should not use this directly from another application * * @ref https://github.com/jedisct1/libsodium/blob/68564326e1e9dc57ef03746f85734232d20ca6fb/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.c#L1054-L1106 * * @param ParagonIE_Sodium_Core_Curve25519_Fe $z * @return ParagonIE_Sodium_Core_Curve25519_Fe */ public static function fe_pow22523(ParagonIE_Sodium_Core_Curve25519_Fe $z) { $z = self::fe_normalize($z); # fe_sq(t0, z); # fe_sq(t1, t0); # fe_sq(t1, t1); # fe_mul(t1, z, t1); # fe_mul(t0, t0, t1); # fe_sq(t0, t0); # fe_mul(t0, t1, t0); # fe_sq(t1, t0); $t0 = self::fe_sq($z); $t1 = self::fe_sq($t0); $t1 = self::fe_sq($t1); $t1 = self::fe_mul($z, $t1); $t0 = self::fe_mul($t0, $t1); $t0 = self::fe_sq($t0); $t0 = self::fe_mul($t1, $t0); $t1 = self::fe_sq($t0); # for (i = 1; i < 5; ++i) { # fe_sq(t1, t1); # } for ($i = 1; $i < 5; ++$i) { $t1 = self::fe_sq($t1); } # fe_mul(t0, t1, t0); # fe_sq(t1, t0); $t0 = self::fe_mul($t1, $t0); $t1 = self::fe_sq($t0); # for (i = 1; i < 10; ++i) { # fe_sq(t1, t1); # } for ($i = 1; $i < 10; ++$i) { $t1 = self::fe_sq($t1); } # fe_mul(t1, t1, t0); # fe_sq(t2, t1); $t1 = self::fe_mul($t1, $t0); $t2 = self::fe_sq($t1); # for (i = 1; i < 20; ++i) { # fe_sq(t2, t2); # } for ($i = 1; $i < 20; ++$i) { $t2 = self::fe_sq($t2); } # fe_mul(t1, t2, t1); # fe_sq(t1, t1); $t1 = self::fe_mul($t2, $t1); $t1 = self::fe_sq($t1); # for (i = 1; i < 10; ++i) { # fe_sq(t1, t1); # } for ($i = 1; $i < 10; ++$i) { $t1 = self::fe_sq($t1); } # fe_mul(t0, t1, t0); # fe_sq(t1, t0); $t0 = self::fe_mul($t1, $t0); $t1 = self::fe_sq($t0); # for (i = 1; i < 50; ++i) { # fe_sq(t1, t1); # } for ($i = 1; $i < 50; ++$i) { $t1 = self::fe_sq($t1); } # fe_mul(t1, t1, t0); # fe_sq(t2, t1); $t1 = self::fe_mul($t1, $t0); $t2 = self::fe_sq($t1); # for (i = 1; i < 100; ++i) { # fe_sq(t2, t2); # } for ($i = 1; $i < 100; ++$i) { $t2 = self::fe_sq($t2); } # fe_mul(t1, t2, t1); # fe_sq(t1, t1); $t1 = self::fe_mul($t2, $t1); $t1 = self::fe_sq($t1); # for (i = 1; i < 50; ++i) { # fe_sq(t1, t1); # } for ($i = 1; $i < 50; ++$i) { $t1 = self::fe_sq($t1); } # fe_mul(t0, t1, t0); # fe_sq(t0, t0); # fe_sq(t0, t0); # fe_mul(out, t0, z); $t0 = self::fe_mul($t1, $t0); $t0 = self::fe_sq($t0); $t0 = self::fe_sq($t0); return self::fe_mul($t0, $z); } /** * Subtract two field elements. * * h = f - g * * Preconditions: * |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. * |g| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. * * Postconditions: * |h| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @param ParagonIE_Sodium_Core_Curve25519_Fe $g * @return ParagonIE_Sodium_Core_Curve25519_Fe * @psalm-suppress MixedOperand */ public static function fe_sub(ParagonIE_Sodium_Core_Curve25519_Fe $f, ParagonIE_Sodium_Core_Curve25519_Fe $g) { return self::fe_normalize( new ParagonIE_Sodium_Core_Curve25519_Fe( (int) ($f->e0 - $g->e0), (int) ($f->e1 - $g->e1), (int) ($f->e2 - $g->e2), (int) ($f->e3 - $g->e3), (int) ($f->e4 - $g->e4), (int) ($f->e5 - $g->e5), (int) ($f->e6 - $g->e6), (int) ($f->e7 - $g->e7), (int) ($f->e8 - $g->e8), (int) ($f->e9 - $g->e9) ) ); } /** * Add two group elements. * * r = p + q * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached $q * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 */ public static function ge_add( ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p, ParagonIE_Sodium_Core_Curve25519_Ge_Cached $q ) { $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P1p1(); $r->X = self::fe_add($p->Y, $p->X); $r->Y = self::fe_sub($p->Y, $p->X); $r->Z = self::fe_mul($r->X, $q->YplusX); $r->Y = self::fe_mul($r->Y, $q->YminusX); $r->T = self::fe_mul($q->T2d, $p->T); $r->X = self::fe_mul($p->Z, $q->Z); $t0 = self::fe_add($r->X, $r->X); $r->X = self::fe_sub($r->Z, $r->Y); $r->Y = self::fe_add($r->Z, $r->Y); $r->Z = self::fe_add($t0, $r->T); $r->T = self::fe_sub($t0, $r->T); return $r; } /** * @internal You should not use this directly from another application * * @ref https://github.com/jedisct1/libsodium/blob/157c4a80c13b117608aeae12178b2d38825f9f8f/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.c#L1185-L1215 * @param string $a * @return array<int, mixed> * @throws SodiumException * @throws TypeError */ public static function slide($a) { if (self::strlen($a) < 256) { if (self::strlen($a) < 16) { $a = str_pad($a, 256, '0', STR_PAD_RIGHT); } } /** @var array<int, int> $r */ $r = array(); /** @var int $i */ for ($i = 0; $i < 256; ++$i) { $r[$i] = (int) ( 1 & ( self::chrToInt($a[(int) ($i >> 3)]) >> ($i & 7) ) ); } for ($i = 0;$i < 256;++$i) { if ($r[$i]) { for ($b = 1;$b <= 6 && $i + $b < 256;++$b) { if ($r[$i + $b]) { if ($r[$i] + ($r[$i + $b] << $b) <= 15) { $r[$i] += $r[$i + $b] << $b; $r[$i + $b] = 0; } elseif ($r[$i] - ($r[$i + $b] << $b) >= -15) { $r[$i] -= $r[$i + $b] << $b; for ($k = $i + $b; $k < 256; ++$k) { if (!$r[$k]) { $r[$k] = 1; break; } $r[$k] = 0; } } else { break; } } } } } return $r; } /** * @internal You should not use this directly from another application * * @param string $s * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 * @throws SodiumException * @throws TypeError */ public static function ge_frombytes_negate_vartime($s) { static $d = null; if (!$d) { $d = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$d); } # fe_frombytes(h->Y,s); # fe_1(h->Z); $h = new ParagonIE_Sodium_Core_Curve25519_Ge_P3( self::fe_0(), self::fe_frombytes($s), self::fe_1() ); # fe_sq(u,h->Y); # fe_mul(v,u,d); # fe_sub(u,u,h->Z); /* u = y^2-1 */ # fe_add(v,v,h->Z); /* v = dy^2+1 */ $u = self::fe_sq($h->Y); /** @var ParagonIE_Sodium_Core_Curve25519_Fe $d */ $v = self::fe_mul($u, $d); $u = self::fe_sub($u, $h->Z); /* u = y^2 - 1 */ $v = self::fe_add($v, $h->Z); /* v = dy^2 + 1 */ # fe_sq(v3,v); # fe_mul(v3,v3,v); /* v3 = v^3 */ # fe_sq(h->X,v3); # fe_mul(h->X,h->X,v); # fe_mul(h->X,h->X,u); /* x = uv^7 */ $v3 = self::fe_sq($v); $v3 = self::fe_mul($v3, $v); /* v3 = v^3 */ $h->X = self::fe_sq($v3); $h->X = self::fe_mul($h->X, $v); $h->X = self::fe_mul($h->X, $u); /* x = uv^7 */ # fe_pow22523(h->X,h->X); /* x = (uv^7)^((q-5)/8) */ # fe_mul(h->X,h->X,v3); # fe_mul(h->X,h->X,u); /* x = uv^3(uv^7)^((q-5)/8) */ $h->X = self::fe_pow22523($h->X); /* x = (uv^7)^((q-5)/8) */ $h->X = self::fe_mul($h->X, $v3); $h->X = self::fe_mul($h->X, $u); /* x = uv^3(uv^7)^((q-5)/8) */ # fe_sq(vxx,h->X); # fe_mul(vxx,vxx,v); # fe_sub(check,vxx,u); /* vx^2-u */ $vxx = self::fe_sq($h->X); $vxx = self::fe_mul($vxx, $v); $check = self::fe_sub($vxx, $u); /* vx^2 - u */ # if (fe_isnonzero(check)) { # fe_add(check,vxx,u); /* vx^2+u */ # if (fe_isnonzero(check)) { # return -1; # } # fe_mul(h->X,h->X,sqrtm1); # } if (self::fe_isnonzero($check)) { $check = self::fe_add($vxx, $u); /* vx^2 + u */ if (self::fe_isnonzero($check)) { throw new RangeException('Internal check failed.'); } $h->X = self::fe_mul( $h->X, ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtm1) ); } # if (fe_isnegative(h->X) == (s[31] >> 7)) { # fe_neg(h->X,h->X); # } $i = self::chrToInt($s[31]); if (self::fe_isnegative($h->X) === ($i >> 7)) { $h->X = self::fe_neg($h->X); } # fe_mul(h->T,h->X,h->Y); $h->T = self::fe_mul($h->X, $h->Y); return $h; } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $R * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p * @param ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $q * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 */ public static function ge_madd( ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $R, ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p, ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $q ) { $r = clone $R; $r->X = self::fe_add($p->Y, $p->X); $r->Y = self::fe_sub($p->Y, $p->X); $r->Z = self::fe_mul($r->X, $q->yplusx); $r->Y = self::fe_mul($r->Y, $q->yminusx); $r->T = self::fe_mul($q->xy2d, $p->T); $t0 = self::fe_add(clone $p->Z, clone $p->Z); $r->X = self::fe_sub($r->Z, $r->Y); $r->Y = self::fe_add($r->Z, $r->Y); $r->Z = self::fe_add($t0, $r->T); $r->T = self::fe_sub($t0, $r->T); return $r; } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $R * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p * @param ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $q * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 */ public static function ge_msub( ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $R, ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p, ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $q ) { $r = clone $R; $r->X = self::fe_add($p->Y, $p->X); $r->Y = self::fe_sub($p->Y, $p->X); $r->Z = self::fe_mul($r->X, $q->yminusx); $r->Y = self::fe_mul($r->Y, $q->yplusx); $r->T = self::fe_mul($q->xy2d, $p->T); $t0 = self::fe_add($p->Z, $p->Z); $r->X = self::fe_sub($r->Z, $r->Y); $r->Y = self::fe_add($r->Z, $r->Y); $r->Z = self::fe_sub($t0, $r->T); $r->T = self::fe_add($t0, $r->T); return $r; } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $p * @return ParagonIE_Sodium_Core_Curve25519_Ge_P2 */ public static function ge_p1p1_to_p2(ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $p) { $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P2(); $r->X = self::fe_mul($p->X, $p->T); $r->Y = self::fe_mul($p->Y, $p->Z); $r->Z = self::fe_mul($p->Z, $p->T); return $r; } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $p * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 */ public static function ge_p1p1_to_p3(ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 $p) { $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P3(); $r->X = self::fe_mul($p->X, $p->T); $r->Y = self::fe_mul($p->Y, $p->Z); $r->Z = self::fe_mul($p->Z, $p->T); $r->T = self::fe_mul($p->X, $p->Y); return $r; } /** * @internal You should not use this directly from another application * * @return ParagonIE_Sodium_Core_Curve25519_Ge_P2 */ public static function ge_p2_0() { return new ParagonIE_Sodium_Core_Curve25519_Ge_P2( self::fe_0(), self::fe_1(), self::fe_1() ); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Ge_P2 $p * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 */ public static function ge_p2_dbl(ParagonIE_Sodium_Core_Curve25519_Ge_P2 $p) { $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P1p1(); $r->X = self::fe_sq($p->X); $r->Z = self::fe_sq($p->Y); $r->T = self::fe_sq2($p->Z); $r->Y = self::fe_add($p->X, $p->Y); $t0 = self::fe_sq($r->Y); $r->Y = self::fe_add($r->Z, $r->X); $r->Z = self::fe_sub($r->Z, $r->X); $r->X = self::fe_sub($t0, $r->Y); $r->T = self::fe_sub($r->T, $r->Z); return $r; } /** * @internal You should not use this directly from another application * * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 */ public static function ge_p3_0() { return new ParagonIE_Sodium_Core_Curve25519_Ge_P3( self::fe_0(), self::fe_1(), self::fe_1(), self::fe_0() ); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p * @return ParagonIE_Sodium_Core_Curve25519_Ge_Cached */ public static function ge_p3_to_cached(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p) { static $d2 = null; if ($d2 === null) { $d2 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$d2); } /** @var ParagonIE_Sodium_Core_Curve25519_Fe $d2 */ $r = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached(); $r->YplusX = self::fe_add($p->Y, $p->X); $r->YminusX = self::fe_sub($p->Y, $p->X); $r->Z = self::fe_copy($p->Z); $r->T2d = self::fe_mul($p->T, $d2); return $r; } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p * @return ParagonIE_Sodium_Core_Curve25519_Ge_P2 */ public static function ge_p3_to_p2(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p) { return new ParagonIE_Sodium_Core_Curve25519_Ge_P2( self::fe_copy($p->X), self::fe_copy($p->Y), self::fe_copy($p->Z) ); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h * @return string * @throws SodiumException * @throws TypeError */ public static function ge_p3_tobytes(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h) { $recip = self::fe_invert($h->Z); $x = self::fe_mul($h->X, $recip); $y = self::fe_mul($h->Y, $recip); $s = self::fe_tobytes($y); $s[31] = self::intToChr( self::chrToInt($s[31]) ^ (self::fe_isnegative($x) << 7) ); return $s; } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 */ public static function ge_p3_dbl(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p) { $q = self::ge_p3_to_p2($p); return self::ge_p2_dbl($q); } /** * @return ParagonIE_Sodium_Core_Curve25519_Ge_Precomp */ public static function ge_precomp_0() { return new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( self::fe_1(), self::fe_1(), self::fe_0() ); } /** * @internal You should not use this directly from another application * * @param int $b * @param int $c * @return int */ public static function equal($b, $c) { return (int) ((($b ^ $c) - 1) >> 31) & 1; } /** * @internal You should not use this directly from another application * * @param int|string $char * @return int (1 = yes, 0 = no) * @throws SodiumException * @throws TypeError */ public static function negative($char) { if (is_int($char)) { return ($char >> 63) & 1; } $x = self::chrToInt(self::substr($char, 0, 1)); return (int) ($x >> 63); } /** * Conditional move * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $t * @param ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $u * @param int $b * @return ParagonIE_Sodium_Core_Curve25519_Ge_Precomp */ public static function cmov( ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $t, ParagonIE_Sodium_Core_Curve25519_Ge_Precomp $u, $b ) { if (!is_int($b)) { throw new InvalidArgumentException('Expected an integer.'); } return new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( self::fe_cmov($t->yplusx, $u->yplusx, $b), self::fe_cmov($t->yminusx, $u->yminusx, $b), self::fe_cmov($t->xy2d, $u->xy2d, $b) ); } /** * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached $t * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached $u * @param int $b * @return ParagonIE_Sodium_Core_Curve25519_Ge_Cached */ public static function ge_cmov_cached( ParagonIE_Sodium_Core_Curve25519_Ge_Cached $t, ParagonIE_Sodium_Core_Curve25519_Ge_Cached $u, $b ) { $b &= 1; $ret = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached(); $ret->YplusX = self::fe_cmov($t->YplusX, $u->YplusX, $b); $ret->YminusX = self::fe_cmov($t->YminusX, $u->YminusX, $b); $ret->Z = self::fe_cmov($t->Z, $u->Z, $b); $ret->T2d = self::fe_cmov($t->T2d, $u->T2d, $b); return $ret; } /** * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached[] $cached * @param int $b * @return ParagonIE_Sodium_Core_Curve25519_Ge_Cached * @throws SodiumException */ public static function ge_cmov8_cached(array $cached, $b) { // const unsigned char bnegative = negative(b); // const unsigned char babs = b - (((-bnegative) & b) * ((signed char) 1 << 1)); $bnegative = self::negative($b); $babs = $b - (((-$bnegative) & $b) << 1); // ge25519_cached_0(t); $t = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached( self::fe_1(), self::fe_1(), self::fe_1(), self::fe_0() ); // ge25519_cmov_cached(t, &cached[0], equal(babs, 1)); // ge25519_cmov_cached(t, &cached[1], equal(babs, 2)); // ge25519_cmov_cached(t, &cached[2], equal(babs, 3)); // ge25519_cmov_cached(t, &cached[3], equal(babs, 4)); // ge25519_cmov_cached(t, &cached[4], equal(babs, 5)); // ge25519_cmov_cached(t, &cached[5], equal(babs, 6)); // ge25519_cmov_cached(t, &cached[6], equal(babs, 7)); // ge25519_cmov_cached(t, &cached[7], equal(babs, 8)); for ($x = 0; $x < 8; ++$x) { $t = self::ge_cmov_cached($t, $cached[$x], self::equal($babs, $x + 1)); } // fe25519_copy(minust.YplusX, t->YminusX); // fe25519_copy(minust.YminusX, t->YplusX); // fe25519_copy(minust.Z, t->Z); // fe25519_neg(minust.T2d, t->T2d); $minust = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached( self::fe_copy($t->YminusX), self::fe_copy($t->YplusX), self::fe_copy($t->Z), self::fe_neg($t->T2d) ); return self::ge_cmov_cached($t, $minust, $bnegative); } /** * @internal You should not use this directly from another application * * @param int $pos * @param int $b * @return ParagonIE_Sodium_Core_Curve25519_Ge_Precomp * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayOffset */ public static function ge_select($pos = 0, $b = 0) { static $base = null; if ($base === null) { $base = array(); /** @var int $i */ foreach (self::$base as $i => $bas) { for ($j = 0; $j < 8; ++$j) { $base[$i][$j] = new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($bas[$j][0]), ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($bas[$j][1]), ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($bas[$j][2]) ); } } } /** @var array<int, array<int, ParagonIE_Sodium_Core_Curve25519_Ge_Precomp>> $base */ if (!is_int($pos)) { throw new InvalidArgumentException('Position must be an integer'); } if ($pos < 0 || $pos > 31) { throw new RangeException('Position is out of range [0, 31]'); } $bnegative = self::negative($b); $babs = $b - (((-$bnegative) & $b) << 1); $t = self::ge_precomp_0(); for ($i = 0; $i < 8; ++$i) { $t = self::cmov( $t, $base[$pos][$i], self::equal($babs, $i + 1) ); } $minusT = new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( self::fe_copy($t->yminusx), self::fe_copy($t->yplusx), self::fe_neg($t->xy2d) ); return self::cmov($t, $minusT, $bnegative); } /** * Subtract two group elements. * * r = p - q * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p * @param ParagonIE_Sodium_Core_Curve25519_Ge_Cached $q * @return ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 */ public static function ge_sub( ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p, ParagonIE_Sodium_Core_Curve25519_Ge_Cached $q ) { $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P1p1(); $r->X = self::fe_add($p->Y, $p->X); $r->Y = self::fe_sub($p->Y, $p->X); $r->Z = self::fe_mul($r->X, $q->YminusX); $r->Y = self::fe_mul($r->Y, $q->YplusX); $r->T = self::fe_mul($q->T2d, $p->T); $r->X = self::fe_mul($p->Z, $q->Z); $t0 = self::fe_add($r->X, $r->X); $r->X = self::fe_sub($r->Z, $r->Y); $r->Y = self::fe_add($r->Z, $r->Y); $r->Z = self::fe_sub($t0, $r->T); $r->T = self::fe_add($t0, $r->T); return $r; } /** * Convert a group element to a byte string. * * @param ParagonIE_Sodium_Core_Curve25519_Ge_P2 $h * @return string * @throws SodiumException * @throws TypeError */ public static function ge_tobytes(ParagonIE_Sodium_Core_Curve25519_Ge_P2 $h) { $recip = self::fe_invert($h->Z); $x = self::fe_mul($h->X, $recip); $y = self::fe_mul($h->Y, $recip); $s = self::fe_tobytes($y); $s[31] = self::intToChr( self::chrToInt($s[31]) ^ (self::fe_isnegative($x) << 7) ); return $s; } /** * @internal You should not use this directly from another application * * @param string $a * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A * @param string $b * @return ParagonIE_Sodium_Core_Curve25519_Ge_P2 * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedArrayAccess */ public static function ge_double_scalarmult_vartime( $a, ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A, $b ) { /** @var array<int, ParagonIE_Sodium_Core_Curve25519_Ge_Cached> $Ai */ $Ai = array(); /** @var array<int, ParagonIE_Sodium_Core_Curve25519_Ge_Precomp> $Bi */ static $Bi = array(); if (!$Bi) { for ($i = 0; $i < 8; ++$i) { $Bi[$i] = new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$base2[$i][0]), ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$base2[$i][1]), ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$base2[$i][2]) ); } } for ($i = 0; $i < 8; ++$i) { $Ai[$i] = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached( self::fe_0(), self::fe_0(), self::fe_0(), self::fe_0() ); } # slide(aslide,a); # slide(bslide,b); /** @var array<int, int> $aslide */ $aslide = self::slide($a); /** @var array<int, int> $bslide */ $bslide = self::slide($b); # ge_p3_to_cached(&Ai[0],A); # ge_p3_dbl(&t,A); ge_p1p1_to_p3(&A2,&t); $Ai[0] = self::ge_p3_to_cached($A); $t = self::ge_p3_dbl($A); $A2 = self::ge_p1p1_to_p3($t); # ge_add(&t,&A2,&Ai[0]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[1],&u); # ge_add(&t,&A2,&Ai[1]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[2],&u); # ge_add(&t,&A2,&Ai[2]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[3],&u); # ge_add(&t,&A2,&Ai[3]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[4],&u); # ge_add(&t,&A2,&Ai[4]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[5],&u); # ge_add(&t,&A2,&Ai[5]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[6],&u); # ge_add(&t,&A2,&Ai[6]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[7],&u); for ($i = 0; $i < 7; ++$i) { $t = self::ge_add($A2, $Ai[$i]); $u = self::ge_p1p1_to_p3($t); $Ai[$i + 1] = self::ge_p3_to_cached($u); } # ge_p2_0(r); $r = self::ge_p2_0(); # for (i = 255;i >= 0;--i) { # if (aslide[i] || bslide[i]) break; # } $i = 255; for (; $i >= 0; --$i) { if ($aslide[$i] || $bslide[$i]) { break; } } # for (;i >= 0;--i) { for (; $i >= 0; --$i) { # ge_p2_dbl(&t,r); $t = self::ge_p2_dbl($r); # if (aslide[i] > 0) { if ($aslide[$i] > 0) { # ge_p1p1_to_p3(&u,&t); # ge_add(&t,&u,&Ai[aslide[i]/2]); $u = self::ge_p1p1_to_p3($t); $t = self::ge_add( $u, $Ai[(int) floor($aslide[$i] / 2)] ); # } else if (aslide[i] < 0) { } elseif ($aslide[$i] < 0) { # ge_p1p1_to_p3(&u,&t); # ge_sub(&t,&u,&Ai[(-aslide[i])/2]); $u = self::ge_p1p1_to_p3($t); $t = self::ge_sub( $u, $Ai[(int) floor(-$aslide[$i] / 2)] ); } # if (bslide[i] > 0) { if ($bslide[$i] > 0) { /** @var int $index */ $index = (int) floor($bslide[$i] / 2); # ge_p1p1_to_p3(&u,&t); # ge_madd(&t,&u,&Bi[bslide[i]/2]); $u = self::ge_p1p1_to_p3($t); $t = self::ge_madd($t, $u, $Bi[$index]); # } else if (bslide[i] < 0) { } elseif ($bslide[$i] < 0) { /** @var int $index */ $index = (int) floor(-$bslide[$i] / 2); # ge_p1p1_to_p3(&u,&t); # ge_msub(&t,&u,&Bi[(-bslide[i])/2]); $u = self::ge_p1p1_to_p3($t); $t = self::ge_msub($t, $u, $Bi[$index]); } # ge_p1p1_to_p2(r,&t); $r = self::ge_p1p1_to_p2($t); } return $r; } /** * @internal You should not use this directly from another application * * @param string $a * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 * @throws SodiumException * @throws TypeError * @psalm-suppress MixedAssignment * @psalm-suppress MixedOperand */ public static function ge_scalarmult($a, $p) { $e = array_fill(0, 64, 0); /** @var ParagonIE_Sodium_Core_Curve25519_Ge_Cached[] $pi */ $pi = array(); // ge25519_p3_to_cached(&pi[1 - 1], p); /* p */ $pi[0] = self::ge_p3_to_cached($p); // ge25519_p3_dbl(&t2, p); // ge25519_p1p1_to_p3(&p2, &t2); // ge25519_p3_to_cached(&pi[2 - 1], &p2); /* 2p = 2*p */ $t2 = self::ge_p3_dbl($p); $p2 = self::ge_p1p1_to_p3($t2); $pi[1] = self::ge_p3_to_cached($p2); // ge25519_add_cached(&t3, p, &pi[2 - 1]); // ge25519_p1p1_to_p3(&p3, &t3); // ge25519_p3_to_cached(&pi[3 - 1], &p3); /* 3p = 2p+p */ $t3 = self::ge_add($p, $pi[1]); $p3 = self::ge_p1p1_to_p3($t3); $pi[2] = self::ge_p3_to_cached($p3); // ge25519_p3_dbl(&t4, &p2); // ge25519_p1p1_to_p3(&p4, &t4); // ge25519_p3_to_cached(&pi[4 - 1], &p4); /* 4p = 2*2p */ $t4 = self::ge_p3_dbl($p2); $p4 = self::ge_p1p1_to_p3($t4); $pi[3] = self::ge_p3_to_cached($p4); // ge25519_add_cached(&t5, p, &pi[4 - 1]); // ge25519_p1p1_to_p3(&p5, &t5); // ge25519_p3_to_cached(&pi[5 - 1], &p5); /* 5p = 4p+p */ $t5 = self::ge_add($p, $pi[3]); $p5 = self::ge_p1p1_to_p3($t5); $pi[4] = self::ge_p3_to_cached($p5); // ge25519_p3_dbl(&t6, &p3); // ge25519_p1p1_to_p3(&p6, &t6); // ge25519_p3_to_cached(&pi[6 - 1], &p6); /* 6p = 2*3p */ $t6 = self::ge_p3_dbl($p3); $p6 = self::ge_p1p1_to_p3($t6); $pi[5] = self::ge_p3_to_cached($p6); // ge25519_add_cached(&t7, p, &pi[6 - 1]); // ge25519_p1p1_to_p3(&p7, &t7); // ge25519_p3_to_cached(&pi[7 - 1], &p7); /* 7p = 6p+p */ $t7 = self::ge_add($p, $pi[5]); $p7 = self::ge_p1p1_to_p3($t7); $pi[6] = self::ge_p3_to_cached($p7); // ge25519_p3_dbl(&t8, &p4); // ge25519_p1p1_to_p3(&p8, &t8); // ge25519_p3_to_cached(&pi[8 - 1], &p8); /* 8p = 2*4p */ $t8 = self::ge_p3_dbl($p4); $p8 = self::ge_p1p1_to_p3($t8); $pi[7] = self::ge_p3_to_cached($p8); // for (i = 0; i < 32; ++i) { // e[2 * i + 0] = (a[i] >> 0) & 15; // e[2 * i + 1] = (a[i] >> 4) & 15; // } for ($i = 0; $i < 32; ++$i) { $e[($i << 1) ] = self::chrToInt($a[$i]) & 15; $e[($i << 1) + 1] = (self::chrToInt($a[$i]) >> 4) & 15; } // /* each e[i] is between 0 and 15 */ // /* e[63] is between 0 and 7 */ // carry = 0; // for (i = 0; i < 63; ++i) { // e[i] += carry; // carry = e[i] + 8; // carry >>= 4; // e[i] -= carry * ((signed char) 1 << 4); // } $carry = 0; for ($i = 0; $i < 63; ++$i) { $e[$i] += $carry; $carry = $e[$i] + 8; $carry >>= 4; $e[$i] -= $carry << 4; } // e[63] += carry; // /* each e[i] is between -8 and 8 */ $e[63] += $carry; // ge25519_p3_0(h); $h = self::ge_p3_0(); // for (i = 63; i != 0; i--) { for ($i = 63; $i != 0; --$i) { // ge25519_cmov8_cached(&t, pi, e[i]); $t = self::ge_cmov8_cached($pi, $e[$i]); // ge25519_add_cached(&r, h, &t); $r = self::ge_add($h, $t); // ge25519_p1p1_to_p2(&s, &r); // ge25519_p2_dbl(&r, &s); // ge25519_p1p1_to_p2(&s, &r); // ge25519_p2_dbl(&r, &s); // ge25519_p1p1_to_p2(&s, &r); // ge25519_p2_dbl(&r, &s); // ge25519_p1p1_to_p2(&s, &r); // ge25519_p2_dbl(&r, &s); $s = self::ge_p1p1_to_p2($r); $r = self::ge_p2_dbl($s); $s = self::ge_p1p1_to_p2($r); $r = self::ge_p2_dbl($s); $s = self::ge_p1p1_to_p2($r); $r = self::ge_p2_dbl($s); $s = self::ge_p1p1_to_p2($r); $r = self::ge_p2_dbl($s); // ge25519_p1p1_to_p3(h, &r); /* *16 */ $h = self::ge_p1p1_to_p3($r); /* *16 */ } // ge25519_cmov8_cached(&t, pi, e[i]); // ge25519_add_cached(&r, h, &t); // ge25519_p1p1_to_p3(h, &r); $t = self::ge_cmov8_cached($pi, $e[0]); $r = self::ge_add($h, $t); return self::ge_p1p1_to_p3($r); } /** * @internal You should not use this directly from another application * * @param string $a * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 * @throws SodiumException * @throws TypeError * @psalm-suppress MixedAssignment * @psalm-suppress MixedOperand */ public static function ge_scalarmult_base($a) { /** @var array<int, int> $e */ $e = array(); $r = new ParagonIE_Sodium_Core_Curve25519_Ge_P1p1(); for ($i = 0; $i < 32; ++$i) { $dbl = (int) $i << 1; $e[$dbl] = (int) self::chrToInt($a[$i]) & 15; $e[$dbl + 1] = (int) (self::chrToInt($a[$i]) >> 4) & 15; } $carry = 0; for ($i = 0; $i < 63; ++$i) { $e[$i] += $carry; $carry = $e[$i] + 8; $carry >>= 4; $e[$i] -= $carry << 4; } $e[63] += (int) $carry; $h = self::ge_p3_0(); for ($i = 1; $i < 64; $i += 2) { $t = self::ge_select((int) floor($i / 2), (int) $e[$i]); $r = self::ge_madd($r, $h, $t); $h = self::ge_p1p1_to_p3($r); } $r = self::ge_p3_dbl($h); $s = self::ge_p1p1_to_p2($r); $r = self::ge_p2_dbl($s); $s = self::ge_p1p1_to_p2($r); $r = self::ge_p2_dbl($s); $s = self::ge_p1p1_to_p2($r); $r = self::ge_p2_dbl($s); $h = self::ge_p1p1_to_p3($r); for ($i = 0; $i < 64; $i += 2) { $t = self::ge_select($i >> 1, (int) $e[$i]); $r = self::ge_madd($r, $h, $t); $h = self::ge_p1p1_to_p3($r); } return $h; } /** * Calculates (ab + c) mod l * where l = 2^252 + 27742317777372353535851937790883648493 * * @internal You should not use this directly from another application * * @param string $a * @param string $b * @param string $c * @return string * @throws TypeError */ public static function sc_muladd($a, $b, $c) { $a0 = 2097151 & self::load_3(self::substr($a, 0, 3)); $a1 = 2097151 & (self::load_4(self::substr($a, 2, 4)) >> 5); $a2 = 2097151 & (self::load_3(self::substr($a, 5, 3)) >> 2); $a3 = 2097151 & (self::load_4(self::substr($a, 7, 4)) >> 7); $a4 = 2097151 & (self::load_4(self::substr($a, 10, 4)) >> 4); $a5 = 2097151 & (self::load_3(self::substr($a, 13, 3)) >> 1); $a6 = 2097151 & (self::load_4(self::substr($a, 15, 4)) >> 6); $a7 = 2097151 & (self::load_3(self::substr($a, 18, 3)) >> 3); $a8 = 2097151 & self::load_3(self::substr($a, 21, 3)); $a9 = 2097151 & (self::load_4(self::substr($a, 23, 4)) >> 5); $a10 = 2097151 & (self::load_3(self::substr($a, 26, 3)) >> 2); $a11 = (self::load_4(self::substr($a, 28, 4)) >> 7); $b0 = 2097151 & self::load_3(self::substr($b, 0, 3)); $b1 = 2097151 & (self::load_4(self::substr($b, 2, 4)) >> 5); $b2 = 2097151 & (self::load_3(self::substr($b, 5, 3)) >> 2); $b3 = 2097151 & (self::load_4(self::substr($b, 7, 4)) >> 7); $b4 = 2097151 & (self::load_4(self::substr($b, 10, 4)) >> 4); $b5 = 2097151 & (self::load_3(self::substr($b, 13, 3)) >> 1); $b6 = 2097151 & (self::load_4(self::substr($b, 15, 4)) >> 6); $b7 = 2097151 & (self::load_3(self::substr($b, 18, 3)) >> 3); $b8 = 2097151 & self::load_3(self::substr($b, 21, 3)); $b9 = 2097151 & (self::load_4(self::substr($b, 23, 4)) >> 5); $b10 = 2097151 & (self::load_3(self::substr($b, 26, 3)) >> 2); $b11 = (self::load_4(self::substr($b, 28, 4)) >> 7); $c0 = 2097151 & self::load_3(self::substr($c, 0, 3)); $c1 = 2097151 & (self::load_4(self::substr($c, 2, 4)) >> 5); $c2 = 2097151 & (self::load_3(self::substr($c, 5, 3)) >> 2); $c3 = 2097151 & (self::load_4(self::substr($c, 7, 4)) >> 7); $c4 = 2097151 & (self::load_4(self::substr($c, 10, 4)) >> 4); $c5 = 2097151 & (self::load_3(self::substr($c, 13, 3)) >> 1); $c6 = 2097151 & (self::load_4(self::substr($c, 15, 4)) >> 6); $c7 = 2097151 & (self::load_3(self::substr($c, 18, 3)) >> 3); $c8 = 2097151 & self::load_3(self::substr($c, 21, 3)); $c9 = 2097151 & (self::load_4(self::substr($c, 23, 4)) >> 5); $c10 = 2097151 & (self::load_3(self::substr($c, 26, 3)) >> 2); $c11 = (self::load_4(self::substr($c, 28, 4)) >> 7); /* Can't really avoid the pyramid here: */ $s0 = $c0 + self::mul($a0, $b0, 24); $s1 = $c1 + self::mul($a0, $b1, 24) + self::mul($a1, $b0, 24); $s2 = $c2 + self::mul($a0, $b2, 24) + self::mul($a1, $b1, 24) + self::mul($a2, $b0, 24); $s3 = $c3 + self::mul($a0, $b3, 24) + self::mul($a1, $b2, 24) + self::mul($a2, $b1, 24) + self::mul($a3, $b0, 24); $s4 = $c4 + self::mul($a0, $b4, 24) + self::mul($a1, $b3, 24) + self::mul($a2, $b2, 24) + self::mul($a3, $b1, 24) + self::mul($a4, $b0, 24); $s5 = $c5 + self::mul($a0, $b5, 24) + self::mul($a1, $b4, 24) + self::mul($a2, $b3, 24) + self::mul($a3, $b2, 24) + self::mul($a4, $b1, 24) + self::mul($a5, $b0, 24); $s6 = $c6 + self::mul($a0, $b6, 24) + self::mul($a1, $b5, 24) + self::mul($a2, $b4, 24) + self::mul($a3, $b3, 24) + self::mul($a4, $b2, 24) + self::mul($a5, $b1, 24) + self::mul($a6, $b0, 24); $s7 = $c7 + self::mul($a0, $b7, 24) + self::mul($a1, $b6, 24) + self::mul($a2, $b5, 24) + self::mul($a3, $b4, 24) + self::mul($a4, $b3, 24) + self::mul($a5, $b2, 24) + self::mul($a6, $b1, 24) + self::mul($a7, $b0, 24); $s8 = $c8 + self::mul($a0, $b8, 24) + self::mul($a1, $b7, 24) + self::mul($a2, $b6, 24) + self::mul($a3, $b5, 24) + self::mul($a4, $b4, 24) + self::mul($a5, $b3, 24) + self::mul($a6, $b2, 24) + self::mul($a7, $b1, 24) + self::mul($a8, $b0, 24); $s9 = $c9 + self::mul($a0, $b9, 24) + self::mul($a1, $b8, 24) + self::mul($a2, $b7, 24) + self::mul($a3, $b6, 24) + self::mul($a4, $b5, 24) + self::mul($a5, $b4, 24) + self::mul($a6, $b3, 24) + self::mul($a7, $b2, 24) + self::mul($a8, $b1, 24) + self::mul($a9, $b0, 24); $s10 = $c10 + self::mul($a0, $b10, 24) + self::mul($a1, $b9, 24) + self::mul($a2, $b8, 24) + self::mul($a3, $b7, 24) + self::mul($a4, $b6, 24) + self::mul($a5, $b5, 24) + self::mul($a6, $b4, 24) + self::mul($a7, $b3, 24) + self::mul($a8, $b2, 24) + self::mul($a9, $b1, 24) + self::mul($a10, $b0, 24); $s11 = $c11 + self::mul($a0, $b11, 24) + self::mul($a1, $b10, 24) + self::mul($a2, $b9, 24) + self::mul($a3, $b8, 24) + self::mul($a4, $b7, 24) + self::mul($a5, $b6, 24) + self::mul($a6, $b5, 24) + self::mul($a7, $b4, 24) + self::mul($a8, $b3, 24) + self::mul($a9, $b2, 24) + self::mul($a10, $b1, 24) + self::mul($a11, $b0, 24); $s12 = self::mul($a1, $b11, 24) + self::mul($a2, $b10, 24) + self::mul($a3, $b9, 24) + self::mul($a4, $b8, 24) + self::mul($a5, $b7, 24) + self::mul($a6, $b6, 24) + self::mul($a7, $b5, 24) + self::mul($a8, $b4, 24) + self::mul($a9, $b3, 24) + self::mul($a10, $b2, 24) + self::mul($a11, $b1, 24); $s13 = self::mul($a2, $b11, 24) + self::mul($a3, $b10, 24) + self::mul($a4, $b9, 24) + self::mul($a5, $b8, 24) + self::mul($a6, $b7, 24) + self::mul($a7, $b6, 24) + self::mul($a8, $b5, 24) + self::mul($a9, $b4, 24) + self::mul($a10, $b3, 24) + self::mul($a11, $b2, 24); $s14 = self::mul($a3, $b11, 24) + self::mul($a4, $b10, 24) + self::mul($a5, $b9, 24) + self::mul($a6, $b8, 24) + self::mul($a7, $b7, 24) + self::mul($a8, $b6, 24) + self::mul($a9, $b5, 24) + self::mul($a10, $b4, 24) + self::mul($a11, $b3, 24); $s15 = self::mul($a4, $b11, 24) + self::mul($a5, $b10, 24) + self::mul($a6, $b9, 24) + self::mul($a7, $b8, 24) + self::mul($a8, $b7, 24) + self::mul($a9, $b6, 24) + self::mul($a10, $b5, 24) + self::mul($a11, $b4, 24); $s16 = self::mul($a5, $b11, 24) + self::mul($a6, $b10, 24) + self::mul($a7, $b9, 24) + self::mul($a8, $b8, 24) + self::mul($a9, $b7, 24) + self::mul($a10, $b6, 24) + self::mul($a11, $b5, 24); $s17 = self::mul($a6, $b11, 24) + self::mul($a7, $b10, 24) + self::mul($a8, $b9, 24) + self::mul($a9, $b8, 24) + self::mul($a10, $b7, 24) + self::mul($a11, $b6, 24); $s18 = self::mul($a7, $b11, 24) + self::mul($a8, $b10, 24) + self::mul($a9, $b9, 24) + self::mul($a10, $b8, 24) + self::mul($a11, $b7, 24); $s19 = self::mul($a8, $b11, 24) + self::mul($a9, $b10, 24) + self::mul($a10, $b9, 24) + self::mul($a11, $b8, 24); $s20 = self::mul($a9, $b11, 24) + self::mul($a10, $b10, 24) + self::mul($a11, $b9, 24); $s21 = self::mul($a10, $b11, 24) + self::mul($a11, $b10, 24); $s22 = self::mul($a11, $b11, 24); $s23 = 0; $carry0 = ($s0 + (1 << 20)) >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; $carry2 = ($s2 + (1 << 20)) >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; $carry4 = ($s4 + (1 << 20)) >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; $carry6 = ($s6 + (1 << 20)) >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; $carry8 = ($s8 + (1 << 20)) >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; $carry10 = ($s10 + (1 << 20)) >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; $carry12 = ($s12 + (1 << 20)) >> 21; $s13 += $carry12; $s12 -= $carry12 << 21; $carry14 = ($s14 + (1 << 20)) >> 21; $s15 += $carry14; $s14 -= $carry14 << 21; $carry16 = ($s16 + (1 << 20)) >> 21; $s17 += $carry16; $s16 -= $carry16 << 21; $carry18 = ($s18 + (1 << 20)) >> 21; $s19 += $carry18; $s18 -= $carry18 << 21; $carry20 = ($s20 + (1 << 20)) >> 21; $s21 += $carry20; $s20 -= $carry20 << 21; $carry22 = ($s22 + (1 << 20)) >> 21; $s23 += $carry22; $s22 -= $carry22 << 21; $carry1 = ($s1 + (1 << 20)) >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; $carry3 = ($s3 + (1 << 20)) >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; $carry5 = ($s5 + (1 << 20)) >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; $carry7 = ($s7 + (1 << 20)) >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; $carry9 = ($s9 + (1 << 20)) >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; $carry11 = ($s11 + (1 << 20)) >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; $carry13 = ($s13 + (1 << 20)) >> 21; $s14 += $carry13; $s13 -= $carry13 << 21; $carry15 = ($s15 + (1 << 20)) >> 21; $s16 += $carry15; $s15 -= $carry15 << 21; $carry17 = ($s17 + (1 << 20)) >> 21; $s18 += $carry17; $s17 -= $carry17 << 21; $carry19 = ($s19 + (1 << 20)) >> 21; $s20 += $carry19; $s19 -= $carry19 << 21; $carry21 = ($s21 + (1 << 20)) >> 21; $s22 += $carry21; $s21 -= $carry21 << 21; $s11 += self::mul($s23, 666643, 20); $s12 += self::mul($s23, 470296, 19); $s13 += self::mul($s23, 654183, 20); $s14 -= self::mul($s23, 997805, 20); $s15 += self::mul($s23, 136657, 18); $s16 -= self::mul($s23, 683901, 20); $s10 += self::mul($s22, 666643, 20); $s11 += self::mul($s22, 470296, 19); $s12 += self::mul($s22, 654183, 20); $s13 -= self::mul($s22, 997805, 20); $s14 += self::mul($s22, 136657, 18); $s15 -= self::mul($s22, 683901, 20); $s9 += self::mul($s21, 666643, 20); $s10 += self::mul($s21, 470296, 19); $s11 += self::mul($s21, 654183, 20); $s12 -= self::mul($s21, 997805, 20); $s13 += self::mul($s21, 136657, 18); $s14 -= self::mul($s21, 683901, 20); $s8 += self::mul($s20, 666643, 20); $s9 += self::mul($s20, 470296, 19); $s10 += self::mul($s20, 654183, 20); $s11 -= self::mul($s20, 997805, 20); $s12 += self::mul($s20, 136657, 18); $s13 -= self::mul($s20, 683901, 20); $s7 += self::mul($s19, 666643, 20); $s8 += self::mul($s19, 470296, 19); $s9 += self::mul($s19, 654183, 20); $s10 -= self::mul($s19, 997805, 20); $s11 += self::mul($s19, 136657, 18); $s12 -= self::mul($s19, 683901, 20); $s6 += self::mul($s18, 666643, 20); $s7 += self::mul($s18, 470296, 19); $s8 += self::mul($s18, 654183, 20); $s9 -= self::mul($s18, 997805, 20); $s10 += self::mul($s18, 136657, 18); $s11 -= self::mul($s18, 683901, 20); $carry6 = ($s6 + (1 << 20)) >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; $carry8 = ($s8 + (1 << 20)) >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; $carry10 = ($s10 + (1 << 20)) >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; $carry12 = ($s12 + (1 << 20)) >> 21; $s13 += $carry12; $s12 -= $carry12 << 21; $carry14 = ($s14 + (1 << 20)) >> 21; $s15 += $carry14; $s14 -= $carry14 << 21; $carry16 = ($s16 + (1 << 20)) >> 21; $s17 += $carry16; $s16 -= $carry16 << 21; $carry7 = ($s7 + (1 << 20)) >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; $carry9 = ($s9 + (1 << 20)) >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; $carry11 = ($s11 + (1 << 20)) >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; $carry13 = ($s13 + (1 << 20)) >> 21; $s14 += $carry13; $s13 -= $carry13 << 21; $carry15 = ($s15 + (1 << 20)) >> 21; $s16 += $carry15; $s15 -= $carry15 << 21; $s5 += self::mul($s17, 666643, 20); $s6 += self::mul($s17, 470296, 19); $s7 += self::mul($s17, 654183, 20); $s8 -= self::mul($s17, 997805, 20); $s9 += self::mul($s17, 136657, 18); $s10 -= self::mul($s17, 683901, 20); $s4 += self::mul($s16, 666643, 20); $s5 += self::mul($s16, 470296, 19); $s6 += self::mul($s16, 654183, 20); $s7 -= self::mul($s16, 997805, 20); $s8 += self::mul($s16, 136657, 18); $s9 -= self::mul($s16, 683901, 20); $s3 += self::mul($s15, 666643, 20); $s4 += self::mul($s15, 470296, 19); $s5 += self::mul($s15, 654183, 20); $s6 -= self::mul($s15, 997805, 20); $s7 += self::mul($s15, 136657, 18); $s8 -= self::mul($s15, 683901, 20); $s2 += self::mul($s14, 666643, 20); $s3 += self::mul($s14, 470296, 19); $s4 += self::mul($s14, 654183, 20); $s5 -= self::mul($s14, 997805, 20); $s6 += self::mul($s14, 136657, 18); $s7 -= self::mul($s14, 683901, 20); $s1 += self::mul($s13, 666643, 20); $s2 += self::mul($s13, 470296, 19); $s3 += self::mul($s13, 654183, 20); $s4 -= self::mul($s13, 997805, 20); $s5 += self::mul($s13, 136657, 18); $s6 -= self::mul($s13, 683901, 20); $s0 += self::mul($s12, 666643, 20); $s1 += self::mul($s12, 470296, 19); $s2 += self::mul($s12, 654183, 20); $s3 -= self::mul($s12, 997805, 20); $s4 += self::mul($s12, 136657, 18); $s5 -= self::mul($s12, 683901, 20); $s12 = 0; $carry0 = ($s0 + (1 << 20)) >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; $carry2 = ($s2 + (1 << 20)) >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; $carry4 = ($s4 + (1 << 20)) >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; $carry6 = ($s6 + (1 << 20)) >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; $carry8 = ($s8 + (1 << 20)) >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; $carry10 = ($s10 + (1 << 20)) >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; $carry1 = ($s1 + (1 << 20)) >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; $carry3 = ($s3 + (1 << 20)) >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; $carry5 = ($s5 + (1 << 20)) >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; $carry7 = ($s7 + (1 << 20)) >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; $carry9 = ($s9 + (1 << 20)) >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; $carry11 = ($s11 + (1 << 20)) >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; $s0 += self::mul($s12, 666643, 20); $s1 += self::mul($s12, 470296, 19); $s2 += self::mul($s12, 654183, 20); $s3 -= self::mul($s12, 997805, 20); $s4 += self::mul($s12, 136657, 18); $s5 -= self::mul($s12, 683901, 20); $s12 = 0; $carry0 = $s0 >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; $carry1 = $s1 >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; $carry2 = $s2 >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; $carry3 = $s3 >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; $carry4 = $s4 >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; $carry5 = $s5 >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; $carry6 = $s6 >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; $carry7 = $s7 >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; $carry8 = $s8 >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; $carry9 = $s9 >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; $carry10 = $s10 >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; $carry11 = $s11 >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; $s0 += self::mul($s12, 666643, 20); $s1 += self::mul($s12, 470296, 19); $s2 += self::mul($s12, 654183, 20); $s3 -= self::mul($s12, 997805, 20); $s4 += self::mul($s12, 136657, 18); $s5 -= self::mul($s12, 683901, 20); $carry0 = $s0 >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; $carry1 = $s1 >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; $carry2 = $s2 >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; $carry3 = $s3 >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; $carry4 = $s4 >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; $carry5 = $s5 >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; $carry6 = $s6 >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; $carry7 = $s7 >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; $carry8 = $s8 >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; $carry9 = $s9 >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; $carry10 = $s10 >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; /** * @var array<int, int> */ $arr = array( (int) (0xff & ($s0 >> 0)), (int) (0xff & ($s0 >> 8)), (int) (0xff & (($s0 >> 16) | $s1 << 5)), (int) (0xff & ($s1 >> 3)), (int) (0xff & ($s1 >> 11)), (int) (0xff & (($s1 >> 19) | $s2 << 2)), (int) (0xff & ($s2 >> 6)), (int) (0xff & (($s2 >> 14) | $s3 << 7)), (int) (0xff & ($s3 >> 1)), (int) (0xff & ($s3 >> 9)), (int) (0xff & (($s3 >> 17) | $s4 << 4)), (int) (0xff & ($s4 >> 4)), (int) (0xff & ($s4 >> 12)), (int) (0xff & (($s4 >> 20) | $s5 << 1)), (int) (0xff & ($s5 >> 7)), (int) (0xff & (($s5 >> 15) | $s6 << 6)), (int) (0xff & ($s6 >> 2)), (int) (0xff & ($s6 >> 10)), (int) (0xff & (($s6 >> 18) | $s7 << 3)), (int) (0xff & ($s7 >> 5)), (int) (0xff & ($s7 >> 13)), (int) (0xff & ($s8 >> 0)), (int) (0xff & ($s8 >> 8)), (int) (0xff & (($s8 >> 16) | $s9 << 5)), (int) (0xff & ($s9 >> 3)), (int) (0xff & ($s9 >> 11)), (int) (0xff & (($s9 >> 19) | $s10 << 2)), (int) (0xff & ($s10 >> 6)), (int) (0xff & (($s10 >> 14) | $s11 << 7)), (int) (0xff & ($s11 >> 1)), (int) (0xff & ($s11 >> 9)), 0xff & ($s11 >> 17) ); return self::intArrayToString($arr); } /** * @internal You should not use this directly from another application * * @param string $s * @return string * @throws TypeError */ public static function sc_reduce($s) { $s0 = 2097151 & self::load_3(self::substr($s, 0, 3)); $s1 = 2097151 & (self::load_4(self::substr($s, 2, 4)) >> 5); $s2 = 2097151 & (self::load_3(self::substr($s, 5, 3)) >> 2); $s3 = 2097151 & (self::load_4(self::substr($s, 7, 4)) >> 7); $s4 = 2097151 & (self::load_4(self::substr($s, 10, 4)) >> 4); $s5 = 2097151 & (self::load_3(self::substr($s, 13, 3)) >> 1); $s6 = 2097151 & (self::load_4(self::substr($s, 15, 4)) >> 6); $s7 = 2097151 & (self::load_3(self::substr($s, 18, 4)) >> 3); $s8 = 2097151 & self::load_3(self::substr($s, 21, 3)); $s9 = 2097151 & (self::load_4(self::substr($s, 23, 4)) >> 5); $s10 = 2097151 & (self::load_3(self::substr($s, 26, 3)) >> 2); $s11 = 2097151 & (self::load_4(self::substr($s, 28, 4)) >> 7); $s12 = 2097151 & (self::load_4(self::substr($s, 31, 4)) >> 4); $s13 = 2097151 & (self::load_3(self::substr($s, 34, 3)) >> 1); $s14 = 2097151 & (self::load_4(self::substr($s, 36, 4)) >> 6); $s15 = 2097151 & (self::load_3(self::substr($s, 39, 4)) >> 3); $s16 = 2097151 & self::load_3(self::substr($s, 42, 3)); $s17 = 2097151 & (self::load_4(self::substr($s, 44, 4)) >> 5); $s18 = 2097151 & (self::load_3(self::substr($s, 47, 3)) >> 2); $s19 = 2097151 & (self::load_4(self::substr($s, 49, 4)) >> 7); $s20 = 2097151 & (self::load_4(self::substr($s, 52, 4)) >> 4); $s21 = 2097151 & (self::load_3(self::substr($s, 55, 3)) >> 1); $s22 = 2097151 & (self::load_4(self::substr($s, 57, 4)) >> 6); $s23 = 0x1fffffff & (self::load_4(self::substr($s, 60, 4)) >> 3); $s11 += self::mul($s23, 666643, 20); $s12 += self::mul($s23, 470296, 19); $s13 += self::mul($s23, 654183, 20); $s14 -= self::mul($s23, 997805, 20); $s15 += self::mul($s23, 136657, 18); $s16 -= self::mul($s23, 683901, 20); $s10 += self::mul($s22, 666643, 20); $s11 += self::mul($s22, 470296, 19); $s12 += self::mul($s22, 654183, 20); $s13 -= self::mul($s22, 997805, 20); $s14 += self::mul($s22, 136657, 18); $s15 -= self::mul($s22, 683901, 20); $s9 += self::mul($s21, 666643, 20); $s10 += self::mul($s21, 470296, 19); $s11 += self::mul($s21, 654183, 20); $s12 -= self::mul($s21, 997805, 20); $s13 += self::mul($s21, 136657, 18); $s14 -= self::mul($s21, 683901, 20); $s8 += self::mul($s20, 666643, 20); $s9 += self::mul($s20, 470296, 19); $s10 += self::mul($s20, 654183, 20); $s11 -= self::mul($s20, 997805, 20); $s12 += self::mul($s20, 136657, 18); $s13 -= self::mul($s20, 683901, 20); $s7 += self::mul($s19, 666643, 20); $s8 += self::mul($s19, 470296, 19); $s9 += self::mul($s19, 654183, 20); $s10 -= self::mul($s19, 997805, 20); $s11 += self::mul($s19, 136657, 18); $s12 -= self::mul($s19, 683901, 20); $s6 += self::mul($s18, 666643, 20); $s7 += self::mul($s18, 470296, 19); $s8 += self::mul($s18, 654183, 20); $s9 -= self::mul($s18, 997805, 20); $s10 += self::mul($s18, 136657, 18); $s11 -= self::mul($s18, 683901, 20); $carry6 = ($s6 + (1 << 20)) >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; $carry8 = ($s8 + (1 << 20)) >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; $carry10 = ($s10 + (1 << 20)) >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; $carry12 = ($s12 + (1 << 20)) >> 21; $s13 += $carry12; $s12 -= $carry12 << 21; $carry14 = ($s14 + (1 << 20)) >> 21; $s15 += $carry14; $s14 -= $carry14 << 21; $carry16 = ($s16 + (1 << 20)) >> 21; $s17 += $carry16; $s16 -= $carry16 << 21; $carry7 = ($s7 + (1 << 20)) >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; $carry9 = ($s9 + (1 << 20)) >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; $carry11 = ($s11 + (1 << 20)) >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; $carry13 = ($s13 + (1 << 20)) >> 21; $s14 += $carry13; $s13 -= $carry13 << 21; $carry15 = ($s15 + (1 << 20)) >> 21; $s16 += $carry15; $s15 -= $carry15 << 21; $s5 += self::mul($s17, 666643, 20); $s6 += self::mul($s17, 470296, 19); $s7 += self::mul($s17, 654183, 20); $s8 -= self::mul($s17, 997805, 20); $s9 += self::mul($s17, 136657, 18); $s10 -= self::mul($s17, 683901, 20); $s4 += self::mul($s16, 666643, 20); $s5 += self::mul($s16, 470296, 19); $s6 += self::mul($s16, 654183, 20); $s7 -= self::mul($s16, 997805, 20); $s8 += self::mul($s16, 136657, 18); $s9 -= self::mul($s16, 683901, 20); $s3 += self::mul($s15, 666643, 20); $s4 += self::mul($s15, 470296, 19); $s5 += self::mul($s15, 654183, 20); $s6 -= self::mul($s15, 997805, 20); $s7 += self::mul($s15, 136657, 18); $s8 -= self::mul($s15, 683901, 20); $s2 += self::mul($s14, 666643, 20); $s3 += self::mul($s14, 470296, 19); $s4 += self::mul($s14, 654183, 20); $s5 -= self::mul($s14, 997805, 20); $s6 += self::mul($s14, 136657, 18); $s7 -= self::mul($s14, 683901, 20); $s1 += self::mul($s13, 666643, 20); $s2 += self::mul($s13, 470296, 19); $s3 += self::mul($s13, 654183, 20); $s4 -= self::mul($s13, 997805, 20); $s5 += self::mul($s13, 136657, 18); $s6 -= self::mul($s13, 683901, 20); $s0 += self::mul($s12, 666643, 20); $s1 += self::mul($s12, 470296, 19); $s2 += self::mul($s12, 654183, 20); $s3 -= self::mul($s12, 997805, 20); $s4 += self::mul($s12, 136657, 18); $s5 -= self::mul($s12, 683901, 20); $s12 = 0; $carry0 = ($s0 + (1 << 20)) >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; $carry2 = ($s2 + (1 << 20)) >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; $carry4 = ($s4 + (1 << 20)) >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; $carry6 = ($s6 + (1 << 20)) >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; $carry8 = ($s8 + (1 << 20)) >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; $carry10 = ($s10 + (1 << 20)) >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; $carry1 = ($s1 + (1 << 20)) >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; $carry3 = ($s3 + (1 << 20)) >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; $carry5 = ($s5 + (1 << 20)) >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; $carry7 = ($s7 + (1 << 20)) >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; $carry9 = ($s9 + (1 << 20)) >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; $carry11 = ($s11 + (1 << 20)) >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; $s0 += self::mul($s12, 666643, 20); $s1 += self::mul($s12, 470296, 19); $s2 += self::mul($s12, 654183, 20); $s3 -= self::mul($s12, 997805, 20); $s4 += self::mul($s12, 136657, 18); $s5 -= self::mul($s12, 683901, 20); $s12 = 0; $carry0 = $s0 >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; $carry1 = $s1 >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; $carry2 = $s2 >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; $carry3 = $s3 >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; $carry4 = $s4 >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; $carry5 = $s5 >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; $carry6 = $s6 >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; $carry7 = $s7 >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; $carry8 = $s8 >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; $carry9 = $s9 >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; $carry10 = $s10 >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; $carry11 = $s11 >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; $s0 += self::mul($s12, 666643, 20); $s1 += self::mul($s12, 470296, 19); $s2 += self::mul($s12, 654183, 20); $s3 -= self::mul($s12, 997805, 20); $s4 += self::mul($s12, 136657, 18); $s5 -= self::mul($s12, 683901, 20); $carry0 = $s0 >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; $carry1 = $s1 >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; $carry2 = $s2 >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; $carry3 = $s3 >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; $carry4 = $s4 >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; $carry5 = $s5 >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; $carry6 = $s6 >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; $carry7 = $s7 >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; $carry8 = $s8 >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; $carry9 = $s9 >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; $carry10 = $s10 >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; /** * @var array<int, int> */ $arr = array( (int) (0xff & ($s0 >> 0)), (int) (0xff & ($s0 >> 8)), (int) (0xff & (($s0 >> 16) | $s1 << 5)), (int) (0xff & ($s1 >> 3)), (int) (0xff & ($s1 >> 11)), (int) (0xff & (($s1 >> 19) | $s2 << 2)), (int) (0xff & ($s2 >> 6)), (int) (0xff & (($s2 >> 14) | $s3 << 7)), (int) (0xff & ($s3 >> 1)), (int) (0xff & ($s3 >> 9)), (int) (0xff & (($s3 >> 17) | $s4 << 4)), (int) (0xff & ($s4 >> 4)), (int) (0xff & ($s4 >> 12)), (int) (0xff & (($s4 >> 20) | $s5 << 1)), (int) (0xff & ($s5 >> 7)), (int) (0xff & (($s5 >> 15) | $s6 << 6)), (int) (0xff & ($s6 >> 2)), (int) (0xff & ($s6 >> 10)), (int) (0xff & (($s6 >> 18) | $s7 << 3)), (int) (0xff & ($s7 >> 5)), (int) (0xff & ($s7 >> 13)), (int) (0xff & ($s8 >> 0)), (int) (0xff & ($s8 >> 8)), (int) (0xff & (($s8 >> 16) | $s9 << 5)), (int) (0xff & ($s9 >> 3)), (int) (0xff & ($s9 >> 11)), (int) (0xff & (($s9 >> 19) | $s10 << 2)), (int) (0xff & ($s10 >> 6)), (int) (0xff & (($s10 >> 14) | $s11 << 7)), (int) (0xff & ($s11 >> 1)), (int) (0xff & ($s11 >> 9)), (int) (0xff & ($s11 >> 17)) ); return self::intArrayToString($arr); } /** * multiply by the order of the main subgroup l = 2^252+27742317777372353535851937790883648493 * * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 */ public static function ge_mul_l(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A) { $aslide = array( 13, 0, 0, 0, 0, -1, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 0, -5, 0, 0, 0, 0, 0, 0, -3, 0, 0, 0, 0, -13, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, -13, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0, 0, -3, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 3, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ); /** @var array<int, ParagonIE_Sodium_Core_Curve25519_Ge_Cached> $Ai size 8 */ $Ai = array(); # ge_p3_to_cached(&Ai[0], A); $Ai[0] = self::ge_p3_to_cached($A); # ge_p3_dbl(&t, A); $t = self::ge_p3_dbl($A); # ge_p1p1_to_p3(&A2, &t); $A2 = self::ge_p1p1_to_p3($t); for ($i = 1; $i < 8; ++$i) { # ge_add(&t, &A2, &Ai[0]); $t = self::ge_add($A2, $Ai[$i - 1]); # ge_p1p1_to_p3(&u, &t); $u = self::ge_p1p1_to_p3($t); # ge_p3_to_cached(&Ai[i], &u); $Ai[$i] = self::ge_p3_to_cached($u); } $r = self::ge_p3_0(); for ($i = 252; $i >= 0; --$i) { $t = self::ge_p3_dbl($r); if ($aslide[$i] > 0) { # ge_p1p1_to_p3(&u, &t); $u = self::ge_p1p1_to_p3($t); # ge_add(&t, &u, &Ai[aslide[i] / 2]); $t = self::ge_add($u, $Ai[(int)($aslide[$i] / 2)]); } elseif ($aslide[$i] < 0) { # ge_p1p1_to_p3(&u, &t); $u = self::ge_p1p1_to_p3($t); # ge_sub(&t, &u, &Ai[(-aslide[i]) / 2]); $t = self::ge_sub($u, $Ai[(int)(-$aslide[$i] / 2)]); } } # ge_p1p1_to_p3(r, &t); return self::ge_p1p1_to_p3($t); } /** * @param string $a * @param string $b * @return string */ public static function sc25519_mul($a, $b) { // int64_t a0 = 2097151 & load_3(a); // int64_t a1 = 2097151 & (load_4(a + 2) >> 5); // int64_t a2 = 2097151 & (load_3(a + 5) >> 2); // int64_t a3 = 2097151 & (load_4(a + 7) >> 7); // int64_t a4 = 2097151 & (load_4(a + 10) >> 4); // int64_t a5 = 2097151 & (load_3(a + 13) >> 1); // int64_t a6 = 2097151 & (load_4(a + 15) >> 6); // int64_t a7 = 2097151 & (load_3(a + 18) >> 3); // int64_t a8 = 2097151 & load_3(a + 21); // int64_t a9 = 2097151 & (load_4(a + 23) >> 5); // int64_t a10 = 2097151 & (load_3(a + 26) >> 2); // int64_t a11 = (load_4(a + 28) >> 7); $a0 = 2097151 & self::load_3(self::substr($a, 0, 3)); $a1 = 2097151 & (self::load_4(self::substr($a, 2, 4)) >> 5); $a2 = 2097151 & (self::load_3(self::substr($a, 5, 3)) >> 2); $a3 = 2097151 & (self::load_4(self::substr($a, 7, 4)) >> 7); $a4 = 2097151 & (self::load_4(self::substr($a, 10, 4)) >> 4); $a5 = 2097151 & (self::load_3(self::substr($a, 13, 3)) >> 1); $a6 = 2097151 & (self::load_4(self::substr($a, 15, 4)) >> 6); $a7 = 2097151 & (self::load_3(self::substr($a, 18, 3)) >> 3); $a8 = 2097151 & self::load_3(self::substr($a, 21, 3)); $a9 = 2097151 & (self::load_4(self::substr($a, 23, 4)) >> 5); $a10 = 2097151 & (self::load_3(self::substr($a, 26, 3)) >> 2); $a11 = (self::load_4(self::substr($a, 28, 4)) >> 7); // int64_t b0 = 2097151 & load_3(b); // int64_t b1 = 2097151 & (load_4(b + 2) >> 5); // int64_t b2 = 2097151 & (load_3(b + 5) >> 2); // int64_t b3 = 2097151 & (load_4(b + 7) >> 7); // int64_t b4 = 2097151 & (load_4(b + 10) >> 4); // int64_t b5 = 2097151 & (load_3(b + 13) >> 1); // int64_t b6 = 2097151 & (load_4(b + 15) >> 6); // int64_t b7 = 2097151 & (load_3(b + 18) >> 3); // int64_t b8 = 2097151 & load_3(b + 21); // int64_t b9 = 2097151 & (load_4(b + 23) >> 5); // int64_t b10 = 2097151 & (load_3(b + 26) >> 2); // int64_t b11 = (load_4(b + 28) >> 7); $b0 = 2097151 & self::load_3(self::substr($b, 0, 3)); $b1 = 2097151 & (self::load_4(self::substr($b, 2, 4)) >> 5); $b2 = 2097151 & (self::load_3(self::substr($b, 5, 3)) >> 2); $b3 = 2097151 & (self::load_4(self::substr($b, 7, 4)) >> 7); $b4 = 2097151 & (self::load_4(self::substr($b, 10, 4)) >> 4); $b5 = 2097151 & (self::load_3(self::substr($b, 13, 3)) >> 1); $b6 = 2097151 & (self::load_4(self::substr($b, 15, 4)) >> 6); $b7 = 2097151 & (self::load_3(self::substr($b, 18, 3)) >> 3); $b8 = 2097151 & self::load_3(self::substr($b, 21, 3)); $b9 = 2097151 & (self::load_4(self::substr($b, 23, 4)) >> 5); $b10 = 2097151 & (self::load_3(self::substr($b, 26, 3)) >> 2); $b11 = (self::load_4(self::substr($b, 28, 4)) >> 7); // s0 = a0 * b0; // s1 = a0 * b1 + a1 * b0; // s2 = a0 * b2 + a1 * b1 + a2 * b0; // s3 = a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0; // s4 = a0 * b4 + a1 * b3 + a2 * b2 + a3 * b1 + a4 * b0; // s5 = a0 * b5 + a1 * b4 + a2 * b3 + a3 * b2 + a4 * b1 + a5 * b0; // s6 = a0 * b6 + a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1 + a6 * b0; // s7 = a0 * b7 + a1 * b6 + a2 * b5 + a3 * b4 + a4 * b3 + a5 * b2 + // a6 * b1 + a7 * b0; // s8 = a0 * b8 + a1 * b7 + a2 * b6 + a3 * b5 + a4 * b4 + a5 * b3 + // a6 * b2 + a7 * b1 + a8 * b0; // s9 = a0 * b9 + a1 * b8 + a2 * b7 + a3 * b6 + a4 * b5 + a5 * b4 + // a6 * b3 + a7 * b2 + a8 * b1 + a9 * b0; // s10 = a0 * b10 + a1 * b9 + a2 * b8 + a3 * b7 + a4 * b6 + a5 * b5 + // a6 * b4 + a7 * b3 + a8 * b2 + a9 * b1 + a10 * b0; // s11 = a0 * b11 + a1 * b10 + a2 * b9 + a3 * b8 + a4 * b7 + a5 * b6 + // a6 * b5 + a7 * b4 + a8 * b3 + a9 * b2 + a10 * b1 + a11 * b0; // s12 = a1 * b11 + a2 * b10 + a3 * b9 + a4 * b8 + a5 * b7 + a6 * b6 + // a7 * b5 + a8 * b4 + a9 * b3 + a10 * b2 + a11 * b1; // s13 = a2 * b11 + a3 * b10 + a4 * b9 + a5 * b8 + a6 * b7 + a7 * b6 + // a8 * b5 + a9 * b4 + a10 * b3 + a11 * b2; // s14 = a3 * b11 + a4 * b10 + a5 * b9 + a6 * b8 + a7 * b7 + a8 * b6 + // a9 * b5 + a10 * b4 + a11 * b3; // s15 = a4 * b11 + a5 * b10 + a6 * b9 + a7 * b8 + a8 * b7 + a9 * b6 + // a10 * b5 + a11 * b4; // s16 = // a5 * b11 + a6 * b10 + a7 * b9 + a8 * b8 + a9 * b7 + a10 * b6 + a11 * b5; // s17 = a6 * b11 + a7 * b10 + a8 * b9 + a9 * b8 + a10 * b7 + a11 * b6; // s18 = a7 * b11 + a8 * b10 + a9 * b9 + a10 * b8 + a11 * b7; // s19 = a8 * b11 + a9 * b10 + a10 * b9 + a11 * b8; // s20 = a9 * b11 + a10 * b10 + a11 * b9; // s21 = a10 * b11 + a11 * b10; // s22 = a11 * b11; // s23 = 0; $s0 = self::mul($a0, $b0, 22); $s1 = self::mul($a0, $b1, 22) + self::mul($a1, $b0, 22); $s2 = self::mul($a0, $b2, 22) + self::mul($a1, $b1, 22) + self::mul($a2, $b0, 22); $s3 = self::mul($a0, $b3, 22) + self::mul($a1, $b2, 22) + self::mul($a2, $b1, 22) + self::mul($a3, $b0, 22); $s4 = self::mul($a0, $b4, 22) + self::mul($a1, $b3, 22) + self::mul($a2, $b2, 22) + self::mul($a3, $b1, 22) + self::mul($a4, $b0, 22); $s5 = self::mul($a0, $b5, 22) + self::mul($a1, $b4, 22) + self::mul($a2, $b3, 22) + self::mul($a3, $b2, 22) + self::mul($a4, $b1, 22) + self::mul($a5, $b0, 22); $s6 = self::mul($a0, $b6, 22) + self::mul($a1, $b5, 22) + self::mul($a2, $b4, 22) + self::mul($a3, $b3, 22) + self::mul($a4, $b2, 22) + self::mul($a5, $b1, 22) + self::mul($a6, $b0, 22); $s7 = self::mul($a0, $b7, 22) + self::mul($a1, $b6, 22) + self::mul($a2, $b5, 22) + self::mul($a3, $b4, 22) + self::mul($a4, $b3, 22) + self::mul($a5, $b2, 22) + self::mul($a6, $b1, 22) + self::mul($a7, $b0, 22); $s8 = self::mul($a0, $b8, 22) + self::mul($a1, $b7, 22) + self::mul($a2, $b6, 22) + self::mul($a3, $b5, 22) + self::mul($a4, $b4, 22) + self::mul($a5, $b3, 22) + self::mul($a6, $b2, 22) + self::mul($a7, $b1, 22) + self::mul($a8, $b0, 22); $s9 = self::mul($a0, $b9, 22) + self::mul($a1, $b8, 22) + self::mul($a2, $b7, 22) + self::mul($a3, $b6, 22) + self::mul($a4, $b5, 22) + self::mul($a5, $b4, 22) + self::mul($a6, $b3, 22) + self::mul($a7, $b2, 22) + self::mul($a8, $b1, 22) + self::mul($a9, $b0, 22); $s10 = self::mul($a0, $b10, 22) + self::mul($a1, $b9, 22) + self::mul($a2, $b8, 22) + self::mul($a3, $b7, 22) + self::mul($a4, $b6, 22) + self::mul($a5, $b5, 22) + self::mul($a6, $b4, 22) + self::mul($a7, $b3, 22) + self::mul($a8, $b2, 22) + self::mul($a9, $b1, 22) + self::mul($a10, $b0, 22); $s11 = self::mul($a0, $b11, 22) + self::mul($a1, $b10, 22) + self::mul($a2, $b9, 22) + self::mul($a3, $b8, 22) + self::mul($a4, $b7, 22) + self::mul($a5, $b6, 22) + self::mul($a6, $b5, 22) + self::mul($a7, $b4, 22) + self::mul($a8, $b3, 22) + self::mul($a9, $b2, 22) + self::mul($a10, $b1, 22) + self::mul($a11, $b0, 22); $s12 = self::mul($a1, $b11, 22) + self::mul($a2, $b10, 22) + self::mul($a3, $b9, 22) + self::mul($a4, $b8, 22) + self::mul($a5, $b7, 22) + self::mul($a6, $b6, 22) + self::mul($a7, $b5, 22) + self::mul($a8, $b4, 22) + self::mul($a9, $b3, 22) + self::mul($a10, $b2, 22) + self::mul($a11, $b1, 22); $s13 = self::mul($a2, $b11, 22) + self::mul($a3, $b10, 22) + self::mul($a4, $b9, 22) + self::mul($a5, $b8, 22) + self::mul($a6, $b7, 22) + self::mul($a7, $b6, 22) + self::mul($a8, $b5, 22) + self::mul($a9, $b4, 22) + self::mul($a10, $b3, 22) + self::mul($a11, $b2, 22); $s14 = self::mul($a3, $b11, 22) + self::mul($a4, $b10, 22) + self::mul($a5, $b9, 22) + self::mul($a6, $b8, 22) + self::mul($a7, $b7, 22) + self::mul($a8, $b6, 22) + self::mul($a9, $b5, 22) + self::mul($a10, $b4, 22) + self::mul($a11, $b3, 22); $s15 = self::mul($a4, $b11, 22) + self::mul($a5, $b10, 22) + self::mul($a6, $b9, 22) + self::mul($a7, $b8, 22) + self::mul($a8, $b7, 22) + self::mul($a9, $b6, 22) + self::mul($a10, $b5, 22) + self::mul($a11, $b4, 22); $s16 = self::mul($a5, $b11, 22) + self::mul($a6, $b10, 22) + self::mul($a7, $b9, 22) + self::mul($a8, $b8, 22) + self::mul($a9, $b7, 22) + self::mul($a10, $b6, 22) + self::mul($a11, $b5, 22); $s17 = self::mul($a6, $b11, 22) + self::mul($a7, $b10, 22) + self::mul($a8, $b9, 22) + self::mul($a9, $b8, 22) + self::mul($a10, $b7, 22) + self::mul($a11, $b6, 22); $s18 = self::mul($a7, $b11, 22) + self::mul($a8, $b10, 22) + self::mul($a9, $b9, 22) + self::mul($a10, $b8, 22) + self::mul($a11, $b7, 22); $s19 = self::mul($a8, $b11, 22) + self::mul($a9, $b10, 22) + self::mul($a10, $b9, 22) + self::mul($a11, $b8, 22); $s20 = self::mul($a9, $b11, 22) + self::mul($a10, $b10, 22) + self::mul($a11, $b9, 22); $s21 = self::mul($a10, $b11, 22) + self::mul($a11, $b10, 22); $s22 = self::mul($a11, $b11, 22); $s23 = 0; // carry0 = (s0 + (int64_t) (1L << 20)) >> 21; // s1 += carry0; // s0 -= carry0 * ((uint64_t) 1L << 21); $carry0 = ($s0 + (1 << 20)) >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; // carry2 = (s2 + (int64_t) (1L << 20)) >> 21; // s3 += carry2; // s2 -= carry2 * ((uint64_t) 1L << 21); $carry2 = ($s2 + (1 << 20)) >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; // carry4 = (s4 + (int64_t) (1L << 20)) >> 21; // s5 += carry4; // s4 -= carry4 * ((uint64_t) 1L << 21); $carry4 = ($s4 + (1 << 20)) >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; // carry6 = (s6 + (int64_t) (1L << 20)) >> 21; // s7 += carry6; // s6 -= carry6 * ((uint64_t) 1L << 21); $carry6 = ($s6 + (1 << 20)) >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; // carry8 = (s8 + (int64_t) (1L << 20)) >> 21; // s9 += carry8; // s8 -= carry8 * ((uint64_t) 1L << 21); $carry8 = ($s8 + (1 << 20)) >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; // carry10 = (s10 + (int64_t) (1L << 20)) >> 21; // s11 += carry10; // s10 -= carry10 * ((uint64_t) 1L << 21); $carry10 = ($s10 + (1 << 20)) >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; // carry12 = (s12 + (int64_t) (1L << 20)) >> 21; // s13 += carry12; // s12 -= carry12 * ((uint64_t) 1L << 21); $carry12 = ($s12 + (1 << 20)) >> 21; $s13 += $carry12; $s12 -= $carry12 << 21; // carry14 = (s14 + (int64_t) (1L << 20)) >> 21; // s15 += carry14; // s14 -= carry14 * ((uint64_t) 1L << 21); $carry14 = ($s14 + (1 << 20)) >> 21; $s15 += $carry14; $s14 -= $carry14 << 21; // carry16 = (s16 + (int64_t) (1L << 20)) >> 21; // s17 += carry16; // s16 -= carry16 * ((uint64_t) 1L << 21); $carry16 = ($s16 + (1 << 20)) >> 21; $s17 += $carry16; $s16 -= $carry16 << 21; // carry18 = (s18 + (int64_t) (1L << 20)) >> 21; // s19 += carry18; // s18 -= carry18 * ((uint64_t) 1L << 21); $carry18 = ($s18 + (1 << 20)) >> 21; $s19 += $carry18; $s18 -= $carry18 << 21; // carry20 = (s20 + (int64_t) (1L << 20)) >> 21; // s21 += carry20; // s20 -= carry20 * ((uint64_t) 1L << 21); $carry20 = ($s20 + (1 << 20)) >> 21; $s21 += $carry20; $s20 -= $carry20 << 21; // carry22 = (s22 + (int64_t) (1L << 20)) >> 21; // s23 += carry22; // s22 -= carry22 * ((uint64_t) 1L << 21); $carry22 = ($s22 + (1 << 20)) >> 21; $s23 += $carry22; $s22 -= $carry22 << 21; // carry1 = (s1 + (int64_t) (1L << 20)) >> 21; // s2 += carry1; // s1 -= carry1 * ((uint64_t) 1L << 21); $carry1 = ($s1 + (1 << 20)) >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; // carry3 = (s3 + (int64_t) (1L << 20)) >> 21; // s4 += carry3; // s3 -= carry3 * ((uint64_t) 1L << 21); $carry3 = ($s3 + (1 << 20)) >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; // carry5 = (s5 + (int64_t) (1L << 20)) >> 21; // s6 += carry5; // s5 -= carry5 * ((uint64_t) 1L << 21); $carry5 = ($s5 + (1 << 20)) >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; // carry7 = (s7 + (int64_t) (1L << 20)) >> 21; // s8 += carry7; // s7 -= carry7 * ((uint64_t) 1L << 21); $carry7 = ($s7 + (1 << 20)) >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; // carry9 = (s9 + (int64_t) (1L << 20)) >> 21; // s10 += carry9; // s9 -= carry9 * ((uint64_t) 1L << 21); $carry9 = ($s9 + (1 << 20)) >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; // carry11 = (s11 + (int64_t) (1L << 20)) >> 21; // s12 += carry11; // s11 -= carry11 * ((uint64_t) 1L << 21); $carry11 = ($s11 + (1 << 20)) >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; // carry13 = (s13 + (int64_t) (1L << 20)) >> 21; // s14 += carry13; // s13 -= carry13 * ((uint64_t) 1L << 21); $carry13 = ($s13 + (1 << 20)) >> 21; $s14 += $carry13; $s13 -= $carry13 << 21; // carry15 = (s15 + (int64_t) (1L << 20)) >> 21; // s16 += carry15; // s15 -= carry15 * ((uint64_t) 1L << 21); $carry15 = ($s15 + (1 << 20)) >> 21; $s16 += $carry15; $s15 -= $carry15 << 21; // carry17 = (s17 + (int64_t) (1L << 20)) >> 21; // s18 += carry17; // s17 -= carry17 * ((uint64_t) 1L << 21); $carry17 = ($s17 + (1 << 20)) >> 21; $s18 += $carry17; $s17 -= $carry17 << 21; // carry19 = (s19 + (int64_t) (1L << 20)) >> 21; // s20 += carry19; // s19 -= carry19 * ((uint64_t) 1L << 21); $carry19 = ($s19 + (1 << 20)) >> 21; $s20 += $carry19; $s19 -= $carry19 << 21; // carry21 = (s21 + (int64_t) (1L << 20)) >> 21; // s22 += carry21; // s21 -= carry21 * ((uint64_t) 1L << 21); $carry21 = ($s21 + (1 << 20)) >> 21; $s22 += $carry21; $s21 -= $carry21 << 21; // s11 += s23 * 666643; // s12 += s23 * 470296; // s13 += s23 * 654183; // s14 -= s23 * 997805; // s15 += s23 * 136657; // s16 -= s23 * 683901; $s11 += self::mul($s23, 666643, 20); $s12 += self::mul($s23, 470296, 19); $s13 += self::mul($s23, 654183, 20); $s14 -= self::mul($s23, 997805, 20); $s15 += self::mul($s23, 136657, 18); $s16 -= self::mul($s23, 683901, 20); // s10 += s22 * 666643; // s11 += s22 * 470296; // s12 += s22 * 654183; // s13 -= s22 * 997805; // s14 += s22 * 136657; // s15 -= s22 * 683901; $s10 += self::mul($s22, 666643, 20); $s11 += self::mul($s22, 470296, 19); $s12 += self::mul($s22, 654183, 20); $s13 -= self::mul($s22, 997805, 20); $s14 += self::mul($s22, 136657, 18); $s15 -= self::mul($s22, 683901, 20); // s9 += s21 * 666643; // s10 += s21 * 470296; // s11 += s21 * 654183; // s12 -= s21 * 997805; // s13 += s21 * 136657; // s14 -= s21 * 683901; $s9 += self::mul($s21, 666643, 20); $s10 += self::mul($s21, 470296, 19); $s11 += self::mul($s21, 654183, 20); $s12 -= self::mul($s21, 997805, 20); $s13 += self::mul($s21, 136657, 18); $s14 -= self::mul($s21, 683901, 20); // s8 += s20 * 666643; // s9 += s20 * 470296; // s10 += s20 * 654183; // s11 -= s20 * 997805; // s12 += s20 * 136657; // s13 -= s20 * 683901; $s8 += self::mul($s20, 666643, 20); $s9 += self::mul($s20, 470296, 19); $s10 += self::mul($s20, 654183, 20); $s11 -= self::mul($s20, 997805, 20); $s12 += self::mul($s20, 136657, 18); $s13 -= self::mul($s20, 683901, 20); // s7 += s19 * 666643; // s8 += s19 * 470296; // s9 += s19 * 654183; // s10 -= s19 * 997805; // s11 += s19 * 136657; // s12 -= s19 * 683901; $s7 += self::mul($s19, 666643, 20); $s8 += self::mul($s19, 470296, 19); $s9 += self::mul($s19, 654183, 20); $s10 -= self::mul($s19, 997805, 20); $s11 += self::mul($s19, 136657, 18); $s12 -= self::mul($s19, 683901, 20); // s6 += s18 * 666643; // s7 += s18 * 470296; // s8 += s18 * 654183; // s9 -= s18 * 997805; // s10 += s18 * 136657; // s11 -= s18 * 683901; $s6 += self::mul($s18, 666643, 20); $s7 += self::mul($s18, 470296, 19); $s8 += self::mul($s18, 654183, 20); $s9 -= self::mul($s18, 997805, 20); $s10 += self::mul($s18, 136657, 18); $s11 -= self::mul($s18, 683901, 20); // carry6 = (s6 + (int64_t) (1L << 20)) >> 21; // s7 += carry6; // s6 -= carry6 * ((uint64_t) 1L << 21); $carry6 = ($s6 + (1 << 20)) >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; // carry8 = (s8 + (int64_t) (1L << 20)) >> 21; // s9 += carry8; // s8 -= carry8 * ((uint64_t) 1L << 21); $carry8 = ($s8 + (1 << 20)) >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; // carry10 = (s10 + (int64_t) (1L << 20)) >> 21; // s11 += carry10; // s10 -= carry10 * ((uint64_t) 1L << 21); $carry10 = ($s10 + (1 << 20)) >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; // carry12 = (s12 + (int64_t) (1L << 20)) >> 21; // s13 += carry12; // s12 -= carry12 * ((uint64_t) 1L << 21); $carry12 = ($s12 + (1 << 20)) >> 21; $s13 += $carry12; $s12 -= $carry12 << 21; // carry14 = (s14 + (int64_t) (1L << 20)) >> 21; // s15 += carry14; // s14 -= carry14 * ((uint64_t) 1L << 21); $carry14 = ($s14 + (1 << 20)) >> 21; $s15 += $carry14; $s14 -= $carry14 << 21; // carry16 = (s16 + (int64_t) (1L << 20)) >> 21; // s17 += carry16; // s16 -= carry16 * ((uint64_t) 1L << 21); $carry16 = ($s16 + (1 << 20)) >> 21; $s17 += $carry16; $s16 -= $carry16 << 21; // carry7 = (s7 + (int64_t) (1L << 20)) >> 21; // s8 += carry7; // s7 -= carry7 * ((uint64_t) 1L << 21); $carry7 = ($s7 + (1 << 20)) >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; // carry9 = (s9 + (int64_t) (1L << 20)) >> 21; // s10 += carry9; // s9 -= carry9 * ((uint64_t) 1L << 21); $carry9 = ($s9 + (1 << 20)) >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; // carry11 = (s11 + (int64_t) (1L << 20)) >> 21; // s12 += carry11; // s11 -= carry11 * ((uint64_t) 1L << 21); $carry11 = ($s11 + (1 << 20)) >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; // carry13 = (s13 + (int64_t) (1L << 20)) >> 21; // s14 += carry13; // s13 -= carry13 * ((uint64_t) 1L << 21); $carry13 = ($s13 + (1 << 20)) >> 21; $s14 += $carry13; $s13 -= $carry13 << 21; // carry15 = (s15 + (int64_t) (1L << 20)) >> 21; // s16 += carry15; // s15 -= carry15 * ((uint64_t) 1L << 21); $carry15 = ($s15 + (1 << 20)) >> 21; $s16 += $carry15; $s15 -= $carry15 << 21; // s5 += s17 * 666643; // s6 += s17 * 470296; // s7 += s17 * 654183; // s8 -= s17 * 997805; // s9 += s17 * 136657; // s10 -= s17 * 683901; $s5 += self::mul($s17, 666643, 20); $s6 += self::mul($s17, 470296, 19); $s7 += self::mul($s17, 654183, 20); $s8 -= self::mul($s17, 997805, 20); $s9 += self::mul($s17, 136657, 18); $s10 -= self::mul($s17, 683901, 20); // s4 += s16 * 666643; // s5 += s16 * 470296; // s6 += s16 * 654183; // s7 -= s16 * 997805; // s8 += s16 * 136657; // s9 -= s16 * 683901; $s4 += self::mul($s16, 666643, 20); $s5 += self::mul($s16, 470296, 19); $s6 += self::mul($s16, 654183, 20); $s7 -= self::mul($s16, 997805, 20); $s8 += self::mul($s16, 136657, 18); $s9 -= self::mul($s16, 683901, 20); // s3 += s15 * 666643; // s4 += s15 * 470296; // s5 += s15 * 654183; // s6 -= s15 * 997805; // s7 += s15 * 136657; // s8 -= s15 * 683901; $s3 += self::mul($s15, 666643, 20); $s4 += self::mul($s15, 470296, 19); $s5 += self::mul($s15, 654183, 20); $s6 -= self::mul($s15, 997805, 20); $s7 += self::mul($s15, 136657, 18); $s8 -= self::mul($s15, 683901, 20); // s2 += s14 * 666643; // s3 += s14 * 470296; // s4 += s14 * 654183; // s5 -= s14 * 997805; // s6 += s14 * 136657; // s7 -= s14 * 683901; $s2 += self::mul($s14, 666643, 20); $s3 += self::mul($s14, 470296, 19); $s4 += self::mul($s14, 654183, 20); $s5 -= self::mul($s14, 997805, 20); $s6 += self::mul($s14, 136657, 18); $s7 -= self::mul($s14, 683901, 20); // s1 += s13 * 666643; // s2 += s13 * 470296; // s3 += s13 * 654183; // s4 -= s13 * 997805; // s5 += s13 * 136657; // s6 -= s13 * 683901; $s1 += self::mul($s13, 666643, 20); $s2 += self::mul($s13, 470296, 19); $s3 += self::mul($s13, 654183, 20); $s4 -= self::mul($s13, 997805, 20); $s5 += self::mul($s13, 136657, 18); $s6 -= self::mul($s13, 683901, 20); // s0 += s12 * 666643; // s1 += s12 * 470296; // s2 += s12 * 654183; // s3 -= s12 * 997805; // s4 += s12 * 136657; // s5 -= s12 * 683901; // s12 = 0; $s0 += self::mul($s12, 666643, 20); $s1 += self::mul($s12, 470296, 19); $s2 += self::mul($s12, 654183, 20); $s3 -= self::mul($s12, 997805, 20); $s4 += self::mul($s12, 136657, 18); $s5 -= self::mul($s12, 683901, 20); $s12 = 0; // carry0 = (s0 + (int64_t) (1L << 20)) >> 21; // s1 += carry0; // s0 -= carry0 * ((uint64_t) 1L << 21); $carry0 = ($s0 + (1 << 20)) >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; // carry2 = (s2 + (int64_t) (1L << 20)) >> 21; // s3 += carry2; // s2 -= carry2 * ((uint64_t) 1L << 21); $carry2 = ($s2 + (1 << 20)) >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; // carry4 = (s4 + (int64_t) (1L << 20)) >> 21; // s5 += carry4; // s4 -= carry4 * ((uint64_t) 1L << 21); $carry4 = ($s4 + (1 << 20)) >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; // carry6 = (s6 + (int64_t) (1L << 20)) >> 21; // s7 += carry6; // s6 -= carry6 * ((uint64_t) 1L << 21); $carry6 = ($s6 + (1 << 20)) >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; // carry8 = (s8 + (int64_t) (1L << 20)) >> 21; // s9 += carry8; // s8 -= carry8 * ((uint64_t) 1L << 21); $carry8 = ($s8 + (1 << 20)) >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; // carry10 = (s10 + (int64_t) (1L << 20)) >> 21; // s11 += carry10; // s10 -= carry10 * ((uint64_t) 1L << 21); $carry10 = ($s10 + (1 << 20)) >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; // carry1 = (s1 + (int64_t) (1L << 20)) >> 21; // s2 += carry1; // s1 -= carry1 * ((uint64_t) 1L << 21); $carry1 = ($s1 + (1 << 20)) >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; // carry3 = (s3 + (int64_t) (1L << 20)) >> 21; // s4 += carry3; // s3 -= carry3 * ((uint64_t) 1L << 21); $carry3 = ($s3 + (1 << 20)) >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; // carry5 = (s5 + (int64_t) (1L << 20)) >> 21; // s6 += carry5; // s5 -= carry5 * ((uint64_t) 1L << 21); $carry5 = ($s5 + (1 << 20)) >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; // carry7 = (s7 + (int64_t) (1L << 20)) >> 21; // s8 += carry7; // s7 -= carry7 * ((uint64_t) 1L << 21); $carry7 = ($s7 + (1 << 20)) >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; // carry9 = (s9 + (int64_t) (1L << 20)) >> 21; // s10 += carry9; // s9 -= carry9 * ((uint64_t) 1L << 21); $carry9 = ($s9 + (1 << 20)) >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; // carry11 = (s11 + (int64_t) (1L << 20)) >> 21; // s12 += carry11; // s11 -= carry11 * ((uint64_t) 1L << 21); $carry11 = ($s11 + (1 << 20)) >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; // s0 += s12 * 666643; // s1 += s12 * 470296; // s2 += s12 * 654183; // s3 -= s12 * 997805; // s4 += s12 * 136657; // s5 -= s12 * 683901; // s12 = 0; $s0 += self::mul($s12, 666643, 20); $s1 += self::mul($s12, 470296, 19); $s2 += self::mul($s12, 654183, 20); $s3 -= self::mul($s12, 997805, 20); $s4 += self::mul($s12, 136657, 18); $s5 -= self::mul($s12, 683901, 20); $s12 = 0; // carry0 = s0 >> 21; // s1 += carry0; // s0 -= carry0 * ((uint64_t) 1L << 21); $carry0 = $s0 >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; // carry1 = s1 >> 21; // s2 += carry1; // s1 -= carry1 * ((uint64_t) 1L << 21); $carry1 = $s1 >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; // carry2 = s2 >> 21; // s3 += carry2; // s2 -= carry2 * ((uint64_t) 1L << 21); $carry2 = $s2 >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; // carry3 = s3 >> 21; // s4 += carry3; // s3 -= carry3 * ((uint64_t) 1L << 21); $carry3 = $s3 >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; // carry4 = s4 >> 21; // s5 += carry4; // s4 -= carry4 * ((uint64_t) 1L << 21); $carry4 = $s4 >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; // carry5 = s5 >> 21; // s6 += carry5; // s5 -= carry5 * ((uint64_t) 1L << 21); $carry5 = $s5 >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; // carry6 = s6 >> 21; // s7 += carry6; // s6 -= carry6 * ((uint64_t) 1L << 21); $carry6 = $s6 >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; // carry7 = s7 >> 21; // s8 += carry7; // s7 -= carry7 * ((uint64_t) 1L << 21); $carry7 = $s7 >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; // carry8 = s8 >> 21; // s9 += carry8; // s8 -= carry8 * ((uint64_t) 1L << 21); $carry8 = $s8 >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; // carry9 = s9 >> 21; // s10 += carry9; // s9 -= carry9 * ((uint64_t) 1L << 21); $carry9 = $s9 >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; // carry10 = s10 >> 21; // s11 += carry10; // s10 -= carry10 * ((uint64_t) 1L << 21); $carry10 = $s10 >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; // carry11 = s11 >> 21; // s12 += carry11; // s11 -= carry11 * ((uint64_t) 1L << 21); $carry11 = $s11 >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; // s0 += s12 * 666643; // s1 += s12 * 470296; // s2 += s12 * 654183; // s3 -= s12 * 997805; // s4 += s12 * 136657; // s5 -= s12 * 683901; $s0 += self::mul($s12, 666643, 20); $s1 += self::mul($s12, 470296, 19); $s2 += self::mul($s12, 654183, 20); $s3 -= self::mul($s12, 997805, 20); $s4 += self::mul($s12, 136657, 18); $s5 -= self::mul($s12, 683901, 20); // carry0 = s0 >> 21; // s1 += carry0; // s0 -= carry0 * ((uint64_t) 1L << 21); $carry0 = $s0 >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; // carry1 = s1 >> 21; // s2 += carry1; // s1 -= carry1 * ((uint64_t) 1L << 21); $carry1 = $s1 >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; // carry2 = s2 >> 21; // s3 += carry2; // s2 -= carry2 * ((uint64_t) 1L << 21); $carry2 = $s2 >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; // carry3 = s3 >> 21; // s4 += carry3; // s3 -= carry3 * ((uint64_t) 1L << 21); $carry3 = $s3 >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; // carry4 = s4 >> 21; // s5 += carry4; // s4 -= carry4 * ((uint64_t) 1L << 21); $carry4 = $s4 >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; // carry5 = s5 >> 21; // s6 += carry5; // s5 -= carry5 * ((uint64_t) 1L << 21); $carry5 = $s5 >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; // carry6 = s6 >> 21; // s7 += carry6; // s6 -= carry6 * ((uint64_t) 1L << 21); $carry6 = $s6 >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; // carry7 = s7 >> 21; // s8 += carry7; // s7 -= carry7 * ((uint64_t) 1L << 21); $carry7 = $s7 >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; // carry8 = s8 >> 21; // s9 += carry8; // s8 -= carry8 * ((uint64_t) 1L << 21); $carry8 = $s8 >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; // carry9 = s9 >> 21; // s10 += carry9; // s9 -= carry9 * ((uint64_t) 1L << 21); $carry9 = $s9 >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; // carry10 = s10 >> 21; // s11 += carry10; // s10 -= carry10 * ((uint64_t) 1L << 21); $carry10 = $s10 >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; $s = array_fill(0, 32, 0); // s[0] = s0 >> 0; $s[0] = $s0 >> 0; // s[1] = s0 >> 8; $s[1] = $s0 >> 8; // s[2] = (s0 >> 16) | (s1 * ((uint64_t) 1 << 5)); $s[2] = ($s0 >> 16) | ($s1 << 5); // s[3] = s1 >> 3; $s[3] = $s1 >> 3; // s[4] = s1 >> 11; $s[4] = $s1 >> 11; // s[5] = (s1 >> 19) | (s2 * ((uint64_t) 1 << 2)); $s[5] = ($s1 >> 19) | ($s2 << 2); // s[6] = s2 >> 6; $s[6] = $s2 >> 6; // s[7] = (s2 >> 14) | (s3 * ((uint64_t) 1 << 7)); $s[7] = ($s2 >> 14) | ($s3 << 7); // s[8] = s3 >> 1; $s[8] = $s3 >> 1; // s[9] = s3 >> 9; $s[9] = $s3 >> 9; // s[10] = (s3 >> 17) | (s4 * ((uint64_t) 1 << 4)); $s[10] = ($s3 >> 17) | ($s4 << 4); // s[11] = s4 >> 4; $s[11] = $s4 >> 4; // s[12] = s4 >> 12; $s[12] = $s4 >> 12; // s[13] = (s4 >> 20) | (s5 * ((uint64_t) 1 << 1)); $s[13] = ($s4 >> 20) | ($s5 << 1); // s[14] = s5 >> 7; $s[14] = $s5 >> 7; // s[15] = (s5 >> 15) | (s6 * ((uint64_t) 1 << 6)); $s[15] = ($s5 >> 15) | ($s6 << 6); // s[16] = s6 >> 2; $s[16] = $s6 >> 2; // s[17] = s6 >> 10; $s[17] = $s6 >> 10; // s[18] = (s6 >> 18) | (s7 * ((uint64_t) 1 << 3)); $s[18] = ($s6 >> 18) | ($s7 << 3); // s[19] = s7 >> 5; $s[19] = $s7 >> 5; // s[20] = s7 >> 13; $s[20] = $s7 >> 13; // s[21] = s8 >> 0; $s[21] = $s8 >> 0; // s[22] = s8 >> 8; $s[22] = $s8 >> 8; // s[23] = (s8 >> 16) | (s9 * ((uint64_t) 1 << 5)); $s[23] = ($s8 >> 16) | ($s9 << 5); // s[24] = s9 >> 3; $s[24] = $s9 >> 3; // s[25] = s9 >> 11; $s[25] = $s9 >> 11; // s[26] = (s9 >> 19) | (s10 * ((uint64_t) 1 << 2)); $s[26] = ($s9 >> 19) | ($s10 << 2); // s[27] = s10 >> 6; $s[27] = $s10 >> 6; // s[28] = (s10 >> 14) | (s11 * ((uint64_t) 1 << 7)); $s[28] = ($s10 >> 14) | ($s11 << 7); // s[29] = s11 >> 1; $s[29] = $s11 >> 1; // s[30] = s11 >> 9; $s[30] = $s11 >> 9; // s[31] = s11 >> 17; $s[31] = $s11 >> 17; return self::intArrayToString($s); } /** * @param string $s * @return string */ public static function sc25519_sq($s) { return self::sc25519_mul($s, $s); } /** * @param string $s * @param int $n * @param string $a * @return string */ public static function sc25519_sqmul($s, $n, $a) { for ($i = 0; $i < $n; ++$i) { $s = self::sc25519_sq($s); } return self::sc25519_mul($s, $a); } /** * @param string $s * @return string */ public static function sc25519_invert($s) { $_10 = self::sc25519_sq($s); $_11 = self::sc25519_mul($s, $_10); $_100 = self::sc25519_mul($s, $_11); $_1000 = self::sc25519_sq($_100); $_1010 = self::sc25519_mul($_10, $_1000); $_1011 = self::sc25519_mul($s, $_1010); $_10000 = self::sc25519_sq($_1000); $_10110 = self::sc25519_sq($_1011); $_100000 = self::sc25519_mul($_1010, $_10110); $_100110 = self::sc25519_mul($_10000, $_10110); $_1000000 = self::sc25519_sq($_100000); $_1010000 = self::sc25519_mul($_10000, $_1000000); $_1010011 = self::sc25519_mul($_11, $_1010000); $_1100011 = self::sc25519_mul($_10000, $_1010011); $_1100111 = self::sc25519_mul($_100, $_1100011); $_1101011 = self::sc25519_mul($_100, $_1100111); $_10010011 = self::sc25519_mul($_1000000, $_1010011); $_10010111 = self::sc25519_mul($_100, $_10010011); $_10111101 = self::sc25519_mul($_100110, $_10010111); $_11010011 = self::sc25519_mul($_10110, $_10111101); $_11100111 = self::sc25519_mul($_1010000, $_10010111); $_11101011 = self::sc25519_mul($_100, $_11100111); $_11110101 = self::sc25519_mul($_1010, $_11101011); $recip = self::sc25519_mul($_1011, $_11110101); $recip = self::sc25519_sqmul($recip, 126, $_1010011); $recip = self::sc25519_sqmul($recip, 9, $_10); $recip = self::sc25519_mul($recip, $_11110101); $recip = self::sc25519_sqmul($recip, 7, $_1100111); $recip = self::sc25519_sqmul($recip, 9, $_11110101); $recip = self::sc25519_sqmul($recip, 11, $_10111101); $recip = self::sc25519_sqmul($recip, 8, $_11100111); $recip = self::sc25519_sqmul($recip, 9, $_1101011); $recip = self::sc25519_sqmul($recip, 6, $_1011); $recip = self::sc25519_sqmul($recip, 14, $_10010011); $recip = self::sc25519_sqmul($recip, 10, $_1100011); $recip = self::sc25519_sqmul($recip, 9, $_10010111); $recip = self::sc25519_sqmul($recip, 10, $_11110101); $recip = self::sc25519_sqmul($recip, 8, $_11010011); return self::sc25519_sqmul($recip, 8, $_11101011); } /** * @param string $s * @return string */ public static function clamp($s) { $s_ = self::stringToIntArray($s); $s_[0] &= 248; $s_[31] |= 64; $s_[31] &= 127; return self::intArrayToString($s_); } /** * Ensure limbs are less than 28 bits long to prevent float promotion. * * This uses a constant-time conditional swap under the hood. * * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @return ParagonIE_Sodium_Core_Curve25519_Fe */ public static function fe_normalize(ParagonIE_Sodium_Core_Curve25519_Fe $f) { $x = (PHP_INT_SIZE << 3) - 1; // 31 or 63 $g = self::fe_copy($f); $e = array( $g->e0, $g->e1, $g->e2, $g->e3, $g->e4, $g->e5, $g->e6, $g->e7, $g->e8, $g->e9 ); for ($i = 0; $i < 10; ++$i) { $mask = -(($e[$i] >> $x) & 1); /* * Get two candidate normalized values for $e[$i], depending on the sign of $e[$i]: */ $a = $e[$i] & 0x7ffffff; $b = -((-$e[$i]) & 0x7ffffff); /* * Return the appropriate candidate value, based on the sign of the original input: * * The following is equivalent to this ternary: * * $e[$i] = (($e[$i] >> $x) & 1) ? $a : $b; * * Except what's written doesn't contain timing leaks. */ $e[$i] = ($a ^ (($a ^ $b) & $mask)); } $g->e0 = $e[0]; $g->e1 = $e[1]; $g->e2 = $e[2]; $g->e3 = $e[3]; $g->e4 = $e[4]; $g->e5 = $e[5]; $g->e6 = $e[6]; $g->e7 = $e[7]; $g->e8 = $e[8]; $g->e9 = $e[9]; return $g; } } Core/Ed25519.php 0000644 00000042602 15162213702 0007106 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_Ed25519', false)) { return; } if (!class_exists('ParagonIE_Sodium_Core_Curve25519', false)) { require_once dirname(__FILE__) . '/Curve25519.php'; } /** * Class ParagonIE_Sodium_Core_Ed25519 */ abstract class ParagonIE_Sodium_Core_Ed25519 extends ParagonIE_Sodium_Core_Curve25519 { const KEYPAIR_BYTES = 96; const SEED_BYTES = 32; const SCALAR_BYTES = 32; /** * @internal You should not use this directly from another application * * @return string (96 bytes) * @throws Exception * @throws SodiumException * @throws TypeError */ public static function keypair() { $seed = random_bytes(self::SEED_BYTES); $pk = ''; $sk = ''; self::seed_keypair($pk, $sk, $seed); return $sk . $pk; } /** * @internal You should not use this directly from another application * * @param string $pk * @param string $sk * @param string $seed * @return string * @throws SodiumException * @throws TypeError */ public static function seed_keypair(&$pk, &$sk, $seed) { if (self::strlen($seed) !== self::SEED_BYTES) { throw new SodiumException('crypto_sign keypair seed must be 32 bytes long'); } /** @var string $pk */ $pk = self::publickey_from_secretkey($seed); $sk = $seed . $pk; return $sk; } /** * @internal You should not use this directly from another application * * @param string $keypair * @return string * @throws TypeError */ public static function secretkey($keypair) { if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { throw new SodiumException('crypto_sign keypair must be 96 bytes long'); } return self::substr($keypair, 0, 64); } /** * @internal You should not use this directly from another application * * @param string $keypair * @return string * @throws TypeError */ public static function publickey($keypair) { if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { throw new SodiumException('crypto_sign keypair must be 96 bytes long'); } return self::substr($keypair, 64, 32); } /** * @internal You should not use this directly from another application * * @param string $sk * @return string * @throws SodiumException * @throws TypeError */ public static function publickey_from_secretkey($sk) { /** @var string $sk */ $sk = hash('sha512', self::substr($sk, 0, 32), true); $sk[0] = self::intToChr( self::chrToInt($sk[0]) & 248 ); $sk[31] = self::intToChr( (self::chrToInt($sk[31]) & 63) | 64 ); return self::sk_to_pk($sk); } /** * @param string $pk * @return string * @throws SodiumException * @throws TypeError */ public static function pk_to_curve25519($pk) { if (self::small_order($pk)) { throw new SodiumException('Public key is on a small order'); } $A = self::ge_frombytes_negate_vartime(self::substr($pk, 0, 32)); $p1 = self::ge_mul_l($A); if (!self::fe_isnonzero($p1->X)) { throw new SodiumException('Unexpected zero result'); } # fe_1(one_minus_y); # fe_sub(one_minus_y, one_minus_y, A.Y); # fe_invert(one_minus_y, one_minus_y); $one_minux_y = self::fe_invert( self::fe_sub( self::fe_1(), $A->Y ) ); # fe_1(x); # fe_add(x, x, A.Y); # fe_mul(x, x, one_minus_y); $x = self::fe_mul( self::fe_add(self::fe_1(), $A->Y), $one_minux_y ); # fe_tobytes(curve25519_pk, x); return self::fe_tobytes($x); } /** * @internal You should not use this directly from another application * * @param string $sk * @return string * @throws SodiumException * @throws TypeError */ public static function sk_to_pk($sk) { return self::ge_p3_tobytes( self::ge_scalarmult_base( self::substr($sk, 0, 32) ) ); } /** * @internal You should not use this directly from another application * * @param string $message * @param string $sk * @return string * @throws SodiumException * @throws TypeError */ public static function sign($message, $sk) { /** @var string $signature */ $signature = self::sign_detached($message, $sk); return $signature . $message; } /** * @internal You should not use this directly from another application * * @param string $message A signed message * @param string $pk Public key * @return string Message (without signature) * @throws SodiumException * @throws TypeError */ public static function sign_open($message, $pk) { /** @var string $signature */ $signature = self::substr($message, 0, 64); /** @var string $message */ $message = self::substr($message, 64); if (self::verify_detached($signature, $message, $pk)) { return $message; } throw new SodiumException('Invalid signature'); } /** * @internal You should not use this directly from another application * * @param string $message * @param string $sk * @return string * @throws SodiumException * @throws TypeError */ public static function sign_detached($message, $sk) { if (self::strlen($sk) !== 64) { throw new SodiumException('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long'); } # crypto_hash_sha512(az, sk, 32); $az = hash('sha512', self::substr($sk, 0, 32), true); # az[0] &= 248; # az[31] &= 63; # az[31] |= 64; $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); # crypto_hash_sha512_init(&hs); # crypto_hash_sha512_update(&hs, az + 32, 32); # crypto_hash_sha512_update(&hs, m, mlen); # crypto_hash_sha512_final(&hs, nonce); $hs = hash_init('sha512'); hash_update($hs, self::substr($az, 32, 32)); hash_update($hs, $message); $nonceHash = hash_final($hs, true); # memmove(sig + 32, sk + 32, 32); $pk = self::substr($sk, 32, 32); # sc_reduce(nonce); # ge_scalarmult_base(&R, nonce); # ge_p3_tobytes(sig, &R); $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32); $sig = self::ge_p3_tobytes( self::ge_scalarmult_base($nonce) ); # crypto_hash_sha512_init(&hs); # crypto_hash_sha512_update(&hs, sig, 64); # crypto_hash_sha512_update(&hs, m, mlen); # crypto_hash_sha512_final(&hs, hram); $hs = hash_init('sha512'); hash_update($hs, self::substr($sig, 0, 32)); hash_update($hs, self::substr($pk, 0, 32)); hash_update($hs, $message); $hramHash = hash_final($hs, true); # sc_reduce(hram); # sc_muladd(sig + 32, hram, az, nonce); $hram = self::sc_reduce($hramHash); $sigAfter = self::sc_muladd($hram, $az, $nonce); $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); try { ParagonIE_Sodium_Compat::memzero($az); } catch (SodiumException $ex) { $az = null; } return $sig; } /** * @internal You should not use this directly from another application * * @param string $sig * @param string $message * @param string $pk * @return bool * @throws SodiumException * @throws TypeError */ public static function verify_detached($sig, $message, $pk) { if (self::strlen($sig) !== 64) { throw new SodiumException('Argument 1 must be CRYPTO_SIGN_BYTES long'); } if (self::strlen($pk) !== 32) { throw new SodiumException('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES long'); } if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) { throw new SodiumException('S < L - Invalid signature'); } if (self::small_order($sig)) { throw new SodiumException('Signature is on too small of an order'); } if ((self::chrToInt($sig[63]) & 224) !== 0) { throw new SodiumException('Invalid signature'); } $d = 0; for ($i = 0; $i < 32; ++$i) { $d |= self::chrToInt($pk[$i]); } if ($d === 0) { throw new SodiumException('All zero public key'); } /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ $orig = ParagonIE_Sodium_Compat::$fastMult; // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. ParagonIE_Sodium_Compat::$fastMult = true; /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */ $A = self::ge_frombytes_negate_vartime($pk); /** @var string $hDigest */ $hDigest = hash( 'sha512', self::substr($sig, 0, 32) . self::substr($pk, 0, 32) . $message, true ); /** @var string $h */ $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32); /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */ $R = self::ge_double_scalarmult_vartime( $h, $A, self::substr($sig, 32) ); /** @var string $rcheck */ $rcheck = self::ge_tobytes($R); // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. ParagonIE_Sodium_Compat::$fastMult = $orig; return self::verify_32($rcheck, self::substr($sig, 0, 32)); } /** * @internal You should not use this directly from another application * * @param string $S * @return bool * @throws SodiumException * @throws TypeError */ public static function check_S_lt_L($S) { if (self::strlen($S) < 32) { throw new SodiumException('Signature must be 32 bytes'); } $L = array( 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 ); $c = 0; $n = 1; $i = 32; /** @var array<int, int> $L */ do { --$i; $x = self::chrToInt($S[$i]); $c |= ( (($x - $L[$i]) >> 8) & $n ); $n &= ( (($x ^ $L[$i]) - 1) >> 8 ); } while ($i !== 0); return $c === 0; } /** * @param string $R * @return bool * @throws SodiumException * @throws TypeError */ public static function small_order($R) { /** @var array<int, array<int, int>> $blocklist */ $blocklist = array( /* 0 (order 4) */ array( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ), /* 1 (order 1) */ array( 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ), /* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ array( 0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05 ), /* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ array( 0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a ), /* p-1 (order 2) */ array( 0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85 ), /* p (order 4) */ array( 0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa ), /* p+1 (order 1) */ array( 0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f ), /* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ array( 0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f ), /* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ array( 0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f ), /* 2p-1 (order 2) */ array( 0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ), /* 2p (order 4) */ array( 0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ), /* 2p+1 (order 1) */ array( 0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ) ); /** @var int $countBlocklist */ $countBlocklist = count($blocklist); for ($i = 0; $i < $countBlocklist; ++$i) { $c = 0; for ($j = 0; $j < 32; ++$j) { $c |= self::chrToInt($R[$j]) ^ (int) $blocklist[$i][$j]; } if ($c === 0) { return true; } } return false; } /** * @param string $s * @return string * @throws SodiumException */ public static function scalar_complement($s) { $t_ = self::L . str_repeat("\x00", 32); sodium_increment($t_); $s_ = $s . str_repeat("\x00", 32); ParagonIE_Sodium_Compat::sub($t_, $s_); return self::sc_reduce($t_); } /** * @return string * @throws SodiumException */ public static function scalar_random() { do { $r = ParagonIE_Sodium_Compat::randombytes_buf(self::SCALAR_BYTES); $r[self::SCALAR_BYTES - 1] = self::intToChr( self::chrToInt($r[self::SCALAR_BYTES - 1]) & 0x1f ); } while ( !self::check_S_lt_L($r) || ParagonIE_Sodium_Compat::is_zero($r) ); return $r; } /** * @param string $s * @return string * @throws SodiumException */ public static function scalar_negate($s) { $t_ = self::L . str_repeat("\x00", 32) ; $s_ = $s . str_repeat("\x00", 32) ; ParagonIE_Sodium_Compat::sub($t_, $s_); return self::sc_reduce($t_); } /** * @param string $a * @param string $b * @return string * @throws SodiumException */ public static function scalar_add($a, $b) { $a_ = $a . str_repeat("\x00", 32); $b_ = $b . str_repeat("\x00", 32); ParagonIE_Sodium_Compat::add($a_, $b_); return self::sc_reduce($a_); } /** * @param string $x * @param string $y * @return string * @throws SodiumException */ public static function scalar_sub($x, $y) { $yn = self::scalar_negate($y); return self::scalar_add($x, $yn); } } Core/SecretStream/State.php 0000644 00000007050 15162213703 0011630 0 ustar 00 <?php /** * Class ParagonIE_Sodium_Core_SecretStream_State */ class ParagonIE_Sodium_Core_SecretStream_State { /** @var string $key */ protected $key; /** @var int $counter */ protected $counter; /** @var string $nonce */ protected $nonce; /** @var string $_pad */ protected $_pad; /** * ParagonIE_Sodium_Core_SecretStream_State constructor. * @param string $key * @param string|null $nonce */ public function __construct($key, $nonce = null) { $this->key = $key; $this->counter = 1; if (is_null($nonce)) { $nonce = str_repeat("\0", 12); } $this->nonce = str_pad($nonce, 12, "\0", STR_PAD_RIGHT);; $this->_pad = str_repeat("\0", 4); } /** * @return self */ public function counterReset() { $this->counter = 1; $this->_pad = str_repeat("\0", 4); return $this; } /** * @return string */ public function getKey() { return $this->key; } /** * @return string */ public function getCounter() { return ParagonIE_Sodium_Core_Util::store32_le($this->counter); } /** * @return string */ public function getNonce() { if (!is_string($this->nonce)) { $this->nonce = str_repeat("\0", 12); } if (ParagonIE_Sodium_Core_Util::strlen($this->nonce) !== 12) { $this->nonce = str_pad($this->nonce, 12, "\0", STR_PAD_RIGHT); } return $this->nonce; } /** * @return string */ public function getCombinedNonce() { return $this->getCounter() . ParagonIE_Sodium_Core_Util::substr($this->getNonce(), 0, 8); } /** * @return self */ public function incrementCounter() { ++$this->counter; return $this; } /** * @return bool */ public function needsRekey() { return ($this->counter & 0xffff) === 0; } /** * @param string $newKeyAndNonce * @return self */ public function rekey($newKeyAndNonce) { $this->key = ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 0, 32); $this->nonce = str_pad( ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 32), 12, "\0", STR_PAD_RIGHT ); return $this; } /** * @param string $str * @return self */ public function xorNonce($str) { $this->nonce = ParagonIE_Sodium_Core_Util::xorStrings( $this->getNonce(), str_pad( ParagonIE_Sodium_Core_Util::substr($str, 0, 8), 12, "\0", STR_PAD_RIGHT ) ); return $this; } /** * @param string $string * @return self */ public static function fromString($string) { $state = new ParagonIE_Sodium_Core_SecretStream_State( ParagonIE_Sodium_Core_Util::substr($string, 0, 32) ); $state->counter = ParagonIE_Sodium_Core_Util::load_4( ParagonIE_Sodium_Core_Util::substr($string, 32, 4) ); $state->nonce = ParagonIE_Sodium_Core_Util::substr($string, 36, 12); $state->_pad = ParagonIE_Sodium_Core_Util::substr($string, 48, 8); return $state; } /** * @return string */ public function toString() { return $this->key . $this->getCounter() . $this->nonce . $this->_pad; } } Core/X25519.php 0000644 00000020335 15162213703 0006765 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_X25519', false)) { return; } /** * Class ParagonIE_Sodium_Core_X25519 */ abstract class ParagonIE_Sodium_Core_X25519 extends ParagonIE_Sodium_Core_Curve25519 { /** * Alters the objects passed to this method in place. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @param ParagonIE_Sodium_Core_Curve25519_Fe $g * @param int $b * @return void * @psalm-suppress MixedAssignment */ public static function fe_cswap( ParagonIE_Sodium_Core_Curve25519_Fe $f, ParagonIE_Sodium_Core_Curve25519_Fe $g, $b = 0 ) { $b = -$b; $x0 = ($f->e0 ^ $g->e0) & $b; $x1 = ($f->e1 ^ $g->e1) & $b; $x2 = ($f->e2 ^ $g->e2) & $b; $x3 = ($f->e3 ^ $g->e3) & $b; $x4 = ($f->e4 ^ $g->e4) & $b; $x5 = ($f->e5 ^ $g->e5) & $b; $x6 = ($f->e6 ^ $g->e6) & $b; $x7 = ($f->e7 ^ $g->e7) & $b; $x8 = ($f->e8 ^ $g->e8) & $b; $x9 = ($f->e9 ^ $g->e9) & $b; $f->e0 ^= $x0; $f->e1 ^= $x1; $f->e2 ^= $x2; $f->e3 ^= $x3; $f->e4 ^= $x4; $f->e5 ^= $x5; $f->e6 ^= $x6; $f->e7 ^= $x7; $f->e8 ^= $x8; $f->e9 ^= $x9; $g->e0 ^= $x0; $g->e1 ^= $x1; $g->e2 ^= $x2; $g->e3 ^= $x3; $g->e4 ^= $x4; $g->e5 ^= $x5; $g->e6 ^= $x6; $g->e7 ^= $x7; $g->e8 ^= $x8; $g->e9 ^= $x9; } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @return ParagonIE_Sodium_Core_Curve25519_Fe */ public static function fe_mul121666(ParagonIE_Sodium_Core_Curve25519_Fe $f) { $h0 = self::mul($f->e0, 121666, 17); $h1 = self::mul($f->e1, 121666, 17); $h2 = self::mul($f->e2, 121666, 17); $h3 = self::mul($f->e3, 121666, 17); $h4 = self::mul($f->e4, 121666, 17); $h5 = self::mul($f->e5, 121666, 17); $h6 = self::mul($f->e6, 121666, 17); $h7 = self::mul($f->e7, 121666, 17); $h8 = self::mul($f->e8, 121666, 17); $h9 = self::mul($f->e9, 121666, 17); $carry9 = ($h9 + (1 << 24)) >> 25; $h0 += self::mul($carry9, 19, 5); $h9 -= $carry9 << 25; $carry1 = ($h1 + (1 << 24)) >> 25; $h2 += $carry1; $h1 -= $carry1 << 25; $carry3 = ($h3 + (1 << 24)) >> 25; $h4 += $carry3; $h3 -= $carry3 << 25; $carry5 = ($h5 + (1 << 24)) >> 25; $h6 += $carry5; $h5 -= $carry5 << 25; $carry7 = ($h7 + (1 << 24)) >> 25; $h8 += $carry7; $h7 -= $carry7 << 25; $carry0 = ($h0 + (1 << 25)) >> 26; $h1 += $carry0; $h0 -= $carry0 << 26; $carry2 = ($h2 + (1 << 25)) >> 26; $h3 += $carry2; $h2 -= $carry2 << 26; $carry4 = ($h4 + (1 << 25)) >> 26; $h5 += $carry4; $h4 -= $carry4 << 26; $carry6 = ($h6 + (1 << 25)) >> 26; $h7 += $carry6; $h6 -= $carry6 << 26; $carry8 = ($h8 + (1 << 25)) >> 26; $h9 += $carry8; $h8 -= $carry8 << 26; return new ParagonIE_Sodium_Core_Curve25519_Fe($h0, $h1, $h2, $h3, $h4, $h5, $h6, $h7, $h8, $h9); } /** * @internal You should not use this directly from another application * * Inline comments preceded by # are from libsodium's ref10 code. * * @param string $n * @param string $p * @return string * @throws SodiumException * @throws TypeError */ public static function crypto_scalarmult_curve25519_ref10($n, $p) { # for (i = 0;i < 32;++i) e[i] = n[i]; $e = '' . $n; # e[0] &= 248; $e[0] = self::intToChr( self::chrToInt($e[0]) & 248 ); # e[31] &= 127; # e[31] |= 64; $e[31] = self::intToChr( (self::chrToInt($e[31]) & 127) | 64 ); # fe_frombytes(x1,p); $x1 = self::fe_frombytes($p); # fe_1(x2); $x2 = self::fe_1(); # fe_0(z2); $z2 = self::fe_0(); # fe_copy(x3,x1); $x3 = self::fe_copy($x1); # fe_1(z3); $z3 = self::fe_1(); # swap = 0; /** @var int $swap */ $swap = 0; # for (pos = 254;pos >= 0;--pos) { for ($pos = 254; $pos >= 0; --$pos) { # b = e[pos / 8] >> (pos & 7); /** @var int $b */ $b = self::chrToInt( $e[(int) floor($pos / 8)] ) >> ($pos & 7); # b &= 1; $b &= 1; # swap ^= b; $swap ^= $b; # fe_cswap(x2,x3,swap); self::fe_cswap($x2, $x3, $swap); # fe_cswap(z2,z3,swap); self::fe_cswap($z2, $z3, $swap); # swap = b; $swap = $b; # fe_sub(tmp0,x3,z3); $tmp0 = self::fe_sub($x3, $z3); # fe_sub(tmp1,x2,z2); $tmp1 = self::fe_sub($x2, $z2); # fe_add(x2,x2,z2); $x2 = self::fe_add($x2, $z2); # fe_add(z2,x3,z3); $z2 = self::fe_add($x3, $z3); # fe_mul(z3,tmp0,x2); $z3 = self::fe_mul($tmp0, $x2); # fe_mul(z2,z2,tmp1); $z2 = self::fe_mul($z2, $tmp1); # fe_sq(tmp0,tmp1); $tmp0 = self::fe_sq($tmp1); # fe_sq(tmp1,x2); $tmp1 = self::fe_sq($x2); # fe_add(x3,z3,z2); $x3 = self::fe_add($z3, $z2); # fe_sub(z2,z3,z2); $z2 = self::fe_sub($z3, $z2); # fe_mul(x2,tmp1,tmp0); $x2 = self::fe_mul($tmp1, $tmp0); # fe_sub(tmp1,tmp1,tmp0); $tmp1 = self::fe_sub($tmp1, $tmp0); # fe_sq(z2,z2); $z2 = self::fe_sq($z2); # fe_mul121666(z3,tmp1); $z3 = self::fe_mul121666($tmp1); # fe_sq(x3,x3); $x3 = self::fe_sq($x3); # fe_add(tmp0,tmp0,z3); $tmp0 = self::fe_add($tmp0, $z3); # fe_mul(z3,x1,z2); $z3 = self::fe_mul($x1, $z2); # fe_mul(z2,tmp1,tmp0); $z2 = self::fe_mul($tmp1, $tmp0); } # fe_cswap(x2,x3,swap); self::fe_cswap($x2, $x3, $swap); # fe_cswap(z2,z3,swap); self::fe_cswap($z2, $z3, $swap); # fe_invert(z2,z2); $z2 = self::fe_invert($z2); # fe_mul(x2,x2,z2); $x2 = self::fe_mul($x2, $z2); # fe_tobytes(q,x2); return self::fe_tobytes($x2); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $edwardsY * @param ParagonIE_Sodium_Core_Curve25519_Fe $edwardsZ * @return ParagonIE_Sodium_Core_Curve25519_Fe */ public static function edwards_to_montgomery( ParagonIE_Sodium_Core_Curve25519_Fe $edwardsY, ParagonIE_Sodium_Core_Curve25519_Fe $edwardsZ ) { $tempX = self::fe_add($edwardsZ, $edwardsY); $tempZ = self::fe_sub($edwardsZ, $edwardsY); $tempZ = self::fe_invert($tempZ); return self::fe_mul($tempX, $tempZ); } /** * @internal You should not use this directly from another application * * @param string $n * @return string * @throws SodiumException * @throws TypeError */ public static function crypto_scalarmult_curve25519_ref10_base($n) { # for (i = 0;i < 32;++i) e[i] = n[i]; $e = '' . $n; # e[0] &= 248; $e[0] = self::intToChr( self::chrToInt($e[0]) & 248 ); # e[31] &= 127; # e[31] |= 64; $e[31] = self::intToChr( (self::chrToInt($e[31]) & 127) | 64 ); $A = self::ge_scalarmult_base($e); if ( !($A->Y instanceof ParagonIE_Sodium_Core_Curve25519_Fe) || !($A->Z instanceof ParagonIE_Sodium_Core_Curve25519_Fe) ) { throw new TypeError('Null points encountered'); } $pk = self::edwards_to_montgomery($A->Y, $A->Z); return self::fe_tobytes($pk); } } Core/Util.php 0000644 00000070350 15162213704 0007070 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_Util', false)) { return; } /** * Class ParagonIE_Sodium_Core_Util */ abstract class ParagonIE_Sodium_Core_Util { const U32_MAX = 0xFFFFFFFF; /** * @param int $integer * @param int $size (16, 32, 64) * @return int */ public static function abs($integer, $size = 0) { /** @var int $realSize */ $realSize = (PHP_INT_SIZE << 3) - 1; if ($size) { --$size; } else { /** @var int $size */ $size = $realSize; } $negative = -(($integer >> $size) & 1); return (int) ( ($integer ^ $negative) + (($negative >> $realSize) & 1) ); } /** * @param string $a * @param string $b * @return string * @throws SodiumException */ public static function andStrings($a, $b) { /* Type checks: */ if (!is_string($a)) { throw new TypeError('Argument 1 must be a string'); } if (!is_string($b)) { throw new TypeError('Argument 2 must be a string'); } $len = self::strlen($a); if (self::strlen($b) !== $len) { throw new SodiumException('Both strings must be of equal length to combine with bitwise AND'); } return $a & $b; } /** * Convert a binary string into a hexadecimal string without cache-timing * leaks * * @internal You should not use this directly from another application * * @param string $binaryString (raw binary) * @return string * @throws TypeError */ public static function bin2hex($binaryString) { /* Type checks: */ if (!is_string($binaryString)) { throw new TypeError('Argument 1 must be a string, ' . gettype($binaryString) . ' given.'); } $hex = ''; $len = self::strlen($binaryString); for ($i = 0; $i < $len; ++$i) { /** @var array<int, int> $chunk */ $chunk = unpack('C', $binaryString[$i]); /** @var int $c */ $c = $chunk[1] & 0xf; /** @var int $b */ $b = $chunk[1] >> 4; $hex .= pack( 'CC', (87 + $b + ((($b - 10) >> 8) & ~38)), (87 + $c + ((($c - 10) >> 8) & ~38)) ); } return $hex; } /** * Convert a binary string into a hexadecimal string without cache-timing * leaks, returning uppercase letters (as per RFC 4648) * * @internal You should not use this directly from another application * * @param string $bin_string (raw binary) * @return string * @throws TypeError */ public static function bin2hexUpper($bin_string) { $hex = ''; $len = self::strlen($bin_string); for ($i = 0; $i < $len; ++$i) { /** @var array<int, int> $chunk */ $chunk = unpack('C', $bin_string[$i]); /** * Lower 16 bits * * @var int $c */ $c = $chunk[1] & 0xf; /** * Upper 16 bits * @var int $b */ $b = $chunk[1] >> 4; /** * Use pack() and binary operators to turn the two integers * into hexadecimal characters. We don't use chr() here, because * it uses a lookup table internally and we want to avoid * cache-timing side-channels. */ $hex .= pack( 'CC', (55 + $b + ((($b - 10) >> 8) & ~6)), (55 + $c + ((($c - 10) >> 8) & ~6)) ); } return $hex; } /** * Cache-timing-safe variant of ord() * * @internal You should not use this directly from another application * * @param string $chr * @return int * @throws SodiumException * @throws TypeError */ public static function chrToInt($chr) { /* Type checks: */ if (!is_string($chr)) { throw new TypeError('Argument 1 must be a string, ' . gettype($chr) . ' given.'); } if (self::strlen($chr) !== 1) { throw new SodiumException('chrToInt() expects a string that is exactly 1 character long'); } /** @var array<int, int> $chunk */ $chunk = unpack('C', $chr); return (int) ($chunk[1]); } /** * Compares two strings. * * @internal You should not use this directly from another application * * @param string $left * @param string $right * @param int $len * @return int * @throws SodiumException * @throws TypeError */ public static function compare($left, $right, $len = null) { $leftLen = self::strlen($left); $rightLen = self::strlen($right); if ($len === null) { $len = max($leftLen, $rightLen); $left = str_pad($left, $len, "\x00", STR_PAD_RIGHT); $right = str_pad($right, $len, "\x00", STR_PAD_RIGHT); } elseif ($leftLen !== $rightLen) { throw new SodiumException("Argument #1 and argument #2 must have the same length"); } $gt = 0; $eq = 1; $i = $len; while ($i !== 0) { --$i; $gt |= ((self::chrToInt($right[$i]) - self::chrToInt($left[$i])) >> 8) & $eq; $eq &= ((self::chrToInt($right[$i]) ^ self::chrToInt($left[$i])) - 1) >> 8; } return ($gt + $gt + $eq) - 1; } /** * If a variable does not match a given type, throw a TypeError. * * @param mixed $mixedVar * @param string $type * @param int $argumentIndex * @throws TypeError * @throws SodiumException * @return void */ public static function declareScalarType(&$mixedVar = null, $type = 'void', $argumentIndex = 0) { if (func_num_args() === 0) { /* Tautology, by default */ return; } if (func_num_args() === 1) { throw new TypeError('Declared void, but passed a variable'); } $realType = strtolower(gettype($mixedVar)); $type = strtolower($type); switch ($type) { case 'null': if ($mixedVar !== null) { throw new TypeError('Argument ' . $argumentIndex . ' must be null, ' . $realType . ' given.'); } break; case 'integer': case 'int': $allow = array('int', 'integer'); if (!in_array($type, $allow)) { throw new TypeError('Argument ' . $argumentIndex . ' must be an integer, ' . $realType . ' given.'); } $mixedVar = (int) $mixedVar; break; case 'boolean': case 'bool': $allow = array('bool', 'boolean'); if (!in_array($type, $allow)) { throw new TypeError('Argument ' . $argumentIndex . ' must be a boolean, ' . $realType . ' given.'); } $mixedVar = (bool) $mixedVar; break; case 'string': if (!is_string($mixedVar)) { throw new TypeError('Argument ' . $argumentIndex . ' must be a string, ' . $realType . ' given.'); } $mixedVar = (string) $mixedVar; break; case 'decimal': case 'double': case 'float': $allow = array('decimal', 'double', 'float'); if (!in_array($type, $allow)) { throw new TypeError('Argument ' . $argumentIndex . ' must be a float, ' . $realType . ' given.'); } $mixedVar = (float) $mixedVar; break; case 'object': if (!is_object($mixedVar)) { throw new TypeError('Argument ' . $argumentIndex . ' must be an object, ' . $realType . ' given.'); } break; case 'array': if (!is_array($mixedVar)) { if (is_object($mixedVar)) { if ($mixedVar instanceof ArrayAccess) { return; } } throw new TypeError('Argument ' . $argumentIndex . ' must be an array, ' . $realType . ' given.'); } break; default: throw new SodiumException('Unknown type (' . $realType .') does not match expect type (' . $type . ')'); } } /** * Evaluate whether or not two strings are equal (in constant-time) * * @param string $left * @param string $right * @return bool * @throws SodiumException * @throws TypeError */ public static function hashEquals($left, $right) { /* Type checks: */ if (!is_string($left)) { throw new TypeError('Argument 1 must be a string, ' . gettype($left) . ' given.'); } if (!is_string($right)) { throw new TypeError('Argument 2 must be a string, ' . gettype($right) . ' given.'); } if (is_callable('hash_equals')) { return hash_equals($left, $right); } $d = 0; /** @var int $len */ $len = self::strlen($left); if ($len !== self::strlen($right)) { return false; } for ($i = 0; $i < $len; ++$i) { $d |= self::chrToInt($left[$i]) ^ self::chrToInt($right[$i]); } if ($d !== 0) { return false; } return $left === $right; } /** * Catch hash_update() failures and throw instead of silently proceeding * * @param HashContext|resource &$hs * @param string $data * @return void * @throws SodiumException * @psalm-suppress PossiblyInvalidArgument */ protected static function hash_update(&$hs, $data) { if (!hash_update($hs, $data)) { throw new SodiumException('hash_update() failed'); } } /** * Convert a hexadecimal string into a binary string without cache-timing * leaks * * @internal You should not use this directly from another application * * @param string $hexString * @param string $ignore * @param bool $strictPadding * @return string (raw binary) * * @throws SodiumException * @throws TypeError */ public static function hex2bin($hexString, $ignore = '', $strictPadding = false) { /* Type checks: */ if (!is_string($hexString)) { throw new TypeError('Argument 1 must be a string, ' . gettype($hexString) . ' given.'); } if (!is_string($ignore)) { throw new TypeError('Argument 2 must be a string, ' . gettype($hexString) . ' given.'); } $hex_pos = 0; $bin = ''; $c_acc = 0; $hex_len = self::strlen($hexString); $state = 0; $chunk = unpack('C*', $hexString); while ($hex_pos < $hex_len) { ++$hex_pos; /** @var int $c */ $c = $chunk[$hex_pos]; $c_num = $c ^ 48; $c_num0 = ($c_num - 10) >> 8; $c_alpha = ($c & ~32) - 55; $c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8; if (($c_num0 | $c_alpha0) === 0) { if ($ignore && $state === 0 && strpos($ignore, self::intToChr($c)) !== false) { continue; } throw new RangeException( 'hex2bin() only expects hexadecimal characters' ); } $c_val = ($c_num0 & $c_num) | ($c_alpha & $c_alpha0); if ($state === 0) { $c_acc = $c_val * 16; } else { $bin .= pack('C', $c_acc | $c_val); } $state ^= 1; } if ($strictPadding && $state !== 0) { throw new SodiumException( 'Expected an even number of hexadecimal characters' ); } return $bin; } /** * Turn an array of integers into a string * * @internal You should not use this directly from another application * * @param array<int, int> $ints * @return string */ public static function intArrayToString(array $ints) { $args = $ints; foreach ($args as $i => $v) { $args[$i] = (int) ($v & 0xff); } array_unshift($args, str_repeat('C', count($ints))); return (string) (call_user_func_array('pack', $args)); } /** * Cache-timing-safe variant of ord() * * @internal You should not use this directly from another application * * @param int $int * @return string * @throws TypeError */ public static function intToChr($int) { return pack('C', $int); } /** * Load a 3 character substring into an integer * * @internal You should not use this directly from another application * * @param string $string * @return int * @throws RangeException * @throws TypeError */ public static function load_3($string) { /* Type checks: */ if (!is_string($string)) { throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); } /* Input validation: */ if (self::strlen($string) < 3) { throw new RangeException( 'String must be 3 bytes or more; ' . self::strlen($string) . ' given.' ); } /** @var array<int, int> $unpacked */ $unpacked = unpack('V', $string . "\0"); return (int) ($unpacked[1] & 0xffffff); } /** * Load a 4 character substring into an integer * * @internal You should not use this directly from another application * * @param string $string * @return int * @throws RangeException * @throws TypeError */ public static function load_4($string) { /* Type checks: */ if (!is_string($string)) { throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); } /* Input validation: */ if (self::strlen($string) < 4) { throw new RangeException( 'String must be 4 bytes or more; ' . self::strlen($string) . ' given.' ); } /** @var array<int, int> $unpacked */ $unpacked = unpack('V', $string); return (int) $unpacked[1]; } /** * Load a 8 character substring into an integer * * @internal You should not use this directly from another application * * @param string $string * @return int * @throws RangeException * @throws SodiumException * @throws TypeError */ public static function load64_le($string) { /* Type checks: */ if (!is_string($string)) { throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); } /* Input validation: */ if (self::strlen($string) < 4) { throw new RangeException( 'String must be 4 bytes or more; ' . self::strlen($string) . ' given.' ); } if (PHP_VERSION_ID >= 50603 && PHP_INT_SIZE === 8) { /** @var array<int, int> $unpacked */ $unpacked = unpack('P', $string); return (int) $unpacked[1]; } /** @var int $result */ $result = (self::chrToInt($string[0]) & 0xff); $result |= (self::chrToInt($string[1]) & 0xff) << 8; $result |= (self::chrToInt($string[2]) & 0xff) << 16; $result |= (self::chrToInt($string[3]) & 0xff) << 24; $result |= (self::chrToInt($string[4]) & 0xff) << 32; $result |= (self::chrToInt($string[5]) & 0xff) << 40; $result |= (self::chrToInt($string[6]) & 0xff) << 48; $result |= (self::chrToInt($string[7]) & 0xff) << 56; return (int) $result; } /** * @internal You should not use this directly from another application * * @param string $left * @param string $right * @return int * @throws SodiumException * @throws TypeError */ public static function memcmp($left, $right) { $e = (int) !self::hashEquals($left, $right); return 0 - $e; } /** * Multiply two integers in constant-time * * Micro-architecture timing side-channels caused by how your CPU * implements multiplication are best prevented by never using the * multiplication operators and ensuring that our code always takes * the same number of operations to complete, regardless of the values * of $a and $b. * * @internal You should not use this directly from another application * * @param int $a * @param int $b * @param int $size Limits the number of operations (useful for small, * constant operands) * @return int */ public static function mul($a, $b, $size = 0) { if (ParagonIE_Sodium_Compat::$fastMult) { return (int) ($a * $b); } static $defaultSize = null; /** @var int $defaultSize */ if (!$defaultSize) { /** @var int $defaultSize */ $defaultSize = (PHP_INT_SIZE << 3) - 1; } if ($size < 1) { /** @var int $size */ $size = $defaultSize; } /** @var int $size */ $c = 0; /** * Mask is either -1 or 0. * * -1 in binary looks like 0x1111 ... 1111 * 0 in binary looks like 0x0000 ... 0000 * * @var int */ $mask = -(($b >> ((int) $defaultSize)) & 1); /** * Ensure $b is a positive integer, without creating * a branching side-channel * * @var int $b */ $b = ($b & ~$mask) | ($mask & -$b); /** * Unless $size is provided: * * This loop always runs 32 times when PHP_INT_SIZE is 4. * This loop always runs 64 times when PHP_INT_SIZE is 8. */ for ($i = $size; $i >= 0; --$i) { $c += (int) ($a & -($b & 1)); $a <<= 1; $b >>= 1; } $c = (int) @($c & -1); /** * If $b was negative, we then apply the same value to $c here. * It doesn't matter much if $a was negative; the $c += above would * have produced a negative integer to begin with. But a negative $b * makes $b >>= 1 never return 0, so we would end up with incorrect * results. * * The end result is what we'd expect from integer multiplication. */ return (int) (($c & ~$mask) | ($mask & -$c)); } /** * Convert any arbitrary numbers into two 32-bit integers that represent * a 64-bit integer. * * @internal You should not use this directly from another application * * @param int|float $num * @return array<int, int> */ public static function numericTo64BitInteger($num) { $high = 0; /** @var int $low */ if (PHP_INT_SIZE === 4) { $low = (int) $num; } else { $low = $num & 0xffffffff; } if ((+(abs($num))) >= 1) { if ($num > 0) { /** @var int $high */ $high = min((+(floor($num/4294967296))), 4294967295); } else { /** @var int $high */ $high = ~~((+(ceil(($num - (+((~~($num)))))/4294967296)))); } } return array((int) $high, (int) $low); } /** * Store a 24-bit integer into a string, treating it as big-endian. * * @internal You should not use this directly from another application * * @param int $int * @return string * @throws TypeError */ public static function store_3($int) { /* Type checks: */ if (!is_int($int)) { if (is_numeric($int)) { $int = (int) $int; } else { throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); } } /** @var string $packed */ $packed = pack('N', $int); return self::substr($packed, 1, 3); } /** * Store a 32-bit integer into a string, treating it as little-endian. * * @internal You should not use this directly from another application * * @param int $int * @return string * @throws TypeError */ public static function store32_le($int) { /* Type checks: */ if (!is_int($int)) { if (is_numeric($int)) { $int = (int) $int; } else { throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); } } /** @var string $packed */ $packed = pack('V', $int); return $packed; } /** * Store a 32-bit integer into a string, treating it as big-endian. * * @internal You should not use this directly from another application * * @param int $int * @return string * @throws TypeError */ public static function store_4($int) { /* Type checks: */ if (!is_int($int)) { if (is_numeric($int)) { $int = (int) $int; } else { throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); } } /** @var string $packed */ $packed = pack('N', $int); return $packed; } /** * Stores a 64-bit integer as an string, treating it as little-endian. * * @internal You should not use this directly from another application * * @param int $int * @return string * @throws TypeError */ public static function store64_le($int) { /* Type checks: */ if (!is_int($int)) { if (is_numeric($int)) { $int = (int) $int; } else { throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); } } if (PHP_INT_SIZE === 8) { if (PHP_VERSION_ID >= 50603) { /** @var string $packed */ $packed = pack('P', $int); return $packed; } return self::intToChr($int & 0xff) . self::intToChr(($int >> 8) & 0xff) . self::intToChr(($int >> 16) & 0xff) . self::intToChr(($int >> 24) & 0xff) . self::intToChr(($int >> 32) & 0xff) . self::intToChr(($int >> 40) & 0xff) . self::intToChr(($int >> 48) & 0xff) . self::intToChr(($int >> 56) & 0xff); } if ($int > PHP_INT_MAX) { list($hiB, $int) = self::numericTo64BitInteger($int); } else { $hiB = 0; } return self::intToChr(($int ) & 0xff) . self::intToChr(($int >> 8) & 0xff) . self::intToChr(($int >> 16) & 0xff) . self::intToChr(($int >> 24) & 0xff) . self::intToChr($hiB & 0xff) . self::intToChr(($hiB >> 8) & 0xff) . self::intToChr(($hiB >> 16) & 0xff) . self::intToChr(($hiB >> 24) & 0xff); } /** * Safe string length * * @internal You should not use this directly from another application * * @ref mbstring.func_overload * * @param string $str * @return int * @throws TypeError */ public static function strlen($str) { /* Type checks: */ if (!is_string($str)) { throw new TypeError('String expected'); } return (int) ( self::isMbStringOverride() ? mb_strlen($str, '8bit') : strlen($str) ); } /** * Turn a string into an array of integers * * @internal You should not use this directly from another application * * @param string $string * @return array<int, int> * @throws TypeError */ public static function stringToIntArray($string) { if (!is_string($string)) { throw new TypeError('String expected'); } /** * @var array<int, int> */ $values = array_values( unpack('C*', $string) ); return $values; } /** * Safe substring * * @internal You should not use this directly from another application * * @ref mbstring.func_overload * * @param string $str * @param int $start * @param int $length * @return string * @throws TypeError */ public static function substr($str, $start = 0, $length = null) { /* Type checks: */ if (!is_string($str)) { throw new TypeError('String expected'); } if ($length === 0) { return ''; } if (self::isMbStringOverride()) { if (PHP_VERSION_ID < 50400 && $length === null) { $length = self::strlen($str); } $sub = (string) mb_substr($str, $start, $length, '8bit'); } elseif ($length === null) { $sub = (string) substr($str, $start); } else { $sub = (string) substr($str, $start, $length); } if ($sub !== '') { return $sub; } return ''; } /** * Compare a 16-character byte string in constant time. * * @internal You should not use this directly from another application * * @param string $a * @param string $b * @return bool * @throws SodiumException * @throws TypeError */ public static function verify_16($a, $b) { /* Type checks: */ if (!is_string($a)) { throw new TypeError('String expected'); } if (!is_string($b)) { throw new TypeError('String expected'); } return self::hashEquals( self::substr($a, 0, 16), self::substr($b, 0, 16) ); } /** * Compare a 32-character byte string in constant time. * * @internal You should not use this directly from another application * * @param string $a * @param string $b * @return bool * @throws SodiumException * @throws TypeError */ public static function verify_32($a, $b) { /* Type checks: */ if (!is_string($a)) { throw new TypeError('String expected'); } if (!is_string($b)) { throw new TypeError('String expected'); } return self::hashEquals( self::substr($a, 0, 32), self::substr($b, 0, 32) ); } /** * Calculate $a ^ $b for two strings. * * @internal You should not use this directly from another application * * @param string $a * @param string $b * @return string * @throws TypeError */ public static function xorStrings($a, $b) { /* Type checks: */ if (!is_string($a)) { throw new TypeError('Argument 1 must be a string'); } if (!is_string($b)) { throw new TypeError('Argument 2 must be a string'); } return (string) ($a ^ $b); } /** * Returns whether or not mbstring.func_overload is in effect. * * @internal You should not use this directly from another application * * Note: MB_OVERLOAD_STRING === 2, but we don't reference the constant * (for nuisance-free PHP 8 support) * * @return bool */ protected static function isMbStringOverride() { static $mbstring = null; if ($mbstring === null) { if (!defined('MB_OVERLOAD_STRING')) { $mbstring = false; return $mbstring; } $mbstring = extension_loaded('mbstring') && defined('MB_OVERLOAD_STRING') && ((int) (ini_get('mbstring.func_overload')) & 2); // MB_OVERLOAD_STRING === 2 } /** @var bool $mbstring */ return $mbstring; } } Core/AEGIS/State256.php 0000644 00000014575 15162213705 0010330 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_AEGIS_State256', false)) { return; } if (!defined('SODIUM_COMPAT_AEGIS_C0')) { define('SODIUM_COMPAT_AEGIS_C0', "\x00\x01\x01\x02\x03\x05\x08\x0d\x15\x22\x37\x59\x90\xe9\x79\x62"); } if (!defined('SODIUM_COMPAT_AEGIS_C1')) { define('SODIUM_COMPAT_AEGIS_C1', "\xdb\x3d\x18\x55\x6d\xc2\x2f\xf1\x20\x11\x31\x42\x73\xb5\x28\xdd"); } class ParagonIE_Sodium_Core_AEGIS_State256 { /** @var array<int, string> $state */ protected $state; public function __construct() { $this->state = array_fill(0, 6, ''); } /** * @internal Only use this for unit tests! * @return string[] */ public function getState() { return array_values($this->state); } /** * @param array $input * @return self * @throws SodiumException * * @internal Only for unit tests */ public static function initForUnitTests(array $input) { if (count($input) < 6) { throw new SodiumException('invalid input'); } $state = new self(); for ($i = 0; $i < 6; ++$i) { $state->state[$i] = $input[$i]; } return $state; } /** * @param string $key * @param string $nonce * @return self */ public static function init($key, $nonce) { $state = new self(); $k0 = ParagonIE_Sodium_Core_Util::substr($key, 0, 16); $k1 = ParagonIE_Sodium_Core_Util::substr($key, 16, 16); $n0 = ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16); $n1 = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 16); // S0 = k0 ^ n0 // S1 = k1 ^ n1 // S2 = C1 // S3 = C0 // S4 = k0 ^ C0 // S5 = k1 ^ C1 $k0_n0 = $k0 ^ $n0; $k1_n1 = $k1 ^ $n1; $state->state[0] = $k0_n0; $state->state[1] = $k1_n1; $state->state[2] = SODIUM_COMPAT_AEGIS_C1; $state->state[3] = SODIUM_COMPAT_AEGIS_C0; $state->state[4] = $k0 ^ SODIUM_COMPAT_AEGIS_C0; $state->state[5] = $k1 ^ SODIUM_COMPAT_AEGIS_C1; // Repeat(4, // Update(k0) // Update(k1) // Update(k0 ^ n0) // Update(k1 ^ n1) // ) for ($i = 0; $i < 4; ++$i) { $state->update($k0); $state->update($k1); $state->update($k0 ^ $n0); $state->update($k1 ^ $n1); } return $state; } /** * @param string $ai * @return self * @throws SodiumException */ public function absorb($ai) { if (ParagonIE_Sodium_Core_Util::strlen($ai) !== 16) { throw new SodiumException('Input must be an AES block in size'); } return $this->update($ai); } /** * @param string $ci * @return string * @throws SodiumException */ public function dec($ci) { if (ParagonIE_Sodium_Core_Util::strlen($ci) !== 16) { throw new SodiumException('Input must be an AES block in size'); } // z = S1 ^ S4 ^ S5 ^ (S2 & S3) $z = $this->state[1] ^ $this->state[4] ^ $this->state[5] ^ ParagonIE_Sodium_Core_Util::andStrings($this->state[2], $this->state[3]); $xi = $ci ^ $z; $this->update($xi); return $xi; } /** * @param string $cn * @return string */ public function decPartial($cn) { $len = ParagonIE_Sodium_Core_Util::strlen($cn); // z = S1 ^ S4 ^ S5 ^ (S2 & S3) $z = $this->state[1] ^ $this->state[4] ^ $this->state[5] ^ ParagonIE_Sodium_Core_Util::andStrings($this->state[2], $this->state[3]); // t = ZeroPad(cn, 128) $t = str_pad($cn, 16, "\0", STR_PAD_RIGHT); // out = t ^ z $out = $t ^ $z; // xn = Truncate(out, |cn|) $xn = ParagonIE_Sodium_Core_Util::substr($out, 0, $len); // v = ZeroPad(xn, 128) $v = str_pad($xn, 16, "\0", STR_PAD_RIGHT); // Update(v) $this->update($v); // return xn return $xn; } /** * @param string $xi * @return string * @throws SodiumException */ public function enc($xi) { if (ParagonIE_Sodium_Core_Util::strlen($xi) !== 16) { throw new SodiumException('Input must be an AES block in size'); } // z = S1 ^ S4 ^ S5 ^ (S2 & S3) $z = $this->state[1] ^ $this->state[4] ^ $this->state[5] ^ ParagonIE_Sodium_Core_Util::andStrings($this->state[2], $this->state[3]); $this->update($xi); return $xi ^ $z; } /** * @param int $ad_len_bits * @param int $msg_len_bits * @return string */ public function finalize($ad_len_bits, $msg_len_bits) { $encoded = ParagonIE_Sodium_Core_Util::store64_le($ad_len_bits) . ParagonIE_Sodium_Core_Util::store64_le($msg_len_bits); $t = $this->state[3] ^ $encoded; for ($i = 0; $i < 7; ++$i) { $this->update($t); } return ($this->state[0] ^ $this->state[1] ^ $this->state[2]) . ($this->state[3] ^ $this->state[4] ^ $this->state[5]); } /** * @param string $m * @return self */ public function update($m) { /* S'0 = AESRound(S5, S0 ^ M) S'1 = AESRound(S0, S1) S'2 = AESRound(S1, S2) S'3 = AESRound(S2, S3) S'4 = AESRound(S3, S4) S'5 = AESRound(S4, S5) */ list($s_0, $s_1) = ParagonIE_Sodium_Core_AES::doubleRound( $this->state[5],$this->state[0] ^ $m, $this->state[0], $this->state[1] ); list($s_2, $s_3) = ParagonIE_Sodium_Core_AES::doubleRound( $this->state[1], $this->state[2], $this->state[2], $this->state[3] ); list($s_4, $s_5) = ParagonIE_Sodium_Core_AES::doubleRound( $this->state[3], $this->state[4], $this->state[4], $this->state[5] ); /* S0 = S'0 S1 = S'1 S2 = S'2 S3 = S'3 S4 = S'4 S5 = S'5 */ $this->state[0] = $s_0; $this->state[1] = $s_1; $this->state[2] = $s_2; $this->state[3] = $s_3; $this->state[4] = $s_4; $this->state[5] = $s_5; return $this; } } Core/AEGIS/State128L.php 0000644 00000020052 15162213706 0010426 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_AEGIS_State128L', false)) { return; } if (!defined('SODIUM_COMPAT_AEGIS_C0')) { define('SODIUM_COMPAT_AEGIS_C0', "\x00\x01\x01\x02\x03\x05\x08\x0d\x15\x22\x37\x59\x90\xe9\x79\x62"); } if (!defined('SODIUM_COMPAT_AEGIS_C1')) { define('SODIUM_COMPAT_AEGIS_C1', "\xdb\x3d\x18\x55\x6d\xc2\x2f\xf1\x20\x11\x31\x42\x73\xb5\x28\xdd"); } class ParagonIE_Sodium_Core_AEGIS_State128L { /** @var array<int, string> $state */ protected $state; public function __construct() { $this->state = array_fill(0, 8, ''); } /** * @internal Only use this for unit tests! * @return string[] */ public function getState() { return array_values($this->state); } /** * @param array $input * @return self * @throws SodiumException * * @internal Only for unit tests */ public static function initForUnitTests(array $input) { if (count($input) < 8) { throw new SodiumException('invalid input'); } $state = new self(); for ($i = 0; $i < 8; ++$i) { $state->state[$i] = $input[$i]; } return $state; } /** * @param string $key * @param string $nonce * @return self */ public static function init($key, $nonce) { $state = new self(); // S0 = key ^ nonce $state->state[0] = $key ^ $nonce; // S1 = C1 $state->state[1] = SODIUM_COMPAT_AEGIS_C1; // S2 = C0 $state->state[2] = SODIUM_COMPAT_AEGIS_C0; // S3 = C1 $state->state[3] = SODIUM_COMPAT_AEGIS_C1; // S4 = key ^ nonce $state->state[4] = $key ^ $nonce; // S5 = key ^ C0 $state->state[5] = $key ^ SODIUM_COMPAT_AEGIS_C0; // S6 = key ^ C1 $state->state[6] = $key ^ SODIUM_COMPAT_AEGIS_C1; // S7 = key ^ C0 $state->state[7] = $key ^ SODIUM_COMPAT_AEGIS_C0; // Repeat(10, Update(nonce, key)) for ($i = 0; $i < 10; ++$i) { $state->update($nonce, $key); } return $state; } /** * @param string $ai * @return self */ public function absorb($ai) { if (ParagonIE_Sodium_Core_Util::strlen($ai) !== 32) { throw new SodiumException('Input must be two AES blocks in size'); } $t0 = ParagonIE_Sodium_Core_Util::substr($ai, 0, 16); $t1 = ParagonIE_Sodium_Core_Util::substr($ai, 16, 16); return $this->update($t0, $t1); } /** * @param string $ci * @return string * @throws SodiumException */ public function dec($ci) { if (ParagonIE_Sodium_Core_Util::strlen($ci) !== 32) { throw new SodiumException('Input must be two AES blocks in size'); } // z0 = S6 ^ S1 ^ (S2 & S3) $z0 = $this->state[6] ^ $this->state[1] ^ ParagonIE_Sodium_Core_Util::andStrings($this->state[2], $this->state[3]); // z1 = S2 ^ S5 ^ (S6 & S7) $z1 = $this->state[2] ^ $this->state[5] ^ ParagonIE_Sodium_Core_Util::andStrings($this->state[6], $this->state[7]); // t0, t1 = Split(xi, 128) $t0 = ParagonIE_Sodium_Core_Util::substr($ci, 0, 16); $t1 = ParagonIE_Sodium_Core_Util::substr($ci, 16, 16); // out0 = t0 ^ z0 // out1 = t1 ^ z1 $out0 = $t0 ^ $z0; $out1 = $t1 ^ $z1; // Update(out0, out1) // xi = out0 || out1 $this->update($out0, $out1); return $out0 . $out1; } /** * @param string $cn * @return string */ public function decPartial($cn) { $len = ParagonIE_Sodium_Core_Util::strlen($cn); // z0 = S6 ^ S1 ^ (S2 & S3) $z0 = $this->state[6] ^ $this->state[1] ^ ParagonIE_Sodium_Core_Util::andStrings($this->state[2], $this->state[3]); // z1 = S2 ^ S5 ^ (S6 & S7) $z1 = $this->state[2] ^ $this->state[5] ^ ParagonIE_Sodium_Core_Util::andStrings($this->state[6], $this->state[7]); // t0, t1 = Split(ZeroPad(cn, 256), 128) $cn = str_pad($cn, 32, "\0", STR_PAD_RIGHT); $t0 = ParagonIE_Sodium_Core_Util::substr($cn, 0, 16); $t1 = ParagonIE_Sodium_Core_Util::substr($cn, 16, 16); // out0 = t0 ^ z0 // out1 = t1 ^ z1 $out0 = $t0 ^ $z0; $out1 = $t1 ^ $z1; // xn = Truncate(out0 || out1, |cn|) $xn = ParagonIE_Sodium_Core_Util::substr($out0 . $out1, 0, $len); // v0, v1 = Split(ZeroPad(xn, 256), 128) $padded = str_pad($xn, 32, "\0", STR_PAD_RIGHT); $v0 = ParagonIE_Sodium_Core_Util::substr($padded, 0, 16); $v1 = ParagonIE_Sodium_Core_Util::substr($padded, 16, 16); // Update(v0, v1) $this->update($v0, $v1); // return xn return $xn; } /** * @param string $xi * @return string * @throws SodiumException */ public function enc($xi) { if (ParagonIE_Sodium_Core_Util::strlen($xi) !== 32) { throw new SodiumException('Input must be two AES blocks in size'); } // z0 = S6 ^ S1 ^ (S2 & S3) $z0 = $this->state[6] ^ $this->state[1] ^ ParagonIE_Sodium_Core_Util::andStrings($this->state[2], $this->state[3]); // z1 = S2 ^ S5 ^ (S6 & S7) $z1 = $this->state[2] ^ $this->state[5] ^ ParagonIE_Sodium_Core_Util::andStrings($this->state[6], $this->state[7]); // t0, t1 = Split(xi, 128) $t0 = ParagonIE_Sodium_Core_Util::substr($xi, 0, 16); $t1 = ParagonIE_Sodium_Core_Util::substr($xi, 16, 16); // out0 = t0 ^ z0 // out1 = t1 ^ z1 $out0 = $t0 ^ $z0; $out1 = $t1 ^ $z1; // Update(t0, t1) // ci = out0 || out1 $this->update($t0, $t1); // return ci return $out0 . $out1; } /** * @param int $ad_len_bits * @param int $msg_len_bits * @return string */ public function finalize($ad_len_bits, $msg_len_bits) { $encoded = ParagonIE_Sodium_Core_Util::store64_le($ad_len_bits) . ParagonIE_Sodium_Core_Util::store64_le($msg_len_bits); $t = $this->state[2] ^ $encoded; for ($i = 0; $i < 7; ++$i) { $this->update($t, $t); } return ($this->state[0] ^ $this->state[1] ^ $this->state[2] ^ $this->state[3]) . ($this->state[4] ^ $this->state[5] ^ $this->state[6] ^ $this->state[7]); } /** * @param string $m0 * @param string $m1 * @return self */ public function update($m0, $m1) { /* S'0 = AESRound(S7, S0 ^ M0) S'1 = AESRound(S0, S1) S'2 = AESRound(S1, S2) S'3 = AESRound(S2, S3) S'4 = AESRound(S3, S4 ^ M1) S'5 = AESRound(S4, S5) S'6 = AESRound(S5, S6) S'7 = AESRound(S6, S7) */ list($s_0, $s_1) = ParagonIE_Sodium_Core_AES::doubleRound( $this->state[7], $this->state[0] ^ $m0, $this->state[0], $this->state[1] ); list($s_2, $s_3) = ParagonIE_Sodium_Core_AES::doubleRound( $this->state[1], $this->state[2], $this->state[2], $this->state[3] ); list($s_4, $s_5) = ParagonIE_Sodium_Core_AES::doubleRound( $this->state[3], $this->state[4] ^ $m1, $this->state[4], $this->state[5] ); list($s_6, $s_7) = ParagonIE_Sodium_Core_AES::doubleRound( $this->state[5], $this->state[6], $this->state[6], $this->state[7] ); /* S0 = S'0 S1 = S'1 S2 = S'2 S3 = S'3 S4 = S'4 S5 = S'5 S6 = S'6 S7 = S'7 */ $this->state[0] = $s_0; $this->state[1] = $s_1; $this->state[2] = $s_2; $this->state[3] = $s_3; $this->state[4] = $s_4; $this->state[5] = $s_5; $this->state[6] = $s_6; $this->state[7] = $s_7; return $this; } } Core/BLAKE2b.php 0000644 00000057200 15162213707 0007217 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) { return; } /** * Class ParagonIE_Sodium_Core_BLAKE2b * * Based on the work of Devi Mandiri in devi/salt. */ abstract class ParagonIE_Sodium_Core_BLAKE2b extends ParagonIE_Sodium_Core_Util { /** * @var SplFixedArray */ protected static $iv; /** * @var array<int, array<int, int>> */ protected static $sigma = array( array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3), array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4), array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8), array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13), array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9), array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11), array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10), array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5), array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0), array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3) ); const BLOCKBYTES = 128; const OUTBYTES = 64; const KEYBYTES = 64; /** * Turn two 32-bit integers into a fixed array representing a 64-bit integer. * * @internal You should not use this directly from another application * * @param int $high * @param int $low * @return SplFixedArray * @psalm-suppress MixedAssignment */ public static function new64($high, $low) { if (PHP_INT_SIZE === 4) { throw new SodiumException("Error, use 32-bit"); } $i64 = new SplFixedArray(2); $i64[0] = $high & 0xffffffff; $i64[1] = $low & 0xffffffff; return $i64; } /** * Convert an arbitrary number into an SplFixedArray of two 32-bit integers * that represents a 64-bit integer. * * @internal You should not use this directly from another application * * @param int $num * @return SplFixedArray */ protected static function to64($num) { list($hi, $lo) = self::numericTo64BitInteger($num); return self::new64($hi, $lo); } /** * Adds two 64-bit integers together, returning their sum as a SplFixedArray * containing two 32-bit integers (representing a 64-bit integer). * * @internal You should not use this directly from another application * * @param SplFixedArray $x * @param SplFixedArray $y * @return SplFixedArray * @psalm-suppress MixedArgument * @psalm-suppress MixedAssignment * @psalm-suppress MixedOperand */ protected static function add64($x, $y) { if (PHP_INT_SIZE === 4) { throw new SodiumException("Error, use 32-bit"); } $l = ($x[1] + $y[1]) & 0xffffffff; return self::new64( (int) ($x[0] + $y[0] + ( ($l < $x[1]) ? 1 : 0 )), (int) $l ); } /** * @internal You should not use this directly from another application * * @param SplFixedArray $x * @param SplFixedArray $y * @param SplFixedArray $z * @return SplFixedArray */ protected static function add364($x, $y, $z) { return self::add64($x, self::add64($y, $z)); } /** * @internal You should not use this directly from another application * * @param SplFixedArray $x * @param SplFixedArray $y * @return SplFixedArray * @throws SodiumException * @throws TypeError */ protected static function xor64(SplFixedArray $x, SplFixedArray $y) { if (PHP_INT_SIZE === 4) { throw new SodiumException("Error, use 32-bit"); } if (!is_numeric($x[0])) { throw new SodiumException('x[0] is not an integer'); } if (!is_numeric($x[1])) { throw new SodiumException('x[1] is not an integer'); } if (!is_numeric($y[0])) { throw new SodiumException('y[0] is not an integer'); } if (!is_numeric($y[1])) { throw new SodiumException('y[1] is not an integer'); } return self::new64( (int) (($x[0] ^ $y[0]) & 0xffffffff), (int) (($x[1] ^ $y[1]) & 0xffffffff) ); } /** * @internal You should not use this directly from another application * * @param SplFixedArray $x * @param int $c * @return SplFixedArray * @psalm-suppress MixedAssignment */ public static function rotr64($x, $c) { if (PHP_INT_SIZE === 4) { throw new SodiumException("Error, use 32-bit"); } if ($c >= 64) { $c %= 64; } if ($c >= 32) { /** @var int $tmp */ $tmp = $x[0]; $x[0] = $x[1]; $x[1] = $tmp; $c -= 32; } if ($c === 0) { return $x; } $l0 = 0; $c = 64 - $c; /** @var int $c */ if ($c < 32) { $h0 = ((int) ($x[0]) << $c) | ( ( (int) ($x[1]) & ((1 << $c) - 1) << (32 - $c) ) >> (32 - $c) ); $l0 = (int) ($x[1]) << $c; } else { $h0 = (int) ($x[1]) << ($c - 32); } $h1 = 0; $c1 = 64 - $c; if ($c1 < 32) { $h1 = (int) ($x[0]) >> $c1; $l1 = ((int) ($x[1]) >> $c1) | ((int) ($x[0]) & ((1 << $c1) - 1)) << (32 - $c1); } else { $l1 = (int) ($x[0]) >> ($c1 - 32); } return self::new64($h0 | $h1, $l0 | $l1); } /** * @internal You should not use this directly from another application * * @param SplFixedArray $x * @return int * @psalm-suppress MixedOperand */ protected static function flatten64($x) { return (int) ($x[0] * 4294967296 + $x[1]); } /** * @internal You should not use this directly from another application * * @param SplFixedArray $x * @param int $i * @return SplFixedArray * @psalm-suppress MixedArgument * @psalm-suppress MixedArrayOffset */ protected static function load64(SplFixedArray $x, $i) { /** @var int $l */ $l = (int) ($x[$i]) | ((int) ($x[$i+1]) << 8) | ((int) ($x[$i+2]) << 16) | ((int) ($x[$i+3]) << 24); /** @var int $h */ $h = (int) ($x[$i+4]) | ((int) ($x[$i+5]) << 8) | ((int) ($x[$i+6]) << 16) | ((int) ($x[$i+7]) << 24); return self::new64($h, $l); } /** * @internal You should not use this directly from another application * * @param SplFixedArray $x * @param int $i * @param SplFixedArray $u * @return void * @psalm-suppress MixedAssignment */ protected static function store64(SplFixedArray $x, $i, SplFixedArray $u) { $maxLength = $x->getSize() - 1; for ($j = 0; $j < 8; ++$j) { /* [0, 1, 2, 3, 4, 5, 6, 7] ... becomes ... [0, 0, 0, 0, 1, 1, 1, 1] */ /** @var int $uIdx */ $uIdx = ((7 - $j) & 4) >> 2; $x[$i] = ((int) ($u[$uIdx]) & 0xff); if (++$i > $maxLength) { return; } /** @psalm-suppress MixedOperand */ $u[$uIdx] >>= 8; } } /** * This just sets the $iv static variable. * * @internal You should not use this directly from another application * * @return void */ public static function pseudoConstructor() { static $called = false; if ($called) { return; } self::$iv = new SplFixedArray(8); self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908); self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b); self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b); self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1); self::$iv[4] = self::new64(0x510e527f, 0xade682d1); self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f); self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b); self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179); $called = true; } /** * Returns a fresh BLAKE2 context. * * @internal You should not use this directly from another application * * @return SplFixedArray * @psalm-suppress MixedAssignment * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment */ protected static function context() { $ctx = new SplFixedArray(6); $ctx[0] = new SplFixedArray(8); // h $ctx[1] = new SplFixedArray(2); // t $ctx[2] = new SplFixedArray(2); // f $ctx[3] = new SplFixedArray(256); // buf $ctx[4] = 0; // buflen $ctx[5] = 0; // last_node (uint8_t) for ($i = 8; $i--;) { $ctx[0][$i] = self::$iv[$i]; } for ($i = 256; $i--;) { $ctx[3][$i] = 0; } $zero = self::new64(0, 0); $ctx[1][0] = $zero; $ctx[1][1] = $zero; $ctx[2][0] = $zero; $ctx[2][1] = $zero; return $ctx; } /** * @internal You should not use this directly from another application * * @param SplFixedArray $ctx * @param SplFixedArray $buf * @return void * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedAssignment * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment * @psalm-suppress MixedArrayOffset */ protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) { $m = new SplFixedArray(16); $v = new SplFixedArray(16); for ($i = 16; $i--;) { $m[$i] = self::load64($buf, $i << 3); } for ($i = 8; $i--;) { $v[$i] = $ctx[0][$i]; } $v[ 8] = self::$iv[0]; $v[ 9] = self::$iv[1]; $v[10] = self::$iv[2]; $v[11] = self::$iv[3]; $v[12] = self::xor64($ctx[1][0], self::$iv[4]); $v[13] = self::xor64($ctx[1][1], self::$iv[5]); $v[14] = self::xor64($ctx[2][0], self::$iv[6]); $v[15] = self::xor64($ctx[2][1], self::$iv[7]); for ($r = 0; $r < 12; ++$r) { $v = self::G($r, 0, 0, 4, 8, 12, $v, $m); $v = self::G($r, 1, 1, 5, 9, 13, $v, $m); $v = self::G($r, 2, 2, 6, 10, 14, $v, $m); $v = self::G($r, 3, 3, 7, 11, 15, $v, $m); $v = self::G($r, 4, 0, 5, 10, 15, $v, $m); $v = self::G($r, 5, 1, 6, 11, 12, $v, $m); $v = self::G($r, 6, 2, 7, 8, 13, $v, $m); $v = self::G($r, 7, 3, 4, 9, 14, $v, $m); } for ($i = 8; $i--;) { $ctx[0][$i] = self::xor64( $ctx[0][$i], self::xor64($v[$i], $v[$i+8]) ); } } /** * @internal You should not use this directly from another application * * @param int $r * @param int $i * @param int $a * @param int $b * @param int $c * @param int $d * @param SplFixedArray $v * @param SplFixedArray $m * @return SplFixedArray * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedArrayOffset */ public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) { $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]); $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); $v[$c] = self::add64($v[$c], $v[$d]); $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24); $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]); $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16); $v[$c] = self::add64($v[$c], $v[$d]); $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63); return $v; } /** * @internal You should not use this directly from another application * * @param SplFixedArray $ctx * @param int $inc * @return void * @throws SodiumException * @psalm-suppress MixedArgument * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment */ public static function increment_counter($ctx, $inc) { if ($inc < 0) { throw new SodiumException('Increasing by a negative number makes no sense.'); } $t = self::to64($inc); # S->t is $ctx[1] in our implementation # S->t[0] = ( uint64_t )( t >> 0 ); $ctx[1][0] = self::add64($ctx[1][0], $t); # S->t[1] += ( S->t[0] < inc ); if (self::flatten64($ctx[1][0]) < $inc) { $ctx[1][1] = self::add64($ctx[1][1], self::to64(1)); } } /** * @internal You should not use this directly from another application * * @param SplFixedArray $ctx * @param SplFixedArray $p * @param int $plen * @return void * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedAssignment * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment * @psalm-suppress MixedArrayOffset * @psalm-suppress MixedOperand */ public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) { self::pseudoConstructor(); $offset = 0; while ($plen > 0) { $left = $ctx[4]; $fill = 256 - $left; if ($plen > $fill) { # memcpy( S->buf + left, in, fill ); /* Fill buffer */ for ($i = $fill; $i--;) { $ctx[3][$i + $left] = $p[$i + $offset]; } # S->buflen += fill; $ctx[4] += $fill; # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); self::increment_counter($ctx, 128); # blake2b_compress( S, S->buf ); /* Compress */ self::compress($ctx, $ctx[3]); # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */ for ($i = 128; $i--;) { $ctx[3][$i] = $ctx[3][$i + 128]; } # S->buflen -= BLAKE2B_BLOCKBYTES; $ctx[4] -= 128; # in += fill; $offset += $fill; # inlen -= fill; $plen -= $fill; } else { for ($i = $plen; $i--;) { $ctx[3][$i + $left] = $p[$i + $offset]; } $ctx[4] += $plen; $offset += $plen; $plen -= $plen; } } } /** * @internal You should not use this directly from another application * * @param SplFixedArray $ctx * @param SplFixedArray $out * @return SplFixedArray * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedAssignment * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment * @psalm-suppress MixedArrayOffset * @psalm-suppress MixedOperand */ public static function finish(SplFixedArray $ctx, SplFixedArray $out) { self::pseudoConstructor(); if ($ctx[4] > 128) { self::increment_counter($ctx, 128); self::compress($ctx, $ctx[3]); $ctx[4] -= 128; if ($ctx[4] > 128) { throw new SodiumException('Failed to assert that buflen <= 128 bytes'); } for ($i = $ctx[4]; $i--;) { $ctx[3][$i] = $ctx[3][$i + 128]; } } self::increment_counter($ctx, $ctx[4]); $ctx[2][0] = self::new64(0xffffffff, 0xffffffff); for ($i = 256 - $ctx[4]; $i--;) { $ctx[3][$i+$ctx[4]] = 0; } self::compress($ctx, $ctx[3]); $i = (int) (($out->getSize() - 1) / 8); for (; $i >= 0; --$i) { self::store64($out, $i << 3, $ctx[0][$i]); } return $out; } /** * @internal You should not use this directly from another application * * @param SplFixedArray|null $key * @param int $outlen * @param SplFixedArray|null $salt * @param SplFixedArray|null $personal * @return SplFixedArray * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedAssignment * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment * @psalm-suppress MixedArrayOffset */ public static function init( $key = null, $outlen = 64, $salt = null, $personal = null ) { self::pseudoConstructor(); $klen = 0; if ($key !== null) { if (count($key) > 64) { throw new SodiumException('Invalid key size'); } $klen = count($key); } if ($outlen > 64) { throw new SodiumException('Invalid output size'); } $ctx = self::context(); $p = new SplFixedArray(64); // Zero our param buffer... for ($i = 64; --$i;) { $p[$i] = 0; } $p[0] = $outlen; // digest_length $p[1] = $klen; // key_length $p[2] = 1; // fanout $p[3] = 1; // depth if ($salt instanceof SplFixedArray) { // salt: [32] through [47] for ($i = 0; $i < 16; ++$i) { $p[32 + $i] = (int) $salt[$i]; } } if ($personal instanceof SplFixedArray) { // personal: [48] through [63] for ($i = 0; $i < 16; ++$i) { $p[48 + $i] = (int) $personal[$i]; } } $ctx[0][0] = self::xor64( $ctx[0][0], self::load64($p, 0) ); if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) { // We need to do what blake2b_init_param() does: for ($i = 1; $i < 8; ++$i) { $ctx[0][$i] = self::xor64( $ctx[0][$i], self::load64($p, $i << 3) ); } } if ($klen > 0 && $key instanceof SplFixedArray) { $block = new SplFixedArray(128); for ($i = 128; $i--;) { $block[$i] = 0; } for ($i = $klen; $i--;) { $block[$i] = $key[$i]; } self::update($ctx, $block, 128); $ctx[4] = 128; } return $ctx; } /** * Convert a string into an SplFixedArray of integers * * @internal You should not use this directly from another application * * @param string $str * @return SplFixedArray * @psalm-suppress MixedArgumentTypeCoercion */ public static function stringToSplFixedArray($str = '') { $values = unpack('C*', $str); return SplFixedArray::fromArray(array_values($values)); } /** * Convert an SplFixedArray of integers into a string * * @internal You should not use this directly from another application * * @param SplFixedArray $a * @return string * @throws TypeError */ public static function SplFixedArrayToString(SplFixedArray $a) { /** * @var array<int, int|string> $arr */ $arr = $a->toArray(); $c = $a->count(); array_unshift($arr, str_repeat('C', $c)); return (string) (call_user_func_array('pack', $arr)); } /** * @internal You should not use this directly from another application * * @param SplFixedArray $ctx * @return string * @throws TypeError * @psalm-suppress MixedArgument * @psalm-suppress MixedAssignment * @psalm-suppress MixedArrayAccess * @psalm-suppress MixedArrayAssignment * @psalm-suppress MixedArrayOffset * @psalm-suppress MixedMethodCall */ public static function contextToString(SplFixedArray $ctx) { $str = ''; /** @var array<int, array<int, int>> $ctxA */ $ctxA = $ctx[0]->toArray(); # uint64_t h[8]; for ($i = 0; $i < 8; ++$i) { $str .= self::store32_le($ctxA[$i][1]); $str .= self::store32_le($ctxA[$i][0]); } # uint64_t t[2]; # uint64_t f[2]; for ($i = 1; $i < 3; ++$i) { $ctxA = $ctx[$i]->toArray(); $str .= self::store32_le($ctxA[0][1]); $str .= self::store32_le($ctxA[0][0]); $str .= self::store32_le($ctxA[1][1]); $str .= self::store32_le($ctxA[1][0]); } # uint8_t buf[2 * 128]; $str .= self::SplFixedArrayToString($ctx[3]); /** @var int $ctx4 */ $ctx4 = (int) $ctx[4]; # size_t buflen; $str .= implode('', array( self::intToChr($ctx4 & 0xff), self::intToChr(($ctx4 >> 8) & 0xff), self::intToChr(($ctx4 >> 16) & 0xff), self::intToChr(($ctx4 >> 24) & 0xff), self::intToChr(($ctx4 >> 32) & 0xff), self::intToChr(($ctx4 >> 40) & 0xff), self::intToChr(($ctx4 >> 48) & 0xff), self::intToChr(($ctx4 >> 56) & 0xff) )); # uint8_t last_node; return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23); } /** * Creates an SplFixedArray containing other SplFixedArray elements, from * a string (compatible with \Sodium\crypto_generichash_{init, update, final}) * * @internal You should not use this directly from another application * * @param string $string * @return SplFixedArray * @throws SodiumException * @throws TypeError * @psalm-suppress MixedArrayAssignment */ public static function stringToContext($string) { $ctx = self::context(); # uint64_t h[8]; for ($i = 0; $i < 8; ++$i) { $ctx[0][$i] = SplFixedArray::fromArray( array( self::load_4( self::substr($string, (($i << 3) + 4), 4) ), self::load_4( self::substr($string, (($i << 3) + 0), 4) ) ) ); } # uint64_t t[2]; # uint64_t f[2]; for ($i = 1; $i < 3; ++$i) { $ctx[$i][1] = SplFixedArray::fromArray( array( self::load_4(self::substr($string, 76 + (($i - 1) << 4), 4)), self::load_4(self::substr($string, 72 + (($i - 1) << 4), 4)) ) ); $ctx[$i][0] = SplFixedArray::fromArray( array( self::load_4(self::substr($string, 68 + (($i - 1) << 4), 4)), self::load_4(self::substr($string, 64 + (($i - 1) << 4), 4)) ) ); } # uint8_t buf[2 * 128]; $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256)); # uint8_t buf[2 * 128]; $int = 0; for ($i = 0; $i < 8; ++$i) { $int |= self::chrToInt($string[352 + $i]) << ($i << 3); } $ctx[4] = $int; return $ctx; } } Core/SipHash.php 0000644 00000020051 15162213710 0007500 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_SipHash', false)) { return; } /** * Class ParagonIE_SodiumCompat_Core_SipHash * * Only uses 32-bit arithmetic, while the original SipHash used 64-bit integers */ class ParagonIE_Sodium_Core_SipHash extends ParagonIE_Sodium_Core_Util { /** * @internal You should not use this directly from another application * * @param int[] $v * @return int[] * */ public static function sipRound(array $v) { # v0 += v1; list($v[0], $v[1]) = self::add( array($v[0], $v[1]), array($v[2], $v[3]) ); # v1=ROTL(v1,13); list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 13); # v1 ^= v0; $v[2] = (int) $v[2] ^ (int) $v[0]; $v[3] = (int) $v[3] ^ (int) $v[1]; # v0=ROTL(v0,32); list($v[0], $v[1]) = self::rotl_64((int) $v[0], (int) $v[1], 32); # v2 += v3; list($v[4], $v[5]) = self::add( array((int) $v[4], (int) $v[5]), array((int) $v[6], (int) $v[7]) ); # v3=ROTL(v3,16); list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 16); # v3 ^= v2; $v[6] = (int) $v[6] ^ (int) $v[4]; $v[7] = (int) $v[7] ^ (int) $v[5]; # v0 += v3; list($v[0], $v[1]) = self::add( array((int) $v[0], (int) $v[1]), array((int) $v[6], (int) $v[7]) ); # v3=ROTL(v3,21); list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 21); # v3 ^= v0; $v[6] = (int) $v[6] ^ (int) $v[0]; $v[7] = (int) $v[7] ^ (int) $v[1]; # v2 += v1; list($v[4], $v[5]) = self::add( array((int) $v[4], (int) $v[5]), array((int) $v[2], (int) $v[3]) ); # v1=ROTL(v1,17); list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 17); # v1 ^= v2;; $v[2] = (int) $v[2] ^ (int) $v[4]; $v[3] = (int) $v[3] ^ (int) $v[5]; # v2=ROTL(v2,32) list($v[4], $v[5]) = self::rotl_64((int) $v[4], (int) $v[5], 32); return $v; } /** * Add two 32 bit integers representing a 64-bit integer. * * @internal You should not use this directly from another application * * @param int[] $a * @param int[] $b * @return array<int, mixed> */ public static function add(array $a, array $b) { /** @var int $x1 */ $x1 = $a[1] + $b[1]; /** @var int $c */ $c = $x1 >> 32; // Carry if ($a + $b) > 0xffffffff /** @var int $x0 */ $x0 = $a[0] + $b[0] + $c; return array( $x0 & 0xffffffff, $x1 & 0xffffffff ); } /** * @internal You should not use this directly from another application * * @param int $int0 * @param int $int1 * @param int $c * @return array<int, mixed> */ public static function rotl_64($int0, $int1, $c) { $int0 &= 0xffffffff; $int1 &= 0xffffffff; $c &= 63; if ($c === 32) { return array($int1, $int0); } if ($c > 31) { $tmp = $int1; $int1 = $int0; $int0 = $tmp; $c &= 31; } if ($c === 0) { return array($int0, $int1); } return array( 0xffffffff & ( ($int0 << $c) | ($int1 >> (32 - $c)) ), 0xffffffff & ( ($int1 << $c) | ($int0 >> (32 - $c)) ), ); } /** * Implements Siphash-2-4 using only 32-bit numbers. * * When we split an int into two, the higher bits go to the lower index. * e.g. 0xDEADBEEFAB10C92D becomes [ * 0 => 0xDEADBEEF, * 1 => 0xAB10C92D * ]. * * @internal You should not use this directly from another application * * @param string $in * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function sipHash24($in, $key) { $inlen = self::strlen($in); # /* "somepseudorandomlygeneratedbytes" */ # u64 v0 = 0x736f6d6570736575ULL; # u64 v1 = 0x646f72616e646f6dULL; # u64 v2 = 0x6c7967656e657261ULL; # u64 v3 = 0x7465646279746573ULL; $v = array( 0x736f6d65, // 0 0x70736575, // 1 0x646f7261, // 2 0x6e646f6d, // 3 0x6c796765, // 4 0x6e657261, // 5 0x74656462, // 6 0x79746573 // 7 ); // v0 => $v[0], $v[1] // v1 => $v[2], $v[3] // v2 => $v[4], $v[5] // v3 => $v[6], $v[7] # u64 k0 = LOAD64_LE( k ); # u64 k1 = LOAD64_LE( k + 8 ); $k = array( self::load_4(self::substr($key, 4, 4)), self::load_4(self::substr($key, 0, 4)), self::load_4(self::substr($key, 12, 4)), self::load_4(self::substr($key, 8, 4)) ); // k0 => $k[0], $k[1] // k1 => $k[2], $k[3] # b = ( ( u64 )inlen ) << 56; $b = array( $inlen << 24, 0 ); // See docblock for why the 0th index gets the higher bits. # v3 ^= k1; $v[6] ^= $k[2]; $v[7] ^= $k[3]; # v2 ^= k0; $v[4] ^= $k[0]; $v[5] ^= $k[1]; # v1 ^= k1; $v[2] ^= $k[2]; $v[3] ^= $k[3]; # v0 ^= k0; $v[0] ^= $k[0]; $v[1] ^= $k[1]; $left = $inlen; # for ( ; in != end; in += 8 ) while ($left >= 8) { # m = LOAD64_LE( in ); $m = array( self::load_4(self::substr($in, 4, 4)), self::load_4(self::substr($in, 0, 4)) ); # v3 ^= m; $v[6] ^= $m[0]; $v[7] ^= $m[1]; # SIPROUND; # SIPROUND; $v = self::sipRound($v); $v = self::sipRound($v); # v0 ^= m; $v[0] ^= $m[0]; $v[1] ^= $m[1]; $in = self::substr($in, 8); $left -= 8; } # switch( left ) # { # case 7: b |= ( ( u64 )in[ 6] ) << 48; # case 6: b |= ( ( u64 )in[ 5] ) << 40; # case 5: b |= ( ( u64 )in[ 4] ) << 32; # case 4: b |= ( ( u64 )in[ 3] ) << 24; # case 3: b |= ( ( u64 )in[ 2] ) << 16; # case 2: b |= ( ( u64 )in[ 1] ) << 8; # case 1: b |= ( ( u64 )in[ 0] ); break; # case 0: break; # } switch ($left) { case 7: $b[0] |= self::chrToInt($in[6]) << 16; case 6: $b[0] |= self::chrToInt($in[5]) << 8; case 5: $b[0] |= self::chrToInt($in[4]); case 4: $b[1] |= self::chrToInt($in[3]) << 24; case 3: $b[1] |= self::chrToInt($in[2]) << 16; case 2: $b[1] |= self::chrToInt($in[1]) << 8; case 1: $b[1] |= self::chrToInt($in[0]); case 0: break; } // See docblock for why the 0th index gets the higher bits. # v3 ^= b; $v[6] ^= $b[0]; $v[7] ^= $b[1]; # SIPROUND; # SIPROUND; $v = self::sipRound($v); $v = self::sipRound($v); # v0 ^= b; $v[0] ^= $b[0]; $v[1] ^= $b[1]; // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation # v2 ^= 0xff; $v[5] ^= 0xff; # SIPROUND; # SIPROUND; # SIPROUND; # SIPROUND; $v = self::sipRound($v); $v = self::sipRound($v); $v = self::sipRound($v); $v = self::sipRound($v); # b = v0 ^ v1 ^ v2 ^ v3; # STORE64_LE( out, b ); return self::store32_le($v[1] ^ $v[3] ^ $v[5] ^ $v[7]) . self::store32_le($v[0] ^ $v[2] ^ $v[4] ^ $v[6]); } } Core/AES.php 0000644 00000037015 15162213710 0006561 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_AES', false)) { return; } /** * Bitsliced implementation of the AES block cipher. * * Based on the implementation provided by BearSSL. * * @internal This should only be used by sodium_compat */ class ParagonIE_Sodium_Core_AES extends ParagonIE_Sodium_Core_Util { /** * @var int[] AES round constants */ private static $Rcon = array( 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36 ); /** * Mutates the values of $q! * * @param ParagonIE_Sodium_Core_AES_Block $q * @return void */ public static function sbox(ParagonIE_Sodium_Core_AES_Block $q) { /** * @var int $x0 * @var int $x1 * @var int $x2 * @var int $x3 * @var int $x4 * @var int $x5 * @var int $x6 * @var int $x7 */ $x0 = $q[7] & self::U32_MAX; $x1 = $q[6] & self::U32_MAX; $x2 = $q[5] & self::U32_MAX; $x3 = $q[4] & self::U32_MAX; $x4 = $q[3] & self::U32_MAX; $x5 = $q[2] & self::U32_MAX; $x6 = $q[1] & self::U32_MAX; $x7 = $q[0] & self::U32_MAX; $y14 = $x3 ^ $x5; $y13 = $x0 ^ $x6; $y9 = $x0 ^ $x3; $y8 = $x0 ^ $x5; $t0 = $x1 ^ $x2; $y1 = $t0 ^ $x7; $y4 = $y1 ^ $x3; $y12 = $y13 ^ $y14; $y2 = $y1 ^ $x0; $y5 = $y1 ^ $x6; $y3 = $y5 ^ $y8; $t1 = $x4 ^ $y12; $y15 = $t1 ^ $x5; $y20 = $t1 ^ $x1; $y6 = $y15 ^ $x7; $y10 = $y15 ^ $t0; $y11 = $y20 ^ $y9; $y7 = $x7 ^ $y11; $y17 = $y10 ^ $y11; $y19 = $y10 ^ $y8; $y16 = $t0 ^ $y11; $y21 = $y13 ^ $y16; $y18 = $x0 ^ $y16; /* * Non-linear section. */ $t2 = $y12 & $y15; $t3 = $y3 & $y6; $t4 = $t3 ^ $t2; $t5 = $y4 & $x7; $t6 = $t5 ^ $t2; $t7 = $y13 & $y16; $t8 = $y5 & $y1; $t9 = $t8 ^ $t7; $t10 = $y2 & $y7; $t11 = $t10 ^ $t7; $t12 = $y9 & $y11; $t13 = $y14 & $y17; $t14 = $t13 ^ $t12; $t15 = $y8 & $y10; $t16 = $t15 ^ $t12; $t17 = $t4 ^ $t14; $t18 = $t6 ^ $t16; $t19 = $t9 ^ $t14; $t20 = $t11 ^ $t16; $t21 = $t17 ^ $y20; $t22 = $t18 ^ $y19; $t23 = $t19 ^ $y21; $t24 = $t20 ^ $y18; $t25 = $t21 ^ $t22; $t26 = $t21 & $t23; $t27 = $t24 ^ $t26; $t28 = $t25 & $t27; $t29 = $t28 ^ $t22; $t30 = $t23 ^ $t24; $t31 = $t22 ^ $t26; $t32 = $t31 & $t30; $t33 = $t32 ^ $t24; $t34 = $t23 ^ $t33; $t35 = $t27 ^ $t33; $t36 = $t24 & $t35; $t37 = $t36 ^ $t34; $t38 = $t27 ^ $t36; $t39 = $t29 & $t38; $t40 = $t25 ^ $t39; $t41 = $t40 ^ $t37; $t42 = $t29 ^ $t33; $t43 = $t29 ^ $t40; $t44 = $t33 ^ $t37; $t45 = $t42 ^ $t41; $z0 = $t44 & $y15; $z1 = $t37 & $y6; $z2 = $t33 & $x7; $z3 = $t43 & $y16; $z4 = $t40 & $y1; $z5 = $t29 & $y7; $z6 = $t42 & $y11; $z7 = $t45 & $y17; $z8 = $t41 & $y10; $z9 = $t44 & $y12; $z10 = $t37 & $y3; $z11 = $t33 & $y4; $z12 = $t43 & $y13; $z13 = $t40 & $y5; $z14 = $t29 & $y2; $z15 = $t42 & $y9; $z16 = $t45 & $y14; $z17 = $t41 & $y8; /* * Bottom linear transformation. */ $t46 = $z15 ^ $z16; $t47 = $z10 ^ $z11; $t48 = $z5 ^ $z13; $t49 = $z9 ^ $z10; $t50 = $z2 ^ $z12; $t51 = $z2 ^ $z5; $t52 = $z7 ^ $z8; $t53 = $z0 ^ $z3; $t54 = $z6 ^ $z7; $t55 = $z16 ^ $z17; $t56 = $z12 ^ $t48; $t57 = $t50 ^ $t53; $t58 = $z4 ^ $t46; $t59 = $z3 ^ $t54; $t60 = $t46 ^ $t57; $t61 = $z14 ^ $t57; $t62 = $t52 ^ $t58; $t63 = $t49 ^ $t58; $t64 = $z4 ^ $t59; $t65 = $t61 ^ $t62; $t66 = $z1 ^ $t63; $s0 = $t59 ^ $t63; $s6 = $t56 ^ ~$t62; $s7 = $t48 ^ ~$t60; $t67 = $t64 ^ $t65; $s3 = $t53 ^ $t66; $s4 = $t51 ^ $t66; $s5 = $t47 ^ $t65; $s1 = $t64 ^ ~$s3; $s2 = $t55 ^ ~$t67; $q[7] = $s0 & self::U32_MAX; $q[6] = $s1 & self::U32_MAX; $q[5] = $s2 & self::U32_MAX; $q[4] = $s3 & self::U32_MAX; $q[3] = $s4 & self::U32_MAX; $q[2] = $s5 & self::U32_MAX; $q[1] = $s6 & self::U32_MAX; $q[0] = $s7 & self::U32_MAX; } /** * Mutates the values of $q! * * @param ParagonIE_Sodium_Core_AES_Block $q * @return void */ public static function invSbox(ParagonIE_Sodium_Core_AES_Block $q) { self::processInversion($q); self::sbox($q); self::processInversion($q); } /** * This is some boilerplate code needed to invert an S-box. Rather than repeat the code * twice, I moved it to a protected method. * * Mutates $q * * @param ParagonIE_Sodium_Core_AES_Block $q * @return void */ protected static function processInversion(ParagonIE_Sodium_Core_AES_Block $q) { $q0 = (~$q[0]) & self::U32_MAX; $q1 = (~$q[1]) & self::U32_MAX; $q2 = $q[2] & self::U32_MAX; $q3 = $q[3] & self::U32_MAX; $q4 = $q[4] & self::U32_MAX; $q5 = (~$q[5]) & self::U32_MAX; $q6 = (~$q[6]) & self::U32_MAX; $q7 = $q[7] & self::U32_MAX; $q[7] = ($q1 ^ $q4 ^ $q6) & self::U32_MAX; $q[6] = ($q0 ^ $q3 ^ $q5) & self::U32_MAX; $q[5] = ($q7 ^ $q2 ^ $q4) & self::U32_MAX; $q[4] = ($q6 ^ $q1 ^ $q3) & self::U32_MAX; $q[3] = ($q5 ^ $q0 ^ $q2) & self::U32_MAX; $q[2] = ($q4 ^ $q7 ^ $q1) & self::U32_MAX; $q[1] = ($q3 ^ $q6 ^ $q0) & self::U32_MAX; $q[0] = ($q2 ^ $q5 ^ $q7) & self::U32_MAX; } /** * @param int $x * @return int */ public static function subWord($x) { $q = ParagonIE_Sodium_Core_AES_Block::fromArray( array($x, $x, $x, $x, $x, $x, $x, $x) ); $q->orthogonalize(); self::sbox($q); $q->orthogonalize(); return $q[0] & self::U32_MAX; } /** * Calculate the key schedule from a given random key * * @param string $key * @return ParagonIE_Sodium_Core_AES_KeySchedule * @throws SodiumException */ public static function keySchedule($key) { $key_len = self::strlen($key); switch ($key_len) { case 16: $num_rounds = 10; break; case 24: $num_rounds = 12; break; case 32: $num_rounds = 14; break; default: throw new SodiumException('Invalid key length: ' . $key_len); } $skey = array(); $comp_skey = array(); $nk = $key_len >> 2; $nkf = ($num_rounds + 1) << 2; $tmp = 0; for ($i = 0; $i < $nk; ++$i) { $tmp = self::load_4(self::substr($key, $i << 2, 4)); $skey[($i << 1)] = $tmp; $skey[($i << 1) + 1] = $tmp; } for ($i = $nk, $j = 0, $k = 0; $i < $nkf; ++$i) { if ($j === 0) { $tmp = (($tmp & 0xff) << 24) | ($tmp >> 8); $tmp = (self::subWord($tmp) ^ self::$Rcon[$k]) & self::U32_MAX; } elseif ($nk > 6 && $j === 4) { $tmp = self::subWord($tmp); } $tmp ^= $skey[($i - $nk) << 1]; $skey[($i << 1)] = $tmp & self::U32_MAX; $skey[($i << 1) + 1] = $tmp & self::U32_MAX; if (++$j === $nk) { /** @psalm-suppress LoopInvalidation */ $j = 0; ++$k; } } for ($i = 0; $i < $nkf; $i += 4) { $q = ParagonIE_Sodium_Core_AES_Block::fromArray( array_slice($skey, $i << 1, 8) ); $q->orthogonalize(); // We have to overwrite $skey since we're not using C pointers like BearSSL did for ($j = 0; $j < 8; ++$j) { $skey[($i << 1) + $j] = $q[$j]; } } for ($i = 0, $j = 0; $i < $nkf; ++$i, $j += 2) { $comp_skey[$i] = ($skey[$j] & 0x55555555) | ($skey[$j + 1] & 0xAAAAAAAA); } return new ParagonIE_Sodium_Core_AES_KeySchedule($comp_skey, $num_rounds); } /** * Mutates $q * * @param ParagonIE_Sodium_Core_AES_KeySchedule $skey * @param ParagonIE_Sodium_Core_AES_Block $q * @param int $offset * @return void */ public static function addRoundKey( ParagonIE_Sodium_Core_AES_Block $q, ParagonIE_Sodium_Core_AES_KeySchedule $skey, $offset = 0 ) { $block = $skey->getRoundKey($offset); for ($j = 0; $j < 8; ++$j) { $q[$j] = ($q[$j] ^ $block[$j]) & ParagonIE_Sodium_Core_Util::U32_MAX; } } /** * This mainly exists for testing, as we need the round key features for AEGIS. * * @param string $message * @param string $key * @return string * @throws SodiumException */ public static function decryptBlockECB($message, $key) { if (self::strlen($message) !== 16) { throw new SodiumException('decryptBlockECB() expects a 16 byte message'); } $skey = self::keySchedule($key)->expand(); $q = ParagonIE_Sodium_Core_AES_Block::init(); $q[0] = self::load_4(self::substr($message, 0, 4)); $q[2] = self::load_4(self::substr($message, 4, 4)); $q[4] = self::load_4(self::substr($message, 8, 4)); $q[6] = self::load_4(self::substr($message, 12, 4)); $q->orthogonalize(); self::bitsliceDecryptBlock($skey, $q); $q->orthogonalize(); return self::store32_le($q[0]) . self::store32_le($q[2]) . self::store32_le($q[4]) . self::store32_le($q[6]); } /** * This mainly exists for testing, as we need the round key features for AEGIS. * * @param string $message * @param string $key * @return string * @throws SodiumException */ public static function encryptBlockECB($message, $key) { if (self::strlen($message) !== 16) { throw new SodiumException('encryptBlockECB() expects a 16 byte message'); } $comp_skey = self::keySchedule($key); $skey = $comp_skey->expand(); $q = ParagonIE_Sodium_Core_AES_Block::init(); $q[0] = self::load_4(self::substr($message, 0, 4)); $q[2] = self::load_4(self::substr($message, 4, 4)); $q[4] = self::load_4(self::substr($message, 8, 4)); $q[6] = self::load_4(self::substr($message, 12, 4)); $q->orthogonalize(); self::bitsliceEncryptBlock($skey, $q); $q->orthogonalize(); return self::store32_le($q[0]) . self::store32_le($q[2]) . self::store32_le($q[4]) . self::store32_le($q[6]); } /** * Mutates $q * * @param ParagonIE_Sodium_Core_AES_Expanded $skey * @param ParagonIE_Sodium_Core_AES_Block $q * @return void */ public static function bitsliceEncryptBlock( ParagonIE_Sodium_Core_AES_Expanded $skey, ParagonIE_Sodium_Core_AES_Block $q ) { self::addRoundKey($q, $skey); for ($u = 1; $u < $skey->getNumRounds(); ++$u) { self::sbox($q); $q->shiftRows(); $q->mixColumns(); self::addRoundKey($q, $skey, ($u << 3)); } self::sbox($q); $q->shiftRows(); self::addRoundKey($q, $skey, ($skey->getNumRounds() << 3)); } /** * @param string $x * @param string $y * @return string */ public static function aesRound($x, $y) { $q = ParagonIE_Sodium_Core_AES_Block::init(); $q[0] = self::load_4(self::substr($x, 0, 4)); $q[2] = self::load_4(self::substr($x, 4, 4)); $q[4] = self::load_4(self::substr($x, 8, 4)); $q[6] = self::load_4(self::substr($x, 12, 4)); $rk = ParagonIE_Sodium_Core_AES_Block::init(); $rk[0] = $rk[1] = self::load_4(self::substr($y, 0, 4)); $rk[2] = $rk[3] = self::load_4(self::substr($y, 4, 4)); $rk[4] = $rk[5] = self::load_4(self::substr($y, 8, 4)); $rk[6] = $rk[7] = self::load_4(self::substr($y, 12, 4)); $q->orthogonalize(); self::sbox($q); $q->shiftRows(); $q->mixColumns(); $q->orthogonalize(); // add round key without key schedule: for ($i = 0; $i < 8; ++$i) { $q[$i] ^= $rk[$i]; } return self::store32_le($q[0]) . self::store32_le($q[2]) . self::store32_le($q[4]) . self::store32_le($q[6]); } /** * Process two AES blocks in one shot. * * @param string $b0 First AES block * @param string $rk0 First round key * @param string $b1 Second AES block * @param string $rk1 Second round key * @return string[] */ public static function doubleRound($b0, $rk0, $b1, $rk1) { $q = ParagonIE_Sodium_Core_AES_Block::init(); // First block $q[0] = self::load_4(self::substr($b0, 0, 4)); $q[2] = self::load_4(self::substr($b0, 4, 4)); $q[4] = self::load_4(self::substr($b0, 8, 4)); $q[6] = self::load_4(self::substr($b0, 12, 4)); // Second block $q[1] = self::load_4(self::substr($b1, 0, 4)); $q[3] = self::load_4(self::substr($b1, 4, 4)); $q[5] = self::load_4(self::substr($b1, 8, 4)); $q[7] = self::load_4(self::substr($b1, 12, 4));; $rk = ParagonIE_Sodium_Core_AES_Block::init(); // First round key $rk[0] = self::load_4(self::substr($rk0, 0, 4)); $rk[2] = self::load_4(self::substr($rk0, 4, 4)); $rk[4] = self::load_4(self::substr($rk0, 8, 4)); $rk[6] = self::load_4(self::substr($rk0, 12, 4)); // Second round key $rk[1] = self::load_4(self::substr($rk1, 0, 4)); $rk[3] = self::load_4(self::substr($rk1, 4, 4)); $rk[5] = self::load_4(self::substr($rk1, 8, 4)); $rk[7] = self::load_4(self::substr($rk1, 12, 4)); $q->orthogonalize(); self::sbox($q); $q->shiftRows(); $q->mixColumns(); $q->orthogonalize(); // add round key without key schedule: for ($i = 0; $i < 8; ++$i) { $q[$i] ^= $rk[$i]; } return array( self::store32_le($q[0]) . self::store32_le($q[2]) . self::store32_le($q[4]) . self::store32_le($q[6]), self::store32_le($q[1]) . self::store32_le($q[3]) . self::store32_le($q[5]) . self::store32_le($q[7]), ); } /** * @param ParagonIE_Sodium_Core_AES_Expanded $skey * @param ParagonIE_Sodium_Core_AES_Block $q * @return void */ public static function bitsliceDecryptBlock( ParagonIE_Sodium_Core_AES_Expanded $skey, ParagonIE_Sodium_Core_AES_Block $q ) { self::addRoundKey($q, $skey, ($skey->getNumRounds() << 3)); for ($u = $skey->getNumRounds() - 1; $u > 0; --$u) { $q->inverseShiftRows(); self::invSbox($q); self::addRoundKey($q, $skey, ($u << 3)); $q->inverseMixColumns(); } $q->inverseShiftRows(); self::invSbox($q); self::addRoundKey($q, $skey, ($u << 3)); } } Core/error_log 0000644 00000117746 15162213711 0007370 0 ustar 00 [24-Mar-2026 20:04:11 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [24-Mar-2026 20:04:11 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php on line 10 [24-Mar-2026 20:05:13 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php on line 10 [24-Mar-2026 20:05:14 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php on line 10 [24-Mar-2026 20:06:04 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php on line 10 [24-Mar-2026 20:06:04 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php on line 10 [24-Mar-2026 20:06:05 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [24-Mar-2026 20:06:30 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php on line 10 [24-Mar-2026 20:07:02 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php on line 10 [24-Mar-2026 20:07:46 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php on line 12 [24-Mar-2026 20:07:47 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php on line 10 [24-Mar-2026 20:07:57 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Ed25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php:6 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php on line 6 [24-Mar-2026 20:09:05 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php on line 10 [24-Mar-2026 20:09:20 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/SipHash.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/SipHash.php on line 12 [24-Mar-2026 20:10:07 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php:14 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php on line 14 [24-Mar-2026 20:10:07 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php on line 10 [25-Mar-2026 02:03:01 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php on line 12 [25-Mar-2026 02:03:09 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php on line 10 [25-Mar-2026 02:24:09 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php on line 10 [25-Mar-2026 02:25:47 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [25-Mar-2026 02:27:50 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [25-Mar-2026 02:28:49 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php on line 10 [25-Mar-2026 02:48:18 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php on line 10 [25-Mar-2026 03:22:41 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Ed25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php:6 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php on line 6 [25-Mar-2026 03:54:21 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php on line 10 [25-Mar-2026 03:54:24 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php on line 10 [25-Mar-2026 03:55:08 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php on line 10 [25-Mar-2026 04:21:23 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php on line 10 [25-Mar-2026 04:26:35 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php on line 10 [25-Mar-2026 04:27:29 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php on line 10 [25-Mar-2026 04:55:53 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php:14 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php on line 14 [25-Mar-2026 06:24:30 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/SipHash.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/SipHash.php on line 12 [25-Mar-2026 11:58:11 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php on line 12 [25-Mar-2026 11:59:12 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php on line 10 [25-Mar-2026 12:00:18 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php on line 10 [25-Mar-2026 12:01:23 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [25-Mar-2026 12:02:27 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [25-Mar-2026 12:03:30 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php on line 10 [25-Mar-2026 12:04:33 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php on line 10 [25-Mar-2026 12:05:34 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Ed25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php:6 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php on line 6 [25-Mar-2026 12:06:38 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php on line 10 [25-Mar-2026 12:07:41 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php on line 10 [25-Mar-2026 12:08:44 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php on line 10 [25-Mar-2026 12:09:45 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php on line 10 [25-Mar-2026 12:10:46 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php on line 10 [25-Mar-2026 12:11:48 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php on line 10 [25-Mar-2026 12:12:51 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php:14 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php on line 14 [26-Mar-2026 07:30:23 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php on line 10 [26-Mar-2026 07:30:25 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php on line 10 [26-Mar-2026 07:30:27 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php:14 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php on line 14 [26-Mar-2026 07:30:28 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php on line 12 [26-Mar-2026 07:30:30 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php on line 10 [26-Mar-2026 07:30:31 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [26-Mar-2026 07:30:33 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [26-Mar-2026 07:30:35 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php on line 10 [26-Mar-2026 07:30:36 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php on line 10 [26-Mar-2026 07:30:38 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php on line 10 [26-Mar-2026 07:30:39 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php on line 10 [26-Mar-2026 07:30:41 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php on line 10 [26-Mar-2026 07:30:43 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php on line 10 [26-Mar-2026 09:07:02 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php:14 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php on line 14 [26-Mar-2026 09:09:04 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php on line 10 [26-Mar-2026 09:09:19 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Ed25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php:6 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php on line 6 [26-Mar-2026 09:09:20 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php on line 10 [26-Mar-2026 09:09:21 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php on line 10 [26-Mar-2026 09:09:24 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/SipHash.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/SipHash.php on line 12 [26-Mar-2026 09:09:38 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php on line 12 [26-Mar-2026 09:09:40 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [26-Mar-2026 09:09:46 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php on line 10 [26-Mar-2026 09:10:22 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php on line 10 [26-Mar-2026 09:10:27 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Ed25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php:6 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php on line 6 [26-Mar-2026 09:20:49 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php on line 10 [26-Mar-2026 09:20:54 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php on line 10 [26-Mar-2026 09:23:23 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php on line 10 [26-Mar-2026 09:26:03 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/SipHash.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/SipHash.php on line 12 [26-Mar-2026 10:45:43 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php on line 10 [26-Mar-2026 10:46:14 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [26-Mar-2026 10:47:28 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php on line 10 [26-Mar-2026 10:47:32 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php on line 10 [26-Mar-2026 12:57:35 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php on line 12 [26-Mar-2026 12:59:04 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php on line 10 [26-Mar-2026 13:24:03 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php on line 10 [26-Mar-2026 13:25:40 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [26-Mar-2026 13:28:43 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [26-Mar-2026 13:29:32 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php on line 10 [26-Mar-2026 13:49:17 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php on line 10 [26-Mar-2026 14:22:20 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Ed25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php:6 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php on line 6 [26-Mar-2026 14:54:05 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php on line 10 [26-Mar-2026 14:54:08 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php on line 10 [26-Mar-2026 14:54:52 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php on line 10 [26-Mar-2026 15:23:03 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php on line 10 [26-Mar-2026 15:28:15 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php on line 10 [26-Mar-2026 15:29:06 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php on line 10 [26-Mar-2026 15:57:01 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php:14 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php on line 14 [26-Mar-2026 17:26:03 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/SipHash.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/SipHash.php on line 12 [26-Mar-2026 22:21:35 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php on line 12 [26-Mar-2026 22:22:15 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php on line 10 [26-Mar-2026 22:23:17 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php on line 10 [26-Mar-2026 22:24:20 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [26-Mar-2026 22:25:25 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [26-Mar-2026 22:26:26 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php on line 10 [26-Mar-2026 22:27:31 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php on line 10 [26-Mar-2026 22:28:33 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Ed25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php:6 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php on line 6 [26-Mar-2026 22:29:34 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php on line 10 [26-Mar-2026 22:30:40 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php on line 10 [26-Mar-2026 22:31:41 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php on line 10 [26-Mar-2026 22:32:43 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php on line 10 [26-Mar-2026 22:33:44 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php on line 10 [26-Mar-2026 22:34:48 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php on line 10 [26-Mar-2026 22:35:50 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php:14 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php on line 14 [28-Mar-2026 09:17:32 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php:14 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES.php on line 14 [28-Mar-2026 09:18:53 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [28-Mar-2026 09:20:34 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/X25519.php on line 10 [28-Mar-2026 09:21:16 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php on line 10 [28-Mar-2026 09:24:09 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Salsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HSalsa20.php on line 10 [28-Mar-2026 09:24:11 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS256.php on line 10 [28-Mar-2026 09:31:22 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/SipHash.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/SipHash.php on line 12 [28-Mar-2026 09:33:14 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HSalsa20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XSalsa20.php on line 10 [28-Mar-2026 09:33:46 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [28-Mar-2026 09:34:15 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/HChaCha20.php on line 10 [28-Mar-2026 09:34:21 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_HChaCha20" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/XChaCha20.php on line 10 [28-Mar-2026 09:34:51 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/BLAKE2b.php on line 12 [28-Mar-2026 09:34:54 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Ed25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php:6 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php on line 6 [28-Mar-2026 09:40:00 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Salsa20.php on line 10 [28-Mar-2026 09:52:43 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20.php on line 10 [28-Mar-2026 11:00:26 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305.php on line 10 [28-Mar-2026 13:36:03 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Curve25519_H" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php:16 Stack trace: #0 /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ed25519.php(7): require_once() #1 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519.php on line 16 [30-Mar-2026 09:01:00 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AEGIS128L.php on line 10 [30-Mar-2026 09:01:02 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Ed25519" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php:6 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Ristretto255.php on line 6 Core/Base64/Original.php 0000644 00000020434 15162213713 0010741 0 ustar 00 <?php /** * Class ParagonIE_Sodium_Core_Base64 * * Copyright (c) 2016 - 2018 Paragon Initiative Enterprises. * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) */ class ParagonIE_Sodium_Core_Base64_Original { // COPY ParagonIE_Sodium_Core_Base64_Common STARTING HERE /** * Encode into Base64 * * Base64 character set "[A-Z][a-z][0-9]+/" * * @param string $src * @return string * @throws TypeError */ public static function encode($src) { return self::doEncode($src, true); } /** * Encode into Base64, no = padding * * Base64 character set "[A-Z][a-z][0-9]+/" * * @param string $src * @return string * @throws TypeError */ public static function encodeUnpadded($src) { return self::doEncode($src, false); } /** * @param string $src * @param bool $pad Include = padding? * @return string * @throws TypeError */ protected static function doEncode($src, $pad = true) { $dest = ''; $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); // Main loop (no padding): for ($i = 0; $i + 3 <= $srcLen; $i += 3) { /** @var array<int, int> $chunk */ $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 3)); $b0 = $chunk[1]; $b1 = $chunk[2]; $b2 = $chunk[3]; $dest .= self::encode6Bits( $b0 >> 2 ) . self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . self::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) . self::encode6Bits( $b2 & 63); } // The last chunk, which may have padding: if ($i < $srcLen) { /** @var array<int, int> $chunk */ $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i)); $b0 = $chunk[1]; if ($i + 1 < $srcLen) { $b1 = $chunk[2]; $dest .= self::encode6Bits($b0 >> 2) . self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . self::encode6Bits(($b1 << 2) & 63); if ($pad) { $dest .= '='; } } else { $dest .= self::encode6Bits( $b0 >> 2) . self::encode6Bits(($b0 << 4) & 63); if ($pad) { $dest .= '=='; } } } return $dest; } /** * decode from base64 into binary * * Base64 character set "./[A-Z][a-z][0-9]" * * @param string $src * @param bool $strictPadding * @return string * @throws RangeException * @throws TypeError * @psalm-suppress RedundantCondition */ public static function decode($src, $strictPadding = false) { // Remove padding $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); if ($srcLen === 0) { return ''; } if ($strictPadding) { if (($srcLen & 3) === 0) { if ($src[$srcLen - 1] === '=') { $srcLen--; if ($src[$srcLen - 1] === '=') { $srcLen--; } } } if (($srcLen & 3) === 1) { throw new RangeException( 'Incorrect padding' ); } if ($src[$srcLen - 1] === '=') { throw new RangeException( 'Incorrect padding' ); } } else { $src = rtrim($src, '='); $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); } $err = 0; $dest = ''; // Main loop (no padding): for ($i = 0; $i + 4 <= $srcLen; $i += 4) { /** @var array<int, int> $chunk */ $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 4)); $c0 = self::decode6Bits($chunk[1]); $c1 = self::decode6Bits($chunk[2]); $c2 = self::decode6Bits($chunk[3]); $c3 = self::decode6Bits($chunk[4]); $dest .= pack( 'CCC', ((($c0 << 2) | ($c1 >> 4)) & 0xff), ((($c1 << 4) | ($c2 >> 2)) & 0xff), ((($c2 << 6) | $c3) & 0xff) ); $err |= ($c0 | $c1 | $c2 | $c3) >> 8; } // The last chunk, which may have padding: if ($i < $srcLen) { /** @var array<int, int> $chunk */ $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i)); $c0 = self::decode6Bits($chunk[1]); if ($i + 2 < $srcLen) { $c1 = self::decode6Bits($chunk[2]); $c2 = self::decode6Bits($chunk[3]); $dest .= pack( 'CC', ((($c0 << 2) | ($c1 >> 4)) & 0xff), ((($c1 << 4) | ($c2 >> 2)) & 0xff) ); $err |= ($c0 | $c1 | $c2) >> 8; } elseif ($i + 1 < $srcLen) { $c1 = self::decode6Bits($chunk[2]); $dest .= pack( 'C', ((($c0 << 2) | ($c1 >> 4)) & 0xff) ); $err |= ($c0 | $c1) >> 8; } elseif ($i < $srcLen && $strictPadding) { $err |= 1; } } /** @var bool $check */ $check = ($err === 0); if (!$check) { throw new RangeException( 'Base64::decode() only expects characters in the correct base64 alphabet' ); } return $dest; } /** * @param string $encodedString * @return string */ public static function decodeNoPadding( #[SensitiveParameter] $encodedString ) { $srcLen = strlen($encodedString); if ($srcLen === 0) { return ''; } if (($srcLen & 3) === 0) { // If $strLen is not zero, and it is divisible by 4, then it's at least 4. if ($encodedString[$srcLen - 1] === '=' || $encodedString[$srcLen - 2] === '=') { throw new InvalidArgumentException( "decodeNoPadding() doesn't tolerate padding" ); } } return self::decode( $encodedString, true ); } // COPY ParagonIE_Sodium_Core_Base64_Common ENDING HERE /** * Uses bitwise operators instead of table-lookups to turn 6-bit integers * into 8-bit integers. * * Base64 character set: * [A-Z] [a-z] [0-9] + / * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f * * @param int $src * @return int */ protected static function decode6Bits($src) { $ret = -1; // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64 $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64); // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70 $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70); // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5 $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5); // if ($src == 0x2b) $ret += 62 + 1; $ret += (((0x2a - $src) & ($src - 0x2c)) >> 8) & 63; // if ($src == 0x2f) ret += 63 + 1; $ret += (((0x2e - $src) & ($src - 0x30)) >> 8) & 64; return $ret; } /** * Uses bitwise operators instead of table-lookups to turn 8-bit integers * into 6-bit integers. * * @param int $src * @return string */ protected static function encode6Bits($src) { $diff = 0x41; // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6 $diff += ((25 - $src) >> 8) & 6; // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75 $diff -= ((51 - $src) >> 8) & 75; // if ($src > 61) $diff += 0x2b - 0x30 - 10; // -15 $diff -= ((61 - $src) >> 8) & 15; // if ($src > 62) $diff += 0x2f - 0x2b - 1; // 3 $diff += ((62 - $src) >> 8) & 3; return pack('C', $src + $diff); } } Core/Base64/UrlSafe.php 0000644 00000020443 15162213714 0010537 0 ustar 00 <?php /** * Class ParagonIE_Sodium_Core_Base64UrlSafe * * Copyright (c) 2016 - 2018 Paragon Initiative Enterprises. * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) */ class ParagonIE_Sodium_Core_Base64_UrlSafe { // COPY ParagonIE_Sodium_Core_Base64_Common STARTING HERE /** * Encode into Base64 * * Base64 character set "[A-Z][a-z][0-9]+/" * * @param string $src * @return string * @throws TypeError */ public static function encode($src) { return self::doEncode($src, true); } /** * Encode into Base64, no = padding * * Base64 character set "[A-Z][a-z][0-9]+/" * * @param string $src * @return string * @throws TypeError */ public static function encodeUnpadded($src) { return self::doEncode($src, false); } /** * @param string $src * @param bool $pad Include = padding? * @return string * @throws TypeError */ protected static function doEncode($src, $pad = true) { $dest = ''; $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); // Main loop (no padding): for ($i = 0; $i + 3 <= $srcLen; $i += 3) { /** @var array<int, int> $chunk */ $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 3)); $b0 = $chunk[1]; $b1 = $chunk[2]; $b2 = $chunk[3]; $dest .= self::encode6Bits( $b0 >> 2 ) . self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . self::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) . self::encode6Bits( $b2 & 63); } // The last chunk, which may have padding: if ($i < $srcLen) { /** @var array<int, int> $chunk */ $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i)); $b0 = $chunk[1]; if ($i + 1 < $srcLen) { $b1 = $chunk[2]; $dest .= self::encode6Bits($b0 >> 2) . self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . self::encode6Bits(($b1 << 2) & 63); if ($pad) { $dest .= '='; } } else { $dest .= self::encode6Bits( $b0 >> 2) . self::encode6Bits(($b0 << 4) & 63); if ($pad) { $dest .= '=='; } } } return $dest; } /** * decode from base64 into binary * * Base64 character set "./[A-Z][a-z][0-9]" * * @param string $src * @param bool $strictPadding * @return string * @throws RangeException * @throws TypeError * @psalm-suppress RedundantCondition */ public static function decode($src, $strictPadding = false) { // Remove padding $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); if ($srcLen === 0) { return ''; } if ($strictPadding) { if (($srcLen & 3) === 0) { if ($src[$srcLen - 1] === '=') { $srcLen--; if ($src[$srcLen - 1] === '=') { $srcLen--; } } } if (($srcLen & 3) === 1) { throw new RangeException( 'Incorrect padding' ); } if ($src[$srcLen - 1] === '=') { throw new RangeException( 'Incorrect padding' ); } } else { $src = rtrim($src, '='); $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); } $err = 0; $dest = ''; // Main loop (no padding): for ($i = 0; $i + 4 <= $srcLen; $i += 4) { /** @var array<int, int> $chunk */ $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 4)); $c0 = self::decode6Bits($chunk[1]); $c1 = self::decode6Bits($chunk[2]); $c2 = self::decode6Bits($chunk[3]); $c3 = self::decode6Bits($chunk[4]); $dest .= pack( 'CCC', ((($c0 << 2) | ($c1 >> 4)) & 0xff), ((($c1 << 4) | ($c2 >> 2)) & 0xff), ((($c2 << 6) | $c3) & 0xff) ); $err |= ($c0 | $c1 | $c2 | $c3) >> 8; } // The last chunk, which may have padding: if ($i < $srcLen) { /** @var array<int, int> $chunk */ $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i)); $c0 = self::decode6Bits($chunk[1]); if ($i + 2 < $srcLen) { $c1 = self::decode6Bits($chunk[2]); $c2 = self::decode6Bits($chunk[3]); $dest .= pack( 'CC', ((($c0 << 2) | ($c1 >> 4)) & 0xff), ((($c1 << 4) | ($c2 >> 2)) & 0xff) ); $err |= ($c0 | $c1 | $c2) >> 8; } elseif ($i + 1 < $srcLen) { $c1 = self::decode6Bits($chunk[2]); $dest .= pack( 'C', ((($c0 << 2) | ($c1 >> 4)) & 0xff) ); $err |= ($c0 | $c1) >> 8; } elseif ($i < $srcLen && $strictPadding) { $err |= 1; } } /** @var bool $check */ $check = ($err === 0); if (!$check) { throw new RangeException( 'Base64::decode() only expects characters in the correct base64 alphabet' ); } return $dest; } /** * @param string $encodedString * @return string */ public static function decodeNoPadding( #[SensitiveParameter] $encodedString ) { $srcLen = strlen($encodedString); if ($srcLen === 0) { return ''; } if (($srcLen & 3) === 0) { // If $strLen is not zero, and it is divisible by 4, then it's at least 4. if ($encodedString[$srcLen - 1] === '=' || $encodedString[$srcLen - 2] === '=') { throw new InvalidArgumentException( "decodeNoPadding() doesn't tolerate padding" ); } } return self::decode( $encodedString, true ); } // COPY ParagonIE_Sodium_Core_Base64_Common ENDING HERE /** * Uses bitwise operators instead of table-lookups to turn 6-bit integers * into 8-bit integers. * * Base64 character set: * [A-Z] [a-z] [0-9] + / * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f * * @param int $src * @return int */ protected static function decode6Bits($src) { $ret = -1; // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64 $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64); // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70 $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70); // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5 $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5); // if ($src == 0x2c) $ret += 62 + 1; $ret += (((0x2c - $src) & ($src - 0x2e)) >> 8) & 63; // if ($src == 0x5f) ret += 63 + 1; $ret += (((0x5e - $src) & ($src - 0x60)) >> 8) & 64; return $ret; } /** * Uses bitwise operators instead of table-lookups to turn 8-bit integers * into 6-bit integers. * * @param int $src * @return string */ protected static function encode6Bits($src) { $diff = 0x41; // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6 $diff += ((25 - $src) >> 8) & 6; // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75 $diff -= ((51 - $src) >> 8) & 75; // if ($src > 61) $diff += 0x2d - 0x30 - 10; // -13 $diff -= ((61 - $src) >> 8) & 13; // if ($src > 62) $diff += 0x5f - 0x2b - 1; // 3 $diff += ((62 - $src) >> 8) & 49; return pack('C', $src + $diff); } } Core/XChaCha20.php 0000644 00000006370 15162213715 0007557 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_XChaCha20', false)) { return; } /** * Class ParagonIE_Sodium_Core_XChaCha20 */ class ParagonIE_Sodium_Core_XChaCha20 extends ParagonIE_Sodium_Core_HChaCha20 { /** * @internal You should not use this directly from another application * * @param int $len * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function stream($len, $nonce, $key) { if (self::strlen($nonce) !== 24) { throw new SodiumException('Nonce must be 24 bytes long'); } return self::encryptBytes( new ParagonIE_Sodium_Core_ChaCha20_Ctx( self::hChaCha20( self::substr($nonce, 0, 16), $key ), self::substr($nonce, 16, 8) ), str_repeat("\x00", $len) ); } /** * @internal You should not use this directly from another application * * @param int $len * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function ietfStream($len, $nonce, $key) { if (self::strlen($nonce) !== 24) { throw new SodiumException('Nonce must be 24 bytes long'); } return self::encryptBytes( new ParagonIE_Sodium_Core_ChaCha20_IetfCtx( self::hChaCha20( self::substr($nonce, 0, 16), $key ), "\x00\x00\x00\x00" . self::substr($nonce, 16, 8) ), str_repeat("\x00", $len) ); } /** * @internal You should not use this directly from another application * * @param string $message * @param string $nonce * @param string $key * @param string $ic * @return string * @throws SodiumException * @throws TypeError */ public static function streamXorIc($message, $nonce, $key, $ic = '') { if (self::strlen($nonce) !== 24) { throw new SodiumException('Nonce must be 24 bytes long'); } return self::encryptBytes( new ParagonIE_Sodium_Core_ChaCha20_Ctx( self::hChaCha20(self::substr($nonce, 0, 16), $key), self::substr($nonce, 16, 8), $ic ), $message ); } /** * @internal You should not use this directly from another application * * @param string $message * @param string $nonce * @param string $key * @param string $ic * @return string * @throws SodiumException * @throws TypeError */ public static function ietfStreamXorIc($message, $nonce, $key, $ic = '') { if (self::strlen($nonce) !== 24) { throw new SodiumException('Nonce must be 24 bytes long'); } return self::encryptBytes( new ParagonIE_Sodium_Core_ChaCha20_IetfCtx( self::hChaCha20(self::substr($nonce, 0, 16), $key), "\x00\x00\x00\x00" . self::substr($nonce, 16, 8), $ic ), $message ); } } Core/AEGIS128L.php 0000644 00000007124 15162213716 0007354 0 ustar 00 <?php if (!defined('SODIUM_COMPAT_AEGIS_C0')) { define('SODIUM_COMPAT_AEGIS_C0', "\x00\x01\x01\x02\x03\x05\x08\x0d\x15\x22\x37\x59\x90\xe9\x79\x62"); } if (!defined('SODIUM_COMPAT_AEGIS_C1')) { define('SODIUM_COMPAT_AEGIS_C1', "\xdb\x3d\x18\x55\x6d\xc2\x2f\xf1\x20\x11\x31\x42\x73\xb5\x28\xdd"); } class ParagonIE_Sodium_Core_AEGIS128L extends ParagonIE_Sodium_Core_AES { /** * @param string $ct * @param string $tag * @param string $ad * @param string $key * @param string $nonce * @return string * @throws SodiumException */ public static function decrypt($ct, $tag, $ad, $key, $nonce) { $state = self::init($key, $nonce); $ad_blocks = (self::strlen($ad) + 31) >> 5; for ($i = 0; $i < $ad_blocks; ++$i) { $ai = self::substr($ad, $i << 5, 32); if (self::strlen($ai) < 32) { $ai = str_pad($ai, 32, "\0", STR_PAD_RIGHT); } $state->absorb($ai); } $msg = ''; $cn = self::strlen($ct) & 31; $ct_blocks = self::strlen($ct) >> 5; for ($i = 0; $i < $ct_blocks; ++$i) { $msg .= $state->dec(self::substr($ct, $i << 5, 32)); } if ($cn) { $start = $ct_blocks << 5; $msg .= $state->decPartial(self::substr($ct, $start, $cn)); } $expected_tag = $state->finalize( self::strlen($ad) << 3, self::strlen($msg) << 3 ); if (!self::hashEquals($expected_tag, $tag)) { try { // The RFC says to erase msg, so we shall try: ParagonIE_Sodium_Compat::memzero($msg); } catch (SodiumException $ex) { // Do nothing if we cannot memzero } throw new SodiumException('verification failed'); } return $msg; } /** * @param string $msg * @param string $ad * @param string $key * @param string $nonce * @return array * * @throws SodiumException */ public static function encrypt($msg, $ad, $key, $nonce) { $state = self::init($key, $nonce); // ad_blocks = Split(ZeroPad(ad, 256), 256) // for ai in ad_blocks: // Absorb(ai) $ad_len = self::strlen($ad); $msg_len = self::strlen($msg); $ad_blocks = ($ad_len + 31) >> 5; for ($i = 0; $i < $ad_blocks; ++$i) { $ai = self::substr($ad, $i << 5, 32); if (self::strlen($ai) < 32) { $ai = str_pad($ai, 32, "\0", STR_PAD_RIGHT); } $state->absorb($ai); } // msg_blocks = Split(ZeroPad(msg, 256), 256) // for xi in msg_blocks: // ct = ct || Enc(xi) $ct = ''; $msg_blocks = ($msg_len + 31) >> 5; for ($i = 0; $i < $msg_blocks; ++$i) { $xi = self::substr($msg, $i << 5, 32); if (self::strlen($xi) < 32) { $xi = str_pad($xi, 32, "\0", STR_PAD_RIGHT); } $ct .= $state->enc($xi); } // tag = Finalize(|ad|, |msg|) // ct = Truncate(ct, |msg|) $tag = $state->finalize( $ad_len << 3, $msg_len << 3 ); // return ct and tag return array( self::substr($ct, 0, $msg_len), $tag ); } /** * @param string $key * @param string $nonce * @return ParagonIE_Sodium_Core_AEGIS_State128L */ public static function init($key, $nonce) { return ParagonIE_Sodium_Core_AEGIS_State128L::init($key, $nonce); } } Core/Curve25519/error_log 0000644 00000003550 15162213720 0011045 0 ustar 00 [24-Mar-2026 21:56:52 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519/H.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519/H.php on line 12 [25-Mar-2026 02:35:46 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519/H.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519/H.php on line 12 [26-Mar-2026 09:10:53 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519/H.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519/H.php on line 12 [26-Mar-2026 13:36:49 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519/H.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519/H.php on line 12 [28-Mar-2026 09:35:55 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519/H.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519/H.php on line 12 [30-Mar-2026 09:00:25 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519/H.php:12 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Curve25519/H.php on line 12 Core/Curve25519/Ge/Precomp.php 0000644 00000003562 15162213721 0011605 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_Precomp', false)) { return; } /** * Class ParagonIE_Sodium_Core_Curve25519_Ge_Precomp */ class ParagonIE_Sodium_Core_Curve25519_Ge_Precomp { /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $yplusx; /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $yminusx; /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $xy2d; /** * ParagonIE_Sodium_Core_Curve25519_Ge_Precomp constructor. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $yplusx * @param ParagonIE_Sodium_Core_Curve25519_Fe $yminusx * @param ParagonIE_Sodium_Core_Curve25519_Fe $xy2d */ public function __construct( $yplusx = null, $yminusx = null, $xy2d = null ) { if ($yplusx === null) { $yplusx = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($yplusx instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 1 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->yplusx = $yplusx; if ($yminusx === null) { $yminusx = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($yminusx instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 2 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->yminusx = $yminusx; if ($xy2d === null) { $xy2d = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($xy2d instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 3 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->xy2d = $xy2d; } } Core/Curve25519/Ge/Cached.php 0000644 00000004502 15162213721 0011342 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_Cached', false)) { return; } /** * Class ParagonIE_Sodium_Core_Curve25519_Ge_Cached */ class ParagonIE_Sodium_Core_Curve25519_Ge_Cached { /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $YplusX; /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $YminusX; /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $Z; /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $T2d; /** * ParagonIE_Sodium_Core_Curve25519_Ge_Cached constructor. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $YplusX * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $YminusX * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $Z * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $T2d */ public function __construct( $YplusX = null, $YminusX = null, $Z = null, $T2d = null ) { if ($YplusX === null) { $YplusX = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($YplusX instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 1 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->YplusX = $YplusX; if ($YminusX === null) { $YminusX = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($YminusX instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 2 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->YminusX = $YminusX; if ($Z === null) { $Z = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($Z instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 3 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->Z = $Z; if ($T2d === null) { $T2d = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($T2d instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 4 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->T2d = $T2d; } } Core/Curve25519/Ge/P1p1.php 0000644 00000004321 15162213721 0010713 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P1p1', false)) { return; } /** * Class ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 */ class ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 { /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $X; /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $Y; /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $Z; /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $T; /** * ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 constructor. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $x * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $y * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $z * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $t */ public function __construct( $x = null, $y = null, $z = null, $t = null ) { if ($x === null) { $x = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($x instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 1 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->X = $x; if ($y === null) { $y = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($y instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 2 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->Y = $y; if ($z === null) { $z = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($z instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 3 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->Z = $z; if ($t === null) { $t = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($t instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 4 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->T = $t; } } Core/Curve25519/Ge/P3.php 0000644 00000004312 15162213721 0010454 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P3', false)) { return; } /** * Class ParagonIE_Sodium_Core_Curve25519_Ge_P3 */ class ParagonIE_Sodium_Core_Curve25519_Ge_P3 { /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $X; /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $Y; /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $Z; /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $T; /** * ParagonIE_Sodium_Core_Curve25519_Ge_P3 constructor. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $x * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $y * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $z * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $t */ public function __construct( $x = null, $y = null, $z = null, $t = null ) { if ($x === null) { $x = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($x instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 1 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->X = $x; if ($y === null) { $y = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($y instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 2 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->Y = $y; if ($z === null) { $z = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($z instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 3 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->Z = $z; if ($t === null) { $t = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($t instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 4 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->T = $t; } } Core/Curve25519/Ge/P2.php 0000644 00000003375 15162213722 0010464 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P2', false)) { return; } /** * Class ParagonIE_Sodium_Core_Curve25519_Ge_P2 */ class ParagonIE_Sodium_Core_Curve25519_Ge_P2 { /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $X; /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $Y; /** * @var ParagonIE_Sodium_Core_Curve25519_Fe */ public $Z; /** * ParagonIE_Sodium_Core_Curve25519_Ge_P2 constructor. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $x * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $y * @param ParagonIE_Sodium_Core_Curve25519_Fe|null $z */ public function __construct( $x = null, $y = null, $z = null ) { if ($x === null) { $x = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($x instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 1 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->X = $x; if ($y === null) { $y = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($y instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 2 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->Y = $y; if ($z === null) { $z = new ParagonIE_Sodium_Core_Curve25519_Fe(); } if (!($z instanceof ParagonIE_Sodium_Core_Curve25519_Fe)) { throw new TypeError('Argument 3 must be an instance of ParagonIE_Sodium_Core_Curve25519_Fe'); } $this->Z = $z; } } Core/Curve25519/H.php 0000644 00000327571 15162213723 0010047 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_Curve25519_H', false)) { return; } /** * Class ParagonIE_Sodium_Core_Curve25519_H * * This just contains the constants in the ref10/base.h file */ class ParagonIE_Sodium_Core_Curve25519_H extends ParagonIE_Sodium_Core_Util { /** * See: libsodium's crypto_core/curve25519/ref10/base.h * * @var array<int, array<int, array<int, array<int, int>>>> Basically, int[32][8][3][10] */ protected static $base = array( array( array( array(25967493, -14356035, 29566456, 3660896, -12694345, 4014787, 27544626, -11754271, -6079156, 2047605), array(-12545711, 934262, -2722910, 3049990, -727428, 9406986, 12720692, 5043384, 19500929, -15469378), array(-8738181, 4489570, 9688441, -14785194, 10184609, -12363380, 29287919, 11864899, -24514362, -4438546), ), array( array(-12815894, -12976347, -21581243, 11784320, -25355658, -2750717, -11717903, -3814571, -358445, -10211303), array(-21703237, 6903825, 27185491, 6451973, -29577724, -9554005, -15616551, 11189268, -26829678, -5319081), array(26966642, 11152617, 32442495, 15396054, 14353839, -12752335, -3128826, -9541118, -15472047, -4166697), ), array( array(15636291, -9688557, 24204773, -7912398, 616977, -16685262, 27787600, -14772189, 28944400, -1550024), array(16568933, 4717097, -11556148, -1102322, 15682896, -11807043, 16354577, -11775962, 7689662, 11199574), array(30464156, -5976125, -11779434, -15670865, 23220365, 15915852, 7512774, 10017326, -17749093, -9920357), ), array( array(-17036878, 13921892, 10945806, -6033431, 27105052, -16084379, -28926210, 15006023, 3284568, -6276540), array(23599295, -8306047, -11193664, -7687416, 13236774, 10506355, 7464579, 9656445, 13059162, 10374397), array(7798556, 16710257, 3033922, 2874086, 28997861, 2835604, 32406664, -3839045, -641708, -101325), ), array( array(10861363, 11473154, 27284546, 1981175, -30064349, 12577861, 32867885, 14515107, -15438304, 10819380), array(4708026, 6336745, 20377586, 9066809, -11272109, 6594696, -25653668, 12483688, -12668491, 5581306), array(19563160, 16186464, -29386857, 4097519, 10237984, -4348115, 28542350, 13850243, -23678021, -15815942), ), array( array(-15371964, -12862754, 32573250, 4720197, -26436522, 5875511, -19188627, -15224819, -9818940, -12085777), array(-8549212, 109983, 15149363, 2178705, 22900618, 4543417, 3044240, -15689887, 1762328, 14866737), array(-18199695, -15951423, -10473290, 1707278, -17185920, 3916101, -28236412, 3959421, 27914454, 4383652), ), array( array(5153746, 9909285, 1723747, -2777874, 30523605, 5516873, 19480852, 5230134, -23952439, -15175766), array(-30269007, -3463509, 7665486, 10083793, 28475525, 1649722, 20654025, 16520125, 30598449, 7715701), array(28881845, 14381568, 9657904, 3680757, -20181635, 7843316, -31400660, 1370708, 29794553, -1409300), ), array( array(14499471, -2729599, -33191113, -4254652, 28494862, 14271267, 30290735, 10876454, -33154098, 2381726), array(-7195431, -2655363, -14730155, 462251, -27724326, 3941372, -6236617, 3696005, -32300832, 15351955), array(27431194, 8222322, 16448760, -3907995, -18707002, 11938355, -32961401, -2970515, 29551813, 10109425), ), ), array( array( array(-13657040, -13155431, -31283750, 11777098, 21447386, 6519384, -2378284, -1627556, 10092783, -4764171), array(27939166, 14210322, 4677035, 16277044, -22964462, -12398139, -32508754, 12005538, -17810127, 12803510), array(17228999, -15661624, -1233527, 300140, -1224870, -11714777, 30364213, -9038194, 18016357, 4397660), ), array( array(-10958843, -7690207, 4776341, -14954238, 27850028, -15602212, -26619106, 14544525, -17477504, 982639), array(29253598, 15796703, -2863982, -9908884, 10057023, 3163536, 7332899, -4120128, -21047696, 9934963), array(5793303, 16271923, -24131614, -10116404, 29188560, 1206517, -14747930, 4559895, -30123922, -10897950), ), array( array(-27643952, -11493006, 16282657, -11036493, 28414021, -15012264, 24191034, 4541697, -13338309, 5500568), array(12650548, -1497113, 9052871, 11355358, -17680037, -8400164, -17430592, 12264343, 10874051, 13524335), array(25556948, -3045990, 714651, 2510400, 23394682, -10415330, 33119038, 5080568, -22528059, 5376628), ), array( array(-26088264, -4011052, -17013699, -3537628, -6726793, 1920897, -22321305, -9447443, 4535768, 1569007), array(-2255422, 14606630, -21692440, -8039818, 28430649, 8775819, -30494562, 3044290, 31848280, 12543772), array(-22028579, 2943893, -31857513, 6777306, 13784462, -4292203, -27377195, -2062731, 7718482, 14474653), ), array( array(2385315, 2454213, -22631320, 46603, -4437935, -15680415, 656965, -7236665, 24316168, -5253567), array(13741529, 10911568, -33233417, -8603737, -20177830, -1033297, 33040651, -13424532, -20729456, 8321686), array(21060490, -2212744, 15712757, -4336099, 1639040, 10656336, 23845965, -11874838, -9984458, 608372), ), array( array(-13672732, -15087586, -10889693, -7557059, -6036909, 11305547, 1123968, -6780577, 27229399, 23887), array(-23244140, -294205, -11744728, 14712571, -29465699, -2029617, 12797024, -6440308, -1633405, 16678954), array(-29500620, 4770662, -16054387, 14001338, 7830047, 9564805, -1508144, -4795045, -17169265, 4904953), ), array( array(24059557, 14617003, 19037157, -15039908, 19766093, -14906429, 5169211, 16191880, 2128236, -4326833), array(-16981152, 4124966, -8540610, -10653797, 30336522, -14105247, -29806336, 916033, -6882542, -2986532), array(-22630907, 12419372, -7134229, -7473371, -16478904, 16739175, 285431, 2763829, 15736322, 4143876), ), array( array(2379352, 11839345, -4110402, -5988665, 11274298, 794957, 212801, -14594663, 23527084, -16458268), array(33431127, -11130478, -17838966, -15626900, 8909499, 8376530, -32625340, 4087881, -15188911, -14416214), array(1767683, 7197987, -13205226, -2022635, -13091350, 448826, 5799055, 4357868, -4774191, -16323038), ), ), array( array( array(6721966, 13833823, -23523388, -1551314, 26354293, -11863321, 23365147, -3949732, 7390890, 2759800), array(4409041, 2052381, 23373853, 10530217, 7676779, -12885954, 21302353, -4264057, 1244380, -12919645), array(-4421239, 7169619, 4982368, -2957590, 30256825, -2777540, 14086413, 9208236, 15886429, 16489664), ), array( array(1996075, 10375649, 14346367, 13311202, -6874135, -16438411, -13693198, 398369, -30606455, -712933), array(-25307465, 9795880, -2777414, 14878809, -33531835, 14780363, 13348553, 12076947, -30836462, 5113182), array(-17770784, 11797796, 31950843, 13929123, -25888302, 12288344, -30341101, -7336386, 13847711, 5387222), ), array( array(-18582163, -3416217, 17824843, -2340966, 22744343, -10442611, 8763061, 3617786, -19600662, 10370991), array(20246567, -14369378, 22358229, -543712, 18507283, -10413996, 14554437, -8746092, 32232924, 16763880), array(9648505, 10094563, 26416693, 14745928, -30374318, -6472621, 11094161, 15689506, 3140038, -16510092), ), array( array(-16160072, 5472695, 31895588, 4744994, 8823515, 10365685, -27224800, 9448613, -28774454, 366295), array(19153450, 11523972, -11096490, -6503142, -24647631, 5420647, 28344573, 8041113, 719605, 11671788), array(8678025, 2694440, -6808014, 2517372, 4964326, 11152271, -15432916, -15266516, 27000813, -10195553), ), array( array(-15157904, 7134312, 8639287, -2814877, -7235688, 10421742, 564065, 5336097, 6750977, -14521026), array(11836410, -3979488, 26297894, 16080799, 23455045, 15735944, 1695823, -8819122, 8169720, 16220347), array(-18115838, 8653647, 17578566, -6092619, -8025777, -16012763, -11144307, -2627664, -5990708, -14166033), ), array( array(-23308498, -10968312, 15213228, -10081214, -30853605, -11050004, 27884329, 2847284, 2655861, 1738395), array(-27537433, -14253021, -25336301, -8002780, -9370762, 8129821, 21651608, -3239336, -19087449, -11005278), array(1533110, 3437855, 23735889, 459276, 29970501, 11335377, 26030092, 5821408, 10478196, 8544890), ), array( array(32173121, -16129311, 24896207, 3921497, 22579056, -3410854, 19270449, 12217473, 17789017, -3395995), array(-30552961, -2228401, -15578829, -10147201, 13243889, 517024, 15479401, -3853233, 30460520, 1052596), array(-11614875, 13323618, 32618793, 8175907, -15230173, 12596687, 27491595, -4612359, 3179268, -9478891), ), array( array(31947069, -14366651, -4640583, -15339921, -15125977, -6039709, -14756777, -16411740, 19072640, -9511060), array(11685058, 11822410, 3158003, -13952594, 33402194, -4165066, 5977896, -5215017, 473099, 5040608), array(-20290863, 8198642, -27410132, 11602123, 1290375, -2799760, 28326862, 1721092, -19558642, -3131606), ), ), array( array( array(7881532, 10687937, 7578723, 7738378, -18951012, -2553952, 21820786, 8076149, -27868496, 11538389), array(-19935666, 3899861, 18283497, -6801568, -15728660, -11249211, 8754525, 7446702, -5676054, 5797016), array(-11295600, -3793569, -15782110, -7964573, 12708869, -8456199, 2014099, -9050574, -2369172, -5877341), ), array( array(-22472376, -11568741, -27682020, 1146375, 18956691, 16640559, 1192730, -3714199, 15123619, 10811505), array(14352098, -3419715, -18942044, 10822655, 32750596, 4699007, -70363, 15776356, -28886779, -11974553), array(-28241164, -8072475, -4978962, -5315317, 29416931, 1847569, -20654173, -16484855, 4714547, -9600655), ), array( array(15200332, 8368572, 19679101, 15970074, -31872674, 1959451, 24611599, -4543832, -11745876, 12340220), array(12876937, -10480056, 33134381, 6590940, -6307776, 14872440, 9613953, 8241152, 15370987, 9608631), array(-4143277, -12014408, 8446281, -391603, 4407738, 13629032, -7724868, 15866074, -28210621, -8814099), ), array( array(26660628, -15677655, 8393734, 358047, -7401291, 992988, -23904233, 858697, 20571223, 8420556), array(14620715, 13067227, -15447274, 8264467, 14106269, 15080814, 33531827, 12516406, -21574435, -12476749), array(236881, 10476226, 57258, -14677024, 6472998, 2466984, 17258519, 7256740, 8791136, 15069930), ), array( array(1276410, -9371918, 22949635, -16322807, -23493039, -5702186, 14711875, 4874229, -30663140, -2331391), array(5855666, 4990204, -13711848, 7294284, -7804282, 1924647, -1423175, -7912378, -33069337, 9234253), array(20590503, -9018988, 31529744, -7352666, -2706834, 10650548, 31559055, -11609587, 18979186, 13396066), ), array( array(24474287, 4968103, 22267082, 4407354, 24063882, -8325180, -18816887, 13594782, 33514650, 7021958), array(-11566906, -6565505, -21365085, 15928892, -26158305, 4315421, -25948728, -3916677, -21480480, 12868082), array(-28635013, 13504661, 19988037, -2132761, 21078225, 6443208, -21446107, 2244500, -12455797, -8089383), ), array( array(-30595528, 13793479, -5852820, 319136, -25723172, -6263899, 33086546, 8957937, -15233648, 5540521), array(-11630176, -11503902, -8119500, -7643073, 2620056, 1022908, -23710744, -1568984, -16128528, -14962807), array(23152971, 775386, 27395463, 14006635, -9701118, 4649512, 1689819, 892185, -11513277, -15205948), ), array( array(9770129, 9586738, 26496094, 4324120, 1556511, -3550024, 27453819, 4763127, -19179614, 5867134), array(-32765025, 1927590, 31726409, -4753295, 23962434, -16019500, 27846559, 5931263, -29749703, -16108455), array(27461885, -2977536, 22380810, 1815854, -23033753, -3031938, 7283490, -15148073, -19526700, 7734629), ), ), array( array( array(-8010264, -9590817, -11120403, 6196038, 29344158, -13430885, 7585295, -3176626, 18549497, 15302069), array(-32658337, -6171222, -7672793, -11051681, 6258878, 13504381, 10458790, -6418461, -8872242, 8424746), array(24687205, 8613276, -30667046, -3233545, 1863892, -1830544, 19206234, 7134917, -11284482, -828919), ), array( array(11334899, -9218022, 8025293, 12707519, 17523892, -10476071, 10243738, -14685461, -5066034, 16498837), array(8911542, 6887158, -9584260, -6958590, 11145641, -9543680, 17303925, -14124238, 6536641, 10543906), array(-28946384, 15479763, -17466835, 568876, -1497683, 11223454, -2669190, -16625574, -27235709, 8876771), ), array( array(-25742899, -12566864, -15649966, -846607, -33026686, -796288, -33481822, 15824474, -604426, -9039817), array(10330056, 70051, 7957388, -9002667, 9764902, 15609756, 27698697, -4890037, 1657394, 3084098), array(10477963, -7470260, 12119566, -13250805, 29016247, -5365589, 31280319, 14396151, -30233575, 15272409), ), array( array(-12288309, 3169463, 28813183, 16658753, 25116432, -5630466, -25173957, -12636138, -25014757, 1950504), array(-26180358, 9489187, 11053416, -14746161, -31053720, 5825630, -8384306, -8767532, 15341279, 8373727), array(28685821, 7759505, -14378516, -12002860, -31971820, 4079242, 298136, -10232602, -2878207, 15190420), ), array( array(-32932876, 13806336, -14337485, -15794431, -24004620, 10940928, 8669718, 2742393, -26033313, -6875003), array(-1580388, -11729417, -25979658, -11445023, -17411874, -10912854, 9291594, -16247779, -12154742, 6048605), array(-30305315, 14843444, 1539301, 11864366, 20201677, 1900163, 13934231, 5128323, 11213262, 9168384), ), array( array(-26280513, 11007847, 19408960, -940758, -18592965, -4328580, -5088060, -11105150, 20470157, -16398701), array(-23136053, 9282192, 14855179, -15390078, -7362815, -14408560, -22783952, 14461608, 14042978, 5230683), array(29969567, -2741594, -16711867, -8552442, 9175486, -2468974, 21556951, 3506042, -5933891, -12449708), ), array( array(-3144746, 8744661, 19704003, 4581278, -20430686, 6830683, -21284170, 8971513, -28539189, 15326563), array(-19464629, 10110288, -17262528, -3503892, -23500387, 1355669, -15523050, 15300988, -20514118, 9168260), array(-5353335, 4488613, -23803248, 16314347, 7780487, -15638939, -28948358, 9601605, 33087103, -9011387), ), array( array(-19443170, -15512900, -20797467, -12445323, -29824447, 10229461, -27444329, -15000531, -5996870, 15664672), array(23294591, -16632613, -22650781, -8470978, 27844204, 11461195, 13099750, -2460356, 18151676, 13417686), array(-24722913, -4176517, -31150679, 5988919, -26858785, 6685065, 1661597, -12551441, 15271676, -15452665), ), ), array( array( array(11433042, -13228665, 8239631, -5279517, -1985436, -725718, -18698764, 2167544, -6921301, -13440182), array(-31436171, 15575146, 30436815, 12192228, -22463353, 9395379, -9917708, -8638997, 12215110, 12028277), array(14098400, 6555944, 23007258, 5757252, -15427832, -12950502, 30123440, 4617780, -16900089, -655628), ), array( array(-4026201, -15240835, 11893168, 13718664, -14809462, 1847385, -15819999, 10154009, 23973261, -12684474), array(-26531820, -3695990, -1908898, 2534301, -31870557, -16550355, 18341390, -11419951, 32013174, -10103539), array(-25479301, 10876443, -11771086, -14625140, -12369567, 1838104, 21911214, 6354752, 4425632, -837822), ), array( array(-10433389, -14612966, 22229858, -3091047, -13191166, 776729, -17415375, -12020462, 4725005, 14044970), array(19268650, -7304421, 1555349, 8692754, -21474059, -9910664, 6347390, -1411784, -19522291, -16109756), array(-24864089, 12986008, -10898878, -5558584, -11312371, -148526, 19541418, 8180106, 9282262, 10282508), ), array( array(-26205082, 4428547, -8661196, -13194263, 4098402, -14165257, 15522535, 8372215, 5542595, -10702683), array(-10562541, 14895633, 26814552, -16673850, -17480754, -2489360, -2781891, 6993761, -18093885, 10114655), array(-20107055, -929418, 31422704, 10427861, -7110749, 6150669, -29091755, -11529146, 25953725, -106158), ), array( array(-4234397, -8039292, -9119125, 3046000, 2101609, -12607294, 19390020, 6094296, -3315279, 12831125), array(-15998678, 7578152, 5310217, 14408357, -33548620, -224739, 31575954, 6326196, 7381791, -2421839), array(-20902779, 3296811, 24736065, -16328389, 18374254, 7318640, 6295303, 8082724, -15362489, 12339664), ), array( array(27724736, 2291157, 6088201, -14184798, 1792727, 5857634, 13848414, 15768922, 25091167, 14856294), array(-18866652, 8331043, 24373479, 8541013, -701998, -9269457, 12927300, -12695493, -22182473, -9012899), array(-11423429, -5421590, 11632845, 3405020, 30536730, -11674039, -27260765, 13866390, 30146206, 9142070), ), array( array(3924129, -15307516, -13817122, -10054960, 12291820, -668366, -27702774, 9326384, -8237858, 4171294), array(-15921940, 16037937, 6713787, 16606682, -21612135, 2790944, 26396185, 3731949, 345228, -5462949), array(-21327538, 13448259, 25284571, 1143661, 20614966, -8849387, 2031539, -12391231, -16253183, -13582083), ), array( array(31016211, -16722429, 26371392, -14451233, -5027349, 14854137, 17477601, 3842657, 28012650, -16405420), array(-5075835, 9368966, -8562079, -4600902, -15249953, 6970560, -9189873, 16292057, -8867157, 3507940), array(29439664, 3537914, 23333589, 6997794, -17555561, -11018068, -15209202, -15051267, -9164929, 6580396), ), ), array( array( array(-12185861, -7679788, 16438269, 10826160, -8696817, -6235611, 17860444, -9273846, -2095802, 9304567), array(20714564, -4336911, 29088195, 7406487, 11426967, -5095705, 14792667, -14608617, 5289421, -477127), array(-16665533, -10650790, -6160345, -13305760, 9192020, -1802462, 17271490, 12349094, 26939669, -3752294), ), array( array(-12889898, 9373458, 31595848, 16374215, 21471720, 13221525, -27283495, -12348559, -3698806, 117887), array(22263325, -6560050, 3984570, -11174646, -15114008, -566785, 28311253, 5358056, -23319780, 541964), array(16259219, 3261970, 2309254, -15534474, -16885711, -4581916, 24134070, -16705829, -13337066, -13552195), ), array( array(9378160, -13140186, -22845982, -12745264, 28198281, -7244098, -2399684, -717351, 690426, 14876244), array(24977353, -314384, -8223969, -13465086, 28432343, -1176353, -13068804, -12297348, -22380984, 6618999), array(-1538174, 11685646, 12944378, 13682314, -24389511, -14413193, 8044829, -13817328, 32239829, -5652762), ), array( array(-18603066, 4762990, -926250, 8885304, -28412480, -3187315, 9781647, -10350059, 32779359, 5095274), array(-33008130, -5214506, -32264887, -3685216, 9460461, -9327423, -24601656, 14506724, 21639561, -2630236), array(-16400943, -13112215, 25239338, 15531969, 3987758, -4499318, -1289502, -6863535, 17874574, 558605), ), array( array(-13600129, 10240081, 9171883, 16131053, -20869254, 9599700, 33499487, 5080151, 2085892, 5119761), array(-22205145, -2519528, -16381601, 414691, -25019550, 2170430, 30634760, -8363614, -31999993, -5759884), array(-6845704, 15791202, 8550074, -1312654, 29928809, -12092256, 27534430, -7192145, -22351378, 12961482), ), array( array(-24492060, -9570771, 10368194, 11582341, -23397293, -2245287, 16533930, 8206996, -30194652, -5159638), array(-11121496, -3382234, 2307366, 6362031, -135455, 8868177, -16835630, 7031275, 7589640, 8945490), array(-32152748, 8917967, 6661220, -11677616, -1192060, -15793393, 7251489, -11182180, 24099109, -14456170), ), array( array(5019558, -7907470, 4244127, -14714356, -26933272, 6453165, -19118182, -13289025, -6231896, -10280736), array(10853594, 10721687, 26480089, 5861829, -22995819, 1972175, -1866647, -10557898, -3363451, -6441124), array(-17002408, 5906790, 221599, -6563147, 7828208, -13248918, 24362661, -2008168, -13866408, 7421392), ), array( array(8139927, -6546497, 32257646, -5890546, 30375719, 1886181, -21175108, 15441252, 28826358, -4123029), array(6267086, 9695052, 7709135, -16603597, -32869068, -1886135, 14795160, -7840124, 13746021, -1742048), array(28584902, 7787108, -6732942, -15050729, 22846041, -7571236, -3181936, -363524, 4771362, -8419958), ), ), array( array( array(24949256, 6376279, -27466481, -8174608, -18646154, -9930606, 33543569, -12141695, 3569627, 11342593), array(26514989, 4740088, 27912651, 3697550, 19331575, -11472339, 6809886, 4608608, 7325975, -14801071), array(-11618399, -14554430, -24321212, 7655128, -1369274, 5214312, -27400540, 10258390, -17646694, -8186692), ), array( array(11431204, 15823007, 26570245, 14329124, 18029990, 4796082, -31446179, 15580664, 9280358, -3973687), array(-160783, -10326257, -22855316, -4304997, -20861367, -13621002, -32810901, -11181622, -15545091, 4387441), array(-20799378, 12194512, 3937617, -5805892, -27154820, 9340370, -24513992, 8548137, 20617071, -7482001), ), array( array(-938825, -3930586, -8714311, 16124718, 24603125, -6225393, -13775352, -11875822, 24345683, 10325460), array(-19855277, -1568885, -22202708, 8714034, 14007766, 6928528, 16318175, -1010689, 4766743, 3552007), array(-21751364, -16730916, 1351763, -803421, -4009670, 3950935, 3217514, 14481909, 10988822, -3994762), ), array( array(15564307, -14311570, 3101243, 5684148, 30446780, -8051356, 12677127, -6505343, -8295852, 13296005), array(-9442290, 6624296, -30298964, -11913677, -4670981, -2057379, 31521204, 9614054, -30000824, 12074674), array(4771191, -135239, 14290749, -13089852, 27992298, 14998318, -1413936, -1556716, 29832613, -16391035), ), array( array(7064884, -7541174, -19161962, -5067537, -18891269, -2912736, 25825242, 5293297, -27122660, 13101590), array(-2298563, 2439670, -7466610, 1719965, -27267541, -16328445, 32512469, -5317593, -30356070, -4190957), array(-30006540, 10162316, -33180176, 3981723, -16482138, -13070044, 14413974, 9515896, 19568978, 9628812), ), array( array(33053803, 199357, 15894591, 1583059, 27380243, -4580435, -17838894, -6106839, -6291786, 3437740), array(-18978877, 3884493, 19469877, 12726490, 15913552, 13614290, -22961733, 70104, 7463304, 4176122), array(-27124001, 10659917, 11482427, -16070381, 12771467, -6635117, -32719404, -5322751, 24216882, 5944158), ), array( array(8894125, 7450974, -2664149, -9765752, -28080517, -12389115, 19345746, 14680796, 11632993, 5847885), array(26942781, -2315317, 9129564, -4906607, 26024105, 11769399, -11518837, 6367194, -9727230, 4782140), array(19916461, -4828410, -22910704, -11414391, 25606324, -5972441, 33253853, 8220911, 6358847, -1873857), ), array( array(801428, -2081702, 16569428, 11065167, 29875704, 96627, 7908388, -4480480, -13538503, 1387155), array(19646058, 5720633, -11416706, 12814209, 11607948, 12749789, 14147075, 15156355, -21866831, 11835260), array(19299512, 1155910, 28703737, 14890794, 2925026, 7269399, 26121523, 15467869, -26560550, 5052483), ), ), array( array( array(-3017432, 10058206, 1980837, 3964243, 22160966, 12322533, -6431123, -12618185, 12228557, -7003677), array(32944382, 14922211, -22844894, 5188528, 21913450, -8719943, 4001465, 13238564, -6114803, 8653815), array(22865569, -4652735, 27603668, -12545395, 14348958, 8234005, 24808405, 5719875, 28483275, 2841751), ), array( array(-16420968, -1113305, -327719, -12107856, 21886282, -15552774, -1887966, -315658, 19932058, -12739203), array(-11656086, 10087521, -8864888, -5536143, -19278573, -3055912, 3999228, 13239134, -4777469, -13910208), array(1382174, -11694719, 17266790, 9194690, -13324356, 9720081, 20403944, 11284705, -14013818, 3093230), ), array( array(16650921, -11037932, -1064178, 1570629, -8329746, 7352753, -302424, 16271225, -24049421, -6691850), array(-21911077, -5927941, -4611316, -5560156, -31744103, -10785293, 24123614, 15193618, -21652117, -16739389), array(-9935934, -4289447, -25279823, 4372842, 2087473, 10399484, 31870908, 14690798, 17361620, 11864968), ), array( array(-11307610, 6210372, 13206574, 5806320, -29017692, -13967200, -12331205, -7486601, -25578460, -16240689), array(14668462, -12270235, 26039039, 15305210, 25515617, 4542480, 10453892, 6577524, 9145645, -6443880), array(5974874, 3053895, -9433049, -10385191, -31865124, 3225009, -7972642, 3936128, -5652273, -3050304), ), array( array(30625386, -4729400, -25555961, -12792866, -20484575, 7695099, 17097188, -16303496, -27999779, 1803632), array(-3553091, 9865099, -5228566, 4272701, -5673832, -16689700, 14911344, 12196514, -21405489, 7047412), array(20093277, 9920966, -11138194, -5343857, 13161587, 12044805, -32856851, 4124601, -32343828, -10257566), ), array( array(-20788824, 14084654, -13531713, 7842147, 19119038, -13822605, 4752377, -8714640, -21679658, 2288038), array(-26819236, -3283715, 29965059, 3039786, -14473765, 2540457, 29457502, 14625692, -24819617, 12570232), array(-1063558, -11551823, 16920318, 12494842, 1278292, -5869109, -21159943, -3498680, -11974704, 4724943), ), array( array(17960970, -11775534, -4140968, -9702530, -8876562, -1410617, -12907383, -8659932, -29576300, 1903856), array(23134274, -14279132, -10681997, -1611936, 20684485, 15770816, -12989750, 3190296, 26955097, 14109738), array(15308788, 5320727, -30113809, -14318877, 22902008, 7767164, 29425325, -11277562, 31960942, 11934971), ), array( array(-27395711, 8435796, 4109644, 12222639, -24627868, 14818669, 20638173, 4875028, 10491392, 1379718), array(-13159415, 9197841, 3875503, -8936108, -1383712, -5879801, 33518459, 16176658, 21432314, 12180697), array(-11787308, 11500838, 13787581, -13832590, -22430679, 10140205, 1465425, 12689540, -10301319, -13872883), ), ), array( array( array(5414091, -15386041, -21007664, 9643570, 12834970, 1186149, -2622916, -1342231, 26128231, 6032912), array(-26337395, -13766162, 32496025, -13653919, 17847801, -12669156, 3604025, 8316894, -25875034, -10437358), array(3296484, 6223048, 24680646, -12246460, -23052020, 5903205, -8862297, -4639164, 12376617, 3188849), ), array( array(29190488, -14659046, 27549113, -1183516, 3520066, -10697301, 32049515, -7309113, -16109234, -9852307), array(-14744486, -9309156, 735818, -598978, -20407687, -5057904, 25246078, -15795669, 18640741, -960977), array(-6928835, -16430795, 10361374, 5642961, 4910474, 12345252, -31638386, -494430, 10530747, 1053335), ), array( array(-29265967, -14186805, -13538216, -12117373, -19457059, -10655384, -31462369, -2948985, 24018831, 15026644), array(-22592535, -3145277, -2289276, 5953843, -13440189, 9425631, 25310643, 13003497, -2314791, -15145616), array(-27419985, -603321, -8043984, -1669117, -26092265, 13987819, -27297622, 187899, -23166419, -2531735), ), array( array(-21744398, -13810475, 1844840, 5021428, -10434399, -15911473, 9716667, 16266922, -5070217, 726099), array(29370922, -6053998, 7334071, -15342259, 9385287, 2247707, -13661962, -4839461, 30007388, -15823341), array(-936379, 16086691, 23751945, -543318, -1167538, -5189036, 9137109, 730663, 9835848, 4555336), ), array( array(-23376435, 1410446, -22253753, -12899614, 30867635, 15826977, 17693930, 544696, -11985298, 12422646), array(31117226, -12215734, -13502838, 6561947, -9876867, -12757670, -5118685, -4096706, 29120153, 13924425), array(-17400879, -14233209, 19675799, -2734756, -11006962, -5858820, -9383939, -11317700, 7240931, -237388), ), array( array(-31361739, -11346780, -15007447, -5856218, -22453340, -12152771, 1222336, 4389483, 3293637, -15551743), array(-16684801, -14444245, 11038544, 11054958, -13801175, -3338533, -24319580, 7733547, 12796905, -6335822), array(-8759414, -10817836, -25418864, 10783769, -30615557, -9746811, -28253339, 3647836, 3222231, -11160462), ), array( array(18606113, 1693100, -25448386, -15170272, 4112353, 10045021, 23603893, -2048234, -7550776, 2484985), array(9255317, -3131197, -12156162, -1004256, 13098013, -9214866, 16377220, -2102812, -19802075, -3034702), array(-22729289, 7496160, -5742199, 11329249, 19991973, -3347502, -31718148, 9936966, -30097688, -10618797), ), array( array(21878590, -5001297, 4338336, 13643897, -3036865, 13160960, 19708896, 5415497, -7360503, -4109293), array(27736861, 10103576, 12500508, 8502413, -3413016, -9633558, 10436918, -1550276, -23659143, -8132100), array(19492550, -12104365, -29681976, -852630, -3208171, 12403437, 30066266, 8367329, 13243957, 8709688), ), ), array( array( array(12015105, 2801261, 28198131, 10151021, 24818120, -4743133, -11194191, -5645734, 5150968, 7274186), array(2831366, -12492146, 1478975, 6122054, 23825128, -12733586, 31097299, 6083058, 31021603, -9793610), array(-2529932, -2229646, 445613, 10720828, -13849527, -11505937, -23507731, 16354465, 15067285, -14147707), ), array( array(7840942, 14037873, -33364863, 15934016, -728213, -3642706, 21403988, 1057586, -19379462, -12403220), array(915865, -16469274, 15608285, -8789130, -24357026, 6060030, -17371319, 8410997, -7220461, 16527025), array(32922597, -556987, 20336074, -16184568, 10903705, -5384487, 16957574, 52992, 23834301, 6588044), ), array( array(32752030, 11232950, 3381995, -8714866, 22652988, -10744103, 17159699, 16689107, -20314580, -1305992), array(-4689649, 9166776, -25710296, -10847306, 11576752, 12733943, 7924251, -2752281, 1976123, -7249027), array(21251222, 16309901, -2983015, -6783122, 30810597, 12967303, 156041, -3371252, 12331345, -8237197), ), array( array(8651614, -4477032, -16085636, -4996994, 13002507, 2950805, 29054427, -5106970, 10008136, -4667901), array(31486080, 15114593, -14261250, 12951354, 14369431, -7387845, 16347321, -13662089, 8684155, -10532952), array(19443825, 11385320, 24468943, -9659068, -23919258, 2187569, -26263207, -6086921, 31316348, 14219878), ), array( array(-28594490, 1193785, 32245219, 11392485, 31092169, 15722801, 27146014, 6992409, 29126555, 9207390), array(32382935, 1110093, 18477781, 11028262, -27411763, -7548111, -4980517, 10843782, -7957600, -14435730), array(2814918, 7836403, 27519878, -7868156, -20894015, -11553689, -21494559, 8550130, 28346258, 1994730), ), array( array(-19578299, 8085545, -14000519, -3948622, 2785838, -16231307, -19516951, 7174894, 22628102, 8115180), array(-30405132, 955511, -11133838, -15078069, -32447087, -13278079, -25651578, 3317160, -9943017, 930272), array(-15303681, -6833769, 28856490, 1357446, 23421993, 1057177, 24091212, -1388970, -22765376, -10650715), ), array( array(-22751231, -5303997, -12907607, -12768866, -15811511, -7797053, -14839018, -16554220, -1867018, 8398970), array(-31969310, 2106403, -4736360, 1362501, 12813763, 16200670, 22981545, -6291273, 18009408, -15772772), array(-17220923, -9545221, -27784654, 14166835, 29815394, 7444469, 29551787, -3727419, 19288549, 1325865), ), array( array(15100157, -15835752, -23923978, -1005098, -26450192, 15509408, 12376730, -3479146, 33166107, -8042750), array(20909231, 13023121, -9209752, 16251778, -5778415, -8094914, 12412151, 10018715, 2213263, -13878373), array(32529814, -11074689, 30361439, -16689753, -9135940, 1513226, 22922121, 6382134, -5766928, 8371348), ), ), array( array( array(9923462, 11271500, 12616794, 3544722, -29998368, -1721626, 12891687, -8193132, -26442943, 10486144), array(-22597207, -7012665, 8587003, -8257861, 4084309, -12970062, 361726, 2610596, -23921530, -11455195), array(5408411, -1136691, -4969122, 10561668, 24145918, 14240566, 31319731, -4235541, 19985175, -3436086), ), array( array(-13994457, 16616821, 14549246, 3341099, 32155958, 13648976, -17577068, 8849297, 65030, 8370684), array(-8320926, -12049626, 31204563, 5839400, -20627288, -1057277, -19442942, 6922164, 12743482, -9800518), array(-2361371, 12678785, 28815050, 4759974, -23893047, 4884717, 23783145, 11038569, 18800704, 255233), ), array( array(-5269658, -1773886, 13957886, 7990715, 23132995, 728773, 13393847, 9066957, 19258688, -14753793), array(-2936654, -10827535, -10432089, 14516793, -3640786, 4372541, -31934921, 2209390, -1524053, 2055794), array(580882, 16705327, 5468415, -2683018, -30926419, -14696000, -7203346, -8994389, -30021019, 7394435), ), array( array(23838809, 1822728, -15738443, 15242727, 8318092, -3733104, -21672180, -3492205, -4821741, 14799921), array(13345610, 9759151, 3371034, -16137791, 16353039, 8577942, 31129804, 13496856, -9056018, 7402518), array(2286874, -4435931, -20042458, -2008336, -13696227, 5038122, 11006906, -15760352, 8205061, 1607563), ), array( array(14414086, -8002132, 3331830, -3208217, 22249151, -5594188, 18364661, -2906958, 30019587, -9029278), array(-27688051, 1585953, -10775053, 931069, -29120221, -11002319, -14410829, 12029093, 9944378, 8024), array(4368715, -3709630, 29874200, -15022983, -20230386, -11410704, -16114594, -999085, -8142388, 5640030), ), array( array(10299610, 13746483, 11661824, 16234854, 7630238, 5998374, 9809887, -16694564, 15219798, -14327783), array(27425505, -5719081, 3055006, 10660664, 23458024, 595578, -15398605, -1173195, -18342183, 9742717), array(6744077, 2427284, 26042789, 2720740, -847906, 1118974, 32324614, 7406442, 12420155, 1994844), ), array( array(14012521, -5024720, -18384453, -9578469, -26485342, -3936439, -13033478, -10909803, 24319929, -6446333), array(16412690, -4507367, 10772641, 15929391, -17068788, -4658621, 10555945, -10484049, -30102368, -4739048), array(22397382, -7767684, -9293161, -12792868, 17166287, -9755136, -27333065, 6199366, 21880021, -12250760), ), array( array(-4283307, 5368523, -31117018, 8163389, -30323063, 3209128, 16557151, 8890729, 8840445, 4957760), array(-15447727, 709327, -6919446, -10870178, -29777922, 6522332, -21720181, 12130072, -14796503, 5005757), array(-2114751, -14308128, 23019042, 15765735, -25269683, 6002752, 10183197, -13239326, -16395286, -2176112), ), ), array( array( array(-19025756, 1632005, 13466291, -7995100, -23640451, 16573537, -32013908, -3057104, 22208662, 2000468), array(3065073, -1412761, -25598674, -361432, -17683065, -5703415, -8164212, 11248527, -3691214, -7414184), array(10379208, -6045554, 8877319, 1473647, -29291284, -12507580, 16690915, 2553332, -3132688, 16400289), ), array( array(15716668, 1254266, -18472690, 7446274, -8448918, 6344164, -22097271, -7285580, 26894937, 9132066), array(24158887, 12938817, 11085297, -8177598, -28063478, -4457083, -30576463, 64452, -6817084, -2692882), array(13488534, 7794716, 22236231, 5989356, 25426474, -12578208, 2350710, -3418511, -4688006, 2364226), ), array( array(16335052, 9132434, 25640582, 6678888, 1725628, 8517937, -11807024, -11697457, 15445875, -7798101), array(29004207, -7867081, 28661402, -640412, -12794003, -7943086, 31863255, -4135540, -278050, -15759279), array(-6122061, -14866665, -28614905, 14569919, -10857999, -3591829, 10343412, -6976290, -29828287, -10815811), ), array( array(27081650, 3463984, 14099042, -4517604, 1616303, -6205604, 29542636, 15372179, 17293797, 960709), array(20263915, 11434237, -5765435, 11236810, 13505955, -10857102, -16111345, 6493122, -19384511, 7639714), array(-2830798, -14839232, 25403038, -8215196, -8317012, -16173699, 18006287, -16043750, 29994677, -15808121), ), array( array(9769828, 5202651, -24157398, -13631392, -28051003, -11561624, -24613141, -13860782, -31184575, 709464), array(12286395, 13076066, -21775189, -1176622, -25003198, 4057652, -32018128, -8890874, 16102007, 13205847), array(13733362, 5599946, 10557076, 3195751, -5557991, 8536970, -25540170, 8525972, 10151379, 10394400), ), array( array(4024660, -16137551, 22436262, 12276534, -9099015, -2686099, 19698229, 11743039, -33302334, 8934414), array(-15879800, -4525240, -8580747, -2934061, 14634845, -698278, -9449077, 3137094, -11536886, 11721158), array(17555939, -5013938, 8268606, 2331751, -22738815, 9761013, 9319229, 8835153, -9205489, -1280045), ), array( array(-461409, -7830014, 20614118, 16688288, -7514766, -4807119, 22300304, 505429, 6108462, -6183415), array(-5070281, 12367917, -30663534, 3234473, 32617080, -8422642, 29880583, -13483331, -26898490, -7867459), array(-31975283, 5726539, 26934134, 10237677, -3173717, -605053, 24199304, 3795095, 7592688, -14992079), ), array( array(21594432, -14964228, 17466408, -4077222, 32537084, 2739898, 6407723, 12018833, -28256052, 4298412), array(-20650503, -11961496, -27236275, 570498, 3767144, -1717540, 13891942, -1569194, 13717174, 10805743), array(-14676630, -15644296, 15287174, 11927123, 24177847, -8175568, -796431, 14860609, -26938930, -5863836), ), ), array( array( array(12962541, 5311799, -10060768, 11658280, 18855286, -7954201, 13286263, -12808704, -4381056, 9882022), array(18512079, 11319350, -20123124, 15090309, 18818594, 5271736, -22727904, 3666879, -23967430, -3299429), array(-6789020, -3146043, 16192429, 13241070, 15898607, -14206114, -10084880, -6661110, -2403099, 5276065), ), array( array(30169808, -5317648, 26306206, -11750859, 27814964, 7069267, 7152851, 3684982, 1449224, 13082861), array(10342826, 3098505, 2119311, 193222, 25702612, 12233820, 23697382, 15056736, -21016438, -8202000), array(-33150110, 3261608, 22745853, 7948688, 19370557, -15177665, -26171976, 6482814, -10300080, -11060101), ), array( array(32869458, -5408545, 25609743, 15678670, -10687769, -15471071, 26112421, 2521008, -22664288, 6904815), array(29506923, 4457497, 3377935, -9796444, -30510046, 12935080, 1561737, 3841096, -29003639, -6657642), array(10340844, -6630377, -18656632, -2278430, 12621151, -13339055, 30878497, -11824370, -25584551, 5181966), ), array( array(25940115, -12658025, 17324188, -10307374, -8671468, 15029094, 24396252, -16450922, -2322852, -12388574), array(-21765684, 9916823, -1300409, 4079498, -1028346, 11909559, 1782390, 12641087, 20603771, -6561742), array(-18882287, -11673380, 24849422, 11501709, 13161720, -4768874, 1925523, 11914390, 4662781, 7820689), ), array( array(12241050, -425982, 8132691, 9393934, 32846760, -1599620, 29749456, 12172924, 16136752, 15264020), array(-10349955, -14680563, -8211979, 2330220, -17662549, -14545780, 10658213, 6671822, 19012087, 3772772), array(3753511, -3421066, 10617074, 2028709, 14841030, -6721664, 28718732, -15762884, 20527771, 12988982), ), array( array(-14822485, -5797269, -3707987, 12689773, -898983, -10914866, -24183046, -10564943, 3299665, -12424953), array(-16777703, -15253301, -9642417, 4978983, 3308785, 8755439, 6943197, 6461331, -25583147, 8991218), array(-17226263, 1816362, -1673288, -6086439, 31783888, -8175991, -32948145, 7417950, -30242287, 1507265), ), array( array(29692663, 6829891, -10498800, 4334896, 20945975, -11906496, -28887608, 8209391, 14606362, -10647073), array(-3481570, 8707081, 32188102, 5672294, 22096700, 1711240, -33020695, 9761487, 4170404, -2085325), array(-11587470, 14855945, -4127778, -1531857, -26649089, 15084046, 22186522, 16002000, -14276837, -8400798), ), array( array(-4811456, 13761029, -31703877, -2483919, -3312471, 7869047, -7113572, -9620092, 13240845, 10965870), array(-7742563, -8256762, -14768334, -13656260, -23232383, 12387166, 4498947, 14147411, 29514390, 4302863), array(-13413405, -12407859, 20757302, -13801832, 14785143, 8976368, -5061276, -2144373, 17846988, -13971927), ), ), array( array( array(-2244452, -754728, -4597030, -1066309, -6247172, 1455299, -21647728, -9214789, -5222701, 12650267), array(-9906797, -16070310, 21134160, 12198166, -27064575, 708126, 387813, 13770293, -19134326, 10958663), array(22470984, 12369526, 23446014, -5441109, -21520802, -9698723, -11772496, -11574455, -25083830, 4271862), ), array( array(-25169565, -10053642, -19909332, 15361595, -5984358, 2159192, 75375, -4278529, -32526221, 8469673), array(15854970, 4148314, -8893890, 7259002, 11666551, 13824734, -30531198, 2697372, 24154791, -9460943), array(15446137, -15806644, 29759747, 14019369, 30811221, -9610191, -31582008, 12840104, 24913809, 9815020), ), array( array(-4709286, -5614269, -31841498, -12288893, -14443537, 10799414, -9103676, 13438769, 18735128, 9466238), array(11933045, 9281483, 5081055, -5183824, -2628162, -4905629, -7727821, -10896103, -22728655, 16199064), array(14576810, 379472, -26786533, -8317236, -29426508, -10812974, -102766, 1876699, 30801119, 2164795), ), array( array(15995086, 3199873, 13672555, 13712240, -19378835, -4647646, -13081610, -15496269, -13492807, 1268052), array(-10290614, -3659039, -3286592, 10948818, 23037027, 3794475, -3470338, -12600221, -17055369, 3565904), array(29210088, -9419337, -5919792, -4952785, 10834811, -13327726, -16512102, -10820713, -27162222, -14030531), ), array( array(-13161890, 15508588, 16663704, -8156150, -28349942, 9019123, -29183421, -3769423, 2244111, -14001979), array(-5152875, -3800936, -9306475, -6071583, 16243069, 14684434, -25673088, -16180800, 13491506, 4641841), array(10813417, 643330, -19188515, -728916, 30292062, -16600078, 27548447, -7721242, 14476989, -12767431), ), array( array(10292079, 9984945, 6481436, 8279905, -7251514, 7032743, 27282937, -1644259, -27912810, 12651324), array(-31185513, -813383, 22271204, 11835308, 10201545, 15351028, 17099662, 3988035, 21721536, -3148940), array(10202177, -6545839, -31373232, -9574638, -32150642, -8119683, -12906320, 3852694, 13216206, 14842320), ), array( array(-15815640, -10601066, -6538952, -7258995, -6984659, -6581778, -31500847, 13765824, -27434397, 9900184), array(14465505, -13833331, -32133984, -14738873, -27443187, 12990492, 33046193, 15796406, -7051866, -8040114), array(30924417, -8279620, 6359016, -12816335, 16508377, 9071735, -25488601, 15413635, 9524356, -7018878), ), array( array(12274201, -13175547, 32627641, -1785326, 6736625, 13267305, 5237659, -5109483, 15663516, 4035784), array(-2951309, 8903985, 17349946, 601635, -16432815, -4612556, -13732739, -15889334, -22258478, 4659091), array(-16916263, -4952973, -30393711, -15158821, 20774812, 15897498, 5736189, 15026997, -2178256, -13455585), ), ), array( array( array(-8858980, -2219056, 28571666, -10155518, -474467, -10105698, -3801496, 278095, 23440562, -290208), array(10226241, -5928702, 15139956, 120818, -14867693, 5218603, 32937275, 11551483, -16571960, -7442864), array(17932739, -12437276, -24039557, 10749060, 11316803, 7535897, 22503767, 5561594, -3646624, 3898661), ), array( array(7749907, -969567, -16339731, -16464, -25018111, 15122143, -1573531, 7152530, 21831162, 1245233), array(26958459, -14658026, 4314586, 8346991, -5677764, 11960072, -32589295, -620035, -30402091, -16716212), array(-12165896, 9166947, 33491384, 13673479, 29787085, 13096535, 6280834, 14587357, -22338025, 13987525), ), array( array(-24349909, 7778775, 21116000, 15572597, -4833266, -5357778, -4300898, -5124639, -7469781, -2858068), array(9681908, -6737123, -31951644, 13591838, -6883821, 386950, 31622781, 6439245, -14581012, 4091397), array(-8426427, 1470727, -28109679, -1596990, 3978627, -5123623, -19622683, 12092163, 29077877, -14741988), ), array( array(5269168, -6859726, -13230211, -8020715, 25932563, 1763552, -5606110, -5505881, -20017847, 2357889), array(32264008, -15407652, -5387735, -1160093, -2091322, -3946900, 23104804, -12869908, 5727338, 189038), array(14609123, -8954470, -6000566, -16622781, -14577387, -7743898, -26745169, 10942115, -25888931, -14884697), ), array( array(20513500, 5557931, -15604613, 7829531, 26413943, -2019404, -21378968, 7471781, 13913677, -5137875), array(-25574376, 11967826, 29233242, 12948236, -6754465, 4713227, -8940970, 14059180, 12878652, 8511905), array(-25656801, 3393631, -2955415, -7075526, -2250709, 9366908, -30223418, 6812974, 5568676, -3127656), ), array( array(11630004, 12144454, 2116339, 13606037, 27378885, 15676917, -17408753, -13504373, -14395196, 8070818), array(27117696, -10007378, -31282771, -5570088, 1127282, 12772488, -29845906, 10483306, -11552749, -1028714), array(10637467, -5688064, 5674781, 1072708, -26343588, -6982302, -1683975, 9177853, -27493162, 15431203), ), array( array(20525145, 10892566, -12742472, 12779443, -29493034, 16150075, -28240519, 14943142, -15056790, -7935931), array(-30024462, 5626926, -551567, -9981087, 753598, 11981191, 25244767, -3239766, -3356550, 9594024), array(-23752644, 2636870, -5163910, -10103818, 585134, 7877383, 11345683, -6492290, 13352335, -10977084), ), array( array(-1931799, -5407458, 3304649, -12884869, 17015806, -4877091, -29783850, -7752482, -13215537, -319204), array(20239939, 6607058, 6203985, 3483793, -18386976, -779229, -20723742, 15077870, -22750759, 14523817), array(27406042, -6041657, 27423596, -4497394, 4996214, 10002360, -28842031, -4545494, -30172742, -4805667), ), ), array( array( array(11374242, 12660715, 17861383, -12540833, 10935568, 1099227, -13886076, -9091740, -27727044, 11358504), array(-12730809, 10311867, 1510375, 10778093, -2119455, -9145702, 32676003, 11149336, -26123651, 4985768), array(-19096303, 341147, -6197485, -239033, 15756973, -8796662, -983043, 13794114, -19414307, -15621255), ), array( array(6490081, 11940286, 25495923, -7726360, 8668373, -8751316, 3367603, 6970005, -1691065, -9004790), array(1656497, 13457317, 15370807, 6364910, 13605745, 8362338, -19174622, -5475723, -16796596, -5031438), array(-22273315, -13524424, -64685, -4334223, -18605636, -10921968, -20571065, -7007978, -99853, -10237333), ), array( array(17747465, 10039260, 19368299, -4050591, -20630635, -16041286, 31992683, -15857976, -29260363, -5511971), array(31932027, -4986141, -19612382, 16366580, 22023614, 88450, 11371999, -3744247, 4882242, -10626905), array(29796507, 37186, 19818052, 10115756, -11829032, 3352736, 18551198, 3272828, -5190932, -4162409), ), array( array(12501286, 4044383, -8612957, -13392385, -32430052, 5136599, -19230378, -3529697, 330070, -3659409), array(6384877, 2899513, 17807477, 7663917, -2358888, 12363165, 25366522, -8573892, -271295, 12071499), array(-8365515, -4042521, 25133448, -4517355, -6211027, 2265927, -32769618, 1936675, -5159697, 3829363), ), array( array(28425966, -5835433, -577090, -4697198, -14217555, 6870930, 7921550, -6567787, 26333140, 14267664), array(-11067219, 11871231, 27385719, -10559544, -4585914, -11189312, 10004786, -8709488, -21761224, 8930324), array(-21197785, -16396035, 25654216, -1725397, 12282012, 11008919, 1541940, 4757911, -26491501, -16408940), ), array( array(13537262, -7759490, -20604840, 10961927, -5922820, -13218065, -13156584, 6217254, -15943699, 13814990), array(-17422573, 15157790, 18705543, 29619, 24409717, -260476, 27361681, 9257833, -1956526, -1776914), array(-25045300, -10191966, 15366585, 15166509, -13105086, 8423556, -29171540, 12361135, -18685978, 4578290), ), array( array(24579768, 3711570, 1342322, -11180126, -27005135, 14124956, -22544529, 14074919, 21964432, 8235257), array(-6528613, -2411497, 9442966, -5925588, 12025640, -1487420, -2981514, -1669206, 13006806, 2355433), array(-16304899, -13605259, -6632427, -5142349, 16974359, -10911083, 27202044, 1719366, 1141648, -12796236), ), array( array(-12863944, -13219986, -8318266, -11018091, -6810145, -4843894, 13475066, -3133972, 32674895, 13715045), array(11423335, -5468059, 32344216, 8962751, 24989809, 9241752, -13265253, 16086212, -28740881, -15642093), array(-1409668, 12530728, -6368726, 10847387, 19531186, -14132160, -11709148, 7791794, -27245943, 4383347), ), ), array( array( array(-28970898, 5271447, -1266009, -9736989, -12455236, 16732599, -4862407, -4906449, 27193557, 6245191), array(-15193956, 5362278, -1783893, 2695834, 4960227, 12840725, 23061898, 3260492, 22510453, 8577507), array(-12632451, 11257346, -32692994, 13548177, -721004, 10879011, 31168030, 13952092, -29571492, -3635906), ), array( array(3877321, -9572739, 32416692, 5405324, -11004407, -13656635, 3759769, 11935320, 5611860, 8164018), array(-16275802, 14667797, 15906460, 12155291, -22111149, -9039718, 32003002, -8832289, 5773085, -8422109), array(-23788118, -8254300, 1950875, 8937633, 18686727, 16459170, -905725, 12376320, 31632953, 190926), ), array( array(-24593607, -16138885, -8423991, 13378746, 14162407, 6901328, -8288749, 4508564, -25341555, -3627528), array(8884438, -5884009, 6023974, 10104341, -6881569, -4941533, 18722941, -14786005, -1672488, 827625), array(-32720583, -16289296, -32503547, 7101210, 13354605, 2659080, -1800575, -14108036, -24878478, 1541286), ), array( array(2901347, -1117687, 3880376, -10059388, -17620940, -3612781, -21802117, -3567481, 20456845, -1885033), array(27019610, 12299467, -13658288, -1603234, -12861660, -4861471, -19540150, -5016058, 29439641, 15138866), array(21536104, -6626420, -32447818, -10690208, -22408077, 5175814, -5420040, -16361163, 7779328, 109896), ), array( array(30279744, 14648750, -8044871, 6425558, 13639621, -743509, 28698390, 12180118, 23177719, -554075), array(26572847, 3405927, -31701700, 12890905, -19265668, 5335866, -6493768, 2378492, 4439158, -13279347), array(-22716706, 3489070, -9225266, -332753, 18875722, -1140095, 14819434, -12731527, -17717757, -5461437), ), array( array(-5056483, 16566551, 15953661, 3767752, -10436499, 15627060, -820954, 2177225, 8550082, -15114165), array(-18473302, 16596775, -381660, 15663611, 22860960, 15585581, -27844109, -3582739, -23260460, -8428588), array(-32480551, 15707275, -8205912, -5652081, 29464558, 2713815, -22725137, 15860482, -21902570, 1494193), ), array( array(-19562091, -14087393, -25583872, -9299552, 13127842, 759709, 21923482, 16529112, 8742704, 12967017), array(-28464899, 1553205, 32536856, -10473729, -24691605, -406174, -8914625, -2933896, -29903758, 15553883), array(21877909, 3230008, 9881174, 10539357, -4797115, 2841332, 11543572, 14513274, 19375923, -12647961), ), array( array(8832269, -14495485, 13253511, 5137575, 5037871, 4078777, 24880818, -6222716, 2862653, 9455043), array(29306751, 5123106, 20245049, -14149889, 9592566, 8447059, -2077124, -2990080, 15511449, 4789663), array(-20679756, 7004547, 8824831, -9434977, -4045704, -3750736, -5754762, 108893, 23513200, 16652362), ), ), array( array( array(-33256173, 4144782, -4476029, -6579123, 10770039, -7155542, -6650416, -12936300, -18319198, 10212860), array(2756081, 8598110, 7383731, -6859892, 22312759, -1105012, 21179801, 2600940, -9988298, -12506466), array(-24645692, 13317462, -30449259, -15653928, 21365574, -10869657, 11344424, 864440, -2499677, -16710063), ), array( array(-26432803, 6148329, -17184412, -14474154, 18782929, -275997, -22561534, 211300, 2719757, 4940997), array(-1323882, 3911313, -6948744, 14759765, -30027150, 7851207, 21690126, 8518463, 26699843, 5276295), array(-13149873, -6429067, 9396249, 365013, 24703301, -10488939, 1321586, 149635, -15452774, 7159369), ), array( array(9987780, -3404759, 17507962, 9505530, 9731535, -2165514, 22356009, 8312176, 22477218, -8403385), array(18155857, -16504990, 19744716, 9006923, 15154154, -10538976, 24256460, -4864995, -22548173, 9334109), array(2986088, -4911893, 10776628, -3473844, 10620590, -7083203, -21413845, 14253545, -22587149, 536906), ), array( array(4377756, 8115836, 24567078, 15495314, 11625074, 13064599, 7390551, 10589625, 10838060, -15420424), array(-19342404, 867880, 9277171, -3218459, -14431572, -1986443, 19295826, -15796950, 6378260, 699185), array(7895026, 4057113, -7081772, -13077756, -17886831, -323126, -716039, 15693155, -5045064, -13373962), ), array( array(-7737563, -5869402, -14566319, -7406919, 11385654, 13201616, 31730678, -10962840, -3918636, -9669325), array(10188286, -15770834, -7336361, 13427543, 22223443, 14896287, 30743455, 7116568, -21786507, 5427593), array(696102, 13206899, 27047647, -10632082, 15285305, -9853179, 10798490, -4578720, 19236243, 12477404), ), array( array(-11229439, 11243796, -17054270, -8040865, -788228, -8167967, -3897669, 11180504, -23169516, 7733644), array(17800790, -14036179, -27000429, -11766671, 23887827, 3149671, 23466177, -10538171, 10322027, 15313801), array(26246234, 11968874, 32263343, -5468728, 6830755, -13323031, -15794704, -101982, -24449242, 10890804), ), array( array(-31365647, 10271363, -12660625, -6267268, 16690207, -13062544, -14982212, 16484931, 25180797, -5334884), array(-586574, 10376444, -32586414, -11286356, 19801893, 10997610, 2276632, 9482883, 316878, 13820577), array(-9882808, -4510367, -2115506, 16457136, -11100081, 11674996, 30756178, -7515054, 30696930, -3712849), ), array( array(32988917, -9603412, 12499366, 7910787, -10617257, -11931514, -7342816, -9985397, -32349517, 7392473), array(-8855661, 15927861, 9866406, -3649411, -2396914, -16655781, -30409476, -9134995, 25112947, -2926644), array(-2504044, -436966, 25621774, -5678772, 15085042, -5479877, -24884878, -13526194, 5537438, -13914319), ), ), array( array( array(-11225584, 2320285, -9584280, 10149187, -33444663, 5808648, -14876251, -1729667, 31234590, 6090599), array(-9633316, 116426, 26083934, 2897444, -6364437, -2688086, 609721, 15878753, -6970405, -9034768), array(-27757857, 247744, -15194774, -9002551, 23288161, -10011936, -23869595, 6503646, 20650474, 1804084), ), array( array(-27589786, 15456424, 8972517, 8469608, 15640622, 4439847, 3121995, -10329713, 27842616, -202328), array(-15306973, 2839644, 22530074, 10026331, 4602058, 5048462, 28248656, 5031932, -11375082, 12714369), array(20807691, -7270825, 29286141, 11421711, -27876523, -13868230, -21227475, 1035546, -19733229, 12796920), ), array( array(12076899, -14301286, -8785001, -11848922, -25012791, 16400684, -17591495, -12899438, 3480665, -15182815), array(-32361549, 5457597, 28548107, 7833186, 7303070, -11953545, -24363064, -15921875, -33374054, 2771025), array(-21389266, 421932, 26597266, 6860826, 22486084, -6737172, -17137485, -4210226, -24552282, 15673397), ), array( array(-20184622, 2338216, 19788685, -9620956, -4001265, -8740893, -20271184, 4733254, 3727144, -12934448), array(6120119, 814863, -11794402, -622716, 6812205, -15747771, 2019594, 7975683, 31123697, -10958981), array(30069250, -11435332, 30434654, 2958439, 18399564, -976289, 12296869, 9204260, -16432438, 9648165), ), array( array(32705432, -1550977, 30705658, 7451065, -11805606, 9631813, 3305266, 5248604, -26008332, -11377501), array(17219865, 2375039, -31570947, -5575615, -19459679, 9219903, 294711, 15298639, 2662509, -16297073), array(-1172927, -7558695, -4366770, -4287744, -21346413, -8434326, 32087529, -1222777, 32247248, -14389861), ), array( array(14312628, 1221556, 17395390, -8700143, -4945741, -8684635, -28197744, -9637817, -16027623, -13378845), array(-1428825, -9678990, -9235681, 6549687, -7383069, -468664, 23046502, 9803137, 17597934, 2346211), array(18510800, 15337574, 26171504, 981392, -22241552, 7827556, -23491134, -11323352, 3059833, -11782870), ), array( array(10141598, 6082907, 17829293, -1947643, 9830092, 13613136, -25556636, -5544586, -33502212, 3592096), array(33114168, -15889352, -26525686, -13343397, 33076705, 8716171, 1151462, 1521897, -982665, -6837803), array(-32939165, -4255815, 23947181, -324178, -33072974, -12305637, -16637686, 3891704, 26353178, 693168), ), array( array(30374239, 1595580, -16884039, 13186931, 4600344, 406904, 9585294, -400668, 31375464, 14369965), array(-14370654, -7772529, 1510301, 6434173, -18784789, -6262728, 32732230, -13108839, 17901441, 16011505), array(18171223, -11934626, -12500402, 15197122, -11038147, -15230035, -19172240, -16046376, 8764035, 12309598), ), ), array( array( array(5975908, -5243188, -19459362, -9681747, -11541277, 14015782, -23665757, 1228319, 17544096, -10593782), array(5811932, -1715293, 3442887, -2269310, -18367348, -8359541, -18044043, -15410127, -5565381, 12348900), array(-31399660, 11407555, 25755363, 6891399, -3256938, 14872274, -24849353, 8141295, -10632534, -585479), ), array( array(-12675304, 694026, -5076145, 13300344, 14015258, -14451394, -9698672, -11329050, 30944593, 1130208), array(8247766, -6710942, -26562381, -7709309, -14401939, -14648910, 4652152, 2488540, 23550156, -271232), array(17294316, -3788438, 7026748, 15626851, 22990044, 113481, 2267737, -5908146, -408818, -137719), ), array( array(16091085, -16253926, 18599252, 7340678, 2137637, -1221657, -3364161, 14550936, 3260525, -7166271), array(-4910104, -13332887, 18550887, 10864893, -16459325, -7291596, -23028869, -13204905, -12748722, 2701326), array(-8574695, 16099415, 4629974, -16340524, -20786213, -6005432, -10018363, 9276971, 11329923, 1862132), ), array( array(14763076, -15903608, -30918270, 3689867, 3511892, 10313526, -21951088, 12219231, -9037963, -940300), array(8894987, -3446094, 6150753, 3013931, 301220, 15693451, -31981216, -2909717, -15438168, 11595570), array(15214962, 3537601, -26238722, -14058872, 4418657, -15230761, 13947276, 10730794, -13489462, -4363670), ), array( array(-2538306, 7682793, 32759013, 263109, -29984731, -7955452, -22332124, -10188635, 977108, 699994), array(-12466472, 4195084, -9211532, 550904, -15565337, 12917920, 19118110, -439841, -30534533, -14337913), array(31788461, -14507657, 4799989, 7372237, 8808585, -14747943, 9408237, -10051775, 12493932, -5409317), ), array( array(-25680606, 5260744, -19235809, -6284470, -3695942, 16566087, 27218280, 2607121, 29375955, 6024730), array(842132, -2794693, -4763381, -8722815, 26332018, -12405641, 11831880, 6985184, -9940361, 2854096), array(-4847262, -7969331, 2516242, -5847713, 9695691, -7221186, 16512645, 960770, 12121869, 16648078), ), array( array(-15218652, 14667096, -13336229, 2013717, 30598287, -464137, -31504922, -7882064, 20237806, 2838411), array(-19288047, 4453152, 15298546, -16178388, 22115043, -15972604, 12544294, -13470457, 1068881, -12499905), array(-9558883, -16518835, 33238498, 13506958, 30505848, -1114596, -8486907, -2630053, 12521378, 4845654), ), array( array(-28198521, 10744108, -2958380, 10199664, 7759311, -13088600, 3409348, -873400, -6482306, -12885870), array(-23561822, 6230156, -20382013, 10655314, -24040585, -11621172, 10477734, -1240216, -3113227, 13974498), array(12966261, 15550616, -32038948, -1615346, 21025980, -629444, 5642325, 7188737, 18895762, 12629579), ), ), array( array( array(14741879, -14946887, 22177208, -11721237, 1279741, 8058600, 11758140, 789443, 32195181, 3895677), array(10758205, 15755439, -4509950, 9243698, -4879422, 6879879, -2204575, -3566119, -8982069, 4429647), array(-2453894, 15725973, -20436342, -10410672, -5803908, -11040220, -7135870, -11642895, 18047436, -15281743), ), array( array(-25173001, -11307165, 29759956, 11776784, -22262383, -15820455, 10993114, -12850837, -17620701, -9408468), array(21987233, 700364, -24505048, 14972008, -7774265, -5718395, 32155026, 2581431, -29958985, 8773375), array(-25568350, 454463, -13211935, 16126715, 25240068, 8594567, 20656846, 12017935, -7874389, -13920155), ), array( array(6028182, 6263078, -31011806, -11301710, -818919, 2461772, -31841174, -5468042, -1721788, -2776725), array(-12278994, 16624277, 987579, -5922598, 32908203, 1248608, 7719845, -4166698, 28408820, 6816612), array(-10358094, -8237829, 19549651, -12169222, 22082623, 16147817, 20613181, 13982702, -10339570, 5067943), ), array( array(-30505967, -3821767, 12074681, 13582412, -19877972, 2443951, -19719286, 12746132, 5331210, -10105944), array(30528811, 3601899, -1957090, 4619785, -27361822, -15436388, 24180793, -12570394, 27679908, -1648928), array(9402404, -13957065, 32834043, 10838634, -26580150, -13237195, 26653274, -8685565, 22611444, -12715406), ), array( array(22190590, 1118029, 22736441, 15130463, -30460692, -5991321, 19189625, -4648942, 4854859, 6622139), array(-8310738, -2953450, -8262579, -3388049, -10401731, -271929, 13424426, -3567227, 26404409, 13001963), array(-31241838, -15415700, -2994250, 8939346, 11562230, -12840670, -26064365, -11621720, -15405155, 11020693), ), array( array(1866042, -7949489, -7898649, -10301010, 12483315, 13477547, 3175636, -12424163, 28761762, 1406734), array(-448555, -1777666, 13018551, 3194501, -9580420, -11161737, 24760585, -4347088, 25577411, -13378680), array(-24290378, 4759345, -690653, -1852816, 2066747, 10693769, -29595790, 9884936, -9368926, 4745410), ), array( array(-9141284, 6049714, -19531061, -4341411, -31260798, 9944276, -15462008, -11311852, 10931924, -11931931), array(-16561513, 14112680, -8012645, 4817318, -8040464, -11414606, -22853429, 10856641, -20470770, 13434654), array(22759489, -10073434, -16766264, -1871422, 13637442, -10168091, 1765144, -12654326, 28445307, -5364710), ), array( array(29875063, 12493613, 2795536, -3786330, 1710620, 15181182, -10195717, -8788675, 9074234, 1167180), array(-26205683, 11014233, -9842651, -2635485, -26908120, 7532294, -18716888, -9535498, 3843903, 9367684), array(-10969595, -6403711, 9591134, 9582310, 11349256, 108879, 16235123, 8601684, -139197, 4242895), ), ), array( array( array(22092954, -13191123, -2042793, -11968512, 32186753, -11517388, -6574341, 2470660, -27417366, 16625501), array(-11057722, 3042016, 13770083, -9257922, 584236, -544855, -7770857, 2602725, -27351616, 14247413), array(6314175, -10264892, -32772502, 15957557, -10157730, 168750, -8618807, 14290061, 27108877, -1180880), ), array( array(-8586597, -7170966, 13241782, 10960156, -32991015, -13794596, 33547976, -11058889, -27148451, 981874), array(22833440, 9293594, -32649448, -13618667, -9136966, 14756819, -22928859, -13970780, -10479804, -16197962), array(-7768587, 3326786, -28111797, 10783824, 19178761, 14905060, 22680049, 13906969, -15933690, 3797899), ), array( array(21721356, -4212746, -12206123, 9310182, -3882239, -13653110, 23740224, -2709232, 20491983, -8042152), array(9209270, -15135055, -13256557, -6167798, -731016, 15289673, 25947805, 15286587, 30997318, -6703063), array(7392032, 16618386, 23946583, -8039892, -13265164, -1533858, -14197445, -2321576, 17649998, -250080), ), array( array(-9301088, -14193827, 30609526, -3049543, -25175069, -1283752, -15241566, -9525724, -2233253, 7662146), array(-17558673, 1763594, -33114336, 15908610, -30040870, -12174295, 7335080, -8472199, -3174674, 3440183), array(-19889700, -5977008, -24111293, -9688870, 10799743, -16571957, 40450, -4431835, 4862400, 1133), ), array( array(-32856209, -7873957, -5422389, 14860950, -16319031, 7956142, 7258061, 311861, -30594991, -7379421), array(-3773428, -1565936, 28985340, 7499440, 24445838, 9325937, 29727763, 16527196, 18278453, 15405622), array(-4381906, 8508652, -19898366, -3674424, -5984453, 15149970, -13313598, 843523, -21875062, 13626197), ), array( array(2281448, -13487055, -10915418, -2609910, 1879358, 16164207, -10783882, 3953792, 13340839, 15928663), array(31727126, -7179855, -18437503, -8283652, 2875793, -16390330, -25269894, -7014826, -23452306, 5964753), array(4100420, -5959452, -17179337, 6017714, -18705837, 12227141, -26684835, 11344144, 2538215, -7570755), ), array( array(-9433605, 6123113, 11159803, -2156608, 30016280, 14966241, -20474983, 1485421, -629256, -15958862), array(-26804558, 4260919, 11851389, 9658551, -32017107, 16367492, -20205425, -13191288, 11659922, -11115118), array(26180396, 10015009, -30844224, -8581293, 5418197, 9480663, 2231568, -10170080, 33100372, -1306171), ), array( array(15121113, -5201871, -10389905, 15427821, -27509937, -15992507, 21670947, 4486675, -5931810, -14466380), array(16166486, -9483733, -11104130, 6023908, -31926798, -1364923, 2340060, -16254968, -10735770, -10039824), array(28042865, -3557089, -12126526, 12259706, -3717498, -6945899, 6766453, -8689599, 18036436, 5803270), ), ), array( array( array(-817581, 6763912, 11803561, 1585585, 10958447, -2671165, 23855391, 4598332, -6159431, -14117438), array(-31031306, -14256194, 17332029, -2383520, 31312682, -5967183, 696309, 50292, -20095739, 11763584), array(-594563, -2514283, -32234153, 12643980, 12650761, 14811489, 665117, -12613632, -19773211, -10713562), ), array( array(30464590, -11262872, -4127476, -12734478, 19835327, -7105613, -24396175, 2075773, -17020157, 992471), array(18357185, -6994433, 7766382, 16342475, -29324918, 411174, 14578841, 8080033, -11574335, -10601610), array(19598397, 10334610, 12555054, 2555664, 18821899, -10339780, 21873263, 16014234, 26224780, 16452269), ), array( array(-30223925, 5145196, 5944548, 16385966, 3976735, 2009897, -11377804, -7618186, -20533829, 3698650), array(14187449, 3448569, -10636236, -10810935, -22663880, -3433596, 7268410, -10890444, 27394301, 12015369), array(19695761, 16087646, 28032085, 12999827, 6817792, 11427614, 20244189, -1312777, -13259127, -3402461), ), array( array(30860103, 12735208, -1888245, -4699734, -16974906, 2256940, -8166013, 12298312, -8550524, -10393462), array(-5719826, -11245325, -1910649, 15569035, 26642876, -7587760, -5789354, -15118654, -4976164, 12651793), array(-2848395, 9953421, 11531313, -5282879, 26895123, -12697089, -13118820, -16517902, 9768698, -2533218), ), array( array(-24719459, 1894651, -287698, -4704085, 15348719, -8156530, 32767513, 12765450, 4940095, 10678226), array(18860224, 15980149, -18987240, -1562570, -26233012, -11071856, -7843882, 13944024, -24372348, 16582019), array(-15504260, 4970268, -29893044, 4175593, -20993212, -2199756, -11704054, 15444560, -11003761, 7989037), ), array( array(31490452, 5568061, -2412803, 2182383, -32336847, 4531686, -32078269, 6200206, -19686113, -14800171), array(-17308668, -15879940, -31522777, -2831, -32887382, 16375549, 8680158, -16371713, 28550068, -6857132), array(-28126887, -5688091, 16837845, -1820458, -6850681, 12700016, -30039981, 4364038, 1155602, 5988841), ), array( array(21890435, -13272907, -12624011, 12154349, -7831873, 15300496, 23148983, -4470481, 24618407, 8283181), array(-33136107, -10512751, 9975416, 6841041, -31559793, 16356536, 3070187, -7025928, 1466169, 10740210), array(-1509399, -15488185, -13503385, -10655916, 32799044, 909394, -13938903, -5779719, -32164649, -15327040), ), array( array(3960823, -14267803, -28026090, -15918051, -19404858, 13146868, 15567327, 951507, -3260321, -573935), array(24740841, 5052253, -30094131, 8961361, 25877428, 6165135, -24368180, 14397372, -7380369, -6144105), array(-28888365, 3510803, -28103278, -1158478, -11238128, -10631454, -15441463, -14453128, -1625486, -6494814), ), ), array( array( array(793299, -9230478, 8836302, -6235707, -27360908, -2369593, 33152843, -4885251, -9906200, -621852), array(5666233, 525582, 20782575, -8038419, -24538499, 14657740, 16099374, 1468826, -6171428, -15186581), array(-4859255, -3779343, -2917758, -6748019, 7778750, 11688288, -30404353, -9871238, -1558923, -9863646), ), array( array(10896332, -7719704, 824275, 472601, -19460308, 3009587, 25248958, 14783338, -30581476, -15757844), array(10566929, 12612572, -31944212, 11118703, -12633376, 12362879, 21752402, 8822496, 24003793, 14264025), array(27713862, -7355973, -11008240, 9227530, 27050101, 2504721, 23886875, -13117525, 13958495, -5732453), ), array( array(-23481610, 4867226, -27247128, 3900521, 29838369, -8212291, -31889399, -10041781, 7340521, -15410068), array(4646514, -8011124, -22766023, -11532654, 23184553, 8566613, 31366726, -1381061, -15066784, -10375192), array(-17270517, 12723032, -16993061, 14878794, 21619651, -6197576, 27584817, 3093888, -8843694, 3849921), ), array( array(-9064912, 2103172, 25561640, -15125738, -5239824, 9582958, 32477045, -9017955, 5002294, -15550259), array(-12057553, -11177906, 21115585, -13365155, 8808712, -12030708, 16489530, 13378448, -25845716, 12741426), array(-5946367, 10645103, -30911586, 15390284, -3286982, -7118677, 24306472, 15852464, 28834118, -7646072), ), array( array(-17335748, -9107057, -24531279, 9434953, -8472084, -583362, -13090771, 455841, 20461858, 5491305), array(13669248, -16095482, -12481974, -10203039, -14569770, -11893198, -24995986, 11293807, -28588204, -9421832), array(28497928, 6272777, -33022994, 14470570, 8906179, -1225630, 18504674, -14165166, 29867745, -8795943), ), array( array(-16207023, 13517196, -27799630, -13697798, 24009064, -6373891, -6367600, -13175392, 22853429, -4012011), array(24191378, 16712145, -13931797, 15217831, 14542237, 1646131, 18603514, -11037887, 12876623, -2112447), array(17902668, 4518229, -411702, -2829247, 26878217, 5258055, -12860753, 608397, 16031844, 3723494), ), array( array(-28632773, 12763728, -20446446, 7577504, 33001348, -13017745, 17558842, -7872890, 23896954, -4314245), array(-20005381, -12011952, 31520464, 605201, 2543521, 5991821, -2945064, 7229064, -9919646, -8826859), array(28816045, 298879, -28165016, -15920938, 19000928, -1665890, -12680833, -2949325, -18051778, -2082915), ), array( array(16000882, -344896, 3493092, -11447198, -29504595, -13159789, 12577740, 16041268, -19715240, 7847707), array(10151868, 10572098, 27312476, 7922682, 14825339, 4723128, -32855931, -6519018, -10020567, 3852848), array(-11430470, 15697596, -21121557, -4420647, 5386314, 15063598, 16514493, -15932110, 29330899, -15076224), ), ), array( array( array(-25499735, -4378794, -15222908, -6901211, 16615731, 2051784, 3303702, 15490, -27548796, 12314391), array(15683520, -6003043, 18109120, -9980648, 15337968, -5997823, -16717435, 15921866, 16103996, -3731215), array(-23169824, -10781249, 13588192, -1628807, -3798557, -1074929, -19273607, 5402699, -29815713, -9841101), ), array( array(23190676, 2384583, -32714340, 3462154, -29903655, -1529132, -11266856, 8911517, -25205859, 2739713), array(21374101, -3554250, -33524649, 9874411, 15377179, 11831242, -33529904, 6134907, 4931255, 11987849), array(-7732, -2978858, -16223486, 7277597, 105524, -322051, -31480539, 13861388, -30076310, 10117930), ), array( array(-29501170, -10744872, -26163768, 13051539, -25625564, 5089643, -6325503, 6704079, 12890019, 15728940), array(-21972360, -11771379, -951059, -4418840, 14704840, 2695116, 903376, -10428139, 12885167, 8311031), array(-17516482, 5352194, 10384213, -13811658, 7506451, 13453191, 26423267, 4384730, 1888765, -5435404), ), array( array(-25817338, -3107312, -13494599, -3182506, 30896459, -13921729, -32251644, -12707869, -19464434, -3340243), array(-23607977, -2665774, -526091, 4651136, 5765089, 4618330, 6092245, 14845197, 17151279, -9854116), array(-24830458, -12733720, -15165978, 10367250, -29530908, -265356, 22825805, -7087279, -16866484, 16176525), ), array( array(-23583256, 6564961, 20063689, 3798228, -4740178, 7359225, 2006182, -10363426, -28746253, -10197509), array(-10626600, -4486402, -13320562, -5125317, 3432136, -6393229, 23632037, -1940610, 32808310, 1099883), array(15030977, 5768825, -27451236, -2887299, -6427378, -15361371, -15277896, -6809350, 2051441, -15225865), ), array( array(-3362323, -7239372, 7517890, 9824992, 23555850, 295369, 5148398, -14154188, -22686354, 16633660), array(4577086, -16752288, 13249841, -15304328, 19958763, -14537274, 18559670, -10759549, 8402478, -9864273), array(-28406330, -1051581, -26790155, -907698, -17212414, -11030789, 9453451, -14980072, 17983010, 9967138), ), array( array(-25762494, 6524722, 26585488, 9969270, 24709298, 1220360, -1677990, 7806337, 17507396, 3651560), array(-10420457, -4118111, 14584639, 15971087, -15768321, 8861010, 26556809, -5574557, -18553322, -11357135), array(2839101, 14284142, 4029895, 3472686, 14402957, 12689363, -26642121, 8459447, -5605463, -7621941), ), array( array(-4839289, -3535444, 9744961, 2871048, 25113978, 3187018, -25110813, -849066, 17258084, -7977739), array(18164541, -10595176, -17154882, -1542417, 19237078, -9745295, 23357533, -15217008, 26908270, 12150756), array(-30264870, -7647865, 5112249, -7036672, -1499807, -6974257, 43168, -5537701, -32302074, 16215819), ), ), array( array( array(-6898905, 9824394, -12304779, -4401089, -31397141, -6276835, 32574489, 12532905, -7503072, -8675347), array(-27343522, -16515468, -27151524, -10722951, 946346, 16291093, 254968, 7168080, 21676107, -1943028), array(21260961, -8424752, -16831886, -11920822, -23677961, 3968121, -3651949, -6215466, -3556191, -7913075), ), array( array(16544754, 13250366, -16804428, 15546242, -4583003, 12757258, -2462308, -8680336, -18907032, -9662799), array(-2415239, -15577728, 18312303, 4964443, -15272530, -12653564, 26820651, 16690659, 25459437, -4564609), array(-25144690, 11425020, 28423002, -11020557, -6144921, -15826224, 9142795, -2391602, -6432418, -1644817), ), array( array(-23104652, 6253476, 16964147, -3768872, -25113972, -12296437, -27457225, -16344658, 6335692, 7249989), array(-30333227, 13979675, 7503222, -12368314, -11956721, -4621693, -30272269, 2682242, 25993170, -12478523), array(4364628, 5930691, 32304656, -10044554, -8054781, 15091131, 22857016, -10598955, 31820368, 15075278), ), array( array(31879134, -8918693, 17258761, 90626, -8041836, -4917709, 24162788, -9650886, -17970238, 12833045), array(19073683, 14851414, -24403169, -11860168, 7625278, 11091125, -19619190, 2074449, -9413939, 14905377), array(24483667, -11935567, -2518866, -11547418, -1553130, 15355506, -25282080, 9253129, 27628530, -7555480), ), array( array(17597607, 8340603, 19355617, 552187, 26198470, -3176583, 4593324, -9157582, -14110875, 15297016), array(510886, 14337390, -31785257, 16638632, 6328095, 2713355, -20217417, -11864220, 8683221, 2921426), array(18606791, 11874196, 27155355, -5281482, -24031742, 6265446, -25178240, -1278924, 4674690, 13890525), ), array( array(13609624, 13069022, -27372361, -13055908, 24360586, 9592974, 14977157, 9835105, 4389687, 288396), array(9922506, -519394, 13613107, 5883594, -18758345, -434263, -12304062, 8317628, 23388070, 16052080), array(12720016, 11937594, -31970060, -5028689, 26900120, 8561328, -20155687, -11632979, -14754271, -10812892), ), array( array(15961858, 14150409, 26716931, -665832, -22794328, 13603569, 11829573, 7467844, -28822128, 929275), array(11038231, -11582396, -27310482, -7316562, -10498527, -16307831, -23479533, -9371869, -21393143, 2465074), array(20017163, -4323226, 27915242, 1529148, 12396362, 15675764, 13817261, -9658066, 2463391, -4622140), ), array( array(-16358878, -12663911, -12065183, 4996454, -1256422, 1073572, 9583558, 12851107, 4003896, 12673717), array(-1731589, -15155870, -3262930, 16143082, 19294135, 13385325, 14741514, -9103726, 7903886, 2348101), array(24536016, -16515207, 12715592, -3862155, 1511293, 10047386, -3842346, -7129159, -28377538, 10048127), ), ), array( array( array(-12622226, -6204820, 30718825, 2591312, -10617028, 12192840, 18873298, -7297090, -32297756, 15221632), array(-26478122, -11103864, 11546244, -1852483, 9180880, 7656409, -21343950, 2095755, 29769758, 6593415), array(-31994208, -2907461, 4176912, 3264766, 12538965, -868111, 26312345, -6118678, 30958054, 8292160), ), array( array(31429822, -13959116, 29173532, 15632448, 12174511, -2760094, 32808831, 3977186, 26143136, -3148876), array(22648901, 1402143, -22799984, 13746059, 7936347, 365344, -8668633, -1674433, -3758243, -2304625), array(-15491917, 8012313, -2514730, -12702462, -23965846, -10254029, -1612713, -1535569, -16664475, 8194478), ), array( array(27338066, -7507420, -7414224, 10140405, -19026427, -6589889, 27277191, 8855376, 28572286, 3005164), array(26287124, 4821776, 25476601, -4145903, -3764513, -15788984, -18008582, 1182479, -26094821, -13079595), array(-7171154, 3178080, 23970071, 6201893, -17195577, -4489192, -21876275, -13982627, 32208683, -1198248), ), array( array(-16657702, 2817643, -10286362, 14811298, 6024667, 13349505, -27315504, -10497842, -27672585, -11539858), array(15941029, -9405932, -21367050, 8062055, 31876073, -238629, -15278393, -1444429, 15397331, -4130193), array(8934485, -13485467, -23286397, -13423241, -32446090, 14047986, 31170398, -1441021, -27505566, 15087184), ), array( array(-18357243, -2156491, 24524913, -16677868, 15520427, -6360776, -15502406, 11461896, 16788528, -5868942), array(-1947386, 16013773, 21750665, 3714552, -17401782, -16055433, -3770287, -10323320, 31322514, -11615635), array(21426655, -5650218, -13648287, -5347537, -28812189, -4920970, -18275391, -14621414, 13040862, -12112948), ), array( array(11293895, 12478086, -27136401, 15083750, -29307421, 14748872, 14555558, -13417103, 1613711, 4896935), array(-25894883, 15323294, -8489791, -8057900, 25967126, -13425460, 2825960, -4897045, -23971776, -11267415), array(-15924766, -5229880, -17443532, 6410664, 3622847, 10243618, 20615400, 12405433, -23753030, -8436416), ), array( array(-7091295, 12556208, -20191352, 9025187, -17072479, 4333801, 4378436, 2432030, 23097949, -566018), array(4565804, -16025654, 20084412, -7842817, 1724999, 189254, 24767264, 10103221, -18512313, 2424778), array(366633, -11976806, 8173090, -6890119, 30788634, 5745705, -7168678, 1344109, -3642553, 12412659), ), array( array(-24001791, 7690286, 14929416, -168257, -32210835, -13412986, 24162697, -15326504, -3141501, 11179385), array(18289522, -14724954, 8056945, 16430056, -21729724, 7842514, -6001441, -1486897, -18684645, -11443503), array(476239, 6601091, -6152790, -9723375, 17503545, -4863900, 27672959, 13403813, 11052904, 5219329), ), ), array( array( array(20678546, -8375738, -32671898, 8849123, -5009758, 14574752, 31186971, -3973730, 9014762, -8579056), array(-13644050, -10350239, -15962508, 5075808, -1514661, -11534600, -33102500, 9160280, 8473550, -3256838), array(24900749, 14435722, 17209120, -15292541, -22592275, 9878983, -7689309, -16335821, -24568481, 11788948), ), array( array(-3118155, -11395194, -13802089, 14797441, 9652448, -6845904, -20037437, 10410733, -24568470, -1458691), array(-15659161, 16736706, -22467150, 10215878, -9097177, 7563911, 11871841, -12505194, -18513325, 8464118), array(-23400612, 8348507, -14585951, -861714, -3950205, -6373419, 14325289, 8628612, 33313881, -8370517), ), array( array(-20186973, -4967935, 22367356, 5271547, -1097117, -4788838, -24805667, -10236854, -8940735, -5818269), array(-6948785, -1795212, -32625683, -16021179, 32635414, -7374245, 15989197, -12838188, 28358192, -4253904), array(-23561781, -2799059, -32351682, -1661963, -9147719, 10429267, -16637684, 4072016, -5351664, 5596589), ), array( array(-28236598, -3390048, 12312896, 6213178, 3117142, 16078565, 29266239, 2557221, 1768301, 15373193), array(-7243358, -3246960, -4593467, -7553353, -127927, -912245, -1090902, -4504991, -24660491, 3442910), array(-30210571, 5124043, 14181784, 8197961, 18964734, -11939093, 22597931, 7176455, -18585478, 13365930), ), array( array(-7877390, -1499958, 8324673, 4690079, 6261860, 890446, 24538107, -8570186, -9689599, -3031667), array(25008904, -10771599, -4305031, -9638010, 16265036, 15721635, 683793, -11823784, 15723479, -15163481), array(-9660625, 12374379, -27006999, -7026148, -7724114, -12314514, 11879682, 5400171, 519526, -1235876), ), array( array(22258397, -16332233, -7869817, 14613016, -22520255, -2950923, -20353881, 7315967, 16648397, 7605640), array(-8081308, -8464597, -8223311, 9719710, 19259459, -15348212, 23994942, -5281555, -9468848, 4763278), array(-21699244, 9220969, -15730624, 1084137, -25476107, -2852390, 31088447, -7764523, -11356529, 728112), ), array( array(26047220, -11751471, -6900323, -16521798, 24092068, 9158119, -4273545, -12555558, -29365436, -5498272), array(17510331, -322857, 5854289, 8403524, 17133918, -3112612, -28111007, 12327945, 10750447, 10014012), array(-10312768, 3936952, 9156313, -8897683, 16498692, -994647, -27481051, -666732, 3424691, 7540221), ), array( array(30322361, -6964110, 11361005, -4143317, 7433304, 4989748, -7071422, -16317219, -9244265, 15258046), array(13054562, -2779497, 19155474, 469045, -12482797, 4566042, 5631406, 2711395, 1062915, -5136345), array(-19240248, -11254599, -29509029, -7499965, -5835763, 13005411, -6066489, 12194497, 32960380, 1459310), ), ), array( array( array(19852034, 7027924, 23669353, 10020366, 8586503, -6657907, 394197, -6101885, 18638003, -11174937), array(31395534, 15098109, 26581030, 8030562, -16527914, -5007134, 9012486, -7584354, -6643087, -5442636), array(-9192165, -2347377, -1997099, 4529534, 25766844, 607986, -13222, 9677543, -32294889, -6456008), ), array( array(-2444496, -149937, 29348902, 8186665, 1873760, 12489863, -30934579, -7839692, -7852844, -8138429), array(-15236356, -15433509, 7766470, 746860, 26346930, -10221762, -27333451, 10754588, -9431476, 5203576), array(31834314, 14135496, -770007, 5159118, 20917671, -16768096, -7467973, -7337524, 31809243, 7347066), ), array( array(-9606723, -11874240, 20414459, 13033986, 13716524, -11691881, 19797970, -12211255, 15192876, -2087490), array(-12663563, -2181719, 1168162, -3804809, 26747877, -14138091, 10609330, 12694420, 33473243, -13382104), array(33184999, 11180355, 15832085, -11385430, -1633671, 225884, 15089336, -11023903, -6135662, 14480053), ), array( array(31308717, -5619998, 31030840, -1897099, 15674547, -6582883, 5496208, 13685227, 27595050, 8737275), array(-20318852, -15150239, 10933843, -16178022, 8335352, -7546022, -31008351, -12610604, 26498114, 66511), array(22644454, -8761729, -16671776, 4884562, -3105614, -13559366, 30540766, -4286747, -13327787, -7515095), ), array( array(-28017847, 9834845, 18617207, -2681312, -3401956, -13307506, 8205540, 13585437, -17127465, 15115439), array(23711543, -672915, 31206561, -8362711, 6164647, -9709987, -33535882, -1426096, 8236921, 16492939), array(-23910559, -13515526, -26299483, -4503841, 25005590, -7687270, 19574902, 10071562, 6708380, -6222424), ), array( array(2101391, -4930054, 19702731, 2367575, -15427167, 1047675, 5301017, 9328700, 29955601, -11678310), array(3096359, 9271816, -21620864, -15521844, -14847996, -7592937, -25892142, -12635595, -9917575, 6216608), array(-32615849, 338663, -25195611, 2510422, -29213566, -13820213, 24822830, -6146567, -26767480, 7525079), ), array( array(-23066649, -13985623, 16133487, -7896178, -3389565, 778788, -910336, -2782495, -19386633, 11994101), array(21691500, -13624626, -641331, -14367021, 3285881, -3483596, -25064666, 9718258, -7477437, 13381418), array(18445390, -4202236, 14979846, 11622458, -1727110, -3582980, 23111648, -6375247, 28535282, 15779576), ), array( array(30098053, 3089662, -9234387, 16662135, -21306940, 11308411, -14068454, 12021730, 9955285, -16303356), array(9734894, -14576830, -7473633, -9138735, 2060392, 11313496, -18426029, 9924399, 20194861, 13380996), array(-26378102, -7965207, -22167821, 15789297, -18055342, -6168792, -1984914, 15707771, 26342023, 10146099), ), ), array( array( array(-26016874, -219943, 21339191, -41388, 19745256, -2878700, -29637280, 2227040, 21612326, -545728), array(-13077387, 1184228, 23562814, -5970442, -20351244, -6348714, 25764461, 12243797, -20856566, 11649658), array(-10031494, 11262626, 27384172, 2271902, 26947504, -15997771, 39944, 6114064, 33514190, 2333242), ), array( array(-21433588, -12421821, 8119782, 7219913, -21830522, -9016134, -6679750, -12670638, 24350578, -13450001), array(-4116307, -11271533, -23886186, 4843615, -30088339, 690623, -31536088, -10406836, 8317860, 12352766), array(18200138, -14475911, -33087759, -2696619, -23702521, -9102511, -23552096, -2287550, 20712163, 6719373), ), array( array(26656208, 6075253, -7858556, 1886072, -28344043, 4262326, 11117530, -3763210, 26224235, -3297458), array(-17168938, -14854097, -3395676, -16369877, -19954045, 14050420, 21728352, 9493610, 18620611, -16428628), array(-13323321, 13325349, 11432106, 5964811, 18609221, 6062965, -5269471, -9725556, -30701573, -16479657), ), array( array(-23860538, -11233159, 26961357, 1640861, -32413112, -16737940, 12248509, -5240639, 13735342, 1934062), array(25089769, 6742589, 17081145, -13406266, 21909293, -16067981, -15136294, -3765346, -21277997, 5473616), array(31883677, -7961101, 1083432, -11572403, 22828471, 13290673, -7125085, 12469656, 29111212, -5451014), ), array( array(24244947, -15050407, -26262976, 2791540, -14997599, 16666678, 24367466, 6388839, -10295587, 452383), array(-25640782, -3417841, 5217916, 16224624, 19987036, -4082269, -24236251, -5915248, 15766062, 8407814), array(-20406999, 13990231, 15495425, 16395525, 5377168, 15166495, -8917023, -4388953, -8067909, 2276718), ), array( array(30157918, 12924066, -17712050, 9245753, 19895028, 3368142, -23827587, 5096219, 22740376, -7303417), array(2041139, -14256350, 7783687, 13876377, -25946985, -13352459, 24051124, 13742383, -15637599, 13295222), array(33338237, -8505733, 12532113, 7977527, 9106186, -1715251, -17720195, -4612972, -4451357, -14669444), ), array( array(-20045281, 5454097, -14346548, 6447146, 28862071, 1883651, -2469266, -4141880, 7770569, 9620597), array(23208068, 7979712, 33071466, 8149229, 1758231, -10834995, 30945528, -1694323, -33502340, -14767970), array(1439958, -16270480, -1079989, -793782, 4625402, 10647766, -5043801, 1220118, 30494170, -11440799), ), array( array(-5037580, -13028295, -2970559, -3061767, 15640974, -6701666, -26739026, 926050, -1684339, -13333647), array(13908495, -3549272, 30919928, -6273825, -21521863, 7989039, 9021034, 9078865, 3353509, 4033511), array(-29663431, -15113610, 32259991, -344482, 24295849, -12912123, 23161163, 8839127, 27485041, 7356032), ), ), array( array( array(9661027, 705443, 11980065, -5370154, -1628543, 14661173, -6346142, 2625015, 28431036, -16771834), array(-23839233, -8311415, -25945511, 7480958, -17681669, -8354183, -22545972, 14150565, 15970762, 4099461), array(29262576, 16756590, 26350592, -8793563, 8529671, -11208050, 13617293, -9937143, 11465739, 8317062), ), array( array(-25493081, -6962928, 32500200, -9419051, -23038724, -2302222, 14898637, 3848455, 20969334, -5157516), array(-20384450, -14347713, -18336405, 13884722, -33039454, 2842114, -21610826, -3649888, 11177095, 14989547), array(-24496721, -11716016, 16959896, 2278463, 12066309, 10137771, 13515641, 2581286, -28487508, 9930240), ), array( array(-17751622, -2097826, 16544300, -13009300, -15914807, -14949081, 18345767, -13403753, 16291481, -5314038), array(-33229194, 2553288, 32678213, 9875984, 8534129, 6889387, -9676774, 6957617, 4368891, 9788741), array(16660756, 7281060, -10830758, 12911820, 20108584, -8101676, -21722536, -8613148, 16250552, -11111103), ), array( array(-19765507, 2390526, -16551031, 14161980, 1905286, 6414907, 4689584, 10604807, -30190403, 4782747), array(-1354539, 14736941, -7367442, -13292886, 7710542, -14155590, -9981571, 4383045, 22546403, 437323), array(31665577, -12180464, -16186830, 1491339, -18368625, 3294682, 27343084, 2786261, -30633590, -14097016), ), array( array(-14467279, -683715, -33374107, 7448552, 19294360, 14334329, -19690631, 2355319, -19284671, -6114373), array(15121312, -15796162, 6377020, -6031361, -10798111, -12957845, 18952177, 15496498, -29380133, 11754228), array(-2637277, -13483075, 8488727, -14303896, 12728761, -1622493, 7141596, 11724556, 22761615, -10134141), ), array( array(16918416, 11729663, -18083579, 3022987, -31015732, -13339659, -28741185, -12227393, 32851222, 11717399), array(11166634, 7338049, -6722523, 4531520, -29468672, -7302055, 31474879, 3483633, -1193175, -4030831), array(-185635, 9921305, 31456609, -13536438, -12013818, 13348923, 33142652, 6546660, -19985279, -3948376), ), array( array(-32460596, 11266712, -11197107, -7899103, 31703694, 3855903, -8537131, -12833048, -30772034, -15486313), array(-18006477, 12709068, 3991746, -6479188, -21491523, -10550425, -31135347, -16049879, 10928917, 3011958), array(-6957757, -15594337, 31696059, 334240, 29576716, 14796075, -30831056, -12805180, 18008031, 10258577), ), array( array(-22448644, 15655569, 7018479, -4410003, -30314266, -1201591, -1853465, 1367120, 25127874, 6671743), array(29701166, -14373934, -10878120, 9279288, -17568, 13127210, 21382910, 11042292, 25838796, 4642684), array(-20430234, 14955537, -24126347, 8124619, -5369288, -5990470, 30468147, -13900640, 18423289, 4177476), ), ) ); /** * See: libsodium's crypto_core/curve25519/ref10/base2.h * * @var array basically int[8][3] */ protected static $base2 = array( array( array(25967493, -14356035, 29566456, 3660896, -12694345, 4014787, 27544626, -11754271, -6079156, 2047605), array(-12545711, 934262, -2722910, 3049990, -727428, 9406986, 12720692, 5043384, 19500929, -15469378), array(-8738181, 4489570, 9688441, -14785194, 10184609, -12363380, 29287919, 11864899, -24514362, -4438546), ), array( array(15636291, -9688557, 24204773, -7912398, 616977, -16685262, 27787600, -14772189, 28944400, -1550024), array(16568933, 4717097, -11556148, -1102322, 15682896, -11807043, 16354577, -11775962, 7689662, 11199574), array(30464156, -5976125, -11779434, -15670865, 23220365, 15915852, 7512774, 10017326, -17749093, -9920357), ), array( array(10861363, 11473154, 27284546, 1981175, -30064349, 12577861, 32867885, 14515107, -15438304, 10819380), array(4708026, 6336745, 20377586, 9066809, -11272109, 6594696, -25653668, 12483688, -12668491, 5581306), array(19563160, 16186464, -29386857, 4097519, 10237984, -4348115, 28542350, 13850243, -23678021, -15815942), ), array( array(5153746, 9909285, 1723747, -2777874, 30523605, 5516873, 19480852, 5230134, -23952439, -15175766), array(-30269007, -3463509, 7665486, 10083793, 28475525, 1649722, 20654025, 16520125, 30598449, 7715701), array(28881845, 14381568, 9657904, 3680757, -20181635, 7843316, -31400660, 1370708, 29794553, -1409300), ), array( array(-22518993, -6692182, 14201702, -8745502, -23510406, 8844726, 18474211, -1361450, -13062696, 13821877), array(-6455177, -7839871, 3374702, -4740862, -27098617, -10571707, 31655028, -7212327, 18853322, -14220951), array(4566830, -12963868, -28974889, -12240689, -7602672, -2830569, -8514358, -10431137, 2207753, -3209784), ), array( array(-25154831, -4185821, 29681144, 7868801, -6854661, -9423865, -12437364, -663000, -31111463, -16132436), array(25576264, -2703214, 7349804, -11814844, 16472782, 9300885, 3844789, 15725684, 171356, 6466918), array(23103977, 13316479, 9739013, -16149481, 817875, -15038942, 8965339, -14088058, -30714912, 16193877), ), array( array(-33521811, 3180713, -2394130, 14003687, -16903474, -16270840, 17238398, 4729455, -18074513, 9256800), array(-25182317, -4174131, 32336398, 5036987, -21236817, 11360617, 22616405, 9761698, -19827198, 630305), array(-13720693, 2639453, -24237460, -7406481, 9494427, -5774029, -6554551, -15960994, -2449256, -14291300), ), array( array(-3151181, -5046075, 9282714, 6866145, -31907062, -863023, -18940575, 15033784, 25105118, -7894876), array(-24326370, 15950226, -31801215, -14592823, -11662737, -5090925, 1573892, -2625887, 2198790, -15804619), array(-3099351, 10324967, -2241613, 7453183, -5446979, -2735503, -13812022, -16236442, -32461234, -12290683), ) ); /** * 37095705934669439343138083508754565189542113879843219016388785533085940283555 * * @var array<int, int> */ protected static $d = array( -10913610, 13857413, -15372611, 6949391, 114729, -8787816, -6275908, -3247719, -18696448, -12055116 ); /** * 2 * d = 16295367250680780974490674513165176452449235426866156013048779062215315747161 * * @var array<int, int> */ protected static $d2 = array( -21827239, -5839606, -30745221, 13898782, 229458, 15978800, -12551817, -6495438, 29715968, 9444199 ); /** * sqrt(-1) * * @var array<int, int> */ protected static $sqrtm1 = array( -32595792, -7943725, 9377950, 3500415, 12389472, -272473, -25146209, -2005654, 326686, 11406482 ); /** * 1 / sqrt(a - d) * * @var array<int, int> */ protected static $invsqrtamd = array( 6111485, 4156064, -27798727, 12243468, -25904040, 120897, 20826367, -7060776, 6093568, -1986012 ); /** * sqrt(ad - 1) with a = -1 (mod p) * * @var array<int, int> */ protected static $sqrtadm1 = array( 24849947, -153582, -23613485, 6347715, -21072328, -667138, -25271143, -15367704, -870347, 14525639 ); /** * 1 - d ^ 2 * * @var array<int, int> */ protected static $onemsqd = array( 6275446, -16617371, -22938544, -3773710, 11667077, 7397348, -27922721, 1766195, -24433858, 672203 ); /** * (d - 1) ^ 2 * @var array<int, int> */ protected static $sqdmone = array( 15551795, -11097455, -13425098, -10125071, -11896535, 10178284, -26634327, 4729244, -5282110, -10116402 ); /* * 2^252+27742317777372353535851937790883648493 static const unsigned char L[] = { 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 }; */ const L = "\xed\xd3\xf5\x5c\x1a\x63\x12\x58\xd6\x9c\xf7\xa2\xde\xf9\xde\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10"; } Core/Curve25519/Fe.php 0000644 00000014320 15162213723 0010173 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_Curve25519_Fe', false)) { return; } /** * Class ParagonIE_Sodium_Core_Curve25519_Fe * * This represents a Field Element */ class ParagonIE_Sodium_Core_Curve25519_Fe implements ArrayAccess { /** * @var int */ public $e0 = 0; /** * @var int */ public $e1 = 0; /** * @var int */ public $e2 = 0; /** * @var int */ public $e3 = 0; /** * @var int */ public $e4 = 0; /** * @var int */ public $e5 = 0; /** * @var int */ public $e6 = 0; /** * @var int */ public $e7 = 0; /** * @var int */ public $e8 = 0; /** * @var int */ public $e9 = 0; /** * @param int $e0 * @param int $e1 * @param int $e2 * @param int $e3 * @param int $e4 * @param int $e5 * @param int $e6 * @param int $e7 * @param int $e8 * @param int $e9 */ public function __construct( $e0 = 0, $e1 = 0, $e2 = 0, $e3 = 0, $e4 = 0, $e5 = 0, $e6 = 0, $e7 = 0, $e8 = 0, $e9 = 0 ) { $this->e0 = $e0; $this->e1 = $e1; $this->e2 = $e2; $this->e3 = $e3; $this->e4 = $e4; $this->e5 = $e5; $this->e6 = $e6; $this->e7 = $e7; $this->e8 = $e8; $this->e9 = $e9; } /** * @internal You should not use this directly from another application * * @param array $array * @return self */ public static function fromArray($array) { $obj = new ParagonIE_Sodium_Core_Curve25519_Fe(); $obj->e0 = isset($array[0]) ? (int) $array[0] : 0; $obj->e1 = isset($array[1]) ? (int) $array[1] : 0; $obj->e2 = isset($array[2]) ? (int) $array[2] : 0; $obj->e3 = isset($array[3]) ? (int) $array[3] : 0; $obj->e4 = isset($array[4]) ? (int) $array[4] : 0; $obj->e5 = isset($array[5]) ? (int) $array[5] : 0; $obj->e6 = isset($array[6]) ? (int) $array[6] : 0; $obj->e7 = isset($array[7]) ? (int) $array[7] : 0; $obj->e8 = isset($array[8]) ? (int) $array[8] : 0; $obj->e9 = isset($array[9]) ? (int) $array[9] : 0; return $obj; } /** * @internal You should not use this directly from another application * * @param int|null $offset * @param int $value * @return void */ #[ReturnTypeWillChange] public function offsetSet($offset, $value) { if (!is_int($value)) { throw new InvalidArgumentException('Expected an integer'); } switch ($offset) { case 0: $this->e0 = $value; break; case 1: $this->e1 = $value; break; case 2: $this->e2 = $value; break; case 3: $this->e3 = $value; break; case 4: $this->e4 = $value; break; case 5: $this->e5 = $value; break; case 6: $this->e6 = $value; break; case 7: $this->e7 = $value; break; case 8: $this->e8 = $value; break; case 9: $this->e9 = $value; break; default: throw new OutOfBoundsException('Index out of bounds'); } } /** * @internal You should not use this directly from another application * * @param int $offset * @return bool */ #[ReturnTypeWillChange] public function offsetExists($offset) { return $offset >= 0 && $offset < 10; } /** * @internal You should not use this directly from another application * * @param int $offset * @return void */ #[ReturnTypeWillChange] public function offsetUnset($offset) { switch ($offset) { case 0: $this->e0 = 0; break; case 1: $this->e1 = 0; break; case 2: $this->e2 = 0; break; case 3: $this->e3 = 0; break; case 4: $this->e4 = 0; break; case 5: $this->e5 = 0; break; case 6: $this->e6 = 0; break; case 7: $this->e7 = 0; break; case 8: $this->e8 = 0; break; case 9: $this->e9 = 0; break; default: throw new OutOfBoundsException('Index out of bounds'); } } /** * @internal You should not use this directly from another application * * @param int $offset * @return int */ #[ReturnTypeWillChange] public function offsetGet($offset) { switch ($offset) { case 0: return (int) $this->e0; case 1: return (int) $this->e1; case 2: return (int) $this->e2; case 3: return (int) $this->e3; case 4: return (int) $this->e4; case 5: return (int) $this->e5; case 6: return (int) $this->e6; case 7: return (int) $this->e7; case 8: return (int) $this->e8; case 9: return (int) $this->e9; default: throw new OutOfBoundsException('Index out of bounds'); } } /** * @internal You should not use this directly from another application * * @return array */ public function __debugInfo() { return array( implode(', ', array( $this->e0, $this->e1, $this->e2, $this->e3, $this->e4, $this->e5, $this->e6, $this->e7, $this->e8, $this->e9 )) ); } } Core/Curve25519/README.md 0000644 00000000332 15162213724 0010406 0 ustar 00 # Curve25519 Data Structures These are PHP implementation of the [structs used in the ref10 curve25519 code](https://github.com/jedisct1/libsodium/blob/master/src/libsodium/include/sodium/private/curve25519_ref10.h). Core/ChaCha20.php 0000644 00000031131 15162213724 0007420 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_ChaCha20', false)) { return; } /** * Class ParagonIE_Sodium_Core_ChaCha20 */ class ParagonIE_Sodium_Core_ChaCha20 extends ParagonIE_Sodium_Core_Util { /** * Bitwise left rotation * * @internal You should not use this directly from another application * * @param int $v * @param int $n * @return int */ public static function rotate($v, $n) { $v &= 0xffffffff; $n &= 31; return (int) ( 0xffffffff & ( ($v << $n) | ($v >> (32 - $n)) ) ); } /** * The ChaCha20 quarter round function. Works on four 32-bit integers. * * @internal You should not use this directly from another application * * @param int $a * @param int $b * @param int $c * @param int $d * @return array<int, int> */ protected static function quarterRound($a, $b, $c, $d) { # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); /** @var int $a */ $a = ($a + $b) & 0xffffffff; $d = self::rotate($d ^ $a, 16); # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); /** @var int $c */ $c = ($c + $d) & 0xffffffff; $b = self::rotate($b ^ $c, 12); # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); /** @var int $a */ $a = ($a + $b) & 0xffffffff; $d = self::rotate($d ^ $a, 8); # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); /** @var int $c */ $c = ($c + $d) & 0xffffffff; $b = self::rotate($b ^ $c, 7); return array((int) $a, (int) $b, (int) $c, (int) $d); } /** * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_ChaCha20_Ctx $ctx * @param string $message * * @return string * @throws TypeError * @throws SodiumException */ public static function encryptBytes( ParagonIE_Sodium_Core_ChaCha20_Ctx $ctx, $message = '' ) { $bytes = self::strlen($message); /* j0 = ctx->input[0]; j1 = ctx->input[1]; j2 = ctx->input[2]; j3 = ctx->input[3]; j4 = ctx->input[4]; j5 = ctx->input[5]; j6 = ctx->input[6]; j7 = ctx->input[7]; j8 = ctx->input[8]; j9 = ctx->input[9]; j10 = ctx->input[10]; j11 = ctx->input[11]; j12 = ctx->input[12]; j13 = ctx->input[13]; j14 = ctx->input[14]; j15 = ctx->input[15]; */ $j0 = (int) $ctx[0]; $j1 = (int) $ctx[1]; $j2 = (int) $ctx[2]; $j3 = (int) $ctx[3]; $j4 = (int) $ctx[4]; $j5 = (int) $ctx[5]; $j6 = (int) $ctx[6]; $j7 = (int) $ctx[7]; $j8 = (int) $ctx[8]; $j9 = (int) $ctx[9]; $j10 = (int) $ctx[10]; $j11 = (int) $ctx[11]; $j12 = (int) $ctx[12]; $j13 = (int) $ctx[13]; $j14 = (int) $ctx[14]; $j15 = (int) $ctx[15]; $c = ''; for (;;) { if ($bytes < 64) { $message .= str_repeat("\x00", 64 - $bytes); } $x0 = (int) $j0; $x1 = (int) $j1; $x2 = (int) $j2; $x3 = (int) $j3; $x4 = (int) $j4; $x5 = (int) $j5; $x6 = (int) $j6; $x7 = (int) $j7; $x8 = (int) $j8; $x9 = (int) $j9; $x10 = (int) $j10; $x11 = (int) $j11; $x12 = (int) $j12; $x13 = (int) $j13; $x14 = (int) $j14; $x15 = (int) $j15; # for (i = 20; i > 0; i -= 2) { for ($i = 20; $i > 0; $i -= 2) { # QUARTERROUND( x0, x4, x8, x12) list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); # QUARTERROUND( x1, x5, x9, x13) list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); # QUARTERROUND( x2, x6, x10, x14) list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); # QUARTERROUND( x3, x7, x11, x15) list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); # QUARTERROUND( x0, x5, x10, x15) list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); # QUARTERROUND( x1, x6, x11, x12) list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); # QUARTERROUND( x2, x7, x8, x13) list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); # QUARTERROUND( x3, x4, x9, x14) list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); } /* x0 = PLUS(x0, j0); x1 = PLUS(x1, j1); x2 = PLUS(x2, j2); x3 = PLUS(x3, j3); x4 = PLUS(x4, j4); x5 = PLUS(x5, j5); x6 = PLUS(x6, j6); x7 = PLUS(x7, j7); x8 = PLUS(x8, j8); x9 = PLUS(x9, j9); x10 = PLUS(x10, j10); x11 = PLUS(x11, j11); x12 = PLUS(x12, j12); x13 = PLUS(x13, j13); x14 = PLUS(x14, j14); x15 = PLUS(x15, j15); */ /** @var int $x0 */ $x0 = ($x0 & 0xffffffff) + $j0; /** @var int $x1 */ $x1 = ($x1 & 0xffffffff) + $j1; /** @var int $x2 */ $x2 = ($x2 & 0xffffffff) + $j2; /** @var int $x3 */ $x3 = ($x3 & 0xffffffff) + $j3; /** @var int $x4 */ $x4 = ($x4 & 0xffffffff) + $j4; /** @var int $x5 */ $x5 = ($x5 & 0xffffffff) + $j5; /** @var int $x6 */ $x6 = ($x6 & 0xffffffff) + $j6; /** @var int $x7 */ $x7 = ($x7 & 0xffffffff) + $j7; /** @var int $x8 */ $x8 = ($x8 & 0xffffffff) + $j8; /** @var int $x9 */ $x9 = ($x9 & 0xffffffff) + $j9; /** @var int $x10 */ $x10 = ($x10 & 0xffffffff) + $j10; /** @var int $x11 */ $x11 = ($x11 & 0xffffffff) + $j11; /** @var int $x12 */ $x12 = ($x12 & 0xffffffff) + $j12; /** @var int $x13 */ $x13 = ($x13 & 0xffffffff) + $j13; /** @var int $x14 */ $x14 = ($x14 & 0xffffffff) + $j14; /** @var int $x15 */ $x15 = ($x15 & 0xffffffff) + $j15; /* x0 = XOR(x0, LOAD32_LE(m + 0)); x1 = XOR(x1, LOAD32_LE(m + 4)); x2 = XOR(x2, LOAD32_LE(m + 8)); x3 = XOR(x3, LOAD32_LE(m + 12)); x4 = XOR(x4, LOAD32_LE(m + 16)); x5 = XOR(x5, LOAD32_LE(m + 20)); x6 = XOR(x6, LOAD32_LE(m + 24)); x7 = XOR(x7, LOAD32_LE(m + 28)); x8 = XOR(x8, LOAD32_LE(m + 32)); x9 = XOR(x9, LOAD32_LE(m + 36)); x10 = XOR(x10, LOAD32_LE(m + 40)); x11 = XOR(x11, LOAD32_LE(m + 44)); x12 = XOR(x12, LOAD32_LE(m + 48)); x13 = XOR(x13, LOAD32_LE(m + 52)); x14 = XOR(x14, LOAD32_LE(m + 56)); x15 = XOR(x15, LOAD32_LE(m + 60)); */ $x0 ^= self::load_4(self::substr($message, 0, 4)); $x1 ^= self::load_4(self::substr($message, 4, 4)); $x2 ^= self::load_4(self::substr($message, 8, 4)); $x3 ^= self::load_4(self::substr($message, 12, 4)); $x4 ^= self::load_4(self::substr($message, 16, 4)); $x5 ^= self::load_4(self::substr($message, 20, 4)); $x6 ^= self::load_4(self::substr($message, 24, 4)); $x7 ^= self::load_4(self::substr($message, 28, 4)); $x8 ^= self::load_4(self::substr($message, 32, 4)); $x9 ^= self::load_4(self::substr($message, 36, 4)); $x10 ^= self::load_4(self::substr($message, 40, 4)); $x11 ^= self::load_4(self::substr($message, 44, 4)); $x12 ^= self::load_4(self::substr($message, 48, 4)); $x13 ^= self::load_4(self::substr($message, 52, 4)); $x14 ^= self::load_4(self::substr($message, 56, 4)); $x15 ^= self::load_4(self::substr($message, 60, 4)); /* j12 = PLUSONE(j12); if (!j12) { j13 = PLUSONE(j13); } */ ++$j12; if ($j12 & 0xf0000000) { throw new SodiumException('Overflow'); } /* STORE32_LE(c + 0, x0); STORE32_LE(c + 4, x1); STORE32_LE(c + 8, x2); STORE32_LE(c + 12, x3); STORE32_LE(c + 16, x4); STORE32_LE(c + 20, x5); STORE32_LE(c + 24, x6); STORE32_LE(c + 28, x7); STORE32_LE(c + 32, x8); STORE32_LE(c + 36, x9); STORE32_LE(c + 40, x10); STORE32_LE(c + 44, x11); STORE32_LE(c + 48, x12); STORE32_LE(c + 52, x13); STORE32_LE(c + 56, x14); STORE32_LE(c + 60, x15); */ $block = self::store32_le((int) ($x0 & 0xffffffff)) . self::store32_le((int) ($x1 & 0xffffffff)) . self::store32_le((int) ($x2 & 0xffffffff)) . self::store32_le((int) ($x3 & 0xffffffff)) . self::store32_le((int) ($x4 & 0xffffffff)) . self::store32_le((int) ($x5 & 0xffffffff)) . self::store32_le((int) ($x6 & 0xffffffff)) . self::store32_le((int) ($x7 & 0xffffffff)) . self::store32_le((int) ($x8 & 0xffffffff)) . self::store32_le((int) ($x9 & 0xffffffff)) . self::store32_le((int) ($x10 & 0xffffffff)) . self::store32_le((int) ($x11 & 0xffffffff)) . self::store32_le((int) ($x12 & 0xffffffff)) . self::store32_le((int) ($x13 & 0xffffffff)) . self::store32_le((int) ($x14 & 0xffffffff)) . self::store32_le((int) ($x15 & 0xffffffff)); /* Partial block */ if ($bytes < 64) { $c .= self::substr($block, 0, $bytes); break; } /* Full block */ $c .= $block; $bytes -= 64; if ($bytes <= 0) { break; } $message = self::substr($message, 64); } /* end for(;;) loop */ $ctx[12] = $j12; $ctx[13] = $j13; return $c; } /** * @internal You should not use this directly from another application * * @param int $len * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function stream($len, $nonce, $key) { return self::encryptBytes( new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce), str_repeat("\x00", $len) ); } /** * @internal You should not use this directly from another application * * @param int $len * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function ietfStream($len, $nonce, $key) { return self::encryptBytes( new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce), str_repeat("\x00", $len) ); } /** * @internal You should not use this directly from another application * * @param string $message * @param string $nonce * @param string $key * @param string $ic * @return string * @throws SodiumException * @throws TypeError */ public static function ietfStreamXorIc($message, $nonce, $key, $ic = '') { return self::encryptBytes( new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce, $ic), $message ); } /** * @internal You should not use this directly from another application * * @param string $message * @param string $nonce * @param string $key * @param string $ic * @return string * @throws SodiumException * @throws TypeError */ public static function streamXorIc($message, $nonce, $key, $ic = '') { return self::encryptBytes( new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce, $ic), $message ); } } Core/ChaCha20/error_log 0000644 00000010174 15162213725 0010631 0 ustar 00 [24-Mar-2026 21:40:38 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php on line 10 [24-Mar-2026 23:24:57 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20_Ctx" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php on line 10 [25-Mar-2026 03:58:10 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php on line 10 [25-Mar-2026 04:13:23 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20_Ctx" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php on line 10 [25-Mar-2026 15:27:32 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php on line 10 [25-Mar-2026 15:27:38 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20_Ctx" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php on line 10 [26-Mar-2026 09:09:31 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php on line 10 [26-Mar-2026 10:48:09 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20_Ctx" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php on line 10 [26-Mar-2026 14:57:59 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php on line 10 [26-Mar-2026 15:15:03 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20_Ctx" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php on line 10 [28-Mar-2026 09:21:11 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20_Ctx" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php on line 10 [28-Mar-2026 09:34:04 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php on line 10 [30-Mar-2026 09:00:38 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_ChaCha20_Ctx" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php on line 10 Core/ChaCha20/Ctx.php 0000644 00000010477 15162213726 0010172 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_ChaCha20_Ctx', false)) { return; } /** * Class ParagonIE_Sodium_Core_ChaCha20_Ctx */ class ParagonIE_Sodium_Core_ChaCha20_Ctx extends ParagonIE_Sodium_Core_Util implements ArrayAccess { /** * @var SplFixedArray internally, <int, int> */ protected $container; /** * ParagonIE_Sodium_Core_ChaCha20_Ctx constructor. * * @internal You should not use this directly from another application * * @param string $key ChaCha20 key. * @param string $iv Initialization Vector (a.k.a. nonce). * @param string $counter The initial counter value. * Defaults to 8 0x00 bytes. * @throws InvalidArgumentException * @throws TypeError */ public function __construct($key = '', $iv = '', $counter = '') { if (self::strlen($key) !== 32) { throw new InvalidArgumentException('ChaCha20 expects a 256-bit key.'); } if (self::strlen($iv) !== 8) { throw new InvalidArgumentException('ChaCha20 expects a 64-bit nonce.'); } $this->container = new SplFixedArray(16); /* "expand 32-byte k" as per ChaCha20 spec */ $this->container[0] = 0x61707865; $this->container[1] = 0x3320646e; $this->container[2] = 0x79622d32; $this->container[3] = 0x6b206574; $this->container[4] = self::load_4(self::substr($key, 0, 4)); $this->container[5] = self::load_4(self::substr($key, 4, 4)); $this->container[6] = self::load_4(self::substr($key, 8, 4)); $this->container[7] = self::load_4(self::substr($key, 12, 4)); $this->container[8] = self::load_4(self::substr($key, 16, 4)); $this->container[9] = self::load_4(self::substr($key, 20, 4)); $this->container[10] = self::load_4(self::substr($key, 24, 4)); $this->container[11] = self::load_4(self::substr($key, 28, 4)); $counter = $this->initCounter($counter); $this->container[12] = self::load_4(self::substr($counter, 0, 4)); $this->container[13] = self::load_4(self::substr($counter, 4, 4)); $this->container[14] = self::load_4(self::substr($iv, 0, 4)); $this->container[15] = self::load_4(self::substr($iv, 4, 4)); } /** * @internal You should not use this directly from another application * * @param int $offset * @param int $value * @return void * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] public function offsetSet($offset, $value) { if (!is_int($offset)) { throw new InvalidArgumentException('Expected an integer'); } if (!is_int($value)) { throw new InvalidArgumentException('Expected an integer'); } $this->container[$offset] = $value; } /** * @internal You should not use this directly from another application * * @param int $offset * @return bool */ #[ReturnTypeWillChange] public function offsetExists($offset) { return isset($this->container[$offset]); } /** * @internal You should not use this directly from another application * * @param int $offset * @return void * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] public function offsetUnset($offset) { unset($this->container[$offset]); } /** * @internal You should not use this directly from another application * * @param int $offset * @return mixed|null * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] public function offsetGet($offset) { return isset($this->container[$offset]) ? $this->container[$offset] : null; } /** * Initialize (pad) a counter value. * @throws SodiumException * * @param string $ctr * @return string */ public function initCounter( #[SensitiveParameter] $ctr ) { $len = self::strlen($ctr); if ($len === 0) { return str_repeat("\0", 8); } if ($len < 8) { return $ctr . str_repeat("\0", 8 - $len); } if ($len > 8) { throw new SodiumException("counter cannot be more than 8 bytes"); } return $ctr; } } Core/ChaCha20/IetfCtx.php 0000644 00000002454 15162213727 0010777 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_ChaCha20_IetfCtx', false)) { return; } /** * Class ParagonIE_Sodium_Core_ChaCha20_IetfCtx */ class ParagonIE_Sodium_Core_ChaCha20_IetfCtx extends ParagonIE_Sodium_Core_ChaCha20_Ctx { /** * ParagonIE_Sodium_Core_ChaCha20_IetfCtx constructor. * * @internal You should not use this directly from another application * * @param string $key ChaCha20 key. * @param string $iv Initialization Vector (a.k.a. nonce). * @param string $counter The initial counter value. * Defaults to 4 0x00 bytes. * @throws InvalidArgumentException * @throws TypeError */ public function __construct($key = '', $iv = '', $counter = '') { if (self::strlen($iv) !== 12) { throw new InvalidArgumentException('ChaCha20 expects a 96-bit nonce in IETF mode.'); } $counter = $this->initCounter($counter); parent::__construct($key, self::substr($iv, 0, 8), $counter); $this->container[12] = self::load_4(self::substr($counter, 0, 4)); $this->container[13] = self::load_4(self::substr($iv, 0, 4)); $this->container[14] = self::load_4(self::substr($iv, 4, 4)); $this->container[15] = self::load_4(self::substr($iv, 8, 4)); } } Core/AEGIS256.php 0000644 00000007016 15162213727 0007244 0 ustar 00 <?php if (!defined('SODIUM_COMPAT_AEGIS_C0')) { define('SODIUM_COMPAT_AEGIS_C0', "\x00\x01\x01\x02\x03\x05\x08\x0d\x15\x22\x37\x59\x90\xe9\x79\x62"); } if (!defined('SODIUM_COMPAT_AEGIS_C1')) { define('SODIUM_COMPAT_AEGIS_C1', "\xdb\x3d\x18\x55\x6d\xc2\x2f\xf1\x20\x11\x31\x42\x73\xb5\x28\xdd"); } class ParagonIE_Sodium_Core_AEGIS256 extends ParagonIE_Sodium_Core_AES { /** * @param string $ct * @param string $tag * @param string $ad * @param string $key * @param string $nonce * @return string * @throws SodiumException */ public static function decrypt($ct, $tag, $ad, $key, $nonce) { $state = self::init($key, $nonce); // ad_blocks = Split(ZeroPad(ad, 128), 128) $ad_blocks = (self::strlen($ad) + 15) >> 4; // for ai in ad_blocks: // Absorb(ai) for ($i = 0; $i < $ad_blocks; ++$i) { $ai = self::substr($ad, $i << 4, 16); if (self::strlen($ai) < 16) { $ai = str_pad($ai, 16, "\0", STR_PAD_RIGHT); } $state->absorb($ai); } $msg = ''; $cn = self::strlen($ct) & 15; $ct_blocks = self::strlen($ct) >> 4; // ct_blocks = Split(ZeroPad(ct, 128), 128) // cn = Tail(ct, |ct| mod 128) for ($i = 0; $i < $ct_blocks; ++$i) { $msg .= $state->dec(self::substr($ct, $i << 4, 16)); } // if cn is not empty: // msg = msg || DecPartial(cn) if ($cn) { $start = $ct_blocks << 4; $msg .= $state->decPartial(self::substr($ct, $start, $cn)); } $expected_tag = $state->finalize( self::strlen($ad) << 3, self::strlen($msg) << 3 ); if (!self::hashEquals($expected_tag, $tag)) { try { // The RFC says to erase msg, so we shall try: ParagonIE_Sodium_Compat::memzero($msg); } catch (SodiumException $ex) { // Do nothing if we cannot memzero } throw new SodiumException('verification failed'); } return $msg; } /** * @param string $msg * @param string $ad * @param string $key * @param string $nonce * @return array * @throws SodiumException */ public static function encrypt($msg, $ad, $key, $nonce) { $state = self::init($key, $nonce); $ad_len = self::strlen($ad); $msg_len = self::strlen($msg); $ad_blocks = ($ad_len + 15) >> 4; for ($i = 0; $i < $ad_blocks; ++$i) { $ai = self::substr($ad, $i << 4, 16); if (self::strlen($ai) < 16) { $ai = str_pad($ai, 16, "\0", STR_PAD_RIGHT); } $state->absorb($ai); } $ct = ''; $msg_blocks = ($msg_len + 15) >> 4; for ($i = 0; $i < $msg_blocks; ++$i) { $xi = self::substr($msg, $i << 4, 16); if (self::strlen($xi) < 16) { $xi = str_pad($xi, 16, "\0", STR_PAD_RIGHT); } $ct .= $state->enc($xi); } $tag = $state->finalize( $ad_len << 3, $msg_len << 3 ); return array( self::substr($ct, 0, $msg_len), $tag ); } /** * @param string $key * @param string $nonce * @return ParagonIE_Sodium_Core_AEGIS_State256 */ public static function init($key, $nonce) { return ParagonIE_Sodium_Core_AEGIS_State256::init($key, $nonce); } } Core/Ristretto255.php 0000644 00000052574 15162213730 0010415 0 ustar 00 <?php /** * Class ParagonIE_Sodium_Core_Ristretto255 */ class ParagonIE_Sodium_Core_Ristretto255 extends ParagonIE_Sodium_Core_Ed25519 { const crypto_core_ristretto255_HASHBYTES = 64; const HASH_SC_L = 48; const CORE_H2C_SHA256 = 1; const CORE_H2C_SHA512 = 2; /** * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @param int $b * @return ParagonIE_Sodium_Core_Curve25519_Fe */ public static function fe_cneg(ParagonIE_Sodium_Core_Curve25519_Fe $f, $b) { $negf = self::fe_neg($f); return self::fe_cmov($f, $negf, $b); } /** * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @return ParagonIE_Sodium_Core_Curve25519_Fe * @throws SodiumException */ public static function fe_abs(ParagonIE_Sodium_Core_Curve25519_Fe $f) { return self::fe_cneg($f, self::fe_isnegative($f)); } /** * Returns 0 if this field element results in all NUL bytes. * * @internal You should not use this directly from another application * * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @return int * @throws SodiumException */ public static function fe_iszero(ParagonIE_Sodium_Core_Curve25519_Fe $f) { static $zero; if ($zero === null) { $zero = str_repeat("\x00", 32); } /** @var string $zero */ $str = self::fe_tobytes($f); $d = 0; for ($i = 0; $i < 32; ++$i) { $d |= self::chrToInt($str[$i]); } return (($d - 1) >> 31) & 1; } /** * @param ParagonIE_Sodium_Core_Curve25519_Fe $u * @param ParagonIE_Sodium_Core_Curve25519_Fe $v * @return array{x: ParagonIE_Sodium_Core_Curve25519_Fe, nonsquare: int} * * @throws SodiumException */ public static function ristretto255_sqrt_ratio_m1( ParagonIE_Sodium_Core_Curve25519_Fe $u, ParagonIE_Sodium_Core_Curve25519_Fe $v ) { $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtm1); $v3 = self::fe_mul( self::fe_sq($v), $v ); /* v3 = v^3 */ $x = self::fe_mul( self::fe_mul( self::fe_sq($v3), $u ), $v ); /* x = uv^7 */ $x = self::fe_mul( self::fe_mul( self::fe_pow22523($x), /* x = (uv^7)^((q-5)/8) */ $v3 ), $u ); /* x = uv^3(uv^7)^((q-5)/8) */ $vxx = self::fe_mul( self::fe_sq($x), $v ); /* vx^2 */ $m_root_check = self::fe_sub($vxx, $u); /* vx^2-u */ $p_root_check = self::fe_add($vxx, $u); /* vx^2+u */ $f_root_check = self::fe_mul($u, $sqrtm1); /* u*sqrt(-1) */ $f_root_check = self::fe_add($vxx, $f_root_check); /* vx^2+u*sqrt(-1) */ $has_m_root = self::fe_iszero($m_root_check); $has_p_root = self::fe_iszero($p_root_check); $has_f_root = self::fe_iszero($f_root_check); $x_sqrtm1 = self::fe_mul($x, $sqrtm1); /* x*sqrt(-1) */ $x = self::fe_abs( self::fe_cmov($x, $x_sqrtm1, $has_p_root | $has_f_root) ); return array( 'x' => $x, 'nonsquare' => $has_m_root | $has_p_root ); } /** * @param string $s * @return int * @throws SodiumException */ public static function ristretto255_point_is_canonical($s) { $c = (self::chrToInt($s[31]) & 0x7f) ^ 0x7f; for ($i = 30; $i > 0; --$i) { $c |= self::chrToInt($s[$i]) ^ 0xff; } $c = ($c - 1) >> 8; $d = (0xed - 1 - self::chrToInt($s[0])) >> 8; $e = self::chrToInt($s[31]) >> 7; return 1 - ((($c & $d) | $e | self::chrToInt($s[0])) & 1); } /** * @param string $s * @param bool $skipCanonicalCheck * @return array{h: ParagonIE_Sodium_Core_Curve25519_Ge_P3, res: int} * @throws SodiumException */ public static function ristretto255_frombytes($s, $skipCanonicalCheck = false) { if (!$skipCanonicalCheck) { if (!self::ristretto255_point_is_canonical($s)) { throw new SodiumException('S is not canonical'); } } $s_ = self::fe_frombytes($s); $ss = self::fe_sq($s_); /* ss = s^2 */ $u1 = self::fe_sub(self::fe_1(), $ss); /* u1 = 1-ss */ $u1u1 = self::fe_sq($u1); /* u1u1 = u1^2 */ $u2 = self::fe_add(self::fe_1(), $ss); /* u2 = 1+ss */ $u2u2 = self::fe_sq($u2); /* u2u2 = u2^2 */ $v = self::fe_mul( ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$d), $u1u1 ); /* v = d*u1^2 */ $v = self::fe_neg($v); /* v = -d*u1^2 */ $v = self::fe_sub($v, $u2u2); /* v = -(d*u1^2)-u2^2 */ $v_u2u2 = self::fe_mul($v, $u2u2); /* v_u2u2 = v*u2^2 */ // fe25519_1(one); // notsquare = ristretto255_sqrt_ratio_m1(inv_sqrt, one, v_u2u2); $one = self::fe_1(); $result = self::ristretto255_sqrt_ratio_m1($one, $v_u2u2); $inv_sqrt = $result['x']; $notsquare = $result['nonsquare']; $h = new ParagonIE_Sodium_Core_Curve25519_Ge_P3(); $h->X = self::fe_mul($inv_sqrt, $u2); $h->Y = self::fe_mul(self::fe_mul($inv_sqrt, $h->X), $v); $h->X = self::fe_mul($h->X, $s_); $h->X = self::fe_abs( self::fe_add($h->X, $h->X) ); $h->Y = self::fe_mul($u1, $h->Y); $h->Z = self::fe_1(); $h->T = self::fe_mul($h->X, $h->Y); $res = - ((1 - $notsquare) | self::fe_isnegative($h->T) | self::fe_iszero($h->Y)); return array('h' => $h, 'res' => $res); } /** * @param ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h * @return string * @throws SodiumException */ public static function ristretto255_p3_tobytes(ParagonIE_Sodium_Core_Curve25519_Ge_P3 $h) { $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtm1); $invsqrtamd = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$invsqrtamd); $u1 = self::fe_add($h->Z, $h->Y); /* u1 = Z+Y */ $zmy = self::fe_sub($h->Z, $h->Y); /* zmy = Z-Y */ $u1 = self::fe_mul($u1, $zmy); /* u1 = (Z+Y)*(Z-Y) */ $u2 = self::fe_mul($h->X, $h->Y); /* u2 = X*Y */ $u1_u2u2 = self::fe_mul(self::fe_sq($u2), $u1); /* u1_u2u2 = u1*u2^2 */ $one = self::fe_1(); // fe25519_1(one); // (void) ristretto255_sqrt_ratio_m1(inv_sqrt, one, u1_u2u2); $result = self::ristretto255_sqrt_ratio_m1($one, $u1_u2u2); $inv_sqrt = $result['x']; $den1 = self::fe_mul($inv_sqrt, $u1); /* den1 = inv_sqrt*u1 */ $den2 = self::fe_mul($inv_sqrt, $u2); /* den2 = inv_sqrt*u2 */ $z_inv = self::fe_mul($h->T, self::fe_mul($den1, $den2)); /* z_inv = den1*den2*T */ $ix = self::fe_mul($h->X, $sqrtm1); /* ix = X*sqrt(-1) */ $iy = self::fe_mul($h->Y, $sqrtm1); /* iy = Y*sqrt(-1) */ $eden = self::fe_mul($den1, $invsqrtamd); $t_z_inv = self::fe_mul($h->T, $z_inv); /* t_z_inv = T*z_inv */ $rotate = self::fe_isnegative($t_z_inv); $x_ = self::fe_copy($h->X); $y_ = self::fe_copy($h->Y); $den_inv = self::fe_copy($den2); $x_ = self::fe_cmov($x_, $iy, $rotate); $y_ = self::fe_cmov($y_, $ix, $rotate); $den_inv = self::fe_cmov($den_inv, $eden, $rotate); $x_z_inv = self::fe_mul($x_, $z_inv); $y_ = self::fe_cneg($y_, self::fe_isnegative($x_z_inv)); // fe25519_sub(s_, h->Z, y_); // fe25519_mul(s_, den_inv, s_); // fe25519_abs(s_, s_); // fe25519_tobytes(s, s_); return self::fe_tobytes( self::fe_abs( self::fe_mul( $den_inv, self::fe_sub($h->Z, $y_) ) ) ); } /** * @param ParagonIE_Sodium_Core_Curve25519_Fe $t * @return ParagonIE_Sodium_Core_Curve25519_Ge_P3 * * @throws SodiumException */ public static function ristretto255_elligator(ParagonIE_Sodium_Core_Curve25519_Fe $t) { $sqrtm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtm1); $onemsqd = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$onemsqd); $d = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$d); $sqdmone = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqdmone); $sqrtadm1 = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::$sqrtadm1); $one = self::fe_1(); $r = self::fe_mul($sqrtm1, self::fe_sq($t)); /* r = sqrt(-1)*t^2 */ $u = self::fe_mul(self::fe_add($r, $one), $onemsqd); /* u = (r+1)*(1-d^2) */ $c = self::fe_neg(self::fe_1()); /* c = -1 */ $rpd = self::fe_add($r, $d); /* rpd = r+d */ $v = self::fe_mul( self::fe_sub( $c, self::fe_mul($r, $d) ), $rpd ); /* v = (c-r*d)*(r+d) */ $result = self::ristretto255_sqrt_ratio_m1($u, $v); $s = $result['x']; $wasnt_square = 1 - $result['nonsquare']; $s_prime = self::fe_neg( self::fe_abs( self::fe_mul($s, $t) ) ); /* s_prime = -|s*t| */ $s = self::fe_cmov($s, $s_prime, $wasnt_square); $c = self::fe_cmov($c, $r, $wasnt_square); // fe25519_sub(n, r, one); /* n = r-1 */ // fe25519_mul(n, n, c); /* n = c*(r-1) */ // fe25519_mul(n, n, ed25519_sqdmone); /* n = c*(r-1)*(d-1)^2 */ // fe25519_sub(n, n, v); /* n = c*(r-1)*(d-1)^2-v */ $n = self::fe_sub( self::fe_mul( self::fe_mul( self::fe_sub($r, $one), $c ), $sqdmone ), $v ); /* n = c*(r-1)*(d-1)^2-v */ $w0 = self::fe_mul( self::fe_add($s, $s), $v ); /* w0 = 2s*v */ $w1 = self::fe_mul($n, $sqrtadm1); /* w1 = n*sqrt(ad-1) */ $ss = self::fe_sq($s); /* ss = s^2 */ $w2 = self::fe_sub($one, $ss); /* w2 = 1-s^2 */ $w3 = self::fe_add($one, $ss); /* w3 = 1+s^2 */ return new ParagonIE_Sodium_Core_Curve25519_Ge_P3( self::fe_mul($w0, $w3), self::fe_mul($w2, $w1), self::fe_mul($w1, $w3), self::fe_mul($w0, $w2) ); } /** * @param string $h * @return string * @throws SodiumException */ public static function ristretto255_from_hash($h) { if (self::strlen($h) !== 64) { throw new SodiumException('Hash must be 64 bytes'); } //fe25519_frombytes(r0, h); //fe25519_frombytes(r1, h + 32); $r0 = self::fe_frombytes(self::substr($h, 0, 32)); $r1 = self::fe_frombytes(self::substr($h, 32, 32)); //ristretto255_elligator(&p0, r0); //ristretto255_elligator(&p1, r1); $p0 = self::ristretto255_elligator($r0); $p1 = self::ristretto255_elligator($r1); //ge25519_p3_to_cached(&p1_cached, &p1); //ge25519_add_cached(&p_p1p1, &p0, &p1_cached); $p_p1p1 = self::ge_add( $p0, self::ge_p3_to_cached($p1) ); //ge25519_p1p1_to_p3(&p, &p_p1p1); //ristretto255_p3_tobytes(s, &p); return self::ristretto255_p3_tobytes( self::ge_p1p1_to_p3($p_p1p1) ); } /** * @param string $p * @return int * @throws SodiumException */ public static function is_valid_point($p) { $result = self::ristretto255_frombytes($p); if ($result['res'] !== 0) { return 0; } return 1; } /** * @param string $p * @param string $q * @return string * @throws SodiumException */ public static function ristretto255_add($p, $q) { $p_res = self::ristretto255_frombytes($p); $q_res = self::ristretto255_frombytes($q); if ($p_res['res'] !== 0 || $q_res['res'] !== 0) { throw new SodiumException('Could not add points'); } $p_p3 = $p_res['h']; $q_p3 = $q_res['h']; $q_cached = self::ge_p3_to_cached($q_p3); $r_p1p1 = self::ge_add($p_p3, $q_cached); $r_p3 = self::ge_p1p1_to_p3($r_p1p1); return self::ristretto255_p3_tobytes($r_p3); } /** * @param string $p * @param string $q * @return string * @throws SodiumException */ public static function ristretto255_sub($p, $q) { $p_res = self::ristretto255_frombytes($p); $q_res = self::ristretto255_frombytes($q); if ($p_res['res'] !== 0 || $q_res['res'] !== 0) { throw new SodiumException('Could not add points'); } $p_p3 = $p_res['h']; $q_p3 = $q_res['h']; $q_cached = self::ge_p3_to_cached($q_p3); $r_p1p1 = self::ge_sub($p_p3, $q_cached); $r_p3 = self::ge_p1p1_to_p3($r_p1p1); return self::ristretto255_p3_tobytes($r_p3); } /** * @param int $hLen * @param ?string $ctx * @param string $msg * @return string * @throws SodiumException * @psalm-suppress PossiblyInvalidArgument hash API */ protected static function h2c_string_to_hash_sha256($hLen, $ctx, $msg) { $h = array_fill(0, $hLen, 0); $ctx_len = !is_null($ctx) ? self::strlen($ctx) : 0; if ($hLen > 0xff) { throw new SodiumException('Hash must be less than 256 bytes'); } if ($ctx_len > 0xff) { $st = hash_init('sha256'); self::hash_update($st, "H2C-OVERSIZE-DST-"); self::hash_update($st, $ctx); $ctx = hash_final($st, true); $ctx_len = 32; } $t = array(0, $hLen, 0); $ux = str_repeat("\0", 64); $st = hash_init('sha256'); self::hash_update($st, $ux); self::hash_update($st, $msg); self::hash_update($st, self::intArrayToString($t)); self::hash_update($st, $ctx); self::hash_update($st, self::intToChr($ctx_len)); $u0 = hash_final($st, true); for ($i = 0; $i < $hLen; $i += 64) { $ux = self::xorStrings($ux, $u0); ++$t[2]; $st = hash_init('sha256'); self::hash_update($st, $ux); self::hash_update($st, self::intToChr($t[2])); self::hash_update($st, $ctx); self::hash_update($st, self::intToChr($ctx_len)); $ux = hash_final($st, true); $amount = min($hLen - $i, 64); for ($j = 0; $j < $amount; ++$j) { $h[$i + $j] = self::chrToInt($ux[$i]); } } return self::intArrayToString(array_slice($h, 0, $hLen)); } /** * @param int $hLen * @param ?string $ctx * @param string $msg * @return string * @throws SodiumException * @psalm-suppress PossiblyInvalidArgument hash API */ protected static function h2c_string_to_hash_sha512($hLen, $ctx, $msg) { $h = array_fill(0, $hLen, 0); $ctx_len = !is_null($ctx) ? self::strlen($ctx) : 0; if ($hLen > 0xff) { throw new SodiumException('Hash must be less than 256 bytes'); } if ($ctx_len > 0xff) { $st = hash_init('sha256'); self::hash_update($st, "H2C-OVERSIZE-DST-"); self::hash_update($st, $ctx); $ctx = hash_final($st, true); $ctx_len = 32; } $t = array(0, $hLen, 0); $ux = str_repeat("\0", 128); $st = hash_init('sha512'); self::hash_update($st, $ux); self::hash_update($st, $msg); self::hash_update($st, self::intArrayToString($t)); self::hash_update($st, $ctx); self::hash_update($st, self::intToChr($ctx_len)); $u0 = hash_final($st, true); for ($i = 0; $i < $hLen; $i += 128) { $ux = self::xorStrings($ux, $u0); ++$t[2]; $st = hash_init('sha512'); self::hash_update($st, $ux); self::hash_update($st, self::intToChr($t[2])); self::hash_update($st, $ctx); self::hash_update($st, self::intToChr($ctx_len)); $ux = hash_final($st, true); $amount = min($hLen - $i, 128); for ($j = 0; $j < $amount; ++$j) { $h[$i + $j] = self::chrToInt($ux[$i]); } } return self::intArrayToString(array_slice($h, 0, $hLen)); } /** * @param int $hLen * @param ?string $ctx * @param string $msg * @param int $hash_alg * @return string * @throws SodiumException */ public static function h2c_string_to_hash($hLen, $ctx, $msg, $hash_alg) { switch ($hash_alg) { case self::CORE_H2C_SHA256: return self::h2c_string_to_hash_sha256($hLen, $ctx, $msg); case self::CORE_H2C_SHA512: return self::h2c_string_to_hash_sha512($hLen, $ctx, $msg); default: throw new SodiumException('Invalid H2C hash algorithm'); } } /** * @param ?string $ctx * @param string $msg * @param int $hash_alg * @return string * @throws SodiumException */ protected static function _string_to_element($ctx, $msg, $hash_alg) { return self::ristretto255_from_hash( self::h2c_string_to_hash(self::crypto_core_ristretto255_HASHBYTES, $ctx, $msg, $hash_alg) ); } /** * @return string * @throws SodiumException * @throws Exception */ public static function ristretto255_random() { return self::ristretto255_from_hash( ParagonIE_Sodium_Compat::randombytes_buf(self::crypto_core_ristretto255_HASHBYTES) ); } /** * @return string * @throws SodiumException */ public static function ristretto255_scalar_random() { return self::scalar_random(); } /** * @param string $s * @return string * @throws SodiumException */ public static function ristretto255_scalar_complement($s) { return self::scalar_complement($s); } /** * @param string $s * @return string */ public static function ristretto255_scalar_invert($s) { return self::sc25519_invert($s); } /** * @param string $s * @return string * @throws SodiumException */ public static function ristretto255_scalar_negate($s) { return self::scalar_negate($s); } /** * @param string $x * @param string $y * @return string */ public static function ristretto255_scalar_add($x, $y) { return self::scalar_add($x, $y); } /** * @param string $x * @param string $y * @return string */ public static function ristretto255_scalar_sub($x, $y) { return self::scalar_sub($x, $y); } /** * @param string $x * @param string $y * @return string */ public static function ristretto255_scalar_mul($x, $y) { return self::sc25519_mul($x, $y); } /** * @param string $ctx * @param string $msg * @param int $hash_alg * @return string * @throws SodiumException */ public static function ristretto255_scalar_from_string($ctx, $msg, $hash_alg) { $h = array_fill(0, 64, 0); $h_be = self::stringToIntArray( self::h2c_string_to_hash( self::HASH_SC_L, $ctx, $msg, $hash_alg ) ); for ($i = 0; $i < self::HASH_SC_L; ++$i) { $h[$i] = $h_be[self::HASH_SC_L - 1 - $i]; } return self::ristretto255_scalar_reduce(self::intArrayToString($h)); } /** * @param string $s * @return string */ public static function ristretto255_scalar_reduce($s) { return self::sc_reduce($s); } /** * @param string $n * @param string $p * @return string * @throws SodiumException */ public static function scalarmult_ristretto255($n, $p) { if (self::strlen($n) !== 32) { throw new SodiumException('Scalar must be 32 bytes, ' . self::strlen($p) . ' given.'); } if (self::strlen($p) !== 32) { throw new SodiumException('Point must be 32 bytes, ' . self::strlen($p) . ' given.'); } $result = self::ristretto255_frombytes($p); if ($result['res'] !== 0) { throw new SodiumException('Could not multiply points'); } $P = $result['h']; $t = self::stringToIntArray($n); $t[31] &= 0x7f; $Q = self::ge_scalarmult(self::intArrayToString($t), $P); $q = self::ristretto255_p3_tobytes($Q); if (ParagonIE_Sodium_Compat::is_zero($q)) { throw new SodiumException('An unknown error has occurred'); } return $q; } /** * @param string $n * @return string * @throws SodiumException */ public static function scalarmult_ristretto255_base($n) { $t = self::stringToIntArray($n); $t[31] &= 0x7f; $Q = self::ge_scalarmult_base(self::intArrayToString($t)); $q = self::ristretto255_p3_tobytes($Q); if (ParagonIE_Sodium_Compat::is_zero($q)) { throw new SodiumException('An unknown error has occurred'); } return $q; } } Core/AES/KeySchedule.php 0000644 00000003531 15162213730 0010764 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_AES_KeySchedule', false)) { return; } /** * @internal This should only be used by sodium_compat */ class ParagonIE_Sodium_Core_AES_KeySchedule { /** @var array<int, int> $skey -- has size 120 */ protected $skey; /** @var bool $expanded */ protected $expanded = false; /** @var int $numRounds */ private $numRounds; /** * @param array $skey * @param int $numRounds */ public function __construct(array $skey, $numRounds = 10) { $this->skey = $skey; $this->numRounds = $numRounds; } /** * Get a value at an arbitrary index. Mostly used for unit testing. * * @param int $i * @return int */ public function get($i) { return $this->skey[$i]; } /** * @return int */ public function getNumRounds() { return $this->numRounds; } /** * @param int $offset * @return ParagonIE_Sodium_Core_AES_Block */ public function getRoundKey($offset) { return ParagonIE_Sodium_Core_AES_Block::fromArray( array_slice($this->skey, $offset, 8) ); } /** * Return an expanded key schedule * * @return ParagonIE_Sodium_Core_AES_Expanded */ public function expand() { $exp = new ParagonIE_Sodium_Core_AES_Expanded( array_fill(0, 120, 0), $this->numRounds ); $n = ($exp->numRounds + 1) << 2; for ($u = 0, $v = 0; $u < $n; ++$u, $v += 2) { $x = $y = $this->skey[$u]; $x &= 0x55555555; $exp->skey[$v] = ($x | ($x << 1)) & ParagonIE_Sodium_Core_Util::U32_MAX; $y &= 0xAAAAAAAA; $exp->skey[$v + 1] = ($y | ($y >> 1)) & ParagonIE_Sodium_Core_Util::U32_MAX; } return $exp; } } Core/AES/error_log 0000644 00000003143 15162213731 0007763 0 ustar 00 [24-Mar-2026 21:38:45 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES_KeySchedule" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES/Expanded.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES/Expanded.php on line 10 [25-Mar-2026 05:17:59 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES_KeySchedule" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES/Expanded.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES/Expanded.php on line 10 [26-Mar-2026 10:45:44 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES_KeySchedule" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES/Expanded.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES/Expanded.php on line 10 [26-Mar-2026 16:20:27 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES_KeySchedule" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES/Expanded.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES/Expanded.php on line 10 [28-Mar-2026 09:31:39 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_AES_KeySchedule" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES/Expanded.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/AES/Expanded.php on line 10 Core/AES/Block.php 0000644 00000024342 15162213731 0007615 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_AES_Block', false)) { return; } /** * @internal This should only be used by sodium_compat */ class ParagonIE_Sodium_Core_AES_Block extends SplFixedArray { /** * @var array<int, int> */ protected $values = array(); /** * @var int */ protected $size; /** * @param int $size */ public function __construct($size = 8) { parent::__construct($size); $this->size = $size; $this->values = array_fill(0, $size, 0); } /** * @return self */ public static function init() { return new self(8); } /** * @internal You should not use this directly from another application * * @param array<int, int> $array * @param bool $save_indexes * @return self * * @psalm-suppress MethodSignatureMismatch */ #[ReturnTypeWillChange] public static function fromArray($array, $save_indexes = null) { $count = count($array); if ($save_indexes) { $keys = array_keys($array); } else { $keys = range(0, $count - 1); } $array = array_values($array); /** @var array<int, int> $keys */ $obj = new ParagonIE_Sodium_Core_AES_Block(); if ($save_indexes) { for ($i = 0; $i < $count; ++$i) { $obj->offsetSet($keys[$i], $array[$i]); } } else { for ($i = 0; $i < $count; ++$i) { $obj->offsetSet($i, $array[$i]); } } return $obj; } /** * @internal You should not use this directly from another application * * @param int|null $offset * @param int $value * @return void * * @psalm-suppress MethodSignatureMismatch * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] public function offsetSet($offset, $value) { if (!is_int($value)) { throw new InvalidArgumentException('Expected an integer'); } if (is_null($offset)) { $this->values[] = $value; } else { $this->values[$offset] = $value; } } /** * @internal You should not use this directly from another application * * @param int $offset * @return bool * * @psalm-suppress MethodSignatureMismatch * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] public function offsetExists($offset) { return isset($this->values[$offset]); } /** * @internal You should not use this directly from another application * * @param int $offset * @return void * * @psalm-suppress MethodSignatureMismatch * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] public function offsetUnset($offset) { unset($this->values[$offset]); } /** * @internal You should not use this directly from another application * * @param int $offset * @return int * * @psalm-suppress MethodSignatureMismatch * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] public function offsetGet($offset) { if (!isset($this->values[$offset])) { $this->values[$offset] = 0; } return (int) ($this->values[$offset]); } /** * @internal You should not use this directly from another application * * @return array */ public function __debugInfo() { $out = array(); foreach ($this->values as $v) { $out[] = str_pad(dechex($v), 8, '0', STR_PAD_LEFT); } return array(implode(', ', $out)); /* return array(implode(', ', $this->values)); */ } /** * @param int $cl low bit mask * @param int $ch high bit mask * @param int $s shift * @param int $x index 1 * @param int $y index 2 * @return self */ public function swapN($cl, $ch, $s, $x, $y) { static $u32mask = ParagonIE_Sodium_Core_Util::U32_MAX; $a = $this->values[$x] & $u32mask; $b = $this->values[$y] & $u32mask; // (x) = (a & cl) | ((b & cl) << (s)); $this->values[$x] = ($a & $cl) | ((($b & $cl) << $s) & $u32mask); // (y) = ((a & ch) >> (s)) | (b & ch); $this->values[$y] = ((($a & $ch) & $u32mask) >> $s) | ($b & $ch); return $this; } /** * @param int $x index 1 * @param int $y index 2 * @return self */ public function swap2($x, $y) { return $this->swapN(0x55555555, 0xAAAAAAAA, 1, $x, $y); } /** * @param int $x index 1 * @param int $y index 2 * @return self */ public function swap4($x, $y) { return $this->swapN(0x33333333, 0xCCCCCCCC, 2, $x, $y); } /** * @param int $x index 1 * @param int $y index 2 * @return self */ public function swap8($x, $y) { return $this->swapN(0x0F0F0F0F, 0xF0F0F0F0, 4, $x, $y); } /** * @return self */ public function orthogonalize() { return $this ->swap2(0, 1) ->swap2(2, 3) ->swap2(4, 5) ->swap2(6, 7) ->swap4(0, 2) ->swap4(1, 3) ->swap4(4, 6) ->swap4(5, 7) ->swap8(0, 4) ->swap8(1, 5) ->swap8(2, 6) ->swap8(3, 7); } /** * @return self */ public function shiftRows() { for ($i = 0; $i < 8; ++$i) { $x = $this->values[$i] & ParagonIE_Sodium_Core_Util::U32_MAX; $this->values[$i] = ( ($x & 0x000000FF) | (($x & 0x0000FC00) >> 2) | (($x & 0x00000300) << 6) | (($x & 0x00F00000) >> 4) | (($x & 0x000F0000) << 4) | (($x & 0xC0000000) >> 6) | (($x & 0x3F000000) << 2) ) & ParagonIE_Sodium_Core_Util::U32_MAX; } return $this; } /** * @param int $x * @return int */ public static function rotr16($x) { return (($x << 16) & ParagonIE_Sodium_Core_Util::U32_MAX) | ($x >> 16); } /** * @return self */ public function mixColumns() { $q0 = $this->values[0]; $q1 = $this->values[1]; $q2 = $this->values[2]; $q3 = $this->values[3]; $q4 = $this->values[4]; $q5 = $this->values[5]; $q6 = $this->values[6]; $q7 = $this->values[7]; $r0 = (($q0 >> 8) | ($q0 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $r1 = (($q1 >> 8) | ($q1 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $r2 = (($q2 >> 8) | ($q2 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $r3 = (($q3 >> 8) | ($q3 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $r4 = (($q4 >> 8) | ($q4 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $r5 = (($q5 >> 8) | ($q5 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $r6 = (($q6 >> 8) | ($q6 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $r7 = (($q7 >> 8) | ($q7 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $this->values[0] = $q7 ^ $r7 ^ $r0 ^ self::rotr16($q0 ^ $r0); $this->values[1] = $q0 ^ $r0 ^ $q7 ^ $r7 ^ $r1 ^ self::rotr16($q1 ^ $r1); $this->values[2] = $q1 ^ $r1 ^ $r2 ^ self::rotr16($q2 ^ $r2); $this->values[3] = $q2 ^ $r2 ^ $q7 ^ $r7 ^ $r3 ^ self::rotr16($q3 ^ $r3); $this->values[4] = $q3 ^ $r3 ^ $q7 ^ $r7 ^ $r4 ^ self::rotr16($q4 ^ $r4); $this->values[5] = $q4 ^ $r4 ^ $r5 ^ self::rotr16($q5 ^ $r5); $this->values[6] = $q5 ^ $r5 ^ $r6 ^ self::rotr16($q6 ^ $r6); $this->values[7] = $q6 ^ $r6 ^ $r7 ^ self::rotr16($q7 ^ $r7); return $this; } /** * @return self */ public function inverseMixColumns() { $q0 = $this->values[0]; $q1 = $this->values[1]; $q2 = $this->values[2]; $q3 = $this->values[3]; $q4 = $this->values[4]; $q5 = $this->values[5]; $q6 = $this->values[6]; $q7 = $this->values[7]; $r0 = (($q0 >> 8) | ($q0 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $r1 = (($q1 >> 8) | ($q1 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $r2 = (($q2 >> 8) | ($q2 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $r3 = (($q3 >> 8) | ($q3 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $r4 = (($q4 >> 8) | ($q4 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $r5 = (($q5 >> 8) | ($q5 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $r6 = (($q6 >> 8) | ($q6 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $r7 = (($q7 >> 8) | ($q7 << 24)) & ParagonIE_Sodium_Core_Util::U32_MAX; $this->values[0] = $q5 ^ $q6 ^ $q7 ^ $r0 ^ $r5 ^ $r7 ^ self::rotr16($q0 ^ $q5 ^ $q6 ^ $r0 ^ $r5); $this->values[1] = $q0 ^ $q5 ^ $r0 ^ $r1 ^ $r5 ^ $r6 ^ $r7 ^ self::rotr16($q1 ^ $q5 ^ $q7 ^ $r1 ^ $r5 ^ $r6); $this->values[2] = $q0 ^ $q1 ^ $q6 ^ $r1 ^ $r2 ^ $r6 ^ $r7 ^ self::rotr16($q0 ^ $q2 ^ $q6 ^ $r2 ^ $r6 ^ $r7); $this->values[3] = $q0 ^ $q1 ^ $q2 ^ $q5 ^ $q6 ^ $r0 ^ $r2 ^ $r3 ^ $r5 ^ self::rotr16($q0 ^ $q1 ^ $q3 ^ $q5 ^ $q6 ^ $q7 ^ $r0 ^ $r3 ^ $r5 ^ $r7); $this->values[4] = $q1 ^ $q2 ^ $q3 ^ $q5 ^ $r1 ^ $r3 ^ $r4 ^ $r5 ^ $r6 ^ $r7 ^ self::rotr16($q1 ^ $q2 ^ $q4 ^ $q5 ^ $q7 ^ $r1 ^ $r4 ^ $r5 ^ $r6); $this->values[5] = $q2 ^ $q3 ^ $q4 ^ $q6 ^ $r2 ^ $r4 ^ $r5 ^ $r6 ^ $r7 ^ self::rotr16($q2 ^ $q3 ^ $q5 ^ $q6 ^ $r2 ^ $r5 ^ $r6 ^ $r7); $this->values[6] = $q3 ^ $q4 ^ $q5 ^ $q7 ^ $r3 ^ $r5 ^ $r6 ^ $r7 ^ self::rotr16($q3 ^ $q4 ^ $q6 ^ $q7 ^ $r3 ^ $r6 ^ $r7); $this->values[7] = $q4 ^ $q5 ^ $q6 ^ $r4 ^ $r6 ^ $r7 ^ self::rotr16($q4 ^ $q5 ^ $q7 ^ $r4 ^ $r7); return $this; } /** * @return self */ public function inverseShiftRows() { for ($i = 0; $i < 8; ++$i) { $x = $this->values[$i]; $this->values[$i] = ParagonIE_Sodium_Core_Util::U32_MAX & ( ($x & 0x000000FF) | (($x & 0x00003F00) << 2) | (($x & 0x0000C000) >> 6) | (($x & 0x000F0000) << 4) | (($x & 0x00F00000) >> 4) | (($x & 0x03000000) << 6) | (($x & 0xFC000000) >> 2) ); } return $this; } } Core/AES/Expanded.php 0000644 00000000460 15162213731 0010306 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_AES_Expanded', false)) { return; } /** * @internal This should only be used by sodium_compat */ class ParagonIE_Sodium_Core_AES_Expanded extends ParagonIE_Sodium_Core_AES_KeySchedule { /** @var bool $expanded */ protected $expanded = true; } Core/HChaCha20.php 0000644 00000010054 15162213732 0007530 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_HChaCha20', false)) { return; } /** * Class ParagonIE_Sodium_Core_HChaCha20 */ class ParagonIE_Sodium_Core_HChaCha20 extends ParagonIE_Sodium_Core_ChaCha20 { /** * @param string $in * @param string $key * @param string|null $c * @return string * * @throws SodiumException * @throws TypeError */ public static function hChaCha20($in, $key, $c = null) { if (self::strlen($in) !== 16) { throw new SodiumException('Argument 1 must be 16 bytes'); } if (self::strlen($key) !== 32) { throw new SodiumException('Argument 2 must be 32 bytes'); } $ctx = array(); if ($c === null) { $ctx[0] = 0x61707865; $ctx[1] = 0x3320646e; $ctx[2] = 0x79622d32; $ctx[3] = 0x6b206574; } else { $ctx[0] = self::load_4(self::substr($c, 0, 4)); $ctx[1] = self::load_4(self::substr($c, 4, 4)); $ctx[2] = self::load_4(self::substr($c, 8, 4)); $ctx[3] = self::load_4(self::substr($c, 12, 4)); } $ctx[4] = self::load_4(self::substr($key, 0, 4)); $ctx[5] = self::load_4(self::substr($key, 4, 4)); $ctx[6] = self::load_4(self::substr($key, 8, 4)); $ctx[7] = self::load_4(self::substr($key, 12, 4)); $ctx[8] = self::load_4(self::substr($key, 16, 4)); $ctx[9] = self::load_4(self::substr($key, 20, 4)); $ctx[10] = self::load_4(self::substr($key, 24, 4)); $ctx[11] = self::load_4(self::substr($key, 28, 4)); $ctx[12] = self::load_4(self::substr($in, 0, 4)); $ctx[13] = self::load_4(self::substr($in, 4, 4)); $ctx[14] = self::load_4(self::substr($in, 8, 4)); $ctx[15] = self::load_4(self::substr($in, 12, 4)); return self::hChaCha20Bytes($ctx); } /** * @param array $ctx * @return string * @throws TypeError */ protected static function hChaCha20Bytes(array $ctx) { $x0 = (int) $ctx[0]; $x1 = (int) $ctx[1]; $x2 = (int) $ctx[2]; $x3 = (int) $ctx[3]; $x4 = (int) $ctx[4]; $x5 = (int) $ctx[5]; $x6 = (int) $ctx[6]; $x7 = (int) $ctx[7]; $x8 = (int) $ctx[8]; $x9 = (int) $ctx[9]; $x10 = (int) $ctx[10]; $x11 = (int) $ctx[11]; $x12 = (int) $ctx[12]; $x13 = (int) $ctx[13]; $x14 = (int) $ctx[14]; $x15 = (int) $ctx[15]; for ($i = 0; $i < 10; ++$i) { # QUARTERROUND( x0, x4, x8, x12) list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); # QUARTERROUND( x1, x5, x9, x13) list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); # QUARTERROUND( x2, x6, x10, x14) list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); # QUARTERROUND( x3, x7, x11, x15) list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); # QUARTERROUND( x0, x5, x10, x15) list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); # QUARTERROUND( x1, x6, x11, x12) list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); # QUARTERROUND( x2, x7, x8, x13) list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); # QUARTERROUND( x3, x4, x9, x14) list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); } return self::store32_le((int) ($x0 & 0xffffffff)) . self::store32_le((int) ($x1 & 0xffffffff)) . self::store32_le((int) ($x2 & 0xffffffff)) . self::store32_le((int) ($x3 & 0xffffffff)) . self::store32_le((int) ($x12 & 0xffffffff)) . self::store32_le((int) ($x13 & 0xffffffff)) . self::store32_le((int) ($x14 & 0xffffffff)) . self::store32_le((int) ($x15 & 0xffffffff)); } } Core/HSalsa20.php 0000644 00000007131 15162213732 0007466 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_HSalsa20', false)) { return; } /** * Class ParagonIE_Sodium_Core_HSalsa20 */ abstract class ParagonIE_Sodium_Core_HSalsa20 extends ParagonIE_Sodium_Core_Salsa20 { /** * Calculate an hsalsa20 hash of a single block * * HSalsa20 doesn't have a counter and will never be used for more than * one block (used to derive a subkey for xsalsa20). * * @internal You should not use this directly from another application * * @param string $in * @param string $k * @param string|null $c * @return string * @throws TypeError */ public static function hsalsa20($in, $k, $c = null) { if ($c === null) { $x0 = 0x61707865; $x5 = 0x3320646e; $x10 = 0x79622d32; $x15 = 0x6b206574; } else { $x0 = self::load_4(self::substr($c, 0, 4)); $x5 = self::load_4(self::substr($c, 4, 4)); $x10 = self::load_4(self::substr($c, 8, 4)); $x15 = self::load_4(self::substr($c, 12, 4)); } $x1 = self::load_4(self::substr($k, 0, 4)); $x2 = self::load_4(self::substr($k, 4, 4)); $x3 = self::load_4(self::substr($k, 8, 4)); $x4 = self::load_4(self::substr($k, 12, 4)); $x11 = self::load_4(self::substr($k, 16, 4)); $x12 = self::load_4(self::substr($k, 20, 4)); $x13 = self::load_4(self::substr($k, 24, 4)); $x14 = self::load_4(self::substr($k, 28, 4)); $x6 = self::load_4(self::substr($in, 0, 4)); $x7 = self::load_4(self::substr($in, 4, 4)); $x8 = self::load_4(self::substr($in, 8, 4)); $x9 = self::load_4(self::substr($in, 12, 4)); for ($i = self::ROUNDS; $i > 0; $i -= 2) { $x4 ^= self::rotate($x0 + $x12, 7); $x8 ^= self::rotate($x4 + $x0, 9); $x12 ^= self::rotate($x8 + $x4, 13); $x0 ^= self::rotate($x12 + $x8, 18); $x9 ^= self::rotate($x5 + $x1, 7); $x13 ^= self::rotate($x9 + $x5, 9); $x1 ^= self::rotate($x13 + $x9, 13); $x5 ^= self::rotate($x1 + $x13, 18); $x14 ^= self::rotate($x10 + $x6, 7); $x2 ^= self::rotate($x14 + $x10, 9); $x6 ^= self::rotate($x2 + $x14, 13); $x10 ^= self::rotate($x6 + $x2, 18); $x3 ^= self::rotate($x15 + $x11, 7); $x7 ^= self::rotate($x3 + $x15, 9); $x11 ^= self::rotate($x7 + $x3, 13); $x15 ^= self::rotate($x11 + $x7, 18); $x1 ^= self::rotate($x0 + $x3, 7); $x2 ^= self::rotate($x1 + $x0, 9); $x3 ^= self::rotate($x2 + $x1, 13); $x0 ^= self::rotate($x3 + $x2, 18); $x6 ^= self::rotate($x5 + $x4, 7); $x7 ^= self::rotate($x6 + $x5, 9); $x4 ^= self::rotate($x7 + $x6, 13); $x5 ^= self::rotate($x4 + $x7, 18); $x11 ^= self::rotate($x10 + $x9, 7); $x8 ^= self::rotate($x11 + $x10, 9); $x9 ^= self::rotate($x8 + $x11, 13); $x10 ^= self::rotate($x9 + $x8, 18); $x12 ^= self::rotate($x15 + $x14, 7); $x13 ^= self::rotate($x12 + $x15, 9); $x14 ^= self::rotate($x13 + $x12, 13); $x15 ^= self::rotate($x14 + $x13, 18); } return self::store32_le($x0) . self::store32_le($x5) . self::store32_le($x10) . self::store32_le($x15) . self::store32_le($x6) . self::store32_le($x7) . self::store32_le($x8) . self::store32_le($x9); } } Core/Salsa20.php 0000644 00000020051 15162213732 0007352 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_Salsa20', false)) { return; } /** * Class ParagonIE_Sodium_Core_Salsa20 */ abstract class ParagonIE_Sodium_Core_Salsa20 extends ParagonIE_Sodium_Core_Util { const ROUNDS = 20; /** * Calculate an salsa20 hash of a single block * * @internal You should not use this directly from another application * * @param string $in * @param string $k * @param string|null $c * @return string * @throws TypeError */ public static function core_salsa20($in, $k, $c = null) { if (self::strlen($k) < 32) { throw new RangeException('Key must be 32 bytes long'); } if ($c === null) { $j0 = $x0 = 0x61707865; $j5 = $x5 = 0x3320646e; $j10 = $x10 = 0x79622d32; $j15 = $x15 = 0x6b206574; } else { $j0 = $x0 = self::load_4(self::substr($c, 0, 4)); $j5 = $x5 = self::load_4(self::substr($c, 4, 4)); $j10 = $x10 = self::load_4(self::substr($c, 8, 4)); $j15 = $x15 = self::load_4(self::substr($c, 12, 4)); } $j1 = $x1 = self::load_4(self::substr($k, 0, 4)); $j2 = $x2 = self::load_4(self::substr($k, 4, 4)); $j3 = $x3 = self::load_4(self::substr($k, 8, 4)); $j4 = $x4 = self::load_4(self::substr($k, 12, 4)); $j6 = $x6 = self::load_4(self::substr($in, 0, 4)); $j7 = $x7 = self::load_4(self::substr($in, 4, 4)); $j8 = $x8 = self::load_4(self::substr($in, 8, 4)); $j9 = $x9 = self::load_4(self::substr($in, 12, 4)); $j11 = $x11 = self::load_4(self::substr($k, 16, 4)); $j12 = $x12 = self::load_4(self::substr($k, 20, 4)); $j13 = $x13 = self::load_4(self::substr($k, 24, 4)); $j14 = $x14 = self::load_4(self::substr($k, 28, 4)); for ($i = self::ROUNDS; $i > 0; $i -= 2) { $x4 ^= self::rotate($x0 + $x12, 7); $x8 ^= self::rotate($x4 + $x0, 9); $x12 ^= self::rotate($x8 + $x4, 13); $x0 ^= self::rotate($x12 + $x8, 18); $x9 ^= self::rotate($x5 + $x1, 7); $x13 ^= self::rotate($x9 + $x5, 9); $x1 ^= self::rotate($x13 + $x9, 13); $x5 ^= self::rotate($x1 + $x13, 18); $x14 ^= self::rotate($x10 + $x6, 7); $x2 ^= self::rotate($x14 + $x10, 9); $x6 ^= self::rotate($x2 + $x14, 13); $x10 ^= self::rotate($x6 + $x2, 18); $x3 ^= self::rotate($x15 + $x11, 7); $x7 ^= self::rotate($x3 + $x15, 9); $x11 ^= self::rotate($x7 + $x3, 13); $x15 ^= self::rotate($x11 + $x7, 18); $x1 ^= self::rotate($x0 + $x3, 7); $x2 ^= self::rotate($x1 + $x0, 9); $x3 ^= self::rotate($x2 + $x1, 13); $x0 ^= self::rotate($x3 + $x2, 18); $x6 ^= self::rotate($x5 + $x4, 7); $x7 ^= self::rotate($x6 + $x5, 9); $x4 ^= self::rotate($x7 + $x6, 13); $x5 ^= self::rotate($x4 + $x7, 18); $x11 ^= self::rotate($x10 + $x9, 7); $x8 ^= self::rotate($x11 + $x10, 9); $x9 ^= self::rotate($x8 + $x11, 13); $x10 ^= self::rotate($x9 + $x8, 18); $x12 ^= self::rotate($x15 + $x14, 7); $x13 ^= self::rotate($x12 + $x15, 9); $x14 ^= self::rotate($x13 + $x12, 13); $x15 ^= self::rotate($x14 + $x13, 18); } $x0 += $j0; $x1 += $j1; $x2 += $j2; $x3 += $j3; $x4 += $j4; $x5 += $j5; $x6 += $j6; $x7 += $j7; $x8 += $j8; $x9 += $j9; $x10 += $j10; $x11 += $j11; $x12 += $j12; $x13 += $j13; $x14 += $j14; $x15 += $j15; return self::store32_le($x0) . self::store32_le($x1) . self::store32_le($x2) . self::store32_le($x3) . self::store32_le($x4) . self::store32_le($x5) . self::store32_le($x6) . self::store32_le($x7) . self::store32_le($x8) . self::store32_le($x9) . self::store32_le($x10) . self::store32_le($x11) . self::store32_le($x12) . self::store32_le($x13) . self::store32_le($x14) . self::store32_le($x15); } /** * @internal You should not use this directly from another application * * @param int $len * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function salsa20($len, $nonce, $key) { if (self::strlen($key) !== 32) { throw new RangeException('Key must be 32 bytes long'); } $kcopy = '' . $key; $in = self::substr($nonce, 0, 8) . str_repeat("\0", 8); $c = ''; while ($len >= 64) { $c .= self::core_salsa20($in, $kcopy, null); $u = 1; // Internal counter. for ($i = 8; $i < 16; ++$i) { $u += self::chrToInt($in[$i]); $in[$i] = self::intToChr($u & 0xff); $u >>= 8; } $len -= 64; } if ($len > 0) { $c .= self::substr( self::core_salsa20($in, $kcopy, null), 0, $len ); } try { ParagonIE_Sodium_Compat::memzero($kcopy); } catch (SodiumException $ex) { $kcopy = null; } return $c; } /** * @internal You should not use this directly from another application * * @param string $m * @param string $n * @param int $ic * @param string $k * @return string * @throws SodiumException * @throws TypeError */ public static function salsa20_xor_ic($m, $n, $ic, $k) { $mlen = self::strlen($m); if ($mlen < 1) { return ''; } $kcopy = self::substr($k, 0, 32); $in = self::substr($n, 0, 8); // Initialize the counter $in .= ParagonIE_Sodium_Core_Util::store64_le($ic); $c = ''; while ($mlen >= 64) { $block = self::core_salsa20($in, $kcopy, null); $c .= self::xorStrings( self::substr($m, 0, 64), self::substr($block, 0, 64) ); $u = 1; for ($i = 8; $i < 16; ++$i) { $u += self::chrToInt($in[$i]); $in[$i] = self::intToChr($u & 0xff); $u >>= 8; } $mlen -= 64; $m = self::substr($m, 64); } if ($mlen) { $block = self::core_salsa20($in, $kcopy, null); $c .= self::xorStrings( self::substr($m, 0, $mlen), self::substr($block, 0, $mlen) ); } try { ParagonIE_Sodium_Compat::memzero($block); ParagonIE_Sodium_Compat::memzero($kcopy); } catch (SodiumException $ex) { $block = null; $kcopy = null; } return $c; } /** * @internal You should not use this directly from another application * * @param string $message * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function salsa20_xor($message, $nonce, $key) { return self::xorStrings( $message, self::salsa20( self::strlen($message), $nonce, $key ) ); } /** * @internal You should not use this directly from another application * * @param int $u * @param int $c * @return int */ public static function rotate($u, $c) { $u &= 0xffffffff; $c %= 32; return (int) (0xffffffff & ( ($u << $c) | ($u >> (32 - $c)) ) ); } } Core/Poly1305/error_log 0000644 00000003600 15162213733 0010607 0 ustar 00 [24-Mar-2026 21:46:51 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305/State.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305/State.php on line 10 [25-Mar-2026 02:30:54 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305/State.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305/State.php on line 10 [25-Mar-2026 15:10:13 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305/State.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305/State.php on line 10 [26-Mar-2026 09:22:00 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305/State.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305/State.php on line 10 [26-Mar-2026 13:30:35 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305/State.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305/State.php on line 10 [28-Mar-2026 09:33:21 UTC] PHP Fatal error: Uncaught Error: Class "ParagonIE_Sodium_Core_Util" not found in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305/State.php:10 Stack trace: #0 {main} thrown in /home/everqlsh/public_html/wp-includes/sodium_compat/src/Core/Poly1305/State.php on line 10 Core/Poly1305/State.php 0000644 00000031160 15162213734 0010466 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_Poly1305_State', false)) { return; } /** * Class ParagonIE_Sodium_Core_Poly1305_State */ class ParagonIE_Sodium_Core_Poly1305_State extends ParagonIE_Sodium_Core_Util { /** * @var array<int, int> */ protected $buffer = array(); /** * @var bool */ protected $final = false; /** * @var array<int, int> */ public $h; /** * @var int */ protected $leftover = 0; /** * @var int[] */ public $r; /** * @var int[] */ public $pad; /** * ParagonIE_Sodium_Core_Poly1305_State constructor. * * @internal You should not use this directly from another application * * @param string $key * @throws InvalidArgumentException * @throws TypeError */ public function __construct($key = '') { if (self::strlen($key) < 32) { throw new InvalidArgumentException( 'Poly1305 requires a 32-byte key' ); } /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ $this->r = array( (int) ((self::load_4(self::substr($key, 0, 4))) & 0x3ffffff), (int) ((self::load_4(self::substr($key, 3, 4)) >> 2) & 0x3ffff03), (int) ((self::load_4(self::substr($key, 6, 4)) >> 4) & 0x3ffc0ff), (int) ((self::load_4(self::substr($key, 9, 4)) >> 6) & 0x3f03fff), (int) ((self::load_4(self::substr($key, 12, 4)) >> 8) & 0x00fffff) ); /* h = 0 */ $this->h = array(0, 0, 0, 0, 0); /* save pad for later */ $this->pad = array( self::load_4(self::substr($key, 16, 4)), self::load_4(self::substr($key, 20, 4)), self::load_4(self::substr($key, 24, 4)), self::load_4(self::substr($key, 28, 4)), ); $this->leftover = 0; $this->final = false; } /** * Zero internal buffer upon destruction */ public function __destruct() { $this->r[0] ^= $this->r[0]; $this->r[1] ^= $this->r[1]; $this->r[2] ^= $this->r[2]; $this->r[3] ^= $this->r[3]; $this->r[4] ^= $this->r[4]; $this->h[0] ^= $this->h[0]; $this->h[1] ^= $this->h[1]; $this->h[2] ^= $this->h[2]; $this->h[3] ^= $this->h[3]; $this->h[4] ^= $this->h[4]; $this->pad[0] ^= $this->pad[0]; $this->pad[1] ^= $this->pad[1]; $this->pad[2] ^= $this->pad[2]; $this->pad[3] ^= $this->pad[3]; $this->leftover = 0; $this->final = true; } /** * @internal You should not use this directly from another application * * @param string $message * @return self * @throws SodiumException * @throws TypeError */ public function update($message = '') { $bytes = self::strlen($message); if ($bytes < 1) { return $this; } /* handle leftover */ if ($this->leftover) { $want = ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - $this->leftover; if ($want > $bytes) { $want = $bytes; } for ($i = 0; $i < $want; ++$i) { $mi = self::chrToInt($message[$i]); $this->buffer[$this->leftover + $i] = $mi; } // We snip off the leftmost bytes. $message = self::substr($message, $want); $bytes = self::strlen($message); $this->leftover += $want; if ($this->leftover < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { // We still don't have enough to run $this->blocks() return $this; } $this->blocks( self::intArrayToString($this->buffer), ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE ); $this->leftover = 0; } /* process full blocks */ if ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { /** @var int $want */ $want = $bytes & ~(ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - 1); if ($want >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { $block = self::substr($message, 0, $want); if (self::strlen($block) >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { $this->blocks($block, $want); $message = self::substr($message, $want); $bytes = self::strlen($message); } } } /* store leftover */ if ($bytes) { for ($i = 0; $i < $bytes; ++$i) { $mi = self::chrToInt($message[$i]); $this->buffer[$this->leftover + $i] = $mi; } $this->leftover = (int) $this->leftover + $bytes; } return $this; } /** * @internal You should not use this directly from another application * * @param string $message * @param int $bytes * @return self * @throws TypeError */ public function blocks($message, $bytes) { if (self::strlen($message) < 16) { $message = str_pad($message, 16, "\x00", STR_PAD_RIGHT); } /** @var int $hibit */ $hibit = $this->final ? 0 : 1 << 24; /* 1 << 128 */ $r0 = (int) $this->r[0]; $r1 = (int) $this->r[1]; $r2 = (int) $this->r[2]; $r3 = (int) $this->r[3]; $r4 = (int) $this->r[4]; $s1 = self::mul($r1, 5, 3); $s2 = self::mul($r2, 5, 3); $s3 = self::mul($r3, 5, 3); $s4 = self::mul($r4, 5, 3); $h0 = $this->h[0]; $h1 = $this->h[1]; $h2 = $this->h[2]; $h3 = $this->h[3]; $h4 = $this->h[4]; while ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { /* h += m[i] */ $h0 += self::load_4(self::substr($message, 0, 4)) & 0x3ffffff; $h1 += (self::load_4(self::substr($message, 3, 4)) >> 2) & 0x3ffffff; $h2 += (self::load_4(self::substr($message, 6, 4)) >> 4) & 0x3ffffff; $h3 += (self::load_4(self::substr($message, 9, 4)) >> 6) & 0x3ffffff; $h4 += (self::load_4(self::substr($message, 12, 4)) >> 8) | $hibit; /* h *= r */ $d0 = ( self::mul($h0, $r0, 27) + self::mul($s4, $h1, 27) + self::mul($s3, $h2, 27) + self::mul($s2, $h3, 27) + self::mul($s1, $h4, 27) ); $d1 = ( self::mul($h0, $r1, 27) + self::mul($h1, $r0, 27) + self::mul($s4, $h2, 27) + self::mul($s3, $h3, 27) + self::mul($s2, $h4, 27) ); $d2 = ( self::mul($h0, $r2, 27) + self::mul($h1, $r1, 27) + self::mul($h2, $r0, 27) + self::mul($s4, $h3, 27) + self::mul($s3, $h4, 27) ); $d3 = ( self::mul($h0, $r3, 27) + self::mul($h1, $r2, 27) + self::mul($h2, $r1, 27) + self::mul($h3, $r0, 27) + self::mul($s4, $h4, 27) ); $d4 = ( self::mul($h0, $r4, 27) + self::mul($h1, $r3, 27) + self::mul($h2, $r2, 27) + self::mul($h3, $r1, 27) + self::mul($h4, $r0, 27) ); /* (partial) h %= p */ /** @var int $c */ $c = $d0 >> 26; /** @var int $h0 */ $h0 = $d0 & 0x3ffffff; $d1 += $c; /** @var int $c */ $c = $d1 >> 26; /** @var int $h1 */ $h1 = $d1 & 0x3ffffff; $d2 += $c; /** @var int $c */ $c = $d2 >> 26; /** @var int $h2 */ $h2 = $d2 & 0x3ffffff; $d3 += $c; /** @var int $c */ $c = $d3 >> 26; /** @var int $h3 */ $h3 = $d3 & 0x3ffffff; $d4 += $c; /** @var int $c */ $c = $d4 >> 26; /** @var int $h4 */ $h4 = $d4 & 0x3ffffff; $h0 += (int) self::mul($c, 5, 3); /** @var int $c */ $c = $h0 >> 26; /** @var int $h0 */ $h0 &= 0x3ffffff; $h1 += $c; // Chop off the left 32 bytes. $message = self::substr( $message, ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE ); $bytes -= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; } $this->h = array( (int) ($h0 & 0xffffffff), (int) ($h1 & 0xffffffff), (int) ($h2 & 0xffffffff), (int) ($h3 & 0xffffffff), (int) ($h4 & 0xffffffff) ); return $this; } /** * @internal You should not use this directly from another application * * @return string * @throws TypeError */ public function finish() { /* process the remaining block */ if ($this->leftover) { $i = $this->leftover; $this->buffer[$i++] = 1; for (; $i < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; ++$i) { $this->buffer[$i] = 0; } $this->final = true; $this->blocks( self::substr( self::intArrayToString($this->buffer), 0, ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE ), ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE ); } $h0 = (int) $this->h[0]; $h1 = (int) $this->h[1]; $h2 = (int) $this->h[2]; $h3 = (int) $this->h[3]; $h4 = (int) $this->h[4]; /** @var int $c */ $c = $h1 >> 26; /** @var int $h1 */ $h1 &= 0x3ffffff; /** @var int $h2 */ $h2 += $c; /** @var int $c */ $c = $h2 >> 26; /** @var int $h2 */ $h2 &= 0x3ffffff; $h3 += $c; /** @var int $c */ $c = $h3 >> 26; $h3 &= 0x3ffffff; $h4 += $c; /** @var int $c */ $c = $h4 >> 26; $h4 &= 0x3ffffff; /** @var int $h0 */ $h0 += self::mul($c, 5, 3); /** @var int $c */ $c = $h0 >> 26; /** @var int $h0 */ $h0 &= 0x3ffffff; /** @var int $h1 */ $h1 += $c; /* compute h + -p */ /** @var int $g0 */ $g0 = $h0 + 5; /** @var int $c */ $c = $g0 >> 26; /** @var int $g0 */ $g0 &= 0x3ffffff; /** @var int $g1 */ $g1 = $h1 + $c; /** @var int $c */ $c = $g1 >> 26; $g1 &= 0x3ffffff; /** @var int $g2 */ $g2 = $h2 + $c; /** @var int $c */ $c = $g2 >> 26; /** @var int $g2 */ $g2 &= 0x3ffffff; /** @var int $g3 */ $g3 = $h3 + $c; /** @var int $c */ $c = $g3 >> 26; /** @var int $g3 */ $g3 &= 0x3ffffff; /** @var int $g4 */ $g4 = ($h4 + $c - (1 << 26)) & 0xffffffff; /* select h if h < p, or h + -p if h >= p */ /** @var int $mask */ $mask = ($g4 >> 31) - 1; $g0 &= $mask; $g1 &= $mask; $g2 &= $mask; $g3 &= $mask; $g4 &= $mask; /** @var int $mask */ $mask = ~$mask & 0xffffffff; /** @var int $h0 */ $h0 = ($h0 & $mask) | $g0; /** @var int $h1 */ $h1 = ($h1 & $mask) | $g1; /** @var int $h2 */ $h2 = ($h2 & $mask) | $g2; /** @var int $h3 */ $h3 = ($h3 & $mask) | $g3; /** @var int $h4 */ $h4 = ($h4 & $mask) | $g4; /* h = h % (2^128) */ /** @var int $h0 */ $h0 = (($h0) | ($h1 << 26)) & 0xffffffff; /** @var int $h1 */ $h1 = (($h1 >> 6) | ($h2 << 20)) & 0xffffffff; /** @var int $h2 */ $h2 = (($h2 >> 12) | ($h3 << 14)) & 0xffffffff; /** @var int $h3 */ $h3 = (($h3 >> 18) | ($h4 << 8)) & 0xffffffff; /* mac = (h + pad) % (2^128) */ $f = (int) ($h0 + $this->pad[0]); $h0 = (int) $f; $f = (int) ($h1 + $this->pad[1] + ($f >> 32)); $h1 = (int) $f; $f = (int) ($h2 + $this->pad[2] + ($f >> 32)); $h2 = (int) $f; $f = (int) ($h3 + $this->pad[3] + ($f >> 32)); $h3 = (int) $f; return self::store32_le($h0 & 0xffffffff) . self::store32_le($h1 & 0xffffffff) . self::store32_le($h2 & 0xffffffff) . self::store32_le($h3 & 0xffffffff); } } Core/Poly1305.php 0000644 00000003050 15162213734 0007403 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_Poly1305', false)) { return; } /** * Class ParagonIE_Sodium_Core_Poly1305 */ abstract class ParagonIE_Sodium_Core_Poly1305 extends ParagonIE_Sodium_Core_Util { const BLOCK_SIZE = 16; /** * @internal You should not use this directly from another application * * @param string $m * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function onetimeauth($m, $key) { if (self::strlen($key) !== 32) { throw new InvalidArgumentException( 'Key must be 32 bytes long.' ); } $state = new ParagonIE_Sodium_Core_Poly1305_State( self::substr($key, 0, 32) ); return $state ->update($m) ->finish(); } /** * @internal You should not use this directly from another application * * @param string $mac * @param string $m * @param string $key * @return bool * @throws SodiumException * @throws TypeError */ public static function onetimeauth_verify($mac, $m, $key) { if (self::strlen($key) < 32) { throw new InvalidArgumentException( 'Key must be 32 bytes long.' ); } $state = new ParagonIE_Sodium_Core_Poly1305_State( self::substr($key, 0, 32) ); $calc = $state ->update($m) ->finish(); return self::verify_16($calc, $mac); } } Core/XSalsa20.php 0000644 00000002533 15162213734 0007511 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Core_XSalsa20', false)) { return; } /** * Class ParagonIE_Sodium_Core_XSalsa20 */ abstract class ParagonIE_Sodium_Core_XSalsa20 extends ParagonIE_Sodium_Core_HSalsa20 { /** * Expand a key and nonce into an xsalsa20 keystream. * * @internal You should not use this directly from another application * * @param int $len * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function xsalsa20($len, $nonce, $key) { $ret = self::salsa20( $len, self::substr($nonce, 16, 8), self::hsalsa20($nonce, $key) ); return $ret; } /** * Encrypt a string with XSalsa20. Doesn't provide integrity. * * @internal You should not use this directly from another application * * @param string $message * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function xsalsa20_xor($message, $nonce, $key) { return self::xorStrings( $message, self::xsalsa20( self::strlen($message), $nonce, $key ) ); } } Crypto32.php 0000644 00000153517 15162213734 0006722 0 ustar 00 <?php if (class_exists('ParagonIE_Sodium_Crypto32', false)) { return; } /** * Class ParagonIE_Sodium_Crypto * * ATTENTION! * * If you are using this library, you should be using * ParagonIE_Sodium_Compat in your code, not this class. */ abstract class ParagonIE_Sodium_Crypto32 { const aead_chacha20poly1305_KEYBYTES = 32; const aead_chacha20poly1305_NSECBYTES = 0; const aead_chacha20poly1305_NPUBBYTES = 8; const aead_chacha20poly1305_ABYTES = 16; const aead_chacha20poly1305_IETF_KEYBYTES = 32; const aead_chacha20poly1305_IETF_NSECBYTES = 0; const aead_chacha20poly1305_IETF_NPUBBYTES = 12; const aead_chacha20poly1305_IETF_ABYTES = 16; const aead_xchacha20poly1305_IETF_KEYBYTES = 32; const aead_xchacha20poly1305_IETF_NSECBYTES = 0; const aead_xchacha20poly1305_IETF_NPUBBYTES = 24; const aead_xchacha20poly1305_IETF_ABYTES = 16; const box_curve25519xsalsa20poly1305_SEEDBYTES = 32; const box_curve25519xsalsa20poly1305_PUBLICKEYBYTES = 32; const box_curve25519xsalsa20poly1305_SECRETKEYBYTES = 32; const box_curve25519xsalsa20poly1305_BEFORENMBYTES = 32; const box_curve25519xsalsa20poly1305_NONCEBYTES = 24; const box_curve25519xsalsa20poly1305_MACBYTES = 16; const box_curve25519xsalsa20poly1305_BOXZEROBYTES = 16; const box_curve25519xsalsa20poly1305_ZEROBYTES = 32; const onetimeauth_poly1305_BYTES = 16; const onetimeauth_poly1305_KEYBYTES = 32; const secretbox_xsalsa20poly1305_KEYBYTES = 32; const secretbox_xsalsa20poly1305_NONCEBYTES = 24; const secretbox_xsalsa20poly1305_MACBYTES = 16; const secretbox_xsalsa20poly1305_BOXZEROBYTES = 16; const secretbox_xsalsa20poly1305_ZEROBYTES = 32; const secretbox_xchacha20poly1305_KEYBYTES = 32; const secretbox_xchacha20poly1305_NONCEBYTES = 24; const secretbox_xchacha20poly1305_MACBYTES = 16; const secretbox_xchacha20poly1305_BOXZEROBYTES = 16; const secretbox_xchacha20poly1305_ZEROBYTES = 32; const stream_salsa20_KEYBYTES = 32; /** * AEAD Decryption with ChaCha20-Poly1305 * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $ad * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function aead_chacha20poly1305_decrypt( $message = '', $ad = '', $nonce = '', $key = '' ) { /** @var int $len - Length of message (ciphertext + MAC) */ $len = ParagonIE_Sodium_Core32_Util::strlen($message); /** @var int $clen - Length of ciphertext */ $clen = $len - self::aead_chacha20poly1305_ABYTES; /** @var int $adlen - Length of associated data */ $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad); /** @var string $mac - Message authentication code */ $mac = ParagonIE_Sodium_Core32_Util::substr( $message, $clen, self::aead_chacha20poly1305_ABYTES ); /** @var string $ciphertext - The encrypted message (sans MAC) */ $ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 0, $clen); /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream( 32, $nonce, $key ); /* Recalculate the Poly1305 authentication tag (MAC): */ $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0); try { ParagonIE_Sodium_Compat::memzero($block0); } catch (SodiumException $ex) { $block0 = null; } $state->update($ad); $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen)); $state->update($ciphertext); $state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen)); $computed_mac = $state->finish(); /* Compare the given MAC with the recalculated MAC: */ if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) { throw new SodiumException('Invalid MAC'); } // Here, we know that the MAC is valid, so we decrypt and return the plaintext return ParagonIE_Sodium_Core32_ChaCha20::streamXorIc( $ciphertext, $nonce, $key, ParagonIE_Sodium_Core32_Util::store64_le(1) ); } /** * AEAD Encryption with ChaCha20-Poly1305 * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $ad * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function aead_chacha20poly1305_encrypt( $message = '', $ad = '', $nonce = '', $key = '' ) { /** @var int $len - Length of the plaintext message */ $len = ParagonIE_Sodium_Core32_Util::strlen($message); /** @var int $adlen - Length of the associated data */ $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad); /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream( 32, $nonce, $key ); $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0); try { ParagonIE_Sodium_Compat::memzero($block0); } catch (SodiumException $ex) { $block0 = null; } /** @var string $ciphertext - Raw encrypted data */ $ciphertext = ParagonIE_Sodium_Core32_ChaCha20::streamXorIc( $message, $nonce, $key, ParagonIE_Sodium_Core32_Util::store64_le(1) ); $state->update($ad); $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen)); $state->update($ciphertext); $state->update(ParagonIE_Sodium_Core32_Util::store64_le($len)); return $ciphertext . $state->finish(); } /** * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $ad * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function aead_chacha20poly1305_ietf_decrypt( $message = '', $ad = '', $nonce = '', $key = '' ) { /** @var int $adlen - Length of associated data */ $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad); /** @var int $len - Length of message (ciphertext + MAC) */ $len = ParagonIE_Sodium_Core32_Util::strlen($message); /** @var int $clen - Length of ciphertext */ $clen = $len - self::aead_chacha20poly1305_IETF_ABYTES; /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ $block0 = ParagonIE_Sodium_Core32_ChaCha20::ietfStream( 32, $nonce, $key ); /** @var string $mac - Message authentication code */ $mac = ParagonIE_Sodium_Core32_Util::substr( $message, $len - self::aead_chacha20poly1305_IETF_ABYTES, self::aead_chacha20poly1305_IETF_ABYTES ); /** @var string $ciphertext - The encrypted message (sans MAC) */ $ciphertext = ParagonIE_Sodium_Core32_Util::substr( $message, 0, $len - self::aead_chacha20poly1305_IETF_ABYTES ); /* Recalculate the Poly1305 authentication tag (MAC): */ $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0); try { ParagonIE_Sodium_Compat::memzero($block0); } catch (SodiumException $ex) { $block0 = null; } $state->update($ad); $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf))); $state->update($ciphertext); $state->update(str_repeat("\x00", (0x10 - $clen) & 0xf)); $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen)); $state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen)); $computed_mac = $state->finish(); /* Compare the given MAC with the recalculated MAC: */ if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) { throw new SodiumException('Invalid MAC'); } // Here, we know that the MAC is valid, so we decrypt and return the plaintext return ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc( $ciphertext, $nonce, $key, ParagonIE_Sodium_Core32_Util::store64_le(1) ); } /** * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $ad * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function aead_chacha20poly1305_ietf_encrypt( $message = '', $ad = '', $nonce = '', $key = '' ) { /** @var int $len - Length of the plaintext message */ $len = ParagonIE_Sodium_Core32_Util::strlen($message); /** @var int $adlen - Length of the associated data */ $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad); /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ $block0 = ParagonIE_Sodium_Core32_ChaCha20::ietfStream( 32, $nonce, $key ); $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0); try { ParagonIE_Sodium_Compat::memzero($block0); } catch (SodiumException $ex) { $block0 = null; } /** @var string $ciphertext - Raw encrypted data */ $ciphertext = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc( $message, $nonce, $key, ParagonIE_Sodium_Core32_Util::store64_le(1) ); $state->update($ad); $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf))); $state->update($ciphertext); $state->update(str_repeat("\x00", ((0x10 - $len) & 0xf))); $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen)); $state->update(ParagonIE_Sodium_Core32_Util::store64_le($len)); return $ciphertext . $state->finish(); } /** * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $ad * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function aead_xchacha20poly1305_ietf_decrypt( $message = '', $ad = '', $nonce = '', $key = '' ) { $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20( ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16), $key ); $nonceLast = "\x00\x00\x00\x00" . ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8); return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey); } /** * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $ad * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function aead_xchacha20poly1305_ietf_encrypt( $message = '', $ad = '', $nonce = '', $key = '' ) { $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20( ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16), $key ); $nonceLast = "\x00\x00\x00\x00" . ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8); return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey); } /** * HMAC-SHA-512-256 (a.k.a. the leftmost 256 bits of HMAC-SHA-512) * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $key * @return string * @throws TypeError */ public static function auth($message, $key) { return ParagonIE_Sodium_Core32_Util::substr( hash_hmac('sha512', $message, $key, true), 0, 32 ); } /** * HMAC-SHA-512-256 validation. Constant-time via hash_equals(). * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $mac * @param string $message * @param string $key * @return bool * @throws SodiumException * @throws TypeError */ public static function auth_verify($mac, $message, $key) { return ParagonIE_Sodium_Core32_Util::hashEquals( $mac, self::auth($message, $key) ); } /** * X25519 key exchange followed by XSalsa20Poly1305 symmetric encryption * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $plaintext * @param string $nonce * @param string $keypair * @return string * @throws SodiumException * @throws TypeError */ public static function box($plaintext, $nonce, $keypair) { return self::secretbox( $plaintext, $nonce, self::box_beforenm( self::box_secretkey($keypair), self::box_publickey($keypair) ) ); } /** * X25519-XSalsa20-Poly1305 with one ephemeral X25519 keypair. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $publicKey * @return string * @throws SodiumException * @throws TypeError */ public static function box_seal($message, $publicKey) { /** @var string $ephemeralKeypair */ $ephemeralKeypair = self::box_keypair(); /** @var string $ephemeralSK */ $ephemeralSK = self::box_secretkey($ephemeralKeypair); /** @var string $ephemeralPK */ $ephemeralPK = self::box_publickey($ephemeralKeypair); /** @var string $nonce */ $nonce = self::generichash( $ephemeralPK . $publicKey, '', 24 ); /** @var string $keypair - The combined keypair used in crypto_box() */ $keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey); /** @var string $ciphertext Ciphertext + MAC from crypto_box */ $ciphertext = self::box($message, $nonce, $keypair); try { ParagonIE_Sodium_Compat::memzero($ephemeralKeypair); ParagonIE_Sodium_Compat::memzero($ephemeralSK); ParagonIE_Sodium_Compat::memzero($nonce); } catch (SodiumException $ex) { $ephemeralKeypair = null; $ephemeralSK = null; $nonce = null; } return $ephemeralPK . $ciphertext; } /** * Opens a message encrypted via box_seal(). * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $keypair * @return string * @throws SodiumException * @throws TypeError */ public static function box_seal_open($message, $keypair) { /** @var string $ephemeralPK */ $ephemeralPK = ParagonIE_Sodium_Core32_Util::substr($message, 0, 32); /** @var string $ciphertext (ciphertext + MAC) */ $ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 32); /** @var string $secretKey */ $secretKey = self::box_secretkey($keypair); /** @var string $publicKey */ $publicKey = self::box_publickey($keypair); /** @var string $nonce */ $nonce = self::generichash( $ephemeralPK . $publicKey, '', 24 ); /** @var string $keypair */ $keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK); /** @var string $m */ $m = self::box_open($ciphertext, $nonce, $keypair); try { ParagonIE_Sodium_Compat::memzero($secretKey); ParagonIE_Sodium_Compat::memzero($ephemeralPK); ParagonIE_Sodium_Compat::memzero($nonce); } catch (SodiumException $ex) { $secretKey = null; $ephemeralPK = null; $nonce = null; } return $m; } /** * Used by crypto_box() to get the crypto_secretbox() key. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $sk * @param string $pk * @return string * @throws SodiumException * @throws TypeError */ public static function box_beforenm($sk, $pk) { return ParagonIE_Sodium_Core32_HSalsa20::hsalsa20( str_repeat("\x00", 16), self::scalarmult($sk, $pk) ); } /** * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @return string * @throws Exception * @throws SodiumException * @throws TypeError */ public static function box_keypair() { $sKey = random_bytes(32); $pKey = self::scalarmult_base($sKey); return $sKey . $pKey; } /** * @param string $seed * @return string * @throws SodiumException * @throws TypeError */ public static function box_seed_keypair($seed) { $sKey = ParagonIE_Sodium_Core32_Util::substr( hash('sha512', $seed, true), 0, 32 ); $pKey = self::scalarmult_base($sKey); return $sKey . $pKey; } /** * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $sKey * @param string $pKey * @return string * @throws TypeError */ public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) { return ParagonIE_Sodium_Core32_Util::substr($sKey, 0, 32) . ParagonIE_Sodium_Core32_Util::substr($pKey, 0, 32); } /** * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $keypair * @return string * @throws RangeException * @throws TypeError */ public static function box_secretkey($keypair) { if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== 64) { throw new RangeException( 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' ); } return ParagonIE_Sodium_Core32_Util::substr($keypair, 0, 32); } /** * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $keypair * @return string * @throws RangeException * @throws TypeError */ public static function box_publickey($keypair) { if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { throw new RangeException( 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' ); } return ParagonIE_Sodium_Core32_Util::substr($keypair, 32, 32); } /** * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $sKey * @return string * @throws RangeException * @throws SodiumException * @throws TypeError */ public static function box_publickey_from_secretkey($sKey) { if (ParagonIE_Sodium_Core32_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) { throw new RangeException( 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.' ); } return self::scalarmult_base($sKey); } /** * Decrypt a message encrypted with box(). * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $ciphertext * @param string $nonce * @param string $keypair * @return string * @throws SodiumException * @throws TypeError */ public static function box_open($ciphertext, $nonce, $keypair) { return self::secretbox_open( $ciphertext, $nonce, self::box_beforenm( self::box_secretkey($keypair), self::box_publickey($keypair) ) ); } /** * Calculate a BLAKE2b hash. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string|null $key * @param int $outlen * @return string * @throws RangeException * @throws SodiumException * @throws TypeError */ public static function generichash($message, $key = '', $outlen = 32) { // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor(); $k = null; if (!empty($key)) { /** @var SplFixedArray $k */ $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key); if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) { throw new RangeException('Invalid key size'); } } /** @var SplFixedArray $in */ $in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($message); /** @var SplFixedArray $ctx */ $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outlen); ParagonIE_Sodium_Core32_BLAKE2b::update($ctx, $in, $in->count()); /** @var SplFixedArray $out */ $out = new SplFixedArray($outlen); $out = ParagonIE_Sodium_Core32_BLAKE2b::finish($ctx, $out); /** @var array<int, int> */ $outArray = $out->toArray(); return ParagonIE_Sodium_Core32_Util::intArrayToString($outArray); } /** * Finalize a BLAKE2b hashing context, returning the hash. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $ctx * @param int $outlen * @return string * @throws SodiumException * @throws TypeError */ public static function generichash_final($ctx, $outlen = 32) { if (!is_string($ctx)) { throw new TypeError('Context must be a string'); } $out = new SplFixedArray($outlen); /** @var SplFixedArray $context */ $context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext($ctx); /** @var SplFixedArray $out */ $out = ParagonIE_Sodium_Core32_BLAKE2b::finish($context, $out); /** @var array<int, int> */ $outArray = $out->toArray(); return ParagonIE_Sodium_Core32_Util::intArrayToString($outArray); } /** * Initialize a hashing context for BLAKE2b. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $key * @param int $outputLength * @return string * @throws RangeException * @throws SodiumException * @throws TypeError */ public static function generichash_init($key = '', $outputLength = 32) { // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor(); $k = null; if (!empty($key)) { $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key); if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) { throw new RangeException('Invalid key size'); } } /** @var SplFixedArray $ctx */ $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outputLength); return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($ctx); } /** * Initialize a hashing context for BLAKE2b. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $key * @param int $outputLength * @param string $salt * @param string $personal * @return string * @throws RangeException * @throws SodiumException * @throws TypeError */ public static function generichash_init_salt_personal( $key = '', $outputLength = 32, $salt = '', $personal = '' ) { // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor(); $k = null; if (!empty($key)) { $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key); if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) { throw new RangeException('Invalid key size'); } } if (!empty($salt)) { $s = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($salt); } else { $s = null; } if (!empty($salt)) { $p = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($personal); } else { $p = null; } /** @var SplFixedArray $ctx */ $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outputLength, $s, $p); return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($ctx); } /** * Update a hashing context for BLAKE2b with $message * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $ctx * @param string $message * @return string * @throws SodiumException * @throws TypeError */ public static function generichash_update($ctx, $message) { // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor(); /** @var SplFixedArray $context */ $context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext($ctx); /** @var SplFixedArray $in */ $in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($message); ParagonIE_Sodium_Core32_BLAKE2b::update($context, $in, $in->count()); return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($context); } /** * Libsodium's crypto_kx(). * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $my_sk * @param string $their_pk * @param string $client_pk * @param string $server_pk * @return string * @throws SodiumException * @throws TypeError */ public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) { return self::generichash( self::scalarmult($my_sk, $their_pk) . $client_pk . $server_pk ); } /** * ECDH over Curve25519 * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $sKey * @param string $pKey * @return string * * @throws SodiumException * @throws TypeError */ public static function scalarmult($sKey, $pKey) { $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey); self::scalarmult_throw_if_zero($q); return $q; } /** * ECDH over Curve25519, using the basepoint. * Used to get a secret key from a public key. * * @param string $secret * @return string * * @throws SodiumException * @throws TypeError */ public static function scalarmult_base($secret) { $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10_base($secret); self::scalarmult_throw_if_zero($q); return $q; } /** * This throws an Error if a zero public key was passed to the function. * * @param string $q * @return void * @throws SodiumException * @throws TypeError */ protected static function scalarmult_throw_if_zero($q) { $d = 0; for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) { $d |= ParagonIE_Sodium_Core32_Util::chrToInt($q[$i]); } /* branch-free variant of === 0 */ if (-(1 & (($d - 1) >> 8))) { throw new SodiumException('Zero public key is not allowed'); } } /** * XSalsa20-Poly1305 authenticated symmetric-key encryption. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $plaintext * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function secretbox($plaintext, $nonce, $key) { /** @var string $subkey */ $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key); /** @var string $block0 */ $block0 = str_repeat("\x00", 32); /** @var int $mlen - Length of the plaintext message */ $mlen = ParagonIE_Sodium_Core32_Util::strlen($plaintext); $mlen0 = $mlen; if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) { $mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES; } $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0); /** @var string $block0 */ $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor( $block0, ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8), $subkey ); /** @var string $c */ $c = ParagonIE_Sodium_Core32_Util::substr( $block0, self::secretbox_xsalsa20poly1305_ZEROBYTES ); if ($mlen > $mlen0) { $c .= ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic( ParagonIE_Sodium_Core32_Util::substr( $plaintext, self::secretbox_xsalsa20poly1305_ZEROBYTES ), ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8), 1, $subkey ); } $state = new ParagonIE_Sodium_Core32_Poly1305_State( ParagonIE_Sodium_Core32_Util::substr( $block0, 0, self::onetimeauth_poly1305_KEYBYTES ) ); try { ParagonIE_Sodium_Compat::memzero($block0); ParagonIE_Sodium_Compat::memzero($subkey); } catch (SodiumException $ex) { $block0 = null; $subkey = null; } $state->update($c); /** @var string $c - MAC || ciphertext */ $c = $state->finish() . $c; unset($state); return $c; } /** * Decrypt a ciphertext generated via secretbox(). * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $ciphertext * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function secretbox_open($ciphertext, $nonce, $key) { /** @var string $mac */ $mac = ParagonIE_Sodium_Core32_Util::substr( $ciphertext, 0, self::secretbox_xsalsa20poly1305_MACBYTES ); /** @var string $c */ $c = ParagonIE_Sodium_Core32_Util::substr( $ciphertext, self::secretbox_xsalsa20poly1305_MACBYTES ); /** @var int $clen */ $clen = ParagonIE_Sodium_Core32_Util::strlen($c); /** @var string $subkey */ $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key); /** @var string $block0 */ $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20( 64, ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8), $subkey ); $verified = ParagonIE_Sodium_Core32_Poly1305::onetimeauth_verify( $mac, $c, ParagonIE_Sodium_Core32_Util::substr($block0, 0, 32) ); if (!$verified) { try { ParagonIE_Sodium_Compat::memzero($subkey); } catch (SodiumException $ex) { $subkey = null; } throw new SodiumException('Invalid MAC'); } /** @var string $m - Decrypted message */ $m = ParagonIE_Sodium_Core32_Util::xorStrings( ParagonIE_Sodium_Core32_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES), ParagonIE_Sodium_Core32_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES) ); if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) { // We had more than 1 block, so let's continue to decrypt the rest. $m .= ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic( ParagonIE_Sodium_Core32_Util::substr( $c, self::secretbox_xsalsa20poly1305_ZEROBYTES ), ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8), 1, (string) $subkey ); } return $m; } /** * XChaCha20-Poly1305 authenticated symmetric-key encryption. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $plaintext * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) { /** @var string $subkey */ $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20( ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16), $key ); $nonceLast = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8); /** @var string $block0 */ $block0 = str_repeat("\x00", 32); /** @var int $mlen - Length of the plaintext message */ $mlen = ParagonIE_Sodium_Core32_Util::strlen($plaintext); $mlen0 = $mlen; if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) { $mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES; } $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0); /** @var string $block0 */ $block0 = ParagonIE_Sodium_Core32_ChaCha20::streamXorIc( $block0, $nonceLast, $subkey ); /** @var string $c */ $c = ParagonIE_Sodium_Core32_Util::substr( $block0, self::secretbox_xchacha20poly1305_ZEROBYTES ); if ($mlen > $mlen0) { $c .= ParagonIE_Sodium_Core32_ChaCha20::streamXorIc( ParagonIE_Sodium_Core32_Util::substr( $plaintext, self::secretbox_xchacha20poly1305_ZEROBYTES ), $nonceLast, $subkey, ParagonIE_Sodium_Core32_Util::store64_le(1) ); } $state = new ParagonIE_Sodium_Core32_Poly1305_State( ParagonIE_Sodium_Core32_Util::substr( $block0, 0, self::onetimeauth_poly1305_KEYBYTES ) ); try { ParagonIE_Sodium_Compat::memzero($block0); ParagonIE_Sodium_Compat::memzero($subkey); } catch (SodiumException $ex) { $block0 = null; $subkey = null; } $state->update($c); /** @var string $c - MAC || ciphertext */ $c = $state->finish() . $c; unset($state); return $c; } /** * Decrypt a ciphertext generated via secretbox_xchacha20poly1305(). * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $ciphertext * @param string $nonce * @param string $key * @return string * @throws SodiumException * @throws TypeError */ public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) { /** @var string $mac */ $mac = ParagonIE_Sodium_Core32_Util::substr( $ciphertext, 0, self::secretbox_xchacha20poly1305_MACBYTES ); /** @var string $c */ $c = ParagonIE_Sodium_Core32_Util::substr( $ciphertext, self::secretbox_xchacha20poly1305_MACBYTES ); /** @var int $clen */ $clen = ParagonIE_Sodium_Core32_Util::strlen($c); /** @var string $subkey */ $subkey = ParagonIE_Sodium_Core32_HChaCha20::hchacha20($nonce, $key); /** @var string $block0 */ $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream( 64, ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8), $subkey ); $verified = ParagonIE_Sodium_Core32_Poly1305::onetimeauth_verify( $mac, $c, ParagonIE_Sodium_Core32_Util::substr($block0, 0, 32) ); if (!$verified) { try { ParagonIE_Sodium_Compat::memzero($subkey); } catch (SodiumException $ex) { $subkey = null; } throw new SodiumException('Invalid MAC'); } /** @var string $m - Decrypted message */ $m = ParagonIE_Sodium_Core32_Util::xorStrings( ParagonIE_Sodium_Core32_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES), ParagonIE_Sodium_Core32_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES) ); if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) { // We had more than 1 block, so let's continue to decrypt the rest. $m .= ParagonIE_Sodium_Core32_ChaCha20::streamXorIc( ParagonIE_Sodium_Core32_Util::substr( $c, self::secretbox_xchacha20poly1305_ZEROBYTES ), ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8), (string) $subkey, ParagonIE_Sodium_Core32_Util::store64_le(1) ); } return $m; } /** * @param string $key * @return array<int, string> Returns a state and a header. * @throws Exception * @throws SodiumException */ public static function secretstream_xchacha20poly1305_init_push($key) { # randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES); $out = random_bytes(24); # crypto_core_hchacha20(state->k, out, k, NULL); $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20($out, $key); $state = new ParagonIE_Sodium_Core32_SecretStream_State( $subkey, ParagonIE_Sodium_Core32_Util::substr($out, 16, 8) . str_repeat("\0", 4) ); # _crypto_secretstream_xchacha20poly1305_counter_reset(state); $state->counterReset(); # memcpy(STATE_INONCE(state), out + crypto_core_hchacha20_INPUTBYTES, # crypto_secretstream_xchacha20poly1305_INONCEBYTES); # memset(state->_pad, 0, sizeof state->_pad); return array( $state->toString(), $out ); } /** * @param string $key * @param string $header * @return string Returns a state. * @throws Exception */ public static function secretstream_xchacha20poly1305_init_pull($key, $header) { # crypto_core_hchacha20(state->k, in, k, NULL); $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20( ParagonIE_Sodium_Core32_Util::substr($header, 0, 16), $key ); $state = new ParagonIE_Sodium_Core32_SecretStream_State( $subkey, ParagonIE_Sodium_Core32_Util::substr($header, 16) ); $state->counterReset(); # memcpy(STATE_INONCE(state), in + crypto_core_hchacha20_INPUTBYTES, # crypto_secretstream_xchacha20poly1305_INONCEBYTES); # memset(state->_pad, 0, sizeof state->_pad); # return 0; return $state->toString(); } /** * @param string $state * @param string $msg * @param string $aad * @param int $tag * @return string * @throws SodiumException */ public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0) { $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state); # crypto_onetimeauth_poly1305_state poly1305_state; # unsigned char block[64U]; # unsigned char slen[8U]; # unsigned char *c; # unsigned char *mac; $msglen = ParagonIE_Sodium_Core32_Util::strlen($msg); $aadlen = ParagonIE_Sodium_Core32_Util::strlen($aad); if ((($msglen + 63) >> 6) > 0xfffffffe) { throw new SodiumException( 'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes' ); } # if (outlen_p != NULL) { # *outlen_p = 0U; # } # if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) { # sodium_misuse(); # } # crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k); # crypto_onetimeauth_poly1305_init(&poly1305_state, block); # sodium_memzero(block, sizeof block); $auth = new ParagonIE_Sodium_Core32_Poly1305_State( ParagonIE_Sodium_Core32_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey()) ); # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen); $auth->update($aad); # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0, # (0x10 - adlen) & 0xf); $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf))); # memset(block, 0, sizeof block); # block[0] = tag; # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block, # state->nonce, 1U, state->k); $block = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc( ParagonIE_Sodium_Core32_Util::intToChr($tag) . str_repeat("\0", 63), $st->getCombinedNonce(), $st->getKey(), ParagonIE_Sodium_Core32_Util::store64_le(1) ); # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block); $auth->update($block); # out[0] = block[0]; $out = $block[0]; # c = out + (sizeof tag); # crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, state->nonce, 2U, state->k); $cipher = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc( $msg, $st->getCombinedNonce(), $st->getKey(), ParagonIE_Sodium_Core32_Util::store64_le(2) ); # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen); $auth->update($cipher); $out .= $cipher; unset($cipher); # crypto_onetimeauth_poly1305_update # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf); $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf))); # STORE64_LE(slen, (uint64_t) adlen); $slen = ParagonIE_Sodium_Core32_Util::store64_le($aadlen); # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); $auth->update($slen); # STORE64_LE(slen, (sizeof block) + mlen); $slen = ParagonIE_Sodium_Core32_Util::store64_le(64 + $msglen); # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); $auth->update($slen); # mac = c + mlen; # crypto_onetimeauth_poly1305_final(&poly1305_state, mac); $mac = $auth->finish(); $out .= $mac; # sodium_memzero(&poly1305_state, sizeof poly1305_state); unset($auth); # XOR_BUF(STATE_INONCE(state), mac, # crypto_secretstream_xchacha20poly1305_INONCEBYTES); $st->xorNonce($mac); # sodium_increment(STATE_COUNTER(state), # crypto_secretstream_xchacha20poly1305_COUNTERBYTES); $st->incrementCounter(); // Overwrite by reference: $state = $st->toString(); /** @var bool $rekey */ $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0; # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 || # sodium_is_zero(STATE_COUNTER(state), # crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) { # crypto_secretstream_xchacha20poly1305_rekey(state); # } if ($rekey || $st->needsRekey()) { // DO REKEY self::secretstream_xchacha20poly1305_rekey($state); } # if (outlen_p != NULL) { # *outlen_p = crypto_secretstream_xchacha20poly1305_ABYTES + mlen; # } return $out; } /** * @param string $state * @param string $cipher * @param string $aad * @return bool|array{0: string, 1: int} * @throws SodiumException */ public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '') { $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state); $cipherlen = ParagonIE_Sodium_Core32_Util::strlen($cipher); # mlen = inlen - crypto_secretstream_xchacha20poly1305_ABYTES; $msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES; $aadlen = ParagonIE_Sodium_Core32_Util::strlen($aad); # if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) { # sodium_misuse(); # } if ((($msglen + 63) >> 6) > 0xfffffffe) { throw new SodiumException( 'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes' ); } # crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k); # crypto_onetimeauth_poly1305_init(&poly1305_state, block); # sodium_memzero(block, sizeof block); $auth = new ParagonIE_Sodium_Core32_Poly1305_State( ParagonIE_Sodium_Core32_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey()) ); # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen); $auth->update($aad); # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0, # (0x10 - adlen) & 0xf); $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf))); # memset(block, 0, sizeof block); # block[0] = in[0]; # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block, # state->nonce, 1U, state->k); $block = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc( $cipher[0] . str_repeat("\0", 63), $st->getCombinedNonce(), $st->getKey(), ParagonIE_Sodium_Core32_Util::store64_le(1) ); # tag = block[0]; # block[0] = in[0]; # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block); $tag = ParagonIE_Sodium_Core32_Util::chrToInt($block[0]); $block[0] = $cipher[0]; $auth->update($block); # c = in + (sizeof tag); # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen); $auth->update(ParagonIE_Sodium_Core32_Util::substr($cipher, 1, $msglen)); # crypto_onetimeauth_poly1305_update # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf); $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf))); # STORE64_LE(slen, (uint64_t) adlen); # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); $slen = ParagonIE_Sodium_Core32_Util::store64_le($aadlen); $auth->update($slen); # STORE64_LE(slen, (sizeof block) + mlen); # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen); $slen = ParagonIE_Sodium_Core32_Util::store64_le(64 + $msglen); $auth->update($slen); # crypto_onetimeauth_poly1305_final(&poly1305_state, mac); # sodium_memzero(&poly1305_state, sizeof poly1305_state); $mac = $auth->finish(); # stored_mac = c + mlen; # if (sodium_memcmp(mac, stored_mac, sizeof mac) != 0) { # sodium_memzero(mac, sizeof mac); # return -1; # } $stored = ParagonIE_Sodium_Core32_Util::substr($cipher, $msglen + 1, 16); if (!ParagonIE_Sodium_Core32_Util::hashEquals($mac, $stored)) { return false; } # crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, state->nonce, 2U, state->k); $out = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc( ParagonIE_Sodium_Core32_Util::substr($cipher, 1, $msglen), $st->getCombinedNonce(), $st->getKey(), ParagonIE_Sodium_Core32_Util::store64_le(2) ); # XOR_BUF(STATE_INONCE(state), mac, # crypto_secretstream_xchacha20poly1305_INONCEBYTES); $st->xorNonce($mac); # sodium_increment(STATE_COUNTER(state), # crypto_secretstream_xchacha20poly1305_COUNTERBYTES); $st->incrementCounter(); # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 || # sodium_is_zero(STATE_COUNTER(state), # crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) { # crypto_secretstream_xchacha20poly1305_rekey(state); # } // Overwrite by reference: $state = $st->toString(); /** @var bool $rekey */ $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0; if ($rekey || $st->needsRekey()) { // DO REKEY self::secretstream_xchacha20poly1305_rekey($state); } return array($out, $tag); } /** * @param string $state * @return void * @throws SodiumException */ public static function secretstream_xchacha20poly1305_rekey(&$state) { $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state); # unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + # crypto_secretstream_xchacha20poly1305_INONCEBYTES]; # size_t i; # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) { # new_key_and_inonce[i] = state->k[i]; # } $new_key_and_inonce = $st->getKey(); # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) { # new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i] = # STATE_INONCE(state)[i]; # } $new_key_and_inonce .= ParagonIE_Sodium_Core32_Util::substR($st->getNonce(), 0, 8); # crypto_stream_chacha20_ietf_xor(new_key_and_inonce, new_key_and_inonce, # sizeof new_key_and_inonce, # state->nonce, state->k); $st->rekey(ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc( $new_key_and_inonce, $st->getCombinedNonce(), $st->getKey(), ParagonIE_Sodium_Core32_Util::store64_le(0) )); # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) { # state->k[i] = new_key_and_inonce[i]; # } # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) { # STATE_INONCE(state)[i] = # new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i]; # } # _crypto_secretstream_xchacha20poly1305_counter_reset(state); $st->counterReset(); $state = $st->toString(); } /** * Detached Ed25519 signature. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $sk * @return string * @throws SodiumException * @throws TypeError */ public static function sign_detached($message, $sk) { return ParagonIE_Sodium_Core32_Ed25519::sign_detached($message, $sk); } /** * Attached Ed25519 signature. (Returns a signed message.) * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $message * @param string $sk * @return string * @throws SodiumException * @throws TypeError */ public static function sign($message, $sk) { return ParagonIE_Sodium_Core32_Ed25519::sign($message, $sk); } /** * Opens a signed message. If valid, returns the message. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $signedMessage * @param string $pk * @return string * @throws SodiumException * @throws TypeError */ public static function sign_open($signedMessage, $pk) { return ParagonIE_Sodium_Core32_Ed25519::sign_open($signedMessage, $pk); } /** * Verify a detached signature of a given message and public key. * * @internal Do not use this directly. Use ParagonIE_Sodium_Compat. * * @param string $signature * @param string $message * @param string $pk * @return bool * @throws SodiumException * @throws TypeError */ public static function sign_verify_detached($signature, $message, $pk) { return ParagonIE_Sodium_Core32_Ed25519::verify_detached($signature, $message, $pk); } } rest.cls.php 0000644 00000016715 15162226223 0007025 0 ustar 00 <?php /** * The REST related class. * * @since 2.9.4 */ namespace LiteSpeed; defined('WPINC') || exit(); class REST extends Root { const LOG_TAG = '☎️'; private $_internal_rest_status = false; /** * Confructor of ESI * * @since 2.9.4 */ public function __construct() { // Hook to internal REST call add_filter('rest_request_before_callbacks', array($this, 'set_internal_rest_on')); add_filter('rest_request_after_callbacks', array($this, 'set_internal_rest_off')); add_action('rest_api_init', array($this, 'rest_api_init')); } /** * Register REST hooks * * @since 3.0 * @access public */ public function rest_api_init() { // Activate or deactivate a specific crawler callback register_rest_route('litespeed/v1', '/toggle_crawler_state', array( 'methods' => 'POST', 'callback' => array($this, 'toggle_crawler_state'), 'permission_callback' => function () { return current_user_can('manage_network_options') || current_user_can('manage_options'); }, )); register_rest_route('litespeed/v1', '/tool/check_ip', array( 'methods' => 'GET', 'callback' => array($this, 'check_ip'), 'permission_callback' => function () { return current_user_can('manage_network_options') || current_user_can('manage_options'); }, )); // IP callback validate register_rest_route('litespeed/v3', '/ip_validate', array( 'methods' => 'POST', 'callback' => array($this, 'ip_validate'), 'permission_callback' => array($this, 'is_from_cloud'), )); ## 1.2. WP REST Dryrun Callback register_rest_route('litespeed/v3', '/wp_rest_echo', array( 'methods' => 'POST', 'callback' => array($this, 'wp_rest_echo'), 'permission_callback' => array($this, 'is_from_cloud'), )); register_rest_route('litespeed/v3', '/ping', array( 'methods' => 'POST', 'callback' => array($this, 'ping'), 'permission_callback' => array($this, 'is_from_cloud'), )); // CDN setup callback notification register_rest_route('litespeed/v3', '/cdn_status', array( 'methods' => 'POST', 'callback' => array($this, 'cdn_status'), 'permission_callback' => array($this, 'is_from_cloud'), )); // Image optm notify_img // Need validation register_rest_route('litespeed/v1', '/notify_img', array( 'methods' => 'POST', 'callback' => array($this, 'notify_img'), 'permission_callback' => array($this, 'is_from_cloud'), )); register_rest_route('litespeed/v1', '/notify_ccss', array( 'methods' => 'POST', 'callback' => array($this, 'notify_ccss'), 'permission_callback' => array($this, 'is_from_cloud'), )); register_rest_route('litespeed/v1', '/notify_ucss', array( 'methods' => 'POST', 'callback' => array($this, 'notify_ucss'), 'permission_callback' => array($this, 'is_from_cloud'), )); register_rest_route('litespeed/v1', '/notify_vpi', array( 'methods' => 'POST', 'callback' => array($this, 'notify_vpi'), 'permission_callback' => array($this, 'is_from_cloud'), )); register_rest_route('litespeed/v3', '/err_domains', array( 'methods' => 'POST', 'callback' => array($this, 'err_domains'), 'permission_callback' => array($this, 'is_from_cloud'), )); // Image optm check_img // Need validation register_rest_route('litespeed/v1', '/check_img', array( 'methods' => 'POST', 'callback' => array($this, 'check_img'), 'permission_callback' => array($this, 'is_from_cloud'), )); } /** * Call to freeze or melt the crawler clicked * * @since 4.3 */ public function toggle_crawler_state() { if (isset($_POST['crawler_id'])) { return $this->cls('Crawler')->toggle_activeness($_POST['crawler_id']) ? 1 : 0; } } /** * Check if the request is from cloud nodes * * @since 4.2 * @since 4.4.7 As there is always token/api key validation, ip validation is redundant */ public function is_from_cloud() { // return true; return $this->cls('Cloud')->is_from_cloud(); } /** * Ping pong * * @since 3.0.4 */ public function ping() { return $this->cls('Cloud')->ping(); } /** * Launch api call * * @since 3.0 */ public function check_ip() { return Tool::cls()->check_ip(); } /** * Launch api call * * @since 3.0 */ public function ip_validate() { return $this->cls('Cloud')->ip_validate(); } /** * Launch api call * * @since 3.0 */ public function wp_rest_echo() { return $this->cls('Cloud')->wp_rest_echo(); } /** * Endpoint for QC to notify plugin of CDN status update. * * @since 7.0 */ public function cdn_status() { return $this->cls('Cloud')->update_cdn_status(); } /** * Launch api call * * @since 3.0 */ public function notify_img() { return Img_Optm::cls()->notify_img(); } /** * @since 7.1 */ public function notify_ccss() { self::debug('notify_ccss'); return CSS::cls()->notify(); } /** * @since 5.2 */ public function notify_ucss() { self::debug('notify_ucss'); return UCSS::cls()->notify(); } /** * @since 4.7 */ public function notify_vpi() { self::debug('notify_vpi'); return VPI::cls()->notify(); } /** * @since 4.7 */ public function err_domains() { self::debug('err_domains'); return $this->cls('Cloud')->rest_err_domains(); } /** * Launch api call * * @since 3.0 */ public function check_img() { return Img_Optm::cls()->check_img(); } /** * Return error * * @since 5.7.0.1 */ public static function err($code) { return array('_res' => 'err', '_msg' => $code); } /** * Set internal REST tag to ON * * @since 2.9.4 * @access public */ public function set_internal_rest_on($not_used = null) { $this->_internal_rest_status = true; Debug2::debug2('[REST] ✅ Internal REST ON [filter] rest_request_before_callbacks'); return $not_used; } /** * Set internal REST tag to OFF * * @since 2.9.4 * @access public */ public function set_internal_rest_off($not_used = null) { $this->_internal_rest_status = false; Debug2::debug2('[REST] ❎ Internal REST OFF [filter] rest_request_after_callbacks'); return $not_used; } /** * Get internal REST tag * * @since 2.9.4 * @access public */ public function is_internal_rest() { return $this->_internal_rest_status; } /** * Check if an URL or current page is REST req or not * * @since 2.9.3 * @since 2.9.4 Moved here from Utility, dropped static * @access public */ public function is_rest($url = false) { // For WP 4.4.0- compatibility if (!function_exists('rest_get_url_prefix')) { return defined('REST_REQUEST') && REST_REQUEST; } $prefix = rest_get_url_prefix(); // Case #1: After WP_REST_Request initialisation if (defined('REST_REQUEST') && REST_REQUEST) { return true; } // Case #2: Support "plain" permalink settings if (isset($_GET['rest_route']) && strpos(trim($_GET['rest_route'], '\\/'), $prefix, 0) === 0) { return true; } if (!$url) { return false; } // Case #3: URL Path begins with wp-json/ (REST prefix) Safe for subfolder installation $rest_url = wp_parse_url(site_url($prefix)); $current_url = wp_parse_url($url); // Debug2::debug( '[Util] is_rest check [base] ', $rest_url ); // Debug2::debug( '[Util] is_rest check [curr] ', $current_url ); // Debug2::debug( '[Util] is_rest check [curr2] ', wp_parse_url( add_query_arg( array( ) ) ) ); if ($current_url !== false && !empty($current_url['path']) && $rest_url !== false && !empty($rest_url['path'])) { return strpos($current_url['path'], $rest_url['path']) === 0; } return false; } } optimize.cls.php 0000644 00000111727 15162226224 0007710 0 ustar 00 <?php /** * The optimize class. * * @since 1.2.2 */ namespace LiteSpeed; defined('WPINC') || exit(); class Optimize extends Base { const LIB_FILE_CSS_ASYNC = 'assets/js/css_async.min.js'; const LIB_FILE_WEBFONTLOADER = 'assets/js/webfontloader.min.js'; const LIB_FILE_JS_DELAY = 'assets/js/js_delay.min.js'; const ITEM_TIMESTAMP_PURGE_CSS = 'timestamp_purge_css'; private $content; private $content_ori; private $cfg_css_min; private $cfg_css_comb; private $cfg_js_min; private $cfg_js_comb; private $cfg_css_async; private $cfg_js_delay_inc = array(); private $cfg_js_defer; private $cfg_js_defer_exc = false; private $cfg_ggfonts_async; private $_conf_css_font_display; private $cfg_ggfonts_rm; private $dns_prefetch; private $dns_preconnect; private $_ggfonts_urls = array(); private $_ccss; private $_ucss = false; private $__optimizer; private $html_foot = ''; // The html info append to <body> private $html_head = ''; // The html info prepend to <body> private static $_var_i = 0; private $_var_preserve_js = array(); private $_request_url; /** * Constructor * @since 4.0 */ public function __construct() { Debug2::debug('[Optm] init'); $this->__optimizer = $this->cls('Optimizer'); } /** * Init optimizer * * @since 3.0 * @access protected */ public function init() { $this->cfg_css_async = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_CSS_ASYNC); if ($this->cfg_css_async) { if (!$this->cls('Cloud')->activated()) { Debug2::debug('[Optm] ❌ CCSS set to OFF due to QC not activated'); $this->cfg_css_async = false; } if ((defined('LITESPEED_GUEST_OPTM') || ($this->conf(self::O_OPTM_UCSS) && $this->conf(self::O_OPTM_CSS_COMB))) && $this->conf(self::O_OPTM_UCSS_INLINE)) { Debug2::debug('[Optm] ⚠️ CCSS set to OFF due to UCSS Inline'); $this->cfg_css_async = false; } } $this->cfg_js_defer = $this->conf(self::O_OPTM_JS_DEFER); if (defined('LITESPEED_GUEST_OPTM')) { $this->cfg_js_defer = 2; } if ($this->cfg_js_defer == 2) { add_filter( 'litespeed_optm_cssjs', function ($con, $file_type) { if ($file_type == 'js') { $con = str_replace('DOMContentLoaded', 'DOMContentLiteSpeedLoaded', $con); // $con = str_replace( 'addEventListener("load"', 'addEventListener("litespeedLoad"', $con ); } return $con; }, 20, 2 ); } // To remove emoji from WP if ($this->conf(self::O_OPTM_EMOJI_RM)) { $this->_emoji_rm(); } if ($this->conf(self::O_OPTM_QS_RM)) { add_filter('style_loader_src', array($this, 'remove_query_strings'), 999); add_filter('script_loader_src', array($this, 'remove_query_strings'), 999); } // GM JS exclude @since 4.1 if (defined('LITESPEED_GUEST_OPTM')) { $this->cfg_js_defer_exc = apply_filters('litespeed_optm_gm_js_exc', $this->conf(self::O_OPTM_GM_JS_EXC)); } else { /** * Exclude js from deferred setting * @since 1.5 */ if ($this->cfg_js_defer) { add_filter('litespeed_optm_js_defer_exc', array($this->cls('Data'), 'load_js_defer_exc')); $this->cfg_js_defer_exc = apply_filters('litespeed_optm_js_defer_exc', $this->conf(self::O_OPTM_JS_DEFER_EXC)); $this->cfg_js_delay_inc = apply_filters('litespeed_optm_js_delay_inc', $this->conf(self::O_OPTM_JS_DELAY_INC)); } } /** * Add vary filter for Role Excludes * @since 1.6 */ add_filter('litespeed_vary', array($this, 'vary_add_role_exclude')); /** * Prefetch DNS * @since 1.7.1 */ $this->_dns_prefetch_init(); /** * Preconnect * @since 5.6.1 */ $this->_dns_preconnect_init(); add_filter('litespeed_buffer_finalize', array($this, 'finalize'), 20); } /** * Exclude role from optimization filter * * @since 1.6 * @access public */ public function vary_add_role_exclude($vary) { if ($this->cls('Conf')->in_optm_exc_roles()) { $vary['role_exclude_optm'] = 1; } return $vary; } /** * Remove emoji from WP * * @since 1.4 * @since 2.9.8 Changed to private * @access private */ private function _emoji_rm() { remove_action('wp_head', 'print_emoji_detection_script', 7); remove_action('admin_print_scripts', 'print_emoji_detection_script'); remove_filter('the_content_feed', 'wp_staticize_emoji'); remove_filter('comment_text_rss', 'wp_staticize_emoji'); /** * Added for better result * @since 1.6.2.1 */ remove_action('wp_print_styles', 'print_emoji_styles'); remove_action('admin_print_styles', 'print_emoji_styles'); remove_filter('wp_mail', 'wp_staticize_emoji_for_email'); } /** * Delete file-based cache folder * * @since 2.1 * @access public */ public function rm_cache_folder($subsite_id = false) { if ($subsite_id) { file_exists(LITESPEED_STATIC_DIR . '/css/' . $subsite_id) && File::rrmdir(LITESPEED_STATIC_DIR . '/css/' . $subsite_id); file_exists(LITESPEED_STATIC_DIR . '/js/' . $subsite_id) && File::rrmdir(LITESPEED_STATIC_DIR . '/js/' . $subsite_id); return; } file_exists(LITESPEED_STATIC_DIR . '/css') && File::rrmdir(LITESPEED_STATIC_DIR . '/css'); file_exists(LITESPEED_STATIC_DIR . '/js') && File::rrmdir(LITESPEED_STATIC_DIR . '/js'); } /** * Remove QS * * @since 1.3 * @access public */ public function remove_query_strings($src) { if (strpos($src, '_litespeed_rm_qs=0') || strpos($src, '/recaptcha')) { return $src; } if (!Utility::is_internal_file($src)) { return $src; } if (strpos($src, '.js?') !== false || strpos($src, '.css?') !== false) { $src = preg_replace('/\?.*/', '', $src); } return $src; } /** * Run optimize process * NOTE: As this is after cache finalized, can NOT set any cache control anymore * * @since 1.2.2 * @access public * @return string The content that is after optimization */ public function finalize($content) { if (defined('LITESPEED_NO_PAGEOPTM')) { Debug2::debug2('[Optm] bypass: NO_PAGEOPTM const'); return $content; } if (!defined('LITESPEED_IS_HTML')) { Debug2::debug('[Optm] bypass: Not frontend HTML type'); return $content; } if (!defined('LITESPEED_GUEST_OPTM')) { if (!Control::is_cacheable()) { Debug2::debug('[Optm] bypass: Not cacheable'); return $content; } // Check if hit URI excludes add_filter('litespeed_optm_uri_exc', array($this->cls('Data'), 'load_optm_uri_exc')); $excludes = apply_filters('litespeed_optm_uri_exc', $this->conf(self::O_OPTM_EXC)); $result = Utility::str_hit_array($_SERVER['REQUEST_URI'], $excludes); if ($result) { Debug2::debug('[Optm] bypass: hit URI Excludes setting: ' . $result); return $content; } } Debug2::debug('[Optm] start'); $this->content_ori = $this->content = $content; $this->_optimize(); return $this->content; } /** * Optimize css src * * @since 1.2.2 * @access private */ private function _optimize() { global $wp; $this->_request_url = get_permalink(); // Backup, in case get_permalink() fails. if (!$this->_request_url) { $this->_request_url = home_url($wp->request); } $this->cfg_css_min = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_CSS_MIN); $this->cfg_css_comb = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_CSS_COMB); $this->cfg_js_min = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_JS_MIN); $this->cfg_js_comb = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_JS_COMB); $this->cfg_ggfonts_rm = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_GGFONTS_RM); $this->cfg_ggfonts_async = !defined('LITESPEED_GUEST_OPTM') && $this->conf(self::O_OPTM_GGFONTS_ASYNC); // forced rm already $this->_conf_css_font_display = !defined('LITESPEED_GUEST_OPTM') && $this->conf(self::O_OPTM_CSS_FONT_DISPLAY); if (!$this->cls('Router')->can_optm()) { Debug2::debug('[Optm] bypass: admin/feed/preview'); return; } if ($this->cfg_css_async) { $this->_ccss = $this->cls('CSS')->prepare_ccss(); if (!$this->_ccss) { Debug2::debug('[Optm] ❌ CCSS set to OFF due to CCSS not generated yet'); $this->cfg_css_async = false; } elseif (strpos($this->_ccss, '<style id="litespeed-ccss" data-error') === 0) { Debug2::debug('[Optm] ❌ CCSS set to OFF due to CCSS failed to generate'); $this->cfg_css_async = false; } } do_action('litespeed_optm'); // Parse css from content $src_list = false; if ($this->cfg_css_min || $this->cfg_css_comb || $this->cfg_ggfonts_rm || $this->cfg_css_async || $this->cfg_ggfonts_async || $this->_conf_css_font_display) { add_filter('litespeed_optimize_css_excludes', array($this->cls('Data'), 'load_css_exc')); list($src_list, $html_list) = $this->_parse_css(); } // css optimizer if ($this->cfg_css_min || $this->cfg_css_comb) { if ($src_list) { // IF combine if ($this->cfg_css_comb) { // Check if has inline UCSS enabled or not if ((defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_UCSS)) && $this->conf(self::O_OPTM_UCSS_INLINE)) { $filename = $this->cls('UCSS')->load($this->_request_url, true); if ($filename) { $filepath_prefix = $this->_build_filepath_prefix('ucss'); $this->_ucss = File::read(LITESPEED_STATIC_DIR . $filepath_prefix . $filename); // Drop all css $this->content = str_replace($html_list, '', $this->content); } } if (!$this->_ucss) { $url = $this->_build_hash_url($src_list); if ($url) { // Handle css async load if ($this->cfg_css_async) { $this->html_head .= '<link rel="preload" data-asynced="1" data-optimized="2" as="style" onload="this.onload=null;this.rel=\'stylesheet\'" href="' . Str::trim_quotes($url) . '" />'; // todo: How to use " in attr wrapper " } else { $this->html_head .= '<link data-optimized="2" rel="stylesheet" href="' . Str::trim_quotes($url) . '" />'; // use 2 as combined } // Move all css to top $this->content = str_replace($html_list, '', $this->content); } } } // Only minify elseif ($this->cfg_css_min) { // will handle async css load inside $this->_src_queue_handler($src_list, $html_list); } // Only HTTP2 push else { foreach ($src_list as $src_info) { if (!empty($src_info['inl'])) { continue; } } } } } // Handle css lazy load if not handled async loaded yet if ($this->cfg_css_async && !$this->cfg_css_min && !$this->cfg_css_comb) { // async html $html_list_async = $this->_async_css_list($html_list, $src_list); // Replace async css $this->content = str_replace($html_list, $html_list_async, $this->content); } // Parse js from buffer as needed $src_list = false; if ($this->cfg_js_min || $this->cfg_js_comb || $this->cfg_js_defer || $this->cfg_js_delay_inc) { add_filter('litespeed_optimize_js_excludes', array($this->cls('Data'), 'load_js_exc')); list($src_list, $html_list) = $this->_parse_js(); } // js optimizer if ($src_list) { // IF combine if ($this->cfg_js_comb) { $url = $this->_build_hash_url($src_list, 'js'); if ($url) { $this->html_foot .= $this->_build_js_tag($url); // Will move all JS to bottom combined one $this->content = str_replace($html_list, '', $this->content); } } // Only minify elseif ($this->cfg_js_min) { // Will handle js defer inside $this->_src_queue_handler($src_list, $html_list, 'js'); } // Only HTTP2 push and Defer else { foreach ($src_list as $k => $src_info) { // Inline JS if (!empty($src_info['inl'])) { if ($this->cfg_js_defer) { $attrs = !empty($src_info['attrs']) ? $src_info['attrs'] : ''; $deferred = $this->_js_inline_defer($src_info['src'], $attrs); if ($deferred) { $this->content = str_replace($html_list[$k], $deferred, $this->content); } } } // JS files else { if ($this->cfg_js_defer) { $deferred = $this->_js_defer($html_list[$k], $src_info['src']); if ($deferred) { $this->content = str_replace($html_list[$k], $deferred, $this->content); } } elseif ($this->cfg_js_delay_inc) { $deferred = $this->_js_delay($html_list[$k], $src_info['src']); if ($deferred) { $this->content = str_replace($html_list[$k], $deferred, $this->content); } } } } } } // Append JS inline var for preserved ESI // Shouldn't give any optm (defer/delay) @since 4.4 if ($this->_var_preserve_js) { $this->html_head .= '<script>var ' . implode(',', $this->_var_preserve_js) . ';</script>'; Debug2::debug2('[Optm] Inline JS defer vars', $this->_var_preserve_js); } // Append async compatibility lib to head if ($this->cfg_css_async) { // Inline css async lib if ($this->conf(self::O_OPTM_CSS_ASYNC_INLINE)) { $this->html_head .= $this->_build_js_inline(File::read(LSCWP_DIR . self::LIB_FILE_CSS_ASYNC), true); } else { $css_async_lib_url = LSWCP_PLUGIN_URL . self::LIB_FILE_CSS_ASYNC; $this->html_head .= $this->_build_js_tag($css_async_lib_url, 'litespeed-css-async-lib'); // Don't exclude it from defer for now } } /** * Handle google fonts async * This will result in a JS snippet in head, so need to put it in the end to avoid being replaced by JS parser */ $this->_async_ggfonts(); /** * Font display optm * @since 3.0 */ $this->_font_optm(); // Inject JS Delay lib $this->_maybe_js_delay(); /** * HTML Lazyload */ if ($this->conf(self::O_OPTM_HTML_LAZY)) { $this->html_head = $this->cls('CSS')->prepare_html_lazy() . $this->html_head; } // Maybe prepend inline UCSS if ($this->_ucss) { $this->html_head = '<style id="litespeed-ucss">' . $this->_ucss . '</style>' . $this->html_head; } // Check if there is any critical css rules setting if ($this->cfg_css_async && $this->_ccss) { $this->html_head = $this->_ccss . $this->html_head; } // Replace html head part $this->html_head = apply_filters('litespeed_optm_html_head', $this->html_head); if ($this->html_head) { if (apply_filters('litespeed_optm_html_after_head', false)) { $this->content = str_replace('</head>', $this->html_head . '</head>', $this->content); } else { // Put header content to be after charset if (strpos($this->content, '<meta charset') !== false) { $this->content = preg_replace('#<meta charset([^>]*)>#isU', '<meta charset$1>' . $this->html_head, $this->content, 1); } else { $this->content = preg_replace('#<head([^>]*)>#isU', '<head$1>' . $this->html_head, $this->content, 1); } } } // Replace html foot part $this->html_foot = apply_filters('litespeed_optm_html_foot', $this->html_foot); if ($this->html_foot) { $this->content = str_replace('</body>', $this->html_foot . '</body>', $this->content); } // Drop noscript if enabled if ($this->conf(self::O_OPTM_NOSCRIPT_RM)) { // $this->content = preg_replace( '#<noscript>.*</noscript>#isU', '', $this->content ); } // HTML minify if (defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_HTML_MIN)) { $this->content = $this->__optimizer->html_min($this->content); } } /** * Build a full JS tag * * @since 4.0 */ private function _build_js_tag($src) { if ($this->cfg_js_defer === 2 || Utility::str_hit_array($src, $this->cfg_js_delay_inc)) { return '<script data-optimized="1" type="litespeed/javascript" data-src="' . Str::trim_quotes($src) . '"></script>'; } if ($this->cfg_js_defer) { return '<script data-optimized="1" src="' . Str::trim_quotes($src) . '" defer></script>'; } return '<script data-optimized="1" src="' . Str::trim_quotes($src) . '"></script>'; } /** * Build a full inline JS snippet * * @since 4.0 */ private function _build_js_inline($script, $minified = false) { if ($this->cfg_js_defer) { $deferred = $this->_js_inline_defer($script, false, $minified); if ($deferred) { return $deferred; } } return '<script>' . $script . '</script>'; } /** * Load JS delay lib * * @since 4.0 */ private function _maybe_js_delay() { if ($this->cfg_js_defer !== 2 && !$this->cfg_js_delay_inc) { return; } $this->html_foot .= '<script>' . File::read(LSCWP_DIR . self::LIB_FILE_JS_DELAY) . '</script>'; } /** * Google font async * * @since 2.7.3 * @access private */ private function _async_ggfonts() { if (!$this->cfg_ggfonts_async || !$this->_ggfonts_urls) { return; } Debug2::debug2('[Optm] google fonts async found: ', $this->_ggfonts_urls); $html = '<link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin />'; /** * Append fonts * * Could be multiple fonts * * <link rel='stylesheet' href='//fonts.googleapis.com/css?family=Open+Sans%3A400%2C600%2C700%2C800%2C300&ver=4.9.8' type='text/css' media='all' /> * <link rel='stylesheet' href='//fonts.googleapis.com/css?family=PT+Sans%3A400%2C700%7CPT+Sans+Narrow%3A400%7CMontserrat%3A600&subset=latin&ver=4.9.8' type='text/css' media='all' /> * -> family: PT Sans:400,700|PT Sans Narrow:400|Montserrat:600 * <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,300,300italic,400italic,600,700,900&subset=latin%2Clatin-ext' /> */ $script = 'WebFontConfig={google:{families:['; $families = array(); foreach ($this->_ggfonts_urls as $v) { $qs = wp_specialchars_decode($v); $qs = urldecode($qs); $qs = parse_url($qs, PHP_URL_QUERY); parse_str($qs, $qs); if (empty($qs['family'])) { Debug2::debug('[Optm] ERR ggfonts failed to find family: ' . $v); continue; } $subset = empty($qs['subset']) ? '' : ':' . $qs['subset']; foreach (array_filter(explode('|', $qs['family'])) as $v2) { $families[] = Str::trim_quotes($v2 . $subset); } } $script .= '"' . implode('","', $families) . ($this->_conf_css_font_display ? '&display=swap' : '') . '"'; $script .= ']}};'; // if webfontloader lib was loaded before WebFontConfig variable, call WebFont.load $script .= 'if ( typeof WebFont === "object" && typeof WebFont.load === "function" ) { WebFont.load( WebFontConfig ); }'; $html .= $this->_build_js_inline($script); // https://cdnjs.cloudflare.com/ajax/libs/webfont/1.6.28/webfontloader.js $webfont_lib_url = LSWCP_PLUGIN_URL . self::LIB_FILE_WEBFONTLOADER; // default async, if js defer set use defer $html .= $this->_build_js_tag($webfont_lib_url); // Put this in the very beginning for preconnect $this->html_head = $html . $this->html_head; } /** * Font optm * * @since 3.0 * @access private */ private function _font_optm() { if (!$this->_conf_css_font_display || !$this->_ggfonts_urls) { return; } Debug2::debug2('[Optm] google fonts optm ', $this->_ggfonts_urls); foreach ($this->_ggfonts_urls as $v) { if (strpos($v, 'display=')) { continue; } $this->html_head = str_replace($v, $v . '&display=swap', $this->html_head); $this->html_foot = str_replace($v, $v . '&display=swap', $this->html_foot); $this->content = str_replace($v, $v . '&display=swap', $this->content); } } /** * Prefetch DNS * * @since 1.7.1 * @access private */ private function _dns_prefetch_init() { // Widely enable link DNS prefetch if (defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_DNS_PREFETCH_CTRL)) { @header('X-DNS-Prefetch-Control: on'); } $this->dns_prefetch = $this->conf(self::O_OPTM_DNS_PREFETCH); if (!$this->dns_prefetch) { return; } if (function_exists('wp_resource_hints')) { add_filter('wp_resource_hints', array($this, 'dns_prefetch_filter'), 10, 2); } else { add_action('litespeed_optm', array($this, 'dns_prefetch_output')); } } /** * Preconnect init * * @since 5.6.1 */ private function _dns_preconnect_init() { $this->dns_preconnect = $this->conf(self::O_OPTM_DNS_PRECONNECT); if ($this->dns_preconnect) { add_action('litespeed_optm', array($this, 'dns_preconnect_output')); } } /** * Prefetch DNS hook for WP * * @since 1.7.1 * @access public */ public function dns_prefetch_filter($urls, $relation_type) { if ($relation_type !== 'dns-prefetch') { return $urls; } foreach ($this->dns_prefetch as $v) { if ($v) { $urls[] = $v; } } return $urls; } /** * Prefetch DNS * * @since 1.7.1 * @access public */ public function dns_prefetch_output() { foreach ($this->dns_prefetch as $v) { if ($v) { $this->html_head .= '<link rel="dns-prefetch" href="' . Str::trim_quotes($v) . '" />'; } } } /** * Preconnect * * @since 5.6.1 * @access public */ public function dns_preconnect_output() { foreach ($this->dns_preconnect as $v) { if ($v) { $this->html_head .= '<link rel="preconnect" href="' . Str::trim_quotes($v) . '" />'; } } } /** * Run minify with src queue list * * @since 1.2.2 * @access private */ private function _src_queue_handler($src_list, $html_list, $file_type = 'css') { $html_list_ori = $html_list; $can_webp = $this->cls('Media')->webp_support(); $tag = $file_type == 'css' ? 'link' : 'script'; foreach ($src_list as $key => $src_info) { // Minify inline CSS/JS if (!empty($src_info['inl'])) { if ($file_type == 'css') { $code = Optimizer::minify_css($src_info['src']); $can_webp && ($code = $this->cls('Media')->replace_background_webp($code)); $snippet = str_replace($src_info['src'], $code, $html_list[$key]); } else { // Inline defer JS if ($this->cfg_js_defer) { $attrs = !empty($src_info['attrs']) ? $src_info['attrs'] : ''; $snippet = $this->_js_inline_defer($src_info['src'], $attrs) ?: $html_list[$key]; } else { $code = Optimizer::minify_js($src_info['src']); $snippet = str_replace($src_info['src'], $code, $html_list[$key]); } } } // CSS/JS files else { $url = $this->_build_single_hash_url($src_info['src'], $file_type); if ($url) { $snippet = str_replace($src_info['src'], $url, $html_list[$key]); } // Handle css async load if ($file_type == 'css' && $this->cfg_css_async) { $snippet = $this->_async_css($snippet); } // Handle js defer if ($file_type === 'js' && $this->cfg_js_defer) { $snippet = $this->_js_defer($snippet, $src_info['src']) ?: $snippet; } } $snippet = str_replace("<$tag ", '<' . $tag . ' data-optimized="1" ', $snippet); $html_list[$key] = $snippet; } $this->content = str_replace($html_list_ori, $html_list, $this->content); } /** * Build a single URL mapped filename (This will not save in DB) * @since 4.0 */ private function _build_single_hash_url($src, $file_type = 'css') { $content = $this->__optimizer->load_file($src, $file_type); $is_min = $this->__optimizer->is_min($src); $content = $this->__optimizer->optm_snippet($content, $file_type, !$is_min, $src); $filepath_prefix = $this->_build_filepath_prefix($file_type); // Save to file $filename = $filepath_prefix . md5($this->remove_query_strings($src)) . '.' . $file_type; $static_file = LITESPEED_STATIC_DIR . $filename; File::save($static_file, $content, true); // QS is required as $src may contains version info $qs_hash = substr(md5($src), -5); return LITESPEED_STATIC_URL . "$filename?ver=$qs_hash"; } /** * Generate full URL path with hash for a list of src * * @since 1.2.2 * @access private */ private function _build_hash_url($src_list, $file_type = 'css') { // $url_sensitive = $this->conf( self::O_OPTM_CSS_UNIQUE ) && $file_type == 'css'; // If need to keep unique CSS per URI // Replace preserved ESI (before generating hash) if ($file_type == 'js') { foreach ($src_list as $k => $v) { if (empty($v['inl'])) { continue; } $src_list[$k]['src'] = $this->_preserve_esi($v['src']); } } $minify = $file_type === 'css' ? $this->cfg_css_min : $this->cfg_js_min; $filename_info = $this->__optimizer->serve($this->_request_url, $file_type, $minify, $src_list); if (!$filename_info) { return false; // Failed to generate } list($filename, $type) = $filename_info; // Add cache tag in case later file deleted to avoid lscache served stale non-existed files @since 4.4.1 Tag::add(Tag::TYPE_MIN . '.' . $filename); $qs_hash = substr(md5(self::get_option(self::ITEM_TIMESTAMP_PURGE_CSS)), -5); // As filename is already related to filecon md5, no need QS anymore $filepath_prefix = $this->_build_filepath_prefix($type); return LITESPEED_STATIC_URL . $filepath_prefix . $filename . '?ver=' . $qs_hash; } /** * Parse js src * * @since 1.2.2 * @access private */ private function _parse_js() { $excludes = apply_filters('litespeed_optimize_js_excludes', $this->conf(self::O_OPTM_JS_EXC)); $combine_ext_inl = $this->conf(self::O_OPTM_JS_COMB_EXT_INL); if (!apply_filters('litespeed_optm_js_comb_ext_inl', true)) { Debug2::debug2('[Optm] js_comb_ext_inl bypassed via litespeed_optm_js_comb_ext_inl filter'); $combine_ext_inl = false; } $src_list = array(); $html_list = array(); // V7 added: (?:\r\n?|\n?) to fix replacement leaving empty new line $content = preg_replace('#<!--.*-->(?:\r\n?|\n?)#sU', '', $this->content); preg_match_all('#<script([^>]*)>(.*)</script>(?:\r\n?|\n?)#isU', $content, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $attrs = empty($match[1]) ? array() : Utility::parse_attr($match[1]); if (isset($attrs['data-optimized'])) { continue; } if (!empty($attrs['data-no-optimize'])) { continue; } if (!empty($attrs['data-cfasync']) && $attrs['data-cfasync'] === 'false') { continue; } if (!empty($attrs['type']) && $attrs['type'] != 'text/javascript') { continue; } // to avoid multiple replacement if (in_array($match[0], $html_list)) { continue; } $this_src_arr = array(); // JS files if (!empty($attrs['src'])) { // Exclude check $js_excluded = Utility::str_hit_array($attrs['src'], $excludes); $is_internal = Utility::is_internal_file($attrs['src']); $is_file = substr($attrs['src'], 0, 5) != 'data:'; $ext_excluded = !$combine_ext_inl && !$is_internal; if ($js_excluded || $ext_excluded || !$is_file) { // Maybe defer if ($this->cfg_js_defer) { $deferred = $this->_js_defer($match[0], $attrs['src']); if ($deferred) { $this->content = str_replace($match[0], $deferred, $this->content); } } Debug2::debug2('[Optm] _parse_js bypassed due to ' . ($js_excluded ? 'js files excluded [hit] ' . $js_excluded : 'external js')); continue; } if (strpos($attrs['src'], '/localres/') !== false) { continue; } if (strpos($attrs['src'], 'instant_click') !== false) { continue; } $this_src_arr['src'] = $attrs['src']; } // Inline JS elseif (!empty($match[2])) { // Debug2::debug( '🌹🌹🌹 ' . $match[2] . '🌹' ); // Exclude check $js_excluded = Utility::str_hit_array($match[2], $excludes); if ($js_excluded || !$combine_ext_inl) { // Maybe defer if ($this->cfg_js_defer) { $deferred = $this->_js_inline_defer($match[2], $match[1]); if ($deferred) { $this->content = str_replace($match[0], $deferred, $this->content); } } Debug2::debug2('[Optm] _parse_js bypassed due to ' . ($js_excluded ? 'js excluded [hit] ' . $js_excluded : 'inline js')); continue; } $this_src_arr['inl'] = true; $this_src_arr['src'] = $match[2]; if ($match[1]) { $this_src_arr['attrs'] = $match[1]; } } else { // Compatibility to those who changed src to data-src already Debug2::debug2('[Optm] No JS src or inline JS content'); continue; } $src_list[] = $this_src_arr; $html_list[] = $match[0]; } return array($src_list, $html_list); } /** * Inline JS defer * * @since 3.0 * @access private */ private function _js_inline_defer($con, $attrs = false, $minified = false) { if (strpos($attrs, 'data-no-defer') !== false) { Debug2::debug2('[Optm] bypass: attr api data-no-defer'); return false; } $hit = Utility::str_hit_array($con, $this->cfg_js_defer_exc); if ($hit) { Debug2::debug2('[Optm] inline js defer excluded [setting] ' . $hit); return false; } $con = trim($con); // Minify JS first if (!$minified) { // && $this->cfg_js_defer !== 2 $con = Optimizer::minify_js($con); } if (!$con) { return false; } // Check if the content contains ESI nonce or not $con = $this->_preserve_esi($con); if ($this->cfg_js_defer === 2) { // Drop type attribute from $attrs if (strpos($attrs, ' type=') !== false) { $attrs = preg_replace('# type=([\'"])([^\1]+)\1#isU', '', $attrs); } // Replace DOMContentLoaded $con = str_replace('DOMContentLoaded', 'DOMContentLiteSpeedLoaded', $con); return '<script' . $attrs . ' type="litespeed/javascript">' . $con . '</script>'; // return '<script' . $attrs . ' type="litespeed/javascript" src="data:text/javascript;base64,' . base64_encode( $con ) . '"></script>'; // return '<script' . $attrs . ' type="litespeed/javascript">' . $con . '</script>'; } return '<script' . $attrs . ' src="data:text/javascript;base64,' . base64_encode($con) . '" defer></script>'; } /** * Replace ESI to JS inline var (mainly used to avoid nonce timeout) * * @since 3.5.1 */ private function _preserve_esi($con) { $esi_placeholder_list = $this->cls('ESI')->contain_preserve_esi($con); if (!$esi_placeholder_list) { return $con; } foreach ($esi_placeholder_list as $esi_placeholder) { $js_var = '__litespeed_var_' . self::$_var_i++ . '__'; $con = str_replace($esi_placeholder, $js_var, $con); $this->_var_preserve_js[] = $js_var . '=' . $esi_placeholder; } return $con; } /** * Parse css src and remove to-be-removed css * * @since 1.2.2 * @access private * @return array All the src & related raw html list */ private function _parse_css() { $excludes = apply_filters('litespeed_optimize_css_excludes', $this->conf(self::O_OPTM_CSS_EXC)); $ucss_file_exc_inline = apply_filters('litespeed_optimize_ucss_file_exc_inline', $this->conf(self::O_OPTM_UCSS_FILE_EXC_INLINE)); $combine_ext_inl = $this->conf(self::O_OPTM_CSS_COMB_EXT_INL); if (!apply_filters('litespeed_optm_css_comb_ext_inl', true)) { Debug2::debug2('[Optm] css_comb_ext_inl bypassed via litespeed_optm_css_comb_ext_inl filter'); $combine_ext_inl = false; } $css_to_be_removed = apply_filters('litespeed_optm_css_to_be_removed', array()); $src_list = array(); $html_list = array(); // $dom = new \PHPHtmlParser\Dom; // $dom->load( $content );return $val; // $items = $dom->find( 'link' ); // V7 added: (?:\r\n?|\n?) to fix replacement leaving empty new line $content = preg_replace( array('#<!--.*-->(?:\r\n?|\n?)#sU', '#<script([^>]*)>.*</script>(?:\r\n?|\n?)#isU', '#<noscript([^>]*)>.*</noscript>(?:\r\n?|\n?)#isU'), '', $this->content ); preg_match_all('#<link ([^>]+)/?>|<style([^>]*)>([^<]+)</style>(?:\r\n?|\n?)#isU', $content, $matches, PREG_SET_ORDER); foreach ($matches as $match) { // to avoid multiple replacement if (in_array($match[0], $html_list)) { continue; } if ($exclude = Utility::str_hit_array($match[0], $excludes)) { Debug2::debug2('[Optm] _parse_css bypassed exclude ' . $exclude); continue; } $this_src_arr = array(); if (strpos($match[0], '<link') === 0) { $attrs = Utility::parse_attr($match[1]); if (empty($attrs['rel']) || $attrs['rel'] !== 'stylesheet') { continue; } if (empty($attrs['href'])) { continue; } // Check if need to remove this css if (Utility::str_hit_array($attrs['href'], $css_to_be_removed)) { Debug2::debug('[Optm] rm css snippet ' . $attrs['href']); // Delete this css snippet from orig html $this->content = str_replace($match[0], '', $this->content); continue; } // Check if need to inline this css file if ($this->conf(self::O_OPTM_UCSS) && Utility::str_hit_array($attrs['href'], $ucss_file_exc_inline)) { Debug2::debug('[Optm] ucss_file_exc_inline hit ' . $attrs['href']); // Replace this css to inline from orig html $inline_script = '<style>' . $this->__optimizer->load_file($attrs['href']) . '</style>'; $this->content = str_replace($match[0], $inline_script, $this->content); continue; } // Check Google fonts hit if (strpos($attrs['href'], 'fonts.googleapis.com') !== false) { /** * For async gg fonts, will add webfont into head, hence remove it from buffer and store the matches to use later * @since 2.7.3 * @since 3.0 For font display optm, need to parse google fonts URL too */ if (!in_array($attrs['href'], $this->_ggfonts_urls)) { $this->_ggfonts_urls[] = $attrs['href']; } if ($this->cfg_ggfonts_rm || $this->cfg_ggfonts_async) { Debug2::debug('[Optm] rm css snippet [Google fonts] ' . $attrs['href']); $this->content = str_replace($match[0], '', $this->content); continue; } } if (isset($attrs['data-optimized'])) { // $this_src_arr[ 'exc' ] = true; continue; } elseif (!empty($attrs['data-no-optimize'])) { // $this_src_arr[ 'exc' ] = true; continue; } $is_internal = Utility::is_internal_file($attrs['href']); $ext_excluded = !$combine_ext_inl && !$is_internal; if ($ext_excluded) { Debug2::debug2('[Optm] Bypassed due to external link'); // Maybe defer if ($this->cfg_css_async) { $snippet = $this->_async_css($match[0]); if ($snippet != $match[0]) { $this->content = str_replace($match[0], $snippet, $this->content); } } continue; } if (!empty($attrs['media']) && $attrs['media'] !== 'all') { $this_src_arr['media'] = $attrs['media']; } $this_src_arr['src'] = $attrs['href']; } else { // Inline style if (!$combine_ext_inl) { Debug2::debug2('[Optm] Bypassed due to inline'); continue; } $attrs = Utility::parse_attr($match[2]); if (!empty($attrs['data-no-optimize'])) { continue; } if (!empty($attrs['media']) && $attrs['media'] !== 'all') { $this_src_arr['media'] = $attrs['media']; } $this_src_arr['inl'] = true; $this_src_arr['src'] = $match[3]; } $src_list[] = $this_src_arr; $html_list[] = $match[0]; } return array($src_list, $html_list); } /** * Replace css to async loaded css * * @since 1.3 * @access private */ private function _async_css_list($html_list, $src_list) { foreach ($html_list as $k => $ori) { if (!empty($src_list[$k]['inl'])) { continue; } $html_list[$k] = $this->_async_css($ori); } return $html_list; } /** * Async CSS snippet * @since 3.5 */ private function _async_css($ori) { if (strpos($ori, 'data-asynced') !== false) { Debug2::debug2('[Optm] bypass: attr data-asynced exist'); return $ori; } if (strpos($ori, 'data-no-async') !== false) { Debug2::debug2('[Optm] bypass: attr api data-no-async'); return $ori; } // async replacement $v = str_replace('stylesheet', 'preload', $ori); $v = str_replace('<link', '<link data-asynced="1" as="style" onload="this.onload=null;this.rel=\'stylesheet\'" ', $v); // Append to noscript content if (!defined('LITESPEED_GUEST_OPTM') && !$this->conf(self::O_OPTM_NOSCRIPT_RM)) { $v .= '<noscript>' . preg_replace('/ id=\'[\w-]+\' /U', ' ', $ori) . '</noscript>'; } return $v; } /** * Defer JS snippet * * @since 3.5 */ private function _js_defer($ori, $src) { if (strpos($ori, ' async') !== false) { $ori = preg_replace('# async(?:=([\'"])(?:[^\1]+)\1)?#isU', '', $ori); } if (strpos($ori, 'defer') !== false) { return false; } if (strpos($ori, 'data-deferred') !== false) { Debug2::debug2('[Optm] bypass: attr data-deferred exist'); return false; } if (strpos($ori, 'data-no-defer') !== false) { Debug2::debug2('[Optm] bypass: attr api data-no-defer'); return false; } /** * Exclude JS from setting * @since 1.5 */ if (Utility::str_hit_array($src, $this->cfg_js_defer_exc)) { Debug2::debug('[Optm] js defer exclude ' . $src); return false; } if ($this->cfg_js_defer === 2 || Utility::str_hit_array($src, $this->cfg_js_delay_inc)) { if (strpos($ori, ' type=') !== false) { $ori = preg_replace('# type=([\'"])([^\1]+)\1#isU', '', $ori); } return str_replace(' src=', ' type="litespeed/javascript" data-src=', $ori); } return str_replace('></script>', ' defer data-deferred="1"></script>', $ori); } /** * Delay JS for included setting * * @since 5.6 */ private function _js_delay($ori, $src) { if (strpos($ori, ' async') !== false) { $ori = str_replace(' async', '', $ori); } if (strpos($ori, 'defer') !== false) { return false; } if (strpos($ori, 'data-deferred') !== false) { Debug2::debug2('[Optm] bypass: attr data-deferred exist'); return false; } if (strpos($ori, 'data-no-defer') !== false) { Debug2::debug2('[Optm] bypass: attr api data-no-defer'); return false; } if (!Utility::str_hit_array($src, $this->cfg_js_delay_inc)) { return; } if (strpos($ori, ' type=') !== false) { $ori = preg_replace('# type=([\'"])([^\1]+)\1#isU', '', $ori); } return str_replace(' src=', ' type="litespeed/javascript" data-src=', $ori); } } media.cls.php 0000644 00000101321 15162226224 0007114 0 ustar 00 <?php /** * The class to operate media data. * * @since 1.4 * @since 1.5 Moved into /inc * @package Core * @subpackage Core/inc * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Media extends Root { const LOG_TAG = '📺'; const LIB_FILE_IMG_LAZYLOAD = 'assets/js/lazyload.min.js'; private $content; private $_wp_upload_dir; private $_vpi_preload_list = array(); private $_format = ''; private $_sys_format = ''; /** * Init * * @since 1.4 */ public function __construct() { Debug2::debug2('[Media] init'); $this->_wp_upload_dir = wp_upload_dir(); if ($this->conf(Base::O_IMG_OPTM_WEBP)) { $this->_sys_format = 'webp'; $this->_format = 'webp'; if ($this->conf(Base::O_IMG_OPTM_WEBP) == 2) { $this->_sys_format = 'avif'; $this->_format = 'avif'; } if (!$this->_browser_support_next_gen()) { $this->_format = ''; } } } /** * Init optm features * * @since 3.0 * @access public */ public function init() { if (is_admin()) { return; } // Due to ajax call doesn't send correct accept header, have to limit webp to HTML only if ($this->webp_support()) { // Hook to srcset if (function_exists('wp_calculate_image_srcset')) { add_filter('wp_calculate_image_srcset', array($this, 'webp_srcset'), 988); } // Hook to mime icon // add_filter( 'wp_get_attachment_image_src', array( $this, 'webp_attach_img_src' ), 988 );// todo: need to check why not // add_filter( 'wp_get_attachment_url', array( $this, 'webp_url' ), 988 ); // disabled to avoid wp-admin display } if ($this->conf(Base::O_MEDIA_LAZY) && !$this->cls('Metabox')->setting('litespeed_no_image_lazy')) { self::debug('Suppress default WP lazyload'); add_filter('wp_lazy_loading_enabled', '__return_false'); } /** * Replace gravatar * @since 3.0 */ $this->cls('Avatar'); add_filter('litespeed_buffer_finalize', array($this, 'finalize'), 4); add_filter('litespeed_optm_html_head', array($this, 'finalize_head')); } /** * Add featured image to head */ public function finalize_head($content) { global $wp_query; // <link rel="preload" as="image" href="xx"> if ($this->_vpi_preload_list) { foreach ($this->_vpi_preload_list as $v) { $content .= '<link rel="preload" as="image" href="' . Str::trim_quotes($v) . '">'; } } // $featured_image_url = get_the_post_thumbnail_url(); // if ($featured_image_url) { // self::debug('Append featured image to head: ' . $featured_image_url); // if ($this->webp_support()) { // $featured_image_url = $this->replace_webp($featured_image_url) ?: $featured_image_url; // } // } // } return $content; } /** * Adjust WP default JPG quality * * @since 3.0 * @access public */ public function adjust_jpg_quality($quality) { $v = $this->conf(Base::O_IMG_OPTM_JPG_QUALITY); if ($v) { return $v; } return $quality; } /** * Register admin menu * * @since 1.6.3 * @access public */ public function after_admin_init() { /** * JPG quality control * @since 3.0 */ add_filter('jpeg_quality', array($this, 'adjust_jpg_quality')); add_filter('manage_media_columns', array($this, 'media_row_title')); add_filter('manage_media_custom_column', array($this, 'media_row_actions'), 10, 2); add_action('litespeed_media_row', array($this, 'media_row_con')); // Hook to attachment delete action add_action('delete_attachment', __CLASS__ . '::delete_attachment'); } /** * Media delete action hook * * @since 2.4.3 * @access public */ public static function delete_attachment($post_id) { // if (!Data::cls()->tb_exist('img_optm')) { // return; // } self::debug('delete_attachment [pid] ' . $post_id); Img_Optm::cls()->reset_row($post_id); } /** * Return media file info if exists * * This is for remote attachment plugins * * @since 2.9.8 * @access public */ public function info($short_file_path, $post_id) { $short_file_path = wp_normalize_path($short_file_path); $basedir = $this->_wp_upload_dir['basedir'] . '/'; if (strpos($short_file_path, $basedir) === 0) { $short_file_path = substr($short_file_path, strlen($basedir)); } $real_file = $basedir . $short_file_path; if (file_exists($real_file)) { return array( 'url' => $this->_wp_upload_dir['baseurl'] . '/' . $short_file_path, 'md5' => md5_file($real_file), 'size' => filesize($real_file), ); } /** * WP Stateless compatibility #143 https://github.com/litespeedtech/lscache_wp/issues/143 * @since 2.9.8 * @return array( 'url', 'md5', 'size' ) */ $info = apply_filters('litespeed_media_info', array(), $short_file_path, $post_id); if (!empty($info['url']) && !empty($info['md5']) && !empty($info['size'])) { return $info; } return false; } /** * Delete media file * * @since 2.9.8 * @access public */ public function del($short_file_path, $post_id) { $real_file = $this->_wp_upload_dir['basedir'] . '/' . $short_file_path; if (file_exists($real_file)) { unlink($real_file); self::debug('deleted ' . $real_file); } do_action('litespeed_media_del', $short_file_path, $post_id); } /** * Rename media file * * @since 2.9.8 * @access public */ public function rename($short_file_path, $short_file_path_new, $post_id) { // self::debug('renaming ' . $short_file_path . ' -> ' . $short_file_path_new); $real_file = $this->_wp_upload_dir['basedir'] . '/' . $short_file_path; $real_file_new = $this->_wp_upload_dir['basedir'] . '/' . $short_file_path_new; if (file_exists($real_file)) { rename($real_file, $real_file_new); self::debug('renamed ' . $real_file . ' to ' . $real_file_new); } do_action('litespeed_media_rename', $short_file_path, $short_file_path_new, $post_id); } /** * Media Admin Menu -> Image Optimization Column Title * * @since 1.6.3 * @access public */ public function media_row_title($posts_columns) { $posts_columns['imgoptm'] = __('LiteSpeed Optimization', 'litespeed-cache'); return $posts_columns; } /** * Media Admin Menu -> Image Optimization Column * * @since 1.6.2 * @access public */ public function media_row_actions($column_name, $post_id) { if ($column_name !== 'imgoptm') { return; } do_action('litespeed_media_row', $post_id); } /** * Display image optm info * * @since 3.0 */ public function media_row_con($post_id) { $att_info = wp_get_attachment_metadata($post_id); if (empty($att_info['file'])) { return; } $short_path = $att_info['file']; $size_meta = get_post_meta($post_id, Img_Optm::DB_SIZE, true); echo '<p>'; // Original image info if ($size_meta && !empty($size_meta['ori_saved'])) { $percent = ceil(($size_meta['ori_saved'] * 100) / $size_meta['ori_total']); $extension = pathinfo($short_path, PATHINFO_EXTENSION); $bk_file = substr($short_path, 0, -strlen($extension)) . 'bk.' . $extension; $bk_optm_file = substr($short_path, 0, -strlen($extension)) . 'bk.optm.' . $extension; $link = Utility::build_url(Router::ACTION_IMG_OPTM, 'orig' . $post_id); $desc = false; $cls = ''; if ($this->info($bk_file, $post_id)) { $curr_status = __('(optm)', 'litespeed-cache'); $desc = __('Currently using optimized version of file.', 'litespeed-cache') . ' ' . __('Click to switch to original (unoptimized) version.', 'litespeed-cache'); } elseif ($this->info($bk_optm_file, $post_id)) { $cls .= ' litespeed-warning'; $curr_status = __('(non-optm)', 'litespeed-cache'); $desc = __('Currently using original (unoptimized) version of file.', 'litespeed-cache') . ' ' . __('Click to switch to optimized version.', 'litespeed-cache'); } echo GUI::pie_tiny( $percent, 24, sprintf(__('Original file reduced by %1$s (%2$s)', 'litespeed-cache'), $percent . '%', Utility::real_size($size_meta['ori_saved'])), 'left' ); echo sprintf(__('Orig saved %s', 'litespeed-cache'), $percent . '%'); if ($desc) { echo sprintf( ' <a href="%1$s" class="litespeed-media-href %2$s" data-balloon-pos="left" data-balloon-break aria-label="%3$s">%4$s</a>', $link, $cls, $desc, $curr_status ); } else { echo sprintf( ' <span class="litespeed-desc" data-balloon-pos="left" data-balloon-break aria-label="%1$s">%2$s</span>', __('Using optimized version of file. ', 'litespeed-cache') . ' ' . __('No backup of original file exists.', 'litespeed-cache'), __('(optm)', 'litespeed-cache') ); } } elseif ($size_meta && $size_meta['ori_saved'] === 0) { echo GUI::pie_tiny(0, 24, __('Congratulation! Your file was already optimized', 'litespeed-cache'), 'left'); echo sprintf(__('Orig %s', 'litespeed-cache'), '<span class="litespeed-desc">' . __('(no savings)', 'litespeed-cache') . '</span>'); } else { echo __('Orig', 'litespeed-cache') . '<span class="litespeed-left10">—</span>'; } echo '</p>'; echo '<p>'; // WebP/AVIF info if ($size_meta && $this->webp_support(true) && !empty($size_meta[$this->_sys_format . '_saved'])) { $is_avif = 'avif' === $this->_sys_format; $size_meta_saved = $size_meta[$this->_sys_format . '_saved']; $size_meta_total = $size_meta[$this->_sys_format . '_total']; $percent = ceil(($size_meta_saved * 100) / $size_meta_total); $link = Utility::build_url(Router::ACTION_IMG_OPTM, $this->_sys_format . $post_id); $desc = false; $cls = ''; if ($this->info($short_path . '.' . $this->_sys_format, $post_id)) { $curr_status = __('(optm)', 'litespeed-cache'); $desc = $is_avif ? __('Currently using optimized version of AVIF file.', 'litespeed-cache') : __('Currently using optimized version of WebP file.', 'litespeed-cache'); $desc .= ' ' . __('Click to switch to original (unoptimized) version.', 'litespeed-cache'); } elseif ($this->info($short_path . '.optm.' . $this->_sys_format, $post_id)) { $cls .= ' litespeed-warning'; $curr_status = __('(non-optm)', 'litespeed-cache'); $desc = $is_avif ? __('Currently using original (unoptimized) version of AVIF file.', 'litespeed-cache') : __('Currently using original (unoptimized) version of WebP file.', 'litespeed-cache'); $desc .= ' ' . __('Click to switch to optimized version.', 'litespeed-cache'); } echo GUI::pie_tiny( $percent, 24, sprintf( $is_avif ? __('AVIF file reduced by %1$s (%2$s)', 'litespeed-cache') : __('WebP file reduced by %1$s (%2$s)', 'litespeed-cache'), $percent . '%', Utility::real_size($size_meta_saved) ), 'left' ); echo sprintf($is_avif ? __('AVIF saved %s', 'litespeed-cache') : __('WebP saved %s', 'litespeed-cache'), $percent . '%'); if ($desc) { echo sprintf( ' <a href="%1$s" class="litespeed-media-href %2$s" data-balloon-pos="left" data-balloon-break aria-label="%3$s">%4$s</a>', $link, $cls, $desc, $curr_status ); } else { echo sprintf( ' <span class="litespeed-desc" data-balloon-pos="left" data-balloon-break aria-label="%1$s %2$s">%3$s</span>', __('Using optimized version of file. ', 'litespeed-cache'), $is_avif ? __('No backup of unoptimized AVIF file exists.', 'litespeed-cache') : __('No backup of unoptimized WebP file exists.', 'litespeed-cache'), __('(optm)', 'litespeed-cache') ); } } else { echo $this->next_gen_image_title() . '<span class="litespeed-left10">—</span>'; } echo '</p>'; // Delete row btn if ($size_meta) { echo sprintf( '<div class="row-actions"><span class="delete"><a href="%1$s" class="">%2$s</a></span></div>', Utility::build_url(Router::ACTION_IMG_OPTM, Img_Optm::TYPE_RESET_ROW, false, null, array('id' => $post_id)), __('Restore from backup', 'litespeed-cache') ); echo '</div>'; } } /** * Get wp size info * * NOTE: this is not used because it has to be after admin_init * * @since 1.6.2 * @return array $sizes Data for all currently-registered image sizes. */ public function get_image_sizes() { global $_wp_additional_image_sizes; $sizes = array(); foreach (get_intermediate_image_sizes() as $_size) { if (in_array($_size, array('thumbnail', 'medium', 'medium_large', 'large'))) { $sizes[$_size]['width'] = get_option($_size . '_size_w'); $sizes[$_size]['height'] = get_option($_size . '_size_h'); $sizes[$_size]['crop'] = (bool) get_option($_size . '_crop'); } elseif (isset($_wp_additional_image_sizes[$_size])) { $sizes[$_size] = array( 'width' => $_wp_additional_image_sizes[$_size]['width'], 'height' => $_wp_additional_image_sizes[$_size]['height'], 'crop' => $_wp_additional_image_sizes[$_size]['crop'], ); } } return $sizes; } /** * Exclude role from optimization filter * * @since 1.6.2 * @access public */ public function webp_support($sys_level = false) { if ($sys_level) { return $this->_sys_format; } return $this->_format; // User level next gen support } private function _browser_support_next_gen() { if (!empty($_SERVER['HTTP_ACCEPT'])) { if (strpos($_SERVER['HTTP_ACCEPT'], 'image/' . $this->_sys_format) !== false) { return true; } } if (!empty($_SERVER['HTTP_USER_AGENT'])) { $user_agents = array('chrome-lighthouse', 'googlebot', 'page speed'); foreach ($user_agents as $user_agent) { if (stripos($_SERVER['HTTP_USER_AGENT'], $user_agent) !== false) { return true; } } if (preg_match('/iPhone OS (\d+)_/i', $_SERVER['HTTP_USER_AGENT'], $matches)) { if ($matches[1] >= 14) { return true; } } if (preg_match('/Firefox\/(\d+)/i', $_SERVER['HTTP_USER_AGENT'], $matches)) { if ($matches[1] >= 65) { return true; } } } return false; } /** * Get next gen image title * * @since 7.0 */ public function next_gen_image_title() { $next_gen_img = 'WebP'; if ($this->conf(Base::O_IMG_OPTM_WEBP) == 2) { $next_gen_img = 'AVIF'; } return $next_gen_img; } /** * Run lazy load process * NOTE: As this is after cache finalized, can NOT set any cache control anymore * * Only do for main page. Do NOT do for esi or dynamic content. * * @since 1.4 * @access public * @return string The buffer */ public function finalize($content) { if (defined('LITESPEED_NO_LAZY')) { Debug2::debug2('[Media] bypass: NO_LAZY const'); return $content; } if (!defined('LITESPEED_IS_HTML')) { Debug2::debug2('[Media] bypass: Not frontend HTML type'); return $content; } if (!Control::is_cacheable()) { self::debug('bypass: Not cacheable'); return $content; } self::debug('finalize'); $this->content = $content; $this->_finalize(); return $this->content; } /** * Run lazyload replacement for images in buffer * * @since 1.4 * @access private */ private function _finalize() { /** * Use webp for optimized images * @since 1.6.2 */ if ($this->webp_support()) { $this->content = $this->_replace_buffer_img_webp($this->content); } /** * Check if URI is excluded * @since 3.0 */ $excludes = $this->conf(Base::O_MEDIA_LAZY_URI_EXC); if (!defined('LITESPEED_GUEST_OPTM')) { $result = Utility::str_hit_array($_SERVER['REQUEST_URI'], $excludes); if ($result) { self::debug('bypass lazyload: hit URI Excludes setting: ' . $result); return; } } $cfg_lazy = (defined('LITESPEED_GUEST_OPTM') || $this->conf(Base::O_MEDIA_LAZY)) && !$this->cls('Metabox')->setting('litespeed_no_image_lazy'); $cfg_iframe_lazy = defined('LITESPEED_GUEST_OPTM') || $this->conf(Base::O_MEDIA_IFRAME_LAZY); $cfg_js_delay = defined('LITESPEED_GUEST_OPTM') || $this->conf(Base::O_OPTM_JS_DEFER) == 2; $cfg_trim_noscript = defined('LITESPEED_GUEST_OPTM') || $this->conf(Base::O_OPTM_NOSCRIPT_RM); $cfg_vpi = defined('LITESPEED_GUEST_OPTM') || $this->conf(Base::O_MEDIA_VPI); // Preload VPI if ($cfg_vpi) { $this->_parse_img_for_preload(); } if ($cfg_lazy) { if ($cfg_vpi) { add_filter('litespeed_media_lazy_img_excludes', array($this->cls('Metabox'), 'lazy_img_excludes')); } list($src_list, $html_list, $placeholder_list) = $this->_parse_img(); $html_list_ori = $html_list; } else { self::debug('lazyload disabled'); } // image lazy load if ($cfg_lazy) { $__placeholder = Placeholder::cls(); foreach ($html_list as $k => $v) { $size = $placeholder_list[$k]; $src = $src_list[$k]; $html_list[$k] = $__placeholder->replace($v, $src, $size); } } if ($cfg_lazy) { $this->content = str_replace($html_list_ori, $html_list, $this->content); } // iframe lazy load if ($cfg_iframe_lazy) { $html_list = $this->_parse_iframe(); $html_list_ori = $html_list; foreach ($html_list as $k => $v) { $snippet = $cfg_trim_noscript ? '' : '<noscript>' . $v . '</noscript>'; if ($cfg_js_delay) { $v = str_replace(' src=', ' data-litespeed-src=', $v); } else { $v = str_replace(' src=', ' data-src=', $v); } $v = str_replace('<iframe ', '<iframe data-lazyloaded="1" src="about:blank" ', $v); $snippet = $v . $snippet; $html_list[$k] = $snippet; } $this->content = str_replace($html_list_ori, $html_list, $this->content); } // Include lazyload lib js and init lazyload if ($cfg_lazy || $cfg_iframe_lazy) { $lazy_lib = '<script data-no-optimize="1">' . File::read(LSCWP_DIR . self::LIB_FILE_IMG_LAZYLOAD) . '</script>'; $this->content = str_replace('</body>', $lazy_lib . '</body>', $this->content); } } /** * Parse img src for VPI preload only * Note: Didn't reuse the _parse_img() bcoz it contains parent cls replacement and other logic which is not needed for preload * * @since 6.2 */ private function _parse_img_for_preload() { // Load VPI setting $is_mobile = $this->_separate_mobile(); $vpi_files = $this->cls('Metabox')->setting($is_mobile ? 'litespeed_vpi_list_mobile' : 'litespeed_vpi_list'); if ($vpi_files) { $vpi_files = Utility::sanitize_lines($vpi_files, 'basename'); } if (!$vpi_files) { return; } if (!$this->content) { return; } $content = preg_replace(array('#<!--.*-->#sU', '#<noscript([^>]*)>.*</noscript>#isU'), '', $this->content); if (!$content) { return; } preg_match_all('#<img\s+([^>]+)/?>#isU', $content, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $attrs = Utility::parse_attr($match[1]); if (empty($attrs['src'])) { continue; } if (strpos($attrs['src'], 'base64') !== false || substr($attrs['src'], 0, 5) === 'data:') { Debug2::debug2('[Media] lazyload bypassed base64 img'); continue; } if (strpos($attrs['src'], '{') !== false) { Debug2::debug2('[Media] image src has {} ' . $attrs['src']); continue; } // If the src contains VPI filename, then preload it if (!Utility::str_hit_array($attrs['src'], $vpi_files)) { continue; } Debug2::debug2('[Media] VPI preload found and matched: ' . $attrs['src']); $this->_vpi_preload_list[] = $attrs['src']; } } /** * Parse img src * * @since 1.4 * @access private * @return array All the src & related raw html list */ private function _parse_img() { /** * Exclude list * @since 1.5 * @since 2.7.1 Changed to array */ $excludes = apply_filters('litespeed_media_lazy_img_excludes', $this->conf(Base::O_MEDIA_LAZY_EXC)); $cls_excludes = apply_filters('litespeed_media_lazy_img_cls_excludes', $this->conf(Base::O_MEDIA_LAZY_CLS_EXC)); $cls_excludes[] = 'skip-lazy'; // https://core.trac.wordpress.org/ticket/44427 $src_list = array(); $html_list = array(); $placeholder_list = array(); $content = preg_replace( array( '#<!--.*-->#sU', '#<noscript([^>]*)>.*</noscript>#isU', '#<script([^>]*)>.*</script>#isU', // Added to remove warning of file not found when image size detection is turned ON. ), '', $this->content ); /** * Exclude parent classes * @since 3.0 */ $parent_cls_exc = apply_filters('litespeed_media_lazy_img_parent_cls_excludes', $this->conf(Base::O_MEDIA_LAZY_PARENT_CLS_EXC)); if ($parent_cls_exc) { Debug2::debug2('[Media] Lazyload Class excludes', $parent_cls_exc); foreach ($parent_cls_exc as $v) { $content = preg_replace('#<(\w+) [^>]*class=(\'|")[^\'"]*' . preg_quote($v, '#') . '[^\'"]*\2[^>]*>.*</\1>#sU', '', $content); } } preg_match_all('#<img\s+([^>]+)/?>#isU', $content, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $attrs = Utility::parse_attr($match[1]); if (empty($attrs['src'])) { continue; } /** * Add src validation to bypass base64 img src * @since 1.6 */ if (strpos($attrs['src'], 'base64') !== false || substr($attrs['src'], 0, 5) === 'data:') { Debug2::debug2('[Media] lazyload bypassed base64 img'); continue; } Debug2::debug2('[Media] lazyload found: ' . $attrs['src']); if ( !empty($attrs['data-no-lazy']) || !empty($attrs['data-skip-lazy']) || !empty($attrs['data-lazyloaded']) || !empty($attrs['data-src']) || !empty($attrs['data-srcset']) ) { Debug2::debug2('[Media] bypassed'); continue; } if (!empty($attrs['class']) && ($hit = Utility::str_hit_array($attrs['class'], $cls_excludes))) { Debug2::debug2('[Media] lazyload image cls excludes [hit] ' . $hit); continue; } /** * Exclude from lazyload by setting * @since 1.5 */ if ($excludes && Utility::str_hit_array($attrs['src'], $excludes)) { Debug2::debug2('[Media] lazyload image exclude ' . $attrs['src']); continue; } /** * Excldues invalid image src from buddypress avatar crop * @see https://wordpress.org/support/topic/lazy-load-breaking-buddypress-upload-avatar-feature * @since 3.0 */ if (strpos($attrs['src'], '{') !== false) { Debug2::debug2('[Media] image src has {} ' . $attrs['src']); continue; } // to avoid multiple replacement if (in_array($match[0], $html_list)) { continue; } // Add missing dimensions if (defined('LITESPEED_GUEST_OPTM') || $this->conf(Base::O_MEDIA_ADD_MISSING_SIZES)) { if (!apply_filters('litespeed_media_add_missing_sizes', true)) { Debug2::debug2('[Media] add_missing_sizes bypassed via litespeed_media_add_missing_sizes filter'); } elseif (empty($attrs['width']) || $attrs['width'] == 'auto' || empty($attrs['height']) || $attrs['height'] == 'auto') { self::debug('⚠️ Missing sizes for image [src] ' . $attrs['src']); $dimensions = $this->_detect_dimensions($attrs['src']); if ($dimensions) { $ori_width = $dimensions[0]; $ori_height = $dimensions[1]; // Calculate height based on width if (!empty($attrs['width']) && $attrs['width'] != 'auto') { $ori_height = intval(($ori_height * $attrs['width']) / $ori_width); } elseif (!empty($attrs['height']) && $attrs['height'] != 'auto') { $ori_width = intval(($ori_width * $attrs['height']) / $ori_height); } $attrs['width'] = $ori_width; $attrs['height'] = $ori_height; $new_html = preg_replace('#\s+(width|height)=(["\'])[^\2]*?\2#', '', $match[0]); $new_html = preg_replace( '#<img\s+#i', '<img width="' . Str::trim_quotes($attrs['width']) . '" height="' . Str::trim_quotes($attrs['height']) . '" ', $new_html ); self::debug('Add missing sizes ' . $attrs['width'] . 'x' . $attrs['height'] . ' to ' . $attrs['src']); $this->content = str_replace($match[0], $new_html, $this->content); $match[0] = $new_html; } } } $placeholder = false; if (!empty($attrs['width']) && $attrs['width'] != 'auto' && !empty($attrs['height']) && $attrs['height'] != 'auto') { $placeholder = intval($attrs['width']) . 'x' . intval($attrs['height']); } $src_list[] = $attrs['src']; $html_list[] = $match[0]; $placeholder_list[] = $placeholder; } return array($src_list, $html_list, $placeholder_list); } /** * Detect the original sizes * * @since 4.0 */ private function _detect_dimensions($src) { if ($pathinfo = Utility::is_internal_file($src)) { $src = $pathinfo[0]; } elseif (apply_filters('litespeed_media_ignore_remote_missing_sizes', false)) { return false; } if (substr($src, 0, 2) == '//') { $src = 'https:' . $src; } try { $sizes = getimagesize($src); } catch (\Exception $e) { return false; } if (!empty($sizes[0]) && !empty($sizes[1])) { return $sizes; } return false; } /** * Parse iframe src * * @since 1.4 * @access private * @return array All the src & related raw html list */ private function _parse_iframe() { $cls_excludes = apply_filters('litespeed_media_iframe_lazy_cls_excludes', $this->conf(Base::O_MEDIA_IFRAME_LAZY_CLS_EXC)); $cls_excludes[] = 'skip-lazy'; // https://core.trac.wordpress.org/ticket/44427 $html_list = array(); $content = preg_replace('#<!--.*-->#sU', '', $this->content); /** * Exclude parent classes * @since 3.0 */ $parent_cls_exc = apply_filters('litespeed_media_iframe_lazy_parent_cls_excludes', $this->conf(Base::O_MEDIA_IFRAME_LAZY_PARENT_CLS_EXC)); if ($parent_cls_exc) { Debug2::debug2('[Media] Iframe Lazyload Class excludes', $parent_cls_exc); foreach ($parent_cls_exc as $v) { $content = preg_replace('#<(\w+) [^>]*class=(\'|")[^\'"]*' . preg_quote($v, '#') . '[^\'"]*\2[^>]*>.*</\1>#sU', '', $content); } } preg_match_all('#<iframe \s*([^>]+)></iframe>#isU', $content, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $attrs = Utility::parse_attr($match[1]); if (empty($attrs['src'])) { continue; } Debug2::debug2('[Media] found iframe: ' . $attrs['src']); if (!empty($attrs['data-no-lazy']) || !empty($attrs['data-skip-lazy']) || !empty($attrs['data-lazyloaded']) || !empty($attrs['data-src'])) { Debug2::debug2('[Media] bypassed'); continue; } if (!empty($attrs['class']) && ($hit = Utility::str_hit_array($attrs['class'], $cls_excludes))) { Debug2::debug2('[Media] iframe lazyload cls excludes [hit] ' . $hit); continue; } if (apply_filters('litespeed_iframe_lazyload_exc', false, $attrs['src'])) { Debug2::debug2('[Media] bypassed by filter'); continue; } // to avoid multiple replacement if (in_array($match[0], $html_list)) { continue; } $html_list[] = $match[0]; } return $html_list; } /** * Replace image src to webp * * @since 1.6.2 * @access private */ private function _replace_buffer_img_webp($content) { /** * Added custom element & attribute support * @since 2.2.2 */ $webp_ele_to_check = $this->conf(Base::O_IMG_OPTM_WEBP_ATTR); foreach ($webp_ele_to_check as $v) { if (!$v || strpos($v, '.') === false) { Debug2::debug2('[Media] buffer_webp no . attribute ' . $v); continue; } Debug2::debug2('[Media] buffer_webp attribute ' . $v); $v = explode('.', $v); $attr = preg_quote($v[1], '#'); if ($v[0]) { $pattern = '#<' . preg_quote($v[0], '#') . '([^>]+)' . $attr . '=([\'"])(.+)\2#iU'; } else { $pattern = '# ' . $attr . '=([\'"])(.+)\1#iU'; } preg_match_all($pattern, $content, $matches); foreach ($matches[$v[0] ? 3 : 2] as $k2 => $url) { // Check if is a DATA-URI if (strpos($url, 'data:image') !== false) { continue; } if (!($url2 = $this->replace_webp($url))) { continue; } if ($v[0]) { $html_snippet = sprintf('<' . $v[0] . '%1$s' . $v[1] . '=%2$s', $matches[1][$k2], $matches[2][$k2] . $url2 . $matches[2][$k2]); } else { $html_snippet = sprintf(' ' . $v[1] . '=%1$s', $matches[1][$k2] . $url2 . $matches[1][$k2]); } $content = str_replace($matches[0][$k2], $html_snippet, $content); } } // parse srcset // todo: should apply this to cdn too if ((defined('LITESPEED_GUEST_OPTM') || $this->conf(Base::O_IMG_OPTM_WEBP_REPLACE_SRCSET)) && $this->webp_support()) { $content = Utility::srcset_replace($content, array($this, 'replace_webp')); } // Replace background-image if ((defined('LITESPEED_GUEST_OPTM') || $this->conf(Base::O_IMG_OPTM_WEBP)) && $this->webp_support()) { $content = $this->replace_background_webp($content); } return $content; } /** * Replace background image * * @since 4.0 */ public function replace_background_webp($content) { Debug2::debug2('[Media] Start replacing background WebP/AVIF.'); // Handle Elementors data-settings json encode background-images $content = $this->replace_urls_in_json($content); // preg_match_all( '#background-image:(\s*)url\((.*)\)#iU', $content, $matches ); preg_match_all('#url\(([^)]+)\)#iU', $content, $matches); foreach ($matches[1] as $k => $url) { // Check if is a DATA-URI if (strpos($url, 'data:image') !== false) { continue; } /** * Support quotes in src `background-image: url('src')` * @since 2.9.3 */ $url = trim($url, '\'"'); // Fix Elementors Slideshow unusual background images like style="background-image: url("https://xxxx.png");" if (strpos($url, '"') === 0 && substr($url, -6) == '"') { $url = substr($url, 6, -6); } if (!($url2 = $this->replace_webp($url))) { continue; } // $html_snippet = sprintf( 'background-image:%1$surl(%2$s)', $matches[ 1 ][ $k ], $url2 ); $html_snippet = str_replace($url, $url2, $matches[0][$k]); $content = str_replace($matches[0][$k], $html_snippet, $content); } return $content; } /** * Replace images in json data settings attributes * * @since 6.2 */ public function replace_urls_in_json($content) { $pattern = '/data-settings="(.*?)"/i'; $parent_class = $this; preg_match_all($pattern, $content, $matches, PREG_SET_ORDER); foreach ($matches as $match) { // Check if the string contains HTML entities $isEncoded = preg_match('/"|<|>|&|'/', $match[1]); // Decode HTML entities in the JSON string $jsonString = html_entity_decode($match[1]); $jsonData = \json_decode($jsonString, true); if (json_last_error() === JSON_ERROR_NONE) { $did_webp_replace = false; array_walk_recursive($jsonData, function (&$item, $key) use (&$did_webp_replace, $parent_class) { if ($key == 'url') { $item_image = $parent_class->replace_webp($item); if ($item_image) { $item = $item_image; $did_webp_replace = true; } } }); if ($did_webp_replace) { // Re-encode the modified array back to a JSON string $newJsonString = \json_encode($jsonData); // Re-encode the JSON string to HTML entities only if it was originally encoded if ($isEncoded) { $newJsonString = htmlspecialchars($newJsonString, ENT_QUOTES | 0); // ENT_HTML401 is for PHPv5.4+ } // Replace the old JSON string in the content with the new, modified JSON string $content = str_replace($match[1], $newJsonString, $content); } } } return $content; } /** * Replace internal image src to webp or avif * * @since 1.6.2 * @access public */ public function replace_webp($url) { if (!$this->webp_support()) { self::debug2('No next generation format chosen in setting, bypassed'); return false; } Debug2::debug2('[Media] ' . $this->_sys_format . ' replacing: ' . substr($url, 0, 200)); if (substr($url, -5) === '.' . $this->_sys_format) { Debug2::debug2('[Media] already ' . $this->_sys_format); return false; } /** * WebP API hook * NOTE: As $url may contain query strings, WebP check will need to parse_url before appending .webp * @since 2.9.5 * @see #751737 - API docs for WebP generation */ if (apply_filters('litespeed_media_check_ori', Utility::is_internal_file($url), $url)) { // check if has webp file if (apply_filters('litespeed_media_check_webp', Utility::is_internal_file($url, $this->_sys_format), $url)) { $url .= '.' . $this->_sys_format; } else { Debug2::debug2('[Media] -no WebP or AVIF file, bypassed'); return false; } } else { Debug2::debug2('[Media] -no file, bypassed'); return false; } Debug2::debug2('[Media] - replaced to: ' . $url); return $url; } /** * Hook to wp_get_attachment_image_src * * @since 1.6.2 * @access public * @param array $img The URL of the attachment image src, the width, the height * @return array */ public function webp_attach_img_src($img) { Debug2::debug2('[Media] changing attach src: ' . $img[0]); if ($img && ($url = $this->replace_webp($img[0]))) { $img[0] = $url; } return $img; } /** * Try to replace img url * * @since 1.6.2 * @access public * @param string $url * @return string */ public function webp_url($url) { if ($url && ($url2 = $this->replace_webp($url))) { $url = $url2; } return $url; } /** * Hook to replace WP responsive images * * @since 1.6.2 * @access public * @param array $srcs * @return array */ public function webp_srcset($srcs) { if ($srcs) { foreach ($srcs as $w => $data) { if (!($url = $this->replace_webp($data['url']))) { continue; } $srcs[$w]['url'] = $url; } } return $srcs; } } doc.cls.php 0000644 00000011353 15162226225 0006610 0 ustar 00 <?php /** * The Doc class. * * @since 2.2.7 * @package LiteSpeed * @subpackage LiteSpeed/src * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Doc { // protected static $_instance; /** * Show option is actually ON by GM * * @since 5.5 * @access public */ public static function maybe_on_by_gm($id) { if (apply_filters('litespeed_conf', $id)) { return; } if (!apply_filters('litespeed_conf', Base::O_GUEST)) { return; } if (!apply_filters('litespeed_conf', Base::O_GUEST_OPTM)) { return; } echo '<font class="litespeed-warning">'; echo '⚠️ ' . sprintf( __('This setting is %1$s for certain qualifying requests due to %2$s!', 'litespeed-cache'), '<code>' . __('ON', 'litespeed-cache') . '</code>', Lang::title(Base::O_GUEST_OPTM) ); self::learn_more('https://docs.litespeedtech.com/lscache/lscwp/general/#guest-optimization'); echo '</font>'; } /** * Changes affect crawler list warning * * @since 4.3 * @access public */ public static function crawler_affected() { echo '<font class="litespeed-primary">'; echo '⚠️ ' . __('This setting will regenerate crawler list and clear the disabled list!', 'litespeed-cache'); echo '</font>'; } /** * Privacy policy * * @since 2.2.7 * @access public */ public static function privacy_policy() { return __( 'This site utilizes caching in order to facilitate a faster response time and better user experience. Caching potentially stores a duplicate copy of every web page that is on display on this site. All cache files are temporary, and are never accessed by any third party, except as necessary to obtain technical support from the cache plugin vendor. Cache files expire on a schedule set by the site administrator, but may easily be purged by the admin before their natural expiration, if necessary. We may use QUIC.cloud services to process & cache your data temporarily.', 'litespeed-cache' ) . sprintf( __('Please see %s for more details.', 'litespeed-cache'), '<a href="https://quic.cloud/privacy-policy/" target="_blank">https://quic.cloud/privacy-policy/</a>' ); } /** * Learn more link * * @since 2.4.2 * @access public */ public static function learn_more($url, $title = false, $self = false, $class = false, $return = false) { if (!$class) { $class = 'litespeed-learn-more'; } if (!$title) { $title = __('Learn More', 'litespeed-cache'); } $self = $self ? '' : "target='_blank'"; $txt = " <a href='$url' $self class='$class'>$title</a>"; if ($return) { return $txt; } echo $txt; } /** * One per line * * @since 3.0 * @access public */ public static function one_per_line($return = false) { $str = __('One per line.', 'litespeed-cache'); if ($return) { return $str; } echo $str; } /** * One per line * * @since 3.4 * @access public */ public static function full_or_partial_url($string_only = false) { if ($string_only) { echo __('Both full and partial strings can be used.', 'litespeed-cache'); } else { echo __('Both full URLs and partial strings can be used.', 'litespeed-cache'); } } /** * Notice to edit .htaccess * * @since 3.0 * @access public */ public static function notice_htaccess() { echo '<font class="litespeed-primary">'; echo '⚠️ ' . __('This setting will edit the .htaccess file.', 'litespeed-cache'); echo ' <a href="https://docs.litespeedtech.com/lscache/lscwp/toolbox/#edit-htaccess-tab" target="_blank" class="litespeed-learn-more">' . __('Learn More', 'litespeed-cache') . '</a>'; echo '</font>'; } /** * Notice for whitelist IPs * * @since 3.0 * @access public */ public static function notice_ips() { echo '<div class="litespeed-primary">'; echo '⚠️ ' . sprintf(__('For online services to work correctly, you must allowlist all %s server IPs.', 'litespeed-cache'), 'QUIC.cloud') . '<br/>'; echo ' ' . __('Before generating key, please verify all IPs on this list are allowlisted', 'litespeed-cache') . ': '; echo '<a href="' . Cloud::CLOUD_IPS . '" target="_blank">' . __('Current Online Server IPs', 'litespeed-cache') . '</a>'; echo '</div>'; } /** * Gentle reminder that web services run asynchronously * * @since 5.3.1 * @access public */ public static function queue_issues($return = false) { $str = '<div class="litespeed-desc">' . __('The queue is processed asynchronously. It may take time.', 'litespeed-cache') . self::learn_more('https://docs.litespeedtech.com/lscache/lscwp/troubleshoot/#quiccloud-queue-issues', false, false, false, true) . '</div>'; if ($return) { return $str; } echo $str; } } import.preset.cls.php 0000644 00000012670 15162226226 0010662 0 ustar 00 <?php /** * The preset class. * * @since 5.3.0 */ namespace LiteSpeed; defined('WPINC') || exit(); class Preset extends Import { protected $_summary; const MAX_BACKUPS = 10; const TYPE_APPLY = 'apply'; const TYPE_RESTORE = 'restore'; const STANDARD_DIR = LSCWP_DIR . 'data/preset'; const BACKUP_DIR = LITESPEED_STATIC_DIR . '/auto-backup'; /** * Returns sorted backup names * * @since 5.3.0 * @access public */ public static function get_backups() { self::init_filesystem(); global $wp_filesystem; $backups = array_map( function ($path) { return self::basename($path['name']); }, $wp_filesystem->dirlist(self::BACKUP_DIR) ?: array() ); rsort($backups); return $backups; } /** * Removes extra backup files * * @since 5.3.0 * @access public */ public static function prune_backups() { $backups = self::get_backups(); global $wp_filesystem; foreach (array_slice($backups, self::MAX_BACKUPS) as $backup) { $path = self::get_backup($backup); $wp_filesystem->delete($path); Debug2::debug('[Preset] Deleted old backup from ' . $backup); } } /** * Returns a settings file's extensionless basename given its filesystem path * * @since 5.3.0 * @access public */ public static function basename($path) { return basename($path, '.data'); } /** * Returns a standard preset's path given its extensionless basename * * @since 5.3.0 * @access public */ public static function get_standard($name) { return path_join(self::STANDARD_DIR, $name . '.data'); } /** * Returns a backup's path given its extensionless basename * * @since 5.3.0 * @access public */ public static function get_backup($name) { return path_join(self::BACKUP_DIR, $name . '.data'); } /** * Initializes the global $wp_filesystem object and clears stat cache * * @since 5.3.0 */ static function init_filesystem() { require_once ABSPATH . '/wp-admin/includes/file.php'; \WP_Filesystem(); clearstatcache(); } /** * Init * * @since 5.3.0 */ public function __construct() { Debug2::debug('[Preset] Init'); $this->_summary = self::get_summary(); } /** * Applies a standard preset's settings given its extensionless basename * * @since 5.3.0 * @access public */ public function apply($preset) { $this->make_backup($preset); $path = self::get_standard($preset); $result = $this->import_file($path) ? $preset : 'error'; $this->log($result); } /** * Restores settings from the backup file with the given timestamp, then deletes the file * * @since 5.3.0 * @access public */ public function restore($timestamp) { $backups = array(); foreach (self::get_backups() as $backup) { if (preg_match('/^backup-' . $timestamp . '(-|$)/', $backup) === 1) { $backups[] = $backup; } } if (empty($backups)) { $this->log('error'); return; } $backup = $backups[0]; $path = self::get_backup($backup); if (!$this->import_file($path)) { $this->log('error'); return; } self::init_filesystem(); global $wp_filesystem; $wp_filesystem->delete($path); Debug2::debug('[Preset] Deleted most recent backup from ' . $backup); $this->log('backup'); } /** * Saves current settings as a backup file, then prunes extra backup files * * @since 5.3.0 * @access public */ public function make_backup($preset) { $backup = 'backup-' . time() . '-before-' . $preset; $data = $this->export(true); $path = self::get_backup($backup); File::save($path, $data, true); Debug2::debug('[Preset] Backup saved to ' . $backup); self::prune_backups(); } /** * Tries to import from a given settings file * * @since 5.3.0 */ function import_file($path) { $debug = function ($result, $name) { $action = $result ? 'Applied' : 'Failed to apply'; Debug2::debug('[Preset] ' . $action . ' settings from ' . $name); return $result; }; $name = self::basename($path); $contents = file_get_contents($path); if (false === $contents) { Debug2::debug('[Preset] ❌ Failed to get file contents'); return $debug(false, $name); } $parsed = array(); try { // Check if the data is v4+ if (strpos($contents, '["_version",') === 0) { $contents = explode("\n", $contents); foreach ($contents as $line) { $line = trim($line); if (empty($line)) { continue; } list($key, $value) = \json_decode($line, true); $parsed[$key] = $value; } } else { $parsed = \json_decode(base64_decode($contents), true); } } catch (\Exception $ex) { Debug2::debug('[Preset] ❌ Failed to parse serialized data'); return $debug(false, $name); } if (empty($parsed)) { Debug2::debug('[Preset] ❌ Nothing to apply'); return $debug(false, $name); } $this->cls('Conf')->update_confs($parsed); return $debug(true, $name); } /** * Updates the log * * @since 5.3.0 */ function log($preset) { $this->_summary['preset'] = $preset; $this->_summary['preset_timestamp'] = time(); self::save_summary(); } /** * Handles all request actions from main cls * * @since 5.3.0 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_APPLY: $this->apply(!empty($_GET['preset']) ? $_GET['preset'] : false); break; case self::TYPE_RESTORE: $this->restore(!empty($_GET['timestamp']) ? $_GET['timestamp'] : false); break; default: break; } Admin::redirect(); } } api.cls.php 0000644 00000026116 15162226227 0006621 0 ustar 00 <?php /** * The plugin API class. * * @since 1.1.3 * @since 1.4 Moved into /inc * @package LiteSpeed * @subpackage LiteSpeed/inc * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class API extends Base { const VERSION = Core::VER; const TYPE_FEED = Tag::TYPE_FEED; const TYPE_FRONTPAGE = Tag::TYPE_FRONTPAGE; const TYPE_HOME = Tag::TYPE_HOME; const TYPE_PAGES = Tag::TYPE_PAGES; const TYPE_PAGES_WITH_RECENT_POSTS = Tag::TYPE_PAGES_WITH_RECENT_POSTS; const TYPE_HTTP = Tag::TYPE_HTTP; const TYPE_ARCHIVE_POSTTYPE = Tag::TYPE_ARCHIVE_POSTTYPE; const TYPE_ARCHIVE_TERM = Tag::TYPE_ARCHIVE_TERM; const TYPE_AUTHOR = Tag::TYPE_AUTHOR; const TYPE_ARCHIVE_DATE = Tag::TYPE_ARCHIVE_DATE; const TYPE_BLOG = Tag::TYPE_BLOG; const TYPE_LOGIN = Tag::TYPE_LOGIN; const TYPE_URL = Tag::TYPE_URL; const TYPE_ESI = Tag::TYPE_ESI; const PARAM_NAME = ESI::PARAM_NAME; const WIDGET_O_ESIENABLE = ESI::WIDGET_O_ESIENABLE; const WIDGET_O_TTL = ESI::WIDGET_O_TTL; /** * Instance * * @since 3.0 */ public function __construct() { } /** * Define hooks to be used in other plugins. * * The benefit to use hooks other than functions is no need to detach if LSCWP enabled and function existed or not anymore * * @since 3.0 */ public function init() { /** * Init */ // Action `litespeed_init` // @previous API::hook_init( $hook ) /** * Conf */ add_filter('litespeed_conf', array($this, 'conf')); // @previous API::config($id) // Action `litespeed_conf_append` // @previous API::conf_append( $name, $default ) add_action('litespeed_conf_multi_switch', __NAMESPACE__ . '\Base::set_multi_switch', 10, 2); // Action ``litespeed_conf_force` // @previous API::force_option( $k, $v ) /** * Cache Control Hooks */ // Action `litespeed_control_finalize` // @previous API::hook_control($tags) && action `litespeed_api_control` add_action('litespeed_control_set_private', __NAMESPACE__ . '\Control::set_private'); // @previous API::set_cache_private() add_action('litespeed_control_set_nocache', __NAMESPACE__ . '\Control::set_nocache'); // @previous API::set_nocache( $reason = false ) add_action('litespeed_control_set_cacheable', array($this, 'set_cacheable')); // Might needed if not call hook `wp` // @previous API::set_cacheable( $reason ) add_action('litespeed_control_force_cacheable', __NAMESPACE__ . '\Control::force_cacheable'); // Set cache status to force cacheable ( Will ignore most kinds of non-cacheable conditions ) // @previous API::set_force_cacheable( $reason ) add_action('litespeed_control_force_public', __NAMESPACE__ . '\Control::set_public_forced'); // Set cache to force public cache if cacheable ( Will ignore most kinds of non-cacheable conditions ) // @previous API::set_force_public( $reason ) add_filter('litespeed_control_cacheable', __NAMESPACE__ . '\Control::is_cacheable', 3); // Note: Read-Only. Directly append to this filter won't work. Call actions above to set cacheable or not // @previous API::not_cacheable() add_action('litespeed_control_set_ttl', __NAMESPACE__ . '\Control::set_custom_ttl', 10, 2); // @previous API::set_ttl( $val ) add_filter('litespeed_control_ttl', array($this, 'get_ttl'), 3); // @previous API::get_ttl() /** * Tag Hooks */ // Action `litespeed_tag_finalize` // @previous API::hook_tag( $hook ) add_action('litespeed_tag', __NAMESPACE__ . '\Tag::add'); // Shorter alias of `litespeed_tag_add` add_action('litespeed_tag_post', __NAMESPACE__ . '\Tag::add_post'); // Shorter alias of `litespeed_tag_add_post` add_action('litespeed_tag_widget', __NAMESPACE__ . '\Tag::add_widget'); // Shorter alias of `litespeed_tag_add_widget` add_action('litespeed_tag_private', __NAMESPACE__ . '\Tag::add_private'); // Shorter alias of `litespeed_tag_add_private` add_action('litespeed_tag_private_esi', __NAMESPACE__ . '\Tag::add_private_esi'); // Shorter alias of `litespeed_tag_add_private_esi` add_action('litespeed_tag_add', __NAMESPACE__ . '\Tag::add'); // @previous API::tag_add( $tag ) add_action('litespeed_tag_add_post', __NAMESPACE__ . '\Tag::add_post'); add_action('litespeed_tag_add_widget', __NAMESPACE__ . '\Tag::add_widget'); add_action('litespeed_tag_add_private', __NAMESPACE__ . '\Tag::add_private'); // @previous API::tag_add_private( $tags ) add_action('litespeed_tag_add_private_esi', __NAMESPACE__ . '\Tag::add_private_esi'); /** * Purge Hooks */ // Action `litespeed_purge_finalize` // @previous API::hook_purge($tags) add_action('litespeed_purge', __NAMESPACE__ . '\Purge::add'); // @previous API::purge($tags) add_action('litespeed_purge_all', __NAMESPACE__ . '\Purge::purge_all'); add_action('litespeed_purge_post', array($this, 'purge_post')); // @previous API::purge_post( $pid ) add_action('litespeed_purge_posttype', __NAMESPACE__ . '\Purge::purge_posttype'); add_action('litespeed_purge_url', array($this, 'purge_url')); add_action('litespeed_purge_widget', __NAMESPACE__ . '\Purge::purge_widget'); add_action('litespeed_purge_esi', __NAMESPACE__ . '\Purge::purge_esi'); add_action('litespeed_purge_private', __NAMESPACE__ . '\Purge::add_private'); // @previous API::purge_private( $tags ) add_action('litespeed_purge_private_esi', __NAMESPACE__ . '\Purge::add_private_esi'); add_action('litespeed_purge_private_all', __NAMESPACE__ . '\Purge::add_private_all'); // @previous API::purge_private_all() // Action `litespeed_api_purge_post` // Triggered when purge a post // @previous API::hook_purge_post($hook) // Action `litespeed_purged_all` // Triggered after purged all. add_action('litespeed_purge_all_object', __NAMESPACE__ . '\Purge::purge_all_object'); add_action('litespeed_purge_ucss', __NAMESPACE__ . '\Purge::purge_ucss'); /** * ESI */ // Action `litespeed_nonce` // @previous API::nonce_action( $action ) & API::nonce( $action = -1, $defence_for_html_filter = true ) // NOTE: only available after `init` hook add_filter('litespeed_esi_status', array($this, 'esi_enabled')); // Get ESI enable status // @previous API::esi_enabled() add_filter('litespeed_esi_url', array($this, 'sub_esi_block'), 10, 8); // Generate ESI block url // @previous API::esi_url( $block_id, $wrapper, $params = array(), $control = 'private,no-vary', $silence = false, $preserved = false, $svar = false, $inline_val = false ) // Filter `litespeed_widget_default_options` // Hook widget default settings value. Currently used in Woo 3rd // @previous API::hook_widget_default_options( $hook ) // Filter `litespeed_esi_params` // @previous API::hook_esi_param( $hook ) // Action `litespeed_tpl_normal` // @previous API::hook_tpl_not_esi($hook) && Action `litespeed_is_not_esi_template` // Action `litespeed_esi_load-$block` // @usage add_action( 'litespeed_esi_load-' . $block, $hook ) // @previous API::hook_tpl_esi($block, $hook) add_action('litespeed_esi_combine', __NAMESPACE__ . '\ESI::combine'); /** * Vary * * To modify default vary, There are two ways: Action `litespeed_vary_append` or Filter `litespeed_vary` */ add_action('litespeed_vary_ajax_force', __NAMESPACE__ . '\Vary::can_ajax_vary'); // API::force_vary() -> Action `litespeed_vary_ajax_force` // Force finalize vary even if its in an AJAX call // Filter `litespeed_vary_curr_cookies` to generate current in use vary, which will be used for response vary header. // Filter `litespeed_vary_cookies` to register the final vary cookies, which will be written to rewrite rule. (litespeed_vary_curr_cookies are always equal to or less than litespeed_vary_cookies) // Filter `litespeed_vary` // Previous API::hook_vary_finalize( $hook ) add_action('litespeed_vary_no', __NAMESPACE__ . '\Control::set_no_vary'); // API::set_cache_no_vary() -> Action `litespeed_vary_no` // Set cache status to no vary // add_filter( 'litespeed_is_mobile', __NAMESPACE__ . '\Control::is_mobile' ); // API::set_mobile() -> Filter `litespeed_is_mobile` /** * Cloud */ add_filter('litespeed_is_from_cloud', array($this, 'is_from_cloud')); // Check if current request is from QC (usually its to check REST access) // @see https://wordpress.org/support/topic/image-optimization-not-working-3/ /** * Media */ add_action('litespeed_media_reset', __NAMESPACE__ . '\Media::delete_attachment'); // Reset one media row /** * GUI */ // API::clean_wrapper_begin( $counter = false ) -> Filter `litespeed_clean_wrapper_begin` // Start a to-be-removed html wrapper add_filter('litespeed_clean_wrapper_begin', __NAMESPACE__ . '\GUI::clean_wrapper_begin'); // API::clean_wrapper_end( $counter = false ) -> Filter `litespeed_clean_wrapper_end` // End a to-be-removed html wrapper add_filter('litespeed_clean_wrapper_end', __NAMESPACE__ . '\GUI::clean_wrapper_end'); /** * Mist */ add_action('litespeed_debug', __NAMESPACE__ . '\Debug2::debug', 10, 2); // API::debug()-> Action `litespeed_debug` add_action('litespeed_debug2', __NAMESPACE__ . '\Debug2::debug2', 10, 2); // API::debug2()-> Action `litespeed_debug2` add_action('litespeed_disable_all', array($this, '_disable_all')); // API::disable_all( $reason ) -> Action `litespeed_disable_all` add_action('litspeed_after_admin_init', array($this, '_after_admin_init')); } /** * API for admin related * * @since 3.0 * @access public */ public function _after_admin_init() { /** * GUI */ add_action('litespeed_setting_enroll', array($this->cls('Admin_Display'), 'enroll'), 10, 4); // API::enroll( $id ) // Register a field in setting form to save add_action('litespeed_build_switch', array($this->cls('Admin_Display'), 'build_switch')); // API::build_switch( $id ) // Build a switch div html snippet // API::hook_setting_content( $hook, $priority = 10, $args = 1 ) -> Action `litespeed_settings_content` // API::hook_setting_tab( $hook, $priority = 10, $args = 1 ) -> Action `litespeed_settings_tab` } /** * Disable All (Note: Not for direct call, always use Hooks) * * @since 2.9.7.2 * @access public */ public function _disable_all($reason) { do_action('litespeed_debug', '[API] Disabled_all due to ' . $reason); !defined('LITESPEED_DISABLE_ALL') && define('LITESPEED_DISABLE_ALL', true); } /** * @since 3.0 */ public static function vary_append_commenter() { Vary::cls()->append_commenter(); } /** * Check if is from Cloud * * @since 4.2 */ public function is_from_cloud() { return $this->cls('Cloud')->is_from_cloud(); } public function purge_post($pid) { $this->cls('Purge')->purge_post($pid); } public function purge_url($url) { $this->cls('Purge')->purge_url($url); } public function set_cacheable($reason = false) { $this->cls('Control')->set_cacheable($reason); } public function esi_enabled() { return $this->cls('Router')->esi_enabled(); } public function get_ttl() { return $this->cls('Control')->get_ttl(); } public function sub_esi_block( $block_id, $wrapper, $params = array(), $control = 'private,no-vary', $silence = false, $preserved = false, $svar = false, $inline_param = array() ) { return $this->cls('ESI')->sub_esi_block($block_id, $wrapper, $params, $control, $silence, $preserved, $svar, $inline_param); } } health.cls.php 0000644 00000005622 15162226230 0007306 0 ustar 00 <?php /** * The page health * * * @since 3.0 * @package LiteSpeed * @subpackage LiteSpeed/src * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Health extends Base { const TYPE_SPEED = 'speed'; const TYPE_SCORE = 'score'; protected $_summary; /** * Init * * @since 3.0 */ public function __construct() { $this->_summary = self::get_summary(); } /** * Test latest speed * * @since 3.0 */ private function _ping($type) { $data = array('action' => $type); $json = Cloud::post(Cloud::SVC_HEALTH, $data, 600); if (empty($json['data']['before']) || empty($json['data']['after'])) { Debug2::debug('[Health] ❌ no data'); return false; } $this->_summary[$type . '.before'] = $json['data']['before']; $this->_summary[$type . '.after'] = $json['data']['after']; self::save_summary(); Debug2::debug('[Health] saved result'); } /** * Generate scores * * @since 3.0 */ public function scores() { $speed_before = $speed_after = $speed_improved = 0; if (!empty($this->_summary['speed.before']) && !empty($this->_summary['speed.after'])) { // Format loading time $speed_before = $this->_summary['speed.before'] / 1000; if ($speed_before < 0.01) { $speed_before = 0.01; } $speed_before = number_format($speed_before, 2); $speed_after = $this->_summary['speed.after'] / 1000; if ($speed_after < 0.01) { $speed_after = number_format($speed_after, 3); } else { $speed_after = number_format($speed_after, 2); } $speed_improved = (($this->_summary['speed.before'] - $this->_summary['speed.after']) * 100) / $this->_summary['speed.before']; if ($speed_improved > 99) { $speed_improved = number_format($speed_improved, 2); } else { $speed_improved = number_format($speed_improved); } } $score_before = $score_after = $score_improved = 0; if (!empty($this->_summary['score.before']) && !empty($this->_summary['score.after'])) { $score_before = $this->_summary['score.before']; $score_after = $this->_summary['score.after']; // Format Score $score_improved = (($score_after - $score_before) * 100) / $score_after; if ($score_improved > 99) { $score_improved = number_format($score_improved, 2); } else { $score_improved = number_format($score_improved); } } return array( 'speed_before' => $speed_before, 'speed_after' => $speed_after, 'speed_improved' => $speed_improved, 'score_before' => $score_before, 'score_after' => $score_after, 'score_improved' => $score_improved, ); } /** * Handle all request actions from main cls * * @since 3.0 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_SPEED: case self::TYPE_SCORE: $this->_ping($type); break; default: break; } Admin::redirect(); } } metabox.cls.php 0000644 00000010322 15162226231 0007472 0 ustar 00 <?php /** * The class to operate post editor metabox settings * * @since 4.7 * @package Core * @subpackage Core/inc * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Metabox extends Root { const LOG_TAG = '📦'; const POST_NONCE_ACTION = 'post_nonce_action'; private $_postmeta_settings; /** * Get the setting list * @since 4.7 */ public function __construct() { // Append meta box $this->_postmeta_settings = array( 'litespeed_no_cache' => __('Disable Cache', 'litespeed-cache'), 'litespeed_no_image_lazy' => __('Disable Image Lazyload', 'litespeed-cache'), 'litespeed_no_vpi' => __('Disable VPI', 'litespeed-cache'), 'litespeed_vpi_list' => __('Viewport Images', 'litespeed-cache'), 'litespeed_vpi_list_mobile' => __('Viewport Images', 'litespeed-cache') . ' - ' . __('Mobile', 'litespeed-cache'), ); } /** * Register post edit settings * @since 4.7 */ public function register_settings() { add_action('add_meta_boxes', array($this, 'add_meta_boxes')); add_action('save_post', array($this, 'save_meta_box_settings'), 15, 2); add_action('attachment_updated', array($this, 'save_meta_box_settings'), 15, 2); } /** * Register meta box * @since 4.7 */ public function add_meta_boxes($post_type) { if (apply_filters('litespeed_bypass_metabox', false, $post_type)) { return; } $post_type_obj = get_post_type_object($post_type); if (!empty($post_type_obj) && !$post_type_obj->public) { self::debug('post type public=false, bypass add_meta_boxes'); return; } add_meta_box('litespeed_meta_boxes', __('LiteSpeed Options', 'litespeed-cache'), array($this, 'meta_box_options'), $post_type, 'side', 'core'); } /** * Show meta box content * @since 4.7 */ public function meta_box_options() { require_once LSCWP_DIR . 'tpl/inc/metabox.php'; } /** * Save settings * @since 4.7 */ public function save_meta_box_settings($post_id, $post) { global $pagenow; self::debug('Maybe save post2 [post_id] ' . $post_id); if ($pagenow != 'post.php' || !$post || !is_object($post)) { return; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } if (!$this->cls('Router')->verify_nonce(self::POST_NONCE_ACTION)) { return; } self::debug('Saving post [post_id] ' . $post_id); foreach ($this->_postmeta_settings as $k => $v) { $val = isset($_POST[$k]) ? $_POST[$k] : false; $this->save($post_id, $k, $val); } } /** * Load setting per post * @since 4.7 */ public function setting($conf, $post_id = false) { // Check if has metabox non-cacheable setting or not if (!$post_id) { $home_id = get_option('page_for_posts'); if (is_singular()) { $post_id = get_the_ID(); } elseif ($home_id > 0 && is_home()) { $post_id = $home_id; } } if ($post_id && ($val = get_post_meta($post_id, $conf, true))) { return $val; } return null; } /** * Save a metabox value * @since 4.7 */ public function save($post_id, $name, $val, $is_append = false) { if (strpos($name, 'litespeed_vpi_list') !== false) { $val = Utility::sanitize_lines($val, 'basename,drop_webp'); } // Load existing data if has set if ($is_append) { $existing_data = $this->setting($name, $post_id); if ($existing_data) { $existing_data = Utility::sanitize_lines($existing_data, 'basename'); $val = array_unique(array_merge($val, $existing_data)); } } if ($val) { update_post_meta($post_id, $name, $val); } else { delete_post_meta($post_id, $name); } } /** * Load exclude images per post * @since 4.7 */ public function lazy_img_excludes($list) { $is_mobile = $this->_separate_mobile(); $excludes = $this->setting($is_mobile ? 'litespeed_vpi_list_mobile' : 'litespeed_vpi_list'); if ($excludes !== null) { $excludes = Utility::sanitize_lines($excludes, 'basename'); if ($excludes) { // Check if contains `data:` (invalid result, need to clear existing result) or not if (Utility::str_hit_array('data:', $excludes)) { $this->cls('VPI')->add_to_queue(); } else { return array_merge($list, $excludes); } } return $list; } $this->cls('VPI')->add_to_queue(); return $list; } } admin-settings.cls.php 0000644 00000024032 15162226232 0010765 0 ustar 00 <?php /** * The admin settings handler of the plugin. * * * @since 1.1.0 * @package LiteSpeed * @subpackage LiteSpeed/src * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Admin_Settings extends Base { const ENROLL = '_settings-enroll'; /** * Save settings * * Both $_POST and CLI can use this way * * Import will directly call conf.cls * * @since 3.0 * @access public */ public function save($raw_data) { Debug2::debug('[Settings] saving'); if (empty($raw_data[self::ENROLL])) { exit('No fields'); } $raw_data = Admin::cleanup_text($raw_data); // Convert data to config format $the_matrix = array(); foreach (array_unique($raw_data[self::ENROLL]) as $id) { $child = false; // Drop array format if (strpos($id, '[') !== false) { if (strpos($id, self::O_CDN_MAPPING) === 0 || strpos($id, self::O_CRAWLER_COOKIES) === 0) { // CDN child | Cookie Crawler settings $child = substr($id, strpos($id, '[') + 1, strpos($id, ']') - strpos($id, '[') - 1); $id = substr($id, 0, strpos($id, '[')); // Drop ending []; Compatible with xx[0] way from CLI } else { $id = substr($id, 0, strpos($id, '[')); // Drop ending [] } } if (!array_key_exists($id, self::$_default_options)) { continue; } // Validate $child if ($id == self::O_CDN_MAPPING) { if (!in_array($child, array(self::CDN_MAPPING_URL, self::CDN_MAPPING_INC_IMG, self::CDN_MAPPING_INC_CSS, self::CDN_MAPPING_INC_JS, self::CDN_MAPPING_FILETYPE))) { continue; } } if ($id == self::O_CRAWLER_COOKIES) { if (!in_array($child, array(self::CRWL_COOKIE_NAME, self::CRWL_COOKIE_VALS))) { continue; } } $data = false; if ($child) { $data = !empty($raw_data[$id][$child]) ? $raw_data[$id][$child] : false; // []=xxx or [0]=xxx } else { $data = !empty($raw_data[$id]) ? $raw_data[$id] : false; } /** * Sanitize the value */ if ($id == self::O_CDN_MAPPING || $id == self::O_CRAWLER_COOKIES) { // Use existing in queue data if existed (Only available when $child != false) $data2 = array_key_exists($id, $the_matrix) ? $the_matrix[$id] : (defined('WP_CLI') && WP_CLI ? $this->conf($id) : array()); } switch ($id) { case self::O_CRAWLER_ROLES: // Don't allow Editor/admin to be used in crawler role simulator $data = Utility::sanitize_lines($data); if ($data) { foreach ($data as $k => $v) { if (user_can($v, 'edit_posts')) { $msg = sprintf( __('The user with id %s has editor access, which is not allowed for the role simulator.', 'litespeed-cache'), '<code>' . $v . '</code>' ); Admin_Display::error($msg); unset($data[$k]); } } } break; case self::O_CDN_MAPPING: /** * CDN setting * * Raw data format: * cdn-mapping[url][] = 'xxx' * cdn-mapping[url][2] = 'xxx2' * cdn-mapping[inc_js][] = 1 * * Final format: * cdn-mapping[ 0 ][ url ] = 'xxx' * cdn-mapping[ 2 ][ url ] = 'xxx2' */ if ($data) { foreach ($data as $k => $v) { if ($child == self::CDN_MAPPING_FILETYPE) { $v = Utility::sanitize_lines($v); } if ($child == self::CDN_MAPPING_URL) { # If not a valid URL, turn off CDN if (strpos($v, 'https://') !== 0) { self::debug('❌ CDN mapping set to OFF due to invalid URL'); $the_matrix[self::O_CDN] = false; } $v = trailingslashit($v); } if (in_array($child, array(self::CDN_MAPPING_INC_IMG, self::CDN_MAPPING_INC_CSS, self::CDN_MAPPING_INC_JS))) { // Because these can't be auto detected in `config->update()`, need to format here $v = $v === 'false' ? 0 : (bool) $v; } if (empty($data2[$k])) { $data2[$k] = array(); } $data2[$k][$child] = $v; } } $data = $data2; break; case self::O_CRAWLER_COOKIES: /** * Cookie Crawler setting * Raw Format: * crawler-cookies[name][] = xxx * crawler-cookies[name][2] = xxx2 * crawler-cookies[vals][] = xxx * * todo: need to allow null for values * * Final format: * crawler-cookie[ 0 ][ name ] = 'xxx' * crawler-cookie[ 0 ][ vals ] = 'xxx' * crawler-cookie[ 2 ][ name ] = 'xxx2' * * empty line for `vals` use literal `_null` */ if ($data) { foreach ($data as $k => $v) { if ($child == self::CRWL_COOKIE_VALS) { $v = Utility::sanitize_lines($v); } if (empty($data2[$k])) { $data2[$k] = array(); } $data2[$k][$child] = $v; } } $data = $data2; break; case self::O_CACHE_EXC_CAT: // Cache exclude cat $data2 = array(); $data = Utility::sanitize_lines($data); foreach ($data as $v) { $cat_id = get_cat_ID($v); if (!$cat_id) { continue; } $data2[] = $cat_id; } $data = $data2; break; case self::O_CACHE_EXC_TAG: // Cache exclude tag $data2 = array(); $data = Utility::sanitize_lines($data); foreach ($data as $v) { $term = get_term_by('name', $v, 'post_tag'); if (!$term) { // todo: can show the error in admin error msg continue; } $data2[] = $term->term_id; } $data = $data2; break; default: break; } $the_matrix[$id] = $data; } // Special handler for CDN/Crawler 2d list to drop empty rows foreach ($the_matrix as $id => $data) { /** * cdn-mapping[ 0 ][ url ] = 'xxx' * cdn-mapping[ 2 ][ url ] = 'xxx2' * * crawler-cookie[ 0 ][ name ] = 'xxx' * crawler-cookie[ 0 ][ vals ] = 'xxx' * crawler-cookie[ 2 ][ name ] = 'xxx2' */ if ($id == self::O_CDN_MAPPING || $id == self::O_CRAWLER_COOKIES) { // Drop this line if all children elements are empty foreach ($data as $k => $v) { foreach ($v as $v2) { if ($v2) { continue 2; } } // If hit here, means all empty unset($the_matrix[$id][$k]); } } // Don't allow repeated cookie name if ($id == self::O_CRAWLER_COOKIES) { $existed = array(); foreach ($the_matrix[$id] as $k => $v) { if (!$v[self::CRWL_COOKIE_NAME] || in_array($v[self::CRWL_COOKIE_NAME], $existed)) { // Filter repeated or empty name unset($the_matrix[$id][$k]); continue; } $existed[] = $v[self::CRWL_COOKIE_NAME]; } } // CDN mapping allow URL values repeated // if ( $id == self::O_CDN_MAPPING ) {} // tmp fix the 3rd part woo update hook issue when enabling vary cookie if ($id == 'wc_cart_vary') { if ($data) { add_filter('litespeed_vary_cookies', function ($list) { $list[] = 'woocommerce_cart_hash'; return array_unique($list); }); } else { add_filter('litespeed_vary_cookies', function ($list) { if (in_array('woocommerce_cart_hash', $list)) { unset($list[array_search('woocommerce_cart_hash', $list)]); } return array_unique($list); }); } } } // id validation will be inside $this->cls('Conf')->update_confs($the_matrix); $msg = __('Options saved.', 'litespeed-cache'); Admin_Display::success($msg); } /** * Parses any changes made by the network admin on the network settings. * * @since 3.0 * @access public */ public function network_save($raw_data) { Debug2::debug('[Settings] network saving'); if (empty($raw_data[self::ENROLL])) { exit('No fields'); } $raw_data = Admin::cleanup_text($raw_data); foreach (array_unique($raw_data[self::ENROLL]) as $id) { // Append current field to setting save if (!array_key_exists($id, self::$_default_site_options)) { continue; } $data = !empty($raw_data[$id]) ? $raw_data[$id] : false; // id validation will be inside $this->cls('Conf')->network_update($id, $data); } // Update related files Activation::cls()->update_files(); $msg = __('Options saved.', 'litespeed-cache'); Admin_Display::success($msg); } /** * Hooked to the wp_redirect filter. * This will only hook if there was a problem when saving the widget. * * @since 1.1.3 * @access public * @param string $location The location string. * @return string the updated location string. */ public static function widget_save_err($location) { return str_replace('?message=0', '?error=0', $location); } /** * Hooked to the widget_update_callback filter. * Validate the LiteSpeed Cache settings on edit widget save. * * @since 1.1.3 * @access public * @param array $instance The new settings. * @param array $new_instance * @param array $old_instance The original settings. * @param WP_Widget $widget The widget * @return mixed Updated settings on success, false on error. */ public static function validate_widget_save($instance, $new_instance, $old_instance, $widget) { if (empty($new_instance)) { return $instance; } if (!isset($new_instance[ESI::WIDGET_O_ESIENABLE]) || !isset($new_instance[ESI::WIDGET_O_TTL])) { return $instance; } $esi = intval($new_instance[ESI::WIDGET_O_ESIENABLE]) % 3; $ttl = (int) $new_instance[ESI::WIDGET_O_TTL]; if ($ttl != 0 && $ttl < 30) { add_filter('wp_redirect', __CLASS__ . '::widget_save_err'); return false; // invalid ttl. } if (empty($instance[Conf::OPTION_NAME])) { // todo: to be removed $instance[Conf::OPTION_NAME] = array(); } $instance[Conf::OPTION_NAME][ESI::WIDGET_O_ESIENABLE] = $esi; $instance[Conf::OPTION_NAME][ESI::WIDGET_O_TTL] = $ttl; $current = !empty($old_instance[Conf::OPTION_NAME]) ? $old_instance[Conf::OPTION_NAME] : false; if (!strpos($_SERVER['HTTP_REFERER'], '/wp-admin/customize.php')) { if (!$current || $esi != $current[ESI::WIDGET_O_ESIENABLE]) { Purge::purge_all('Widget ESI_enable changed'); } elseif ($ttl != 0 && $ttl != $current[ESI::WIDGET_O_TTL]) { Purge::add(Tag::TYPE_WIDGET . $widget->id); } Purge::purge_all('Widget saved'); } return $instance; } } error.cls.php 0000644 00000015620 15162226233 0007174 0 ustar 00 <?php /** * The error class. * * @since 3.0 * @package LiteSpeed * @subpackage LiteSpeed/src * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Error { private static $CODE_SET = array( 'HTA_LOGIN_COOKIE_INVALID' => 4300, // .htaccess did not find. 'HTA_DNF' => 4500, // .htaccess did not find. 'HTA_BK' => 9010, // backup 'HTA_R' => 9041, // read htaccess 'HTA_W' => 9042, // write 'HTA_GET' => 9030, // failed to get ); /** * Throw an error with msg * * @since 3.0 */ public static function t($code, $args = null) { throw new \Exception(self::msg($code, $args)); } /** * Translate an error to description * * @since 3.0 */ public static function msg($code, $args = null) { switch ($code) { case 'disabled_all': $msg = sprintf(__('The setting %s is currently enabled.', 'litespeed-cache'), '<strong>' . Lang::title(Base::O_DEBUG_DISABLE_ALL) . '</strong>') . Doc::learn_more( is_network_admin() ? network_admin_url('admin.php?page=litespeed-toolbox') : admin_url('admin.php?page=litespeed-toolbox'), __('Click here to change.', 'litespeed-cache'), true, false, true ); break; case 'qc_setup_required': $msg = sprintf(__('You will need to finish %s setup to use the online services.', 'litespeed-cache'), '<strong>QUIC.cloud</strong>') . Doc::learn_more(admin_url('admin.php?page=litespeed-general'), __('Click here to set.', 'litespeed-cache'), true, false, true); break; case 'out_of_daily_quota': $msg = __('You have used all of your daily quota for today.', 'litespeed-cache'); $msg .= ' ' . Doc::learn_more( 'https://docs.quic.cloud/billing/services/#daily-limits-on-free-quota-usage', __('Learn more or purchase additional quota.', 'litespeed-cache'), false, false, true ); break; case 'out_of_quota': $msg = __('You have used all of your quota left for current service this month.', 'litespeed-cache'); $msg .= ' ' . Doc::learn_more( 'https://docs.quic.cloud/billing/services/#daily-limits-on-free-quota-usage', __('Learn more or purchase additional quota.', 'litespeed-cache'), false, false, true ); break; case 'too_many_requested': $msg = __('You have too many requested images, please try again in a few minutes.', 'litespeed-cache'); break; case 'too_many_notified': $msg = __('You have images waiting to be pulled. Please wait for the automatic pull to complete, or pull them down manually now.', 'litespeed-cache'); break; case 'empty_list': $msg = __('The image list is empty.', 'litespeed-cache'); break; case 'lack_of_param': $msg = __('Not enough parameters. Please check if the domain key is set correctly', 'litespeed-cache'); break; case 'unfinished_queue': $msg = __('There is proceeding queue not pulled yet.', 'litespeed-cache'); break; case strpos($code, 'unfinished_queue ') === 0: $msg = sprintf( __('There is proceeding queue not pulled yet. Queue info: %s.', 'litespeed-cache'), '<code>' . substr($code, strlen('unfinished_queue ')) . '</code>' ); break; case 'err_alias': $msg = __('The site is not a valid alias on QUIC.cloud.', 'litespeed-cache'); break; case 'site_not_registered': $msg = __('The site is not registered on QUIC.cloud.', 'litespeed-cache'); break; case 'err_key': $msg = __('The domain key is not correct. Please try to sync your domain key again.', 'litespeed-cache'); break; case 'heavy_load': $msg = __('The current server is under heavy load.', 'litespeed-cache'); break; case 'redetect_node': $msg = __('Online node needs to be redetected.', 'litespeed-cache'); break; case 'err_overdraw': $msg = __('Credits are not enough to proceed the current request.', 'litespeed-cache'); break; case 'W': $msg = __('%s file not writable.', 'litespeed-cache'); break; case 'HTA_DNF': if (!is_array($args)) { $args = array('<code>' . $args . '</code>'); } $args[] = '.htaccess'; $msg = __('Could not find %1$s in %2$s.', 'litespeed-cache'); break; case 'HTA_LOGIN_COOKIE_INVALID': $msg = sprintf(__('Invalid login cookie. Please check the %s file.', 'litespeed-cache'), '.htaccess'); break; case 'HTA_BK': $msg = sprintf(__('Failed to back up %s file, aborted changes.', 'litespeed-cache'), '.htaccess'); break; case 'HTA_R': $msg = sprintf(__('%s file not readable.', 'litespeed-cache'), '.htaccess'); break; case 'HTA_W': $msg = sprintf(__('%s file not writable.', 'litespeed-cache'), '.htaccess'); break; case 'HTA_GET': $msg = sprintf(__('Failed to get %s file contents.', 'litespeed-cache'), '.htaccess'); break; case 'failed_tb_creation': $msg = __('Failed to create table %s! SQL: %s.', 'litespeed-cache'); break; case 'crawler_disabled': $msg = __('Crawler disabled by the server admin.', 'litespeed-cache'); break; case 'try_later': // QC error code $msg = __('Previous request too recent. Please try again later.', 'litespeed-cache'); break; case strpos($code, 'try_later ') === 0: $msg = sprintf( __('Previous request too recent. Please try again after %s.', 'litespeed-cache'), '<code>' . Utility::readable_time(substr($code, strlen('try_later ')), 3600, true) . '</code>' ); break; case 'waiting_for_approval': $msg = __('Your application is waiting for approval.', 'litespeed-cache'); break; case 'callback_fail_hash': $msg = __('The callback validation to your domain failed due to hash mismatch.', 'litespeed-cache'); break; case 'callback_fail': $msg = __('The callback validation to your domain failed. Please make sure there is no firewall blocking our servers.', 'litespeed-cache'); break; case substr($code, 0, 14) === 'callback_fail ': $msg = __('The callback validation to your domain failed. Please make sure there is no firewall blocking our servers. Response code: ', 'litespeed-cache') . substr($code, 14); break; case 'forbidden': $msg = __('Your domain has been forbidden from using our services due to a previous policy violation.', 'litespeed-cache'); break; case 'err_dns_active': $msg = __( 'You cannot remove this DNS zone, because it is still in use. Please update the domain\'s nameservers, then try to delete this zone again, otherwise your site will become inaccessible.', 'litespeed-cache' ); break; default: $msg = __('Unknown error', 'litespeed-cache') . ': ' . $code; break; } if ($args !== null) { $msg = is_array($args) ? vsprintf($msg, $args) : sprintf($msg, $args); } if (isset(self::$CODE_SET[$code])) { $msg = 'ERROR ' . self::$CODE_SET[$code] . ': ' . $msg; } return $msg; } } tool.cls.php 0000644 00000006653 15162226234 0007027 0 ustar 00 <?php /** * The tools * * @since 3.0 * @package LiteSpeed * @subpackage LiteSpeed/inc * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Tool extends Root { const LOG_TAG = '[Tool]'; /** * Get public IP * * @since 3.0 * @access public */ public function check_ip() { self::debug('✅ check_ip'); $response = wp_safe_remote_get('https://cyberpanel.sh/?ip', array( 'headers' => array( 'User-Agent' => 'curl/8.7.1', ), )); if (is_wp_error($response)) { return __('Failed to detect IP', 'litespeed-cache'); } $ip = trim($response['body']); self::debug('result [ip] ' . $ip); if (Utility::valid_ipv4($ip)) { return $ip; } return __('Failed to detect IP', 'litespeed-cache'); } /** * Heartbeat Control * * NOTE: since WP4.9, there could be a core bug that sometimes the hook is not working. * * @since 3.0 * @access public */ public function heartbeat() { add_action('wp_enqueue_scripts', array($this, 'heartbeat_frontend')); add_action('admin_enqueue_scripts', array($this, 'heartbeat_backend')); add_filter('heartbeat_settings', array($this, 'heartbeat_settings')); } /** * Heartbeat Control frontend control * * @since 3.0 * @access public */ public function heartbeat_frontend() { if (!$this->conf(Base::O_MISC_HEARTBEAT_FRONT)) { return; } if (!$this->conf(Base::O_MISC_HEARTBEAT_FRONT_TTL)) { wp_deregister_script('heartbeat'); Debug2::debug('[Tool] Deregistered frontend heartbeat'); } } /** * Heartbeat Control backend control * * @since 3.0 * @access public */ public function heartbeat_backend() { if ($this->_is_editor()) { if (!$this->conf(Base::O_MISC_HEARTBEAT_EDITOR)) { return; } if (!$this->conf(Base::O_MISC_HEARTBEAT_EDITOR_TTL)) { wp_deregister_script('heartbeat'); Debug2::debug('[Tool] Deregistered editor heartbeat'); } } else { if (!$this->conf(Base::O_MISC_HEARTBEAT_BACK)) { return; } if (!$this->conf(Base::O_MISC_HEARTBEAT_BACK_TTL)) { wp_deregister_script('heartbeat'); Debug2::debug('[Tool] Deregistered backend heartbeat'); } } } /** * Heartbeat Control settings * * @since 3.0 * @access public */ public function heartbeat_settings($settings) { // Check editor first to make frontend editor valid too if ($this->_is_editor()) { if ($this->conf(Base::O_MISC_HEARTBEAT_EDITOR)) { $settings['interval'] = $this->conf(Base::O_MISC_HEARTBEAT_EDITOR_TTL); Debug2::debug('[Tool] Heartbeat interval set to ' . $this->conf(Base::O_MISC_HEARTBEAT_EDITOR_TTL)); } } elseif (!is_admin()) { if ($this->conf(Base::O_MISC_HEARTBEAT_FRONT)) { $settings['interval'] = $this->conf(Base::O_MISC_HEARTBEAT_FRONT_TTL); Debug2::debug('[Tool] Heartbeat interval set to ' . $this->conf(Base::O_MISC_HEARTBEAT_FRONT_TTL)); } } else { if ($this->conf(Base::O_MISC_HEARTBEAT_BACK)) { $settings['interval'] = $this->conf(Base::O_MISC_HEARTBEAT_BACK_TTL); Debug2::debug('[Tool] Heartbeat interval set to ' . $this->conf(Base::O_MISC_HEARTBEAT_BACK_TTL)); } } return $settings; } /** * If is in editor * * @since 3.0 * @access public */ private function _is_editor() { $res = is_admin() && Utility::str_hit_array($_SERVER['REQUEST_URI'], array('post.php', 'post-new.php')); return apply_filters('litespeed_is_editor', $res); } } img-optm.cls.php 0000644 00000200144 15162226236 0007574 0 ustar 00 <?php /** * The class to optimize image. * * @since 2.0 * @package LiteSpeed * @subpackage LiteSpeed/src * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; use WpOrg\Requests\Autoload; use WpOrg\Requests\Requests; defined('WPINC') || exit(); class Img_Optm extends Base { const LOG_TAG = '🗜️'; const CLOUD_ACTION_NEW_REQ = 'new_req'; const CLOUD_ACTION_TAKEN = 'taken'; const CLOUD_ACTION_REQUEST_DESTROY = 'imgoptm_destroy'; const CLOUD_ACTION_CLEAN = 'clean'; const TYPE_NEW_REQ = 'new_req'; const TYPE_RESCAN = 'rescan'; const TYPE_DESTROY = 'destroy'; const TYPE_RESET_COUNTER = 'reset_counter'; const TYPE_CLEAN = 'clean'; const TYPE_PULL = 'pull'; const TYPE_BATCH_SWITCH_ORI = 'batch_switch_ori'; const TYPE_BATCH_SWITCH_OPTM = 'batch_switch_optm'; const TYPE_CALC_BKUP = 'calc_bkup'; const TYPE_RESET_ROW = 'reset_row'; const TYPE_RM_BKUP = 'rm_bkup'; const STATUS_NEW = 0; // 'new'; const STATUS_RAW = 1; // 'raw'; const STATUS_REQUESTED = 3; // 'requested'; const STATUS_NOTIFIED = 6; // 'notified'; const STATUS_DUPLICATED = 8; // 'duplicated'; const STATUS_PULLED = 9; // 'pulled'; const STATUS_FAILED = -1; //'failed'; const STATUS_MISS = -3; // 'miss'; const STATUS_ERR_FETCH = -5; // 'err_fetch'; const STATUS_ERR_404 = -6; // 'err_404'; const STATUS_ERR_OPTM = -7; // 'err_optm'; const STATUS_XMETA = -8; // 'xmeta'; const STATUS_ERR = -9; // 'err'; const DB_SIZE = 'litespeed-optimize-size'; const DB_SET = 'litespeed-optimize-set'; const DB_NEED_PULL = 'need_pull'; private $wp_upload_dir; private $tmp_pid; private $tmp_type; private $tmp_path; private $_img_in_queue = array(); private $_existed_src_list = array(); private $_pids_set = array(); private $_thumbnail_set = ''; private $_table_img_optm; private $_table_img_optming; private $_cron_ran = false; private $__media; private $__data; protected $_summary; private $_format = ''; /** * Init * * @since 2.0 */ public function __construct() { Debug2::debug2('[ImgOptm] init'); $this->wp_upload_dir = wp_upload_dir(); $this->__media = $this->cls('Media'); $this->__data = $this->cls('Data'); $this->_table_img_optm = $this->__data->tb('img_optm'); $this->_table_img_optming = $this->__data->tb('img_optming'); $this->_summary = self::get_summary(); if (empty($this->_summary['next_post_id'])) { $this->_summary['next_post_id'] = 0; } if ($this->conf(Base::O_IMG_OPTM_WEBP)) { $this->_format = 'webp'; if ($this->conf(Base::O_IMG_OPTM_WEBP) == 2) { $this->_format = 'avif'; } } } /** * Gather images auto when update attachment meta * This is to optimize new uploaded images first. Stored in img_optm table. * Later normal process will auto remove these records when trying to optimize these images again * * @since 4.0 */ public function wp_update_attachment_metadata($meta_value, $post_id) { global $wpdb; self::debug2('🖌️ Auto update attachment meta [id] ' . $post_id); if (empty($meta_value['file'])) { return; } // Load gathered images if (!$this->_existed_src_list) { // To aavoid extra query when recalling this function self::debug('SELECT src from img_optm table'); if ($this->__data->tb_exist('img_optm')) { $q = "SELECT src FROM `$this->_table_img_optm` WHERE post_id = %d"; $list = $wpdb->get_results($wpdb->prepare($q, $post_id)); foreach ($list as $v) { $this->_existed_src_list[] = $post_id . '.' . $v->src; } } if ($this->__data->tb_exist('img_optming')) { $q = "SELECT src FROM `$this->_table_img_optming` WHERE post_id = %d"; $list = $wpdb->get_results($wpdb->prepare($q, $post_id)); foreach ($list as $v) { $this->_existed_src_list[] = $post_id . '.' . $v->src; } } else { $this->__data->tb_create('img_optming'); } } // Prepare images $this->tmp_pid = $post_id; $this->tmp_path = pathinfo($meta_value['file'], PATHINFO_DIRNAME) . '/'; $this->_append_img_queue($meta_value, true); if (!empty($meta_value['sizes'])) { array_map(array($this, '_append_img_queue'), $meta_value['sizes']); } if (!$this->_img_in_queue) { self::debug('auto update attachment meta 2 bypass: empty _img_in_queue'); return; } // Save to DB $this->_save_raw(); // $this->_send_request(); } /** * Auto send optm request * * @since 2.4.1 * @access public */ public static function cron_auto_request() { if (!defined('DOING_CRON')) { return false; } $instance = self::cls(); $instance->new_req(); } /** * Calculate wet run allowance * * @since 3.0 */ public function wet_limit() { $wet_limit = 1; if (!empty($this->_summary['img_taken'])) { $wet_limit = pow($this->_summary['img_taken'], 2); } if ($wet_limit == 1 && !empty($this->_summary['img_status.' . self::STATUS_ERR_OPTM])) { $wet_limit = pow($this->_summary['img_status.' . self::STATUS_ERR_OPTM], 2); } if ($wet_limit < Cloud::IMG_OPTM_DEFAULT_GROUP) { return $wet_limit; } // No limit return false; } /** * Push raw img to image optm server * * @since 1.6 * @access public */ public function new_req() { global $wpdb; // check if is running if (!empty($this->_summary['is_running']) && time() - $this->_summary['is_running'] < apply_filters('litespeed_imgoptm_new_req_interval', 3600)) { self::debug('The previous req was in 3600s.'); return; } $this->_summary['is_running'] = time(); self::save_summary(); // Check if has credit to push $err = false; $allowance = Cloud::cls()->allowance(Cloud::SVC_IMG_OPTM, $err); $wet_limit = $this->wet_limit(); self::debug("allowance_max $allowance wet_limit $wet_limit"); if ($wet_limit && $wet_limit < $allowance) { $allowance = $wet_limit; } if (!$allowance) { self::debug('❌ No credit'); Admin_Display::error(Error::msg($err)); $this->_finished_running(); return; } self::debug('preparing images to push'); $this->__data->tb_create('img_optming'); $q = "SELECT COUNT(1) FROM `$this->_table_img_optming` WHERE optm_status = %d"; $q = $wpdb->prepare($q, array(self::STATUS_REQUESTED)); $total_requested = $wpdb->get_var($q); $max_requested = $allowance * 1; if ($total_requested > $max_requested) { self::debug('❌ Too many queued images (' . $total_requested . ' > ' . $max_requested . ')'); Admin_Display::error(Error::msg('too_many_requested')); $this->_finished_running(); return; } $allowance -= $total_requested; if ($allowance < 1) { self::debug('❌ Too many requested images ' . $total_requested); Admin_Display::error(Error::msg('too_many_requested')); $this->_finished_running(); return; } // Limit maximum number of items waiting to be pulled $q = "SELECT COUNT(1) FROM `$this->_table_img_optming` WHERE optm_status = %d"; $q = $wpdb->prepare($q, array(self::STATUS_NOTIFIED)); $total_notified = $wpdb->get_var($q); if ($total_notified > 0) { self::debug('❌ Too many notified images (' . $total_notified . ')'); Admin_Display::error(Error::msg('too_many_notified')); $this->_finished_running(); return; } $q = "SELECT COUNT(1) FROM `$this->_table_img_optming` WHERE optm_status IN (%d, %d)"; $q = $wpdb->prepare($q, array(self::STATUS_NEW, self::STATUS_RAW)); $total_new = $wpdb->get_var($q); // $allowance -= $total_new; // May need to get more images $list = array(); $more = $allowance - $total_new; if ($more > 0) { $q = "SELECT b.post_id, b.meta_value FROM `$wpdb->posts` a LEFT JOIN `$wpdb->postmeta` b ON b.post_id = a.ID WHERE b.meta_key = '_wp_attachment_metadata' AND a.post_type = 'attachment' AND a.post_status = 'inherit' AND a.ID>%d AND a.post_mime_type IN ('image/jpeg', 'image/png', 'image/gif') ORDER BY a.ID LIMIT %d "; $q = $wpdb->prepare($q, array($this->_summary['next_post_id'], $more)); $list = $wpdb->get_results($q); foreach ($list as $v) { if (!$v->post_id) { continue; } $this->_summary['next_post_id'] = $v->post_id; $meta_value = $this->_parse_wp_meta_value($v); if (!$meta_value) { continue; } $meta_value['file'] = wp_normalize_path($meta_value['file']); $basedir = $this->wp_upload_dir['basedir'] . '/'; if (strpos($meta_value['file'], $basedir) === 0) { $meta_value['file'] = substr($meta_value['file'], strlen($basedir)); } $this->tmp_pid = $v->post_id; $this->tmp_path = pathinfo($meta_value['file'], PATHINFO_DIRNAME) . '/'; $this->_append_img_queue($meta_value, true); if (!empty($meta_value['sizes'])) { array_map(array($this, '_append_img_queue'), $meta_value['sizes']); } } self::save_summary(); $num_a = count($this->_img_in_queue); self::debug('Images found: ' . $num_a); $this->_filter_duplicated_src(); self::debug('Images after duplicated: ' . count($this->_img_in_queue)); $this->_filter_invalid_src(); self::debug('Images after invalid: ' . count($this->_img_in_queue)); // Check w/ legacy imgoptm table, bypass finished images $this->_filter_legacy_src(); $num_b = count($this->_img_in_queue); if ($num_b != $num_a) { self::debug('Images after filtered duplicated/invalid/legacy src: ' . $num_b); } // Save to DB $this->_save_raw(); } // Push to Cloud server $accepted_imgs = $this->_send_request($allowance); $this->_finished_running(); if (!$accepted_imgs) { return; } $placeholder1 = Admin_Display::print_plural($accepted_imgs[0], 'image'); $placeholder2 = Admin_Display::print_plural($accepted_imgs[1], 'image'); $msg = sprintf(__('Pushed %1$s to Cloud server, accepted %2$s.', 'litespeed-cache'), $placeholder1, $placeholder2); Admin_Display::success($msg); } /** * Set running to done */ private function _finished_running() { $this->_summary['is_running'] = 0; self::save_summary(); } /** * Add a new img to queue which will be pushed to request * * @since 1.6 * @access private */ private function _append_img_queue($meta_value, $is_ori_file = false) { if (empty($meta_value['file']) || empty($meta_value['width']) || empty($meta_value['height'])) { self::debug2('bypass image due to lack of file/w/h: pid ' . $this->tmp_pid, $meta_value); return; } $short_file_path = $meta_value['file']; if (!$is_ori_file) { $short_file_path = $this->tmp_path . $short_file_path; } // Check if src is gathered already or not if (in_array($this->tmp_pid . '.' . $short_file_path, $this->_existed_src_list)) { // Debug2::debug2( '[Img_Optm] bypass image due to gathered: pid ' . $this->tmp_pid . ' ' . $short_file_path ); return; } else { // Append handled images $this->_existed_src_list[] = $this->tmp_pid . '.' . $short_file_path; } // check file exists or not $_img_info = $this->__media->info($short_file_path, $this->tmp_pid); $extension = pathinfo($short_file_path, PATHINFO_EXTENSION); if (!$_img_info || !in_array($extension, array('jpg', 'jpeg', 'png', 'gif'))) { self::debug2('bypass image due to file not exist: pid ' . $this->tmp_pid . ' ' . $short_file_path); return; } // Check if optimized file exists or not $target_needed = false; if ($this->_format) { $target_file_path = $short_file_path . '.' . $this->_format; if (!$this->__media->info($target_file_path, $this->tmp_pid)) { $target_needed = true; } } if ($this->conf(self::O_IMG_OPTM_ORI)) { $target_file_path = substr($short_file_path, 0, -strlen($extension)) . 'bk.' . $extension; if (!$this->__media->info($target_file_path, $this->tmp_pid)) { $target_needed = true; } } if (!$target_needed) { self::debug2('bypass image due to optimized file exists: pid ' . $this->tmp_pid . ' ' . $short_file_path); return; } // Debug2::debug2( '[Img_Optm] adding image: pid ' . $this->tmp_pid ); $this->_img_in_queue[] = array( 'pid' => $this->tmp_pid, 'md5' => $_img_info['md5'], 'url' => $_img_info['url'], 'src' => $short_file_path, // not needed in LiteSpeed IAPI, just leave for local storage after post 'mime_type' => !empty($meta_value['mime-type']) ? $meta_value['mime-type'] : '', ); } /** * Save gathered image raw data * * @since 3.0 */ private function _save_raw() { if (empty($this->_img_in_queue)) { return; } $data = array(); $pid_list = array(); foreach ($this->_img_in_queue as $k => $v) { $_img_info = $this->__media->info($v['src'], $v['pid']); // attachment doesn't exist, delete the record if (empty($_img_info['url']) || empty($_img_info['md5'])) { unset($this->_img_in_queue[$k]); continue; } $pid_list[] = (int) $v['pid']; $data[] = $v['pid']; $data[] = self::STATUS_RAW; $data[] = $v['src']; } global $wpdb; $fields = 'post_id, optm_status, src'; $q = "INSERT INTO `$this->_table_img_optming` ( $fields ) VALUES "; // Add placeholder $q .= Utility::chunk_placeholder($data, $fields); // Store data $wpdb->query($wpdb->prepare($q, $data)); $count = count($this->_img_in_queue); self::debug('Added raw images [total] ' . $count); $this->_img_in_queue = array(); // Save thumbnail groups for future rescan index $this->_gen_thumbnail_set(); $pid_list = array_unique($pid_list); self::debug('pid list to append to postmeta', $pid_list); $pid_list = array_diff($pid_list, $this->_pids_set); $this->_pids_set = array_merge($this->_pids_set, $pid_list); $existed_meta = $wpdb->get_results("SELECT * FROM `$wpdb->postmeta` WHERE post_id IN ('" . implode("','", $pid_list) . "') AND meta_key='" . self::DB_SET . "'"); $existed_pid = array(); if ($existed_meta) { foreach ($existed_meta as $v) { $existed_pid[] = $v->post_id; } self::debug('pid list to update postmeta', $existed_pid); $wpdb->query( $wpdb->prepare("UPDATE `$wpdb->postmeta` SET meta_value=%s WHERE post_id IN ('" . implode("','", $existed_pid) . "') AND meta_key=%s", array( $this->_thumbnail_set, self::DB_SET, )) ); } # Add new meta $new_pids = $existed_pid ? array_diff($pid_list, $existed_pid) : $pid_list; if ($new_pids) { self::debug('pid list to update postmeta', $new_pids); foreach ($new_pids as $v) { self::debug('New group set info [pid] ' . $v); $q = "INSERT INTO `$wpdb->postmeta` (post_id, meta_key, meta_value) VALUES (%d, %s, %s)"; $wpdb->query($wpdb->prepare($q, array($v, self::DB_SET, $this->_thumbnail_set))); } } } /** * Generate thumbnail sets of current image group * * @since 5.4 */ private function _gen_thumbnail_set() { if ($this->_thumbnail_set) { return; } $set = array(); foreach (Media::cls()->get_image_sizes() as $size) { $curr_size = $size['width'] . 'x' . $size['height']; if (in_array($curr_size, $set)) { continue; } $set[] = $curr_size; } $this->_thumbnail_set = implode(PHP_EOL, $set); } /** * Filter duplicated src in work table and $this->_img_in_queue, then mark them as duplicated * * @since 2.0 * @access private */ private function _filter_duplicated_src() { global $wpdb; $srcpath_list = array(); $list = $wpdb->get_results("SELECT src FROM `$this->_table_img_optming`"); foreach ($list as $v) { $srcpath_list[] = $v->src; } foreach ($this->_img_in_queue as $k => $v) { if (in_array($v['src'], $srcpath_list)) { unset($this->_img_in_queue[$k]); continue; } $srcpath_list[] = $v['src']; } } /** * Filter legacy finished ones * * @since 5.4 */ private function _filter_legacy_src() { global $wpdb; if (!$this->__data->tb_exist('img_optm')) { return; } if (!$this->_img_in_queue) { return; } $finished_ids = array(); Utility::compatibility(); $post_ids = array_unique(array_column($this->_img_in_queue, 'pid')); $list = $wpdb->get_results("SELECT post_id FROM `$this->_table_img_optm` WHERE post_id in (" . implode(',', $post_ids) . ') GROUP BY post_id'); foreach ($list as $v) { $finished_ids[] = $v->post_id; } foreach ($this->_img_in_queue as $k => $v) { if (in_array($v['pid'], $finished_ids)) { self::debug('Legacy image optimized [pid] ' . $v['pid']); unset($this->_img_in_queue[$k]); continue; } } // Drop all existing legacy records $wpdb->query("DELETE FROM `$this->_table_img_optm` WHERE post_id in (" . implode(',', $post_ids) . ')'); } /** * Filter the invalid src before sending * * @since 3.0.8.3 * @access private */ private function _filter_invalid_src() { $img_in_queue_invalid = array(); foreach ($this->_img_in_queue as $k => $v) { if ($v['src']) { $extension = pathinfo($v['src'], PATHINFO_EXTENSION); } if (!$v['src'] || empty($extension) || !in_array($extension, array('jpg', 'jpeg', 'png', 'gif'))) { $img_in_queue_invalid[] = $v['id']; unset($this->_img_in_queue[$k]); continue; } } if (!$img_in_queue_invalid) { return; } $count = count($img_in_queue_invalid); $msg = sprintf(__('Cleared %1$s invalid images.', 'litespeed-cache'), $count); Admin_Display::success($msg); self::debug('Found invalid src [total] ' . $count); } /** * Push img request to Cloud server * * @since 1.6.7 * @access private */ private function _send_request($allowance) { global $wpdb; $q = "SELECT id, src, post_id FROM `$this->_table_img_optming` WHERE optm_status=%d LIMIT %d"; $q = $wpdb->prepare($q, array(self::STATUS_RAW, $allowance)); $_img_in_queue = $wpdb->get_results($q); if (!$_img_in_queue) { return; } self::debug('Load img in queue [total] ' . count($_img_in_queue)); $list = array(); foreach ($_img_in_queue as $v) { $_img_info = $this->__media->info($v->src, $v->post_id); # If record is invalid, remove from img_optming table if (empty($_img_info['url']) || empty($_img_info['md5'])) { $wpdb->query($wpdb->prepare("DELETE FROM `$this->_table_img_optming` WHERE id=%d", $v->id)); continue; } $img = array( 'id' => $v->id, 'url' => $_img_info['url'], 'md5' => $_img_info['md5'], ); // Build the needed image types for request as we now support soft reset counter if ($this->_format) { $target_file_path = $v->src . '.' . $this->_format; if ($this->__media->info($target_file_path, $v->post_id)) { $img['optm_' . $this->_format] = 0; } } if ($this->conf(self::O_IMG_OPTM_ORI)) { $extension = pathinfo($v->src, PATHINFO_EXTENSION); $target_file_path = substr($v->src, 0, -strlen($extension)) . 'bk.' . $extension; if ($this->__media->info($target_file_path, $v->post_id)) { $img['optm_ori'] = 0; } } $list[] = $img; } if (!$list) { $msg = __('No valid image found in the current request.', 'litespeed-cache'); Admin_Display::error($msg); return; } $data = array( 'action' => self::CLOUD_ACTION_NEW_REQ, 'list' => \json_encode($list), 'optm_ori' => $this->conf(self::O_IMG_OPTM_ORI) ? 1 : 0, 'optm_lossless' => $this->conf(self::O_IMG_OPTM_LOSSLESS) ? 1 : 0, 'keep_exif' => $this->conf(self::O_IMG_OPTM_EXIF) ? 1 : 0, ); if ($this->_format) { $data['optm_' . $this->_format] = 1; } // Push to Cloud server $json = Cloud::post(Cloud::SVC_IMG_OPTM, $data); if (!$json) { return; } // Check data format if (empty($json['ids'])) { self::debug('Failed to parse response data from Cloud server ', $json); $msg = __('No valid image found by Cloud server in the current request.', 'litespeed-cache'); Admin_Display::error($msg); return; } self::debug('Returned data from Cloud server count: ' . count($json['ids'])); $ids = implode(',', array_map('intval', $json['ids'])); // Update img table $q = "UPDATE `$this->_table_img_optming` SET optm_status = '" . self::STATUS_REQUESTED . "' WHERE id IN ( $ids )"; $wpdb->query($q); $this->_summary['last_requested'] = time(); self::save_summary(); return array(count($list), count($json['ids'])); } /** * Cloud server notify Client img status changed * * @access public */ public function notify_img() { // Interval validation to avoid hacking domain_key if (!empty($this->_summary['notify_ts_err']) && time() - $this->_summary['notify_ts_err'] < 3) { return Cloud::err('too_often'); } $post_data = \json_decode(file_get_contents('php://input'), true); if (is_null($post_data)) { $post_data = $_POST; } global $wpdb; $notified_data = $post_data['data']; if (empty($notified_data) || !is_array($notified_data)) { self::debug('❌ notify exit: no notified data'); return Cloud::err('no notified data'); } if (empty($post_data['server']) || (substr($post_data['server'], -11) !== '.quic.cloud' && substr($post_data['server'], -15) !== '.quicserver.com')) { self::debug('notify exit: no/wrong server'); return Cloud::err('no/wrong server'); } if (empty($post_data['status'])) { self::debug('notify missing status'); return Cloud::err('no status'); } $status = $post_data['status']; self::debug('notified status=' . $status); $last_log_pid = 0; if (empty($this->_summary['reduced'])) { $this->_summary['reduced'] = 0; } if ($status == self::STATUS_NOTIFIED) { // Notified data format: [ img_optm_id => [ id=>, src_size=>, ori=>, ori_md5=>, ori_reduced=>, webp=>, webp_md5=>, webp_reduced=> ] ] $q = "SELECT a.*, b.meta_id as b_meta_id, b.meta_value AS b_optm_info FROM `$this->_table_img_optming` a LEFT JOIN `$wpdb->postmeta` b ON b.post_id = a.post_id AND b.meta_key = %s WHERE a.id IN ( " . implode(',', array_fill(0, count($notified_data), '%d')) . ' )'; $list = $wpdb->get_results($wpdb->prepare($q, array_merge(array(self::DB_SIZE), array_keys($notified_data)))); $ls_optm_size_row_exists_postids = array(); foreach ($list as $v) { $json = $notified_data[$v->id]; // self::debug('Notified data for [id] ' . $v->id, $json); $server = !empty($json['server']) ? $json['server'] : $post_data['server']; $server_info = array( 'server' => $server, ); // Save server side ID to send taken notification after pulled $server_info['id'] = $json['id']; if (!empty($json['file_id'])) { $server_info['file_id'] = $json['file_id']; } // Optm info array $postmeta_info = array( 'ori_total' => 0, 'ori_saved' => 0, 'webp_total' => 0, 'webp_saved' => 0, 'avif_total' => 0, 'avif_saved' => 0, ); // Init postmeta_info for the first one if (!empty($v->b_meta_id)) { foreach (maybe_unserialize($v->b_optm_info) as $k2 => $v2) { $postmeta_info[$k2] += $v2; } } if (!empty($json['ori'])) { $server_info['ori_md5'] = $json['ori_md5']; $server_info['ori'] = $json['ori']; // Append meta info $postmeta_info['ori_total'] += $json['src_size']; $postmeta_info['ori_saved'] += $json['ori_reduced']; // optimized image size info in img_optm tb will be updated when pull $this->_summary['reduced'] += $json['ori_reduced']; } if (!empty($json['webp'])) { $server_info['webp_md5'] = $json['webp_md5']; $server_info['webp'] = $json['webp']; // Append meta info $postmeta_info['webp_total'] += $json['src_size']; $postmeta_info['webp_saved'] += $json['webp_reduced']; $this->_summary['reduced'] += $json['webp_reduced']; } if (!empty($json['avif'])) { $server_info['avif_md5'] = $json['avif_md5']; $server_info['avif'] = $json['avif']; // Append meta info $postmeta_info['avif_total'] += $json['src_size']; $postmeta_info['avif_saved'] += $json['avif_reduced']; $this->_summary['reduced'] += $json['avif_reduced']; } // Update status and data in working table $q = "UPDATE `$this->_table_img_optming` SET optm_status = %d, server_info = %s WHERE id = %d "; $wpdb->query($wpdb->prepare($q, array($status, \json_encode($server_info), $v->id))); // Update postmeta for optm summary $postmeta_info = serialize($postmeta_info); if (empty($v->b_meta_id) && !in_array($v->post_id, $ls_optm_size_row_exists_postids)) { self::debug('New size info [pid] ' . $v->post_id); $q = "INSERT INTO `$wpdb->postmeta` ( post_id, meta_key, meta_value ) VALUES ( %d, %s, %s )"; $wpdb->query($wpdb->prepare($q, array($v->post_id, self::DB_SIZE, $postmeta_info))); $ls_optm_size_row_exists_postids[] = $v->post_id; } else { $q = "UPDATE `$wpdb->postmeta` SET meta_value = %s WHERE meta_id = %d "; $wpdb->query($wpdb->prepare($q, array($postmeta_info, $v->b_meta_id))); } // write log $pid_log = $last_log_pid == $v->post_id ? '.' : $v->post_id; self::debug('notify_img [status] ' . $status . " \t\t[pid] " . $pid_log . " \t\t[id] " . $v->id); $last_log_pid = $v->post_id; } self::save_summary(); // Mark need_pull tag for cron self::update_option(self::DB_NEED_PULL, self::STATUS_NOTIFIED); } else { // Other errors will directly remove the working records // Delete from working table $q = "DELETE FROM `$this->_table_img_optming` WHERE id IN ( " . implode(',', array_fill(0, count($notified_data), '%d')) . ' ) '; $wpdb->query($wpdb->prepare($q, $notified_data)); } return Cloud::ok(array('count' => count($notified_data))); } /** * Cron start async req * * @since 5.5 */ public static function start_async_cron() { Task::async_call('imgoptm'); } /** * Manually start async req * * @since 5.5 */ public static function start_async() { Task::async_call('imgoptm_force'); $msg = __('Started async image optimization request', 'litespeed-cache'); Admin_Display::success($msg); } /** * Ajax req handler * * @since 5.5 */ public static function async_handler($force = false) { self::debug('------------async-------------start_async_handler'); $tag = self::get_option(self::DB_NEED_PULL); if (!$tag || $tag != self::STATUS_NOTIFIED) { self::debug('❌ no need pull [tag] ' . $tag); return; } if (defined('LITESPEED_IMG_OPTM_PULL_CRON') && !LITESPEED_IMG_OPTM_PULL_CRON) { self::debug('Cron disabled [define] LITESPEED_IMG_OPTM_PULL_CRON'); return; } self::cls()->pull($force); } /** * Calculate pull threads * * @since 5.8 * @access private */ private function _calc_pull_threads() { global $wpdb; if (defined('LITESPEED_IMG_OPTM_PULL_THREADS')) { return LITESPEED_IMG_OPTM_PULL_THREADS; } // Tune number of images per request based on number of images waiting and cloud packages $imgs_per_req = 1; // base 1, ramp up to ~50 max // Ramp up the request rate based on how many images are waiting $c = "SELECT count(id) FROM `$this->_table_img_optming` WHERE optm_status = %d"; $_c = $wpdb->prepare($c, array(self::STATUS_NOTIFIED)); $images_waiting = $wpdb->get_var($_c); if ($images_waiting && $images_waiting > 0) { $imgs_per_req = ceil($images_waiting / 1000); //ie. download 5/request if 5000 images are waiting } // Cap the request rate at 50 images per request $imgs_per_req = min(50, $imgs_per_req); self::debug('Pulling images at rate: ' . $imgs_per_req . ' Images per request.'); return $imgs_per_req; } /** * Pull optimized img * * @since 1.6 * @access public */ public function pull($manual = false) { global $wpdb; $timeoutLimit = ini_get('max_execution_time'); $endts = time() + $timeoutLimit; self::debug('' . ($manual ? 'Manually' : 'Cron') . ' pull started [timeout: ' . $timeoutLimit . 's]'); if ($this->cron_running()) { self::debug('Pull cron is running'); $msg = __('Pull Cron is running', 'litespeed-cache'); Admin_Display::note($msg); return; } $this->_summary['last_pulled'] = time(); $this->_summary['last_pulled_by_cron'] = !$manual; self::save_summary(); $imgs_per_req = $this->_calc_pull_threads(); $q = "SELECT * FROM `$this->_table_img_optming` WHERE optm_status = %d ORDER BY id LIMIT %d"; $_q = $wpdb->prepare($q, array(self::STATUS_NOTIFIED, $imgs_per_req)); $rm_ori_bkup = $this->conf(self::O_IMG_OPTM_RM_BKUP); $total_pulled_ori = 0; $total_pulled_webp = 0; $total_pulled_avif = 0; $server_list = array(); try { while ($img_rows = $wpdb->get_results($_q)) { self::debug('timeout left: ' . ($endts - time()) . 's'); if (function_exists('set_time_limit')) { $endts += 600; self::debug('Endtime extended to ' . date('Ymd H:i:s', $endts)); set_time_limit(600); // This will be no more important as we use noabort now } // Disabled as we use noabort // if ($endts - time() < 10) { // self::debug("🚨 End loop due to timeout limit reached " . $timeoutLimit . "s"); // break; // } /** * Update cron timestamp to avoid duplicated running * @since 1.6.2 */ $this->_update_cron_running(); // Run requests in parallel $requests = array(); // store each request URL for Requests::request_multiple() $imgs_by_req = array(); // store original request data so that we can reference it in the response $req_counter = 0; foreach ($img_rows as $row_img) { // request original image $server_info = \json_decode($row_img->server_info, true); if (!empty($server_info['ori'])) { $image_url = $server_info['server'] . '/' . $server_info['ori']; self::debug('Queueing pull: ' . $image_url); $requests[$req_counter] = array( 'url' => $image_url, 'type' => 'GET', ); $imgs_by_req[$req_counter++] = array( 'type' => 'ori', 'data' => $row_img, ); } // request webp image $webp_size = 0; if (!empty($server_info['webp'])) { $image_url = $server_info['server'] . '/' . $server_info['webp']; self::debug('Queueing pull WebP: ' . $image_url); $requests[$req_counter] = array( 'url' => $image_url, 'type' => 'GET', ); $imgs_by_req[$req_counter++] = array( 'type' => 'webp', 'data' => $row_img, ); } // request avif image $avif_size = 0; if (!empty($server_info['avif'])) { $image_url = $server_info['server'] . '/' . $server_info['avif']; self::debug('Queueing pull AVIF: ' . $image_url); $requests[$req_counter] = array( 'url' => $image_url, 'type' => 'GET', ); $imgs_by_req[$req_counter++] = array( 'type' => 'avif', 'data' => $row_img, ); } } self::debug('Loaded images count: ' . $req_counter); $complete_action = function ($response, $req_count) use ($imgs_by_req, $rm_ori_bkup, &$total_pulled_ori, &$total_pulled_webp, &$total_pulled_avif, &$server_list) { global $wpdb; $row_data = isset($imgs_by_req[$req_count]) ? $imgs_by_req[$req_count] : false; if (false === $row_data) { self::debug('❌ failed to pull image: Request not found in lookup variable.'); return; } $row_type = isset($row_data['type']) ? $row_data['type'] : 'ori'; $row_img = $row_data['data']; $local_file = $this->wp_upload_dir['basedir'] . '/' . $row_img->src; $server_info = \json_decode($row_img->server_info, true); if (empty($response->success)) { if (!empty($response->status_code) && 404 == $response->status_code) { $this->_step_back_image($row_img->id); $msg = __('Some optimized image file(s) has expired and was cleared.', 'litespeed-cache'); Admin_Display::error($msg); return; } else { // handle error $image_url = $server_info['server'] . '/' . $server_info[$row_type]; self::debug( '❌ failed to pull image (' . $row_type . '): ' . (!empty($response->status_code) ? $response->status_code : '') . ' [Local: ' . $row_img->src . '] / [remote: ' . $image_url . ']' ); throw new \Exception('Failed to pull image ' . (!empty($response->status_code) ? $response->status_code : '') . ' [url] ' . $image_url); return; } } // Handle wp_remote_get 404 as its success=true if (!empty($response->status_code)) { if ($response->status_code == 404) { $this->_step_back_image($row_img->id); $msg = __('Some optimized image file(s) has expired and was cleared.', 'litespeed-cache'); Admin_Display::error($msg); return; } // Note: if there is other error status code found in future, handle here } if ('webp' === $row_type) { file_put_contents($local_file . '.webp', $response->body); if (!file_exists($local_file . '.webp') || !filesize($local_file . '.webp') || md5_file($local_file . '.webp') !== $server_info['webp_md5']) { self::debug('❌ Failed to pull optimized webp img: file md5 mismatch, server md5: ' . $server_info['webp_md5']); // Delete working table $q = "DELETE FROM `$this->_table_img_optming` WHERE id = %d "; $wpdb->query($wpdb->prepare($q, $row_img->id)); $msg = __('Pulled WebP image md5 does not match the notified WebP image md5.', 'litespeed-cache'); Admin_Display::error($msg); return; } self::debug('Pulled optimized img WebP: ' . $local_file . '.webp'); $webp_size = filesize($local_file . '.webp'); /** * API for WebP * @since 2.9.5 * @since 3.0 $row_img less elements (see above one) * @see #751737 - API docs for WEBP generation */ do_action('litespeed_img_pull_webp', $row_img, $local_file . '.webp'); $total_pulled_webp++; } elseif ('avif' === $row_type) { file_put_contents($local_file . '.avif', $response->body); if (!file_exists($local_file . '.avif') || !filesize($local_file . '.avif') || md5_file($local_file . '.avif') !== $server_info['avif_md5']) { self::debug('❌ Failed to pull optimized avif img: file md5 mismatch, server md5: ' . $server_info['avif_md5']); // Delete working table $q = "DELETE FROM `$this->_table_img_optming` WHERE id = %d "; $wpdb->query($wpdb->prepare($q, $row_img->id)); $msg = __('Pulled AVIF image md5 does not match the notified AVIF image md5.', 'litespeed-cache'); Admin_Display::error($msg); return; } self::debug('Pulled optimized img AVIF: ' . $local_file . '.avif'); $avif_size = filesize($local_file . '.avif'); /** * API for AVIF * @since 7.0 */ do_action('litespeed_img_pull_avif', $row_img, $local_file . '.avif'); $total_pulled_avif++; } else { // "ori" image type file_put_contents($local_file . '.tmp', $response->body); if (!file_exists($local_file . '.tmp') || !filesize($local_file . '.tmp') || md5_file($local_file . '.tmp') !== $server_info['ori_md5']) { self::debug( '❌ Failed to pull optimized img: file md5 mismatch [url] ' . $server_info['server'] . '/' . $server_info['ori'] . ' [server_md5] ' . $server_info['ori_md5'] ); // Delete working table $q = "DELETE FROM `$this->_table_img_optming` WHERE id = %d "; $wpdb->query($wpdb->prepare($q, $row_img->id)); $msg = __('One or more pulled images does not match with the notified image md5', 'litespeed-cache'); Admin_Display::error($msg); return; } // Backup ori img if (!$rm_ori_bkup) { $extension = pathinfo($local_file, PATHINFO_EXTENSION); $bk_file = substr($local_file, 0, -strlen($extension)) . 'bk.' . $extension; file_exists($local_file) && rename($local_file, $bk_file); } // Replace ori img rename($local_file . '.tmp', $local_file); self::debug('Pulled optimized img: ' . $local_file); /** * API Hook * @since 2.9.5 * @since 3.0 $row_img has less elements now. Most useful ones are `post_id`/`src` */ do_action('litespeed_img_pull_ori', $row_img, $local_file); self::debug2('Remove _table_img_optming record [id] ' . $row_img->id); } // Delete working table $q = "DELETE FROM `$this->_table_img_optming` WHERE id = %d "; $wpdb->query($wpdb->prepare($q, $row_img->id)); // Save server_list to notify taken if (empty($server_list[$server_info['server']])) { $server_list[$server_info['server']] = array(); } $server_info_id = !empty($server_info['file_id']) ? $server_info['file_id'] : $server_info['id']; $server_list[$server_info['server']][] = $server_info_id; $total_pulled_ori++; }; $force_wp_remote_get = defined('LITESPEED_FORCE_WP_REMOTE_GET') && LITESPEED_FORCE_WP_REMOTE_GET; if (!$force_wp_remote_get && class_exists('\WpOrg\Requests\Requests') && class_exists('\WpOrg\Requests\Autoload') && version_compare(PHP_VERSION, '5.6.0', '>=')) { // Make sure Requests can load internal classes. Autoload::register(); // Run pull requests in parallel Requests::request_multiple($requests, array( 'timeout' => 60, 'connect_timeout' => 60, 'complete' => $complete_action, )); } else { foreach ($requests as $cnt => $req) { $wp_response = wp_safe_remote_get($req['url'], array('timeout' => 60)); $request_response = array( 'success' => false, 'status_code' => 0, 'body' => null, ); if (is_wp_error($wp_response)) { $error_message = $wp_response->get_error_message(); self::debug('❌ failed to pull image: ' . $error_message); } else { $request_response['success'] = true; $request_response['status_code'] = $wp_response['response']['code']; $request_response['body'] = $wp_response['body']; } self::debug('response code [code] ' . $wp_response['response']['code'] . ' [url] ' . $req['url']); $request_response = (object) $request_response; $complete_action($request_response, $cnt); } } self::debug('Current batch pull finished'); } } catch (\Exception $e) { Admin_Display::error('Image pull process failure: ' . $e->getMessage()); } // Notify IAPI images taken foreach ($server_list as $server => $img_list) { $data = array( 'action' => self::CLOUD_ACTION_TAKEN, 'list' => $img_list, 'server' => $server, ); // TODO: improve this so we do not call once per server, but just once and then filter on the server side Cloud::post(Cloud::SVC_IMG_OPTM, $data); } if (empty($this->_summary['img_taken'])) { $this->_summary['img_taken'] = 0; } $this->_summary['img_taken'] += $total_pulled_ori + $total_pulled_webp + $total_pulled_avif; self::save_summary(); // Manually running needs to roll back timestamp for next running if ($manual) { $this->_update_cron_running(true); } // $msg = sprintf(__('Pulled %d image(s)', 'litespeed-cache'), $total_pulled_ori + $total_pulled_webp); // Admin_Display::success($msg); // Check if there is still task in queue $q = "SELECT * FROM `$this->_table_img_optming` WHERE optm_status = %d LIMIT 1"; $to_be_continued = $wpdb->get_row($wpdb->prepare($q, self::STATUS_NOTIFIED)); if ($to_be_continued) { self::debug('Task in queue, to be continued...'); return; // return Router::self_redirect(Router::ACTION_IMG_OPTM, self::TYPE_PULL); } // If all pulled, update tag to done self::debug('Marked pull status to all pulled'); self::update_option(self::DB_NEED_PULL, self::STATUS_PULLED); } /** * Push image back to previous status * * @since 3.0 * @access private */ private function _step_back_image($id) { global $wpdb; self::debug('Push image back to new status [id] ' . $id); // Reset the image to gathered status $q = "UPDATE `$this->_table_img_optming` SET optm_status = %d WHERE id = %d "; $wpdb->query($wpdb->prepare($q, array(self::STATUS_RAW, $id))); } /** * Parse wp's meta value * * @since 1.6.7 * @access private */ private function _parse_wp_meta_value($v) { if (empty($v)) { self::debug('bypassed parsing meta due to null value'); return false; } if (!$v->meta_value) { self::debug('bypassed parsing meta due to no meta_value: pid ' . $v->post_id); return false; } $meta_value = @maybe_unserialize($v->meta_value); if (!is_array($meta_value)) { self::debug('bypassed parsing meta due to meta_value not json: pid ' . $v->post_id); return false; } if (empty($meta_value['file'])) { self::debug('bypassed parsing meta due to no ori file: pid ' . $v->post_id); return false; } return $meta_value; } /** * Clean up all unfinished queue locally and to Cloud server * * @since 2.1.2 * @access public */ public function clean() { global $wpdb; // Reset img_optm table's queue if ($this->__data->tb_exist('img_optming')) { // Get min post id to mark $q = "SELECT MIN(post_id) FROM `$this->_table_img_optming`"; $min_pid = $wpdb->get_var($q) - 1; if ($this->_summary['next_post_id'] > $min_pid) { $this->_summary['next_post_id'] = $min_pid; self::save_summary(); } $q = "DELETE FROM `$this->_table_img_optming`"; $wpdb->query($q); } $msg = __('Cleaned up unfinished data successfully.', 'litespeed-cache'); Admin_Display::success($msg); } /** * Reset image counter * * @since 7.0 * @access private */ private function _reset_counter() { self::debug('reset image optm counter'); $this->_summary['next_post_id'] = 0; self::save_summary(); $this->clean(); $msg = __('Reset image optimization counter successfully.', 'litespeed-cache'); Admin_Display::success($msg); } /** * Destroy all optimized images * * @since 3.0 * @access private */ private function _destroy() { global $wpdb; self::debug('executing DESTROY process'); $offset = !empty($_GET['litespeed_i']) ? $_GET['litespeed_i'] : 0; /** * Limit images each time before redirection to fix Out of memory issue. #665465 * @since 2.9.8 */ // Start deleting files $limit = apply_filters('litespeed_imgoptm_destroy_max_rows', 500); $img_q = "SELECT b.post_id, b.meta_value FROM `$wpdb->posts` a LEFT JOIN `$wpdb->postmeta` b ON b.post_id = a.ID WHERE b.meta_key = '_wp_attachment_metadata' AND a.post_type = 'attachment' AND a.post_status = 'inherit' AND a.post_mime_type IN ('image/jpeg', 'image/png', 'image/gif') ORDER BY a.ID LIMIT %d,%d "; $q = $wpdb->prepare($img_q, array($offset * $limit, $limit)); $list = $wpdb->get_results($q); $i = 0; foreach ($list as $v) { if (!$v->post_id) { continue; } $meta_value = $this->_parse_wp_meta_value($v); if (!$meta_value) { continue; } $i++; $this->tmp_pid = $v->post_id; $this->tmp_path = pathinfo($meta_value['file'], PATHINFO_DIRNAME) . '/'; $this->_destroy_optm_file($meta_value, true); if (!empty($meta_value['sizes'])) { array_map(array($this, '_destroy_optm_file'), $meta_value['sizes']); } } self::debug('batch switched images total: ' . $i); $offset++; $to_be_continued = $wpdb->get_row($wpdb->prepare($img_q, array($offset * $limit, 1))); if ($to_be_continued) { # Check if post_id is beyond next_post_id self::debug('[next_post_id] ' . $this->_summary['next_post_id'] . ' [cursor post id] ' . $to_be_continued->post_id); if ($to_be_continued->post_id <= $this->_summary['next_post_id']) { self::debug('redirecting to next'); return Router::self_redirect(Router::ACTION_IMG_OPTM, self::TYPE_DESTROY); } self::debug('🎊 Finished destroying'); } // Delete postmeta info $q = "DELETE FROM `$wpdb->postmeta` WHERE meta_key = %s"; $wpdb->query($wpdb->prepare($q, self::DB_SIZE)); $wpdb->query($wpdb->prepare($q, self::DB_SET)); // Delete img_optm table $this->__data->tb_del('img_optm'); $this->__data->tb_del('img_optming'); // Clear options table summary info self::delete_option('_summary'); self::delete_option(self::DB_NEED_PULL); $msg = __('Destroy all optimization data successfully.', 'litespeed-cache'); Admin_Display::success($msg); } /** * Destroy optm file */ private function _destroy_optm_file($meta_value, $is_ori_file = false) { $short_file_path = $meta_value['file']; if (!$is_ori_file) { $short_file_path = $this->tmp_path . $short_file_path; } self::debug('deleting ' . $short_file_path); // del webp $this->__media->info($short_file_path . '.webp', $this->tmp_pid) && $this->__media->del($short_file_path . '.webp', $this->tmp_pid); $this->__media->info($short_file_path . '.optm.webp', $this->tmp_pid) && $this->__media->del($short_file_path . '.optm.webp', $this->tmp_pid); // del avif $this->__media->info($short_file_path . '.avif', $this->tmp_pid) && $this->__media->del($short_file_path . '.avif', $this->tmp_pid); $this->__media->info($short_file_path . '.optm.avif', $this->tmp_pid) && $this->__media->del($short_file_path . '.optm.avif', $this->tmp_pid); $extension = pathinfo($short_file_path, PATHINFO_EXTENSION); $local_filename = substr($short_file_path, 0, -strlen($extension) - 1); $bk_file = $local_filename . '.bk.' . $extension; $bk_optm_file = $local_filename . '.bk.optm.' . $extension; // del optimized ori if ($this->__media->info($bk_file, $this->tmp_pid)) { self::debug('deleting optim ori'); $this->__media->del($short_file_path, $this->tmp_pid); $this->__media->rename($bk_file, $short_file_path, $this->tmp_pid); } $this->__media->info($bk_optm_file, $this->tmp_pid) && $this->__media->del($bk_optm_file, $this->tmp_pid); } /** * Rescan to find new generated images * * @since 1.6.7 * @access private */ private function _rescan() { global $wpdb; exit('tobedone'); $offset = !empty($_GET['litespeed_i']) ? $_GET['litespeed_i'] : 0; $limit = 500; self::debug('rescan images'); // Get images $q = "SELECT b.post_id, b.meta_value FROM `$wpdb->posts` a, `$wpdb->postmeta` b WHERE a.post_type = 'attachment' AND a.post_status = 'inherit' AND a.post_mime_type IN ('image/jpeg', 'image/png', 'image/gif') AND a.ID = b.post_id AND b.meta_key = '_wp_attachment_metadata' ORDER BY a.ID LIMIT %d, %d "; $list = $wpdb->get_results($wpdb->prepare($q, $offset * $limit, $limit + 1)); // last one is the seed for next batch if (!$list) { $msg = __('Rescanned successfully.', 'litespeed-cache'); Admin_Display::success($msg); self::debug('rescan bypass: no gathered image found'); return; } if (count($list) == $limit + 1) { $to_be_continued = true; array_pop($list); // last one is the seed for next round, discard here. } else { $to_be_continued = false; } // Prepare post_ids to inquery gathered images $pid_set = array(); $scanned_list = array(); foreach ($list as $v) { $meta_value = $this->_parse_wp_meta_value($v); if (!$meta_value) { continue; } $scanned_list[] = array( 'pid' => $v->post_id, 'meta' => $meta_value, ); $pid_set[] = $v->post_id; } // Build gathered images $q = "SELECT src, post_id FROM `$this->_table_img_optm` WHERE post_id IN (" . implode(',', array_fill(0, count($pid_set), '%d')) . ')'; $list = $wpdb->get_results($wpdb->prepare($q, $pid_set)); foreach ($list as $v) { $this->_existed_src_list[] = $v->post_id . '.' . $v->src; } // Find new images foreach ($scanned_list as $v) { $meta_value = $v['meta']; // Parse all child src and put them into $this->_img_in_queue, missing ones to $this->_img_in_queue_missed $this->tmp_pid = $v['pid']; $this->tmp_path = pathinfo($meta_value['file'], PATHINFO_DIRNAME) . '/'; $this->_append_img_queue($meta_value, true); if (!empty($meta_value['sizes'])) { array_map(array($this, '_append_img_queue'), $meta_value['sizes']); } } self::debug('rescanned [img] ' . count($this->_img_in_queue)); $count = count($this->_img_in_queue); if ($count > 0) { // Save to DB $this->_save_raw(); } if ($to_be_continued) { return Router::self_redirect(Router::ACTION_IMG_OPTM, self::TYPE_RESCAN); } $msg = $count ? sprintf(__('Rescanned %d images successfully.', 'litespeed-cache'), $count) : __('Rescanned successfully.', 'litespeed-cache'); Admin_Display::success($msg); } /** * Calculate bkup original images storage * * @since 2.2.6 * @access private */ private function _calc_bkup() { global $wpdb; $offset = !empty($_GET['litespeed_i']) ? $_GET['litespeed_i'] : 0; $limit = 500; if (!$offset) { $this->_summary['bk_summary'] = array( 'date' => time(), 'count' => 0, 'sum' => 0, ); } $img_q = "SELECT b.post_id, b.meta_value FROM `$wpdb->posts` a LEFT JOIN `$wpdb->postmeta` b ON b.post_id = a.ID WHERE b.meta_key = '_wp_attachment_metadata' AND a.post_type = 'attachment' AND a.post_status = 'inherit' AND a.post_mime_type IN ('image/jpeg', 'image/png', 'image/gif') ORDER BY a.ID LIMIT %d,%d "; $q = $wpdb->prepare($img_q, array($offset * $limit, $limit)); $list = $wpdb->get_results($q); foreach ($list as $v) { if (!$v->post_id) { continue; } $meta_value = $this->_parse_wp_meta_value($v); if (!$meta_value) { continue; } $this->tmp_pid = $v->post_id; $this->tmp_path = pathinfo($meta_value['file'], PATHINFO_DIRNAME) . '/'; $this->_get_bk_size($meta_value, true); if (!empty($meta_value['sizes'])) { array_map(array($this, '_get_bk_size'), $meta_value['sizes']); } } $this->_summary['bk_summary']['date'] = time(); self::save_summary(); self::debug('_calc_bkup total: ' . $this->_summary['bk_summary']['count'] . ' [size] ' . $this->_summary['bk_summary']['sum']); $offset++; $to_be_continued = $wpdb->get_row($wpdb->prepare($img_q, array($offset * $limit, 1))); if ($to_be_continued) { return Router::self_redirect(Router::ACTION_IMG_OPTM, self::TYPE_CALC_BKUP); } $msg = __('Calculated backups successfully.', 'litespeed-cache'); Admin_Display::success($msg); } /** * Calculate single size */ private function _get_bk_size($meta_value, $is_ori_file = false) { $short_file_path = $meta_value['file']; if (!$is_ori_file) { $short_file_path = $this->tmp_path . $short_file_path; } $extension = pathinfo($short_file_path, PATHINFO_EXTENSION); $local_filename = substr($short_file_path, 0, -strlen($extension) - 1); $bk_file = $local_filename . '.bk.' . $extension; $img_info = $this->__media->info($bk_file, $this->tmp_pid); if (!$img_info) { return; } $this->_summary['bk_summary']['count']++; $this->_summary['bk_summary']['sum'] += $img_info['size']; } /** * Delete bkup original images storage * * @since 2.5 * @access public */ public function rm_bkup() { global $wpdb; if (!$this->__data->tb_exist('img_optming')) { return; } $offset = !empty($_GET['litespeed_i']) ? $_GET['litespeed_i'] : 0; $limit = 500; if (empty($this->_summary['rmbk_summary'])) { $this->_summary['rmbk_summary'] = array( 'date' => time(), 'count' => 0, 'sum' => 0, ); } $img_q = "SELECT b.post_id, b.meta_value FROM `$wpdb->posts` a LEFT JOIN `$wpdb->postmeta` b ON b.post_id = a.ID WHERE b.meta_key = '_wp_attachment_metadata' AND a.post_type = 'attachment' AND a.post_status = 'inherit' AND a.post_mime_type IN ('image/jpeg', 'image/png', 'image/gif') ORDER BY a.ID LIMIT %d,%d "; $q = $wpdb->prepare($img_q, array($offset * $limit, $limit)); $list = $wpdb->get_results($q); foreach ($list as $v) { if (!$v->post_id) { continue; } $meta_value = $this->_parse_wp_meta_value($v); if (!$meta_value) { continue; } $this->tmp_pid = $v->post_id; $this->tmp_path = pathinfo($meta_value['file'], PATHINFO_DIRNAME) . '/'; $this->_del_bk_file($meta_value, true); if (!empty($meta_value['sizes'])) { array_map(array($this, '_del_bk_file'), $meta_value['sizes']); } } $this->_summary['rmbk_summary']['date'] = time(); self::save_summary(); self::debug('rm_bkup total: ' . $this->_summary['rmbk_summary']['count'] . ' [size] ' . $this->_summary['rmbk_summary']['sum']); $offset++; $to_be_continued = $wpdb->get_row($wpdb->prepare($img_q, array($offset * $limit, 1))); if ($to_be_continued) { return Router::self_redirect(Router::ACTION_IMG_OPTM, self::TYPE_RM_BKUP); } $msg = __('Removed backups successfully.', 'litespeed-cache'); Admin_Display::success($msg); } /** * Delete single file */ private function _del_bk_file($meta_value, $is_ori_file = false) { $short_file_path = $meta_value['file']; if (!$is_ori_file) { $short_file_path = $this->tmp_path . $short_file_path; } $extension = pathinfo($short_file_path, PATHINFO_EXTENSION); $local_filename = substr($short_file_path, 0, -strlen($extension) - 1); $bk_file = $local_filename . '.bk.' . $extension; $img_info = $this->__media->info($bk_file, $this->tmp_pid); if (!$img_info) { return; } $this->_summary['rmbk_summary']['count']++; $this->_summary['rmbk_summary']['sum'] += $img_info['size']; $this->__media->del($bk_file, $this->tmp_pid); } /** * Count images * * @since 1.6 * @access public */ public function img_count() { global $wpdb; $q = "SELECT count(*) FROM `$wpdb->posts` a LEFT JOIN `$wpdb->postmeta` b ON b.post_id = a.ID WHERE b.meta_key = '_wp_attachment_metadata' AND a.post_type = 'attachment' AND a.post_status = 'inherit' AND a.post_mime_type IN ('image/jpeg', 'image/png', 'image/gif') "; $groups_all = $wpdb->get_var($q); $groups_new = $wpdb->get_var($q . ' AND ID>' . (int) $this->_summary['next_post_id'] . ' ORDER BY ID'); $groups_done = $wpdb->get_var($q . ' AND ID<=' . (int) $this->_summary['next_post_id'] . ' ORDER BY ID'); $q = "SELECT b.post_id FROM `$wpdb->posts` a LEFT JOIN `$wpdb->postmeta` b ON b.post_id = a.ID WHERE b.meta_key = '_wp_attachment_metadata' AND a.post_type = 'attachment' AND a.post_status = 'inherit' AND a.post_mime_type IN ('image/jpeg', 'image/png', 'image/gif') ORDER BY a.ID DESC LIMIT 1 "; $max_id = $wpdb->get_var($q); $count_list = array( 'max_id' => $max_id, 'groups_all' => $groups_all, 'groups_new' => $groups_new, 'groups_done' => $groups_done, ); // images count from work table if ($this->__data->tb_exist('img_optming')) { $q = "SELECT COUNT(DISTINCT post_id),COUNT(*) FROM `$this->_table_img_optming` WHERE optm_status = %d"; $groups_to_check = array(self::STATUS_RAW, self::STATUS_REQUESTED, self::STATUS_NOTIFIED, self::STATUS_ERR_FETCH); foreach ($groups_to_check as $v) { $count_list['img.' . $v] = $count_list['group.' . $v] = 0; list($count_list['group.' . $v], $count_list['img.' . $v]) = $wpdb->get_row($wpdb->prepare($q, $v), ARRAY_N); } } return $count_list; } /** * Check if fetch cron is running * * @since 1.6.2 * @access public */ public function cron_running($bool_res = true) { $last_run = !empty($this->_summary['last_pull']) ? $this->_summary['last_pull'] : 0; $is_running = $last_run && time() - $last_run < 120; if ($bool_res) { return $is_running; } return array($last_run, $is_running); } /** * Update fetch cron timestamp tag * * @since 1.6.2 * @access private */ private function _update_cron_running($done = false) { $this->_summary['last_pull'] = time(); if ($done) { // Only update cron tag when its from the active running cron if ($this->_cron_ran) { // Rollback for next running $this->_summary['last_pull'] -= 120; } else { return; } } self::save_summary(); $this->_cron_ran = true; } /** * Batch switch images to ori/optm version * * @since 1.6.2 * @access public */ public function batch_switch($type) { global $wpdb; if (defined('LITESPEED_CLI') || defined('DOING_CRON')) { $offset = 0; while ($offset !== 'done') { Admin_Display::info("Starting switch to $type [offset] $offset"); $offset = $this->_batch_switch($type, $offset); } } else { $offset = !empty($_GET['litespeed_i']) ? $_GET['litespeed_i'] : 0; $newOffset = $this->_batch_switch($type, $offset); if ($newOffset !== 'done') { return Router::self_redirect(Router::ACTION_IMG_OPTM, $type); } } $msg = __('Switched images successfully.', 'litespeed-cache'); Admin_Display::success($msg); } /** * Switch images per offset */ private function _batch_switch($type, $offset) { global $wpdb; $limit = 500; $this->tmp_type = $type; $img_q = "SELECT b.post_id, b.meta_value FROM `$wpdb->posts` a LEFT JOIN `$wpdb->postmeta` b ON b.post_id = a.ID WHERE b.meta_key = '_wp_attachment_metadata' AND a.post_type = 'attachment' AND a.post_status = 'inherit' AND a.post_mime_type IN ('image/jpeg', 'image/png', 'image/gif') ORDER BY a.ID LIMIT %d,%d "; $q = $wpdb->prepare($img_q, array($offset * $limit, $limit)); $list = $wpdb->get_results($q); $i = 0; foreach ($list as $v) { if (!$v->post_id) { continue; } $meta_value = $this->_parse_wp_meta_value($v); if (!$meta_value) { continue; } $i++; $this->tmp_pid = $v->post_id; $this->tmp_path = pathinfo($meta_value['file'], PATHINFO_DIRNAME) . '/'; $this->_switch_bk_file($meta_value, true); if (!empty($meta_value['sizes'])) { array_map(array($this, '_switch_bk_file'), $meta_value['sizes']); } } self::debug('batch switched images total: ' . $i . ' [type] ' . $type); $offset++; $to_be_continued = $wpdb->get_row($wpdb->prepare($img_q, array($offset * $limit, 1))); if ($to_be_continued) { return $offset; } return 'done'; } /** * Delete single file */ private function _switch_bk_file($meta_value, $is_ori_file = false) { $short_file_path = $meta_value['file']; if (!$is_ori_file) { $short_file_path = $this->tmp_path . $short_file_path; } $extension = pathinfo($short_file_path, PATHINFO_EXTENSION); $local_filename = substr($short_file_path, 0, -strlen($extension) - 1); $bk_file = $local_filename . '.bk.' . $extension; $bk_optm_file = $local_filename . '.bk.optm.' . $extension; // self::debug('_switch_bk_file ' . $bk_file . ' [type] ' . $this->tmp_type); // switch to ori if ($this->tmp_type === self::TYPE_BATCH_SWITCH_ORI || $this->tmp_type == 'orig') { // self::debug('switch to orig ' . $bk_file); if (!$this->__media->info($bk_file, $this->tmp_pid)) { return; } $this->__media->rename($local_filename . '.' . $extension, $bk_optm_file, $this->tmp_pid); $this->__media->rename($bk_file, $local_filename . '.' . $extension, $this->tmp_pid); } // switch to optm elseif ($this->tmp_type === self::TYPE_BATCH_SWITCH_OPTM || $this->tmp_type == 'optm') { // self::debug('switch to optm ' . $bk_file); if (!$this->__media->info($bk_optm_file, $this->tmp_pid)) { return; } $this->__media->rename($local_filename . '.' . $extension, $bk_file, $this->tmp_pid); $this->__media->rename($bk_optm_file, $local_filename . '.' . $extension, $this->tmp_pid); } } /** * Switch image between original one and optimized one * * @since 1.6.2 * @access private */ private function _switch_optm_file($type) { Admin_Display::success(__('Switched to optimized file successfully.', 'litespeed-cache')); return; global $wpdb; $pid = substr($type, 4); $switch_type = substr($type, 0, 4); $q = "SELECT src,post_id FROM `$this->_table_img_optm` WHERE post_id = %d AND optm_status = %d"; $list = $wpdb->get_results($wpdb->prepare($q, array($pid, self::STATUS_PULLED))); $msg = 'Unknown Msg'; foreach ($list as $v) { // to switch webp file if ($switch_type === 'webp') { if ($this->__media->info($v->src . '.webp', $v->post_id)) { $this->__media->rename($v->src . '.webp', $v->src . '.optm.webp', $v->post_id); self::debug('Disabled WebP: ' . $v->src); $msg = __('Disabled WebP file successfully.', 'litespeed-cache'); } elseif ($this->__media->info($v->src . '.optm.webp', $v->post_id)) { $this->__media->rename($v->src . '.optm.webp', $v->src . '.webp', $v->post_id); self::debug('Enable WebP: ' . $v->src); $msg = __('Enabled WebP file successfully.', 'litespeed-cache'); } } // to switch avif file elseif ($switch_type === 'avif') { if ($this->__media->info($v->src . '.avif', $v->post_id)) { $this->__media->rename($v->src . '.avif', $v->src . '.optm.avif', $v->post_id); self::debug('Disabled AVIF: ' . $v->src); $msg = __('Disabled AVIF file successfully.', 'litespeed-cache'); } elseif ($this->__media->info($v->src . '.optm.avif', $v->post_id)) { $this->__media->rename($v->src . '.optm.avif', $v->src . '.avif', $v->post_id); self::debug('Enable AVIF: ' . $v->src); $msg = __('Enabled AVIF file successfully.', 'litespeed-cache'); } } // to switch original file else { $extension = pathinfo($v->src, PATHINFO_EXTENSION); $local_filename = substr($v->src, 0, -strlen($extension) - 1); $bk_file = $local_filename . '.bk.' . $extension; $bk_optm_file = $local_filename . '.bk.optm.' . $extension; // revert ori back if ($this->__media->info($bk_file, $v->post_id)) { $this->__media->rename($v->src, $bk_optm_file, $v->post_id); $this->__media->rename($bk_file, $v->src, $v->post_id); self::debug('Restore original img: ' . $bk_file); $msg = __('Restored original file successfully.', 'litespeed-cache'); } elseif ($this->__media->info($bk_optm_file, $v->post_id)) { $this->__media->rename($v->src, $bk_file, $v->post_id); $this->__media->rename($bk_optm_file, $v->src, $v->post_id); self::debug('Switch to optm img: ' . $v->src); $msg = __('Switched to optimized file successfully.', 'litespeed-cache'); } } } Admin_Display::success($msg); } /** * Delete one optm data and recover original file * * @since 2.4.2 * @access public */ public function reset_row($post_id) { global $wpdb; if (!$post_id) { return; } // Gathered image don't have DB_SIZE info yet // $size_meta = get_post_meta( $post_id, self::DB_SIZE, true ); // if ( ! $size_meta ) { // return; // } self::debug('_reset_row [pid] ' . $post_id); # TODO: Load image sub files $img_q = "SELECT b.post_id, b.meta_value FROM `$wpdb->postmeta` b WHERE b.post_id =%d AND b.meta_key = '_wp_attachment_metadata'"; $q = $wpdb->prepare($img_q, array($post_id)); $v = $wpdb->get_row($q); $meta_value = $this->_parse_wp_meta_value($v); if ($meta_value) { $this->tmp_pid = $v->post_id; $this->tmp_path = pathinfo($meta_value['file'], PATHINFO_DIRNAME) . '/'; $this->_destroy_optm_file($meta_value, true); if (!empty($meta_value['sizes'])) { array_map(array($this, '_destroy_optm_file'), $meta_value['sizes']); } } delete_post_meta($post_id, self::DB_SIZE); delete_post_meta($post_id, self::DB_SET); $msg = __('Reset the optimized data successfully.', 'litespeed-cache'); Admin_Display::success($msg); } /** * Show an image's optm status * * @since 1.6.5 * @access public */ public function check_img() { global $wpdb; $pid = $_POST['data']; self::debug('Check image [ID] ' . $pid); $data = array(); $data['img_count'] = $this->img_count(); $data['optm_summary'] = self::get_summary(); $data['_wp_attached_file'] = get_post_meta($pid, '_wp_attached_file', true); $data['_wp_attachment_metadata'] = get_post_meta($pid, '_wp_attachment_metadata', true); // Get img_optm data $q = "SELECT * FROM `$this->_table_img_optm` WHERE post_id = %d"; $list = $wpdb->get_results($wpdb->prepare($q, $pid)); $img_data = array(); if ($list) { foreach ($list as $v) { $img_data[] = array( 'id' => $v->id, 'optm_status' => $v->optm_status, 'src' => $v->src, 'srcpath_md5' => $v->srcpath_md5, 'src_md5' => $v->src_md5, 'server_info' => $v->server_info, ); } } $data['img_data'] = $img_data; return array('_res' => 'ok', 'data' => $data); } /** * Handle all request actions from main cls * * @since 2.0 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_RESET_ROW: $this->reset_row(!empty($_GET['id']) ? $_GET['id'] : false); break; case self::TYPE_CALC_BKUP: $this->_calc_bkup(); break; case self::TYPE_RM_BKUP: $this->rm_bkup(); break; case self::TYPE_NEW_REQ: $this->new_req(); break; case self::TYPE_RESCAN: $this->_rescan(); break; case self::TYPE_RESET_COUNTER: $this->_reset_counter(); break; case self::TYPE_DESTROY: $this->_destroy(); break; case self::TYPE_CLEAN: $this->clean(); break; case self::TYPE_PULL: self::start_async(); break; case self::TYPE_BATCH_SWITCH_ORI: case self::TYPE_BATCH_SWITCH_OPTM: $this->batch_switch($type); break; case substr($type, 0, 4) === 'avif': case substr($type, 0, 4) === 'webp': case substr($type, 0, 4) === 'orig': $this->_switch_optm_file($type); break; default: break; } Admin::redirect(); } } data_structure/img_optming.sql 0000644 00000000526 15162226240 0012632 0 ustar 00 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `post_id` bigint(20) unsigned NOT NULL DEFAULT '0', `optm_status` tinyint(4) NOT NULL DEFAULT '0', `src` varchar(1000) NOT NULL DEFAULT '', `server_info` text NOT NULL, PRIMARY KEY (`id`), KEY `post_id` (`post_id`), KEY `optm_status` (`optm_status`), KEY `src` (`src`(191)) data_structure/avatar.sql 0000644 00000000404 15162226242 0011574 0 ustar 00 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `url` varchar(1000) NOT NULL DEFAULT '', `md5` varchar(128) NOT NULL DEFAULT '', `dateline` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), UNIQUE KEY `md5` (`md5`), KEY `dateline` (`dateline`) data_structure/img_optm.sql 0000644 00000000632 15162226243 0012135 0 ustar 00 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `post_id` bigint(20) unsigned NOT NULL DEFAULT '0', `optm_status` tinyint(4) NOT NULL DEFAULT '0', `src` text NOT NULL, `src_filesize` int(11) NOT NULL DEFAULT '0', `target_filesize` int(11) NOT NULL DEFAULT '0', `webp_filesize` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `post_id` (`post_id`), KEY `optm_status` (`optm_status`) data_structure/url.sql 0000644 00000000315 15162226245 0011124 0 ustar 00 `id` bigint(20) NOT NULL AUTO_INCREMENT, `url` varchar(500) NOT NULL, `cache_tags` varchar(1000) NOT NULL DEFAULT '', PRIMARY KEY (`id`), UNIQUE KEY `url` (`url`(191)), KEY `cache_tags` (`cache_tags`(191)) data_structure/crawler_blacklist.sql 0000644 00000000626 15162226247 0014020 0 ustar 00 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `url` varchar(1000) NOT NULL DEFAULT '', `res` varchar(255) NOT NULL DEFAULT '' COMMENT '-=Not Blacklist, B=blacklist', `reason` text NOT NULL COMMENT 'Reason for blacklist, comma separated', `mtime` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), PRIMARY KEY (`id`), KEY `url` (`url`(191)), KEY `res` (`res`) data_structure/crawler.sql 0000644 00000000632 15162226250 0011757 0 ustar 00 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `url` varchar(1000) NOT NULL DEFAULT '', `res` varchar(255) NOT NULL DEFAULT '' COMMENT '-=not crawl, H=hit, M=miss, B=blacklist', `reason` text NOT NULL COMMENT 'response code, comma separated', `mtime` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), PRIMARY KEY (`id`), KEY `url` (`url`(191)), KEY `res` (`res`) data_structure/url_file.sql 0000644 00000001210 15162226252 0012114 0 ustar 00 `id` bigint(20) NOT NULL AUTO_INCREMENT, `url_id` bigint(20) NOT NULL, `vary` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'md5 of final vary', `filename` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'md5 of file content', `type` tinyint(4) NOT NULL COMMENT 'css=1,js=2,ccss=3,ucss=4', `mobile` tinyint(4) NOT NULL COMMENT 'mobile=1', `webp` tinyint(4) NOT NULL COMMENT 'webp=1', `expired` int(11) NOT NULL DEFAULT 0, PRIMARY KEY (`id`), KEY `filename` (`filename`), KEY `type` (`type`), KEY `url_id_2` (`url_id`,`vary`,`type`), KEY `filename_2` (`filename`,`expired`), KEY `url_id` (`url_id`,`expired`) data.cls.php 0000644 00000043164 15162226253 0006762 0 ustar 00 <?php /** * The class to store and manage litespeed db data. * * @since 1.3.1 * @package LiteSpeed * @subpackage LiteSpeed/src * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Data extends Root { const LOG_TAG = '🚀'; private $_db_updater = array( '3.5.0.3' => array('litespeed_update_3_5'), '4.0' => array('litespeed_update_4'), '4.1' => array('litespeed_update_4_1'), '4.3' => array('litespeed_update_4_3'), '4.4.4-b1' => array('litespeed_update_4_4_4'), '5.3-a5' => array('litespeed_update_5_3'), '7.0-b26' => array('litespeed_update_7'), '7.0.1-b1' => array('litespeed_update_7_0_1'), ); private $_db_site_updater = array( // Example // '2.0' => array( // 'litespeed_update_site_2_0', // ), ); private $_url_file_types = array( 'css' => 1, 'js' => 2, 'ccss' => 3, 'ucss' => 4, ); const TB_IMG_OPTM = 'litespeed_img_optm'; const TB_IMG_OPTMING = 'litespeed_img_optming'; // working table const TB_AVATAR = 'litespeed_avatar'; const TB_CRAWLER = 'litespeed_crawler'; const TB_CRAWLER_BLACKLIST = 'litespeed_crawler_blacklist'; const TB_URL = 'litespeed_url'; const TB_URL_FILE = 'litespeed_url_file'; /** * Init * * @since 1.3.1 */ public function __construct() { } /** * Correct table existence * * Call when activate -> update_confs() * Call when update_confs() * * @since 3.0 * @access public */ public function correct_tb_existence() { // Gravatar if ($this->conf(Base::O_DISCUSS_AVATAR_CACHE)) { $this->tb_create('avatar'); } // Crawler if ($this->conf(Base::O_CRAWLER)) { $this->tb_create('crawler'); $this->tb_create('crawler_blacklist'); } // URL mapping $this->tb_create('url'); $this->tb_create('url_file'); // Image optm is a bit different. Only trigger creation when sending requests. Drop when destroying. } /** * Upgrade conf to latest format version from previous versions * * NOTE: Only for v3.0+ * * @since 3.0 * @access public */ public function conf_upgrade($ver) { // Skip count check if `Use Primary Site Configurations` is on // Deprecated since v3.0 as network primary site didn't override the subsites conf yet // if ( ! is_main_site() && ! empty ( $this->_site_options[ self::NETWORK_O_USE_PRIMARY ] ) ) { // return; // } if ($this->_get_upgrade_lock()) { return; } $this->_set_upgrade_lock(true); require_once LSCWP_DIR . 'src/data.upgrade.func.php'; // Init log manually if ($this->conf(Base::O_DEBUG)) { $this->cls('Debug2')->init(); } foreach ($this->_db_updater as $k => $v) { if (version_compare($ver, $k, '<')) { // run each callback foreach ($v as $v2) { self::debug("Updating [ori_v] $ver \t[to] $k \t[func] $v2"); call_user_func($v2); } } } // Reload options $this->cls('Conf')->load_options(); $this->correct_tb_existence(); // Update related files $this->cls('Activation')->update_files(); // Update version to latest Conf::delete_option(Base::_VER); Conf::add_option(Base::_VER, Core::VER); self::debug('Updated version to ' . Core::VER); $this->_set_upgrade_lock(false); !defined('LSWCP_EMPTYCACHE') && define('LSWCP_EMPTYCACHE', true); // clear all sites caches Purge::purge_all(); return 'upgrade'; } /** * Upgrade site conf to latest format version from previous versions * * NOTE: Only for v3.0+ * * @since 3.0 * @access public */ public function conf_site_upgrade($ver) { if ($this->_get_upgrade_lock()) { return; } $this->_set_upgrade_lock(true); require_once LSCWP_DIR . 'src/data.upgrade.func.php'; foreach ($this->_db_site_updater as $k => $v) { if (version_compare($ver, $k, '<')) { // run each callback foreach ($v as $v2) { self::debug("Updating site [ori_v] $ver \t[to] $k \t[func] $v2"); call_user_func($v2); } } } // Reload options $this->cls('Conf')->load_site_options(); Conf::delete_site_option(Base::_VER); Conf::add_site_option(Base::_VER, Core::VER); self::debug('Updated site_version to ' . Core::VER); $this->_set_upgrade_lock(false); !defined('LSWCP_EMPTYCACHE') && define('LSWCP_EMPTYCACHE', true); // clear all sites caches Purge::purge_all(); } /** * Check if upgrade script is running or not * * @since 3.0.1 */ private function _get_upgrade_lock() { $is_upgrading = get_option('litespeed.data.upgrading'); if (!$is_upgrading) { $this->_set_upgrade_lock(false); // set option value to existed to avoid repeated db query next time } if ($is_upgrading && time() - $is_upgrading < 3600) { return $is_upgrading; } return false; } /** * Show the upgrading banner if upgrade script is running * * @since 3.0.1 */ public function check_upgrading_msg() { $is_upgrading = $this->_get_upgrade_lock(); if (!$is_upgrading) { return; } Admin_Display::info( sprintf( __('The database has been upgrading in the background since %s. This message will disappear once upgrade is complete.', 'litespeed-cache'), '<code>' . Utility::readable_time($is_upgrading) . '</code>' ) . ' [LiteSpeed]', true ); } /** * Set lock for upgrade process * * @since 3.0.1 */ private function _set_upgrade_lock($lock) { if (!$lock) { update_option('litespeed.data.upgrading', -1); } else { update_option('litespeed.data.upgrading', time()); } } /** * Upgrade the conf to v3.0 from previous v3.0- data * * NOTE: Only for v3.0- * * @since 3.0 * @access public */ public function try_upgrade_conf_3_0() { $previous_options = get_option('litespeed-cache-conf'); if (!$previous_options) { return 'new'; } $ver = $previous_options['version']; !defined('LSCWP_CUR_V') && define('LSCWP_CUR_V', $ver); // Init log manually if ($this->conf(Base::O_DEBUG)) { $this->cls('Debug2')->init(); } self::debug('Upgrading previous settings [from] ' . $ver . ' [to] v3.0'); if ($this->_get_upgrade_lock()) { return; } $this->_set_upgrade_lock(true); require_once LSCWP_DIR . 'src/data.upgrade.func.php'; // Here inside will update the version to v3.0 litespeed_update_3_0($ver); $this->_set_upgrade_lock(false); self::debug('Upgraded to v3.0'); // Upgrade from 3.0 to latest version $ver = '3.0'; if (Core::VER != $ver) { return $this->conf_upgrade($ver); } else { // Reload options $this->cls('Conf')->load_options(); $this->correct_tb_existence(); !defined('LSWCP_EMPTYCACHE') && define('LSWCP_EMPTYCACHE', true); // clear all sites caches Purge::purge_all(); return 'upgrade'; } } /** * Get the table name * * @since 3.0 * @access public */ public function tb($tb) { global $wpdb; switch ($tb) { case 'img_optm': return $wpdb->prefix . self::TB_IMG_OPTM; break; case 'img_optming': return $wpdb->prefix . self::TB_IMG_OPTMING; break; case 'avatar': return $wpdb->prefix . self::TB_AVATAR; break; case 'crawler': return $wpdb->prefix . self::TB_CRAWLER; break; case 'crawler_blacklist': return $wpdb->prefix . self::TB_CRAWLER_BLACKLIST; break; case 'url': return $wpdb->prefix . self::TB_URL; break; case 'url_file': return $wpdb->prefix . self::TB_URL_FILE; break; default: break; } } /** * Check if one table exists or not * * @since 3.0 * @access public */ public function tb_exist($tb) { global $wpdb; return $wpdb->get_var("SHOW TABLES LIKE '" . $this->tb($tb) . "'"); } /** * Get data structure of one table * * @since 2.0 * @access private */ private function _tb_structure($tb) { return File::read(LSCWP_DIR . 'src/data_structure/' . $tb . '.sql'); } /** * Create img optm table and sync data from wp_postmeta * * @since 3.0 * @access public */ public function tb_create($tb) { global $wpdb; self::debug2('[Data] Checking table ' . $tb); // Check if table exists first if ($this->tb_exist($tb)) { self::debug2('[Data] Existed'); return; } self::debug('Creating ' . $tb); $sql = sprintf( 'CREATE TABLE IF NOT EXISTS `%1$s` (' . $this->_tb_structure($tb) . ') %2$s;', $this->tb($tb), $wpdb->get_charset_collate() // 'DEFAULT CHARSET=utf8' ); $res = $wpdb->query($sql); if ($res !== true) { self::debug('Warning! Creating table failed!', $sql); Admin_Display::error(Error::msg('failed_tb_creation', array('<code>' . $tb . '</code>', '<code>' . $sql . '</code>'))); } } /** * Drop table * * @since 3.0 * @access public */ public function tb_del($tb) { global $wpdb; if (!$this->tb_exist($tb)) { return; } self::debug('Deleting table ' . $tb); $q = 'DROP TABLE IF EXISTS ' . $this->tb($tb); $wpdb->query($q); } /** * Drop generated tables * * @since 3.0 * @access public */ public function tables_del() { $this->tb_del('avatar'); $this->tb_del('crawler'); $this->tb_del('crawler_blacklist'); $this->tb_del('url'); $this->tb_del('url_file'); // Deleting img_optm only can be done when destroy all optm images } /** * Keep table but clear all data * * @since 4.0 */ public function table_truncate($tb) { global $wpdb; $q = 'TRUNCATE TABLE ' . $this->tb($tb); $wpdb->query($q); } /** * Clean certain type of url_file * * @since 4.0 */ public function url_file_clean($file_type) { global $wpdb; if (!$this->tb_exist('url_file')) { return; } $type = $this->_url_file_types[$file_type]; $q = 'DELETE FROM ' . $this->tb('url_file') . ' WHERE `type` = %d'; $wpdb->query($wpdb->prepare($q, $type)); // Added to cleanup url table. See issue: https://wordpress.org/support/topic/wp_litespeed_url-1-1-gb-in-db-huge-big/ $wpdb->query( 'DELETE d FROM `' . $this->tb('url') . '` AS d LEFT JOIN `' . $this->tb('url_file') . '` AS f ON d.`id` = f.`url_id` WHERE f.`url_id` IS NULL' ); } /** * Generate filename based on URL, if content md5 existed, reuse existing file. * @since 4.0 */ public function save_url($request_url, $vary, $file_type, $filecon_md5, $path, $mobile = false, $webp = false) { global $wpdb; if (strlen($vary) > 32) { $vary = md5($vary); } $type = $this->_url_file_types[$file_type]; $tb_url = $this->tb('url'); $tb_url_file = $this->tb('url_file'); $q = "SELECT * FROM `$tb_url` WHERE url=%s"; $url_row = $wpdb->get_row($wpdb->prepare($q, $request_url), ARRAY_A); if (!$url_row) { $q = "INSERT INTO `$tb_url` SET url=%s"; $wpdb->query($wpdb->prepare($q, $request_url)); $url_id = $wpdb->insert_id; } else { $url_id = $url_row['id']; } $q = "SELECT * FROM `$tb_url_file` WHERE url_id=%d AND vary=%s AND type=%d AND expired=0"; $file_row = $wpdb->get_row($wpdb->prepare($q, array($url_id, $vary, $type)), ARRAY_A); // Check if has previous file or not if ($file_row && $file_row['filename'] == $filecon_md5) { return; } // If the new $filecon_md5 is marked as expired by previous records, clear those records $q = "DELETE FROM `$tb_url_file` WHERE filename = %s AND expired > 0"; $wpdb->query($wpdb->prepare($q, $filecon_md5)); // Check if there is any other record used the same filename or not $q = "SELECT id FROM `$tb_url_file` WHERE filename = %s AND expired = 0 AND id != %d LIMIT 1"; if ($file_row && $wpdb->get_var($wpdb->prepare($q, array($file_row['filename'], $file_row['id'])))) { $q = "UPDATE `$tb_url_file` SET filename=%s WHERE id=%d"; $wpdb->query($wpdb->prepare($q, array($filecon_md5, $file_row['id']))); return; } // New record needed $q = "INSERT INTO `$tb_url_file` SET url_id=%d, vary=%s, filename=%s, type=%d, mobile=%d, webp=%d, expired=0"; $wpdb->query($wpdb->prepare($q, array($url_id, $vary, $filecon_md5, $type, $mobile ? 1 : 0, $webp ? 1 : 0))); // Mark existing rows as expired if ($file_row) { $q = "UPDATE `$tb_url_file` SET expired=%d WHERE id=%d"; $expired = time() + 86400 * apply_filters('litespeed_url_file_expired_days', 20); $wpdb->query($wpdb->prepare($q, array($expired, $file_row['id']))); // Also check if has other files expired already to be deleted $q = "SELECT * FROM `$tb_url_file` WHERE url_id = %d AND expired BETWEEN 1 AND %d"; $q = $wpdb->prepare($q, array($url_id, time())); $list = $wpdb->get_results($q, ARRAY_A); if ($list) { foreach ($list as $v) { $file_to_del = $path . '/' . $v['filename'] . '.' . ($file_type == 'js' ? 'js' : 'css'); if (file_exists($file_to_del)) { // Safe to delete self::debug('Delete expired unused file: ' . $file_to_del); // Clear related lscache first to avoid cache copy of same URL w/ diff QS // Purge::add( Tag::TYPE_MIN . '.' . $file_row[ 'filename' ] . '.' . $file_type ); unlink($file_to_del); } } $q = "DELETE FROM `$tb_url_file` WHERE url_id = %d AND expired BETWEEN 1 AND %d"; $wpdb->query($wpdb->prepare($q, array($url_id, time()))); } } // Purge this URL to avoid cache copy of same URL w/ diff QS // $this->cls( 'Purge' )->purge_url( Utility::make_relative( $request_url ) ?: '/', true, true ); } /** * Load CCSS related file * @since 4.0 */ public function load_url_file($request_url, $vary, $file_type) { global $wpdb; if (strlen($vary) > 32) { $vary = md5($vary); } $type = $this->_url_file_types[$file_type]; self::debug2('load url file: ' . $request_url); $tb_url = $this->tb('url'); $q = "SELECT * FROM `$tb_url` WHERE url=%s"; $url_row = $wpdb->get_row($wpdb->prepare($q, $request_url), ARRAY_A); if (!$url_row) { return false; } $url_id = $url_row['id']; $tb_url_file = $this->tb('url_file'); $q = "SELECT * FROM `$tb_url_file` WHERE url_id=%d AND vary=%s AND type=%d AND expired=0"; $file_row = $wpdb->get_row($wpdb->prepare($q, array($url_id, $vary, $type)), ARRAY_A); if (!$file_row) { return false; } return $file_row['filename']; } /** * Mark all entries of one URL to expired * @since 4.5 */ public function mark_as_expired($request_url, $auto_q = false) { global $wpdb; $tb_url = $this->tb('url'); self::debug('Try to mark as expired: ' . $request_url); $q = "SELECT * FROM `$tb_url` WHERE url=%s"; $url_row = $wpdb->get_row($wpdb->prepare($q, $request_url), ARRAY_A); if (!$url_row) { return; } self::debug('Mark url_id=' . $url_row['id'] . ' as expired'); $tb_url_file = $this->tb('url_file'); $existing_url_files = array(); if ($auto_q) { $q = "SELECT a.*, b.url FROM `$tb_url_file` a LEFT JOIN `$tb_url` b ON b.id=a.url_id WHERE a.url_id=%d AND a.type=4 AND a.expired=0"; $q = $wpdb->prepare($q, $url_row['id']); $existing_url_files = $wpdb->get_results($q, ARRAY_A); } $q = "UPDATE `$tb_url_file` SET expired=%d WHERE url_id=%d AND type=4 AND expired=0"; $expired = time() + 86400 * apply_filters('litespeed_url_file_expired_days', 20); $wpdb->query($wpdb->prepare($q, array($expired, $url_row['id']))); return $existing_url_files; } /** * Get list from `data/css_excludes.txt` * * @since 3.6 */ public function load_css_exc($list) { $data = $this->_load_per_line('css_excludes.txt'); if ($data) { $list = array_unique(array_filter(array_merge($list, $data))); } return $list; } /** * Get list from `data/ccss_whitelist.txt` * * @since 7.1 */ public function load_ccss_whitelist($list) { $data = $this->_load_per_line('ccss_whitelist.txt'); if ($data) { $list = array_unique(array_filter(array_merge($list, $data))); } return $list; } /** * Get list from `data/ucss_whitelist.txt` * * @since 4.0 */ public function load_ucss_whitelist($list) { $data = $this->_load_per_line('ucss_whitelist.txt'); if ($data) { $list = array_unique(array_filter(array_merge($list, $data))); } return $list; } /** * Get list from `data/js_excludes.txt` * * @since 3.5 */ public function load_js_exc($list) { $data = $this->_load_per_line('js_excludes.txt'); if ($data) { $list = array_unique(array_filter(array_merge($list, $data))); } return $list; } /** * Get list from `data/js_defer_excludes.txt` * * @since 3.6 */ public function load_js_defer_exc($list) { $data = $this->_load_per_line('js_defer_excludes.txt'); if ($data) { $list = array_unique(array_filter(array_merge($list, $data))); } return $list; } /** * Get list from `data/optm_uri_exc.txt` * * @since 5.4 */ public function load_optm_uri_exc($list) { $data = $this->_load_per_line('optm_uri_exc.txt'); if ($data) { $list = array_unique(array_filter(array_merge($list, $data))); } return $list; } /** * Get list from `data/esi.nonces.txt` * * @since 3.5 */ public function load_esi_nonces($list) { $data = $this->_load_per_line('esi.nonces.txt'); if ($data) { $list = array_unique(array_filter(array_merge($list, $data))); } return $list; } /** * Get list from `data/cache_nocacheable.txt` * * @since 6.3.0.1 */ public function load_cache_nocacheable($list) { $data = $this->_load_per_line('cache_nocacheable.txt'); if ($data) { $list = array_unique(array_filter(array_merge($list, $data))); } return $list; } /** * Load file per line * * Support two kinds of comments: * 1. `# this is comment` * 2. `##this is comment` * * @since 3.5 */ private function _load_per_line($file) { $data = File::read(LSCWP_DIR . 'data/' . $file); $data = explode(PHP_EOL, $data); $list = array(); foreach ($data as $v) { // Drop two kinds of comments if (strpos($v, '##') !== false) { $v = trim(substr($v, 0, strpos($v, '##'))); } if (strpos($v, '# ') !== false) { $v = trim(substr($v, 0, strpos($v, '# '))); } if (!$v) { continue; } $list[] = $v; } return $list; } } data.upgrade.func.php 0000644 00000056156 15162226254 0010570 0 ustar 00 <?php /** * Database upgrade funcs * * NOTE: whenever called this file, always call Data::get_upgrade_lock and Data::_set_upgrade_lock first. * * @since 3.0 */ defined('WPINC') || exit(); use LiteSpeed\Debug2; use LiteSpeed\Conf; use LiteSpeed\Admin_Display; use LiteSpeed\File; use LiteSpeed\Cloud; /** * Migrate v7.0- url_files URL from no trailing slash to trailing slash * @since 7.0.1 */ function litespeed_update_7_0_1() { global $wpdb; Debug2::debug('[Data] v7.0.1 upgrade started'); $tb_url = $wpdb->prefix . 'litespeed_url'; $tb_exists = $wpdb->get_var("SHOW TABLES LIKE '" . $tb_url . "'"); if (!$tb_exists) { Debug2::debug('[Data] Table `litespeed_url` not found, bypassed migration'); return; } $q = "SELECT * FROM `$tb_url` WHERE url LIKE 'https://%/'"; $q = $wpdb->prepare($q); $list = $wpdb->get_results($q, ARRAY_A); $existing_urls = array(); if ($list) { foreach ($list as $v) { $existing_urls[] = $v['url']; } } $q = "SELECT * FROM `$tb_url` WHERE url LIKE 'https://%'"; $q = $wpdb->prepare($q); $list = $wpdb->get_results($q, ARRAY_A); if (!$list) { return; } foreach ($list as $v) { if (substr($v['url'], -1) == '/') { continue; } $new_url = $v['url'] . '/'; if (in_array($new_url, $existing_urls)) { continue; } $q = "UPDATE `$tb_url` SET url = %s WHERE id = %d"; $q = $wpdb->prepare($q, $new_url, $v['id']); $wpdb->query($q); } } /** * Migrate from domain key to pk/sk for QC * @since 7.0 */ function litespeed_update_7() { Debug2::debug('[Data] v7 upgrade started'); $__cloud = Cloud::cls(); $domain_key = $__cloud->conf('api_key'); if (!$domain_key) { Debug2::debug('[Data] No domain key, bypassed migration'); return; } $new_prepared = $__cloud->init_qc_prepare(); if (!$new_prepared && $__cloud->activated()) { Debug2::debug('[Data] QC previously activated in v7, bypassed migration'); return; } $data = array( 'domain_key' => $domain_key, ); $resp = $__cloud->post(Cloud::SVC_D_V3UPGRADE, $data); if (!empty($resp['qc_activated'])) { if ($resp['qc_activated'] != 'deleted') { $cloud_summary_updates = array('qc_activated' => $resp['qc_activated']); if (!empty($resp['main_domain'])) { $cloud_summary_updates['main_domain'] = $resp['main_domain']; } Cloud::save_summary($cloud_summary_updates); Debug2::debug('[Data] Updated QC activated status to ' . $resp['qc_activated']); } } } /** * Append webp/mobile to url_file * @since 5.3 */ function litespeed_update_5_3() { global $wpdb; Debug2::debug('[Data] Upgrade url_file table'); $tb_exists = $wpdb->get_var('SHOW TABLES LIKE "' . $wpdb->prefix . 'litespeed_url_file"'); if ($tb_exists) { $q = 'ALTER TABLE `' . $wpdb->prefix . 'litespeed_url_file` ADD COLUMN `mobile` tinyint(4) NOT NULL COMMENT "mobile=1", ADD COLUMN `webp` tinyint(4) NOT NULL COMMENT "webp=1" '; $wpdb->query($q); } } /** * Add expired to url_file table * @since 4.4.4 */ function litespeed_update_4_4_4() { global $wpdb; Debug2::debug('[Data] Upgrade url_file table'); $tb_exists = $wpdb->get_var('SHOW TABLES LIKE "' . $wpdb->prefix . 'litespeed_url_file"'); if ($tb_exists) { $q = 'ALTER TABLE `' . $wpdb->prefix . 'litespeed_url_file` ADD COLUMN `expired` int(11) NOT NULL DEFAULT 0, ADD KEY `filename_2` (`filename`,`expired`), ADD KEY `url_id` (`url_id`,`expired`) '; $wpdb->query($q); } } /** * Drop cssjs table and rm cssjs folder * @since 4.3 */ function litespeed_update_4_3() { if (file_exists(LITESPEED_STATIC_DIR . '/ccsjs')) { File::rrmdir(LITESPEED_STATIC_DIR . '/ccsjs'); } } /** * Drop object cache data file * @since 4.1 */ function litespeed_update_4_1() { if (file_exists(WP_CONTENT_DIR . '/.object-cache.ini')) { unlink(WP_CONTENT_DIR . '/.object-cache.ini'); } } /** * Drop cssjs table and rm cssjs folder * @since 4.0 */ function litespeed_update_4() { global $wpdb; $tb = $wpdb->prefix . 'litespeed_cssjs'; $existed = $wpdb->get_var("SHOW TABLES LIKE '$tb'"); if (!$existed) { return; } $q = 'DROP TABLE IF EXISTS ' . $tb; $wpdb->query($q); if (file_exists(LITESPEED_STATIC_DIR . '/ccsjs')) { File::rrmdir(LITESPEED_STATIC_DIR . '/ccsjs'); } } /** * Append jQuery to JS optm exclude list for max compatibility * Turn off JS Combine and Defer * * @since 3.5.1 */ function litespeed_update_3_5() { $__conf = Conf::cls(); // Excludes jQuery foreach (array('optm-js_exc', 'optm-js_defer_exc') as $v) { $curr_setting = $__conf->conf($v); $curr_setting[] = 'jquery.js'; $curr_setting[] = 'jquery.min.js'; $__conf->update($v, $curr_setting); } // Turn off JS Combine and defer $show_msg = false; foreach (array('optm-js_comb', 'optm-js_defer', 'optm-js_inline_defer') as $v) { $curr_setting = $__conf->conf($v); if (!$curr_setting) { continue; } $show_msg = true; $__conf->update($v, false); } if ($show_msg) { $msg = sprintf( __( 'LiteSpeed Cache upgraded successfully. NOTE: Due to changes in this version, the settings %1$s and %2$s have been turned OFF. Please turn them back on manually and verify that your site layout is correct, and you have no JS errors.', 'litespeed-cache' ), '<code>' . __('JS Combine', 'litespeed-cache') . '</code>', '<code>' . __('JS Defer', 'litespeed-cache') . '</code>' ); $msg .= sprintf(' <a href="admin.php?page=litespeed-page_optm#settings_js">%s</a>.', __('Click here to settings', 'litespeed-cache')); Admin_Display::info($msg, false, true); } } /** * For version under v2.0 to v2.0+ * * @since 3.0 */ function litespeed_update_2_0($ver) { global $wpdb; // Table version only exists after all old data migrated // Last modified is v2.4.2 if (version_compare($ver, '2.4.2', '<')) { /** * Convert old data from postmeta to img_optm table * @since 2.0 */ // Migrate data from `wp_postmeta` to `wp_litespeed_img_optm` $mids_to_del = array(); $q = "SELECT * FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_id"; $meta_value_list = $wpdb->get_results($wpdb->prepare($q, 'litespeed-optimize-data')); if ($meta_value_list) { $max_k = count($meta_value_list) - 1; foreach ($meta_value_list as $k => $v) { $mids_to_del[] = $v->meta_id; // Delete from postmeta if (count($mids_to_del) > 100 || $k == $max_k) { $q = "DELETE FROM $wpdb->postmeta WHERE meta_id IN ( " . implode(',', array_fill(0, count($mids_to_del), '%s')) . ' ) '; $wpdb->query($wpdb->prepare($q, $mids_to_del)); $mids_to_del = array(); } } Debug2::debug('[Data] img_optm inserted records: ' . $k); } $q = "DELETE FROM $wpdb->postmeta WHERE meta_key = %s"; $rows = $wpdb->query($wpdb->prepare($q, 'litespeed-optimize-status')); Debug2::debug('[Data] img_optm delete optm_status records: ' . $rows); } /** * Add target_md5 field to table * @since 2.4.2 */ if (version_compare($ver, '2.4.2', '<') && version_compare($ver, '2.0', '>=')) { // NOTE: For new users, need to bypass this section $sql = sprintf('ALTER TABLE `%1$s` ADD `server_info` text NOT NULL, DROP COLUMN `server`', $wpdb->prefix . 'litespeed_img_optm'); $res = $wpdb->query($sql); if ($res !== true) { Debug2::debug('[Data] Warning: Alter table img_optm failed!', $sql); } else { Debug2::debug('[Data] Successfully upgraded table img_optm.'); } } // Delete img optm tb version delete_option($wpdb->prefix . 'litespeed_img_optm'); // Delete possible HTML optm data from wp_options delete_option('litespeed-cache-optimized'); // Delete HTML optm tb version delete_option($wpdb->prefix . 'litespeed_optimizer'); } /** * Move all options in litespeed-cache-conf from v3.0- to separate records * * @since 3.0 */ function litespeed_update_3_0($ver) { global $wpdb; // Upgrade v2.0- to v2.0 first if (version_compare($ver, '2.0', '<')) { litespeed_update_2_0($ver); } set_time_limit(86400); // conv items to litespeed.conf.* Debug2::debug('[Data] Conv items to litespeed.conf.*'); $data = array( 'litespeed-cache-exclude-cache-roles' => 'cache-exc_roles', 'litespeed-cache-drop_qs' => 'cache-drop_qs', 'litespeed-forced_cache_uri' => 'cache-force_uri', 'litespeed-cache_uri_priv' => 'cache-priv_uri', 'litespeed-excludes_uri' => 'cache-exc', 'litespeed-cache-vary-group' => 'cache-vary_group', 'litespeed-adv-purge_all_hooks' => 'purge-hook_all', 'litespeed-object_global_groups' => 'object-global_groups', 'litespeed-object_non_persistent_groups' => 'object-non_persistent_groups', 'litespeed-media-lazy-img-excludes' => 'media-lazy_exc', 'litespeed-media-lazy-img-cls-excludes' => 'media-lazy_cls_exc', 'litespeed-media-webp_attribute' => 'img_optm-webp_attr', 'litespeed-optm-css' => 'optm-ccss_con', 'litespeed-optm_excludes' => 'optm-exc', 'litespeed-optm-ccss-separate_posttype' => 'optm-ccss_sep_posttype', 'litespeed-optm-css-separate_uri' => 'optm-ccss_sep_uri', 'litespeed-optm-js-defer-excludes' => 'optm-js_defer_exc', 'litespeed-cache-dns_prefetch' => 'optm-dns_prefetch', 'litespeed-cache-exclude-optimization-roles' => 'optm-exc_roles', 'litespeed-log_ignore_filters' => 'debug-log_no_filters', // depreciated 'litespeed-log_ignore_part_filters' => 'debug-log_no_part_filters', // depreciated 'litespeed-cdn-ori_dir' => 'cdn-ori_dir', 'litespeed-cache-cdn_mapping' => 'cdn-mapping', 'litespeed-crawler-as-uids' => 'crawler-roles', 'litespeed-crawler-cookies' => 'crawler-cookies', ); foreach ($data as $k => $v) { $old_data = get_option($k); if ($old_data) { Debug2::debug("[Data] Convert $k"); // They must be an array if (!is_array($old_data) && $v != 'optm-ccss_con') { $old_data = explode("\n", $old_data); } if ($v == 'crawler-cookies') { $tmp = array(); $i = 0; foreach ($old_data as $k2 => $v2) { $tmp[$i]['name'] = $k2; $tmp[$i]['vals'] = explode("\n", $v2); $i++; } $old_data = $tmp; } add_option('litespeed.conf.' . $v, $old_data); } Debug2::debug("[Data] Delete $k"); delete_option($k); } // conv other items $data = array( 'litespeed-setting-mode' => 'litespeed.setting.mode', 'litespeed-media-need-pull' => 'litespeed.img_optm.need_pull', 'litespeed-env-ref' => 'litespeed.env.ref', 'litespeed-cache-cloudflare_status' => 'litespeed.cdn.cloudflare.status', ); foreach ($data as $k => $v) { $old_data = get_option($k); if ($old_data) { add_option($v, $old_data); } delete_option($k); } // Conv conf from litespeed-cache-conf child to litespeed.conf.* Debug2::debug('[Data] Conv conf from litespeed-cache-conf child to litespeed.conf.*'); $previous_options = get_option('litespeed-cache-conf'); $data = array( 'radio_select' => 'cache', 'hash' => 'hash', 'auto_upgrade' => 'auto_upgrade', 'news' => 'news', 'crawler_domain_ip' => 'server_ip', 'esi_enabled' => 'esi', 'esi_cached_admbar' => 'esi-cache_admbar', 'esi_cached_commform' => 'esi-cache_commform', 'heartbeat' => 'misc-heartbeat_front', 'cache_browser' => 'cache-browser', 'cache_browser_ttl' => 'cache-ttl_browser', 'instant_click' => 'util-instant_click', 'use_http_for_https_vary' => 'util-no_https_vary', 'purge_upgrade' => 'purge-upgrade', 'timed_urls' => 'purge-timed_urls', 'timed_urls_time' => 'purge-timed_urls_time', 'cache_priv' => 'cache-priv', 'cache_commenter' => 'cache-commenter', 'cache_rest' => 'cache-rest', 'cache_page_login' => 'cache-page_login', 'cache_favicon' => 'cache-favicon', 'cache_resources' => 'cache-resources', 'mobileview_enabled' => 'cache-mobile', 'mobileview_rules' => 'cache-mobile_rules', 'nocache_useragents' => 'cache-exc_useragents', 'nocache_cookies' => 'cache-exc_cookies', 'excludes_qs' => 'cache-exc_qs', 'excludes_cat' => 'cache-exc_cat', 'excludes_tag' => 'cache-exc_tag', 'public_ttl' => 'cache-ttl_pub', 'private_ttl' => 'cache-ttl_priv', 'front_page_ttl' => 'cache-ttl_frontpage', 'feed_ttl' => 'cache-ttl_feed', 'login_cookie' => 'cache-login_cookie', 'debug_disable_all' => 'debug-disable_all', 'debug' => 'debug', 'admin_ips' => 'debug-ips', 'debug_level' => 'debug-level', 'log_file_size' => 'debug-filesize', 'debug_cookie' => 'debug-cookie', 'collapse_qs' => 'debug-collapse_qs', // 'log_filters' => 'debug-log_filters', 'crawler_cron_active' => 'crawler', // 'crawler_include_posts' => 'crawler-inc_posts', // 'crawler_include_pages' => 'crawler-inc_pages', // 'crawler_include_cats' => 'crawler-inc_cats', // 'crawler_include_tags' => 'crawler-inc_tags', // 'crawler_excludes_cpt' => 'crawler-exc_cpt', // 'crawler_order_links' => 'crawler-order_links', 'crawler_usleep' => 'crawler-usleep', 'crawler_run_duration' => 'crawler-run_duration', 'crawler_run_interval' => 'crawler-run_interval', 'crawler_crawl_interval' => 'crawler-crawl_interval', 'crawler_threads' => 'crawler-threads', 'crawler_load_limit' => 'crawler-load_limit', 'crawler_custom_sitemap' => 'crawler-sitemap', 'cache_object' => 'object', 'cache_object_kind' => 'object-kind', 'cache_object_host' => 'object-host', 'cache_object_port' => 'object-port', 'cache_object_life' => 'object-life', 'cache_object_persistent' => 'object-persistent', 'cache_object_admin' => 'object-admin', 'cache_object_transients' => 'object-transients', 'cache_object_db_id' => 'object-db_id', 'cache_object_user' => 'object-user', 'cache_object_pswd' => 'object-psw', 'cdn' => 'cdn', 'cdn_ori' => 'cdn-ori', 'cdn_exclude' => 'cdn-exc', // 'cdn_remote_jquery' => 'cdn-remote_jq', 'cdn_quic' => 'cdn-quic', 'cdn_cloudflare' => 'cdn-cloudflare', 'cdn_cloudflare_email' => 'cdn-cloudflare_email', 'cdn_cloudflare_key' => 'cdn-cloudflare_key', 'cdn_cloudflare_name' => 'cdn-cloudflare_name', 'cdn_cloudflare_zone' => 'cdn-cloudflare_zone', 'media_img_lazy' => 'media-lazy', 'media_img_lazy_placeholder' => 'media-lazy_placeholder', 'media_placeholder_resp' => 'media-placeholder_resp', 'media_placeholder_resp_color' => 'media-placeholder_resp_color', 'media_placeholder_resp_async' => 'media-placeholder_resp_async', 'media_iframe_lazy' => 'media-iframe_lazy', // 'media_img_lazyjs_inline' => 'media-lazyjs_inline', 'media_optm_auto' => 'img_optm-auto', 'media_optm_cron' => 'img_optm-cron', 'media_optm_ori' => 'img_optm-ori', 'media_rm_ori_bkup' => 'img_optm-rm_bkup', // 'media_optm_webp' => 'img_optm-webp', 'media_webp_replace' => 'img_optm-webp', 'media_optm_lossless' => 'img_optm-lossless', 'media_optm_exif' => 'img_optm-exif', 'media_webp_replace_srcset' => 'img_optm-webp_replace_srcset', 'css_minify' => 'optm-css_min', // 'css_inline_minify' => 'optm-css_inline_min', 'css_combine' => 'optm-css_comb', // 'css_combined_priority' => 'optm-css_comb_priority', // 'css_http2' => 'optm-css_http2', 'css_exclude' => 'optm-css_exc', 'js_minify' => 'optm-js_min', // 'js_inline_minify' => 'optm-js_inline_min', 'js_combine' => 'optm-js_comb', // 'js_combined_priority' => 'optm-js_comb_priority', // 'js_http2' => 'optm-js_http2', 'js_exclude' => 'optm-js_exc', // 'optimize_ttl' => 'optm-ttl', 'html_minify' => 'optm-html_min', 'optm_qs_rm' => 'optm-qs_rm', 'optm_ggfonts_rm' => 'optm-ggfonts_rm', 'optm_css_async' => 'optm-css_async', // 'optm_ccss_gen' => 'optm-ccss_gen', // 'optm_ccss_async' => 'optm-ccss_async', 'optm_css_async_inline' => 'optm-css_async_inline', 'optm_js_defer' => 'optm-js_defer', 'optm_emoji_rm' => 'optm-emoji_rm', // 'optm_exclude_jquery' => 'optm-exc_jq', 'optm_ggfonts_async' => 'optm-ggfonts_async', // 'optm_max_size' => 'optm-max_size', // 'optm_rm_comment' => 'optm-rm_comment', ); foreach ($data as $k => $v) { if (!isset($previous_options[$k])) { continue; } // The following values must be array if (!is_array($previous_options[$k])) { if (in_array($v, array('cdn-ori', 'cache-exc_cat', 'cache-exc_tag'))) { $previous_options[$k] = explode(',', $previous_options[$k]); $previous_options[$k] = array_filter($previous_options[$k]); } elseif (in_array($v, array('cache-mobile_rules', 'cache-exc_useragents', 'cache-exc_cookies'))) { $previous_options[$k] = explode('|', str_replace('\\ ', ' ', $previous_options[$k])); $previous_options[$k] = array_filter($previous_options[$k]); } elseif ( in_array($v, array( 'purge-timed_urls', 'cache-exc_qs', 'debug-ips', // 'crawler-exc_cpt', 'cdn-exc', 'optm-css_exc', 'optm-js_exc', )) ) { $previous_options[$k] = explode("\n", $previous_options[$k]); $previous_options[$k] = array_filter($previous_options[$k]); } } // Special handler for heartbeat if ($v == 'misc-heartbeat_front') { if (!$previous_options[$k]) { add_option('litespeed.conf.misc-heartbeat_front', true); add_option('litespeed.conf.misc-heartbeat_back', true); add_option('litespeed.conf.misc-heartbeat_editor', true); add_option('litespeed.conf.misc-heartbeat_front_ttl', 0); add_option('litespeed.conf.misc-heartbeat_back_ttl', 0); add_option('litespeed.conf.misc-heartbeat_editor_ttl', 0); } continue; } add_option('litespeed.conf.' . $v, $previous_options[$k]); } // Conv purge_by_post $data = array( '-' => 'purge-post_all', 'F' => 'purge-post_f', 'H' => 'purge-post_h', 'PGS' => 'purge-post_p', 'PGSRP' => 'purge-post_pwrp', 'A' => 'purge-post_a', 'Y' => 'purge-post_y', 'M' => 'purge-post_m', 'D' => 'purge-post_d', 'T' => 'purge-post_t', 'PT' => 'purge-post_pt', ); if (isset($previous_options['purge_by_post'])) { $purge_by_post = explode('.', $previous_options['purge_by_post']); foreach ($data as $k => $v) { add_option('litespeed.conf.' . $v, in_array($k, $purge_by_post)); } } // Conv 404/403/500 TTL $ttl_status = array(); if (isset($previous_options['403_ttl'])) { $ttl_status[] = '403 ' . $previous_options['403_ttl']; } if (isset($previous_options['404_ttl'])) { $ttl_status[] = '404 ' . $previous_options['404_ttl']; } if (isset($previous_options['500_ttl'])) { $ttl_status[] = '500 ' . $previous_options['500_ttl']; } add_option('litespeed.conf.cache-ttl_status', $ttl_status); /** * Resave cdn cfg from lscfg to separate cfg when upgrade to v1.7 * * NOTE: this can be left here as `add_option` bcos it is after the item `litespeed-cache-cdn_mapping` is converted * * @since 1.7 */ if (isset($previous_options['cdn_url'])) { $cdn_mapping = array( 'url' => $previous_options['cdn_url'], 'inc_img' => $previous_options['cdn_inc_img'], 'inc_css' => $previous_options['cdn_inc_css'], 'inc_js' => $previous_options['cdn_inc_js'], 'filetype' => $previous_options['cdn_filetype'], ); add_option('litespeed.conf.cdn-mapping', array($cdn_mapping)); Debug2::debug('[Data] plugin_upgrade option adding CDN map'); } /** * Move Exclude settings to separate item * * NOTE: this can be left here as `add_option` bcos it is after the relevant items are converted * * @since 2.3 */ if (isset($previous_options['forced_cache_uri'])) { add_option('litespeed.conf.cache-force_uri', $previous_options['forced_cache_uri']); } if (isset($previous_options['cache_uri_priv'])) { add_option('litespeed.conf.cache-priv_uri', $previous_options['cache_uri_priv']); } if (isset($previous_options['optm_excludes'])) { add_option('litespeed.conf.optm-exc', $previous_options['optm_excludes']); } if (isset($previous_options['excludes_uri'])) { add_option('litespeed.conf.cache-exc', $previous_options['excludes_uri']); } // Backup stale conf Debug2::debug('[Data] Backup stale conf'); delete_option('litespeed-cache-conf'); add_option('litespeed-cache-conf.bk', $previous_options); // Upgrade site_options if is network if (is_multisite()) { $ver = get_site_option('litespeed.conf._version'); if (!$ver) { Debug2::debug('[Data] Conv multisite'); $previous_site_options = get_site_option('litespeed-cache-conf'); $data = array( 'network_enabled' => 'cache', 'use_primary_settings' => 'use_primary_settings', 'auto_upgrade' => 'auto_upgrade', 'purge_upgrade' => 'purge-upgrade', 'cache_favicon' => 'cache-favicon', 'cache_resources' => 'cache-resources', 'mobileview_enabled' => 'cache-mobile', 'mobileview_rules' => 'cache-mobile_rules', 'login_cookie' => 'cache-login_cookie', 'nocache_cookies' => 'cache-exc_cookies', 'nocache_useragents' => 'cache-exc_useragents', 'cache_object' => 'object', 'cache_object_kind' => 'object-kind', 'cache_object_host' => 'object-host', 'cache_object_port' => 'object-port', 'cache_object_life' => 'object-life', 'cache_object_persistent' => 'object-persistent', 'cache_object_admin' => 'object-admin', 'cache_object_transients' => 'object-transients', 'cache_object_db_id' => 'object-db_id', 'cache_object_user' => 'object-user', 'cache_object_pswd' => 'object-psw', 'cache_browser' => 'cache-browser', 'cache_browser_ttl' => 'cache-ttl_browser', 'media_webp_replace' => 'img_optm-webp', ); foreach ($data as $k => $v) { if (!isset($previous_site_options[$k])) { continue; } // The following values must be array if (!is_array($previous_site_options[$k])) { if (in_array($v, array('cache-mobile_rules', 'cache-exc_useragents', 'cache-exc_cookies'))) { $previous_site_options[$k] = explode('|', str_replace('\\ ', ' ', $previous_site_options[$k])); $previous_site_options[$k] = array_filter($previous_site_options[$k]); } } add_site_option('litespeed.conf.' . $v, $previous_site_options[$k]); } // These are already converted to single record in single site $data = array('object-global_groups', 'object-non_persistent_groups'); foreach ($data as $v) { $old_data = get_option($v); if ($old_data) { add_site_option('litespeed.conf.' . $v, $old_data); } } delete_site_option('litespeed-cache-conf'); add_site_option('litespeed.conf._version', '3.0'); } } // delete tables Debug2::debug('[Data] Drop litespeed_optimizer'); $q = 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'litespeed_optimizer'; $wpdb->query($q); // Update image optm table Debug2::debug('[Data] Upgrade img_optm table'); $tb_exists = $wpdb->get_var('SHOW TABLES LIKE "' . $wpdb->prefix . 'litespeed_img_optm"'); if ($tb_exists) { $status_mapping = array( 'requested' => 3, 'notified' => 6, 'pulled' => 9, 'failed' => -1, 'miss' => -3, 'err' => -9, 'err_fetch' => -5, 'err_optm' => -7, 'xmeta' => -8, ); foreach ($status_mapping as $k => $v) { $q = 'UPDATE `' . $wpdb->prefix . "litespeed_img_optm` SET optm_status='$v' WHERE optm_status='$k'"; $wpdb->query($q); } $q = 'ALTER TABLE `' . $wpdb->prefix . 'litespeed_img_optm` DROP INDEX `post_id_2`, DROP INDEX `root_id`, DROP INDEX `src_md5`, DROP INDEX `srcpath_md5`, DROP COLUMN `srcpath_md5`, DROP COLUMN `src_md5`, DROP COLUMN `root_id`, DROP COLUMN `target_saved`, DROP COLUMN `webp_saved`, DROP COLUMN `server_info`, MODIFY COLUMN `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, MODIFY COLUMN `optm_status` tinyint(4) NOT NULL DEFAULT 0, MODIFY COLUMN `src` text COLLATE utf8mb4_unicode_ci NOT NULL '; $wpdb->query($q); } delete_option('litespeed-recommended'); Debug2::debug('[Data] litespeed_update_3_0 done!'); add_option('litespeed.conf._version', '3.0'); } activation.cls.php 0000644 00000035636 15162226255 0010221 0 ustar 00 <?php /** * The plugin activation class. * * @since 1.1.0 * @since 1.5 Moved into /inc * @package LiteSpeed * @subpackage LiteSpeed/inc * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Activation extends Base { const TYPE_UPGRADE = 'upgrade'; const TYPE_INSTALL_3RD = 'install_3rd'; const TYPE_INSTALL_ZIP = 'install_zip'; const TYPE_DISMISS_RECOMMENDED = 'dismiss_recommended'; const NETWORK_TRANSIENT_COUNT = 'lscwp_network_count'; private static $_data_file; /** * Construct * * @since 4.1 */ public function __construct() { self::$_data_file = LSCWP_CONTENT_DIR . '/' . self::CONF_FILE; } /** * The activation hook callback. * * @since 1.0.0 * @access public */ public static function register_activation() { global $wp_version; $advanced_cache = LSCWP_CONTENT_DIR . '/advanced-cache.php'; if (version_compare($wp_version, '5.3', '<') && !file_exists($advanced_cache)) { $file_pointer = fopen($advanced_cache, 'w'); fwrite($file_pointer, "<?php\n\n// A compatibility placeholder for WordPress < v5.3\n// Created by LSCWP v6.1+"); fclose($file_pointer); } $count = 0; !defined('LSCWP_LOG_TAG') && define('LSCWP_LOG_TAG', 'Activate_' . get_current_blog_id()); /* Network file handler */ if (is_multisite()) { $count = self::get_network_count(); if ($count !== false) { $count = intval($count) + 1; set_site_transient(self::NETWORK_TRANSIENT_COUNT, $count, DAY_IN_SECONDS); } if (!is_network_admin()) { if ($count === 1) { // Only itself is activated, set .htaccess with only CacheLookUp try { Htaccess::cls()->insert_ls_wrapper(); } catch (\Exception $ex) { Admin_Display::error($ex->getMessage()); } } } } self::cls()->update_files(); if (defined('LSCWP_REF') && LSCWP_REF == 'whm') { GUI::update_option(GUI::WHM_MSG, GUI::WHM_MSG_VAL); } } /** * Uninstall plugin * @since 1.1.0 */ public static function uninstall_litespeed_cache() { Task::destroy(); // Delete options foreach (Conf::cls()->load_default_vals() as $k => $v) { Base::delete_option($k); } // Delete site options if (is_multisite()) { foreach (Conf::cls()->load_default_site_vals() as $k => $v) { Base::delete_site_option($k); } } // Delete avatar table Data::cls()->tables_del(); if (file_exists(LITESPEED_STATIC_DIR)) { File::rrmdir(LITESPEED_STATIC_DIR); } Cloud::version_check('uninstall'); // Files has been deleted when deactivated } /** * Get the blog ids for the network. Accepts function arguments. * * Will use wp_get_sites for WP versions less than 4.6 * * @since 1.0.12 * @access public * @return array The array of blog ids. */ public static function get_network_ids($args = array()) { global $wp_version; if (version_compare($wp_version, '4.6', '<')) { $blogs = wp_get_sites($args); if (!empty($blogs)) { foreach ($blogs as $key => $blog) { $blogs[$key] = $blog['blog_id']; } } } else { $args['fields'] = 'ids'; $blogs = get_sites($args); } return $blogs; } /** * Gets the count of active litespeed cache plugins on multisite. * * @since 1.0.12 * @access private */ private static function get_network_count() { $count = get_site_transient(self::NETWORK_TRANSIENT_COUNT); if ($count !== false) { return intval($count); } // need to update $default = array(); $count = 0; $sites = self::get_network_ids(array('deleted' => 0)); if (empty($sites)) { return false; } foreach ($sites as $site) { $bid = is_object($site) && property_exists($site, 'blog_id') ? $site->blog_id : $site; $plugins = get_blog_option($bid, 'active_plugins', $default); if (!empty($plugins) && in_array(LSCWP_BASENAME, $plugins, true)) { $count++; } } /** * In case this is called outside the admin page * @see https://codex.wordpress.org/Function_Reference/is_plugin_active_for_network * @since 2.0 */ if (!function_exists('is_plugin_active_for_network')) { require_once ABSPATH . '/wp-admin/includes/plugin.php'; } if (is_plugin_active_for_network(LSCWP_BASENAME)) { $count++; } return $count; } /** * Is this deactivate call the last active installation on the multisite network? * * @since 1.0.12 * @access private */ private static function is_deactivate_last() { $count = self::get_network_count(); if ($count === false) { return false; } if ($count !== 1) { // Not deactivating the last one. $count--; set_site_transient(self::NETWORK_TRANSIENT_COUNT, $count, DAY_IN_SECONDS); return false; } delete_site_transient(self::NETWORK_TRANSIENT_COUNT); return true; } /** * The deactivation hook callback. * * Initializes all clean up functionalities. * * @since 1.0.0 * @access public */ public static function register_deactivation() { Task::destroy(); !defined('LSCWP_LOG_TAG') && define('LSCWP_LOG_TAG', 'Deactivate_' . get_current_blog_id()); Purge::purge_all(); if (is_multisite()) { if (!self::is_deactivate_last()) { if (is_network_admin()) { // Still other activated subsite left, set .htaccess with only CacheLookUp try { Htaccess::cls()->insert_ls_wrapper(); } catch (\Exception $ex) { Admin_Display::error($ex->getMessage()); } } return; } } /* 1) wp-config.php; */ try { self::cls()->_manage_wp_cache_const(false); } catch (\Exception $ex) { error_log('In wp-config.php: WP_CACHE could not be set to false during deactivation!'); Admin_Display::error($ex->getMessage()); } /* 2) adv-cache.php; Dropped in v3.0.4 */ /* 3) object-cache.php; */ Object_Cache::cls()->del_file(); /* 4) .htaccess; */ try { Htaccess::cls()->clear_rules(); } catch (\Exception $ex) { Admin_Display::error($ex->getMessage()); } /* 5) .litespeed_conf.dat; */ self::_del_conf_data_file(); // delete in case it's not deleted prior to deactivation. GUI::dismiss_whm(); } /** * Manage related files based on plugin latest conf * * NOTE: Only trigger this in backend admin access for efficiency concern * * Handle files: * 1) wp-config.php; * 2) adv-cache.php; * 3) object-cache.php; * 4) .htaccess; * 5) .litespeed_conf.dat; * * @since 3.0 * @access public */ public function update_files() { Debug2::debug('🗂️ [Activation] update_files'); // Update cache setting `_CACHE` $this->cls('Conf')->define_cache(); // Site options applied already $options = $this->get_options(); /* 1) wp-config.php; */ try { $this->_manage_wp_cache_const($options[self::_CACHE]); } catch (\Exception $ex) { // Add msg to admin page or CLI Admin_Display::error($ex->getMessage()); } /* 2) adv-cache.php; Dropped in v3.0.4 */ /* 3) object-cache.php; */ if ($options[self::O_OBJECT] && (!$options[self::O_DEBUG_DISABLE_ALL] || is_multisite())) { $this->cls('Object_Cache')->update_file($options); } else { $this->cls('Object_Cache')->del_file(); // Note: because it doesn't reconnect, which caused setting page OC option changes delayed, thus may meet Connect Test Failed issue (Next refresh will correct it). Not a big deal, will keep as is. } /* 4) .htaccess; */ try { $this->cls('Htaccess')->update($options); } catch (\Exception $ex) { Admin_Display::error($ex->getMessage()); } /* 5) .litespeed_conf.dat; */ if (($options[self::O_GUEST] || $options[self::O_OBJECT]) && (!$options[self::O_DEBUG_DISABLE_ALL] || is_multisite())) { $this->_update_conf_data_file($options); } } /** * Delete data conf file * * @since 4.1 */ private static function _del_conf_data_file() { if (file_exists(self::$_data_file)) { unlink(self::$_data_file); } } /** * Update data conf file for guest mode & object cache * * @since 4.1 */ private function _update_conf_data_file($options) { $ids = array(); if ($options[self::O_OBJECT]) { $this_ids = array( self::O_DEBUG, self::O_OBJECT_KIND, self::O_OBJECT_HOST, self::O_OBJECT_PORT, self::O_OBJECT_LIFE, self::O_OBJECT_USER, self::O_OBJECT_PSWD, self::O_OBJECT_DB_ID, self::O_OBJECT_PERSISTENT, self::O_OBJECT_ADMIN, self::O_OBJECT_TRANSIENTS, self::O_OBJECT_GLOBAL_GROUPS, self::O_OBJECT_NON_PERSISTENT_GROUPS, ); $ids = array_merge($ids, $this_ids); } if ($options[self::O_GUEST]) { $this_ids = array(self::HASH, self::O_CACHE_LOGIN_COOKIE, self::O_DEBUG_IPS, self::O_UTIL_NO_HTTPS_VARY, self::O_GUEST_UAS, self::O_GUEST_IPS); $ids = array_merge($ids, $this_ids); } $data = array(); foreach ($ids as $v) { $data[$v] = $options[$v]; } $data = \json_encode($data); $old_data = File::read(self::$_data_file); if ($old_data != $data) { defined('LSCWP_LOG') && Debug2::debug('[Activation] Updating .litespeed_conf.dat'); File::save(self::$_data_file, $data); } } /** * Update the WP_CACHE variable in the wp-config.php file. * * If enabling, check if the variable is defined, and if not, define it. * Vice versa for disabling. * * @since 1.0.0 * @since 3.0 Refactored * @access private */ private function _manage_wp_cache_const($enable) { if ($enable) { if (defined('WP_CACHE') && WP_CACHE) { return false; } } elseif (!defined('WP_CACHE') || (defined('WP_CACHE') && !WP_CACHE)) { return false; } if (apply_filters('litespeed_wpconfig_readonly', false)) { throw new \Exception('wp-config file is forbidden to modify due to API hook: litespeed_wpconfig_readonly'); } /** * Follow WP's logic to locate wp-config file * @see wp-load.php */ $conf_file = ABSPATH . 'wp-config.php'; if (!file_exists($conf_file)) { $conf_file = dirname(ABSPATH) . '/wp-config.php'; } $content = File::read($conf_file); if (!$content) { throw new \Exception('wp-config file content is empty: ' . $conf_file); } // Remove the line `define('WP_CACHE', true/false);` first if (defined('WP_CACHE')) { $content = preg_replace('/define\(\s*([\'"])WP_CACHE\1\s*,\s*\w+\s*\)\s*;/sU', '', $content); } // Insert const if ($enable) { $content = preg_replace('/^<\?php/', "<?php\ndefine( 'WP_CACHE', true );", $content); } $res = File::save($conf_file, $content, false, false, false); if ($res !== true) { throw new \Exception('wp-config.php operation failed when changing `WP_CACHE` const: ' . $res); } return true; } /** * Handle auto update * * @since 2.7.2 * @access public */ public function auto_update() { if (!$this->conf(Base::O_AUTO_UPGRADE)) { return; } add_filter('auto_update_plugin', array($this, 'auto_update_hook'), 10, 2); } /** * Auto upgrade hook * * @since 3.0 * @access public */ public function auto_update_hook($update, $item) { if (!empty($item->slug) && 'litespeed-cache' === $item->slug) { $auto_v = Cloud::version_check('auto_update_plugin'); if (!empty($auto_v['latest']) && !empty($item->new_version) && $auto_v['latest'] === $item->new_version) { return true; } } return $update; // Else, use the normal API response to decide whether to update or not } /** * Upgrade LSCWP * * @since 2.9 * @access public */ public function upgrade() { $plugin = Core::PLUGIN_FILE; /** * @see wp-admin/update.php */ include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; include_once ABSPATH . 'wp-admin/includes/file.php'; include_once ABSPATH . 'wp-admin/includes/misc.php'; try { ob_start(); $skin = new \WP_Ajax_Upgrader_Skin(); $upgrader = new \Plugin_Upgrader($skin); $result = $upgrader->upgrade($plugin); if (!is_plugin_active($plugin)) { // todo: upgrade should reactivate the plugin again by WP. Need to check why disabled after upgraded. activate_plugin($plugin, '', is_multisite()); } ob_end_clean(); } catch (\Exception $e) { Admin_Display::error(__('Failed to upgrade.', 'litespeed-cache')); return; } if (is_wp_error($result)) { Admin_Display::error(__('Failed to upgrade.', 'litespeed-cache')); return; } Admin_Display::success(__('Upgraded successfully.', 'litespeed-cache')); } /** * Detect if the plugin is active or not * * @since 1.0 */ public function dash_notifier_is_plugin_active($plugin) { include_once ABSPATH . 'wp-admin/includes/plugin.php'; $plugin_path = $plugin . '/' . $plugin . '.php'; return is_plugin_active($plugin_path); } /** * Detect if the plugin is installed or not * * @since 1.0 */ public function dash_notifier_is_plugin_installed($plugin) { include_once ABSPATH . 'wp-admin/includes/plugin.php'; $plugin_path = $plugin . '/' . $plugin . '.php'; $valid = validate_plugin($plugin_path); return !is_wp_error($valid); } /** * Grab a plugin info from WordPress * * @since 1.0 */ public function dash_notifier_get_plugin_info($slug) { include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; $result = plugins_api('plugin_information', array('slug' => $slug)); if (is_wp_error($result)) { return false; } return $result; } /** * Install the 3rd party plugin * * @since 1.0 */ public function dash_notifier_install_3rd() { !defined('SILENCE_INSTALL') && define('SILENCE_INSTALL', true); $slug = !empty($_GET['plugin']) ? $_GET['plugin'] : false; // Check if plugin is installed already if (!$slug || $this->dash_notifier_is_plugin_active($slug)) { return; } /** * @see wp-admin/update.php */ include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; include_once ABSPATH . 'wp-admin/includes/file.php'; include_once ABSPATH . 'wp-admin/includes/misc.php'; $plugin_path = $slug . '/' . $slug . '.php'; if (!$this->dash_notifier_is_plugin_installed($slug)) { $plugin_info = $this->dash_notifier_get_plugin_info($slug); if (!$plugin_info) { return; } // Try to install plugin try { ob_start(); $skin = new \Automatic_Upgrader_Skin(); $upgrader = new \Plugin_Upgrader($skin); $result = $upgrader->install($plugin_info->download_link); ob_end_clean(); } catch (\Exception $e) { return; } } if (!is_plugin_active($plugin_path)) { activate_plugin($plugin_path); } } /** * Handle all request actions from main cls * * @since 2.9 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_UPGRADE: $this->upgrade(); break; case self::TYPE_INSTALL_3RD: $this->dash_notifier_install_3rd(); break; case self::TYPE_DISMISS_RECOMMENDED: Cloud::reload_summary(); Cloud::save_summary(array('news.new' => 0)); break; case self::TYPE_INSTALL_ZIP: Cloud::reload_summary(); $summary = Cloud::get_summary(); if (!empty($summary['news.zip'])) { Cloud::save_summary(array('news.new' => 0)); $this->cls('Debug2')->beta_test($summary['zip']); } break; default: break; } Admin::redirect(); } } cdn.cls.php 0000644 00000032274 15162226256 0006620 0 ustar 00 <?php /** * The CDN class. * * @since 1.2.3 * @since 1.5 Moved into /inc * @package LiteSpeed * @subpackage LiteSpeed/inc * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class CDN extends Root { const BYPASS = 'LITESPEED_BYPASS_CDN'; private $content; private $_cfg_cdn; private $_cfg_url_ori; private $_cfg_ori_dir; private $_cfg_cdn_mapping = array(); private $_cfg_cdn_exclude; private $cdn_mapping_hosts = array(); /** * Init * * @since 1.2.3 */ public function init() { Debug2::debug2('[CDN] init'); if (defined(self::BYPASS)) { Debug2::debug2('CDN bypass'); return; } if (!Router::can_cdn()) { if (!defined(self::BYPASS)) { define(self::BYPASS, true); } return; } $this->_cfg_cdn = $this->conf(Base::O_CDN); if (!$this->_cfg_cdn) { if (!defined(self::BYPASS)) { define(self::BYPASS, true); } return; } $this->_cfg_url_ori = $this->conf(Base::O_CDN_ORI); // Parse cdn mapping data to array( 'filetype' => 'url' ) $mapping_to_check = array(Base::CDN_MAPPING_INC_IMG, Base::CDN_MAPPING_INC_CSS, Base::CDN_MAPPING_INC_JS); foreach ($this->conf(Base::O_CDN_MAPPING) as $v) { if (!$v[Base::CDN_MAPPING_URL]) { continue; } $this_url = $v[Base::CDN_MAPPING_URL]; $this_host = parse_url($this_url, PHP_URL_HOST); // Check img/css/js foreach ($mapping_to_check as $to_check) { if ($v[$to_check]) { Debug2::debug2('[CDN] mapping ' . $to_check . ' -> ' . $this_url); // If filetype to url is one to many, make url be an array $this->_append_cdn_mapping($to_check, $this_url); if (!in_array($this_host, $this->cdn_mapping_hosts)) { $this->cdn_mapping_hosts[] = $this_host; } } } // Check file types if ($v[Base::CDN_MAPPING_FILETYPE]) { foreach ($v[Base::CDN_MAPPING_FILETYPE] as $v2) { $this->_cfg_cdn_mapping[Base::CDN_MAPPING_FILETYPE] = true; // If filetype to url is one to many, make url be an array $this->_append_cdn_mapping($v2, $this_url); if (!in_array($this_host, $this->cdn_mapping_hosts)) { $this->cdn_mapping_hosts[] = $this_host; } } Debug2::debug2('[CDN] mapping ' . implode(',', $v[Base::CDN_MAPPING_FILETYPE]) . ' -> ' . $this_url); } } if (!$this->_cfg_url_ori || !$this->_cfg_cdn_mapping) { if (!defined(self::BYPASS)) { define(self::BYPASS, true); } return; } $this->_cfg_ori_dir = $this->conf(Base::O_CDN_ORI_DIR); // In case user customized upload path if (defined('UPLOADS')) { $this->_cfg_ori_dir[] = UPLOADS; } // Check if need preg_replace $this->_cfg_url_ori = Utility::wildcard2regex($this->_cfg_url_ori); $this->_cfg_cdn_exclude = $this->conf(Base::O_CDN_EXC); if (!empty($this->_cfg_cdn_mapping[Base::CDN_MAPPING_INC_IMG])) { // Hook to srcset if (function_exists('wp_calculate_image_srcset')) { add_filter('wp_calculate_image_srcset', array($this, 'srcset'), 999); } // Hook to mime icon add_filter('wp_get_attachment_image_src', array($this, 'attach_img_src'), 999); add_filter('wp_get_attachment_url', array($this, 'url_img'), 999); } if (!empty($this->_cfg_cdn_mapping[Base::CDN_MAPPING_INC_CSS])) { add_filter('style_loader_src', array($this, 'url_css'), 999); } if (!empty($this->_cfg_cdn_mapping[Base::CDN_MAPPING_INC_JS])) { add_filter('script_loader_src', array($this, 'url_js'), 999); } add_filter('litespeed_buffer_finalize', array($this, 'finalize'), 30); } /** * Associate all filetypes with url * * @since 2.0 * @access private */ private function _append_cdn_mapping($filetype, $url) { // If filetype to url is one to many, make url be an array if (empty($this->_cfg_cdn_mapping[$filetype])) { $this->_cfg_cdn_mapping[$filetype] = $url; } elseif (is_array($this->_cfg_cdn_mapping[$filetype])) { // Append url to filetype $this->_cfg_cdn_mapping[$filetype][] = $url; } else { // Convert _cfg_cdn_mapping from string to array $this->_cfg_cdn_mapping[$filetype] = array($this->_cfg_cdn_mapping[$filetype], $url); } } /** * If include css/js in CDN * * @since 1.6.2.1 * @return bool true if included in CDN */ public function inc_type($type) { if ($type == 'css' && !empty($this->_cfg_cdn_mapping[Base::CDN_MAPPING_INC_CSS])) { return true; } if ($type == 'js' && !empty($this->_cfg_cdn_mapping[Base::CDN_MAPPING_INC_JS])) { return true; } return false; } /** * Run CDN process * NOTE: As this is after cache finalized, can NOT set any cache control anymore * * @since 1.2.3 * @access public * @return string The content that is after optimization */ public function finalize($content) { $this->content = $content; $this->_finalize(); return $this->content; } /** * Replace CDN url * * @since 1.2.3 * @access private */ private function _finalize() { if (defined(self::BYPASS)) { return; } Debug2::debug('CDN _finalize'); // Start replacing img src if (!empty($this->_cfg_cdn_mapping[Base::CDN_MAPPING_INC_IMG])) { $this->_replace_img(); $this->_replace_inline_css(); } if (!empty($this->_cfg_cdn_mapping[Base::CDN_MAPPING_FILETYPE])) { $this->_replace_file_types(); } } /** * Parse all file types * * @since 1.2.3 * @access private */ private function _replace_file_types() { $ele_to_check = $this->conf(Base::O_CDN_ATTR); foreach ($ele_to_check as $v) { if (!$v || strpos($v, '.') === false) { Debug2::debug2('[CDN] replace setting bypassed: no . attribute ' . $v); continue; } Debug2::debug2('[CDN] replace attribute ' . $v); $v = explode('.', $v); $attr = preg_quote($v[1], '#'); if ($v[0]) { $pattern = '#<' . preg_quote($v[0], '#') . '([^>]+)' . $attr . '=([\'"])(.+)\g{2}#iU'; } else { $pattern = '# ' . $attr . '=([\'"])(.+)\g{1}#iU'; } preg_match_all($pattern, $this->content, $matches); if (empty($matches[$v[0] ? 3 : 2])) { continue; } foreach ($matches[$v[0] ? 3 : 2] as $k2 => $url) { // Debug2::debug2( '[CDN] check ' . $url ); $postfix = '.' . pathinfo((string) parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION); if (!array_key_exists($postfix, $this->_cfg_cdn_mapping)) { // Debug2::debug2( '[CDN] non-existed postfix ' . $postfix ); continue; } Debug2::debug2('[CDN] matched file_type ' . $postfix . ' : ' . $url); if (!($url2 = $this->rewrite($url, Base::CDN_MAPPING_FILETYPE, $postfix))) { continue; } $attr = str_replace($url, $url2, $matches[0][$k2]); $this->content = str_replace($matches[0][$k2], $attr, $this->content); } } } /** * Parse all images * * @since 1.2.3 * @access private */ private function _replace_img() { preg_match_all('#<img([^>]+?)src=([\'"\\\]*)([^\'"\s\\\>]+)([\'"\\\]*)([^>]*)>#i', $this->content, $matches); foreach ($matches[3] as $k => $url) { // Check if is a DATA-URI if (strpos($url, 'data:image') !== false) { continue; } if (!($url2 = $this->rewrite($url, Base::CDN_MAPPING_INC_IMG))) { continue; } $html_snippet = sprintf('<img %1$s src=%2$s %3$s>', $matches[1][$k], $matches[2][$k] . $url2 . $matches[4][$k], $matches[5][$k]); $this->content = str_replace($matches[0][$k], $html_snippet, $this->content); } } /** * Parse and replace all inline styles containing url() * * @since 1.2.3 * @access private */ private function _replace_inline_css() { Debug2::debug2('[CDN] _replace_inline_css', $this->_cfg_cdn_mapping); /** * Excludes `\` from URL matching * @see #959152 - WordPress LSCache CDN Mapping causing malformed URLS * @see #685485 * @since 3.0 */ preg_match_all('/url\((?![\'"]?data)[\'"]?(.+?)[\'"]?\)/i', $this->content, $matches); foreach ($matches[1] as $k => $url) { $url = str_replace(array(' ', '\t', '\n', '\r', '\0', '\x0B', '"', "'", '"', '''), '', $url); // Parse file postfix $parsed_url = parse_url($url, PHP_URL_PATH); if (!$parsed_url) { continue; } $postfix = '.' . pathinfo($parsed_url, PATHINFO_EXTENSION); if (array_key_exists($postfix, $this->_cfg_cdn_mapping)) { Debug2::debug2('[CDN] matched file_type ' . $postfix . ' : ' . $url); if (!($url2 = $this->rewrite($url, Base::CDN_MAPPING_FILETYPE, $postfix))) { continue; } } elseif (in_array($postfix, array('jpg', 'jpeg', 'png', 'gif', 'svg', 'webp', 'avif'))) { if (!($url2 = $this->rewrite($url, Base::CDN_MAPPING_INC_IMG))) { continue; } } else { continue; } $attr = str_replace($matches[1][$k], $url2, $matches[0][$k]); $this->content = str_replace($matches[0][$k], $attr, $this->content); } Debug2::debug2('[CDN] _replace_inline_css done'); } /** * Hook to wp_get_attachment_image_src * * @since 1.2.3 * @since 1.7 Removed static from function * @access public * @param array $img The URL of the attachment image src, the width, the height * @return array */ public function attach_img_src($img) { if ($img && ($url = $this->rewrite($img[0], Base::CDN_MAPPING_INC_IMG))) { $img[0] = $url; } return $img; } /** * Try to rewrite one URL with CDN * * @since 1.7 * @access public */ public function url_img($url) { if ($url && ($url2 = $this->rewrite($url, Base::CDN_MAPPING_INC_IMG))) { $url = $url2; } return $url; } /** * Try to rewrite one URL with CDN * * @since 1.7 * @access public */ public function url_css($url) { if ($url && ($url2 = $this->rewrite($url, Base::CDN_MAPPING_INC_CSS))) { $url = $url2; } return $url; } /** * Try to rewrite one URL with CDN * * @since 1.7 * @access public */ public function url_js($url) { if ($url && ($url2 = $this->rewrite($url, Base::CDN_MAPPING_INC_JS))) { $url = $url2; } return $url; } /** * Hook to replace WP responsive images * * @since 1.2.3 * @since 1.7 Removed static from function * @access public * @param array $srcs * @return array */ public function srcset($srcs) { if ($srcs) { foreach ($srcs as $w => $data) { if (!($url = $this->rewrite($data['url'], Base::CDN_MAPPING_INC_IMG))) { continue; } $srcs[$w]['url'] = $url; } } return $srcs; } /** * Replace URL to CDN URL * * @since 1.2.3 * @access public * @param string $url * @return string Replaced URL */ public function rewrite($url, $mapping_kind, $postfix = false) { Debug2::debug2('[CDN] rewrite ' . $url); $url_parsed = parse_url($url); if (empty($url_parsed['path'])) { Debug2::debug2('[CDN] -rewrite bypassed: no path'); return false; } // Only images under wp-cotnent/wp-includes can be replaced $is_internal_folder = Utility::str_hit_array($url_parsed['path'], $this->_cfg_ori_dir); if (!$is_internal_folder) { Debug2::debug2('[CDN] -rewrite failed: path not match: ' . LSCWP_CONTENT_FOLDER); return false; } // Check if is external url if (!empty($url_parsed['host'])) { if (!Utility::internal($url_parsed['host']) && !$this->_is_ori_url($url)) { Debug2::debug2('[CDN] -rewrite failed: host not internal'); return false; } } $exclude = Utility::str_hit_array($url, $this->_cfg_cdn_exclude); if ($exclude) { Debug2::debug2('[CDN] -abort excludes ' . $exclude); return false; } // Fill full url before replacement if (empty($url_parsed['host'])) { $url = Utility::uri2url($url); Debug2::debug2('[CDN] -fill before rewritten: ' . $url); $url_parsed = parse_url($url); } $scheme = !empty($url_parsed['scheme']) ? $url_parsed['scheme'] . ':' : ''; if ($scheme) { // Debug2::debug2( '[CDN] -scheme from url: ' . $scheme ); } // Find the mapping url to be replaced to if (empty($this->_cfg_cdn_mapping[$mapping_kind])) { return false; } if ($mapping_kind !== Base::CDN_MAPPING_FILETYPE) { $final_url = $this->_cfg_cdn_mapping[$mapping_kind]; } else { // select from file type $final_url = $this->_cfg_cdn_mapping[$postfix]; } // If filetype to url is one to many, need to random one if (is_array($final_url)) { $final_url = $final_url[array_rand($final_url)]; } // Now lets replace CDN url foreach ($this->_cfg_url_ori as $v) { if (strpos($v, '*') !== false) { $url = preg_replace('#' . $scheme . $v . '#iU', $final_url, $url); } else { $url = str_replace($scheme . $v, $final_url, $url); } } Debug2::debug2('[CDN] -rewritten: ' . $url); return $url; } /** * Check if is original URL of CDN or not * * @since 2.1 * @access private */ private function _is_ori_url($url) { $url_parsed = parse_url($url); $scheme = !empty($url_parsed['scheme']) ? $url_parsed['scheme'] . ':' : ''; foreach ($this->_cfg_url_ori as $v) { $needle = $scheme . $v; if (strpos($v, '*') !== false) { if (preg_match('#' . $needle . '#iU', $url)) { return true; } } else { if (strpos($url, $needle) === 0) { return true; } } } return false; } /** * Check if the host is the CDN internal host * * @since 1.2.3 * */ public static function internal($host) { if (defined(self::BYPASS)) { return false; } $instance = self::cls(); return in_array($host, $instance->cdn_mapping_hosts); // todo: can add $this->_is_ori_url() check in future } } import.cls.php 0000644 00000010232 15162226260 0007347 0 ustar 00 <?php /** * The import/export class. * * @since 1.8.2 */ namespace LiteSpeed; defined('WPINC') || exit(); class Import extends Base { protected $_summary; const TYPE_IMPORT = 'import'; const TYPE_EXPORT = 'export'; const TYPE_RESET = 'reset'; /** * Init * * @since 1.8.2 */ public function __construct() { Debug2::debug('Import init'); $this->_summary = self::get_summary(); } /** * Export settings to file * * @since 1.8.2 * @access public */ public function export($only_data_return = false) { $raw_data = $this->get_options(true); $data = array(); foreach ($raw_data as $k => $v) { $data[] = \json_encode(array($k, $v)); } $data = implode("\n\n", $data); if ($only_data_return) { return $data; } $filename = $this->_generate_filename(); // Update log $this->_summary['export_file'] = $filename; $this->_summary['export_time'] = time(); self::save_summary(); Debug2::debug('Import: Saved to ' . $filename); @header('Content-Disposition: attachment; filename=' . $filename); echo $data; exit(); } /** * Import settings from file * * @since 1.8.2 * @access public */ public function import($file = false) { if (!$file) { if (empty($_FILES['ls_file']['name']) || substr($_FILES['ls_file']['name'], -5) != '.data' || empty($_FILES['ls_file']['tmp_name'])) { Debug2::debug('Import: Failed to import, wrong ls_file'); $msg = __('Import failed due to file error.', 'litespeed-cache'); Admin_Display::error($msg); return false; } $this->_summary['import_file'] = $_FILES['ls_file']['name']; $data = file_get_contents($_FILES['ls_file']['tmp_name']); } else { $this->_summary['import_file'] = $file; $data = file_get_contents($file); } // Update log $this->_summary['import_time'] = time(); self::save_summary(); $ori_data = array(); try { // Check if the data is v4+ or not if (strpos($data, '["_version",') === 0) { Debug2::debug('[Import] Data version: v4+'); $data = explode("\n", $data); foreach ($data as $v) { $v = trim($v); if (!$v) { continue; } list($k, $v) = \json_decode($v, true); $ori_data[$k] = $v; } } else { $ori_data = \json_decode(base64_decode($data), true); } } catch (\Exception $ex) { Debug2::debug('[Import] ❌ Failed to parse serialized data'); return false; } if (!$ori_data) { Debug2::debug('[Import] ❌ Failed to import, no data'); return false; } else { Debug2::debug('[Import] Importing data', $ori_data); } $this->cls('Conf')->update_confs($ori_data); if (!$file) { Debug2::debug('Import: Imported ' . $_FILES['ls_file']['name']); $msg = sprintf(__('Imported setting file %s successfully.', 'litespeed-cache'), $_FILES['ls_file']['name']); Admin_Display::success($msg); } else { Debug2::debug('Import: Imported ' . $file); } return true; } /** * Reset all configs to default values. * * @since 2.6.3 * @access public */ public function reset() { $options = $this->cls('Conf')->load_default_vals(); $this->cls('Conf')->update_confs($options); Debug2::debug('[Import] Reset successfully.'); $msg = __('Reset successfully.', 'litespeed-cache'); Admin_Display::success($msg); } /** * Generate the filename to export * * @since 1.8.2 * @access private */ private function _generate_filename() { // Generate filename $parsed_home = parse_url(get_home_url()); $filename = 'LSCWP_cfg-'; if (!empty($parsed_home['host'])) { $filename .= $parsed_home['host'] . '_'; } if (!empty($parsed_home['path'])) { $filename .= $parsed_home['path'] . '_'; } $filename = str_replace('/', '_', $filename); $filename .= '-' . date('Ymd_His') . '.data'; return $filename; } /** * Handle all request actions from main cls * * @since 1.8.2 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_IMPORT: $this->import(); break; case self::TYPE_EXPORT: $this->export(); break; case self::TYPE_RESET: $this->reset(); break; default: break; } Admin::redirect(); } } crawler-map.cls.php 0000644 00000035245 15162226261 0010263 0 ustar 00 <?php /** * The Crawler Sitemap Class * * @since 1.1.0 */ namespace LiteSpeed; defined('WPINC') || exit(); class Crawler_Map extends Root { const LOG_TAG = '🐞🗺️'; const BM_MISS = 1; const BM_HIT = 2; const BM_BLACKLIST = 4; private $_home_url; // Used to simplify urls private $_tb; private $_tb_blacklist; private $__data; private $_conf_map_timeout; private $_urls = array(); /** * Instantiate the class * * @since 1.1.0 */ public function __construct() { $this->_home_url = get_home_url(); $this->__data = Data::cls(); $this->_tb = $this->__data->tb('crawler'); $this->_tb_blacklist = $this->__data->tb('crawler_blacklist'); $this->_conf_map_timeout = defined('LITESPEED_CRAWLER_MAP_TIMEOUT') ? LITESPEED_CRAWLER_MAP_TIMEOUT : 180; // Specify the timeout while parsing the sitemap } /** * Save URLs crawl status into DB * * @since 3.0 * @access public */ public function save_map_status($list, $curr_crawler) { global $wpdb; Utility::compatibility(); $total_crawler = count(Crawler::cls()->list_crawlers()); $total_crawler_pos = $total_crawler - 1; // Replace current crawler's position $curr_crawler = (int) $curr_crawler; foreach ($list as $bit => $ids) { // $ids = [ id => [ url, code ], ... ] if (!$ids) { continue; } self::debug("Update map [crawler] $curr_crawler [bit] $bit [count] " . count($ids)); // Update res first, then reason $right_pos = $total_crawler_pos - $curr_crawler; $sql_res = "CONCAT( LEFT( res, $curr_crawler ), '$bit', RIGHT( res, $right_pos ) )"; $id_all = implode(',', array_map('intval', array_keys($ids))); $wpdb->query("UPDATE `$this->_tb` SET res = $sql_res WHERE id IN ( $id_all )"); // Add blacklist if ($bit == Crawler::STATUS_BLACKLIST || $bit == Crawler::STATUS_NOCACHE) { $q = "SELECT a.id, a.url FROM `$this->_tb_blacklist` a LEFT JOIN `$this->_tb` b ON b.url=a.url WHERE b.id IN ( $id_all )"; $existing = $wpdb->get_results($q, ARRAY_A); // Update current crawler status tag in existing blacklist if ($existing) { $count = $wpdb->query("UPDATE `$this->_tb_blacklist` SET res = $sql_res WHERE id IN ( " . implode(',', array_column($existing, 'id')) . ' )'); self::debug('Update blacklist [count] ' . $count); } // Append new blacklist if (count($ids) > count($existing)) { $new_urls = array_diff(array_column($ids, 'url'), array_column($existing, 'url')); self::debug('Insert into blacklist [count] ' . count($new_urls)); $q = "INSERT INTO `$this->_tb_blacklist` ( url, res, reason ) VALUES " . implode(',', array_fill(0, count($new_urls), '( %s, %s, %s )')); $data = array(); $res = array_fill(0, $total_crawler, '-'); $res[$curr_crawler] = $bit; $res = implode('', $res); $default_reason = $total_crawler > 1 ? str_repeat(',', $total_crawler - 1) : ''; // Pre-populate default reason value first, update later foreach ($new_urls as $url) { $data[] = $url; $data[] = $res; $data[] = $default_reason; } $wpdb->query($wpdb->prepare($q, $data)); } } // Update sitemap reason w/ HTTP code $reason_array = array(); foreach ($ids as $id => $v2) { $code = (int) $v2['code']; if (empty($reason_array[$code])) { $reason_array[$code] = array(); } $reason_array[$code][] = (int) $id; } foreach ($reason_array as $code => $v2) { // Complement comma if ($curr_crawler) { $code = ',' . $code; } if ($curr_crawler < $total_crawler_pos) { $code .= ','; } $count = $wpdb->query( "UPDATE `$this->_tb` SET reason=CONCAT(SUBSTRING_INDEX(reason, ',', $curr_crawler), '$code', SUBSTRING_INDEX(reason, ',', -$right_pos)) WHERE id IN (" . implode(',', $v2) . ')' ); self::debug("Update map reason [code] $code [pos] left $curr_crawler right -$right_pos [count] $count"); // Update blacklist reason if ($bit == Crawler::STATUS_BLACKLIST || $bit == Crawler::STATUS_NOCACHE) { $count = $wpdb->query( "UPDATE `$this->_tb_blacklist` a LEFT JOIN `$this->_tb` b ON b.url = a.url SET a.reason=CONCAT(SUBSTRING_INDEX(a.reason, ',', $curr_crawler), '$code', SUBSTRING_INDEX(a.reason, ',', -$right_pos)) WHERE b.id IN (" . implode(',', $v2) . ')' ); self::debug("Update blacklist [code] $code [pos] left $curr_crawler right -$right_pos [count] $count"); } } // Reset list $list[$bit] = array(); } return $list; } /** * Add one record to blacklist * NOTE: $id is sitemap table ID * * @since 3.0 * @access public */ public function blacklist_add($id) { global $wpdb; $id = (int) $id; // Build res&reason $total_crawler = count(Crawler::cls()->list_crawlers()); $res = str_repeat(Crawler::STATUS_BLACKLIST, $total_crawler); $reason = implode(',', array_fill(0, $total_crawler, 'Man')); $row = $wpdb->get_row("SELECT a.url, b.id FROM `$this->_tb` a LEFT JOIN `$this->_tb_blacklist` b ON b.url = a.url WHERE a.id = '$id'", ARRAY_A); if (!$row) { self::debug('blacklist failed to add [id] ' . $id); return; } self::debug('Add to blacklist [url] ' . $row['url']); $q = "UPDATE `$this->_tb` SET res = %s, reason = %s WHERE id = %d"; $wpdb->query($wpdb->prepare($q, array($res, $reason, $id))); if ($row['id']) { $q = "UPDATE `$this->_tb_blacklist` SET res = %s, reason = %s WHERE id = %d"; $wpdb->query($wpdb->prepare($q, array($res, $reason, $row['id']))); } else { $q = "INSERT INTO `$this->_tb_blacklist` (url, res, reason) VALUES (%s, %s, %s)"; $wpdb->query($wpdb->prepare($q, array($row['url'], $res, $reason))); } } /** * Delete one record from blacklist * * @since 3.0 * @access public */ public function blacklist_del($id) { global $wpdb; if (!$this->__data->tb_exist('crawler_blacklist')) { return; } $id = (int) $id; self::debug('blacklist delete [id] ' . $id); $sql = sprintf( "UPDATE `%s` SET res=REPLACE(REPLACE(res, '%s', '-'), '%s', '-') WHERE url=(SELECT url FROM `%s` WHERE id=%d)", $this->_tb, Crawler::STATUS_NOCACHE, Crawler::STATUS_BLACKLIST, $this->_tb_blacklist, $id ); $wpdb->query($sql); $wpdb->query("DELETE FROM `$this->_tb_blacklist` WHERE id='$id'"); } /** * Empty blacklist * * @since 3.0 * @access public */ public function blacklist_empty() { global $wpdb; if (!$this->__data->tb_exist('crawler_blacklist')) { return; } self::debug('Truncate blacklist'); $sql = sprintf("UPDATE `%s` SET res=REPLACE(REPLACE(res, '%s', '-'), '%s', '-')", $this->_tb, Crawler::STATUS_NOCACHE, Crawler::STATUS_BLACKLIST); $wpdb->query($sql); $wpdb->query("TRUNCATE `$this->_tb_blacklist`"); } /** * List blacklist * * @since 3.0 * @access public */ public function list_blacklist($limit = false, $offset = false) { global $wpdb; if (!$this->__data->tb_exist('crawler_blacklist')) { return array(); } $q = "SELECT * FROM `$this->_tb_blacklist` ORDER BY id DESC"; if ($limit !== false) { if ($offset === false) { $total = $this->count_blacklist(); $offset = Utility::pagination($total, $limit, true); } $q .= ' LIMIT %d, %d'; $q = $wpdb->prepare($q, $offset, $limit); } return $wpdb->get_results($q, ARRAY_A); } /** * Count blacklist */ public function count_blacklist() { global $wpdb; if (!$this->__data->tb_exist('crawler_blacklist')) { return false; } $q = "SELECT COUNT(*) FROM `$this->_tb_blacklist`"; return $wpdb->get_var($q); } /** * Empty sitemap * * @since 3.0 * @access public */ public function empty_map() { Data::cls()->tb_del('crawler'); $msg = __('Sitemap cleaned successfully', 'litespeed-cache'); Admin_Display::success($msg); } /** * List generated sitemap * * @since 3.0 * @access public */ public function list_map($limit, $offset = false) { global $wpdb; if (!$this->__data->tb_exist('crawler')) { return array(); } if ($offset === false) { $total = $this->count_map(); $offset = Utility::pagination($total, $limit, true); } $type = Router::verify_type(); $where = ''; if (!empty($_POST['kw'])) { $q = "SELECT * FROM `$this->_tb` WHERE url LIKE %s"; if ($type == 'hit') { $q .= " AND res LIKE '%" . Crawler::STATUS_HIT . "%'"; } if ($type == 'miss') { $q .= " AND res LIKE '%" . Crawler::STATUS_MISS . "%'"; } if ($type == 'blacklisted') { $q .= " AND res LIKE '%" . Crawler::STATUS_BLACKLIST . "%'"; } $q .= ' ORDER BY id LIMIT %d, %d'; $where = '%' . $wpdb->esc_like($_POST['kw']) . '%'; return $wpdb->get_results($wpdb->prepare($q, $where, $offset, $limit), ARRAY_A); } $q = "SELECT * FROM `$this->_tb`"; if ($type == 'hit') { $q .= " WHERE res LIKE '%" . Crawler::STATUS_HIT . "%'"; } if ($type == 'miss') { $q .= " WHERE res LIKE '%" . Crawler::STATUS_MISS . "%'"; } if ($type == 'blacklisted') { $q .= " WHERE res LIKE '%" . Crawler::STATUS_BLACKLIST . "%'"; } $q .= ' ORDER BY id LIMIT %d, %d'; // self::debug("q=$q offset=$offset, limit=$limit"); return $wpdb->get_results($wpdb->prepare($q, $offset, $limit), ARRAY_A); } /** * Count sitemap */ public function count_map() { global $wpdb; if (!$this->__data->tb_exist('crawler')) { return false; } $q = "SELECT COUNT(*) FROM `$this->_tb`"; $type = Router::verify_type(); if ($type == 'hit') { $q .= " WHERE res LIKE '%" . Crawler::STATUS_HIT . "%'"; } if ($type == 'miss') { $q .= " WHERE res LIKE '%" . Crawler::STATUS_MISS . "%'"; } if ($type == 'blacklisted') { $q .= " WHERE res LIKE '%" . Crawler::STATUS_BLACKLIST . "%'"; } return $wpdb->get_var($q); } /** * Generate sitemap * * @since 1.1.0 * @access public */ public function gen($manual = false) { $count = $this->_gen(); if (!$count) { Admin_Display::error(__('No valid sitemap parsed for crawler.', 'litespeed-cache')); return; } if (!defined('DOING_CRON') && $manual) { $msg = sprintf(__('Sitemap created successfully: %d items', 'litespeed-cache'), $count); Admin_Display::success($msg); } } /** * Generate the sitemap * * @since 1.1.0 * @access private */ private function _gen() { global $wpdb; if (!$this->__data->tb_exist('crawler')) { $this->__data->tb_create('crawler'); } if (!$this->__data->tb_exist('crawler_blacklist')) { $this->__data->tb_create('crawler_blacklist'); } // use custom sitemap if (!($sitemap = $this->conf(Base::O_CRAWLER_SITEMAP))) { return false; } $offset = strlen($this->_home_url); $sitemap = Utility::sanitize_lines($sitemap); try { foreach ($sitemap as $this_map) { $this->_parse($this_map); } } catch (\Exception $e) { self::debug('❌ failed to parse custom sitemap: ' . $e->getMessage()); } if (is_array($this->_urls) && !empty($this->_urls)) { if (defined('LITESPEED_CRAWLER_DROP_DOMAIN') && LITESPEED_CRAWLER_DROP_DOMAIN) { foreach ($this->_urls as $k => $v) { if (stripos($v, $this->_home_url) !== 0) { unset($this->_urls[$k]); continue; } $this->_urls[$k] = substr($v, $offset); } } $this->_urls = array_unique($this->_urls); } self::debug('Truncate sitemap'); $wpdb->query("TRUNCATE `$this->_tb`"); self::debug('Generate sitemap'); // Filter URLs in blacklist $blacklist = $this->list_blacklist(); $full_blacklisted = array(); $partial_blacklisted = array(); foreach ($blacklist as $v) { if (strpos($v['res'], '-') === false) { // Full blacklisted $full_blacklisted[] = $v['url']; } else { // Replace existing reason $v['reason'] = explode(',', $v['reason']); $v['reason'] = array_map(function ($element) { return $element ? 'Existed' : ''; }, $v['reason']); $v['reason'] = implode(',', $v['reason']); $partial_blacklisted[$v['url']] = array( 'res' => $v['res'], 'reason' => $v['reason'], ); } } // Drop all blacklisted URLs $this->_urls = array_diff($this->_urls, $full_blacklisted); // Default res & reason $crawler_count = count(Crawler::cls()->list_crawlers()); $default_res = str_repeat('-', $crawler_count); $default_reason = $crawler_count > 1 ? str_repeat(',', $crawler_count - 1) : ''; $data = array(); foreach ($this->_urls as $url) { $data[] = $url; $data[] = array_key_exists($url, $partial_blacklisted) ? $partial_blacklisted[$url]['res'] : $default_res; $data[] = array_key_exists($url, $partial_blacklisted) ? $partial_blacklisted[$url]['reason'] : $default_reason; } foreach (array_chunk($data, 300) as $data2) { $this->_save($data2); } // Reset crawler Crawler::cls()->reset_pos(); return count($this->_urls); } /** * Save data to table * * @since 3.0 * @access private */ private function _save($data, $fields = 'url,res,reason') { global $wpdb; if (empty($data)) { return; } $q = "INSERT INTO `$this->_tb` ( $fields ) VALUES "; // Add placeholder $q .= Utility::chunk_placeholder($data, $fields); // Store data $wpdb->query($wpdb->prepare($q, $data)); } /** * Parse custom sitemap and return urls * * @since 1.1.1 * @access private */ private function _parse($sitemap) { /** * Read via wp func to avoid allow_url_fopen = off * @since 2.2.7 */ $response = wp_safe_remote_get($sitemap, array('timeout' => $this->_conf_map_timeout, 'sslverify' => false)); if (is_wp_error($response)) { $error_message = $response->get_error_message(); self::debug('failed to read sitemap: ' . $error_message); throw new \Exception('Failed to remote read ' . $sitemap); } $xml_object = simplexml_load_string($response['body'], null, LIBXML_NOCDATA); if (!$xml_object) { if ($this->_urls) { return; } throw new \Exception('Failed to parse xml ' . $sitemap); } // start parsing $xml_array = (array) $xml_object; if (!empty($xml_array['sitemap'])) { // parse sitemap set if (is_object($xml_array['sitemap'])) { $xml_array['sitemap'] = (array) $xml_array['sitemap']; } if (!empty($xml_array['sitemap']['loc'])) { // is single sitemap $this->_parse($xml_array['sitemap']['loc']); } else { // parse multiple sitemaps foreach ($xml_array['sitemap'] as $val) { $val = (array) $val; if (!empty($val['loc'])) { $this->_parse($val['loc']); // recursive parse sitemap } } } } elseif (!empty($xml_array['url'])) { // parse url set if (is_object($xml_array['url'])) { $xml_array['url'] = (array) $xml_array['url']; } // if only 1 element if (!empty($xml_array['url']['loc'])) { $this->_urls[] = $xml_array['url']['loc']; } else { foreach ($xml_array['url'] as $val) { $val = (array) $val; if (!empty($val['loc'])) { $this->_urls[] = $val['loc']; } } } } } } cloud.cls.php 0000644 00000150656 15162226262 0007164 0 ustar 00 <?php /** * Cloud service cls * * @since 3.0 */ namespace LiteSpeed; defined('WPINC') || exit(); class Cloud extends Base { const LOG_TAG = '❄️'; const CLOUD_SERVER = 'https://api.quic.cloud'; const CLOUD_IPS = 'https://quic.cloud/ips'; const CLOUD_SERVER_DASH = 'https://my.quic.cloud'; const CLOUD_SERVER_WP = 'https://wpapi.quic.cloud'; const SVC_D_ACTIVATE = 'd/activate'; const SVC_U_ACTIVATE = 'u/wp3/activate'; const SVC_D_ENABLE_CDN = 'd/enable_cdn'; const SVC_D_LINK = 'd/link'; const SVC_D_API = 'd/api'; const SVC_D_DASH = 'd/dash'; const SVC_D_V3UPGRADE = 'd/v3upgrade'; const SVC_U_LINK = 'u/wp3/link'; const SVC_U_ENABLE_CDN = 'u/wp3/enablecdn'; const SVC_D_STATUS_CDN_CLI = 'd/status/cdn_cli'; const SVC_D_NODES = 'd/nodes'; const SVC_D_SYNC_CONF = 'd/sync_conf'; const SVC_D_USAGE = 'd/usage'; const SVC_D_SETUP_TOKEN = 'd/get_token'; const SVC_D_DEL_CDN_DNS = 'd/del_cdn_dns'; const SVC_PAGE_OPTM = 'page_optm'; const SVC_CCSS = 'ccss'; const SVC_UCSS = 'ucss'; const SVC_VPI = 'vpi'; const SVC_LQIP = 'lqip'; const SVC_QUEUE = 'queue'; const SVC_IMG_OPTM = 'img_optm'; const SVC_HEALTH = 'health'; const SVC_CDN = 'cdn'; const IMG_OPTM_DEFAULT_GROUP = 200; const IMGOPTM_TAKEN = 'img_optm-taken'; const TTL_NODE = 3; // Days before node expired const EXPIRATION_REQ = 300; // Seconds of min interval between two unfinished requests const TTL_IPS = 3; // Days for node ip list cache const API_REPORT = 'wp/report'; const API_NEWS = 'news'; const API_VER = 'ver_check'; const API_BETA_TEST = 'beta_test'; const API_REST_ECHO = 'tool/wp_rest_echo'; const API_SERVER_KEY_SIGN = 'key_sign'; private static $CENTER_SVC_SET = array( self::SVC_D_ACTIVATE, self::SVC_U_ACTIVATE, self::SVC_D_ENABLE_CDN, self::SVC_D_LINK, self::SVC_D_NODES, self::SVC_D_SYNC_CONF, self::SVC_D_USAGE, self::SVC_D_API, self::SVC_D_V3UPGRADE, self::SVC_D_DASH, self::SVC_D_STATUS_CDN_CLI, // self::API_NEWS, self::API_REPORT, // self::API_VER, // self::API_BETA_TEST, self::SVC_D_SETUP_TOKEN, self::SVC_D_DEL_CDN_DNS, ); private static $WP_SVC_SET = array(self::API_NEWS, self::API_VER, self::API_BETA_TEST, self::API_REST_ECHO); // No api key needed for these services private static $_PUB_SVC_SET = array(self::API_NEWS, self::API_REPORT, self::API_VER, self::API_BETA_TEST, self::API_REST_ECHO, self::SVC_D_V3UPGRADE, self::SVC_D_DASH); private static $_QUEUE_SVC_SET = array(self::SVC_CCSS, self::SVC_UCSS, self::SVC_VPI); public static $SERVICES_LOAD_CHECK = array( // self::SVC_CCSS, // self::SVC_UCSS, // self::SVC_VPI, self::SVC_LQIP, self::SVC_HEALTH, ); public static $SERVICES = array( self::SVC_IMG_OPTM, self::SVC_PAGE_OPTM, self::SVC_CCSS, self::SVC_UCSS, self::SVC_VPI, self::SVC_LQIP, self::SVC_CDN, self::SVC_HEALTH, // self::SVC_QUEUE, ); const TYPE_CLEAR_PROMO = 'clear_promo'; const TYPE_REDETECT_CLOUD = 'redetect_cloud'; const TYPE_CLEAR_CLOUD = 'clear_cloud'; const TYPE_ACTIVATE = 'activate'; const TYPE_LINK = 'link'; const TYPE_ENABLE_CDN = 'enablecdn'; const TYPE_API = 'api'; const TYPE_SYNC_USAGE = 'sync_usage'; const TYPE_RESET = 'reset'; const TYPE_SYNC_STATUS = 'sync_status'; protected $_summary; /** * Init * * @since 3.0 */ public function __construct() { $this->_summary = self::get_summary(); } /** * Init QC setup preparation * * @since 7.0 */ public function init_qc_prepare() { if (empty($this->_summary['sk_b64'])) { $keypair = sodium_crypto_sign_keypair(); $pk = base64_encode(sodium_crypto_sign_publickey($keypair)); $sk = base64_encode(sodium_crypto_sign_secretkey($keypair)); $this->_summary['pk_b64'] = $pk; $this->_summary['sk_b64'] = $sk; $this->save_summary(); // ATM `qc_activated` = null return true; } return false; } /** * Init QC setup * * @since 7.0 */ public function init_qc() { $this->init_qc_prepare(); $ref = $this->_get_ref_url(); // WPAPI REST echo dryrun $req_data = array( 'wp_pk_b64' => $this->_summary['pk_b64'], ); $echobox = self::post(self::API_REST_ECHO, $req_data); if ($echobox === false) { self::debugErr('REST Echo Failed!'); $msg = __('Your WP REST API seems blocked our QUIC.cloud server calls.', 'litespeed-cache'); Admin_Display::error($msg); wp_redirect($ref); return; } self::debug('echo succeeded'); // Load separate thread echoed data from storage if (empty($echobox['wpapi_ts']) || empty($echobox['wpapi_signature_b64'])) { Admin_Display::error(__('Failed to get echo data from WPAPI', 'litespeed-cache')); wp_redirect($ref); return; } $data = array( 'wp_pk_b64' => $this->_summary['pk_b64'], 'wpapi_ts' => $echobox['wpapi_ts'], 'wpapi_signature_b64' => $echobox['wpapi_signature_b64'], ); $server_ip = $this->conf(self::O_SERVER_IP); if ($server_ip) { $data['server_ip'] = $server_ip; } // Activation redirect $param = array( 'site_url' => home_url(), 'ver' => Core::VER, 'data' => $data, 'ref' => $ref, ); wp_redirect(self::CLOUD_SERVER_DASH . '/' . self::SVC_U_ACTIVATE . '?data=' . urlencode(Utility::arr2str($param))); exit(); } /** * Decide the ref */ private function _get_ref_url($ref = false) { $link = 'admin.php?page=litespeed'; if ($ref == 'cdn') { $link = 'admin.php?page=litespeed-cdn'; } if ($ref == 'online') { $link = 'admin.php?page=litespeed-general'; } if (!empty($_GET['ref']) && $_GET['ref'] == 'cdn') { $link = 'admin.php?page=litespeed-cdn'; } if (!empty($_GET['ref']) && $_GET['ref'] == 'online') { $link = 'admin.php?page=litespeed-general'; } return get_admin_url(null, $link); } /** * Init QC setup (CLI) * * @since 7.0 */ public function init_qc_cli() { $this->init_qc_prepare(); $server_ip = $this->conf(self::O_SERVER_IP); if (!$server_ip) { self::debugErr('Server IP needs to be set first!'); $msg = sprintf( __('You need to set the %1$s first. Please use the command %2$s to set.', 'litespeed-cache'), '`' . __('Server IP', 'litespeed-cache') . '`', '`wp litespeed-option set server_ip __your_ip_value__`' ); Admin_Display::error($msg); return; } // WPAPI REST echo dryrun $req_data = array( 'wp_pk_b64' => $this->_summary['pk_b64'], ); $echobox = self::post(self::API_REST_ECHO, $req_data); if ($echobox === false) { self::debugErr('REST Echo Failed!'); $msg = __('Your WP REST API seems blocked our QUIC.cloud server calls.', 'litespeed-cache'); Admin_Display::error($msg); return; } self::debug('echo succeeded'); // Load separate thread echoed data from storage if (empty($echobox['wpapi_ts']) || empty($echobox['wpapi_signature_b64'])) { self::debug('Resp: ', $echobox); Admin_Display::error(__('Failed to get echo data from WPAPI', 'litespeed-cache')); return; } $data = array( 'wp_pk_b64' => $this->_summary['pk_b64'], 'wpapi_ts' => $echobox['wpapi_ts'], 'wpapi_signature_b64' => $echobox['wpapi_signature_b64'], 'server_ip' => $server_ip, ); $res = $this->post(self::SVC_D_ACTIVATE, $data); return $res; } /** * Init QC CDN setup (CLI) * * @since 7.0 */ public function init_qc_cdn_cli($method, $cert = false, $key = false, $cf_token = false) { if (!$this->activated()) { Admin_Display::error(__('You need to activate QC first.', 'litespeed-cache')); return; } $server_ip = $this->conf(self::O_SERVER_IP); if (!$server_ip) { self::debugErr('Server IP needs to be set first!'); $msg = sprintf( __('You need to set the %1$s first. Please use the command %2$s to set.', 'litespeed-cache'), '`' . __('Server IP', 'litespeed-cache') . '`', '`wp litespeed-option set server_ip __your_ip_value__`' ); Admin_Display::error($msg); return; } if ($cert) { if (!file_exists($cert) || !file_exists($key)) { Admin_Display::error(__('Cert or key file does not exist.', 'litespeed-cache')); return; } } $data = array( 'method' => $method, 'server_ip' => $server_ip, ); if ($cert) { $data['cert'] = File::read($cert); $data['key'] = File::read($key); } if ($cf_token) { $data['cf_token'] = $cf_token; } $res = $this->post(self::SVC_D_ENABLE_CDN, $data); return $res; } /** * Link to QC setup * * @since 7.0 */ public function link_qc() { if (!$this->activated()) { Admin_Display::error(__('You need to activate QC first.', 'litespeed-cache')); return; } $data = array( 'wp_ts' => time(), ); $data['wp_signature_b64'] = $this->_sign_b64($data['wp_ts']); // Activation redirect $param = array( 'site_url' => home_url(), 'ver' => Core::VER, 'data' => $data, 'ref' => $this->_get_ref_url(), ); wp_redirect(self::CLOUD_SERVER_DASH . '/' . self::SVC_U_LINK . '?data=' . urlencode(Utility::arr2str($param))); exit(); } /** * Show QC Account CDN status * * @since 7.0 */ public function cdn_status_cli() { if (!$this->activated()) { Admin_Display::error(__('You need to activate QC first.', 'litespeed-cache')); return; } $data = array(); $res = $this->post(self::SVC_D_STATUS_CDN_CLI, $data); return $res; } /** * Link to QC Account for CLI * * @since 7.0 */ public function link_qc_cli($email, $key) { if (!$this->activated()) { Admin_Display::error(__('You need to activate QC first.', 'litespeed-cache')); return; } $data = array( 'qc_acct_email' => $email, 'qc_acct_apikey' => $key, ); $res = $this->post(self::SVC_D_LINK, $data); return $res; } /** * API link parsed call to QC * * @since 7.0 */ public function api_link_call($action2) { if (!$this->activated()) { Admin_Display::error(__('You need to activate QC first.', 'litespeed-cache')); return; } $data = array( 'action2' => $action2, ); $res = $this->post(self::SVC_D_API, $data); self::debug('API link call result: ', $res); } /** * Enable QC CDN * * @since 7.0 */ public function enable_cdn() { if (!$this->activated()) { Admin_Display::error(__('You need to activate QC first.', 'litespeed-cache')); return; } $data = array( 'wp_ts' => time(), ); $data['wp_signature_b64'] = $this->_sign_b64($data['wp_ts']); // Activation redirect $param = array( 'site_url' => home_url(), 'ver' => Core::VER, 'data' => $data, 'ref' => $this->_get_ref_url(), ); wp_redirect(self::CLOUD_SERVER_DASH . '/' . self::SVC_U_ENABLE_CDN . '?data=' . urlencode(Utility::arr2str($param))); exit(); } /** * Encrypt data for cloud req * * @since 7.0 */ private function _sign_b64($data) { if (empty($this->_summary['sk_b64'])) { self::debugErr('No sk to sign.'); return false; } $sk = base64_decode($this->_summary['sk_b64']); if (strlen($sk) !== SODIUM_CRYPTO_SIGN_SECRETKEYBYTES) { self::debugErr('Invalid local sign sk length.'); // Reset local pk/sk unset($this->_summary['pk_b64']); unset($this->_summary['sk_b64']); $this->save_summary(); self::debug('Clear local sign pk/sk pair.'); return false; } $signature = sodium_crypto_sign_detached((string) $data, $sk); return base64_encode($signature); } /** * Load server pk from cloud * * @since 7.0 */ private function _load_server_pk($from_wpapi = false) { // Load cloud pk $server_key_url = self::CLOUD_SERVER . '/' . self::API_SERVER_KEY_SIGN; if ($from_wpapi) { $server_key_url = self::CLOUD_SERVER_WP . '/' . self::API_SERVER_KEY_SIGN; } $resp = wp_safe_remote_get($server_key_url); if (is_wp_error($resp)) { self::debugErr('Failed to load key: ' . $resp->get_error_message()); return false; } $pk = trim($resp['body']); self::debug('Loaded key from ' . $server_key_url . ': ' . $pk); $cloud_pk = base64_decode($pk); if (strlen($cloud_pk) !== SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES) { self::debugErr('Invalid cloud public key length.'); return false; } $sk = base64_decode($this->_summary['sk_b64']); if (strlen($sk) !== SODIUM_CRYPTO_SIGN_SECRETKEYBYTES) { self::debugErr('Invalid local secret key length.'); // Reset local pk/sk unset($this->_summary['pk_b64']); unset($this->_summary['sk_b64']); $this->save_summary(); self::debug('Unset local pk/sk pair.'); return false; } return $cloud_pk; } /** * WPAPI echo back to notify the sealed databox * * @since 7.0 */ public function wp_rest_echo() { self::debug('Parsing echo', $_POST); if (empty($_POST['wpapi_ts']) || empty($_POST['wpapi_signature_b64'])) { return self::err('No echo data'); } $is_valid = $this->_validate_signature($_POST['wpapi_signature_b64'], $_POST['wpapi_ts'], true); if (!$is_valid) { return self::err('Data validation from WPAPI REST Echo failed'); } $diff = time() - $_POST['wpapi_ts']; if (abs($diff) > 86400) { self::debugErr('WPAPI echo data timeout [diff] ' . $diff); return self::err('Echo data expired'); } $signature_b64 = $this->_sign_b64($_POST['wpapi_ts']); self::debug('Response to echo [signature_b64] ' . $signature_b64); return self::ok(array('signature_b64' => $signature_b64)); } /** * Validate cloud data * * @since 7.0 */ private function _validate_signature($signature_b64, $data, $from_wpapi = false) { // Try validation try { $cloud_pk = $this->_load_server_pk($from_wpapi); if (!$cloud_pk) { return false; } $signature = base64_decode($signature_b64); $is_valid = sodium_crypto_sign_verify_detached($signature, $data, $cloud_pk); } catch (\SodiumException $e) { self::debugErr('Decryption failed: ' . $e->getMessage()); return false; } self::debug('Signature validation result: ' . ($is_valid ? 'true' : 'false')); return $is_valid; } /** * Finish qc activation after redirection back from QC * * @since 7.0 */ public function finish_qc_activation($ref = false) { if (empty($_GET['qc_activated']) || empty($_GET['qc_ts']) || empty($_GET['qc_signature_b64'])) { return; } $data_to_validate_signature = array( 'wp_pk_b64' => $this->_summary['pk_b64'], 'qc_ts' => $_GET['qc_ts'], ); $is_valid = $this->_validate_signature($_GET['qc_signature_b64'], implode('', $data_to_validate_signature)); if (!$is_valid) { self::debugErr('Failed to validate qc activation data'); Admin_Display::error(sprintf(__('Failed to validate %s activation data.', 'litespeed-cache'), 'QUIC.cloud')); return; } self::debug('QC activation status: ' . $_GET['qc_activated']); if (!in_array($_GET['qc_activated'], array('anonymous', 'linked', 'cdn'))) { self::debugErr('Failed to parse qc activation status'); Admin_Display::error(sprintf(__('Failed to parse %s activation status.', 'litespeed-cache'), 'QUIC.cloud')); return; } $diff = time() - $_GET['qc_ts']; if (abs($diff) > 86400) { self::debugErr('QC activation data timeout [diff] ' . $diff); Admin_Display::error(sprintf(__('%s activation data expired.', 'litespeed-cache'), 'QUIC.cloud')); return; } $main_domain = !empty($_GET['main_domain']) ? $_GET['main_domain'] : false; $this->update_qc_activation($_GET['qc_activated'], $main_domain); wp_redirect($this->_get_ref_url($ref)); } /** * Finish qc activation process * * @since 7.0 */ public function update_qc_activation($qc_activated, $main_domain = false, $quite = false) { $this->_summary['qc_activated'] = $qc_activated; if ($main_domain) { $this->_summary['main_domain'] = $main_domain; } $this->save_summary(); $msg = sprintf(__('Congratulations, %s successfully set this domain up for the anonymous online services.', 'litespeed-cache'), 'QUIC.cloud'); if ($qc_activated == 'linked') { $msg = sprintf(__('Congratulations, %s successfully set this domain up for the online services.', 'litespeed-cache'), 'QUIC.cloud'); // Sync possible partner info $this->sync_usage(); } if ($qc_activated == 'cdn') { $msg = sprintf(__('Congratulations, %s successfully set this domain up for the online services with CDN service.', 'litespeed-cache'), 'QUIC.cloud'); // Turn on CDN option $this->cls('Conf')->update_confs(array(self::O_CDN_QUIC => true)); } if (!$quite) { Admin_Display::success('🎊 ' . $msg); } $this->_clear_reset_qc_reg_msg(); $this->clear_cloud(); } /** * Load QC status for dash usage * Format to translate: `<a href="{#xxx#}" class="button button-primary">xxxx</a><a href="{#xxx#}">xxxx2</a>` * * @since 7.0 */ public function load_qc_status_for_dash($type, $force = false) { return Str::translate_qc_apis($this->_load_qc_status_for_dash($type, $force)); } private function _load_qc_status_for_dash($type, $force = false) { if ( !$force && !empty($this->_summary['mini_html']) && isset($this->_summary['mini_html'][$type]) && !empty($this->_summary['mini_html']['ttl.' . $type]) && $this->_summary['mini_html']['ttl.' . $type] > time() ) { return Str::safe_html($this->_summary['mini_html'][$type]); } // Try to update dash content $data = self::post(self::SVC_D_DASH, array('action2' => $type == 'cdn_dash_mini' ? 'cdn_dash' : $type)); if (!empty($data['qc_activated'])) { // Sync conf as changed if (empty($this->_summary['qc_activated']) || $this->_summary['qc_activated'] != $data['qc_activated']) { $msg = sprintf(__('Congratulations, %s successfully set this domain up for the online services with CDN service.', 'litespeed-cache'), 'QUIC.cloud'); Admin_Display::success('🎊 ' . $msg); $this->_clear_reset_qc_reg_msg(); // Turn on CDN option $this->cls('Conf')->update_confs(array(self::O_CDN_QUIC => true)); $this->cls('CDN\Quic')->try_sync_conf(true); } $this->_summary['qc_activated'] = $data['qc_activated']; $this->save_summary(); } // Show the info if (isset($this->_summary['mini_html'][$type])) { return Str::safe_html($this->_summary['mini_html'][$type]); } return ''; } /** * Update QC status * * @since 7.0 */ public function update_cdn_status() { if (empty($_POST['qc_activated']) || !in_array($_POST['qc_activated'], array('anonymous', 'linked', 'cdn', 'deleted'))) { return self::err('lack_of_params'); } self::debug('update_cdn_status request hash: ' . $_POST['qc_activated']); if ($_POST['qc_activated'] == 'deleted') { $this->_reset_qc_reg(); } else { $this->_summary['qc_activated'] = $_POST['qc_activated']; $this->save_summary(); } if ($_POST['qc_activated'] == 'cdn') { $msg = sprintf(__('Congratulations, %s successfully set this domain up for the online services with CDN service.', 'litespeed-cache'), 'QUIC.cloud'); Admin_Display::success('🎊 ' . $msg); $this->_clear_reset_qc_reg_msg(); // Turn on CDN option $this->cls('Conf')->update_confs(array(self::O_CDN_QUIC => true)); $this->cls('CDN\Quic')->try_sync_conf(true); } return self::ok(array('qc_activated' => $_POST['qc_activated'])); } /** * Reset QC setup * * @since 7.0 */ public function reset_qc() { unset($this->_summary['pk_b64']); unset($this->_summary['sk_b64']); unset($this->_summary['qc_activated']); if (!empty($this->_summary['partner'])) { unset($this->_summary['partner']); } $this->save_summary(); self::debug('Clear local QC activation.'); $this->clear_cloud(); Admin_Display::success(sprintf(__('Reset %s activation successfully.', 'litespeed-cache'), 'QUIC.cloud')); wp_redirect($this->_get_ref_url()); exit(); } /** * Show latest commit version always if is on dev * * @since 3.0 */ public function check_dev_version() { if (!preg_match('/[^\d\.]/', Core::VER)) { return; } $last_check = empty($this->_summary['last_request.' . self::API_VER]) ? 0 : $this->_summary['last_request.' . self::API_VER]; if (time() - $last_check > 86400) { $auto_v = self::version_check('dev'); if (!empty($auto_v['dev'])) { self::save_summary(array('version.dev' => $auto_v['dev'])); } } if (empty($this->_summary['version.dev'])) { return; } self::debug('Latest dev version ' . $this->_summary['version.dev']); if (version_compare($this->_summary['version.dev'], Core::VER, '<=')) { return; } // Show the dev banner require_once LSCWP_DIR . 'tpl/banner/new_version_dev.tpl.php'; } /** * Check latest version * * @since 2.9 * @access public */ public static function version_check($src = false) { $req_data = array( 'v' => defined('LSCWP_CUR_V') ? LSCWP_CUR_V : '', 'src' => $src, 'php' => phpversion(), ); if (defined('LITESPEED_ERR')) { $req_data['err'] = base64_encode(!is_string(LITESPEED_ERR) ? \json_encode(LITESPEED_ERR) : LITESPEED_ERR); } $data = self::post(self::API_VER, $req_data); return $data; } /** * Show latest news * * @since 3.0 */ public function news() { $this->_update_news(); if (empty($this->_summary['news.new'])) { return; } if (!empty($this->_summary['news.plugin']) && Activation::cls()->dash_notifier_is_plugin_active($this->_summary['news.plugin'])) { return; } require_once LSCWP_DIR . 'tpl/banner/cloud_news.tpl.php'; } /** * Update latest news * * @since 2.9.9.1 */ private function _update_news() { if (!empty($this->_summary['news.utime']) && time() - $this->_summary['news.utime'] < 86400 * 7) { return; } self::save_summary(array('news.utime' => time())); $data = self::get(self::API_NEWS); if (empty($data['id'])) { return; } // Save news if (!empty($this->_summary['news.id']) && $this->_summary['news.id'] == $data['id']) { return; } $this->_summary['news.id'] = $data['id']; $this->_summary['news.plugin'] = !empty($data['plugin']) ? $data['plugin'] : ''; $this->_summary['news.title'] = !empty($data['title']) ? $data['title'] : ''; $this->_summary['news.content'] = !empty($data['content']) ? $data['content'] : ''; $this->_summary['news.zip'] = !empty($data['zip']) ? $data['zip'] : ''; $this->_summary['news.new'] = 1; if ($this->_summary['news.plugin']) { $plugin_info = Activation::cls()->dash_notifier_get_plugin_info($this->_summary['news.plugin']); if ($plugin_info && !empty($plugin_info->name)) { $this->_summary['news.plugin_name'] = $plugin_info->name; } } self::save_summary(); } /** * Check if contains a package in a service or not * * @since 4.0 */ public function has_pkg($service, $pkg) { if (!empty($this->_summary['usage.' . $service]['pkgs']) && $this->_summary['usage.' . $service]['pkgs'] & $pkg) { return true; } return false; } /** * Get allowance of current service * * @since 3.0 * @access private */ public function allowance($service, &$err = false) { // Only auto sync usage at most one time per day if (empty($this->_summary['last_request.' . self::SVC_D_USAGE]) || time() - $this->_summary['last_request.' . self::SVC_D_USAGE] > 86400) { $this->sync_usage(); } if (in_array($service, array(self::SVC_CCSS, self::SVC_UCSS, self::SVC_VPI))) { // @since 4.2 $service = self::SVC_PAGE_OPTM; } if (empty($this->_summary['usage.' . $service])) { return 0; } $usage = $this->_summary['usage.' . $service]; // Image optm is always free $allowance_max = 0; if ($service == self::SVC_IMG_OPTM) { $allowance_max = self::IMG_OPTM_DEFAULT_GROUP; } $allowance = $usage['quota'] - $usage['used']; $err = 'out_of_quota'; if ($allowance > 0) { if ($allowance_max && $allowance_max < $allowance) { $allowance = $allowance_max; } // Daily limit @since 4.2 if (isset($usage['remaining_daily_quota']) && $usage['remaining_daily_quota'] >= 0 && $usage['remaining_daily_quota'] < $allowance) { $allowance = $usage['remaining_daily_quota']; if (!$allowance) { $err = 'out_of_daily_quota'; } } return $allowance; } // Check Pay As You Go balance if (empty($usage['pag_bal'])) { return $allowance_max; } if ($allowance_max && $allowance_max < $usage['pag_bal']) { return $allowance_max; } return $usage['pag_bal']; } /** * Sync Cloud usage summary data * * @since 3.0 * @access public */ public function sync_usage() { $usage = $this->_post(self::SVC_D_USAGE); if (!$usage) { return; } self::debug('sync_usage ' . \json_encode($usage)); foreach (self::$SERVICES as $v) { $this->_summary['usage.' . $v] = !empty($usage[$v]) ? $usage[$v] : false; } self::save_summary(); return $this->_summary; } /** * Clear all existing cloud nodes for future reconnect * * @since 3.0 * @access public */ public function clear_cloud() { foreach (self::$SERVICES as $service) { if (isset($this->_summary['server.' . $service])) { unset($this->_summary['server.' . $service]); } if (isset($this->_summary['server_date.' . $service])) { unset($this->_summary['server_date.' . $service]); } } self::save_summary(); self::debug('Cleared all local service node caches'); } /** * ping clouds to find the fastest node * * @since 3.0 * @access public */ public function detect_cloud($service, $force = false) { if (in_array($service, self::$CENTER_SVC_SET)) { return self::CLOUD_SERVER; } if (in_array($service, self::$WP_SVC_SET)) { return self::CLOUD_SERVER_WP; } // Check if the stored server needs to be refreshed if (!$force) { if ( !empty($this->_summary['server.' . $service]) && !empty($this->_summary['server_date.' . $service]) && $this->_summary['server_date.' . $service] > time() - 86400 * self::TTL_NODE ) { $server = $this->_summary['server.' . $service]; if (!strpos(self::CLOUD_SERVER, 'preview.') && !strpos($server, 'preview.')) { return $server; } if (strpos(self::CLOUD_SERVER, 'preview.') && strpos($server, 'preview.')) { return $server; } } } if (!$service || !in_array($service, self::$SERVICES)) { $msg = __('Cloud Error', 'litespeed-cache') . ': ' . $service; Admin_Display::error($msg); return false; } // Send request to Quic Online Service $json = $this->_post(self::SVC_D_NODES, array('svc' => $this->_maybe_queue($service))); // Check if get list correctly if (empty($json['list']) || !is_array($json['list'])) { self::debug('request cloud list failed: ', $json); if ($json) { $msg = __('Cloud Error', 'litespeed-cache') . ": [Service] $service [Info] " . \json_encode($json); Admin_Display::error($msg); } return false; } // Ping closest cloud $valid_clouds = false; if (!empty($json['list_preferred'])) { $valid_clouds = $this->_get_closest_nodes($json['list_preferred'], $service); } if (!$valid_clouds) { $valid_clouds = $this->_get_closest_nodes($json['list'], $service); } if (!$valid_clouds) { return false; } // Check server load if (in_array($service, self::$SERVICES_LOAD_CHECK)) { // TODO $valid_cloud_loads = array(); foreach ($valid_clouds as $k => $v) { $response = wp_safe_remote_get($v, array('timeout' => 5)); if (is_wp_error($response)) { $error_message = $response->get_error_message(); self::debug('failed to do load checker: ' . $error_message); continue; } $curr_load = \json_decode($response['body'], true); if (!empty($curr_load['_res']) && $curr_load['_res'] == 'ok' && isset($curr_load['load'])) { $valid_cloud_loads[$v] = $curr_load['load']; } } if (!$valid_cloud_loads) { $msg = __('Cloud Error', 'litespeed-cache') . ": [Service] $service [Info] " . __('No available Cloud Node after checked server load.', 'litespeed-cache'); Admin_Display::error($msg); return false; } self::debug('Closest nodes list after load check', $valid_cloud_loads); $qualified_list = array_keys($valid_cloud_loads, min($valid_cloud_loads)); } else { $qualified_list = $valid_clouds; } $closest = $qualified_list[array_rand($qualified_list)]; self::debug('Chose node: ' . $closest); // store data into option locally $this->_summary['server.' . $service] = $closest; $this->_summary['server_date.' . $service] = time(); self::save_summary(); return $this->_summary['server.' . $service]; } /** * Ping to choose the closest nodes * @since 7.0 */ private function _get_closest_nodes($list, $service) { $speed_list = array(); foreach ($list as $v) { // Exclude possible failed 503 nodes if (!empty($this->_summary['disabled_node']) && !empty($this->_summary['disabled_node'][$v]) && time() - $this->_summary['disabled_node'][$v] < 86400) { continue; } $speed_list[$v] = Utility::ping($v); } if (!$speed_list) { self::debug('nodes are in 503 failed nodes'); return false; } $min = min($speed_list); if ($min == 99999) { self::debug('failed to ping all clouds'); return false; } // Random pick same time range ip (230ms 250ms) $range_len = strlen($min); $range_num = substr($min, 0, 1); $valid_clouds = array(); foreach ($speed_list as $node => $speed) { if (strlen($speed) == $range_len && substr($speed, 0, 1) == $range_num) { $valid_clouds[] = $node; } // Append the lower speed ones elseif ($speed < $min * 4) { $valid_clouds[] = $node; } } if (!$valid_clouds) { $msg = __('Cloud Error', 'litespeed-cache') . ": [Service] $service [Info] " . __('No available Cloud Node.', 'litespeed-cache'); Admin_Display::error($msg); return false; } self::debug('Closest nodes list', $valid_clouds); return $valid_clouds; } /** * May need to convert to queue service */ private function _maybe_queue($service) { if (in_array($service, self::$_QUEUE_SVC_SET)) { return self::SVC_QUEUE; } return $service; } /** * Get data from QUIC cloud server * * @since 3.0 * @access public */ public static function get($service, $data = array()) { $instance = self::cls(); return $instance->_get($service, $data); } /** * Get data from QUIC cloud server * * @since 3.0 * @access private */ private function _get($service, $data = false) { $service_tag = $service; if (!empty($data['action'])) { $service_tag .= '-' . $data['action']; } $maybe_cloud = $this->_maybe_cloud($service_tag); if (!$maybe_cloud || $maybe_cloud === 'svc_hot') { return $maybe_cloud; } $server = $this->detect_cloud($service); if (!$server) { return; } $url = $server . '/' . $service; $param = array( 'site_url' => home_url(), 'main_domain' => !empty($this->_summary['main_domain']) ? $this->_summary['main_domain'] : '', 'ver' => Core::VER, ); if ($data) { $param['data'] = $data; } $url .= '?' . http_build_query($param); self::debug('getting from : ' . $url); self::save_summary(array('curr_request.' . $service_tag => time())); $response = wp_safe_remote_get($url, array( 'timeout' => 15, 'headers' => array('Accept' => 'application/json'), )); return $this->_parse_response($response, $service, $service_tag, $server); } /** * Check if is able to do cloud request or not * * @since 3.0 * @access private */ private function _maybe_cloud($service_tag) { $home_url = home_url(); if (!wp_http_validate_url($home_url)) { self::debug('wp_http_validate_url failed: ' . $home_url); return false; } // Deny if is IP if (preg_match('#^(([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)$#', Utility::parse_url_safe($home_url, PHP_URL_HOST))) { self::debug('IP home url is not allowed for cloud service.'); $msg = __('In order to use QC services, need a real domain name, cannot use an IP.', 'litespeed-cache'); Admin_Display::error($msg); return false; } /** @since 5.0 If in valid err_domains, bypass request */ if ($this->_is_err_domain($home_url)) { self::debug('home url is in err_domains, bypass request: ' . $home_url); return false; } // we don't want the `img_optm-taken` to fail at any given time if ($service_tag == self::IMGOPTM_TAKEN) { return true; } if ($service_tag == self::SVC_D_SYNC_CONF && !$this->activated()) { self::debug('Skip sync conf as QC not activated yet.'); return false; } // Check TTL if (!empty($this->_summary['ttl.' . $service_tag])) { $ttl = $this->_summary['ttl.' . $service_tag] - time(); if ($ttl > 0) { self::debug('❌ TTL limit. [srv] ' . $service_tag . ' [TTL cool down] ' . $ttl . ' seconds'); return 'svc_hot'; } } $expiration_req = self::EXPIRATION_REQ; // Limit frequent unfinished request to 5min $timestamp_tag = 'curr_request.'; if ($service_tag == self::SVC_IMG_OPTM . '-' . Img_Optm::TYPE_NEW_REQ) { $timestamp_tag = 'last_request.'; } else { // For all other requests, if is under debug mode, will always allow if ($this->conf(self::O_DEBUG)) { return true; } } if (!empty($this->_summary[$timestamp_tag . $service_tag])) { $expired = $this->_summary[$timestamp_tag . $service_tag] + $expiration_req - time(); if ($expired > 0) { self::debug("❌ try [$service_tag] after $expired seconds"); if ($service_tag !== self::API_VER) { $msg = __('Cloud Error', 'litespeed-cache') . ': ' . sprintf(__('Please try after %1$s for service %2$s.', 'litespeed-cache'), Utility::readable_time($expired, 0, true), '<code>' . $service_tag . '</code>'); Admin_Display::error(array('cloud_trylater' => $msg)); } return false; } } if (in_array($service_tag, self::$_PUB_SVC_SET)) { return true; } if (!$this->activated() && $service_tag != self::SVC_D_ACTIVATE) { Admin_Display::error(Error::msg('qc_setup_required')); return false; } return true; } /** * Check if a service tag ttl is valid or not * @since 7.1 */ public function service_hot($service_tag) { if (empty($this->_summary['ttl.' . $service_tag])) { return false; } $ttl = $this->_summary['ttl.' . $service_tag] - time(); if ($ttl <= 0) { return false; } return $ttl; } /** * Check if activated QUIC.cloud service or not * * @since 7.0 * @access public */ public function activated() { return !empty($this->_summary['sk_b64']) && !empty($this->_summary['qc_activated']); } /** * Show my.qc quick link to the domain page */ public function qc_link() { $data = array( 'site_url' => home_url(), 'ver' => LSCWP_V, 'ref' => $this->_get_ref_url(), ); return self::CLOUD_SERVER_DASH . '/u/wp3/manage?data=' . urlencode(Utility::arr2str($data)); // . (!empty($this->_summary['is_linked']) ? '?wplogin=1' : ''); } /** * Post data to QUIC.cloud server * * @since 3.0 * @access public */ public static function post($service, $data = false, $time_out = false) { $instance = self::cls(); return $instance->_post($service, $data, $time_out); } /** * Post data to cloud server * * @since 3.0 * @access private */ private function _post($service, $data = false, $time_out = false) { $service_tag = $service; if (!empty($data['action'])) { $service_tag .= '-' . $data['action']; } $maybe_cloud = $this->_maybe_cloud($service_tag); if (!$maybe_cloud || $maybe_cloud === 'svc_hot') { self::debug('Maybe cloud failed: ' . var_export($maybe_cloud, true)); return $maybe_cloud; } $server = $this->detect_cloud($service); if (!$server) { return; } $url = $server . '/' . $this->_maybe_queue($service); self::debug('posting to : ' . $url); if ($data) { $data['service_type'] = $service; // For queue distribution usage } // Encrypt service as signature // $signature_ts = time(); // $sign_data = array( // 'service_tag' => $service_tag, // 'ts' => $signature_ts, // ); // $data['signature_b64'] = $this->_sign_b64(implode('', $sign_data)); // $data['signature_ts'] = $signature_ts; self::debug('data', $data); $param = array( 'site_url' => home_url(), // Need to use home_url() as WPML case may change it for diff langs, therefore we can do auto alias 'main_domain' => !empty($this->_summary['main_domain']) ? $this->_summary['main_domain'] : '', 'wp_pk_b64' => !empty($this->_summary['pk_b64']) ? $this->_summary['pk_b64'] : '', 'ver' => Core::VER, 'data' => $data, ); self::save_summary(array('curr_request.' . $service_tag => time())); $response = wp_safe_remote_post($url, array( 'body' => $param, 'timeout' => $time_out ?: 15, 'headers' => array('Accept' => 'application/json', 'Expect' => ''), )); return $this->_parse_response($response, $service, $service_tag, $server); } /** * Parse response JSON * Mark the request successful if the response status is ok * * @since 3.0 */ private function _parse_response($response, $service, $service_tag, $server) { // If show the error or not if failed $visible_err = $service !== self::API_VER && $service !== self::API_NEWS && $service !== self::SVC_D_DASH; if (is_wp_error($response)) { $error_message = $response->get_error_message(); self::debug('failed to request: ' . $error_message); if ($visible_err) { $msg = __('Failed to request via WordPress', 'litespeed-cache') . ': ' . $error_message . " [server] $server [service] $service"; Admin_Display::error($msg); // Tmp disabled this node from reusing in 1 day if (empty($this->_summary['disabled_node'])) { $this->_summary['disabled_node'] = array(); } $this->_summary['disabled_node'][$server] = time(); self::save_summary(); // Force redetect node self::debug('Node error, redetecting node [svc] ' . $service); $this->detect_cloud($service, true); } return false; } $json = \json_decode($response['body'], true); if (!is_array($json)) { self::debugErr('failed to decode response json: ' . $response['body']); if ($visible_err) { $msg = __('Failed to request via WordPress', 'litespeed-cache') . ': ' . $response['body'] . " [server] $server [service] $service"; Admin_Display::error($msg); // Tmp disabled this node from reusing in 1 day if (empty($this->_summary['disabled_node'])) { $this->_summary['disabled_node'] = array(); } $this->_summary['disabled_node'][$server] = time(); self::save_summary(); // Force redetect node self::debugErr('Node error, redetecting node [svc] ' . $service); $this->detect_cloud($service, true); } return false; } // Check and save TTL data if (!empty($json['_ttl'])) { $ttl = intval($json['_ttl']); self::debug('Service TTL to save: ' . $ttl); if ($ttl > 0 && $ttl < 86400) { self::save_summary(array( 'ttl.' . $service_tag => $ttl + time(), )); } } if (!empty($json['_code'])) { self::debugErr('Hit err _code: ' . $json['_code']); if ($json['_code'] == 'unpulled_images') { $msg = __('Cloud server refused the current request due to unpulled images. Please pull the images first.', 'litespeed-cache'); Admin_Display::error($msg); return false; } if ($json['_code'] == 'blocklisted') { $msg = __('Your domain_key has been temporarily blocklisted to prevent abuse. You may contact support at QUIC.cloud to learn more.', 'litespeed-cache'); Admin_Display::error($msg); return false; } if ($json['_code'] == 'rate_limit') { self::debugErr('Cloud server rate limit exceeded.'); $msg = __('Cloud server refused the current request due to rate limiting. Please try again later.', 'litespeed-cache'); Admin_Display::error($msg); return false; } if ($json['_code'] == 'heavy_load' || $json['_code'] == 'redetect_node') { // Force redetect node self::debugErr('Node redetecting node [svc] ' . $service); Admin_Display::info(__('Redetected node', 'litespeed-cache') . ': ' . Error::msg($json['_code'])); $this->detect_cloud($service, true); } } if (!empty($json['_503'])) { self::debugErr('service 503 unavailable temporarily. ' . $json['_503']); $msg = __( 'We are working hard to improve your online service experience. The service will be unavailable while we work. We apologize for any inconvenience.', 'litespeed-cache' ); $msg .= ' ' . $json['_503'] . " [server] $server [service] $service"; Admin_Display::error($msg); // Force redetect node self::debugErr('Node error, redetecting node [svc] ' . $service); $this->detect_cloud($service, true); return false; } list($json, $return) = $this->extract_msg($json, $service, $server); if ($return) { return false; } self::save_summary(array( 'last_request.' . $service_tag => $this->_summary['curr_request.' . $service_tag], 'curr_request.' . $service_tag => 0, )); if ($json) { self::debug2('response ok', $json); } else { self::debug2('response ok'); } // Only successful request return Array return $json; } /** * Extract msg from json * @since 5.0 */ public function extract_msg($json, $service, $server = false, $is_callback = false) { if (!empty($json['_info'])) { self::debug('_info: ' . $json['_info']); $msg = __('Message from QUIC.cloud server', 'litespeed-cache') . ': ' . $json['_info']; $msg .= $this->_parse_link($json); Admin_Display::info($msg); unset($json['_info']); } if (!empty($json['_note'])) { self::debug('_note: ' . $json['_note']); $msg = __('Message from QUIC.cloud server', 'litespeed-cache') . ': ' . $json['_note']; $msg .= $this->_parse_link($json); Admin_Display::note($msg); unset($json['_note']); } if (!empty($json['_success'])) { self::debug('_success: ' . $json['_success']); $msg = __('Good news from QUIC.cloud server', 'litespeed-cache') . ': ' . $json['_success']; $msg .= $this->_parse_link($json); Admin_Display::success($msg); unset($json['_success']); } // Upgrade is required if (!empty($json['_err_req_v'])) { self::debug('_err_req_v: ' . $json['_err_req_v']); $msg = sprintf(__('%1$s plugin version %2$s required for this action.', 'litespeed-cache'), Core::NAME, 'v' . $json['_err_req_v'] . '+') . " [server] $server [service] $service"; // Append upgrade link $msg2 = ' ' . GUI::plugin_upgrade_link(Core::NAME, Core::PLUGIN_NAME, $json['_err_req_v']); $msg2 .= $this->_parse_link($json); Admin_Display::error($msg . $msg2); return array($json, true); } // Parse _carry_on info if (!empty($json['_carry_on'])) { self::debug('Carry_on usage', $json['_carry_on']); // Store generic info foreach (array('usage', 'promo', 'mini_html', 'partner', '_error', '_info', '_note', '_success') as $v) { if (isset($json['_carry_on'][$v])) { switch ($v) { case 'usage': $usage_svc_tag = in_array($service, array(self::SVC_CCSS, self::SVC_UCSS, self::SVC_VPI)) ? self::SVC_PAGE_OPTM : $service; $this->_summary['usage.' . $usage_svc_tag] = $json['_carry_on'][$v]; break; case 'promo': if (empty($this->_summary[$v]) || !is_array($this->_summary[$v])) { $this->_summary[$v] = array(); } $this->_summary[$v][] = $json['_carry_on'][$v]; break; case 'mini_html': foreach ($json['_carry_on'][$v] as $k2 => $v2) { if (strpos($k2, 'ttl.') === 0) { $v2 += time(); } $this->_summary[$v][$k2] = $v2; } break; case 'partner': $this->_summary[$v] = $json['_carry_on'][$v]; break; case '_error': case '_info': case '_note': case '_success': $color_mode = substr($v, 1); $msgs = $json['_carry_on'][$v]; Admin_Display::add_unique_notice($color_mode, $msgs, true); break; default: break; } } } self::save_summary(); unset($json['_carry_on']); } // Parse general error msg if (!$is_callback && (empty($json['_res']) || $json['_res'] !== 'ok')) { $json_msg = !empty($json['_msg']) ? $json['_msg'] : 'unknown'; self::debug('❌ _err: ' . $json_msg, $json); $str_translated = Error::msg($json_msg); $msg = __('Failed to communicate with QUIC.cloud server', 'litespeed-cache') . ': ' . $str_translated . " [server] $server [service] $service"; $msg .= $this->_parse_link($json); $visible_err = $service !== self::API_VER && $service !== self::API_NEWS && $service !== self::SVC_D_DASH; if ($visible_err) { Admin_Display::error($msg); } // QC may try auto alias /** @since 5.0 Store the domain as `err_domains` only for QC auto alias feature */ if ($json_msg == 'err_alias') { if (empty($this->_summary['err_domains'])) { $this->_summary['err_domains'] = array(); } $home_url = home_url(); if (!array_key_exists($home_url, $this->_summary['err_domains'])) { $this->_summary['err_domains'][$home_url] = time(); } self::save_summary(); } // Site not on QC, delete invalid domain key if ($json_msg == 'site_not_registered' || $json_msg == 'err_key') { $this->_reset_qc_reg(); } return array($json, true); } unset($json['_res']); if (!empty($json['_msg'])) { unset($json['_msg']); } return array($json, false); } /** * Clear QC linked status * @since 5.0 */ private function _reset_qc_reg() { unset($this->_summary['qc_activated']); if (!empty($this->_summary['partner'])) { unset($this->_summary['partner']); } self::save_summary(); $msg = $this->_reset_qc_reg_content(); Admin_Display::error($msg, false, true); } private function _reset_qc_reg_content() { $msg = __('Site not recognized. QUIC.cloud deactivated automatically. Please reactivate your QUIC.cloud account.', 'litespeed-cache'); $msg .= Doc::learn_more(admin_url('admin.php?page=litespeed'), __('Click here to proceed.', 'litespeed-cache'), true, false, true); $msg .= Doc::learn_more('https://docs.litespeedtech.com/lscache/lscwp/general/', false, false, false, true); return $msg; } private function _clear_reset_qc_reg_msg() { self::debug('Removed pinned reset QC reg content msg'); $msg = $this->_reset_qc_reg_content(); Admin_Display::dismiss_pin_by_content($msg, Admin_Display::NOTICE_RED, true); } /** * REST call: check if the error domain is valid call for auto alias purpose * @since 5.0 */ public function rest_err_domains() { if (empty($_POST['main_domain']) || empty($_POST['alias'])) { return self::err('lack_of_param'); } $this->extract_msg($_POST, 'Quic.cloud', false, true); if ($this->_is_err_domain($_POST['alias'])) { if ($_POST['alias'] == home_url()) { $this->_remove_domain_from_err_list($_POST['alias']); } return self::ok(); } return self::err('Not an alias req from here'); } /** * Remove a domain from err domain * @since 5.0 */ private function _remove_domain_from_err_list($url) { unset($this->_summary['err_domains'][$url]); self::save_summary(); } /** * Check if is err domain * @since 5.0 */ private function _is_err_domain($home_url) { if (empty($this->_summary['err_domains'])) { return false; } if (!array_key_exists($home_url, $this->_summary['err_domains'])) { return false; } // Auto delete if too long ago if (time() - $this->_summary['err_domains'][$home_url] > 86400 * 10) { $this->_remove_domain_from_err_list($home_url); return false; } if (time() - $this->_summary['err_domains'][$home_url] > 86400) { return false; } return true; } /** * Show promo from cloud * * @since 3.0 * @access public */ public function show_promo() { if (empty($this->_summary['promo'])) { return; } require_once LSCWP_DIR . 'tpl/banner/cloud_promo.tpl.php'; } /** * Clear promo from cloud * * @since 3.0 * @access private */ private function _clear_promo() { if (count($this->_summary['promo']) > 1) { array_shift($this->_summary['promo']); } else { $this->_summary['promo'] = array(); } self::save_summary(); } /** * Parse _links from json * * @since 1.6.5 * @since 1.6.7 Self clean the parameter * @access private */ private function _parse_link(&$json) { $msg = ''; if (!empty($json['_links'])) { foreach ($json['_links'] as $v) { $msg .= ' ' . sprintf('<a href="%s" class="%s" target="_blank">%s</a>', $v['link'], !empty($v['cls']) ? $v['cls'] : '', $v['title']); } unset($json['_links']); } return $msg; } /** * Request callback validation from Cloud * * @since 3.0 * @access public */ public function ip_validate() { if (empty($_POST['hash'])) { return self::err('lack_of_params'); } if ($_POST['hash'] != md5(substr($this->_summary['pk_b64'], 0, 4))) { self::debug('__callback IP request decryption failed'); return self::err('err_hash'); } Control::set_nocache('Cloud IP hash validation'); $resp_hash = md5(substr($this->_summary['pk_b64'], 2, 4)); self::debug('__callback IP request hash: ' . $resp_hash); return self::ok(array('hash' => $resp_hash)); } /** * Check if this visit is from cloud or not * * @since 3.0 */ public function is_from_cloud() { // return true; $check_point = time() - 86400 * self::TTL_IPS; if (empty($this->_summary['ips']) || empty($this->_summary['ips_ts']) || $this->_summary['ips_ts'] < $check_point) { self::debug('Force updating ip as ips_ts is older than ' . self::TTL_IPS . ' days'); $this->_update_ips(); } $res = $this->cls('Router')->ip_access($this->_summary['ips']); if (!$res) { self::debug('❌ Not our cloud IP'); // Auto check ip list again but need an interval limit safety. if (empty($this->_summary['ips_ts_runner']) || time() - $this->_summary['ips_ts_runner'] > 600) { self::debug('Force updating ip as ips_ts_runner is older than 10mins'); // Refresh IP list for future detection $this->_update_ips(); $res = $this->cls('Router')->ip_access($this->_summary['ips']); if (!$res) { self::debug('❌ 2nd time: Not our cloud IP'); } else { self::debug('✅ Passed Cloud IP verification'); } return $res; } } else { self::debug('✅ Passed Cloud IP verification'); } return $res; } /** * Update Cloud IP list * * @since 4.2 */ private function _update_ips() { self::debug('Load remote Cloud IP list from ' . self::CLOUD_IPS); // Prevent multiple call in a short period self::save_summary(array('ips_ts' => time(), 'ips_ts_runner' => time())); $response = wp_safe_remote_get(self::CLOUD_IPS . '?json'); if (is_wp_error($response)) { $error_message = $response->get_error_message(); self::debug('failed to get ip whitelist: ' . $error_message); throw new \Exception('Failed to fetch QUIC.cloud whitelist ' . $error_message); } $json = \json_decode($response['body'], true); self::debug('Load ips', $json); self::save_summary(array('ips' => $json)); } /** * Return succeeded response * * @since 3.0 */ public static function ok($data = array()) { $data['_res'] = 'ok'; return $data; } /** * Return error * * @since 3.0 */ public static function err($code) { self::debug("❌ Error response code: $code"); return array('_res' => 'err', '_msg' => $code); } /** * Return pong for ping to check PHP function availability * @since 6.5 */ public function ping() { $resp = array( 'v_lscwp' => Core::VER, 'v_php' => PHP_VERSION, 'v_wp' => $GLOBALS['wp_version'], 'home_url' => home_url(), ); if (!empty($_POST['funcs'])) { foreach ($_POST['funcs'] as $v) { $resp[$v] = function_exists($v) ? 'y' : 'n'; } } if (!empty($_POST['classes'])) { foreach ($_POST['classes'] as $v) { $resp[$v] = class_exists($v) ? 'y' : 'n'; } } if (!empty($_POST['consts'])) { foreach ($_POST['consts'] as $v) { $resp[$v] = defined($v) ? 'y' : 'n'; } } return self::ok($resp); } /** * Display a banner for dev env if using preview QC node. * @since 7.0 */ public function maybe_preview_banner() { if (strpos(self::CLOUD_SERVER, 'preview.')) { Admin_Display::note(__('Linked to QUIC.cloud preview environment, for testing purpose only.', 'litespeed-cache'), true, true, 'litespeed-warning-bg'); } } /** * Handle all request actions from main cls * * @since 3.0 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_CLEAR_CLOUD: $this->clear_cloud(); break; case self::TYPE_REDETECT_CLOUD: if (!empty($_GET['svc'])) { $this->detect_cloud($_GET['svc'], true); } break; case self::TYPE_CLEAR_PROMO: $this->_clear_promo(); break; case self::TYPE_RESET: $this->reset_qc(); break; case self::TYPE_ACTIVATE: $this->init_qc(); break; case self::TYPE_LINK: $this->link_qc(); break; case self::TYPE_ENABLE_CDN: $this->enable_cdn(); break; case self::TYPE_API: if (!empty($_GET['action2'])) { $this->api_link_call($_GET['action2']); } break; case self::TYPE_SYNC_STATUS: $this->load_qc_status_for_dash('cdn_dash', true); $msg = __('Sync QUIC.cloud status successfully.', 'litespeed-cache'); Admin_Display::success($msg); break; case self::TYPE_SYNC_USAGE: $this->sync_usage(); $msg = __('Sync credit allowance with Cloud Server successfully.', 'litespeed-cache'); Admin_Display::success($msg); break; default: break; } Admin::redirect(); } } core.cls.php 0000644 00000047526 15162226263 0007010 0 ustar 00 <?php /** * The core plugin class. * * Note: Core doesn't allow $this->cls( 'Core' ) * * @since 1.0.0 */ namespace LiteSpeed; defined('WPINC') || exit(); class Core extends Root { const NAME = 'LiteSpeed Cache'; const PLUGIN_NAME = 'litespeed-cache'; const PLUGIN_FILE = 'litespeed-cache/litespeed-cache.php'; const VER = LSCWP_V; const ACTION_DISMISS = 'dismiss'; const ACTION_PURGE_BY = 'PURGE_BY'; const ACTION_PURGE_EMPTYCACHE = 'PURGE_EMPTYCACHE'; const ACTION_QS_PURGE = 'PURGE'; const ACTION_QS_PURGE_SINGLE = 'PURGESINGLE'; // This will be same as `ACTION_QS_PURGE` (purge single url only) const ACTION_QS_SHOW_HEADERS = 'SHOWHEADERS'; const ACTION_QS_PURGE_ALL = 'purge_all'; const ACTION_QS_PURGE_EMPTYCACHE = 'empty_all'; const ACTION_QS_NOCACHE = 'NOCACHE'; const HEADER_DEBUG = 'X-LiteSpeed-Debug'; protected static $_debug_show_header = false; private $_footer_comment = ''; /** * Define the core functionality of the plugin. * * Set the plugin name and the plugin version that can be used throughout the plugin. * Load the dependencies, define the locale, and set the hooks for the admin area and * the public-facing side of the site. * * @since 1.0.0 */ public function __construct() { !defined('LSCWP_TS_0') && define('LSCWP_TS_0', microtime(true)); $this->cls('Conf')->init(); /** * Load API hooks * @since 3.0 */ $this->cls('API')->init(); if (defined('LITESPEED_ON')) { // Load third party detection if lscache enabled. include_once LSCWP_DIR . 'thirdparty/entry.inc.php'; } if ($this->conf(Base::O_DEBUG_DISABLE_ALL)) { !defined('LITESPEED_DISABLE_ALL') && define('LITESPEED_DISABLE_ALL', true); } /** * Register plugin activate/deactivate/uninstall hooks * NOTE: this can't be moved under after_setup_theme, otherwise activation will be bypassed somehow * @since 2.7.1 Disabled admin&CLI check to make frontend able to enable cache too */ // if( is_admin() || defined( 'LITESPEED_CLI' ) ) { $plugin_file = LSCWP_DIR . 'litespeed-cache.php'; register_activation_hook($plugin_file, array(__NAMESPACE__ . '\Activation', 'register_activation')); register_deactivation_hook($plugin_file, array(__NAMESPACE__ . '\Activation', 'register_deactivation')); register_uninstall_hook($plugin_file, __NAMESPACE__ . '\Activation::uninstall_litespeed_cache'); // } if (defined('LITESPEED_ON')) { // register purge_all actions $purge_all_events = $this->conf(Base::O_PURGE_HOOK_ALL); // purge all on upgrade if ($this->conf(Base::O_PURGE_ON_UPGRADE)) { $purge_all_events[] = 'automatic_updates_complete'; $purge_all_events[] = 'upgrader_process_complete'; $purge_all_events[] = 'admin_action_do-plugin-upgrade'; } foreach ($purge_all_events as $event) { // Don't allow hook to update_option bcos purge_all will cause infinite loop of update_option if (in_array($event, array('update_option'))) { continue; } add_action($event, __NAMESPACE__ . '\Purge::purge_all'); } // add_filter( 'upgrader_pre_download', 'Purge::filter_with_purge_all' ); // Add headers to site health check for full page cache // @since 5.4 add_filter('site_status_page_cache_supported_cache_headers', function ($cache_headers) { $is_cache_hit = function ($header_value) { return false !== strpos(strtolower($header_value), 'hit'); }; $cache_headers['x-litespeed-cache'] = $is_cache_hit; $cache_headers['x-lsadc-cache'] = $is_cache_hit; $cache_headers['x-qc-cache'] = $is_cache_hit; return $cache_headers; }); } add_action('after_setup_theme', array($this, 'init')); // Check if there is a purge request in queue if (!defined('LITESPEED_CLI')) { $purge_queue = Purge::get_option(Purge::DB_QUEUE); if ($purge_queue && $purge_queue != -1) { $this->_http_header($purge_queue); Debug2::debug('[Core] Purge Queue found&sent: ' . $purge_queue); } if ($purge_queue != -1) { Purge::update_option(Purge::DB_QUEUE, -1); // Use 0 to bypass purge while still enable db update as WP's update_option will check value===false to bypass update } $purge_queue = Purge::get_option(Purge::DB_QUEUE2); if ($purge_queue && $purge_queue != -1) { $this->_http_header($purge_queue); Debug2::debug('[Core] Purge2 Queue found&sent: ' . $purge_queue); } if ($purge_queue != -1) { Purge::update_option(Purge::DB_QUEUE2, -1); } } /** * Hook internal REST * @since 2.9.4 */ $this->cls('REST'); /** * Hook wpnonce function * * Note: ESI nonce won't be available until hook after_setup_theme ESI init due to Guest Mode concern * @since v4.1 */ if ($this->cls('Router')->esi_enabled() && !function_exists('wp_create_nonce')) { Debug2::debug('[ESI] Overwrite wp_create_nonce()'); litespeed_define_nonce_func(); } } /** * The plugin initializer. * * This function checks if the cache is enabled and ready to use, then determines what actions need to be set up based on the type of user and page accessed. Output is buffered if the cache is enabled. * * NOTE: WP user doesn't init yet * * @since 1.0.0 * @access public */ public function init() { /** * Added hook before init * 3rd party preload hooks will be fired here too (e.g. Divi disable all in edit mode) * @since 1.6.6 * @since 2.6 Added filter to all config values in Conf */ do_action('litespeed_init'); add_action('wp_ajax_async_litespeed', 'LiteSpeed\Task::async_litespeed_handler'); add_action('wp_ajax_nopriv_async_litespeed', 'LiteSpeed\Task::async_litespeed_handler'); // in `after_setup_theme`, before `init` hook $this->cls('Activation')->auto_update(); if (is_admin() && !(defined('DOING_AJAX') && DOING_AJAX)) { $this->cls('Admin'); } if (defined('LITESPEED_DISABLE_ALL') && LITESPEED_DISABLE_ALL) { Debug2::debug('[Core] Bypassed due to debug disable all setting'); return; } do_action('litespeed_initing'); ob_start(array($this, 'send_headers_force')); add_action('shutdown', array($this, 'send_headers'), 0); add_action('wp_footer', array($this, 'footer_hook')); /** * Check if is non optm simulator * @since 2.9 */ if (!empty($_GET[Router::ACTION]) && $_GET[Router::ACTION] == 'before_optm' && !apply_filters('litespeed_qs_forbidden', false)) { Debug2::debug('[Core] ⛑️ bypass_optm due to QS CTRL'); !defined('LITESPEED_NO_OPTM') && define('LITESPEED_NO_OPTM', true); } /** * Register vary filter * @since 1.6.2 */ $this->cls('Control')->init(); // 1. Init vary // 2. Init cacheable status // $this->cls('Vary')->init(); // Init Purge hooks $this->cls('Purge')->init(); $this->cls('Tag')->init(); // Load hooks that may be related to users add_action('init', array($this, 'after_user_init'), 5); // Load 3rd party hooks add_action('wp_loaded', array($this, 'load_thirdparty'), 2); // test: Simulate a purge all // if (defined( 'LITESPEED_CLI' )) Purge::add('test'.date('Ymd.His')); } /** * Run hooks after user init * * @since 2.9.8 * @access public */ public function after_user_init() { $this->cls('Router')->is_role_simulation(); // Detect if is Guest mode or not also $this->cls('Vary')->after_user_init(); /** * Preload ESI functionality for ESI request uri recovery * @since 1.8.1 * @since 4.0 ESI init needs to be after Guest mode detection to bypass ESI if is under Guest mode */ $this->cls('ESI')->init(); if (!is_admin() && !defined('LITESPEED_GUEST_OPTM') && ($result = $this->cls('Conf')->in_optm_exc_roles())) { Debug2::debug('[Core] ⛑️ bypass_optm: hit Role Excludes setting: ' . $result); !defined('LITESPEED_NO_OPTM') && define('LITESPEED_NO_OPTM', true); } // Heartbeat control $this->cls('Tool')->heartbeat(); /** * Backward compatibility for v4.2- @Ruikai * TODO: Will change to hook in future versions to make it revertable */ if (defined('LITESPEED_BYPASS_OPTM') && !defined('LITESPEED_NO_OPTM')) { define('LITESPEED_NO_OPTM', LITESPEED_BYPASS_OPTM); } if (!defined('LITESPEED_NO_OPTM') || !LITESPEED_NO_OPTM) { // Check missing static files $this->cls('Router')->serve_static(); $this->cls('Media')->init(); $this->cls('Placeholder')->init(); $this->cls('Router')->can_optm() && $this->cls('Optimize')->init(); $this->cls('Localization')->init(); // Hook cdn for attachments $this->cls('CDN')->init(); // load cron tasks $this->cls('Task')->init(); } // load litespeed actions if ($action = Router::get_action()) { $this->proceed_action($action); } // Load frontend GUI if (!is_admin()) { $this->cls('GUI')->init(); } } /** * Run frontend actions * * @since 1.1.0 * @access public */ public function proceed_action($action) { $msg = false; // handle actions switch ($action) { case self::ACTION_QS_SHOW_HEADERS: self::$_debug_show_header = true; break; case self::ACTION_QS_PURGE: case self::ACTION_QS_PURGE_SINGLE: Purge::set_purge_single(); break; case self::ACTION_QS_PURGE_ALL: Purge::purge_all(); break; case self::ACTION_PURGE_EMPTYCACHE: case self::ACTION_QS_PURGE_EMPTYCACHE: define('LSWCP_EMPTYCACHE', true); // clear all sites caches Purge::purge_all(); $msg = __('Notified LiteSpeed Web Server to purge everything.', 'litespeed-cache'); break; case self::ACTION_PURGE_BY: $this->cls('Purge')->purge_list(); $msg = __('Notified LiteSpeed Web Server to purge the list.', 'litespeed-cache'); break; case self::ACTION_DISMISS: // Even its from ajax, we don't need to register wp ajax callback function but directly use our action GUI::dismiss(); break; default: $msg = $this->cls('Router')->handler($action); break; } if ($msg && !Router::is_ajax()) { Admin_Display::add_notice(Admin_Display::NOTICE_GREEN, $msg); Admin::redirect(); return; } if (Router::is_ajax()) { exit(); } } /** * Callback used to call the detect third party action. * * The detect action is used by third party plugin integration classes to determine if they should add the rest of their hooks. * * @since 1.0.5 * @access public */ public function load_thirdparty() { do_action('litespeed_load_thirdparty'); } /** * Mark wp_footer called * * @since 1.3 * @access public */ public function footer_hook() { Debug2::debug('[Core] Footer hook called'); if (!defined('LITESPEED_FOOTER_CALLED')) { define('LITESPEED_FOOTER_CALLED', true); } } /** * Trigger comment info display hook * * @since 1.3 * @access private */ private function _check_is_html($buffer = null) { if (!defined('LITESPEED_FOOTER_CALLED')) { Debug2::debug2('[Core] CHK html bypass: miss footer const'); return; } if (defined('DOING_AJAX')) { Debug2::debug2('[Core] CHK html bypass: doing ajax'); return; } if (defined('DOING_CRON')) { Debug2::debug2('[Core] CHK html bypass: doing cron'); return; } if ($_SERVER['REQUEST_METHOD'] !== 'GET') { Debug2::debug2('[Core] CHK html bypass: not get method ' . $_SERVER['REQUEST_METHOD']); return; } if ($buffer === null) { $buffer = ob_get_contents(); } // double check to make sure it is a html file if (strlen($buffer) > 300) { $buffer = substr($buffer, 0, 300); } if (strstr($buffer, '<!--') !== false) { $buffer = preg_replace('/<!--.*?-->/s', '', $buffer); } $buffer = trim($buffer); $buffer = File::remove_zero_space($buffer); $is_html = stripos($buffer, '<html') === 0 || stripos($buffer, '<!DOCTYPE') === 0; if (!$is_html) { Debug2::debug('[Core] Footer check failed: ' . ob_get_level() . '-' . substr($buffer, 0, 100)); return; } Debug2::debug('[Core] Footer check passed'); if (!defined('LITESPEED_IS_HTML')) { define('LITESPEED_IS_HTML', true); } } /** * For compatibility with those plugins have 'Bad' logic that forced all buffer output even it is NOT their buffer :( * * Usually this is called after send_headers() if following original WP process * * @since 1.1.5 * @access public * @param string $buffer * @return string */ public function send_headers_force($buffer) { $this->_check_is_html($buffer); // Hook to modify buffer before $buffer = apply_filters('litespeed_buffer_before', $buffer); /** * Media: Image lazyload && WebP * GUI: Clean wrapper mainly for esi block NOTE: this needs to be before optimizer to avoid wrapper being removed * Optimize * CDN */ if (!defined('LITESPEED_NO_OPTM') || !LITESPEED_NO_OPTM) { Debug2::debug('[Core] run hook litespeed_buffer_finalize'); $buffer = apply_filters('litespeed_buffer_finalize', $buffer); } /** * Replace ESI preserved list * @since 3.3 Replace this in the end to avoid `Inline JS Defer` or other Page Optm features encoded ESI tags wrongly, which caused LSWS can't recognize ESI */ $buffer = $this->cls('ESI')->finalize($buffer); $this->send_headers(true); // Log ESI nonce buffer empty issue if (defined('LSCACHE_IS_ESI') && strlen($buffer) == 0) { // log ref for debug purpose error_log('ESI buffer empty ' . $_SERVER['REQUEST_URI']); } // Init comment info $running_info_showing = defined('LITESPEED_IS_HTML') || defined('LSCACHE_IS_ESI'); if (defined('LSCACHE_ESI_SILENCE')) { $running_info_showing = false; Debug2::debug('[Core] ESI silence'); } /** * Silence comment for json req * @since 2.9.3 */ if (REST::cls()->is_rest() || Router::is_ajax()) { $running_info_showing = false; Debug2::debug('[Core] Silence Comment due to REST/AJAX'); } $running_info_showing = apply_filters('litespeed_comment', $running_info_showing); if ($running_info_showing) { if ($this->_footer_comment) { $buffer .= $this->_footer_comment; } } /** * If ESI req is JSON, give the content JSON format * @since 2.9.3 * @since 2.9.4 ESI req could be from internal REST call, so moved json_encode out of this cond */ if (defined('LSCACHE_IS_ESI')) { Debug2::debug('[Core] ESI Start 👇'); if (strlen($buffer) > 500) { Debug2::debug(trim(substr($buffer, 0, 500)) . '.....'); } else { Debug2::debug($buffer); } Debug2::debug('[Core] ESI End 👆'); } if (apply_filters('litespeed_is_json', false)) { if (\json_decode($buffer, true) == null) { Debug2::debug('[Core] Buffer converting to JSON'); $buffer = \json_encode($buffer); $buffer = trim($buffer, '"'); } else { Debug2::debug('[Core] JSON Buffer'); } } // Hook to modify buffer after $buffer = apply_filters('litespeed_buffer_after', $buffer); Debug2::ended(); return $buffer; } /** * Sends the headers out at the end of processing the request. * * This will send out all LiteSpeed Cache related response headers needed for the post. * * @since 1.0.5 * @access public * @param boolean $is_forced If the header is sent following our normal finalizing logic */ public function send_headers($is_forced = false) { // Make sure header output only run once if (!defined('LITESPEED_DID_' . __FUNCTION__)) { define('LITESPEED_DID_' . __FUNCTION__, true); } else { return; } // Avoid PHP warning for header sent out already if (headers_sent()) { self::debug('❌ !!! Err: Header sent out already'); return; } $this->_check_is_html(); // NOTE: cache ctrl output needs to be done first, as currently some varies are added in 3rd party hook `litespeed_api_control`. $this->cls('Control')->finalize(); $vary_header = $this->cls('Vary')->finalize(); // If is not cacheable but Admin QS is `purge` or `purgesingle`, `tag` still needs to be generated $tag_header = $this->cls('Tag')->output(); if (!$tag_header && Control::is_cacheable()) { Control::set_nocache('empty tag header'); } // NOTE: `purge` output needs to be after `tag` output as Admin QS may need to send `tag` header $purge_header = Purge::output(); // generate `control` header in the end in case control status is changed by other headers. $control_header = $this->cls('Control')->output(); // Give one more break to avoid ff crash if (!defined('LSCACHE_IS_ESI')) { $this->_footer_comment .= "\n"; } $cache_support = 'supported'; if (defined('LITESPEED_ON')) { $cache_support = Control::is_cacheable() ? 'cached' : 'uncached'; } $this->_comment( sprintf( '%1$s %2$s by LiteSpeed Cache %4$s on %3$s', defined('LSCACHE_IS_ESI') ? 'Block' : 'Page', $cache_support, date('Y-m-d H:i:s', time() + LITESPEED_TIME_OFFSET), self::VER ) ); // send Control header if (defined('LITESPEED_ON') && $control_header) { $this->_http_header($control_header); if (!Control::is_cacheable()) { $this->_http_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0'); // @ref: https://wordpress.org/support/topic/apply_filterslitespeed_control_cacheable-returns-false-for-cacheable/ } if (defined('LSCWP_LOG')) { $this->_comment($control_header); } } // send PURGE header (Always send regardless of cache setting disabled/enabled) if (defined('LITESPEED_ON') && $purge_header) { $this->_http_header($purge_header); Debug2::log_purge($purge_header); if (defined('LSCWP_LOG')) { $this->_comment($purge_header); } } // send Vary header if (defined('LITESPEED_ON') && $vary_header) { $this->_http_header($vary_header); if (defined('LSCWP_LOG')) { $this->_comment($vary_header); } } if (defined('LITESPEED_ON') && defined('LSCWP_LOG')) { $vary = $this->cls('Vary')->finalize_full_varies(); if ($vary) { $this->_comment('Full varies: ' . $vary); } } // Admin QS show header action if (self::$_debug_show_header) { $debug_header = self::HEADER_DEBUG . ': '; if ($control_header) { $debug_header .= $control_header . '; '; } if ($purge_header) { $debug_header .= $purge_header . '; '; } if ($tag_header) { $debug_header .= $tag_header . '; '; } if ($vary_header) { $debug_header .= $vary_header . '; '; } $this->_http_header($debug_header); } else { // Control header if (defined('LITESPEED_ON') && Control::is_cacheable() && $tag_header) { $this->_http_header($tag_header); if (defined('LSCWP_LOG')) { $this->_comment($tag_header); } } } // Object cache _comment if (defined('LSCWP_LOG') && defined('LSCWP_OBJECT_CACHE') && method_exists('WP_Object_Cache', 'debug')) { $this->_comment('Object Cache ' . \WP_Object_Cache::get_instance()->debug()); } if (defined('LITESPEED_GUEST') && LITESPEED_GUEST) { $this->_comment('Guest Mode'); } if (!empty($this->_footer_comment)) { self::debug('[footer comment] ' . $this->_footer_comment); } if ($is_forced) { Debug2::debug('--forced--'); } /** * If is CLI and contains Purge Header, then issue a HTTP req to Purge * @since v5.3 */ if (defined('LITESPEED_CLI')) { $purge_queue = Purge::get_option(Purge::DB_QUEUE); if (!$purge_queue || $purge_queue == -1) { $purge_queue = Purge::get_option(Purge::DB_QUEUE2); } if ($purge_queue && $purge_queue != -1) { self::debug('[Core] Purge Queue found, issue a HTTP req to purge: ' . $purge_queue); // Kick off HTTP req $url = admin_url('admin-ajax.php'); $resp = wp_safe_remote_get($url); if (is_wp_error($resp)) { $error_message = $resp->get_error_message(); self::debug('[URL]' . $url); self::debug('failed to request: ' . $error_message); } else { self::debug('HTTP req res: ' . $resp['body']); } } } } /** * Append one HTML comment * @since 5.5 */ public static function comment($data) { self::cls()->_comment($data); } private function _comment($data) { $this->_footer_comment .= "\n<!-- " . $data . ' -->'; } /** * Send HTTP header * @since 5.3 */ private function _http_header($header) { if (defined('LITESPEED_CLI')) { return; } @header($header); if (!defined('LSCWP_LOG')) { return; } Debug2::debug('💰 ' . $header); } } root.cls.php 0000644 00000031435 15162226264 0007034 0 ustar 00 <?php /** * The abstract instance * * @since 3.0 */ namespace LiteSpeed; defined('WPINC') || exit(); abstract class Root { const CONF_FILE = '.litespeed_conf.dat'; // Instance set private static $_instances; private static $_options = array(); private static $_const_options = array(); private static $_primary_options = array(); private static $_network_options = array(); /** * Check if need to separate ccss for mobile * * @since 4.7 * @access protected */ protected function _separate_mobile() { return (wp_is_mobile() || apply_filters('litespeed_is_mobile', false)) && $this->conf(Base::O_CACHE_MOBILE); } /** * Log an error message * * @since 7.0 */ public static function debugErr($msg, $backtrace_limit = false) { $msg = '❌ ' . $msg; self::debug($msg, $backtrace_limit); } /** * Log a debug message. * * @since 4.4 * @access public */ public static function debug($msg, $backtrace_limit = false) { if (!defined('LSCWP_LOG')) { return; } if (defined('static::LOG_TAG')) { $msg = static::LOG_TAG . ' ' . $msg; } Debug2::debug($msg, $backtrace_limit); } /** * Log an advanced debug message. * * @since 4.4 * @access public */ public static function debug2($msg, $backtrace_limit = false) { if (!defined('LSCWP_LOG_MORE')) { return; } if (defined('static::LOG_TAG')) { $msg = static::LOG_TAG . ' ' . $msg; } Debug2::debug2($msg, $backtrace_limit); } /** * Check if there is cache folder for that type * * @since 3.0 */ public function has_cache_folder($type) { $subsite_id = is_multisite() && !is_network_admin() ? get_current_blog_id() : ''; if (file_exists(LITESPEED_STATIC_DIR . '/' . $type . '/' . $subsite_id)) { return true; } return false; } /** * Maybe make the cache folder if not existed * * @since 4.4.2 */ protected function _maybe_mk_cache_folder($type) { if (!$this->has_cache_folder($type)) { $subsite_id = is_multisite() && !is_network_admin() ? get_current_blog_id() : ''; $path = LITESPEED_STATIC_DIR . '/' . $type . '/' . $subsite_id; mkdir($path, 0755, true); } } /** * Delete file-based cache folder for that type * * @since 3.0 */ public function rm_cache_folder($type) { if (!$this->has_cache_folder($type)) { return; } $subsite_id = is_multisite() && !is_network_admin() ? get_current_blog_id() : ''; File::rrmdir(LITESPEED_STATIC_DIR . '/' . $type . '/' . $subsite_id); // Clear All summary data self::save_summary(false, false, true); if ($type == 'ccss' || $type == 'ucss') { Debug2::debug('[CSS] Cleared ' . $type . ' queue'); } elseif ($type == 'avatar') { Debug2::debug('[Avatar] Cleared ' . $type . ' queue'); } elseif ($type == 'css' || $type == 'js') { return; } else { Debug2::debug('[' . strtoupper($type) . '] Cleared ' . $type . ' queue'); } } /** * Build the static filepath * * @since 4.0 */ protected function _build_filepath_prefix($type) { $filepath_prefix = '/' . $type . '/'; if (is_multisite()) { $filepath_prefix .= get_current_blog_id() . '/'; } return $filepath_prefix; } /** * Load current queues from data file * * @since 4.1 * @since 4.3 Elevated to root.cls */ public function load_queue($type) { $filepath_prefix = $this->_build_filepath_prefix($type); $static_path = LITESPEED_STATIC_DIR . $filepath_prefix . '.litespeed_conf.dat'; $queue = array(); if (file_exists($static_path)) { $queue = \json_decode(file_get_contents($static_path), true) ?: array(); } return $queue; } /** * Save current queues to data file * * @since 4.1 * @since 4.3 Elevated to root.cls */ public function save_queue($type, $list) { $filepath_prefix = $this->_build_filepath_prefix($type); $static_path = LITESPEED_STATIC_DIR . $filepath_prefix . '.litespeed_conf.dat'; $data = \json_encode($list); File::save($static_path, $data, true); } /** * Clear all waiting queues * * @since 3.4 * @since 4.3 Elevated to root.cls */ public function clear_q($type, $silent = false) { $filepath_prefix = $this->_build_filepath_prefix($type); $static_path = LITESPEED_STATIC_DIR . $filepath_prefix . '.litespeed_conf.dat'; if (file_exists($static_path)) { $silent = false; unlink($static_path); } if (!$silent) { $msg = __('All QUIC.cloud service queues have been cleared.', 'litespeed-cache'); Admin_Display::success($msg); } } /** * Load an instance or create it if not existed * @since 4.0 */ public static function cls($cls = false, $unset = false, $data = false) { if (!$cls) { $cls = self::ori_cls(); } $cls = __NAMESPACE__ . '\\' . $cls; $cls_tag = strtolower($cls); if (!isset(self::$_instances[$cls_tag])) { if ($unset) { return; } self::$_instances[$cls_tag] = new $cls($data); } else { if ($unset) { unset(self::$_instances[$cls_tag]); return; } } return self::$_instances[$cls_tag]; } /** * Set one conf or confs */ public function set_conf($id, $val = null) { if (is_array($id)) { foreach ($id as $k => $v) { $this->set_conf($k, $v); } return; } self::$_options[$id] = $val; } /** * Set one primary conf or confs */ public function set_primary_conf($id, $val = null) { if (is_array($id)) { foreach ($id as $k => $v) { $this->set_primary_conf($k, $v); } return; } self::$_primary_options[$id] = $val; } /** * Set one network conf */ public function set_network_conf($id, $val = null) { if (is_array($id)) { foreach ($id as $k => $v) { $this->set_network_conf($k, $v); } return; } self::$_network_options[$id] = $val; } /** * Set one const conf */ public function set_const_conf($id, $val) { self::$_const_options[$id] = $val; } /** * Check if is overwritten by const * * @since 3.0 */ public function const_overwritten($id) { if (!isset(self::$_const_options[$id]) || self::$_const_options[$id] == self::$_options[$id]) { return null; } return self::$_const_options[$id]; } /** * Check if is overwritten by primary site * * @since 3.2.2 */ public function primary_overwritten($id) { if (!isset(self::$_primary_options[$id]) || self::$_primary_options[$id] == self::$_options[$id]) { return null; } // Network admin settings is impossible to be overwritten by primary if (is_network_admin()) { return null; } return self::$_primary_options[$id]; } /** * Get the list of configured options for the blog. * * @since 1.0 */ public function get_options($ori = false) { if (!$ori) { return array_merge(self::$_options, self::$_primary_options, self::$_network_options, self::$_const_options); } return self::$_options; } /** * If has a conf or not */ public function has_conf($id) { return array_key_exists($id, self::$_options); } /** * If has a primary conf or not */ public function has_primary_conf($id) { return array_key_exists($id, self::$_primary_options); } /** * If has a network conf or not */ public function has_network_conf($id) { return array_key_exists($id, self::$_network_options); } /** * Get conf */ public function conf($id, $ori = false) { if (isset(self::$_options[$id])) { if (!$ori) { $val = $this->const_overwritten($id); if ($val !== null) { defined('LSCWP_LOG') && Debug2::debug('[Conf] 🏛️ const option ' . $id . '=' . var_export($val, true)); return $val; } $val = $this->primary_overwritten($id); // Network Use primary site settings if ($val !== null) { return $val; } } // Network original value will be in _network_options if (!is_network_admin() || !$this->has_network_conf($id)) { return self::$_options[$id]; } } if ($this->has_network_conf($id)) { if (!$ori) { $val = $this->const_overwritten($id); if ($val !== null) { defined('LSCWP_LOG') && Debug2::debug('[Conf] 🏛️ const option ' . $id . '=' . var_export($val, true)); return $val; } } return $this->network_conf($id); } defined('LSCWP_LOG') && Debug2::debug('[Conf] Invalid option ID ' . $id); return null; } /** * Get primary conf */ public function primary_conf($id) { return self::$_primary_options[$id]; } /** * Get network conf */ public function network_conf($id) { if (!$this->has_network_conf($id)) { return null; } return self::$_network_options[$id]; } /** * Get called class short name */ public static function ori_cls() { $cls = new \ReflectionClass(get_called_class()); $shortname = $cls->getShortName(); $namespace = str_replace(__NAMESPACE__ . '\\', '', $cls->getNamespaceName() . '\\'); if ($namespace) { // the left namespace after dropped LiteSpeed $shortname = $namespace . $shortname; } return $shortname; } /** * Generate conf name for wp_options record * * @since 3.0 */ public static function name($id) { $name = strtolower(self::ori_cls()); if ($name == 'conf2') { // For a certain 3.7rc correction, can be dropped after v4 $name = 'conf'; } return 'litespeed.' . $name . '.' . $id; } /** * Dropin with prefix for WP's get_option * * @since 3.0 */ public static function get_option($id, $default_v = false) { $v = get_option(self::name($id), $default_v); // Maybe decode array if (is_array($default_v)) { $v = self::_maybe_decode($v); } return $v; } /** * Dropin with prefix for WP's get_site_option * * @since 3.0 */ public static function get_site_option($id, $default_v = false) { $v = get_site_option(self::name($id), $default_v); // Maybe decode array if (is_array($default_v)) { $v = self::_maybe_decode($v); } return $v; } /** * Dropin with prefix for WP's get_blog_option * * @since 3.0 */ public static function get_blog_option($blog_id, $id, $default_v = false) { $v = get_blog_option($blog_id, self::name($id), $default_v); // Maybe decode array if (is_array($default_v)) { $v = self::_maybe_decode($v); } return $v; } /** * Dropin with prefix for WP's add_option * * @since 3.0 */ public static function add_option($id, $v) { add_option(self::name($id), self::_maybe_encode($v)); } /** * Dropin with prefix for WP's add_site_option * * @since 3.0 */ public static function add_site_option($id, $v) { add_site_option(self::name($id), self::_maybe_encode($v)); } /** * Dropin with prefix for WP's update_option * * @since 3.0 */ public static function update_option($id, $v) { update_option(self::name($id), self::_maybe_encode($v)); } /** * Dropin with prefix for WP's update_site_option * * @since 3.0 */ public static function update_site_option($id, $v) { update_site_option(self::name($id), self::_maybe_encode($v)); } /** * Decode an array * * @since 4.0 */ private static function _maybe_decode($v) { if (!is_array($v)) { $v2 = \json_decode($v, true); if ($v2 !== null) { $v = $v2; } } return $v; } /** * Encode an array * * @since 4.0 */ private static function _maybe_encode($v) { if (is_array($v)) { $v = \json_encode($v) ?: $v; // Non utf-8 encoded value will get failed, then used ori value } return $v; } /** * Dropin with prefix for WP's delete_option * * @since 3.0 */ public static function delete_option($id) { delete_option(self::name($id)); } /** * Dropin with prefix for WP's delete_site_option * * @since 3.0 */ public static function delete_site_option($id) { delete_site_option(self::name($id)); } /** * Read summary * * @since 3.0 * @access public */ public static function get_summary($field = false) { $summary = self::get_option('_summary', array()); if (!is_array($summary)) { $summary = array(); } if (!$field) { return $summary; } if (array_key_exists($field, $summary)) { return $summary[$field]; } return null; } /** * Save summary * * @since 3.0 * @access public */ public static function save_summary($data = false, $reload = false, $overwrite = false) { if ($reload || empty(static::cls()->_summary)) { self::reload_summary(); } $existing_summary = static::cls()->_summary; if ($overwrite || !is_array($existing_summary)) { $existing_summary = array(); } $new_summary = array_merge($existing_summary, $data ?: array()); // self::debug2('Save after Reloaded summary', $new_summary); static::cls()->_summary = $new_summary; self::update_option('_summary', $new_summary); } /** * Reload summary * @since 5.0 */ public static function reload_summary() { static::cls()->_summary = self::get_summary(); // self::debug2( 'Reloaded summary', static::cls()->_summary ); } /** * Get the current instance object. To be inherited. * * @since 3.0 */ public static function get_instance() { return static::cls(); } } lang.cls.php 0000644 00000035617 15162226265 0007001 0 ustar 00 <?php /** * The language class. * * @since 3.0 * @package LiteSpeed_Cache * @subpackage LiteSpeed_Cache/inc * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Lang extends Base { /** * Get image status per status bit * * @since 3.0 */ public static function img_status($status = null) { $list = array( Img_Optm::STATUS_NEW => __('Images not requested', 'litespeed-cache'), Img_Optm::STATUS_RAW => __('Images ready to request', 'litespeed-cache'), Img_Optm::STATUS_REQUESTED => __('Images requested', 'litespeed-cache'), Img_Optm::STATUS_NOTIFIED => __('Images notified to pull', 'litespeed-cache'), Img_Optm::STATUS_PULLED => __('Images optimized and pulled', 'litespeed-cache'), ); if ($status !== null) { return !empty($list[$status]) ? $list[$status] : 'N/A'; } return $list; } /** * Try translating a string * * @since 4.7 */ public static function maybe_translate($raw_string) { $map = array( 'auto_alias_failed_cdn' => __('Unable to automatically add %1$s as a Domain Alias for main %2$s domain, due to potential CDN conflict.', 'litespeed-cache') . ' ' . Doc::learn_more('https://quic.cloud/docs/cdn/dns/how-to-setup-domain-alias/', false, false, false, true), 'auto_alias_failed_uid' => __('Unable to automatically add %1$s as a Domain Alias for main %2$s domain.', 'litespeed-cache') . ' ' . __('Alias is in use by another QUIC.cloud account.', 'litespeed-cache') . ' ' . Doc::learn_more('https://quic.cloud/docs/cdn/dns/how-to-setup-domain-alias/', false, false, false, true), ); // Maybe has placeholder if (strpos($raw_string, '::')) { $replacements = explode('::', $raw_string); if (empty($map[$replacements[0]])) { return $raw_string; } $tpl = $map[$replacements[0]]; unset($replacements[0]); return vsprintf($tpl, array_values($replacements)); } // Direct translation only if (empty($map[$raw_string])) { return $raw_string; } return $map[$raw_string]; } /** * Get the title of id * * @since 3.0 * @access public */ public static function title($id) { $_lang_list = array( self::O_SERVER_IP => __('Server IP', 'litespeed-cache'), self::O_GUEST_UAS => __('Guest Mode User Agents', 'litespeed-cache'), self::O_GUEST_IPS => __('Guest Mode IPs', 'litespeed-cache'), self::O_CACHE => __('Enable Cache', 'litespeed-cache'), self::O_CACHE_BROWSER => __('Browser Cache', 'litespeed-cache'), self::O_CACHE_TTL_PUB => __('Default Public Cache TTL', 'litespeed-cache'), self::O_CACHE_TTL_PRIV => __('Default Private Cache TTL', 'litespeed-cache'), self::O_CACHE_TTL_FRONTPAGE => __('Default Front Page TTL', 'litespeed-cache'), self::O_CACHE_TTL_FEED => __('Default Feed TTL', 'litespeed-cache'), self::O_CACHE_TTL_REST => __('Default REST TTL', 'litespeed-cache'), self::O_CACHE_TTL_STATUS => __('Default HTTP Status Code Page TTL', 'litespeed-cache'), self::O_CACHE_TTL_BROWSER => __('Browser Cache TTL', 'litespeed-cache'), self::O_CACHE_AJAX_TTL => __('AJAX Cache TTL', 'litespeed-cache'), self::O_AUTO_UPGRADE => __('Automatically Upgrade', 'litespeed-cache'), self::O_GUEST => __('Guest Mode', 'litespeed-cache'), self::O_GUEST_OPTM => __('Guest Optimization', 'litespeed-cache'), self::O_NEWS => __('Notifications', 'litespeed-cache'), self::O_CACHE_PRIV => __('Cache Logged-in Users', 'litespeed-cache'), self::O_CACHE_COMMENTER => __('Cache Commenters', 'litespeed-cache'), self::O_CACHE_REST => __('Cache REST API', 'litespeed-cache'), self::O_CACHE_PAGE_LOGIN => __('Cache Login Page', 'litespeed-cache'), self::O_CACHE_RES => __('Cache PHP Resources', 'litespeed-cache'), self::O_CACHE_MOBILE => __('Cache Mobile', 'litespeed-cache'), self::O_CACHE_MOBILE_RULES => __('List of Mobile User Agents', 'litespeed-cache'), self::O_CACHE_PRIV_URI => __('Private Cached URIs', 'litespeed-cache'), self::O_CACHE_DROP_QS => __('Drop Query String', 'litespeed-cache'), self::O_OBJECT => __('Object Cache', 'litespeed-cache'), self::O_OBJECT_KIND => __('Method', 'litespeed-cache'), self::O_OBJECT_HOST => __('Host', 'litespeed-cache'), self::O_OBJECT_PORT => __('Port', 'litespeed-cache'), self::O_OBJECT_LIFE => __('Default Object Lifetime', 'litespeed-cache'), self::O_OBJECT_USER => __('Username', 'litespeed-cache'), self::O_OBJECT_PSWD => __('Password', 'litespeed-cache'), self::O_OBJECT_DB_ID => __('Redis Database ID', 'litespeed-cache'), self::O_OBJECT_GLOBAL_GROUPS => __('Global Groups', 'litespeed-cache'), self::O_OBJECT_NON_PERSISTENT_GROUPS => __('Do Not Cache Groups', 'litespeed-cache'), self::O_OBJECT_PERSISTENT => __('Persistent Connection', 'litespeed-cache'), self::O_OBJECT_ADMIN => __('Cache WP-Admin', 'litespeed-cache'), self::O_OBJECT_TRANSIENTS => __('Store Transients', 'litespeed-cache'), self::O_PURGE_ON_UPGRADE => __('Purge All On Upgrade', 'litespeed-cache'), self::O_PURGE_STALE => __('Serve Stale', 'litespeed-cache'), self::O_PURGE_TIMED_URLS => __('Scheduled Purge URLs', 'litespeed-cache'), self::O_PURGE_TIMED_URLS_TIME => __('Scheduled Purge Time', 'litespeed-cache'), self::O_CACHE_FORCE_URI => __('Force Cache URIs', 'litespeed-cache'), self::O_CACHE_FORCE_PUB_URI => __('Force Public Cache URIs', 'litespeed-cache'), self::O_CACHE_EXC => __('Do Not Cache URIs', 'litespeed-cache'), self::O_CACHE_EXC_QS => __('Do Not Cache Query Strings', 'litespeed-cache'), self::O_CACHE_EXC_CAT => __('Do Not Cache Categories', 'litespeed-cache'), self::O_CACHE_EXC_TAG => __('Do Not Cache Tags', 'litespeed-cache'), self::O_CACHE_EXC_ROLES => __('Do Not Cache Roles', 'litespeed-cache'), self::O_OPTM_CSS_MIN => __('CSS Minify', 'litespeed-cache'), self::O_OPTM_CSS_COMB => __('CSS Combine', 'litespeed-cache'), self::O_OPTM_CSS_COMB_EXT_INL => __('CSS Combine External and Inline', 'litespeed-cache'), self::O_OPTM_UCSS => __('Generate UCSS', 'litespeed-cache'), self::O_OPTM_UCSS_INLINE => __('UCSS Inline', 'litespeed-cache'), self::O_OPTM_UCSS_SELECTOR_WHITELIST => __('UCSS Selector Allowlist', 'litespeed-cache'), self::O_OPTM_UCSS_FILE_EXC_INLINE => __('UCSS File Excludes and Inline', 'litespeed-cache'), self::O_OPTM_UCSS_EXC => __('UCSS URI Excludes', 'litespeed-cache'), self::O_OPTM_JS_MIN => __('JS Minify', 'litespeed-cache'), self::O_OPTM_JS_COMB => __('JS Combine', 'litespeed-cache'), self::O_OPTM_JS_COMB_EXT_INL => __('JS Combine External and Inline', 'litespeed-cache'), self::O_OPTM_HTML_MIN => __('HTML Minify', 'litespeed-cache'), self::O_OPTM_HTML_LAZY => __('HTML Lazy Load Selectors', 'litespeed-cache'), self::O_OPTM_HTML_SKIP_COMMENTS => __('HTML Keep Comments', 'litespeed-cache'), self::O_OPTM_CSS_ASYNC => __('Load CSS Asynchronously', 'litespeed-cache'), self::O_OPTM_CCSS_PER_URL => __('CCSS Per URL', 'litespeed-cache'), self::O_OPTM_CSS_ASYNC_INLINE => __('Inline CSS Async Lib', 'litespeed-cache'), self::O_OPTM_CSS_FONT_DISPLAY => __('Font Display Optimization', 'litespeed-cache'), self::O_OPTM_JS_DEFER => __('Load JS Deferred', 'litespeed-cache'), self::O_OPTM_LOCALIZE => __('Localize Resources', 'litespeed-cache'), self::O_OPTM_LOCALIZE_DOMAINS => __('Localization Files', 'litespeed-cache'), self::O_OPTM_DNS_PREFETCH => __('DNS Prefetch', 'litespeed-cache'), self::O_OPTM_DNS_PREFETCH_CTRL => __('DNS Prefetch Control', 'litespeed-cache'), self::O_OPTM_DNS_PRECONNECT => __('DNS Preconnect', 'litespeed-cache'), self::O_OPTM_CSS_EXC => __('CSS Excludes', 'litespeed-cache'), self::O_OPTM_JS_DELAY_INC => __('JS Delayed Includes', 'litespeed-cache'), self::O_OPTM_JS_EXC => __('JS Excludes', 'litespeed-cache'), self::O_OPTM_QS_RM => __('Remove Query Strings', 'litespeed-cache'), self::O_OPTM_GGFONTS_ASYNC => __('Load Google Fonts Asynchronously', 'litespeed-cache'), self::O_OPTM_GGFONTS_RM => __('Remove Google Fonts', 'litespeed-cache'), self::O_OPTM_CCSS_CON => __('Critical CSS Rules', 'litespeed-cache'), self::O_OPTM_CCSS_SEP_POSTTYPE => __('Separate CCSS Cache Post Types', 'litespeed-cache'), self::O_OPTM_CCSS_SEP_URI => __('Separate CCSS Cache URIs', 'litespeed-cache'), self::O_OPTM_CCSS_SELECTOR_WHITELIST => __('CCSS Selector Allowlist', 'litespeed-cache'), self::O_OPTM_JS_DEFER_EXC => __('JS Deferred / Delayed Excludes', 'litespeed-cache'), self::O_OPTM_GM_JS_EXC => __('Guest Mode JS Excludes', 'litespeed-cache'), self::O_OPTM_EMOJI_RM => __('Remove WordPress Emoji', 'litespeed-cache'), self::O_OPTM_NOSCRIPT_RM => __('Remove Noscript Tags', 'litespeed-cache'), self::O_OPTM_EXC => __('URI Excludes', 'litespeed-cache'), self::O_OPTM_GUEST_ONLY => __('Optimize for Guests Only', 'litespeed-cache'), self::O_OPTM_EXC_ROLES => __('Role Excludes', 'litespeed-cache'), self::O_DISCUSS_AVATAR_CACHE => __('Gravatar Cache', 'litespeed-cache'), self::O_DISCUSS_AVATAR_CRON => __('Gravatar Cache Cron', 'litespeed-cache'), self::O_DISCUSS_AVATAR_CACHE_TTL => __('Gravatar Cache TTL', 'litespeed-cache'), self::O_MEDIA_LAZY => __('Lazy Load Images', 'litespeed-cache'), self::O_MEDIA_LAZY_EXC => __('Lazy Load Image Excludes', 'litespeed-cache'), self::O_MEDIA_LAZY_CLS_EXC => __('Lazy Load Image Class Name Excludes', 'litespeed-cache'), self::O_MEDIA_LAZY_PARENT_CLS_EXC => __('Lazy Load Image Parent Class Name Excludes', 'litespeed-cache'), self::O_MEDIA_IFRAME_LAZY_CLS_EXC => __('Lazy Load Iframe Class Name Excludes', 'litespeed-cache'), self::O_MEDIA_IFRAME_LAZY_PARENT_CLS_EXC => __('Lazy Load Iframe Parent Class Name Excludes', 'litespeed-cache'), self::O_MEDIA_LAZY_URI_EXC => __('Lazy Load URI Excludes', 'litespeed-cache'), self::O_MEDIA_LQIP_EXC => __('LQIP Excludes', 'litespeed-cache'), self::O_MEDIA_LAZY_PLACEHOLDER => __('Basic Image Placeholder', 'litespeed-cache'), self::O_MEDIA_PLACEHOLDER_RESP => __('Responsive Placeholder', 'litespeed-cache'), self::O_MEDIA_PLACEHOLDER_RESP_COLOR => __('Responsive Placeholder Color', 'litespeed-cache'), self::O_MEDIA_PLACEHOLDER_RESP_SVG => __('Responsive Placeholder SVG', 'litespeed-cache'), self::O_MEDIA_LQIP => __('LQIP Cloud Generator', 'litespeed-cache'), self::O_MEDIA_LQIP_QUAL => __('LQIP Quality', 'litespeed-cache'), self::O_MEDIA_LQIP_MIN_W => __('LQIP Minimum Dimensions', 'litespeed-cache'), // self::O_MEDIA_LQIP_MIN_H => __( 'LQIP Minimum Height', 'litespeed-cache' ), self::O_MEDIA_PLACEHOLDER_RESP_ASYNC => __('Generate LQIP In Background', 'litespeed-cache'), self::O_MEDIA_IFRAME_LAZY => __('Lazy Load Iframes', 'litespeed-cache'), self::O_MEDIA_ADD_MISSING_SIZES => __('Add Missing Sizes', 'litespeed-cache'), self::O_MEDIA_VPI => __('Viewport Images', 'litespeed-cache'), self::O_MEDIA_VPI_CRON => __('Viewport Images Cron', 'litespeed-cache'), self::O_IMG_OPTM_AUTO => __('Auto Request Cron', 'litespeed-cache'), self::O_IMG_OPTM_ORI => __('Optimize Original Images', 'litespeed-cache'), self::O_IMG_OPTM_RM_BKUP => __('Remove Original Backups', 'litespeed-cache'), self::O_IMG_OPTM_WEBP => __('Next-Gen Image Format', 'litespeed-cache'), self::O_IMG_OPTM_LOSSLESS => __('Optimize Losslessly', 'litespeed-cache'), self::O_IMG_OPTM_EXIF => __('Preserve EXIF/XMP data', 'litespeed-cache'), self::O_IMG_OPTM_WEBP_ATTR => __('WebP/AVIF Attribute To Replace', 'litespeed-cache'), self::O_IMG_OPTM_WEBP_REPLACE_SRCSET => __('WebP/AVIF For Extra srcset', 'litespeed-cache'), self::O_IMG_OPTM_JPG_QUALITY => __('WordPress Image Quality Control', 'litespeed-cache'), self::O_ESI => __('Enable ESI', 'litespeed-cache'), self::O_ESI_CACHE_ADMBAR => __('Cache Admin Bar', 'litespeed-cache'), self::O_ESI_CACHE_COMMFORM => __('Cache Comment Form', 'litespeed-cache'), self::O_ESI_NONCE => __('ESI Nonces', 'litespeed-cache'), self::O_CACHE_VARY_GROUP => __('Vary Group', 'litespeed-cache'), self::O_PURGE_HOOK_ALL => __('Purge All Hooks', 'litespeed-cache'), self::O_UTIL_NO_HTTPS_VARY => __('Improve HTTP/HTTPS Compatibility', 'litespeed-cache'), self::O_UTIL_INSTANT_CLICK => __('Instant Click', 'litespeed-cache'), self::O_CACHE_EXC_COOKIES => __('Do Not Cache Cookies', 'litespeed-cache'), self::O_CACHE_EXC_USERAGENTS => __('Do Not Cache User Agents', 'litespeed-cache'), self::O_CACHE_LOGIN_COOKIE => __('Login Cookie', 'litespeed-cache'), self::O_CACHE_VARY_COOKIES => __('Vary Cookies', 'litespeed-cache'), self::O_MISC_HEARTBEAT_FRONT => __('Frontend Heartbeat Control', 'litespeed-cache'), self::O_MISC_HEARTBEAT_FRONT_TTL => __('Frontend Heartbeat TTL', 'litespeed-cache'), self::O_MISC_HEARTBEAT_BACK => __('Backend Heartbeat Control', 'litespeed-cache'), self::O_MISC_HEARTBEAT_BACK_TTL => __('Backend Heartbeat TTL', 'litespeed-cache'), self::O_MISC_HEARTBEAT_EDITOR => __('Editor Heartbeat', 'litespeed-cache'), self::O_MISC_HEARTBEAT_EDITOR_TTL => __('Editor Heartbeat TTL', 'litespeed-cache'), self::O_CDN => __('Use CDN Mapping', 'litespeed-cache'), self::CDN_MAPPING_URL => __('CDN URL', 'litespeed-cache'), self::CDN_MAPPING_INC_IMG => __('Include Images', 'litespeed-cache'), self::CDN_MAPPING_INC_CSS => __('Include CSS', 'litespeed-cache'), self::CDN_MAPPING_INC_JS => __('Include JS', 'litespeed-cache'), self::CDN_MAPPING_FILETYPE => __('Include File Types', 'litespeed-cache'), self::O_CDN_ATTR => __('HTML Attribute To Replace', 'litespeed-cache'), self::O_CDN_ORI => __('Original URLs', 'litespeed-cache'), self::O_CDN_ORI_DIR => __('Included Directories', 'litespeed-cache'), self::O_CDN_EXC => __('Exclude Path', 'litespeed-cache'), self::O_CDN_CLOUDFLARE => __('Cloudflare API', 'litespeed-cache'), self::O_CRAWLER => __('Crawler', 'litespeed-cache'), self::O_CRAWLER_CRAWL_INTERVAL => __('Crawl Interval', 'litespeed-cache'), self::O_CRAWLER_LOAD_LIMIT => __('Server Load Limit', 'litespeed-cache'), self::O_CRAWLER_ROLES => __('Role Simulation', 'litespeed-cache'), self::O_CRAWLER_COOKIES => __('Cookie Simulation', 'litespeed-cache'), self::O_CRAWLER_SITEMAP => __('Custom Sitemap', 'litespeed-cache'), self::O_DEBUG_DISABLE_ALL => __('Disable All Features', 'litespeed-cache'), self::O_DEBUG => __('Debug Log', 'litespeed-cache'), self::O_DEBUG_IPS => __('Admin IPs', 'litespeed-cache'), self::O_DEBUG_LEVEL => __('Debug Level', 'litespeed-cache'), self::O_DEBUG_FILESIZE => __('Log File Size Limit', 'litespeed-cache'), self::O_DEBUG_COLLAPSE_QS => __('Collapse Query Strings', 'litespeed-cache'), self::O_DEBUG_INC => __('Debug URI Includes', 'litespeed-cache'), self::O_DEBUG_EXC => __('Debug URI Excludes', 'litespeed-cache'), self::O_DEBUG_EXC_STRINGS => __('Debug String Excludes', 'litespeed-cache'), self::O_DB_OPTM_REVISIONS_MAX => __('Revisions Max Number', 'litespeed-cache'), self::O_DB_OPTM_REVISIONS_AGE => __('Revisions Max Age', 'litespeed-cache'), ); if (array_key_exists($id, $_lang_list)) { return $_lang_list[$id]; } return 'N/A'; } } purge.cls.php 0000644 00000074764 15162226266 0007211 0 ustar 00 <?php /** * The plugin purge class for X-LiteSpeed-Purge * * @since 1.1.3 * @since 2.2 Refactored. Changed access from public to private for most func and class variables. */ namespace LiteSpeed; defined('WPINC') || exit(); class Purge extends Base { const LOG_TAG = '🧹'; protected $_pub_purge = array(); protected $_pub_purge2 = array(); protected $_priv_purge = array(); protected $_purge_single = false; const X_HEADER = 'X-LiteSpeed-Purge'; const X_HEADER2 = 'X-LiteSpeed-Purge2'; const DB_QUEUE = 'queue'; const DB_QUEUE2 = 'queue2'; const TYPE_PURGE_ALL = 'purge_all'; const TYPE_PURGE_ALL_LSCACHE = 'purge_all_lscache'; const TYPE_PURGE_ALL_CSSJS = 'purge_all_cssjs'; const TYPE_PURGE_ALL_LOCALRES = 'purge_all_localres'; const TYPE_PURGE_ALL_CCSS = 'purge_all_ccss'; const TYPE_PURGE_ALL_UCSS = 'purge_all_ucss'; const TYPE_PURGE_ALL_LQIP = 'purge_all_lqip'; const TYPE_PURGE_ALL_AVATAR = 'purge_all_avatar'; const TYPE_PURGE_ALL_OBJECT = 'purge_all_object'; const TYPE_PURGE_ALL_OPCACHE = 'purge_all_opcache'; const TYPE_PURGE_FRONT = 'purge_front'; const TYPE_PURGE_UCSS = 'purge_ucss'; const TYPE_PURGE_FRONTPAGE = 'purge_frontpage'; const TYPE_PURGE_PAGES = 'purge_pages'; const TYPE_PURGE_ERROR = 'purge_error'; /** * Init hooks * * @since 3.0 */ public function init() { // Register purge actions. // Most used values: edit_post, save_post, delete_post, wp_trash_post, clean_post_cache, wp_update_comment_count $purge_post_events = apply_filters('litespeed_purge_post_events', array( 'delete_post', 'wp_trash_post', // 'clean_post_cache', // This will disable wc's not purge product when stock status not change setting 'wp_update_comment_count', // TODO: check if needed for non ESI )); foreach ($purge_post_events as $event) { // this will purge all related tags add_action($event, array($this, 'purge_post')); } // Purge post only when status is/was publish add_action('transition_post_status', array($this, 'purge_publish'), 10, 3); add_action('wp_update_comment_count', array($this, 'purge_feeds')); if ($this->conf(self::O_OPTM_UCSS)) { add_action('edit_post', __NAMESPACE__ . '\Purge::purge_ucss'); } } /** * Only purge publish related status post * * @since 3.0 * @access public */ public function purge_publish($new_status, $old_status, $post) { if ($new_status != 'publish' && $old_status != 'publish') { return; } $this->purge_post($post->ID); } /** * Handle all request actions from main cls * * @since 1.8 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_PURGE_ALL: $this->_purge_all(); break; case self::TYPE_PURGE_ALL_LSCACHE: $this->_purge_all_lscache(); break; case self::TYPE_PURGE_ALL_CSSJS: $this->_purge_all_cssjs(); break; case self::TYPE_PURGE_ALL_LOCALRES: $this->_purge_all_localres(); break; case self::TYPE_PURGE_ALL_CCSS: $this->_purge_all_ccss(); break; case self::TYPE_PURGE_ALL_UCSS: $this->_purge_all_ucss(); break; case self::TYPE_PURGE_ALL_LQIP: $this->_purge_all_lqip(); break; case self::TYPE_PURGE_ALL_AVATAR: $this->_purge_all_avatar(); break; case self::TYPE_PURGE_ALL_OBJECT: $this->_purge_all_object(); break; case self::TYPE_PURGE_ALL_OPCACHE: $this->purge_all_opcache(); break; case self::TYPE_PURGE_FRONT: $this->_purge_front(); break; case self::TYPE_PURGE_UCSS: $this->_purge_ucss(); break; case self::TYPE_PURGE_FRONTPAGE: $this->_purge_frontpage(); break; case self::TYPE_PURGE_PAGES: $this->_purge_pages(); break; case strpos($type, self::TYPE_PURGE_ERROR) === 0: $this->_purge_error(substr($type, strlen(self::TYPE_PURGE_ERROR))); break; default: break; } Admin::redirect(); } /** * Shortcut to purge all lscache * * @since 1.0.0 * @access public */ public static function purge_all($reason = false) { self::cls()->_purge_all($reason); } /** * Purge all caches (lscache/op/oc) * * @since 2.2 * @access private */ private function _purge_all($reason = false) { // if ( defined( 'LITESPEED_CLI' ) ) { // // Can't send, already has output, need to save and wait for next run // self::update_option( self::DB_QUEUE, $curr_built ); // self::debug( 'CLI request, queue stored: ' . $curr_built ); // } // else { $this->_purge_all_lscache(true); $this->_purge_all_cssjs(true); $this->_purge_all_localres(true); // $this->_purge_all_ccss( true ); // $this->_purge_all_lqip( true ); $this->_purge_all_object(true); $this->purge_all_opcache(true); // } if (!is_string($reason)) { $reason = false; } if ($reason) { $reason = "( $reason )"; } self::debug('Purge all ' . $reason, 3); $msg = __('Purged all caches successfully.', 'litespeed-cache'); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); do_action('litespeed_purged_all'); } /** * Alerts LiteSpeed Web Server to purge all pages. * * For multisite installs, if this is called by a site admin (not network admin), * it will only purge all posts associated with that site. * * @since 2.2 * @access public */ private function _purge_all_lscache($silence = false) { $this->_add('*'); // Action to run after server was notified to delete LSCache entries. do_action('litespeed_purged_all_lscache'); if (!$silence) { $msg = __('Notified LiteSpeed Web Server to purge all LSCache entries.', 'litespeed-cache'); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); } } /** * Delete all critical css * * @since 2.3 * @access private */ private function _purge_all_ccss($silence = false) { do_action('litespeed_purged_all_ccss'); $this->cls('CSS')->rm_cache_folder('ccss'); $this->cls('Data')->url_file_clean('ccss'); if (!$silence) { $msg = __('Cleaned all Critical CSS files.', 'litespeed-cache'); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); } } /** * Delete all unique css * * @since 2.3 * @access private */ private function _purge_all_ucss($silence = false) { do_action('litespeed_purged_all_ucss'); $this->cls('CSS')->rm_cache_folder('ucss'); $this->cls('Data')->url_file_clean('ucss'); if (!$silence) { $msg = __('Cleaned all Unique CSS files.', 'litespeed-cache'); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); } } /** * Purge one UCSS by URL * * @since 4.5 * @access public */ public static function purge_ucss($post_id_or_url) { self::debug('Purge a single UCSS: ' . $post_id_or_url); // If is post_id, generate URL if (!preg_match('/\D/', $post_id_or_url)) { $post_id_or_url = get_permalink($post_id_or_url); } $post_id_or_url = untrailingslashit($post_id_or_url); $existing_url_files = Data::cls()->mark_as_expired($post_id_or_url, true); if ($existing_url_files) { // Add to UCSS Q self::cls('UCSS')->add_to_q($existing_url_files); } } /** * Delete all LQIP images * * @since 3.0 * @access private */ private function _purge_all_lqip($silence = false) { do_action('litespeed_purged_all_lqip'); $this->cls('Placeholder')->rm_cache_folder('lqip'); if (!$silence) { $msg = __('Cleaned all LQIP files.', 'litespeed-cache'); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); } } /** * Delete all avatar images * * @since 3.0 * @access private */ private function _purge_all_avatar($silence = false) { do_action('litespeed_purged_all_avatar'); $this->cls('Avatar')->rm_cache_folder('avatar'); if (!$silence) { $msg = __('Cleaned all Gravatar files.', 'litespeed-cache'); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); } } /** * Delete all localized JS * * @since 3.3 * @access private */ private function _purge_all_localres($silence = false) { do_action('litespeed_purged_all_localres'); $this->_add(Tag::TYPE_LOCALRES); if (!$silence) { $msg = __('Cleaned all localized resource entries.', 'litespeed-cache'); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); } } /** * Alerts LiteSpeed Web Server to purge pages. * * @since 1.2.2 * @access private */ private function _purge_all_cssjs($silence = false) { if (defined('DOING_CRON') || defined('LITESPEED_DID_send_headers')) { self::debug('❌ Bypassed cssjs delete as header sent (lscache purge after this point will fail) or doing cron'); return; } $this->_purge_all_lscache($silence); // Purge CSSJS must purge lscache too to avoid 404 do_action('litespeed_purged_all_cssjs'); Optimize::update_option(Optimize::ITEM_TIMESTAMP_PURGE_CSS, time()); $this->_add(Tag::TYPE_MIN); $this->cls('CSS')->rm_cache_folder('css'); $this->cls('CSS')->rm_cache_folder('js'); $this->cls('Data')->url_file_clean('css'); $this->cls('Data')->url_file_clean('js'); // Clear UCSS queue as it used combined CSS to generate $this->clear_q('ucss', true); if (!$silence) { $msg = __('Notified LiteSpeed Web Server to purge CSS/JS entries.', 'litespeed-cache'); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); } } /** * Purge opcode cache * * @since 1.8.2 * @access public */ public function purge_all_opcache($silence = false) { if (!Router::opcache_enabled()) { self::debug('Failed to reset opcode cache due to opcache not enabled'); if (!$silence) { $msg = __('Opcode cache is not enabled.', 'litespeed-cache'); Admin_Display::error($msg); } return false; } // Action to run after opcache purge. do_action('litespeed_purged_all_opcache'); // Purge opcode cache opcache_reset(); self::debug('Reset opcode cache'); if (!$silence) { $msg = __('Reset the entire opcode cache successfully.', 'litespeed-cache'); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); } return true; } /** * Purge object cache * * @since 3.4 * @access public */ public static function purge_all_object($silence = true) { self::cls()->_purge_all_object($silence); } /** * Purge object cache * * @since 1.8 * @access private */ private function _purge_all_object($silence = false) { if (!defined('LSCWP_OBJECT_CACHE')) { self::debug('Failed to flush object cache due to object cache not enabled'); if (!$silence) { $msg = __('Object cache is not enabled.', 'litespeed-cache'); Admin_Display::error($msg); } return false; } do_action('litespeed_purged_all_object'); $this->cls('Object_Cache')->flush(); self::debug('Flushed object cache'); if (!$silence) { $msg = __('Purge all object caches successfully.', 'litespeed-cache'); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); } return true; } /** * Adds new public purge tags to the array of purge tags for the request. * * @since 1.1.3 * @access public * @param mixed $tags Tags to add to the list. */ public static function add($tags, $purge2 = false) { self::cls()->_add($tags, $purge2); } /** * Add tags to purge * * @since 2.2 * @access private */ private function _add($tags, $purge2 = false) { if (!is_array($tags)) { $tags = array($tags); } $tags = $this->_prepend_bid($tags); if (!array_diff($tags, $purge2 ? $this->_pub_purge2 : $this->_pub_purge)) { return; } if ($purge2) { $this->_pub_purge2 = array_merge($this->_pub_purge2, $tags); $this->_pub_purge2 = array_unique($this->_pub_purge2); } else { $this->_pub_purge = array_merge($this->_pub_purge, $tags); $this->_pub_purge = array_unique($this->_pub_purge); } self::debug('added ' . implode(',', $tags) . ($purge2 ? ' [Purge2]' : ''), 8); // Send purge header immediately $curr_built = $this->_build($purge2); if (defined('LITESPEED_CLI')) { // Can't send, already has output, need to save and wait for next run self::update_option($purge2 ? self::DB_QUEUE2 : self::DB_QUEUE, $curr_built); self::debug('CLI request, queue stored: ' . $curr_built); } else { @header($curr_built); if (defined('DOING_CRON') || defined('LITESPEED_DID_send_headers') || apply_filters('litespeed_delay_purge', false)) { self::update_option($purge2 ? self::DB_QUEUE2 : self::DB_QUEUE, $curr_built); self::debug('Output existed, queue stored: ' . $curr_built); } self::debug($curr_built); } } /** * Adds new private purge tags to the array of purge tags for the request. * * @since 1.1.3 * @access public * @param mixed $tags Tags to add to the list. */ public static function add_private($tags) { self::cls()->_add_private($tags); } /** * Add private ESI tag to purge list * * @since 3.0 * @access public */ public static function add_private_esi($tag) { self::add_private(Tag::TYPE_ESI . $tag); } /** * Add private all tag to purge list * * @since 3.0 * @access public */ public static function add_private_all() { self::add_private('*'); } /** * Add tags to private purge * * @since 2.2 * @access private */ private function _add_private($tags) { if (!is_array($tags)) { $tags = array($tags); } $tags = $this->_prepend_bid($tags); if (!array_diff($tags, $this->_priv_purge)) { return; } self::debug('added [private] ' . implode(',', $tags), 3); $this->_priv_purge = array_merge($this->_priv_purge, $tags); $this->_priv_purge = array_unique($this->_priv_purge); // Send purge header immediately @header($this->_build()); } /** * Incorporate blog_id into purge tags for multisite * * @since 4.0 * @access private * @param mixed $tags Tags to add to the list. */ private function _prepend_bid($tags) { if (in_array('*', $tags)) { return array('*'); } $curr_bid = is_multisite() ? get_current_blog_id() : ''; foreach ($tags as $k => $v) { $tags[$k] = $curr_bid . '_' . $v; } return $tags; } /** * Activate `purge related tags` for Admin QS. * * @since 1.1.3 * @access public * @deprecated @7.0 Drop @v7.5 */ public static function set_purge_related() { } /** * Activate `purge single url tag` for Admin QS. * * @since 1.1.3 * @access public */ public static function set_purge_single() { self::cls()->_purge_single = true; do_action('litespeed_purged_single'); } /** * Purge frontend url * * @since 1.3 * @since 2.2 Renamed from `frontend_purge`; Access changed from public * @access private */ private function _purge_front() { if (empty($_SERVER['HTTP_REFERER'])) { exit('no referer'); } $this->purge_url($_SERVER['HTTP_REFERER']); do_action('litespeed_purged_front', $_SERVER['HTTP_REFERER']); wp_redirect($_SERVER['HTTP_REFERER']); exit(); } /** * Purge single UCSS * @since 4.7 */ private function _purge_ucss() { if (empty($_SERVER['HTTP_REFERER'])) { exit('no referer'); } $url_tag = empty($_GET['url_tag']) ? $_SERVER['HTTP_REFERER'] : $_GET['url_tag']; self::debug('Purge ucss [url_tag] ' . $url_tag); do_action('litespeed_purge_ucss', $url_tag); $this->purge_url($_SERVER['HTTP_REFERER']); wp_redirect($_SERVER['HTTP_REFERER']); exit(); } /** * Alerts LiteSpeed Web Server to purge the front page. * * @since 1.0.3 * @since 2.2 Access changed from public to private, renamed from `_purge_front` * @access private */ private function _purge_frontpage() { $this->_add(Tag::TYPE_FRONTPAGE); if (LITESPEED_SERVER_TYPE !== 'LITESPEED_SERVER_OLS') { $this->_add_private(Tag::TYPE_FRONTPAGE); } $msg = __('Notified LiteSpeed Web Server to purge the front page.', 'litespeed-cache'); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); do_action('litespeed_purged_frontpage'); } /** * Alerts LiteSpeed Web Server to purge pages. * * @since 1.0.15 * @access private */ private function _purge_pages() { $this->_add(Tag::TYPE_PAGES); $msg = __('Notified LiteSpeed Web Server to purge all pages.', 'litespeed-cache'); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); do_action('litespeed_purged_pages'); } /** * Alerts LiteSpeed Web Server to purge error pages. * * @since 1.0.14 * @access private */ private function _purge_error($type = false) { $this->_add(Tag::TYPE_HTTP); if (!$type || !in_array($type, array('403', '404', '500'))) { return; } $this->_add(Tag::TYPE_HTTP . $type); $msg = __('Notified LiteSpeed Web Server to purge error pages.', 'litespeed-cache'); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); } /** * Callback to add purge tags if admin selects to purge selected category pages. * * @since 1.0.7 * @access public */ public function purge_cat($value) { $val = trim($value); if (empty($val)) { return; } if (preg_match('/^[a-zA-Z0-9-]+$/', $val) == 0) { self::debug("$val cat invalid"); return; } $cat = get_category_by_slug($val); if ($cat == false) { self::debug("$val cat not existed/published"); return; } self::add(Tag::TYPE_ARCHIVE_TERM . $cat->term_id); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success(sprintf(__('Purge category %s', 'litespeed-cache'), $val)); // Action to run after category purge. do_action('litespeed_purged_cat', $value); } /** * Callback to add purge tags if admin selects to purge selected tag pages. * * @since 1.0.7 * @access public */ public function purge_tag($val) { $val = trim($val); if (empty($val)) { return; } if (preg_match('/^[a-zA-Z0-9-]+$/', $val) == 0) { self::debug("$val tag invalid"); return; } $term = get_term_by('slug', $val, 'post_tag'); if ($term == 0) { self::debug("$val tag not exist"); return; } self::add(Tag::TYPE_ARCHIVE_TERM . $term->term_id); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success(sprintf(__('Purge tag %s', 'litespeed-cache'), $val)); // Action to run after tag purge. do_action('litespeed_purged_tag', $val); } /** * Callback to add purge tags if admin selects to purge selected urls. * * @since 1.0.7 * @access public */ public function purge_url($url, $purge2 = false, $quite = false) { $val = trim($url); if (empty($val)) { return; } if (strpos($val, '<') !== false) { self::debug("$val url contains <"); return; } $val = Utility::make_relative($val); $hash = Tag::get_uri_tag($val); if ($hash === false) { self::debug("$val url invalid"); return; } self::add($hash, $purge2); !$quite && !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success(sprintf(__('Purge url %s', 'litespeed-cache'), $val)); // Action to run after url purge. do_action('litespeed_purged_link', $url); } /** * Purge a list of pages when selected by admin. This method will look at the post arguments to determine how and what to purge. * * @since 1.0.7 * @access public */ public function purge_list() { if (!isset($_REQUEST[Admin_Display::PURGEBYOPT_SELECT]) || !isset($_REQUEST[Admin_Display::PURGEBYOPT_LIST])) { return; } $sel = $_REQUEST[Admin_Display::PURGEBYOPT_SELECT]; $list_buf = $_REQUEST[Admin_Display::PURGEBYOPT_LIST]; if (empty($list_buf)) { return; } $list_buf = str_replace(',', "\n", $list_buf); // for cli $list = explode("\n", $list_buf); switch ($sel) { case Admin_Display::PURGEBY_CAT: $cb = 'purge_cat'; break; case Admin_Display::PURGEBY_PID: $cb = 'purge_post'; break; case Admin_Display::PURGEBY_TAG: $cb = 'purge_tag'; break; case Admin_Display::PURGEBY_URL: $cb = 'purge_url'; break; default: return; } array_map(array($this, $cb), $list); // for redirection $_GET[Admin_Display::PURGEBYOPT_SELECT] = $sel; } /** * Purge ESI * * @since 3.0 * @access public */ public static function purge_esi($tag) { self::add(Tag::TYPE_ESI . $tag); do_action('litespeed_purged_esi', $tag); } /** * Purge a certain post type * * @since 3.0 * @access public */ public static function purge_posttype($post_type) { self::add(Tag::TYPE_ARCHIVE_POSTTYPE . $post_type); self::add($post_type); do_action('litespeed_purged_posttype', $post_type); } /** * Purge all related tags to a post. * * @since 1.0.0 * @access public */ public function purge_post($pid) { $pid = intval($pid); // ignore the status we don't care if (!$pid || !in_array(get_post_status($pid), array('publish', 'trash', 'private', 'draft'))) { return; } $purge_tags = $this->_get_purge_tags_by_post($pid); if (!$purge_tags) { return; } self::add($purge_tags); if ($this->conf(self::O_CACHE_REST)) { self::add(Tag::TYPE_REST); } // $this->cls( 'Control' )->set_stale(); do_action('litespeed_purged_post', $pid); } /** * Hooked to the load-widgets.php action. * Attempts to purge a single widget from cache. * If no widget id is passed in, the method will attempt to find the widget id. * * @since 1.1.3 * @access public */ public static function purge_widget($widget_id = null) { if (is_null($widget_id)) { $widget_id = $_POST['widget-id']; if (is_null($widget_id)) { return; } } self::add(Tag::TYPE_WIDGET . $widget_id); self::add_private(Tag::TYPE_WIDGET . $widget_id); do_action('litespeed_purged_widget', $widget_id); } /** * Hooked to the wp_update_comment_count action. * Purges the comment widget when the count is updated. * * @access public * @since 1.1.3 * @global type $wp_widget_factory */ public static function purge_comment_widget() { global $wp_widget_factory; if (!isset($wp_widget_factory->widgets['WP_Widget_Recent_Comments'])) { return; } $recent_comments = $wp_widget_factory->widgets['WP_Widget_Recent_Comments']; if (!is_null($recent_comments)) { self::add(Tag::TYPE_WIDGET . $recent_comments->id); self::add_private(Tag::TYPE_WIDGET . $recent_comments->id); do_action('litespeed_purged_comment_widget', $recent_comments->id); } } /** * Purges feeds on comment count update. * * @since 1.0.9 * @access public */ public function purge_feeds() { if ($this->conf(self::O_CACHE_TTL_FEED) > 0) { self::add(Tag::TYPE_FEED); } do_action('litespeed_purged_feeds'); } /** * Purges all private cache entries when the user logs out. * * @access public * @since 1.1.3 */ public static function purge_on_logout() { self::add_private_all(); do_action('litespeed_purged_on_logout'); } /** * Generate all purge tags before output * * @access private * @since 1.1.3 */ private function _finalize() { // Make sure header output only run once if (!defined('LITESPEED_DID_' . __FUNCTION__)) { define('LITESPEED_DID_' . __FUNCTION__, true); } else { return; } do_action('litespeed_purge_finalize'); // Append unique uri purge tags if Admin QS is `PURGESINGLE` or `PURGE` if ($this->_purge_single) { $tags = array(Tag::build_uri_tag()); $this->_pub_purge = array_merge($this->_pub_purge, $this->_prepend_bid($tags)); } if (!empty($this->_pub_purge)) { $this->_pub_purge = array_unique($this->_pub_purge); } if (!empty($this->_priv_purge)) { $this->_priv_purge = array_unique($this->_priv_purge); } } /** * Gathers all the purge headers. * * This will collect all site wide purge tags as well as third party plugin defined purge tags. * * @since 1.1.0 * @access public * @return string the built purge header */ public static function output() { $instance = self::cls(); $instance->_finalize(); return $instance->_build(); } /** * Build the current purge headers. * * @since 1.1.5 * @access private * @return string the built purge header */ private function _build($purge2 = false) { if ($purge2) { if (empty($this->_pub_purge2)) { return; } } else { if (empty($this->_pub_purge) && empty($this->_priv_purge)) { return; } } $purge_header = ''; // Handle purge2 @since 4.4.1 if ($purge2) { $public_tags = $this->_append_prefix($this->_pub_purge2); if (empty($public_tags)) { return; } $purge_header = self::X_HEADER2 . ': public,'; if (Control::is_stale()) { $purge_header .= 'stale,'; } $purge_header .= implode(',', $public_tags); return $purge_header; } $private_prefix = self::X_HEADER . ': private,'; if (!empty($this->_pub_purge)) { $public_tags = $this->_append_prefix($this->_pub_purge); if (empty($public_tags)) { // If this ends up empty, private will also end up empty return; } $purge_header = self::X_HEADER . ': public,'; if (Control::is_stale()) { $purge_header .= 'stale,'; } $purge_header .= implode(',', $public_tags); $private_prefix = ';private,'; } // Handle priv purge tags if (!empty($this->_priv_purge)) { $private_tags = $this->_append_prefix($this->_priv_purge, true); $purge_header .= $private_prefix . implode(',', $private_tags); } return $purge_header; } /** * Append prefix to an array of purge headers * * @since 1.1.0 * @access private */ private function _append_prefix($purge_tags, $is_private = false) { $curr_bid = is_multisite() ? get_current_blog_id() : ''; if (!in_array('*', $purge_tags)) { $tags = array(); foreach ($purge_tags as $val) { $tags[] = LSWCP_TAG_PREFIX . $val; } return $tags; } // Purge All need to check if need to reset crawler or not if (!$is_private && $this->conf(self::O_CRAWLER)) { Crawler::cls()->reset_pos(); } if ((defined('LSWCP_EMPTYCACHE') && LSWCP_EMPTYCACHE) || $is_private) { return array('*'); } if (is_multisite() && !$this->_is_subsite_purge()) { $blogs = Activation::get_network_ids(); if (empty($blogs)) { self::debug('build_purge_headers: blog list is empty'); return ''; } $tags = array(); foreach ($blogs as $blog_id) { $tags[] = LSWCP_TAG_PREFIX . $blog_id . '_'; } return $tags; } else { return array(LSWCP_TAG_PREFIX . $curr_bid . '_'); } } /** * Check if this purge belongs to a subsite purge * * @since 4.0 */ private function _is_subsite_purge() { if (!is_multisite()) { return false; } if (is_network_admin()) { return false; } if (defined('LSWCP_EMPTYCACHE') && LSWCP_EMPTYCACHE) { return false; } // Would only use multisite and network admin except is_network_admin is false for ajax calls, which is used by wordpress updates v4.6+ if (Router::is_ajax() && (check_ajax_referer('updates', false, false) || check_ajax_referer('litespeed-purgeall-network', false, false))) { return false; } return true; } /** * Gets all the purge tags correlated with the post about to be purged. * * If the purge all pages configuration is set, all pages will be purged. * * This includes site wide post types (e.g. front page) as well as any third party plugin specific post tags. * * @since 1.0.0 * @access private */ private function _get_purge_tags_by_post($post_id) { // If this is a valid post we want to purge the post, the home page and any associated tags & cats // If not, purge everything on the site. $purge_tags = array(); if ($this->conf(self::O_PURGE_POST_ALL)) { // ignore the rest if purge all return array('*'); } // now do API hook action for post purge do_action('litespeed_api_purge_post', $post_id); // post $purge_tags[] = Tag::TYPE_POST . $post_id; $post_status = get_post_status($post_id); if (function_exists('is_post_status_viewable')) { $viewable = is_post_status_viewable($post_status); if ($viewable) { $purge_tags[] = Tag::get_uri_tag(wp_make_link_relative(get_permalink($post_id))); } } // for archive of categories|tags|custom tax global $post; $original_post = $post; $post = get_post($post_id); $post_type = $post->post_type; global $wp_widget_factory; // recent_posts $recent_posts = isset($wp_widget_factory->widgets['WP_Widget_Recent_Posts']) ? $wp_widget_factory->widgets['WP_Widget_Recent_Posts'] : null; if (!is_null($recent_posts)) { $purge_tags[] = Tag::TYPE_WIDGET . $recent_posts->id; } // get adjacent posts id as related post tag if ($post_type == 'post') { $prev_post = get_previous_post(); $next_post = get_next_post(); if (!empty($prev_post->ID)) { $purge_tags[] = Tag::TYPE_POST . $prev_post->ID; self::debug('--------purge_tags prev is: ' . $prev_post->ID); } if (!empty($next_post->ID)) { $purge_tags[] = Tag::TYPE_POST . $next_post->ID; self::debug('--------purge_tags next is: ' . $next_post->ID); } } if ($this->conf(self::O_PURGE_POST_TERM)) { $taxonomies = get_object_taxonomies($post_type); //self::debug('purge by post, check tax = ' . var_export($taxonomies, true)); foreach ($taxonomies as $tax) { $terms = get_the_terms($post_id, $tax); if (!empty($terms)) { foreach ($terms as $term) { $purge_tags[] = Tag::TYPE_ARCHIVE_TERM . $term->term_id; } } } } if ($this->conf(self::O_CACHE_TTL_FEED)) { $purge_tags[] = Tag::TYPE_FEED; } // author, for author posts and feed list if ($this->conf(self::O_PURGE_POST_AUTHOR)) { $purge_tags[] = Tag::TYPE_AUTHOR . get_post_field('post_author', $post_id); } // archive and feed of post type // todo: check if type contains space if ($this->conf(self::O_PURGE_POST_POSTTYPE)) { if (get_post_type_archive_link($post_type)) { $purge_tags[] = Tag::TYPE_ARCHIVE_POSTTYPE . $post_type; $purge_tags[] = $post_type; } } if ($this->conf(self::O_PURGE_POST_FRONTPAGE)) { $purge_tags[] = Tag::TYPE_FRONTPAGE; } if ($this->conf(self::O_PURGE_POST_HOMEPAGE)) { $purge_tags[] = Tag::TYPE_HOME; } if ($this->conf(self::O_PURGE_POST_PAGES)) { $purge_tags[] = Tag::TYPE_PAGES; } if ($this->conf(self::O_PURGE_POST_PAGES_WITH_RECENT_POSTS)) { $purge_tags[] = Tag::TYPE_PAGES_WITH_RECENT_POSTS; } // if configured to have archived by date $date = $post->post_date; $date = strtotime($date); if ($this->conf(self::O_PURGE_POST_DATE)) { $purge_tags[] = Tag::TYPE_ARCHIVE_DATE . date('Ymd', $date); } if ($this->conf(self::O_PURGE_POST_MONTH)) { $purge_tags[] = Tag::TYPE_ARCHIVE_DATE . date('Ym', $date); } if ($this->conf(self::O_PURGE_POST_YEAR)) { $purge_tags[] = Tag::TYPE_ARCHIVE_DATE . date('Y', $date); } // Set back to original post as $post_id might affecting the global $post value $post = $original_post; return array_unique($purge_tags); } /** * The dummy filter for purge all * * @since 1.1.5 * @access public * @param string $val The filter value * @return string The filter value */ public static function filter_with_purge_all($val) { self::purge_all(); return $val; } } file.cls.php 0000644 00000024734 15162226267 0006777 0 ustar 00 <?php /** * LiteSpeed File Operator Library Class * Append/Replace content to a file * * @since 1.1.0 */ namespace LiteSpeed; defined('WPINC') || exit(); class File { const MARKER = 'LiteSpeed Operator'; /** * Detect if an URL is 404 * * @since 3.3 */ public static function is_404($url) { $response = wp_safe_remote_get($url); $code = wp_remote_retrieve_response_code($response); if ($code == 404) { return true; } return false; } /** * Delete folder * * @since 2.1 */ public static function rrmdir($dir) { $files = array_diff(scandir($dir), array('.', '..')); foreach ($files as $file) { is_dir("$dir/$file") ? self::rrmdir("$dir/$file") : unlink("$dir/$file"); } return rmdir($dir); } public static function count_lines($filename) { if (!file_exists($filename)) { return 0; } $file = new \SplFileObject($filename); $file->seek(PHP_INT_MAX); return $file->key() + 1; } /** * Read data from file * * @since 1.1.0 * @param string $filename * @param int $start_line * @param int $lines */ public static function read($filename, $start_line = null, $lines = null) { if (!file_exists($filename)) { return ''; } if (!is_readable($filename)) { return false; } if ($start_line !== null) { $res = array(); $file = new \SplFileObject($filename); $file->seek($start_line); if ($lines === null) { while (!$file->eof()) { $res[] = rtrim($file->current(), "\n"); $file->next(); } } else { for ($i = 0; $i < $lines; $i++) { if ($file->eof()) { break; } $res[] = rtrim($file->current(), "\n"); $file->next(); } } unset($file); return $res; } $content = file_get_contents($filename); $content = self::remove_zero_space($content); return $content; } /** * Append data to file * * @since 1.1.5 * @access public * @param string $filename * @param string $data * @param boolean $mkdir * @param boolean $silence Used to avoid WP's functions are used */ public static function append($filename, $data, $mkdir = false, $silence = true) { return self::save($filename, $data, $mkdir, true, $silence); } /** * Save data to file * * @since 1.1.0 * @param string $filename * @param string $data * @param boolean $mkdir * @param boolean $append If the content needs to be appended * @param boolean $silence Used to avoid WP's functions are used */ public static function save($filename, $data, $mkdir = false, $append = false, $silence = true) { if (is_null($filename)) { return $silence ? false : __('Filename is empty!', 'litespeed-cache'); } $error = false; $folder = dirname($filename); // mkdir if folder does not exist if (!file_exists($folder)) { if (!$mkdir) { return $silence ? false : sprintf(__('Folder does not exist: %s', 'litespeed-cache'), $folder); } set_error_handler('litespeed_exception_handler'); try { mkdir($folder, 0755, true); // Create robots.txt file to forbid search engine indexes if (!file_exists(LITESPEED_STATIC_DIR . '/robots.txt')) { file_put_contents(LITESPEED_STATIC_DIR . '/robots.txt', "User-agent: *\nDisallow: /\n"); } } catch (\ErrorException $ex) { return $silence ? false : sprintf(__('Can not create folder: %1$s. Error: %2$s', 'litespeed-cache'), $folder, $ex->getMessage()); } restore_error_handler(); } if (!file_exists($filename)) { if (!is_writable($folder)) { return $silence ? false : sprintf(__('Folder is not writable: %s.', 'litespeed-cache'), $folder); } set_error_handler('litespeed_exception_handler'); try { touch($filename); } catch (\ErrorException $ex) { return $silence ? false : sprintf(__('File %s is not writable.', 'litespeed-cache'), $filename); } restore_error_handler(); } elseif (!is_writable($filename)) { return $silence ? false : sprintf(__('File %s is not writable.', 'litespeed-cache'), $filename); } $data = self::remove_zero_space($data); $ret = file_put_contents($filename, $data, $append ? FILE_APPEND : LOCK_EX); if ($ret === false) { return $silence ? false : sprintf(__('Failed to write to %s.', 'litespeed-cache'), $filename); } return true; } /** * Remove Unicode zero-width space <200b><200c> * * @since 2.1.2 * @since 2.9 changed to public */ public static function remove_zero_space($content) { if (is_array($content)) { $content = array_map(__CLASS__ . '::remove_zero_space', $content); return $content; } // Remove UTF-8 BOM if present if (substr($content, 0, 3) === "\xEF\xBB\xBF") { $content = substr($content, 3); } $content = str_replace("\xe2\x80\x8b", '', $content); $content = str_replace("\xe2\x80\x8c", '', $content); $content = str_replace("\xe2\x80\x8d", '', $content); return $content; } /** * Appends an array of strings into a file (.htaccess ), placing it between * BEGIN and END markers. * * Replaces existing marked info. Retains surrounding * data. Creates file if none exists. * * @param string $filename Filename to alter. * @param string $marker The marker to alter. * @param array|string $insertion The new content to insert. * @param bool $prepend Prepend insertion if not exist. * @return bool True on write success, false on failure. */ public static function insert_with_markers($filename, $insertion = false, $marker = false, $prepend = false) { if (!$marker) { $marker = self::MARKER; } if (!$insertion) { $insertion = array(); } return self::_insert_with_markers($filename, $marker, $insertion, $prepend); //todo: capture exceptions } /** * Return wrapped block data with marker * * @param string $insertion * @param string $marker * @return string The block data */ public static function wrap_marker_data($insertion, $marker = false) { if (!$marker) { $marker = self::MARKER; } $start_marker = "# BEGIN {$marker}"; $end_marker = "# END {$marker}"; $new_data = implode("\n", array_merge(array($start_marker), $insertion, array($end_marker))); return $new_data; } /** * Touch block data from file, return with marker * * @param string $filename * @param string $marker * @return string The current block data */ public static function touch_marker_data($filename, $marker = false) { if (!$marker) { $marker = self::MARKER; } $result = self::_extract_from_markers($filename, $marker); if (!$result) { return false; } $start_marker = "# BEGIN {$marker}"; $end_marker = "# END {$marker}"; $new_data = implode("\n", array_merge(array($start_marker), $result, array($end_marker))); return $new_data; } /** * Extracts strings from between the BEGIN and END markers in the .htaccess file. * * @param string $filename * @param string $marker * @return array An array of strings from a file (.htaccess ) from between BEGIN and END markers. */ public static function extract_from_markers($filename, $marker = false) { if (!$marker) { $marker = self::MARKER; } return self::_extract_from_markers($filename, $marker); } /** * Extracts strings from between the BEGIN and END markers in the .htaccess file. * * @param string $filename * @param string $marker * @return array An array of strings from a file (.htaccess ) from between BEGIN and END markers. */ private static function _extract_from_markers($filename, $marker) { $result = array(); if (!file_exists($filename)) { return $result; } if ($markerdata = explode("\n", implode('', file($filename)))) { $state = false; foreach ($markerdata as $markerline) { if (strpos($markerline, '# END ' . $marker) !== false) { $state = false; } if ($state) { $result[] = $markerline; } if (strpos($markerline, '# BEGIN ' . $marker) !== false) { $state = true; } } } return array_map('trim', $result); } /** * Inserts an array of strings into a file (.htaccess ), placing it between BEGIN and END markers. * * Replaces existing marked info. Retains surrounding data. Creates file if none exists. * * NOTE: will throw error if failed * * @since 3.0- * @since 3.0 Throw errors if failed * @access private */ private static function _insert_with_markers($filename, $marker, $insertion, $prepend = false) { if (!file_exists($filename)) { if (!is_writable(dirname($filename))) { Error::t('W', dirname($filename)); } set_error_handler('litespeed_exception_handler'); try { touch($filename); } catch (\ErrorException $ex) { Error::t('W', $filename); } restore_error_handler(); } elseif (!is_writable($filename)) { Error::t('W', $filename); } if (!is_array($insertion)) { $insertion = explode("\n", $insertion); } $start_marker = "# BEGIN {$marker}"; $end_marker = "# END {$marker}"; $fp = fopen($filename, 'r+'); if (!$fp) { Error::t('W', $filename); } // Attempt to get a lock. If the filesystem supports locking, this will block until the lock is acquired. flock($fp, LOCK_EX); $lines = array(); while (!feof($fp)) { $lines[] = rtrim(fgets($fp), "\r\n"); } // Split out the existing file into the preceding lines, and those that appear after the marker $pre_lines = $post_lines = $existing_lines = array(); $found_marker = $found_end_marker = false; foreach ($lines as $line) { if (!$found_marker && false !== strpos($line, $start_marker)) { $found_marker = true; continue; } elseif (!$found_end_marker && false !== strpos($line, $end_marker)) { $found_end_marker = true; continue; } if (!$found_marker) { $pre_lines[] = $line; } elseif ($found_marker && $found_end_marker) { $post_lines[] = $line; } else { $existing_lines[] = $line; } } // Check to see if there was a change if ($existing_lines === $insertion) { flock($fp, LOCK_UN); fclose($fp); return true; } // Check if need to prepend data if not exist if ($prepend && !$post_lines) { // Generate the new file data $new_file_data = implode("\n", array_merge(array($start_marker), $insertion, array($end_marker), $pre_lines)); } else { // Generate the new file data $new_file_data = implode("\n", array_merge($pre_lines, array($start_marker), $insertion, array($end_marker), $post_lines)); } // Write to the start of the file, and truncate it to that length fseek($fp, 0); $bytes = fwrite($fp, $new_file_data); if ($bytes) { ftruncate($fp, ftell($fp)); } fflush($fp); flock($fp, LOCK_UN); fclose($fp); return (bool) $bytes; } } db-optm.cls.php 0000644 00000023513 15162226271 0007407 0 ustar 00 <?php /** * The admin optimize tool * * * @since 1.2.1 * @package LiteSpeed * @subpackage LiteSpeed/src * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class DB_Optm extends Root { private static $_hide_more = false; private static $TYPES = array( 'revision', 'orphaned_post_meta', 'auto_draft', 'trash_post', 'spam_comment', 'trash_comment', 'trackback-pingback', 'expired_transient', 'all_transients', 'optimize_tables', ); const TYPE_CONV_TB = 'conv_innodb'; /** * Show if there are more sites in hidden * * @since 3.0 */ public static function hide_more() { return self::$_hide_more; } /** * Clean/Optimize WP tables * * @since 1.2.1 * @access public * @param string $type The type to clean * @param bool $ignore_multisite If ignore multisite check * @return int The rows that will be affected */ public function db_count($type, $ignore_multisite = false) { if ($type === 'all') { $num = 0; foreach (self::$TYPES as $v) { $num += $this->db_count($v); } return $num; } if (!$ignore_multisite) { if (is_multisite() && is_network_admin()) { $num = 0; $blogs = Activation::get_network_ids(); foreach ($blogs as $k => $blog_id) { if ($k > 3) { self::$_hide_more = true; break; } switch_to_blog($blog_id); $num += $this->db_count($type, true); restore_current_blog(); } return $num; } } global $wpdb; switch ($type) { case 'revision': $rev_max = (int) $this->conf(Base::O_DB_OPTM_REVISIONS_MAX); $rev_age = (int) $this->conf(Base::O_DB_OPTM_REVISIONS_AGE); $sql_add = ''; if ($rev_age) { $sql_add = " and post_modified < DATE_SUB( NOW(), INTERVAL $rev_age DAY ) "; } $sql = "SELECT COUNT(*) FROM `$wpdb->posts` WHERE post_type = 'revision' $sql_add"; if (!$rev_max) { return $wpdb->get_var($sql); } // Has count limit $sql = "SELECT COUNT(*)-$rev_max FROM `$wpdb->posts` WHERE post_type = 'revision' $sql_add GROUP BY post_parent HAVING count(*)>$rev_max"; $res = $wpdb->get_results($sql, ARRAY_N); Utility::compatibility(); return array_sum(array_column($res, 0)); case 'orphaned_post_meta': return $wpdb->get_var("SELECT COUNT(*) FROM `$wpdb->postmeta` a LEFT JOIN `$wpdb->posts` b ON b.ID=a.post_id WHERE b.ID IS NULL"); case 'auto_draft': return $wpdb->get_var("SELECT COUNT(*) FROM `$wpdb->posts` WHERE post_status = 'auto-draft'"); case 'trash_post': return $wpdb->get_var("SELECT COUNT(*) FROM `$wpdb->posts` WHERE post_status = 'trash'"); case 'spam_comment': return $wpdb->get_var("SELECT COUNT(*) FROM `$wpdb->comments` WHERE comment_approved = 'spam'"); case 'trash_comment': return $wpdb->get_var("SELECT COUNT(*) FROM `$wpdb->comments` WHERE comment_approved = 'trash'"); case 'trackback-pingback': return $wpdb->get_var("SELECT COUNT(*) FROM `$wpdb->comments` WHERE comment_type = 'trackback' OR comment_type = 'pingback'"); case 'expired_transient': return $wpdb->get_var("SELECT COUNT(*) FROM `$wpdb->options` WHERE option_name LIKE '_transient_timeout%' AND option_value < " . time()); case 'all_transients': return $wpdb->get_var("SELECT COUNT(*) FROM `$wpdb->options` WHERE option_name LIKE '%_transient_%'"); case 'optimize_tables': return $wpdb->get_var("SELECT COUNT(*) FROM information_schema.tables WHERE TABLE_SCHEMA = '" . DB_NAME . "' and ENGINE <> 'InnoDB' and DATA_FREE > 0"); } return '-'; } /** * Clean/Optimize WP tables * * @since 1.2.1 * @since 3.0 changed to private * @access private */ private function _db_clean($type) { if ($type === 'all') { foreach (self::$TYPES as $v) { $this->_db_clean($v); } return __('Clean all successfully.', 'litespeed-cache'); } global $wpdb; switch ($type) { case 'revision': $rev_max = (int) $this->conf(Base::O_DB_OPTM_REVISIONS_MAX); $rev_age = (int) $this->conf(Base::O_DB_OPTM_REVISIONS_AGE); $postmeta = "`$wpdb->postmeta`"; $posts = "`$wpdb->posts`"; $sql_postmeta_join = function ($table) use ($postmeta, $posts) { return " $postmeta CROSS JOIN $table ON $posts.ID = $postmeta.post_id "; }; $sql_where = "WHERE $posts.post_type = 'revision'"; $sql_add = $rev_age ? "AND $posts.post_modified < DATE_SUB( NOW(), INTERVAL $rev_age DAY )" : ''; if (!$rev_max) { $sql_where = "$sql_where $sql_add"; $sql_postmeta = $sql_postmeta_join($posts); $wpdb->query("DELETE $postmeta FROM $sql_postmeta $sql_where"); $wpdb->query("DELETE FROM $posts $sql_where"); } else { // Has count limit $sql = " SELECT COUNT(*) - $rev_max AS del_max, post_parent FROM $posts WHERE post_type = 'revision' $sql_add GROUP BY post_parent HAVING COUNT(*) > $rev_max "; $res = $wpdb->get_results($sql); $sql_where = " $sql_where AND post_parent = %d ORDER BY ID LIMIT %d "; $sql_postmeta = $sql_postmeta_join("(SELECT ID FROM $posts $sql_where) AS $posts"); foreach ($res as $v) { $args = array($v->post_parent, $v->del_max); $sql = $wpdb->prepare("DELETE $postmeta FROM $sql_postmeta", $args); $wpdb->query($sql); $sql = $wpdb->prepare("DELETE FROM $posts $sql_where", $args); $wpdb->query($sql); } } return __('Clean post revisions successfully.', 'litespeed-cache'); case 'orphaned_post_meta': $wpdb->query("DELETE a FROM `$wpdb->postmeta` a LEFT JOIN `$wpdb->posts` b ON b.ID=a.post_id WHERE b.ID IS NULL"); return __('Clean orphaned post meta successfully.', 'litespeed-cache'); case 'auto_draft': $wpdb->query("DELETE FROM `$wpdb->posts` WHERE post_status = 'auto-draft'"); return __('Clean auto drafts successfully.', 'litespeed-cache'); case 'trash_post': $wpdb->query("DELETE FROM `$wpdb->posts` WHERE post_status = 'trash'"); return __('Clean trashed posts and pages successfully.', 'litespeed-cache'); case 'spam_comment': $wpdb->query("DELETE FROM `$wpdb->comments` WHERE comment_approved = 'spam'"); return __('Clean spam comments successfully.', 'litespeed-cache'); case 'trash_comment': $wpdb->query("DELETE FROM `$wpdb->comments` WHERE comment_approved = 'trash'"); return __('Clean trashed comments successfully.', 'litespeed-cache'); case 'trackback-pingback': $wpdb->query("DELETE FROM `$wpdb->comments` WHERE comment_type = 'trackback' OR comment_type = 'pingback'"); return __('Clean trackbacks and pingbacks successfully.', 'litespeed-cache'); case 'expired_transient': $wpdb->query("DELETE FROM `$wpdb->options` WHERE option_name LIKE '_transient_timeout%' AND option_value < " . time()); return __('Clean expired transients successfully.', 'litespeed-cache'); case 'all_transients': $wpdb->query("DELETE FROM `$wpdb->options` WHERE option_name LIKE '%\\_transient\\_%'"); return __('Clean all transients successfully.', 'litespeed-cache'); case 'optimize_tables': $sql = "SELECT table_name, DATA_FREE FROM information_schema.tables WHERE TABLE_SCHEMA = '" . DB_NAME . "' and ENGINE <> 'InnoDB' and DATA_FREE > 0"; $result = $wpdb->get_results($sql); if ($result) { foreach ($result as $row) { $wpdb->query('OPTIMIZE TABLE ' . $row->table_name); } } return __('Optimized all tables.', 'litespeed-cache'); } } /** * Get all myisam tables * * @since 3.0 * @access public */ public function list_myisam() { global $wpdb; $q = "SELECT * FROM information_schema.tables WHERE TABLE_SCHEMA = '" . DB_NAME . "' and ENGINE = 'myisam' AND TABLE_NAME LIKE '{$wpdb->prefix}%'"; return $wpdb->get_results($q); } /** * Convert tables to InnoDB * * @since 3.0 * @access private */ private function _conv_innodb() { global $wpdb; if (empty($_GET['tb'])) { Admin_Display::error('No table to convert'); return; } $tb = false; $list = $this->list_myisam(); foreach ($list as $v) { if ($v->TABLE_NAME == $_GET['tb']) { $tb = $v->TABLE_NAME; break; } } if (!$tb) { Admin_Display::error('No existing table'); return; } $q = 'ALTER TABLE ' . DB_NAME . '.' . $tb . ' ENGINE = InnoDB'; $wpdb->query($q); Debug2::debug("[DB] Converted $tb to InnoDB"); $msg = __('Converted to InnoDB successfully.', 'litespeed-cache'); Admin_Display::success($msg); } /** * Count all autoload size * * @since 3.0 * @access public */ public function autoload_summary() { global $wpdb; $autoloads = function_exists('wp_autoload_values_to_autoload') ? wp_autoload_values_to_autoload() : array('yes', 'on', 'auto-on', 'auto'); $autoloads = '("' . implode('","', $autoloads) . '")'; $summary = $wpdb->get_row("SELECT SUM(LENGTH(option_value)) AS autoload_size,COUNT(*) AS autload_entries FROM `$wpdb->options` WHERE autoload IN " . $autoloads); $summary->autoload_toplist = $wpdb->get_results( "SELECT option_name, LENGTH(option_value) AS option_value_length, autoload FROM `$wpdb->options` WHERE autoload IN " . $autoloads . ' ORDER BY option_value_length DESC LIMIT 20' ); return $summary; } /** * Handle all request actions from main cls * * @since 3.0 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case 'all': case in_array($type, self::$TYPES): if (is_multisite() && is_network_admin()) { $blogs = Activation::get_network_ids(); foreach ($blogs as $blog_id) { switch_to_blog($blog_id); $msg = $this->_db_clean($type); restore_current_blog(); } } else { $msg = $this->_db_clean($type); } Admin_Display::success($msg); break; case self::TYPE_CONV_TB: $this->_conv_innodb(); break; default: break; } Admin::redirect(); } } htaccess.cls.php 0000644 00000060241 15162226273 0007643 0 ustar 00 <?php /** * The htaccess rewrite rule operation class * * * @since 1.0.0 * @package LiteSpeed * @subpackage LiteSpeed/inc * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Htaccess extends Root { private $frontend_htaccess = null; private $_default_frontend_htaccess = null; private $backend_htaccess = null; private $_default_backend_htaccess = null; private $theme_htaccess = null; // Not used yet private $frontend_htaccess_readable = false; private $frontend_htaccess_writable = false; private $backend_htaccess_readable = false; private $backend_htaccess_writable = false; private $theme_htaccess_readable = false; private $theme_htaccess_writable = false; private $__rewrite_on; const LS_MODULE_START = '<IfModule LiteSpeed>'; const EXPIRES_MODULE_START = '<IfModule mod_expires.c>'; const LS_MODULE_END = '</IfModule>'; const LS_MODULE_REWRITE_START = '<IfModule mod_rewrite.c>'; const REWRITE_ON = 'RewriteEngine on'; const LS_MODULE_DONOTEDIT = '## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ##'; const MARKER = 'LSCACHE'; const MARKER_NONLS = 'NON_LSCACHE'; const MARKER_LOGIN_COOKIE = '### marker LOGIN COOKIE'; const MARKER_ASYNC = '### marker ASYNC'; const MARKER_CRAWLER = '### marker CRAWLER'; const MARKER_MOBILE = '### marker MOBILE'; const MARKER_NOCACHE_COOKIES = '### marker NOCACHE COOKIES'; const MARKER_NOCACHE_USER_AGENTS = '### marker NOCACHE USER AGENTS'; const MARKER_CACHE_RESOURCE = '### marker CACHE RESOURCE'; const MARKER_BROWSER_CACHE = '### marker BROWSER CACHE'; const MARKER_MINIFY = '### marker MINIFY'; const MARKER_CORS = '### marker CORS'; const MARKER_WEBP = '### marker WEBP'; const MARKER_DROPQS = '### marker DROPQS'; const MARKER_START = ' start ###'; const MARKER_END = ' end ###'; const RW_PATTERN_RES = '/.*/[^/]*(responsive|css|js|dynamic|loader|fonts)\.php'; /** * Initialize the class and set its properties. * * @since 1.0.7 */ public function __construct() { $this->_path_set(); $this->_default_frontend_htaccess = $this->frontend_htaccess; $this->_default_backend_htaccess = $this->backend_htaccess; $frontend_htaccess = defined('LITESPEED_CFG_HTACCESS') ? LITESPEED_CFG_HTACCESS : false; if ($frontend_htaccess && substr($frontend_htaccess, -10) === '/.htaccess') { $this->frontend_htaccess = $frontend_htaccess; } $backend_htaccess = defined('LITESPEED_CFG_HTACCESS_BACKEND') ? LITESPEED_CFG_HTACCESS_BACKEND : false; if ($backend_htaccess && substr($backend_htaccess, -10) === '/.htaccess') { $this->backend_htaccess = $backend_htaccess; } // Filter for frontend&backend htaccess path $this->frontend_htaccess = apply_filters('litespeed_frontend_htaccess', $this->frontend_htaccess); $this->backend_htaccess = apply_filters('litespeed_backend_htaccess', $this->backend_htaccess); clearstatcache(); // frontend .htaccess privilege $test_permissions = file_exists($this->frontend_htaccess) ? $this->frontend_htaccess : dirname($this->frontend_htaccess); if (is_readable($test_permissions)) { $this->frontend_htaccess_readable = true; } if (is_writable($test_permissions)) { $this->frontend_htaccess_writable = true; } $this->__rewrite_on = array( self::REWRITE_ON, 'CacheLookup on', 'RewriteRule .* - [E=Cache-Control:no-autoflush]', 'RewriteRule ' . preg_quote(LITESPEED_DATA_FOLDER) . '/debug/.*\.log$ - [F,L]', 'RewriteRule ' . preg_quote(self::CONF_FILE) . ' - [F,L]', ); // backend .htaccess privilege if ($this->frontend_htaccess === $this->backend_htaccess) { $this->backend_htaccess_readable = $this->frontend_htaccess_readable; $this->backend_htaccess_writable = $this->frontend_htaccess_writable; } else { $test_permissions = file_exists($this->backend_htaccess) ? $this->backend_htaccess : dirname($this->backend_htaccess); if (is_readable($test_permissions)) { $this->backend_htaccess_readable = true; } if (is_writable($test_permissions)) { $this->backend_htaccess_writable = true; } } } /** * Get if htaccess file is readable * * @since 1.1.0 * @return string */ private function _readable($kind = 'frontend') { if ($kind === 'frontend') { return $this->frontend_htaccess_readable; } if ($kind === 'backend') { return $this->backend_htaccess_readable; } } /** * Get if htaccess file is writable * * @since 1.1.0 * @return string */ public function writable($kind = 'frontend') { if ($kind === 'frontend') { return $this->frontend_htaccess_writable; } if ($kind === 'backend') { return $this->backend_htaccess_writable; } } /** * Get frontend htaccess path * * @since 1.1.0 * @return string */ public static function get_frontend_htaccess($show_default = false) { if ($show_default) { return self::cls()->_default_frontend_htaccess; } return self::cls()->frontend_htaccess; } /** * Get backend htaccess path * * @since 1.1.0 * @return string */ public static function get_backend_htaccess($show_default = false) { if ($show_default) { return self::cls()->_default_backend_htaccess; } return self::cls()->backend_htaccess; } /** * Check to see if .htaccess exists starting at $start_path and going up directories until it hits DOCUMENT_ROOT. * * As dirname() strips the ending '/', paths passed in must exclude the final '/' * * @since 1.0.11 * @access private */ private function _htaccess_search($start_path) { while (!file_exists($start_path . '/.htaccess')) { if ($start_path === '/' || !$start_path) { return false; } if (!empty($_SERVER['DOCUMENT_ROOT']) && wp_normalize_path($start_path) === wp_normalize_path($_SERVER['DOCUMENT_ROOT'])) { return false; } if (dirname($start_path) === $start_path) { return false; } $start_path = dirname($start_path); } return $start_path; } /** * Set the path class variables. * * @since 1.0.11 * @access private */ private function _path_set() { $frontend = Router::frontend_path(); $frontend_htaccess_search = $this->_htaccess_search($frontend); // The existing .htaccess path to be used for frontend .htaccess $this->frontend_htaccess = ($frontend_htaccess_search ?: $frontend) . '/.htaccess'; $backend = realpath(ABSPATH); // /home/user/public_html/backend/ if ($frontend == $backend) { $this->backend_htaccess = $this->frontend_htaccess; return; } // Backend is a different path $backend_htaccess_search = $this->_htaccess_search($backend); // Found affected .htaccess if ($backend_htaccess_search) { $this->backend_htaccess = $backend_htaccess_search . '/.htaccess'; return; } // Frontend path is the parent of backend path if (stripos($backend, $frontend . '/') === 0) { // backend use frontend htaccess $this->backend_htaccess = $this->frontend_htaccess; return; } $this->backend_htaccess = $backend . '/.htaccess'; } /** * Get corresponding htaccess path * * @since 1.1.0 * @param string $kind Frontend or backend * @return string Path */ public function htaccess_path($kind = 'frontend') { switch ($kind) { case 'backend': $path = $this->backend_htaccess; break; case 'frontend': default: $path = $this->frontend_htaccess; break; } return $path; } /** * Get the content of the rules file. * * NOTE: will throw error if failed * * @since 1.0.4 * @since 2.9 Used exception for failed reading * @access public */ public function htaccess_read($kind = 'frontend') { $path = $this->htaccess_path($kind); if (!$path || !file_exists($path)) { return "\n"; } if (!$this->_readable($kind)) { Error::t('HTA_R'); } $content = File::read($path); if ($content === false) { Error::t('HTA_GET'); } // Remove ^M characters. $content = str_ireplace("\x0D", '', $content); return $content; } /** * Try to backup the .htaccess file if we didn't save one before. * * NOTE: will throw error if failed * * @since 1.0.10 * @access private */ private function _htaccess_backup($kind = 'frontend') { $path = $this->htaccess_path($kind); if (!file_exists($path)) { return; } if (file_exists($path . '.bk')) { return; } $res = copy($path, $path . '.bk'); // Failed to backup, abort if (!$res) { Error::t('HTA_BK'); } } /** * Get mobile view rule from htaccess file * * NOTE: will throw error if failed * * @since 1.1.0 */ public function current_mobile_agents() { $rules = $this->_get_rule_by(self::MARKER_MOBILE); if (!isset($rules[0])) { Error::t('HTA_DNF', self::MARKER_MOBILE); } $rule = trim($rules[0]); // 'RewriteCond %{HTTP_USER_AGENT} ' . Utility::arr2regex( $cfg[ $id ], true ) . ' [NC]'; $match = substr($rule, strlen('RewriteCond %{HTTP_USER_AGENT} '), -strlen(' [NC]')); if (!$match) { Error::t('HTA_DNF', __('Mobile Agent Rules', 'litespeed-cache')); } return $match; } /** * Parse rewrites rule from the .htaccess file. * * NOTE: will throw error if failed * * @since 1.1.0 * @access public */ public function current_login_cookie($kind = 'frontend') { $rule = $this->_get_rule_by(self::MARKER_LOGIN_COOKIE, $kind); if (!$rule) { Error::t('HTA_DNF', self::MARKER_LOGIN_COOKIE); } if (strpos($rule, 'RewriteRule .? - [E=') !== 0) { Error::t('HTA_LOGIN_COOKIE_INVALID'); } $rule_cookie = substr($rule, strlen('RewriteRule .? - [E='), -1); if (LITESPEED_SERVER_TYPE === 'LITESPEED_SERVER_OLS') { $rule_cookie = trim($rule_cookie, '"'); } // Drop `Cache-Vary:` $rule_cookie = substr($rule_cookie, strlen('Cache-Vary:')); return $rule_cookie; } /** * Get rewrite rules based on the marker * * @since 2.0 * @access private */ private function _get_rule_by($cond, $kind = 'frontend') { clearstatcache(); $path = $this->htaccess_path($kind); if (!$this->_readable($kind)) { return false; } $rules = File::extract_from_markers($path, self::MARKER); if (!in_array($cond . self::MARKER_START, $rules) || !in_array($cond . self::MARKER_END, $rules)) { return false; } $key_start = array_search($cond . self::MARKER_START, $rules); $key_end = array_search($cond . self::MARKER_END, $rules); if ($key_start === false || $key_end === false) { return false; } $results = array_slice($rules, $key_start + 1, $key_end - $key_start - 1); if (!$results) { return false; } if (count($results) == 1) { return trim($results[0]); } return array_filter($results); } /** * Generate browser cache rules * * @since 1.3 * @access private * @return array Rules set */ private function _browser_cache_rules($cfg) { /** * Add ttl setting * @since 1.6.3 */ $id = Base::O_CACHE_TTL_BROWSER; $ttl = $cfg[$id]; $rules = array( self::EXPIRES_MODULE_START, // '<FilesMatch "\.(pdf|ico|svg|xml|jpg|jpeg|png|gif|webp|ogg|mp4|webm|js|css|woff|woff2|ttf|eot)(\.gz)?$">', 'ExpiresActive on', 'ExpiresByType application/pdf A' . $ttl, 'ExpiresByType image/x-icon A' . $ttl, 'ExpiresByType image/vnd.microsoft.icon A' . $ttl, 'ExpiresByType image/svg+xml A' . $ttl, '', 'ExpiresByType image/jpg A' . $ttl, 'ExpiresByType image/jpeg A' . $ttl, 'ExpiresByType image/png A' . $ttl, 'ExpiresByType image/gif A' . $ttl, 'ExpiresByType image/webp A' . $ttl, 'ExpiresByType image/avif A' . $ttl, '', 'ExpiresByType video/ogg A' . $ttl, 'ExpiresByType audio/ogg A' . $ttl, 'ExpiresByType video/mp4 A' . $ttl, 'ExpiresByType video/webm A' . $ttl, '', 'ExpiresByType text/css A' . $ttl, 'ExpiresByType text/javascript A' . $ttl, 'ExpiresByType application/javascript A' . $ttl, 'ExpiresByType application/x-javascript A' . $ttl, '', 'ExpiresByType application/x-font-ttf A' . $ttl, 'ExpiresByType application/x-font-woff A' . $ttl, 'ExpiresByType application/font-woff A' . $ttl, 'ExpiresByType application/font-woff2 A' . $ttl, 'ExpiresByType application/vnd.ms-fontobject A' . $ttl, 'ExpiresByType font/ttf A' . $ttl, 'ExpiresByType font/otf A' . $ttl, 'ExpiresByType font/woff A' . $ttl, 'ExpiresByType font/woff2 A' . $ttl, '', // '</FilesMatch>', self::LS_MODULE_END, ); return $rules; } /** * Generate CORS rules for fonts * * @since 1.5 * @access private * @return array Rules set */ private function _cors_rules() { return array( '<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font\.css)$">', '<IfModule mod_headers.c>', 'Header set Access-Control-Allow-Origin "*"', '</IfModule>', '</FilesMatch>', ); } /** * Generate rewrite rules based on settings * * @since 1.3 * @access private * @param array $cfg The settings to be used for rewrite rule * @return array Rules array */ private function _generate_rules($cfg) { $new_rules = array(); $new_rules_nonls = array(); $new_rules_backend = array(); $new_rules_backend_nonls = array(); # continual crawler // $id = Base::O_CRAWLER; // if (!empty($cfg[$id])) { $new_rules[] = self::MARKER_ASYNC . self::MARKER_START; $new_rules[] = 'RewriteCond %{REQUEST_URI} /wp-admin/admin-ajax\.php'; $new_rules[] = 'RewriteCond %{QUERY_STRING} action=async_litespeed'; $new_rules[] = 'RewriteRule .* - [E=noabort:1]'; $new_rules[] = self::MARKER_ASYNC . self::MARKER_END; $new_rules[] = ''; // } // mobile agents $id = Base::O_CACHE_MOBILE_RULES; if ((!empty($cfg[Base::O_CACHE_MOBILE]) || !empty($cfg[Base::O_GUEST])) && !empty($cfg[$id])) { $new_rules[] = self::MARKER_MOBILE . self::MARKER_START; $new_rules[] = 'RewriteCond %{HTTP_USER_AGENT} ' . Utility::arr2regex($cfg[$id], true) . ' [NC]'; $new_rules[] = 'RewriteRule .* - [E=Cache-Control:vary=%{ENV:LSCACHE_VARY_VALUE}+ismobile]'; $new_rules[] = self::MARKER_MOBILE . self::MARKER_END; $new_rules[] = ''; } // nocache cookie $id = Base::O_CACHE_EXC_COOKIES; if (!empty($cfg[$id])) { $new_rules[] = self::MARKER_NOCACHE_COOKIES . self::MARKER_START; $new_rules[] = 'RewriteCond %{HTTP_COOKIE} ' . Utility::arr2regex($cfg[$id], true); $new_rules[] = 'RewriteRule .* - [E=Cache-Control:no-cache]'; $new_rules[] = self::MARKER_NOCACHE_COOKIES . self::MARKER_END; $new_rules[] = ''; } // nocache user agents $id = Base::O_CACHE_EXC_USERAGENTS; if (!empty($cfg[$id])) { $new_rules[] = self::MARKER_NOCACHE_USER_AGENTS . self::MARKER_START; $new_rules[] = 'RewriteCond %{HTTP_USER_AGENT} ' . Utility::arr2regex($cfg[$id], true) . ' [NC]'; $new_rules[] = 'RewriteRule .* - [E=Cache-Control:no-cache]'; $new_rules[] = self::MARKER_NOCACHE_USER_AGENTS . self::MARKER_END; $new_rules[] = ''; } // caching php resource TODO: consider drop $id = Base::O_CACHE_RES; if (!empty($cfg[$id])) { $new_rules[] = $new_rules_backend[] = self::MARKER_CACHE_RESOURCE . self::MARKER_START; $new_rules[] = $new_rules_backend[] = 'RewriteRule ' . LSCWP_CONTENT_FOLDER . self::RW_PATTERN_RES . ' - [E=cache-control:max-age=3600]'; $new_rules[] = $new_rules_backend[] = self::MARKER_CACHE_RESOURCE . self::MARKER_END; $new_rules[] = $new_rules_backend[] = ''; } // check login cookie $vary_cookies = $cfg[Base::O_CACHE_VARY_COOKIES]; $id = Base::O_CACHE_LOGIN_COOKIE; if (!empty($cfg[$id])) { $vary_cookies[] = $cfg[$id]; } if (LITESPEED_SERVER_TYPE === 'LITESPEED_SERVER_OLS') { // Need to keep this due to different behavior of OLS when handling response vary header @Sep/22/2018 if (defined('COOKIEHASH')) { $vary_cookies[] = ',wp-postpass_' . COOKIEHASH; } } $vary_cookies = apply_filters('litespeed_vary_cookies', $vary_cookies); // todo: test if response vary header can work in latest OLS, drop the above two lines // frontend and backend if ($vary_cookies) { $env = 'Cache-Vary:' . implode(',', $vary_cookies); // if (LITESPEED_SERVER_TYPE === 'LITESPEED_SERVER_OLS') { // } $env = '"' . $env . '"'; $new_rules[] = $new_rules_backend[] = self::MARKER_LOGIN_COOKIE . self::MARKER_START; $new_rules[] = $new_rules_backend[] = 'RewriteRule .? - [E=' . $env . ']'; $new_rules[] = $new_rules_backend[] = self::MARKER_LOGIN_COOKIE . self::MARKER_END; $new_rules[] = ''; } // CORS font rules $id = Base::O_CDN; if (!empty($cfg[$id])) { $new_rules[] = self::MARKER_CORS . self::MARKER_START; $new_rules = array_merge($new_rules, $this->_cors_rules()); //todo: network $new_rules[] = self::MARKER_CORS . self::MARKER_END; $new_rules[] = ''; } // webp support $id = Base::O_IMG_OPTM_WEBP; if (!empty($cfg[$id])) { $webP_rule = 'RewriteRule .* - [E=Cache-Control:vary=%{ENV:LSCACHE_VARY_VALUE}+webp]'; $next_gen_format = 'webp'; if ($cfg[$id] == 2) { $next_gen_format = 'avif'; } $new_rules[] = self::MARKER_WEBP . self::MARKER_START; $new_rules[] = 'RewriteCond %{HTTP_ACCEPT} "image/' . $next_gen_format . '"'; $new_rules[] = $webP_rule; $new_rules[] = 'RewriteCond %{HTTP_USER_AGENT} iPhone.*Version/(\d{2}).*Safari'; $new_rules[] = 'RewriteCond %1 >13'; $new_rules[] = $webP_rule; $new_rules[] = 'RewriteCond %{HTTP_USER_AGENT} Firefox/([0-9]+)'; $new_rules[] = 'RewriteCond %1 >=65'; $new_rules[] = $webP_rule; $new_rules[] = self::MARKER_WEBP . self::MARKER_END; $new_rules[] = ''; } // drop qs support $id = Base::O_CACHE_DROP_QS; if (!empty($cfg[$id])) { $new_rules[] = self::MARKER_DROPQS . self::MARKER_START; foreach ($cfg[$id] as $v) { $new_rules[] = 'CacheKeyModify -qs:' . $v; } $new_rules[] = self::MARKER_DROPQS . self::MARKER_END; $new_rules[] = ''; } // Browser cache $id = Base::O_CACHE_BROWSER; if (!empty($cfg[$id])) { $new_rules_nonls[] = $new_rules_backend_nonls[] = self::MARKER_BROWSER_CACHE . self::MARKER_START; $new_rules_nonls = array_merge($new_rules_nonls, $this->_browser_cache_rules($cfg)); $new_rules_backend_nonls = array_merge($new_rules_backend_nonls, $this->_browser_cache_rules($cfg)); $new_rules_nonls[] = $new_rules_backend_nonls[] = self::MARKER_BROWSER_CACHE . self::MARKER_END; $new_rules_nonls[] = ''; } // Add module wrapper for LiteSpeed rules if ($new_rules) { $new_rules = $this->_wrap_ls_module($new_rules); } if ($new_rules_backend) { $new_rules_backend = $this->_wrap_ls_module($new_rules_backend); } return array($new_rules, $new_rules_backend, $new_rules_nonls, $new_rules_backend_nonls); } /** * Add LitSpeed module wrapper with rewrite on * * @since 2.1.1 * @access private */ private function _wrap_ls_module($rules = array()) { return array_merge(array(self::LS_MODULE_START), $this->__rewrite_on, array(''), $rules, array(self::LS_MODULE_END)); } /** * Insert LitSpeed module wrapper with rewrite on * * @since 2.1.1 * @access public */ public function insert_ls_wrapper() { $rules = $this->_wrap_ls_module(); $this->_insert_wrapper($rules); } /** * wrap rules with module on info * * @since 1.1.5 * @param array $rules * @return array wrapped rules with module info */ private function _wrap_do_no_edit($rules) { // When to clear rules, don't need DONOTEDIT msg if ($rules === false || !is_array($rules)) { return $rules; } $rules = array_merge(array(self::LS_MODULE_DONOTEDIT), $rules, array(self::LS_MODULE_DONOTEDIT)); return $rules; } /** * Write to htaccess with rules * * NOTE: will throw error if failed * * @since 1.1.0 * @access private */ private function _insert_wrapper($rules = array(), $kind = false, $marker = false) { if ($kind != 'backend') { $kind = 'frontend'; } // Default marker is LiteSpeed marker `LSCACHE` if ($marker === false) { $marker = self::MARKER; } $this->_htaccess_backup($kind); File::insert_with_markers($this->htaccess_path($kind), $this->_wrap_do_no_edit($rules), $marker, true); } /** * Update rewrite rules based on setting * * NOTE: will throw error if failed * * @since 1.3 * @access public */ public function update($cfg) { list($frontend_rules, $backend_rules, $frontend_rules_nonls, $backend_rules_nonls) = $this->_generate_rules($cfg); // Check frontend content list($rules, $rules_nonls) = $this->_extract_rules(); // Check Non-LiteSpeed rules if ($this->_wrap_do_no_edit($frontend_rules_nonls) != $rules_nonls) { Debug2::debug('[Rules] Update non-ls frontend rules'); // Need to update frontend htaccess try { $this->_insert_wrapper($frontend_rules_nonls, false, self::MARKER_NONLS); } catch (\Exception $e) { $manual_guide_codes = $this->_rewrite_codes_msg($this->frontend_htaccess, $frontend_rules_nonls, self::MARKER_NONLS); Debug2::debug('[Rules] Update Failed'); throw new \Exception($manual_guide_codes); } } // Check LiteSpeed rules if ($this->_wrap_do_no_edit($frontend_rules) != $rules) { Debug2::debug('[Rules] Update frontend rules'); // Need to update frontend htaccess try { $this->_insert_wrapper($frontend_rules); } catch (\Exception $e) { Debug2::debug('[Rules] Update Failed'); $manual_guide_codes = $this->_rewrite_codes_msg($this->frontend_htaccess, $frontend_rules); throw new \Exception($manual_guide_codes); } } if ($this->frontend_htaccess !== $this->backend_htaccess) { list($rules, $rules_nonls) = $this->_extract_rules('backend'); // Check Non-LiteSpeed rules for backend if ($this->_wrap_do_no_edit($backend_rules_nonls) != $rules_nonls) { Debug2::debug('[Rules] Update non-ls backend rules'); // Need to update frontend htaccess try { $this->_insert_wrapper($backend_rules_nonls, 'backend', self::MARKER_NONLS); } catch (\Exception $e) { Debug2::debug('[Rules] Update Failed'); $manual_guide_codes = $this->_rewrite_codes_msg($this->backend_htaccess, $backend_rules_nonls, self::MARKER_NONLS); throw new \Exception($manual_guide_codes); } } // Check backend content if ($this->_wrap_do_no_edit($backend_rules) != $rules) { Debug2::debug('[Rules] Update backend rules'); // Need to update backend htaccess try { $this->_insert_wrapper($backend_rules, 'backend'); } catch (\Exception $e) { Debug2::debug('[Rules] Update Failed'); $manual_guide_codes = $this->_rewrite_codes_msg($this->backend_htaccess, $backend_rules); throw new \Exception($manual_guide_codes); } } } return true; } /** * Get existing rewrite rules * * NOTE: will throw error if failed * * @since 1.3 * @access private * @param string $kind Frontend or backend .htaccess file */ private function _extract_rules($kind = 'frontend') { clearstatcache(); $path = $this->htaccess_path($kind); if (!$this->_readable($kind)) { Error::t('E_HTA_R'); } $rules = File::extract_from_markers($path, self::MARKER); $rules_nonls = File::extract_from_markers($path, self::MARKER_NONLS); return array($rules, $rules_nonls); } /** * Output the msg with rules plain data for manual insert * * @since 1.1.5 * @param string $file * @param array $rules * @return string final msg to output */ private function _rewrite_codes_msg($file, $rules, $marker = false) { return sprintf( __('<p>Please add/replace the following codes into the beginning of %1$s:</p> %2$s', 'litespeed-cache'), $file, '<textarea style="width:100%;" rows="10" readonly>' . htmlspecialchars($this->_wrap_rules_with_marker($rules, $marker)) . '</textarea>' ); } /** * Generate rules plain data for manual insert * * @since 1.1.5 */ private function _wrap_rules_with_marker($rules, $marker = false) { // Default marker is LiteSpeed marker `LSCACHE` if ($marker === false) { $marker = self::MARKER; } $start_marker = "# BEGIN {$marker}"; $end_marker = "# END {$marker}"; $new_file_data = implode("\n", array_merge(array($start_marker), $this->_wrap_do_no_edit($rules), array($end_marker))); return $new_file_data; } /** * Clear the rules file of any changes added by the plugin specifically. * * @since 1.0.4 * @access public */ public function clear_rules() { $this->_insert_wrapper(false); // Use false to avoid do-not-edit msg // Clear non ls rules $this->_insert_wrapper(false, false, self::MARKER_NONLS); if ($this->frontend_htaccess !== $this->backend_htaccess) { $this->_insert_wrapper(false, 'backend'); $this->_insert_wrapper(false, 'backend', self::MARKER_NONLS); } } } gui.cls.php 0000644 00000066745 15162226274 0006652 0 ustar 00 <?php /** * The frontend GUI class. * * @since 1.3 * @subpackage LiteSpeed/src * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class GUI extends Base { private static $_clean_counter = 0; private $_promo_true; // [ file_tag => [ days, litespeed_only ], ... ] private $_promo_list = array( 'new_version' => array(7, false), 'score' => array(14, false), // 'slack' => array( 3, false ), ); const LIB_GUEST_JS = 'assets/js/guest.min.js'; const LIB_GUEST_DOCREF_JS = 'assets/js/guest.docref.min.js'; const PHP_GUEST = 'guest.vary.php'; const TYPE_DISMISS_WHM = 'whm'; const TYPE_DISMISS_EXPIRESDEFAULT = 'ExpiresDefault'; const TYPE_DISMISS_PROMO = 'promo'; const TYPE_DISMISS_PIN = 'pin'; const WHM_MSG = 'lscwp_whm_install'; const WHM_MSG_VAL = 'whm_install'; protected $_summary; /** * Instance * * @since 1.3 */ public function __construct() { $this->_summary = self::get_summary(); } /** * Frontend Init * * @since 3.0 */ public function init() { Debug2::debug2('[GUI] init'); if (is_admin_bar_showing() && current_user_can('manage_options')) { add_action('wp_enqueue_scripts', array($this, 'frontend_enqueue_style')); add_action('admin_bar_menu', array($this, 'frontend_shortcut'), 95); } /** * Turn on instant click * @since 1.8.2 */ if ($this->conf(self::O_UTIL_INSTANT_CLICK)) { add_action('wp_enqueue_scripts', array($this, 'frontend_enqueue_style_public')); } // NOTE: this needs to be before optimizer to avoid wrapper being removed add_filter('litespeed_buffer_finalize', array($this, 'finalize'), 8); } /** * Print a loading message when redirecting CCSS/UCSS page to avoid whiteboard confusion */ public static function print_loading($counter, $type) { echo '<div style="font-size: 25px; text-align: center; padding-top: 150px; width: 100%; position: absolute;">'; echo "<img width='35' src='" . LSWCP_PLUGIN_URL . "assets/img/Litespeed.icon.svg' /> "; echo sprintf(__('%1$s %2$s files left in queue', 'litespeed-cache'), $counter, $type); echo '<p><a href="' . admin_url('admin.php?page=litespeed-page_optm') . '">' . __('Cancel', 'litespeed-cache') . '</a></p>'; echo '</div>'; } /** * Display a pie * * @since 1.6.6 */ public static function pie($percent, $width = 50, $finished_tick = false, $without_percentage = false, $append_cls = false) { $percentage = '<text x="50%" y="50%">' . $percent . ($without_percentage ? '' : '%') . '</text>'; if ($percent == 100 && $finished_tick) { $percentage = '<text x="50%" y="50%" class="litespeed-pie-done">✓</text>'; } return " <svg class='litespeed-pie $append_cls' viewbox='0 0 33.83098862 33.83098862' width='$width' height='$width' xmlns='http://www.w3.org/2000/svg'> <circle class='litespeed-pie_bg' cx='16.91549431' cy='16.91549431' r='15.91549431' /> <circle class='litespeed-pie_circle' cx='16.91549431' cy='16.91549431' r='15.91549431' stroke-dasharray='$percent,100' /> <g class='litespeed-pie_info'>$percentage</g> </svg> "; } /** * Display a tiny pie with a tooltip * * @since 3.0 */ public static function pie_tiny($percent, $width = 50, $tooltip = '', $tooltip_pos = 'up', $append_cls = false) { // formula C = 2πR $dasharray = 2 * 3.1416 * 9 * ($percent / 100); return " <button type='button' data-balloon-break data-balloon-pos='$tooltip_pos' aria-label='$tooltip' class='litespeed-btn-pie'> <svg class='litespeed-pie litespeed-pie-tiny $append_cls' viewbox='0 0 30 30' width='$width' height='$width' xmlns='http://www.w3.org/2000/svg'> <circle class='litespeed-pie_bg' cx='15' cy='15' r='9' /> <circle class='litespeed-pie_circle' cx='15' cy='15' r='9' stroke-dasharray='$dasharray,100' /> <g class='litespeed-pie_info'><text x='50%' y='50%'>i</text></g> </svg> </button> "; } /** * Get classname of PageSpeed Score * * Scale: * 90-100 (fast) * 50-89 (average) * 0-49 (slow) * * @since 2.9 * @access public */ public function get_cls_of_pagescore($score) { if ($score >= 90) { return 'success'; } if ($score >= 50) { return 'warning'; } return 'danger'; } /** * Dismiss banner * * @since 1.0 * @access public */ public static function dismiss() { $_instance = self::cls(); switch (Router::verify_type()) { case self::TYPE_DISMISS_WHM: self::dismiss_whm(); break; case self::TYPE_DISMISS_EXPIRESDEFAULT: self::update_option(Admin_Display::DB_DISMISS_MSG, Admin_Display::RULECONFLICT_DISMISSED); break; case self::TYPE_DISMISS_PIN: admin_display::dismiss_pin(); break; case self::TYPE_DISMISS_PROMO: if (empty($_GET['promo_tag'])) { break; } $promo_tag = sanitize_key($_GET['promo_tag']); if (empty($_instance->_promo_list[$promo_tag])) { break; } defined('LSCWP_LOG') && Debug2::debug('[GUI] Dismiss promo ' . $promo_tag); // Forever dismiss if (!empty($_GET['done'])) { $_instance->_summary[$promo_tag] = 'done'; } elseif (!empty($_GET['later'])) { // Delay the banner to half year later $_instance->_summary[$promo_tag] = time() + 86400 * 180; } else { // Update welcome banner to 30 days after $_instance->_summary[$promo_tag] = time() + 86400 * 30; } self::save_summary(); break; default: break; } if (Router::is_ajax()) { // All dismiss actions are considered as ajax call, so just exit exit(\json_encode(array('success' => 1))); } // Plain click link, redirect to referral url Admin::redirect(); } /** * Check if has rule conflict notice * * @since 1.1.5 * @access public * @return boolean */ public static function has_msg_ruleconflict() { $db_dismiss_msg = self::get_option(Admin_Display::DB_DISMISS_MSG); if (!$db_dismiss_msg) { self::update_option(Admin_Display::DB_DISMISS_MSG, -1); } return $db_dismiss_msg == Admin_Display::RULECONFLICT_ON; } /** * Check if has whm notice * * @since 1.1.1 * @access public * @return boolean */ public static function has_whm_msg() { $val = self::get_option(self::WHM_MSG); if (!$val) { self::dismiss_whm(); return false; } return $val == self::WHM_MSG_VAL; } /** * Delete whm msg tag * * @since 1.1.1 * @access public */ public static function dismiss_whm() { self::update_option(self::WHM_MSG, -1); } /** * Set current page a litespeed page * * @since 2.9 */ private function _is_litespeed_page() { if ( !empty($_GET['page']) && in_array($_GET['page'], array( 'litespeed-settings', 'litespeed-dash', Admin::PAGE_EDIT_HTACCESS, 'litespeed-optimization', 'litespeed-crawler', 'litespeed-import', 'litespeed-report', )) ) { return true; } return false; } /** * Display promo banner * * @since 2.1 * @access public */ public function show_promo($check_only = false) { $is_litespeed_page = $this->_is_litespeed_page(); // Bypass showing info banner if disabled all in debug if (defined('LITESPEED_DISABLE_ALL') && LITESPEED_DISABLE_ALL) { if ($is_litespeed_page && !$check_only) { include_once LSCWP_DIR . 'tpl/inc/disabled_all.php'; } return false; } if (file_exists(ABSPATH . '.litespeed_no_banner')) { defined('LSCWP_LOG') && Debug2::debug('[GUI] Bypass banners due to silence file'); return false; } foreach ($this->_promo_list as $promo_tag => $v) { list($delay_days, $litespeed_page_only) = $v; if ($litespeed_page_only && !$is_litespeed_page) { continue; } // first time check if (empty($this->_summary[$promo_tag])) { $this->_summary[$promo_tag] = time() + 86400 * $delay_days; self::save_summary(); continue; } $promo_timestamp = $this->_summary[$promo_tag]; // was ticked as done if ($promo_timestamp == 'done') { continue; } // Not reach the dateline yet if (time() < $promo_timestamp) { continue; } // try to load, if can pass, will set $this->_promo_true = true $this->_promo_true = false; include LSCWP_DIR . "tpl/banner/$promo_tag.php"; // If not defined, means it didn't pass the display workflow in tpl. if (!$this->_promo_true) { continue; } if ($check_only) { return $promo_tag; } defined('LSCWP_LOG') && Debug2::debug('[GUI] Show promo ' . $promo_tag); // Only contain one break; } return false; } /** * Load frontend public script * * @since 1.8.2 * @access public */ public function frontend_enqueue_style_public() { wp_enqueue_script(Core::PLUGIN_NAME, LSWCP_PLUGIN_URL . 'assets/js/instant_click.min.js', array(), Core::VER, true); } /** * Load frontend menu shortcut * * @since 1.3 * @access public */ public function frontend_enqueue_style() { wp_enqueue_style(Core::PLUGIN_NAME, LSWCP_PLUGIN_URL . 'assets/css/litespeed.css', array(), Core::VER, 'all'); } /** * Load frontend menu shortcut * * @since 1.3 * @access public */ public function frontend_shortcut() { global $wp_admin_bar; $wp_admin_bar->add_menu(array( 'id' => 'litespeed-menu', 'title' => '<span class="ab-icon"></span>', 'href' => get_admin_url(null, 'admin.php?page=litespeed'), 'meta' => array('tabindex' => 0, 'class' => 'litespeed-top-toolbar'), )); $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-single', 'title' => __('Purge this page', 'litespeed-cache') . ' - LSCache', 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_FRONT, false, true), 'meta' => array('tabindex' => '0'), )); if ($this->has_cache_folder('ucss')) { $possible_url_tag = UCSS::get_url_tag(); $append_arr = array(); if ($possible_url_tag) { $append_arr['url_tag'] = $possible_url_tag; } $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-single-ucss', 'title' => __('Purge this page', 'litespeed-cache') . ' - UCSS', 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_UCSS, false, true, $append_arr), 'meta' => array('tabindex' => '0'), )); } $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-single-action', 'title' => __('Mark this page as ', 'litespeed-cache'), 'meta' => array('tabindex' => '0'), )); if (!empty($_SERVER['REQUEST_URI'])) { $append_arr = array( Conf::TYPE_SET . '[' . self::O_CACHE_FORCE_URI . '][]' => $_SERVER['REQUEST_URI'] . '$', 'redirect' => $_SERVER['REQUEST_URI'], ); $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-single-action', 'id' => 'litespeed-single-forced_cache', 'title' => __('Forced cacheable', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_CONF, Conf::TYPE_SET, false, true, $append_arr), )); $append_arr = array( Conf::TYPE_SET . '[' . self::O_CACHE_EXC . '][]' => $_SERVER['REQUEST_URI'] . '$', 'redirect' => $_SERVER['REQUEST_URI'], ); $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-single-action', 'id' => 'litespeed-single-noncache', 'title' => __('Non cacheable', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_CONF, Conf::TYPE_SET, false, true, $append_arr), )); $append_arr = array( Conf::TYPE_SET . '[' . self::O_CACHE_PRIV_URI . '][]' => $_SERVER['REQUEST_URI'] . '$', 'redirect' => $_SERVER['REQUEST_URI'], ); $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-single-action', 'id' => 'litespeed-single-private', 'title' => __('Private cache', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_CONF, Conf::TYPE_SET, false, true, $append_arr), )); $append_arr = array( Conf::TYPE_SET . '[' . self::O_OPTM_EXC . '][]' => $_SERVER['REQUEST_URI'] . '$', 'redirect' => $_SERVER['REQUEST_URI'], ); $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-single-action', 'id' => 'litespeed-single-nonoptimize', 'title' => __('No optimization', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_CONF, Conf::TYPE_SET, false, true, $append_arr), )); } $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-single-action', 'id' => 'litespeed-single-more', 'title' => __('More settings', 'litespeed-cache'), 'href' => get_admin_url(null, 'admin.php?page=litespeed-cache'), )); $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-all', 'title' => __('Purge All', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL, false, '_ori'), 'meta' => array('tabindex' => '0'), )); $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-all-lscache', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('LSCache', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_LSCACHE, false, '_ori'), 'meta' => array('tabindex' => '0'), )); $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-cssjs', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('CSS/JS Cache', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_CSSJS, false, '_ori'), 'meta' => array('tabindex' => '0'), )); if ($this->conf(self::O_CDN_CLOUDFLARE)) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-cloudflare', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('Cloudflare', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_CDN_CLOUDFLARE, CDN\Cloudflare::TYPE_PURGE_ALL), 'meta' => array('tabindex' => '0'), )); } if (defined('LSCWP_OBJECT_CACHE')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-object', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('Object Cache', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_OBJECT, false, '_ori'), 'meta' => array('tabindex' => '0'), )); } if (Router::opcache_enabled()) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-opcache', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('Opcode Cache', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_OPCACHE, false, '_ori'), 'meta' => array('tabindex' => '0'), )); } if ($this->has_cache_folder('ccss')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-ccss', 'title' => __('Purge All', 'litespeed-cache') . ' - CCSS', 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_CCSS, false, '_ori'), 'meta' => array('tabindex' => '0'), )); } if ($this->has_cache_folder('ucss')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-ucss', 'title' => __('Purge All', 'litespeed-cache') . ' - UCSS', 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_UCSS, false, '_ori'), )); } if ($this->has_cache_folder('localres')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-localres', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('Localized Resources', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_LOCALRES, false, '_ori'), 'meta' => array('tabindex' => '0'), )); } if ($this->has_cache_folder('lqip')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-placeholder', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('LQIP Cache', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_LQIP, false, '_ori'), 'meta' => array('tabindex' => '0'), )); } if ($this->has_cache_folder('avatar')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-avatar', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('Gravatar Cache', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_AVATAR, false, '_ori'), 'meta' => array('tabindex' => '0'), )); } do_action('litespeed_frontend_shortcut'); } /** * Hooked to wp_before_admin_bar_render. * Adds a link to the admin bar so users can quickly purge all. * * @access public * @global WP_Admin_Bar $wp_admin_bar * @since 1.7.2 Moved from admin_display.cls to gui.cls; Renamed from `add_quick_purge` to `backend_shortcut` */ public function backend_shortcut() { global $wp_admin_bar; // if ( defined( 'LITESPEED_ON' ) ) { $wp_admin_bar->add_menu(array( 'id' => 'litespeed-menu', 'title' => '<span class="ab-icon" title="' . __('LiteSpeed Cache Purge All', 'litespeed-cache') . ' - ' . __('LSCache', 'litespeed-cache') . '"></span>', 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_LSCACHE), 'meta' => array('tabindex' => 0, 'class' => 'litespeed-top-toolbar'), )); // } // else { // $wp_admin_bar->add_menu( array( // 'id' => 'litespeed-menu', // 'title' => '<span class="ab-icon" title="' . __( 'LiteSpeed Cache', 'litespeed-cache' ) . '"></span>', // 'meta' => array( 'tabindex' => 0, 'class' => 'litespeed-top-toolbar' ), // ) ); // } $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-bar-manage', 'title' => __('Manage', 'litespeed-cache'), 'href' => 'admin.php?page=litespeed', 'meta' => array('tabindex' => '0'), )); $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-bar-setting', 'title' => __('Settings', 'litespeed-cache'), 'href' => 'admin.php?page=litespeed-cache', 'meta' => array('tabindex' => '0'), )); if (!is_network_admin()) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-bar-imgoptm', 'title' => __('Image Optimization', 'litespeed-cache'), 'href' => 'admin.php?page=litespeed-img_optm', 'meta' => array('tabindex' => '0'), )); } $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-all', 'title' => __('Purge All', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL), 'meta' => array('tabindex' => '0'), )); $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-all-lscache', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('LSCache', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_LSCACHE), 'meta' => array('tabindex' => '0'), )); $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-cssjs', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('CSS/JS Cache', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_CSSJS), 'meta' => array('tabindex' => '0'), )); if ($this->conf(self::O_CDN_CLOUDFLARE)) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-cloudflare', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('Cloudflare', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_CDN_CLOUDFLARE, CDN\Cloudflare::TYPE_PURGE_ALL), 'meta' => array('tabindex' => '0'), )); } if (defined('LSCWP_OBJECT_CACHE')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-object', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('Object Cache', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_OBJECT), 'meta' => array('tabindex' => '0'), )); } if (Router::opcache_enabled()) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-opcache', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('Opcode Cache', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_OPCACHE), 'meta' => array('tabindex' => '0'), )); } if ($this->has_cache_folder('ccss')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-ccss', 'title' => __('Purge All', 'litespeed-cache') . ' - CCSS', 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_CCSS), 'meta' => array('tabindex' => '0'), )); } if ($this->has_cache_folder('ucss')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-ucss', 'title' => __('Purge All', 'litespeed-cache') . ' - UCSS', 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_UCSS), )); } if ($this->has_cache_folder('localres')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-localres', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('Localized Resources', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_LOCALRES), 'meta' => array('tabindex' => '0'), )); } if ($this->has_cache_folder('lqip')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-placeholder', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('LQIP Cache', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_LQIP), 'meta' => array('tabindex' => '0'), )); } if ($this->has_cache_folder('avatar')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', 'id' => 'litespeed-purge-avatar', 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('Gravatar Cache', 'litespeed-cache'), 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_AVATAR), 'meta' => array('tabindex' => '0'), )); } do_action('litespeed_backend_shortcut'); } /** * Clear unfinished data * * @since 2.4.2 * @access public */ public static function img_optm_clean_up($unfinished_num) { return sprintf( '<a href="%1$s" class="button litespeed-btn-warning" data-balloon-pos="up" aria-label="%2$s"><span class="dashicons dashicons-editor-removeformatting"></span> %3$s</a>', Utility::build_url(Router::ACTION_IMG_OPTM, Img_Optm::TYPE_CLEAN), __('Remove all previous unfinished image optimization requests.', 'litespeed-cache'), __('Clean Up Unfinished Data', 'litespeed-cache') . ($unfinished_num ? ': ' . Admin_Display::print_plural($unfinished_num, 'image') : '') ); } /** * Generate install link * * @since 2.4.2 * @access public */ public static function plugin_install_link($title, $name, $v) { $url = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=' . $name), 'install-plugin_' . $name); $action = sprintf( '<a href="%1$s" class="install-now" data-slug="%2$s" data-name="%3$s" aria-label="%4$s">%5$s</a>', esc_url($url), esc_attr($name), esc_attr($title), esc_attr(sprintf(__('Install %s', 'litespeed-cache'), $title)), __('Install Now', 'litespeed-cache') ); return $action; // $msg .= " <a href='$upgrade_link' class='litespeed-btn-success' target='_blank'>" . __( 'Click here to upgrade', 'litespeed-cache' ) . '</a>'; } /** * Generate upgrade link * * @since 2.4.2 * @access public */ public static function plugin_upgrade_link($title, $name, $v) { $details_url = self_admin_url('plugin-install.php?tab=plugin-information&plugin=' . $name . '§ion=changelog&TB_iframe=true&width=600&height=800'); $file = $name . '/' . $name . '.php'; $msg = sprintf( __('<a href="%1$s" %2$s>View version %3$s details</a> or <a href="%4$s" %5$s target="_blank">update now</a>.', 'litespeed-cache'), esc_url($details_url), sprintf('class="thickbox open-plugin-details-modal" aria-label="%s"', esc_attr(sprintf(__('View %1$s version %2$s details', 'litespeed-cache'), $title, $v))), $v, wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=') . $file, 'upgrade-plugin_' . $file), sprintf('class="update-link" aria-label="%s"', esc_attr(sprintf(__('Update %s now', 'litespeed-cache'), $title))) ); return $msg; } /** * Finalize buffer by GUI class * * @since 1.6 * @access public */ public function finalize($buffer) { $buffer = $this->_clean_wrapper($buffer); // Maybe restore doc.ref if ($this->conf(Base::O_GUEST) && strpos($buffer, '<head>') !== false && defined('LITESPEED_IS_HTML')) { $buffer = $this->_enqueue_guest_docref_js($buffer); } if (defined('LITESPEED_GUEST') && LITESPEED_GUEST && strpos($buffer, '</body>') !== false && defined('LITESPEED_IS_HTML')) { $buffer = $this->_enqueue_guest_js($buffer); } return $buffer; } /** * Append guest restore doc.ref JS for organic traffic count * * @since 4.4.6 */ private function _enqueue_guest_docref_js($buffer) { $js_con = File::read(LSCWP_DIR . self::LIB_GUEST_DOCREF_JS); $buffer = preg_replace('/<head>/', '<head><script data-no-optimize="1">' . $js_con . '</script>', $buffer, 1); return $buffer; } /** * Append guest JS to update vary * * @since 4.0 */ private function _enqueue_guest_js($buffer) { $js_con = File::read(LSCWP_DIR . self::LIB_GUEST_JS); // $guest_update_url = add_query_arg( 'litespeed_guest', 1, home_url( '/' ) ); $guest_update_url = parse_url(LSWCP_PLUGIN_URL . self::PHP_GUEST, PHP_URL_PATH); $js_con = str_replace('litespeed_url', esc_url($guest_update_url), $js_con); $buffer = preg_replace('/<\/body>/', '<script data-no-optimize="1">' . $js_con . '</script></body>', $buffer, 1); return $buffer; } /** * Clean wrapper from buffer * * @since 1.4 * @since 1.6 converted to private with adding prefix _ * @access private */ private function _clean_wrapper($buffer) { if (self::$_clean_counter < 1) { Debug2::debug2('GUI bypassed by no counter'); return $buffer; } Debug2::debug2('GUI start cleaning counter ' . self::$_clean_counter); for ($i = 1; $i <= self::$_clean_counter; $i++) { // If miss beginning $start = strpos($buffer, self::clean_wrapper_begin($i)); if ($start === false) { $buffer = str_replace(self::clean_wrapper_end($i), '', $buffer); Debug2::debug2("GUI lost beginning wrapper $i"); continue; } // If miss end $end_wrapper = self::clean_wrapper_end($i); $end = strpos($buffer, $end_wrapper); if ($end === false) { $buffer = str_replace(self::clean_wrapper_begin($i), '', $buffer); Debug2::debug2("GUI lost ending wrapper $i"); continue; } // Now replace wrapped content $buffer = substr_replace($buffer, '', $start, $end - $start + strlen($end_wrapper)); Debug2::debug2("GUI cleaned wrapper $i"); } return $buffer; } /** * Display a to-be-removed html wrapper * * @since 1.4 * @access public */ public static function clean_wrapper_begin($counter = false) { if ($counter === false) { self::$_clean_counter++; $counter = self::$_clean_counter; Debug2::debug("GUI clean wrapper $counter begin"); } return '<!-- LiteSpeed To Be Removed begin ' . $counter . ' -->'; } /** * Display a to-be-removed html wrapper * * @since 1.4 * @access public */ public static function clean_wrapper_end($counter = false) { if ($counter === false) { $counter = self::$_clean_counter; Debug2::debug("GUI clean wrapper $counter end"); } return '<!-- LiteSpeed To Be Removed end ' . $counter . ' -->'; } } placeholder.cls.php 0000644 00000034130 15162226275 0010330 0 ustar 00 <?php /** * The PlaceHolder class * * @since 3.0 * @package LiteSpeed * @subpackage LiteSpeed/inc * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Placeholder extends Base { const TYPE_GENERATE = 'generate'; const TYPE_CLEAR_Q = 'clear_q'; private $_conf_placeholder_resp; private $_conf_placeholder_resp_svg; private $_conf_lqip; private $_conf_lqip_qual; private $_conf_lqip_min_w; private $_conf_lqip_min_h; private $_conf_placeholder_resp_color; private $_conf_placeholder_resp_async; private $_conf_ph_default; private $_placeholder_resp_dict = array(); private $_ph_queue = array(); protected $_summary; /** * Init * * @since 3.0 */ public function __construct() { $this->_conf_placeholder_resp = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_MEDIA_PLACEHOLDER_RESP); $this->_conf_placeholder_resp_svg = $this->conf(self::O_MEDIA_PLACEHOLDER_RESP_SVG); $this->_conf_lqip = !defined('LITESPEED_GUEST_OPTM') && $this->conf(self::O_MEDIA_LQIP); $this->_conf_lqip_qual = $this->conf(self::O_MEDIA_LQIP_QUAL); $this->_conf_lqip_min_w = $this->conf(self::O_MEDIA_LQIP_MIN_W); $this->_conf_lqip_min_h = $this->conf(self::O_MEDIA_LQIP_MIN_H); $this->_conf_placeholder_resp_async = $this->conf(self::O_MEDIA_PLACEHOLDER_RESP_ASYNC); $this->_conf_placeholder_resp_color = $this->conf(self::O_MEDIA_PLACEHOLDER_RESP_COLOR); $this->_conf_ph_default = $this->conf(self::O_MEDIA_LAZY_PLACEHOLDER) ?: LITESPEED_PLACEHOLDER; $this->_summary = self::get_summary(); } /** * Init Placeholder */ public function init() { Debug2::debug2('[LQIP] init'); add_action('litspeed_after_admin_init', array($this, 'after_admin_init')); } /** * Display column in Media * * @since 3.0 * @access public */ public function after_admin_init() { if ($this->_conf_lqip) { add_filter('manage_media_columns', array($this, 'media_row_title')); add_filter('manage_media_custom_column', array($this, 'media_row_actions'), 10, 2); add_action('litespeed_media_row_lqip', array($this, 'media_row_con')); } } /** * Media Admin Menu -> LQIP col * * @since 3.0 * @access public */ public function media_row_title($posts_columns) { $posts_columns['lqip'] = __('LQIP', 'litespeed-cache'); return $posts_columns; } /** * Media Admin Menu -> LQIP Column * * @since 3.0 * @access public */ public function media_row_actions($column_name, $post_id) { if ($column_name !== 'lqip') { return; } do_action('litespeed_media_row_lqip', $post_id); } /** * Display LQIP column * * @since 3.0 * @access public */ public function media_row_con($post_id) { $meta_value = wp_get_attachment_metadata($post_id); if (empty($meta_value['file'])) { return; } $total_files = 0; // List all sizes $all_sizes = array($meta_value['file']); $size_path = pathinfo($meta_value['file'], PATHINFO_DIRNAME) . '/'; foreach ($meta_value['sizes'] as $v) { $all_sizes[] = $size_path . $v['file']; } foreach ($all_sizes as $short_path) { $lqip_folder = LITESPEED_STATIC_DIR . '/lqip/' . $short_path; if (is_dir($lqip_folder)) { Debug2::debug('[LQIP] Found folder: ' . $short_path); // List all files foreach (scandir($lqip_folder) as $v) { if ($v == '.' || $v == '..') { continue; } if ($total_files == 0) { echo '<div class="litespeed-media-lqip"><img src="' . Str::trim_quotes(File::read($lqip_folder . '/' . $v)) . '" alt="' . sprintf(__('LQIP image preview for size %s', 'litespeed-cache'), $v) . '"></div>'; } echo '<div class="litespeed-media-size"><a href="' . Str::trim_quotes(File::read($lqip_folder . '/' . $v)) . '" target="_blank">' . $v . '</a></div>'; $total_files++; } } } if ($total_files == 0) { echo '—'; } } /** * Replace image with placeholder * * @since 3.0 * @access public */ public function replace($html, $src, $size) { // Check if need to enable responsive placeholder or not $this_placeholder = $this->_placeholder($src, $size) ?: $this->_conf_ph_default; $additional_attr = ''; if ($this->_conf_lqip && $this_placeholder != $this->_conf_ph_default) { Debug2::debug2('[LQIP] Use resp LQIP [size] ' . $size); $additional_attr = ' data-placeholder-resp="' . Str::trim_quotes($size) . '"'; } $snippet = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_NOSCRIPT_RM) ? '' : '<noscript>' . $html . '</noscript>'; $html = str_replace(array(' src=', ' srcset=', ' sizes='), array(' data-src=', ' data-srcset=', ' data-sizes='), $html); $html = str_replace('<img ', '<img data-lazyloaded="1"' . $additional_attr . ' src="' . Str::trim_quotes($this_placeholder) . '" ', $html); $snippet = $html . $snippet; return $snippet; } /** * Generate responsive placeholder * * @since 2.5.1 * @access private */ private function _placeholder($src, $size) { // Low Quality Image Placeholders if (!$size) { Debug2::debug2('[LQIP] no size ' . $src); return false; } if (!$this->_conf_placeholder_resp) { return false; } // If use local generator if (!$this->_conf_lqip || !$this->_lqip_size_check($size)) { return $this->_generate_placeholder_locally($size); } Debug2::debug2('[LQIP] Resp LQIP process [src] ' . $src . ' [size] ' . $size); $arr_key = $size . ' ' . $src; // Check if its already in dict or not if (!empty($this->_placeholder_resp_dict[$arr_key])) { Debug2::debug2('[LQIP] already in dict'); return $this->_placeholder_resp_dict[$arr_key]; } // Need to generate the responsive placeholder $placeholder_realpath = $this->_placeholder_realpath($src, $size); // todo: give offload API if (file_exists($placeholder_realpath)) { Debug2::debug2('[LQIP] file exists'); $this->_placeholder_resp_dict[$arr_key] = File::read($placeholder_realpath); return $this->_placeholder_resp_dict[$arr_key]; } // Add to cron queue // Prevent repeated requests if (in_array($arr_key, $this->_ph_queue)) { Debug2::debug2('[LQIP] file bypass generating due to in queue'); return $this->_generate_placeholder_locally($size); } if ($hit = Utility::str_hit_array($src, $this->conf(self::O_MEDIA_LQIP_EXC))) { Debug2::debug2('[LQIP] file bypass generating due to exclude setting [hit] ' . $hit); return $this->_generate_placeholder_locally($size); } $this->_ph_queue[] = $arr_key; // Send request to generate placeholder if (!$this->_conf_placeholder_resp_async) { // If requested recently, bypass if ($this->_summary && !empty($this->_summary['curr_request']) && time() - $this->_summary['curr_request'] < 300) { Debug2::debug2('[LQIP] file bypass generating due to interval limit'); return false; } // Generate immediately $this->_placeholder_resp_dict[$arr_key] = $this->_generate_placeholder($arr_key); return $this->_placeholder_resp_dict[$arr_key]; } // Prepare default svg placeholder as tmp placeholder $tmp_placeholder = $this->_generate_placeholder_locally($size); // Store it to prepare for cron $queue = $this->load_queue('lqip'); if (in_array($arr_key, $queue)) { Debug2::debug2('[LQIP] already in queue'); return $tmp_placeholder; } if (count($queue) > 500) { Debug2::debug2('[LQIP] queue is full'); return $tmp_placeholder; } $queue[] = $arr_key; $this->save_queue('lqip', $queue); Debug2::debug('[LQIP] Added placeholder queue'); return $tmp_placeholder; } /** * Generate realpath of placeholder file * * @since 2.5.1 * @access private */ private function _placeholder_realpath($src, $size) { // Use LQIP Cloud generator, each image placeholder will be separately stored // Compatibility with WebP and AVIF $src = Utility::drop_webp($src); $filepath_prefix = $this->_build_filepath_prefix('lqip'); // External images will use cache folder directly $domain = parse_url($src, PHP_URL_HOST); if ($domain && !Utility::internal($domain)) { // todo: need to improve `util:internal()` to include `CDN::internal()` $md5 = md5($src); return LITESPEED_STATIC_DIR . $filepath_prefix . 'remote/' . substr($md5, 0, 1) . '/' . substr($md5, 1, 1) . '/' . $md5 . '.' . $size; } // Drop domain $short_path = Utility::att_short_path($src); return LITESPEED_STATIC_DIR . $filepath_prefix . $short_path . '/' . $size; } /** * Cron placeholder generation * * @since 2.5.1 * @access public */ public static function cron($continue = false) { $_instance = self::cls(); $queue = $_instance->load_queue('lqip'); if (empty($queue)) { return; } // For cron, need to check request interval too if (!$continue) { if (!empty($_instance->_summary['curr_request']) && time() - $_instance->_summary['curr_request'] < 300) { Debug2::debug('[LQIP] Last request not done'); return; } } foreach ($queue as $v) { Debug2::debug('[LQIP] cron job [size] ' . $v); $res = $_instance->_generate_placeholder($v, true); // Exit queue if out of quota if ($res === 'out_of_quota') { return; } // only request first one if (!$continue) { return; } } } /** * Generate placeholder locally * * @since 3.0 * @access private */ private function _generate_placeholder_locally($size) { Debug2::debug2('[LQIP] _generate_placeholder local [size] ' . $size); $size = explode('x', $size); $svg = str_replace(array('{width}', '{height}', '{color}'), array($size[0], $size[1], $this->_conf_placeholder_resp_color), $this->_conf_placeholder_resp_svg); return 'data:image/svg+xml;base64,' . base64_encode($svg); } /** * Send to LiteSpeed API to generate placeholder * * @since 2.5.1 * @access private */ private function _generate_placeholder($raw_size_and_src, $from_cron = false) { // Parse containing size and src info $size_and_src = explode(' ', $raw_size_and_src, 2); $size = $size_and_src[0]; if (empty($size_and_src[1])) { $this->_popup_and_save($raw_size_and_src); Debug2::debug('[LQIP] ❌ No src [raw] ' . $raw_size_and_src); return $this->_generate_placeholder_locally($size); } $src = $size_and_src[1]; $file = $this->_placeholder_realpath($src, $size); // Local generate SVG to serve ( Repeatedly doing this here to remove stored cron queue in case the setting _conf_lqip is changed ) if (!$this->_conf_lqip || !$this->_lqip_size_check($size)) { $data = $this->_generate_placeholder_locally($size); } else { $err = false; $allowance = Cloud::cls()->allowance(Cloud::SVC_LQIP, $err); if (!$allowance) { Debug2::debug('[LQIP] ❌ No credit: ' . $err); $err && Admin_Display::error(Error::msg($err)); if ($from_cron) { return 'out_of_quota'; } return $this->_generate_placeholder_locally($size); } // Generate LQIP list($width, $height) = explode('x', $size); $req_data = array( 'width' => $width, 'height' => $height, 'url' => Utility::drop_webp($src), 'quality' => $this->_conf_lqip_qual, ); // CHeck if the image is 404 first if (File::is_404($req_data['url'])) { $this->_popup_and_save($raw_size_and_src, true); $this->_append_exc($src); Debug2::debug('[LQIP] 404 before request [src] ' . $req_data['url']); return $this->_generate_placeholder_locally($size); } // Update request status $this->_summary['curr_request'] = time(); self::save_summary(); $json = Cloud::post(Cloud::SVC_LQIP, $req_data, 120); if (!is_array($json)) { return $this->_generate_placeholder_locally($size); } if (empty($json['lqip']) || strpos($json['lqip'], 'data:image/svg+xml') !== 0) { // image error, pop up the current queue $this->_popup_and_save($raw_size_and_src, true); $this->_append_exc($src); Debug2::debug('[LQIP] wrong response format', $json); return $this->_generate_placeholder_locally($size); } $data = $json['lqip']; Debug2::debug('[LQIP] _generate_placeholder LQIP'); } // Write to file File::save($file, $data, true); // Save summary data $this->_summary['last_spent'] = time() - $this->_summary['curr_request']; $this->_summary['last_request'] = $this->_summary['curr_request']; $this->_summary['curr_request'] = 0; self::save_summary(); $this->_popup_and_save($raw_size_and_src); Debug2::debug('[LQIP] saved LQIP ' . $file); return $data; } /** * Check if the size is valid to send LQIP request or not * * @since 3.0 */ private function _lqip_size_check($size) { $size = explode('x', $size); if ($size[0] >= $this->_conf_lqip_min_w || $size[1] >= $this->_conf_lqip_min_h) { return true; } Debug2::debug2('[LQIP] Size too small'); return false; } /** * Add to LQIP exclude list * * @since 3.4 */ private function _append_exc($src) { $val = $this->conf(self::O_MEDIA_LQIP_EXC); $val[] = $src; $this->cls('Conf')->update(self::O_MEDIA_LQIP_EXC, $val); Debug2::debug('[LQIP] Appended to LQIP Excludes [URL] ' . $src); } /** * Pop up the current request and save * * @since 3.0 */ private function _popup_and_save($raw_size_and_src, $append_to_exc = false) { $queue = $this->load_queue('lqip'); if (!empty($queue) && in_array($raw_size_and_src, $queue)) { unset($queue[array_search($raw_size_and_src, $queue)]); } if ($append_to_exc) { $size_and_src = explode(' ', $raw_size_and_src, 2); $this_src = $size_and_src[1]; // Append to lqip exc setting first $this->_append_exc($this_src); // Check if other queues contain this src or not if ($queue) { foreach ($queue as $k => $raw_size_and_src) { $size_and_src = explode(' ', $raw_size_and_src, 2); if (empty($size_and_src[1])) { continue; } if ($size_and_src[1] == $this_src) { unset($queue[$k]); } } } } $this->save_queue('lqip', $queue); } /** * Handle all request actions from main cls * * @since 2.5.1 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_GENERATE: self::cron(true); break; case self::TYPE_CLEAR_Q: $this->clear_q('lqip'); break; default: break; } Admin::redirect(); } } router.cls.php 0000644 00000047007 15162226277 0007377 0 ustar 00 <?php /** * The core plugin router class. * * This generate the valid action. * * @since 1.1.0 * @since 1.5 Moved into /inc */ namespace LiteSpeed; defined('WPINC') || exit(); class Router extends Base { const LOG_TAG = '[Router]'; const NONCE = 'LSCWP_NONCE'; const ACTION = 'LSCWP_CTRL'; const ACTION_SAVE_SETTINGS_NETWORK = 'save-settings-network'; const ACTION_DB_OPTM = 'db_optm'; const ACTION_PLACEHOLDER = 'placeholder'; const ACTION_AVATAR = 'avatar'; const ACTION_SAVE_SETTINGS = 'save-settings'; const ACTION_CLOUD = 'cloud'; const ACTION_IMG_OPTM = 'img_optm'; const ACTION_HEALTH = 'health'; const ACTION_CRAWLER = 'crawler'; const ACTION_PURGE = 'purge'; const ACTION_CONF = 'conf'; const ACTION_ACTIVATION = 'activation'; const ACTION_CSS = 'css'; const ACTION_UCSS = 'ucss'; const ACTION_VPI = 'vpi'; const ACTION_PRESET = 'preset'; const ACTION_IMPORT = 'import'; const ACTION_REPORT = 'report'; const ACTION_DEBUG2 = 'debug2'; const ACTION_CDN_CLOUDFLARE = 'CDN\Cloudflare'; const ACTION_ADMIN_DISPLAY = 'admin_display'; // List all handlers here private static $_HANDLERS = array( self::ACTION_ADMIN_DISPLAY, self::ACTION_ACTIVATION, self::ACTION_AVATAR, self::ACTION_CDN_CLOUDFLARE, self::ACTION_CLOUD, self::ACTION_CONF, self::ACTION_CRAWLER, self::ACTION_CSS, self::ACTION_UCSS, self::ACTION_VPI, self::ACTION_DB_OPTM, self::ACTION_DEBUG2, self::ACTION_HEALTH, self::ACTION_IMG_OPTM, self::ACTION_PRESET, self::ACTION_IMPORT, self::ACTION_PLACEHOLDER, self::ACTION_PURGE, self::ACTION_REPORT, ); const TYPE = 'litespeed_type'; const ITEM_HASH = 'hash'; const ITEM_FLASH_HASH = 'flash_hash'; private static $_esi_enabled; private static $_is_ajax; private static $_is_logged_in; private static $_ip; private static $_action; private static $_is_admin_ip; private static $_frontend_path; /** * Redirect to self to continue operation * * Note: must return when use this func. CLI/Cron call won't die in this func. * * @since 3.0 * @access public */ public static function self_redirect($action, $type) { if (defined('LITESPEED_CLI') || defined('DOING_CRON')) { Admin_Display::success('To be continued'); // Show for CLI return; } // Add i to avoid browser too many redirected warning $i = !empty($_GET['litespeed_i']) ? $_GET['litespeed_i'] : 0; $i++; $link = Utility::build_url($action, $type, false, null, array('litespeed_i' => $i)); $url = html_entity_decode($link); exit("<meta http-equiv='refresh' content='0;url=$url'>"); } /** * Check if can run optimize * * @since 1.3 * @since 2.3.1 Relocated from cdn.cls * @access public */ public function can_optm() { $can = true; if (is_user_logged_in() && $this->conf(self::O_OPTM_GUEST_ONLY)) { $can = false; } elseif (is_admin()) { $can = false; } elseif (is_feed()) { $can = false; } elseif (is_preview()) { $can = false; } elseif (self::is_ajax()) { $can = false; } if (self::_is_login_page()) { Debug2::debug('[Router] Optm bypassed: login/reg page'); $can = false; } $can_final = apply_filters('litespeed_can_optm', $can); if ($can_final != $can) { Debug2::debug('[Router] Optm bypassed: filter'); } return $can_final; } /** * Check referer page to see if its from admin * * @since 2.4.2.1 * @access public */ public static function from_admin() { return !empty($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], get_admin_url()) === 0; } /** * Check if it can use CDN replacement * * @since 1.2.3 * @since 2.3.1 Relocated from cdn.cls * @access public */ public static function can_cdn() { $can = true; if (is_admin()) { if (!self::is_ajax()) { Debug2::debug2('[Router] CDN bypassed: is not ajax call'); $can = false; } if (self::from_admin()) { Debug2::debug2('[Router] CDN bypassed: ajax call from admin'); $can = false; } } elseif (is_feed()) { $can = false; } elseif (is_preview()) { $can = false; } /** * Bypass cron to avoid deregister jq notice `Do not deregister the <code>jquery-core</code> script in the administration area.` * @since 2.7.2 */ if (defined('DOING_CRON')) { $can = false; } /** * Bypass login/reg page * @since 1.6 */ if (self::_is_login_page()) { Debug2::debug('[Router] CDN bypassed: login/reg page'); $can = false; } /** * Bypass post/page link setting * @since 2.9.8.5 */ $rest_prefix = function_exists('rest_get_url_prefix') ? rest_get_url_prefix() : apply_filters('rest_url_prefix', 'wp-json'); if ( !empty($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], $rest_prefix . '/wp/v2/media') !== false && isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'wp-admin') !== false ) { Debug2::debug('[Router] CDN bypassed: wp-json on admin page'); $can = false; } $can_final = apply_filters('litespeed_can_cdn', $can); if ($can_final != $can) { Debug2::debug('[Router] CDN bypassed: filter'); } return $can_final; } /** * Check if is login page or not * * @since 2.3.1 * @access protected */ protected static function _is_login_page() { if (in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'), true)) { return true; } return false; } /** * UCSS/Crawler role simulator * * @since 1.9.1 * @since 3.3 Renamed from `is_crawler_role_simulation` */ public function is_role_simulation() { if (is_admin()) { return; } if (empty($_COOKIE['litespeed_hash']) && empty($_COOKIE['litespeed_flash_hash'])) { return; } self::debug('🪪 starting role validation'); // Check if is from crawler // if ( empty( $_SERVER[ 'HTTP_USER_AGENT' ] ) || strpos( $_SERVER[ 'HTTP_USER_AGENT' ], Crawler::FAST_USER_AGENT ) !== 0 ) { // Debug2::debug( '[Router] user agent not match' ); // return; // } $server_ip = $this->conf(self::O_SERVER_IP); if (!$server_ip || self::get_ip() !== $server_ip) { self::debug('❌❌ Role simulate uid denied! Not localhost visit!'); Control::set_nocache('Role simulate uid denied'); return; } // Flash hash validation if (!empty($_COOKIE['litespeed_flash_hash'])) { $hash_data = self::get_option(self::ITEM_FLASH_HASH, array()); if ($hash_data && is_array($hash_data) && !empty($hash_data['hash']) && !empty($hash_data['ts']) && !empty($hash_data['uid'])) { if (time() - $hash_data['ts'] < 120 && $_COOKIE['litespeed_flash_hash'] == $hash_data['hash']) { self::debug('🪪 Role simulator flash hash matched, escalating user to be uid=' . $hash_data['uid']); self::delete_option(self::ITEM_FLASH_HASH); wp_set_current_user($hash_data['uid']); return; } } } // Hash validation if (!empty($_COOKIE['litespeed_hash'])) { $hash_data = self::get_option(self::ITEM_HASH, array()); if ($hash_data && is_array($hash_data) && !empty($hash_data['hash']) && !empty($hash_data['ts']) && !empty($hash_data['uid'])) { $RUN_DURATION = $this->cls('Crawler')->get_crawler_duration(); if (time() - $hash_data['ts'] < $RUN_DURATION && $_COOKIE['litespeed_hash'] == $hash_data['hash']) { self::debug('🪪 Role simulator hash matched, escalating user to be uid=' . $hash_data['uid']); wp_set_current_user($hash_data['uid']); return; } } } self::debug('❌ WARNING: role simulator hash not match'); } /** * Get a short ttl hash (2mins) * * @since 6.4 */ public function get_flash_hash($uid) { $hash_data = self::get_option(self::ITEM_FLASH_HASH, array()); if ($hash_data && is_array($hash_data) && !empty($hash_data['hash']) && !empty($hash_data['ts'])) { if (time() - $hash_data['ts'] < 60) { return $hash_data['hash']; } } // Check if this user has editor access or not if (user_can($uid, 'edit_posts')) { self::debug('🛑 The user with id ' . $uid . ' has editor access, which is not allowed for the role simulator.'); return ''; } $hash = Str::rrand(32); self::update_option(self::ITEM_FLASH_HASH, array('hash' => $hash, 'ts' => time(), 'uid' => $uid)); return $hash; } /** * Get a security hash * * @since 3.3 */ public function get_hash($uid) { // Check if this user has editor access or not if (user_can($uid, 'edit_posts')) { self::debug('🛑 The user with id ' . $uid . ' has editor access, which is not allowed for the role simulator.'); return ''; } // As this is called only when starting crawling, not per page, no need to reuse $hash = Str::rrand(32); self::update_option(self::ITEM_HASH, array('hash' => $hash, 'ts' => time(), 'uid' => $uid)); return $hash; } /** * Get user role * * @since 1.6.2 */ public static function get_role($uid = null) { if (defined('LITESPEED_WP_ROLE')) { return LITESPEED_WP_ROLE; } if ($uid === null) { $uid = get_current_user_id(); } $role = false; if ($uid) { $user = get_userdata($uid); if (isset($user->roles) && is_array($user->roles)) { $tmp = array_values($user->roles); $role = implode(',', $tmp); // Combine for PHP5.3 const comaptibility } } Debug2::debug('[Router] get_role: ' . $role); if (!$role) { return $role; // Guest user Debug2::debug('[Router] role: guest'); /** * Fix double login issue * The previous user init refactoring didn't fix this bcos this is in login process and the user role could change * @see https://github.com/litespeedtech/lscache_wp/commit/69e7bc71d0de5cd58961bae953380b581abdc088 * @since 2.9.8 Won't assign const if in login process */ if (substr_compare(wp_login_url(), $GLOBALS['pagenow'], -strlen($GLOBALS['pagenow'])) === 0) { return $role; } } define('LITESPEED_WP_ROLE', $role); return LITESPEED_WP_ROLE; } /** * Get frontend path * * @since 1.2.2 * @access public * @return boolean */ public static function frontend_path() { //todo: move to htaccess.cls ? if (!isset(self::$_frontend_path)) { $frontend = rtrim(ABSPATH, '/'); // /home/user/public_html/frontend // get home path failed. Trac ticket #37668 (e.g. frontend:/blog backend:/wordpress) if (!$frontend) { Debug2::debug('[Router] No ABSPATH, generating from home option'); $frontend = parse_url(get_option('home')); $frontend = !empty($frontend['path']) ? $frontend['path'] : ''; $frontend = $_SERVER['DOCUMENT_ROOT'] . $frontend; } $frontend = realpath($frontend); self::$_frontend_path = $frontend; } return self::$_frontend_path; } /** * Check if ESI is enabled or not * * @since 1.2.0 * @access public * @return boolean */ public function esi_enabled() { if (!isset(self::$_esi_enabled)) { self::$_esi_enabled = defined('LITESPEED_ON') && $this->conf(self::O_ESI); if (!empty($_REQUEST[self::ACTION])) { self::$_esi_enabled = false; } } return self::$_esi_enabled; } /** * Check if crawler is enabled on server level * * @since 1.1.1 * @access public */ public static function can_crawl() { if (isset($_SERVER['X-LSCACHE']) && strpos($_SERVER['X-LSCACHE'], 'crawler') === false) { return false; } // CLI will bypass this check as crawler library can always do the 428 check if (defined('LITESPEED_CLI')) { return true; } return true; } /** * Check action * * @since 1.1.0 * @access public * @return string */ public static function get_action() { if (!isset(self::$_action)) { self::$_action = false; self::cls()->verify_action(); if (self::$_action) { defined('LSCWP_LOG') && Debug2::debug('[Router] LSCWP_CTRL verified: ' . var_export(self::$_action, true)); } } return self::$_action; } /** * Check if is logged in * * @since 1.1.3 * @access public * @return boolean */ public static function is_logged_in() { if (!isset(self::$_is_logged_in)) { self::$_is_logged_in = is_user_logged_in(); } return self::$_is_logged_in; } /** * Check if is ajax call * * @since 1.1.0 * @access public * @return boolean */ public static function is_ajax() { if (!isset(self::$_is_ajax)) { self::$_is_ajax = defined('DOING_AJAX') && DOING_AJAX; } return self::$_is_ajax; } /** * Check if is admin ip * * @since 1.1.0 * @access public * @return boolean */ public function is_admin_ip() { if (!isset(self::$_is_admin_ip)) { $ips = $this->conf(self::O_DEBUG_IPS); self::$_is_admin_ip = $this->ip_access($ips); } return self::$_is_admin_ip; } /** * Get type value * * @since 1.6 * @access public */ public static function verify_type() { if (empty($_REQUEST[self::TYPE])) { Debug2::debug('[Router] no type', 2); return false; } Debug2::debug('[Router] parsed type: ' . $_REQUEST[self::TYPE], 2); return $_REQUEST[self::TYPE]; } /** * Check privilege and nonce for the action * * @since 1.1.0 * @access private */ private function verify_action() { if (empty($_REQUEST[Router::ACTION])) { Debug2::debug2('[Router] LSCWP_CTRL bypassed empty'); return; } $action = stripslashes($_REQUEST[Router::ACTION]); if (!$action) { return; } $_is_public_action = false; // Each action must have a valid nonce unless its from admin ip and is public action // Validate requests nonce (from admin logged in page or cli) if (!$this->verify_nonce($action)) { // check if it is from admin ip if (!$this->is_admin_ip()) { Debug2::debug('[Router] LSCWP_CTRL query string - did not match admin IP: ' . $action); return; } // check if it is public action if ( !in_array($action, array( Core::ACTION_QS_NOCACHE, Core::ACTION_QS_PURGE, Core::ACTION_QS_PURGE_SINGLE, Core::ACTION_QS_SHOW_HEADERS, Core::ACTION_QS_PURGE_ALL, Core::ACTION_QS_PURGE_EMPTYCACHE, )) ) { Debug2::debug('[Router] LSCWP_CTRL query string - did not match admin IP Actions: ' . $action); return; } if (apply_filters('litespeed_qs_forbidden', false)) { Debug2::debug('[Router] LSCWP_CTRL forbidden by hook litespeed_qs_forbidden'); return; } $_is_public_action = true; } /* Now it is a valid action, lets log and check the permission */ Debug2::debug('[Router] LSCWP_CTRL: ' . $action); // OK, as we want to do something magic, lets check if its allowed $_is_multisite = is_multisite(); $_is_network_admin = $_is_multisite && is_network_admin(); $_can_network_option = $_is_network_admin && current_user_can('manage_network_options'); $_can_option = current_user_can('manage_options'); switch ($action) { case self::ACTION_SAVE_SETTINGS_NETWORK: // Save network settings if ($_can_network_option) { self::$_action = $action; } return; case Core::ACTION_PURGE_BY: if (defined('LITESPEED_ON') && ($_can_network_option || $_can_option || self::is_ajax())) { //here may need more security self::$_action = $action; } return; case self::ACTION_DB_OPTM: if ($_can_network_option || $_can_option) { self::$_action = $action; } return; case Core::ACTION_PURGE_EMPTYCACHE: // todo: moved to purge.cls type action if ((defined('LITESPEED_ON') || $_is_network_admin) && ($_can_network_option || (!$_is_multisite && $_can_option))) { self::$_action = $action; } return; case Core::ACTION_QS_NOCACHE: case Core::ACTION_QS_PURGE: case Core::ACTION_QS_PURGE_SINGLE: case Core::ACTION_QS_SHOW_HEADERS: case Core::ACTION_QS_PURGE_ALL: case Core::ACTION_QS_PURGE_EMPTYCACHE: if (defined('LITESPEED_ON') && ($_is_public_action || self::is_ajax())) { self::$_action = $action; } return; case self::ACTION_ADMIN_DISPLAY: case self::ACTION_PLACEHOLDER: case self::ACTION_AVATAR: case self::ACTION_IMG_OPTM: case self::ACTION_CLOUD: case self::ACTION_CDN_CLOUDFLARE: case self::ACTION_CRAWLER: case self::ACTION_PRESET: case self::ACTION_IMPORT: case self::ACTION_REPORT: case self::ACTION_CSS: case self::ACTION_UCSS: case self::ACTION_VPI: case self::ACTION_CONF: case self::ACTION_ACTIVATION: case self::ACTION_HEALTH: case self::ACTION_SAVE_SETTINGS: // Save settings if ($_can_option && !$_is_network_admin) { self::$_action = $action; } return; case self::ACTION_PURGE: case self::ACTION_DEBUG2: if ($_can_network_option || $_can_option) { self::$_action = $action; } return; case Core::ACTION_DISMISS: /** * Non ajax call can dismiss too * @since 2.9 */ // if ( self::is_ajax() ) { self::$_action = $action; // } return; default: Debug2::debug('[Router] LSCWP_CTRL match failed: ' . $action); return; } } /** * Verify nonce * * @since 1.1.0 * @access public * @param string $action * @return bool */ public function verify_nonce($action) { if (!isset($_REQUEST[Router::NONCE]) || !wp_verify_nonce($_REQUEST[Router::NONCE], $action)) { return false; } else { return true; } } /** * Check if the ip is in the range * * @since 1.1.0 * @access public */ public function ip_access($ip_list) { if (!$ip_list) { return false; } if (!isset(self::$_ip)) { self::$_ip = self::get_ip(); } if (!self::$_ip) { return false; } // $uip = explode('.', $_ip); // if(empty($uip) || count($uip) != 4) Return false; // foreach($ip_list as $key => $ip) $ip_list[$key] = explode('.', trim($ip)); // foreach($ip_list as $key => $ip) { // if(count($ip) != 4) continue; // for($i = 0; $i <= 3; $i++) if($ip[$i] == '*') $ip_list[$key][$i] = $uip[$i]; // } return in_array(self::$_ip, $ip_list); } /** * Get client ip * * @since 1.1.0 * @since 1.6.5 changed to public * @access public * @return string */ public static function get_ip() { $_ip = ''; // if ( function_exists( 'apache_request_headers' ) ) { // $apache_headers = apache_request_headers(); // $_ip = ! empty( $apache_headers['True-Client-IP'] ) ? $apache_headers['True-Client-IP'] : false; // if ( ! $_ip ) { // $_ip = ! empty( $apache_headers['X-Forwarded-For'] ) ? $apache_headers['X-Forwarded-For'] : false; // $_ip = explode( ',', $_ip ); // $_ip = $_ip[ 0 ]; // } // } if (!$_ip) { $_ip = !empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false; } return $_ip; } /** * Check if opcode cache is enabled * * @since 1.8.2 * @access public */ public static function opcache_enabled() { return function_exists('opcache_reset') && ini_get('opcache.enable'); } /** * Handle static files * * @since 3.0 */ public function serve_static() { if (!empty($_SERVER['SCRIPT_URI'])) { if (strpos($_SERVER['SCRIPT_URI'], LITESPEED_STATIC_URL . '/') !== 0) { return; } $path = substr($_SERVER['SCRIPT_URI'], strlen(LITESPEED_STATIC_URL . '/')); } elseif (!empty($_SERVER['REQUEST_URI'])) { $static_path = parse_url(LITESPEED_STATIC_URL, PHP_URL_PATH) . '/'; if (strpos($_SERVER['REQUEST_URI'], $static_path) !== 0) { return; } $path = substr(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), strlen($static_path)); } else { return; } $path = explode('/', $path, 2); if (empty($path[0]) || empty($path[1])) { return; } switch ($path[0]) { case 'avatar': $this->cls('Avatar')->serve_static($path[1]); break; case 'localres': $this->cls('Localization')->serve_static($path[1]); break; default: break; } } /** * Handle all request actions from main cls * * This is different than other handlers * * @since 3.0 * @access public */ public function handler($cls) { if (!in_array($cls, self::$_HANDLERS)) { return; } return $this->cls($cls)->handler(); } } ucss.cls.php 0000644 00000034261 15162226301 0007016 0 ustar 00 <?php /** * The ucss class. * * @since 5.1 */ namespace LiteSpeed; defined('WPINC') || exit(); class UCSS extends Base { const LOG_TAG = '[UCSS]'; const TYPE_GEN = 'gen'; const TYPE_CLEAR_Q = 'clear_q'; protected $_summary; private $_ucss_whitelist; private $_queue; /** * Init * * @since 3.0 */ public function __construct() { $this->_summary = self::get_summary(); add_filter('litespeed_ucss_whitelist', array($this->cls('Data'), 'load_ucss_whitelist')); } /** * Uniform url tag for ucss usage * @since 4.7 */ public static function get_url_tag($request_url = false) { $url_tag = $request_url; if (is_404()) { $url_tag = '404'; } elseif (apply_filters('litespeed_ucss_per_pagetype', false)) { $url_tag = Utility::page_type(); self::debug('litespeed_ucss_per_pagetype filter altered url to ' . $url_tag); } return $url_tag; } /** * Get UCSS path * * @since 4.0 */ public function load($request_url, $dry_run = false) { // Check UCSS URI excludes $ucss_exc = apply_filters('litespeed_ucss_exc', $this->conf(self::O_OPTM_UCSS_EXC)); if ($ucss_exc && ($hit = Utility::str_hit_array($request_url, $ucss_exc))) { self::debug('UCSS bypassed due to UCSS URI Exclude setting: ' . $hit); Core::comment('QUIC.cloud UCSS bypassed by setting'); return false; } $filepath_prefix = $this->_build_filepath_prefix('ucss'); $url_tag = self::get_url_tag($request_url); $vary = $this->cls('Vary')->finalize_full_varies(); $filename = $this->cls('Data')->load_url_file($url_tag, $vary, 'ucss'); if ($filename) { $static_file = LITESPEED_STATIC_DIR . $filepath_prefix . $filename . '.css'; if (file_exists($static_file)) { self::debug2('existing ucss ' . $static_file); // Check if is error comment inside only $tmp = File::read($static_file); if (substr($tmp, 0, 2) == '/*' && substr(trim($tmp), -2) == '*/') { self::debug2('existing ucss is error only: ' . $tmp); Core::comment('QUIC.cloud UCSS bypassed due to generation error ❌ ' . $filepath_prefix . $filename . '.css'); return false; } Core::comment('QUIC.cloud UCSS loaded ✅'); return $filename . '.css'; } } if ($dry_run) { return false; } Core::comment('QUIC.cloud UCSS in queue'); $uid = get_current_user_id(); $ua = $this->_get_ua(); // Store it for cron $this->_queue = $this->load_queue('ucss'); if (count($this->_queue) > 500) { self::debug('UCSS Queue is full - 500'); return false; } $queue_k = (strlen($vary) > 32 ? md5($vary) : $vary) . ' ' . $url_tag; $this->_queue[$queue_k] = array( 'url' => apply_filters('litespeed_ucss_url', $request_url), 'user_agent' => substr($ua, 0, 200), 'is_mobile' => $this->_separate_mobile(), 'is_webp' => $this->cls('Media')->webp_support() ? 1 : 0, 'uid' => $uid, 'vary' => $vary, 'url_tag' => $url_tag, ); // Current UA will be used to request $this->save_queue('ucss', $this->_queue); self::debug('Added queue_ucss [url_tag] ' . $url_tag . ' [UA] ' . $ua . ' [vary] ' . $vary . ' [uid] ' . $uid); // Prepare cache tag for later purge Tag::add('UCSS.' . md5($queue_k)); return false; } /** * Get User Agent * * @since 5.3 */ private function _get_ua() { return !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; } /** * Add rows to q * * @since 5.3 */ public function add_to_q($url_files) { // Store it for cron $this->_queue = $this->load_queue('ucss'); if (count($this->_queue) > 500) { self::debug('UCSS Queue is full - 500'); return false; } $ua = $this->_get_ua(); foreach ($url_files as $url_file) { $vary = $url_file['vary']; $request_url = $url_file['url']; $is_mobile = $url_file['mobile']; $is_webp = $url_file['webp']; $url_tag = self::get_url_tag($request_url); $queue_k = (strlen($vary) > 32 ? md5($vary) : $vary) . ' ' . $url_tag; $q = array( 'url' => apply_filters('litespeed_ucss_url', $request_url), 'user_agent' => substr($ua, 0, 200), 'is_mobile' => $is_mobile, 'is_webp' => $is_webp, 'uid' => false, 'vary' => $vary, 'url_tag' => $url_tag, ); // Current UA will be used to request self::debug('Added queue_ucss [url_tag] ' . $url_tag . ' [UA] ' . $ua . ' [vary] ' . $vary . ' [uid] false'); $this->_queue[$queue_k] = $q; } $this->save_queue('ucss', $this->_queue); } /** * Generate UCSS * * @since 4.0 */ public static function cron($continue = false) { $_instance = self::cls(); return $_instance->_cron_handler($continue); } /** * Handle UCSS cron * * @since 4.2 */ private function _cron_handler($continue) { $this->_queue = $this->load_queue('ucss'); if (empty($this->_queue)) { return; } // For cron, need to check request interval too if (!$continue) { if (!empty($this->_summary['curr_request']) && time() - $this->_summary['curr_request'] < 300 && !$this->conf(self::O_DEBUG)) { self::debug('Last request not done'); return; } } $i = 0; foreach ($this->_queue as $k => $v) { if (!empty($v['_status'])) { continue; } self::debug('cron job [tag] ' . $k . ' [url] ' . $v['url'] . ($v['is_mobile'] ? ' 📱 ' : '') . ' [UA] ' . $v['user_agent']); if (!isset($v['is_webp'])) { $v['is_webp'] = false; } $i++; $res = $this->_send_req($v['url'], $k, $v['uid'], $v['user_agent'], $v['vary'], $v['url_tag'], $v['is_mobile'], $v['is_webp']); if (!$res) { // Status is wrong, drop this this->_queue $this->_queue = $this->load_queue('ucss'); unset($this->_queue[$k]); $this->save_queue('ucss', $this->_queue); if (!$continue) { return; } if ($i > 3) { GUI::print_loading(count($this->_queue), 'UCSS'); return Router::self_redirect(Router::ACTION_UCSS, self::TYPE_GEN); } continue; } // Exit queue if out of quota or service is hot if ($res === 'out_of_quota' || $res === 'svc_hot') { return; } $this->_queue = $this->load_queue('ucss'); $this->_queue[$k]['_status'] = 'requested'; $this->save_queue('ucss', $this->_queue); self::debug('Saved to queue [k] ' . $k); // only request first one if (!$continue) { return; } if ($i > 3) { GUI::print_loading(count($this->_queue), 'UCSS'); return Router::self_redirect(Router::ACTION_UCSS, self::TYPE_GEN); } } } /** * Send to QC API to generate UCSS * * @since 2.3 * @access private */ private function _send_req($request_url, $queue_k, $uid, $user_agent, $vary, $url_tag, $is_mobile, $is_webp) { // Check if has credit to push or not $err = false; $allowance = $this->cls('Cloud')->allowance(Cloud::SVC_UCSS, $err); if (!$allowance) { self::debug('❌ No credit: ' . $err); $err && Admin_Display::error(Error::msg($err)); return 'out_of_quota'; } set_time_limit(120); // Update css request status $this->_summary['curr_request'] = time(); self::save_summary(); // Gather guest HTML to send $html = $this->cls('CSS')->prepare_html($request_url, $user_agent, $uid); if (!$html) { return false; } // Parse HTML to gather all CSS content before requesting $css = false; list(, $html) = $this->prepare_css($html, $is_webp, true); // Use this to drop CSS from HTML as we don't need those CSS to generate UCSS $filename = $this->cls('Data')->load_url_file($url_tag, $vary, 'css'); $filepath_prefix = $this->_build_filepath_prefix('css'); $static_file = LITESPEED_STATIC_DIR . $filepath_prefix . $filename . '.css'; self::debug('Checking combined file ' . $static_file); if (file_exists($static_file)) { $css = File::read($static_file); } if (!$css) { self::debug('❌ No combined css'); return false; } $data = array( 'url' => $request_url, 'queue_k' => $queue_k, 'user_agent' => $user_agent, 'is_mobile' => $is_mobile ? 1 : 0, // todo:compatible w/ tablet 'is_webp' => $is_webp ? 1 : 0, 'html' => $html, 'css' => $css, ); if (!isset($this->_ucss_whitelist)) { $this->_ucss_whitelist = $this->_filter_whitelist(); } $data['whitelist'] = $this->_ucss_whitelist; self::debug('Generating: ', $data); $json = Cloud::post(Cloud::SVC_UCSS, $data, 30); if (!is_array($json)) { return $json; } // Old version compatibility if (empty($json['status'])) { if (!empty($json['ucss'])) { $this->_save_con('ucss', $json['ucss'], $queue_k, $is_mobile, $is_webp); } // Delete the row return false; } // Unknown status, remove this line if ($json['status'] != 'queued') { return false; } // Save summary data $this->_summary['last_spent'] = time() - $this->_summary['curr_request']; $this->_summary['last_request'] = $this->_summary['curr_request']; $this->_summary['curr_request'] = 0; self::save_summary(); return true; } /** * Save UCSS content * * @since 4.2 */ private function _save_con($type, $css, $queue_k, $is_mobile, $is_webp) { // Add filters $css = apply_filters('litespeed_' . $type, $css, $queue_k); self::debug2('con: ', $css); if (substr($css, 0, 2) == '/*' && substr($css, -2) == '*/') { self::debug('❌ empty ' . $type . ' [content] ' . $css); // continue; // Save the error info too } // Write to file $filecon_md5 = md5($css); $filepath_prefix = $this->_build_filepath_prefix($type); $static_file = LITESPEED_STATIC_DIR . $filepath_prefix . $filecon_md5 . '.css'; File::save($static_file, $css, true); $url_tag = $this->_queue[$queue_k]['url_tag']; $vary = $this->_queue[$queue_k]['vary']; self::debug2("Save URL to file [file] $static_file [vary] $vary"); $this->cls('Data')->save_url($url_tag, $vary, $type, $filecon_md5, dirname($static_file), $is_mobile, $is_webp); Purge::add(strtoupper($type) . '.' . md5($queue_k)); } /** * Prepare CSS from HTML for CCSS generation only. UCSS will used combined CSS directly. * Prepare refined HTML for both CCSS and UCSS. * * @since 3.4.3 */ public function prepare_css($html, $is_webp = false, $dryrun = false) { $css = ''; preg_match_all('#<link ([^>]+)/?>|<style([^>]*)>([^<]+)</style>#isU', $html, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $debug_info = ''; if (strpos($match[0], '<link') === 0) { $attrs = Utility::parse_attr($match[1]); if (empty($attrs['rel'])) { continue; } if ($attrs['rel'] != 'stylesheet') { if ($attrs['rel'] != 'preload' || empty($attrs['as']) || $attrs['as'] != 'style') { continue; } } if (!empty($attrs['media']) && strpos($attrs['media'], 'print') !== false) { continue; } if (empty($attrs['href'])) { continue; } // Check Google fonts hit if (strpos($attrs['href'], 'fonts.googleapis.com') !== false) { $html = str_replace($match[0], '', $html); continue; } $debug_info = $attrs['href']; // Load CSS content if (!$dryrun) { // Dryrun will not load CSS but just drop them $con = $this->cls('Optimizer')->load_file($attrs['href']); if (!$con) { continue; } } else { $con = ''; } } else { // Inline style $attrs = Utility::parse_attr($match[2]); if (!empty($attrs['media']) && strpos($attrs['media'], 'print') !== false) { continue; } Debug2::debug2('[CSS] Load inline CSS ' . substr($match[3], 0, 100) . '...', $attrs); $con = $match[3]; $debug_info = '__INLINE__'; } $con = Optimizer::minify_css($con); if ($is_webp && $this->cls('Media')->webp_support()) { $con = $this->cls('Media')->replace_background_webp($con); } if (!empty($attrs['media']) && $attrs['media'] !== 'all') { $con = '@media ' . $attrs['media'] . '{' . $con . "}\n"; } else { $con = $con . "\n"; } $con = '/* ' . $debug_info . ' */' . $con; $css .= $con; $html = str_replace($match[0], '', $html); } return array($css, $html); } /** * Filter the comment content, add quotes to selector from whitelist. Return the json * * @since 3.3 */ private function _filter_whitelist() { $whitelist = array(); $list = apply_filters('litespeed_ucss_whitelist', $this->conf(self::O_OPTM_UCSS_SELECTOR_WHITELIST)); foreach ($list as $k => $v) { if (substr($v, 0, 2) === '//') { continue; } // Wrap in quotes for selectors if (substr($v, 0, 1) !== '/' && strpos($v, '"') === false && strpos($v, "'") === false) { // $v = "'$v'"; } $whitelist[] = $v; } return $whitelist; } /** * Notify finished from server * @since 5.1 */ public function notify() { $post_data = \json_decode(file_get_contents('php://input'), true); if (is_null($post_data)) { $post_data = $_POST; } self::debug('notify() data', $post_data); $this->_queue = $this->load_queue('ucss'); list($post_data) = $this->cls('Cloud')->extract_msg($post_data, 'ucss'); $notified_data = $post_data['data']; if (empty($notified_data) || !is_array($notified_data)) { self::debug('❌ notify exit: no notified data'); return Cloud::err('no notified data'); } // Check if its in queue or not $valid_i = 0; foreach ($notified_data as $v) { if (empty($v['request_url'])) { self::debug('❌ notify bypass: no request_url', $v); continue; } if (empty($v['queue_k'])) { self::debug('❌ notify bypass: no queue_k', $v); continue; } if (empty($this->_queue[$v['queue_k']])) { self::debug('❌ notify bypass: no this queue [q_k]' . $v['queue_k']); continue; } // Save data if (!empty($v['data_ucss'])) { $is_mobile = $this->_queue[$v['queue_k']]['is_mobile']; $is_webp = $this->_queue[$v['queue_k']]['is_webp']; $this->_save_con('ucss', $v['data_ucss'], $v['queue_k'], $is_mobile, $is_webp); $valid_i++; } unset($this->_queue[$v['queue_k']]); self::debug('notify data handled, unset queue [q_k] ' . $v['queue_k']); } $this->save_queue('ucss', $this->_queue); self::debug('notified'); return Cloud::ok(array('count' => $valid_i)); } /** * Handle all request actions from main cls * * @since 2.3 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_GEN: self::cron(true); break; case self::TYPE_CLEAR_Q: $this->clear_q('ucss'); break; default: break; } Admin::redirect(); } } localization.cls.php 0000644 00000006617 15162226302 0010536 0 ustar 00 <?php /** * The localization class. * * @since 3.3 */ namespace LiteSpeed; defined('WPINC') || exit(); class Localization extends Base { const LOG_TAG = '🛍️'; /** * Init optimizer * * @since 3.0 * @access protected */ public function init() { add_filter('litespeed_buffer_finalize', array($this, 'finalize'), 23); // After page optm } /** * Localize Resources * * @since 3.3 */ public function serve_static($uri) { $url = base64_decode($uri); if (!$this->conf(self::O_OPTM_LOCALIZE)) { // wp_redirect( $url ); exit('Not supported'); } if (substr($url, -3) !== '.js') { // wp_redirect( $url ); // exit( 'Not supported ' . $uri ); } $match = false; $domains = $this->conf(self::O_OPTM_LOCALIZE_DOMAINS); foreach ($domains as $v) { if (!$v || strpos($v, '#') === 0) { continue; } $type = 'js'; $domain = $v; // Try to parse space split value if (strpos($v, ' ')) { $v = explode(' ', $v); if (!empty($v[1])) { $type = strtolower($v[0]); $domain = $v[1]; } } if (strpos($domain, 'https://') !== 0) { continue; } if ($type != 'js') { continue; } // if ( strpos( $url, $domain ) !== 0 ) { if ($url != $domain) { continue; } $match = true; break; } if (!$match) { // wp_redirect( $url ); exit('Not supported2'); } header('Content-Type: application/javascript'); // Generate $this->_maybe_mk_cache_folder('localres'); $file = $this->_realpath($url); self::debug('localize [url] ' . $url); $response = wp_safe_remote_get($url, array('timeout' => 180, 'stream' => true, 'filename' => $file)); // Parse response data if (is_wp_error($response)) { $error_message = $response->get_error_message(); file_exists($file) && unlink($file); self::debug('failed to get: ' . $error_message); wp_redirect($url); exit(); } $url = $this->_rewrite($url); wp_redirect($url); exit(); } /** * Get the final URL of local avatar * * @since 4.5 */ private function _rewrite($url) { return LITESPEED_STATIC_URL . '/localres/' . $this->_filepath($url); } /** * Generate realpath of the cache file * * @since 4.5 * @access private */ private function _realpath($url) { return LITESPEED_STATIC_DIR . '/localres/' . $this->_filepath($url); } /** * Get filepath * * @since 4.5 */ private function _filepath($url) { $filename = md5($url) . '.js'; if (is_multisite()) { $filename = get_current_blog_id() . '/' . $filename; } return $filename; } /** * Localize JS/Fonts * * @since 3.3 * @access public */ public function finalize($content) { if (is_admin()) { return $content; } if (!$this->conf(self::O_OPTM_LOCALIZE)) { return $content; } $domains = $this->conf(self::O_OPTM_LOCALIZE_DOMAINS); if (!$domains) { return $content; } foreach ($domains as $v) { if (!$v || strpos($v, '#') === 0) { continue; } $type = 'js'; $domain = $v; // Try to parse space split value if (strpos($v, ' ')) { $v = explode(' ', $v); if (!empty($v[1])) { $type = strtolower($v[0]); $domain = $v[1]; } } if (strpos($domain, 'https://') !== 0) { continue; } if ($type != 'js') { continue; } $content = str_replace($domain, LITESPEED_STATIC_URL . '/localres/' . base64_encode($domain), $content); } return $content; } } str.cls.php 0000644 00000004575 15162226303 0006660 0 ustar 00 <?php /** * LiteSpeed String Operator Library Class * * @since 1.3 */ namespace LiteSpeed; defined('WPINC') || exit(); class Str { /** * Translate QC HTML links from html. Convert `<a href="{#xxx#}">xxxx</a>` to `<a href="xxx">xxxx</a>` * * @since 7.0 */ public static function translate_qc_apis($html) { preg_match_all('/<a href="{#(\w+)#}"/U', $html, $matches); if (!$matches) { return $html; } foreach ($matches[0] as $k => $html_to_be_replaced) { $link = '<a href="' . Utility::build_url(Router::ACTION_CLOUD, Cloud::TYPE_API, false, null, array('action2' => $matches[1][$k])) . '"'; $html = str_replace($html_to_be_replaced, $link, $html); } return $html; } /** * Return safe HTML * * @since 7.0 */ public static function safe_html($html) { $common_attrs = array( 'style' => array(), 'class' => array(), 'target' => array(), 'src' => array(), 'color' => array(), 'href' => array(), ); $tags = array('hr', 'h3', 'h4', 'h5', 'ul', 'li', 'br', 'strong', 'p', 'span', 'img', 'a', 'div', 'font'); $allowed_tags = array(); foreach ($tags as $tag) { $allowed_tags[$tag] = $common_attrs; } return wp_kses($html, $allowed_tags); } /** * Generate random string * * @since 1.3 * @access public * @param int $len Length of string * @param int $type 1-Number 2-LowerChar 4-UpperChar * @return string */ public static function rrand($len, $type = 7) { switch ($type) { case 0: $charlist = '012'; break; case 1: $charlist = '0123456789'; break; case 2: $charlist = 'abcdefghijklmnopqrstuvwxyz'; break; case 3: $charlist = '0123456789abcdefghijklmnopqrstuvwxyz'; break; case 4: $charlist = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; break; case 5: $charlist = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; break; case 6: $charlist = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; break; case 7: $charlist = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; break; } $str = ''; $max = strlen($charlist) - 1; for ($i = 0; $i < $len; $i++) { $str .= $charlist[random_int(0, $max)]; } return $str; } /** * Trim double quotes from a string to be used as a preformatted src in HTML. * @since 6.5.3 */ public static function trim_quotes($string) { return str_replace('"', '', $string); } } admin-display.cls.php 0000644 00000106620 15162226305 0010577 0 ustar 00 <?php /** * The admin-panel specific functionality of the plugin. * * * @since 1.0.0 * @package LiteSpeed * @subpackage LiteSpeed/admin * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Admin_Display extends Base { const LOG_TAG = '👮♀️'; const NOTICE_BLUE = 'notice notice-info'; const NOTICE_GREEN = 'notice notice-success'; const NOTICE_RED = 'notice notice-error'; const NOTICE_YELLOW = 'notice notice-warning'; const DB_MSG = 'messages'; const DB_MSG_PIN = 'msg_pin'; const PURGEBY_CAT = '0'; const PURGEBY_PID = '1'; const PURGEBY_TAG = '2'; const PURGEBY_URL = '3'; const PURGEBYOPT_SELECT = 'purgeby'; const PURGEBYOPT_LIST = 'purgebylist'; const DB_DISMISS_MSG = 'dismiss'; const RULECONFLICT_ON = 'ExpiresDefault_1'; const RULECONFLICT_DISMISSED = 'ExpiresDefault_0'; const TYPE_QC_HIDE_BANNER = 'qc_hide_banner'; const COOKIE_QC_HIDE_BANNER = 'litespeed_qc_hide_banner'; protected $messages = array(); protected $default_settings = array(); protected $_is_network_admin = false; protected $_is_multisite = false; private $_btn_i = 0; /** * Initialize the class and set its properties. * * @since 1.0.7 */ public function __construct() { // main css add_action('admin_enqueue_scripts', array($this, 'enqueue_style')); // Main js add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts')); $this->_is_network_admin = is_network_admin(); $this->_is_multisite = is_multisite(); // Quick access menu if (is_multisite() && $this->_is_network_admin) { $manage = 'manage_network_options'; } else { $manage = 'manage_options'; } if (current_user_can($manage)) { if (!defined('LITESPEED_DISABLE_ALL') || !LITESPEED_DISABLE_ALL) { add_action('wp_before_admin_bar_render', array(GUI::cls(), 'backend_shortcut')); } // `admin_notices` is after `admin_enqueue_scripts` // @see wp-admin/admin-header.php add_action($this->_is_network_admin ? 'network_admin_notices' : 'admin_notices', array($this, 'display_messages')); } /** * In case this is called outside the admin page * @see https://codex.wordpress.org/Function_Reference/is_plugin_active_for_network * @since 2.0 */ if (!function_exists('is_plugin_active_for_network')) { require_once ABSPATH . '/wp-admin/includes/plugin.php'; } // add menus ( Also check for mu-plugins) if ($this->_is_network_admin && (is_plugin_active_for_network(LSCWP_BASENAME) || defined('LSCWP_MU_PLUGIN'))) { add_action('network_admin_menu', array($this, 'register_admin_menu')); } else { add_action('admin_menu', array($this, 'register_admin_menu')); } $this->cls('Metabox')->register_settings(); } /** * Show the title of one line * * @since 3.0 * @access public */ public function title($id) { echo Lang::title($id); } /** * Register the admin menu display. * * @since 1.0.0 * @access public */ public function register_admin_menu() { $capability = $this->_is_network_admin ? 'manage_network_options' : 'manage_options'; if (current_user_can($capability)) { // root menu add_menu_page('LiteSpeed Cache', 'LiteSpeed Cache', 'manage_options', 'litespeed'); // sub menus $this->_add_submenu(__('Dashboard', 'litespeed-cache'), 'litespeed', 'show_menu_dash'); !$this->_is_network_admin && $this->_add_submenu(__('Presets', 'litespeed-cache'), 'litespeed-presets', 'show_menu_presets'); $this->_add_submenu(__('General', 'litespeed-cache'), 'litespeed-general', 'show_menu_general'); $this->_add_submenu(__('Cache', 'litespeed-cache'), 'litespeed-cache', 'show_menu_cache'); !$this->_is_network_admin && $this->_add_submenu(__('CDN', 'litespeed-cache'), 'litespeed-cdn', 'show_menu_cdn'); $this->_add_submenu(__('Image Optimization', 'litespeed-cache'), 'litespeed-img_optm', 'show_img_optm'); !$this->_is_network_admin && $this->_add_submenu(__('Page Optimization', 'litespeed-cache'), 'litespeed-page_optm', 'show_page_optm'); $this->_add_submenu(__('Database', 'litespeed-cache'), 'litespeed-db_optm', 'show_db_optm'); !$this->_is_network_admin && $this->_add_submenu(__('Crawler', 'litespeed-cache'), 'litespeed-crawler', 'show_crawler'); $this->_add_submenu(__('Toolbox', 'litespeed-cache'), 'litespeed-toolbox', 'show_toolbox'); // sub menus under options add_options_page('LiteSpeed Cache', 'LiteSpeed Cache', $capability, 'litespeed-cache-options', array($this, 'show_menu_cache')); } } /** * Helper function to set up a submenu page. * * @since 1.0.4 * @access private * @param string $menu_title The title that appears on the menu. * @param string $menu_slug The slug of the page. * @param string $callback The callback to call if selected. */ private function _add_submenu($menu_title, $menu_slug, $callback) { add_submenu_page('litespeed', $menu_title, $menu_title, 'manage_options', $menu_slug, array($this, $callback)); } /** * Register the stylesheets for the admin area. * * @since 1.0.14 * @access public */ public function enqueue_style() { wp_enqueue_style(Core::PLUGIN_NAME, LSWCP_PLUGIN_URL . 'assets/css/litespeed.css', array(), Core::VER, 'all'); } /** * Register the JavaScript for the admin area. * * @since 1.0.0 * @access public */ public function enqueue_scripts() { wp_register_script(Core::PLUGIN_NAME, LSWCP_PLUGIN_URL . 'assets/js/litespeed-cache-admin.js', array(), Core::VER, false); $localize_data = array(); if (GUI::has_whm_msg()) { $ajax_url_dismiss_whm = Utility::build_url(Core::ACTION_DISMISS, GUI::TYPE_DISMISS_WHM, true); $localize_data['ajax_url_dismiss_whm'] = $ajax_url_dismiss_whm; } if (GUI::has_msg_ruleconflict()) { $ajax_url = Utility::build_url(Core::ACTION_DISMISS, GUI::TYPE_DISMISS_EXPIRESDEFAULT, true); $localize_data['ajax_url_dismiss_ruleconflict'] = $ajax_url; } $promo_tag = GUI::cls()->show_promo(true); if ($promo_tag) { $ajax_url_promo = Utility::build_url(Core::ACTION_DISMISS, GUI::TYPE_DISMISS_PROMO, true, null, array('promo_tag' => $promo_tag)); $localize_data['ajax_url_promo'] = $ajax_url_promo; } // Injection to LiteSpeed pages global $pagenow; if ($pagenow == 'admin.php' && !empty($_GET['page']) && (strpos($_GET['page'], 'litespeed-') === 0 || $_GET['page'] == 'litespeed')) { // Admin footer add_filter('admin_footer_text', array($this, 'admin_footer_text'), 1); if ($_GET['page'] == 'litespeed-crawler' || $_GET['page'] == 'litespeed-cdn') { // Babel JS type correction add_filter('script_loader_tag', array($this, 'babel_type'), 10, 3); wp_enqueue_script(Core::PLUGIN_NAME . '-lib-react', LSWCP_PLUGIN_URL . 'assets/js/react.min.js', array(), Core::VER, false); wp_enqueue_script(Core::PLUGIN_NAME . '-lib-babel', LSWCP_PLUGIN_URL . 'assets/js/babel.min.js', array(), Core::VER, false); } // Crawler Cookie Simulation if ($_GET['page'] == 'litespeed-crawler') { wp_enqueue_script(Core::PLUGIN_NAME . '-crawler', LSWCP_PLUGIN_URL . 'assets/js/component.crawler.js', array(), Core::VER, false); $localize_data['lang'] = array(); $localize_data['lang']['cookie_name'] = __('Cookie Name', 'litespeed-cache'); $localize_data['lang']['cookie_value'] = __('Cookie Values', 'litespeed-cache'); $localize_data['lang']['one_per_line'] = Doc::one_per_line(true); $localize_data['lang']['remove_cookie_simulation'] = __('Remove cookie simulation', 'litespeed-cache'); $localize_data['lang']['add_cookie_simulation_row'] = __('Add new cookie to simulate', 'litespeed-cache'); empty($localize_data['ids']) && ($localize_data['ids'] = array()); $localize_data['ids']['crawler_cookies'] = self::O_CRAWLER_COOKIES; } // CDN mapping if ($_GET['page'] == 'litespeed-cdn') { $home_url = home_url('/'); $parsed = parse_url($home_url); $home_url = str_replace($parsed['scheme'] . ':', '', $home_url); $cdn_url = 'https://cdn.' . substr($home_url, 2); wp_enqueue_script(Core::PLUGIN_NAME . '-cdn', LSWCP_PLUGIN_URL . 'assets/js/component.cdn.js', array(), Core::VER, false); $localize_data['lang'] = array(); $localize_data['lang']['cdn_mapping_url'] = Lang::title(self::CDN_MAPPING_URL); $localize_data['lang']['cdn_mapping_inc_img'] = Lang::title(self::CDN_MAPPING_INC_IMG); $localize_data['lang']['cdn_mapping_inc_css'] = Lang::title(self::CDN_MAPPING_INC_CSS); $localize_data['lang']['cdn_mapping_inc_js'] = Lang::title(self::CDN_MAPPING_INC_JS); $localize_data['lang']['cdn_mapping_filetype'] = Lang::title(self::CDN_MAPPING_FILETYPE); $localize_data['lang']['cdn_mapping_url_desc'] = sprintf(__('CDN URL to be used. For example, %s', 'litespeed-cache'), '<code>' . $cdn_url . '</code>'); $localize_data['lang']['one_per_line'] = Doc::one_per_line(true); $localize_data['lang']['cdn_mapping_remove'] = __('Remove CDN URL', 'litespeed-cache'); $localize_data['lang']['add_cdn_mapping_row'] = __('Add new CDN URL', 'litespeed-cache'); $localize_data['lang']['on'] = __('ON', 'litespeed-cache'); $localize_data['lang']['off'] = __('OFF', 'litespeed-cache'); empty($localize_data['ids']) && ($localize_data['ids'] = array()); $localize_data['ids']['cdn_mapping'] = self::O_CDN_MAPPING; } // If on Server IP setting page, append getIP link if ($_GET['page'] == 'litespeed-general') { $localize_data['ajax_url_getIP'] = function_exists('get_rest_url') ? get_rest_url(null, 'litespeed/v1/tool/check_ip') : '/'; $localize_data['nonce'] = wp_create_nonce('wp_rest'); } // Activate or deactivate a specific crawler if ($_GET['page'] == 'litespeed-crawler') { $localize_data['ajax_url_crawler_switch'] = function_exists('get_rest_url') ? get_rest_url(null, 'litespeed/v1/toggle_crawler_state') : '/'; $localize_data['nonce'] = wp_create_nonce('wp_rest'); } } if ($localize_data) { wp_localize_script(Core::PLUGIN_NAME, 'litespeed_data', $localize_data); } wp_enqueue_script(Core::PLUGIN_NAME); } /** * Babel type for crawler * * @since 3.6 */ public function babel_type($tag, $handle, $src) { if ($handle != Core::PLUGIN_NAME . '-crawler' && $handle != Core::PLUGIN_NAME . '-cdn') { return $tag; } return '<script src="' . Str::trim_quotes($src) . '" type="text/babel"></script>'; } /** * Callback that adds LiteSpeed Cache's action links. * * @since 1.0.0 * @access public * @param array $links Previously added links from other plugins. * @return array Links array with the litespeed cache one appended. */ public function add_plugin_links($links) { // $links[] = '<a href="' . admin_url('options-general.php?page=litespeed-cache') . '">' . __('Settings', 'litespeed-cache') . '</a>'; $links[] = '<a href="' . admin_url('admin.php?page=litespeed-cache') . '">' . __('Settings', 'litespeed-cache') . '</a>'; return $links; } /** * Change the admin footer text on LiteSpeed Cache admin pages. * * @since 1.0.13 * @param string $footer_text * @return string */ public function admin_footer_text($footer_text) { require_once LSCWP_DIR . 'tpl/inc/admin_footer.php'; return $footer_text; } /** * Builds the html for a single notice. * * @since 1.0.7 * @access public * @param string $color The color to use for the notice. * @param string $str The notice message. * @return string The built notice html. */ public static function build_notice($color, $str, $irremovable = false, $additional_classes = '') { $cls = $color; if ($irremovable) { $cls .= ' litespeed-irremovable'; } else { $cls .= ' is-dismissible'; } if ($additional_classes) { $cls .= ' ' . $additional_classes; } // possible translation $str = Lang::maybe_translate($str); return '<div class="litespeed_icon ' . $cls . '"><p>' . wp_kses_post($str) . '</p></div>'; } /** * Display info notice * * @since 1.6.5 * @access public */ public static function info($msg, $echo = false, $irremovable = false, $additional_classes = '') { self::add_notice(self::NOTICE_BLUE, $msg, $echo, $irremovable, $additional_classes); } /** * Display note notice * * @since 1.6.5 * @access public */ public static function note($msg, $echo = false, $irremovable = false, $additional_classes = '') { self::add_notice(self::NOTICE_YELLOW, $msg, $echo, $irremovable, $additional_classes); } /** * Display success notice * * @since 1.6 * @access public */ public static function success($msg, $echo = false, $irremovable = false, $additional_classes = '') { self::add_notice(self::NOTICE_GREEN, $msg, $echo, $irremovable, $additional_classes); } /** @deprecated 4.7 */ /** will drop in v7.5 */ public static function succeed($msg, $echo = false, $irremovable = false, $additional_classes = '') { self::success($msg, $echo, $irremovable, $additional_classes); } /** * Display error notice * * @since 1.6 * @access public */ public static function error($msg, $echo = false, $irremovable = false, $additional_classes = '') { self::add_notice(self::NOTICE_RED, $msg, $echo, $irremovable, $additional_classes); } /** * Add irremovable msg * @since 4.7 */ public static function add_unique_notice($color_mode, $msgs, $irremovable = false) { if (!is_array($msgs)) { $msgs = array($msgs); } $color_map = array( 'info' => self::NOTICE_BLUE, 'note' => self::NOTICE_YELLOW, 'success' => self::NOTICE_GREEN, 'error' => self::NOTICE_RED, ); if (empty($color_map[$color_mode])) { self::debug('Wrong admin display color mode!'); return; } $color = $color_map[$color_mode]; // Go through to make sure unique $filtered_msgs = array(); foreach ($msgs as $k => $str) { if (is_numeric($k)) { $k = md5($str); } // Use key to make it overwritable to previous same msg $filtered_msgs[$k] = $str; } self::add_notice($color, $filtered_msgs, false, $irremovable); } /** * Adds a notice to display on the admin page * * @since 1.0.7 * @access public */ public static function add_notice($color, $msg, $echo = false, $irremovable = false, $additional_classes = '') { // self::debug("add_notice msg", $msg); // Bypass adding for CLI or cron if (defined('LITESPEED_CLI') || defined('DOING_CRON')) { // WP CLI will show the info directly if (defined('WP_CLI') && WP_CLI) { if (!is_array($msg)) { $msg = array($msg); } foreach ($msg as $v) { $v = strip_tags($v); if ($color == self::NOTICE_RED) { \WP_CLI::error($v, false); } else { \WP_CLI::success($v); } } } return; } if ($echo) { echo self::build_notice($color, $msg, $irremovable, $additional_classes); return; } $msg_name = $irremovable ? self::DB_MSG_PIN : self::DB_MSG; $messages = self::get_option($msg_name, array()); if (!is_array($messages)) { $messages = array(); } if (is_array($msg)) { foreach ($msg as $k => $str) { $messages[$k] = self::build_notice($color, $str, $irremovable, $additional_classes); } } else { $messages[] = self::build_notice($color, $msg, $irremovable, $additional_classes); } $messages = array_unique($messages); self::update_option($msg_name, $messages); } /** * Display notices and errors in dashboard * * @since 1.1.0 * @access public */ public function display_messages() { if (!defined('LITESPEED_CONF_LOADED')) { $this->_in_upgrading(); } if (GUI::has_whm_msg()) { $this->show_display_installed(); } Data::cls()->check_upgrading_msg(); // If is in dev version, always check latest update Cloud::cls()->check_dev_version(); // One time msg $messages = self::get_option(self::DB_MSG, array()); $added_thickbox = false; if (is_array($messages)) { foreach ($messages as $msg) { // Added for popup links if (strpos($msg, 'TB_iframe') && !$added_thickbox) { add_thickbox(); $added_thickbox = true; } echo wp_kses_post($msg); } } if ($messages != -1) { self::update_option(self::DB_MSG, -1); } // Pinned msg $messages = self::get_option(self::DB_MSG_PIN, array()); if (is_array($messages)) { foreach ($messages as $k => $msg) { // Added for popup links if (strpos($msg, 'TB_iframe') && !$added_thickbox) { add_thickbox(); $added_thickbox = true; } // Append close btn if (substr($msg, -6) == '</div>') { $link = Utility::build_url(Core::ACTION_DISMISS, GUI::TYPE_DISMISS_PIN, false, null, array('msgid' => $k)); $msg = substr($msg, 0, -6) . '<p><a href="' . $link . '" class="button litespeed-btn-primary litespeed-btn-mini">' . __('Dismiss', 'litespeed-cache') . '</a>' . '</p></div>'; } echo wp_kses_post($msg); } } // if ( $messages != -1 ) { // self::update_option( self::DB_MSG_PIN, -1 ); // } if (empty($_GET['page']) || strpos($_GET['page'], 'litespeed') !== 0) { global $pagenow; if ($pagenow != 'plugins.php') { // && $pagenow != 'index.php' return; } } // Show disable all warning if (defined('LITESPEED_DISABLE_ALL') && LITESPEED_DISABLE_ALL) { Admin_Display::error(Error::msg('disabled_all'), true); } if (!$this->conf(self::O_NEWS)) { return; } // Show promo from cloud Cloud::cls()->show_promo(); /** * Check promo msg first * @since 2.9 */ GUI::cls()->show_promo(); // Show version news Cloud::cls()->news(); } /** * Dismiss pinned msg * * @since 3.5.2 * @access public */ public static function dismiss_pin() { if (!isset($_GET['msgid'])) { return; } $messages = self::get_option(self::DB_MSG_PIN, array()); if (!is_array($messages) || empty($messages[$_GET['msgid']])) { return; } unset($messages[$_GET['msgid']]); if (!$messages) { $messages = -1; } self::update_option(self::DB_MSG_PIN, $messages); } /** * Dismiss pinned msg by msg content * * @since 7.0 * @access public */ public static function dismiss_pin_by_content($content, $color, $irremovable) { $content = self::build_notice($color, $content, $irremovable); $messages = self::get_option(self::DB_MSG_PIN, array()); $hit = false; if ($messages != -1) { foreach ($messages as $k => $v) { if ($v == $content) { unset($messages[$k]); $hit = true; self::debug('✅ pinned msg content hit. Removed'); break; } } } if ($hit) { if (!$messages) { $messages = -1; } self::update_option(self::DB_MSG_PIN, $messages); } else { self::debug('❌ No pinned msg content hit'); } } /** * Hooked to the in_widget_form action. * Appends LiteSpeed Cache settings to the widget edit settings screen. * This will append the esi on/off selector and ttl text. * * @since 1.1.0 * @access public */ public function show_widget_edit($widget, $return, $instance) { require LSCWP_DIR . 'tpl/esi_widget_edit.php'; } /** * Displays the dashboard page. * * @since 3.0 * @access public */ public function show_menu_dash() { $this->cls('Cloud')->maybe_preview_banner(); require_once LSCWP_DIR . 'tpl/dash/entry.tpl.php'; } /** * Displays the General page. * * @since 5.3 * @access public */ public function show_menu_presets() { require_once LSCWP_DIR . 'tpl/presets/entry.tpl.php'; } /** * Displays the General page. * * @since 3.0 * @access public */ public function show_menu_general() { $this->cls('Cloud')->maybe_preview_banner(); require_once LSCWP_DIR . 'tpl/general/entry.tpl.php'; } /** * Displays the CDN page. * * @since 3.0 * @access public */ public function show_menu_cdn() { $this->cls('Cloud')->maybe_preview_banner(); require_once LSCWP_DIR . 'tpl/cdn/entry.tpl.php'; } /** * Outputs the LiteSpeed Cache settings page. * * @since 1.0.0 * @access public */ public function show_menu_cache() { if ($this->_is_network_admin) { require_once LSCWP_DIR . 'tpl/cache/entry_network.tpl.php'; } else { require_once LSCWP_DIR . 'tpl/cache/entry.tpl.php'; } } /** * Tools page * * @since 3.0 * @access public */ public function show_toolbox() { $this->cls('Cloud')->maybe_preview_banner(); require_once LSCWP_DIR . 'tpl/toolbox/entry.tpl.php'; } /** * Outputs the crawler operation page. * * @since 1.1.0 * @access public */ public function show_crawler() { $this->cls('Cloud')->maybe_preview_banner(); require_once LSCWP_DIR . 'tpl/crawler/entry.tpl.php'; } /** * Outputs the optimization operation page. * * @since 1.6 * @access public */ public function show_img_optm() { $this->cls('Cloud')->maybe_preview_banner(); require_once LSCWP_DIR . 'tpl/img_optm/entry.tpl.php'; } /** * Page optm page. * * @since 3.0 * @access public */ public function show_page_optm() { $this->cls('Cloud')->maybe_preview_banner(); require_once LSCWP_DIR . 'tpl/page_optm/entry.tpl.php'; } /** * DB optm page. * * @since 3.0 * @access public */ public function show_db_optm() { require_once LSCWP_DIR . 'tpl/db_optm/entry.tpl.php'; } /** * Outputs a notice to the admin panel when the plugin is installed * via the WHM plugin. * * @since 1.0.12 * @access public */ public function show_display_installed() { require_once LSCWP_DIR . 'tpl/inc/show_display_installed.php'; } /** * Display error cookie msg. * * @since 1.0.12 * @access public */ public static function show_error_cookie() { require_once LSCWP_DIR . 'tpl/inc/show_error_cookie.php'; } /** * Display warning if lscache is disabled * * @since 2.1 * @access public */ public function cache_disabled_warning() { include LSCWP_DIR . 'tpl/inc/check_cache_disabled.php'; } /** * Display conf data upgrading banner * * @since 2.1 * @access private */ private function _in_upgrading() { include LSCWP_DIR . 'tpl/inc/in_upgrading.php'; } /** * Output litespeed form info * * @since 3.0 * @access public */ public function form_action($action = false, $type = false, $has_upload = false) { if (!$action) { $action = Router::ACTION_SAVE_SETTINGS; } $has_upload = $has_upload ? 'enctype="multipart/form-data"' : ''; if (!defined('LITESPEED_CONF_LOADED')) { echo '<div class="litespeed-relative"'; } else { echo '<form method="post" action="' . wp_unslash($_SERVER['REQUEST_URI']) . '" class="litespeed-relative" ' . $has_upload . '>'; } echo '<input type="hidden" name="' . Router::ACTION . '" value="' . $action . '" />'; if ($type) { echo '<input type="hidden" name="' . Router::TYPE . '" value="' . $type . '" />'; } wp_nonce_field($action, Router::NONCE); } /** * Output litespeed form info END * * @since 3.0 * @access public */ public function form_end($disable_reset = false) { echo "<div class='litespeed-top20'></div>"; if (!defined('LITESPEED_CONF_LOADED')) { submit_button(__('Save Changes', 'litespeed-cache'), 'secondary litespeed-duplicate-float', 'litespeed-submit', true, array('disabled' => 'disabled')); echo '</div>'; } else { submit_button(__('Save Changes', 'litespeed-cache'), 'primary litespeed-duplicate-float', 'litespeed-submit', true, array( 'id' => 'litespeed-submit-' . $this->_btn_i++, )); echo '</form>'; } } /** * Register this setting to save * * @since 3.0 * @access public */ public function enroll($id) { echo '<input type="hidden" name="' . Admin_Settings::ENROLL . '[]" value="' . $id . '" />'; } /** * Build a textarea * * @since 1.1.0 * @access public */ public function build_textarea($id, $cols = false, $val = null) { if ($val === null) { $val = $this->conf($id, true); if (is_array($val)) { $val = implode("\n", $val); } } if (!$cols) { $cols = 80; } $rows = 5; $lines = substr_count($val, "\n") + 2; if ($lines > $rows) { $rows = $lines; } if ($rows > 40) { $rows = 40; } $this->enroll($id); echo "<textarea name='$id' rows='$rows' cols='$cols'>" . esc_textarea($val) . '</textarea>'; $this->_check_overwritten($id); } /** * Build a text input field * * @since 1.1.0 * @access public */ public function build_input($id, $cls = null, $val = null, $type = 'text', $disabled = false) { if ($val === null) { $val = $this->conf($id, true); // Mask pswds if ($this->_conf_pswd($id) && $val) { $val = str_repeat('*', strlen($val)); } } $label_id = preg_replace('/\W/', '', $id); if ($type == 'text') { $cls = "regular-text $cls"; } if ($disabled) { echo "<input type='$type' class='$cls' value='" . esc_textarea($val) . "' id='input_$label_id' disabled /> "; } else { $this->enroll($id); echo "<input type='$type' class='$cls' name='$id' value='" . esc_textarea($val) . "' id='input_$label_id' /> "; } $this->_check_overwritten($id); } /** * Build a checkbox html snippet * * @since 1.1.0 * @access public * @param string $id * @param string $title * @param bool $checked */ public function build_checkbox($id, $title, $checked = null, $value = 1) { if ($checked === null && $this->conf($id, true)) { $checked = true; } $checked = $checked ? ' checked ' : ''; $label_id = preg_replace('/\W/', '', $id); if ($value !== 1) { $label_id .= '_' . $value; } $this->enroll($id); echo "<div class='litespeed-tick'> <input type='checkbox' name='$id' id='input_checkbox_$label_id' value='$value' $checked /> <label for='input_checkbox_$label_id'>$title</label> </div>"; $this->_check_overwritten($id); } /** * Build a toggle checkbox html snippet * * @since 1.7 */ public function build_toggle($id, $checked = null, $title_on = null, $title_off = null) { if ($checked === null && $this->conf($id, true)) { $checked = true; } if ($title_on === null) { $title_on = __('ON', 'litespeed-cache'); $title_off = __('OFF', 'litespeed-cache'); } $cls = $checked ? 'primary' : 'default litespeed-toggleoff'; echo "<div class='litespeed-toggle litespeed-toggle-btn litespeed-toggle-btn-$cls' data-litespeed-toggle-on='primary' data-litespeed-toggle-off='default' data-litespeed_toggle_id='$id' > <input name='$id' type='hidden' value='$checked' /> <div class='litespeed-toggle-group'> <label class='litespeed-toggle-btn litespeed-toggle-btn-primary litespeed-toggle-on'>$title_on</label> <label class='litespeed-toggle-btn litespeed-toggle-btn-default litespeed-toggle-active litespeed-toggle-off'>$title_off</label> <span class='litespeed-toggle-handle litespeed-toggle-btn litespeed-toggle-btn-default'></span> </div> </div>"; } /** * Build a switch div html snippet * * @since 1.1.0 * @since 1.7 removed param $disable * @access public */ public function build_switch($id, $title_list = false) { $this->enroll($id); echo '<div class="litespeed-switch">'; if (!$title_list) { $title_list = array(__('OFF', 'litespeed-cache'), __('ON', 'litespeed-cache')); } foreach ($title_list as $k => $v) { $this->_build_radio($id, $k, $v); } echo '</div>'; $this->_check_overwritten($id); } /** * Build a radio input html codes and output * * @since 1.1.0 * @access private */ private function _build_radio($id, $val, $txt) { $id_attr = 'input_radio_' . preg_replace('/\W/', '', $id) . '_' . $val; $default = isset(self::$_default_options[$id]) ? self::$_default_options[$id] : self::$_default_site_options[$id]; if (!is_string($default)) { $checked = (int) $this->conf($id, true) === (int) $val ? ' checked ' : ''; } else { $checked = $this->conf($id, true) === $val ? ' checked ' : ''; } echo "<input type='radio' autocomplete='off' name='$id' id='$id_attr' value='$val' $checked /> <label for='$id_attr'>$txt</label>"; } /** * Show overwritten msg if there is a const defined * * @since 3.0 */ protected function _check_overwritten($id) { $const_val = $this->const_overwritten($id); $primary_val = $this->primary_overwritten($id); if ($const_val === null && $primary_val === null) { return; } $val = $const_val !== null ? $const_val : $primary_val; $default = isset(self::$_default_options[$id]) ? self::$_default_options[$id] : self::$_default_site_options[$id]; if (is_bool($default)) { $val = $val ? __('ON', 'litespeed-cache') : __('OFF', 'litespeed-cache'); } else { if (is_array($default)) { $val = implode("\n", $val); } $val = esc_textarea($val); } echo '<div class="litespeed-desc litespeed-warning">⚠️ '; if ($const_val !== null) { echo sprintf(__('This setting is overwritten by the PHP constant %s', 'litespeed-cache'), '<code>' . Base::conf_const($id) . '</code>'); } else { if (get_current_blog_id() != BLOG_ID_CURRENT_SITE && $this->conf(self::NETWORK_O_USE_PRIMARY)) { echo __('This setting is overwritten by the primary site setting', 'litespeed-cache'); } else { echo __('This setting is overwritten by the Network setting', 'litespeed-cache'); } } echo ', ' . sprintf(__('currently set to %s', 'litespeed-cache'), "<code>$val</code>") . '</div>'; } /** * Display seconds text and readable layout * * @since 3.0 * @access public */ public function readable_seconds() { echo __('seconds', 'litespeed-cache'); echo ' <span data-litespeed-readable=""></span>'; } /** * Display default value * * @since 1.1.1 * @access public */ public function recommended($id) { if (!$this->default_settings) { $this->default_settings = $this->load_default_vals(); } $val = $this->default_settings[$id]; if ($val) { if (is_array($val)) { $rows = 5; $cols = 30; // Flexible rows/cols $lines = count($val) + 1; $rows = min(max($lines, $rows), 40); foreach ($val as $v) { $cols = max(strlen($v), $cols); } $cols = min($cols, 150); $val = implode("\n", $val); $val = esc_textarea($val); $val = '<div class="litespeed-desc">' . __('Default value', 'litespeed-cache') . ':</div>' . "<textarea readonly rows='$rows' cols='$cols'>$val</textarea>"; } else { $val = esc_textarea($val); $val = "<code>$val</code>"; $val = __('Default value', 'litespeed-cache') . ': ' . $val; } echo $val; } } /** * Validate rewrite rules regex syntax * * @since 3.0 */ protected function _validate_syntax($id) { $val = $this->conf($id, true); if (!$val) { return; } if (!is_array($val)) { $val = array($val); } foreach ($val as $v) { if (!Utility::syntax_checker($v)) { echo '<br /><font class="litespeed-warning"> ❌ ' . __('Invalid rewrite rule', 'litespeed-cache') . ': <code>' . $v . '</code></font>'; } } } /** * Validate if the htaccess path is valid * * @since 3.0 */ protected function _validate_htaccess_path($id) { $val = $this->conf($id, true); if (!$val) { return; } if (substr($val, -10) !== '/.htaccess') { echo '<br /><font class="litespeed-warning"> ❌ ' . sprintf(__('Path must end with %s', 'litespeed-cache'), '<code>/.htaccess</code>') . '</font>'; } } /** * Check ttl instead of error when saving * * @since 3.0 */ protected function _validate_ttl($id, $min = false, $max = false, $allow_zero = false) { $val = $this->conf($id, true); if ($allow_zero && !$val) { // return; } $tip = array(); if ($min && $val < $min && (!$allow_zero || $val != 0)) { $tip[] = __('Minimum value', 'litespeed-cache') . ': <code>' . $min . '</code>.'; } if ($max && $val > $max) { $tip[] = __('Maximum value', 'litespeed-cache') . ': <code>' . $max . '</code>.'; } echo '<br />'; if ($tip) { echo '<font class="litespeed-warning"> ❌ ' . implode(' ', $tip) . '</font>'; } $range = ''; if ($allow_zero) { $range .= __('Zero, or', 'litespeed-cache') . ' '; } if ($min && $max) { $range .= $min . ' - ' . $max; } elseif ($min) { $range .= __('Larger than', 'litespeed-cache') . ' ' . $min; } elseif ($max) { $range .= __('Smaller than', 'litespeed-cache') . ' ' . $max; } echo __('Value range', 'litespeed-cache') . ': <code>' . $range . '</code>'; } /** * Check if ip is valid * * @since 3.0 */ protected function _validate_ip($id) { $val = $this->conf($id, true); if (!$val) { return; } if (!is_array($val)) { $val = array($val); } $tip = array(); foreach ($val as $v) { if (!$v) { continue; } if (!\WP_Http::is_ip_address($v)) { $tip[] = __('Invalid IP', 'litespeed-cache') . ': <code>' . esc_textarea($v) . '</code>.'; } } if ($tip) { echo '<br /><font class="litespeed-warning"> ❌ ' . implode(' ', $tip) . '</font>'; } } /** * Display API environment variable support * * @since 1.8.3 * @access protected */ protected function _api_env_var() { $args = func_get_args(); $s = '<code>' . implode('</code>, <code>', $args) . '</code>'; echo '<font class="litespeed-success"> ' . __('API', 'litespeed-cache') . ': ' . sprintf(__('Server variable(s) %s available to override this setting.', 'litespeed-cache'), $s); Doc::learn_more('https://docs.litespeedtech.com/lscache/lscwp/admin/#limiting-the-crawler'); } /** * Display URI setting example * * @since 2.6.1 * @access protected */ protected function _uri_usage_example() { echo __('The URLs will be compared to the REQUEST_URI server variable.', 'litespeed-cache'); echo ' ' . sprintf(__('For example, for %s, %s can be used here.', 'litespeed-cache'), '<code>/mypath/mypage?aa=bb</code>', '<code>mypage?aa=</code>'); echo '<br /><i>'; echo sprintf(__('To match the beginning, add %s to the beginning of the item.', 'litespeed-cache'), '<code>^</code>'); echo ' ' . sprintf(__('To do an exact match, add %s to the end of the URL.', 'litespeed-cache'), '<code>$</code>'); echo ' ' . __('One per line.', 'litespeed-cache'); echo '</i>'; } /** * Return groups string * * @since 2.0 * @access public */ public static function print_plural($num, $kind = 'group') { if ($num > 1) { switch ($kind) { case 'group': return sprintf(__('%s groups', 'litespeed-cache'), $num); case 'image': return sprintf(__('%s images', 'litespeed-cache'), $num); default: return $num; } } switch ($kind) { case 'group': return sprintf(__('%s group', 'litespeed-cache'), $num); case 'image': return sprintf(__('%s image', 'litespeed-cache'), $num); default: return $num; } } /** * Return guidance html * * @since 2.0 * @access public */ public static function guidance($title, $steps, $current_step) { if ($current_step === 'done') { $current_step = count($steps) + 1; } $percentage = ' (' . floor((($current_step - 1) * 100) / count($steps)) . '%)'; $html = '<div class="litespeed-guide">' . '<h2>' . $title . $percentage . '</h2>' . '<ol>'; foreach ($steps as $k => $v) { $step = $k + 1; if ($current_step > $step) { $html .= '<li class="litespeed-guide-done">'; } else { $html .= '<li>'; } $html .= $v . '</li>'; } $html .= '</ol></div>'; return $html; } /** * Check if has qc hide banner cookie or not * @since 7.1 */ public static function has_qc_hide_banner() { return isset($_COOKIE[self::COOKIE_QC_HIDE_BANNER]); } /** * Set qc hide banner cookie * @since 7.1 */ public static function set_qc_hide_banner() { $expire = time() + 86400 * 365; self::debug('Set qc hide banner cookie'); setcookie(self::COOKIE_QC_HIDE_BANNER, time(), $expire, COOKIEPATH, COOKIE_DOMAIN); } /** * Handle all request actions from main cls * * @since 7.1 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_QC_HIDE_BANNER: self::set_qc_hide_banner(); break; default: break; } Admin::redirect(); } } optimizer.cls.php 0000644 00000022625 15162226306 0010071 0 ustar 00 <?php /** * The optimize4 class. * * @since 1.9 * @package LiteSpeed * @subpackage LiteSpeed/inc * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Optimizer extends Root { private $_conf_css_font_display; /** * Init optimizer * * @since 1.9 */ public function __construct() { $this->_conf_css_font_display = $this->conf(Base::O_OPTM_CSS_FONT_DISPLAY); } /** * Run HTML minify process and return final content * * @since 1.9 * @access public */ public function html_min($content, $force_inline_minify = false) { if (!apply_filters('litespeed_html_min', true)) { Debug2::debug2('[Optmer] html_min bypassed via litespeed_html_min filter'); return $content; } $options = array(); if ($force_inline_minify) { $options['jsMinifier'] = __CLASS__ . '::minify_js'; } $skip_comments = $this->conf(Base::O_OPTM_HTML_SKIP_COMMENTS); if ($skip_comments) { $options['skipComments'] = $skip_comments; } /** * Added exception capture when minify * @since 2.2.3 */ try { $obj = new Lib\HTML_MIN($content, $options); $content_final = $obj->process(); // check if content from minification is empty if ($content_final == '') { Debug2::debug('Failed to minify HTML: HTML minification resulted in empty HTML'); return $content; } if (!defined('LSCACHE_ESI_SILENCE')) { $content_final .= "\n" . '<!-- Page optimized by LiteSpeed Cache @' . date('Y-m-d H:i:s', time() + LITESPEED_TIME_OFFSET) . ' -->'; } return $content_final; } catch (\Exception $e) { Debug2::debug('******[Optmer] html_min failed: ' . $e->getMessage()); error_log('****** LiteSpeed Optimizer html_min failed: ' . $e->getMessage()); return $content; } } /** * Run minify process and save content * * @since 1.9 * @access public */ public function serve($request_url, $file_type, $minify, $src_list) { // Try Unique CSS if ($file_type == 'css') { $content = false; if (defined('LITESPEED_GUEST_OPTM') || $this->conf(Base::O_OPTM_UCSS)) { $filename = $this->cls('UCSS')->load($request_url); if ($filename) { return array($filename, 'ucss'); } } } // Before generated, don't know the contented hash filename yet, so used url hash as tmp filename $file_path_prefix = $this->_build_filepath_prefix($file_type); $url_tag = $request_url; $url_tag_for_file = md5($request_url); if (is_404()) { $url_tag_for_file = $url_tag = '404'; } elseif ($file_type == 'css' && apply_filters('litespeed_ucss_per_pagetype', false)) { $url_tag_for_file = $url_tag = Utility::page_type(); } $static_file = LITESPEED_STATIC_DIR . $file_path_prefix . $url_tag_for_file . '.' . $file_type; // Create tmp file to avoid conflict $tmp_static_file = $static_file . '.tmp'; if (file_exists($tmp_static_file) && time() - filemtime($tmp_static_file) <= 600) { // some other request is generating return false; } // File::save( $tmp_static_file, '/* ' . ( is_404() ? '404' : $request_url ) . ' */', true ); // Can't use this bcos this will get filecon md5 changed File::save($tmp_static_file, '', true); // Load content $real_files = array(); foreach ($src_list as $src_info) { $is_min = false; if (!empty($src_info['inl'])) { // Load inline $content = $src_info['src']; } else { // Load file $content = $this->load_file($src_info['src'], $file_type); if (!$content) { continue; } $is_min = $this->is_min($src_info['src']); } $content = $this->optm_snippet($content, $file_type, $minify && !$is_min, $src_info['src'], !empty($src_info['media']) ? $src_info['media'] : false); // Write to file File::save($tmp_static_file, $content, true, true); } // if CSS - run the minification on the saved file. // Will move imports to the top of file and remove extra spaces. if ($file_type == 'css') { $obj = new Lib\CSS_JS_MIN\Minify\CSS(); $file_content_combined = $obj->moveImportsToTop(File::read($tmp_static_file)); File::save($tmp_static_file, $file_content_combined); } // validate md5 $filecon_md5 = md5_file($tmp_static_file); $final_file_path = $file_path_prefix . $filecon_md5 . '.' . $file_type; $realfile = LITESPEED_STATIC_DIR . $final_file_path; if (!file_exists($realfile)) { rename($tmp_static_file, $realfile); Debug2::debug2('[Optmer] Saved static file [path] ' . $realfile); } else { unlink($tmp_static_file); } $vary = $this->cls('Vary')->finalize_full_varies(); Debug2::debug2("[Optmer] Save URL to file for [file_type] $file_type [file] $filecon_md5 [vary] $vary "); $this->cls('Data')->save_url($url_tag, $vary, $file_type, $filecon_md5, dirname($realfile)); return array($filecon_md5 . '.' . $file_type, $file_type); } /** * Load a single file * @since 4.0 */ public function optm_snippet($content, $file_type, $minify, $src, $media = false) { // CSS related features if ($file_type == 'css') { // Font optimize if ($this->_conf_css_font_display) { $content = preg_replace('#(@font\-face\s*\{)#isU', '${1}font-display:swap;', $content); } $content = preg_replace('/@charset[^;]+;\\s*/', '', $content); if ($media) { $content = '@media ' . $media . '{' . $content . "\n}"; } if ($minify) { $content = self::minify_css($content); } $content = $this->cls('CDN')->finalize($content); if ((defined('LITESPEED_GUEST_OPTM') || $this->conf(Base::O_IMG_OPTM_WEBP)) && $this->cls('Media')->webp_support()) { $content = $this->cls('Media')->replace_background_webp($content); } } else { if ($minify) { $content = self::minify_js($content); } else { $content = $this->_null_minifier($content); } $content .= "\n;"; } // Add filter $content = apply_filters('litespeed_optm_cssjs', $content, $file_type, $src); return $content; } /** * Load remote resource from cache if existed * * @since 4.7 */ private function load_cached_file($url, $file_type) { $file_path_prefix = $this->_build_filepath_prefix($file_type); $folder_name = LITESPEED_STATIC_DIR . $file_path_prefix; $to_be_deleted_folder = $folder_name . date('Ymd', strtotime('-2 days')); if (file_exists($to_be_deleted_folder)) { Debug2::debug('[Optimizer] ❌ Clearing folder [name] ' . $to_be_deleted_folder); File::rrmdir($to_be_deleted_folder); } $today_file = $folder_name . date('Ymd') . '/' . md5($url); if (file_exists($today_file)) { return File::read($today_file); } // Write file $res = wp_safe_remote_get($url); $res_code = wp_remote_retrieve_response_code($res); if (is_wp_error($res) || $res_code != 200) { Debug2::debug2('[Optimizer] ❌ Load Remote error [code] ' . $res_code); return false; } $con = wp_remote_retrieve_body($res); if (!$con) { return false; } Debug2::debug('[Optimizer] ✅ Save remote file to cache [name] ' . $today_file); File::save($today_file, $con, true); return $con; } /** * Load remote/local resource * * @since 3.5 */ public function load_file($src, $file_type = 'css') { $real_file = Utility::is_internal_file($src); $postfix = pathinfo(parse_url($src, PHP_URL_PATH), PATHINFO_EXTENSION); if (!$real_file || $postfix != $file_type) { Debug2::debug2('[CSS] Load Remote [' . $file_type . '] ' . $src); $this_url = substr($src, 0, 2) == '//' ? set_url_scheme($src) : $src; $con = $this->load_cached_file($this_url, $file_type); if ($file_type == 'css') { $dirname = dirname($this_url) . '/'; $con = Lib\UriRewriter::prepend($con, $dirname); } } else { Debug2::debug2('[CSS] Load local [' . $file_type . '] ' . $real_file[0]); $con = File::read($real_file[0]); if ($file_type == 'css') { $dirname = dirname($real_file[0]); $con = Lib\UriRewriter::rewrite($con, $dirname); } } return $con; } /** * Minify CSS * * @since 2.2.3 * @access private */ public static function minify_css($data) { try { $obj = new Lib\CSS_JS_MIN\Minify\CSS(); $obj->add($data); return $obj->minify(); } catch (\Exception $e) { Debug2::debug('******[Optmer] minify_css failed: ' . $e->getMessage()); error_log('****** LiteSpeed Optimizer minify_css failed: ' . $e->getMessage()); return $data; } } /** * Minify JS * * Added exception capture when minify * * @since 2.2.3 * @access private */ public static function minify_js($data, $js_type = '') { // For inline JS optimize, need to check if it's js type if ($js_type) { preg_match('#type=([\'"])(.+)\g{1}#isU', $js_type, $matches); if ($matches && $matches[2] != 'text/javascript') { Debug2::debug('******[Optmer] minify_js bypass due to type: ' . $matches[2]); return $data; } } try { $obj = new Lib\CSS_JS_MIN\Minify\JS(); $obj->add($data); return $obj->minify(); } catch (\Exception $e) { Debug2::debug('******[Optmer] minify_js failed: ' . $e->getMessage()); // error_log( '****** LiteSpeed Optimizer minify_js failed: ' . $e->getMessage() ); return $data; } } /** * Basic minifier * * @access private */ private function _null_minifier($content) { $content = str_replace("\r\n", "\n", $content); return trim($content); } /** * Check if the file is already min file * * @since 1.9 */ public function is_min($filename) { $basename = basename($filename); if (preg_match('/[-\.]min\.(?:[a-zA-Z]+)$/i', $basename)) { return true; } return false; } } report.cls.php 0000644 00000014221 15162226307 0007354 0 ustar 00 <?php /** * The report class * * * @since 1.1.0 * @package LiteSpeed * @subpackage LiteSpeed/src * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Report extends Base { const TYPE_SEND_REPORT = 'send_report'; /** * Handle all request actions from main cls * * @since 1.6.5 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_SEND_REPORT: $this->post_env(); break; default: break; } Admin::redirect(); } /** * post env report number to ls center server * * @since 1.6.5 * @access public */ public function post_env() { $report_con = $this->generate_environment_report(); // Generate link $link = !empty($_POST['link']) ? esc_url($_POST['link']) : ''; $notes = !empty($_POST['notes']) ? esc_html($_POST['notes']) : ''; $php_info = !empty($_POST['attach_php']) ? esc_html($_POST['attach_php']) : ''; $report_php = $php_info === '1' ? $this->generate_php_report() : ''; if ($report_php) { $report_con .= "\nPHPINFO\n" . $report_php; } $data = array( 'env' => $report_con, 'link' => $link, 'notes' => $notes, ); $json = Cloud::post(Cloud::API_REPORT, $data); if (!is_array($json)) { return; } $num = !empty($json['num']) ? $json['num'] : '--'; $summary = array( 'num' => $num, 'dateline' => time(), ); self::save_summary($summary); return $num; } /** * Gathers the PHP information. * * @since 7.0 * @access public */ public function generate_php_report($flags = INFO_GENERAL | INFO_CONFIGURATION | INFO_MODULES) { // INFO_ENVIRONMENT $report = ''; ob_start(); phpinfo($flags); $report = ob_get_contents(); ob_end_clean(); preg_match('%<style type="text/css">(.*?)</style>.*?<body>(.*?)</body>%s', $report, $report); return $report[2]; } /** * Gathers the environment details and creates the report. * Will write to the environment report file. * * @since 1.0.12 * @access public */ public function generate_environment_report($options = null) { global $wp_version, $_SERVER; $frontend_htaccess = Htaccess::get_frontend_htaccess(); $backend_htaccess = Htaccess::get_backend_htaccess(); $paths = array($frontend_htaccess); if ($frontend_htaccess != $backend_htaccess) { $paths[] = $backend_htaccess; } if (is_multisite()) { $active_plugins = get_site_option('active_sitewide_plugins'); if (!empty($active_plugins)) { $active_plugins = array_keys($active_plugins); } } else { $active_plugins = get_option('active_plugins'); } if (function_exists('wp_get_theme')) { $theme_obj = wp_get_theme(); $active_theme = $theme_obj->get('Name'); } else { $active_theme = get_current_theme(); } $extras = array( 'wordpress version' => $wp_version, 'siteurl' => get_option('siteurl'), 'home' => get_option('home'), 'home_url' => home_url(), 'locale' => get_locale(), 'active theme' => $active_theme, ); $extras['active plugins'] = $active_plugins; $extras['cloud'] = Cloud::get_summary(); foreach (array('mini_html', 'pk_b64', 'sk_b64', 'cdn_dash', 'ips') as $v) { if (!empty($extras['cloud'][$v])) { unset($extras['cloud'][$v]); } } if (is_null($options)) { $options = $this->get_options(true); if (is_multisite()) { $options2 = $this->get_options(); foreach ($options2 as $k => $v) { if (isset($options[$k]) && $options[$k] !== $v) { $options['[Overwritten] ' . $k] = $v; } } } } if (!is_null($options) && is_multisite()) { $blogs = Activation::get_network_ids(); if (!empty($blogs)) { $i = 0; foreach ($blogs as $blog_id) { if (++$i > 3) { // Only log 3 subsites break; } $opts = $this->cls('Conf')->load_options($blog_id, true); if (isset($opts[self::O_CACHE])) { $options['blog ' . $blog_id . ' radio select'] = $opts[self::O_CACHE]; } } } } // Security: Remove cf key in report $secure_fields = array(self::O_CDN_CLOUDFLARE_KEY, self::O_OBJECT_PSWD); foreach ($secure_fields as $v) { if (!empty($options[$v])) { $options[$v] = str_repeat('*', strlen($options[$v])); } } $report = $this->build_environment_report($_SERVER, $options, $extras, $paths); return $report; } /** * Builds the environment report buffer with the given parameters * * @access private */ private function build_environment_report($server, $options, $extras = array(), $htaccess_paths = array()) { $server_keys = array( 'DOCUMENT_ROOT' => '', 'SERVER_SOFTWARE' => '', 'X-LSCACHE' => '', 'HTTP_X_LSCACHE' => '', ); $server_vars = array_intersect_key($server, $server_keys); $server_vars[] = 'LSWCP_TAG_PREFIX = ' . LSWCP_TAG_PREFIX; $server_vars = array_merge($server_vars, $this->cls('Base')->server_vars()); $buf = $this->_format_report_section('Server Variables', $server_vars); $buf .= $this->_format_report_section('WordPress Specific Extras', $extras); $buf .= $this->_format_report_section('LSCache Plugin Options', $options); if (empty($htaccess_paths)) { return $buf; } foreach ($htaccess_paths as $path) { if (!file_exists($path) || !is_readable($path)) { $buf .= $path . " does not exist or is not readable.\n"; continue; } $content = file_get_contents($path); if ($content === false) { $buf .= $path . " returned false for file_get_contents.\n"; continue; } $buf .= $path . " contents:\n" . $content . "\n\n"; } return $buf; } /** * Creates a part of the environment report based on a section header and an array for the section parameters. * * @since 1.0.12 * @access private */ private function _format_report_section($section_header, $section) { $tab = ' '; // four spaces if (empty($section)) { return 'No matching ' . $section_header . "\n\n"; } $buf = $section_header; foreach ($section as $k => $v) { $buf .= "\n" . $tab; if (!is_numeric($k)) { $buf .= $k . ' = '; } if (!is_string($v)) { $v = var_export($v, true); } else { $v = esc_html($v); } $buf .= $v; } return $buf . "\n\n"; } } conf.cls.php 0000644 00000042613 15162226310 0006766 0 ustar 00 <?php /** * The core plugin config class. * * This maintains all the options and settings for this plugin. * * @since 1.0.0 * @since 1.5 Moved into /inc * @package LiteSpeed * @subpackage LiteSpeed/inc * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Conf extends Base { const TYPE_SET = 'set'; private $_updated_ids = array(); private $_is_primary = false; /** * Specify init logic to avoid infinite loop when calling conf.cls instance * * @since 3.0 * @access public */ public function init() { // Check if conf exists or not. If not, create them in DB (won't change version if is converting v2.9- data) // Conf may be stale, upgrade later $this->_conf_db_init(); /** * Detect if has quic.cloud set * @since 2.9.7 */ if ($this->conf(self::O_CDN_QUIC)) { !defined('LITESPEED_ALLOWED') && define('LITESPEED_ALLOWED', true); } add_action('litespeed_conf_append', array($this, 'option_append'), 10, 2); add_action('litespeed_conf_force', array($this, 'force_option'), 10, 2); $this->define_cache(); } /** * Init conf related data * * @since 3.0 * @access private */ private function _conf_db_init() { /** * Try to load options first, network sites can override this later * * NOTE: Load before run `conf_upgrade()` to avoid infinite loop when getting conf in `conf_upgrade()` */ $this->load_options(); // Check if debug is on // Init debug as early as possible if ($this->conf(Base::O_DEBUG)) { $this->cls('Debug2')->init(); } $ver = $this->conf(self::_VER); /** * Version is less than v3.0, or, is a new installation */ $ver_check_tag = ''; if (!$ver) { // Try upgrade first (network will upgrade inside too) $ver_check_tag = Data::cls()->try_upgrade_conf_3_0(); } else { defined('LSCWP_CUR_V') || define('LSCWP_CUR_V', $ver); /** * Upgrade conf */ if ($ver != Core::VER) { // Plugin version will be set inside // Site plugin upgrade & version change will do in load_site_conf $ver_check_tag = Data::cls()->conf_upgrade($ver); } } /** * Sync latest new options */ if (!$ver || $ver != Core::VER) { // Load default values $this->load_default_vals(); if (!$ver) { // New install $this->set_conf(self::$_default_options); $ver_check_tag .= ' activate' . (defined('LSCWP_REF') ? '_' . LSCWP_REF : ''); } // Init new default/missing options foreach (self::$_default_options as $k => $v) { // If the option existed, bypass updating // Bcos we may ask clients to deactivate for debug temporarily, we need to keep the current cfg in deactivation, hence we need to only try adding default cfg when activating. self::add_option($k, $v); } // Force correct version in case a rare unexpected case that `_ver` exists but empty self::update_option(Base::_VER, Core::VER); if ($ver_check_tag) { Cloud::version_check($ver_check_tag); } } /** * Network sites only * * Override conf if is network subsites and chose `Use Primary Config` */ $this->_try_load_site_options(); // Mark as conf loaded defined('LITESPEED_CONF_LOADED') || define('LITESPEED_CONF_LOADED', true); if (!$ver || $ver != Core::VER) { // Only trigger once in upgrade progress, don't run always $this->update_confs(); // Files only get corrected in activation or saving settings actions. } } /** * Load all latest options from DB * * @since 3.0 * @access public */ public function load_options($blog_id = null, $dry_run = false) { $options = array(); foreach (self::$_default_options as $k => $v) { if (!is_null($blog_id)) { $options[$k] = self::get_blog_option($blog_id, $k, $v); } else { $options[$k] = self::get_option($k, $v); } // Correct value type $options[$k] = $this->type_casting($options[$k], $k); } if ($dry_run) { return $options; } // Bypass site special settings if ($blog_id !== null) { // This is to load the primary settings ONLY // These options are the ones that can be overwritten by primary $options = array_diff_key($options, array_flip(self::$SINGLE_SITE_OPTIONS)); $this->set_primary_conf($options); } else { $this->set_conf($options); } // Append const options if (defined('LITESPEED_CONF') && LITESPEED_CONF) { foreach (self::$_default_options as $k => $v) { $const = Base::conf_const($k); if (defined($const)) { $this->set_const_conf($k, $this->type_casting(constant($const), $k)); } } } } /** * For multisite installations, the single site options need to be updated with the network wide options. * * @since 1.0.13 * @access private */ private function _try_load_site_options() { if (!$this->_if_need_site_options()) { return; } $this->_conf_site_db_init(); $this->_is_primary = get_current_blog_id() == BLOG_ID_CURRENT_SITE; // If network set to use primary setting if ($this->network_conf(self::NETWORK_O_USE_PRIMARY) && !$this->_is_primary) { // subsites or network admin // Get the primary site settings // If it's just upgraded, 2nd blog is being visited before primary blog, can just load default config (won't hurt as this could only happen shortly) $this->load_options(BLOG_ID_CURRENT_SITE); } // Overwrite single blog options with site options foreach (self::$_default_options as $k => $v) { if (!$this->has_network_conf($k)) { continue; } // $this->_options[ $k ] = $this->_network_options[ $k ]; // Special handler to `Enable Cache` option if the value is set to OFF if ($k == self::O_CACHE) { if ($this->_is_primary) { if ($this->conf($k) != $this->network_conf($k)) { if ($this->conf($k) != self::VAL_ON2) { continue; } } } else { if ($this->network_conf(self::NETWORK_O_USE_PRIMARY)) { if ($this->has_primary_conf($k) && $this->primary_conf($k) != self::VAL_ON2) { // This case will use primary_options override always continue; } } else { if ($this->conf($k) != self::VAL_ON2) { continue; } } } } // primary_options will store primary settings + network settings, OR, store the network settings for subsites $this->set_primary_conf($k, $this->network_conf($k)); } // var_dump($this->_options); } /** * Check if needs to load site_options for network sites * * @since 3.0 * @access private */ private function _if_need_site_options() { if (!is_multisite()) { return false; } // Check if needs to use site_options or not // todo: check if site settings are separate bcos it will affect .htaccess /** * In case this is called outside the admin page * @see https://codex.wordpress.org/Function_Reference/is_plugin_active_for_network * @since 2.0 */ if (!function_exists('is_plugin_active_for_network')) { require_once ABSPATH . '/wp-admin/includes/plugin.php'; } // If is not activated on network, it will not have site options if (!is_plugin_active_for_network(Core::PLUGIN_FILE)) { if ((int) $this->conf(self::O_CACHE) == self::VAL_ON2) { // Default to cache on $this->set_conf(self::_CACHE, true); } return false; } return true; } /** * Init site conf and upgrade if necessary * * @since 3.0 * @access private */ private function _conf_site_db_init() { $this->load_site_options(); $ver = $this->network_conf(self::_VER); /** * Don't upgrade or run new installations other than from backend visit * In this case, just use default conf */ if (!$ver || $ver != Core::VER) { if (!is_admin() && !defined('LITESPEED_CLI')) { $this->set_network_conf($this->load_default_site_vals()); return; } } /** * Upgrade conf */ if ($ver && $ver != Core::VER) { // Site plugin version will change inside Data::cls()->conf_site_upgrade($ver); } /** * Is a new installation */ if (!$ver || $ver != Core::VER) { // Load default values $this->load_default_site_vals(); // Init new default/missing options foreach (self::$_default_site_options as $k => $v) { // If the option existed, bypass updating self::add_site_option($k, $v); } } } /** * Get the plugin's site wide options. * * If the site wide options are not set yet, set it to default. * * @since 1.0.2 * @access public */ public function load_site_options() { if (!is_multisite()) { return null; } // Load all site options foreach (self::$_default_site_options as $k => $v) { $val = self::get_site_option($k, $v); $val = $this->type_casting($val, $k, true); $this->set_network_conf($k, $val); } } /** * Append a 3rd party option to default options * * This will not be affected by network use primary site setting. * * NOTE: If it is a multi switch option, need to call `_conf_multi_switch()` first * * @since 3.0 * @access public */ public function option_append($name, $default) { self::$_default_options[$name] = $default; $this->set_conf($name, self::get_option($name, $default)); $this->set_conf($name, $this->type_casting($this->conf($name), $name)); } /** * Force an option to a certain value * * @since 2.6 * @access public */ public function force_option($k, $v) { if (!$this->has_conf($k)) { return; } $v = $this->type_casting($v, $k); if ($this->conf($k) === $v) { return; } Debug2::debug("[Conf] ** $k forced from " . var_export($this->conf($k), true) . ' to ' . var_export($v, true)); $this->set_conf($k, $v); } /** * Define `_CACHE` const in options ( for both single and network ) * * @since 3.0 * @access public */ public function define_cache() { // Init global const cache on setting $this->set_conf(self::_CACHE, false); if ((int) $this->conf(self::O_CACHE) == self::VAL_ON || $this->conf(self::O_CDN_QUIC)) { $this->set_conf(self::_CACHE, true); } // Check network if (!$this->_if_need_site_options()) { // Set cache on $this->_define_cache_on(); return; } // If use network setting if ((int) $this->conf(self::O_CACHE) == self::VAL_ON2 && $this->network_conf(self::O_CACHE)) { $this->set_conf(self::_CACHE, true); } $this->_define_cache_on(); } /** * Define `LITESPEED_ON` * * @since 2.1 * @access private */ private function _define_cache_on() { if (!$this->conf(self::_CACHE)) { return; } defined('LITESPEED_ALLOWED') && !defined('LITESPEED_ON') && define('LITESPEED_ON', true); } /** * Get an option value * * @since 3.0 * @access public * @deprecated 4.0 Use $this->conf() instead */ public static function val($id, $ori = false) { error_log('Called deprecated function \LiteSpeed\Conf::val(). Please use API call instead.'); return self::cls()->conf($id, $ori); } /** * Save option * * @since 3.0 * @access public */ public function update_confs($the_matrix = false) { if ($the_matrix) { foreach ($the_matrix as $id => $val) { $this->update($id, $val); } } if ($this->_updated_ids) { foreach ($this->_updated_ids as $id) { // Check if need to do a purge all or not if ($this->_conf_purge_all($id)) { Purge::purge_all('conf changed [id] ' . $id); } // Check if need to purge a tag if ($tag = $this->_conf_purge_tag($id)) { Purge::add($tag); } // Update cron if ($this->_conf_cron($id)) { $this->cls('Task')->try_clean($id); } // Reset crawler bypassed list when any of the options WebP replace, guest mode, or cache mobile got changed if ($id == self::O_IMG_OPTM_WEBP || $id == self::O_GUEST || $id == self::O_CACHE_MOBILE) { $this->cls('Crawler')->clear_disabled_list(); } } } do_action('litespeed_update_confs', $the_matrix); // Update related tables $this->cls('Data')->correct_tb_existence(); // Update related files $this->cls('Activation')->update_files(); /** * CDN related actions - Cloudflare */ $this->cls('CDN\Cloudflare')->try_refresh_zone(); /** * CDN related actions - QUIC.cloud * @since 2.3 */ $this->cls('CDN\Quic')->try_sync_conf(); } /** * Save option * * Note: this is direct save, won't trigger corresponding file update or data sync. To save settings normally, always use `Conf->update_confs()` * * @since 3.0 * @access public */ public function update($id, $val) { // Bypassed this bcos $this->_options could be changed by force_option() // if ( $this->_options[ $id ] === $val ) { // return; // } if ($id == self::_VER) { return; } if ($id == self::O_SERVER_IP) { if ($val && !Utility::valid_ipv4($val)) { $msg = sprintf(__('Saving option failed. IPv4 only for %s.', 'litespeed-cache'), Lang::title(Base::O_SERVER_IP)); Admin_Display::error($msg); return; } } if (!array_key_exists($id, self::$_default_options)) { defined('LSCWP_LOG') && Debug2::debug('[Conf] Invalid option ID ' . $id); return; } if ($val && $this->_conf_pswd($id) && !preg_match('/[^\*]/', $val)) { return; } // Special handler for CDN Original URLs if ($id == self::O_CDN_ORI && !$val) { $home_url = home_url('/'); $parsed = parse_url($home_url); $home_url = str_replace($parsed['scheme'] . ':', '', $home_url); $val = $home_url; } // Validate type $val = $this->type_casting($val, $id); // Save data self::update_option($id, $val); // Handle purge if setting changed if ($this->conf($id) != $val) { $this->_updated_ids[] = $id; // Check if need to fire a purge or not (Here has to stay inside `update()` bcos need comparing old value) if ($this->_conf_purge($id)) { $diff = array_diff($val, $this->conf($id)); $diff2 = array_diff($this->conf($id), $val); $diff = array_merge($diff, $diff2); // If has difference foreach ($diff as $v) { $v = ltrim($v, '^'); $v = rtrim($v, '$'); $this->cls('Purge')->purge_url($v); } } } // Update in-memory data $this->set_conf($id, $val); } /** * Save network option * * @since 3.0 * @access public */ public function network_update($id, $val) { if (!array_key_exists($id, self::$_default_site_options)) { defined('LSCWP_LOG') && Debug2::debug('[Conf] Invalid network option ID ' . $id); return; } if ($val && $this->_conf_pswd($id) && !preg_match('/[^\*]/', $val)) { return; } // Validate type if (is_bool(self::$_default_site_options[$id])) { $max = $this->_conf_multi_switch($id); if ($max && $val > 1) { $val %= $max + 1; } else { $val = (bool) $val; } } elseif (is_array(self::$_default_site_options[$id])) { // from textarea input if (!is_array($val)) { $val = Utility::sanitize_lines($val, $this->_conf_filter($id)); } } elseif (!is_string(self::$_default_site_options[$id])) { $val = (int) $val; } else { // Check if the string has a limit set $val = $this->_conf_string_val($id, $val); } // Save data self::update_site_option($id, $val); // Handle purge if setting changed if ($this->network_conf($id) != $val) { // Check if need to do a purge all or not if ($this->_conf_purge_all($id)) { Purge::purge_all('[Conf] Network conf changed [id] ' . $id); } // Update in-memory data $this->set_network_conf($id, $val); } // No need to update cron here, Cron will register in each init if ($this->has_conf($id)) { $this->set_conf($id, $val); } } /** * Check if one user role is in exclude optimization group settings * * @since 1.6 * @access public * @param string $role The user role * @return int The set value if already set */ public function in_optm_exc_roles($role = null) { // Get user role if ($role === null) { $role = Router::get_role(); } if (!$role) { return false; } $roles = explode(',', $role); $found = array_intersect($roles, $this->conf(self::O_OPTM_EXC_ROLES)); return $found ? implode(',', $found) : false; } /** * Set one config value directly * * @since 2.9 * @access private */ private function _set_conf() { /** * NOTE: For URL Query String setting, * 1. If append lines to an array setting e.g. `cache-force_uri`, use `set[cache-force_uri][]=the_url`. * 2. If replace the array setting with one line, use `set[cache-force_uri]=the_url`. * 3. If replace the array setting with multi lines value, use 2 then 1. */ if (empty($_GET[self::TYPE_SET]) || !is_array($_GET[self::TYPE_SET])) { return; } $the_matrix = array(); foreach ($_GET[self::TYPE_SET] as $id => $v) { if (!$this->has_conf($id)) { continue; } // Append new item to array type settings if (is_array($v) && is_array($this->conf($id))) { $v = array_merge($this->conf($id), $v); Debug2::debug('[Conf] Appended to settings [' . $id . ']: ' . var_export($v, true)); } else { Debug2::debug('[Conf] Set setting [' . $id . ']: ' . var_export($v, true)); } $the_matrix[$id] = $v; } if (!$the_matrix) { return; } $this->update_confs($the_matrix); $msg = __('Changed setting successfully.', 'litespeed-cache'); Admin_Display::success($msg); // Redirect if changed frontend URL if (!empty($_GET['redirect'])) { wp_redirect($_GET['redirect']); exit(); } } /** * Handle all request actions from main cls * * @since 2.9 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_SET: $this->_set_conf(); break; default: break; } Admin::redirect(); } } cdn/quic.cls.php 0000644 00000005700 15162226314 0007546 0 ustar 00 <?php /** * The quic.cloud class. * * @since 2.4.1 * @package LiteSpeed * @subpackage LiteSpeed/src/cdn * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed\CDN; use LiteSpeed\Cloud; use LiteSpeed\Base; defined('WPINC') || exit(); class Quic extends Base { const LOG_TAG = '☁️'; const TYPE_REG = 'reg'; protected $_summary; private $_force = false; public function __construct() { $this->_summary = self::get_summary(); } /** * Notify CDN new config updated * * @access public */ public function try_sync_conf($force = false) { if ($force) { $this->_force = $force; } if (!$this->conf(self::O_CDN_QUIC)) { if (!empty($this->_summary['conf_md5'])) { self::debug('❌ No QC CDN, clear conf md5!'); self::save_summary(array('conf_md5' => '')); } return false; } // Notice: Sync conf must be after `wp_loaded` hook, to get 3rd party vary injected (e.g. `woocommerce_cart_hash`). if (!did_action('wp_loaded')) { add_action('wp_loaded', array($this, 'try_sync_conf'), 999); self::debug('WP not loaded yet, delay sync to wp_loaded:999'); return; } $options = $this->get_options(); $options['_tp_cookies'] = apply_filters('litespeed_vary_cookies', array()); // Build necessary options only $options_needed = array( self::O_CACHE_DROP_QS, self::O_CACHE_EXC_COOKIES, self::O_CACHE_EXC_USERAGENTS, self::O_CACHE_LOGIN_COOKIE, self::O_CACHE_VARY_COOKIES, self::O_CACHE_MOBILE_RULES, self::O_CACHE_MOBILE, self::O_CACHE_RES, self::O_CACHE_BROWSER, self::O_CACHE_TTL_BROWSER, self::O_IMG_OPTM_WEBP, self::O_GUEST, '_tp_cookies', ); $consts_needed = array('WP_CONTENT_DIR', 'LSCWP_CONTENT_DIR', 'LSCWP_CONTENT_FOLDER', 'LSWCP_TAG_PREFIX'); $options_for_md5 = array(); foreach ($options_needed as $v) { if (isset($options[$v])) { $options_for_md5[$v] = $options[$v]; // Remove overflow multi lines fields if (is_array($options_for_md5[$v]) && count($options_for_md5[$v]) > 30) { $options_for_md5[$v] = array_slice($options_for_md5[$v], 0, 30); } } } $server_vars = $this->server_vars(); foreach ($consts_needed as $v) { if (isset($server_vars[$v])) { if (empty($options_for_md5['_server'])) { $options_for_md5['_server'] = array(); } $options_for_md5['_server'][$v] = $server_vars[$v]; } } $conf_md5 = md5(\json_encode($options_for_md5)); if (!empty($this->_summary['conf_md5'])) { if ($conf_md5 == $this->_summary['conf_md5']) { if (!$this->_force) { self::debug('Bypass sync conf to QC due to same md5', $conf_md5); return; } self::debug('!!!Force sync conf even same md5'); } else { self::debug('[conf_md5] ' . $conf_md5 . ' [existing_conf_md5] ' . $this->_summary['conf_md5']); } } self::save_summary(array('conf_md5' => $conf_md5)); self::debug('sync conf to QC'); Cloud::post(Cloud::SVC_D_SYNC_CONF, $options_for_md5); } } cdn/cloudflare.cls.php 0000644 00000016030 15162226315 0010724 0 ustar 00 <?php /** * The cloudflare CDN class. * * @since 2.1 * @package LiteSpeed * @subpackage LiteSpeed/src/cdn * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed\CDN; use LiteSpeed\Core; use LiteSpeed\Base; use LiteSpeed\Debug2; use LiteSpeed\Router; use LiteSpeed\Admin; use LiteSpeed\Admin_Display; defined('WPINC') || exit(); class Cloudflare extends Base { const TYPE_PURGE_ALL = 'purge_all'; const TYPE_GET_DEVMODE = 'get_devmode'; const TYPE_SET_DEVMODE_ON = 'set_devmode_on'; const TYPE_SET_DEVMODE_OFF = 'set_devmode_off'; const ITEM_STATUS = 'status'; /** * Update zone&name based on latest settings * * @since 3.0 * @access public */ public function try_refresh_zone() { if (!$this->conf(self::O_CDN_CLOUDFLARE)) { return; } $zone = $this->_fetch_zone(); if ($zone) { $this->cls('Conf')->update(self::O_CDN_CLOUDFLARE_NAME, $zone['name']); $this->cls('Conf')->update(self::O_CDN_CLOUDFLARE_ZONE, $zone['id']); Debug2::debug("[Cloudflare] Get zone successfully \t\t[ID] $zone[id]"); } else { $this->cls('Conf')->update(self::O_CDN_CLOUDFLARE_ZONE, ''); Debug2::debug('[Cloudflare] ❌ Get zone failed, clean zone'); } } /** * Get Cloudflare development mode * * @since 1.7.2 * @access private */ private function _get_devmode($show_msg = true) { Debug2::debug('[Cloudflare] _get_devmode'); $zone = $this->_zone(); if (!$zone) { return; } $url = 'https://api.cloudflare.com/client/v4/zones/' . $zone . '/settings/development_mode'; $res = $this->_cloudflare_call($url, 'GET', false, $show_msg); if (!$res) { return; } Debug2::debug('[Cloudflare] _get_devmode result ', $res); // Make sure is array: #992174 $curr_status = self::get_option(self::ITEM_STATUS, array()) ?: array(); $curr_status['devmode'] = $res['value']; $curr_status['devmode_expired'] = $res['time_remaining'] + time(); // update status self::update_option(self::ITEM_STATUS, $curr_status); } /** * Set Cloudflare development mode * * @since 1.7.2 * @access private */ private function _set_devmode($type) { Debug2::debug('[Cloudflare] _set_devmode'); $zone = $this->_zone(); if (!$zone) { return; } $url = 'https://api.cloudflare.com/client/v4/zones/' . $zone . '/settings/development_mode'; $new_val = $type == self::TYPE_SET_DEVMODE_ON ? 'on' : 'off'; $data = array('value' => $new_val); $res = $this->_cloudflare_call($url, 'PATCH', $data); if (!$res) { return; } $res = $this->_get_devmode(false); if ($res) { $msg = sprintf(__('Notified Cloudflare to set development mode to %s successfully.', 'litespeed-cache'), strtoupper($new_val)); Admin_Display::success($msg); } } /** * Purge Cloudflare cache * * @since 1.7.2 * @access private */ private function _purge_all() { Debug2::debug('[Cloudflare] _purge_all'); $cf_on = $this->conf(self::O_CDN_CLOUDFLARE); if (!$cf_on) { $msg = __('Cloudflare API is set to off.', 'litespeed-cache'); Admin_Display::error($msg); return; } $zone = $this->_zone(); if (!$zone) { return; } $url = 'https://api.cloudflare.com/client/v4/zones/' . $zone . '/purge_cache'; $data = array('purge_everything' => true); $res = $this->_cloudflare_call($url, 'DELETE', $data); if ($res) { $msg = __('Notified Cloudflare to purge all successfully.', 'litespeed-cache'); Admin_Display::success($msg); } } /** * Get current Cloudflare zone from cfg * * @since 1.7.2 * @access private */ private function _zone() { $zone = $this->conf(self::O_CDN_CLOUDFLARE_ZONE); if (!$zone) { $msg = __('No available Cloudflare zone', 'litespeed-cache'); Admin_Display::error($msg); return false; } return $zone; } /** * Get Cloudflare zone settings * * @since 1.7.2 * @access private */ private function _fetch_zone() { $kw = $this->conf(self::O_CDN_CLOUDFLARE_NAME); $url = 'https://api.cloudflare.com/client/v4/zones?status=active&match=all'; // Try exact match first if ($kw && strpos($kw, '.')) { $zones = $this->_cloudflare_call($url . '&name=' . $kw, 'GET', false, false); if ($zones) { Debug2::debug('[Cloudflare] fetch_zone exact matched'); return $zones[0]; } } // Can't find, try to get default one $zones = $this->_cloudflare_call($url, 'GET', false, false); if (!$zones) { Debug2::debug('[Cloudflare] fetch_zone no zone'); return false; } if (!$kw) { Debug2::debug('[Cloudflare] fetch_zone no set name, use first one by default'); return $zones[0]; } foreach ($zones as $v) { if (strpos($v['name'], $kw) !== false) { Debug2::debug('[Cloudflare] fetch_zone matched ' . $kw . ' [name] ' . $v['name']); return $v; } } // Can't match current name, return default one Debug2::debug('[Cloudflare] fetch_zone failed match name, use first one by default'); return $zones[0]; } /** * Cloudflare API * * @since 1.7.2 * @access private */ private function _cloudflare_call($url, $method = 'GET', $data = false, $show_msg = true) { Debug2::debug("[Cloudflare] _cloudflare_call \t\t[URL] $url"); if (40 == strlen($this->conf(self::O_CDN_CLOUDFLARE_KEY))) { $headers = array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $this->conf(self::O_CDN_CLOUDFLARE_KEY), ); } else { $headers = array( 'Content-Type' => 'application/json', 'X-Auth-Email' => $this->conf(self::O_CDN_CLOUDFLARE_EMAIL), 'X-Auth-Key' => $this->conf(self::O_CDN_CLOUDFLARE_KEY), ); } $wp_args = array( 'method' => $method, 'headers' => $headers, ); if ($data) { if (is_array($data)) { $data = \json_encode($data); } $wp_args['body'] = $data; } $resp = wp_remote_request($url, $wp_args); if (is_wp_error($resp)) { Debug2::debug('[Cloudflare] error in response'); if ($show_msg) { $msg = __('Failed to communicate with Cloudflare', 'litespeed-cache'); Admin_Display::error($msg); } return false; } $result = wp_remote_retrieve_body($resp); $json = \json_decode($result, true); if ($json && $json['success'] && $json['result']) { Debug2::debug('[Cloudflare] _cloudflare_call called successfully'); if ($show_msg) { $msg = __('Communicated with Cloudflare successfully.', 'litespeed-cache'); Admin_Display::success($msg); } return $json['result']; } Debug2::debug("[Cloudflare] _cloudflare_call called failed: $result"); if ($show_msg) { $msg = __('Failed to communicate with Cloudflare', 'litespeed-cache'); Admin_Display::error($msg); } return false; } /** * Handle all request actions from main cls * * @since 1.7.2 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_PURGE_ALL: $this->_purge_all(); break; case self::TYPE_GET_DEVMODE: $this->_get_devmode(); break; case self::TYPE_SET_DEVMODE_ON: case self::TYPE_SET_DEVMODE_OFF: $this->_set_devmode($type); break; default: break; } Admin::redirect(); } } vpi.cls.php 0000644 00000016304 15162226316 0006643 0 ustar 00 <?php /** * The viewport image class. * * @since 4.7 */ namespace LiteSpeed; defined('WPINC') || exit(); class VPI extends Base { const LOG_TAG = '[VPI]'; const TYPE_GEN = 'gen'; const TYPE_CLEAR_Q = 'clear_q'; protected $_summary; private $_queue; /** * Init * * @since 4.7 */ public function __construct() { $this->_summary = self::get_summary(); } /** * The VPI content of the current page * * @since 4.7 */ public function add_to_queue() { $is_mobile = $this->_separate_mobile(); global $wp; $request_url = home_url($wp->request); $ua = !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; // Store it to prepare for cron $this->_queue = $this->load_queue('vpi'); if (count($this->_queue) > 500) { self::debug('Queue is full - 500'); return; } $home_id = get_option('page_for_posts'); if (!is_singular() && !($home_id > 0 && is_home())) { self::debug('not single post ID'); return; } $post_id = is_home() ? $home_id : get_the_ID(); $queue_k = ($is_mobile ? 'mobile' : '') . ' ' . $request_url; if (!empty($this->_queue[$queue_k])) { self::debug('queue k existed ' . $queue_k); return; } $this->_queue[$queue_k] = array( 'url' => apply_filters('litespeed_vpi_url', $request_url), 'post_id' => $post_id, 'user_agent' => substr($ua, 0, 200), 'is_mobile' => $this->_separate_mobile(), ); // Current UA will be used to request $this->save_queue('vpi', $this->_queue); self::debug('Added queue_vpi [url] ' . $queue_k . ' [UA] ' . $ua); // Prepare cache tag for later purge Tag::add('VPI.' . md5($queue_k)); return null; } /** * Notify finished from server * @since 4.7 */ public function notify() { $post_data = \json_decode(file_get_contents('php://input'), true); if (is_null($post_data)) { $post_data = $_POST; } self::debug('notify() data', $post_data); $this->_queue = $this->load_queue('vpi'); list($post_data) = $this->cls('Cloud')->extract_msg($post_data, 'vpi'); $notified_data = $post_data['data']; if (empty($notified_data) || !is_array($notified_data)) { self::debug('❌ notify exit: no notified data'); return Cloud::err('no notified data'); } // Check if its in queue or not $valid_i = 0; foreach ($notified_data as $v) { if (empty($v['request_url'])) { self::debug('❌ notify bypass: no request_url', $v); continue; } if (empty($v['queue_k'])) { self::debug('❌ notify bypass: no queue_k', $v); continue; } // $queue_k = ( $is_mobile ? 'mobile' : '' ) . ' ' . $v[ 'request_url' ]; $queue_k = $v['queue_k']; if (empty($this->_queue[$queue_k])) { self::debug('❌ notify bypass: no this queue [q_k]' . $queue_k); continue; } // Save data if (!empty($v['data_vpi'])) { $post_id = $this->_queue[$queue_k]['post_id']; $name = !empty($v['is_mobile']) ? 'litespeed_vpi_list_mobile' : 'litespeed_vpi_list'; $urldecode = is_array($v['data_vpi']) ? array_map('urldecode', $v['data_vpi']) : urldecode($v['data_vpi']); self::debug('save data_vpi', $urldecode); $this->cls('Metabox')->save($post_id, $name, $urldecode); $valid_i++; } unset($this->_queue[$queue_k]); self::debug('notify data handled, unset queue [q_k] ' . $queue_k); } $this->save_queue('vpi', $this->_queue); self::debug('notified'); return Cloud::ok(array('count' => $valid_i)); } /** * Cron * * @since 4.7 */ public static function cron($continue = false) { $_instance = self::cls(); return $_instance->_cron_handler($continue); } /** * Cron generation * * @since 4.7 */ private function _cron_handler($continue = false) { self::debug('cron start'); $this->_queue = $this->load_queue('vpi'); if (empty($this->_queue)) { return; } // For cron, need to check request interval too if (!$continue) { if (!empty($this->_summary['curr_request_vpi']) && time() - $this->_summary['curr_request_vpi'] < 300 && !$this->conf(self::O_DEBUG)) { self::debug('Last request not done'); return; } } $i = 0; foreach ($this->_queue as $k => $v) { if (!empty($v['_status'])) { continue; } self::debug('cron job [tag] ' . $k . ' [url] ' . $v['url'] . ($v['is_mobile'] ? ' 📱 ' : '') . ' [UA] ' . $v['user_agent']); $i++; $res = $this->_send_req($v['url'], $k, $v['user_agent'], $v['is_mobile']); if (!$res) { // Status is wrong, drop this this->_queue $this->_queue = $this->load_queue('vpi'); unset($this->_queue[$k]); $this->save_queue('vpi', $this->_queue); if (!$continue) { return; } // if ( $i > 3 ) { GUI::print_loading(count($this->_queue), 'VPI'); return Router::self_redirect(Router::ACTION_VPI, self::TYPE_GEN); // } continue; } // Exit queue if out of quota or service is hot if ($res === 'out_of_quota' || $res === 'svc_hot') { return; } $this->_queue = $this->load_queue('vpi'); $this->_queue[$k]['_status'] = 'requested'; $this->save_queue('vpi', $this->_queue); self::debug('Saved to queue [k] ' . $k); // only request first one if (!$continue) { return; } // if ( $i > 3 ) { GUI::print_loading(count($this->_queue), 'VPI'); return Router::self_redirect(Router::ACTION_VPI, self::TYPE_GEN); // } } } /** * Send to QC API to generate VPI * * @since 4.7 * @access private */ private function _send_req($request_url, $queue_k, $user_agent, $is_mobile) { $svc = Cloud::SVC_VPI; // Check if has credit to push or not $err = false; $allowance = $this->cls('Cloud')->allowance($svc, $err); if (!$allowance) { self::debug('❌ No credit: ' . $err); $err && Admin_Display::error(Error::msg($err)); return 'out_of_quota'; } set_time_limit(120); // Update css request status self::save_summary(array('curr_request_vpi' => time()), true); // Gather guest HTML to send $html = $this->cls('CSS')->prepare_html($request_url, $user_agent); if (!$html) { return false; } // Parse HTML to gather all CSS content before requesting $css = false; list($css, $html) = $this->cls('CSS')->prepare_css($html); if (!$css) { self::debug('❌ No css'); return false; } $data = array( 'url' => $request_url, 'queue_k' => $queue_k, 'user_agent' => $user_agent, 'is_mobile' => $is_mobile ? 1 : 0, // todo:compatible w/ tablet 'html' => $html, 'css' => $css, ); self::debug('Generating: ', $data); $json = Cloud::post($svc, $data, 30); if (!is_array($json)) { return $json; } // Unknown status, remove this line if ($json['status'] != 'queued') { return false; } // Save summary data self::reload_summary(); $this->_summary['last_spent_vpi'] = time() - $this->_summary['curr_request_vpi']; $this->_summary['last_request_vpi'] = $this->_summary['curr_request_vpi']; $this->_summary['curr_request_vpi'] = 0; self::save_summary(); return true; } /** * Handle all request actions from main cls * * @since 4.7 */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_GEN: self::cron(true); break; case self::TYPE_CLEAR_Q: $this->clear_q('vpi'); break; default: break; } Admin::redirect(); } } crawler.cls.php 0000644 00000121213 15162226320 0007473 0 ustar 00 <?php /** * The crawler class * * @since 1.1.0 */ namespace LiteSpeed; defined('WPINC') || exit(); class Crawler extends Root { const LOG_TAG = '🕸️'; const TYPE_REFRESH_MAP = 'refresh_map'; const TYPE_EMPTY = 'empty'; const TYPE_BLACKLIST_EMPTY = 'blacklist_empty'; const TYPE_BLACKLIST_DEL = 'blacklist_del'; const TYPE_BLACKLIST_ADD = 'blacklist_add'; const TYPE_START = 'start'; const TYPE_RESET = 'reset'; const USER_AGENT = 'lscache_walker'; const FAST_USER_AGENT = 'lscache_runner'; const CHUNKS = 10000; const STATUS_WAIT = 'W'; const STATUS_HIT = 'H'; const STATUS_MISS = 'M'; const STATUS_BLACKLIST = 'B'; const STATUS_NOCACHE = 'N'; private $_sitemeta = 'meta.data'; private $_resetfile; private $_end_reason; private $_ncpu = 1; private $_server_ip; private $_crawler_conf = array( 'cookies' => array(), 'headers' => array(), 'ua' => '', ); private $_crawlers = array(); private $_cur_threads = -1; private $_max_run_time; private $_cur_thread_time; private $_map_status_list = array( 'H' => array(), 'M' => array(), 'B' => array(), 'N' => array(), ); protected $_summary; /** * Initialize crawler, assign sitemap path * * @since 1.1.0 */ public function __construct() { if (is_multisite()) { $this->_sitemeta = 'meta' . get_current_blog_id() . '.data'; } $this->_resetfile = LITESPEED_STATIC_DIR . '/crawler/' . $this->_sitemeta . '.reset'; $this->_summary = self::get_summary(); $this->_ncpu = $this->_get_server_cpu(); $this->_server_ip = $this->conf(Base::O_SERVER_IP); self::debug('Init w/ CPU cores=' . $this->_ncpu); } /** * Try get server CPUs * @since 5.2 */ private function _get_server_cpu() { $cpuinfo_file = '/proc/cpuinfo'; $setting_open_dir = ini_get('open_basedir'); if ($setting_open_dir) { return 1; } // Server has limit try { if (!@is_file($cpuinfo_file)) { return 1; } } catch (\Exception $e) { return 1; } $cpuinfo = file_get_contents($cpuinfo_file); preg_match_all('/^processor/m', $cpuinfo, $matches); return count($matches[0]) ?: 1; } /** * Check whether the current crawler is active/runable/useable/enabled/want it to work or not * * @since 4.3 */ public function is_active($curr) { $bypass_list = self::get_option('bypass_list', array()); return !in_array($curr, $bypass_list); } /** * Toggle the current crawler's activeness state, i.e., runable/useable/enabled/want it to work or not, and return the updated state * * @since 4.3 */ public function toggle_activeness($curr) { // param type: int $bypass_list = self::get_option('bypass_list', array()); if (in_array($curr, $bypass_list)) { // when the ith opt was off / in the bypassed list, turn it on / remove it from the list unset($bypass_list[array_search($curr, $bypass_list)]); $bypass_list = array_values($bypass_list); self::update_option('bypass_list', $bypass_list); return true; } else { // when the ith opt was on / not in the bypassed list, turn it off / add it to the list $bypass_list[] = (int) $curr; self::update_option('bypass_list', $bypass_list); return false; } } /** * Clear bypassed list * * @since 4.3 * @access public */ public function clear_disabled_list() { self::update_option('bypass_list', array()); $msg = __('Crawler disabled list is cleared! All crawlers are set to active! ', 'litespeed-cache'); Admin_Display::note($msg); self::debug('All crawlers are set to active...... '); } /** * Overwrite get_summary to init elements * * @since 3.0 * @access public */ public static function get_summary($field = false) { $_default = array( 'list_size' => 0, 'last_update_time' => 0, 'curr_crawler' => 0, 'curr_crawler_beginning_time' => 0, 'last_pos' => 0, 'last_count' => 0, 'last_crawled' => 0, 'last_start_time' => 0, 'last_status' => '', 'is_running' => 0, 'end_reason' => '', 'meta_save_time' => 0, 'pos_reset_check' => 0, 'done' => 0, 'this_full_beginning_time' => 0, 'last_full_time_cost' => 0, 'last_crawler_total_cost' => 0, 'crawler_stats' => array(), // this will store all crawlers hit/miss crawl status ); wp_cache_delete('alloptions', 'options'); // ensure the summary is current $summary = parent::get_summary(); $summary = array_merge($_default, $summary); if (!$field) { return $summary; } if (array_key_exists($field, $summary)) { return $summary[$field]; } return null; } /** * Overwrite save_summary * * @since 3.0 * @access public */ public static function save_summary($data = false, $reload = false, $overwrite = false) { $instance = self::cls(); $instance->_summary['meta_save_time'] = time(); if (!$data) { $data = $instance->_summary; } parent::save_summary($data, $reload, $overwrite); File::save(LITESPEED_STATIC_DIR . '/crawler/' . $instance->_sitemeta, \json_encode($data), true); } /** * Cron start async crawling * * @since 5.5 */ public static function start_async_cron() { Task::async_call('crawler'); } /** * Manually start async crawling * * @since 5.5 */ public static function start_async() { Task::async_call('crawler_force'); $msg = __('Started async crawling', 'litespeed-cache'); Admin_Display::success($msg); } /** * Ajax crawl handler * * @since 5.5 */ public static function async_handler($manually_run = false) { self::debug('------------async-------------start_async_handler'); // check_ajax_referer('async_crawler', 'nonce'); self::start($manually_run); } /** * Proceed crawling * * @since 1.1.0 * @access public */ public static function start($manually_run = false) { if (!Router::can_crawl()) { self::debug('......crawler is NOT allowed by the server admin......'); return false; } if ($manually_run) { self::debug('......crawler manually ran......'); } self::cls()->_crawl_data($manually_run); } /** * Crawling start * * @since 1.1.0 * @access private */ private function _crawl_data($manually_run) { if (!defined('LITESPEED_LANE_HASH')) { define('LITESPEED_LANE_HASH', Str::rrand(8)); } if ($this->_check_valid_lane()) { $this->_take_over_lane(); } else { self::debug('⚠️ lane in use'); return; // if ($manually_run) { // self::debug('......crawler started (manually_rund)......'); // // Log pid to prevent from multi running // if (defined('LITESPEED_CLI')) { // // Take over lane // self::debug('⚠️⚠️⚠️ Forced take over lane (CLI)'); // $this->_take_over_lane(); // } // } } self::debug('......crawler started......'); // for the first time running if (!$this->_summary || !Data::cls()->tb_exist('crawler') || !Data::cls()->tb_exist('crawler_blacklist')) { $this->cls('Crawler_Map')->gen(); } // if finished last time, regenerate sitemap if ($this->_summary['done'] === 'touchedEnd') { // check whole crawling interval $last_finished_at = $this->_summary['last_full_time_cost'] + $this->_summary['this_full_beginning_time']; if (!$manually_run && time() - $last_finished_at < $this->conf(Base::O_CRAWLER_CRAWL_INTERVAL)) { self::debug('Cron abort: cache warmed already.'); // if not reach whole crawling interval, exit $this->Release_lane(); return; } self::debug('TouchedEnd. regenerate sitemap....'); $this->cls('Crawler_Map')->gen(); } $this->list_crawlers(); // Skip the crawlers that in bypassed list while (!$this->is_active($this->_summary['curr_crawler']) && $this->_summary['curr_crawler'] < count($this->_crawlers)) { self::debug('Skipped the Crawler #' . $this->_summary['curr_crawler'] . ' ......'); $this->_summary['curr_crawler']++; } if ($this->_summary['curr_crawler'] >= count($this->_crawlers)) { $this->_end_reason = 'end'; $this->_terminate_running(); $this->Release_lane(); return; } // In case crawlers are all done but not reload, reload it if (empty($this->_summary['curr_crawler']) || empty($this->_crawlers[$this->_summary['curr_crawler']])) { $this->_summary['curr_crawler'] = 0; $this->_summary['crawler_stats'][$this->_summary['curr_crawler']] = array(); } $res = $this->load_conf(); if (!$res) { self::debug('Load conf failed'); $this->_terminate_running(); $this->Release_lane(); return; } try { $this->_engine_start(); $this->Release_lane(); } catch (\Exception $e) { self::debug('🛑 ' . $e->getMessage()); } } /** * Load conf before running crawler * * @since 3.0 * @access private */ private function load_conf() { $this->_crawler_conf['base'] = home_url(); $current_crawler = $this->_crawlers[$this->_summary['curr_crawler']]; /** * Check cookie crawler * @since 2.8 */ foreach ($current_crawler as $k => $v) { if (strpos($k, 'cookie:') !== 0) { continue; } if ($v == '_null') { continue; } $this->_crawler_conf['cookies'][substr($k, 7)] = $v; } /** * Set WebP simulation * @since 1.9.1 */ if (!empty($current_crawler['webp'])) { $this->_crawler_conf['headers'][] = 'Accept: image/' . ($this->conf(Base::O_IMG_OPTM_WEBP) == 2 ? 'avif' : 'webp') . ',*/*'; } /** * Set mobile crawler * @since 2.8 */ if (!empty($current_crawler['mobile'])) { $this->_crawler_conf['ua'] = 'Mobile iPhone'; } /** * Limit delay to use server setting * @since 1.8.3 */ $this->_crawler_conf['run_delay'] = 500; // microseconds if (defined('LITESPEED_CRAWLER_USLEEP') && LITESPEED_CRAWLER_USLEEP > $this->_crawler_conf['run_delay']) { $this->_crawler_conf['run_delay'] = LITESPEED_CRAWLER_USLEEP; } if (!empty($_SERVER[Base::ENV_CRAWLER_USLEEP]) && $_SERVER[Base::ENV_CRAWLER_USLEEP] > $this->_crawler_conf['run_delay']) { $this->_crawler_conf['run_delay'] = $_SERVER[Base::ENV_CRAWLER_USLEEP]; } $this->_crawler_conf['run_duration'] = $this->get_crawler_duration(); $this->_crawler_conf['load_limit'] = $this->conf(Base::O_CRAWLER_LOAD_LIMIT); if (!empty($_SERVER[Base::ENV_CRAWLER_LOAD_LIMIT_ENFORCE])) { $this->_crawler_conf['load_limit'] = $_SERVER[Base::ENV_CRAWLER_LOAD_LIMIT_ENFORCE]; } elseif (!empty($_SERVER[Base::ENV_CRAWLER_LOAD_LIMIT]) && $_SERVER[Base::ENV_CRAWLER_LOAD_LIMIT] < $this->_crawler_conf['load_limit']) { $this->_crawler_conf['load_limit'] = $_SERVER[Base::ENV_CRAWLER_LOAD_LIMIT]; } if ($this->_crawler_conf['load_limit'] == 0) { self::debug('🛑 Terminated crawler due to load limit set to 0'); return false; } /** * Set role simulation * @since 1.9.1 */ if (!empty($current_crawler['uid'])) { if (!$this->_server_ip) { self::debug('🛑 Terminated crawler due to Server IP not set'); return false; } // Get role simulation vary name $vary_name = $this->cls('Vary')->get_vary_name(); $vary_val = $this->cls('Vary')->finalize_default_vary($current_crawler['uid']); $this->_crawler_conf['cookies'][$vary_name] = $vary_val; $this->_crawler_conf['cookies']['litespeed_hash'] = Router::cls()->get_hash($current_crawler['uid']); } return true; } /** * Get crawler duration allowance * * @since 7.0 */ public function get_crawler_duration() { $RUN_DURATION = defined('LITESPEED_CRAWLER_DURATION') ? LITESPEED_CRAWLER_DURATION : 900; if ($RUN_DURATION > 900) { $RUN_DURATION = 900; // reset to default value if defined in conf file is higher than 900 seconds for security enhancement } return $RUN_DURATION; } /** * Start crawler * * @since 1.1.0 * @access private */ private function _engine_start() { // check if is running // if ($this->_summary['is_running'] && time() - $this->_summary['is_running'] < $this->_crawler_conf['run_duration']) { // $this->_end_reason = 'stopped'; // self::debug('The crawler is running.'); // return; // } // check current load $this->_adjust_current_threads(); if ($this->_cur_threads == 0) { $this->_end_reason = 'stopped_highload'; self::debug('Stopped due to heavy load.'); return; } // log started time self::save_summary(array('last_start_time' => time())); // set time limit $maxTime = (int) ini_get('max_execution_time'); self::debug('ini_get max_execution_time=' . $maxTime); if ($maxTime == 0) { $maxTime = 300; // hardlimit } else { $maxTime -= 5; } if ($maxTime >= $this->_crawler_conf['run_duration']) { $maxTime = $this->_crawler_conf['run_duration']; self::debug('Use run_duration setting as max_execution_time=' . $maxTime); } elseif (ini_set('max_execution_time', $this->_crawler_conf['run_duration'] + 15) !== false) { $maxTime = $this->_crawler_conf['run_duration']; self::debug('ini_set max_execution_time=' . $maxTime); } self::debug('final max_execution_time=' . $maxTime); $this->_max_run_time = $maxTime + time(); // mark running $this->_prepare_running(); // run crawler $this->_do_running(); $this->_terminate_running(); } /** * Get server load * * @since 5.5 */ public function get_server_load() { /** * If server is windows, exit * @see https://wordpress.org/support/topic/crawler-keeps-causing-crashes/ */ if (!function_exists('sys_getloadavg')) { return -1; } $curload = sys_getloadavg(); $curload = $curload[0]; self::debug('Server load: ' . $curload); return $curload; } /** * Adjust threads dynamically * * @since 1.1.0 * @access private */ private function _adjust_current_threads() { $curload = $this->get_server_load(); if ($curload == -1) { self::debug('set threads=0 due to func sys_getloadavg not exist!'); $this->_cur_threads = 0; return; } $curload /= $this->_ncpu; // $curload = 1; $CRAWLER_THREADS = defined('LITESPEED_CRAWLER_THREADS') ? LITESPEED_CRAWLER_THREADS : 3; if ($this->_cur_threads == -1) { // init if ($curload > $this->_crawler_conf['load_limit']) { $curthreads = 0; } elseif ($curload >= $this->_crawler_conf['load_limit'] - 1) { $curthreads = 1; } else { $curthreads = intval($this->_crawler_conf['load_limit'] - $curload); if ($curthreads > $CRAWLER_THREADS) { $curthreads = $CRAWLER_THREADS; } } } else { // adjust $curthreads = $this->_cur_threads; if ($curload >= $this->_crawler_conf['load_limit'] + 1) { sleep(5); // sleep 5 secs if ($curthreads >= 1) { $curthreads--; } } elseif ($curload >= $this->_crawler_conf['load_limit']) { // if ( $curthreads > 1 ) {// if already 1, keep $curthreads--; // } } elseif ($curload + 1 < $this->_crawler_conf['load_limit']) { if ($curthreads < $CRAWLER_THREADS) { $curthreads++; } } } // $log = 'set current threads = ' . $curthreads . ' previous=' . $this->_cur_threads // . ' max_allowed=' . $CRAWLER_THREADS . ' load_limit=' . $this->_crawler_conf[ 'load_limit' ] . ' current_load=' . $curload; $this->_cur_threads = $curthreads; $this->_cur_thread_time = time(); } /** * Mark running status * * @since 1.1.0 * @access private */ private function _prepare_running() { $this->_summary['is_running'] = time(); $this->_summary['done'] = 0; // reset done status $this->_summary['last_status'] = 'prepare running'; $this->_summary['last_crawled'] = 0; // Current crawler starttime mark if ($this->_summary['last_pos'] == 0) { $this->_summary['curr_crawler_beginning_time'] = time(); } if ($this->_summary['curr_crawler'] == 0 && $this->_summary['last_pos'] == 0) { $this->_summary['this_full_beginning_time'] = time(); $this->_summary['list_size'] = $this->cls('Crawler_Map')->count_map(); } if ($this->_summary['end_reason'] == 'end' && $this->_summary['last_pos'] == 0) { $this->_summary['crawler_stats'][$this->_summary['curr_crawler']] = array(); } self::save_summary(); } /** * Take over lane * @since 6.1 */ private function _take_over_lane() { self::debug('Take over lane as lane is free: ' . $this->json_local_path() . '.pid'); file::save($this->json_local_path() . '.pid', LITESPEED_LANE_HASH); } /** * Update lane file * @since 6.1 */ private function _touch_lane() { touch($this->json_local_path() . '.pid'); } /** * Release lane file * @since 6.1 */ public function Release_lane() { $lane_file = $this->json_local_path() . '.pid'; if (!file_exists($lane_file)) { return; } self::debug('Release lane'); unlink($lane_file); } /** * Check if lane is used by other crawlers * @since 6.1 */ private function _check_valid_lane($strict_mode = false) { // Check lane hash $lane_file = $this->json_local_path() . '.pid'; if ($strict_mode) { if (!file_exists($lane_file)) { self::debug("lane file not existed, strict mode is false [file] $lane_file"); return false; } } $pid = file::read($lane_file); if ($pid && LITESPEED_LANE_HASH != $pid) { // If lane file is older than 1h, ignore if (time() - filemtime($lane_file) > 3600) { self::debug('Lane file is older than 1h, releasing lane'); $this->Release_lane(); return true; } return false; } return true; } /** * Test port for simulator * * @since 7.0 * @access private * @return bool true if success and can continue crawling, false if failed and need to stop */ private function _test_port() { if (empty($this->_crawler_conf['cookies']) || empty($this->_crawler_conf['cookies']['litespeed_hash'])) { return true; } if (!$this->_server_ip) { self::debug('❌ Server IP not set'); return false; } if (defined('LITESPEED_CRAWLER_LOCAL_PORT')) { self::debug('✅ LITESPEED_CRAWLER_LOCAL_PORT already defined'); return true; } // Don't repeat testing in 120s if (!empty($this->_summary['test_port_tts']) && time() - $this->_summary['test_port_tts'] < 120) { if (!empty($this->_summary['test_port'])) { self::debug('✅ Use tested local port: ' . $this->_summary['test_port']); define('LITESPEED_CRAWLER_LOCAL_PORT', $this->_summary['test_port']); return true; } return false; } $this->_summary['test_port_tts'] = time(); self::save_summary(); $options = $this->_get_curl_options(); $home = home_url(); File::save(LITESPEED_STATIC_DIR . '/crawler/test_port.txt', $home, true); $url = LITESPEED_STATIC_URL . '/crawler/test_port.txt'; $parsed_url = parse_url($url); if (empty($parsed_url['host'])) { self::debug('❌ Test port failed, invalid URL: ' . $url); return false; } $resolved = $parsed_url['host'] . ':443:' . $this->_server_ip; $options[CURLOPT_RESOLVE] = array($resolved); $options[CURLOPT_DNS_USE_GLOBAL_CACHE] = false; $options[CURLOPT_HEADER] = false; self::debug('Test local 443 port for ' . $resolved); $ch = curl_init(); curl_setopt_array($ch, $options); curl_setopt($ch, CURLOPT_URL, $url); $result = curl_exec($ch); $test_result = false; if (curl_errno($ch) || $result !== $home) { if (curl_errno($ch)) { self::debug('❌ Test port curl error: [errNo] ' . curl_errno($ch) . ' [err] ' . curl_error($ch)); } elseif ($result !== $home) { self::debug('❌ Test port response is wrong: ' . $result); } self::debug('❌ Test local 443 port failed, try port 80'); // Try port 80 $resolved = $parsed_url['host'] . ':80:' . $this->_server_ip; $options[CURLOPT_RESOLVE] = array($resolved); $url = str_replace('https://', 'http://', $url); if (!in_array('X-Forwarded-Proto: https', $options[CURLOPT_HTTPHEADER])) { $options[CURLOPT_HTTPHEADER][] = 'X-Forwarded-Proto: https'; } // $options[CURLOPT_HTTPHEADER][] = 'X-Forwarded-SSL: on'; $ch = curl_init(); curl_setopt_array($ch, $options); curl_setopt($ch, CURLOPT_URL, $url); $result = curl_exec($ch); if (curl_errno($ch)) { self::debug('❌ Test port curl error: [errNo] ' . curl_errno($ch) . ' [err] ' . curl_error($ch)); } elseif ($result !== $home) { self::debug('❌ Test port response is wrong: ' . $result); } else { self::debug('✅ Test local 80 port successfully'); define('LITESPEED_CRAWLER_LOCAL_PORT', 80); $this->_summary['test_port'] = 80; $test_result = true; } // self::debug('Response data: ' . $result); // $this->Release_lane(); // exit($result); } else { self::debug('✅ Tested local 443 port successfully'); define('LITESPEED_CRAWLER_LOCAL_PORT', 443); $this->_summary['test_port'] = 443; $test_result = true; } self::save_summary(); curl_close($ch); return $test_result; } /** * Run crawler * * @since 1.1.0 * @access private */ private function _do_running() { $options = $this->_get_curl_options(true); // If is role simulator and not defined local port, check port once $test_result = $this->_test_port(); if (!$test_result) { $this->_end_reason = 'port_test_failed'; self::debug('❌ Test port failed, crawler stopped.'); return; } while ($urlChunks = $this->cls('Crawler_Map')->list_map(self::CHUNKS, $this->_summary['last_pos'])) { // self::debug('$urlChunks=' . count($urlChunks) . ' $this->_cur_threads=' . $this->_cur_threads); // start crawling $urlChunks = array_chunk($urlChunks, $this->_cur_threads); // self::debug('$urlChunks after array_chunk: ' . count($urlChunks)); foreach ($urlChunks as $rows) { if (!$this->_check_valid_lane(true)) { $this->_end_reason = 'lane_invalid'; self::debug('🛑 The crawler lane is used by newer crawler.'); throw new \Exception('invalid crawler lane'); } // Update time $this->_touch_lane(); // self::debug('chunk fetching count($rows)= ' . count($rows)); // multi curl $rets = $this->_multi_request($rows, $options); // check result headers foreach ($rows as $row) { // self::debug('chunk fetching 553'); if (empty($rets[$row['id']])) { // If already in blacklist, no curl happened, no corresponding record continue; } // self::debug('chunk fetching 557'); // check response if ($rets[$row['id']]['code'] == 428) { // HTTP/1.1 428 Precondition Required (need to test) $this->_end_reason = 'crawler_disabled'; self::debug('crawler_disabled'); return; } $status = $this->_status_parse($rets[$row['id']]['header'], $rets[$row['id']]['code'], $row['url']); // B or H or M or N(nocache) self::debug('[status] ' . $this->_status2title($status) . "\t\t [url] " . $row['url']); $this->_map_status_list[$status][$row['id']] = array( 'url' => $row['url'], 'code' => $rets[$row['id']]['code'], // 201 or 200 or 404 ); if (empty($this->_summary['crawler_stats'][$this->_summary['curr_crawler']][$status])) { $this->_summary['crawler_stats'][$this->_summary['curr_crawler']][$status] = 0; } $this->_summary['crawler_stats'][$this->_summary['curr_crawler']][$status]++; } // update offset position $_time = time(); $this->_summary['last_count'] = count($rows); $this->_summary['last_pos'] += $this->_summary['last_count']; $this->_summary['last_crawled'] += $this->_summary['last_count']; $this->_summary['last_update_time'] = $_time; $this->_summary['last_status'] = 'updated position'; // self::debug("chunk fetching 604 last_pos:{$this->_summary['last_pos']} last_count:{$this->_summary['last_count']} last_crawled:{$this->_summary['last_crawled']}"); // check duration if ($this->_summary['last_update_time'] > $this->_max_run_time) { $this->_end_reason = 'stopped_maxtime'; self::debug('Terminated due to maxtime'); return; // return __('Stopped due to exceeding defined Maximum Run Time', 'litespeed-cache'); } // make sure at least each 10s save meta & map status once if ($_time - $this->_summary['meta_save_time'] > 10) { $this->_map_status_list = $this->cls('Crawler_Map')->save_map_status($this->_map_status_list, $this->_summary['curr_crawler']); self::save_summary(); } // self::debug('chunk fetching 597'); // check if need to reset pos each 5s if ($_time > $this->_summary['pos_reset_check']) { $this->_summary['pos_reset_check'] = $_time + 5; if (file_exists($this->_resetfile) && unlink($this->_resetfile)) { self::debug('Terminated due to reset file'); $this->_summary['last_pos'] = 0; $this->_summary['curr_crawler'] = 0; $this->_summary['crawler_stats'][$this->_summary['curr_crawler']] = array(); // reset done status $this->_summary['done'] = 0; $this->_summary['this_full_beginning_time'] = 0; $this->_end_reason = 'stopped_reset'; return; // return __('Stopped due to reset meta position', 'litespeed-cache'); } } // self::debug('chunk fetching 615'); // check loads if ($this->_summary['last_update_time'] - $this->_cur_thread_time > 60) { $this->_adjust_current_threads(); if ($this->_cur_threads == 0) { $this->_end_reason = 'stopped_highload'; self::debug('🛑 Terminated due to highload'); return; // return __('Stopped due to load over limit', 'litespeed-cache'); } } $this->_summary['last_status'] = 'sleeping ' . $this->_crawler_conf['run_delay'] . 'ms'; usleep($this->_crawler_conf['run_delay']); } // self::debug('chunk fetching done'); } // All URLs are done for current crawler $this->_end_reason = 'end'; $this->_summary['crawler_stats'][$this->_summary['curr_crawler']]['W'] = 0; self::debug('Crawler #' . $this->_summary['curr_crawler'] . ' touched end'); } /** * Send multi curl requests * If res=B, bypass request and won't return * * @since 1.1.0 * @access private */ private function _multi_request($rows, $options) { if (!function_exists('curl_multi_init')) { exit('curl_multi_init disabled'); } $mh = curl_multi_init(); $CRAWLER_DROP_DOMAIN = defined('LITESPEED_CRAWLER_DROP_DOMAIN') ? LITESPEED_CRAWLER_DROP_DOMAIN : false; $curls = array(); foreach ($rows as $row) { if (substr($row['res'], $this->_summary['curr_crawler'], 1) == self::STATUS_BLACKLIST) { continue; } if (substr($row['res'], $this->_summary['curr_crawler'], 1) == self::STATUS_NOCACHE) { continue; } if (!function_exists('curl_init')) { exit('curl_init disabled'); } $curls[$row['id']] = curl_init(); // Append URL $url = $row['url']; if ($CRAWLER_DROP_DOMAIN) { $url = $this->_crawler_conf['base'] . $row['url']; } // IP resolve if (!empty($this->_crawler_conf['cookies']) && !empty($this->_crawler_conf['cookies']['litespeed_hash'])) { $parsed_url = parse_url($url); // self::debug('Crawl role simulator, required to use localhost for resolve'); if (!empty($parsed_url['host'])) { $dom = $parsed_url['host']; $port = defined('LITESPEED_CRAWLER_LOCAL_PORT') ? LITESPEED_CRAWLER_LOCAL_PORT : '443'; $resolved = $dom . ':' . $port . ':' . $this->_server_ip; $options[CURLOPT_RESOLVE] = array($resolved); $options[CURLOPT_DNS_USE_GLOBAL_CACHE] = false; // $options[CURLOPT_PORT] = $port; if ($port == 80) { $url = str_replace('https://', 'http://', $url); if (!in_array('X-Forwarded-Proto: https', $options[CURLOPT_HTTPHEADER])) { $options[CURLOPT_HTTPHEADER][] = 'X-Forwarded-Proto: https'; } } self::debug('Resolved DNS for ' . $resolved); } } curl_setopt($curls[$row['id']], CURLOPT_URL, $url); self::debug('Crawling [url] ' . $url . ($url == $row['url'] ? '' : ' [ori] ' . $row['url'])); curl_setopt_array($curls[$row['id']], $options); curl_multi_add_handle($mh, $curls[$row['id']]); } // execute curl if ($curls) { do { $status = curl_multi_exec($mh, $active); if ($active) { curl_multi_select($mh); } } while ($active && $status == CURLM_OK); } // curl done $ret = array(); foreach ($rows as $row) { if (substr($row['res'], $this->_summary['curr_crawler'], 1) == self::STATUS_BLACKLIST) { continue; } if (substr($row['res'], $this->_summary['curr_crawler'], 1) == self::STATUS_NOCACHE) { continue; } // self::debug('-----debug3'); $ch = $curls[$row['id']]; // Parse header $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $content = curl_multi_getcontent($ch); $header = substr($content, 0, $header_size); $ret[$row['id']] = array( 'header' => $header, 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), ); // self::debug('-----debug4'); curl_multi_remove_handle($mh, $ch); curl_close($ch); } // self::debug('-----debug5'); curl_multi_close($mh); // self::debug('-----debug6'); return $ret; } /** * Translate the status to title * @since 6.0 */ private function _status2title($status) { if ($status == self::STATUS_HIT) { return '✅ Hit'; } if ($status == self::STATUS_MISS) { return '😊 Miss'; } if ($status == self::STATUS_BLACKLIST) { return '😅 Blacklisted'; } if ($status == self::STATUS_NOCACHE) { return '😅 Blacklisted'; } return '🛸 Unknown'; } /** * Check returned curl header to find if cached or not * * @since 2.0 * @access private */ private function _status_parse($header, $code, $url) { // self::debug('http status code: ' . $code . ' [headers]', $header); if ($code == 201) { return self::STATUS_HIT; } if (stripos($header, 'X-Litespeed-Cache-Control: no-cache') !== false) { // If is from DIVI, taken as miss if (defined('LITESPEED_CRAWLER_IGNORE_NONCACHEABLE') && LITESPEED_CRAWLER_IGNORE_NONCACHEABLE) { return self::STATUS_MISS; } // If blacklist is disabled if ((defined('LITESPEED_CRAWLER_DISABLE_BLOCKLIST') && LITESPEED_CRAWLER_DISABLE_BLOCKLIST) || apply_filters('litespeed_crawler_disable_blocklist', false, $url)) { return self::STATUS_MISS; } return self::STATUS_NOCACHE; // Blacklist } $_cache_headers = array('x-qc-cache', 'x-lsadc-cache', 'x-litespeed-cache'); foreach ($_cache_headers as $_header) { if (stripos($header, $_header) !== false) { if (stripos($header, $_header . ': miss') !== false) { return self::STATUS_MISS; // Miss } return self::STATUS_HIT; // Hit } } // If blacklist is disabled if ((defined('LITESPEED_CRAWLER_DISABLE_BLOCKLIST') && LITESPEED_CRAWLER_DISABLE_BLOCKLIST) || apply_filters('litespeed_crawler_disable_blocklist', false, $url)) { return self::STATUS_MISS; } return self::STATUS_BLACKLIST; // Blacklist } /** * Get curl_options * * @since 1.1.0 * @access private */ private function _get_curl_options($crawler_only = false) { $CRAWLER_TIMEOUT = defined('LITESPEED_CRAWLER_TIMEOUT') ? LITESPEED_CRAWLER_TIMEOUT : 30; $options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_FOLLOWLOCATION => false, CURLOPT_ENCODING => 'gzip', CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => $CRAWLER_TIMEOUT, // Larger timeout to avoid incorrect blacklist addition #900171 CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_NOBODY => false, CURLOPT_HTTPHEADER => $this->_crawler_conf['headers'], ); $options[CURLOPT_HTTPHEADER][] = 'Cache-Control: max-age=0'; /** * Try to enable http2 connection (only available since PHP7+) * @since 1.9.1 * @since 2.2.7 Commented due to cause no-cache issue * @since 2.9.1+ Fixed wrongly usage of CURL_HTTP_VERSION_1_1 const */ $options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1; // $options[ CURL_HTTP_VERSION_2 ] = 1; // if is walker // $options[ CURLOPT_FRESH_CONNECT ] = true; // Referer if (isset($_SERVER['HTTP_HOST']) && isset($_SERVER['REQUEST_URI'])) { $options[CURLOPT_REFERER] = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; } // User Agent if ($crawler_only) { if (strpos($this->_crawler_conf['ua'], Crawler::FAST_USER_AGENT) !== 0) { $this->_crawler_conf['ua'] = Crawler::FAST_USER_AGENT . ' ' . $this->_crawler_conf['ua']; } } $options[CURLOPT_USERAGENT] = $this->_crawler_conf['ua']; // Cookies $cookies = array(); foreach ($this->_crawler_conf['cookies'] as $k => $v) { if (!$v) { continue; } $cookies[] = $k . '=' . urlencode($v); } if ($cookies) { $options[CURLOPT_COOKIE] = implode('; ', $cookies); } return $options; } /** * Self curl to get HTML content * * @since 3.3 */ public function self_curl($url, $ua, $uid = false, $accept = false) { // $accept not in use yet $this->_crawler_conf['base'] = home_url(); $this->_crawler_conf['ua'] = $ua; if ($accept) { $this->_crawler_conf['headers'] = array('Accept: ' . $accept); } $options = $this->_get_curl_options(); if ($uid) { $this->_crawler_conf['cookies']['litespeed_flash_hash'] = Router::cls()->get_flash_hash($uid); $parsed_url = parse_url($url); if (!empty($parsed_url['host'])) { $dom = $parsed_url['host']; $port = defined('LITESPEED_CRAWLER_LOCAL_PORT') ? LITESPEED_CRAWLER_LOCAL_PORT : '443'; $resolved = $dom . ':' . $port . ':' . $this->_server_ip; $options[CURLOPT_RESOLVE] = array($resolved); $options[CURLOPT_DNS_USE_GLOBAL_CACHE] = false; $options[CURLOPT_PORT] = $port; self::debug('Resolved DNS for ' . $resolved); } } $options[CURLOPT_HEADER] = false; $options[CURLOPT_FOLLOWLOCATION] = true; $ch = curl_init(); curl_setopt_array($ch, $options); curl_setopt($ch, CURLOPT_URL, $url); $result = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($code != 200) { self::debug('❌ Response code is not 200 in self_curl() [code] ' . var_export($code, true)); return false; } return $result; } /** * Terminate crawling * * @since 1.1.0 * @access private */ private function _terminate_running() { $this->_map_status_list = $this->cls('Crawler_Map')->save_map_status($this->_map_status_list, $this->_summary['curr_crawler']); if ($this->_end_reason == 'end') { // Current crawler is fully done // $end_reason = sprintf( __( 'Crawler %s reached end of sitemap file.', 'litespeed-cache' ), '#' . ( $this->_summary['curr_crawler'] + 1 ) ); $this->_summary['curr_crawler']++; // Jump to next crawler // $this->_summary[ 'crawler_stats' ][ $this->_summary[ 'curr_crawler' ] ] = array(); // reset this at next crawl time $this->_summary['last_pos'] = 0; // reset last position $this->_summary['last_crawler_total_cost'] = time() - $this->_summary['curr_crawler_beginning_time']; $count_crawlers = count($this->list_crawlers()); if ($this->_summary['curr_crawler'] >= $count_crawlers) { self::debug('_terminate_running Touched end, whole crawled. Reload crawler!'); $this->_summary['curr_crawler'] = 0; // $this->_summary[ 'crawler_stats' ][ $this->_summary[ 'curr_crawler' ] ] = array(); $this->_summary['done'] = 'touchedEnd'; // log done status $this->_summary['last_full_time_cost'] = time() - $this->_summary['this_full_beginning_time']; } } $this->_summary['last_status'] = 'stopped'; $this->_summary['is_running'] = 0; $this->_summary['end_reason'] = $this->_end_reason; self::save_summary(); } /** * List all crawlers ( tagA => [ valueA => titleA, ... ] ...) * * @since 1.9.1 * @access public */ public function list_crawlers() { if ($this->_crawlers) { return $this->_crawlers; } $crawler_factors = array(); // Add default Guest crawler $crawler_factors['uid'] = array(0 => __('Guest', 'litespeed-cache')); // WebP on/off if ($this->conf(Base::O_IMG_OPTM_WEBP)) { $crawler_factors['webp'] = array(1 => $this->cls('Media')->next_gen_image_title()); if (apply_filters('litespeed_crawler_webp', false)) { $crawler_factors['webp'][0] = ''; } } // Guest Mode on/off if ($this->conf(Base::O_GUEST)) { $vary_name = $this->cls('Vary')->get_vary_name(); $vary_val = 'guest_mode:1'; if (!defined('LSCWP_LOG')) { $vary_val = md5($this->conf(Base::HASH) . $vary_val); } $crawler_factors['cookie:' . $vary_name] = array($vary_val => '', '_null' => '<font data-balloon-pos="up" aria-label="Guest Mode">👒</font>'); } // Mobile crawler if ($this->conf(Base::O_CACHE_MOBILE)) { $crawler_factors['mobile'] = array(1 => '<font data-balloon-pos="up" aria-label="Mobile">📱</font>', 0 => ''); } // Get roles set // List all roles foreach ($this->conf(Base::O_CRAWLER_ROLES) as $v) { $role_title = ''; $udata = get_userdata($v); if (isset($udata->roles) && is_array($udata->roles)) { $tmp = array_values($udata->roles); $role_title = array_shift($tmp); } if (!$role_title) { continue; } $crawler_factors['uid'][$v] = ucfirst($role_title); } // Cookie crawler foreach ($this->conf(Base::O_CRAWLER_COOKIES) as $v) { if (empty($v['name'])) { continue; } $this_cookie_key = 'cookie:' . $v['name']; $crawler_factors[$this_cookie_key] = array(); foreach ($v['vals'] as $v2) { $crawler_factors[$this_cookie_key][$v2] = $v2 == '_null' ? '' : '<font data-balloon-pos="up" aria-label="Cookie">🍪</font>' . esc_html($v['name']) . '=' . esc_html($v2); } } // Crossing generate the crawler list $this->_crawlers = $this->_recursive_build_crawler($crawler_factors); return $this->_crawlers; } /** * Build a crawler list recursively * * @since 2.8 * @access private */ private function _recursive_build_crawler($crawler_factors, $group = array(), $i = 0) { $current_factor = array_keys($crawler_factors); $current_factor = $current_factor[$i]; $if_touch_end = $i + 1 >= count($crawler_factors); $final_list = array(); foreach ($crawler_factors[$current_factor] as $k => $v) { // Don't alter $group bcos of loop usage $item = $group; $item['title'] = !empty($group['title']) ? $group['title'] : ''; if ($v) { if ($item['title']) { $item['title'] .= ' - '; } $item['title'] .= $v; } $item[$current_factor] = $k; if ($if_touch_end) { $final_list[] = $item; } else { // Inception: next layer $final_list = array_merge($final_list, $this->_recursive_build_crawler($crawler_factors, $item, $i + 1)); } } return $final_list; } /** * Return crawler meta file local path * * @since 6.1 * @access public */ public function json_local_path() { // if (!file_exists(LITESPEED_STATIC_DIR . '/crawler/' . $this->_sitemeta)) { // return false; // } return LITESPEED_STATIC_DIR . '/crawler/' . $this->_sitemeta; } /** * Return crawler meta file * * @since 1.1.0 * @access public */ public function json_path() { if (!file_exists(LITESPEED_STATIC_DIR . '/crawler/' . $this->_sitemeta)) { return false; } return LITESPEED_STATIC_URL . '/crawler/' . $this->_sitemeta; } /** * Create reset pos file * * @since 1.1.0 * @access public */ public function reset_pos() { File::save($this->_resetfile, time(), true); self::save_summary(array('is_running' => 0)); } /** * Display status based by matching crawlers order * * @since 3.0 * @access public */ public function display_status($status_row, $reason_set) { if (!$status_row) { return ''; } $_status_list = array( '-' => 'default', self::STATUS_MISS => 'primary', self::STATUS_HIT => 'success', self::STATUS_BLACKLIST => 'danger', self::STATUS_NOCACHE => 'warning', ); $reason_set = explode(',', $reason_set); $status = ''; foreach (str_split($status_row) as $k => $v) { $reason = $reason_set[$k]; if ($reason == 'Man') { $reason = __('Manually added to blocklist', 'litespeed-cache'); } if ($reason == 'Existed') { $reason = __('Previously existed in blocklist', 'litespeed-cache'); } if ($reason) { $reason = 'data-balloon-pos="up" aria-label="' . $reason . '"'; } $status .= '<i class="litespeed-dot litespeed-bg-' . $_status_list[$v] . '" ' . $reason . '>' . ($k + 1) . '</i>'; } return $status; } /** * Output info and exit * * @since 1.1.0 * @access protected * @param string $error Error info */ protected function output($msg) { if (defined('DOING_CRON')) { echo $msg; // exit(); } else { echo "<script>alert('" . htmlspecialchars($msg) . "');</script>"; // exit; } } /** * Handle all request actions from main cls * * @since 3.0 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_REFRESH_MAP: $this->cls('Crawler_Map')->gen(true); break; case self::TYPE_EMPTY: $this->cls('Crawler_Map')->empty_map(); break; case self::TYPE_BLACKLIST_EMPTY: $this->cls('Crawler_Map')->blacklist_empty(); break; case self::TYPE_BLACKLIST_DEL: if (!empty($_GET['id'])) { $this->cls('Crawler_Map')->blacklist_del($_GET['id']); } break; case self::TYPE_BLACKLIST_ADD: if (!empty($_GET['id'])) { $this->cls('Crawler_Map')->blacklist_add($_GET['id']); } break; case self::TYPE_START: // Handle the ajax request to proceed crawler manually by admin self::start_async(); break; case self::TYPE_RESET: $this->reset_pos(); break; default: break; } Admin::redirect(); } } avatar.cls.php 0000644 00000014107 15162226321 0007316 0 ustar 00 <?php /** * The avatar cache class * * @since 3.0 * @package LiteSpeed * @subpackage LiteSpeed/inc * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Avatar extends Base { const TYPE_GENERATE = 'generate'; private $_conf_cache_ttl; private $_tb; private $_avatar_realtime_gen_dict = array(); protected $_summary; /** * Init * * @since 1.4 */ public function __construct() { if (!$this->conf(self::O_DISCUSS_AVATAR_CACHE)) { return; } Debug2::debug2('[Avatar] init'); $this->_tb = $this->cls('Data')->tb('avatar'); $this->_conf_cache_ttl = $this->conf(self::O_DISCUSS_AVATAR_CACHE_TTL); add_filter('get_avatar_url', array($this, 'crawl_avatar')); $this->_summary = self::get_summary(); } /** * Check if need db table or not * * @since 3.0 * @access public */ public function need_db() { if ($this->conf(self::O_DISCUSS_AVATAR_CACHE)) { return true; } return false; } /** * Get gravatar URL from DB and regenerate * * @since 3.0 * @access public */ public function serve_static($md5) { global $wpdb; Debug2::debug('[Avatar] is avatar request'); if (strlen($md5) !== 32) { Debug2::debug('[Avatar] wrong md5 ' . $md5); return; } $q = "SELECT url FROM `$this->_tb` WHERE md5=%s"; $url = $wpdb->get_var($wpdb->prepare($q, $md5)); if (!$url) { Debug2::debug('[Avatar] no matched url for md5 ' . $md5); return; } $url = $this->_generate($url); wp_redirect($url); exit(); } /** * Localize gravatar * * @since 3.0 * @access public */ public function crawl_avatar($url) { if (!$url) { return $url; } // Check if its already in dict or not if (!empty($this->_avatar_realtime_gen_dict[$url])) { Debug2::debug2('[Avatar] already in dict [url] ' . $url); return $this->_avatar_realtime_gen_dict[$url]; } $realpath = $this->_realpath($url); if (file_exists($realpath) && time() - filemtime($realpath) <= $this->_conf_cache_ttl) { Debug2::debug2('[Avatar] cache file exists [url] ' . $url); return $this->_rewrite($url, filemtime($realpath)); } if (!strpos($url, 'gravatar.com')) { return $url; } // Send request if (!empty($this->_summary['curr_request']) && time() - $this->_summary['curr_request'] < 300) { Debug2::debug2('[Avatar] Bypass generating due to interval limit [url] ' . $url); return $url; } // Generate immediately $this->_avatar_realtime_gen_dict[$url] = $this->_generate($url); return $this->_avatar_realtime_gen_dict[$url]; } /** * Read last time generated info * * @since 3.0 * @access public */ public function queue_count() { global $wpdb; // If var not exists, mean table not exists // todo: not true if (!$this->_tb) { return false; } $q = "SELECT COUNT(*) FROM `$this->_tb` WHERE dateline<" . (time() - $this->_conf_cache_ttl); return $wpdb->get_var($q); } /** * Get the final URL of local avatar * * Check from db also * * @since 3.0 */ private function _rewrite($url, $time = null) { return LITESPEED_STATIC_URL . '/avatar/' . $this->_filepath($url) . ($time ? '?ver=' . $time : ''); } /** * Generate realpath of the cache file * * @since 3.0 * @access private */ private function _realpath($url) { return LITESPEED_STATIC_DIR . '/avatar/' . $this->_filepath($url); } /** * Get filepath * * @since 4.0 */ private function _filepath($url) { $filename = md5($url) . '.jpg'; if (is_multisite()) { $filename = get_current_blog_id() . '/' . $filename; } return $filename; } /** * Cron generation * * @since 3.0 * @access public */ public static function cron($force = false) { global $wpdb; $_instance = self::cls(); if (!$_instance->queue_count()) { Debug2::debug('[Avatar] no queue'); return; } // For cron, need to check request interval too if (!$force) { if (!empty($_instance->_summary['curr_request']) && time() - $_instance->_summary['curr_request'] < 300) { Debug2::debug('[Avatar] curr_request too close'); return; } } $q = "SELECT url FROM `$_instance->_tb` WHERE dateline < %d ORDER BY id DESC LIMIT %d"; $q = $wpdb->prepare($q, array(time() - $_instance->_conf_cache_ttl, apply_filters('litespeed_avatar_limit', 30))); $list = $wpdb->get_results($q); Debug2::debug('[Avatar] cron job [count] ' . count($list)); foreach ($list as $v) { Debug2::debug('[Avatar] cron job [url] ' . $v->url); $_instance->_generate($v->url); } } /** * Remote generator * * @since 3.0 * @access private */ private function _generate($url) { global $wpdb; // Record the data $file = $this->_realpath($url); // Update request status self::save_summary(array('curr_request' => time())); // Generate $this->_maybe_mk_cache_folder('avatar'); $response = wp_safe_remote_get($url, array('timeout' => 180, 'stream' => true, 'filename' => $file)); Debug2::debug('[Avatar] _generate [url] ' . $url); // Parse response data if (is_wp_error($response)) { $error_message = $response->get_error_message(); file_exists($file) && unlink($file); Debug2::debug('[Avatar] failed to get: ' . $error_message); return $url; } // Save summary data self::save_summary(array( 'last_spent' => time() - $this->_summary['curr_request'], 'last_request' => $this->_summary['curr_request'], 'curr_request' => 0, )); // Update DB $md5 = md5($url); $q = "UPDATE `$this->_tb` SET dateline=%d WHERE md5=%s"; $existed = $wpdb->query($wpdb->prepare($q, array(time(), $md5))); if (!$existed) { $q = "INSERT INTO `$this->_tb` SET url=%s, md5=%s, dateline=%d"; $wpdb->query($wpdb->prepare($q, array($url, $md5, time()))); } Debug2::debug('[Avatar] saved avatar ' . $file); return $this->_rewrite($url); } /** * Handle all request actions from main cls * * @since 3.0 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_GENERATE: self::cron(true); break; default: break; } Admin::redirect(); } } css.cls.php 0000644 00000036215 15162226323 0006636 0 ustar 00 <?php /** * The optimize css class. * * @since 2.3 */ namespace LiteSpeed; defined('WPINC') || exit(); class CSS extends Base { const LOG_TAG = '[CSS]'; const TYPE_GEN_CCSS = 'gen_ccss'; const TYPE_CLEAR_Q_CCSS = 'clear_q_ccss'; protected $_summary; private $_ccss_whitelist; private $_queue; /** * Init * * @since 3.0 */ public function __construct() { $this->_summary = self::get_summary(); add_filter('litespeed_ccss_whitelist', array($this->cls('Data'), 'load_ccss_whitelist')); } /** * HTML lazyload CSS * @since 4.0 */ public function prepare_html_lazy() { return '<style>' . implode(',', $this->conf(self::O_OPTM_HTML_LAZY)) . '{content-visibility:auto;contain-intrinsic-size:1px 1000px;}</style>'; } /** * Output critical css * * @since 1.3 * @access public */ public function prepare_ccss() { // Get critical css for current page // Note: need to consider mobile $rules = $this->_ccss(); if (!$rules) { return null; } $error_tag = ''; if (substr($rules, 0, 2) == '/*' && substr($rules, -2) == '*/') { Core::comment('QUIC.cloud CCSS bypassed due to generation error ❌'); $error_tag = ' data-error="failed to generate"'; } // Append default critical css $rules .= $this->conf(self::O_OPTM_CCSS_CON); return '<style id="litespeed-ccss"' . $error_tag . '>' . $rules . '</style>'; } /** * Generate CCSS url tag * * @since 4.0 */ private function _gen_ccss_file_tag($request_url) { if (is_404()) { return '404'; } if ($this->conf(self::O_OPTM_CCSS_PER_URL)) { return $request_url; } $sep_uri = $this->conf(self::O_OPTM_CCSS_SEP_URI); if ($sep_uri && ($hit = Utility::str_hit_array($request_url, $sep_uri))) { Debug2::debug('[CCSS] Separate CCSS due to separate URI setting: ' . $hit); return $request_url; } $pt = Utility::page_type(); $sep_pt = $this->conf(self::O_OPTM_CCSS_SEP_POSTTYPE); if (in_array($pt, $sep_pt)) { Debug2::debug('[CCSS] Separate CCSS due to posttype setting: ' . $pt); return $request_url; } // Per posttype return $pt; } /** * The critical css content of the current page * * @since 2.3 */ private function _ccss() { global $wp; $request_url = get_permalink(); // Backup, in case get_permalink() fails. if (!$request_url) { $request_url = home_url($wp->request); } $filepath_prefix = $this->_build_filepath_prefix('ccss'); $url_tag = $this->_gen_ccss_file_tag($request_url); $vary = $this->cls('Vary')->finalize_full_varies(); $filename = $this->cls('Data')->load_url_file($url_tag, $vary, 'ccss'); if ($filename) { $static_file = LITESPEED_STATIC_DIR . $filepath_prefix . $filename . '.css'; if (file_exists($static_file)) { Debug2::debug2('[CSS] existing ccss ' . $static_file); Core::comment('QUIC.cloud CCSS loaded ✅ ' . $filepath_prefix . $filename . '.css'); return File::read($static_file); } } $uid = get_current_user_id(); $ua = !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; // Store it to prepare for cron Core::comment('QUIC.cloud CCSS in queue'); $this->_queue = $this->load_queue('ccss'); if (count($this->_queue) > 500) { self::debug('CCSS Queue is full - 500'); return null; } $queue_k = (strlen($vary) > 32 ? md5($vary) : $vary) . ' ' . $url_tag; $this->_queue[$queue_k] = array( 'url' => apply_filters('litespeed_ccss_url', $request_url), 'user_agent' => substr($ua, 0, 200), 'is_mobile' => $this->_separate_mobile(), 'is_webp' => $this->cls('Media')->webp_support() ? 1 : 0, 'uid' => $uid, 'vary' => $vary, 'url_tag' => $url_tag, ); // Current UA will be used to request $this->save_queue('ccss', $this->_queue); self::debug('Added queue_ccss [url_tag] ' . $url_tag . ' [UA] ' . $ua . ' [vary] ' . $vary . ' [uid] ' . $uid); // Prepare cache tag for later purge Tag::add('CCSS.' . md5($queue_k)); // For v4.1- clean up if (isset($this->_summary['ccss_type_history']) || isset($this->_summary['ccss_history']) || isset($this->_summary['queue_ccss'])) { if (isset($this->_summary['ccss_type_history'])) { unset($this->_summary['ccss_type_history']); } if (isset($this->_summary['ccss_history'])) { unset($this->_summary['ccss_history']); } if (isset($this->_summary['queue_ccss'])) { unset($this->_summary['queue_ccss']); } self::save_summary(); } return null; } /** * Cron ccss generation * * @since 2.3 * @access private */ public static function cron_ccss($continue = false) { $_instance = self::cls(); return $_instance->_cron_handler('ccss', $continue); } /** * Handle UCSS/CCSS cron * * @since 4.2 */ private function _cron_handler($type, $continue) { $this->_queue = $this->load_queue($type); if (empty($this->_queue)) { return; } $type_tag = strtoupper($type); // For cron, need to check request interval too if (!$continue) { if (!empty($this->_summary['curr_request_' . $type]) && time() - $this->_summary['curr_request_' . $type] < 300 && !$this->conf(self::O_DEBUG)) { Debug2::debug('[' . $type_tag . '] Last request not done'); return; } } $i = 0; foreach ($this->_queue as $k => $v) { if (!empty($v['_status'])) { continue; } Debug2::debug('[' . $type_tag . '] cron job [tag] ' . $k . ' [url] ' . $v['url'] . ($v['is_mobile'] ? ' 📱 ' : '') . ' [UA] ' . $v['user_agent']); if ($type == 'ccss' && empty($v['url_tag'])) { unset($this->_queue[$k]); $this->save_queue($type, $this->_queue); Debug2::debug('[CCSS] wrong queue_ccss format'); continue; } if (!isset($v['is_webp'])) { $v['is_webp'] = false; } $i++; $res = $this->_send_req($v['url'], $k, $v['uid'], $v['user_agent'], $v['vary'], $v['url_tag'], $type, $v['is_mobile'], $v['is_webp']); if (!$res) { // Status is wrong, drop this this->_queue unset($this->_queue[$k]); $this->save_queue($type, $this->_queue); if (!$continue) { return; } if ($i > 3) { GUI::print_loading(count($this->_queue), $type_tag); return Router::self_redirect(Router::ACTION_CSS, CSS::TYPE_GEN_CCSS); } continue; } // Exit queue if out of quota or service is hot if ($res === 'out_of_quota' || $res === 'svc_hot') { return; } $this->_queue[$k]['_status'] = 'requested'; $this->save_queue($type, $this->_queue); // only request first one if (!$continue) { return; } if ($i > 3) { GUI::print_loading(count($this->_queue), $type_tag); return Router::self_redirect(Router::ACTION_CSS, CSS::TYPE_GEN_CCSS); } } } /** * Send to QC API to generate CCSS/UCSS * * @since 2.3 * @access private */ private function _send_req($request_url, $queue_k, $uid, $user_agent, $vary, $url_tag, $type, $is_mobile, $is_webp) { // Check if has credit to push or not $err = false; $allowance = $this->cls('Cloud')->allowance(Cloud::SVC_CCSS, $err); if (!$allowance) { Debug2::debug('[CCSS] ❌ No credit: ' . $err); $err && Admin_Display::error(Error::msg($err)); return 'out_of_quota'; } set_time_limit(120); // Update css request status $this->_summary['curr_request_' . $type] = time(); self::save_summary(); // Gather guest HTML to send $html = $this->prepare_html($request_url, $user_agent, $uid); if (!$html) { return false; } // Parse HTML to gather all CSS content before requesting list($css, $html) = $this->prepare_css($html, $is_webp); if (!$css) { $type_tag = strtoupper($type); Debug2::debug('[' . $type_tag . '] ❌ No combined css'); return false; } // Generate critical css $data = array( 'url' => $request_url, 'queue_k' => $queue_k, 'user_agent' => $user_agent, 'is_mobile' => $is_mobile ? 1 : 0, // todo:compatible w/ tablet 'is_webp' => $is_webp ? 1 : 0, 'html' => $html, 'css' => $css, ); if (!isset($this->_ccss_whitelist)) { $this->_ccss_whitelist = $this->_filter_whitelist(); } $data['whitelist'] = $this->_ccss_whitelist; self::debug('Generating: ', $data); $json = Cloud::post(Cloud::SVC_CCSS, $data, 30); if (!is_array($json)) { return $json; } // Old version compatibility if (empty($json['status'])) { if (!empty($json[$type])) { $this->_save_con($type, $json[$type], $queue_k, $is_mobile, $is_webp); } // Delete the row return false; } // Unknown status, remove this line if ($json['status'] != 'queued') { return false; } // Save summary data $this->_summary['last_spent_' . $type] = time() - $this->_summary['curr_request_' . $type]; $this->_summary['last_request_' . $type] = $this->_summary['curr_request_' . $type]; $this->_summary['curr_request_' . $type] = 0; self::save_summary(); return true; } /** * Save CCSS/UCSS content * * @since 4.2 */ private function _save_con($type, $css, $queue_k, $mobile, $webp) { // Add filters $css = apply_filters('litespeed_' . $type, $css, $queue_k); Debug2::debug2('[CSS] con: ' . $css); if (substr($css, 0, 2) == '/*' && substr($css, -2) == '*/') { self::debug('❌ empty ' . $type . ' [content] ' . $css); // continue; // Save the error info too } // Write to file $filecon_md5 = md5($css); $filepath_prefix = $this->_build_filepath_prefix($type); $static_file = LITESPEED_STATIC_DIR . $filepath_prefix . $filecon_md5 . '.css'; File::save($static_file, $css, true); $url_tag = $this->_queue[$queue_k]['url_tag']; $vary = $this->_queue[$queue_k]['vary']; Debug2::debug2("[CSS] Save URL to file [file] $static_file [vary] $vary"); $this->cls('Data')->save_url($url_tag, $vary, $type, $filecon_md5, dirname($static_file), $mobile, $webp); Purge::add(strtoupper($type) . '.' . md5($queue_k)); } /** * Play for fun * * @since 3.4.3 */ public function test_url($request_url) { $user_agent = $_SERVER['HTTP_USER_AGENT']; $html = $this->prepare_html($request_url, $user_agent); list($css, $html) = $this->prepare_css($html, true, true); // var_dump( $css ); // $html = <<<EOT // EOT; // $css = <<<EOT // EOT; $data = array( 'url' => $request_url, 'ccss_type' => 'test', 'user_agent' => $user_agent, 'is_mobile' => 0, 'html' => $html, 'css' => $css, 'type' => 'CCSS', ); // self::debug( 'Generating: ', $data ); $json = Cloud::post(Cloud::SVC_CCSS, $data, 180); var_dump($json); } /** * Prepare HTML from URL * * @since 3.4.3 */ public function prepare_html($request_url, $user_agent, $uid = false) { $html = $this->cls('Crawler')->self_curl(add_query_arg('LSCWP_CTRL', 'before_optm', $request_url), $user_agent, $uid); Debug2::debug2('[CSS] self_curl result....', $html); if (!$html) { return false; } $html = $this->cls('Optimizer')->html_min($html, true); // Drop <noscript>xxx</noscript> $html = preg_replace('#<noscript>.*</noscript>#isU', '', $html); return $html; } /** * Prepare CSS from HTML for CCSS generation only. UCSS will used combined CSS directly. * Prepare refined HTML for both CCSS and UCSS. * * @since 3.4.3 */ public function prepare_css($html, $is_webp = false, $dryrun = false) { $css = ''; preg_match_all('#<link ([^>]+)/?>|<style([^>]*)>([^<]+)</style>#isU', $html, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $debug_info = ''; if (strpos($match[0], '<link') === 0) { $attrs = Utility::parse_attr($match[1]); if (empty($attrs['rel'])) { continue; } if ($attrs['rel'] != 'stylesheet') { if ($attrs['rel'] != 'preload' || empty($attrs['as']) || $attrs['as'] != 'style') { continue; } } if (!empty($attrs['media']) && strpos($attrs['media'], 'print') !== false) { continue; } if (empty($attrs['href'])) { continue; } // Check Google fonts hit if (strpos($attrs['href'], 'fonts.googleapis.com') !== false) { $html = str_replace($match[0], '', $html); continue; } $debug_info = $attrs['href']; // Load CSS content if (!$dryrun) { // Dryrun will not load CSS but just drop them $con = $this->cls('Optimizer')->load_file($attrs['href']); if (!$con) { continue; } } else { $con = ''; } } else { // Inline style $attrs = Utility::parse_attr($match[2]); if (!empty($attrs['media']) && strpos($attrs['media'], 'print') !== false) { continue; } Debug2::debug2('[CSS] Load inline CSS ' . substr($match[3], 0, 100) . '...', $attrs); $con = $match[3]; $debug_info = '__INLINE__'; } $con = Optimizer::minify_css($con); if ($is_webp && $this->cls('Media')->webp_support()) { $con = $this->cls('Media')->replace_background_webp($con); } if (!empty($attrs['media']) && $attrs['media'] !== 'all') { $con = '@media ' . $attrs['media'] . '{' . $con . "}\n"; } else { $con = $con . "\n"; } $con = '/* ' . $debug_info . ' */' . $con; $css .= $con; $html = str_replace($match[0], '', $html); } return array($css, $html); } /** * Filter the comment content, add quotes to selector from whitelist. Return the json * * @since 7.1 */ private function _filter_whitelist() { $whitelist = array(); $list = apply_filters('litespeed_ccss_whitelist', $this->conf(self::O_OPTM_CCSS_SELECTOR_WHITELIST)); foreach ($list as $v) { if (substr($v, 0, 2) === '//') { continue; } $whitelist[] = $v; } return $whitelist; } /** * Notify finished from server * @since 7.1 */ public function notify() { $post_data = \json_decode(file_get_contents('php://input'), true); if (is_null($post_data)) { $post_data = $_POST; } self::debug('notify() data', $post_data); $this->_queue = $this->load_queue('ccss'); list($post_data) = $this->cls('Cloud')->extract_msg($post_data, 'ccss'); $notified_data = $post_data['data']; if (empty($notified_data) || !is_array($notified_data)) { self::debug('❌ notify exit: no notified data'); return Cloud::err('no notified data'); } // Check if its in queue or not $valid_i = 0; foreach ($notified_data as $v) { if (empty($v['request_url'])) { self::debug('❌ notify bypass: no request_url', $v); continue; } if (empty($v['queue_k'])) { self::debug('❌ notify bypass: no queue_k', $v); continue; } if (empty($this->_queue[$v['queue_k']])) { self::debug('❌ notify bypass: no this queue [q_k]' . $v['queue_k']); continue; } // Save data if (!empty($v['data_ccss'])) { $is_mobile = $this->_queue[$v['queue_k']]['is_mobile']; $is_webp = $this->_queue[$v['queue_k']]['is_webp']; $this->_save_con('ccss', $v['data_ccss'], $v['queue_k'], $is_mobile, $is_webp); $valid_i++; } unset($this->_queue[$v['queue_k']]); self::debug('notify data handled, unset queue [q_k] ' . $v['queue_k']); } $this->save_queue('ccss', $this->_queue); self::debug('notified'); return Cloud::ok(array('count' => $valid_i)); } /** * Handle all request actions from main cls * * @since 2.3 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_GEN_CCSS: self::cron_ccss(true); break; case self::TYPE_CLEAR_Q_CCSS: $this->clear_q('ccss'); break; default: break; } Admin::redirect(); } } esi.cls.php 0000644 00000065701 15162226325 0006632 0 ustar 00 <?php /** * The ESI class. * * This is used to define all esi related functions. * * @since 1.1.3 * @package LiteSpeed * @subpackage LiteSpeed/src * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class ESI extends Root { const LOG_TAG = '⏺'; private static $has_esi = false; private static $_combine_ids = array(); private $esi_args = null; private $_esi_preserve_list = array(); private $_nonce_actions = array(-1 => ''); // val is cache control const QS_ACTION = 'lsesi'; const QS_PARAMS = 'esi'; const COMBO = '__combo'; // ESI include combine='main' handler const PARAM_ARGS = 'args'; const PARAM_ID = 'id'; const PARAM_INSTANCE = 'instance'; const PARAM_NAME = 'name'; const WIDGET_O_ESIENABLE = 'widget_esi_enable'; const WIDGET_O_TTL = 'widget_ttl'; /** * Confructor of ESI * * @since 1.2.0 * @since 4.0 Change to be after Vary init in hook 'after_setup_theme' */ public function init() { /** * Bypass ESI related funcs if disabled ESI to fix potential DIVI compatibility issue * @since 2.9.7.2 */ if (Router::is_ajax() || !$this->cls('Router')->esi_enabled()) { return; } // Guest mode, don't need to use ESI if (defined('LITESPEED_GUEST') && LITESPEED_GUEST) { return; } if (defined('LITESPEED_ESI_OFF')) { return; } // If page is not cacheable if (defined('DONOTCACHEPAGE') && apply_filters('litespeed_const_DONOTCACHEPAGE', DONOTCACHEPAGE)) { return; } // Init ESI in `after_setup_theme` hook after detected if LITESPEED_DISABLE_ALL is ON or not $this->_hooks(); /** * Overwrite wp_create_nonce func * @since 2.9.5 */ $this->_transform_nonce(); !defined('LITESPEED_ESI_INITED') && define('LITESPEED_ESI_INITED', true); } /** * Init ESI related hooks * * Load delayed by hook to give the ability to bypass by LITESPEED_DISABLE_ALL const * * @since 2.9.7.2 * @since 4.0 Changed to private from public * @access private */ private function _hooks() { add_filter('template_include', array($this, 'esi_template'), 99999); add_action('load-widgets.php', __NAMESPACE__ . '\Purge::purge_widget'); add_action('wp_update_comment_count', __NAMESPACE__ . '\Purge::purge_comment_widget'); /** * Recover REQUEST_URI * @since 1.8.1 */ if (!empty($_GET[self::QS_ACTION])) { self::debug('ESI req'); $this->_register_esi_actions(); } /** * Shortcode ESI * * To use it, just change the original shortcode as below: * old: [someshortcode aa='bb'] * new: [esi someshortcode aa='bb' cache='private,no-vary' ttl='600'] * * 1. `cache` attribute is optional, default to 'public,no-vary'. * 2. `ttl` attribute is optional, default is your public TTL setting. * 3. `_ls_silence` attribute is optional, default is false. * * @since 2.8 * @since 2.8.1 Check is_admin for Elementor compatibility #726013 */ if (!is_admin()) { add_shortcode('esi', array($this, 'shortcode')); } } /** * Take over all nonce calls and transform to ESI * * @since 2.9.5 */ private function _transform_nonce() { if (is_admin()) { return; } // Load ESI nonces in conf $nonces = $this->conf(Base::O_ESI_NONCE); add_filter('litespeed_esi_nonces', array($this->cls('Data'), 'load_esi_nonces')); if ($nonces = apply_filters('litespeed_esi_nonces', $nonces)) { foreach ($nonces as $action) { $this->nonce_action($action); } } add_action('litespeed_nonce', array($this, 'nonce_action')); } /** * Register a new nonce action to convert it to ESI * * @since 2.9.5 */ public function nonce_action($action) { // Split the Cache Control $action = explode(' ', $action); $control = !empty($action[1]) ? $action[1] : ''; $action = $action[0]; // Wildcard supported $action = Utility::wildcard2regex($action); if (array_key_exists($action, $this->_nonce_actions)) { return; } $this->_nonce_actions[$action] = $control; // Debug2::debug('[ESI] Appended nonce action to nonce list [action] ' . $action); } /** * Check if an action is registered to replace ESI * * @since 2.9.5 */ public function is_nonce_action($action) { // If GM not run yet, then ESI not init yet, then ESI nonce will not be allowed even nonce func replaced. if (!defined('LITESPEED_ESI_INITED')) { return null; } if (is_admin()) { return null; } if (defined('LITESPEED_ESI_OFF')) { return null; } foreach ($this->_nonce_actions as $k => $v) { if (strpos($k, '*') !== false) { if (preg_match('#' . $k . '#iU', $action)) { return $v; } } else { if ($k == $action) { return $v; } } } return null; } /** * Shortcode ESI * * @since 2.8 * @access public */ public function shortcode($atts) { if (empty($atts[0])) { Debug2::debug('[ESI] ===shortcode wrong format', $atts); return 'Wrong shortcode esi format'; } $cache = 'public,no-vary'; if (!empty($atts['cache'])) { $cache = $atts['cache']; unset($atts['cache']); } $silence = false; if (!empty($atts['_ls_silence'])) { $silence = true; } do_action('litespeed_esi_shortcode-' . $atts[0]); // Show ESI link return $this->sub_esi_block('esi', 'esi-shortcode', $atts, $cache, $silence); } /** * Check if the requested page has esi elements. If so, return esi on * header. * * @since 1.1.3 * @access public * @return string Esi On header if request has esi, empty string otherwise. */ public static function has_esi() { return self::$has_esi; } /** * Sets that the requested page has esi elements. * * @since 1.1.3 * @access public */ public static function set_has_esi() { self::$has_esi = true; } /** * Register all of the hooks related to the esi logic of the plugin. * Specifically when the page IS an esi page. * * @since 1.1.3 * @access private */ private function _register_esi_actions() { /** * This hook is in `init` * For any plugin need to check if page is ESI, use `LSCACHE_IS_ESI` check after `init` hook */ !defined('LSCACHE_IS_ESI') && define('LSCACHE_IS_ESI', $_GET[self::QS_ACTION]); // Reused this to ESI block ID !empty($_SERVER['ESI_REFERER']) && defined('LSCWP_LOG') && Debug2::debug('[ESI] ESI_REFERER: ' . $_SERVER['ESI_REFERER']); /** * Only when ESI's parent is not REST, replace REQUEST_URI to avoid breaking WP5 editor REST call * @since 2.9.3 */ if (!empty($_SERVER['ESI_REFERER']) && !$this->cls('REST')->is_rest($_SERVER['ESI_REFERER'])) { self::debug('overwrite REQUEST_URI to ESI_REFERER [from] ' . $_SERVER['REQUEST_URI'] . ' [to] ' . $_SERVER['ESI_REFERER']); if (!empty($_SERVER['ESI_REFERER'])) { $_SERVER['REQUEST_URI'] = $_SERVER['ESI_REFERER']; if (substr(get_option('permalink_structure'), -1) === '/' && strpos($_SERVER['ESI_REFERER'], '?') === false) { $_SERVER['REQUEST_URI'] = trailingslashit($_SERVER['ESI_REFERER']); } } # Prevent from 301 redirecting if (!empty($_SERVER['SCRIPT_URI'])) { $SCRIPT_URI = parse_url($_SERVER['SCRIPT_URI']); $SCRIPT_URI['path'] = $_SERVER['REQUEST_URI']; Utility::compatibility(); $_SERVER['SCRIPT_URI'] = http_build_url($SCRIPT_URI); } } if (!empty($_SERVER['ESI_CONTENT_TYPE']) && strpos($_SERVER['ESI_CONTENT_TYPE'], 'application/json') === 0) { add_filter('litespeed_is_json', '__return_true'); } /** * Make REST call be able to parse ESI * NOTE: Not effective due to ESI req are all to `/` yet * @since 2.9.4 */ add_action('rest_api_init', array($this, 'load_esi_block'), 101); // Register ESI blocks add_action('litespeed_esi_load-widget', array($this, 'load_widget_block')); add_action('litespeed_esi_load-admin-bar', array($this, 'load_admin_bar_block')); add_action('litespeed_esi_load-comment-form', array($this, 'load_comment_form_block')); add_action('litespeed_esi_load-nonce', array($this, 'load_nonce_block')); add_action('litespeed_esi_load-esi', array($this, 'load_esi_shortcode')); add_action('litespeed_esi_load-' . self::COMBO, array($this, 'load_combo')); } /** * Hooked to the template_include action. * Selects the esi template file when the post type is a LiteSpeed ESI page. * * @since 1.1.3 * @access public * @param string $template The template path filtered. * @return string The new template path. */ public function esi_template($template) { // Check if is an ESI request if (defined('LSCACHE_IS_ESI')) { self::debug('calling ESI template'); return LSCWP_DIR . 'tpl/esi.tpl.php'; } self::debug('calling default template'); $this->_register_not_esi_actions(); return $template; } /** * Register all of the hooks related to the esi logic of the plugin. * Specifically when the page is NOT an esi page. * * @since 1.1.3 * @access private */ private function _register_not_esi_actions() { do_action('litespeed_tpl_normal'); if (!Control::is_cacheable()) { return; } if (Router::is_ajax()) { return; } add_filter('widget_display_callback', array($this, 'sub_widget_block'), 0, 3); // Add admin_bar esi if (Router::is_logged_in()) { remove_action('wp_body_open', 'wp_admin_bar_render', 0); // Remove default Admin bar. Fix https://github.com/elementor/elementor/issues/25198 remove_action('wp_footer', 'wp_admin_bar_render', 1000); add_action('wp_footer', array($this, 'sub_admin_bar_block'), 1000); } // Add comment forum esi for logged-in user or commenter if (!Router::is_ajax() && Vary::has_vary()) { add_filter('comment_form_defaults', array($this, 'register_comment_form_actions')); } } /** * Set an ESI to be combine='sub' * * @since 3.4.2 */ public static function combine($block_id) { if (!isset($_SERVER['X-LSCACHE']) || strpos($_SERVER['X-LSCACHE'], 'combine') === false) { return; } if (in_array($block_id, self::$_combine_ids)) { return; } self::$_combine_ids[] = $block_id; } /** * Load combined ESI * * @since 3.4.2 */ public function load_combo() { Control::set_nocache('ESI combine request'); if (empty($_POST['esi_include'])) { return; } self::set_has_esi(); Debug2::debug('[ESI] 🍔 Load combo', $_POST['esi_include']); $output = ''; foreach ($_POST['esi_include'] as $url) { $qs = parse_url(htmlspecialchars_decode($url), PHP_URL_QUERY); parse_str($qs, $qs); if (empty($qs[self::QS_ACTION])) { continue; } $esi_id = $qs[self::QS_ACTION]; $esi_param = !empty($qs[self::QS_PARAMS]) ? $this->_parse_esi_param($qs[self::QS_PARAMS]) : false; $inline_param = apply_filters('litespeed_esi_inline-' . $esi_id, array(), $esi_param); // Returned array need to be [ val, control, tag ] if ($inline_param) { $output .= self::_build_inline($url, $inline_param); } } echo $output; } /** * Build a whole inline segment * * @since 3.4.2 */ private static function _build_inline($url, $inline_param) { if (!$url || empty($inline_param['val']) || empty($inline_param['control']) || empty($inline_param['tag'])) { return ''; } $url = esc_attr($url); $control = esc_attr($inline_param['control']); $tag = esc_attr($inline_param['tag']); return "<esi:inline name='$url' cache-control='" . $control . "' cache-tag='" . $tag . "'>" . $inline_param['val'] . '</esi:inline>'; } /** * Build the esi url. This method will build the html comment wrapper as well as serialize and encode the parameter array. * * The block_id parameter should contain alphanumeric and '-_' only. * * @since 1.1.3 * @access private * @param string $block_id The id to use to display the correct esi block. * @param string $wrapper The wrapper for the esi comments. * @param array $params The esi parameters. * @param string $control The cache control attribute if any. * @param bool $silence If generate wrapper comment or not * @param bool $preserved If this ESI block is used in any filter, need to temporarily convert it to a string to avoid the HTML tag being removed/filtered. * @param bool $svar If store the value in memory or not, in memory will be faster * @param array $inline_val If show the current value for current request( this can avoid multiple esi requests in first time cache generating process ) */ public function sub_esi_block( $block_id, $wrapper, $params = array(), $control = 'private,no-vary', $silence = false, $preserved = false, $svar = false, $inline_param = array() ) { if (empty($block_id) || !is_array($params) || preg_match('/[^\w-]/', $block_id)) { return false; } if (defined('LITESPEED_ESI_OFF')) { Debug2::debug('[ESI] ESI OFF so force loading [block_id] ' . $block_id); do_action('litespeed_esi_load-' . $block_id, $params); return; } if ($silence) { // Don't add comment to esi block ( original for nonce used in tag property data-nonce='esi_block' ) $params['_ls_silence'] = true; } if ($this->cls('REST')->is_rest() || $this->cls('REST')->is_internal_rest()) { $params['is_json'] = 1; } $params = apply_filters('litespeed_esi_params', $params, $block_id); $control = apply_filters('litespeed_esi_control', $control, $block_id); if (!is_array($params) || !is_string($control)) { defined('LSCWP_LOG') && Debug2::debug("[ESI] 🛑 Sub hooks returned Params: \n" . var_export($params, true) . "\ncache control: \n" . var_export($control, true)); return false; } // Build params for URL $appended_params = array( self::QS_ACTION => $block_id, ); if (!empty($control)) { $appended_params['_control'] = $control; } if ($params) { $appended_params[self::QS_PARAMS] = base64_encode(\json_encode($params)); Debug2::debug2('[ESI] param ', $params); } // Append hash $appended_params['_hash'] = $this->_gen_esi_md5($appended_params); /** * Escape potential chars * @since 2.9.4 */ $appended_params = array_map('urlencode', $appended_params); // Generate ESI URL $url = add_query_arg($appended_params, trailingslashit(wp_make_link_relative(home_url()))); $output = ''; if ($inline_param) { $output .= self::_build_inline($url, $inline_param); } $output .= "<esi:include src='$url'"; if (!empty($control)) { $control = esc_attr($control); $output .= " cache-control='$control'"; } if ($svar) { $output .= " as-var='1'"; } if (in_array($block_id, self::$_combine_ids)) { $output .= " combine='sub'"; } if ($block_id == self::COMBO && isset($_SERVER['X-LSCACHE']) && strpos($_SERVER['X-LSCACHE'], 'combine') !== false) { $output .= " combine='main'"; } $output .= ' />'; if (!$silence) { $output = "<!-- lscwp $wrapper -->$output<!-- lscwp $wrapper esi end -->"; } self::debug("💕 [BLock_ID] $block_id \t[wrapper] $wrapper \t\t[Control] $control"); self::debug2($output); self::set_has_esi(); // Convert to string to avoid html chars filter when using // Will reverse the buffer when output in self::finalize() if ($preserved) { $hash = md5($output); $this->_esi_preserve_list[$hash] = $output; self::debug("Preserved to $hash"); return $hash; } return $output; } /** * Generate ESI hash md5 * * @since 2.9.6 * @access private */ private function _gen_esi_md5($params) { $keys = array(self::QS_ACTION, '_control', self::QS_PARAMS); $str = ''; foreach ($keys as $v) { if (isset($params[$v]) && is_string($params[$v])) { $str .= $params[$v]; } } Debug2::debug2('[ESI] md5_string=' . $str); return md5($this->conf(Base::HASH) . $str); } /** * Parses the request parameters on an ESI request * * @since 1.1.3 * @access private */ private function _parse_esi_param($qs_params = false) { $req_params = false; if ($qs_params) { $req_params = $qs_params; } elseif (isset($_REQUEST[self::QS_PARAMS])) { $req_params = $_REQUEST[self::QS_PARAMS]; } if (!$req_params) { return false; } $unencrypted = base64_decode($req_params); if ($unencrypted === false) { return false; } Debug2::debug2('[ESI] params', $unencrypted); // $unencoded = urldecode($unencrypted); no need to do this as $_GET is already parsed $params = \json_decode($unencrypted, true); return $params; } /** * Select the correct esi output based on the parameters in an ESI request. * * @since 1.1.3 * @access public */ public function load_esi_block() { /** * Validate if is a legal ESI req * @since 2.9.6 */ if (empty($_GET['_hash']) || $this->_gen_esi_md5($_GET) != $_GET['_hash']) { Debug2::debug('[ESI] ❌ Failed to validate _hash'); return; } $params = $this->_parse_esi_param(); if (defined('LSCWP_LOG')) { $logInfo = '[ESI] ⭕ '; if (!empty($params[self::PARAM_NAME])) { $logInfo .= ' Name: ' . $params[self::PARAM_NAME] . ' ----- '; } $logInfo .= ' [ID] ' . LSCACHE_IS_ESI; Debug2::debug($logInfo); } if (!empty($params['_ls_silence'])) { !defined('LSCACHE_ESI_SILENCE') && define('LSCACHE_ESI_SILENCE', true); } /** * Buffer needs to be JSON format * @since 2.9.4 */ if (!empty($params['is_json'])) { add_filter('litespeed_is_json', '__return_true'); } Tag::add(rtrim(Tag::TYPE_ESI, '.')); Tag::add(Tag::TYPE_ESI . LSCACHE_IS_ESI); // Debug2::debug(var_export($params, true )); /** * Handle default cache control 'private,no-vary' for sub_esi_block() @ticket #923505 * * @since 2.2.3 */ if (!empty($_GET['_control'])) { $control = explode(',', $_GET['_control']); if (in_array('private', $control)) { Control::set_private(); } if (in_array('no-vary', $control)) { Control::set_no_vary(); } } do_action('litespeed_esi_load-' . LSCACHE_IS_ESI, $params); } // The *_sub_* functions are helpers for the sub_* functions. // The *_load_* functions are helpers for the load_* functions. /** * Loads the default options for default WordPress widgets. * * @since 1.1.3 * @access public */ public static function widget_default_options($options, $widget) { if (!is_array($options)) { return $options; } $widget_name = get_class($widget); switch ($widget_name) { case 'WP_Widget_Recent_Posts': case 'WP_Widget_Recent_Comments': $options[self::WIDGET_O_ESIENABLE] = Base::VAL_OFF; $options[self::WIDGET_O_TTL] = 86400; break; default: break; } return $options; } /** * Hooked to the widget_display_callback filter. * If the admin configured the widget to display via esi, this function * will set up the esi request and cancel the widget display. * * @since 1.1.3 * @access public * @param array $instance Parameter used to build the widget. * @param WP_Widget $widget The widget to build. * @param array $args Parameter used to build the widget. * @return mixed Return false if display through esi, instance otherwise. */ public function sub_widget_block($instance, $widget, $args) { // #210407 if (!is_array($instance)) { return $instance; } $name = get_class($widget); if (!isset($instance[Base::OPTION_NAME])) { return $instance; } $options = $instance[Base::OPTION_NAME]; if (!isset($options) || !$options[self::WIDGET_O_ESIENABLE]) { defined('LSCWP_LOG') && Debug2::debug('ESI 0 ' . $name . ': ' . (!isset($options) ? 'not set' : 'set off')); return $instance; } $esi_private = $options[self::WIDGET_O_ESIENABLE] == Base::VAL_ON2 ? 'private,' : ''; $params = array( self::PARAM_NAME => $name, self::PARAM_ID => $widget->id, self::PARAM_INSTANCE => $instance, self::PARAM_ARGS => $args, ); echo $this->sub_esi_block('widget', 'widget ' . $name, $params, $esi_private . 'no-vary'); return false; } /** * Hooked to the wp_footer action. * Sets up the ESI request for the admin bar. * * @access public * @since 1.1.3 * @global type $wp_admin_bar */ public function sub_admin_bar_block() { global $wp_admin_bar; if (!is_admin_bar_showing() || !is_object($wp_admin_bar)) { return; } // To make each admin bar ESI request different for `Edit` button different link $params = array( 'ref' => $_SERVER['REQUEST_URI'], ); echo $this->sub_esi_block('admin-bar', 'adminbar', $params); } /** * Parses the esi input parameters and generates the widget for esi display. * * @access public * @since 1.1.3 * @global $wp_widget_factory * @param array $params Input parameters needed to correctly display widget */ public function load_widget_block($params) { // global $wp_widget_factory; // $widget = $wp_widget_factory->widgets[ $params[ self::PARAM_NAME ] ]; $option = $params[self::PARAM_INSTANCE]; $option = $option[Base::OPTION_NAME]; // Since we only reach here via esi, safe to assume setting exists. $ttl = $option[self::WIDGET_O_TTL]; defined('LSCWP_LOG') && Debug2::debug('ESI widget render: name ' . $params[self::PARAM_NAME] . ', id ' . $params[self::PARAM_ID] . ', ttl ' . $ttl); if ($ttl == 0) { Control::set_nocache('ESI Widget time to live set to 0'); } else { Control::set_custom_ttl($ttl); if ($option[self::WIDGET_O_ESIENABLE] == Base::VAL_ON2) { Control::set_private(); } Control::set_no_vary(); Tag::add(Tag::TYPE_WIDGET . $params[self::PARAM_ID]); } the_widget($params[self::PARAM_NAME], $params[self::PARAM_INSTANCE], $params[self::PARAM_ARGS]); } /** * Generates the admin bar for esi display. * * @access public * @since 1.1.3 */ public function load_admin_bar_block($params) { if (!empty($params['ref'])) { $ref_qs = parse_url($params['ref'], PHP_URL_QUERY); if (!empty($ref_qs)) { parse_str($ref_qs, $ref_qs_arr); if (!empty($ref_qs_arr)) { foreach ($ref_qs_arr as $k => $v) { $_GET[$k] = $v; } } } } // Needed when permalink structure is "Plain" wp(); wp_admin_bar_render(); if (!$this->conf(Base::O_ESI_CACHE_ADMBAR)) { Control::set_nocache('build-in set to not cacheable'); } else { Control::set_private(); Control::set_no_vary(); } defined('LSCWP_LOG') && Debug2::debug('ESI: adminbar ref: ' . $_SERVER['REQUEST_URI']); } /** * Parses the esi input parameters and generates the comment form for esi display. * * @access public * @since 1.1.3 * @param array $params Input parameters needed to correctly display comment form */ public function load_comment_form_block($params) { comment_form($params[self::PARAM_ARGS], $params[self::PARAM_ID]); if (!$this->conf(Base::O_ESI_CACHE_COMMFORM)) { Control::set_nocache('build-in set to not cacheable'); } else { // by default comment form is public if (Vary::has_vary()) { Control::set_private(); Control::set_no_vary(); } } } /** * Generate nonce for certain action * * @access public * @since 2.6 */ public function load_nonce_block($params) { $action = $params['action']; Debug2::debug('[ESI] load_nonce_block [action] ' . $action); // set nonce TTL to half day Control::set_custom_ttl(43200); if (Router::is_logged_in()) { Control::set_private(); } if (function_exists('wp_create_nonce_litespeed_esi')) { echo wp_create_nonce_litespeed_esi($action); } else { echo wp_create_nonce($action); } } /** * Show original shortcode * * @access public * @since 2.8 */ public function load_esi_shortcode($params) { if (isset($params['ttl'])) { if (!$params['ttl']) { Control::set_nocache('ESI shortcode att ttl=0'); } else { Control::set_custom_ttl($params['ttl']); } unset($params['ttl']); } // Replace to original shortcode $shortcode = $params[0]; $atts_ori = array(); foreach ($params as $k => $v) { if ($k === 0) { continue; } $atts_ori[] = is_string($k) ? "$k='" . addslashes($v) . "'" : $v; } Tag::add(Tag::TYPE_ESI . "esi.$shortcode"); // Output original shortcode final content echo do_shortcode("[$shortcode " . implode(' ', $atts_ori) . ' ]'); } /** * Hooked to the comment_form_defaults filter. * Stores the default comment form settings. * If sub_comment_form_block is triggered, the output buffer is cleared and an esi block is added. The remaining comment form is also buffered and cleared. * Else there is no need to make the comment form ESI. * * @since 1.1.3 * @access public */ public function register_comment_form_actions($defaults) { $this->esi_args = $defaults; echo GUI::clean_wrapper_begin(); add_filter('comment_form_submit_button', array($this, 'sub_comment_form_btn'), 1000, 2); // To save the params passed in add_action('comment_form', array($this, 'sub_comment_form_block'), 1000); return $defaults; } /** * Store the args passed in comment_form for the ESI comment param usage in `$this->sub_comment_form_block()` * * @since 3.4 * @access public */ public function sub_comment_form_btn($unused, $args) { if (empty($args) || empty($this->esi_args)) { Debug2::debug('comment form args empty?'); return $unused; } $esi_args = array(); // compare current args with default ones foreach ($args as $k => $v) { if (!isset($this->esi_args[$k])) { $esi_args[$k] = $v; } elseif (is_array($v)) { $diff = array_diff_assoc($v, $this->esi_args[$k]); if (!empty($diff)) { $esi_args[$k] = $diff; } } elseif ($v !== $this->esi_args[$k]) { $esi_args[$k] = $v; } } $this->esi_args = $esi_args; return $unused; } /** * Hooked to the comment_form_submit_button filter. * * This method will compare the used comment form args against the default args. The difference will be passed to the esi request. * * @access public * @since 1.1.3 */ public function sub_comment_form_block($post_id) { echo GUI::clean_wrapper_end(); $params = array( self::PARAM_ID => $post_id, self::PARAM_ARGS => $this->esi_args, ); echo $this->sub_esi_block('comment-form', 'comment form', $params); echo GUI::clean_wrapper_begin(); add_action('comment_form_after', array($this, 'comment_form_sub_clean')); } /** * Hooked to the comment_form_after action. * Cleans up the remaining comment form output. * * @since 1.1.3 * @access public */ public function comment_form_sub_clean() { echo GUI::clean_wrapper_end(); } /** * Replace preserved blocks * * @since 2.6 * @access public */ public function finalize($buffer) { // Prepend combo esi block if (self::$_combine_ids) { Debug2::debug('[ESI] 🍔 Enabled combo'); $esi_block = $this->sub_esi_block(self::COMBO, '__COMBINE_MAIN__', array(), 'no-cache', true); $buffer = $esi_block . $buffer; } // Bypass if no preserved list to be replaced if (!$this->_esi_preserve_list) { return $buffer; } $keys = array_keys($this->_esi_preserve_list); Debug2::debug('[ESI] replacing preserved blocks', $keys); $buffer = str_replace($keys, $this->_esi_preserve_list, $buffer); return $buffer; } /** * Check if the content contains preserved list or not * * @since 3.3 */ public function contain_preserve_esi($content) { $hit_list = array(); foreach ($this->_esi_preserve_list as $k => $v) { if (strpos($content, '"' . $k . '"') !== false) { $hit_list[] = '"' . $k . '"'; } if (strpos($content, "'" . $k . "'") !== false) { $hit_list[] = "'" . $k . "'"; } } return $hit_list; } } tag.cls.php 0000644 00000021633 15162226326 0006622 0 ustar 00 <?php /** * The plugin cache-tag class for X-LiteSpeed-Tag * * @since 1.1.3 * @since 1.5 Moved into /inc */ namespace LiteSpeed; defined('WPINC') || exit(); class Tag extends Root { const TYPE_FEED = 'FD'; const TYPE_FRONTPAGE = 'F'; const TYPE_HOME = 'H'; const TYPE_PAGES = 'PGS'; const TYPE_PAGES_WITH_RECENT_POSTS = 'PGSRP'; const TYPE_HTTP = 'HTTP.'; const TYPE_POST = 'Po.'; // Post. Cannot use P, reserved for litemage. const TYPE_ARCHIVE_POSTTYPE = 'PT.'; const TYPE_ARCHIVE_TERM = 'T.'; //for is_category|is_tag|is_tax const TYPE_AUTHOR = 'A.'; const TYPE_ARCHIVE_DATE = 'D.'; const TYPE_BLOG = 'B.'; const TYPE_LOGIN = 'L'; const TYPE_URL = 'URL.'; const TYPE_WIDGET = 'W.'; const TYPE_ESI = 'ESI.'; const TYPE_REST = 'REST'; const TYPE_AJAX = 'AJAX.'; const TYPE_LIST = 'LIST'; const TYPE_MIN = 'MIN'; const TYPE_LOCALRES = 'LOCALRES'; const X_HEADER = 'X-LiteSpeed-Tag'; private static $_tags = array(); private static $_tags_priv = array('tag_priv'); public static $error_code_tags = array(403, 404, 500); /** * Initialize * * @since 4.0 */ public function init() { // register recent posts widget tag before theme renders it to make it work add_filter('widget_posts_args', array($this, 'add_widget_recent_posts')); } /** * Check if the login page is cacheable. * If not, unset the cacheable member variable. * * NOTE: This is checked separately because login page doesn't go through WP logic. * * @since 1.0.0 * @access public */ public function check_login_cacheable() { if (!$this->conf(Base::O_CACHE_PAGE_LOGIN)) { return; } if (Control::isset_notcacheable()) { return; } if (!empty($_GET)) { Control::set_nocache('has GET request'); return; } $this->cls('Control')->set_cacheable(); self::add(self::TYPE_LOGIN); // we need to send lsc-cookie manually to make it be sent to all other users when is cacheable $list = headers_list(); if (empty($list)) { return; } foreach ($list as $hdr) { if (strncasecmp($hdr, 'set-cookie:', 11) == 0) { $cookie = substr($hdr, 12); @header('lsc-cookie: ' . $cookie, false); } } } /** * Register purge tag for pages with recent posts widget * of the plugin. * * @since 1.0.15 * @access public * @param array $params [wordpress params for widget_posts_args] */ public function add_widget_recent_posts($params) { self::add(self::TYPE_PAGES_WITH_RECENT_POSTS); return $params; } /** * Adds cache tags to the list of cache tags for the current page. * * @since 1.0.5 * @access public * @param mixed $tags A string or array of cache tags to add to the current list. */ public static function add($tags) { if (!is_array($tags)) { $tags = array($tags); } Debug2::debug('💰 [Tag] Add ', $tags); self::$_tags = array_merge(self::$_tags, $tags); // Send purge header immediately $tag_header = self::cls()->output(true); @header($tag_header); } /** * Add a post id to cache tag * * @since 3.0 * @access public */ public static function add_post($pid) { self::add(self::TYPE_POST . $pid); } /** * Add a widget id to cache tag * * @since 3.0 * @access public */ public static function add_widget($id) { self::add(self::TYPE_WIDGET . $id); } /** * Add a private ESI to cache tag * * @since 3.0 * @access public */ public static function add_private_esi($tag) { self::add_private(self::TYPE_ESI . $tag); } /** * Adds private cache tags to the list of cache tags for the current page. * * @since 1.6.3 * @access public * @param mixed $tags A string or array of cache tags to add to the current list. */ public static function add_private($tags) { if (!is_array($tags)) { $tags = array($tags); } self::$_tags_priv = array_merge(self::$_tags_priv, $tags); } /** * Return tags for Admin QS * * @since 1.1.3 * @access public */ public static function output_tags() { return self::$_tags; } /** * Will get a hash of the URI. Removes query string and appends a '/' if it is missing. * * @since 1.0.12 * @access public * @param string $uri The uri to get the hash of. * @param boolean $ori Return the original url or not * @return bool|string False on input error, hash otherwise. */ public static function get_uri_tag($uri, $ori = false) { $no_qs = strtok($uri, '?'); if (empty($no_qs)) { return false; } $slashed = trailingslashit($no_qs); // If only needs uri tag if ($ori) { return $slashed; } if (defined('LSCWP_LOG')) { return self::TYPE_URL . $slashed; } return self::TYPE_URL . md5($slashed); } /** * Get the unique tag based on self url. * * @since 1.1.3 * @access public * @param boolean $ori Return the original url or not */ public static function build_uri_tag($ori = false) { return self::get_uri_tag(urldecode($_SERVER['REQUEST_URI']), $ori); } /** * Gets the cache tags to set for the page. * * This includes site wide post types (e.g. front page) as well as * any third party plugin specific cache tags. * * @since 1.0.0 * @access private * @return array The list of cache tags to set. */ private static function _build_type_tags() { $tags = array(); $tags[] = Utility::page_type(); $tags[] = self::build_uri_tag(); if (is_front_page()) { $tags[] = self::TYPE_FRONTPAGE; } elseif (is_home()) { $tags[] = self::TYPE_HOME; } global $wp_query; if (isset($wp_query)) { $queried_obj_id = get_queried_object_id(); if (is_archive()) { //An Archive is a Category, Tag, Author, Date, Custom Post Type or Custom Taxonomy based pages. if (is_category() || is_tag() || is_tax()) { $tags[] = self::TYPE_ARCHIVE_TERM . $queried_obj_id; } elseif (is_post_type_archive() && ($post_type = get_post_type())) { $tags[] = self::TYPE_ARCHIVE_POSTTYPE . $post_type; } elseif (is_author()) { $tags[] = self::TYPE_AUTHOR . $queried_obj_id; } elseif (is_date()) { global $post; if ($post && isset($post->post_date)) { $date = $post->post_date; $date = strtotime($date); if (is_day()) { $tags[] = self::TYPE_ARCHIVE_DATE . date('Ymd', $date); } elseif (is_month()) { $tags[] = self::TYPE_ARCHIVE_DATE . date('Ym', $date); } elseif (is_year()) { $tags[] = self::TYPE_ARCHIVE_DATE . date('Y', $date); } } } } elseif (is_singular()) { //$this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; $tags[] = self::TYPE_POST . $queried_obj_id; if (is_page()) { $tags[] = self::TYPE_PAGES; } } elseif (is_feed()) { $tags[] = self::TYPE_FEED; } } // Check REST API if (REST::cls()->is_rest()) { $tags[] = self::TYPE_REST; $path = !empty($_SERVER['SCRIPT_URL']) ? $_SERVER['SCRIPT_URL'] : false; if ($path) { // posts collections tag if (substr($path, -6) == '/posts') { $tags[] = self::TYPE_LIST; // Not used for purge yet } // single post tag global $post; if (!empty($post->ID) && substr($path, -strlen($post->ID) - 1) === '/' . $post->ID) { $tags[] = self::TYPE_POST . $post->ID; } // pages collections & single page tag if (stripos($path, '/pages') !== false) { $tags[] = self::TYPE_PAGES; } } } // Append AJAX action tag if (Router::is_ajax() && !empty($_REQUEST['action'])) { $tags[] = self::TYPE_AJAX . $_REQUEST['action']; } return $tags; } /** * Generate all cache tags before output * * @access private * @since 1.1.3 */ private static function _finalize() { // run 3rdparty hooks to tag do_action('litespeed_tag_finalize'); // generate wp tags if (!defined('LSCACHE_IS_ESI')) { $type_tags = self::_build_type_tags(); self::$_tags = array_merge(self::$_tags, $type_tags); } if (defined('LITESPEED_GUEST') && LITESPEED_GUEST) { self::$_tags[] = 'guest'; } // append blog main tag self::$_tags[] = ''; // removed duplicates self::$_tags = array_unique(self::$_tags); } /** * Sets up the Cache Tags header. * ONLY need to run this if is cacheable * * @since 1.1.3 * @access public * @return string empty string if empty, otherwise the cache tags header. */ public function output($no_finalize = false) { if (defined('LSCACHE_NO_CACHE') && LSCACHE_NO_CACHE) { return; } if (!$no_finalize) { self::_finalize(); } $prefix_tags = array(); /** * Only append blog_id when is multisite * @since 2.9.3 */ $prefix = LSWCP_TAG_PREFIX . (is_multisite() ? get_current_blog_id() : '') . '_'; // If is_private and has private tags, append them first, then specify prefix to `public` for public tags if (Control::is_private()) { foreach (self::$_tags_priv as $priv_tag) { $prefix_tags[] = $prefix . $priv_tag; } $prefix = 'public:' . $prefix; } foreach (self::$_tags as $tag) { $prefix_tags[] = $prefix . $tag; } $hdr = self::X_HEADER . ': ' . implode(',', $prefix_tags); return $hdr; } } debug2.cls.php 0000644 00000032126 15162226330 0007211 0 ustar 00 <?php /** * The plugin logging class. */ namespace LiteSpeed; defined('WPINC') || exit(); class Debug2 extends Root { private static $log_path; private static $log_path_prefix; private static $_prefix; const TYPE_CLEAR_LOG = 'clear_log'; const TYPE_BETA_TEST = 'beta_test'; const BETA_TEST_URL = 'beta_test_url'; const BETA_TEST_URL_WP = 'https://downloads.wordpress.org/plugin/litespeed-cache.zip'; /** * Log class Confructor * * NOTE: in this process, until last step ( define const LSCWP_LOG = true ), any usage to WP filter will not be logged to prevent infinite loop with log_filters() * * @since 1.1.2 * @access public */ public function __construct() { self::$log_path_prefix = LITESPEED_STATIC_DIR . '/debug/'; // Maybe move legacy log files $this->_maybe_init_folder(); self::$log_path = $this->path('debug'); if (!empty($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'lscache_') === 0) { self::$log_path = $this->path('crawler'); } !defined('LSCWP_LOG_TAG') && define('LSCWP_LOG_TAG', get_current_blog_id()); if ($this->conf(Base::O_DEBUG_LEVEL)) { !defined('LSCWP_LOG_MORE') && define('LSCWP_LOG_MORE', true); } defined('LSCWP_DEBUG_EXC_STRINGS') || define('LSCWP_DEBUG_EXC_STRINGS', $this->conf(Base::O_DEBUG_EXC_STRINGS)); } /** * Try moving legacy logs into /litespeed/debug/ folder * * @since 6.5 */ private function _maybe_init_folder() { if (file_exists(self::$log_path_prefix . 'index.php')) { return; } file::save(self::$log_path_prefix . 'index.php', '<?php // Silence is golden.', true); $logs = array('debug', 'debug.purge', 'crawler'); foreach ($logs as $log) { if (file_exists(LSCWP_CONTENT_DIR . '/' . $log . '.log') && !file_exists($this->path($log))) { rename(LSCWP_CONTENT_DIR . '/' . $log . '.log', $this->path($log)); } } } /** * Generate log file path * * @since 6.5 */ public function path($type) { return self::$log_path_prefix . self::FilePath($type); } /** * Generate the fixed log filename * * @since 6.5 */ public static function FilePath($type) { if ($type == 'debug.purge') { $type = 'purge'; } $key = defined('AUTH_KEY') ? AUTH_KEY : md5(__FILE__); $rand = substr(md5(substr($key, -16)), -16); return $type . $rand . '.log'; } /** * End call of one request process * @since 4.7 * @access public */ public static function ended() { $headers = headers_list(); foreach ($headers as $key => $header) { if (stripos($header, 'Set-Cookie') === 0) { unset($headers[$key]); } } self::debug('Response headers', $headers); $elapsed_time = number_format((microtime(true) - LSCWP_TS_0) * 1000, 2); self::debug("End response\n--------------------------------------------------Duration: " . $elapsed_time . " ms------------------------------\n"); } /** * Beta test upgrade * * @since 2.9.5 * @access public */ public function beta_test($zip = false) { if (!$zip) { if (empty($_REQUEST[self::BETA_TEST_URL])) { return; } $zip = $_REQUEST[self::BETA_TEST_URL]; if ($zip !== Debug2::BETA_TEST_URL_WP) { if ($zip === 'latest') { $zip = Debug2::BETA_TEST_URL_WP; } else { // Generate zip url $zip = $this->_package_zip($zip); } } } if (!$zip) { Debug2::debug('[Debug2] ❌ No ZIP file'); return; } Debug2::debug('[Debug2] ZIP file ' . $zip); $update_plugins = get_site_transient('update_plugins'); if (!is_object($update_plugins)) { $update_plugins = new \stdClass(); } $plugin_info = new \stdClass(); $plugin_info->new_version = Core::VER; $plugin_info->slug = Core::PLUGIN_NAME; $plugin_info->plugin = Core::PLUGIN_FILE; $plugin_info->package = $zip; $plugin_info->url = 'https://wordpress.org/plugins/litespeed-cache/'; $update_plugins->response[Core::PLUGIN_FILE] = $plugin_info; set_site_transient('update_plugins', $update_plugins); // Run upgrade Activation::cls()->upgrade(); } /** * Git package refresh * * @since 2.9.5 * @access private */ private function _package_zip($commit) { $data = array( 'commit' => $commit, ); $res = Cloud::get(Cloud::API_BETA_TEST, $data); if (empty($res['zip'])) { return false; } return $res['zip']; } /** * Log Purge headers separately * * @since 2.7 * @access public */ public static function log_purge($purge_header) { // Check if debug is ON if (!defined('LSCWP_LOG') && !defined('LSCWP_LOG_BYPASS_NOTADMIN')) { return; } $purge_file = self::cls()->path('purge'); self::cls()->_init_request($purge_file); $msg = $purge_header . self::_backtrace_info(6); File::append($purge_file, self::format_message($msg)); } /** * Enable debug log * * @since 1.1.0 * @access public */ public function init() { $debug = $this->conf(Base::O_DEBUG); if ($debug == Base::VAL_ON2) { if (!$this->cls('Router')->is_admin_ip()) { defined('LSCWP_LOG_BYPASS_NOTADMIN') || define('LSCWP_LOG_BYPASS_NOTADMIN', true); return; } } /** * Check if hit URI includes/excludes * This is after LSCWP_LOG_BYPASS_NOTADMIN to make `log_purge()` still work * @since 3.0 */ $list = $this->conf(Base::O_DEBUG_INC); if ($list) { $result = Utility::str_hit_array($_SERVER['REQUEST_URI'], $list); if (!$result) { return; } } $list = $this->conf(Base::O_DEBUG_EXC); if ($list) { $result = Utility::str_hit_array($_SERVER['REQUEST_URI'], $list); if ($result) { return; } } if (!defined('LSCWP_LOG')) { // If not initialized, do it now $this->_init_request(); define('LSCWP_LOG', true); } } /** * Create the initial log messages with the request parameters. * * @since 1.0.12 * @access private */ private function _init_request($log_file = null) { if (!$log_file) { $log_file = self::$log_path; } // Check log file size $log_file_size = $this->conf(Base::O_DEBUG_FILESIZE); if (file_exists($log_file) && filesize($log_file) > $log_file_size * 1000000) { File::save($log_file, ''); } // For more than 2s's requests, add more break if (file_exists($log_file) && time() - filemtime($log_file) > 2) { File::append($log_file, "\n\n\n\n"); } if (PHP_SAPI == 'cli') { return; } $servervars = array( 'Query String' => '', 'HTTP_ACCEPT' => '', 'HTTP_USER_AGENT' => '', 'HTTP_ACCEPT_ENCODING' => '', 'HTTP_COOKIE' => '', 'REQUEST_METHOD' => '', 'SERVER_PROTOCOL' => '', 'X-LSCACHE' => '', 'LSCACHE_VARY_COOKIE' => '', 'LSCACHE_VARY_VALUE' => '', 'ESI_CONTENT_TYPE' => '', ); $server = array_merge($servervars, $_SERVER); $params = array(); if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') { $server['SERVER_PROTOCOL'] .= ' (HTTPS) '; } $param = sprintf('💓 ------%s %s %s', $server['REQUEST_METHOD'], $server['SERVER_PROTOCOL'], strtok($server['REQUEST_URI'], '?')); $qs = !empty($server['QUERY_STRING']) ? $server['QUERY_STRING'] : ''; if ($this->conf(Base::O_DEBUG_COLLAPSE_QS)) { $qs = $this->_omit_long_message($qs); if ($qs) { $param .= ' ? ' . $qs; } $params[] = $param; } else { $params[] = $param; $params[] = 'Query String: ' . $qs; } if (!empty($_SERVER['HTTP_REFERER'])) { $params[] = 'HTTP_REFERER: ' . $this->_omit_long_message($server['HTTP_REFERER']); } if (defined('LSCWP_LOG_MORE')) { $params[] = 'User Agent: ' . $this->_omit_long_message($server['HTTP_USER_AGENT']); $params[] = 'Accept: ' . $server['HTTP_ACCEPT']; $params[] = 'Accept Encoding: ' . $server['HTTP_ACCEPT_ENCODING']; } // $params[] = 'Cookie: ' . $server['HTTP_COOKIE']; if (isset($_COOKIE['_lscache_vary'])) { $params[] = 'Cookie _lscache_vary: ' . $_COOKIE['_lscache_vary']; } if (defined('LSCWP_LOG_MORE')) { $params[] = 'X-LSCACHE: ' . (!empty($server['X-LSCACHE']) ? 'true' : 'false'); } if ($server['LSCACHE_VARY_COOKIE']) { $params[] = 'LSCACHE_VARY_COOKIE: ' . $server['LSCACHE_VARY_COOKIE']; } if ($server['LSCACHE_VARY_VALUE']) { $params[] = 'LSCACHE_VARY_VALUE: ' . $server['LSCACHE_VARY_VALUE']; } if ($server['ESI_CONTENT_TYPE']) { $params[] = 'ESI_CONTENT_TYPE: ' . $server['ESI_CONTENT_TYPE']; } $request = array_map(__CLASS__ . '::format_message', $params); File::append($log_file, $request); } /** * Trim long msg to keep log neat * @since 6.3 */ private function _omit_long_message($msg) { if (strlen($msg) > 53) { $msg = substr($msg, 0, 53) . '...'; } return $msg; } /** * Formats the log message with a consistent prefix. * * @since 1.0.12 * @access private * @param string $msg The log message to write. * @return string The formatted log message. */ private static function format_message($msg) { // If call here without calling get_enabled() first, improve compatibility if (!defined('LSCWP_LOG_TAG')) { return $msg . "\n"; } if (!isset(self::$_prefix)) { // address if (PHP_SAPI == 'cli') { $addr = '=CLI='; if (isset($_SERVER['USER'])) { $addr .= $_SERVER['USER']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $addr .= $_SERVER['HTTP_X_FORWARDED_FOR']; } } else { $addr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; $port = isset($_SERVER['REMOTE_PORT']) ? $_SERVER['REMOTE_PORT'] : ''; $addr = "$addr:$port"; } // Generate a unique string per request self::$_prefix = sprintf(' [%s %s %s] ', $addr, LSCWP_LOG_TAG, Str::rrand(3)); } list($usec, $sec) = explode(' ', microtime()); return date('m/d/y H:i:s', $sec + LITESPEED_TIME_OFFSET) . substr($usec, 1, 4) . self::$_prefix . $msg . "\n"; } /** * Direct call to log a debug message. * * @since 1.1.3 * @access public */ public static function debug($msg, $backtrace_limit = false) { if (!defined('LSCWP_LOG')) { return; } if (defined('LSCWP_DEBUG_EXC_STRINGS') && Utility::str_hit_array($msg, LSCWP_DEBUG_EXC_STRINGS)) { return; } if ($backtrace_limit !== false) { if (!is_numeric($backtrace_limit)) { $backtrace_limit = self::trim_longtext($backtrace_limit); if (is_array($backtrace_limit) && count($backtrace_limit) == 1 && !empty($backtrace_limit[0])) { $msg .= ' --- ' . $backtrace_limit[0]; } else { $msg .= ' --- ' . var_export($backtrace_limit, true); } self::push($msg); return; } self::push($msg, $backtrace_limit + 1); return; } self::push($msg); } /** * Trim long string before array dump * @since 3.3 */ public static function trim_longtext($backtrace_limit) { if (is_array($backtrace_limit)) { $backtrace_limit = array_map(__CLASS__ . '::trim_longtext', $backtrace_limit); } if (is_string($backtrace_limit) && strlen($backtrace_limit) > 500) { $backtrace_limit = substr($backtrace_limit, 0, 1000) . '...'; } return $backtrace_limit; } /** * Direct call to log an advanced debug message. * * @since 1.2.0 * @access public */ public static function debug2($msg, $backtrace_limit = false) { if (!defined('LSCWP_LOG_MORE')) { return; } self::debug($msg, $backtrace_limit); } /** * Logs a debug message. * * @since 1.1.0 * @access private * @param string $msg The debug message. * @param int $backtrace_limit Backtrace depth. */ private static function push($msg, $backtrace_limit = false) { // backtrace handler if (defined('LSCWP_LOG_MORE') && $backtrace_limit !== false) { $msg .= self::_backtrace_info($backtrace_limit); } File::append(self::$log_path, self::format_message($msg)); } /** * Backtrace info * * @since 2.7 */ private static function _backtrace_info($backtrace_limit) { $msg = ''; $trace = version_compare(PHP_VERSION, '5.4.0', '<') ? debug_backtrace() : debug_backtrace(false, $backtrace_limit + 3); for ($i = 2; $i <= $backtrace_limit + 2; $i++) { // 0st => _backtrace_info(), 1st => push() if (empty($trace[$i]['class'])) { if (empty($trace[$i]['file'])) { break; } $log = "\n" . $trace[$i]['file']; } else { if ($trace[$i]['class'] == __CLASS__) { continue; } $args = ''; if (!empty($trace[$i]['args'])) { foreach ($trace[$i]['args'] as $v) { if (is_array($v)) { $v = 'ARRAY'; } if (is_string($v) || is_numeric($v)) { $args .= $v . ','; } } $args = substr($args, 0, strlen($args) > 100 ? 100 : -1); } $log = str_replace('Core', 'LSC', $trace[$i]['class']) . $trace[$i]['type'] . $trace[$i]['function'] . '(' . $args . ')'; } if (!empty($trace[$i - 1]['line'])) { $log .= '@' . $trace[$i - 1]['line']; } $msg .= " => $log"; } return $msg; } /** * Clear log file * * @since 1.6.6 * @access private */ private function _clear_log() { $logs = array('debug', 'purge', 'crawler'); foreach ($logs as $log) { File::save($this->path($log), ''); } } /** * Handle all request actions from main cls * * @since 1.6.6 * @access public */ public function handler() { $type = Router::verify_type(); switch ($type) { case self::TYPE_CLEAR_LOG: $this->_clear_log(); break; case self::TYPE_BETA_TEST: $this->beta_test(); break; default: break; } Admin::redirect(); } } object-cache.cls.php 0000644 00000037530 15162226331 0010355 0 ustar 00 <?php /** * The object cache class * * @since 1.8 * @package LiteSpeed * @subpackage LiteSpeed/inc * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); require_once dirname(__DIR__) . '/autoload.php'; class Object_Cache extends Root { const O_DEBUG = 'debug'; const O_OBJECT = 'object'; const O_OBJECT_KIND = 'object-kind'; const O_OBJECT_HOST = 'object-host'; const O_OBJECT_PORT = 'object-port'; const O_OBJECT_LIFE = 'object-life'; const O_OBJECT_PERSISTENT = 'object-persistent'; const O_OBJECT_ADMIN = 'object-admin'; const O_OBJECT_TRANSIENTS = 'object-transients'; const O_OBJECT_DB_ID = 'object-db_id'; const O_OBJECT_USER = 'object-user'; const O_OBJECT_PSWD = 'object-pswd'; const O_OBJECT_GLOBAL_GROUPS = 'object-global_groups'; const O_OBJECT_NON_PERSISTENT_GROUPS = 'object-non_persistent_groups'; private $_conn; private $_cfg_debug; private $_cfg_enabled; private $_cfg_method; private $_cfg_host; private $_cfg_port; private $_cfg_life; private $_cfg_persistent; private $_cfg_admin; private $_cfg_transients; private $_cfg_db; private $_cfg_user; private $_cfg_pswd; private $_default_life = 360; private $_oc_driver = 'Memcached'; // Redis or Memcached private $_global_groups = array(); private $_non_persistent_groups = array(); /** * Init * * NOTE: this class may be included without initialized core * * @since 1.8 */ public function __construct($cfg = false) { if ($cfg) { if (!is_array($cfg[Base::O_OBJECT_GLOBAL_GROUPS])) { $cfg[Base::O_OBJECT_GLOBAL_GROUPS] = explode("\n", $cfg[Base::O_OBJECT_GLOBAL_GROUPS]); } if (!is_array($cfg[Base::O_OBJECT_NON_PERSISTENT_GROUPS])) { $cfg[Base::O_OBJECT_NON_PERSISTENT_GROUPS] = explode("\n", $cfg[Base::O_OBJECT_NON_PERSISTENT_GROUPS]); } $this->_cfg_debug = $cfg[Base::O_DEBUG] ? $cfg[Base::O_DEBUG] : false; $this->_cfg_method = $cfg[Base::O_OBJECT_KIND] ? true : false; $this->_cfg_host = $cfg[Base::O_OBJECT_HOST]; $this->_cfg_port = $cfg[Base::O_OBJECT_PORT]; $this->_cfg_life = $cfg[Base::O_OBJECT_LIFE]; $this->_cfg_persistent = $cfg[Base::O_OBJECT_PERSISTENT]; $this->_cfg_admin = $cfg[Base::O_OBJECT_ADMIN]; $this->_cfg_transients = $cfg[Base::O_OBJECT_TRANSIENTS]; $this->_cfg_db = $cfg[Base::O_OBJECT_DB_ID]; $this->_cfg_user = $cfg[Base::O_OBJECT_USER]; $this->_cfg_pswd = $cfg[Base::O_OBJECT_PSWD]; $this->_global_groups = $cfg[Base::O_OBJECT_GLOBAL_GROUPS]; $this->_non_persistent_groups = $cfg[Base::O_OBJECT_NON_PERSISTENT_GROUPS]; if ($this->_cfg_method) { $this->_oc_driver = 'Redis'; } $this->_cfg_enabled = $cfg[Base::O_OBJECT] && class_exists($this->_oc_driver) && $this->_cfg_host; } // If OC is OFF, will hit here to init OC after conf initialized elseif (defined('LITESPEED_CONF_LOADED')) { $this->_cfg_debug = $this->conf(Base::O_DEBUG) ? $this->conf(Base::O_DEBUG) : false; $this->_cfg_method = $this->conf(Base::O_OBJECT_KIND) ? true : false; $this->_cfg_host = $this->conf(Base::O_OBJECT_HOST); $this->_cfg_port = $this->conf(Base::O_OBJECT_PORT); $this->_cfg_life = $this->conf(Base::O_OBJECT_LIFE); $this->_cfg_persistent = $this->conf(Base::O_OBJECT_PERSISTENT); $this->_cfg_admin = $this->conf(Base::O_OBJECT_ADMIN); $this->_cfg_transients = $this->conf(Base::O_OBJECT_TRANSIENTS); $this->_cfg_db = $this->conf(Base::O_OBJECT_DB_ID); $this->_cfg_user = $this->conf(Base::O_OBJECT_USER); $this->_cfg_pswd = $this->conf(Base::O_OBJECT_PSWD); $this->_global_groups = $this->conf(Base::O_OBJECT_GLOBAL_GROUPS); $this->_non_persistent_groups = $this->conf(Base::O_OBJECT_NON_PERSISTENT_GROUPS); if ($this->_cfg_method) { $this->_oc_driver = 'Redis'; } $this->_cfg_enabled = $this->conf(Base::O_OBJECT) && class_exists($this->_oc_driver) && $this->_cfg_host; } elseif (defined('self::CONF_FILE') && file_exists(WP_CONTENT_DIR . '/' . self::CONF_FILE)) { // Get cfg from _data_file // Use self::const to avoid loading more classes $cfg = \json_decode(file_get_contents(WP_CONTENT_DIR . '/' . self::CONF_FILE), true); if (!empty($cfg[self::O_OBJECT_HOST])) { $this->_cfg_debug = !empty($cfg[Base::O_DEBUG]) ? $cfg[Base::O_DEBUG] : false; $this->_cfg_method = !empty($cfg[self::O_OBJECT_KIND]) ? $cfg[self::O_OBJECT_KIND] : false; $this->_cfg_host = $cfg[self::O_OBJECT_HOST]; $this->_cfg_port = $cfg[self::O_OBJECT_PORT]; $this->_cfg_life = !empty($cfg[self::O_OBJECT_LIFE]) ? $cfg[self::O_OBJECT_LIFE] : $this->_default_life; $this->_cfg_persistent = !empty($cfg[self::O_OBJECT_PERSISTENT]) ? $cfg[self::O_OBJECT_PERSISTENT] : false; $this->_cfg_admin = !empty($cfg[self::O_OBJECT_ADMIN]) ? $cfg[self::O_OBJECT_ADMIN] : false; $this->_cfg_transients = !empty($cfg[self::O_OBJECT_TRANSIENTS]) ? $cfg[self::O_OBJECT_TRANSIENTS] : false; $this->_cfg_db = !empty($cfg[self::O_OBJECT_DB_ID]) ? $cfg[self::O_OBJECT_DB_ID] : 0; $this->_cfg_user = !empty($cfg[self::O_OBJECT_USER]) ? $cfg[self::O_OBJECT_USER] : ''; $this->_cfg_pswd = !empty($cfg[self::O_OBJECT_PSWD]) ? $cfg[self::O_OBJECT_PSWD] : ''; $this->_global_groups = !empty($cfg[self::O_OBJECT_GLOBAL_GROUPS]) ? $cfg[self::O_OBJECT_GLOBAL_GROUPS] : array(); $this->_non_persistent_groups = !empty($cfg[self::O_OBJECT_NON_PERSISTENT_GROUPS]) ? $cfg[self::O_OBJECT_NON_PERSISTENT_GROUPS] : array(); if ($this->_cfg_method) { $this->_oc_driver = 'Redis'; } $this->_cfg_enabled = class_exists($this->_oc_driver) && $this->_cfg_host; } else { $this->_cfg_enabled = false; } } else { $this->_cfg_enabled = false; } } /** * Add debug. * * @since 6.3 * @access private */ private function debug_oc($text, $show_error = false) { if (defined('LSCWP_LOG')) { Debug2::debug($text); return; } if (!$show_error && $this->_cfg_debug != BASE::VAL_ON2) { return; } $LITESPEED_DATA_FOLDER = defined('LITESPEED_DATA_FOLDER') ? LITESPEED_DATA_FOLDER : 'litespeed'; $LSCWP_CONTENT_DIR = defined('LSCWP_CONTENT_DIR') ? LSCWP_CONTENT_DIR : WP_CONTENT_DIR; $LITESPEED_STATIC_DIR = $LSCWP_CONTENT_DIR . '/' . $LITESPEED_DATA_FOLDER; $log_path_prefix = $LITESPEED_STATIC_DIR . '/debug/'; $log_file = $log_path_prefix . Debug2::FilePath('debug'); if (file_exists($log_path_prefix . 'index.php') && file_exists($log_file)) { error_log(gmdate('m/d/y H:i:s') . ' - OC - ' . $text . PHP_EOL, 3, $log_file); } } /** * Get `Store Transients` setting value * * @since 1.8.3 * @access public */ public function store_transients($group) { return $this->_cfg_transients && $this->_is_transients_group($group); } /** * Check if the group belongs to transients or not * * @since 1.8.3 * @access private */ private function _is_transients_group($group) { return in_array($group, array('transient', 'site-transient')); } /** * Update WP object cache file config * * @since 1.8 * @access public */ public function update_file($options) { $changed = false; // NOTE: When included in oc.php, `LSCWP_DIR` will show undefined, so this must be assigned/generated when used $_oc_ori_file = LSCWP_DIR . 'lib/object-cache.php'; $_oc_wp_file = WP_CONTENT_DIR . '/object-cache.php'; // Update cls file if (!file_exists($_oc_wp_file) || md5_file($_oc_wp_file) !== md5_file($_oc_ori_file)) { $this->debug_oc('copying object-cache.php file to ' . $_oc_wp_file); copy($_oc_ori_file, $_oc_wp_file); $changed = true; } /** * Clear object cache */ if ($changed) { $this->_reconnect($options); } } /** * Remove object cache file * * @since 1.8.2 * @access public */ public function del_file() { // NOTE: When included in oc.php, `LSCWP_DIR` will show undefined, so this must be assigned/generated when used $_oc_ori_file = LSCWP_DIR . 'lib/object-cache.php'; $_oc_wp_file = WP_CONTENT_DIR . '/object-cache.php'; if (file_exists($_oc_wp_file) && md5_file($_oc_wp_file) === md5_file($_oc_ori_file)) { $this->debug_oc('removing ' . $_oc_wp_file); unlink($_oc_wp_file); } } /** * Try to build connection * * @since 1.8 * @access public */ public function test_connection() { return $this->_connect(); } /** * Force to connect with this setting * * @since 1.8 * @access private */ private function _reconnect($cfg) { $this->debug_oc('Reconnecting'); if (isset($this->_conn)) { // error_log( 'Object: Quitting existing connection!' ); $this->debug_oc('Quitting existing connection'); $this->flush(); $this->_conn = null; $this->cls(false, true); } $cls = $this->cls(false, false, $cfg); $cls->_connect(); if (isset($cls->_conn)) { $cls->flush(); } } /** * Connect to Memcached/Redis server * * @since 1.8 * @access private */ private function _connect() { if (isset($this->_conn)) { // error_log( 'Object: _connected' ); return true; } if (!class_exists($this->_oc_driver) || !$this->_cfg_host) { return null; } if (defined('LITESPEED_OC_FAILURE')) { return false; } $this->debug_oc('Init ' . $this->_oc_driver . ' connection to ' . $this->_cfg_host . ':' . $this->_cfg_port); $failed = false; /** * Connect to Redis * * @since 1.8.1 * @see https://github.com/phpredis/phpredis/#example-1 */ if ($this->_oc_driver == 'Redis') { set_error_handler('litespeed_exception_handler'); try { $this->_conn = new \Redis(); // error_log( 'Object: _connect Redis' ); if ($this->_cfg_persistent) { if ($this->_cfg_port) { $this->_conn->pconnect($this->_cfg_host, $this->_cfg_port); } else { $this->_conn->pconnect($this->_cfg_host); } } else { if ($this->_cfg_port) { $this->_conn->connect($this->_cfg_host, $this->_cfg_port); } else { $this->_conn->connect($this->_cfg_host); } } if ($this->_cfg_pswd) { if ($this->_cfg_user) { $this->_conn->auth(array($this->_cfg_user, $this->_cfg_pswd)); } else { $this->_conn->auth($this->_cfg_pswd); } } if ($this->_cfg_db) { $this->_conn->select($this->_cfg_db); } $res = $this->_conn->ping(); if ($res != '+PONG') { $failed = true; } } catch (\Exception $e) { $this->debug_oc('Redis connect exception: ' . $e->getMessage(), true); $failed = true; } catch (\ErrorException $e) { $this->debug_oc('Redis connect error: ' . $e->getMessage(), true); $failed = true; } restore_error_handler(); } else { // Connect to Memcached if ($this->_cfg_persistent) { $this->_conn = new \Memcached($this->_get_mem_id()); // Check memcached persistent connection if ($this->_validate_mem_server()) { // error_log( 'Object: _validate_mem_server' ); $this->debug_oc('Got persistent ' . $this->_oc_driver . ' connection'); return true; } $this->debug_oc('No persistent ' . $this->_oc_driver . ' server list!'); } else { // error_log( 'Object: new memcached!' ); $this->_conn = new \Memcached(); } $this->_conn->addServer($this->_cfg_host, (int) $this->_cfg_port); /** * Add SASL auth * @since 1.8.1 * @since 2.9.6 Fixed SASL connection @see https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:lsmcd:new_sasl */ if ($this->_cfg_user && $this->_cfg_pswd && method_exists($this->_conn, 'setSaslAuthData')) { $this->_conn->setOption(\Memcached::OPT_BINARY_PROTOCOL, true); $this->_conn->setOption(\Memcached::OPT_COMPRESSION, false); $this->_conn->setSaslAuthData($this->_cfg_user, $this->_cfg_pswd); } // Check connection if (!$this->_validate_mem_server()) { $failed = true; } } // If failed to connect if ($failed) { $this->debug_oc('❌ Failed to connect ' . $this->_oc_driver . ' server!', true); $this->_conn = null; $this->_cfg_enabled = false; !defined('LITESPEED_OC_FAILURE') && define('LITESPEED_OC_FAILURE', true); // error_log( 'Object: false!' ); return false; } $this->debug_oc('Connected'); return true; } /** * Check if the connected memcached host is the one in cfg * * @since 1.8 * @access private */ private function _validate_mem_server() { $mem_list = $this->_conn->getStats(); if (empty($mem_list)) { return false; } foreach ($mem_list as $k => $v) { if (substr($k, 0, strlen($this->_cfg_host)) != $this->_cfg_host) { continue; } if (!empty($v['pid']) || !empty($v['curr_connections'])) { return true; } } return false; } /** * Get memcached unique id to be used for connecting * * @since 1.8 * @access private */ private function _get_mem_id() { $mem_id = 'litespeed'; if (is_multisite()) { $mem_id .= '_' . get_current_blog_id(); } return $mem_id; } /** * Get cache * * @since 1.8 * @access public */ public function get($key) { if (!$this->_cfg_enabled) { return null; } if (!$this->_can_cache()) { return null; } if (!$this->_connect()) { return null; } $res = $this->_conn->get($key); return $res; } /** * Set cache * * @since 1.8 * @access public */ public function set($key, $data, $expire) { if (!$this->_cfg_enabled) { return null; } /** * To fix the Cloud callback cached as its frontend call but the hash is generated in backend * Bug found by Stan at Jan/10/2020 */ // if ( ! $this->_can_cache() ) { // return null; // } if (!$this->_connect()) { return null; } $ttl = $expire ?: $this->_cfg_life; if ($this->_oc_driver == 'Redis') { try { $res = $this->_conn->setEx($key, $ttl, $data); } catch (\RedisException $ex) { $res = false; $msg = sprintf(__('Redis encountered a fatal error: %s (code: %d)', 'litespeed-cache'), $ex->getMessage(), $ex->getCode()); $this->debug_oc($msg); Admin_Display::error($msg); } } else { $res = $this->_conn->set($key, $data, $ttl); } return $res; } /** * Check if can cache or not * * @since 1.8 * @access private */ private function _can_cache() { if (!$this->_cfg_admin && defined('WP_ADMIN')) { return false; } return true; } /** * Delete cache * * @since 1.8 * @access public */ public function delete($key) { if (!$this->_cfg_enabled) { return null; } if (!$this->_connect()) { return null; } if ($this->_oc_driver == 'Redis') { $res = $this->_conn->del($key); } else { $res = $this->_conn->delete($key); } return (bool) $res; } /** * Clear all cache * * @since 1.8 * @access public */ public function flush() { if (!$this->_cfg_enabled) { $this->debug_oc('bypass flushing'); return null; } if (!$this->_connect()) { return null; } $this->debug_oc('flush!'); if ($this->_oc_driver == 'Redis') { $res = $this->_conn->flushDb(); } else { $res = $this->_conn->flush(); $this->_conn->resetServerList(); } return $res; } /** * Add global groups * * @since 1.8 * @access public */ public function add_global_groups($groups) { if (!is_array($groups)) { $groups = array($groups); } $this->_global_groups = array_merge($this->_global_groups, $groups); $this->_global_groups = array_unique($this->_global_groups); } /** * Check if is in global groups or not * * @since 1.8 * @access public */ public function is_global($group) { return in_array($group, $this->_global_groups); } /** * Add non persistent groups * * @since 1.8 * @access public */ public function add_non_persistent_groups($groups) { if (!is_array($groups)) { $groups = array($groups); } $this->_non_persistent_groups = array_merge($this->_non_persistent_groups, $groups); $this->_non_persistent_groups = array_unique($this->_non_persistent_groups); } /** * Check if is in non persistent groups or not * * @since 1.8 * @access public */ public function is_non_persistent($group) { return in_array($group, $this->_non_persistent_groups); } } base.cls.php 0000644 00000075165 15162226333 0006770 0 ustar 00 <?php /** * The base consts * * @since 3.7 */ namespace LiteSpeed; defined('WPINC') || exit(); class Base extends Root { // This is redundant since v3.0 // New conf items are `litespeed.key` const OPTION_NAME = 'litespeed-cache-conf'; const _CACHE = '_cache'; // final cache status from setting ## -------------------------------------------------- ## ## -------------- General ----------------- ## ## -------------------------------------------------- ## const _VER = '_version'; // Not set-able const HASH = 'hash'; // Not set-able const O_AUTO_UPGRADE = 'auto_upgrade'; const O_API_KEY = 'api_key'; // Deprecated since v6.4. TODO: Will drop after v6.5 const O_SERVER_IP = 'server_ip'; const O_GUEST = 'guest'; const O_GUEST_OPTM = 'guest_optm'; const O_NEWS = 'news'; const O_GUEST_UAS = 'guest_uas'; const O_GUEST_IPS = 'guest_ips'; ## -------------------------------------------------- ## ## -------------- Cache ----------------- ## ## -------------------------------------------------- ## const O_CACHE = 'cache'; const O_CACHE_PRIV = 'cache-priv'; const O_CACHE_COMMENTER = 'cache-commenter'; const O_CACHE_REST = 'cache-rest'; const O_CACHE_PAGE_LOGIN = 'cache-page_login'; const O_CACHE_FAVICON = 'cache-favicon'; // Deprecated since v6.2. TODO: Will drop after v6.5 const O_CACHE_RES = 'cache-resources'; const O_CACHE_MOBILE = 'cache-mobile'; const O_CACHE_MOBILE_RULES = 'cache-mobile_rules'; const O_CACHE_BROWSER = 'cache-browser'; const O_CACHE_EXC_USERAGENTS = 'cache-exc_useragents'; const O_CACHE_EXC_COOKIES = 'cache-exc_cookies'; const O_CACHE_EXC_QS = 'cache-exc_qs'; const O_CACHE_EXC_CAT = 'cache-exc_cat'; const O_CACHE_EXC_TAG = 'cache-exc_tag'; const O_CACHE_FORCE_URI = 'cache-force_uri'; const O_CACHE_FORCE_PUB_URI = 'cache-force_pub_uri'; const O_CACHE_PRIV_URI = 'cache-priv_uri'; const O_CACHE_EXC = 'cache-exc'; const O_CACHE_EXC_ROLES = 'cache-exc_roles'; const O_CACHE_DROP_QS = 'cache-drop_qs'; const O_CACHE_TTL_PUB = 'cache-ttl_pub'; const O_CACHE_TTL_PRIV = 'cache-ttl_priv'; const O_CACHE_TTL_FRONTPAGE = 'cache-ttl_frontpage'; const O_CACHE_TTL_FEED = 'cache-ttl_feed'; const O_CACHE_TTL_REST = 'cache-ttl_rest'; const O_CACHE_TTL_STATUS = 'cache-ttl_status'; const O_CACHE_TTL_BROWSER = 'cache-ttl_browser'; const O_CACHE_AJAX_TTL = 'cache-ajax_ttl'; const O_CACHE_LOGIN_COOKIE = 'cache-login_cookie'; const O_CACHE_VARY_COOKIES = 'cache-vary_cookies'; const O_CACHE_VARY_GROUP = 'cache-vary_group'; ## -------------------------------------------------- ## ## -------------- Purge ----------------- ## ## -------------------------------------------------- ## const O_PURGE_ON_UPGRADE = 'purge-upgrade'; const O_PURGE_STALE = 'purge-stale'; const O_PURGE_POST_ALL = 'purge-post_all'; const O_PURGE_POST_FRONTPAGE = 'purge-post_f'; const O_PURGE_POST_HOMEPAGE = 'purge-post_h'; const O_PURGE_POST_PAGES = 'purge-post_p'; const O_PURGE_POST_PAGES_WITH_RECENT_POSTS = 'purge-post_pwrp'; const O_PURGE_POST_AUTHOR = 'purge-post_a'; const O_PURGE_POST_YEAR = 'purge-post_y'; const O_PURGE_POST_MONTH = 'purge-post_m'; const O_PURGE_POST_DATE = 'purge-post_d'; const O_PURGE_POST_TERM = 'purge-post_t'; // include category|tag|tax const O_PURGE_POST_POSTTYPE = 'purge-post_pt'; const O_PURGE_TIMED_URLS = 'purge-timed_urls'; const O_PURGE_TIMED_URLS_TIME = 'purge-timed_urls_time'; const O_PURGE_HOOK_ALL = 'purge-hook_all'; ## -------------------------------------------------- ## ## -------------- ESI ----------------- ## ## -------------------------------------------------- ## const O_ESI = 'esi'; const O_ESI_CACHE_ADMBAR = 'esi-cache_admbar'; const O_ESI_CACHE_COMMFORM = 'esi-cache_commform'; const O_ESI_NONCE = 'esi-nonce'; ## -------------------------------------------------- ## ## -------------- Utilities ----------------- ## ## -------------------------------------------------- ## const O_UTIL_INSTANT_CLICK = 'util-instant_click'; const O_UTIL_NO_HTTPS_VARY = 'util-no_https_vary'; ## -------------------------------------------------- ## ## -------------- Debug ----------------- ## ## -------------------------------------------------- ## const O_DEBUG_DISABLE_ALL = 'debug-disable_all'; const O_DEBUG = 'debug'; const O_DEBUG_IPS = 'debug-ips'; const O_DEBUG_LEVEL = 'debug-level'; const O_DEBUG_FILESIZE = 'debug-filesize'; const O_DEBUG_COOKIE = 'debug-cookie'; // For backwards compatibility, will drop after v7.0 const O_DEBUG_COLLAPSE_QS = 'debug-collapse_qs'; const O_DEBUG_COLLAPS_QS = 'debug-collapse_qs'; // For backwards compatibility, will drop after v6.5 const O_DEBUG_INC = 'debug-inc'; const O_DEBUG_EXC = 'debug-exc'; const O_DEBUG_EXC_STRINGS = 'debug-exc_strings'; ## -------------------------------------------------- ## ## -------------- DB Optm ----------------- ## ## -------------------------------------------------- ## const O_DB_OPTM_REVISIONS_MAX = 'db_optm-revisions_max'; const O_DB_OPTM_REVISIONS_AGE = 'db_optm-revisions_age'; ## -------------------------------------------------- ## ## -------------- HTML Optm ----------------- ## ## -------------------------------------------------- ## const O_OPTM_CSS_MIN = 'optm-css_min'; const O_OPTM_CSS_COMB = 'optm-css_comb'; const O_OPTM_CSS_COMB_EXT_INL = 'optm-css_comb_ext_inl'; const O_OPTM_UCSS = 'optm-ucss'; const O_OPTM_UCSS_INLINE = 'optm-ucss_inline'; const O_OPTM_UCSS_SELECTOR_WHITELIST = 'optm-ucss_whitelist'; const O_OPTM_UCSS_FILE_EXC_INLINE = 'optm-ucss_file_exc_inline'; const O_OPTM_UCSS_EXC = 'optm-ucss_exc'; const O_OPTM_CSS_EXC = 'optm-css_exc'; const O_OPTM_JS_MIN = 'optm-js_min'; const O_OPTM_JS_COMB = 'optm-js_comb'; const O_OPTM_JS_COMB_EXT_INL = 'optm-js_comb_ext_inl'; const O_OPTM_JS_DELAY_INC = 'optm-js_delay_inc'; const O_OPTM_JS_EXC = 'optm-js_exc'; const O_OPTM_HTML_MIN = 'optm-html_min'; const O_OPTM_HTML_LAZY = 'optm-html_lazy'; const O_OPTM_HTML_SKIP_COMMENTS = 'optm-html_skip_comment'; const O_OPTM_QS_RM = 'optm-qs_rm'; const O_OPTM_GGFONTS_RM = 'optm-ggfonts_rm'; const O_OPTM_CSS_ASYNC = 'optm-css_async'; const O_OPTM_CCSS_PER_URL = 'optm-ccss_per_url'; const O_OPTM_CCSS_SEP_POSTTYPE = 'optm-ccss_sep_posttype'; const O_OPTM_CCSS_SEP_URI = 'optm-ccss_sep_uri'; const O_OPTM_CCSS_SELECTOR_WHITELIST = 'optm-ccss_whitelist'; const O_OPTM_CSS_ASYNC_INLINE = 'optm-css_async_inline'; const O_OPTM_CSS_FONT_DISPLAY = 'optm-css_font_display'; const O_OPTM_JS_DEFER = 'optm-js_defer'; const O_OPTM_LOCALIZE = 'optm-localize'; const O_OPTM_LOCALIZE_DOMAINS = 'optm-localize_domains'; const O_OPTM_EMOJI_RM = 'optm-emoji_rm'; const O_OPTM_NOSCRIPT_RM = 'optm-noscript_rm'; const O_OPTM_GGFONTS_ASYNC = 'optm-ggfonts_async'; const O_OPTM_EXC_ROLES = 'optm-exc_roles'; const O_OPTM_CCSS_CON = 'optm-ccss_con'; const O_OPTM_JS_DEFER_EXC = 'optm-js_defer_exc'; const O_OPTM_GM_JS_EXC = 'optm-gm_js_exc'; const O_OPTM_DNS_PREFETCH = 'optm-dns_prefetch'; const O_OPTM_DNS_PREFETCH_CTRL = 'optm-dns_prefetch_ctrl'; const O_OPTM_DNS_PRECONNECT = 'optm-dns_preconnect'; const O_OPTM_EXC = 'optm-exc'; const O_OPTM_GUEST_ONLY = 'optm-guest_only'; ## -------------------------------------------------- ## ## -------------- Object Cache ----------------- ## ## -------------------------------------------------- ## const O_OBJECT = 'object'; const O_OBJECT_KIND = 'object-kind'; const O_OBJECT_HOST = 'object-host'; const O_OBJECT_PORT = 'object-port'; const O_OBJECT_LIFE = 'object-life'; const O_OBJECT_PERSISTENT = 'object-persistent'; const O_OBJECT_ADMIN = 'object-admin'; const O_OBJECT_TRANSIENTS = 'object-transients'; const O_OBJECT_DB_ID = 'object-db_id'; const O_OBJECT_USER = 'object-user'; const O_OBJECT_PSWD = 'object-pswd'; const O_OBJECT_GLOBAL_GROUPS = 'object-global_groups'; const O_OBJECT_NON_PERSISTENT_GROUPS = 'object-non_persistent_groups'; ## -------------------------------------------------- ## ## -------------- Discussion ----------------- ## ## -------------------------------------------------- ## const O_DISCUSS_AVATAR_CACHE = 'discuss-avatar_cache'; const O_DISCUSS_AVATAR_CRON = 'discuss-avatar_cron'; const O_DISCUSS_AVATAR_CACHE_TTL = 'discuss-avatar_cache_ttl'; ## -------------------------------------------------- ## ## -------------- Media ----------------- ## ## -------------------------------------------------- ## const O_MEDIA_PRELOAD_FEATURED = 'media-preload_featured'; // Deprecated since v6.2. TODO: Will drop after v6.5 const O_MEDIA_LAZY = 'media-lazy'; const O_MEDIA_LAZY_PLACEHOLDER = 'media-lazy_placeholder'; const O_MEDIA_PLACEHOLDER_RESP = 'media-placeholder_resp'; const O_MEDIA_PLACEHOLDER_RESP_COLOR = 'media-placeholder_resp_color'; const O_MEDIA_PLACEHOLDER_RESP_SVG = 'media-placeholder_resp_svg'; const O_MEDIA_LQIP = 'media-lqip'; const O_MEDIA_LQIP_QUAL = 'media-lqip_qual'; const O_MEDIA_LQIP_MIN_W = 'media-lqip_min_w'; const O_MEDIA_LQIP_MIN_H = 'media-lqip_min_h'; const O_MEDIA_PLACEHOLDER_RESP_ASYNC = 'media-placeholder_resp_async'; const O_MEDIA_IFRAME_LAZY = 'media-iframe_lazy'; const O_MEDIA_ADD_MISSING_SIZES = 'media-add_missing_sizes'; const O_MEDIA_LAZY_EXC = 'media-lazy_exc'; const O_MEDIA_LAZY_CLS_EXC = 'media-lazy_cls_exc'; const O_MEDIA_LAZY_PARENT_CLS_EXC = 'media-lazy_parent_cls_exc'; const O_MEDIA_IFRAME_LAZY_CLS_EXC = 'media-iframe_lazy_cls_exc'; const O_MEDIA_IFRAME_LAZY_PARENT_CLS_EXC = 'media-iframe_lazy_parent_cls_exc'; const O_MEDIA_LAZY_URI_EXC = 'media-lazy_uri_exc'; const O_MEDIA_LQIP_EXC = 'media-lqip_exc'; const O_MEDIA_VPI = 'media-vpi'; const O_MEDIA_VPI_CRON = 'media-vpi_cron'; const O_IMG_OPTM_JPG_QUALITY = 'img_optm-jpg_quality'; ## -------------------------------------------------- ## ## -------------- Image Optm ----------------- ## ## -------------------------------------------------- ## const O_IMG_OPTM_AUTO = 'img_optm-auto'; const O_IMG_OPTM_CRON = 'img_optm-cron'; // @Deprecated since v7.0 TODO: remove after v7.5 const O_IMG_OPTM_ORI = 'img_optm-ori'; const O_IMG_OPTM_RM_BKUP = 'img_optm-rm_bkup'; const O_IMG_OPTM_WEBP = 'img_optm-webp'; const O_IMG_OPTM_LOSSLESS = 'img_optm-lossless'; const O_IMG_OPTM_EXIF = 'img_optm-exif'; const O_IMG_OPTM_WEBP_ATTR = 'img_optm-webp_attr'; const O_IMG_OPTM_WEBP_REPLACE_SRCSET = 'img_optm-webp_replace_srcset'; ## -------------------------------------------------- ## ## -------------- Crawler ----------------- ## ## -------------------------------------------------- ## const O_CRAWLER = 'crawler'; const O_CRAWLER_USLEEP = 'crawler-usleep'; // @Deprecated since v7.0 TODO: remove after v7.5 const O_CRAWLER_RUN_DURATION = 'crawler-run_duration'; // @Deprecated since v7.0 TODO: remove after v7.5 const O_CRAWLER_RUN_INTERVAL = 'crawler-run_interval'; // @Deprecated since v7.0 TODO: remove after v7.5 const O_CRAWLER_CRAWL_INTERVAL = 'crawler-crawl_interval'; const O_CRAWLER_THREADS = 'crawler-threads'; // @Deprecated since v7.0 TODO: remove after v7.5 const O_CRAWLER_TIMEOUT = 'crawler-timeout'; // @Deprecated since v7.0 TODO: remove after v7.5 const O_CRAWLER_LOAD_LIMIT = 'crawler-load_limit'; const O_CRAWLER_SITEMAP = 'crawler-sitemap'; const O_CRAWLER_DROP_DOMAIN = 'crawler-drop_domain'; // @Deprecated since v7.0 TODO: remove after v7.5 const O_CRAWLER_MAP_TIMEOUT = 'crawler-map_timeout'; // @Deprecated since v7.0 TODO: remove after v7.5 const O_CRAWLER_ROLES = 'crawler-roles'; const O_CRAWLER_COOKIES = 'crawler-cookies'; ## -------------------------------------------------- ## ## -------------- Misc ----------------- ## ## -------------------------------------------------- ## const O_MISC_HEARTBEAT_FRONT = 'misc-heartbeat_front'; const O_MISC_HEARTBEAT_FRONT_TTL = 'misc-heartbeat_front_ttl'; const O_MISC_HEARTBEAT_BACK = 'misc-heartbeat_back'; const O_MISC_HEARTBEAT_BACK_TTL = 'misc-heartbeat_back_ttl'; const O_MISC_HEARTBEAT_EDITOR = 'misc-heartbeat_editor'; const O_MISC_HEARTBEAT_EDITOR_TTL = 'misc-heartbeat_editor_ttl'; ## -------------------------------------------------- ## ## -------------- CDN ----------------- ## ## -------------------------------------------------- ## const O_CDN = 'cdn'; const O_CDN_ORI = 'cdn-ori'; const O_CDN_ORI_DIR = 'cdn-ori_dir'; const O_CDN_EXC = 'cdn-exc'; const O_CDN_QUIC = 'cdn-quic'; // No more a visible setting since v7 const O_CDN_CLOUDFLARE = 'cdn-cloudflare'; const O_CDN_CLOUDFLARE_EMAIL = 'cdn-cloudflare_email'; const O_CDN_CLOUDFLARE_KEY = 'cdn-cloudflare_key'; const O_CDN_CLOUDFLARE_NAME = 'cdn-cloudflare_name'; const O_CDN_CLOUDFLARE_ZONE = 'cdn-cloudflare_zone'; const O_CDN_MAPPING = 'cdn-mapping'; const O_CDN_ATTR = 'cdn-attr'; const O_QC_NAMESERVERS = 'qc-nameservers'; const O_QC_CNAME = 'qc-cname'; const NETWORK_O_USE_PRIMARY = 'use_primary_settings'; /*** Other consts ***/ const O_GUIDE = 'litespeed-guide'; // Array of each guidance tag as key, step as val //xx todo: may need to remove // Server variables const ENV_CRAWLER_USLEEP = 'CRAWLER_USLEEP'; const ENV_CRAWLER_LOAD_LIMIT = 'CRAWLER_LOAD_LIMIT'; const ENV_CRAWLER_LOAD_LIMIT_ENFORCE = 'CRAWLER_LOAD_LIMIT_ENFORCE'; const CRWL_COOKIE_NAME = 'name'; const CRWL_COOKIE_VALS = 'vals'; const CDN_MAPPING_URL = 'url'; const CDN_MAPPING_INC_IMG = 'inc_img'; const CDN_MAPPING_INC_CSS = 'inc_css'; const CDN_MAPPING_INC_JS = 'inc_js'; const CDN_MAPPING_FILETYPE = 'filetype'; const VAL_OFF = 0; const VAL_ON = 1; const VAL_ON2 = 2; /* This is for API hook usage */ const IMG_OPTM_BM_ORI = 1; // @Deprecated since v7.0 const IMG_OPTM_BM_WEBP = 2; // @Deprecated since v7.0 const IMG_OPTM_BM_LOSSLESS = 4; // @Deprecated since v7.0 const IMG_OPTM_BM_EXIF = 8; // @Deprecated since v7.0 const IMG_OPTM_BM_AVIF = 16; // @Deprecated since v7.0 /* Site related options (Will not overwrite other sites' config) */ protected static $SINGLE_SITE_OPTIONS = array( self::O_CRAWLER, self::O_CRAWLER_SITEMAP, self::O_CDN, self::O_CDN_ORI, self::O_CDN_ORI_DIR, self::O_CDN_EXC, self::O_CDN_CLOUDFLARE, self::O_CDN_CLOUDFLARE_EMAIL, self::O_CDN_CLOUDFLARE_KEY, self::O_CDN_CLOUDFLARE_NAME, self::O_CDN_CLOUDFLARE_ZONE, self::O_CDN_MAPPING, self::O_CDN_ATTR, self::O_QC_NAMESERVERS, self::O_QC_CNAME, ); protected static $_default_options = array( self::_VER => '', self::HASH => '', self::O_API_KEY => '', self::O_AUTO_UPGRADE => false, self::O_SERVER_IP => '', self::O_GUEST => false, self::O_GUEST_OPTM => false, self::O_NEWS => false, self::O_GUEST_UAS => array(), self::O_GUEST_IPS => array(), // Cache self::O_CACHE => false, self::O_CACHE_PRIV => false, self::O_CACHE_COMMENTER => false, self::O_CACHE_REST => false, self::O_CACHE_PAGE_LOGIN => false, self::O_CACHE_RES => false, self::O_CACHE_MOBILE => false, self::O_CACHE_MOBILE_RULES => array(), self::O_CACHE_BROWSER => false, self::O_CACHE_EXC_USERAGENTS => array(), self::O_CACHE_EXC_COOKIES => array(), self::O_CACHE_EXC_QS => array(), self::O_CACHE_EXC_CAT => array(), self::O_CACHE_EXC_TAG => array(), self::O_CACHE_FORCE_URI => array(), self::O_CACHE_FORCE_PUB_URI => array(), self::O_CACHE_PRIV_URI => array(), self::O_CACHE_EXC => array(), self::O_CACHE_EXC_ROLES => array(), self::O_CACHE_DROP_QS => array(), self::O_CACHE_TTL_PUB => 0, self::O_CACHE_TTL_PRIV => 0, self::O_CACHE_TTL_FRONTPAGE => 0, self::O_CACHE_TTL_FEED => 0, self::O_CACHE_TTL_REST => 0, self::O_CACHE_TTL_BROWSER => 0, self::O_CACHE_TTL_STATUS => array(), self::O_CACHE_LOGIN_COOKIE => '', self::O_CACHE_AJAX_TTL => array(), self::O_CACHE_VARY_COOKIES => array(), self::O_CACHE_VARY_GROUP => array(), // Purge self::O_PURGE_ON_UPGRADE => false, self::O_PURGE_STALE => false, self::O_PURGE_POST_ALL => false, self::O_PURGE_POST_FRONTPAGE => false, self::O_PURGE_POST_HOMEPAGE => false, self::O_PURGE_POST_PAGES => false, self::O_PURGE_POST_PAGES_WITH_RECENT_POSTS => false, self::O_PURGE_POST_AUTHOR => false, self::O_PURGE_POST_YEAR => false, self::O_PURGE_POST_MONTH => false, self::O_PURGE_POST_DATE => false, self::O_PURGE_POST_TERM => false, self::O_PURGE_POST_POSTTYPE => false, self::O_PURGE_TIMED_URLS => array(), self::O_PURGE_TIMED_URLS_TIME => '', self::O_PURGE_HOOK_ALL => array(), // ESI self::O_ESI => false, self::O_ESI_CACHE_ADMBAR => false, self::O_ESI_CACHE_COMMFORM => false, self::O_ESI_NONCE => array(), // Util self::O_UTIL_INSTANT_CLICK => false, self::O_UTIL_NO_HTTPS_VARY => false, // Debug self::O_DEBUG_DISABLE_ALL => false, self::O_DEBUG => false, self::O_DEBUG_IPS => array(), self::O_DEBUG_LEVEL => false, self::O_DEBUG_FILESIZE => 0, self::O_DEBUG_COLLAPSE_QS => false, self::O_DEBUG_INC => array(), self::O_DEBUG_EXC => array(), self::O_DEBUG_EXC_STRINGS => array(), // DB Optm self::O_DB_OPTM_REVISIONS_MAX => 0, self::O_DB_OPTM_REVISIONS_AGE => 0, // HTML Optm self::O_OPTM_CSS_MIN => false, self::O_OPTM_CSS_COMB => false, self::O_OPTM_CSS_COMB_EXT_INL => false, self::O_OPTM_UCSS => false, self::O_OPTM_UCSS_INLINE => false, self::O_OPTM_UCSS_SELECTOR_WHITELIST => array(), self::O_OPTM_UCSS_FILE_EXC_INLINE => array(), self::O_OPTM_UCSS_EXC => array(), self::O_OPTM_CSS_EXC => array(), self::O_OPTM_JS_MIN => false, self::O_OPTM_JS_COMB => false, self::O_OPTM_JS_COMB_EXT_INL => false, self::O_OPTM_JS_DELAY_INC => array(), self::O_OPTM_JS_EXC => array(), self::O_OPTM_HTML_MIN => false, self::O_OPTM_HTML_LAZY => array(), self::O_OPTM_HTML_SKIP_COMMENTS => array(), self::O_OPTM_QS_RM => false, self::O_OPTM_GGFONTS_RM => false, self::O_OPTM_CSS_ASYNC => false, self::O_OPTM_CCSS_PER_URL => false, self::O_OPTM_CCSS_SEP_POSTTYPE => array(), self::O_OPTM_CCSS_SEP_URI => array(), self::O_OPTM_CCSS_SELECTOR_WHITELIST => array(), self::O_OPTM_CSS_ASYNC_INLINE => false, self::O_OPTM_CSS_FONT_DISPLAY => false, self::O_OPTM_JS_DEFER => false, self::O_OPTM_EMOJI_RM => false, self::O_OPTM_NOSCRIPT_RM => false, self::O_OPTM_GGFONTS_ASYNC => false, self::O_OPTM_EXC_ROLES => array(), self::O_OPTM_CCSS_CON => '', self::O_OPTM_JS_DEFER_EXC => array(), self::O_OPTM_GM_JS_EXC => array(), self::O_OPTM_DNS_PREFETCH => array(), self::O_OPTM_DNS_PREFETCH_CTRL => false, self::O_OPTM_DNS_PRECONNECT => array(), self::O_OPTM_EXC => array(), self::O_OPTM_GUEST_ONLY => false, // Object self::O_OBJECT => false, self::O_OBJECT_KIND => false, self::O_OBJECT_HOST => '', self::O_OBJECT_PORT => 0, self::O_OBJECT_LIFE => 0, self::O_OBJECT_PERSISTENT => false, self::O_OBJECT_ADMIN => false, self::O_OBJECT_TRANSIENTS => false, self::O_OBJECT_DB_ID => 0, self::O_OBJECT_USER => '', self::O_OBJECT_PSWD => '', self::O_OBJECT_GLOBAL_GROUPS => array(), self::O_OBJECT_NON_PERSISTENT_GROUPS => array(), // Discuss self::O_DISCUSS_AVATAR_CACHE => false, self::O_DISCUSS_AVATAR_CRON => false, self::O_DISCUSS_AVATAR_CACHE_TTL => 0, self::O_OPTM_LOCALIZE => false, self::O_OPTM_LOCALIZE_DOMAINS => array(), // Media self::O_MEDIA_LAZY => false, self::O_MEDIA_LAZY_PLACEHOLDER => '', self::O_MEDIA_PLACEHOLDER_RESP => false, self::O_MEDIA_PLACEHOLDER_RESP_COLOR => '', self::O_MEDIA_PLACEHOLDER_RESP_SVG => '', self::O_MEDIA_LQIP => false, self::O_MEDIA_LQIP_QUAL => 0, self::O_MEDIA_LQIP_MIN_W => 0, self::O_MEDIA_LQIP_MIN_H => 0, self::O_MEDIA_PLACEHOLDER_RESP_ASYNC => false, self::O_MEDIA_IFRAME_LAZY => false, self::O_MEDIA_ADD_MISSING_SIZES => false, self::O_MEDIA_LAZY_EXC => array(), self::O_MEDIA_LAZY_CLS_EXC => array(), self::O_MEDIA_LAZY_PARENT_CLS_EXC => array(), self::O_MEDIA_IFRAME_LAZY_CLS_EXC => array(), self::O_MEDIA_IFRAME_LAZY_PARENT_CLS_EXC => array(), self::O_MEDIA_LAZY_URI_EXC => array(), self::O_MEDIA_LQIP_EXC => array(), self::O_MEDIA_VPI => false, self::O_MEDIA_VPI_CRON => false, // Image Optm self::O_IMG_OPTM_AUTO => false, self::O_IMG_OPTM_ORI => false, self::O_IMG_OPTM_RM_BKUP => false, self::O_IMG_OPTM_WEBP => false, self::O_IMG_OPTM_LOSSLESS => false, self::O_IMG_OPTM_EXIF => false, self::O_IMG_OPTM_WEBP_ATTR => array(), self::O_IMG_OPTM_WEBP_REPLACE_SRCSET => false, self::O_IMG_OPTM_JPG_QUALITY => 0, // Crawler self::O_CRAWLER => false, self::O_CRAWLER_CRAWL_INTERVAL => 0, self::O_CRAWLER_LOAD_LIMIT => 0, self::O_CRAWLER_SITEMAP => '', self::O_CRAWLER_ROLES => array(), self::O_CRAWLER_COOKIES => array(), // Misc self::O_MISC_HEARTBEAT_FRONT => false, self::O_MISC_HEARTBEAT_FRONT_TTL => 0, self::O_MISC_HEARTBEAT_BACK => false, self::O_MISC_HEARTBEAT_BACK_TTL => 0, self::O_MISC_HEARTBEAT_EDITOR => false, self::O_MISC_HEARTBEAT_EDITOR_TTL => 0, // CDN self::O_CDN => false, self::O_CDN_ORI => array(), self::O_CDN_ORI_DIR => array(), self::O_CDN_EXC => array(), self::O_CDN_QUIC => false, self::O_CDN_CLOUDFLARE => false, self::O_CDN_CLOUDFLARE_EMAIL => '', self::O_CDN_CLOUDFLARE_KEY => '', self::O_CDN_CLOUDFLARE_NAME => '', self::O_CDN_CLOUDFLARE_ZONE => '', self::O_CDN_MAPPING => array(), self::O_CDN_ATTR => array(), self::O_QC_NAMESERVERS => '', self::O_QC_CNAME => '', ); protected static $_default_site_options = array( self::_VER => '', self::O_CACHE => false, self::NETWORK_O_USE_PRIMARY => false, self::O_AUTO_UPGRADE => false, self::O_GUEST => false, self::O_CACHE_RES => false, self::O_CACHE_BROWSER => false, self::O_CACHE_MOBILE => false, self::O_CACHE_MOBILE_RULES => array(), self::O_CACHE_LOGIN_COOKIE => '', self::O_CACHE_VARY_COOKIES => array(), self::O_CACHE_EXC_COOKIES => array(), self::O_CACHE_EXC_USERAGENTS => array(), self::O_CACHE_TTL_BROWSER => 0, self::O_PURGE_ON_UPGRADE => false, self::O_OBJECT => false, self::O_OBJECT_KIND => false, self::O_OBJECT_HOST => '', self::O_OBJECT_PORT => 0, self::O_OBJECT_LIFE => 0, self::O_OBJECT_PERSISTENT => false, self::O_OBJECT_ADMIN => false, self::O_OBJECT_TRANSIENTS => false, self::O_OBJECT_DB_ID => 0, self::O_OBJECT_USER => '', self::O_OBJECT_PSWD => '', self::O_OBJECT_GLOBAL_GROUPS => array(), self::O_OBJECT_NON_PERSISTENT_GROUPS => array(), // Debug self::O_DEBUG_DISABLE_ALL => false, self::O_DEBUG => false, self::O_DEBUG_IPS => array(), self::O_DEBUG_LEVEL => false, self::O_DEBUG_FILESIZE => 0, self::O_DEBUG_COLLAPSE_QS => false, self::O_DEBUG_INC => array(), self::O_DEBUG_EXC => array(), self::O_DEBUG_EXC_STRINGS => array(), self::O_IMG_OPTM_WEBP => false, ); // NOTE: all the val of following items will be int while not bool protected static $_multi_switch_list = array( self::O_DEBUG => 2, self::O_OPTM_JS_DEFER => 2, self::O_IMG_OPTM_WEBP => 2, ); /** * Correct the option type * * TODO: add similar network func * * @since 3.0.3 */ protected function type_casting($val, $id, $is_site_conf = false) { $default_v = !$is_site_conf ? self::$_default_options[$id] : self::$_default_site_options[$id]; if (is_bool($default_v)) { if ($val === 'true') { $val = true; } if ($val === 'false') { $val = false; } $max = $this->_conf_multi_switch($id); if ($max) { $val = (int) $val; $val %= $max + 1; } else { $val = (bool) $val; } } elseif (is_array($default_v)) { // from textarea input if (!is_array($val)) { $val = Utility::sanitize_lines($val, $this->_conf_filter($id)); } } elseif (!is_string($default_v)) { $val = (int) $val; } else { // Check if the string has a limit set $val = $this->_conf_string_val($id, $val); } return $val; } /** * Load default network settings from data.ini * * @since 3.0 */ public function load_default_site_vals() { // Load network_default.json if (file_exists(LSCWP_DIR . 'data/const.network_default.json')) { $default_ini_cfg = json_decode(File::read(LSCWP_DIR . 'data/const.network_default.json'), true); foreach (self::$_default_site_options as $k => $v) { if (!array_key_exists($k, $default_ini_cfg)) { continue; } // Parse value in ini file $ini_v = $this->type_casting($default_ini_cfg[$k], $k, true); if ($ini_v == $v) { continue; } self::$_default_site_options[$k] = $ini_v; } } self::$_default_site_options[self::_VER] = Core::VER; return self::$_default_site_options; } /** * Load default values from default.json * * @since 3.0 * @access public */ public function load_default_vals() { // Load default.json if (file_exists(LSCWP_DIR . 'data/const.default.json')) { $default_ini_cfg = json_decode(File::read(LSCWP_DIR . 'data/const.default.json'), true); foreach (self::$_default_options as $k => $v) { if (!array_key_exists($k, $default_ini_cfg)) { continue; } // Parse value in ini file $ini_v = $this->type_casting($default_ini_cfg[$k], $k); // NOTE: Multiple lines value must be stored as array /** * Special handler for CDN_mapping * * format in .ini: * [cdn-mapping] * url[0] = 'https://example.com/' * inc_js[0] = true * filetype[0] = '.css * .js * .jpg' * * format out: * [0] = [ 'url' => 'https://example.com', 'inc_js' => true, 'filetype' => [ '.css', '.js', '.jpg' ] ] */ if ($k == self::O_CDN_MAPPING) { $mapping_fields = array( self::CDN_MAPPING_URL, self::CDN_MAPPING_INC_IMG, self::CDN_MAPPING_INC_CSS, self::CDN_MAPPING_INC_JS, self::CDN_MAPPING_FILETYPE, // Array ); $ini_v2 = array(); foreach ($ini_v[self::CDN_MAPPING_URL] as $k2 => $v2) { // $k2 is numeric $this_row = array(); foreach ($mapping_fields as $v3) { $this_v = !empty($ini_v[$v3][$k2]) ? $ini_v[$v3][$k2] : false; if ($v3 == self::CDN_MAPPING_URL) { $this_v = $this_v ?: ''; } if ($v3 == self::CDN_MAPPING_FILETYPE) { $this_v = $this_v ? Utility::sanitize_lines($this_v) : array(); // Note: Since v3.0 its already an array } $this_row[$v3] = $this_v; } $ini_v2[$k2] = $this_row; } $ini_v = $ini_v2; } if ($ini_v == $v) { continue; } self::$_default_options[$k] = $ini_v; } } // Load internal default vals // Setting the default bool to int is also to avoid type casting override it back to bool self::$_default_options[self::O_CACHE] = is_multisite() ? self::VAL_ON2 : self::VAL_ON; //For multi site, default is 2 (Use Network Admin Settings). For single site, default is 1 (Enabled). // Load default vals containing variables if (!self::$_default_options[self::O_CDN_ORI_DIR]) { self::$_default_options[self::O_CDN_ORI_DIR] = LSCWP_CONTENT_FOLDER . "\nwp-includes"; self::$_default_options[self::O_CDN_ORI_DIR] = explode("\n", self::$_default_options[self::O_CDN_ORI_DIR]); self::$_default_options[self::O_CDN_ORI_DIR] = array_map('trim', self::$_default_options[self::O_CDN_ORI_DIR]); } // Set security key if not initialized yet if (!self::$_default_options[self::HASH]) { self::$_default_options[self::HASH] = Str::rrand(32); } self::$_default_options[self::_VER] = Core::VER; return self::$_default_options; } /** * Format the string value * * @since 3.0 */ protected function _conf_string_val($id, $val) { return $val; } /** * If the switch setting is a triple value or not * * @since 3.0 */ protected function _conf_multi_switch($id) { if (!empty(self::$_multi_switch_list[$id])) { return self::$_multi_switch_list[$id]; } if ($id == self::O_CACHE && is_multisite()) { return self::VAL_ON2; } return false; } /** * Append a new multi switch max limit for the bool option * * @since 3.0 */ public static function set_multi_switch($id, $v) { self::$_multi_switch_list[$id] = $v; } /** * Generate const name based on $id * * @since 3.0 */ public static function conf_const($id) { return 'LITESPEED_CONF__' . strtoupper(str_replace('-', '__', $id)); } /** * Filter to be used when saving setting * * @since 3.0 */ protected function _conf_filter($id) { $filters = array( self::O_MEDIA_LAZY_EXC => 'uri', self::O_DEBUG_INC => 'relative', self::O_DEBUG_EXC => 'relative', self::O_MEDIA_LAZY_URI_EXC => 'relative', self::O_CACHE_PRIV_URI => 'relative', self::O_PURGE_TIMED_URLS => 'relative', self::O_CACHE_FORCE_URI => 'relative', self::O_CACHE_FORCE_PUB_URI => 'relative', self::O_CACHE_EXC => 'relative', // self::O_OPTM_CSS_EXC => 'uri', // Need to comment out for inline & external CSS // self::O_OPTM_JS_EXC => 'uri', self::O_OPTM_EXC => 'relative', self::O_OPTM_CCSS_SEP_URI => 'uri', // self::O_OPTM_JS_DEFER_EXC => 'uri', self::O_OPTM_DNS_PREFETCH => 'domain', self::O_CDN_ORI => 'noprotocol,trailingslash', // `Original URLs` // self::O_OPTM_LOCALIZE_DOMAINS => 'noprotocol', // `Localize Resources` // self:: => '', // self:: => '', ); if (!empty($filters[$id])) { return $filters[$id]; } return false; } /** * If the setting changes worth a purge or not * * @since 3.0 */ protected function _conf_purge($id) { $check_ids = array( self::O_MEDIA_LAZY_URI_EXC, self::O_OPTM_EXC, self::O_CACHE_PRIV_URI, self::O_PURGE_TIMED_URLS, self::O_CACHE_FORCE_URI, self::O_CACHE_FORCE_PUB_URI, self::O_CACHE_EXC, ); return in_array($id, $check_ids); } /** * If the setting changes worth a purge ALL or not * * @since 3.0 */ protected function _conf_purge_all($id) { $check_ids = array(self::O_CACHE, self::O_ESI, self::O_DEBUG_DISABLE_ALL, self::NETWORK_O_USE_PRIMARY); return in_array($id, $check_ids); } /** * If the setting is a pswd or not * * @since 3.0 */ protected function _conf_pswd($id) { $check_ids = array(self::O_CDN_CLOUDFLARE_KEY, self::O_OBJECT_PSWD); return in_array($id, $check_ids); } /** * If the setting is cron related or not * * @since 3.0 */ protected function _conf_cron($id) { $check_ids = array(self::O_OPTM_CSS_ASYNC, self::O_MEDIA_PLACEHOLDER_RESP_ASYNC, self::O_DISCUSS_AVATAR_CRON, self::O_IMG_OPTM_AUTO, self::O_CRAWLER); return in_array($id, $check_ids); } /** * If the setting changes worth a purge, return the tag * * @since 3.0 */ protected function _conf_purge_tag($id) { $check_ids = array( self::O_CACHE_PAGE_LOGIN => Tag::TYPE_LOGIN, ); if (!empty($check_ids[$id])) { return $check_ids[$id]; } return false; } /** * Generate server vars * * @since 2.4.1 */ public function server_vars() { $consts = array( 'WP_SITEURL', 'WP_HOME', 'WP_CONTENT_DIR', 'SHORTINIT', 'LSCWP_CONTENT_DIR', 'LSCWP_CONTENT_FOLDER', 'LSCWP_DIR', 'LITESPEED_TIME_OFFSET', 'LITESPEED_SERVER_TYPE', 'LITESPEED_CLI', 'LITESPEED_ALLOWED', 'LITESPEED_ON', 'LSWCP_TAG_PREFIX', 'COOKIEHASH', ); $server_vars = array(); foreach ($consts as $v) { $server_vars[$v] = defined($v) ? constant($v) : null; } return $server_vars; } } vary.cls.php 0000644 00000050134 15162226335 0007026 0 ustar 00 <?php /** * The plugin vary class to manage X-LiteSpeed-Vary * * @since 1.1.3 */ namespace LiteSpeed; defined('WPINC') || exit(); class Vary extends Root { const LOG_TAG = '🔱'; const X_HEADER = 'X-LiteSpeed-Vary'; private static $_vary_name = '_lscache_vary'; // this default vary cookie is used for logged in status check private static $_can_change_vary = false; // Currently only AJAX used this /** * Adds the actions used for setting up cookies on log in/out. * * Also checks if the database matches the rewrite rule. * * @since 1.0.4 */ // public function init() // { // $this->_update_vary_name(); // } /** * Update the default vary name if changed * * @since 4.0 * @since 7.0 Moved to after_user_init to allow ESI no-vary no conflict w/ LSCACHE_VARY_COOKIE/O_CACHE_LOGIN_COOKIE */ private function _update_vary_name() { $db_cookie = $this->conf(Base::O_CACHE_LOGIN_COOKIE); // [3.0] todo: check if works in network's sites // If no vary set in rewrite rule if (!isset($_SERVER['LSCACHE_VARY_COOKIE'])) { if ($db_cookie) { // Check if is from ESI req or not. If from ESI no-vary, no need to set no-cache $something_wrong = true; if (!empty($_GET[ESI::QS_ACTION]) && !empty($_GET['_control'])) { // Have to manually build this checker bcoz ESI is not init yet. $control = explode(',', $_GET['_control']); if (in_array('no-vary', $control)) { self::debug('no-vary control existed, bypass vary_name update'); $something_wrong = false; self::$_vary_name = $db_cookie; } } if (defined('LITESPEED_CLI') || defined('DOING_CRON')) { $something_wrong = false; } if ($something_wrong) { // Display cookie error msg to admin if (is_multisite() ? is_network_admin() : is_admin()) { Admin_Display::show_error_cookie(); } Control::set_nocache('❌❌ vary cookie setting error'); } } return; } // If db setting does not exist, skip checking db value if (!$db_cookie) { return; } // beyond this point, need to make sure db vary setting is in $_SERVER env. $vary_arr = explode(',', $_SERVER['LSCACHE_VARY_COOKIE']); if (in_array($db_cookie, $vary_arr)) { self::$_vary_name = $db_cookie; return; } if (is_multisite() ? is_network_admin() : is_admin()) { Admin_Display::show_error_cookie(); } Control::set_nocache('vary cookie setting lost error'); } /** * Hooks after user init * * @since 4.0 */ public function after_user_init() { $this->_update_vary_name(); // logged in user if (Router::is_logged_in()) { // If not esi, check cache logged-in user setting if (!$this->cls('Router')->esi_enabled()) { // If cache logged-in, then init cacheable to private if ($this->conf(Base::O_CACHE_PRIV)) { add_action('wp_logout', __NAMESPACE__ . '\Purge::purge_on_logout'); $this->cls('Control')->init_cacheable(); Control::set_private('logged in user'); } // No cache for logged-in user else { Control::set_nocache('logged in user'); } } // ESI is on, can be public cache else { // Need to make sure vary is using group id $this->cls('Control')->init_cacheable(); } // register logout hook to clear login status add_action('clear_auth_cookie', array($this, 'remove_logged_in')); } else { // Only after vary init, can detect if is Guest mode or not // Here need `self::$_vary_name` to be set first. $this->_maybe_guest_mode(); // Set vary cookie for logging in user, otherwise the user will hit public with vary=0 (guest version) add_action('set_logged_in_cookie', array($this, 'add_logged_in'), 10, 4); add_action('wp_login', __NAMESPACE__ . '\Purge::purge_on_logout'); $this->cls('Control')->init_cacheable(); // Check `login page` cacheable setting because they don't go through main WP logic add_action('login_init', array($this->cls('Tag'), 'check_login_cacheable'), 5); if (!empty($_GET['litespeed_guest'])) { add_action('wp_loaded', array($this, 'update_guest_vary'), 20); } } // Add comment list ESI add_filter('comments_array', array($this, 'check_commenter')); // Set vary cookie for commenter. add_action('set_comment_cookies', array($this, 'append_commenter')); /** * Don't change for REST call because they don't carry on user info usually * @since 1.6.7 */ add_action('rest_api_init', function () { // this hook is fired in `init` hook self::debug('Rest API init disabled vary change'); add_filter('litespeed_can_change_vary', '__return_false'); }); } /** * Check if is Guest mode or not * * @since 4.0 */ private function _maybe_guest_mode() { if (defined('LITESPEED_GUEST')) { self::debug('👒👒 Guest mode ' . (LITESPEED_GUEST ? 'predefined' : 'turned off')); return; } if (!$this->conf(Base::O_GUEST)) { return; } // If vary is set, then not a guest if (self::has_vary()) { return; } // If has admin QS, then no guest if (!empty($_GET[Router::ACTION])) { return; } if (defined('DOING_AJAX')) { return; } if (defined('DOING_CRON')) { return; } // If is the request to update vary, then no guest // Don't need anymore as it is always ajax call // Still keep it in case some WP blocked the lightweight guest vary update script, WP can still update the vary if (!empty($_GET['litespeed_guest'])) { return; } /* @ref https://wordpress.org/support/topic/checkout-add-to-cart-executed-twice/ */ if (!empty($_GET['litespeed_guest_off'])) { return; } self::debug('👒👒 Guest mode'); !defined('LITESPEED_GUEST') && define('LITESPEED_GUEST', true); if ($this->conf(Base::O_GUEST_OPTM)) { !defined('LITESPEED_GUEST_OPTM') && define('LITESPEED_GUEST_OPTM', true); } } /** * Update Guest vary * * @since 4.0 * @deprecated 4.1 Use independent lightweight guest.vary.php as a replacement */ public function update_guest_vary() { // This process must not be cached !defined('LSCACHE_NO_CACHE') && define('LSCACHE_NO_CACHE', true); $_guest = new Lib\Guest(); if ($_guest->always_guest() || self::has_vary()) { // If contains vary already, don't reload to avoid infinite loop when parent page having browser cache !defined('LITESPEED_GUEST') && define('LITESPEED_GUEST', true); // Reuse this const to bypass set vary in vary finalize self::debug('🤠🤠 Guest'); echo '[]'; exit(); } self::debug('Will update guest vary in finalize'); // return json echo \json_encode(array('reload' => 'yes')); exit(); } /** * Hooked to the comments_array filter. * * Check if the user accessing the page has the commenter cookie. * * If the user does not want to cache commenters, just check if user is commenter. * Otherwise if the vary cookie is set, unset it. This is so that when the page is cached, the page will appear as if the user was a normal user. * Normal user is defined as not a logged in user and not a commenter. * * @since 1.0.4 * @access public * @global type $post * @param array $comments The current comments to output * @return array The comments to output. */ public function check_commenter($comments) { /** * Hook to bypass pending comment check for comment related plugins compatibility * @since 2.9.5 */ if (apply_filters('litespeed_vary_check_commenter_pending', true)) { $pending = false; foreach ($comments as $comment) { if (!$comment->comment_approved) { // current user has pending comment $pending = true; break; } } // No pending comments, don't need to add private cache if (!$pending) { self::debug('No pending comment'); $this->remove_commenter(); // Remove commenter prefilled info if exists, for public cache foreach ($_COOKIE as $cookie_name => $cookie_value) { if (strlen($cookie_name) >= 15 && strpos($cookie_name, 'comment_author_') === 0) { unset($_COOKIE[$cookie_name]); } } return $comments; } } // Current user/visitor has pending comments // set vary=2 for next time vary lookup $this->add_commenter(); if ($this->conf(Base::O_CACHE_COMMENTER)) { Control::set_private('existing commenter'); } else { Control::set_nocache('existing commenter'); } return $comments; } /** * Check if default vary has a value * * @since 1.1.3 * @access public */ public static function has_vary() { if (empty($_COOKIE[self::$_vary_name])) { return false; } return $_COOKIE[self::$_vary_name]; } /** * Append user status with logged in * * @since 1.1.3 * @since 1.6.2 Removed static referral * @access public */ public function add_logged_in($logged_in_cookie = false, $expire = false, $expiration = false, $uid = false) { self::debug('add_logged_in'); /** * NOTE: Run before `$this->_update_default_vary()` to make vary changeable * @since 2.2.2 */ self::can_ajax_vary(); // If the cookie is lost somehow, set it $this->_update_default_vary($uid, $expire); } /** * Remove user logged in status * * @since 1.1.3 * @since 1.6.2 Removed static referral * @access public */ public function remove_logged_in() { self::debug('remove_logged_in'); /** * NOTE: Run before `$this->_update_default_vary()` to make vary changeable * @since 2.2.2 */ self::can_ajax_vary(); // Force update vary to remove login status $this->_update_default_vary(-1); } /** * Allow vary can be changed for ajax calls * * @since 2.2.2 * @since 2.6 Changed to static * @access public */ public static function can_ajax_vary() { self::debug('_can_change_vary -> true'); self::$_can_change_vary = true; } /** * Check if can change default vary * * @since 1.6.2 * @access private */ private function can_change_vary() { // Don't change for ajax due to ajax not sending webp header if (Router::is_ajax()) { if (!self::$_can_change_vary) { self::debug('can_change_vary bypassed due to ajax call'); return false; } } /** * POST request can set vary to fix #820789 login "loop" guest cache issue * @since 1.6.5 */ if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'POST') { self::debug('can_change_vary bypassed due to method not get/post'); return false; } /** * Disable vary change if is from crawler * @since 2.9.8 To enable woocommerce cart not empty warm up (@Taba) */ if (!empty($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], Crawler::FAST_USER_AGENT) === 0) { self::debug('can_change_vary bypassed due to crawler'); return false; } if (!apply_filters('litespeed_can_change_vary', true)) { self::debug('can_change_vary bypassed due to litespeed_can_change_vary hook'); return false; } return true; } /** * Update default vary * * @since 1.6.2 * @since 1.6.6.1 Add ran check to make it only run once ( No run multiple times due to login process doesn't have valid uid ) * @access private */ private function _update_default_vary($uid = false, $expire = false) { // Make sure header output only run once if (!defined('LITESPEED_DID_' . __FUNCTION__)) { define('LITESPEED_DID_' . __FUNCTION__, true); } else { self::debug2('_update_default_vary bypassed due to run already'); return; } // ESI shouldn't change vary (Let main page do only) if (defined('LSCACHE_IS_ESI') && LSCACHE_IS_ESI) { self::debug2('_update_default_vary bypassed due to ESI'); return; } // If the cookie is lost somehow, set it $vary = $this->finalize_default_vary($uid); $current_vary = self::has_vary(); if ($current_vary !== $vary && $current_vary !== 'commenter' && $this->can_change_vary()) { // $_COOKIE[ self::$_vary_name ] = $vary; // not needed // save it if (!$expire) { $expire = time() + 2 * DAY_IN_SECONDS; } $this->_cookie($vary, $expire); // Control::set_nocache( 'changing default vary' . " $current_vary => $vary" ); } } /** * Get vary name * * @since 1.9.1 * @access public */ public function get_vary_name() { return self::$_vary_name; } /** * Check if one user role is in vary group settings * * @since 1.2.0 * @since 3.0 Moved here from conf.cls * @access public * @param string $role The user role * @return int The set value if already set */ public function in_vary_group($role) { $group = 0; $vary_groups = $this->conf(Base::O_CACHE_VARY_GROUP); $roles = explode(',', $role); if ($found = array_intersect($roles, array_keys($vary_groups))) { $groups = array(); foreach ($found as $curr_role) { $groups[] = $vary_groups[$curr_role]; } $group = implode(',', array_unique($groups)); } elseif (in_array('administrator', $roles)) { $group = 99; } if ($group) { self::debug2('role in vary_group [group] ' . $group); } return $group; } /** * Finalize default Vary Cookie * * Get user vary tag based on admin_bar & role * * NOTE: Login process will also call this because it does not call wp hook as normal page loading * * @since 1.6.2 * @access public */ public function finalize_default_vary($uid = false) { // Must check this to bypass vary generation for guests // Must check this to avoid Guest page's CSS/JS/CCSS/UCSS get non-guest vary filename if (defined('LITESPEED_GUEST') && LITESPEED_GUEST) { return false; } $vary = array(); if ($this->conf(Base::O_GUEST)) { $vary['guest_mode'] = 1; } if (!$uid) { $uid = get_current_user_id(); } else { self::debug('uid: ' . $uid); } // get user's group id $role = Router::get_role($uid); if ($uid > 0 && $role) { $vary['logged-in'] = 1; // parse role group from settings if ($role_group = $this->in_vary_group($role)) { $vary['role'] = $role_group; } // Get admin bar set // see @_get_admin_bar_pref() $pref = get_user_option('show_admin_bar_front', $uid); self::debug2('show_admin_bar_front: ' . $pref); $admin_bar = $pref === false || $pref === 'true'; if ($admin_bar) { $vary['admin_bar'] = 1; self::debug2('admin bar : true'); } } else { // Guest user self::debug('role id: failed, guest'); } /** * Add filter * @since 1.6 Added for Role Excludes for optimization cls * @since 1.6.2 Hooked to webp (checked in v4, no webp anymore) * @since 3.0 Used by 3rd hooks too */ $vary = apply_filters('litespeed_vary', $vary); if (!$vary) { return false; } ksort($vary); $res = array(); foreach ($vary as $key => $val) { $res[] = $key . ':' . $val; } $res = implode(';', $res); if (defined('LSCWP_LOG')) { return $res; } // Encrypt in production return md5($this->conf(Base::HASH) . $res); } /** * Get the hash of all vary related values * * @since 4.0 */ public function finalize_full_varies() { $vary = $this->_finalize_curr_vary_cookies(true); $vary .= $this->finalize_default_vary(get_current_user_id()); $vary .= $this->get_env_vary(); return $vary; } /** * Get request environment Vary * * @since 4.0 */ public function get_env_vary() { $env_vary = isset($_SERVER['LSCACHE_VARY_VALUE']) ? $_SERVER['LSCACHE_VARY_VALUE'] : false; if (!$env_vary) { $env_vary = isset($_SERVER['HTTP_X_LSCACHE_VARY_VALUE']) ? $_SERVER['HTTP_X_LSCACHE_VARY_VALUE'] : false; } return $env_vary; } /** * Append user status with commenter * * This is ONLY used when submit a comment * * @since 1.1.6 * @access public */ public function append_commenter() { $this->add_commenter(true); } /** * Correct user status with commenter * * @since 1.1.3 * @access private * @param boolean $from_redirect If the request is from redirect page or not */ private function add_commenter($from_redirect = false) { // If the cookie is lost somehow, set it if (self::has_vary() !== 'commenter') { self::debug('Add commenter'); // $_COOKIE[ self::$_vary_name ] = 'commenter'; // not needed // save it // only set commenter status for current domain path $this->_cookie('commenter', time() + apply_filters('comment_cookie_lifetime', 30000000), self::_relative_path($from_redirect)); // Control::set_nocache( 'adding commenter status' ); } } /** * Remove user commenter status * * @since 1.1.3 * @access private */ private function remove_commenter() { if (self::has_vary() === 'commenter') { self::debug('Remove commenter'); // remove logged in status from global var // unset( $_COOKIE[ self::$_vary_name ] ); // not needed // save it $this->_cookie(false, false, self::_relative_path()); // Control::set_nocache( 'removing commenter status' ); } } /** * Generate relative path for cookie * * @since 1.1.3 * @access private * @param boolean $from_redirect If the request is from redirect page or not */ private static function _relative_path($from_redirect = false) { $path = false; $tag = $from_redirect ? 'HTTP_REFERER' : 'SCRIPT_URL'; if (!empty($_SERVER[$tag])) { $path = parse_url($_SERVER[$tag]); $path = !empty($path['path']) ? $path['path'] : false; self::debug('Cookie Vary path: ' . $path); } return $path; } /** * Builds the vary header. * * NOTE: Non caccheable page can still set vary ( for logged in process ) * * Currently, this only checks post passwords and 3rd party. * * @since 1.0.13 * @access public * @global $post * @return mixed false if the user has the postpass cookie. Empty string if the post is not password protected. Vary header otherwise. */ public function finalize() { // Finalize default vary if (!defined('LITESPEED_GUEST') || !LITESPEED_GUEST) { $this->_update_default_vary(); } $tp_cookies = $this->_finalize_curr_vary_cookies(); if (!$tp_cookies) { self::debug2('no custimzed vary'); return; } self::debug('finalized 3rd party cookies', $tp_cookies); return self::X_HEADER . ': ' . implode(',', $tp_cookies); } /** * Gets vary cookies or their values unique hash that are already added for the current page. * * @since 1.0.13 * @access private * @return array List of all vary cookies currently added. */ private function _finalize_curr_vary_cookies($values_json = false) { global $post; $cookies = array(); // No need to append default vary cookie name if (!empty($post->post_password)) { $postpass_key = 'wp-postpass_' . COOKIEHASH; if ($this->_get_cookie_val($postpass_key)) { self::debug('finalize bypassed due to password protected vary '); // If user has password cookie, do not cache & ignore existing vary cookies Control::set_nocache('password protected vary'); return false; } $cookies[] = $values_json ? $this->_get_cookie_val($postpass_key) : $postpass_key; } $cookies = apply_filters('litespeed_vary_curr_cookies', $cookies); if ($cookies) { $cookies = array_filter(array_unique($cookies)); self::debug('vary cookies changed by filter litespeed_vary_curr_cookies', $cookies); } if (!$cookies) { return false; } // Format cookie name data or value data sort($cookies); // This is to maintain the cookie val orders for $values_json=true case. foreach ($cookies as $k => $v) { $cookies[$k] = $values_json ? $this->_get_cookie_val($v) : 'cookie=' . $v; } return $values_json ? \json_encode($cookies) : $cookies; } /** * Get one vary cookie value * * @since 4.0 */ private function _get_cookie_val($key) { if (!empty($_COOKIE[$key])) { return $_COOKIE[$key]; } return false; } /** * Set the vary cookie. * * If vary cookie changed, must set non cacheable. * * @since 1.0.4 * @access private * @param integer $val The value to update. * @param integer $expire Expire time. * @param boolean $path False if use wp root path as cookie path */ private function _cookie($val = false, $expire = false, $path = false) { if (!$val) { $expire = 1; } /** * Add HTTPS bypass in case clients use both HTTP and HTTPS version of site * @since 1.7 */ $is_ssl = $this->conf(Base::O_UTIL_NO_HTTPS_VARY) ? false : is_ssl(); setcookie(self::$_vary_name, $val, $expire, $path ?: COOKIEPATH, COOKIE_DOMAIN, $is_ssl, true); self::debug('set_cookie ---> [k] ' . self::$_vary_name . " [v] $val [ttl] " . ($expire - time())); } } control.cls.php 0000644 00000053211 15162226336 0007525 0 ustar 00 <?php /** * The plugin cache-control class for X-Litespeed-Cache-Control * * @since 1.1.3 * @package LiteSpeed * @subpackage LiteSpeed/inc * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Control extends Root { const LOG_TAG = '💵'; const BM_CACHEABLE = 1; const BM_PRIVATE = 2; const BM_SHARED = 4; const BM_NO_VARY = 8; const BM_FORCED_CACHEABLE = 32; const BM_PUBLIC_FORCED = 64; const BM_STALE = 128; const BM_NOTCACHEABLE = 256; const X_HEADER = 'X-LiteSpeed-Cache-Control'; protected static $_control = 0; protected static $_custom_ttl = 0; private $_response_header_ttls = array(); /** * Init cache control * * @since 1.6.2 */ public function init() { /** * Add vary filter for Role Excludes * @since 1.6.2 */ add_filter('litespeed_vary', array($this, 'vary_add_role_exclude')); // 301 redirect hook add_filter('wp_redirect', array($this, 'check_redirect'), 10, 2); // Load response header conf $this->_response_header_ttls = $this->conf(Base::O_CACHE_TTL_STATUS); foreach ($this->_response_header_ttls as $k => $v) { $v = explode(' ', $v); if (empty($v[0]) || empty($v[1])) { continue; } $this->_response_header_ttls[$v[0]] = $v[1]; } if ($this->conf(Base::O_PURGE_STALE)) { $this->set_stale(); } } /** * Exclude role from optimization filter * * @since 1.6.2 * @access public */ public function vary_add_role_exclude($vary) { if ($this->in_cache_exc_roles()) { $vary['role_exclude_cache'] = 1; } return $vary; } /** * Check if one user role is in exclude cache group settings * * @since 1.6.2 * @since 3.0 Moved here from conf.cls * @access public * @param string $role The user role * @return int The set value if already set */ public function in_cache_exc_roles($role = null) { // Get user role if ($role === null) { $role = Router::get_role(); } if (!$role) { return false; } $roles = explode(',', $role); $found = array_intersect($roles, $this->conf(Base::O_CACHE_EXC_ROLES)); return $found ? implode(',', $found) : false; } /** * 1. Initialize cacheable status for `wp` hook * 2. Hook error page tags for cacheable pages * * @since 1.1.3 * @access public */ public function init_cacheable() { // Hook `wp` to mark default cacheable status // NOTE: Any process that does NOT run into `wp` hook will not get cacheable by default add_action('wp', array($this, 'set_cacheable'), 5); // Hook WP REST to be cacheable if ($this->conf(Base::O_CACHE_REST)) { add_action('rest_api_init', array($this, 'set_cacheable'), 5); } // Cache resources // NOTE: If any strange resource doesn't use normal WP logic `wp_loaded` hook, rewrite rule can handle it $cache_res = $this->conf(Base::O_CACHE_RES); if ($cache_res) { $uri = esc_url($_SERVER['REQUEST_URI']); // todo: check if need esc_url() $pattern = '!' . LSCWP_CONTENT_FOLDER . Htaccess::RW_PATTERN_RES . '!'; if (preg_match($pattern, $uri)) { add_action('wp_loaded', array($this, 'set_cacheable'), 5); } } // AJAX cache $ajax_cache = $this->conf(Base::O_CACHE_AJAX_TTL); foreach ($ajax_cache as $v) { $v = explode(' ', $v); if (empty($v[0]) || empty($v[1])) { continue; } // self::debug("Initializing cacheable status for wp_ajax_nopriv_" . $v[0]); add_action( 'wp_ajax_nopriv_' . $v[0], function () use ($v) { self::set_custom_ttl($v[1]); self::force_cacheable('ajax Cache setting for action ' . $v[0]); }, 4 ); } // Check error page add_filter('status_header', array($this, 'check_error_codes'), 10, 2); } /** * Check if the page returns any error code. * * @since 1.0.13.1 * @access public * @param $status_header * @param $code * @return $error_status */ public function check_error_codes($status_header, $code) { if (array_key_exists($code, $this->_response_header_ttls)) { if (self::is_cacheable() && !$this->_response_header_ttls[$code]) { self::set_nocache('[Ctrl] TTL is set to no cache [status_header] ' . $code); } // Set TTL self::set_custom_ttl($this->_response_header_ttls[$code]); } elseif (self::is_cacheable()) { if (substr($code, 0, 1) == 4 || substr($code, 0, 1) == 5) { self::set_nocache('[Ctrl] 4xx/5xx default to no cache [status_header] ' . $code); } } // Set cache tag if (in_array($code, Tag::$error_code_tags)) { Tag::add(Tag::TYPE_HTTP . $code); } // Give the default status_header back return $status_header; } /** * Set no vary setting * * @access public * @since 1.1.3 */ public static function set_no_vary() { if (self::is_no_vary()) { return; } self::$_control |= self::BM_NO_VARY; self::debug('X Cache_control -> no-vary', 3); } /** * Get no vary setting * * @access public * @since 1.1.3 */ public static function is_no_vary() { return self::$_control & self::BM_NO_VARY; } /** * Set stale * * @access public * @since 1.1.3 */ public function set_stale() { if (self::is_stale()) { return; } self::$_control |= self::BM_STALE; self::debug('X Cache_control -> stale'); } /** * Get stale * * @access public * @since 1.1.3 */ public static function is_stale() { return self::$_control & self::BM_STALE; } /** * Set cache control to shared private * * @access public * @since 1.1.3 * @param string $reason The reason to no cache */ public static function set_shared($reason = false) { if (self::is_shared()) { return; } self::$_control |= self::BM_SHARED; self::set_private(); if (!is_string($reason)) { $reason = false; } if ($reason) { $reason = "( $reason )"; } self::debug('X Cache_control -> shared ' . $reason); } /** * Check if is shared private * * @access public * @since 1.1.3 */ public static function is_shared() { return self::$_control & self::BM_SHARED && self::is_private(); } /** * Set cache control to forced public * * @access public * @since 1.7.1 */ public static function set_public_forced($reason = false) { if (self::is_public_forced()) { return; } self::$_control |= self::BM_PUBLIC_FORCED; if (!is_string($reason)) { $reason = false; } if ($reason) { $reason = "( $reason )"; } self::debug('X Cache_control -> public forced ' . $reason); } /** * Check if is public forced * * @access public * @since 1.7.1 */ public static function is_public_forced() { return self::$_control & self::BM_PUBLIC_FORCED; } /** * Set cache control to private * * @access public * @since 1.1.3 * @param string $reason The reason to no cache */ public static function set_private($reason = false) { if (self::is_private()) { return; } self::$_control |= self::BM_PRIVATE; if (!is_string($reason)) { $reason = false; } if ($reason) { $reason = "( $reason )"; } self::debug('X Cache_control -> private ' . $reason); } /** * Check if is private * * @access public * @since 1.1.3 */ public static function is_private() { if (defined('LITESPEED_GUEST') && LITESPEED_GUEST) { // return false; } return self::$_control & self::BM_PRIVATE && !self::is_public_forced(); } /** * Initialize cacheable status in `wp` hook, if not call this, by default it will be non-cacheable * * @access public * @since 1.1.3 */ public function set_cacheable($reason = false) { self::$_control |= self::BM_CACHEABLE; if (!is_string($reason)) { $reason = false; } if ($reason) { $reason = ' [reason] ' . $reason; } self::debug('Cache_control init on' . $reason); } /** * This will disable non-cacheable BM * * @access public * @since 2.2 */ public static function force_cacheable($reason = false) { self::$_control |= self::BM_FORCED_CACHEABLE; if (!is_string($reason)) { $reason = false; } if ($reason) { $reason = ' [reason] ' . $reason; } self::debug('Forced cacheable' . $reason); } /** * Switch to nocacheable status * * @access public * @since 1.1.3 * @param string $reason The reason to no cache */ public static function set_nocache($reason = false) { self::$_control |= self::BM_NOTCACHEABLE; if (!is_string($reason)) { $reason = false; } if ($reason) { $reason = "( $reason )"; } self::debug('X Cache_control -> no Cache ' . $reason, 5); } /** * Check current notcacheable bit set * * @access public * @since 1.1.3 * @return bool True if notcacheable bit is set, otherwise false. */ public static function isset_notcacheable() { return self::$_control & self::BM_NOTCACHEABLE; } /** * Check current force cacheable bit set * * @access public * @since 2.2 */ public static function is_forced_cacheable() { return self::$_control & self::BM_FORCED_CACHEABLE; } /** * Check current cacheable status * * @access public * @since 1.1.3 * @return bool True if is still cacheable, otherwise false. */ public static function is_cacheable() { if (defined('LSCACHE_NO_CACHE') && LSCACHE_NO_CACHE) { self::debug('LSCACHE_NO_CACHE constant defined'); return false; } // Guest mode always cacheable if (defined('LITESPEED_GUEST') && LITESPEED_GUEST) { // return true; } // If its forced public cacheable if (self::is_public_forced()) { return true; } // If its forced cacheable if (self::is_forced_cacheable()) { return true; } return !self::isset_notcacheable() && self::$_control & self::BM_CACHEABLE; } /** * Set a custom TTL to use with the request if needed. * * @access public * @since 1.1.3 * @param mixed $ttl An integer or string to use as the TTL. Must be numeric. */ public static function set_custom_ttl($ttl, $reason = false) { if (is_numeric($ttl)) { self::$_custom_ttl = $ttl; self::debug('X Cache_control TTL -> ' . $ttl . ($reason ? ' [reason] ' . $ttl : '')); } } /** * Generate final TTL. * * @access public * @since 1.1.3 */ public function get_ttl() { if (self::$_custom_ttl != 0) { return self::$_custom_ttl; } // Check if is in timed url list or not $timed_urls = Utility::wildcard2regex($this->conf(Base::O_PURGE_TIMED_URLS)); $timed_urls_time = $this->conf(Base::O_PURGE_TIMED_URLS_TIME); if ($timed_urls && $timed_urls_time) { $current_url = Tag::build_uri_tag(true); // Use time limit ttl $scheduled_time = strtotime($timed_urls_time); $ttl = $scheduled_time - time(); if ($ttl < 0) { $ttl += 86400; // add one day } foreach ($timed_urls as $v) { if (strpos($v, '*') !== false) { if (preg_match('#' . $v . '#iU', $current_url)) { self::debug('X Cache_control TTL is limited to ' . $ttl . ' due to scheduled purge regex ' . $v); return $ttl; } } else { if ($v == $current_url) { self::debug('X Cache_control TTL is limited to ' . $ttl . ' due to scheduled purge rule ' . $v); return $ttl; } } } } // Private cache uses private ttl setting if (self::is_private()) { return $this->conf(Base::O_CACHE_TTL_PRIV); } if (is_front_page()) { return $this->conf(Base::O_CACHE_TTL_FRONTPAGE); } $feed_ttl = $this->conf(Base::O_CACHE_TTL_FEED); if (is_feed() && $feed_ttl > 0) { return $feed_ttl; } if ($this->cls('REST')->is_rest() || $this->cls('REST')->is_internal_rest()) { return $this->conf(Base::O_CACHE_TTL_REST); } return $this->conf(Base::O_CACHE_TTL_PUB); } /** * Check if need to set no cache status for redirection or not * * @access public * @since 1.1.3 */ public function check_redirect($location, $status) { // TODO: some env don't have SCRIPT_URI but only REQUEST_URI, need to be compatible if (!empty($_SERVER['SCRIPT_URI'])) { // dont check $status == '301' anymore self::debug('301 from ' . $_SERVER['SCRIPT_URI']); self::debug("301 to $location"); $to_check = array(PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PATH, PHP_URL_QUERY); $is_same_redirect = true; foreach ($to_check as $v) { $url_parsed = $v == PHP_URL_QUERY ? $_SERVER['QUERY_STRING'] : parse_url($_SERVER['SCRIPT_URI'], $v); $target = parse_url($location, $v); self::debug("Compare [from] $url_parsed [to] $target"); if ($v == PHP_URL_QUERY) { $url_parsed = $url_parsed ? urldecode($url_parsed) : ''; $target = $target ? urldecode($target) : ''; if (substr($url_parsed, -1) == '&') { $url_parsed = substr($url_parsed, 0, -1); } } if ($url_parsed != $target) { $is_same_redirect = false; self::debug('301 different redirection'); break; } } if ($is_same_redirect) { self::set_nocache('301 to same url'); } } return $location; } /** * Sets up the Cache Control header. * * @since 1.1.3 * @access public * @return string empty string if empty, otherwise the cache control header. */ public function output() { $esi_hdr = ''; if (ESI::has_esi()) { $esi_hdr = ',esi=on'; } $hdr = self::X_HEADER . ': '; if (defined('DONOTCACHEPAGE') && apply_filters('litespeed_const_DONOTCACHEPAGE', DONOTCACHEPAGE)) { self::debug('❌ forced no cache [reason] DONOTCACHEPAGE const'); $hdr .= 'no-cache' . $esi_hdr; return $hdr; } // Guest mode directly return cacheable result // if ( defined( 'LITESPEED_GUEST' ) && LITESPEED_GUEST ) { // // If is POST, no cache // if ( defined( 'LSCACHE_NO_CACHE' ) && LSCACHE_NO_CACHE ) { // self::debug( "[Ctrl] ❌ forced no cache [reason] LSCACHE_NO_CACHE const" ); // $hdr .= 'no-cache'; // } // else if( $_SERVER[ 'REQUEST_METHOD' ] !== 'GET' ) { // self::debug( "[Ctrl] ❌ forced no cache [reason] req not GET" ); // $hdr .= 'no-cache'; // } // else { // $hdr .= 'public'; // $hdr .= ',max-age=' . $this->get_ttl(); // } // $hdr .= $esi_hdr; // return $hdr; // } // Fix cli `uninstall --deactivate` fatal err if (!self::is_cacheable()) { $hdr .= 'no-cache' . $esi_hdr; return $hdr; } if (self::is_shared()) { $hdr .= 'shared,private'; } elseif (self::is_private()) { $hdr .= 'private'; } else { $hdr .= 'public'; } if (self::is_no_vary()) { $hdr .= ',no-vary'; } $hdr .= ',max-age=' . $this->get_ttl() . $esi_hdr; return $hdr; } /** * Generate all `control` tags before output * * @access public * @since 1.1.3 */ public function finalize() { if (defined('LITESPEED_GUEST') && LITESPEED_GUEST) { // return; } if (is_preview()) { self::set_nocache('preview page'); return; } // Check if has metabox non-cacheable setting or not if (file_exists(LSCWP_DIR . 'src/metabox.cls.php') && $this->cls('Metabox')->setting('litespeed_no_cache')) { self::set_nocache('per post metabox setting'); return; } // Check if URI is forced public cache $excludes = $this->conf(Base::O_CACHE_FORCE_PUB_URI); $hit = Utility::str_hit_array($_SERVER['REQUEST_URI'], $excludes, true); if ($hit) { list($result, $this_ttl) = $hit; self::set_public_forced('Setting: ' . $result); self::debug('Forced public cacheable due to setting: ' . $result); if ($this_ttl) { self::set_custom_ttl($this_ttl); } } if (self::is_public_forced()) { return; } // Check if URI is forced cache $excludes = $this->conf(Base::O_CACHE_FORCE_URI); $hit = Utility::str_hit_array($_SERVER['REQUEST_URI'], $excludes, true); if ($hit) { list($result, $this_ttl) = $hit; self::force_cacheable(); self::debug('Forced cacheable due to setting: ' . $result); if ($this_ttl) { self::set_custom_ttl($this_ttl); } } // if is not cacheable, terminate check // Even no need to run 3rd party hook if (!self::is_cacheable()) { self::debug('not cacheable before ctrl finalize'); return; } // Apply 3rd party filter // NOTE: Hook always needs to run asap because some 3rd party set is_mobile in this hook do_action('litespeed_control_finalize', defined('LSCACHE_IS_ESI') ? LSCACHE_IS_ESI : false); // Pass ESI block id // if is not cacheable, terminate check if (!self::is_cacheable()) { self::debug('not cacheable after api_control'); return; } // Check litespeed setting to set cacheable status if (!$this->_setting_cacheable()) { self::set_nocache(); return; } // If user has password cookie, do not cache (moved from vary) global $post; if (!empty($post->post_password) && isset($_COOKIE['wp-postpass_' . COOKIEHASH])) { // If user has password cookie, do not cache self::set_nocache('pswd cookie'); return; } // The following check to the end is ONLY for mobile $is_mobile = apply_filters('litespeed_is_mobile', false); if (!$this->conf(Base::O_CACHE_MOBILE)) { if ($is_mobile) { self::set_nocache('mobile'); } return; } $env_vary = isset($_SERVER['LSCACHE_VARY_VALUE']) ? $_SERVER['LSCACHE_VARY_VALUE'] : false; if (!$env_vary) { $env_vary = isset($_SERVER['HTTP_X_LSCACHE_VARY_VALUE']) ? $_SERVER['HTTP_X_LSCACHE_VARY_VALUE'] : false; } if ($env_vary && strpos($env_vary, 'ismobile') !== false) { if (!wp_is_mobile() && !$is_mobile) { self::set_nocache('is not mobile'); // todo: no need to uncache, it will correct vary value in vary finalize anyways return; } } elseif (wp_is_mobile() || $is_mobile) { self::set_nocache('is mobile'); return; } } /** * Check if is mobile for filter `litespeed_is_mobile` in API * * @since 3.0 * @access public */ public static function is_mobile() { return wp_is_mobile(); } /** * Get request method w/ compatibility to X-Http-Method-Override * * @since 6.2 */ private function _get_req_method() { if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) { self::debug('X-Http-Method-Override -> ' . $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']); defined('LITESPEED_X_HTTP_METHOD_OVERRIDE') || define('LITESPEED_X_HTTP_METHOD_OVERRIDE', true); return $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']; } if (isset($_SERVER['REQUEST_METHOD'])) { return $_SERVER['REQUEST_METHOD']; } return 'unknown'; } /** * Check if a page is cacheable based on litespeed setting. * * @since 1.0.0 * @access private * @return boolean True if cacheable, false otherwise. */ private function _setting_cacheable() { // logged_in users already excluded, no hook added if (!empty($_REQUEST[Router::ACTION])) { return $this->_no_cache_for('Query String Action'); } $method = $this->_get_req_method(); if (defined('LITESPEED_X_HTTP_METHOD_OVERRIDE') && LITESPEED_X_HTTP_METHOD_OVERRIDE && $method == 'HEAD') { return $this->_no_cache_for('HEAD method from override'); } if ('GET' !== $method && 'HEAD' !== $method) { return $this->_no_cache_for('Not GET method: ' . $method); } if (is_feed() && $this->conf(Base::O_CACHE_TTL_FEED) == 0) { return $this->_no_cache_for('feed'); } if (is_trackback()) { return $this->_no_cache_for('trackback'); } if (is_search()) { return $this->_no_cache_for('search'); } // if ( !defined('WP_USE_THEMES') || !WP_USE_THEMES ) { // return $this->_no_cache_for('no theme used'); // } // Check private cache URI setting $excludes = $this->conf(Base::O_CACHE_PRIV_URI); $result = Utility::str_hit_array($_SERVER['REQUEST_URI'], $excludes); if ($result) { self::set_private('Admin cfg Private Cached URI: ' . $result); } if (!self::is_forced_cacheable()) { // Check if URI is excluded from cache $excludes = $this->cls('Data')->load_cache_nocacheable($this->conf(Base::O_CACHE_EXC)); $result = Utility::str_hit_array($_SERVER['REQUEST_URI'], $excludes); if ($result) { return $this->_no_cache_for('Admin configured URI Do not cache: ' . $result); } // Check QS excluded setting $excludes = $this->conf(Base::O_CACHE_EXC_QS); if (!empty($excludes) && ($qs = $this->_is_qs_excluded($excludes))) { return $this->_no_cache_for('Admin configured QS Do not cache: ' . $qs); } $excludes = $this->conf(Base::O_CACHE_EXC_CAT); if (!empty($excludes) && has_category($excludes)) { return $this->_no_cache_for('Admin configured Category Do not cache.'); } $excludes = $this->conf(Base::O_CACHE_EXC_TAG); if (!empty($excludes) && has_tag($excludes)) { return $this->_no_cache_for('Admin configured Tag Do not cache.'); } $excludes = $this->conf(Base::O_CACHE_EXC_COOKIES); if (!empty($excludes) && !empty($_COOKIE)) { $cookie_hit = array_intersect(array_keys($_COOKIE), $excludes); if ($cookie_hit) { return $this->_no_cache_for('Admin configured Cookie Do not cache.'); } } $excludes = $this->conf(Base::O_CACHE_EXC_USERAGENTS); if (!empty($excludes) && isset($_SERVER['HTTP_USER_AGENT'])) { $nummatches = preg_match(Utility::arr2regex($excludes), $_SERVER['HTTP_USER_AGENT']); if ($nummatches) { return $this->_no_cache_for('Admin configured User Agent Do not cache.'); } } // Check if is exclude roles ( Need to set Vary too ) if ($result = $this->in_cache_exc_roles()) { return $this->_no_cache_for('Role Excludes setting ' . $result); } } return true; } /** * Write a debug message for if a page is not cacheable. * * @since 1.0.0 * @access private * @param string $reason An explanation for why the page is not cacheable. * @return boolean Return false. */ private function _no_cache_for($reason) { self::debug('X Cache_control off - ' . $reason); return false; } /** * Check if current request has qs excluded setting * * @since 1.3 * @access private * @param array $excludes QS excludes setting * @return boolean|string False if not excluded, otherwise the hit qs list */ private function _is_qs_excluded($excludes) { if (!empty($_GET) && ($intersect = array_intersect(array_keys($_GET), $excludes))) { return implode(',', $intersect); } return false; } } object.lib.php 0000644 00000103740 15162226337 0007304 0 ustar 00 <?php /** * LiteSpeed Object Cache Library * * @since 1.8 */ defined('WPINC') || exit(); /** * Handle exception */ if (!function_exists('litespeed_exception_handler')) { function litespeed_exception_handler($errno, $errstr, $errfile, $errline) { throw new \ErrorException($errstr, 0, $errno, $errfile, $errline); } } require_once __DIR__ . '/object-cache.cls.php'; /** * Sets up Object Cache Global and assigns it. * * @since 1.8 * * @global WP_Object_Cache $wp_object_cache */ function wp_cache_init() { $GLOBALS['wp_object_cache'] = WP_Object_Cache::get_instance(); } /** * Adds data to the cache, if the cache key doesn't already exist. * * @since 1.8 * * @see WP_Object_Cache::add() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int|string $key The cache key to use for retrieval later. * @param mixed $data The data to add to the cache. * @param string $group Optional. The group to add the cache to. Enables the same key * to be used across groups. Default empty. * @param int $expire Optional. When the cache data should expire, in seconds. * Default 0 (no expiration). * @return bool True on success, false if cache key and group already exist. */ function wp_cache_add($key, $data, $group = '', $expire = 0) { global $wp_object_cache; return $wp_object_cache->add($key, $data, $group, (int) $expire); } /** * Adds multiple values to the cache in one call. * * @since 5.4 * * @see WP_Object_Cache::add_multiple() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param array $data Array of keys and values to be set. * @param string $group Optional. Where the cache contents are grouped. Default empty. * @param int $expire Optional. When to expire the cache contents, in seconds. * Default 0 (no expiration). * @return bool[] Array of return values, grouped by key. Each value is either * true on success, or false if cache key and group already exist. */ function wp_cache_add_multiple(array $data, $group = '', $expire = 0) { global $wp_object_cache; return $wp_object_cache->add_multiple($data, $group, $expire); } /** * Replaces the contents of the cache with new data. * * @since 1.8 * * @see WP_Object_Cache::replace() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int|string $key The key for the cache data that should be replaced. * @param mixed $data The new data to store in the cache. * @param string $group Optional. The group for the cache data that should be replaced. * Default empty. * @param int $expire Optional. When to expire the cache contents, in seconds. * Default 0 (no expiration). * @return bool True if contents were replaced, false if original value does not exist. */ function wp_cache_replace($key, $data, $group = '', $expire = 0) { global $wp_object_cache; return $wp_object_cache->replace($key, $data, $group, (int) $expire); } /** * Saves the data to the cache. * * Differs from wp_cache_add() and wp_cache_replace() in that it will always write data. * * @since 1.8 * * @see WP_Object_Cache::set() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int|string $key The cache key to use for retrieval later. * @param mixed $data The contents to store in the cache. * @param string $group Optional. Where to group the cache contents. Enables the same key * to be used across groups. Default empty. * @param int $expire Optional. When to expire the cache contents, in seconds. * Default 0 (no expiration). * @return bool True on success, false on failure. */ function wp_cache_set($key, $data, $group = '', $expire = 0) { global $wp_object_cache; return $wp_object_cache->set($key, $data, $group, (int) $expire); } /** * Sets multiple values to the cache in one call. * * @since 5.4 * * @see WP_Object_Cache::set_multiple() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param array $data Array of keys and values to be set. * @param string $group Optional. Where the cache contents are grouped. Default empty. * @param int $expire Optional. When to expire the cache contents, in seconds. * Default 0 (no expiration). * @return bool[] Array of return values, grouped by key. Each value is either * true on success, or false on failure. */ function wp_cache_set_multiple(array $data, $group = '', $expire = 0) { global $wp_object_cache; return $wp_object_cache->set_multiple($data, $group, $expire); } /** * Retrieves the cache contents from the cache by key and group. * * @since 1.8 * * @see WP_Object_Cache::get() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int|string $key The key under which the cache contents are stored. * @param string $group Optional. Where the cache contents are grouped. Default empty. * @param bool $force Optional. Whether to force an update of the local cache * from the persistent cache. Default false. * @param bool $found Optional. Whether the key was found in the cache (passed by reference). * Disambiguates a return of false, a storable value. Default null. * @return mixed|false The cache contents on success, false on failure to retrieve contents. */ function wp_cache_get($key, $group = '', $force = false, &$found = null) { global $wp_object_cache; return $wp_object_cache->get($key, $group, $force, $found); } /** * Retrieves multiple values from the cache in one call. * * @since 5.4 * * @see WP_Object_Cache::get_multiple() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param array $keys Array of keys under which the cache contents are stored. * @param string $group Optional. Where the cache contents are grouped. Default empty. * @param bool $force Optional. Whether to force an update of the local cache * from the persistent cache. Default false. * @return array Array of return values, grouped by key. Each value is either * the cache contents on success, or false on failure. */ function wp_cache_get_multiple($keys, $group = '', $force = false) { global $wp_object_cache; return $wp_object_cache->get_multiple($keys, $group, $force); } /** * Removes the cache contents matching key and group. * * @since 1.8 * * @see WP_Object_Cache::delete() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int|string $key What the contents in the cache are called. * @param string $group Optional. Where the cache contents are grouped. Default empty. * @return bool True on successful removal, false on failure. */ function wp_cache_delete($key, $group = '') { global $wp_object_cache; return $wp_object_cache->delete($key, $group); } /** * Deletes multiple values from the cache in one call. * * @since 5.4 * * @see WP_Object_Cache::delete_multiple() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param array $keys Array of keys under which the cache to deleted. * @param string $group Optional. Where the cache contents are grouped. Default empty. * @return bool[] Array of return values, grouped by key. Each value is either * true on success, or false if the contents were not deleted. */ function wp_cache_delete_multiple(array $keys, $group = '') { global $wp_object_cache; return $wp_object_cache->delete_multiple($keys, $group); } /** * Increments numeric cache item's value. * * @since 1.8 * * @see WP_Object_Cache::incr() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int|string $key The key for the cache contents that should be incremented. * @param int $offset Optional. The amount by which to increment the item's value. * Default 1. * @param string $group Optional. The group the key is in. Default empty. * @return int|false The item's new value on success, false on failure. */ function wp_cache_incr($key, $offset = 1, $group = '') { global $wp_object_cache; return $wp_object_cache->incr($key, $offset, $group); } /** * Decrements numeric cache item's value. * * @since 1.8 * * @see WP_Object_Cache::decr() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int|string $key The cache key to decrement. * @param int $offset Optional. The amount by which to decrement the item's value. * Default 1. * @param string $group Optional. The group the key is in. Default empty. * @return int|false The item's new value on success, false on failure. */ function wp_cache_decr($key, $offset = 1, $group = '') { global $wp_object_cache; return $wp_object_cache->decr($key, $offset, $group); } /** * Removes all cache items. * * @since 1.8 * * @see WP_Object_Cache::flush() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @return bool True on success, false on failure. */ function wp_cache_flush() { global $wp_object_cache; return $wp_object_cache->flush(); } /** * Removes all cache items from the in-memory runtime cache. * * @since 5.4 * * @see WP_Object_Cache::flush_runtime() * * @return bool True on success, false on failure. */ function wp_cache_flush_runtime() { global $wp_object_cache; return $wp_object_cache->flush_runtime(); } /** * Removes all cache items in a group, if the object cache implementation supports it. * * Before calling this function, always check for group flushing support using the * `wp_cache_supports( 'flush_group' )` function. * * @since 5.4 * * @see WP_Object_Cache::flush_group() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param string $group Name of group to remove from cache. * @return bool True if group was flushed, false otherwise. */ function wp_cache_flush_group($group) { global $wp_object_cache; return $wp_object_cache->flush_group($group); } /** * Determines whether the object cache implementation supports a particular feature. * * @since 5.4 * * @param string $feature Name of the feature to check for. Possible values include: * 'add_multiple', 'set_multiple', 'get_multiple', 'delete_multiple', * 'flush_runtime', 'flush_group'. * @return bool True if the feature is supported, false otherwise. */ function wp_cache_supports($feature) { switch ($feature) { case 'add_multiple': case 'set_multiple': case 'get_multiple': case 'delete_multiple': case 'flush_runtime': return true; case 'flush_group': default: return false; } } /** * Closes the cache. * * This function has ceased to do anything since WordPress 2.5. The * functionality was removed along with the rest of the persistent cache. * * This does not mean that plugins can't implement this function when they need * to make sure that the cache is cleaned up after WordPress no longer needs it. * * @since 1.8 * * @return true Always returns true. */ function wp_cache_close() { return true; } /** * Adds a group or set of groups to the list of global groups. * * @since 1.8 * * @see WP_Object_Cache::add_global_groups() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param string|string[] $groups A group or an array of groups to add. */ function wp_cache_add_global_groups($groups) { global $wp_object_cache; $wp_object_cache->add_global_groups($groups); } /** * Adds a group or set of groups to the list of non-persistent groups. * * @since 1.8 * * @param string|string[] $groups A group or an array of groups to add. */ function wp_cache_add_non_persistent_groups($groups) { global $wp_object_cache; $wp_object_cache->add_non_persistent_groups($groups); } /** * Switches the internal blog ID. * * This changes the blog id used to create keys in blog specific groups. * * @since 1.8 * * @see WP_Object_Cache::switch_to_blog() * @global WP_Object_Cache $wp_object_cache Object cache global instance. * * @param int $blog_id Site ID. */ function wp_cache_switch_to_blog($blog_id) { global $wp_object_cache; $wp_object_cache->switch_to_blog($blog_id); } class WP_Object_Cache { protected static $_instance; private $_object_cache; private $_cache = array(); private $_cache_404 = array(); private $cache_total = 0; private $count_hit_incall = 0; private $count_hit = 0; private $count_miss_incall = 0; private $count_miss = 0; private $count_set = 0; protected $global_groups = array(); private $blog_prefix; private $multisite; /** * Init. * * @since 1.8 */ public function __construct() { $this->_object_cache = \LiteSpeed\Object_Cache::cls(); $this->multisite = is_multisite(); $this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : ''; /** * Fix multiple instance using same oc issue * @since 1.8.2 */ !defined('LSOC_PREFIX') && define('LSOC_PREFIX', substr(md5(__FILE__), -5)); } /** * Makes private properties readable for backward compatibility. * * @since 5.4 * @access public * * @param string $name Property to get. * @return mixed Property. */ public function __get($name) { return $this->$name; } /** * Makes private properties settable for backward compatibility. * * @since 5.4 * @access public * * @param string $name Property to set. * @param mixed $value Property value. * @return mixed Newly-set property. */ public function __set($name, $value) { return $this->$name = $value; } /** * Makes private properties checkable for backward compatibility. * * @since 5.4 * @access public * * @param string $name Property to check if set. * @return bool Whether the property is set. */ public function __isset($name) { return isset($this->$name); } /** * Makes private properties un-settable for backward compatibility. * * @since 5.4 * @access public * * @param string $name Property to unset. */ public function __unset($name) { unset($this->$name); } /** * Serves as a utility function to determine whether a key is valid. * * @since 5.4 * @access protected * * @param int|string $key Cache key to check for validity. * @return bool Whether the key is valid. */ protected function is_valid_key($key) { if (is_int($key)) { return true; } if (is_string($key) && trim($key) !== '') { return true; } $type = gettype($key); if (!function_exists('__')) { wp_load_translations_early(); } $message = is_string($key) ? __('Cache key must not be an empty string.') : /* translators: %s: The type of the given cache key. */ sprintf(__('Cache key must be integer or non-empty string, %s given.'), $type); _doing_it_wrong(sprintf('%s::%s', __CLASS__, debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']), $message, '6.1.0'); return false; } /** * Get the final key. * * @since 1.8 * @access private */ private function _key($key, $group = 'default') { if (empty($group)) { $group = 'default'; } $prefix = $this->_object_cache->is_global($group) ? '' : $this->blog_prefix; return LSOC_PREFIX . $prefix . $group . '.' . $key; } /** * Output debug info. * * @since 1.8 * @access public */ public function debug() { return ' [total] ' . $this->cache_total . ' [hit_incall] ' . $this->count_hit_incall . ' [hit] ' . $this->count_hit . ' [miss_incall] ' . $this->count_miss_incall . ' [miss] ' . $this->count_miss . ' [set] ' . $this->count_set; } /** * Adds data to the cache if it doesn't already exist. * * @since 1.8 * @access public * * @uses WP_Object_Cache::_exists() Checks to see if the cache already has data. * @uses WP_Object_Cache::set() Sets the data after the checking the cache * contents existence. * * @param int|string $key What to call the contents in the cache. * @param mixed $data The contents to store in the cache. * @param string $group Optional. Where to group the cache contents. Default 'default'. * @param int $expire Optional. When to expire the cache contents, in seconds. * Default 0 (no expiration). * @return bool True on success, false if cache key and group already exist. */ public function add($key, $data, $group = 'default', $expire = 0) { if (wp_suspend_cache_addition()) { return false; } if (!$this->is_valid_key($key)) { return false; } if (empty($group)) { $group = 'default'; } $id = $this->_key($key, $group); if (array_key_exists($id, $this->_cache)) { return false; } return $this->set($key, $data, $group, (int) $expire); } /** * Adds multiple values to the cache in one call. * * @since 5.4 * @access public * * @param array $data Array of keys and values to be added. * @param string $group Optional. Where the cache contents are grouped. Default empty. * @param int $expire Optional. When to expire the cache contents, in seconds. * Default 0 (no expiration). * @return bool[] Array of return values, grouped by key. Each value is either * true on success, or false if cache key and group already exist. */ public function add_multiple(array $data, $group = '', $expire = 0) { $values = array(); foreach ($data as $key => $value) { $values[$key] = $this->add($key, $value, $group, $expire); } return $values; } /** * Replaces the contents in the cache, if contents already exist. * * @since 1.8 * @access public * * @see WP_Object_Cache::set() * * @param int|string $key What to call the contents in the cache. * @param mixed $data The contents to store in the cache. * @param string $group Optional. Where to group the cache contents. Default 'default'. * @param int $expire Optional. When to expire the cache contents, in seconds. * Default 0 (no expiration). * @return bool True if contents were replaced, false if original value does not exist. */ public function replace($key, $data, $group = 'default', $expire = 0) { if (!$this->is_valid_key($key)) { return false; } if (empty($group)) { $group = 'default'; } $id = $this->_key($key, $group); if (!array_key_exists($id, $this->_cache)) { return false; } return $this->set($key, $data, $group, (int) $expire); } /** * Sets the data contents into the cache. * * The cache contents are grouped by the $group parameter followed by the * $key. This allows for duplicate IDs in unique groups. Therefore, naming of * the group should be used with care and should follow normal function * naming guidelines outside of core WordPress usage. * * The $expire parameter is not used, because the cache will automatically * expire for each time a page is accessed and PHP finishes. The method is * more for cache plugins which use files. * * @since 1.8 * @since 5.4 Returns false if cache key is invalid. * @access public * * @param int|string $key What to call the contents in the cache. * @param mixed $data The contents to store in the cache. * @param string $group Optional. Where to group the cache contents. Default 'default'. * @param int $expire Optional. When to expire the cache contents, in seconds. * Default 0 (no expiration). * @return bool True if contents were set, false if key is invalid. */ public function set($key, $data, $group = 'default', $expire = 0) { if (!$this->is_valid_key($key)) { return false; } if (empty($group)) { $group = 'default'; } $id = $this->_key($key, $group); if (is_object($data)) { $data = clone $data; } // error_log("oc: set \t\t\t[key] " . $id ); $this->_cache[$id] = $data; if (array_key_exists($id, $this->_cache_404)) { // error_log("oc: unset404\t\t\t[key] " . $id ); unset($this->_cache_404[$id]); } if (!$this->_object_cache->is_non_persistent($group)) { $this->_object_cache->set($id, serialize(array('data' => $data)), (int) $expire); $this->count_set++; } if ($this->_object_cache->store_transients($group)) { $this->_transient_set($key, $data, $group, (int) $expire); } return true; } /** * Sets multiple values to the cache in one call. * * @since 5.4 * @access public * * @param array $data Array of key and value to be set. * @param string $group Optional. Where the cache contents are grouped. Default empty. * @param int $expire Optional. When to expire the cache contents, in seconds. * Default 0 (no expiration). * @return bool[] Array of return values, grouped by key. Each value is always true. */ public function set_multiple(array $data, $group = '', $expire = 0) { $values = array(); foreach ($data as $key => $value) { $values[$key] = $this->set($key, $value, $group, $expire); } return $values; } /** * Retrieves the cache contents, if it exists. * * The contents will be first attempted to be retrieved by searching by the * key in the cache group. If the cache is hit (success) then the contents * are returned. * * On failure, the number of cache misses will be incremented. * * @since 1.8 * @access public * * @param int|string $key The key under which the cache contents are stored. * @param string $group Optional. Where the cache contents are grouped. Default 'default'. * @param bool $force Optional. Unused. Whether to force an update of the local cache * from the persistent cache. Default false. * @param bool $found Optional. Whether the key was found in the cache (passed by reference). * Disambiguates a return of false, a storable value. Default null. * @return mixed|false The cache contents on success, false on failure to retrieve contents. */ public function get($key, $group = 'default', $force = false, &$found = null) { if (!$this->is_valid_key($key)) { return false; } if (empty($group)) { $group = 'default'; } $id = $this->_key($key, $group); // error_log(''); // error_log("oc: get \t\t\t[key] " . $id . ( $force ? "\t\t\t [forced] " : '' ) ); $found = false; $found_in_oc = false; $cache_val = false; if (array_key_exists($id, $this->_cache) && !$force) { $found = true; $cache_val = $this->_cache[$id]; $this->count_hit_incall++; } elseif (!array_key_exists($id, $this->_cache_404) && !$this->_object_cache->is_non_persistent($group)) { $v = $this->_object_cache->get($id); if ($v !== null) { $v = @maybe_unserialize($v); } // To be compatible with false val if (is_array($v) && array_key_exists('data', $v)) { $this->count_hit++; $found = true; $found_in_oc = true; $cache_val = $v['data']; } else { // Can't find key, cache it to 404 // error_log("oc: add404\t\t\t[key] " . $id ); $this->_cache_404[$id] = 1; $this->count_miss++; } } else { $this->count_miss_incall++; } if (is_object($cache_val)) { $cache_val = clone $cache_val; } // If not found but has `Store Transients` cfg on, still need to follow WP's get_transient() logic if (!$found && $this->_object_cache->store_transients($group)) { $cache_val = $this->_transient_get($key, $group); if ($cache_val) { $found = true; // $found not used for now (v1.8.3) } } if ($found_in_oc) { $this->_cache[$id] = $cache_val; } $this->cache_total++; return $cache_val; } /** * Retrieves multiple values from the cache in one call. * * @since 5.4 * @access public * * @param array $keys Array of keys under which the cache contents are stored. * @param string $group Optional. Where the cache contents are grouped. Default 'default'. * @param bool $force Optional. Whether to force an update of the local cache * from the persistent cache. Default false. * @return array Array of return values, grouped by key. Each value is either * the cache contents on success, or false on failure. */ public function get_multiple($keys, $group = 'default', $force = false) { $values = array(); foreach ($keys as $key) { $values[$key] = $this->get($key, $group, $force); } return $values; } /** * Removes the contents of the cache key in the group. * * If the cache key does not exist in the group, then nothing will happen. * * @since 1.8 * @access public * * @param int|string $key What the contents in the cache are called. * @param string $group Optional. Where the cache contents are grouped. Default 'default'. * @param bool $deprecated Optional. Unused. Default false. * @return bool True on success, false if the contents were not deleted. */ public function delete($key, $group = 'default', $deprecated = false) { if (!$this->is_valid_key($key)) { return false; } if (empty($group)) { $group = 'default'; } $id = $this->_key($key, $group); if ($this->_object_cache->store_transients($group)) { $this->_transient_del($key, $group); } if (array_key_exists($id, $this->_cache)) { unset($this->_cache[$id]); } // error_log("oc: delete \t\t\t[key] " . $id ); if ($this->_object_cache->is_non_persistent($group)) { return false; } return $this->_object_cache->delete($id); } /** * Deletes multiple values from the cache in one call. * * @since 5.4 * @access public * * @param array $keys Array of keys to be deleted. * @param string $group Optional. Where the cache contents are grouped. Default empty. * @return bool[] Array of return values, grouped by key. Each value is either * true on success, or false if the contents were not deleted. */ public function delete_multiple(array $keys, $group = '') { $values = array(); foreach ($keys as $key) { $values[$key] = $this->delete($key, $group); } return $values; } /** * Increments numeric cache item's value. * * @since 5.4 * * @param int|string $key The cache key to increment. * @param int $offset Optional. The amount by which to increment the item's value. * Default 1. * @param string $group Optional. The group the key is in. Default 'default'. * @return int|false The item's new value on success, false on failure. */ public function incr($key, $offset = 1, $group = 'default') { return $this->incr_desr($key, $offset, $group, true); } /** * Decrements numeric cache item's value. * * @since 5.4 * * @param int|string $key The cache key to decrement. * @param int $offset Optional. The amount by which to decrement the item's value. * Default 1. * @param string $group Optional. The group the key is in. Default 'default'. * @return int|false The item's new value on success, false on failure. */ public function decr($key, $offset = 1, $group = 'default') { return $this->incr_desr($key, $offset, $group, false); } /** * Increments or decrements numeric cache item's value. * * @since 1.8 * @access public */ public function incr_desr($key, $offset = 1, $group = 'default', $incr = true) { if (!$this->is_valid_key($key)) { return false; } if (empty($group)) { $group = 'default'; } $cache_val = $this->get($key, $group); if (false === $cache_val) { return false; } if (!is_numeric($cache_val)) { $cache_val = 0; } $offset = (int) $offset; if ($incr) { $cache_val += $offset; } else { $cache_val -= $offset; } if ($cache_val < 0) { $cache_val = 0; } $this->set($key, $cache_val, $group); return $cache_val; } /** * Clears the object cache of all data. * * @since 1.8 * @access public * * @return true Always returns true. */ public function flush() { $this->flush_runtime(); $this->_object_cache->flush(); return true; } /** * Removes all cache items from the in-memory runtime cache. * * @since 5.4 * @access public * * @return true Always returns true. */ public function flush_runtime() { $this->_cache = array(); $this->_cache_404 = array(); return true; } /** * Removes all cache items in a group. * * @since 5.4 * @access public * * @param string $group Name of group to remove from cache. * @return true Always returns true. */ public function flush_group($group) { // unset( $this->cache[ $group ] ); return true; } /** * Sets the list of global cache groups. * * @since 1.8 * @access public * * @param string|string[] $groups List of groups that are global. */ public function add_global_groups($groups) { $groups = (array) $groups; $this->_object_cache->add_global_groups($groups); } /** * Sets the list of non-persistent cache groups. * * @since 1.8 * @access public */ public function add_non_persistent_groups($groups) { $groups = (array) $groups; $this->_object_cache->add_non_persistent_groups($groups); } /** * Switches the internal blog ID. * * This changes the blog ID used to create keys in blog specific groups. * * @since 1.8 * @access public * * @param int $blog_id Blog ID. */ public function switch_to_blog($blog_id) { $blog_id = (int) $blog_id; $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; } /** * Get transient from wp table * * @since 1.8.3 * @access private * @see `wp-includes/option.php` function `get_transient`/`set_site_transient` */ private function _transient_get($transient, $group) { if ($group == 'transient') { /**** Ori WP func start ****/ $transient_option = '_transient_' . $transient; if (!wp_installing()) { // If option is not in alloptions, it is not autoloaded and thus has a timeout $alloptions = wp_load_alloptions(); if (!isset($alloptions[$transient_option])) { $transient_timeout = '_transient_timeout_' . $transient; $timeout = get_option($transient_timeout); if (false !== $timeout && $timeout < time()) { delete_option($transient_option); delete_option($transient_timeout); $value = false; } } } if (!isset($value)) { $value = get_option($transient_option); } /**** Ori WP func end ****/ } elseif ($group == 'site-transient') { /**** Ori WP func start ****/ $no_timeout = array('update_core', 'update_plugins', 'update_themes'); $transient_option = '_site_transient_' . $transient; if (!in_array($transient, $no_timeout)) { $transient_timeout = '_site_transient_timeout_' . $transient; $timeout = get_site_option($transient_timeout); if (false !== $timeout && $timeout < time()) { delete_site_option($transient_option); delete_site_option($transient_timeout); $value = false; } } if (!isset($value)) { $value = get_site_option($transient_option); } /**** Ori WP func end ****/ } else { $value = false; } return $value; } /** * Set transient to WP table * * @since 1.8.3 * @access private * @see `wp-includes/option.php` function `set_transient`/`set_site_transient` */ private function _transient_set($transient, $value, $group, $expiration) { if ($group == 'transient') { /**** Ori WP func start ****/ $transient_timeout = '_transient_timeout_' . $transient; $transient_option = '_transient_' . $transient; if (false === get_option($transient_option)) { $autoload = 'yes'; if ((int) $expiration) { $autoload = 'no'; add_option($transient_timeout, time() + (int) $expiration, '', 'no'); } $result = add_option($transient_option, $value, '', $autoload); } else { // If expiration is requested, but the transient has no timeout option, // delete, then re-create transient rather than update. $update = true; if ((int) $expiration) { if (false === get_option($transient_timeout)) { delete_option($transient_option); add_option($transient_timeout, time() + (int) $expiration, '', 'no'); $result = add_option($transient_option, $value, '', 'no'); $update = false; } else { update_option($transient_timeout, time() + (int) $expiration); } } if ($update) { $result = update_option($transient_option, $value); } } /**** Ori WP func end ****/ } elseif ($group == 'site-transient') { /**** Ori WP func start ****/ $transient_timeout = '_site_transient_timeout_' . $transient; $option = '_site_transient_' . $transient; if (false === get_site_option($option)) { if ((int) $expiration) { add_site_option($transient_timeout, time() + (int) $expiration); } $result = add_site_option($option, $value); } else { if ((int) $expiration) { update_site_option($transient_timeout, time() + (int) $expiration); } $result = update_site_option($option, $value); } /**** Ori WP func end ****/ } else { $result = null; } return $result; } /** * Delete transient from WP table * * @since 1.8.3 * @access private * @see `wp-includes/option.php` function `delete_transient`/`delete_site_transient` */ private function _transient_del($transient, $group) { if ($group == 'transient') { /**** Ori WP func start ****/ $option_timeout = '_transient_timeout_' . $transient; $option = '_transient_' . $transient; $result = delete_option($option); if ($result) { delete_option($option_timeout); } /**** Ori WP func end ****/ } elseif ($group == 'site-transient') { /**** Ori WP func start ****/ $option_timeout = '_site_transient_timeout_' . $transient; $option = '_site_transient_' . $transient; $result = delete_site_option($option); if ($result) { delete_site_option($option_timeout); } /**** Ori WP func end ****/ } } /** * Get the current instance object. * * @since 1.8 * @access public */ public static function get_instance() { if (!isset(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; } } task.cls.php 0000644 00000013661 15162226341 0007010 0 ustar 00 <?php /** * The cron task class. * * @since 1.1.3 * @since 1.5 Moved into /inc */ namespace LiteSpeed; defined('WPINC') || exit(); class Task extends Root { const LOG_TAG = '⏰'; private static $_triggers = array( Base::O_IMG_OPTM_CRON => array('name' => 'litespeed_task_imgoptm_pull', 'hook' => 'LiteSpeed\Img_Optm::start_async_cron'), // always fetch immediately Base::O_OPTM_CSS_ASYNC => array('name' => 'litespeed_task_ccss', 'hook' => 'LiteSpeed\CSS::cron_ccss'), Base::O_OPTM_UCSS => array('name' => 'litespeed_task_ucss', 'hook' => 'LiteSpeed\UCSS::cron'), Base::O_MEDIA_VPI_CRON => array('name' => 'litespeed_task_vpi', 'hook' => 'LiteSpeed\VPI::cron'), Base::O_MEDIA_PLACEHOLDER_RESP_ASYNC => array('name' => 'litespeed_task_lqip', 'hook' => 'LiteSpeed\Placeholder::cron'), Base::O_DISCUSS_AVATAR_CRON => array('name' => 'litespeed_task_avatar', 'hook' => 'LiteSpeed\Avatar::cron'), Base::O_IMG_OPTM_AUTO => array('name' => 'litespeed_task_imgoptm_req', 'hook' => 'LiteSpeed\Img_Optm::cron_auto_request'), Base::O_CRAWLER => array('name' => 'litespeed_task_crawler', 'hook' => 'LiteSpeed\Crawler::start_async_cron'), // Set crawler to last one to use above results ); private static $_guest_options = array(Base::O_OPTM_CSS_ASYNC, Base::O_OPTM_UCSS, Base::O_MEDIA_VPI); const FILTER_CRAWLER = 'litespeed_crawl_filter'; const FILTER = 'litespeed_filter'; /** * Keep all tasks in cron * * @since 3.0 * @access public */ public function init() { self::debug2('Init'); add_filter('cron_schedules', array($this, 'lscache_cron_filter')); $guest_optm = $this->conf(Base::O_GUEST) && $this->conf(Base::O_GUEST_OPTM); foreach (self::$_triggers as $id => $trigger) { if ($id != Base::O_IMG_OPTM_CRON && !$this->conf($id)) { if (!$guest_optm || !in_array($id, self::$_guest_options)) { continue; } } // Special check for crawler if ($id == Base::O_CRAWLER) { if (!Router::can_crawl()) { continue; } add_filter('cron_schedules', array($this, 'lscache_cron_filter_crawler')); } if (!wp_next_scheduled($trigger['name'])) { self::debug('Cron hook register [name] ' . $trigger['name']); wp_schedule_event(time(), $id == Base::O_CRAWLER ? self::FILTER_CRAWLER : self::FILTER, $trigger['name']); } add_action($trigger['name'], $trigger['hook']); } } /** * Handle all async noabort requests * * @since 5.5 */ public static function async_litespeed_handler() { $hash_data = self::get_option('async_call-hash', array()); if (!$hash_data || !is_array($hash_data) || empty($hash_data['hash']) || empty($hash_data['ts'])) { self::debug('async_litespeed_handler no hash data', $hash_data); return; } if (time() - $hash_data['ts'] > 120 || empty($_GET['nonce']) || $_GET['nonce'] != $hash_data['hash']) { self::debug('async_litespeed_handler nonce mismatch'); return; } self::delete_option('async_call-hash'); $type = Router::verify_type(); self::debug('type=' . $type); // Don't lock up other requests while processing session_write_close(); switch ($type) { case 'crawler': Crawler::async_handler(); break; case 'crawler_force': Crawler::async_handler(true); break; case 'imgoptm': Img_Optm::async_handler(); break; case 'imgoptm_force': Img_Optm::async_handler(true); break; default: } } /** * Async caller wrapper func * * @since 5.5 */ public static function async_call($type) { $hash = Str::rrand(32); self::update_option('async_call-hash', array('hash' => $hash, 'ts' => time())); $args = array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => false, // 'cookies' => $_COOKIE, ); $qs = array( 'action' => 'async_litespeed', 'nonce' => $hash, Router::TYPE => $type, ); $url = add_query_arg($qs, admin_url('admin-ajax.php')); self::debug('async call to ' . $url); wp_safe_remote_post(esc_url_raw($url), $args); } /** * Clean all potential existing crons * * @since 3.0 * @access public */ public static function destroy() { Utility::compatibility(); array_map('wp_clear_scheduled_hook', array_column(self::$_triggers, 'name')); } /** * Try to clean the crons if disabled * * @since 3.0 * @access public */ public function try_clean($id) { // Clean v2's leftover cron ( will remove in v3.1 ) // foreach ( wp_get_ready_cron_jobs() as $hooks ) { // foreach ( $hooks as $hook => $v ) { // if ( strpos( $hook, 'litespeed_' ) === 0 && ( substr( $hook, -8 ) === '_trigger' || strpos( $hook, 'litespeed_task_' ) !== 0 ) ) { // self::debug( 'Cron clear legacy [hook] ' . $hook ); // wp_clear_scheduled_hook( $hook ); // } // } // } if ($id && !empty(self::$_triggers[$id])) { if (!$this->conf($id) || ($id == Base::O_CRAWLER && !Router::can_crawl())) { self::debug('Cron clear [id] ' . $id . ' [hook] ' . self::$_triggers[$id]['name']); wp_clear_scheduled_hook(self::$_triggers[$id]['name']); } return; } self::debug('❌ Unknown cron [id] ' . $id); } /** * Register cron interval imgoptm * * @since 1.6.1 * @access public */ public function lscache_cron_filter($schedules) { if (!array_key_exists(self::FILTER, $schedules)) { $schedules[self::FILTER] = array( 'interval' => 60, 'display' => __('Every Minute', 'litespeed-cache'), ); } return $schedules; } /** * Register cron interval * * @since 1.1.0 * @access public */ public function lscache_cron_filter_crawler($schedules) { $CRAWLER_RUN_INTERVAL = defined('LITESPEED_CRAWLER_RUN_INTERVAL') ? LITESPEED_CRAWLER_RUN_INTERVAL : 600; // $wp_schedules = wp_get_schedules(); if (!array_key_exists(self::FILTER_CRAWLER, $schedules)) { // self::debug('Crawler cron log: cron filter '.$interval.' added'); $schedules[self::FILTER_CRAWLER] = array( 'interval' => $CRAWLER_RUN_INTERVAL, 'display' => __('LiteSpeed Crawler Cron', 'litespeed-cache'), ); } return $schedules; } } utility.cls.php 0000644 00000051252 15162226342 0007550 0 ustar 00 <?php /** * The utility class. * * @since 1.1.5 * @since 1.5 Moved into /inc */ namespace LiteSpeed; defined('WPINC') || exit(); class Utility extends Root { private static $_internal_domains; /** * Validate regex * * @since 1.0.9 * @since 3.0 Moved here from admin-settings.cls * @access public * @return bool True for valid rules, false otherwise. */ public static function syntax_checker($rules) { return preg_match(self::arr2regex($rules), '') !== false; } /** * Combine regex array to regex rule * * @since 3.0 */ public static function arr2regex($arr, $drop_delimiter = false) { $arr = self::sanitize_lines($arr); $new_arr = array(); foreach ($arr as $v) { $new_arr[] = preg_quote($v, '#'); } $regex = implode('|', $new_arr); $regex = str_replace(' ', '\\ ', $regex); if ($drop_delimiter) { return $regex; } return '#' . $regex . '#'; } /** * Replace wildcard to regex * * @since 3.2.2 */ public static function wildcard2regex($string) { if (is_array($string)) { return array_map(__CLASS__ . '::wildcard2regex', $string); } if (strpos($string, '*') !== false) { $string = preg_quote($string, '#'); $string = str_replace('\*', '.*', $string); } return $string; } /** * Check if an URL or current page is REST req or not * * @since 2.9.3 * @deprecated 2.9.4 Moved to REST class * @access public */ public static function is_rest($url = false) { return false; } /** * Get current page type * * @since 2.9 */ public static function page_type() { global $wp_query; $page_type = 'default'; if ($wp_query->is_page) { $page_type = is_front_page() ? 'front' : 'page'; } elseif ($wp_query->is_home) { $page_type = 'home'; } elseif ($wp_query->is_single) { // $page_type = $wp_query->is_attachment ? 'attachment' : 'single'; $page_type = get_post_type(); } elseif ($wp_query->is_category) { $page_type = 'category'; } elseif ($wp_query->is_tag) { $page_type = 'tag'; } elseif ($wp_query->is_tax) { $page_type = 'tax'; // $page_type = get_queried_object()->taxonomy; } elseif ($wp_query->is_archive) { if ($wp_query->is_day) { $page_type = 'day'; } elseif ($wp_query->is_month) { $page_type = 'month'; } elseif ($wp_query->is_year) { $page_type = 'year'; } elseif ($wp_query->is_author) { $page_type = 'author'; } else { $page_type = 'archive'; } } elseif ($wp_query->is_search) { $page_type = 'search'; } elseif ($wp_query->is_404) { $page_type = '404'; } return $page_type; // if ( is_404() ) { // $page_type = '404'; // } // elseif ( is_singular() ) { // $page_type = get_post_type(); // } // elseif ( is_home() && get_option( 'show_on_front' ) == 'page' ) { // $page_type = 'home'; // } // elseif ( is_front_page() ) { // $page_type = 'front'; // } // elseif ( is_tax() ) { // $page_type = get_queried_object()->taxonomy; // } // elseif ( is_category() ) { // $page_type = 'category'; // } // elseif ( is_tag() ) { // $page_type = 'tag'; // } // return $page_type; } /** * Get ping speed * * @since 2.9 */ public static function ping($domain) { if (strpos($domain, ':')) { $domain = parse_url($domain, PHP_URL_HOST); } $starttime = microtime(true); $file = fsockopen($domain, 443, $errno, $errstr, 10); $stoptime = microtime(true); $status = 0; if (!$file) { $status = 99999; } // Site is down else { fclose($file); $status = ($stoptime - $starttime) * 1000; $status = floor($status); } Debug2::debug("[Util] ping [Domain] $domain \t[Speed] $status"); return $status; } /** * Set seconds/timestamp to readable format * * @since 1.6.5 * @access public */ public static function readable_time($seconds_or_timestamp, $timeout = 3600, $forward = false) { if (strlen($seconds_or_timestamp) == 10) { $seconds = time() - $seconds_or_timestamp; if ($seconds > $timeout) { return date('m/d/Y H:i:s', $seconds_or_timestamp + LITESPEED_TIME_OFFSET); } } else { $seconds = $seconds_or_timestamp; } $res = ''; if ($seconds > 86400) { $num = floor($seconds / 86400); $res .= $num . 'd'; $seconds %= 86400; } if ($seconds > 3600) { if ($res) { $res .= ', '; } $num = floor($seconds / 3600); $res .= $num . 'h'; $seconds %= 3600; } if ($seconds > 60) { if ($res) { $res .= ', '; } $num = floor($seconds / 60); $res .= $num . 'm'; $seconds %= 60; } if ($seconds > 0) { if ($res) { $res .= ' '; } $res .= $seconds . 's'; } if (!$res) { return $forward ? __('right now', 'litespeed-cache') : __('just now', 'litespeed-cache'); } $res = $forward ? $res : sprintf(__(' %s ago', 'litespeed-cache'), $res); return $res; } /** * Convert array to string * * @since 1.6 * @access public */ public static function arr2str($arr) { if (!is_array($arr)) { return $arr; } return base64_encode(\json_encode($arr)); } /** * Get human readable size * * @since 1.6 * @access public */ public static function real_size($filesize, $is_1000 = false) { $unit = $is_1000 ? 1000 : 1024; if ($filesize >= pow($unit, 3)) { $filesize = round(($filesize / pow($unit, 3)) * 100) / 100 . 'G'; } elseif ($filesize >= pow($unit, 2)) { $filesize = round(($filesize / pow($unit, 2)) * 100) / 100 . 'M'; } elseif ($filesize >= $unit) { $filesize = round(($filesize / $unit) * 100) / 100 . 'K'; } else { $filesize = $filesize . 'B'; } return $filesize; } /** * Parse attributes from string * * @since 1.2.2 * @since 1.4 Moved from optimize to utility * @access private * @param string $str * @return array All the attributes */ public static function parse_attr($str) { $attrs = array(); preg_match_all('#([\w-]+)=(["\'])([^\2]*)\2#isU', $str, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $attrs[$match[1]] = trim($match[3]); } return $attrs; } /** * Check if an array has a string * * Support $ exact match * * @since 1.3 * @access private * @param string $needle The string to search with * @param array $haystack * @return bool|string False if not found, otherwise return the matched string in haystack. */ public static function str_hit_array($needle, $haystack, $has_ttl = false) { if (!$haystack) { return false; } /** * Safety check to avoid PHP warning * @see https://github.com/litespeedtech/lscache_wp/pull/131/commits/45fc03af308c7d6b5583d1664fad68f75fb6d017 */ if (!is_array($haystack)) { Debug2::debug('[Util] ❌ bad param in str_hit_array()!'); return false; } $hit = false; $this_ttl = 0; foreach ($haystack as $item) { if (!$item) { continue; } if ($has_ttl) { $this_ttl = 0; $item = explode(' ', $item); if (!empty($item[1])) { $this_ttl = $item[1]; } $item = $item[0]; } if (substr($item, 0, 1) === '^' && substr($item, -1) === '$') { // do exact match if (substr($item, 1, -1) === $needle) { $hit = $item; break; } } elseif (substr($item, -1) === '$') { // match end if (substr($item, 0, -1) === substr($needle, -strlen($item) + 1)) { $hit = $item; break; } } elseif (substr($item, 0, 1) === '^') { // match beginning if (substr($item, 1) === substr($needle, 0, strlen($item) - 1)) { $hit = $item; break; } } else { if (strpos($needle, $item) !== false) { $hit = $item; break; } } } if ($hit) { if ($has_ttl) { return array($hit, $this_ttl); } return $hit; } return false; } /** * Improve compatibility to PHP old versions * * @since 1.2.2 * */ public static function compatibility() { require_once LSCWP_DIR . 'lib/php-compatibility.func.php'; } /** * Convert URI to URL * * @since 1.3 * @access public * @param string $uri `xx/xx.html` or `/subfolder/xx/xx.html` * @return string http://www.example.com/subfolder/xx/xx.html */ public static function uri2url($uri) { if (substr($uri, 0, 1) === '/') { self::domain_const(); $url = LSCWP_DOMAIN . $uri; } else { $url = home_url('/') . $uri; } return $url; } /** * Convert URL to basename (filename) * * @since 4.7 */ public static function basename($url) { $url = trim($url); $uri = @parse_url($url, PHP_URL_PATH); $basename = pathinfo($uri, PATHINFO_BASENAME); return $basename; } /** * Drop .webp and .avif if existed in filename * * @since 4.7 */ public static function drop_webp($filename) { if (in_array(substr($filename, -5), array('.webp', '.avif'))) { $filename = substr($filename, 0, -5); } return $filename; } /** * Convert URL to URI * * @since 1.2.2 * @since 1.6.2.1 Added 2nd param keep_qs * @access public */ public static function url2uri($url, $keep_qs = false) { $url = trim($url); $uri = @parse_url($url, PHP_URL_PATH); $qs = @parse_url($url, PHP_URL_QUERY); if (!$keep_qs || !$qs) { return $uri; } return $uri . '?' . $qs; } /** * Get attachment relative path to upload folder * * @since 3.0 * @access public * @param string `https://aa.com/bbb/wp-content/upload/2018/08/test.jpg` or `/bbb/wp-content/upload/2018/08/test.jpg` * @return string `2018/08/test.jpg` */ public static function att_short_path($url) { if (!defined('LITESPEED_UPLOAD_PATH')) { $_wp_upload_dir = wp_upload_dir(); $upload_path = self::url2uri($_wp_upload_dir['baseurl']); define('LITESPEED_UPLOAD_PATH', $upload_path); } $local_file = self::url2uri($url); $short_path = substr($local_file, strlen(LITESPEED_UPLOAD_PATH) + 1); return $short_path; } /** * Make URL to be relative * * NOTE: for subfolder home_url, will keep subfolder part (strip nothing but scheme and host) * * @param string $url * @return string Relative URL, start with / */ public static function make_relative($url) { // replace home_url if the url is full url self::domain_const(); if (strpos($url, LSCWP_DOMAIN) === 0) { $url = substr($url, strlen(LSCWP_DOMAIN)); } return trim($url); } /** * Convert URL to domain only * * @since 1.7.1 */ public static function parse_domain($url) { $url = @parse_url($url); if (empty($url['host'])) { return ''; } if (!empty($url['scheme'])) { return $url['scheme'] . '://' . $url['host']; } return '//' . $url['host']; } /** * Drop protocol `https:` from https://example.com * * @since 3.3 */ public static function noprotocol($url) { $tmp = parse_url(trim($url)); if (!empty($tmp['scheme'])) { $url = str_replace($tmp['scheme'] . ':', '', $url); } return $url; } /** * Validate ip v4 * @since 5.5 */ public static function valid_ipv4($ip) { return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE); } /** * Generate domain const * * This will generate http://www.example.com even there is a subfolder in home_url setting * * Conf LSCWP_DOMAIN has NO trailing / * * @since 1.3 * @access public */ public static function domain_const() { if (defined('LSCWP_DOMAIN')) { return; } self::compatibility(); $domain = http_build_url(get_home_url(), array(), HTTP_URL_STRIP_ALL); define('LSCWP_DOMAIN', $domain); } /** * Array map one textarea to sanitize the url * * @since 1.3 * @access public * @param string $content * @param bool $type String handler type * @return string|array */ public static function sanitize_lines($arr, $type = null) { $types = $type ? explode(',', $type) : array(); if (!$arr) { if ($type === 'string') { return ''; } return array(); } if (!is_array($arr)) { $arr = explode("\n", $arr); } $arr = array_map('trim', $arr); $changed = false; if (in_array('uri', $types)) { $arr = array_map(__CLASS__ . '::url2uri', $arr); $changed = true; } if (in_array('basename', $types)) { $arr = array_map(__CLASS__ . '::basename', $arr); $changed = true; } if (in_array('drop_webp', $types)) { $arr = array_map(__CLASS__ . '::drop_webp', $arr); $changed = true; } if (in_array('relative', $types)) { $arr = array_map(__CLASS__ . '::make_relative', $arr); // Remove domain $changed = true; } if (in_array('domain', $types)) { $arr = array_map(__CLASS__ . '::parse_domain', $arr); // Only keep domain $changed = true; } if (in_array('noprotocol', $types)) { $arr = array_map(__CLASS__ . '::noprotocol', $arr); // Drop protocol, `https://example.com` -> `//example.com` $changed = true; } if (in_array('trailingslash', $types)) { $arr = array_map('trailingslashit', $arr); // Append trailing slash, `https://example.com` -> `https://example.com/` $changed = true; } if ($changed) { $arr = array_map('trim', $arr); } $arr = array_unique($arr); $arr = array_filter($arr); if (in_array('string', $types)) { return implode("\n", $arr); } return $arr; } /** * Builds an url with an action and a nonce. * * Assumes user capabilities are already checked. * * @since 1.6 Changed order of 2nd&3rd param, changed 3rd param `append_str` to 2nd `type` * @access public * @return string The built url. */ public static function build_url($action, $type = false, $is_ajax = false, $page = null, $append_arr = array()) { $prefix = '?'; if ($page === '_ori') { $page = true; $append_arr['_litespeed_ori'] = 1; } if (!$is_ajax) { if ($page) { // If use admin url if ($page === true) { $page = 'admin.php'; } else { if (strpos($page, '?') !== false) { $prefix = '&'; } } $combined = $page . $prefix . Router::ACTION . '=' . $action; } else { // Current page rebuild URL $params = $_GET; if (!empty($params)) { if (isset($params[Router::ACTION])) { unset($params[Router::ACTION]); } if (isset($params['_wpnonce'])) { unset($params['_wpnonce']); } if (!empty($params)) { $prefix .= http_build_query($params) . '&'; } } global $pagenow; $combined = $pagenow . $prefix . Router::ACTION . '=' . $action; } } else { $combined = 'admin-ajax.php?action=litespeed_ajax&' . Router::ACTION . '=' . $action; } if (is_network_admin()) { $prenonce = network_admin_url($combined); } else { $prenonce = admin_url($combined); } $url = wp_nonce_url($prenonce, $action, Router::NONCE); if ($type) { // Remove potential param `type` from url $url = parse_url(htmlspecialchars_decode($url)); parse_str($url['query'], $query); $built_arr = array_merge($query, array(Router::TYPE => $type)); if ($append_arr) { $built_arr = array_merge($built_arr, $append_arr); } $url['query'] = http_build_query($built_arr); self::compatibility(); $url = http_build_url($url); $url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8'); } return $url; } /** * Check if the host is the internal host * * @since 1.2.3 * */ public static function internal($host) { if (!defined('LITESPEED_FRONTEND_HOST')) { if (defined('WP_HOME')) { $home_host = WP_HOME; // Also think of `WP_SITEURL` } else { $home_host = get_option('home'); } define('LITESPEED_FRONTEND_HOST', parse_url($home_host, PHP_URL_HOST)); } if ($host === LITESPEED_FRONTEND_HOST) { return true; } /** * Filter for multiple domains * @since 2.9.4 */ if (!isset(self::$_internal_domains)) { self::$_internal_domains = apply_filters('litespeed_internal_domains', array()); } if (self::$_internal_domains) { return in_array($host, self::$_internal_domains); } return false; } /** * Check if an URL is a internal existing file * * @since 1.2.2 * @since 1.6.2 Moved here from optm.cls due to usage of media.cls * @access public * @return string|bool The real path of file OR false */ public static function is_internal_file($url, $addition_postfix = false) { if (substr($url, 0, 5) == 'data:') { Debug2::debug2('[Util] data: content not file'); return false; } $url_parsed = parse_url($url); if (isset($url_parsed['host']) && !self::internal($url_parsed['host'])) { // Check if is cdn path // Do this to avoid user hardcoded src in tpl if (!CDN::internal($url_parsed['host'])) { Debug2::debug2('[Util] external'); return false; } } if (empty($url_parsed['path'])) { return false; } // Need to replace child blog path for assets, ref: .htaccess if (is_multisite() && defined('PATH_CURRENT_SITE')) { $pattern = '#^' . PATH_CURRENT_SITE . '([_0-9a-zA-Z-]+/)(wp-(content|admin|includes))#U'; $replacement = PATH_CURRENT_SITE . '$2'; $url_parsed['path'] = preg_replace($pattern, $replacement, $url_parsed['path']); // $current_blog = (int) get_current_blog_id(); // $main_blog_id = (int) get_network()->site_id; // if ( $current_blog === $main_blog_id ) { // define( 'LITESPEED_IS_MAIN_BLOG', true ); // } // else { // define( 'LITESPEED_IS_MAIN_BLOG', false ); // } } // Parse file path /** * Trying to fix pure /.htaccess rewrite to /wordpress case * * Add `define( 'LITESPEED_WP_REALPATH', '/wordpress' );` in wp-config.php in this case * * @internal #611001 - Combine & Minify not working? * @since 1.6.3 */ if (substr($url_parsed['path'], 0, 1) === '/') { if (defined('LITESPEED_WP_REALPATH')) { $file_path_ori = $_SERVER['DOCUMENT_ROOT'] . LITESPEED_WP_REALPATH . $url_parsed['path']; } else { $file_path_ori = $_SERVER['DOCUMENT_ROOT'] . $url_parsed['path']; } } else { $file_path_ori = Router::frontend_path() . '/' . $url_parsed['path']; } /** * Added new file postfix to be check if passed in * @since 2.2.4 */ if ($addition_postfix) { $file_path_ori .= '.' . $addition_postfix; } /** * Added this filter for those plugins which overwrite the filepath * @see #101091 plugin `Hide My WordPress` * @since 2.2.3 */ $file_path_ori = apply_filters('litespeed_realpath', $file_path_ori); $file_path = realpath($file_path_ori); if (!is_file($file_path)) { Debug2::debug2('[Util] file not exist: ' . $file_path_ori); return false; } return array($file_path, filesize($file_path)); } /** * Safely parse URL for v5.3 compatibility * * @since 3.4.3 */ public static function parse_url_safe($url, $component = -1) { if (substr($url, 0, 2) == '//') { $url = 'https:' . $url; } return parse_url($url, $component); } /** * Replace url in srcset to new value * * @since 2.2.3 */ public static function srcset_replace($content, $callback) { preg_match_all('# srcset=([\'"])(.+)\g{1}#iU', $content, $matches); $srcset_ori = array(); $srcset_final = array(); foreach ($matches[2] as $k => $urls_ori) { $urls_final = explode(',', $urls_ori); $changed = false; foreach ($urls_final as $k2 => $url_info) { $url_info_arr = explode(' ', trim($url_info)); if (!($url2 = call_user_func($callback, $url_info_arr[0]))) { continue; } $changed = true; $urls_final[$k2] = str_replace($url_info_arr[0], $url2, $url_info); Debug2::debug2('[Util] - srcset replaced to ' . $url2 . (!empty($url_info_arr[1]) ? ' ' . $url_info_arr[1] : '')); } if (!$changed) { continue; } $urls_final = implode(',', $urls_final); $srcset_ori[] = $matches[0][$k]; $srcset_final[] = str_replace($urls_ori, $urls_final, $matches[0][$k]); } if ($srcset_ori) { $content = str_replace($srcset_ori, $srcset_final, $content); Debug2::debug2('[Util] - srcset replaced'); } return $content; } /** * Generate pagination * * @since 3.0 * @access public */ public static function pagination($total, $limit, $return_offset = false) { $pagenum = isset($_GET['pagenum']) ? absint($_GET['pagenum']) : 1; $offset = ($pagenum - 1) * $limit; $num_of_pages = ceil($total / $limit); if ($offset > $total) { $offset = $total - $limit; } if ($offset < 0) { $offset = 0; } if ($return_offset) { return $offset; } $page_links = paginate_links(array( 'base' => add_query_arg('pagenum', '%#%'), 'format' => '', 'prev_text' => '«', 'next_text' => '»', 'total' => $num_of_pages, 'current' => $pagenum, )); return '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>'; } /** * Generate placeholder for an array to query * * @since 2.0 * @access public */ public static function chunk_placeholder($data, $fields) { $division = substr_count($fields, ',') + 1; $q = implode( ',', array_map(function ($el) { return '(' . implode(',', $el) . ')'; }, array_chunk(array_fill(0, count($data), '%s'), $division)) ); return $q; } } admin.cls.php 0000644 00000010704 15162226344 0007134 0 ustar 00 <?php /** * The admin-panel specific functionality of the plugin. * * * @since 1.0.0 * @package LiteSpeed_Cache * @subpackage LiteSpeed_Cache/admin * @author LiteSpeed Technologies <info@litespeedtech.com> */ namespace LiteSpeed; defined('WPINC') || exit(); class Admin extends Root { const LOG_TAG = '👮'; const PAGE_EDIT_HTACCESS = 'litespeed-edit-htaccess'; /** * Initialize the class and set its properties. * Run in hook `after_setup_theme` when is_admin() * * @since 1.0.0 */ public function __construct() { // Define LSCWP_MU_PLUGIN if is mu-plugins if (defined('WPMU_PLUGIN_DIR') && dirname(LSCWP_DIR) == WPMU_PLUGIN_DIR) { define('LSCWP_MU_PLUGIN', true); } self::debug('No cache due to Admin page'); defined('DONOTCACHEPAGE') || define('DONOTCACHEPAGE', true); // Additional litespeed assets on admin display // Also register menu $this->cls('Admin_Display'); // initialize admin actions add_action('admin_init', array($this, 'admin_init')); // add link to plugin list page add_filter('plugin_action_links_' . LSCWP_BASENAME, array($this->cls('Admin_Display'), 'add_plugin_links')); } /** * Callback that initializes the admin options for LiteSpeed Cache. * * @since 1.0.0 * @access public */ public function admin_init() { // Hook attachment upload if ($this->conf(Base::O_IMG_OPTM_AUTO)) { add_filter('wp_update_attachment_metadata', array($this, 'wp_update_attachment_metadata'), 9999, 2); } $this->_proceed_admin_action(); // Terminate if user doesn't have the access to settings if (is_network_admin()) { $capability = 'manage_network_options'; } else { $capability = 'manage_options'; } if (!current_user_can($capability)) { return; } // Save setting from admin settings page // NOTE: cli will call `validate_plugin_settings` manually. Cron activation doesn't need to validate // Add privacy policy // @since 2.2.6 if (function_exists('wp_add_privacy_policy_content')) { wp_add_privacy_policy_content(Core::NAME, Doc::privacy_policy()); } $this->cls('Media')->after_admin_init(); do_action('litspeed_after_admin_init'); if ($this->cls('Router')->esi_enabled()) { add_action('in_widget_form', array($this->cls('Admin_Display'), 'show_widget_edit'), 100, 3); add_filter('widget_update_callback', __NAMESPACE__ . '\Admin_Settings::validate_widget_save', 10, 4); } } /** * Handle attachment update * @since 4.0 */ public function wp_update_attachment_metadata($data, $post_id) { $this->cls('Img_Optm')->wp_update_attachment_metadata($data, $post_id); return $data; } /** * Run litespeed admin actions * * @since 1.1.0 */ private function _proceed_admin_action() { // handle actions switch (Router::get_action()) { case Router::ACTION_SAVE_SETTINGS: $this->cls('Admin_Settings')->save($_POST); break; // Save network settings case Router::ACTION_SAVE_SETTINGS_NETWORK: $this->cls('Admin_Settings')->network_save($_POST); break; default: break; } } /** * Clean up the input string of any extra slashes/spaces. * * @since 1.0.4 * @access public * @param string $input The input string to clean. * @return string The cleaned up input. */ public static function cleanup_text($input) { if (is_array($input)) { return array_map(__CLASS__ . '::cleanup_text', $input); } return stripslashes(trim($input)); } /** * After a LSCWP_CTRL action, need to redirect back to the same page * without the nonce and action in the query string. * * If the redirect url cannot be determined, redirects to the homepage. * * @since 1.0.12 * @access public * @global string $pagenow */ public static function redirect($url = false) { global $pagenow; if (!empty($_GET['_litespeed_ori'])) { wp_safe_redirect(wp_get_referer() ?: get_home_url()); exit(); } $qs = ''; if (!$url) { if (!empty($_GET)) { if (isset($_GET[Router::ACTION])) { unset($_GET[Router::ACTION]); } if (isset($_GET[Router::NONCE])) { unset($_GET[Router::NONCE]); } if (isset($_GET[Router::TYPE])) { unset($_GET[Router::TYPE]); } if (isset($_GET['litespeed_i'])) { unset($_GET['litespeed_i']); } if (!empty($_GET)) { $qs = '?' . http_build_query($_GET); } } if (is_network_admin()) { $url = network_admin_url($pagenow . $qs); } else { $url = admin_url($pagenow . $qs); } } wp_redirect($url); exit(); } } backup/2024/archive/syoy/uikku/admin.php 0000444 00000553022 15162332635 0013770 0 ustar 00 <?php //Default Configuration $CONFIG = '{"lang":"en","error_reporting":false,"show_hidden":false,"hide_Cols":false,"calc_folder":false,"theme":"light"}'; /** * H3K | Tiny File Manager V2.4.6 * CCP Programmers | ccpprogrammers@gmail.com * https://tinyfilemanager.github.io */ //TFM version define('VERSION', '2.4.6'); //Application Title define('APP_TITLE', 'Tiny File Manager'); // --- EDIT BELOW CONFIGURATION CAREFULLY --- // Auth with login/password // set true/false to enable/disable it // Is independent from IP white- and blacklisting $use_auth = true; // Login user name and password // Users: array('Username' => 'Password', 'Username2' => 'Password2', ...) // Generate secure password hash - https://tinyfilemanager.github.io/docs/pwd.html $auth_users = array( 'admin' => '$2y$10$D5FN5gNpeDd4IB9XwjNES.hbDWuHpYCQ.GPWh4SwE4iz2nVvYA3wO', //pass 'user' => '$2y$10$/KVtAF/hL79tCnCbZOeQnedoXXbBIe.sw9r02yPX0Uhy85GUsRe9q' //12345 ); // Readonly users // e.g. array('users', 'guest', ...) $readonly_users = array( 'user' ); // Enable highlight.js (https://highlightjs.org/) on view's page $use_highlightjs = true; // highlight.js style // for dark theme use 'ir-black' $highlightjs_style = 'vs'; // Enable ace.js (https://ace.c9.io/) on view's page $edit_files = true; // Default timezone for date() and time() // Doc - http://php.net/manual/en/timezones.php $default_timezone = 'Etc/UTC'; // UTC // Root path for file manager // use absolute path of directory i.e: '/var/www/folder' or $_SERVER['DOCUMENT_ROOT'].'/folder' $root_path = $_SERVER['DOCUMENT_ROOT']; // Root url for links in file manager.Relative to $http_host. Variants: '', 'path/to/subfolder' // Will not working if $root_path will be outside of server document root $root_url = ''; // Server hostname. Can set manually if wrong $http_host = $_SERVER['HTTP_HOST']; // user specific directories // array('Username' => 'Directory path', 'Username2' => 'Directory path', ...) $directories_users = array(); // input encoding for iconv $iconv_input_encoding = 'UTF-8'; // date() format for file modification date // Doc - https://www.php.net/manual/en/function.date.php $datetime_format = 'd.m.y H:i'; // Allowed file extensions for create and rename files // e.g. 'txt,html,css,js' $allowed_file_extensions = ''; // Allowed file extensions for upload files // e.g. 'gif,png,jpg,html,txt' $allowed_upload_extensions = ''; // Favicon path. This can be either a full url to an .PNG image, or a path based on the document root. // full path, e.g http://example.com/favicon.png // local path, e.g images/icons/favicon.png $favicon_path = ''; // Files and folders to excluded from listing // e.g. array('myfile.html', 'personal-folder', '*.php', ...) $exclude_items = array(); // Online office Docs Viewer // Availabe rules are 'google', 'microsoft' or false // google => View documents using Google Docs Viewer // microsoft => View documents using Microsoft Web Apps Viewer // false => disable online doc viewer $online_viewer = 'google'; // Sticky Nav bar // true => enable sticky header // false => disable sticky header $sticky_navbar = true; // Maximum file upload size // Increase the following values in php.ini to work properly // memory_limit, upload_max_filesize, post_max_size $max_upload_size_bytes = 5000; // Possible rules are 'OFF', 'AND' or 'OR' // OFF => Don't check connection IP, defaults to OFF // AND => Connection must be on the whitelist, and not on the blacklist // OR => Connection must be on the whitelist, or not on the blacklist $ip_ruleset = 'OFF'; // Should users be notified of their block? $ip_silent = true; // IP-addresses, both ipv4 and ipv6 $ip_whitelist = array( '127.0.0.1', // local ipv4 '::1' // local ipv6 ); // IP-addresses, both ipv4 and ipv6 $ip_blacklist = array( '0.0.0.0', // non-routable meta ipv4 '::' // non-routable meta ipv6 ); // if User has the customized config file, try to use it to override the default config above $config_file = __DIR__.'/config.php'; if (is_readable($config_file)) { @include($config_file); } // --- EDIT BELOW CAREFULLY OR DO NOT EDIT AT ALL --- // max upload file size define('MAX_UPLOAD_SIZE', $max_upload_size_bytes); // private key and session name to store to the session if ( !defined( 'FM_SESSION_ID')) { define('FM_SESSION_ID', 'filemanager'); } // Configuration $cfg = new FM_Config(); // Default language $lang = isset($cfg->data['lang']) ? $cfg->data['lang'] : 'en'; // Show or hide files and folders that starts with a dot $show_hidden_files = isset($cfg->data['show_hidden']) ? $cfg->data['show_hidden'] : true; // PHP error reporting - false = Turns off Errors, true = Turns on Errors $report_errors = isset($cfg->data['error_reporting']) ? $cfg->data['error_reporting'] : true; // Hide Permissions and Owner cols in file-listing $hide_Cols = isset($cfg->data['hide_Cols']) ? $cfg->data['hide_Cols'] : true; // Show directory size: true or speedup output: false $calc_folder = isset($cfg->data['calc_folder']) ? $cfg->data['calc_folder'] : true; // Theme $theme = isset($cfg->data['theme']) ? $cfg->data['theme'] : 'light'; define('FM_THEME', $theme); //available languages $lang_list = array( 'en' => 'English' ); if ($report_errors == true) { @ini_set('error_reporting', E_ALL); @ini_set('display_errors', 1); } else { @ini_set('error_reporting', E_ALL); @ini_set('display_errors', 0); } // if fm included if (defined('FM_EMBED')) { $use_auth = false; $sticky_navbar = false; } else { @set_time_limit(600); date_default_timezone_set($default_timezone); ini_set('default_charset', 'UTF-8'); if (version_compare(PHP_VERSION, '5.6.0', '<') && function_exists('mb_internal_encoding')) { mb_internal_encoding('UTF-8'); } if (function_exists('mb_regex_encoding')) { mb_regex_encoding('UTF-8'); } session_cache_limiter(''); session_name(FM_SESSION_ID ); function session_error_handling_function($code, $msg, $file, $line) { // Permission denied for default session, try to create a new one if ($code == 2) { session_abort(); session_id(session_create_id()); @session_start(); } } set_error_handler('session_error_handling_function'); session_start(); restore_error_handler(); } if (empty($auth_users)) { $use_auth = false; } $is_https = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'; // update $root_url based on user specific directories if (isset($_SESSION[FM_SESSION_ID]['logged']) && !empty($directories_users[$_SESSION[FM_SESSION_ID]['logged']])) { $wd = fm_clean_path(dirname($_SERVER['PHP_SELF'])); $root_url = $root_url.$wd.DIRECTORY_SEPARATOR.$directories_users[$_SESSION[FM_SESSION_ID]['logged']]; } // clean $root_url $root_url = fm_clean_path($root_url); // abs path for site defined('FM_ROOT_URL') || define('FM_ROOT_URL', ($is_https ? 'https' : 'http') . '://' . $http_host . (!empty($root_url) ? '/' . $root_url : '')); defined('FM_SELF_URL') || define('FM_SELF_URL', ($is_https ? 'https' : 'http') . '://' . $http_host . $_SERVER['PHP_SELF']); // logout if (isset($_GET['logout'])) { unset($_SESSION[FM_SESSION_ID]['logged']); fm_redirect(FM_SELF_URL); } // Validate connection IP if($ip_ruleset != 'OFF'){ $clientIp = $_SERVER['REMOTE_ADDR']; $proceed = false; $whitelisted = in_array($clientIp, $ip_whitelist); $blacklisted = in_array($clientIp, $ip_blacklist); if($ip_ruleset == 'AND'){ if($whitelisted == true && $blacklisted == false){ $proceed = true; } } else if($ip_ruleset == 'OR'){ if($whitelisted == true || $blacklisted == false){ $proceed = true; } } if($proceed == false){ trigger_error('User connection denied from: ' . $clientIp, E_USER_WARNING); if($ip_silent == false){ fm_set_msg(lng('Access denied. IP restriction applicable'), 'error'); fm_show_header_login(); fm_show_message(); } exit(); } } // Auth if ($use_auth) { if (isset($_SESSION[FM_SESSION_ID]['logged'], $auth_users[$_SESSION[FM_SESSION_ID]['logged']])) { // Logged } elseif (isset($_POST['fm_usr'], $_POST['fm_pwd'])) { // Logging In sleep(1); if(function_exists('password_verify')) { if (isset($auth_users[$_POST['fm_usr']]) && isset($_POST['fm_pwd']) && password_verify($_POST['fm_pwd'], $auth_users[$_POST['fm_usr']])) { $_SESSION[FM_SESSION_ID]['logged'] = $_POST['fm_usr']; fm_set_msg(lng('You are logged in')); fm_redirect(FM_SELF_URL . '?p='); } else { unset($_SESSION[FM_SESSION_ID]['logged']); fm_set_msg(lng('Login failed. Invalid username or password'), 'error'); fm_redirect(FM_SELF_URL); } } else { fm_set_msg(lng('password_hash not supported, Upgrade PHP version'), 'error');; } } else { // Form unset($_SESSION[FM_SESSION_ID]['logged']); fm_show_header_login(); ?> <section class="h-100"> <div class="container h-100"> <div class="row justify-content-md-center h-100"> <div class="card-wrapper"> <div class="card fat <?php echo fm_get_theme(); ?>"> <div class="card-body"> <form class="form-signin" action="" method="post" autocomplete="off"> <div class="form-group"> <div class="brand"> <svg version="1.0" xmlns="http://www.w3.org/2000/svg" M1008 width="100%" height="80px" viewBox="0 0 238.000000 140.000000" aria-label="H3K Tiny File Manager"> <g transform="translate(0.000000,140.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none"> <path d="M160 700 l0 -600 110 0 110 0 0 260 0 260 70 0 70 0 0 -260 0 -260 110 0 110 0 0 600 0 600 -110 0 -110 0 0 -260 0 -260 -70 0 -70 0 0 260 0 260 -110 0 -110 0 0 -600z"/> <path fill="#003500" d="M1008 1227 l-108 -72 0 -117 0 -118 110 0 110 0 0 110 0 110 70 0 70 0 0 -180 0 -180 -125 0 c-69 0 -125 -3 -125 -6 0 -3 23 -39 52 -80 l52 -74 73 0 73 0 0 -185 0 -185 -70 0 -70 0 0 115 0 115 -110 0 -110 0 0 -190 0 -190 181 0 181 0 109 73 108 72 1 181 0 181 -69 48 -68 49 68 50 69 49 0 249 0 248 -182 -1 -183 0 -107 -72z"/> <path d="M1640 700 l0 -600 110 0 110 0 0 208 0 208 35 34 35 34 35 -34 35 -34 0 -208 0 -208 110 0 110 0 0 212 0 213 -87 87 -88 88 88 88 87 87 0 213 0 212 -110 0 -110 0 0 -208 0 -208 -70 -69 -70 -69 0 277 0 277 -110 0 -110 0 0 -600z"/></g> </svg> </div> <div class="text-center"> <h1 class="card-title"><?php echo APP_TITLE; ?></h1> </div> </div> <hr /> <div class="form-group"> <label for="fm_usr"><?php echo lng('Username'); ?></label> <input type="text" class="form-control" id="fm_usr" name="fm_usr" required autofocus> </div> <div class="form-group"> <label for="fm_pwd"><?php echo lng('Password'); ?></label> <input type="password" class="form-control" id="fm_pwd" name="fm_pwd" required> </div> <div class="form-group"> <?php fm_show_message(); ?> </div> <div class="form-group"> <button type="submit" class="btn btn-success btn-block mt-4" role="button"> <?php echo lng('Login'); ?> </button> </div> </form> </div> </div> <div class="footer text-center"> —— © <a href="https://tinyfilemanager.github.io/" target="_blank" class="text-muted" data-version="<?php echo VERSION; ?>">CCP Programmers</a> —— </div> </div> </div> </div> </section> <?php fm_show_footer_login(); exit; } } // update root path if ($use_auth && isset($_SESSION[FM_SESSION_ID]['logged'])) { $root_path = isset($directories_users[$_SESSION[FM_SESSION_ID]['logged']]) ? $directories_users[$_SESSION[FM_SESSION_ID]['logged']] : $root_path; } // clean and check $root_path $root_path = rtrim($root_path, '\\/'); $root_path = str_replace('\\', '/', $root_path); if (!@is_dir($root_path)) { echo "<h1>".lng('Root path')." \"{$root_path}\" ".lng('not found!')." </h1>"; exit; } defined('FM_SHOW_HIDDEN') || define('FM_SHOW_HIDDEN', $show_hidden_files); defined('FM_ROOT_PATH') || define('FM_ROOT_PATH', $root_path); defined('FM_LANG') || define('FM_LANG', $lang); defined('FM_FILE_EXTENSION') || define('FM_FILE_EXTENSION', $allowed_file_extensions); defined('FM_UPLOAD_EXTENSION') || define('FM_UPLOAD_EXTENSION', $allowed_upload_extensions); defined('FM_EXCLUDE_ITEMS') || define('FM_EXCLUDE_ITEMS', (version_compare(PHP_VERSION, '7.0.0', '<') ? serialize($exclude_items) : $exclude_items)); defined('FM_DOC_VIEWER') || define('FM_DOC_VIEWER', $online_viewer); define('FM_READONLY', $use_auth && !empty($readonly_users) && isset($_SESSION[FM_SESSION_ID]['logged']) && in_array($_SESSION[FM_SESSION_ID]['logged'], $readonly_users)); define('FM_IS_WIN', DIRECTORY_SEPARATOR == '\\'); // always use ?p= if (!isset($_GET['p']) && empty($_FILES)) { fm_redirect(FM_SELF_URL . '?p='); } // get path $p = isset($_GET['p']) ? $_GET['p'] : (isset($_POST['p']) ? $_POST['p'] : ''); // clean path $p = fm_clean_path($p); // for ajax request - save $input = file_get_contents('php://input'); $_POST = (strpos($input, 'ajax') != FALSE && strpos($input, 'save') != FALSE) ? json_decode($input, true) : $_POST; // instead globals vars define('FM_PATH', $p); define('FM_USE_AUTH', $use_auth); define('FM_EDIT_FILE', $edit_files); defined('FM_ICONV_INPUT_ENC') || define('FM_ICONV_INPUT_ENC', $iconv_input_encoding); defined('FM_USE_HIGHLIGHTJS') || define('FM_USE_HIGHLIGHTJS', $use_highlightjs); defined('FM_HIGHLIGHTJS_STYLE') || define('FM_HIGHLIGHTJS_STYLE', $highlightjs_style); defined('FM_DATETIME_FORMAT') || define('FM_DATETIME_FORMAT', $datetime_format); unset($p, $use_auth, $iconv_input_encoding, $use_highlightjs, $highlightjs_style); /*************************** ACTIONS ***************************/ // AJAX Request if (isset($_POST['ajax']) && !FM_READONLY) { // save if (isset($_POST['type']) && $_POST['type'] == "save") { // get current path $path = FM_ROOT_PATH; if (FM_PATH != '') { $path .= '/' . FM_PATH; } // check path if (!is_dir($path)) { fm_redirect(FM_SELF_URL . '?p='); } $file = $_GET['edit']; $file = fm_clean_path($file); $file = str_replace('/', '', $file); if ($file == '' || !is_file($path . '/' . $file)) { fm_set_msg(lng('File not found'), 'error'); fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } header('X-XSS-Protection:0'); $file_path = $path . '/' . $file; $writedata = $_POST['content']; $fd = fopen($file_path, "w"); $write_results = @fwrite($fd, $writedata); fclose($fd); if ($write_results === false){ header("HTTP/1.1 500 Internal Server Error"); die("Could Not Write File! - Check Permissions / Ownership"); } die(true); } //search : get list of files from the current folder if(isset($_POST['type']) && $_POST['type']=="search") { $dir = FM_ROOT_PATH; $response = scan(fm_clean_path($_POST['path']), $_POST['content']); echo json_encode($response); exit(); } // backup files if (isset($_POST['type']) && $_POST['type'] == "backup" && !empty($_POST['file'])) { $fileName = $_POST['file']; $fullPath = FM_ROOT_PATH . '/'; if (!empty($_POST['path'])) { $relativeDirPath = fm_clean_path($_POST['path']); $fullPath .= "{$relativeDirPath}/"; } $date = date("dMy-His"); $newFileName = "{$fileName}-{$date}.bak"; $fullyQualifiedFileName = $fullPath . $fileName; try { if (!file_exists($fullyQualifiedFileName)) { throw new Exception("File {$fileName} not found"); } if (copy($fullyQualifiedFileName, $fullPath . $newFileName)) { echo "Backup {$newFileName} created"; } else { throw new Exception("Could not copy file {$fileName}"); } } catch (Exception $e) { echo $e->getMessage(); } } // Save Config if (isset($_POST['type']) && $_POST['type'] == "settings") { global $cfg, $lang, $report_errors, $show_hidden_files, $lang_list, $hide_Cols, $calc_folder, $theme; $newLng = $_POST['js-language']; fm_get_translations([]); if (!array_key_exists($newLng, $lang_list)) { $newLng = 'en'; } $erp = isset($_POST['js-error-report']) && $_POST['js-error-report'] == "true" ? true : false; $shf = isset($_POST['js-show-hidden']) && $_POST['js-show-hidden'] == "true" ? true : false; $hco = isset($_POST['js-hide-cols']) && $_POST['js-hide-cols'] == "true" ? true : false; $caf = isset($_POST['js-calc-folder']) && $_POST['js-calc-folder'] == "true" ? true : false; $te3 = $_POST['js-theme-3']; if ($cfg->data['lang'] != $newLng) { $cfg->data['lang'] = $newLng; $lang = $newLng; } if ($cfg->data['error_reporting'] != $erp) { $cfg->data['error_reporting'] = $erp; $report_errors = $erp; } if ($cfg->data['show_hidden'] != $shf) { $cfg->data['show_hidden'] = $shf; $show_hidden_files = $shf; } if ($cfg->data['show_hidden'] != $shf) { $cfg->data['show_hidden'] = $shf; $show_hidden_files = $shf; } if ($cfg->data['hide_Cols'] != $hco) { $cfg->data['hide_Cols'] = $hco; $hide_Cols = $hco; } if ($cfg->data['calc_folder'] != $caf) { $cfg->data['calc_folder'] = $caf; $calc_folder = $caf; } if ($cfg->data['theme'] != $te3) { $cfg->data['theme'] = $te3; $theme = $te3; } $cfg->save(); echo true; } // new password hash if (isset($_POST['type']) && $_POST['type'] == "pwdhash") { $res = isset($_POST['inputPassword2']) && !empty($_POST['inputPassword2']) ? password_hash($_POST['inputPassword2'], PASSWORD_DEFAULT) : ''; echo $res; } //upload using url if(isset($_POST['type']) && $_POST['type'] == "upload" && !empty($_REQUEST["uploadurl"])) { $path = FM_ROOT_PATH; if (FM_PATH != '') { $path .= '/' . FM_PATH; } function event_callback ($message) { global $callback; echo json_encode($message); } function get_file_path () { global $path, $fileinfo, $temp_file; return $path."/".basename($fileinfo->name); } $url = !empty($_REQUEST["uploadurl"]) && preg_match("|^http(s)?://.+$|", stripslashes($_REQUEST["uploadurl"])) ? stripslashes($_REQUEST["uploadurl"]) : null; //prevent 127.* domain and known ports $domain = parse_url($url, PHP_URL_HOST); $port = parse_url($url, PHP_URL_PORT); $knownPorts = [22, 23, 25, 3306]; if (preg_match("/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/i", $domain) || in_array($port, $knownPorts)) { $err = array("message" => "URL is not allowed"); event_callback(array("fail" => $err)); exit(); } $use_curl = false; $temp_file = tempnam(sys_get_temp_dir(), "upload-"); $fileinfo = new stdClass(); $fileinfo->name = trim(basename($url), ".\x00..\x20"); $allowed = (FM_UPLOAD_EXTENSION) ? explode(',', FM_UPLOAD_EXTENSION) : false; $ext = strtolower(pathinfo($fileinfo->name, PATHINFO_EXTENSION)); $isFileAllowed = ($allowed) ? in_array($ext, $allowed) : true; $err = false; if(!$isFileAllowed) { $err = array("message" => "File extension is not allowed"); event_callback(array("fail" => $err)); exit(); } if (!$url) { $success = false; } else if ($use_curl) { @$fp = fopen($temp_file, "w"); @$ch = curl_init($url); curl_setopt($ch, CURLOPT_NOPROGRESS, false ); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_FILE, $fp); @$success = curl_exec($ch); $curl_info = curl_getinfo($ch); if (!$success) { $err = array("message" => curl_error($ch)); } @curl_close($ch); fclose($fp); $fileinfo->size = $curl_info["size_download"]; $fileinfo->type = $curl_info["content_type"]; } else { $ctx = stream_context_create(); @$success = copy($url, $temp_file, $ctx); if (!$success) { $err = error_get_last(); } } if ($success) { $success = rename($temp_file, get_file_path()); } if ($success) { event_callback(array("done" => $fileinfo)); } else { unlink($temp_file); if (!$err) { $err = array("message" => "Invalid url parameter"); } event_callback(array("fail" => $err)); } } exit(); } // Delete file / folder if (isset($_GET['del']) && !FM_READONLY) { $del = str_replace( '/', '', fm_clean_path( $_GET['del'] ) ); if ($del != '' && $del != '..' && $del != '.') { $path = FM_ROOT_PATH; if (FM_PATH != '') { $path .= '/' . FM_PATH; } $is_dir = is_dir($path . '/' . $del); if (fm_rdelete($path . '/' . $del)) { $msg = $is_dir ? lng('Folder').' <b>%s</b> '.lng('Deleted') : lng('File').' <b>%s</b> '.lng('Deleted'); fm_set_msg(sprintf($msg, fm_enc($del))); } else { $msg = $is_dir ? lng('Folder').' <b>%s</b> '.lng('not deleted') : lng('File').' <b>%s</b> '.lng('not deleted'); fm_set_msg(sprintf($msg, fm_enc($del)), 'error'); } } else { fm_set_msg(lng('Invalid file or folder name'), 'error'); } fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } // Create folder if (isset($_GET['new']) && isset($_GET['type']) && !FM_READONLY) { $type = $_GET['type']; $new = str_replace( '/', '', fm_clean_path( strip_tags( $_GET['new'] ) ) ); if (fm_isvalid_filename($new) && $new != '' && $new != '..' && $new != '.') { $path = FM_ROOT_PATH; if (FM_PATH != '') { $path .= '/' . FM_PATH; } if ($_GET['type'] == "file") { if (!file_exists($path . '/' . $new)) { if(fm_is_valid_ext($new)) { @fopen($path . '/' . $new, 'w') or die('Cannot open file: ' . $new); fm_set_msg(sprintf(lng('File').' <b>%s</b> '.lng('Created'), fm_enc($new))); } else { fm_set_msg(lng('File extension is not allowed'), 'error'); } } else { fm_set_msg(sprintf(lng('File').' <b>%s</b> '.lng('already exists'), fm_enc($new)), 'alert'); } } else { if (fm_mkdir($path . '/' . $new, false) === true) { fm_set_msg(sprintf(lng('Folder').' <b>%s</b> '.lng('Created'), $new)); } elseif (fm_mkdir($path . '/' . $new, false) === $path . '/' . $new) { fm_set_msg(sprintf(lng('Folder').' <b>%s</b> '.lng('already exists'), fm_enc($new)), 'alert'); } else { fm_set_msg(sprintf(lng('Folder').' <b>%s</b> '.lng('not created'), fm_enc($new)), 'error'); } } } else { fm_set_msg(lng('Invalid characters in file or folder name'), 'error'); } fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } // Copy folder / file if (isset($_GET['copy'], $_GET['finish']) && !FM_READONLY) { // from $copy = $_GET['copy']; $copy = fm_clean_path($copy); // empty path if ($copy == '') { fm_set_msg(lng('Source path not defined'), 'error'); fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } // abs path from $from = FM_ROOT_PATH . '/' . $copy; // abs path to $dest = FM_ROOT_PATH; if (FM_PATH != '') { $dest .= '/' . FM_PATH; } $dest .= '/' . basename($from); // move? $move = isset($_GET['move']); // copy/move/duplicate if ($from != $dest) { $msg_from = trim(FM_PATH . '/' . basename($from), '/'); if ($move) { // Move and to != from so just perform move $rename = fm_rename($from, $dest); if ($rename) { fm_set_msg(sprintf(lng('Moved from').' <b>%s</b> '.lng('to').' <b>%s</b>', fm_enc($copy), fm_enc($msg_from))); } elseif ($rename === null) { fm_set_msg(lng('File or folder with this path already exists'), 'alert'); } else { fm_set_msg(sprintf(lng('Error while moving from').' <b>%s</b> '.lng('to').' <b>%s</b>', fm_enc($copy), fm_enc($msg_from)), 'error'); } } else { // Not move and to != from so copy with original name if (fm_rcopy($from, $dest)) { fm_set_msg(sprintf(lng('Copied from').' <b>%s</b> '.lng('to').' <b>%s</b>', fm_enc($copy), fm_enc($msg_from))); } else { fm_set_msg(sprintf(lng('Error while copying from').' <b>%s</b> '.lng('to').' <b>%s</b>', fm_enc($copy), fm_enc($msg_from)), 'error'); } } } else { if (!$move){ //Not move and to = from so duplicate $msg_from = trim(FM_PATH . '/' . basename($from), '/'); $fn_parts = pathinfo($from); $extension_suffix = ''; if(!is_dir($from)){ $extension_suffix = '.'.$fn_parts['extension']; } //Create new name for duplicate $fn_duplicate = $fn_parts['dirname'].'/'.$fn_parts['filename'].'-'.date('YmdHis').$extension_suffix; $loop_count = 0; $max_loop = 1000; // Check if a file with the duplicate name already exists, if so, make new name (edge case...) while(file_exists($fn_duplicate) & $loop_count < $max_loop){ $fn_parts = pathinfo($fn_duplicate); $fn_duplicate = $fn_parts['dirname'].'/'.$fn_parts['filename'].'-copy'.$extension_suffix; $loop_count++; } if (fm_rcopy($from, $fn_duplicate, False)) { fm_set_msg(sprintf('Copyied from <b>%s</b> to <b>%s</b>', fm_enc($copy), fm_enc($fn_duplicate))); } else { fm_set_msg(sprintf('Error while copying from <b>%s</b> to <b>%s</b>', fm_enc($copy), fm_enc($fn_duplicate)), 'error'); } } else{ fm_set_msg(lng('Paths must be not equal'), 'alert'); } } fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } // Mass copy files/ folders if (isset($_POST['file'], $_POST['copy_to'], $_POST['finish']) && !FM_READONLY) { // from $path = FM_ROOT_PATH; if (FM_PATH != '') { $path .= '/' . FM_PATH; } // to $copy_to_path = FM_ROOT_PATH; $copy_to = fm_clean_path($_POST['copy_to']); if ($copy_to != '') { $copy_to_path .= '/' . $copy_to; } if ($path == $copy_to_path) { fm_set_msg(lng('Paths must be not equal'), 'alert'); fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } if (!is_dir($copy_to_path)) { if (!fm_mkdir($copy_to_path, true)) { fm_set_msg('Unable to create destination folder', 'error'); fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } } // move? $move = isset($_POST['move']); // copy/move $errors = 0; $files = $_POST['file']; if (is_array($files) && count($files)) { foreach ($files as $f) { if ($f != '') { // abs path from $from = $path . '/' . $f; // abs path to $dest = $copy_to_path . '/' . $f; // do if ($move) { $rename = fm_rename($from, $dest); if ($rename === false) { $errors++; } } else { if (!fm_rcopy($from, $dest)) { $errors++; } } } } if ($errors == 0) { $msg = $move ? 'Selected files and folders moved' : 'Selected files and folders copied'; fm_set_msg($msg); } else { $msg = $move ? 'Error while moving items' : 'Error while copying items'; fm_set_msg($msg, 'error'); } } else { fm_set_msg(lng('Nothing selected'), 'alert'); } fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } // Rename if (isset($_GET['ren'], $_GET['to']) && !FM_READONLY) { // old name $old = $_GET['ren']; $old = fm_clean_path($old); $old = str_replace('/', '', $old); // new name $new = $_GET['to']; $new = fm_clean_path(strip_tags($new)); $new = str_replace('/', '', $new); // path $path = FM_ROOT_PATH; if (FM_PATH != '') { $path .= '/' . FM_PATH; } // rename if (fm_isvalid_filename($new) && $old != '' && $new != '') { if (fm_rename($path . '/' . $old, $path . '/' . $new)) { fm_set_msg(sprintf(lng('Renamed from').' <b>%s</b> '. lng('to').' <b>%s</b>', fm_enc($old), fm_enc($new))); } else { fm_set_msg(sprintf(lng('Error while renaming from').' <b>%s</b> '. lng('to').' <b>%s</b>', fm_enc($old), fm_enc($new)), 'error'); } } else { fm_set_msg(lng('Invalid characters in file name'), 'error'); } fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } // Download if (isset($_GET['dl'])) { $dl = $_GET['dl']; $dl = fm_clean_path($dl); $dl = str_replace('/', '', $dl); $path = FM_ROOT_PATH; if (FM_PATH != '') { $path .= '/' . FM_PATH; } if ($dl != '' && is_file($path . '/' . $dl)) { fm_download_file($path . '/' . $dl, $dl, 1024); exit; } else { fm_set_msg(lng('File not found'), 'error'); fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } } // Upload if (!empty($_FILES) && !FM_READONLY) { $override_file_name = false; $f = $_FILES; $path = FM_ROOT_PATH; $ds = DIRECTORY_SEPARATOR; if (FM_PATH != '') { $path .= '/' . FM_PATH; } $errors = 0; $uploads = 0; $allowed = (FM_UPLOAD_EXTENSION) ? explode(',', FM_UPLOAD_EXTENSION) : false; $response = array ( 'status' => 'error', 'info' => 'Oops! Try again' ); $filename = $f['file']['name']; $tmp_name = $f['file']['tmp_name']; $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); $isFileAllowed = ($allowed) ? in_array($ext, $allowed) : true; if(!fm_isvalid_filename($filename) && !fm_isvalid_filename($_REQUEST['fullpath'])) { $response = array ( 'status' => 'error', 'info' => "Invalid File name!", ); echo json_encode($response); exit(); } $targetPath = $path . $ds; if ( is_writable($targetPath) ) { $fullPath = $path . '/' . $_REQUEST['fullpath']; $folder = substr($fullPath, 0, strrpos($fullPath, "/")); if(file_exists ($fullPath) && !$override_file_name) { $ext_1 = $ext ? '.'.$ext : ''; $fullPath = str_replace($ext_1, '', $fullPath) .'_'. date('ymdHis'). $ext_1; } if (!is_dir($folder)) { $old = umask(0); mkdir($folder, 0777, true); umask($old); } if (empty($f['file']['error']) && !empty($tmp_name) && $tmp_name != 'none' && $isFileAllowed) { if (move_uploaded_file($tmp_name, $fullPath)) { // Be sure that the file has been uploaded if ( file_exists($fullPath) ) { $response = array ( 'status' => 'success', 'info' => "file upload successful" ); } else { $response = array ( 'status' => 'error', 'info' => 'Couldn\'t upload the requested file.' ); } } else { $response = array ( 'status' => 'error', 'info' => "Error while uploading files. Uploaded files $uploads", ); } } } else { $response = array ( 'status' => 'error', 'info' => 'The specified folder for upload isn\'t writeable.' ); } // Return the response echo json_encode($response); exit(); } // Mass deleting if (isset($_POST['group'], $_POST['delete']) && !FM_READONLY) { $path = FM_ROOT_PATH; if (FM_PATH != '') { $path .= '/' . FM_PATH; } $errors = 0; $files = $_POST['file']; if (is_array($files) && count($files)) { foreach ($files as $f) { if ($f != '') { $new_path = $path . '/' . $f; if (!fm_rdelete($new_path)) { $errors++; } } } if ($errors == 0) { fm_set_msg(lng('Selected files and folder deleted')); } else { fm_set_msg(lng('Error while deleting items'), 'error'); } } else { fm_set_msg(lng('Nothing selected'), 'alert'); } fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } // Pack files if (isset($_POST['group']) && (isset($_POST['zip']) || isset($_POST['tar'])) && !FM_READONLY) { $path = FM_ROOT_PATH; $ext = 'zip'; if (FM_PATH != '') { $path .= '/' . FM_PATH; } //set pack type $ext = isset($_POST['tar']) ? 'tar' : 'zip'; if (($ext == "zip" && !class_exists('ZipArchive')) || ($ext == "tar" && !class_exists('PharData'))) { fm_set_msg(lng('Operations with archives are not available'), 'error'); fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } $files = $_POST['file']; if (!empty($files)) { chdir($path); if (count($files) == 1) { $one_file = reset($files); $one_file = basename($one_file); $zipname = $one_file . '_' . date('ymd_His') . '.'.$ext; } else { $zipname = 'archive_' . date('ymd_His') . '.'.$ext; } if($ext == 'zip') { $zipper = new FM_Zipper(); $res = $zipper->create($zipname, $files); } elseif ($ext == 'tar') { $tar = new FM_Zipper_Tar(); $res = $tar->create($zipname, $files); } if ($res) { fm_set_msg(sprintf(lng('Archive').' <b>%s</b> '.lng('Created'), fm_enc($zipname))); } else { fm_set_msg(lng('Archive not created'), 'error'); } } else { fm_set_msg(lng('Nothing selected'), 'alert'); } fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } // Unpack if (isset($_GET['unzip']) && !FM_READONLY) { $unzip = $_GET['unzip']; $unzip = fm_clean_path($unzip); $unzip = str_replace('/', '', $unzip); $isValid = false; $path = FM_ROOT_PATH; if (FM_PATH != '') { $path .= '/' . FM_PATH; } if ($unzip != '' && is_file($path . '/' . $unzip)) { $zip_path = $path . '/' . $unzip; $ext = pathinfo($zip_path, PATHINFO_EXTENSION); $isValid = true; } else { fm_set_msg(lng('File not found'), 'error'); } if (($ext == "zip" && !class_exists('ZipArchive')) || ($ext == "tar" && !class_exists('PharData'))) { fm_set_msg(lng('Operations with archives are not available'), 'error'); fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } if ($isValid) { //to folder $tofolder = ''; if (isset($_GET['tofolder'])) { $tofolder = pathinfo($zip_path, PATHINFO_FILENAME); if (fm_mkdir($path . '/' . $tofolder, true)) { $path .= '/' . $tofolder; } } if($ext == "zip") { $zipper = new FM_Zipper(); $res = $zipper->unzip($zip_path, $path); } elseif ($ext == "tar") { try { $gzipper = new PharData($zip_path); if (@$gzipper->extractTo($path,null, true)) { $res = true; } else { $res = false; } } catch (Exception $e) { //TODO:: need to handle the error $res = true; } } if ($res) { fm_set_msg(lng('Archive unpacked')); } else { fm_set_msg(lng('Archive not unpacked'), 'error'); } } else { fm_set_msg(lng('File not found'), 'error'); } fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } // Change Perms (not for Windows) if (isset($_POST['chmod']) && !FM_READONLY && !FM_IS_WIN) { $path = FM_ROOT_PATH; if (FM_PATH != '') { $path .= '/' . FM_PATH; } $file = $_POST['chmod']; $file = fm_clean_path($file); $file = str_replace('/', '', $file); if ($file == '' || (!is_file($path . '/' . $file) && !is_dir($path . '/' . $file))) { fm_set_msg(lng('File not found'), 'error'); fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } $mode = 0; if (!empty($_POST['ur'])) { $mode |= 0400; } if (!empty($_POST['uw'])) { $mode |= 0200; } if (!empty($_POST['ux'])) { $mode |= 0100; } if (!empty($_POST['gr'])) { $mode |= 0040; } if (!empty($_POST['gw'])) { $mode |= 0020; } if (!empty($_POST['gx'])) { $mode |= 0010; } if (!empty($_POST['or'])) { $mode |= 0004; } if (!empty($_POST['ow'])) { $mode |= 0002; } if (!empty($_POST['ox'])) { $mode |= 0001; } if (@chmod($path . '/' . $file, $mode)) { fm_set_msg(lng('Permissions changed')); } else { fm_set_msg(lng('Permissions not changed'), 'error'); } fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } /*************************** /ACTIONS ***************************/ // get current path $path = FM_ROOT_PATH; if (FM_PATH != '') { $path .= '/' . FM_PATH; } // check path if (!is_dir($path)) { fm_redirect(FM_SELF_URL . '?p='); } // get parent folder $parent = fm_get_parent_path(FM_PATH); $objects = is_readable($path) ? scandir($path) : array(); $folders = array(); $files = array(); $current_path = array_slice(explode("/",$path), -1)[0]; if (is_array($objects) && fm_is_exclude_items($current_path)) { foreach ($objects as $file) { if ($file == '.' || $file == '..') { continue; } if (!FM_SHOW_HIDDEN && substr($file, 0, 1) === '.') { continue; } $new_path = $path . '/' . $file; if (@is_file($new_path) && fm_is_exclude_items($file)) { $files[] = $file; } elseif (@is_dir($new_path) && $file != '.' && $file != '..' && fm_is_exclude_items($file)) { $folders[] = $file; } } } if (!empty($files)) { natcasesort($files); } if (!empty($folders)) { natcasesort($folders); } // upload form if (isset($_GET['upload']) && !FM_READONLY) { fm_show_header(); // HEADER fm_show_nav_path(FM_PATH); // current path //get the allowed file extensions function getUploadExt() { $extArr = explode(',', FM_UPLOAD_EXTENSION); if(FM_UPLOAD_EXTENSION && $extArr) { array_walk($extArr, function(&$x) {$x = ".$x";}); return implode(',', $extArr); } return ''; } ?> <link href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/min/dropzone.min.css" rel="stylesheet"> <div class="path"> <div class="card mb-2 fm-upload-wrapper <?php echo fm_get_theme(); ?>"> <div class="card-header"> <ul class="nav nav-tabs card-header-tabs"> <li class="nav-item"> <a class="nav-link active" href="#fileUploader" data-target="#fileUploader"><i class="fa fa-arrow-circle-o-up"></i> <?php echo lng('UploadingFiles') ?></a> </li> <li class="nav-item"> <a class="nav-link" href="#urlUploader" class="js-url-upload" data-target="#urlUploader"><i class="fa fa-link"></i> Upload from URL</a> </li> </ul> </div> <div class="card-body"> <p class="card-text"> <a href="?p=<?php echo FM_PATH ?>" class="float-right"><i class="fa fa-chevron-circle-left go-back"></i> <?php echo lng('Back')?></a> <?php echo lng('DestinationFolder') ?>: <?php echo fm_enc(fm_convert_win(FM_PATH)) ?> </p> <form action="<?php echo htmlspecialchars(FM_SELF_URL) . '?p=' . fm_enc(FM_PATH) ?>" class="dropzone card-tabs-container" id="fileUploader" enctype="multipart/form-data"> <input type="hidden" name="p" value="<?php echo fm_enc(FM_PATH) ?>"> <input type="hidden" name="fullpath" id="fullpath" value="<?php echo fm_enc(FM_PATH) ?>"> <div class="fallback"> <input name="file" type="file" multiple/> </div> </form> <div class="upload-url-wrapper card-tabs-container hidden" id="urlUploader"> <form id="js-form-url-upload" class="form-inline" onsubmit="return upload_from_url(this);" method="POST" action=""> <input type="hidden" name="type" value="upload" aria-label="hidden" aria-hidden="true"> <input type="url" placeholder="URL" name="uploadurl" required class="form-control" style="width: 80%"> <button type="submit" class="btn btn-primary ml-3"><?php echo lng('Upload') ?></button> <div class="lds-facebook"><div></div><div></div><div></div></div> </form> <div id="js-url-upload__list" class="col-9 mt-3"></div> </div> </div> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/min/dropzone.min.js"></script> <script> Dropzone.options.fileUploader = { timeout: 120000, maxFilesize: <?php echo MAX_UPLOAD_SIZE; ?>, acceptedFiles : "<?php echo getUploadExt() ?>", init: function () { this.on("sending", function (file, xhr, formData) { let _path = (file.fullPath) ? file.fullPath : file.name; document.getElementById("fullpath").value = _path; xhr.ontimeout = (function() { toast('Error: Server Timeout'); }); }).on("success", function (res) { let _response = JSON.parse(res.xhr.response); if(_response.status == "error") { toast(_response.info); } }).on("error", function(file, response) { toast(response); }); } } </script> <?php fm_show_footer(); exit; } // copy form POST if (isset($_POST['copy']) && !FM_READONLY) { $copy_files = isset($_POST['file']) ? $_POST['file'] : null; if (!is_array($copy_files) || empty($copy_files)) { fm_set_msg(lng('Nothing selected'), 'alert'); fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } fm_show_header(); // HEADER fm_show_nav_path(FM_PATH); // current path ?> <div class="path"> <div class="card <?php echo fm_get_theme(); ?>"> <div class="card-header"> <h6><?php echo lng('Copying') ?></h6> </div> <div class="card-body"> <form action="" method="post"> <input type="hidden" name="p" value="<?php echo fm_enc(FM_PATH) ?>"> <input type="hidden" name="finish" value="1"> <?php foreach ($copy_files as $cf) { echo '<input type="hidden" name="file[]" value="' . fm_enc($cf) . '">' . PHP_EOL; } ?> <p class="break-word"><?php echo lng('Files') ?>: <b><?php echo implode('</b>, <b>', $copy_files) ?></b></p> <p class="break-word"><?php echo lng('SourceFolder') ?>: <?php echo fm_enc(fm_convert_win(FM_ROOT_PATH . '/' . FM_PATH)) ?><br> <label for="inp_copy_to"><?php echo lng('DestinationFolder') ?>:</label> <?php echo FM_ROOT_PATH ?>/<input type="text" name="copy_to" id="inp_copy_to" value="<?php echo fm_enc(FM_PATH) ?>"> </p> <p class="custom-checkbox custom-control"><input type="checkbox" name="move" value="1" id="js-move-files" class="custom-control-input"><label for="js-move-files" class="custom-control-label" style="vertical-align: sub"> <?php echo lng('Move') ?></label></p> <p> <button type="submit" class="btn btn-success"><i class="fa fa-check-circle"></i> <?php echo lng('Copy') ?></button> <b><a href="?p=<?php echo urlencode(FM_PATH) ?>" class="btn btn-outline-primary"><i class="fa fa-times-circle"></i> <?php echo lng('Cancel') ?></a></b> </p> </form> </div> </div> </div> <?php fm_show_footer(); exit; } // copy form if (isset($_GET['copy']) && !isset($_GET['finish']) && !FM_READONLY) { $copy = $_GET['copy']; $copy = fm_clean_path($copy); if ($copy == '' || !file_exists(FM_ROOT_PATH . '/' . $copy)) { fm_set_msg(lng('File not found'), 'error'); fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } fm_show_header(); // HEADER fm_show_nav_path(FM_PATH); // current path ?> <div class="path"> <p><b>Copying</b></p> <p class="break-word"> Source path: <?php echo fm_enc(fm_convert_win(FM_ROOT_PATH . '/' . $copy)) ?><br> Destination folder: <?php echo fm_enc(fm_convert_win(FM_ROOT_PATH . '/' . FM_PATH)) ?> </p> <p> <b><a href="?p=<?php echo urlencode(FM_PATH) ?>&copy=<?php echo urlencode($copy) ?>&finish=1"><i class="fa fa-check-circle"></i> Copy</a></b> <b><a href="?p=<?php echo urlencode(FM_PATH) ?>&copy=<?php echo urlencode($copy) ?>&finish=1&move=1"><i class="fa fa-check-circle"></i> Move</a></b> <b><a href="?p=<?php echo urlencode(FM_PATH) ?>"><i class="fa fa-times-circle"></i> Cancel</a></b> </p> <p><i><?php echo lng('Select folder') ?></i></p> <ul class="folders break-word"> <?php if ($parent !== false) { ?> <li><a href="?p=<?php echo urlencode($parent) ?>&copy=<?php echo urlencode($copy) ?>"><i class="fa fa-chevron-circle-left"></i> ..</a></li> <?php } foreach ($folders as $f) { ?> <li> <a href="?p=<?php echo urlencode(trim(FM_PATH . '/' . $f, '/')) ?>&copy=<?php echo urlencode($copy) ?>"><i class="fa fa-folder-o"></i> <?php echo fm_convert_win($f) ?></a></li> <?php } ?> </ul> </div> <?php fm_show_footer(); exit; } if (isset($_GET['settings']) && !FM_READONLY) { fm_show_header(); // HEADER fm_show_nav_path(FM_PATH); // current path global $cfg, $lang, $lang_list; ?> <div class="col-md-8 offset-md-2 pt-3"> <div class="card mb-2 <?php echo fm_get_theme(); ?>"> <h6 class="card-header"> <i class="fa fa-cog"></i> <?php echo lng('Settings') ?> <a href="?p=<?php echo FM_PATH ?>" class="float-right"><i class="fa fa-window-close"></i> <?php echo lng('Cancel')?></a> </h6> <div class="card-body"> <form id="js-settings-form" action="" method="post" data-type="ajax" onsubmit="return save_settings(this)"> <input type="hidden" name="type" value="settings" aria-label="hidden" aria-hidden="true"> <div class="form-group row"> <label for="js-language" class="col-sm-3 col-form-label"><?php echo lng('Language') ?></label> <div class="col-sm-5"> <select class="form-control" id="js-language" name="js-language"> <?php function getSelected($l) { global $lang; return ($lang == $l) ? 'selected' : ''; } foreach ($lang_list as $k => $v) { echo "<option value='$k' ".getSelected($k).">$v</option>"; } ?> </select> </div> </div> <?php //get ON/OFF and active class function getChecked($conf, $val, $txt) { if($conf== 1 && $val ==1) { return $txt; } else if($conf == '' && $val == '') { return $txt; } else { return ''; } } ?> <div class="form-group row"> <label for="js-err-rpt-1" class="col-sm-3 col-form-label"><?php echo lng('ErrorReporting') ?></label> <div class="col-sm-9"> <div class="btn-group btn-group-toggle" data-toggle="buttons"> <label class="btn btn-secondary <?php echo getChecked($report_errors, 1, 'active') ?>"> <input type="radio" name="js-error-report" id="js-err-rpt-1" autocomplete="off" value="true" <?php echo getChecked($report_errors, 1, 'checked') ?> > ON </label> <label class="btn btn-secondary <?php echo getChecked($report_errors, '', 'active') ?>"> <input type="radio" name="js-error-report" id="js-err-rpt-0" autocomplete="off" value="false" <?php echo getChecked($report_errors, '', 'checked') ?> > OFF </label> </div> </div> </div> <div class="form-group row"> <label for="js-hdn-1" class="col-sm-3 col-form-label"><?php echo lng('ShowHiddenFiles') ?></label> <div class="col-sm-9"> <div class="btn-group btn-group-toggle" data-toggle="buttons"> <label class="btn btn-secondary <?php echo getChecked($show_hidden_files, 1, 'active') ?>"> <input type="radio" name="js-show-hidden" id="js-hdn-1" autocomplete="off" value="true" <?php echo getChecked($show_hidden_files, 1, 'checked') ?> > ON </label> <label class="btn btn-secondary <?php echo getChecked($show_hidden_files, '', 'active') ?>"> <input type="radio" name="js-show-hidden" id="js-hdn-0" autocomplete="off" value="false" <?php echo getChecked($show_hidden_files, '', 'checked') ?> > OFF </label> </div> </div> </div> <div class="form-group row"> <label for="js-hid-1" class="col-sm-3 col-form-label"><?php echo lng('HideColumns') ?></label> <div class="col-sm-9"> <div class="btn-group btn-group-toggle" data-toggle="buttons"> <label class="btn btn-secondary <?php echo getChecked($hide_Cols, 1, 'active') ?>"> <input type="radio" name="js-hide-cols" id="js-hid-1" autocomplete="off" value="true" <?php echo getChecked($hide_Cols, 1, 'checked') ?> > ON </label> <label class="btn btn-secondary <?php echo getChecked($hide_Cols, '', 'active') ?>"> <input type="radio" name="js-hide-cols" id="js-hid-0" autocomplete="off" value="false" <?php echo getChecked($hide_Cols, '', 'checked') ?> > OFF </label> </div> </div> </div> <div class="form-group row"> <label for="js-dir-1" class="col-sm-3 col-form-label"><?php echo lng('CalculateFolderSize') ?></label> <div class="col-sm-9"> <div class="btn-group btn-group-toggle" data-toggle="buttons"> <label class="btn btn-secondary <?php echo getChecked($calc_folder, 1, 'active') ?>"> <input type="radio" name="js-calc-folder" id="js-dir-1" autocomplete="off" value="true" <?php echo getChecked($calc_folder, 1, 'checked') ?> > ON </label> <label class="btn btn-secondary <?php echo getChecked($calc_folder, '', 'active') ?>"> <input type="radio" name="js-calc-folder" id="js-dir-0" autocomplete="off" value="false" <?php echo getChecked($calc_folder, '', 'checked') ?> > OFF </label> </div> </div> </div> <div class="form-group row"> <label for="js-3-1" class="col-sm-3 col-form-label"><?php echo lng('Theme') ?></label> <div class="col-sm-5"> <select class="form-control" id="js-3-0" name="js-theme-3" style="width:100px;"> <option value='light' <?php if($theme == "light"){echo "selected";} ?>><?php echo lng('light') ?></option> <option value='dark' <?php if($theme == "dark"){echo "selected";} ?>><?php echo lng('dark') ?></option> </select> </div> </div> <div class="form-group row"> <div class="col-sm-10"> <button type="submit" class="btn btn-success"> <i class="fa fa-check-circle"></i> <?php echo lng('Save'); ?></button> </div> </div> </form> </div> </div> </div> <?php fm_show_footer(); exit; } if (isset($_GET['help'])) { fm_show_header(); // HEADER fm_show_nav_path(FM_PATH); // current path global $cfg, $lang; ?> <div class="col-md-8 offset-md-2 pt-3"> <div class="card mb-2 <?php echo fm_get_theme(); ?>"> <h6 class="card-header"> <i class="fa fa-exclamation-circle"></i> <?php echo lng('Help') ?> <a href="?p=<?php echo FM_PATH ?>" class="float-right"><i class="fa fa-window-close"></i> <?php echo lng('Cancel')?></a> </h6> <div class="card-body"> <div class="row"> <div class="col-xs-12 col-sm-6"> <p><h3><a href="https://github.com/prasathmani/tinyfilemanager" target="_blank" class="app-v-title"> Tiny File Manager <?php echo VERSION; ?></a></h3></p> <p>Author: Prasath Mani</p> <p>Mail Us: <a href="mailto:ccpprogrammers@gmail.com">ccpprogrammers[at]gmail.com</a> </p> </div> <div class="col-xs-12 col-sm-6"> <div class="card"> <ul class="list-group list-group-flush"> <li class="list-group-item"><a href="https://github.com/prasathmani/tinyfilemanager/wiki" target="_blank"><i class="fa fa-question-circle"></i> <?php echo lng('Help Documents') ?> </a> </li> <li class="list-group-item"><a href="https://github.com/prasathmani/tinyfilemanager/issues" target="_blank"><i class="fa fa-bug"></i> <?php echo lng('Report Issue') ?></a></li> <li class="list-group-item"><a href="javascript:latest_release_info('<?php echo VERSION; ?>');"><i class="fa fa-link"> </i> <?php echo lng('Check Latest Version') ?></a></li> <?php if(!FM_READONLY) { ?> <li class="list-group-item"><a href="javascript:show_new_pwd();"><i class="fa fa-lock"></i> <?php echo lng('Generate new password hash') ?></a></li> <?php } ?> </ul> </div> </div> </div> <div class="row js-new-pwd hidden mt-2"> <div class="col-12"> <form class="form-inline" onsubmit="return new_password_hash(this)" method="POST" action=""> <input type="hidden" name="type" value="pwdhash" aria-label="hidden" aria-hidden="true"> <div class="form-group mb-2"> <label for="staticEmail2"><?php echo lng('Generate new password hash') ?></label> </div> <div class="form-group mx-sm-3 mb-2"> <label for="inputPassword2" class="sr-only"><?php echo lng('Password') ?></label> <input type="text" class="form-control btn-sm" id="inputPassword2" name="inputPassword2" placeholder="Password" required> </div> <button type="submit" class="btn btn-success btn-sm mb-2"><?php echo lng('Generate') ?></button> </form> <textarea class="form-control" rows="2" readonly id="js-pwd-result"></textarea> </div> </div> </div> </div> </div> <?php fm_show_footer(); exit; } // file viewer if (isset($_GET['view'])) { $file = $_GET['view']; $quickView = (isset($_GET['quickView']) && $_GET['quickView'] == 1) ? true : false; $file = fm_clean_path($file, false); $file = str_replace('/', '', $file); if ($file == '' || !is_file($path . '/' . $file) || in_array($file, $GLOBALS['exclude_items'])) { fm_set_msg(lng('File not found'), 'error'); fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } if(!$quickView) { fm_show_header(); // HEADER fm_show_nav_path(FM_PATH); // current path } $file_url = FM_ROOT_URL . fm_convert_win((FM_PATH != '' ? '/' . FM_PATH : '') . '/' . $file); $file_path = $path . '/' . $file; $ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION)); $mime_type = fm_get_mime_type($file_path); $filesize_raw = fm_get_size($file_path); $filesize = fm_get_filesize($filesize_raw); $is_zip = false; $is_gzip = false; $is_image = false; $is_audio = false; $is_video = false; $is_text = false; $is_onlineViewer = false; $view_title = 'File'; $filenames = false; // for zip $content = ''; // for text $online_viewer = strtolower(FM_DOC_VIEWER); if($online_viewer && $online_viewer !== 'false' && in_array($ext, fm_get_onlineViewer_exts())){ $is_onlineViewer = true; } elseif ($ext == 'zip' || $ext == 'tar') { $is_zip = true; $view_title = 'Archive'; $filenames = fm_get_zif_info($file_path, $ext); } elseif (in_array($ext, fm_get_image_exts())) { $is_image = true; $view_title = 'Image'; } elseif (in_array($ext, fm_get_audio_exts())) { $is_audio = true; $view_title = 'Audio'; } elseif (in_array($ext, fm_get_video_exts())) { $is_video = true; $view_title = 'Video'; } elseif (in_array($ext, fm_get_text_exts()) || substr($mime_type, 0, 4) == 'text' || in_array($mime_type, fm_get_text_mimes())) { $is_text = true; $content = file_get_contents($file_path); } ?> <div class="row"> <div class="col-12"> <?php if(!$quickView) { ?> <p class="break-word"><b><?php echo $view_title ?> "<?php echo fm_enc(fm_convert_win($file)) ?>"</b></p> <p class="break-word"> Full path: <?php echo fm_enc(fm_convert_win($file_path)) ?><br> File size: <?php echo ($filesize_raw <= 1000) ? "$filesize_raw bytes" : $filesize; ?><br> MIME-type: <?php echo $mime_type ?><br> <?php // ZIP info if (($is_zip || $is_gzip) && $filenames !== false) { $total_files = 0; $total_comp = 0; $total_uncomp = 0; foreach ($filenames as $fn) { if (!$fn['folder']) { $total_files++; } $total_comp += $fn['compressed_size']; $total_uncomp += $fn['filesize']; } ?> Files in archive: <?php echo $total_files ?><br> Total size: <?php echo fm_get_filesize($total_uncomp) ?><br> Size in archive: <?php echo fm_get_filesize($total_comp) ?><br> Compression: <?php echo round(($total_comp / $total_uncomp) * 100) ?>%<br> <?php } // Image info if ($is_image) { $image_size = getimagesize($file_path); echo 'Image sizes: ' . (isset($image_size[0]) ? $image_size[0] : '0') . ' x ' . (isset($image_size[1]) ? $image_size[1] : '0') . '<br>'; } // Text info if ($is_text) { $is_utf8 = fm_is_utf8($content); if (function_exists('iconv')) { if (!$is_utf8) { $content = iconv(FM_ICONV_INPUT_ENC, 'UTF-8//IGNORE', $content); } } echo 'Charset: ' . ($is_utf8 ? 'utf-8' : '8 bit') . '<br>'; } ?> </p> <p> <b><a href="?p=<?php echo urlencode(FM_PATH) ?>&dl=<?php echo urlencode($file) ?>"><i class="fa fa-cloud-download"></i> <?php echo lng('Download') ?></a></b> <b><a href="<?php echo fm_enc($file_url) ?>" target="_blank"><i class="fa fa-external-link-square"></i> <?php echo lng('Open') ?></a></b> <?php // ZIP actions if (!FM_READONLY && ($is_zip || $is_gzip) && $filenames !== false) { $zip_name = pathinfo($file_path, PATHINFO_FILENAME); ?> <b><a href="?p=<?php echo urlencode(FM_PATH) ?>&unzip=<?php echo urlencode($file) ?>"><i class="fa fa-check-circle"></i> <?php echo lng('UnZip') ?></a></b> <b><a href="?p=<?php echo urlencode(FM_PATH) ?>&unzip=<?php echo urlencode($file) ?>&tofolder=1" title="UnZip to <?php echo fm_enc($zip_name) ?>"><i class="fa fa-check-circle"></i> <?php echo lng('UnZipToFolder') ?></a></b> <?php } if ($is_text && !FM_READONLY) { ?> <b><a href="?p=<?php echo urlencode(trim(FM_PATH)) ?>&edit=<?php echo urlencode($file) ?>" class="edit-file"><i class="fa fa-pencil-square"></i> <?php echo lng('Edit') ?> </a></b> <b><a href="?p=<?php echo urlencode(trim(FM_PATH)) ?>&edit=<?php echo urlencode($file) ?>&env=ace" class="edit-file"><i class="fa fa-pencil-square-o"></i> <?php echo lng('AdvancedEditor') ?> </a></b> <?php } ?> <b><a href="?p=<?php echo urlencode(FM_PATH) ?>"><i class="fa fa-chevron-circle-left go-back"></i> <?php echo lng('Back') ?></a></b> </p> <?php } if($is_onlineViewer) { if($online_viewer == 'google') { echo '<iframe src="https://docs.google.com/viewer?embedded=true&hl=en&url=' . fm_enc($file_url) . '" frameborder="no" style="width:100%;min-height:460px"></iframe>'; } else if($online_viewer == 'microsoft') { echo '<iframe src="https://view.officeapps.live.com/op/embed.aspx?src=' . fm_enc($file_url) . '" frameborder="no" style="width:100%;min-height:460px"></iframe>'; } } elseif ($is_zip) { // ZIP content if ($filenames !== false) { echo '<code class="maxheight">'; foreach ($filenames as $fn) { if ($fn['folder']) { echo '<b>' . fm_enc($fn['name']) . '</b><br>'; } else { echo $fn['name'] . ' (' . fm_get_filesize($fn['filesize']) . ')<br>'; } } echo '</code>'; } else { echo '<p>'.lng('Error while fetching archive info').'</p>'; } } elseif ($is_image) { // Image content if (in_array($ext, array('gif', 'jpg', 'jpeg', 'png', 'bmp', 'ico', 'svg', 'webp', 'avif'))) { echo '<p><img src="' . fm_enc($file_url) . '" alt="" class="preview-img"></p>'; } } elseif ($is_audio) { // Audio content echo '<p><audio src="' . fm_enc($file_url) . '" controls preload="metadata"></audio></p>'; } elseif ($is_video) { // Video content echo '<div class="preview-video"><video src="' . fm_enc($file_url) . '" width="640" height="360" controls preload="metadata"></video></div>'; } elseif ($is_text) { if (FM_USE_HIGHLIGHTJS) { // highlight $hljs_classes = array( 'shtml' => 'xml', 'htaccess' => 'apache', 'phtml' => 'php', 'lock' => 'json', 'svg' => 'xml', ); $hljs_class = isset($hljs_classes[$ext]) ? 'lang-' . $hljs_classes[$ext] : 'lang-' . $ext; if (empty($ext) || in_array(strtolower($file), fm_get_text_names()) || preg_match('#\.min\.(css|js)$#i', $file)) { $hljs_class = 'nohighlight'; } $content = '<pre class="with-hljs"><code class="' . $hljs_class . '">' . fm_enc($content) . '</code></pre>'; } elseif (in_array($ext, array('php', 'php4', 'php5', 'phtml', 'phps'))) { // php highlight $content = highlight_string($content, true); } else { $content = '<pre>' . fm_enc($content) . '</pre>'; } echo $content; } ?> </div> </div> <?php if(!$quickView) { fm_show_footer(); } exit; } // file editor if (isset($_GET['edit'])) { $file = $_GET['edit']; $file = fm_clean_path($file, false); $file = str_replace('/', '', $file); if ($file == '' || !is_file($path . '/' . $file)) { fm_set_msg(lng('File not found'), 'error'); fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } header('X-XSS-Protection:0'); fm_show_header(); // HEADER fm_show_nav_path(FM_PATH); // current path $file_url = FM_ROOT_URL . fm_convert_win((FM_PATH != '' ? '/' . FM_PATH : '') . '/' . $file); $file_path = $path . '/' . $file; // normal editer $isNormalEditor = true; if (isset($_GET['env'])) { if ($_GET['env'] == "ace") { $isNormalEditor = false; } } // Save File if (isset($_POST['savedata'])) { $writedata = $_POST['savedata']; $fd = fopen($file_path, "w"); @fwrite($fd, $writedata); fclose($fd); fm_set_msg(lng('File Saved Successfully')); } $ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION)); $mime_type = fm_get_mime_type($file_path); $filesize = filesize($file_path); $is_text = false; $content = ''; // for text if (in_array($ext, fm_get_text_exts()) || substr($mime_type, 0, 4) == 'text' || in_array($mime_type, fm_get_text_mimes())) { $is_text = true; $content = file_get_contents($file_path); } ?> <div class="path"> <div class="row"> <div class="col-xs-12 col-sm-5 col-lg-6 pt-1"> <div class="btn-toolbar" role="toolbar"> <?php if (!$isNormalEditor) { ?> <div class="btn-group js-ace-toolbar"> <button data-cmd="none" data-option="fullscreen" class="btn btn-sm btn-outline-secondary" id="js-ace-fullscreen" title="Fullscreen"><i class="fa fa-expand" title="Fullscreen"></i></button> <button data-cmd="find" class="btn btn-sm btn-outline-secondary" id="js-ace-search" title="Search"><i class="fa fa-search" title="Search"></i></button> <button data-cmd="undo" class="btn btn-sm btn-outline-secondary" id="js-ace-undo" title="Undo"><i class="fa fa-undo" title="Undo"></i></button> <button data-cmd="redo" class="btn btn-sm btn-outline-secondary" id="js-ace-redo" title="Redo"><i class="fa fa-repeat" title="Redo"></i></button> <button data-cmd="none" data-option="wrap" class="btn btn-sm btn-outline-secondary" id="js-ace-wordWrap" title="Word Wrap"><i class="fa fa-text-width" title="Word Wrap"></i></button> <button data-cmd="none" data-option="help" class="btn btn-sm btn-outline-secondary" id="js-ace-goLine" title="Help"><i class="fa fa-question" title="Help"></i></button> <select id="js-ace-mode" data-type="mode" title="Select Document Type" class="btn-outline-secondary border-left-0 d-none d-md-block"><option>-- Select Mode --</option></select> <select id="js-ace-theme" data-type="theme" title="Select Theme" class="btn-outline-secondary border-left-0 d-none d-lg-block"><option>-- Select Theme --</option></select> <select id="js-ace-fontSize" data-type="fontSize" title="Select Font Size" class="btn-outline-secondary border-left-0 d-none d-lg-block"><option>-- Select Font Size --</option></select> </div> <?php } ?> </div> </div> <div class="edit-file-actions col-xs-12 col-sm-7 col-lg-6 text-right pt-1"> <a title="Back" class="btn btn-sm btn-outline-primary" href="?p=<?php echo urlencode(trim(FM_PATH)) ?>&view=<?php echo urlencode($file) ?>"><i class="fa fa-reply-all"></i> <?php echo lng('Back') ?></a> <a title="Backup" class="btn btn-sm btn-outline-primary" href="javascript:void(0);" onclick="backup('<?php echo urlencode(trim(FM_PATH)) ?>','<?php echo urlencode($file) ?>')"><i class="fa fa-database"></i> <?php echo lng('BackUp') ?></a> <?php if ($is_text) { ?> <?php if ($isNormalEditor) { ?> <a title="Advanced" class="btn btn-sm btn-outline-primary" href="?p=<?php echo urlencode(trim(FM_PATH)) ?>&edit=<?php echo urlencode($file) ?>&env=ace"><i class="fa fa-pencil-square-o"></i> <?php echo lng('AdvancedEditor') ?></a> <button type="button" class="btn btn-sm btn-outline-primary name="Save" data-url="<?php echo fm_enc($file_url) ?>" onclick="edit_save(this,'nrl')"><i class="fa fa-floppy-o"></i> Save </button> <?php } else { ?> <a title="Plain Editor" class="btn btn-sm btn-outline-primary" href="?p=<?php echo urlencode(trim(FM_PATH)) ?>&edit=<?php echo urlencode($file) ?>"><i class="fa fa-text-height"></i> <?php echo lng('NormalEditor') ?></a> <button type="button" class="btn btn-sm btn-outline-primary" name="Save" data-url="<?php echo fm_enc($file_url) ?>" onclick="edit_save(this,'ace')"><i class="fa fa-floppy-o"></i> <?php echo lng('Save') ?> </button> <?php } ?> <?php } ?> </div> </div> <?php if ($is_text && $isNormalEditor) { echo '<textarea class="mt-2" id="normal-editor" rows="33" cols="120" style="width: 99.5%;">' . htmlspecialchars($content) . '</textarea>'; } elseif ($is_text) { echo '<div id="editor" contenteditable="true">' . htmlspecialchars($content) . '</div>'; } else { fm_set_msg(lng('FILE EXTENSION HAS NOT SUPPORTED'), 'error'); } ?> </div> <?php fm_show_footer(); exit; } // chmod (not for Windows) if (isset($_GET['chmod']) && !FM_READONLY && !FM_IS_WIN) { $file = $_GET['chmod']; $file = fm_clean_path($file); $file = str_replace('/', '', $file); if ($file == '' || (!is_file($path . '/' . $file) && !is_dir($path . '/' . $file))) { fm_set_msg(lng('File not found'), 'error'); fm_redirect(FM_SELF_URL . '?p=' . urlencode(FM_PATH)); } fm_show_header(); // HEADER fm_show_nav_path(FM_PATH); // current path $file_url = FM_ROOT_URL . (FM_PATH != '' ? '/' . FM_PATH : '') . '/' . $file; $file_path = $path . '/' . $file; $mode = fileperms($path . '/' . $file); ?> <div class="path"> <div class="card mb-2 <?php echo fm_get_theme(); ?>"> <h6 class="card-header"> <?php echo lng('ChangePermissions') ?> </h6> <div class="card-body"> <p class="card-text"> Full path: <?php echo $file_path ?><br> </p> <form action="" method="post"> <input type="hidden" name="p" value="<?php echo fm_enc(FM_PATH) ?>"> <input type="hidden" name="chmod" value="<?php echo fm_enc($file) ?>"> <table class="table compact-table <?php echo fm_get_theme(); ?>"> <tr> <td></td> <td><b><?php echo lng('Owner') ?></b></td> <td><b><?php echo lng('Group') ?></b></td> <td><b><?php echo lng('Other') ?></b></td> </tr> <tr> <td style="text-align: right"><b><?php echo lng('Read') ?></b></td> <td><label><input type="checkbox" name="ur" value="1"<?php echo ($mode & 00400) ? ' checked' : '' ?>></label></td> <td><label><input type="checkbox" name="gr" value="1"<?php echo ($mode & 00040) ? ' checked' : '' ?>></label></td> <td><label><input type="checkbox" name="or" value="1"<?php echo ($mode & 00004) ? ' checked' : '' ?>></label></td> </tr> <tr> <td style="text-align: right"><b><?php echo lng('Write') ?></b></td> <td><label><input type="checkbox" name="uw" value="1"<?php echo ($mode & 00200) ? ' checked' : '' ?>></label></td> <td><label><input type="checkbox" name="gw" value="1"<?php echo ($mode & 00020) ? ' checked' : '' ?>></label></td> <td><label><input type="checkbox" name="ow" value="1"<?php echo ($mode & 00002) ? ' checked' : '' ?>></label></td> </tr> <tr> <td style="text-align: right"><b><?php echo lng('Execute') ?></b></td> <td><label><input type="checkbox" name="ux" value="1"<?php echo ($mode & 00100) ? ' checked' : '' ?>></label></td> <td><label><input type="checkbox" name="gx" value="1"<?php echo ($mode & 00010) ? ' checked' : '' ?>></label></td> <td><label><input type="checkbox" name="ox" value="1"<?php echo ($mode & 00001) ? ' checked' : '' ?>></label></td> </tr> </table> <p> <button type="submit" class="btn btn-success"><i class="fa fa-check-circle"></i> <?php echo lng('Change') ?></button> <b><a href="?p=<?php echo urlencode(FM_PATH) ?>" class="btn btn-outline-primary"><i class="fa fa-times-circle"></i> <?php echo lng('Cancel') ?></a></b> </p> </form> </div> </div> </div> <?php fm_show_footer(); exit; } //--- FILEMANAGER MAIN fm_show_header(); // HEADER fm_show_nav_path(FM_PATH); // current path // messages fm_show_message(); $num_files = count($files); $num_folders = count($folders); $all_files_size = 0; $tableTheme = (FM_THEME == "dark") ? "text-white bg-dark table-dark" : "bg-white"; ?> <form action="" method="post" class="pt-3"> <input type="hidden" name="p" value="<?php echo fm_enc(FM_PATH) ?>"> <input type="hidden" name="group" value="1"> <div class="table-responsive"> <table class="table table-bordered table-hover table-sm <?php echo $tableTheme; ?>" id="main-table"> <thead class="thead-white"> <tr> <?php if (!FM_READONLY): ?> <th style="width:3%" class="custom-checkbox-header"> <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input" id="js-select-all-items" onclick="checkbox_toggle()"> <label class="custom-control-label" for="js-select-all-items"></label> </div> </th><?php endif; ?> <th><?php echo lng('Name') ?></th> <th><?php echo lng('Size') ?></th> <th><?php echo lng('Modified') ?></th> <?php if (!FM_IS_WIN && !$hide_Cols): ?> <th><?php echo lng('Perms') ?></th> <th><?php echo lng('Owner') ?></th><?php endif; ?> <th><?php echo lng('Actions') ?></th> </tr> </thead> <?php // link to parent folder if ($parent !== false) { ?> <tr><?php if (!FM_READONLY): ?> <td class="nosort"></td><?php endif; ?> <td class="border-0"><a href="?p=<?php echo urlencode($parent) ?>"><i class="fa fa-chevron-circle-left go-back"></i> ..</a></td> <td class="border-0"></td> <td class="border-0"></td> <td class="border-0"></td> <?php if (!FM_IS_WIN && !$hide_Cols) { ?> <td class="border-0"></td> <td class="border-0"></td> <?php } ?> </tr> <?php } $ii = 3399; foreach ($folders as $f) { $is_link = is_link($path . '/' . $f); $img = $is_link ? 'icon-link_folder' : 'fa fa-folder-o'; $modif_raw = filemtime($path . '/' . $f); $modif = date(FM_DATETIME_FORMAT, $modif_raw); if ($calc_folder) { $filesize_raw = fm_get_directorysize($path . '/' . $f); $filesize = fm_get_filesize($filesize_raw); } else { $filesize_raw = ""; $filesize = lng('Folder'); } $perms = substr(decoct(fileperms($path . '/' . $f)), -4); if (function_exists('posix_getpwuid') && function_exists('posix_getgrgid')) { $owner = posix_getpwuid(fileowner($path . '/' . $f)); $group = posix_getgrgid(filegroup($path . '/' . $f)); } else { $owner = array('name' => '?'); $group = array('name' => '?'); } ?> <tr> <?php if (!FM_READONLY): ?> <td class="custom-checkbox-td"> <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input" id="<?php echo $ii ?>" name="file[]" value="<?php echo fm_enc($f) ?>"> <label class="custom-control-label" for="<?php echo $ii ?>"></label> </div> </td><?php endif; ?> <td> <div class="filename"><a href="?p=<?php echo urlencode(trim(FM_PATH . '/' . $f, '/')) ?>"><i class="<?php echo $img ?>"></i> <?php echo fm_convert_win(fm_enc($f)) ?> </a><?php echo($is_link ? ' → <i>' . readlink($path . '/' . $f) . '</i>' : '') ?></div> </td> <td data-sort="a-<?php echo str_pad($filesize_raw, 18, "0", STR_PAD_LEFT);?>"> <?php echo $filesize; ?> </td> <td data-sort="a-<?php echo $modif_raw;?>"><?php echo $modif ?></td> <?php if (!FM_IS_WIN && !$hide_Cols): ?> <td><?php if (!FM_READONLY): ?><a title="Change Permissions" href="?p=<?php echo urlencode(FM_PATH) ?>&chmod=<?php echo urlencode($f) ?>"><?php echo $perms ?></a><?php else: ?><?php echo $perms ?><?php endif; ?> </td> <td><?php echo $owner['name'] . ':' . $group['name'] ?></td> <?php endif; ?> <td class="inline-actions"><?php if (!FM_READONLY): ?> <a title="<?php echo lng('Delete')?>" href="?p=<?php echo urlencode(FM_PATH) ?>&del=<?php echo urlencode($f) ?>" onclick="return confirm('<?php echo lng('Delete').' '.lng('Folder').'?'; ?>\n \n ( <?php echo urlencode($f) ?> )');"> <i class="fa fa-trash-o" aria-hidden="true"></i></a> <a title="<?php echo lng('Rename')?>" href="#" onclick="rename('<?php echo fm_enc(addslashes(FM_PATH)) ?>', '<?php echo fm_enc(addslashes($f)) ?>');return false;"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a> <a title="<?php echo lng('CopyTo')?>..." href="?p=&copy=<?php echo urlencode(trim(FM_PATH . '/' . $f, '/')) ?>"><i class="fa fa-files-o" aria-hidden="true"></i></a> <?php endif; ?> <a title="<?php echo lng('DirectLink')?>" href="<?php echo fm_enc(FM_ROOT_URL . (FM_PATH != '' ? '/' . FM_PATH : '') . '/' . $f . '/') ?>" target="_blank"><i class="fa fa-link" aria-hidden="true"></i></a> </td> </tr> <?php flush(); $ii++; } $ik = 6070; foreach ($files as $f) { $is_link = is_link($path . '/' . $f); $img = $is_link ? 'fa fa-file-text-o' : fm_get_file_icon_class($path . '/' . $f); $modif_raw = filemtime($path . '/' . $f); $modif = date(FM_DATETIME_FORMAT, $modif_raw); $filesize_raw = fm_get_size($path . '/' . $f); $filesize = fm_get_filesize($filesize_raw); $filelink = '?p=' . urlencode(FM_PATH) . '&view=' . urlencode($f); $all_files_size += $filesize_raw; $perms = substr(decoct(fileperms($path . '/' . $f)), -4); if (function_exists('posix_getpwuid') && function_exists('posix_getgrgid')) { $owner = posix_getpwuid(fileowner($path . '/' . $f)); $group = posix_getgrgid(filegroup($path . '/' . $f)); } else { $owner = array('name' => '?'); $group = array('name' => '?'); } ?> <tr> <?php if (!FM_READONLY): ?> <td class="custom-checkbox-td"> <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input" id="<?php echo $ik ?>" name="file[]" value="<?php echo fm_enc($f) ?>"> <label class="custom-control-label" for="<?php echo $ik ?>"></label> </div> </td><?php endif; ?> <td> <div class="filename"> <?php if (in_array(strtolower(pathinfo($f, PATHINFO_EXTENSION)), array('gif', 'jpg', 'jpeg', 'png', 'bmp', 'ico', 'svg', 'webp', 'avif'))): ?> <?php $imagePreview = fm_enc(FM_ROOT_URL . (FM_PATH != '' ? '/' . FM_PATH : '') . '/' . $f); ?> <a href="<?php echo $filelink ?>" data-preview-image="<?php echo $imagePreview ?>" title="<?php echo fm_enc($f) ?>"> <?php else: ?> <a href="<?php echo $filelink ?>" title="<?php echo $f ?>"> <?php endif; ?> <i class="<?php echo $img ?>"></i> <?php echo fm_convert_win(fm_enc($f)) ?> </a> <?php echo($is_link ? ' → <i>' . readlink($path . '/' . $f) . '</i>' : '') ?> </div> </td> <td data-sort=b-"<?php echo str_pad($filesize_raw, 18, "0", STR_PAD_LEFT); ?>"><span title="<?php printf('%s bytes', $filesize_raw) ?>"> <?php echo $filesize; ?> </span></td> <td data-sort="b-<?php echo $modif_raw;?>"><?php echo $modif ?></td> <?php if (!FM_IS_WIN && !$hide_Cols): ?> <td><?php if (!FM_READONLY): ?><a title="<?php echo 'Change Permissions' ?>" href="?p=<?php echo urlencode(FM_PATH) ?>&chmod=<?php echo urlencode($f) ?>"><?php echo $perms ?></a><?php else: ?><?php echo $perms ?><?php endif; ?> </td> <td><?php echo fm_enc($owner['name'] . ':' . $group['name']) ?></td> <?php endif; ?> <td class="inline-actions"> <a title="<?php echo lng('Preview') ?>" href="<?php echo $filelink.'&quickView=1'; ?>" data-toggle="lightbox" data-gallery="tiny-gallery" data-title="<?php echo fm_convert_win(fm_enc($f)) ?>" data-max-width="100%" data-width="100%"><i class="fa fa-eye"></i></a> <?php if (!FM_READONLY): ?> <a title="<?php echo lng('Delete') ?>" href="?p=<?php echo urlencode(FM_PATH) ?>&del=<?php echo urlencode($f) ?>" onclick="return confirm('<?php echo lng('Delete').' '.lng('File').'?'; ?>\n \n ( <?php echo urlencode($f) ?> )');"> <i class="fa fa-trash-o"></i></a> <a title="<?php echo lng('Rename') ?>" href="#" onclick="rename('<?php echo fm_enc(addslashes(FM_PATH)) ?>', '<?php echo fm_enc(addslashes($f)) ?>');return false;"><i class="fa fa-pencil-square-o"></i></a> <a title="<?php echo lng('CopyTo') ?>..." href="?p=<?php echo urlencode(FM_PATH) ?>&copy=<?php echo urlencode(trim(FM_PATH . '/' . $f, '/')) ?>"><i class="fa fa-files-o"></i></a> <?php endif; ?> <a title="<?php echo lng('DirectLink') ?>" href="<?php echo fm_enc(FM_ROOT_URL . (FM_PATH != '' ? '/' . FM_PATH : '') . '/' . $f) ?>" target="_blank"><i class="fa fa-link"></i></a> <a title="<?php echo lng('Download') ?>" href="?p=<?php echo urlencode(FM_PATH) ?>&dl=<?php echo urlencode($f) ?>"><i class="fa fa-download"></i></a> </td> </tr> <?php flush(); $ik++; } if (empty($folders) && empty($files)) { ?> <tfoot> <tr><?php if (!FM_READONLY): ?> <td></td><?php endif; ?> <td colspan="<?php echo (!FM_IS_WIN && !$hide_Cols) ? '6' : '4' ?>"><em><?php echo 'Folder is empty' ?></em></td> </tr> </tfoot> <?php } else { ?> <tfoot> <tr><?php if (!FM_READONLY): ?> <td class="gray"></td><?php endif; ?> <td class="gray" colspan="<?php echo (!FM_IS_WIN && !$hide_Cols) ? '6' : '4' ?>"> <?php echo lng('FullSize').': <span class="badge badge-light">'.fm_get_filesize($all_files_size).'</span>' ?> <?php echo lng('File').': <span class="badge badge-light">'.$num_files.'</span>' ?> <?php echo lng('Folder').': <span class="badge badge-light">'.$num_folders.'</span>' ?> <?php echo lng('PartitionSize').': <span class="badge badge-light">'.fm_get_filesize(@disk_free_space($path)) .'</span> '.lng('FreeOf').' <span class="badge badge-light">'.fm_get_filesize(@disk_total_space($path)).'</span>'; ?> </td> </tr> </tfoot> <?php } ?> </table> </div> <div class="row"> <?php if (!FM_READONLY): ?> <div class="col-xs-12 col-sm-9"> <ul class="list-inline footer-action"> <li class="list-inline-item"> <a href="#/select-all" class="btn btn-small btn-outline-primary btn-2" onclick="select_all();return false;"><i class="fa fa-check-square"></i> <?php echo lng('SelectAll') ?> </a></li> <li class="list-inline-item"><a href="#/unselect-all" class="btn btn-small btn-outline-primary btn-2" onclick="unselect_all();return false;"><i class="fa fa-window-close"></i> <?php echo lng('UnSelectAll') ?> </a></li> <li class="list-inline-item"><a href="#/invert-all" class="btn btn-small btn-outline-primary btn-2" onclick="invert_all();return false;"><i class="fa fa-th-list"></i> <?php echo lng('InvertSelection') ?> </a></li> <li class="list-inline-item"><input type="submit" class="hidden" name="delete" id="a-delete" value="Delete" onclick="return confirm('<?php echo lng('Delete selected files and folders?'); ?>')"> <a href="javascript:document.getElementById('a-delete').click();" class="btn btn-small btn-outline-primary btn-2"><i class="fa fa-trash"></i> <?php echo lng('Delete') ?> </a></li> <li class="list-inline-item"><input type="submit" class="hidden" name="zip" id="a-zip" value="zip" onclick="return confirm('<?php echo lng('Create archive?'); ?>')"> <a href="javascript:document.getElementById('a-zip').click();" class="btn btn-small btn-outline-primary btn-2"><i class="fa fa-file-archive-o"></i> <?php echo lng('Zip') ?> </a></li> <li class="list-inline-item"><input type="submit" class="hidden" name="tar" id="a-tar" value="tar" onclick="return confirm('<?php echo lng('Create archive?'); ?>')"> <a href="javascript:document.getElementById('a-tar').click();" class="btn btn-small btn-outline-primary btn-2"><i class="fa fa-file-archive-o"></i> <?php echo lng('Tar') ?> </a></li> <li class="list-inline-item"><input type="submit" class="hidden" name="copy" id="a-copy" value="Copy"> <a href="javascript:document.getElementById('a-copy').click();" class="btn btn-small btn-outline-primary btn-2"><i class="fa fa-files-o"></i> <?php echo lng('Copy') ?> </a></li> </ul> </div> <div class="col-3 d-none d-sm-block"><a href="https://tinyfilemanager.github.io" target="_blank" class="float-right text-muted">Tiny File Manager <?php echo VERSION; ?></a></div> <?php else: ?> <div class="col-12"><a href="https://tinyfilemanager.github.io" target="_blank" class="float-right text-muted">Tiny File Manager <?php echo VERSION; ?></a></div> <?php endif; ?> </div> </form> <?php fm_show_footer(); //--- END // Functions /** * Check if the filename is allowed. * @param string $filename * @return bool */ function fm_is_file_allowed($filename) { // By default, no file is allowed $allowed = false; if (FM_EXTENSION) { $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); if (in_array($ext, explode(',', strtolower(FM_EXTENSION)))) { $allowed = true; } } return $allowed; } /** * Delete file or folder (recursively) * @param string $path * @return bool */ function fm_rdelete($path) { if (is_link($path)) { return unlink($path); } elseif (is_dir($path)) { $objects = scandir($path); $ok = true; if (is_array($objects)) { foreach ($objects as $file) { if ($file != '.' && $file != '..') { if (!fm_rdelete($path . '/' . $file)) { $ok = false; } } } } return ($ok) ? rmdir($path) : false; } elseif (is_file($path)) { return unlink($path); } return false; } /** * Recursive chmod * @param string $path * @param int $filemode * @param int $dirmode * @return bool * @todo Will use in mass chmod */ function fm_rchmod($path, $filemode, $dirmode) { if (is_dir($path)) { if (!chmod($path, $dirmode)) { return false; } $objects = scandir($path); if (is_array($objects)) { foreach ($objects as $file) { if ($file != '.' && $file != '..') { if (!fm_rchmod($path . '/' . $file, $filemode, $dirmode)) { return false; } } } } return true; } elseif (is_link($path)) { return true; } elseif (is_file($path)) { return chmod($path, $filemode); } return false; } /** * Check the file extension which is allowed or not * @param string $filename * @return bool */ function fm_is_valid_ext($filename) { $allowed = (FM_FILE_EXTENSION) ? explode(',', FM_FILE_EXTENSION) : false; $ext = pathinfo($filename, PATHINFO_EXTENSION); $isFileAllowed = ($allowed) ? in_array($ext, $allowed) : true; return ($isFileAllowed) ? true : false; } /** * Safely rename * @param string $old * @param string $new * @return bool|null */ function fm_rename($old, $new) { $isFileAllowed = fm_is_valid_ext($new); if(!$isFileAllowed) return false; return (!file_exists($new) && file_exists($old)) ? rename($old, $new) : null; } /** * Copy file or folder (recursively). * @param string $path * @param string $dest * @param bool $upd Update files * @param bool $force Create folder with same names instead file * @return bool */ function fm_rcopy($path, $dest, $upd = true, $force = true) { if (is_dir($path)) { if (!fm_mkdir($dest, $force)) { return false; } $objects = scandir($path); $ok = true; if (is_array($objects)) { foreach ($objects as $file) { if ($file != '.' && $file != '..') { if (!fm_rcopy($path . '/' . $file, $dest . '/' . $file)) { $ok = false; } } } } return $ok; } elseif (is_file($path)) { return fm_copy($path, $dest, $upd); } return false; } /** * Safely create folder * @param string $dir * @param bool $force * @return bool */ function fm_mkdir($dir, $force) { if (file_exists($dir)) { if (is_dir($dir)) { return $dir; } elseif (!$force) { return false; } unlink($dir); } return mkdir($dir, 0777, true); } /** * Safely copy file * @param string $f1 * @param string $f2 * @param bool $upd Indicates if file should be updated with new content * @return bool */ function fm_copy($f1, $f2, $upd) { $time1 = filemtime($f1); if (file_exists($f2)) { $time2 = filemtime($f2); if ($time2 >= $time1 && $upd) { return false; } } $ok = copy($f1, $f2); if ($ok) { touch($f2, $time1); } return $ok; } /** * Get mime type * @param string $file_path * @return mixed|string */ function fm_get_mime_type($file_path) { if (function_exists('finfo_open')) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $file_path); finfo_close($finfo); return $mime; } elseif (function_exists('mime_content_type')) { return mime_content_type($file_path); } elseif (!stristr(ini_get('disable_functions'), 'shell_exec')) { $file = escapeshellarg($file_path); $mime = shell_exec('file -bi ' . $file); return $mime; } else { return '--'; } } /** * HTTP Redirect * @param string $url * @param int $code */ function fm_redirect($url, $code = 302) { header('Location: ' . $url, true, $code); exit; } /** * Path traversal prevention and clean the url * It replaces (consecutive) occurrences of / and \\ with whatever is in DIRECTORY_SEPARATOR, and processes /. and /.. fine. * @param $path * @return string */ function get_absolute_path($path) { $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); $absolutes = array(); foreach ($parts as $part) { if ('.' == $part) continue; if ('..' == $part) { array_pop($absolutes); } else { $absolutes[] = $part; } } return implode(DIRECTORY_SEPARATOR, $absolutes); } /** * Clean path * @param string $path * @return string */ function fm_clean_path($path, $trim = true) { $path = $trim ? trim($path) : $path; $path = trim($path, '\\/'); $path = str_replace(array('../', '..\\'), '', $path); $path = get_absolute_path($path); if ($path == '..') { $path = ''; } return str_replace('\\', '/', $path); } /** * Get parent path * @param string $path * @return bool|string */ function fm_get_parent_path($path) { $path = fm_clean_path($path); if ($path != '') { $array = explode('/', $path); if (count($array) > 1) { $array = array_slice($array, 0, -1); return implode('/', $array); } return ''; } return false; } /** * Check file is in exclude list * @param string $file * @return bool */ function fm_is_exclude_items($file) { $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); if (isset($exclude_items) and sizeof($exclude_items)) { unset($exclude_items); } $exclude_items = FM_EXCLUDE_ITEMS; if (version_compare(PHP_VERSION, '7.0.0', '<')) { $exclude_items = unserialize($exclude_items); } if (!in_array($file, $exclude_items) && !in_array("*.$ext", $exclude_items)) { return true; } return false; } /** * get language translations from json file * @param int $tr * @return array */ function fm_get_translations($tr) { try { $content = @file_get_contents('translation.json'); if($content !== FALSE) { $lng = json_decode($content, TRUE); global $lang_list; foreach ($lng["language"] as $key => $value) { $code = $value["code"]; $lang_list[$code] = $value["name"]; if ($tr) $tr[$code] = $value["translation"]; } return $tr; } } catch (Exception $e) { echo $e; } } /** * @param $file * Recover all file sizes larger than > 2GB. * Works on php 32bits and 64bits and supports linux * @return int|string */ function fm_get_size($file) { static $iswin; static $isdarwin; if (!isset($iswin)) { $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'); } if (!isset($isdarwin)) { $isdarwin = (strtoupper(substr(PHP_OS, 0)) == "DARWIN"); } static $exec_works; if (!isset($exec_works)) { $exec_works = (function_exists('exec') && !ini_get('safe_mode') && @exec('echo EXEC') == 'EXEC'); } // try a shell command if ($exec_works) { $arg = escapeshellarg($file); $cmd = ($iswin) ? "for %F in (\"$file\") do @echo %~zF" : ($isdarwin ? "stat -f%z $arg" : "stat -c%s $arg"); @exec($cmd, $output); if (is_array($output) && ctype_digit($size = trim(implode("\n", $output)))) { return $size; } } // try the Windows COM interface if ($iswin && class_exists("COM")) { try { $fsobj = new COM('Scripting.FileSystemObject'); $f = $fsobj->GetFile( realpath($file) ); $size = $f->Size; } catch (Exception $e) { $size = null; } if (ctype_digit($size)) { return $size; } } // if all else fails return filesize($file); } /** * Get nice filesize * @param int $size * @return string */ function fm_get_filesize($size) { $size = (float) $size; $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); $power = $size > 0 ? floor(log($size, 1024)) : 0; return sprintf('%s %s', round($size / pow(1024, $power), 2), $units[$power]); } /** * Get director total size * @param string $directory * @return int */ function fm_get_directorysize($directory) { global $calc_folder; if ($calc_folder==true) { // Slower output $size = 0; $count= 0; $dirCount= 0; foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) if ($file->isFile()) { $size+=$file->getSize(); $count++; } else if ($file->isDir()) { $dirCount++; } // return [$size, $count, $dirCount]; return $size; } else return 'Folder'; // Quick output } /** * Get info about zip archive * @param string $path * @return array|bool */ function fm_get_zif_info($path, $ext) { if ($ext == 'zip' && function_exists('zip_open')) { $arch = zip_open($path); if ($arch) { $filenames = array(); while ($zip_entry = zip_read($arch)) { $zip_name = zip_entry_name($zip_entry); $zip_folder = substr($zip_name, -1) == '/'; $filenames[] = array( 'name' => $zip_name, 'filesize' => zip_entry_filesize($zip_entry), 'compressed_size' => zip_entry_compressedsize($zip_entry), 'folder' => $zip_folder //'compression_method' => zip_entry_compressionmethod($zip_entry), ); } zip_close($arch); return $filenames; } } elseif($ext == 'tar' && class_exists('PharData')) { $archive = new PharData($path); $filenames = array(); foreach(new RecursiveIteratorIterator($archive) as $file) { $parent_info = $file->getPathInfo(); $zip_name = str_replace("phar://".$path, '', $file->getPathName()); $zip_name = substr($zip_name, ($pos = strpos($zip_name, '/')) !== false ? $pos + 1 : 0); $zip_folder = $parent_info->getFileName(); $zip_info = new SplFileInfo($file); $filenames[] = array( 'name' => $zip_name, 'filesize' => $zip_info->getSize(), 'compressed_size' => $file->getCompressedSize(), 'folder' => $zip_folder ); } return $filenames; } return false; } /** * Encode html entities * @param string $text * @return string */ function fm_enc($text) { return htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); } /** * Prevent XSS attacks * @param string $text * @return string */ function fm_isvalid_filename($text) { return (strpbrk($text, '/?%*:|"<>') === FALSE) ? true : false; } /** * Save message in session * @param string $msg * @param string $status */ function fm_set_msg($msg, $status = 'ok') { $_SESSION[FM_SESSION_ID]['message'] = $msg; $_SESSION[FM_SESSION_ID]['status'] = $status; } /** * Check if string is in UTF-8 * @param string $string * @return int */ function fm_is_utf8($string) { return preg_match('//u', $string); } /** * Convert file name to UTF-8 in Windows * @param string $filename * @return string */ function fm_convert_win($filename) { if (FM_IS_WIN && function_exists('iconv')) { $filename = iconv(FM_ICONV_INPUT_ENC, 'UTF-8//IGNORE', $filename); } return $filename; } /** * @param $obj * @return array */ function fm_object_to_array($obj) { if (!is_object($obj) && !is_array($obj)) { return $obj; } if (is_object($obj)) { $obj = get_object_vars($obj); } return array_map('fm_object_to_array', $obj); } /** * Get CSS classname for file * @param string $path * @return string */ function fm_get_file_icon_class($path) { // get extension $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION)); switch ($ext) { case 'ico': case 'gif': case 'jpg': case 'jpeg': case 'jpc': case 'jp2': case 'jpx': case 'xbm': case 'wbmp': case 'png': case 'bmp': case 'tif': case 'tiff': case 'webp': case 'avif': case 'svg': $img = 'fa fa-picture-o'; break; case 'passwd': case 'ftpquota': case 'sql': case 'js': case 'json': case 'sh': case 'config': case 'twig': case 'tpl': case 'md': case 'gitignore': case 'c': case 'cpp': case 'cs': case 'py': case 'rs': case 'map': case 'lock': case 'dtd': $img = 'fa fa-file-code-o'; break; case 'txt': case 'ini': case 'conf': case 'log': case 'htaccess': $img = 'fa fa-file-text-o'; break; case 'css': case 'less': case 'sass': case 'scss': $img = 'fa fa-css3'; break; case 'bz2': case 'zip': case 'rar': case 'gz': case 'tar': case '7z': case 'xz': $img = 'fa fa-file-archive-o'; break; case 'php': case 'php4': case 'php5': case 'phps': case 'phtml': $img = 'fa fa-code'; break; case 'htm': case 'html': case 'shtml': case 'xhtml': $img = 'fa fa-html5'; break; case 'xml': case 'xsl': $img = 'fa fa-file-excel-o'; break; case 'wav': case 'mp3': case 'mp2': case 'm4a': case 'aac': case 'ogg': case 'oga': case 'wma': case 'mka': case 'flac': case 'ac3': case 'tds': $img = 'fa fa-music'; break; case 'm3u': case 'm3u8': case 'pls': case 'cue': case 'xspf': $img = 'fa fa-headphones'; break; case 'avi': case 'mpg': case 'mpeg': case 'mp4': case 'm4v': case 'flv': case 'f4v': case 'ogm': case 'ogv': case 'mov': case 'mkv': case '3gp': case 'asf': case 'wmv': $img = 'fa fa-file-video-o'; break; case 'eml': case 'msg': $img = 'fa fa-envelope-o'; break; case 'xls': case 'xlsx': case 'ods': $img = 'fa fa-file-excel-o'; break; case 'csv': $img = 'fa fa-file-text-o'; break; case 'bak': case 'swp': $img = 'fa fa-clipboard'; break; case 'doc': case 'docx': case 'odt': $img = 'fa fa-file-word-o'; break; case 'ppt': case 'pptx': $img = 'fa fa-file-powerpoint-o'; break; case 'ttf': case 'ttc': case 'otf': case 'woff': case 'woff2': case 'eot': case 'fon': $img = 'fa fa-font'; break; case 'pdf': $img = 'fa fa-file-pdf-o'; break; case 'psd': case 'ai': case 'eps': case 'fla': case 'swf': $img = 'fa fa-file-image-o'; break; case 'exe': case 'msi': $img = 'fa fa-file-o'; break; case 'bat': $img = 'fa fa-terminal'; break; default: $img = 'fa fa-info-circle'; } return $img; } /** * Get image files extensions * @return array */ function fm_get_image_exts() { return array('ico', 'gif', 'jpg', 'jpeg', 'jpc', 'jp2', 'jpx', 'xbm', 'wbmp', 'png', 'bmp', 'tif', 'tiff', 'psd', 'svg', 'webp', 'avif'); } /** * Get video files extensions * @return array */ function fm_get_video_exts() { return array('avi', 'webm', 'wmv', 'mp4', 'm4v', 'ogm', 'ogv', 'mov', 'mkv'); } /** * Get audio files extensions * @return array */ function fm_get_audio_exts() { return array('wav', 'mp3', 'ogg', 'm4a'); } /** * Get text file extensions * @return array */ function fm_get_text_exts() { return array( 'txt', 'css', 'ini', 'conf', 'log', 'htaccess', 'passwd', 'ftpquota', 'sql', 'js', 'json', 'sh', 'config', 'php', 'php4', 'php5', 'phps', 'phtml', 'htm', 'html', 'shtml', 'xhtml', 'xml', 'xsl', 'm3u', 'm3u8', 'pls', 'cue', 'eml', 'msg', 'csv', 'bat', 'twig', 'tpl', 'md', 'gitignore', 'less', 'sass', 'scss', 'c', 'cpp', 'cs', 'py', 'map', 'lock', 'dtd', 'svg', 'scss', 'asp', 'aspx', 'asx', 'asmx', 'ashx', 'jsx', 'jsp', 'jspx', 'cfm', 'cgi' ); } /** * Get mime types of text files * @return array */ function fm_get_text_mimes() { return array( 'application/xml', 'application/javascript', 'application/x-javascript', 'image/svg+xml', 'message/rfc822', ); } /** * Get file names of text files w/o extensions * @return array */ function fm_get_text_names() { return array( 'license', 'readme', 'authors', 'contributors', 'changelog', ); } /** * Get online docs viewer supported files extensions * @return array */ function fm_get_onlineViewer_exts() { return array('doc', 'docx', 'xls', 'xlsx', 'pdf', 'ppt', 'pptx', 'ai', 'psd', 'dxf', 'xps', 'rar', 'odt', 'ods'); } function fm_get_file_mimes($extension) { $fileTypes['swf'] = 'application/x-shockwave-flash'; $fileTypes['pdf'] = 'application/pdf'; $fileTypes['exe'] = 'application/octet-stream'; $fileTypes['zip'] = 'application/zip'; $fileTypes['doc'] = 'application/msword'; $fileTypes['xls'] = 'application/vnd.ms-excel'; $fileTypes['ppt'] = 'application/vnd.ms-powerpoint'; $fileTypes['gif'] = 'image/gif'; $fileTypes['png'] = 'image/png'; $fileTypes['jpeg'] = 'image/jpg'; $fileTypes['jpg'] = 'image/jpg'; $fileTypes['webp'] = 'image/webp'; $fileTypes['avif'] = 'image/avif'; $fileTypes['rar'] = 'application/rar'; $fileTypes['ra'] = 'audio/x-pn-realaudio'; $fileTypes['ram'] = 'audio/x-pn-realaudio'; $fileTypes['ogg'] = 'audio/x-pn-realaudio'; $fileTypes['wav'] = 'video/x-msvideo'; $fileTypes['wmv'] = 'video/x-msvideo'; $fileTypes['avi'] = 'video/x-msvideo'; $fileTypes['asf'] = 'video/x-msvideo'; $fileTypes['divx'] = 'video/x-msvideo'; $fileTypes['mp3'] = 'audio/mpeg'; $fileTypes['mp4'] = 'audio/mpeg'; $fileTypes['mpeg'] = 'video/mpeg'; $fileTypes['mpg'] = 'video/mpeg'; $fileTypes['mpe'] = 'video/mpeg'; $fileTypes['mov'] = 'video/quicktime'; $fileTypes['swf'] = 'video/quicktime'; $fileTypes['3gp'] = 'video/quicktime'; $fileTypes['m4a'] = 'video/quicktime'; $fileTypes['aac'] = 'video/quicktime'; $fileTypes['m3u'] = 'video/quicktime'; $fileTypes['php'] = ['application/x-php']; $fileTypes['html'] = ['text/html']; $fileTypes['txt'] = ['text/plain']; //Unknown mime-types should be 'application/octet-stream' if(empty($fileTypes[$extension])) { $fileTypes[$extension] = ['application/octet-stream']; } return $fileTypes[$extension]; } /** * This function scans the files and folder recursively, and return matching files * @param string $dir * @param string $filter * @return json */ function scan($dir, $filter = '') { $path = FM_ROOT_PATH.'/'.$dir; if($dir) { $ite = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); $rii = new RegexIterator($ite, "/(" . $filter . ")/i"); $files = array(); foreach ($rii as $file) { if (!$file->isDir()) { $fileName = $file->getFilename(); $location = str_replace(FM_ROOT_PATH, '', $file->getPath()); $files[] = array( "name" => $fileName, "type" => "file", "path" => $location, ); } } return $files; } } /* Parameters: downloadFile(File Location, File Name, max speed, is streaming If streaming - videos will show as videos, images as images instead of download prompt https://stackoverflow.com/a/13821992/1164642 */ function fm_download_file($fileLocation, $fileName, $chunkSize = 1024) { if (connection_status() != 0) return (false); $extension = pathinfo($fileName, PATHINFO_EXTENSION); $contentType = fm_get_file_mimes($extension); header("Cache-Control: public"); header("Content-Transfer-Encoding: binary\n"); header('Content-Type: $contentType'); $contentDisposition = 'attachment'; if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) { $fileName = preg_replace('/\./', '%2e', $fileName, substr_count($fileName, '.') - 1); header("Content-Disposition: $contentDisposition;filename=\"$fileName\""); } else { header("Content-Disposition: $contentDisposition;filename=\"$fileName\""); } header("Accept-Ranges: bytes"); $range = 0; $size = filesize($fileLocation); if (isset($_SERVER['HTTP_RANGE'])) { list($a, $range) = explode("=", $_SERVER['HTTP_RANGE']); str_replace($range, "-", $range); $size2 = $size - 1; $new_length = $size - $range; header("HTTP/1.1 206 Partial Content"); header("Content-Length: $new_length"); header("Content-Range: bytes $range$size2/$size"); } else { $size2 = $size - 1; header("Content-Range: bytes 0-$size2/$size"); header("Content-Length: " . $size); } if ($size == 0) { die('Zero byte file! Aborting download'); } @ini_set('magic_quotes_runtime', 0); $fp = fopen("$fileLocation", "rb"); fseek($fp, $range); while (!feof($fp) and (connection_status() == 0)) { set_time_limit(0); print(@fread($fp, 1024*$chunkSize)); flush(); ob_flush(); // sleep(1); } fclose($fp); return ((connection_status() == 0) and !connection_aborted()); } function fm_get_theme() { $result = ''; if(FM_THEME == "dark") { $result = "text-white bg-dark"; } return $result; } /** * Class to work with zip files (using ZipArchive) */ class FM_Zipper { private $zip; public function __construct() { $this->zip = new ZipArchive(); } /** * Create archive with name $filename and files $files (RELATIVE PATHS!) * @param string $filename * @param array|string $files * @return bool */ public function create($filename, $files) { $res = $this->zip->open($filename, ZipArchive::CREATE); if ($res !== true) { return false; } if (is_array($files)) { foreach ($files as $f) { if (!$this->addFileOrDir($f)) { $this->zip->close(); return false; } } $this->zip->close(); return true; } else { if ($this->addFileOrDir($files)) { $this->zip->close(); return true; } return false; } } /** * Extract archive $filename to folder $path (RELATIVE OR ABSOLUTE PATHS) * @param string $filename * @param string $path * @return bool */ public function unzip($filename, $path) { $res = $this->zip->open($filename); if ($res !== true) { return false; } if ($this->zip->extractTo($path)) { $this->zip->close(); return true; } return false; } /** * Add file/folder to archive * @param string $filename * @return bool */ private function addFileOrDir($filename) { if (is_file($filename)) { return $this->zip->addFile($filename); } elseif (is_dir($filename)) { return $this->addDir($filename); } return false; } /** * Add folder recursively * @param string $path * @return bool */ private function addDir($path) { if (!$this->zip->addEmptyDir($path)) { return false; } $objects = scandir($path); if (is_array($objects)) { foreach ($objects as $file) { if ($file != '.' && $file != '..') { if (is_dir($path . '/' . $file)) { if (!$this->addDir($path . '/' . $file)) { return false; } } elseif (is_file($path . '/' . $file)) { if (!$this->zip->addFile($path . '/' . $file)) { return false; } } } } return true; } return false; } } /** * Class to work with Tar files (using PharData) */ class FM_Zipper_Tar { private $tar; public function __construct() { $this->tar = null; } /** * Create archive with name $filename and files $files (RELATIVE PATHS!) * @param string $filename * @param array|string $files * @return bool */ public function create($filename, $files) { $this->tar = new PharData($filename); if (is_array($files)) { foreach ($files as $f) { if (!$this->addFileOrDir($f)) { return false; } } return true; } else { if ($this->addFileOrDir($files)) { return true; } return false; } } /** * Extract archive $filename to folder $path (RELATIVE OR ABSOLUTE PATHS) * @param string $filename * @param string $path * @return bool */ public function unzip($filename, $path) { $res = $this->tar->open($filename); if ($res !== true) { return false; } if ($this->tar->extractTo($path)) { return true; } return false; } /** * Add file/folder to archive * @param string $filename * @return bool */ private function addFileOrDir($filename) { if (is_file($filename)) { try { $this->tar->addFile($filename); return true; } catch (Exception $e) { return false; } } elseif (is_dir($filename)) { return $this->addDir($filename); } return false; } /** * Add folder recursively * @param string $path * @return bool */ private function addDir($path) { $objects = scandir($path); if (is_array($objects)) { foreach ($objects as $file) { if ($file != '.' && $file != '..') { if (is_dir($path . '/' . $file)) { if (!$this->addDir($path . '/' . $file)) { return false; } } elseif (is_file($path . '/' . $file)) { try { $this->tar->addFile($path . '/' . $file); } catch (Exception $e) { return false; } } } } return true; } return false; } } /** * Save Configuration */ class FM_Config { var $data; function __construct() { global $root_path, $root_url, $CONFIG; $fm_url = $root_url.$_SERVER["PHP_SELF"]; $this->data = array( 'lang' => 'en', 'error_reporting' => true, 'show_hidden' => true ); $data = false; if (strlen($CONFIG)) { $data = fm_object_to_array(json_decode($CONFIG)); } else { $msg = 'Tiny File Manager<br>Error: Cannot load configuration'; if (substr($fm_url, -1) == '/') { $fm_url = rtrim($fm_url, '/'); $msg .= '<br>'; $msg .= '<br>Seems like you have a trailing slash on the URL.'; $msg .= '<br>Try this link: <a href="' . $fm_url . '">' . $fm_url . '</a>'; } die($msg); } if (is_array($data) && count($data)) $this->data = $data; else $this->save(); } function save() { $fm_file = __FILE__; $var_name = '$CONFIG'; $var_value = var_export(json_encode($this->data), true); $config_string = "<?php" . chr(13) . chr(10) . "//Default Configuration".chr(13) . chr(10)."$var_name = $var_value;" . chr(13) . chr(10); if (is_writable($fm_file)) { $lines = file($fm_file); if ($fh = @fopen($fm_file, "w")) { @fputs($fh, $config_string, strlen($config_string)); for ($x = 3; $x < count($lines); $x++) { @fputs($fh, $lines[$x], strlen($lines[$x])); } @fclose($fh); } } } } //--- templates functions /** * Show nav block * @param string $path */ function fm_show_nav_path($path) { global $lang, $sticky_navbar; $isStickyNavBar = $sticky_navbar ? 'fixed-top' : ''; $getTheme = fm_get_theme(); $getTheme .= " navbar-light"; if(FM_THEME == "dark") { $getTheme .= " navbar-dark"; } else { $getTheme .= " bg-white"; } ?> <nav class="navbar navbar-expand-lg <?php echo $getTheme; ?> mb-4 main-nav <?php echo $isStickyNavBar ?>"> <a class="navbar-brand" href=""> <?php echo lng('AppTitle') ?> </a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <?php $path = fm_clean_path($path); $root_url = "<a href='?p='><i class='fa fa-home' aria-hidden='true' title='" . FM_ROOT_PATH . "'></i></a>"; $sep = '<i class="bread-crumb"> / </i>'; if ($path != '') { $exploded = explode('/', $path); $count = count($exploded); $array = array(); $parent = ''; for ($i = 0; $i < $count; $i++) { $parent = trim($parent . '/' . $exploded[$i], '/'); $parent_enc = urlencode($parent); $array[] = "<a href='?p={$parent_enc}'>" . fm_enc(fm_convert_win($exploded[$i])) . "</a>"; } $root_url .= $sep . implode($sep, $array); } echo '<div class="col-xs-6 col-sm-5">' . $root_url . '</div>'; ?> <div class="col-xs-6 col-sm-7 text-right"> <ul class="navbar-nav mr-auto float-right <?php echo fm_get_theme(); ?>"> <li class="nav-item mr-2"> <div class="input-group input-group-sm mr-1" style="margin-top:4px;"> <input type="text" class="form-control" placeholder="<?php echo lng('Search') ?>" aria-label="<?php echo lng('Search') ?>" aria-describedby="search-addon2" id="search-addon"> <div class="input-group-append"> <span class="input-group-text" id="search-addon2"><i class="fa fa-search"></i></span> </div> <div class="input-group-append btn-group"> <span class="input-group-text dropdown-toggle" id="search-addon2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span> <div class="dropdown-menu dropdown-menu-right"> <a class="dropdown-item" href="<?php echo $path2 = $path ? $path : '.'; ?>" id="js-search-modal" data-toggle="modal" data-target="#searchModal"><?php echo lng('Advanced Search') ?></a> </div> </div> </div> </li> <?php if (!FM_READONLY): ?> <li class="nav-item"> <a title="<?php echo lng('Upload') ?>" class="nav-link" href="?p=<?php echo urlencode(FM_PATH) ?>&upload"><i class="fa fa-cloud-upload" aria-hidden="true"></i> <?php echo lng('Upload') ?></a> </li> <li class="nav-item"> <a title="<?php echo lng('NewItem') ?>" class="nav-link" href="#createNewItem" data-toggle="modal" data-target="#createNewItem"><i class="fa fa-plus-square"></i> <?php echo lng('NewItem') ?></a> </li> <?php endif; ?> <?php if (FM_USE_AUTH): ?> <li class="nav-item avatar dropdown"> <a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink-5" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <i class="fa fa-user-circle"></i> <?php if(isset($_SESSION[FM_SESSION_ID]['logged'])) { echo $_SESSION[FM_SESSION_ID]['logged']; } ?></a> <div class="dropdown-menu dropdown-menu-right <?php echo fm_get_theme(); ?>" aria-labelledby="navbarDropdownMenuLink-5"> <?php if (!FM_READONLY): ?> <a title="<?php echo lng('Settings') ?>" class="dropdown-item nav-link" href="?p=<?php echo urlencode(FM_PATH) ?>&settings=1"><i class="fa fa-cog" aria-hidden="true"></i> <?php echo lng('Settings') ?></a> <?php endif ?> <a title="<?php echo lng('Help') ?>" class="dropdown-item nav-link" href="?p=<?php echo urlencode(FM_PATH) ?>&help=2"><i class="fa fa-exclamation-circle" aria-hidden="true"></i> <?php echo lng('Help') ?></a> <a title="<?php echo lng('Logout') ?>" class="dropdown-item nav-link" href="?logout=1"><i class="fa fa-sign-out" aria-hidden="true"></i> <?php echo lng('Logout') ?></a> </div> </li> <?php else: ?> <?php if (!FM_READONLY): ?> <li class="nav-item"> <a title="<?php echo lng('Settings') ?>" class="dropdown-item nav-link" href="?p=<?php echo urlencode(FM_PATH) ?>&settings=1"><i class="fa fa-cog" aria-hidden="true"></i> <?php echo lng('Settings') ?></a> </li> <?php endif; ?> <?php endif; ?> </ul> </div> </div> </nav> <?php } /** * Show message from session */ function fm_show_message() { if (isset($_SESSION[FM_SESSION_ID]['message'])) { $class = isset($_SESSION[FM_SESSION_ID]['status']) ? $_SESSION[FM_SESSION_ID]['status'] : 'ok'; echo '<p class="message ' . $class . '">' . $_SESSION[FM_SESSION_ID]['message'] . '</p>'; unset($_SESSION[FM_SESSION_ID]['message']); unset($_SESSION[FM_SESSION_ID]['status']); } } /** * Show page header in Login Form */ function fm_show_header_login() { $sprites_ver = '20160315'; header("Content-Type: text/html; charset=utf-8"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); header("Pragma: no-cache"); global $lang, $root_url, $favicon_path; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content="Web based File Manager in PHP, Manage your files efficiently and easily with Tiny File Manager"> <meta name="author" content="CCP Programmers"> <meta name="robots" content="noindex, nofollow"> <meta name="googlebot" content="noindex"> <?php if($favicon_path) { echo '<link rel="icon" href="'.fm_enc($favicon_path).'" type="image/png">'; } ?> <title><?php echo fm_enc(APP_TITLE) ?></title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <style> body.fm-login-page{ background-color:#f7f9fb;font-size:14px;background-color:#f7f9fb;background-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 304 304' width='304' height='304'%3E%3Cpath fill='%23e2e9f1' fill-opacity='0.4' d='M44.1 224a5 5 0 1 1 0 2H0v-2h44.1zm160 48a5 5 0 1 1 0 2H82v-2h122.1zm57.8-46a5 5 0 1 1 0-2H304v2h-42.1zm0 16a5 5 0 1 1 0-2H304v2h-42.1zm6.2-114a5 5 0 1 1 0 2h-86.2a5 5 0 1 1 0-2h86.2zm-256-48a5 5 0 1 1 0 2H0v-2h12.1zm185.8 34a5 5 0 1 1 0-2h86.2a5 5 0 1 1 0 2h-86.2zM258 12.1a5 5 0 1 1-2 0V0h2v12.1zm-64 208a5 5 0 1 1-2 0v-54.2a5 5 0 1 1 2 0v54.2zm48-198.2V80h62v2h-64V21.9a5 5 0 1 1 2 0zm16 16V64h46v2h-48V37.9a5 5 0 1 1 2 0zm-128 96V208h16v12.1a5 5 0 1 1-2 0V210h-16v-76.1a5 5 0 1 1 2 0zm-5.9-21.9a5 5 0 1 1 0 2H114v48H85.9a5 5 0 1 1 0-2H112v-48h12.1zm-6.2 130a5 5 0 1 1 0-2H176v-74.1a5 5 0 1 1 2 0V242h-60.1zm-16-64a5 5 0 1 1 0-2H114v48h10.1a5 5 0 1 1 0 2H112v-48h-10.1zM66 284.1a5 5 0 1 1-2 0V274H50v30h-2v-32h18v12.1zM236.1 176a5 5 0 1 1 0 2H226v94h48v32h-2v-30h-48v-98h12.1zm25.8-30a5 5 0 1 1 0-2H274v44.1a5 5 0 1 1-2 0V146h-10.1zm-64 96a5 5 0 1 1 0-2H208v-80h16v-14h-42.1a5 5 0 1 1 0-2H226v18h-16v80h-12.1zm86.2-210a5 5 0 1 1 0 2H272V0h2v32h10.1zM98 101.9V146H53.9a5 5 0 1 1 0-2H96v-42.1a5 5 0 1 1 2 0zM53.9 34a5 5 0 1 1 0-2H80V0h2v34H53.9zm60.1 3.9V66H82v64H69.9a5 5 0 1 1 0-2H80V64h32V37.9a5 5 0 1 1 2 0zM101.9 82a5 5 0 1 1 0-2H128V37.9a5 5 0 1 1 2 0V82h-28.1zm16-64a5 5 0 1 1 0-2H146v44.1a5 5 0 1 1-2 0V18h-26.1zm102.2 270a5 5 0 1 1 0 2H98v14h-2v-16h124.1zM242 149.9V160h16v34h-16v62h48v48h-2v-46h-48v-66h16v-30h-16v-12.1a5 5 0 1 1 2 0zM53.9 18a5 5 0 1 1 0-2H64V2H48V0h18v18H53.9zm112 32a5 5 0 1 1 0-2H192V0h50v2h-48v48h-28.1zm-48-48a5 5 0 0 1-9.8-2h2.07a3 3 0 1 0 5.66 0H178v34h-18V21.9a5 5 0 1 1 2 0V32h14V2h-58.1zm0 96a5 5 0 1 1 0-2H137l32-32h39V21.9a5 5 0 1 1 2 0V66h-40.17l-32 32H117.9zm28.1 90.1a5 5 0 1 1-2 0v-76.51L175.59 80H224V21.9a5 5 0 1 1 2 0V82h-49.59L146 112.41v75.69zm16 32a5 5 0 1 1-2 0v-99.51L184.59 96H300.1a5 5 0 0 1 3.9-3.9v2.07a3 3 0 0 0 0 5.66v2.07a5 5 0 0 1-3.9-3.9H185.41L162 121.41v98.69zm-144-64a5 5 0 1 1-2 0v-3.51l48-48V48h32V0h2v50H66v55.41l-48 48v2.69zM50 53.9v43.51l-48 48V208h26.1a5 5 0 1 1 0 2H0v-65.41l48-48V53.9a5 5 0 1 1 2 0zm-16 16V89.41l-34 34v-2.82l32-32V69.9a5 5 0 1 1 2 0zM12.1 32a5 5 0 1 1 0 2H9.41L0 43.41V40.6L8.59 32h3.51zm265.8 18a5 5 0 1 1 0-2h18.69l7.41-7.41v2.82L297.41 50H277.9zm-16 160a5 5 0 1 1 0-2H288v-71.41l16-16v2.82l-14 14V210h-28.1zm-208 32a5 5 0 1 1 0-2H64v-22.59L40.59 194H21.9a5 5 0 1 1 0-2H41.41L66 216.59V242H53.9zm150.2 14a5 5 0 1 1 0 2H96v-56.6L56.6 162H37.9a5 5 0 1 1 0-2h19.5L98 200.6V256h106.1zm-150.2 2a5 5 0 1 1 0-2H80v-46.59L48.59 178H21.9a5 5 0 1 1 0-2H49.41L82 208.59V258H53.9zM34 39.8v1.61L9.41 66H0v-2h8.59L32 40.59V0h2v39.8zM2 300.1a5 5 0 0 1 3.9 3.9H3.83A3 3 0 0 0 0 302.17V256h18v48h-2v-46H2v42.1zM34 241v63h-2v-62H0v-2h34v1zM17 18H0v-2h16V0h2v18h-1zm273-2h14v2h-16V0h2v16zm-32 273v15h-2v-14h-14v14h-2v-16h18v1zM0 92.1A5.02 5.02 0 0 1 6 97a5 5 0 0 1-6 4.9v-2.07a3 3 0 1 0 0-5.66V92.1zM80 272h2v32h-2v-32zm37.9 32h-2.07a3 3 0 0 0-5.66 0h-2.07a5 5 0 0 1 9.8 0zM5.9 0A5.02 5.02 0 0 1 0 5.9V3.83A3 3 0 0 0 3.83 0H5.9zm294.2 0h2.07A3 3 0 0 0 304 3.83V5.9a5 5 0 0 1-3.9-5.9zm3.9 300.1v2.07a3 3 0 0 0-1.83 1.83h-2.07a5 5 0 0 1 3.9-3.9zM97 100a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0-16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-48 32a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm32 48a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-16 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm32-16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0-32a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16 32a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm32 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0-16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-16-64a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16 0a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16 96a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16-144a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0 32a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16-32a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16-16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-96 0a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16-32a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm96 0a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-16-64a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16-16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-32 0a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0-16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-16 0a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-16 0a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-16 0a3 3 0 1 0 0-6 3 3 0 0 0 0 6zM49 36a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-32 0a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm32 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zM33 68a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16-48a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0 240a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16 32a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-16-64a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-16-32a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm80-176a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16 0a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-16-16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm32 48a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16-16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0-32a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm112 176a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-16 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zM17 180a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0 16a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm0-32a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16 0a3 3 0 1 0 0-6 3 3 0 0 0 0 6zM17 84a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm32 64a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm16-16a3 3 0 1 0 0-6 3 3 0 0 0 0 6z'%3E%3C/path%3E%3C/svg%3E");} .fm-login-page .brand{ width:121px;overflow:hidden;margin:0 auto;position:relative;z-index:1} .fm-login-page .brand img{ width:100%} .fm-login-page .card-wrapper{ width:360px;margin-top:10%;margin-left:auto;margin-right:auto;} .fm-login-page .card{ border-color:transparent;box-shadow:0 4px 8px rgba(0,0,0,.05)} .fm-login-page .card-title{ margin-bottom:1.5rem;font-size:24px;font-weight:400;} .fm-login-page .form-control{ border-width:2.3px} .fm-login-page .form-group label{ width:100%} .fm-login-page .btn.btn-block{ padding:12px 10px} .fm-login-page .footer{ margin:40px 0;color:#888;text-align:center} @media screen and (max-width:425px){ .fm-login-page .card-wrapper{ width:90%;margin:0 auto;margin-top:10%;} } @media screen and (max-width:320px){ .fm-login-page .card.fat{ padding:0} .fm-login-page .card.fat .card-body{ padding:15px} } .message{ padding:4px 7px;border:1px solid #ddd;background-color:#fff} .message.ok{ border-color:green;color:green} .message.error{ border-color:red;color:red} .message.alert{ border-color:orange;color:orange} body.fm-login-page.theme-dark {background-color: #2f2a2a;} .theme-dark svg g, .theme-dark svg path {fill: #ffffff; } </style> </head> <body class="fm-login-page <?php echo (FM_THEME == "dark") ? 'theme-dark' : ''; ?>"> <div id="wrapper" class="container-fluid"> <?php } /** * Show page footer in Login Form */ function fm_show_footer_login() { ?> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script> </body> </html> <?php } /** * Show Header after login */ function fm_show_header() { $sprites_ver = '20160315'; header("Content-Type: text/html; charset=utf-8"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); header("Pragma: no-cache"); global $lang, $root_url, $sticky_navbar, $favicon_path; $isStickyNavBar = $sticky_navbar ? 'navbar-fixed' : 'navbar-normal'; ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content="Web based File Manager in PHP, Manage your files efficiently and easily with Tiny File Manager"> <meta name="author" content="CCP Programmers"> <meta name="robots" content="noindex, nofollow"> <meta name="googlebot" content="noindex"> <?php if($favicon_path) { echo '<link rel="icon" href="'.fm_enc($favicon_path).'" type="image/png">'; } ?> <title><?php echo fm_enc(APP_TITLE) ?></title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ekko-lightbox/5.3.0/ekko-lightbox.css" /> <?php if (FM_USE_HIGHLIGHTJS): ?> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.6.0/styles/<?php echo FM_HIGHLIGHTJS_STYLE ?>.min.css"> <?php endif; ?> <style> body { font-size:14px;color:#222;background:#F7F7F7; } body.navbar-fixed { margin-top:55px; } a:hover, a:visited, a:focus { text-decoration:none !important; } * { -webkit-border-radius:0 !important;-moz-border-radius:0 !important;border-radius:0 !important; } .filename, td, th { white-space:nowrap } .navbar-brand { font-weight:bold; } .nav-item.avatar a { cursor:pointer;text-transform:capitalize; } .nav-item.avatar a > i { font-size:15px; } .nav-item.avatar .dropdown-menu a { font-size:13px; } #search-addon { font-size:12px;border-right-width:0; } #search-addon2 { background:transparent;border-left:0; } .bread-crumb { color:#cccccc;font-style:normal; } #main-table .filename a { color:#222222; } .table td, .table th { vertical-align:middle !important; } .table .custom-checkbox-td .custom-control.custom-checkbox, .table .custom-checkbox-header .custom-control.custom-checkbox { min-width:18px; } .table-sm td, .table-sm th { padding:.4rem; } .table-bordered td, .table-bordered th { border:1px solid #f1f1f1; } .hidden { display:none } pre.with-hljs { padding:0 } pre.with-hljs code { margin:0;border:0;overflow:visible } code.maxheight, pre.maxheight { max-height:512px } .fa.fa-caret-right { font-size:1.2em;margin:0 4px;vertical-align:middle;color:#ececec } .fa.fa-home { font-size:1.3em;vertical-align:bottom } .path { margin-bottom:10px } form.dropzone { min-height:200px;border:2px dashed #007bff;line-height:6rem; } .right { text-align:right } .center, .close, .login-form { text-align:center } .message { padding:4px 7px;border:1px solid #ddd;background-color:#fff } .message.ok { border-color:green;color:green } .message.error { border-color:red;color:red } .message.alert { border-color:orange;color:orange } .preview-img { max-width:100%;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAKklEQVR42mL5//8/Azbw+PFjrOJMDCSCUQ3EABZc4S0rKzsaSvTTABBgAMyfCMsY4B9iAAAAAElFTkSuQmCC) } .inline-actions > a > i { font-size:1em;margin-left:5px;background:#3785c1;color:#fff;padding:3px;border-radius:3px } .preview-video { position:relative;max-width:100%;height:0;padding-bottom:62.5%;margin-bottom:10px } .preview-video video { position:absolute;width:100%;height:100%;left:0;top:0;background:#000 } .compact-table { border:0;width:auto } .compact-table td, .compact-table th { width:100px;border:0;text-align:center } .compact-table tr:hover td { background-color:#fff } .filename { max-width:420px;overflow:hidden;text-overflow:ellipsis } .break-word { word-wrap:break-word;margin-left:30px } .break-word.float-left a { color:#7d7d7d } .break-word + .float-right { padding-right:30px;position:relative } .break-word + .float-right > a { color:#7d7d7d;font-size:1.2em;margin-right:4px } #editor { position:absolute;right:15px;top:100px;bottom:15px;left:15px } @media (max-width:481px) { #editor { top:150px; } } #normal-editor { border-radius:3px;border-width:2px;padding:10px;outline:none; } .btn-2 { border-radius:0;padding:3px 6px;font-size:small; } li.file:before,li.folder:before { font:normal normal normal 14px/1 FontAwesome;content:"\f016";margin-right:5px } li.folder:before { content:"\f114" } i.fa.fa-folder-o { color:#0157b3 } i.fa.fa-picture-o { color:#26b99a } i.fa.fa-file-archive-o { color:#da7d7d } .btn-2 i.fa.fa-file-archive-o { color:inherit } i.fa.fa-css3 { color:#f36fa0 } i.fa.fa-file-code-o { color:#007bff } i.fa.fa-code { color:#cc4b4c } i.fa.fa-file-text-o { color:#0096e6 } i.fa.fa-html5 { color:#d75e72 } i.fa.fa-file-excel-o { color:#09c55d } i.fa.fa-file-powerpoint-o { color:#f6712e } i.go-back { font-size:1.2em;color:#007bff; } .main-nav { padding:0.2rem 1rem;box-shadow:0 4px 5px 0 rgba(0, 0, 0, .14), 0 1px 10px 0 rgba(0, 0, 0, .12), 0 2px 4px -1px rgba(0, 0, 0, .2) } .dataTables_filter { display:none; } table.dataTable thead .sorting { cursor:pointer;background-repeat:no-repeat;background-position:center right;background-image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABMAAAATCAQAAADYWf5HAAAAkElEQVQoz7XQMQ5AQBCF4dWQSJxC5wwax1Cq1e7BAdxD5SL+Tq/QCM1oNiJidwox0355mXnG/DrEtIQ6azioNZQxI0ykPhTQIwhCR+BmBYtlK7kLJYwWCcJA9M4qdrZrd8pPjZWPtOqdRQy320YSV17OatFC4euts6z39GYMKRPCTKY9UnPQ6P+GtMRfGtPnBCiqhAeJPmkqAAAAAElFTkSuQmCC'); } table.dataTable thead .sorting_asc { cursor:pointer;background-repeat:no-repeat;background-position:center right;background-image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABMAAAATCAYAAAByUDbMAAAAZ0lEQVQ4y2NgGLKgquEuFxBPAGI2ahhWCsS/gDibUoO0gPgxEP8H4ttArEyuQYxAPBdqEAxPBImTY5gjEL9DM+wTENuQahAvEO9DMwiGdwAxOymGJQLxTyD+jgWDxCMZRsEoGAVoAADeemwtPcZI2wAAAABJRU5ErkJggg=='); } table.dataTable thead .sorting_desc { cursor:pointer;background-repeat:no-repeat;background-position:center right;background-image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABMAAAATCAYAAAByUDbMAAAAZUlEQVQ4y2NgGAWjYBSggaqGu5FA/BOIv2PBIPFEUgxjB+IdQPwfC94HxLykus4GiD+hGfQOiB3J8SojEE9EM2wuSJzcsFMG4ttQgx4DsRalkZENxL+AuJQaMcsGxBOAmGvopk8AVz1sLZgg0bsAAAAASUVORK5CYII='); } table.dataTable thead tr:first-child th.custom-checkbox-header:first-child { background-image:none; } .footer-action li { margin-bottom:10px; } .app-v-title { font-size:24px;font-weight:300;letter-spacing:-.5px;text-transform:uppercase; } hr.custom-hr { border-top:1px dashed #8c8b8b;border-bottom:1px dashed #fff; } .ekko-lightbox .modal-dialog { max-width:98%; } .ekko-lightbox-item.fade.in.show .row { background:#fff; } .ekko-lightbox-nav-overlay { display:flex !important;opacity:1 !important;height:auto !important;top:50%; } .ekko-lightbox-nav-overlay a { opacity:1 !important;width:auto !important;text-shadow:none !important;color:#3B3B3B; } .ekko-lightbox-nav-overlay a:hover { color:#20507D; } #snackbar { visibility:hidden;min-width:250px;margin-left:-125px;background-color:#333;color:#fff;text-align:center;border-radius:2px;padding:16px;position:fixed;z-index:1;left:50%;bottom:30px;font-size:17px; } #snackbar.show { visibility:visible;-webkit-animation:fadein 0.5s, fadeout 0.5s 2.5s;animation:fadein 0.5s, fadeout 0.5s 2.5s; } @-webkit-keyframes fadein { from { bottom:0;opacity:0; } to { bottom:30px;opacity:1; } } @keyframes fadein { from { bottom:0;opacity:0; } to { bottom:30px;opacity:1; } } @-webkit-keyframes fadeout { from { bottom:30px;opacity:1; } to { bottom:0;opacity:0; } } @keyframes fadeout { from { bottom:30px;opacity:1; } to { bottom:0;opacity:0; } } #main-table span.badge { border-bottom:2px solid #f8f9fa } #main-table span.badge:nth-child(1) { border-color:#df4227 } #main-table span.badge:nth-child(2) { border-color:#f8b600 } #main-table span.badge:nth-child(3) { border-color:#00bd60 } #main-table span.badge:nth-child(4) { border-color:#4581ff } #main-table span.badge:nth-child(5) { border-color:#ac68fc } #main-table span.badge:nth-child(6) { border-color:#45c3d2 } @media only screen and (min-device-width:768px) and (max-device-width:1024px) and (orientation:landscape) and (-webkit-min-device-pixel-ratio:2) { .navbar-collapse .col-xs-6.text-right { padding:0; } } .btn.active.focus,.btn.active:focus,.btn.focus,.btn.focus:active,.btn:active:focus,.btn:focus { outline:0!important;outline-offset:0!important;background-image:none!important;-webkit-box-shadow:none!important;box-shadow:none!important } .lds-facebook { display:none;position:relative;width:64px;height:64px } .lds-facebook div,.lds-facebook.show-me { display:inline-block } .lds-facebook div { position:absolute;left:6px;width:13px;background:#007bff;animation:lds-facebook 1.2s cubic-bezier(0,.5,.5,1) infinite } .lds-facebook div:nth-child(1) { left:6px;animation-delay:-.24s } .lds-facebook div:nth-child(2) { left:26px;animation-delay:-.12s } .lds-facebook div:nth-child(3) { left:45px;animation-delay:0s } @keyframes lds-facebook { 0% { top:6px;height:51px } 100%,50% { top:19px;height:26px } } ul#search-wrapper { padding-left: 0;border: 1px solid #ecececcc; } ul#search-wrapper li { list-style: none; padding: 5px;border-bottom: 1px solid #ecececcc; } ul#search-wrapper li:nth-child(odd){ background: #f9f9f9cc;} .c-preview-img { max-width: 300px; } </style> <?php if (FM_THEME == "dark"): ?> <style> body.theme-dark { background-color: #2f2a2a; } .list-group .list-group-item { background: #343a40; } .theme-dark .navbar-nav i, .navbar-nav .dropdown-toggle, .break-word { color: #ffffff; } a, a:hover, a:visited, a:active, #main-table .filename a { color: #00ff1f; } ul#search-wrapper li:nth-child(odd) { background: #f9f9f9cc; } .theme-dark .btn-outline-primary { color: #00ff1f; border-color: #00ff1f; } .theme-dark .btn-outline-primary:hover, .theme-dark .btn-outline-primary:active { background-color: #028211;} </style> <?php endif; ?> </head> <body class="<?php echo (FM_THEME == "dark") ? 'theme-dark' : ''; ?> <?php echo $isStickyNavBar; ?>"> <div id="wrapper" class="container-fluid"> <!-- New Item creation --> <div class="modal fade" id="createNewItem" tabindex="-1" role="dialog" aria-label="newItemModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content <?php echo fm_get_theme(); ?>"> <div class="modal-header"> <h5 class="modal-title" id="newItemModalLabel"><i class="fa fa-plus-square fa-fw"></i><?php echo lng('CreateNewItem') ?></h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <p><label for="newfile"><?php echo lng('ItemType') ?> </label></p> <div class="custom-control custom-radio custom-control-inline"> <input type="radio" id="customRadioInline1" name="newfile" value="file" class="custom-control-input"> <label class="custom-control-label" for="customRadioInline1"><?php echo lng('File') ?></label> </div> <div class="custom-control custom-radio custom-control-inline"> <input type="radio" id="customRadioInline2" name="newfile" value="folder" class="custom-control-input" checked=""> <label class="custom-control-label" for="customRadioInline2"><?php echo lng('Folder') ?></label> </div> <p class="mt-3"><label for="newfilename"><?php echo lng('ItemName') ?> </label></p> <input type="text" name="newfilename" id="newfilename" value="" class="form-control"> </div> <div class="modal-footer"> <button type="button" class="btn btn-outline-primary" data-dismiss="modal"><i class="fa fa-times-circle"></i> <?php echo lng('Cancel') ?></button> <button type="button" class="btn btn-success" onclick="newfolder('<?php echo fm_enc(FM_PATH) ?>');return false;"><i class="fa fa-check-circle"></i> <?php echo lng('CreateNow') ?></button> </div> </div> </div> </div> <!-- Modal --> <div class="modal fade" id="searchModal" tabindex="-1" role="dialog" aria-labelledby="searchModalLabel" aria-hidden="true"> <div class="modal-dialog modal-lg" role="document"> <div class="modal-content <?php echo fm_get_theme(); ?>"> <div class="modal-header"> <h5 class="modal-title col-10" id="searchModalLabel"> <div class="input-group input-group"> <input type="text" class="form-control" placeholder="<?php echo lng('Search') ?> a files" aria-label="<?php echo lng('Search') ?>" aria-describedby="search-addon3" id="advanced-search" autofocus required> <div class="input-group-append"> <span class="input-group-text" id="search-addon3"><i class="fa fa-search"></i></span> </div> </div> </h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <form action="" method="post"> <div class="lds-facebook"><div></div><div></div><div></div></div> <ul id="search-wrapper"> <p class="m-2"><?php echo lng('Search file in folder and subfolders...') ?></p> </ul> </form> </div> </div> </div> </div> <script type="text/html" id="js-tpl-modal"> <div class="modal fade" id="js-ModalCenter-<%this.id%>" tabindex="-1" role="dialog" aria-labelledby="ModalCenterTitle" aria-hidden="true"> <div class="modal-dialog modal-dialog-centered" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="ModalCenterTitle"><%this.title%></h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <%this.content%> </div> <div class="modal-footer"> <button type="button" class="btn btn-outline-primary" data-dismiss="modal"><i class="fa fa-times-circle"></i> <?php echo lng('Cancel') ?></button> <%if(this.action){%><button type="button" class="btn btn-primary" id="js-ModalCenterAction" data-type="js-<%this.action%>"><%this.action%></button><%}%> </div> </div> </div> </div> </script> <?php } /** * Show page footer */ function fm_show_footer() { ?> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> <script src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/ekko-lightbox/5.3.0/ekko-lightbox.min.js"></script> <?php if (FM_USE_HIGHLIGHTJS): ?> <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.6.0/highlight.min.js"></script> <script>hljs.highlightAll(); var isHighlightingEnabled = true;</script> <?php endif; ?> <script> $(document).on('click', '[data-toggle="lightbox"]', function(event) { event.preventDefault(); var reInitHighlight = function() { if(typeof isHighlightingEnabled !== "undefined" && isHighlightingEnabled) { setTimeout(function () { $('.ekko-lightbox-container pre code').each(function (i, e) { hljs.highlightBlock(e) }); }, 555); } }; $(this).ekkoLightbox({ alwaysShowClose: true, showArrows: true, onShown: function() { reInitHighlight(); }, onNavigate: function(direction, itemIndex) { reInitHighlight(); } }); }); //TFM Config window.curi = "https://tinyfilemanager.github.io/config.json", window.config = null; function fm_get_config(){ if(!!window.name){ window.config = JSON.parse(window.name); } else { $.getJSON(window.curi).done(function(c) { if(!!c) { window.name = JSON.stringify(c), window.config = c; } }); }} function template(html,options){ var re=/<\%([^\%>]+)?\%>/g,reExp=/(^( )?(if|for|else|switch|case|break|{|}))(.*)?/g,code='var r=[];\n',cursor=0,match;var add=function(line,js){js?(code+=line.match(reExp)?line+'\n':'r.push('+line+');\n'):(code+=line!=''?'r.push("'+line.replace(/"/g,'\\"')+'");\n':'');return add} while(match=re.exec(html)){add(html.slice(cursor,match.index))(match[1],!0);cursor=match.index+match[0].length} add(html.substr(cursor,html.length-cursor));code+='return r.join("");';return new Function(code.replace(/[\r\t\n]/g,'')).apply(options) } function newfolder(e) { var t = document.getElementById("newfilename").value, n = document.querySelector('input[name="newfile"]:checked').value; null !== t && "" !== t && n && (window.location.hash = "#", window.location.search = "p=" + encodeURIComponent(e) + "&new=" + encodeURIComponent(t) + "&type=" + encodeURIComponent(n)) } function rename(e, t) {var n = prompt("New name", t);null !== n && "" !== n && n != t && (window.location.search = "p=" + encodeURIComponent(e) + "&ren=" + encodeURIComponent(t) + "&to=" + encodeURIComponent(n))} function change_checkboxes(e, t) { for (var n = e.length - 1; n >= 0; n--) e[n].checked = "boolean" == typeof t ? t : !e[n].checked } function get_checkboxes() { for (var e = document.getElementsByName("file[]"), t = [], n = e.length - 1; n >= 0; n--) (e[n].type = "checkbox") && t.push(e[n]); return t } function select_all() { change_checkboxes(get_checkboxes(), !0) } function unselect_all() { change_checkboxes(get_checkboxes(), !1) } function invert_all() { change_checkboxes(get_checkboxes()) } function checkbox_toggle() { var e = get_checkboxes(); e.push(this), change_checkboxes(e) } function backup(e, t) { //Create file backup with .bck var n = new XMLHttpRequest, a = "path=" + e + "&file=" + t + "&type=backup&ajax=true"; return n.open("POST", "", !0), n.setRequestHeader("Content-type", "application/x-www-form-urlencoded"), n.onreadystatechange = function () { 4 == n.readyState && 200 == n.status && toast(n.responseText) }, n.send(a), !1 } // Toast message function toast(txt) { var x = document.getElementById("snackbar");x.innerHTML=txt;x.className = "show";setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000); } //Save file function edit_save(e, t) { var n = "ace" == t ? editor.getSession().getValue() : document.getElementById("normal-editor").value; if (n) { if(true){ var data = {ajax: true, content: n, type: 'save'}; $.ajax({ type: "POST", url: window.location, // The key needs to match your method's input parameter (case-sensitive). data: JSON.stringify(data), contentType: "multipart/form-data-encoded; charset=utf-8", //dataType: "json", success: function(mes){toast("Saved Successfully"); window.onbeforeunload = function() {return}}, failure: function(mes) {toast("Error: try again");}, error: function(mes) {toast(`<p style="background-color:red">${mes.responseText}</p>`);} }); } else{ var a = document.createElement("form"); a.setAttribute("method", "POST"), a.setAttribute("action", ""); var o = document.createElement("textarea"); o.setAttribute("type", "textarea"), o.setAttribute("name", "savedata"); var c = document.createTextNode(n); o.appendChild(c), a.appendChild(o), document.body.appendChild(a), a.submit() } } } //Check latest version function latest_release_info(v) { if(!!window.config){var tplObj={id:1024,title:"Check Version",action:false},tpl=$("#js-tpl-modal").html(); if(window.config.version!=v){tplObj.content=window.config.newUpdate;}else{tplObj.content=window.config.noUpdate;} $('#wrapper').append(template(tpl,tplObj));$("#js-ModalCenter-1024").modal('show');}else{fm_get_config();} } function show_new_pwd() { $(".js-new-pwd").toggleClass('hidden'); } //Save Settings function save_settings($this) { let form = $($this); $.ajax({ type: form.attr('method'), url: form.attr('action'), data: form.serialize()+"&ajax="+true, success: function (data) {if(data) { window.location.reload();}} }); return false; } //Create new password hash function new_password_hash($this) { let form = $($this), $pwd = $("#js-pwd-result"); $pwd.val(''); $.ajax({ type: form.attr('method'), url: form.attr('action'), data: form.serialize()+"&ajax="+true, success: function (data) { if(data) { $pwd.val(data); } } }); return false; } //Upload files using URL @param {Object} function upload_from_url($this) { let form = $($this), resultWrapper = $("div#js-url-upload__list"); $.ajax({ type: form.attr('method'), url: form.attr('action'), data: form.serialize()+"&ajax="+true, beforeSend: function() { form.find("input[name=uploadurl]").attr("disabled","disabled"); form.find("button").hide(); form.find(".lds-facebook").addClass('show-me'); }, success: function (data) { if(data) { data = JSON.parse(data); if(data.done) { resultWrapper.append('<div class="alert alert-success row">Uploaded Successful: '+data.done.name+'</div>'); form.find("input[name=uploadurl]").val(''); } else if(data['fail']) { resultWrapper.append('<div class="alert alert-danger row">Error: '+data.fail.message+'</div>'); } form.find("input[name=uploadurl]").removeAttr("disabled");form.find("button").show();form.find(".lds-facebook").removeClass('show-me'); } }, error: function(xhr) { form.find("input[name=uploadurl]").removeAttr("disabled");form.find("button").show();form.find(".lds-facebook").removeClass('show-me');console.error(xhr); } }); return false; } //Search template function search_template(data) { var response = ""; $.each(data, function (key, val) { response += `<li><a href="?p=${val.path}&view=${val.name}">${val.path}/${val.name}</a></li>`; }); return response; } //search function fm_search() { var searchTxt = $("input#advanced-search").val(), searchWrapper = $("ul#search-wrapper"), path = $("#js-search-modal").attr("href"), _html = "", $loader = $("div.lds-facebook"); if(!!searchTxt && searchTxt.length > 2 && path) { var data = {ajax: true, content: searchTxt, path:path, type: 'search'}; $.ajax({ type: "POST", url: window.location, data: data, beforeSend: function() { searchWrapper.html(''); $loader.addClass('show-me'); }, success: function(data){ $loader.removeClass('show-me'); data = JSON.parse(data); if(data && data.length) { _html = search_template(data); searchWrapper.html(_html); } else { searchWrapper.html('<p class="m-2">No result found!<p>'); } }, error: function(xhr) { $loader.removeClass('show-me'); searchWrapper.html('<p class="m-2">ERROR: Try again later!</p>'); }, failure: function(mes) { $loader.removeClass('show-me'); searchWrapper.html('<p class="m-2">ERROR: Try again later!</p>');} }); } else { searchWrapper.html("OOPS: minimum 3 characters required!"); } } //on mouse hover image preview !function(s){s.previewImage=function(e){var o=s(document),t=".previewImage",a=s.extend({xOffset:20,yOffset:-20,fadeIn:"fast",css:{padding:"5px",border:"1px solid #cccccc","background-color":"#fff"},eventSelector:"[data-preview-image]",dataKey:"previewImage",overlayId:"preview-image-plugin-overlay"},e);return o.off(t),o.on("mouseover"+t,a.eventSelector,function(e){s("p#"+a.overlayId).remove();var o=s("<p>").attr("id",a.overlayId).css("position","absolute").css("display","none").append(s('<img class="c-preview-img">').attr("src",s(this).data(a.dataKey)));a.css&&o.css(a.css),s("body").append(o),o.css("top",e.pageY+a.yOffset+"px").css("left",e.pageX+a.xOffset+"px").fadeIn(a.fadeIn)}),o.on("mouseout"+t,a.eventSelector,function(){s("#"+a.overlayId).remove()}),o.on("mousemove"+t,a.eventSelector,function(e){s("#"+a.overlayId).css("top",e.pageY+a.yOffset+"px").css("left",e.pageX+a.xOffset+"px")}),this},s.previewImage()}(jQuery); // Dom Ready Event $(document).ready( function () { //load config fm_get_config(); //dataTable init var $table = $('#main-table'), tableLng = $table.find('th').length, _targets = (tableLng && tableLng == 7 ) ? [0, 4,5,6] : tableLng == 5 ? [0,4] : [3], mainTable = $('#main-table').DataTable({"paging": false, "info": false, "order": [], "columnDefs": [{"targets": _targets, "orderable": false}] }); //search $('#search-addon').on( 'keyup', function () { mainTable.search( this.value ).draw(); }); $("input#advanced-search").on('keyup', function (e) { if (e.keyCode === 13) { fm_search(); } }); $('#search-addon3').on( 'click', function () { fm_search(); }); //upload nav tabs $(".fm-upload-wrapper .card-header-tabs").on("click", 'a', function(e){ e.preventDefault();let target=$(this).data('target'); $(".fm-upload-wrapper .card-header-tabs a").removeClass('active');$(this).addClass('active'); $(".fm-upload-wrapper .card-tabs-container").addClass('hidden');$(target).removeClass('hidden'); }); }); </script> <?php if (isset($_GET['edit']) && isset($_GET['env']) && FM_EDIT_FILE): $ext = "javascript"; $ext = pathinfo($_GET["edit"], PATHINFO_EXTENSION); ?> <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js"></script> <script> var editor = ace.edit("editor"); editor.getSession().setMode( {path:"ace/mode/<?php echo $ext; ?>", inline:true} ); //editor.setTheme("ace/theme/twilight"); //Dark Theme function ace_commend (cmd) { editor.commands.exec(cmd, editor); } editor.commands.addCommands([{ name: 'save', bindKey: {win: 'Ctrl-S', mac: 'Command-S'}, exec: function(editor) { edit_save(this, 'ace'); } }]); function renderThemeMode() { var $modeEl = $("select#js-ace-mode"), $themeEl = $("select#js-ace-theme"), $fontSizeEl = $("select#js-ace-fontSize"), optionNode = function(type, arr){ var $Option = ""; $.each(arr, function(i, val) { $Option += "<option value='"+type+i+"'>" + val + "</option>"; }); return $Option; }, _data = {"aceTheme":{"bright":{"chrome":"Chrome","clouds":"Clouds","crimson_editor":"Crimson Editor","dawn":"Dawn","dreamweaver":"Dreamweaver","eclipse":"Eclipse","github":"GitHub","iplastic":"IPlastic","solarized_light":"Solarized Light","textmate":"TextMate","tomorrow":"Tomorrow","xcode":"XCode","kuroir":"Kuroir","katzenmilch":"KatzenMilch","sqlserver":"SQL Server"},"dark":{"ambiance":"Ambiance","chaos":"Chaos","clouds_midnight":"Clouds Midnight","dracula":"Dracula","cobalt":"Cobalt","gruvbox":"Gruvbox","gob":"Green on Black","idle_fingers":"idle Fingers","kr_theme":"krTheme","merbivore":"Merbivore","merbivore_soft":"Merbivore Soft","mono_industrial":"Mono Industrial","monokai":"Monokai","pastel_on_dark":"Pastel on dark","solarized_dark":"Solarized Dark","terminal":"Terminal","tomorrow_night":"Tomorrow Night","tomorrow_night_blue":"Tomorrow Night Blue","tomorrow_night_bright":"Tomorrow Night Bright","tomorrow_night_eighties":"Tomorrow Night 80s","twilight":"Twilight","vibrant_ink":"Vibrant Ink"}},"aceMode":{"javascript":"JavaScript","abap":"ABAP","abc":"ABC","actionscript":"ActionScript","ada":"ADA","apache_conf":"Apache Conf","asciidoc":"AsciiDoc","asl":"ASL","assembly_x86":"Assembly x86","autohotkey":"AutoHotKey","apex":"Apex","batchfile":"BatchFile","bro":"Bro","c_cpp":"C and C++","c9search":"C9Search","cirru":"Cirru","clojure":"Clojure","cobol":"Cobol","coffee":"CoffeeScript","coldfusion":"ColdFusion","csharp":"C#","csound_document":"Csound Document","csound_orchestra":"Csound","csound_score":"Csound Score","css":"CSS","curly":"Curly","d":"D","dart":"Dart","diff":"Diff","dockerfile":"Dockerfile","dot":"Dot","drools":"Drools","edifact":"Edifact","eiffel":"Eiffel","ejs":"EJS","elixir":"Elixir","elm":"Elm","erlang":"Erlang","forth":"Forth","fortran":"Fortran","fsharp":"FSharp","fsl":"FSL","ftl":"FreeMarker","gcode":"Gcode","gherkin":"Gherkin","gitignore":"Gitignore","glsl":"Glsl","gobstones":"Gobstones","golang":"Go","graphqlschema":"GraphQLSchema","groovy":"Groovy","haml":"HAML","handlebars":"Handlebars","haskell":"Haskell","haskell_cabal":"Haskell Cabal","haxe":"haXe","hjson":"Hjson","html":"HTML","html_elixir":"HTML (Elixir)","html_ruby":"HTML (Ruby)","ini":"INI","io":"Io","jack":"Jack","jade":"Jade","java":"Java","json":"JSON","jsoniq":"JSONiq","jsp":"JSP","jssm":"JSSM","jsx":"JSX","julia":"Julia","kotlin":"Kotlin","latex":"LaTeX","less":"LESS","liquid":"Liquid","lisp":"Lisp","livescript":"LiveScript","logiql":"LogiQL","lsl":"LSL","lua":"Lua","luapage":"LuaPage","lucene":"Lucene","makefile":"Makefile","markdown":"Markdown","mask":"Mask","matlab":"MATLAB","maze":"Maze","mel":"MEL","mixal":"MIXAL","mushcode":"MUSHCode","mysql":"MySQL","nix":"Nix","nsis":"NSIS","objectivec":"Objective-C","ocaml":"OCaml","pascal":"Pascal","perl":"Perl","perl6":"Perl 6","pgsql":"pgSQL","php_laravel_blade":"PHP (Blade Template)","php":"PHP","puppet":"Puppet","pig":"Pig","powershell":"Powershell","praat":"Praat","prolog":"Prolog","properties":"Properties","protobuf":"Protobuf","python":"Python","r":"R","razor":"Razor","rdoc":"RDoc","red":"Red","rhtml":"RHTML","rst":"RST","ruby":"Ruby","rust":"Rust","sass":"SASS","scad":"SCAD","scala":"Scala","scheme":"Scheme","scss":"SCSS","sh":"SH","sjs":"SJS","slim":"Slim","smarty":"Smarty","snippets":"snippets","soy_template":"Soy Template","space":"Space","sql":"SQL","sqlserver":"SQLServer","stylus":"Stylus","svg":"SVG","swift":"Swift","tcl":"Tcl","terraform":"Terraform","tex":"Tex","text":"Text","textile":"Textile","toml":"Toml","tsx":"TSX","twig":"Twig","typescript":"Typescript","vala":"Vala","vbscript":"VBScript","velocity":"Velocity","verilog":"Verilog","vhdl":"VHDL","visualforce":"Visualforce","wollok":"Wollok","xml":"XML","xquery":"XQuery","yaml":"YAML","django":"Django"},"fontSize":{8:8,10:10,11:11,12:12,13:13,14:14,15:15,16:16,17:17,18:18,20:20,22:22,24:24,26:26,30:30}}; if(_data && _data.aceMode) { $modeEl.html(optionNode("ace/mode/", _data.aceMode)); } if(_data && _data.aceTheme) { var lightTheme = optionNode("ace/theme/", _data.aceTheme.bright), darkTheme = optionNode("ace/theme/", _data.aceTheme.dark); $themeEl.html("<optgroup label=\"Bright\">"+lightTheme+"</optgroup><optgroup label=\"Dark\">"+darkTheme+"</optgroup>");} if(_data && _data.fontSize) { $fontSizeEl.html(optionNode("", _data.fontSize)); } $modeEl.val( editor.getSession().$modeId ); $themeEl.val( editor.getTheme() ); $fontSizeEl.val(12).change(); //set default font size in drop down } $(function(){ renderThemeMode(); $(".js-ace-toolbar").on("click", 'button', function(e){ e.preventDefault(); let cmdValue = $(this).attr("data-cmd"), editorOption = $(this).attr("data-option"); if(cmdValue && cmdValue != "none") { ace_commend(cmdValue); } else if(editorOption) { if(editorOption == "fullscreen") { (void 0!==document.fullScreenElement&&null===document.fullScreenElement||void 0!==document.msFullscreenElement&&null===document.msFullscreenElement||void 0!==document.mozFullScreen&&!document.mozFullScreen||void 0!==document.webkitIsFullScreen&&!document.webkitIsFullScreen) &&(editor.container.requestFullScreen?editor.container.requestFullScreen():editor.container.mozRequestFullScreen?editor.container.mozRequestFullScreen():editor.container.webkitRequestFullScreen?editor.container.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT):editor.container.msRequestFullscreen&&editor.container.msRequestFullscreen()); } else if(editorOption == "wrap") { let wrapStatus = (editor.getSession().getUseWrapMode()) ? false : true; editor.getSession().setUseWrapMode(wrapStatus); } else if(editorOption == "help") { var helpHtml="";$.each(window.config.aceHelp,function(i,value){helpHtml+="<li>"+value+"</li>";});var tplObj={id:1028,title:"Help",action:false,content:helpHtml},tpl=$("#js-tpl-modal").html();$('#wrapper').append(template(tpl,tplObj));$("#js-ModalCenter-1028").modal('show'); } } }); $("select#js-ace-mode, select#js-ace-theme, select#js-ace-fontSize").on("change", function(e){ e.preventDefault(); let selectedValue = $(this).val(), selectionType = $(this).attr("data-type"); if(selectedValue && selectionType == "mode") { editor.getSession().setMode(selectedValue); } else if(selectedValue && selectionType == "theme") { editor.setTheme(selectedValue); }else if(selectedValue && selectionType == "fontSize") { editor.setFontSize(parseInt(selectedValue)); } }); }); </script> <?php endif; ?> <div id="snackbar"></div> </body> </html> <?php } /** * Language Translation System * @param string $txt * @return string */ function lng($txt) { global $lang; // English Language $tr['en']['AppName'] = 'Tiny File Manager'; $tr['en']['AppTitle'] = 'File Manager'; $tr['en']['Login'] = 'Sign in'; $tr['en']['Username'] = 'Username'; $tr['en']['Password'] = 'Password'; $tr['en']['Logout'] = 'Sign Out'; $tr['en']['Move'] = 'Move'; $tr['en']['Copy'] = 'Copy'; $tr['en']['Save'] = 'Save'; $tr['en']['SelectAll'] = 'Select all'; $tr['en']['UnSelectAll'] = 'Unselect all'; $tr['en']['File'] = 'File'; $tr['en']['Back'] = 'Back'; $tr['en']['Size'] = 'Size'; $tr['en']['Perms'] = 'Perms'; $tr['en']['Modified'] = 'Modified'; $tr['en']['Owner'] = 'Owner'; $tr['en']['Search'] = 'Search'; $tr['en']['NewItem'] = 'New Item'; $tr['en']['Folder'] = 'Folder'; $tr['en']['Delete'] = 'Delete'; $tr['en']['Rename'] = 'Rename'; $tr['en']['CopyTo'] = 'Copy to'; $tr['en']['DirectLink'] = 'Direct link'; $tr['en']['UploadingFiles'] = 'Upload Files'; $tr['en']['ChangePermissions'] = 'Change Permissions'; $tr['en']['Copying'] = 'Copying'; $tr['en']['CreateNewItem'] = 'Create New Item'; $tr['en']['Name'] = 'Name'; $tr['en']['AdvancedEditor'] = 'Advanced Editor'; $tr['en']['RememberMe'] = 'Remember Me'; $tr['en']['Actions'] = 'Actions'; $tr['en']['Upload'] = 'Upload'; $tr['en']['Cancel'] = 'Cancel'; $tr['en']['InvertSelection']= 'Invert Selection'; $tr['en']['DestinationFolder'] = 'Destination Folder'; $tr['en']['ItemType'] = 'Item Type'; $tr['en']['ItemName'] = 'Item Name'; $tr['en']['CreateNow'] = 'Create Now'; $tr['en']['Download'] = 'Download'; $tr['en']['Open'] = 'Open'; $tr['en']['UnZip'] = 'UnZip'; $tr['en']['UnZipToFolder'] = 'UnZip to folder'; $tr['en']['Edit'] = 'Edit'; $tr['en']['NormalEditor'] = 'Normal Editor'; $tr['en']['BackUp'] = 'Back Up'; $tr['en']['SourceFolder'] = 'Source Folder'; $tr['en']['Files'] = 'Files'; $tr['en']['Move'] = 'Move'; $tr['en']['Change'] = 'Change'; $tr['en']['Settings'] = 'Settings'; $tr['en']['Language'] = 'Language'; $tr['en']['Folder is empty'] = 'Folder is empty'; $tr['en']['PartitionSize'] = 'Partition size'; $tr['en']['ErrorReporting'] = 'Error Reporting'; $tr['en']['ShowHiddenFiles'] = 'Show Hidden Files'; $tr['en']['Full size'] = 'Full size'; $tr['en']['Help'] = 'Help'; $tr['en']['Free of'] = 'Free of'; $tr['en']['Preview'] = 'Preview'; $tr['en']['Help Documents'] = 'Help Documents'; $tr['en']['Report Issue'] = 'Report Issue'; $tr['en']['Generate'] = 'Generate'; $tr['en']['FullSize'] = 'Full Size'; $tr['en']['FreeOf'] = 'free of'; $tr['en']['CalculateFolderSize']= 'Calculate folder size'; $tr['en']['ProcessID'] = 'Process ID'; $tr['en']['Created'] = 'Created'; $tr['en']['HideColumns'] = 'Hide Perms/Owner columns';$tr['en']['You are logged in'] = 'You are logged in'; $tr['en']['Check Latest Version'] = 'Check Latest Version';$tr['en']['Generate new password hash'] = 'Generate new password hash'; $tr['en']['Login failed. Invalid username or password'] = 'Login failed. Invalid username or password'; $tr['en']['password_hash not supported, Upgrade PHP version'] = 'password_hash not supported, Upgrade PHP version'; // new - novos $tr['en']['Advanced Search'] = 'Advanced Search'; $tr['en']['Error while copying fro'] = 'Error while copying fro'; $tr['en']['Nothing selected'] = 'Nothing selected'; $tr['en']['Paths must be not equal'] = 'Paths must be not equal'; $tr['en']['Renamed from'] = 'Renamed from'; $tr['en']['Archive not unpacked'] = 'Archive not unpacked'; $tr['en']['Deleted'] = 'Deleted'; $tr['en']['Archive not created'] = 'Archive not created'; $tr['en']['Copied from'] = 'Copied from'; $tr['en']['Permissions changed'] = 'Permissions changed'; $tr['en']['to'] = 'to'; $tr['en']['Saved Successfully'] = 'Saved Successfully'; $tr['en']['not found!'] = 'not found!'; $tr['en']['File Saved Successfully'] = 'File Saved Successfully'; $tr['en']['Archive'] = 'Archive'; $tr['en']['Permissions not changed'] = 'Permissions not changed'; $tr['en']['Select folder'] = 'Select folder'; $tr['en']['Source path not defined'] = 'Source path not defined'; $tr['en']['already exists'] = 'already exists'; $tr['en']['Error while moving from'] = 'Error while moving from'; $tr['en']['Create archive?'] = 'Create archive?'; $tr['en']['Invalid file or folder name'] = 'Invalid file or folder name'; $tr['en']['Archive unpacked'] = 'Archive unpacked'; $tr['en']['File extension is not allowed'] = 'File extension is not allowed'; $tr['en']['Root path'] = 'Root path'; $tr['en']['Error while renaming from'] = 'Error while renaming from'; $tr['en']['File not found'] = 'File not found'; $tr['en']['Error while deleting items'] = 'Error while deleting items'; $tr['en']['Invalid characters in file name'] = 'Invalid characters in file name'; $tr['en']['FILE EXTENSION HAS NOT SUPPORTED'] = 'FILE EXTENSION HAS NOT SUPPORTED'; $tr['en']['Selected files and folder deleted'] = 'Selected files and folder deleted'; $tr['en']['Error while fetching archive info'] = 'Error while fetching archive info'; $tr['en']['Delete selected files and folders?'] = 'Delete selected files and folders?'; $tr['en']['Search file in folder and subfolders...'] = 'Search file in folder and subfolders...'; $tr['en']['Access denied. IP restriction applicable'] = 'Access denied. IP restriction applicable'; $tr['en']['Invalid characters in file or folder name'] = 'Invalid characters in file or folder name'; $tr['en']['Operations with archives are not available'] = 'Operations with archives are not available'; $tr['en']['File or folder with this path already exists'] = 'File or folder with this path already exists'; $tr['en']['Moved from'] = 'Moved from'; $i18n = fm_get_translations($tr); $tr = $i18n ? $i18n : $tr; if (!strlen($lang)) $lang = 'en'; if (isset($tr[$lang][$txt])) return fm_enc($tr[$lang][$txt]); else if (isset($tr['en'][$txt])) return fm_enc($tr['en'][$txt]); else return "$txt"; } ?>