-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch
executable file
·267 lines (239 loc) · 8.54 KB
/
patch
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env php
<?php
$scriptDir = dirname(__FILE__) . '/';
class Patch {
/**
* Install the patch specified
* @param type $address Location hotfix file
* @param type $patch Name of the hotfix file
* @return int Return 0 when an error occurs
*/
public function add_patch($address = '', $patch = '') {
echo "Add patch on Processmaker ...\n";
if ($address != '') {
if(! $this->copy_patch(trim($address))){
return;
}
}
$this->remove_temporals();
if ($patch == '') {
$patch = basename($address);
}
if (!$this->untar_patch($patch)) {
return false;
}
// Backup files listed on changes.log file
echo "Backup files\n";
$changeLog = fopen(PROCESSMAKER_PATH . 'shared/patches/processmaker/changes.log', 'r');
while (!feof($changeLog)) {
$linea = trim(fgets($changeLog));
// Backup file and then replace
$s1 = substr(PROCESSMAKER_PATH, 0, strpos(PROCESSMAKER_PATH, 'processmaker'))
. $linea;
// Location where must be the original
$s2 = PROCESSMAKER_PATH . 'shared/patches/original/' . $linea;
if (!is_file($s2)) {
if (!is_dir(substr($s2, 0, strrpos($s2, "/")))) {
mkdir(substr($s2, 0, strrpos($s2, "/")), 0777, true);
}
if (is_file($s1)) {
echo $s1."\n";
copy($s1, $s2);
}
}
}
fclose($changeLog);
echo "Apply changes ...\n";
$this->copy_directory(PROCESSMAKER_PATH
. '/shared/patches/processmaker', PROCESSMAKER_PATH);
$this->remove_temporals();
echo "Patch installed sucessfully\n";
}
/**
* Utar the hotfix file
* @return boolean true or false
*/
public function untar_patch($patch) {
echo "Untar patch file\n";
$p = new PharData(PROCESSMAKER_PATH . 'shared/patches/'.$patch);
if ($p->extractTo(PROCESSMAKER_PATH . 'shared/patches')) {
echo "Untar sucessfull\n";
return true;
} else {
echo "Error to untar patch file.\n";
return false;
}
}
/**
* This method list of origin version and patches
*/
public function list_patches() {
echo "The available versions are:\n";
$original_version = '';
if (is_file(PROCESSMAKER_PATH .
'shared/patches/original/processmaker/workflow/engine/' .
'methods/login/version-pmos.php')) {
$original_version = $this->get_version(PROCESSMAKER_PATH .
'shared/patches/original/processmaker/workflow/engine/' .
'methods/login/version-pmos.php');
} else {
$original_version = $this->get_version(PROCESSMAKER_PATH .
'workflow/engine/methods/login/version-pmos.php');
}
$current_installation = $this->get_version(PROCESSMAKER_PATH .
'workflow/engine/methods/login/version-pmos.php');
echo "Original version:\n";
echo $original_version . "\n";
echo "Patch installed:\n";
$directory = opendir(PROCESSMAKER_PATH . 'shared/patches');
while ($file = readdir($directory)) {
if (!is_dir($file) && strpos($file,'.tar')) {
if ( str_replace($file,'.tar','')==$current_installation) {
echo $file . " *\n";
} else {
echo $file . "\n";
}
}
}
}
/**
* This method is used to obtain the current version or the original
* @param type $address File location with version
* @return type Returns the version of ProcessMaker requested
*/
private function get_version($address) {
$result = '';
$file = fopen($address, 'r');
while (!feof($file)) {
$linea = trim(fgets($file));
if (strpos($linea, 'PM_VERSION')) {
$result = substr($linea, strpos($linea, ", ") + 3);
$result = substr($result,0,strlen($result)-3);
}
}
fclose($file);
return $result;
}
/**
* You change the version of a hotfix available
* @param type $hotfix_version
*/
public function rollback_patch($patch_version) {
echo "Rollback to ".$patch_version."\n";
$this->register('rollback_patch ' . $patch_version);
$this->rollback_origin();
if (strpos($patch_version, '.tar')) {
$this->add_patch('', $patch_version);
}
}
/**
* Installation returns to its original state.
*/
private function rollback_origin() {
echo "Rollback to original state\n";
$this->copy_directory(PROCESSMAKER_PATH .
'/shared/patches/original/processmaker', PROCESSMAKER_PATH);
echo "Rollback to original state sucessfull\n";
}
/**
* Copy a entire directory
* @param type $source Location of files to copy
* @param type $target Destination to be copied
*/
private function copy_directory($source, $target) {
if (is_dir($source)) {
@mkdir($target);
$d = dir($source);
while (FALSE !== ( $entry = $d->read() )) {
if ($entry == '.' || $entry == '..') {
continue;
}
$Entry = $source . '/' . $entry;
if (is_dir($Entry)) {
$this->copy_directory($Entry, $target . '/' . $entry);
continue;
}
copy($Entry, $target . '/' . $entry);
}
$d->close();
} else {
copy($source, $target);
}
}
/**
* This function remove a folder complete
* @param type $address Folder to remove
* @return type true or false
*/
private function delete_directory($address) {
$files = array_diff(scandir($address), array('.', '..'));
foreach ($files as $file) {
(is_dir("$address/$file")) ?
$this->delete_directory("$address/$file") :
unlink("$address/$file");
}
return rmdir($address);
}
/**
* Remove the temporal folder for install hotfix
*/
private function remove_temporals() {
echo "Removing temporals files\n";
if (is_Dir(PROCESSMAKER_PATH . "shared/patches/processmaker")) {
$this->delete_directory(PROCESSMAKER_PATH . "shared/patches/processmaker");
}
}
/**
* Copy the hotfix and create shared/hotfixes
* @param type $address Ubicación del hotfix
* @return boolean falso en caso de q no exista el archivo hotfix
*/
private function copy_patch($address) {
echo "Copy hotfix file to shared/hotfixes\n";
$this->verify_directory();
// Verify that the address is correct hotfix
if (is_file($address)) {
copy($address, PROCESSMAKER_PATH . "shared/patches/" . basename($address));
} else {
echo 'The file route is incorrect, please check the route\n';
return false;
}
return true;
}
private function verify_directory(){
// Verify that there is a shared/hotfixes directory exist
if (!is_Dir(PROCESSMAKER_PATH . "shared/patches")) {
mkdir(PROCESSMAKER_PATH . "shared/patches", 0777);
}
}
/**
* Add a log message in the patches.log file
* @param type $log
*/
private function register($log) {
$this->verify_directory();
$file = fopen(PROCESSMAKER_PATH . 'shared/patches/patches.log', "a");
$message = date('l jS \of F Y h:i:s A') . ' ' . $log . PHP_EOL;
fwrite($file, $message);
fclose($file);
}
}
define('PROCESSMAKER_PATH', $scriptDir);
$p = new Patch();
switch (isset($argv[1]) ? $argv[1] : "-h") {
case "-l":
$p->list_patches();
break;
case "-a":
$p->add_patch($argv[2]);
break;
case "-r":
$p->rollback_patch($argv[2]);
break;
case "-h":
default:
echo "-a <directory>/patch.tar _add/install a patch from a file parameter\n";
echo "-r patch_name _remove/uninstall a patch\n";
echo "-l _list all patches installed in processmaker\n";
echo "-h _help list the options in the patch tool\n";
}