[0] | 1 | <?php |
---|
[3731] | 2 | /** |
---|
| 3 | * @brief importExport, a plugin for Dotclear 2 |
---|
| 4 | * |
---|
| 5 | * @package Dotclear |
---|
| 6 | * @subpackage Plugins |
---|
| 7 | * |
---|
| 8 | * @copyright Olivier Meunier & Association Dotclear |
---|
| 9 | * @copyright GPL-2.0-only |
---|
| 10 | */ |
---|
| 11 | |
---|
[3730] | 12 | if (!defined('DC_RC_PATH')) {return;} |
---|
[0] | 13 | |
---|
[840] | 14 | class flatBackup |
---|
[0] | 15 | { |
---|
[3730] | 16 | protected $fp; |
---|
| 17 | private $line_cols = array(); |
---|
| 18 | private $line_name; |
---|
| 19 | private $line_num; |
---|
[2566] | 20 | |
---|
[3730] | 21 | private $replacement = array( |
---|
| 22 | '/(?<!\\\\)(?>(\\\\\\\\)*+)(\\\\n)/u' => "\$1\n", |
---|
| 23 | '/(?<!\\\\)(?>(\\\\\\\\)*+)(\\\\r)/u' => "\$1\r", |
---|
| 24 | '/(?<!\\\\)(?>(\\\\\\\\)*+)(\\\\")/u' => '$1"', |
---|
| 25 | '/(\\\\\\\\)/' => '\\' |
---|
| 26 | ); |
---|
[2566] | 27 | |
---|
[3730] | 28 | public function __construct($file) |
---|
| 29 | { |
---|
| 30 | if (file_exists($file) && is_readable($file)) { |
---|
| 31 | $this->fp = fopen($file, 'rb'); |
---|
| 32 | $this->line_num = 1; |
---|
| 33 | } else { |
---|
| 34 | throw new Exception(__('No file to read.')); |
---|
| 35 | } |
---|
| 36 | } |
---|
[2566] | 37 | |
---|
[3730] | 38 | public function __destruct() |
---|
| 39 | { |
---|
| 40 | if ($this->fp) { |
---|
| 41 | @fclose($this->fp); |
---|
| 42 | } |
---|
| 43 | } |
---|
[2566] | 44 | |
---|
[3730] | 45 | public function getLine() |
---|
| 46 | { |
---|
| 47 | if (($line = $this->nextLine()) === false) { |
---|
| 48 | return false; |
---|
| 49 | } |
---|
[2566] | 50 | |
---|
[3730] | 51 | if (substr($line, 0, 1) == '[') { |
---|
| 52 | $this->line_name = substr($line, 1, strpos($line, ' ') - 1); |
---|
[2566] | 53 | |
---|
[3730] | 54 | $line = substr($line, strpos($line, ' ') + 1, -1); |
---|
| 55 | $this->line_cols = explode(',', $line); |
---|
[2566] | 56 | |
---|
[3730] | 57 | return $this->getLine(); |
---|
| 58 | } elseif (substr($line, 0, 1) == '"') { |
---|
| 59 | $line = preg_replace('/^"|"$/', '', $line); |
---|
| 60 | $line = preg_split('/(^"|","|(?<!\\\)\"$)/m', $line); |
---|
[2566] | 61 | |
---|
[3730] | 62 | if (count($this->line_cols) != count($line)) { |
---|
| 63 | throw new Exception(sprintf('Invalid row count at line %s', $this->line_num)); |
---|
| 64 | } |
---|
[2566] | 65 | |
---|
[3730] | 66 | $res = array(); |
---|
[2566] | 67 | |
---|
[3730] | 68 | for ($i = 0; $i < count($line); $i++) { |
---|
| 69 | $res[$this->line_cols[$i]] = |
---|
| 70 | preg_replace(array_keys($this->replacement), array_values($this->replacement), $line[$i]); |
---|
| 71 | } |
---|
[2566] | 72 | |
---|
[3730] | 73 | return new flatBackupItem($this->line_name, $res, $this->line_num); |
---|
| 74 | } else { |
---|
| 75 | return $this->getLine(); |
---|
| 76 | } |
---|
| 77 | } |
---|
[2566] | 78 | |
---|
[3730] | 79 | private function nextLine() |
---|
| 80 | { |
---|
| 81 | if (feof($this->fp)) { |
---|
| 82 | return false; |
---|
| 83 | } |
---|
| 84 | $this->line_num++; |
---|
[2566] | 85 | |
---|
[3730] | 86 | $line = fgets($this->fp); |
---|
| 87 | $line = trim($line); |
---|
[2566] | 88 | |
---|
[3730] | 89 | return empty($line) ? $this->nextLine() : $line; |
---|
| 90 | } |
---|
[0] | 91 | } |
---|
| 92 | |
---|
[840] | 93 | class flatBackupItem |
---|
[0] | 94 | { |
---|
[3730] | 95 | public $__name; |
---|
| 96 | public $__line; |
---|
| 97 | private $__data = array(); |
---|
[2566] | 98 | |
---|
[3730] | 99 | public function __construct($name, $data, $line) |
---|
| 100 | { |
---|
| 101 | $this->__name = $name; |
---|
| 102 | $this->__data = $data; |
---|
| 103 | $this->__line = $line; |
---|
| 104 | } |
---|
[2566] | 105 | |
---|
[3730] | 106 | public function f($name) |
---|
| 107 | { |
---|
| 108 | return iconv('UTF-8', 'UTF-8//IGNORE', $this->__data[$name]); |
---|
| 109 | } |
---|
[2566] | 110 | |
---|
[3730] | 111 | public function __get($name) |
---|
| 112 | { |
---|
| 113 | return $this->f($name); |
---|
| 114 | } |
---|
[2566] | 115 | |
---|
[3730] | 116 | public function __set($n, $v) |
---|
| 117 | { |
---|
| 118 | $this->__data[$n] = $v; |
---|
| 119 | } |
---|
[2566] | 120 | |
---|
[3730] | 121 | public function exists($n) |
---|
| 122 | { |
---|
| 123 | return isset($this->__data[$n]); |
---|
| 124 | } |
---|
[2566] | 125 | |
---|
[3730] | 126 | public function drop() |
---|
| 127 | { |
---|
| 128 | foreach (func_get_args() as $n) { |
---|
| 129 | if (isset($this->__data[$n])) { |
---|
| 130 | unset($this->__data[$n]); |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | } |
---|
[2566] | 134 | |
---|
[3730] | 135 | public function substitute($old, $new) |
---|
| 136 | { |
---|
| 137 | if (isset($this->__data[$old])) { |
---|
| 138 | $this->__data[$new] = $this->__data[$old]; |
---|
| 139 | unset($this->__data[$old]); |
---|
| 140 | } |
---|
| 141 | } |
---|
[0] | 142 | } |
---|