1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of importExport, a plugin for DotClear2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2012 Olivier Meunier & Association Dotclear |
---|
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 | |
---|
14 | class flatBackup |
---|
15 | { |
---|
16 | protected $fp; |
---|
17 | private $line_cols = array(); |
---|
18 | private $line_name; |
---|
19 | private $line_num; |
---|
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'); |
---|
32 | $this->line_num = 1; |
---|
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 | { |
---|
47 | if (($line = $this->nextLine()) === false) { |
---|
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); |
---|
63 | $line = preg_split('/(^"|","|(?<!\\\)\"$)/m',$line); |
---|
64 | |
---|
65 | if (count($this->line_cols) != count($line)) { |
---|
66 | throw new Exception(sprintf('Invalid row count at line %s',$this->line_num)); |
---|
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 | |
---|
76 | return new flatBackupItem($this->line_name,$res,$this->line_num); |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | return $this->getLine(); |
---|
81 | } |
---|
82 | } |
---|
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 | } |
---|
96 | } |
---|
97 | |
---|
98 | class flatBackupItem |
---|
99 | { |
---|
100 | public $__name; |
---|
101 | public $__line; |
---|
102 | private $__data = array(); |
---|
103 | |
---|
104 | public function __construct($name,$data,$line) |
---|
105 | { |
---|
106 | $this->__name = $name; |
---|
107 | $this->__data = $data; |
---|
108 | $this->__line = $line; |
---|
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 | } |
---|