From: <jon...@us...> - 2014-01-07 14:47:16
|
Revision: 49329 http://sourceforge.net/p/tikiwiki/code/49329 Author: jonnybradley Date: 2014-01-07 14:47:12 +0000 (Tue, 07 Jan 2014) Log Message: ----------- [FIX] Check for BOM in custom.php in init_language and fix if possible (thanks marclaporte) Modified Paths: -------------- branches/12.x/lib/init/tra.php branches/12.x/lib/tikiaccesslib.php Modified: branches/12.x/lib/init/tra.php =================================================================== --- branches/12.x/lib/init/tra.php 2014-01-07 14:45:15 UTC (rev 49328) +++ branches/12.x/lib/init/tra.php 2014-01-07 14:47:12 UTC (rev 49329) @@ -83,12 +83,19 @@ } } - if (is_file("lang/$lg/custom.php")) { - include_once("lang/$lg/custom.php"); + $access = TikiLib::lib('access'); + $customfile = "lang/$lg/custom.php"; + if (is_file($customfile)) { + if (! $access->check_file_BOM($customfile)) { + include_once($customfile); + } } - if (!empty($tikidomain) && is_file("lang/$lg/$tikidomain/custom.php")) { - include_once("lang/$lg/$tikidomain/custom.php"); + $customfile = "lang/$lg/$tikidomain/custom.php"; + if (!empty($tikidomain) && is_file($customfile)) { + if (! $access->check_file_BOM($customfile)) { + include_once($customfile); + } } if ( isset( $prefs['lang_use_db'] ) && $prefs['lang_use_db'] == 'y' ) { Modified: branches/12.x/lib/tikiaccesslib.php =================================================================== --- branches/12.x/lib/tikiaccesslib.php 2014-01-07 14:45:15 UTC (rev 49328) +++ branches/12.x/lib/tikiaccesslib.php 2014-01-07 14:47:12 UTC (rev 49329) @@ -307,6 +307,42 @@ } } + /** + * Checks a php file for a Byte Order Mark (BOM) and trigger error (and report error for admin) + * + * @param string $filename full path of file to check + * @param bool $try_to_fix if file perms allow remove BOM if found + * + * @return bool true if file still has a BOM + */ + function check_file_BOM($filename, $try_to_fix = true) { + $BOM_found = false; + + if (is_readable($filename)) { + $file = @fopen($filename, "r"); + $BOM_found = (fread($file, 3) === "\xEF\xBB\xBF"); + + if ($try_to_fix && $BOM_found && is_writable($filename)) { + $content = fread($file, filesize($filename)); + fclose($file); + file_put_contents($filename, $content); + trigger_error('File "' . $filename . '" contained a BOM which has been fixed.'); + $BOM_found = false; + } else { + fclose($file); + } + } + if ($BOM_found) { + $message = 'Warning: File "' . $filename . '" contains a BOM which cannot be fixed. Please re-edit and save as "UTF-8 without BOM"'; + if (Perms::get()->admin) { + TikiLib::lib('errorreport')->report($message); + } + trigger_error($message); + } + + return $BOM_found; + } + /** * @param $page * @param string $errortitle This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |