1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) { 9 header("location: index.php"); 10 exit; 11} 12 13/** 14 * We change the client_charset value from 'utf8' to 'utf8mb4' if it is still 'utf8'. 15 * Since db/local.php is a very important file, we keep a backup if we edit it. 16 * 17 * @param $installer 18 */ 19function upgrade_20181127_convert_db_local_to_utf8mb4_tiki($installer) 20{ 21 $localfile = TikiInit::getCredentialsFile(); 22 $date = date("Ymd"); 23 $time = date("His"); 24 // Unique name so as to avoid losing the backup in case something goes wrong 25 $backuplocalfile = "db/obsolete_${date}-${time}_local.php"; 26 27 // Parse local.php file and look for obsolete 'utf8' client_charset value 28 $contents = @file($localfile); 29 30 if ($contents !== false) { 31 $last_matched_line = ""; 32 $last_matched_charset = ""; 33 foreach ($contents as $key => $line) { 34 // Detect last value and line for client_charset 35 $extract = preg_match("/^[ ]*[$]client_charset[ ]*=[ ]*['\"](.*)['\"];/", $line, $match); 36 if (isset($match[1])) { 37 // echo "Match: " . $match[1] . PHP_EOL; 38 $last_matched_line = $key; 39 $last_matched_charset = $match[1]; 40 } 41 } 42 } else { 43 echo "Failed to read 'db/local.php'" . PHP_EOL; 44 echo "Please edit db/local.php manually and change the \$client_charset value from 'utf8' to 'utf8mb4'" . PHP_EOL; 45 return true; 46 } 47 48 // If obsolete 'utf8' client_charset value was found, backup and edit 49 if ($last_matched_charset == 'utf8') { 50 // Backup db/local.php 51 if (! rename($localfile, $backuplocalfile)) { 52 echo "Failed to backup 'db/local.php'" . PHP_EOL; 53 echo "Please edit db/local.php manually and change the \$client_charset value from 'utf8' to 'utf8mb4'" . PHP_EOL; 54 return false; 55 } 56 // Rewrite the new db/local.php 57 $handle = fopen($localfile, 'xb'); 58 $contents[$last_matched_line] = "// Commented by installer on ${date} // " . $contents[$last_matched_line] . "\$client_charset='utf8mb4';" . PHP_EOL; 59 reset($contents); 60 foreach ($contents as $key => $line) { 61 fwrite($handle, $line); 62 } 63 fclose($handle); 64 return true; 65 } elseif ($last_matched_charset == 'utf8mb4') { 66 // Nothing to do, leave with success return code 67 // echo "debug nothing to do\n"; 68 return true; 69 } else { 70 echo "Please edit db/local.php manually and change the \$client_charset value from '" . $last_matched_charset . "' to 'utf8mb4'" . PHP_EOL; 71 return true; 72 } 73 74 // This place should not be reached 75 return false; 76} 77