X-Git-Url: https://git.ao2.it/tweeper.git/blobdiff_plain/3e82c7699c0cf07313fe1ae7d5c5de18b13da470..44f33491eb6a10a5860489cf75c52cc7456ca53c:/tweeper.php?ds=inline diff --git a/tweeper.php b/tweeper.php index 14c3a7a..1d50f60 100644 --- a/tweeper.php +++ b/tweeper.php @@ -51,12 +51,13 @@ class Tweeper { { $ch = curl_init($url); curl_setopt_array($ch, array( - CURLOPT_HEADER => FALSE, - CURLOPT_RETURNTRANSFER => TRUE, - CURLOPT_SSL_VERIFYHOST => FALSE, - CURLOPT_SSL_VERIFYPEER => FALSE, - CURLOPT_HTTPHEADER => array('Accept-language: en'), - CURLOPT_USERAGENT => Tweeper::$USER_AGENT, + CURLOPT_HEADER => FALSE, + CURLOPT_FOLLOWLOCATION => TRUE, // follow http redirects to get the real URL + CURLOPT_RETURNTRANSFER => TRUE, + CURLOPT_SSL_VERIFYHOST => FALSE, + CURLOPT_SSL_VERIFYPEER => FALSE, + CURLOPT_HTTPHEADER => array('Accept-language: en'), + CURLOPT_USERAGENT => Tweeper::$USER_AGENT, )); $contents = curl_exec($ch); curl_close($ch); @@ -68,13 +69,13 @@ class Tweeper { { $ch = curl_init($url); curl_setopt_array($ch, array( - CURLOPT_HEADER => TRUE, - CURLOPT_NOBODY => TRUE, - CURLOPT_FOLLOWLOCATION => TRUE, // follow http redirects to get the real URL - CURLOPT_RETURNTRANSFER => TRUE, - CURLOPT_SSL_VERIFYHOST => FALSE, - CURLOPT_SSL_VERIFYPEER => FALSE, - CURLOPT_USERAGENT => Tweeper::$USER_AGENT, + CURLOPT_HEADER => TRUE, + CURLOPT_NOBODY => TRUE, + CURLOPT_FOLLOWLOCATION => TRUE, // follow http redirects to get the real URL + CURLOPT_RETURNTRANSFER => TRUE, + CURLOPT_SSL_VERIFYHOST => FALSE, + CURLOPT_SSL_VERIFYPEER => FALSE, + CURLOPT_USERAGENT => Tweeper::$USER_AGENT, )); curl_exec($ch); $url_info = curl_getinfo($ch); @@ -121,11 +122,48 @@ class Tweeper { 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); + } + public function tweep($uri) { $html = Tweeper::get_contents($uri); $xmlDoc = new DOMDocument(); + + // Handle warnings and errors when loading invalid HTML. + $xml_errors_value = libxml_use_internal_errors(true); $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); $output = $this->xsltProcessor->transformToXML($xmlDoc); @@ -167,7 +205,8 @@ function parse_options_cli($argv, $argc) echo usage($argv); exit(0); default: - die(usage($argv)); + fwrite(STDERR, usage($argv)); + exit(1); } } @@ -192,21 +231,30 @@ function parse_options_query_string() } -if (php_sapi_name() != 'cli') +if (php_sapi_name() != 'cli') { $options = parse_options_query_string(); -else + $ERROR_STREAM = fopen('php://output', 'w'); +} else { $options = parse_options_cli($argv, $argc); + $ERROR_STREAM = fopen('php://stderr', 'w'); +} -if (!isset($options['src_url'])) - die(usage($argv)); +if (!isset($options['src_url'])) { + fwrite($ERROR_STREAM, usage($argv)); + exit(1); +} $url = parse_url($options['src_url']); -if (FALSE === $url || empty($url["host"])) - die("Invalid url: ${options['src_url']}\n"); +if (FALSE === $url || empty($url["host"])) { + fwrite($ERROR_STREAM, "Invalid url: ${options['src_url']}\n"); + exit(1); +} $stylesheet = "file://" . __DIR__ . "/rss_converter_" . $url["host"] . ".xsl"; -if (FALSE === file_exists($stylesheet)) - die("Conversion to RSS not supported: {$url["host"]}\n"); +if (FALSE === file_exists($stylesheet)) { + fwrite($ERROR_STREAM, "Conversion to RSS not supported: {$url["host"]}\n"); + exit(1); +} $tweeper = new Tweeper($stylesheet, $options['generate_enclosure']); echo $tweeper->tweep($options['src_url']);