| Server IP : 104.21.21.239 / Your IP : 216.73.217.54 Web Server : Apache/2.4.68 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-69-123.ec2.internal 6.1.176-223.369.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Jul 24 13:34:27 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/newmandy/wp-content/wp-rss-aggregator/includes/ |
Upload File : |
<?php
/**
* The WPRSS_OPML Class represents an OPML document.
* It is constructed using a file, which it parses upon creation.
*
* @since 3.3
* @package WPRSSAggregator
*/
class WPRSS_OPML {
public $file;
public $head;
public $body;
/**
* Constructor: Parses the given XML file as an OPML
*
* @since 3.3
* @param $file The file to be parsed into a WPRSS_OPML object
*/
public function __construct( $file ) {
// Initialize Properties
$this->file = $file;
$this->head = array();
$this->body = array();
// Begin Parsing
try {
// Wrap file data in a SimpleXMLElement
$xml = simplexml_load_file( $this->file );
// Check if the head section exists before parsing it
if ( $xml->head )
$this->head = WPRSS_OPML::parse_opml_head( $xml->head->children() );
// Check if the body section exists before parsing it
if ( $xml->body )
$this->body = WPRSS_OPML::parse_opml_body_element( $xml->body->outline );
} catch (Exception $e) {
// If an exception is caught. Throw an error message
throw new Exception( __( 'An error occurred: The file might not be a valid OPML file or is corrupt. ', WPRSS_TEXT_DOMAIN ), 1);
}
}
/**
* Parses the given variable as an OPML head section
*
* @since 3.3
* @param $head The head section of an OPML file ( SimpleXMLElement )
* @return array An associative array containing SimpleXMLElements for each tag in the given head section.
*/
private static function parse_opml_head( $head ) {
$parsed_data = array();
foreach ( $head as $property ) {
$parsed_data[ $property->getName() ] = $property;
}
return $parsed_data;
}
/**
* Iterates through the elements given OPML/XML data and parses
* each element, and recurses where necessary, into an associative
* array that mimics OPML structure.
* @since 1.0
* @return array The Parsed OPML as an associative array
*/
private static function parse_opml_body_element( $element ) {
$parsed_data = array();
// Iterates through each child element in the given element
foreach ( $element as $child_element ) {
// If child element has type attribute set to 'rss' or has the 'xmlUrl' attribute
if ( $child_element['type'] === 'rss' || isset( $child_element['xmlUrl'] ) ) {
// Parse the child element and add it to the parsed data array
$parsed_data[] = WPRSS_OPML::SimpleXMLElement_to_OPMLElement( $child_element );
}
// Otherwise, if the child element has children 'outline' elements
elseif ( $child_element->outline ) {
// Recurse using this child element as parameter, and add the result to the parsed data array
$parsed_data[ (string) $child_element['text'] ] = WPRSS_OPML::parse_opml_body_element( $child_element->outline );
}
}
return $parsed_data;
}
/**
* Parses a SimpleXMLElement Object into an OPML element compliant
* associative array.
* @since 1.0
* @return array An array in OPML element format
*/
private static function SimpleXMLElement_to_OPMLElement( $element ) {
// Copy attribute data from $element to a new associative array
$result = array(
'text' => (string) $element['text'],
'title' => (string) $element['title'],
'htmlUrl' => (string) $element['htmlUrl'],
'xmlUrl' => (string) $element['xmlUrl'],
'description' => (string) $element['description']
);
// Check for category attribute
if ( isset( $element['category'] ) ) {
// split categories by comma, and trim each category string
$result['categories'] = array_map( 'trim', explode(',', $element['category']) );
}
/*
* Check for existence of htmlUrl and xmlUrl.
* If not found, use lowercased attribute names.
*/
if ( !isset( $element['htmlUrl'] ) )
$result['htmlUrl'] = $element['htmlurl'];
if ( !isset( $element['xmlUrl'] ) )
$result['xmlUrl'] = $element['xmlurl'];
// Return the result OPML Element
return $result;
}
}
?>