|
26 | 26 | } |
27 | 27 |
|
28 | 28 | if (empty($argv[1])) { |
29 | | - die("usage: {$argv[0]} <txpath> [update|rebuild]\n"); |
| 29 | + die("usage: {$argv[0]} <txpath> [all|core|admin-themes]\n"); |
30 | 30 | } |
31 | 31 |
|
32 | | -$action = empty($argv[2]) ? 'update' : $argv[2]; |
| 32 | +$locs = empty($argv[2]) ? 'all' : $argv[2]; |
33 | 33 |
|
34 | 34 | define('txpath', rtrim(realpath($argv[1]), '/')); |
35 | 35 |
|
36 | 36 | $event = ''; |
37 | 37 | $prefs['enable_xmlrpc_server'] = true; |
38 | | -require_once(txpath . '/lib/constants.php'); |
39 | | -require_once(txpath . '/lib/txplib_misc.php'); |
40 | | -require_once(txpath . '/lib/txplib_admin.php'); |
| 38 | +require_once(txpath.'/lib/constants.php'); |
| 39 | +require_once(txpath.'/lib/txplib_misc.php'); |
| 40 | +require_once(txpath.'/lib/txplib_admin.php'); |
| 41 | + |
41 | 42 | $files = array(); |
42 | | -$destination = txpath . DS . 'checksums.txt'; |
| 43 | +$fpattern = '/.*\.(?:php|js)$/'; |
| 44 | + |
| 45 | +if ($locs === 'all' || $locs === 'core') { |
| 46 | + $destination = txpath; |
| 47 | + |
| 48 | + $files = files_to_checksum($destination, $fpattern); |
| 49 | + |
| 50 | + // Append root and rpc files. |
| 51 | + $files = array_merge($files, glob(txpath.DS.'..'.DS.'*.php')); |
| 52 | + $files = array_merge($files, glob(txpath.DS.'..'.DS.'rpc'.DS.'*.php')); |
| 53 | + |
| 54 | + // Remove setup and config-dist.php. |
| 55 | + $files = array_filter($files, function($e) { return (strpos($e, '/setup') === false); }); |
| 56 | + $files = array_filter($files, function($e) { return (strpos($e, '/config-dist') === false); }); |
| 57 | + |
| 58 | + // Remove admin-themes because they're checksummed independently. |
| 59 | + $files = array_filter($files, function($e) { return (strpos($e, '/admin-themes') === false); }); |
43 | 60 |
|
44 | | -switch ($action) { |
45 | | - case 'update': |
46 | | - $files = calculate_checksums(); |
47 | | - break; |
48 | | - case 'rebuild': |
49 | | - $files = files_to_checksum(txpath, '/.*\.(?:php|js)$/'); |
| 61 | + // Output list. |
| 62 | + if ($files) { |
| 63 | + $files = prep_checksums($files); |
| 64 | + write_checksums($destination, $files); |
| 65 | + $files = calculate_checksums($files); |
| 66 | + write_checksums($destination, $files); |
| 67 | + echo "Checksums updated in ".$destination."\n"; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +if ($locs === 'all' || $locs === 'admin-themes') { |
| 72 | + $destination = txpath.DS.'admin-themes'; |
50 | 73 |
|
51 | | - // Append root and rpc files. |
52 | | - $files = array_merge($files, glob(txpath . DS . '..' . DS . '*.php')); |
53 | | - $files = array_merge($files, glob(txpath . DS . '..' . DS . 'rpc' . DS . '*.php')); |
| 74 | + $iterator = new DirectoryIterator($destination); |
| 75 | + $themesInstalled = array(); |
| 76 | + |
| 77 | + foreach ($iterator as $fileinfo) { |
| 78 | + if ($fileinfo->isDir() && !$fileinfo->isDot()) { |
| 79 | + $themesInstalled[] = $fileinfo->getFilename(); |
| 80 | + } |
| 81 | + } |
54 | 82 |
|
55 | | - // Remove setup and config-dist.php. |
56 | | - $files = array_filter($files, function($e) { return (strpos($e, '/setup') === false); }); |
57 | | - $files = array_filter($files, function($e) { return (strpos($e, '/config-dist') === false); }); |
| 83 | + foreach ($themesInstalled as $themeName) { |
| 84 | + $endpoint = $destination.DS.$themeName; |
| 85 | + $files = files_to_checksum($endpoint, $fpattern); |
58 | 86 |
|
59 | 87 | // Output list. |
60 | 88 | if ($files) { |
61 | | - $files = array_map(function ($str) { return str_replace(txpath, '', $str . ": " . str_repeat('a', 32)); }, $files); |
62 | | - file_put_contents($destination, implode(n, $files)); |
63 | | - $files = calculate_checksums(); |
| 89 | + $files = prep_checksums($files); |
| 90 | + write_checksums($endpoint, $files); |
| 91 | + $files = calculate_checksums($files); |
| 92 | + write_checksums($endpoint, $files); |
| 93 | + echo "Checksums updated in ".$endpoint."\n"; |
64 | 94 | } |
65 | | - break; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Create a file list, including new matching files added to the repo |
| 100 | + * |
| 101 | + * @param [type] $dir [description] |
| 102 | + * @param [type] $files [description] |
| 103 | + * @return [type] [description] |
| 104 | + */ |
| 105 | +function prep_checksums($files) |
| 106 | +{ |
| 107 | + return array_map(function ($str) { return str_replace(txpath, '', $str.": ".str_repeat('a', CHECKSUM_BYTES)); }, $files); |
66 | 108 | } |
67 | 109 |
|
68 | | -if ($files) { |
69 | | - file_put_contents($destination, implode(n, $files)); |
70 | | - echo "Checksums updated in " . $destination . ".\n"; |
| 110 | +/** |
| 111 | + * Commit the passed list of checksummed $files (prepped or computed) to the $dir |
| 112 | + * |
| 113 | + * @param string $dir Destination directory for the checksums.txt file |
| 114 | + * @param string $files Checksummed files |
| 115 | + */ |
| 116 | +function write_checksums($dir, $files) |
| 117 | +{ |
| 118 | + file_put_contents($dir.DS.'checksums.txt', implode(n, $files)); |
71 | 119 | } |
72 | 120 |
|
73 | 121 | /** |
@@ -94,16 +142,18 @@ function files_to_checksum($folder, $pattern) |
94 | 142 | } |
95 | 143 |
|
96 | 144 | /** |
97 | | - * Recalculate checksums of all files in the destination file. |
| 145 | + * Recalculate checksums of all files in the destination folder. |
98 | 146 | * |
99 | 147 | * @return array List of files and their checksums |
100 | 148 | */ |
101 | | -function calculate_checksums() |
| 149 | +function calculate_checksums($filter = array()) |
102 | 150 | { |
103 | 151 | $fileList = array(); |
104 | 152 |
|
105 | | - foreach (check_file_integrity(INTEGRITY_MD5) as $file => $md5) { |
106 | | - $fileList[] = "$file: $md5"; |
| 153 | + foreach (check_file_integrity(INTEGRITY_HASH, true) as $file => $hash) { |
| 154 | + if (empty($filter) || preg_grep('/^' . preg_quote($file, '/') . ':/', $filter)) { |
| 155 | + $fileList[] = "$file: $hash"; |
| 156 | + } |
107 | 157 | } |
108 | 158 |
|
109 | 159 | return $fileList; |
|