-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathtxp-checksums.php
More file actions
160 lines (133 loc) · 4.84 KB
/
Copy pathtxp-checksums.php
File metadata and controls
160 lines (133 loc) · 4.84 KB
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
<?php
/*
* Textpattern Content Management System
* https://textpattern.com/
*
* Copyright (C) 2026 The Textpattern Development Team
*
* Textpattern is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2.
*
* Textpattern 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 Textpattern. If not, see <http://www.gnu.org/licenses/>.
*/
define('TXPINTERFACE', 'cli');
if (php_sapi_name() !== 'cli') {
die('command line only');
}
if (empty($argv[1])) {
die("usage: {$argv[0]} <txpath> [all|core|admin-themes]\n");
}
$locs = empty($argv[2]) ? 'all' : $argv[2];
define('txpath', rtrim(realpath($argv[1]), '/'));
$event = '';
$prefs['enable_xmlrpc_server'] = true;
require_once(txpath.'/lib/constants.php');
require_once(txpath.'/lib/txplib_misc.php');
require_once(txpath.'/lib/txplib_admin.php');
$files = array();
$fpattern = '/.*\.(?:php|js)$/';
if ($locs === 'all' || $locs === 'core') {
$destination = txpath;
$files = files_to_checksum($destination, $fpattern);
// Append root and rpc files.
$files = array_merge($files, glob(txpath.DS.'..'.DS.'*.php'));
$files = array_merge($files, glob(txpath.DS.'..'.DS.'rpc'.DS.'*.php'));
// Remove setup and config-dist.php.
$files = array_filter($files, function($e) { return (strpos($e, '/setup') === false); });
$files = array_filter($files, function($e) { return (strpos($e, '/config-dist') === false); });
// Remove admin-themes because they're checksummed independently.
$files = array_filter($files, function($e) { return (strpos($e, '/admin-themes') === false); });
// Output list.
if ($files) {
$files = prep_checksums($files);
write_checksums($destination, $files);
$files = calculate_checksums($files);
write_checksums($destination, $files);
echo "Checksums updated in ".$destination."\n";
}
}
if ($locs === 'all' || $locs === 'admin-themes') {
$destination = txpath.DS.'admin-themes';
$iterator = new DirectoryIterator($destination);
$themesInstalled = array();
foreach ($iterator as $fileinfo) {
if (($fileinfo->isDir() || $fileinfo->isLink()) && !$fileinfo->isDot()) {
$themesInstalled[] = $fileinfo->getFilename();
}
}
foreach ($themesInstalled as $themeName) {
$endpoint = $destination.DS.$themeName;
$files = files_to_checksum($endpoint, $fpattern);
// Output list.
if ($files) {
$files = prep_checksums($files);
write_checksums($endpoint, $files);
$files = calculate_checksums($files);
write_checksums($endpoint, $files);
echo "Checksums updated in ".$endpoint."\n";
}
}
}
/**
* Create a file list, including new matching files added to the repo
*
* @param [type] $dir [description]
* @param [type] $files [description]
* @return [type] [description]
*/
function prep_checksums($files)
{
return array_map(function ($str) { return str_replace(txpath, '', $str.": ".str_repeat('a', CHECKSUM_BYTES)); }, $files);
}
/**
* Commit the passed list of checksummed $files (prepped or computed) to the $dir
*
* @param string $dir Destination directory for the checksums.txt file
* @param string $files Checksummed files
*/
function write_checksums($dir, $files)
{
file_put_contents($dir.DS.'checksums.txt', implode(n, $files));
}
/**
* Recursively fetch files from the given root.
*
* Dot files and directories are skipped.
*
* @param string $folder Path to the start point (root directory to traverse)
* @param string $pattern Full regex filter to apply
* @return array List of files
*/
function files_to_checksum($folder, $pattern)
{
$dir = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS | RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
$iter = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($iter, $pattern, RegexIterator::GET_MATCH);
$fileList = array();
foreach ($files as $file) {
$fileList = array_merge($fileList, $file);
}
return $fileList;
}
/**
* Recalculate checksums of all files in the destination folder.
*
* @return array List of files and their checksums
*/
function calculate_checksums($filter = array())
{
$fileList = array();
foreach (check_file_integrity(INTEGRITY_HASH, true) as $file => $hash) {
if (empty($filter) || preg_grep('/^' . preg_quote($file, '/') . ':/', $filter)) {
$fileList[] = "$file: $hash";
}
}
return $fileList;
}