1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2010 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 backupFile |
---|
15 | { |
---|
16 | protected $fp; |
---|
17 | private $line_cols = array(); |
---|
18 | private $line_name; |
---|
19 | |
---|
20 | private $replacement = array( |
---|
21 | '/(?<!\\\\)(?>(\\\\\\\\)*+)(\\\\n)/u' => "\$1\n", |
---|
22 | '/(?<!\\\\)(?>(\\\\\\\\)*+)(\\\\r)/u' => "\$1\r", |
---|
23 | '/(?<!\\\\)(?>(\\\\\\\\)*+)(\\\\")/u' => '$1"', |
---|
24 | '/(\\\\\\\\)/' => '\\' |
---|
25 | ); |
---|
26 | |
---|
27 | public function __construct($file) |
---|
28 | { |
---|
29 | if (file_exists($file) && is_readable($file)) { |
---|
30 | $this->fp = fopen($file,'rb'); |
---|
31 | } else { |
---|
32 | throw new Exception(__('No file to read.')); |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | public function __destruct() |
---|
37 | { |
---|
38 | if ($this->fp) { |
---|
39 | fclose($this->fp); |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | public function getLine() |
---|
44 | { |
---|
45 | if (feof($this->fp)) { |
---|
46 | return false; |
---|
47 | } |
---|
48 | |
---|
49 | $line = trim(fgets($this->fp)); |
---|
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('Invalid row count'); |
---|
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 backupFileItem($this->line_name,$res); |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | return $this->getLine(); |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | class backupFileItem |
---|
86 | { |
---|
87 | public $__name; |
---|
88 | private $__data = array(); |
---|
89 | |
---|
90 | public function __construct($name,$data) |
---|
91 | { |
---|
92 | $this->__name = $name; |
---|
93 | $this->__data = $data; |
---|
94 | } |
---|
95 | |
---|
96 | public function f($name) |
---|
97 | { |
---|
98 | return iconv('UTF-8','UTF-8//IGNORE',$this->__data[$name]); |
---|
99 | } |
---|
100 | |
---|
101 | public function __get($name) |
---|
102 | { |
---|
103 | return $this->f($name); |
---|
104 | } |
---|
105 | |
---|
106 | public function __set($n,$v) |
---|
107 | { |
---|
108 | $this->__data[$n] = $v; |
---|
109 | } |
---|
110 | |
---|
111 | public function exists($n) |
---|
112 | { |
---|
113 | return isset($this->__data[$n]); |
---|
114 | } |
---|
115 | |
---|
116 | public function drop() |
---|
117 | { |
---|
118 | foreach (func_get_args() as $n) { |
---|
119 | if (isset($this->__data[$n])) { |
---|
120 | unset($this->__data[$n]); |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | public function substitute($old,$new) |
---|
126 | { |
---|
127 | if (isset($this->__data[$old])) { |
---|
128 | $this->__data[$new] = $this->__data[$old]; |
---|
129 | unset($this->__data[$old]); |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | ?> |
---|