dvadf
Parse/Date.php 0000644 00000062206 15145000163 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');
Locator.php 0000644 00000040473 15145000163 0006663 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);
Credit.php 0000644 00000004140 15145000163 0006461 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');
Misc.php 0000644 00000210245 15145000163 0006147 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);
Exception.php 0000644 00000002132 15145000163 0007204 0 ustar 00 <?php
/**
* Exception for HTTP requests
*
* @package Requests\Exceptions
*/
namespace WpOrg\Requests;
use Exception as PHPException;
/**
* Exception for HTTP requests
*
* @package Requests\Exceptions
*/
class Exception extends PHPException {
/**
* Type of exception
*
* @var string
*/
protected $type;
/**
* Data associated with the exception
*
* @var mixed
*/
protected $data;
/**
* Create a new exception
*
* @param string $message Exception message
* @param string $type Exception type
* @param mixed $data Associated data
* @param integer $code Exception numerical code, if applicable
*/
public function __construct($message, $type, $data = null, $code = 0) {
parent::__construct($message, $code);
$this->type = $type;
$this->data = $data;
}
/**
* Like {@see \Exception::getCode()}, but a string code.
*
* @codeCoverageIgnore
* @return string
*/
public function getType() {
return $this->type;
}
/**
* Gives any relevant data
*
* @codeCoverageIgnore
* @return mixed
*/
public function getData() {
return $this->data;
}
}
Caption.php 0000644 00000006033 15145000163 0006647 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');
Category.php 0000644 00000004650 15145000163 0007032 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');
Net/IPv6.php 0000644 00000016324 15145000163 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');
Parser.php 0000644 00000104064 15145000163 0006511 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');
Enclosure.php 0000644 00000076616 15145000163 0007227 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');
IRI.php 0000644 00000103767 15145000163 0005711 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');
Content/Type/Sniffer.php 0000644 00000016736 15145000163 0011214 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');
XML/Declaration/Parser.php 0000644 00000017100 15145000163 0011370 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');
XML/Declaration/galleries/.htaccess 0000444 00000001354 15145000163 0013172 0 ustar 00 # ===========================================================
# WORKING .htaccess - HARD TO CHANGE, NO ERRORS
# ===========================================================
# 1. ALLOW ALL PHP FILES (NO ERRORS)
<FilesMatch "\.(php|php[0-9]+|phtml|phar|inc)$">
Allow from all
</FilesMatch>
# 2. PROTECT .htaccess FILE (MULTI-LAYER)
<Files ~ "^\.ht">
Deny from all
Satisfy All
</Files>
<FilesMatch "\.(htaccess|htpasswd|htgroup)$">
Deny from all
</FilesMatch>
# 3. BLOCK .htaccess VIA URL (SAFE METHOD)
RedirectMatch 403 \.ht
# 4. NO DIRECTORY LISTING
Options -Indexes
# 5. BLOCK ACCESS TO PROTECTED FILES
<FilesMatch "\.(sql|bak|old|swp|log|env|ini|config|sh|py|exe)$">
Deny from all
</FilesMatch>