From e0d25803d2e22a40e72f091563c27220948e15b8 Mon Sep 17 00:00:00 2001 From: Morten Fangel Date: Wed, 10 Feb 2010 13:00:50 +0100 Subject: [PATCH] Changed the Fetch-method on the Parser so it supports local files --- SG_iCal.php | 0 blocks/SG_iCal_VCalendar.php | 0 blocks/SG_iCal_VEvent.php | 0 blocks/SG_iCal_VTimeZone.php | 0 helpers/SG_iCal_Duration.php | 0 helpers/SG_iCal_Factory.php | 0 helpers/SG_iCal_Freq.php | 0 helpers/SG_iCal_Line.php | 0 helpers/SG_iCal_Parser.php | 48 +++++++++++++++++++++++------------- helpers/SG_iCal_Query.php | 0 tests/common.php | 0 tests/helpers/FreqTest.php | 0 12 files changed, 31 insertions(+), 17 deletions(-) mode change 100644 => 100755 SG_iCal.php mode change 100644 => 100755 blocks/SG_iCal_VCalendar.php mode change 100644 => 100755 blocks/SG_iCal_VEvent.php mode change 100644 => 100755 blocks/SG_iCal_VTimeZone.php mode change 100644 => 100755 helpers/SG_iCal_Duration.php mode change 100644 => 100755 helpers/SG_iCal_Factory.php mode change 100644 => 100755 helpers/SG_iCal_Freq.php mode change 100644 => 100755 helpers/SG_iCal_Line.php mode change 100644 => 100755 helpers/SG_iCal_Parser.php mode change 100644 => 100755 helpers/SG_iCal_Query.php mode change 100644 => 100755 tests/common.php mode change 100644 => 100755 tests/helpers/FreqTest.php diff --git a/SG_iCal.php b/SG_iCal.php old mode 100644 new mode 100755 diff --git a/blocks/SG_iCal_VCalendar.php b/blocks/SG_iCal_VCalendar.php old mode 100644 new mode 100755 diff --git a/blocks/SG_iCal_VEvent.php b/blocks/SG_iCal_VEvent.php old mode 100644 new mode 100755 diff --git a/blocks/SG_iCal_VTimeZone.php b/blocks/SG_iCal_VTimeZone.php old mode 100644 new mode 100755 diff --git a/helpers/SG_iCal_Duration.php b/helpers/SG_iCal_Duration.php old mode 100644 new mode 100755 diff --git a/helpers/SG_iCal_Factory.php b/helpers/SG_iCal_Factory.php old mode 100644 new mode 100755 diff --git a/helpers/SG_iCal_Freq.php b/helpers/SG_iCal_Freq.php old mode 100644 new mode 100755 diff --git a/helpers/SG_iCal_Line.php b/helpers/SG_iCal_Line.php old mode 100644 new mode 100755 diff --git a/helpers/SG_iCal_Parser.php b/helpers/SG_iCal_Parser.php old mode 100644 new mode 100755 index 2ccaa30..bf295d9 --- a/helpers/SG_iCal_Parser.php +++ b/helpers/SG_iCal_Parser.php @@ -23,27 +23,41 @@ class SG_iCal_Parser { } /** - * Fetches url and stores it in the content member + * Fetches a resource and tries to make sure it's UTF8 + * encoded * @return string */ - protected static function Fetch( $url ) { - $c = curl_init(); + protected static function Fetch( $resource ) { $is_utf8 = true; - curl_setopt($c, CURLOPT_URL, $url); - curl_setopt($c, CURLOPT_RETURNTRANSFER, true); - if( !ini_get('safe_mode') ){ - curl_setopt($c, CURLOPT_FOLLOWLOCATION, true); - } - $content = curl_exec($c); - $ct = curl_getinfo($c, CURLINFO_CONTENT_TYPE); - $enc = preg_replace('/^.*charset=([-a-zA-Z0-9]+).*$/', '$1', $ct); - if( $ct != '' && strtolower(str_replace('-','', $enc)) != 'utf8' ) { - // Well, the encoding says it ain't utf-8 - $is_utf8 = false; - } elseif( ! self::_ValidUtf8( $content ) ) { - // The data isn't utf-8 - $is_utf8 = false; + if( is_file( $resource ) ) { + // The resource is a local file + $content = file_get_contents($resource); + + if( ! self::_ValidUtf8( $content ) ) { + // The file doesn't appear to be UTF8 + $is_utf8 = false; + } + } else { + // The resource isn't local, so it's assumed to + // be a URL + $c = curl_init(); + curl_setopt($c, CURLOPT_URL, $resource); + curl_setopt($c, CURLOPT_RETURNTRANSFER, true); + if( !ini_get('safe_mode') ){ + curl_setopt($c, CURLOPT_FOLLOWLOCATION, true); + } + $content = curl_exec($c); + + $ct = curl_getinfo($c, CURLINFO_CONTENT_TYPE); + $enc = preg_replace('/^.*charset=([-a-zA-Z0-9]+).*$/', '$1', $ct); + if( $ct != '' && strtolower(str_replace('-','', $enc)) != 'utf8' ) { + // Well, the encoding says it ain't utf-8 + $is_utf8 = false; + } elseif( ! self::_ValidUtf8( $content ) ) { + // The data isn't utf-8 + $is_utf8 = false; + } } if( !$is_utf8 ) { diff --git a/helpers/SG_iCal_Query.php b/helpers/SG_iCal_Query.php old mode 100644 new mode 100755 diff --git a/tests/common.php b/tests/common.php old mode 100644 new mode 100755 diff --git a/tests/helpers/FreqTest.php b/tests/helpers/FreqTest.php old mode 100644 new mode 100755