Edit File by line
/home/zeestwma/richards.../wp-inclu.../SimplePi.../src
File: Locator.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* SimplePie
[3] Fix | Delete
*
[4] Fix | Delete
* A PHP-Based RSS and Atom Feed Framework.
[5] Fix | Delete
* Takes the hard work out of managing a complete RSS/Atom solution.
[6] Fix | Delete
*
[7] Fix | Delete
* Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
[8] Fix | Delete
* All rights reserved.
[9] Fix | Delete
*
[10] Fix | Delete
* Redistribution and use in source and binary forms, with or without modification, are
[11] Fix | Delete
* permitted provided that the following conditions are met:
[12] Fix | Delete
*
[13] Fix | Delete
* * Redistributions of source code must retain the above copyright notice, this list of
[14] Fix | Delete
* conditions and the following disclaimer.
[15] Fix | Delete
*
[16] Fix | Delete
* * Redistributions in binary form must reproduce the above copyright notice, this list
[17] Fix | Delete
* of conditions and the following disclaimer in the documentation and/or other materials
[18] Fix | Delete
* provided with the distribution.
[19] Fix | Delete
*
[20] Fix | Delete
* * Neither the name of the SimplePie Team nor the names of its contributors may be used
[21] Fix | Delete
* to endorse or promote products derived from this software without specific prior
[22] Fix | Delete
* written permission.
[23] Fix | Delete
*
[24] Fix | Delete
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
[25] Fix | Delete
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
[26] Fix | Delete
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
[27] Fix | Delete
* AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
[28] Fix | Delete
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
[29] Fix | Delete
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
[30] Fix | Delete
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
[31] Fix | Delete
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
[32] Fix | Delete
* POSSIBILITY OF SUCH DAMAGE.
[33] Fix | Delete
*
[34] Fix | Delete
* @package SimplePie
[35] Fix | Delete
* @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
[36] Fix | Delete
* @author Ryan Parman
[37] Fix | Delete
* @author Sam Sneddon
[38] Fix | Delete
* @author Ryan McCue
[39] Fix | Delete
* @link http://simplepie.org/ SimplePie
[40] Fix | Delete
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
[41] Fix | Delete
*/
[42] Fix | Delete
[43] Fix | Delete
namespace SimplePie;
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* Used for feed auto-discovery
[47] Fix | Delete
*
[48] Fix | Delete
*
[49] Fix | Delete
* This class can be overloaded with {@see \SimplePie\SimplePie::set_locator_class()}
[50] Fix | Delete
*
[51] Fix | Delete
* @package SimplePie
[52] Fix | Delete
*/
[53] Fix | Delete
class Locator implements RegistryAware
[54] Fix | Delete
{
[55] Fix | Delete
public $useragent;
[56] Fix | Delete
public $timeout;
[57] Fix | Delete
public $file;
[58] Fix | Delete
public $local = [];
[59] Fix | Delete
public $elsewhere = [];
[60] Fix | Delete
public $cached_entities = [];
[61] Fix | Delete
public $http_base;
[62] Fix | Delete
public $base;
[63] Fix | Delete
public $base_location = 0;
[64] Fix | Delete
public $checked_feeds = 0;
[65] Fix | Delete
public $max_checked_feeds = 10;
[66] Fix | Delete
public $force_fsockopen = false;
[67] Fix | Delete
public $curl_options = [];
[68] Fix | Delete
public $dom;
[69] Fix | Delete
protected $registry;
[70] Fix | Delete
[71] Fix | Delete
public function __construct(\SimplePie\File $file, $timeout = 10, $useragent = null, $max_checked_feeds = 10, $force_fsockopen = false, $curl_options = [])
[72] Fix | Delete
{
[73] Fix | Delete
$this->file = $file;
[74] Fix | Delete
$this->useragent = $useragent;
[75] Fix | Delete
$this->timeout = $timeout;
[76] Fix | Delete
$this->max_checked_feeds = $max_checked_feeds;
[77] Fix | Delete
$this->force_fsockopen = $force_fsockopen;
[78] Fix | Delete
$this->curl_options = $curl_options;
[79] Fix | Delete
[80] Fix | Delete
if (class_exists('DOMDocument') && $this->file->body != '') {
[81] Fix | Delete
$this->dom = new \DOMDocument();
[82] Fix | Delete
[83] Fix | Delete
set_error_handler(['SimplePie\Misc', 'silence_errors']);
[84] Fix | Delete
try {
[85] Fix | Delete
$this->dom->loadHTML($this->file->body);
[86] Fix | Delete
} catch (\Throwable $ex) {
[87] Fix | Delete
$this->dom = null;
[88] Fix | Delete
}
[89] Fix | Delete
restore_error_handler();
[90] Fix | Delete
} else {
[91] Fix | Delete
$this->dom = null;
[92] Fix | Delete
}
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
public function set_registry(\SimplePie\Registry $registry)/* : void */
[96] Fix | Delete
{
[97] Fix | Delete
$this->registry = $registry;
[98] Fix | Delete
}
[99] Fix | Delete
[100] Fix | Delete
public function find($type = \SimplePie\SimplePie::LOCATOR_ALL, &$working = null)
[101] Fix | Delete
{
[102] Fix | Delete
if ($this->is_feed($this->file)) {
[103] Fix | Delete
return $this->file;
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
if ($this->file->method & \SimplePie\SimplePie::FILE_SOURCE_REMOTE) {
[107] Fix | Delete
$sniffer = $this->registry->create(Content\Type\Sniffer::class, [$this->file]);
[108] Fix | Delete
if ($sniffer->get_type() !== 'text/html') {
[109] Fix | Delete
return null;
[110] Fix | Delete
}
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
if ($type & ~\SimplePie\SimplePie::LOCATOR_NONE) {
[114] Fix | Delete
$this->get_base();
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
if ($type & \SimplePie\SimplePie::LOCATOR_AUTODISCOVERY && $working = $this->autodiscovery()) {
[118] Fix | Delete
return $working[0];
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
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()) {
[122] Fix | Delete
if ($type & \SimplePie\SimplePie::LOCATOR_LOCAL_EXTENSION && $working = $this->extension($this->local)) {
[123] Fix | Delete
return $working[0];
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
if ($type & \SimplePie\SimplePie::LOCATOR_LOCAL_BODY && $working = $this->body($this->local)) {
[127] Fix | Delete
return $working[0];
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
if ($type & \SimplePie\SimplePie::LOCATOR_REMOTE_EXTENSION && $working = $this->extension($this->elsewhere)) {
[131] Fix | Delete
return $working[0];
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
if ($type & \SimplePie\SimplePie::LOCATOR_REMOTE_BODY && $working = $this->body($this->elsewhere)) {
[135] Fix | Delete
return $working[0];
[136] Fix | Delete
}
[137] Fix | Delete
}
[138] Fix | Delete
return null;
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
public function is_feed($file, $check_html = false)
[142] Fix | Delete
{
[143] Fix | Delete
if ($file->method & \SimplePie\SimplePie::FILE_SOURCE_REMOTE) {
[144] Fix | Delete
$sniffer = $this->registry->create(Content\Type\Sniffer::class, [$file]);
[145] Fix | Delete
$sniffed = $sniffer->get_type();
[146] Fix | Delete
$mime_types = ['application/rss+xml', 'application/rdf+xml',
[147] Fix | Delete
'text/rdf', 'application/atom+xml', 'text/xml',
[148] Fix | Delete
'application/xml', 'application/x-rss+xml'];
[149] Fix | Delete
if ($check_html) {
[150] Fix | Delete
$mime_types[] = 'text/html';
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
return in_array($sniffed, $mime_types);
[154] Fix | Delete
} elseif ($file->method & \SimplePie\SimplePie::FILE_SOURCE_LOCAL) {
[155] Fix | Delete
return true;
[156] Fix | Delete
} else {
[157] Fix | Delete
return false;
[158] Fix | Delete
}
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
public function get_base()
[162] Fix | Delete
{
[163] Fix | Delete
if ($this->dom === null) {
[164] Fix | Delete
throw new \SimplePie\Exception('DOMDocument not found, unable to use locator');
[165] Fix | Delete
}
[166] Fix | Delete
$this->http_base = $this->file->url;
[167] Fix | Delete
$this->base = $this->http_base;
[168] Fix | Delete
$elements = $this->dom->getElementsByTagName('base');
[169] Fix | Delete
foreach ($elements as $element) {
[170] Fix | Delete
if ($element->hasAttribute('href')) {
[171] Fix | Delete
$base = $this->registry->call(Misc::class, 'absolutize_url', [trim($element->getAttribute('href')), $this->http_base]);
[172] Fix | Delete
if ($base === false) {
[173] Fix | Delete
continue;
[174] Fix | Delete
}
[175] Fix | Delete
$this->base = $base;
[176] Fix | Delete
$this->base_location = method_exists($element, 'getLineNo') ? $element->getLineNo() : 0;
[177] Fix | Delete
break;
[178] Fix | Delete
}
[179] Fix | Delete
}
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
public function autodiscovery()
[183] Fix | Delete
{
[184] Fix | Delete
$done = [];
[185] Fix | Delete
$feeds = [];
[186] Fix | Delete
$feeds = array_merge($feeds, $this->search_elements_by_tag('link', $done, $feeds));
[187] Fix | Delete
$feeds = array_merge($feeds, $this->search_elements_by_tag('a', $done, $feeds));
[188] Fix | Delete
$feeds = array_merge($feeds, $this->search_elements_by_tag('area', $done, $feeds));
[189] Fix | Delete
[190] Fix | Delete
if (!empty($feeds)) {
[191] Fix | Delete
return array_values($feeds);
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
return null;
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
protected function search_elements_by_tag($name, &$done, $feeds)
[198] Fix | Delete
{
[199] Fix | Delete
if ($this->dom === null) {
[200] Fix | Delete
throw new \SimplePie\Exception('DOMDocument not found, unable to use locator');
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
$links = $this->dom->getElementsByTagName($name);
[204] Fix | Delete
foreach ($links as $link) {
[205] Fix | Delete
if ($this->checked_feeds === $this->max_checked_feeds) {
[206] Fix | Delete
break;
[207] Fix | Delete
}
[208] Fix | Delete
if ($link->hasAttribute('href') && $link->hasAttribute('rel')) {
[209] Fix | Delete
$rel = array_unique($this->registry->call(Misc::class, 'space_separated_tokens', [strtolower($link->getAttribute('rel'))]));
[210] Fix | Delete
$line = method_exists($link, 'getLineNo') ? $link->getLineNo() : 1;
[211] Fix | Delete
[212] Fix | Delete
if ($this->base_location < $line) {
[213] Fix | Delete
$href = $this->registry->call(Misc::class, 'absolutize_url', [trim($link->getAttribute('href')), $this->base]);
[214] Fix | Delete
} else {
[215] Fix | Delete
$href = $this->registry->call(Misc::class, 'absolutize_url', [trim($link->getAttribute('href')), $this->http_base]);
[216] Fix | Delete
}
[217] Fix | Delete
if ($href === false) {
[218] Fix | Delete
continue;
[219] Fix | Delete
}
[220] Fix | Delete
[221] Fix | Delete
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])) {
[222] Fix | Delete
$this->checked_feeds++;
[223] Fix | Delete
$headers = [
[224] Fix | Delete
'Accept' => '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',
[225] Fix | Delete
];
[226] Fix | Delete
$feed = $this->registry->create(File::class, [$href, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options]);
[227] Fix | Delete
if ($feed->success && ($feed->method & \SimplePie\SimplePie::FILE_SOURCE_REMOTE === 0 || ($feed->status_code === 200 || $feed->status_code > 206 && $feed->status_code < 300)) && $this->is_feed($feed, true)) {
[228] Fix | Delete
$feeds[$href] = $feed;
[229] Fix | Delete
}
[230] Fix | Delete
}
[231] Fix | Delete
$done[] = $href;
[232] Fix | Delete
}
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
return $feeds;
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
public function get_links()
[239] Fix | Delete
{
[240] Fix | Delete
if ($this->dom === null) {
[241] Fix | Delete
throw new \SimplePie\Exception('DOMDocument not found, unable to use locator');
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
$links = $this->dom->getElementsByTagName('a');
[245] Fix | Delete
foreach ($links as $link) {
[246] Fix | Delete
if ($link->hasAttribute('href')) {
[247] Fix | Delete
$href = trim($link->getAttribute('href'));
[248] Fix | Delete
$parsed = $this->registry->call(Misc::class, 'parse_url', [$href]);
[249] Fix | Delete
if ($parsed['scheme'] === '' || preg_match('/^(https?|feed)?$/i', $parsed['scheme'])) {
[250] Fix | Delete
if (method_exists($link, 'getLineNo') && $this->base_location < $link->getLineNo()) {
[251] Fix | Delete
$href = $this->registry->call(Misc::class, 'absolutize_url', [trim($link->getAttribute('href')), $this->base]);
[252] Fix | Delete
} else {
[253] Fix | Delete
$href = $this->registry->call(Misc::class, 'absolutize_url', [trim($link->getAttribute('href')), $this->http_base]);
[254] Fix | Delete
}
[255] Fix | Delete
if ($href === false) {
[256] Fix | Delete
continue;
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
$current = $this->registry->call(Misc::class, 'parse_url', [$this->file->url]);
[260] Fix | Delete
[261] Fix | Delete
if ($parsed['authority'] === '' || $parsed['authority'] === $current['authority']) {
[262] Fix | Delete
$this->local[] = $href;
[263] Fix | Delete
} else {
[264] Fix | Delete
$this->elsewhere[] = $href;
[265] Fix | Delete
}
[266] Fix | Delete
}
[267] Fix | Delete
}
[268] Fix | Delete
}
[269] Fix | Delete
$this->local = array_unique($this->local);
[270] Fix | Delete
$this->elsewhere = array_unique($this->elsewhere);
[271] Fix | Delete
if (!empty($this->local) || !empty($this->elsewhere)) {
[272] Fix | Delete
return true;
[273] Fix | Delete
}
[274] Fix | Delete
return null;
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
public function get_rel_link($rel)
[278] Fix | Delete
{
[279] Fix | Delete
if ($this->dom === null) {
[280] Fix | Delete
throw new \SimplePie\Exception('DOMDocument not found, unable to use '.
[281] Fix | Delete
'locator');
[282] Fix | Delete
}
[283] Fix | Delete
if (!class_exists('DOMXpath')) {
[284] Fix | Delete
throw new \SimplePie\Exception('DOMXpath not found, unable to use '.
[285] Fix | Delete
'get_rel_link');
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
$xpath = new \DOMXpath($this->dom);
[289] Fix | Delete
$query = '//a[@rel and @href] | //link[@rel and @href]';
[290] Fix | Delete
foreach ($xpath->query($query) as $link) {
[291] Fix | Delete
$href = trim($link->getAttribute('href'));
[292] Fix | Delete
$parsed = $this->registry->call(Misc::class, 'parse_url', [$href]);
[293] Fix | Delete
if ($parsed['scheme'] === '' ||
[294] Fix | Delete
preg_match('/^https?$/i', $parsed['scheme'])) {
[295] Fix | Delete
if (method_exists($link, 'getLineNo') &&
[296] Fix | Delete
$this->base_location < $link->getLineNo()) {
[297] Fix | Delete
$href = $this->registry->call(
[298] Fix | Delete
Misc::class,
[299] Fix | Delete
'absolutize_url',
[300] Fix | Delete
[trim($link->getAttribute('href')), $this->base]
[301] Fix | Delete
);
[302] Fix | Delete
} else {
[303] Fix | Delete
$href = $this->registry->call(
[304] Fix | Delete
Misc::class,
[305] Fix | Delete
'absolutize_url',
[306] Fix | Delete
[trim($link->getAttribute('href')), $this->http_base]
[307] Fix | Delete
);
[308] Fix | Delete
}
[309] Fix | Delete
if ($href === false) {
[310] Fix | Delete
return null;
[311] Fix | Delete
}
[312] Fix | Delete
$rel_values = explode(' ', strtolower($link->getAttribute('rel')));
[313] Fix | Delete
if (in_array($rel, $rel_values)) {
[314] Fix | Delete
return $href;
[315] Fix | Delete
}
[316] Fix | Delete
}
[317] Fix | Delete
}
[318] Fix | Delete
return null;
[319] Fix | Delete
}
[320] Fix | Delete
[321] Fix | Delete
public function extension(&$array)
[322] Fix | Delete
{
[323] Fix | Delete
foreach ($array as $key => $value) {
[324] Fix | Delete
if ($this->checked_feeds === $this->max_checked_feeds) {
[325] Fix | Delete
break;
[326] Fix | Delete
}
[327] Fix | Delete
if (in_array(strtolower(strrchr($value, '.')), ['.rss', '.rdf', '.atom', '.xml'])) {
[328] Fix | Delete
$this->checked_feeds++;
[329] Fix | Delete
[330] Fix | Delete
$headers = [
[331] Fix | Delete
'Accept' => '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',
[332] Fix | Delete
];
[333] Fix | Delete
$feed = $this->registry->create(File::class, [$value, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options]);
[334] Fix | Delete
if ($feed->success && ($feed->method & \SimplePie\SimplePie::FILE_SOURCE_REMOTE === 0 || ($feed->status_code === 200 || $feed->status_code > 206 && $feed->status_code < 300)) && $this->is_feed($feed)) {
[335] Fix | Delete
return [$feed];
[336] Fix | Delete
} else {
[337] Fix | Delete
unset($array[$key]);
[338] Fix | Delete
}
[339] Fix | Delete
}
[340] Fix | Delete
}
[341] Fix | Delete
return null;
[342] Fix | Delete
}
[343] Fix | Delete
[344] Fix | Delete
public function body(&$array)
[345] Fix | Delete
{
[346] Fix | Delete
foreach ($array as $key => $value) {
[347] Fix | Delete
if ($this->checked_feeds === $this->max_checked_feeds) {
[348] Fix | Delete
break;
[349] Fix | Delete
}
[350] Fix | Delete
if (preg_match('/(feed|rss|rdf|atom|xml)/i', $value)) {
[351] Fix | Delete
$this->checked_feeds++;
[352] Fix | Delete
$headers = [
[353] Fix | Delete
'Accept' => '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',
[354] Fix | Delete
];
[355] Fix | Delete
$feed = $this->registry->create(File::class, [$value, $this->timeout, 5, null, $this->useragent, $this->force_fsockopen, $this->curl_options]);
[356] Fix | Delete
if ($feed->success && ($feed->method & \SimplePie\SimplePie::FILE_SOURCE_REMOTE === 0 || ($feed->status_code === 200 || $feed->status_code > 206 && $feed->status_code < 300)) && $this->is_feed($feed)) {
[357] Fix | Delete
return [$feed];
[358] Fix | Delete
} else {
[359] Fix | Delete
unset($array[$key]);
[360] Fix | Delete
}
[361] Fix | Delete
}
[362] Fix | Delete
}
[363] Fix | Delete
return null;
[364] Fix | Delete
}
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
class_alias('SimplePie\Locator', 'SimplePie_Locator', false);
[368] Fix | Delete
[369] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function