- return $dom->saveXML($enc);
- }
-
- /* Mimic the message from libxml.c::php_libxml_ctx_error_level() */
- private function log_xml_error($error) {
- $output = "";
-
- switch ($error->level) {
- case LIBXML_ERR_WARNING:
- $output .= "Warning $error->code: ";
- break;
- case LIBXML_ERR_ERROR:
- $output .= "Error $error->code: ";
- break;
- case LIBXML_ERR_FATAL:
- $output .= "Fatal Error $error->code: ";
- break;
- }
-
- $output .= trim($error->message);
-
- if ($error->file) {
- $output .= " in $error->file";
- } else {
- $output .= " in Entity,";
- }
-
- $output .=" line $error->line";
-
- error_log($output);
- }
-
- private function load_stylesheet($host) {
- $stylesheet = "file://" . __DIR__ . "/rss_converter_" . $host . ".xsl";
- if (FALSE === file_exists($stylesheet)) {
- trigger_error("Conversion to RSS not supported: $host", E_USER_ERROR);
- return NULL;
- }
-
- $stylesheet_contents = $this->get_contents($stylesheet);
-
- $xslDoc = new DOMDocument();
- $xslDoc->loadXML($stylesheet_contents);
-
- $xsltProcessor = new XSLTProcessor();
- $xsltProcessor->registerPHPFunctions();
- $xsltProcessor->setParameter('', 'generateEnclosure', $this->generate_enclosure);
- $xsltProcessor->importStylesheet($xslDoc);
-
- return $xsltProcessor;
- }
-
- private function json_to_xml($html, $json_match_expr, $rootName) {
- // pre-process, convert json to XML
- $ret = preg_match($json_match_expr, $html, $matches);
- if ($ret !== 1) {
- trigger_error("Cannot match expression: $json_match_expr\n", E_USER_ERROR);
- return NULL;
- }
-
- $data = json_decode($matches[1]);
- if (!$data) {
- return NULL;
- }
-
- $serializer_options = array (
- 'addDecl' => TRUE,
- 'encoding' => "UTF-8",
- 'indent' => ' ',
- 'rootName' => $rootName,
- );
-
- $serializer = new XML_Serializer($serializer_options);
-
- $status = $serializer->serialize($data);
- if (PEAR::isError($status)) {
- trigger_error($status->getMessage(), E_USER_ERROR);
- return NULL;
- }
-
- return $serializer->getSerializedData();
- }
-
- private function get_xml_instagram_com($html) {
- return $this->json_to_xml($html, '/window._sharedData = (.*);/', 'instagram');
- }
-
- private function html_to_xml($html, $host) {
- $xmlDoc = new DOMDocument();
-
- // Handle warnings and errors when loading invalid HTML.
- $xml_errors_value = libxml_use_internal_errors(true);
-
- // If there is a host-specific method to get the xml data, use it!
- $get_xml_host_method = 'get_xml_' . str_replace(".", "_", $host);
- if (method_exists($this, $get_xml_host_method)) {
- $xml_data = call_user_func_array(array($this, $get_xml_host_method), array($html));
- $xmlDoc->loadXML($xml_data);
- } else {
- $xmlDoc->loadHTML($html);
- }
-
- foreach (libxml_get_errors() as $xml_error) {
- $this->log_xml_error($xml_error);
- }
- libxml_clear_errors();
- libxml_use_internal_errors($xml_errors_value);
-
- return $xmlDoc;
- }
-
- public function tweep($src_url) {
- $url = parse_url($src_url);
- if (FALSE === $url || empty($url["host"])) {
- trigger_error("Invalid url: $src_url", E_USER_ERROR);
- return NULL;
- }
-
- $xsltProcessor = $this->load_stylesheet($url["host"]);
- if (NULL === $xsltProcessor) {
- return NULL;
- }
-
- $html = $this->get_contents($src_url);
- if (FALSE === $html) {
- return NULL;
- }
-
- $xmlDoc = $this->html_to_xml($html, $url["host"]);
- if (NULL === $xmlDoc) {
- return NULL;
- }
-
- $output = $xsltProcessor->transformToXML($xmlDoc);
-
- if (FALSE === $output) {
- trigger_error('XSL transformation failed.', E_USER_ERROR);
- return NULL;
- }
- return $output;
- }
-}