From: <var...@us...> - 2016-02-10 17:06:04
|
Revision: 9787 http://sourceforge.net/p/phpwiki/code/9787 Author: vargenau Date: 2016-02-10 17:06:01 +0000 (Wed, 10 Feb 2016) Log Message: ----------- HtmlConverter plugin: check file is encoded in UTF-8 Modified Paths: -------------- trunk/lib/plugin/HtmlConverter.php trunk/lib/stdlib.php Modified: trunk/lib/plugin/HtmlConverter.php =================================================================== --- trunk/lib/plugin/HtmlConverter.php 2016-02-10 14:57:07 UTC (rev 9786) +++ trunk/lib/plugin/HtmlConverter.php 2016-02-10 17:06:01 UTC (rev 9787) @@ -59,7 +59,7 @@ 'name' => 'MAX_FILE_SIZE', 'value' => MAX_UPLOAD_SIZE))); $contents->pushContent(HTML::input(array('name' => 'userfile', - 'type' => 'file'))); + 'type' => 'file', 'required' => 'required'))); $contents->pushContent(HTML::raw(" ")); $contents->pushContent(HTML::input(array('value' => _("Convert"), 'type' => 'submit'))); @@ -73,10 +73,10 @@ $userfile_tmpname = $userfile->getTmpName(); if (!preg_match("/(\.html|\.htm)$/i", $userfile_name)) { - $message->pushContent(_("Only files with extension HTML are allowed"), HTML::br(), HTML::br()); + $message->pushContent(HTML::p(array('class' => 'error'), + _("Only files with extension HTML are allowed"))); } else { - $message->pushContent(_("Processed $userfile_name"), HTML::br(), HTML::br()); - $message->pushContent(_("Copy the output below and paste it into your Wiki page."), HTML::br()); + $message->pushContent(HTML::p(_("Processed $userfile_name"))); $message->pushContent($this->process($userfile_tmpname)); } } else { @@ -130,6 +130,11 @@ { $result = HTML(); $file = file_get_contents($file_name); + if (!is_utf8($file)) { + $result->pushContent(HTML::p(array('class' => 'error'), + _("Error: file must be encoded in UTF-8"))); + return $result; + } $file = html_entity_decode($file); $this->processA($file); @@ -180,6 +185,8 @@ // strip attributes from <pre>-Tags and add a new-line before $file = preg_replace("_<pre(\s[^>]*|)>_iU", "\n<pre>", $file); + $result->pushContent(HTML::p(_("Copy the output below and paste it into your Wiki page."))); + $outputArea = HTML::textarea(array('rows' => '30', 'cols' => '80')); $outputArea->pushContent(_($file)); Modified: trunk/lib/stdlib.php =================================================================== --- trunk/lib/stdlib.php 2016-02-10 14:57:07 UTC (rev 9786) +++ trunk/lib/stdlib.php 2016-02-10 17:06:01 UTC (rev 9787) @@ -2162,3 +2162,37 @@ } echo "</body></html>\n"; } + +/** + * is_utf8 - utf-8 detection + * + * @param string $str the string to analyze + * @return bool + * + * From http://www.php.net/manual/en/function.mb-detect-encoding.php#85294 + */ +function is_utf8($str) { + $c=0; $b=0; + $bits=0; + $len=strlen($str); + for($i=0; $i<$len; $i++){ + $c=ord($str[$i]); + if($c > 128){ + if(($c >= 254)) return false; + elseif($c >= 252) $bits=6; + elseif($c >= 248) $bits=5; + elseif($c >= 240) $bits=4; + elseif($c >= 224) $bits=3; + elseif($c >= 192) $bits=2; + else return false; + if(($i+$bits) > $len) return false; + while($bits > 1){ + $i++; + $b=ord($str[$i]); + if($b < 128 || $b > 191) return false; + $bits--; + } + } + } + return true; +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |