+/*
+ Changes in extensions system.
+ This function is executed once on first Upgrade System display.
+*/
+function fix_extensions() {
+ global $path_to_root, $db_connections;
+
+ if (!file_exists($path_to_root.'/modules/installed_modules.php'))
+ return true; // already converted
+
+ if (!is_writable($path_to_root.'/modules/installed_modules.php')) {
+ display_error(_('Cannot upgrade extensions system: file /modules/installed_modules.php is not writeable'));
+ return false;
+ }
+
+ $exts = array();
+ include($path_to_root.'/installed_extensions.php');
+ foreach($installed_extensions as $ext) {
+ $ext['filename'] = $ext['app_file']; unset($ext['app_file']);
+ $ext['tab'] = $ext['name'];
+ $ext['name'] = access_string($ext['title'], true);
+ $ext['path'] = $ext['folder']; unset($ext['folder']);
+ $ext['type'] = 'extension';
+ $ext['active'] = '1';
+ $exts[] = $ext;
+ }
+
+ if (!write_extensions($exts))
+ return false;
+
+ $cnt = count($db_connections);
+ for ($i = 0; $i < $cnt; $i++)
+ write_extensions($exts, $i);
+
+ unlink($path_to_root.'/modules/installed_modules.php');
+ return true;
+}
+
+/*
+ Find and update all database records with special chars in text fields
+ to ensure all of them are changed to html entites.
+*/
+function sanitize_database($pref, $test = false) {
+
+ if ($test)
+ error_log('Sanitizing database ...');
+
+ $tsql = "SHOW TABLES LIKE '".($pref=='' ? '' : substr($pref,0,-1).'\\_')."%'";
+ $tresult = db_query($tsql, "Cannot select all tables with prefix '$pref'");
+ while($tbl = db_fetch($tresult)) {
+ $table = $tbl[0];
+ $csql = "SHOW COLUMNS FROM $table";
+ $cresult = db_query($csql, "Cannot select column names for table '$table'");
+ $textcols = $keys = array();
+ while($col = db_fetch($cresult)) {
+ if (strpos($col['Type'], 'char')!==false
+ || strpos($col['Type'], 'text')!==false)
+ $textcols[] = '`'.$col['Field'].'`';
+ if ($col['Key'] == 'PRI') {
+ $keys[] = '`'.$col['Field'].'`';
+ }
+ }
+
+ if (empty($keys)) { // comments table have no primary key, so give up
+ continue;
+ }
+ if ($test)
+ error_log("Table $table (".implode(',',$keys)."):(".implode(',',$textcols)."):");
+
+ if (!count($textcols)) continue;