| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* Read and write an existing file. |
| 8 |
*/ |
| 9 |
class SQLite_Object_Cache_File { |
| 10 |
|
| 11 |
private $lines; |
| 12 |
private $filename; |
| 13 |
|
| 14 |
public function __construct( $filename ) { |
| 15 |
|
| 16 |
$this->filename = $filename; |
| 17 |
$this->lines = self::read_lines( $this->filename ); |
| 18 |
|
| 19 |
} |
| 20 |
|
| 21 |
public function remove( $item ) { |
| 22 |
$result = array(); |
| 23 |
$found = false; |
| 24 |
foreach ( $this->lines as $line ) { |
| 25 |
if ( ! str_contains( $line, $item ) ) { |
| 26 |
$result[] = $line; |
| 27 |
} else { |
| 28 |
$found = true; |
| 29 |
} |
| 30 |
} |
| 31 |
if ( $found ) { |
| 32 |
$this->lines = $result; |
| 33 |
} |
| 34 |
return $found; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @throws Exception if no opening php tag was found. |
| 39 |
*/ |
| 40 |
public function insert_before( $item, $before = "That's all, stop editing!" ) { |
| 41 |
$result = array(); |
| 42 |
$found = false; |
| 43 |
foreach ( $this->lines as $line ) { |
| 44 |
if ( ! $found && str_contains( $line, $before ) ) { |
| 45 |
$result[] = $item; |
| 46 |
$found = true; |
| 47 |
} |
| 48 |
$result[] = $line; |
| 49 |
} |
| 50 |
if ( $found ) { |
| 51 |
$this->lines = $result; |
| 52 |
return true; |
| 53 |
} else { |
| 54 |
return $this->insert_after( $item ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @throws Exception |
| 60 |
*/ |
| 61 |
public function insert_after( $item, $after = '<?php' ) { |
| 62 |
$result = array(); |
| 63 |
$found = false; |
| 64 |
foreach ( $this->lines as $line ) { |
| 65 |
$result[] = $line; |
| 66 |
if ( ! $found && str_contains( $line, $after ) ) { |
| 67 |
$result[] = $item; |
| 68 |
$found = true; |
| 69 |
} |
| 70 |
} |
| 71 |
if ( ! $found ) { |
| 72 |
throw new \Exception( 'No opening php tag in ', $this->filename ); |
| 73 |
} |
| 74 |
$this->lines = $result; |
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Read data into line-by-line array. |
| 80 |
* |
| 81 |
* @param string $filename |
| 82 |
* |
| 83 |
* @return array |
| 84 |
*/ |
| 85 |
public static function read_lines( $filename ) { |
| 86 |
$str = self::read( $filename ); |
| 87 |
$result = explode( "\n", $str ); |
| 88 |
$result = array_map( function ( $line ) { |
| 89 |
return ltrim( $line, "\r" ); |
| 90 |
}, $result ); |
| 91 |
return $result; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* Read data from file. |
| 97 |
* |
| 98 |
* @param string $filename The pathname of the file to read. |
| 99 |
* |
| 100 |
* @return bool|string The file's contents. Empty string if the file doesn't already exist. false if it is not readable. |
| 101 |
*/ |
| 102 |
public static function read( $filename ) { |
| 103 |
if ( ! file_exists( $filename ) ) { |
| 104 |
return ''; |
| 105 |
} |
| 106 |
if ( ! is_readable( $filename ) ) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
$content = file_get_contents( $filename ); |
| 110 |
return self::remove_zero_space( $content ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Save data to file. |
| 115 |
*/ |
| 116 |
public function save() { |
| 117 |
|
| 118 |
$data = implode( "\n", $this->lines ); |
| 119 |
$data = self::remove_zero_space( $data ); |
| 120 |
file_put_contents( $this->filename, $data, LOCK_EX ); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Remove Unicode zero-width spaced <200b><200c> and BOMs |
| 125 |
* |
| 126 |
* @param array|string $content |
| 127 |
* |
| 128 |
* @return array|string|string[] |
| 129 |
*/ |
| 130 |
public static function remove_zero_space( $content ) { |
| 131 |
if ( is_array( $content ) ) { |
| 132 |
$content = array_map( __CLASS__ . '::remove_zero_space', $content ); |
| 133 |
return $content; |
| 134 |
} |
| 135 |
|
| 136 |
// Remove UTF-8 BOM if present |
| 137 |
if ( substr( $content, 0, 3 ) === "\xEF\xBB\xBF" ) { |
| 138 |
$content = substr( $content, 3 ); |
| 139 |
} |
| 140 |
|
| 141 |
$content = str_replace( "\xe2\x80\x8b", '', $content ); |
| 142 |
$content = str_replace( "\xe2\x80\x8c", '', $content ); |
| 143 |
$content = str_replace( "\xe2\x80\x8d", '', $content ); |
| 144 |
|
| 145 |
return $content; |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
|