| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_DEFENDER_VENDOR\Safe; |
| 4 |
|
| 5 |
use WP_DEFENDER_VENDOR\Safe\Exceptions\XdiffException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Makes a binary diff of two files and stores the result in a patch file. |
| 9 |
* This function works with both text and binary files. Resulting patch |
| 10 |
* file can be later applied using xdiff_file_bpatch/xdiff_string_bpatch. |
| 11 |
* |
| 12 |
* @param string $old_file Path to the first file. This file acts as "old" file. |
| 13 |
* @param string $new_file Path to the second file. This file acts as "new" file. |
| 14 |
* @param string $dest Path of the resulting patch file. Resulting file contains differences |
| 15 |
* between "old" and "new" files. It is in binary format and is human-unreadable. |
| 16 |
* @throws XdiffException |
| 17 |
* |
| 18 |
*/ |
| 19 |
function xdiff_file_bdiff(string $old_file, string $new_file, string $dest): void |
| 20 |
{ |
| 21 |
error_clear_last(); |
| 22 |
$result = \xdiff_file_bdiff($old_file, $new_file, $dest); |
| 23 |
if ($result === false) { |
| 24 |
throw XdiffException::createFromPhpError(); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Patches a file with a binary |
| 31 |
* patch and stores the result in a file dest. |
| 32 |
* This function accepts patches created both via xdiff_file_bdiff |
| 33 |
* and xdiff_file_rabdiff functions or their string counterparts. |
| 34 |
* |
| 35 |
* @param string $file The original file. |
| 36 |
* @param string $patch The binary patch file. |
| 37 |
* @param string $dest Path of the resulting file. |
| 38 |
* @throws XdiffException |
| 39 |
* |
| 40 |
*/ |
| 41 |
function xdiff_file_bpatch(string $file, string $patch, string $dest): void |
| 42 |
{ |
| 43 |
error_clear_last(); |
| 44 |
$result = \xdiff_file_bpatch($file, $patch, $dest); |
| 45 |
if ($result === false) { |
| 46 |
throw XdiffException::createFromPhpError(); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Makes a binary diff of two files and stores the result in a patch file. |
| 53 |
* This function works with both text and binary files. Resulting patch |
| 54 |
* file can be later applied using xdiff_file_bpatch. |
| 55 |
* |
| 56 |
* Starting with version 1.5.0 this function is an alias of xdiff_file_bdiff. |
| 57 |
* |
| 58 |
* @param string $old_file Path to the first file. This file acts as "old" file. |
| 59 |
* @param string $new_file Path to the second file. This file acts as "new" file. |
| 60 |
* @param string $dest Path of the resulting patch file. Resulting file contains differences |
| 61 |
* between "old" and "new" files. It is in binary format and is human-unreadable. |
| 62 |
* @throws XdiffException |
| 63 |
* |
| 64 |
*/ |
| 65 |
function xdiff_file_diff_binary(string $old_file, string $new_file, string $dest): void |
| 66 |
{ |
| 67 |
error_clear_last(); |
| 68 |
$result = \xdiff_file_diff_binary($old_file, $new_file, $dest); |
| 69 |
if ($result === false) { |
| 70 |
throw XdiffException::createFromPhpError(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* Makes an unified diff containing differences between old_file and |
| 77 |
* new_file and stores it in dest file. The |
| 78 |
* resulting file is human-readable. An optional context parameter |
| 79 |
* specifies how many lines of context should be added around each change. |
| 80 |
* Setting minimal parameter to true will result in outputting the shortest |
| 81 |
* patch file possible (can take a long time). |
| 82 |
* |
| 83 |
* @param string $old_file Path to the first file. This file acts as "old" file. |
| 84 |
* @param string $new_file Path to the second file. This file acts as "new" file. |
| 85 |
* @param string $dest Path of the resulting patch file. |
| 86 |
* @param int $context Indicates how many lines of context you want to include in diff |
| 87 |
* result. |
| 88 |
* @param bool $minimal Set this parameter to TRUE if you want to minimalize size of the result |
| 89 |
* (can take a long time). |
| 90 |
* @throws XdiffException |
| 91 |
* |
| 92 |
*/ |
| 93 |
function xdiff_file_diff(string $old_file, string $new_file, string $dest, int $context = 3, bool $minimal = false): void |
| 94 |
{ |
| 95 |
error_clear_last(); |
| 96 |
$result = \xdiff_file_diff($old_file, $new_file, $dest, $context, $minimal); |
| 97 |
if ($result === false) { |
| 98 |
throw XdiffException::createFromPhpError(); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* Patches a file with a binary |
| 105 |
* patch and stores the result in a file dest. |
| 106 |
* This function accepts patches created both via xdiff_file_bdiff |
| 107 |
* or xdiff_file_rabdiff functions or their string counterparts. |
| 108 |
* |
| 109 |
* Starting with version 1.5.0 this function is an alias of xdiff_file_bpatch. |
| 110 |
* |
| 111 |
* @param string $file The original file. |
| 112 |
* @param string $patch The binary patch file. |
| 113 |
* @param string $dest Path of the resulting file. |
| 114 |
* @throws XdiffException |
| 115 |
* |
| 116 |
*/ |
| 117 |
function xdiff_file_patch_binary(string $file, string $patch, string $dest): void |
| 118 |
{ |
| 119 |
error_clear_last(); |
| 120 |
$result = \xdiff_file_patch_binary($file, $patch, $dest); |
| 121 |
if ($result === false) { |
| 122 |
throw XdiffException::createFromPhpError(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* Makes a binary diff of two files and stores the result in a patch file. |
| 129 |
* The difference between this function and xdiff_file_bdiff is different |
| 130 |
* algorithm used which should result in faster execution and smaller diff produced. |
| 131 |
* This function works with both text and binary files. Resulting patch |
| 132 |
* file can be later applied using xdiff_file_bpatch/xdiff_string_bpatch. |
| 133 |
* |
| 134 |
* For more details about differences between algorithm used please check libxdiff |
| 135 |
* website. |
| 136 |
* |
| 137 |
* @param string $old_file Path to the first file. This file acts as "old" file. |
| 138 |
* @param string $new_file Path to the second file. This file acts as "new" file. |
| 139 |
* @param string $dest Path of the resulting patch file. Resulting file contains differences |
| 140 |
* between "old" and "new" files. It is in binary format and is human-unreadable. |
| 141 |
* @throws XdiffException |
| 142 |
* |
| 143 |
*/ |
| 144 |
function xdiff_file_rabdiff(string $old_file, string $new_file, string $dest): void |
| 145 |
{ |
| 146 |
error_clear_last(); |
| 147 |
$result = \xdiff_file_rabdiff($old_file, $new_file, $dest); |
| 148 |
if ($result === false) { |
| 149 |
throw XdiffException::createFromPhpError(); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
/** |
| 155 |
* Patches a string str with a binary patch. |
| 156 |
* This function accepts patches created both via xdiff_string_bdiff |
| 157 |
* and xdiff_string_rabdiff functions or their file counterparts. |
| 158 |
* |
| 159 |
* @param string $str The original binary string. |
| 160 |
* @param string $patch The binary patch string. |
| 161 |
* @return string Returns the patched string. |
| 162 |
* @throws XdiffException |
| 163 |
* |
| 164 |
*/ |
| 165 |
function xdiff_string_bpatch(string $str, string $patch): string |
| 166 |
{ |
| 167 |
error_clear_last(); |
| 168 |
$result = \xdiff_string_bpatch($str, $patch); |
| 169 |
if ($result === false) { |
| 170 |
throw XdiffException::createFromPhpError(); |
| 171 |
} |
| 172 |
return $result; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
/** |
| 177 |
* Patches a string str with a binary patch. |
| 178 |
* This function accepts patches created both via xdiff_string_bdiff |
| 179 |
* and xdiff_string_rabdiff functions or their file counterparts. |
| 180 |
* |
| 181 |
* Starting with version 1.5.0 this function is an alias of xdiff_string_bpatch. |
| 182 |
* |
| 183 |
* @param string $str The original binary string. |
| 184 |
* @param string $patch The binary patch string. |
| 185 |
* @return string Returns the patched string. |
| 186 |
* @throws XdiffException |
| 187 |
* |
| 188 |
*/ |
| 189 |
function xdiff_string_patch_binary(string $str, string $patch): string |
| 190 |
{ |
| 191 |
error_clear_last(); |
| 192 |
$result = \xdiff_string_patch_binary($str, $patch); |
| 193 |
if ($result === false) { |
| 194 |
throw XdiffException::createFromPhpError(); |
| 195 |
} |
| 196 |
return $result; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
/** |
| 201 |
* Patches a str string with an unified patch in patch parameter |
| 202 |
* and returns the result. patch has to be an unified diff created by |
| 203 |
* xdiff_file_diff/xdiff_string_diff function. |
| 204 |
* An optional flags parameter specifies mode of operation. Any |
| 205 |
* rejected parts of the patch will be stored inside error variable if |
| 206 |
* it is provided. |
| 207 |
* |
| 208 |
* @param string $str The original string. |
| 209 |
* @param string $patch The unified patch string. It has to be created using xdiff_string_diff, |
| 210 |
* xdiff_file_diff functions or compatible tools. |
| 211 |
* @param int|null $flags flags can be either |
| 212 |
* XDIFF_PATCH_NORMAL (default mode, normal patch) |
| 213 |
* or XDIFF_PATCH_REVERSE (reversed patch). |
| 214 |
* |
| 215 |
* Starting from version 1.5.0, you can also use binary OR to enable |
| 216 |
* XDIFF_PATCH_IGNORESPACE flag. |
| 217 |
* @param string|null $error If provided then rejected parts are stored inside this variable. |
| 218 |
* |
| 219 |
* @return string Returns the patched string. |
| 220 |
* @throws XdiffException |
| 221 |
* |
| 222 |
*/ |
| 223 |
function xdiff_string_patch(string $str, string $patch, ?int $flags = null, ?string &$error = null): string |
| 224 |
{ |
| 225 |
error_clear_last(); |
| 226 |
if ($error !== null) { |
| 227 |
$result = \xdiff_string_patch($str, $patch, $flags, $error); |
| 228 |
} elseif ($flags !== null) { |
| 229 |
$result = \xdiff_string_patch($str, $patch, $flags); |
| 230 |
} else { |
| 231 |
$result = \xdiff_string_patch($str, $patch); |
| 232 |
} |
| 233 |
if ($result === false) { |
| 234 |
throw XdiffException::createFromPhpError(); |
| 235 |
} |
| 236 |
return $result; |
| 237 |
} |
| 238 |
|