summaryrefslogtreecommitdiffstats
path: root/bin/fix_filenames.inc
blob: 94531b89ded5caf3bf4ad8678e2361979492a7c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */
/**
 *
 * LICENSE: GNU General Public License, version 2 (GPLv2)
 * Copyright 2001 - 2013 Ampache.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License v2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

define('NO_SESSION','1');
$path = dirname(__FILE__);
$prefix = realpath($path . '/../');
require_once $prefix . '/lib/init.php';

ob_end_clean();

/*
 * Pull the root path of your catalogs one by one
 * and then do a directory sweep and check all of the files
 * that would be cataloged and see if they have the correct charset
 * if they don't prompt for a rename, unless $i_am_crazy is true then just
 * do it
 */

// If set to true / 1 then it will not prompt!
//$GLOBALS['i_am_crazy'] = true;

if (!function_exists('iconv')) {
	echo T_("ERROR: Iconv required for this functionality, quiting");
	echo "\n";
	exit;
}

// Attempt to figure out what the System Charset is
$source_encoding = iconv_get_encoding('output_encoding');

// Attempt a simple translation
$string = sprintf(T_('%s For the Love of Music'), iconv(Config::get('site_charset'),Config::get('site_charset')));
echo T_('Testing Basic Translation, the two strings below should look the same');
echo "\n";
echo T_('Original: For the Love of Music');
echo "\n";
printf (T_('Translated: %s'), $string);
echo "\n";
echo "---------------------------------------------------------------------\n";
printf (T_('Input Charset (%s):'), $source_encoding);
$input = trim(fgets(STDIN));

if (strlen($input) > 0) { $source_encoding = trim($input); }
printf (T_('Using %s as source character set'), $source_encoding);
echo "\n";

$sql = "SELECT * FROM `catalog` WHERE `catalog_type`='local'";
$db_results = Dba::read($sql);

while ($row = Dba::fetch_assoc($db_results)) {

	printf(T_('Checking %s (%s)'), $row['name'], $row['path']);
	echo "\n";
	charset_directory_correct($row['path']);

} // end of the catalogs

echo T_('Finished checking filenames for valid chacters');
echo "\n";

/**************************************************
 ****************** FUNCTIONS *********************
 **************************************************/
/**
 * charset_directory_correct
 * This function calls its self recursivly
 * and corrects all of the non-matching filenames
 * it looks at the i_am_crazy var and if not set prompts for change
 */
function charset_directory_correct($path) {

	// Correctly detect the slash we need to use here
        if (strstr($path,"/")) {
        	$slash_type = '/';
	}
        else {
        	$slash_type = '\\';
	}

	/* Open up the directory */
        $handle = opendir($path);

	if (!is_resource($handle)) {
		printf (T_('ERROR: Unable to open %s'), $path);
		echo "\n";
		return false;
	}

	if (!chdir($path)) {
		printf (T_('ERROR: Unable to chdir to %s'), $path);
		echo "\n";
		return false;
	}

	while ( false !== ($file = readdir($handle) ) ) {

		if ($file == '.' || $file == '..') { continue; }

		$full_file = $path.$slash_type.$file;

		if (is_dir($full_file)) {
			charset_directory_correct($full_file);
			continue;
		}

		$verify_filename = iconv(Config::get('site_charset'),Config::get('site_charset') . '//IGNORE',$full_file);

		if (strcmp($full_file,$verify_filename) != '0') {
			$translated_filename = iconv($source_encoding,Config::get('site_charset') . '//TRANSLIT',$full_file);

			// Make sure the extension stayed the same
			if (substr($translated_filename,strlen($translated_filename)-3,3) != substr($full_file,strlen($full_file)-3,3)) {
				echo T_("Translation failure, stripping non-valid characters");
				echo "\n";
				$translated_filename = iconv($source_encoding,Config::get('site_charset') . '//IGNORE',$full_file);
			}

			printf (T_('Attempting to Transcode to %s'), Config::get('site_charset'));
			echo "\n";
			echo "--------------------------------------------------------------------------------------------\n";
			printf (T_('OLD: %s has invalid chars'), $full_file);
			echo "\n";
			printf (T_('NEW: %s'), $translated_filename);
			echo "\n";
			echo "--------------------------------------------------------------------------------------------\n";
			if (!$GLOBALS['i_am_crazy']) {
				echo T_("Rename File (Y/N):");
				$input = trim(fgets(STDIN));
				if (strcasecmp($input,'Y') == 0) { charset_rename_file($full_file,$translated_filename); }
				else { echo "\n\t"; echo T_('Not Renaming...'); echo "\n\n"; }
			}
			else {
				charset_rename_file($full_file,$translated_filename);
			}
		}

	} // while reading file

} // charset_directory_correct

/**
 * charset_rename_file
 * This just takes a source / dest and does the renaming
 */
function charset_rename_file($full_file,$translated_filename) {

	// First break out the base directory name and make sure it exists
	// in case our crap char is in the directory
	$directory = dirname($translated_filename);
	$data = preg_split("/[\/\\\]/",$directory);
	$path = '';

	foreach ($data as $dir) {

		$dir = charset_clean_name($dir);
		$path .= "/" . $dir;

		if (!is_dir($path)) {
			echo "\tMaking $path directory\n";
			$results = mkdir($path);
			if (!$results) {
				printf (T_('Error: Unable to create %s move failed, stopping'), $path);
				echo "\n";
				return false;
			}
		} // if the dir doesn't exist

	} // end foreach

	// Now to copy the file
	$results = copy($full_file,$translated_filename);

	if (!$results) {
		echo T_('Error: Copy Failed, not deleteing old file');
		echo "\n";
		return false;
	}

	$old_sum = filesize($full_file);
	$new_sum = filesize($translated_filename);

	if ($old_sum != $new_sum OR !$new_sum) {
		printf (T_('Error: Size Inconsistency, not deleting %s'), $full_file);
		echo "\n";
		return false;
	}

	$results = unlink($full_file);

	if (!$results) { printf (T_('Error: Unable to delete %s'), $full_file); echo "\n"; return false; }

	echo T_("File Moved...");
	echo "\n\n";

	return true;

} // charset_rename_file

/**
 * charset_clean_name
 * We have to have some special rules here
 * This is run on every individual element of the search
 * Before it is put togeather, this removes / and \ and also
 * once I figure it out, it'll clean other stuff
 */
function charset_clean_name($string) {

        /* First remove any / or \ chars */
        $string = preg_replace('/[\/\\\]/','-',$string);

        $string = str_replace(':',' ',$string);

        $string = preg_replace('/[\!\:\*]/','_',$string);

        return $string;

} // charset_clean_name

?>