| 1 |
<?php |
| 2 |
/** |
| 3 |
* Troubleshoot Config. |
| 4 |
* |
| 5 |
* @since 0.0.0 |
| 6 |
* @package Troubleshoot |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Troubleshoot Config. |
| 11 |
* |
| 12 |
* @since 0.0.0 |
| 13 |
*/ |
| 14 |
class PDT_Config { |
| 15 |
// *** Private |
| 16 |
private $block_begin_line = false; |
| 17 |
private $block_end_line = 0; |
| 18 |
private $last_key_line = -1; |
| 19 |
private $last_key = ''; |
| 20 |
private $block = array(); |
| 21 |
private $data = array(); |
| 22 |
|
| 23 |
private function mergeBlockData() { |
| 24 |
$line_no = 0; |
| 25 |
$data = array(); |
| 26 |
if ( $this->block_begin_line !== false ) { |
| 27 |
for ($line_no = 0; $line_no < count( $this->data ); $line_no++ ) { |
| 28 |
if ( $line_no < $this->block_begin_line ) { |
| 29 |
array_push( $data, $this->data[$line_no] ); |
| 30 |
} |
| 31 |
if ( $line_no > $this->block_begin_line && $line_no < $this->block_end_line ) { |
| 32 |
foreach( $this->block as $line ) { |
| 33 |
array_push( $data, $line ); |
| 34 |
} |
| 35 |
$line_no = $this->block_end_line; |
| 36 |
} |
| 37 |
if ( $line_no > $this->block_end_line ) { |
| 38 |
array_push( $data, $this->data[$line_no] ); |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
if ( $this->block_end_line >= count( $this->data ) ) { |
| 43 |
foreach( $this->block as $line ) { |
| 44 |
array_push( $data, $line ); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// Trim last line, prevent line feed creep |
| 49 |
if ( $data[count( $data )-1] === "" && $data[count( $data ) - 2] === "" ) { |
| 50 |
array_pop( $data ); |
| 51 |
array_pop( $data ); |
| 52 |
} |
| 53 |
$this->data = $data; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Load the given file if provided. |
| 58 |
*/ |
| 59 |
function __construct( $file = '', $normalize = false ) { |
| 60 |
if ( $file !== '' ){ |
| 61 |
$this->load( $file, $normalize ); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// *** Public |
| 66 |
public $ignore_spaces = false; |
| 67 |
public $comment_chars = ''; |
| 68 |
public $prepend_value = ''; |
| 69 |
public $append_value = ''; |
| 70 |
public $block_begin = ''; |
| 71 |
public $prepend_key = ''; |
| 72 |
public $append_key = ''; |
| 73 |
public $block_end = ''; |
| 74 |
|
| 75 |
/** |
| 76 |
* Load a file for analysis |
| 77 |
* |
| 78 |
* @param string $file The path and filename of the file to load. |
| 79 |
*/ |
| 80 |
function load( $file, $normalize = false ) { |
| 81 |
// Set configuration file type based on extension |
| 82 |
$s = new PDT_String( $file ); |
| 83 |
$this->set_type( $s->getRightMost( '.' )->get_string() ); |
| 84 |
|
| 85 |
// Convert Windows CRLF to LF |
| 86 |
$d = str_replace( "\r\n", "\n", file_get_contents( $file ) ); |
| 87 |
if ( !empty( $normalize ) ) { |
| 88 |
$d = str_replace( "\\\"", "\\escapeddoublequote", $d ); |
| 89 |
$d = str_replace( "\"", "'", $d ); |
| 90 |
$d = str_replace( "\\escapeddoublequote", "\\\"", $d ); |
| 91 |
} |
| 92 |
|
| 93 |
$this->data = explode( "\n", $d ); |
| 94 |
$this->block = $this->data; |
| 95 |
$this->block_end_line = count( $this->data ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Overwrites the current block of data with the given content. |
| 100 |
* |
| 101 |
* @param string $contents the string data to replace the block with. |
| 102 |
*/ |
| 103 |
function set_block( $contents ) { |
| 104 |
|
| 105 |
// Trim last line, prevent line feed creep |
| 106 |
if ( substr( $contents, -1 ) === "\n" && substr( $contents, -2, 1 ) === "\n" ) { |
| 107 |
$contents = substr( $contents, 0, -1 ); |
| 108 |
} |
| 109 |
$block = str_replace( "\r\n", "\n", $contents ); |
| 110 |
$block = explode( "\n", $block ); |
| 111 |
array_unshift( $block, $this->block[0] ); |
| 112 |
array_push( $block, $this->block[count( $this->block ) - 1] ); |
| 113 |
$this->block = $block; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Set the given boundary markers and options for parsing the file. This |
| 118 |
* method is automatically called from our load file method. |
| 119 |
* |
| 120 |
* @param string $ext The file extension. |
| 121 |
*/ |
| 122 |
function set_type( $ext ) { |
| 123 |
switch ( $ext ) { |
| 124 |
case 'ini': // PHP ini file |
| 125 |
$this->comment_chars = "; "; |
| 126 |
$this->ignore_spaces = true; |
| 127 |
$this->prepend_value = ""; |
| 128 |
$this->append_value = ""; |
| 129 |
$this->prepend_key = ""; |
| 130 |
$this->append_key = "="; |
| 131 |
break; |
| 132 |
case 'php-define': |
| 133 |
case 'php': // PHP source code file, where define('key', 'value'); |
| 134 |
$this->comment_chars = "//"; |
| 135 |
$this->ignore_spaces = true; |
| 136 |
$this->prepend_value = " '"; |
| 137 |
$this->append_value = "');"; |
| 138 |
$this->prepend_key = "define('"; |
| 139 |
$this->append_key = "',"; |
| 140 |
break; |
| 141 |
case 'php-define-double-quote': |
| 142 |
$this->comment_chars = "//"; |
| 143 |
$this->ignore_spaces = true; |
| 144 |
$this->prepend_value = " \""; |
| 145 |
$this->append_value = "\");"; |
| 146 |
$this->prepend_key = "define(\""; |
| 147 |
$this->append_key = "\","; |
| 148 |
break; |
| 149 |
case 'php-unquoted': // PHP source code file, where define('key', unquoted value); |
| 150 |
$this->comment_chars = "//"; |
| 151 |
$this->ignore_spaces = true; |
| 152 |
$this->prepend_value = " "; |
| 153 |
$this->append_value = ");"; |
| 154 |
$this->prepend_key = "define('"; |
| 155 |
$this->append_key = "',"; |
| 156 |
break; |
| 157 |
case 'conf': // Apache configuration file |
| 158 |
$this->comment_chars = "# "; |
| 159 |
$this->prepend_value = ""; |
| 160 |
$this->append_value = ""; |
| 161 |
$this->prepend_key = ""; |
| 162 |
$this->append_key = " "; |
| 163 |
break; |
| 164 |
case 'cnf': // MySQL my.cnf |
| 165 |
$this->comment_chars = "# "; |
| 166 |
$this->ignore_spaces = true; |
| 167 |
$this->prepend_value = ""; |
| 168 |
$this->append_value = ""; |
| 169 |
$this->prepend_key = ""; |
| 170 |
$this->append_key = "="; |
| 171 |
break; |
| 172 |
case 'php-variable': |
| 173 |
$this->comment_chars = "// "; |
| 174 |
$this->ignore_spaces = true; |
| 175 |
$this->prepend_value = " '"; |
| 176 |
$this->append_value = "';"; |
| 177 |
$this->prepend_key = "$"; |
| 178 |
$this->append_key = " ="; |
| 179 |
break; |
| 180 |
default: |
| 181 |
$this->comment_chars = "//"; |
| 182 |
$this->ignore_spaces = true; |
| 183 |
$this->prepend_value = "'"; |
| 184 |
$this->append_value = "';"; |
| 185 |
$this->prepend_key = ""; |
| 186 |
$this->append_key = "="; |
| 187 |
break; |
| 188 |
} |
| 189 |
} |
| 190 |
/** |
| 191 |
* Narrow the focus of our methods to the given unique block within the |
| 192 |
* configuration file. Our methods (find, get, set, add, remove, etc) will |
| 193 |
* only act within the given valid region. If the region does not exist or |
| 194 |
* the arguments are empty, then our methods will apply to the entire file. |
| 195 |
* |
| 196 |
* @param string $begin A unique string that markes the beginning of the region. |
| 197 |
* @param string $end A unique string that identifies the end of the region. |
| 198 |
* @return boolean Returns true if an existing isolated block could be found. |
| 199 |
*/ |
| 200 |
function isolate_block( $begin, $end ) { |
| 201 |
$this->mergeBlockData(); |
| 202 |
if ( $begin === null && $end === null || $begin === '' || $end === '' ) { |
| 203 |
$this->block_begin_line = false; |
| 204 |
$this->block_end_line = 0; |
| 205 |
$this->last_key_line = -1; |
| 206 |
$this->last_key = ''; |
| 207 |
$this->block = $this->data; |
| 208 |
$this->block_end_line = count( $this->data ); |
| 209 |
return; |
| 210 |
} |
| 211 |
$this->block_begin_line = false; |
| 212 |
$this->block_begin = $begin; |
| 213 |
$this->block_end = $end; |
| 214 |
$this->last_key = ''; |
| 215 |
$this->block = array(); |
| 216 |
$line_no = 0; |
| 217 |
foreach ( $this->data as $line ) { |
| 218 |
if ( false !== strpos( $line, $begin ) ) { |
| 219 |
$this->block_begin_line = $line_no; |
| 220 |
array_push( $this->block, $line ); |
| 221 |
}else{ |
| 222 |
if ( false !== strpos( $line, $end ) ) { |
| 223 |
array_push( $this->block, $line ); |
| 224 |
$this->block_end_line = $line_no; |
| 225 |
return true; |
| 226 |
}else{ |
| 227 |
if ( false !== $this->block_begin_line ) { |
| 228 |
array_push( $this->block, $line ); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
$line_no++; |
| 233 |
} |
| 234 |
|
| 235 |
// Use last blank line if present |
| 236 |
if ( end($this->data) === "" ) { |
| 237 |
$line_no--; |
| 238 |
} |
| 239 |
$this->block_begin_line = $line_no; |
| 240 |
$this->block_end_line = $line_no + 1; |
| 241 |
$this->block = array( $begin, $end ); |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Finds the given key within the configuration file and within the narrowed |
| 247 |
* scope of an isolated block of code if the "isolate_block" method was used |
| 248 |
* prior. Invoking the method with the same parameter finds the next instance |
| 249 |
* of the key if present. |
| 250 |
* |
| 251 |
* @param string $key The key to find within the configuration file. |
| 252 |
* @return boolean Returns true if the key could be found or false if missing. |
| 253 |
*/ |
| 254 |
function find( $key ) { |
| 255 |
// Reset to top of block |
| 256 |
if ( $key === '' || $key === null ) { |
| 257 |
$this->last_key_line = -1; |
| 258 |
$this->last_key = ''; |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
// Search for the key in the current block |
| 263 |
$line_no = 0; |
| 264 |
$find = $this->prepend_key . $key . $this->append_key; |
| 265 |
if ( $this->ignore_spaces ) { |
| 266 |
$find = str_replace( ' ', '', $find ); |
| 267 |
} |
| 268 |
if ( $this->last_key !== $key ) { |
| 269 |
$this->last_key_line = -1; |
| 270 |
} |
| 271 |
$this->last_key = $key; |
| 272 |
$this->last_key_line = -1; |
| 273 |
foreach ( $this->block as $line ) { |
| 274 |
if ( ! $this->is_string_commented( $line ) ) { |
| 275 |
if ( $line_no > $this->last_key_line ) { |
| 276 |
if ( $this->ignore_spaces ) { |
| 277 |
$line = str_replace( ' ', '', $line ); |
| 278 |
} |
| 279 |
if ( false !== strpos( $line, $find ) ) { |
| 280 |
$this->last_key_line = $line_no; |
| 281 |
return true; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
$line_no++; |
| 286 |
} |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Comments out the line containing the key that was found from a prior find |
| 292 |
* method call. |
| 293 |
* |
| 294 |
*/ |
| 295 |
function comment() { |
| 296 |
if ( $this->last_key_line === -1 ) { |
| 297 |
return; |
| 298 |
} |
| 299 |
$line = new PDT_String( $this->block[$this->last_key_line] ); |
| 300 |
if ( false === $this->is_commented() ) { |
| 301 |
$this->block[$this->last_key_line] = $this->comment_chars . $line->get_string(); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Checks if the line containing the key that was found from a prior find |
| 307 |
* method call is commented or uncommented. |
| 308 |
* |
| 309 |
* @return boolean Returns true if the given line is commented out. |
| 310 |
*/ |
| 311 |
function is_commented() { |
| 312 |
if ( $this->last_key_line === -1 ) { |
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
$line = new PDT_String( $this->block[$this->last_key_line] ); |
| 317 |
$cc = $this->comment_chars; |
| 318 |
if ( $this->ignore_spaces ) { |
| 319 |
$line = $line->replace(' ', ''); |
| 320 |
$cc = str_replace( ' ', '', $cc ); |
| 321 |
} |
| 322 |
|
| 323 |
return $line->trim()->startsWith( $cc ); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Checks if the line containing the key that was found from a prior find |
| 328 |
* method call is commented or uncommented. |
| 329 |
* |
| 330 |
* @return boolean Returns true if the given line is commented out. |
| 331 |
*/ |
| 332 |
function is_string_commented($string) { |
| 333 |
$line = new PDT_String( $string ); |
| 334 |
$cc = $this->comment_chars; |
| 335 |
if ( $this->ignore_spaces ) { |
| 336 |
$line = $line->replace(' ', ''); |
| 337 |
$cc = str_replace( ' ', '', $cc ); |
| 338 |
} |
| 339 |
|
| 340 |
return $line->trim()->startsWith( $cc ); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
/** |
| 345 |
* Uncomments out the line containing the key that was found from a prior find |
| 346 |
* method call. |
| 347 |
* |
| 348 |
*/ |
| 349 |
function uncomment() { |
| 350 |
if ( $this->last_key_line === -1 ) { |
| 351 |
return; |
| 352 |
} |
| 353 |
$line = new PDT_String( $this->block[$this->last_key_line] ); |
| 354 |
if ( true === $this->is_commented() ) { |
| 355 |
$this->block[$this->last_key_line] = $line->delLeftMost( $this->comment_chars )->get_string(); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* The get_key method provides a single method to find and return the existing |
| 361 |
* value for a single given matching key. The default is returned if no key |
| 362 |
* has been found. |
| 363 |
* |
| 364 |
* @param string $key |
| 365 |
* @return string |
| 366 |
*/ |
| 367 |
function get_key( $key, $default = '' ) { |
| 368 |
$this->find( $key ); |
| 369 |
if ( $this->last_key_line === -1 ) { |
| 370 |
return $default; |
| 371 |
} else { |
| 372 |
return $this->get(); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Gets the current value of a key that was found with a last prior method call |
| 378 |
* to find. Returns null if no valid key was originally found. |
| 379 |
* |
| 380 |
* @return string The value of the key found from a prior find method call. |
| 381 |
*/ |
| 382 |
function get() { |
| 383 |
if ( $this->last_key_line === -1 || $this->last_key === '' ) { |
| 384 |
return null; |
| 385 |
} |
| 386 |
$line = $this->block[$this->last_key_line]; |
| 387 |
$key = $this->prepend_key . $this->last_key . $this->append_key; |
| 388 |
if ( $this->ignore_spaces ) { |
| 389 |
$line = str_replace( ' ', '', $line ); |
| 390 |
$key = str_replace( ' ', '', $key ); |
| 391 |
} |
| 392 |
$s = new PDT_String( $line ); |
| 393 |
$s = $s->delLeftMost( $key ); |
| 394 |
$pre = $this->prepend_value; |
| 395 |
$app = $this->append_value; |
| 396 |
if ( $this->ignore_spaces ) { |
| 397 |
$pre = str_replace( ' ', '', $this->prepend_value); |
| 398 |
$app = str_replace( ' ', '', $this->append_value); |
| 399 |
} |
| 400 |
return $s->delLeftMost( $pre )->getLeftMost( $app )->get_string(); |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* The set_key method provides one convenient call to set an existing a value |
| 405 |
* for a single key in a given configuration file. The key is automatically |
| 406 |
* added if it does not exist prior. |
| 407 |
* |
| 408 |
* @param string $key |
| 409 |
* @param string $value |
| 410 |
*/ |
| 411 |
function set_key( $key, $value ) { |
| 412 |
$this->find( $key ); |
| 413 |
if ( $this->last_key_line === -1 ) { |
| 414 |
$this->add( $value ); |
| 415 |
} else { |
| 416 |
$this->set( $value ); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Adds a value to the end of the current block for the given key that was |
| 422 |
* searched for via the find method. Ignored if no prior find was invoked or |
| 423 |
* no valid key could be found. |
| 424 |
* |
| 425 |
* @param string $value The value to add to the block. |
| 426 |
*/ |
| 427 |
function add( $value ) { |
| 428 |
$new = $this->prepend_key . $this->last_key . $this->append_key; |
| 429 |
$new .= $this->prepend_value . $value . $this->append_value; |
| 430 |
if ( $this->block[count( $this->block ) -1] === $this->block_end ) { |
| 431 |
array_splice( $this->block, -1, 0, $new ); |
| 432 |
}else{ |
| 433 |
array_push( $this->block, $new ); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Sets or updates the value for the given key that was found from a prior |
| 439 |
* find method call. Ignored if no prior find was invoked or no valid key |
| 440 |
* could be found. |
| 441 |
* |
| 442 |
* @param string $value The new value to |
| 443 |
*/ |
| 444 |
function set( $value ) { |
| 445 |
if ( $this->last_key_line === -1 ) { |
| 446 |
return; |
| 447 |
} |
| 448 |
$update = $this->prepend_key . $this->last_key . $this->append_key; |
| 449 |
$update .= $this->prepend_value . $value . $this->append_value; |
| 450 |
$this->block[$this->last_key_line] = $update; |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Removes the line containing the key that was found from a prior find |
| 455 |
* method call. |
| 456 |
* |
| 457 |
*/ |
| 458 |
function remove() { |
| 459 |
if ( $this->last_key_line === -1 ) { |
| 460 |
return; |
| 461 |
} |
| 462 |
unset( $this->block[$this->last_key_line] ); |
| 463 |
} |
| 464 |
|
| 465 |
/** |
| 466 |
* Saves the current configuration file data from memory to the given file. |
| 467 |
* |
| 468 |
* @param string $file The file path and name to save the configuration data to. |
| 469 |
*/ |
| 470 |
function save( $file ) { |
| 471 |
$this->mergeBlockData(); |
| 472 |
|
| 473 |
// Too large for impload, concat ourselves |
| 474 |
$data = ''; |
| 475 |
foreach( $this->data as $line ) { |
| 476 |
$data .= $line . "\n"; |
| 477 |
} |
| 478 |
|
| 479 |
file_put_contents( $file, $data ); |
| 480 |
} |
| 481 |
} |
| 482 |
|