- 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 for $host ($stylesheet not found)", 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;
- }
-
- // Strip the leading www. to be more forgiving on input URLs
- $host = preg_replace('/^www\./', '', $url["host"]);
-
- $xsltProcessor = $this->load_stylesheet($host);
- if (NULL === $xsltProcessor) {
- return NULL;
- }
-
- $html = $this->get_contents($src_url);
- if (FALSE === $html) {
- return NULL;
- }
-
- $xmlDoc = $this->html_to_xml($html, $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;
- }
-}