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