1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Nip Framework. |
5
|
|
|
* |
6
|
|
|
* @category Nip |
7
|
|
|
* |
8
|
|
|
* @copyright 2009 Nip Framework |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
10
|
|
|
* |
11
|
|
|
* @version SVN: $Id: Handler.php 70 2009-04-29 11:47:12Z victor.stanciu $ |
12
|
|
|
*/ |
13
|
|
|
class Nip_File_Handler |
14
|
|
|
{ |
15
|
|
|
const MODE_READ = 'r'; |
16
|
|
|
const MODE_WRITE = 'w'; |
17
|
|
|
const MODE_APPEND = 'a'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public $name; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var null|string |
26
|
|
|
*/ |
27
|
|
|
public $path = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var null|string |
31
|
|
|
*/ |
32
|
|
|
public $url = null; |
33
|
|
|
|
34
|
|
|
public $data; |
35
|
|
|
public $extension; |
36
|
|
|
public $permissions = 0777; |
37
|
|
|
protected $handle = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Nip_File_Handler constructor. |
41
|
|
|
* |
42
|
|
|
* @param bool $data |
43
|
|
|
*/ |
44
|
|
|
public function __construct($data = false) |
45
|
|
|
{ |
46
|
|
|
if ($data) { |
47
|
|
|
foreach ($data as $key => $value) { |
|
|
|
|
48
|
|
|
$this->{$key} = $value; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $name |
55
|
|
|
* @param $arguments |
56
|
|
|
* |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function __call($name, $arguments) |
60
|
|
|
{ |
61
|
|
|
if (substr($name, 0, 3) == 'set') { |
62
|
|
|
$name = str_replace('set', '', $name); |
63
|
|
|
$name[0] = strtolower($name[0]); |
64
|
|
|
|
65
|
|
|
$this->$name = $arguments[0]; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} else { |
69
|
|
|
trigger_error("Method [$name] not defined", E_USER_ERROR); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $upload |
75
|
|
|
*/ |
76
|
|
|
public function upload($upload) |
77
|
|
|
{ |
78
|
|
|
move_uploaded_file($upload['tmp_name'], $this->path); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function delete() |
85
|
|
|
{ |
86
|
|
|
return Nip_File_System::instance()->deleteFile($this->path); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return $this|false |
91
|
|
|
*/ |
92
|
|
|
public function gzip() |
93
|
|
|
{ |
94
|
|
|
if (function_exists('gzencode')) { |
95
|
|
|
$this->data = gzencode($this->data, 9, FORCE_GZIP); |
96
|
|
|
} else { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param bool $data |
105
|
|
|
* @param string $mode |
106
|
|
|
*/ |
107
|
|
|
public function write($data = false, $mode = self::MODE_APPEND) |
108
|
|
|
{ |
109
|
|
|
if ($data) { |
110
|
|
|
$this->setData($data); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->open($mode); |
114
|
|
|
fwrite($this->handle, $this->data); |
|
|
|
|
115
|
|
|
$this->close($mode); |
|
|
|
|
116
|
|
|
|
117
|
|
|
chmod($this->path, $this->permissions); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param bool $data |
122
|
|
|
*/ |
123
|
|
|
public function rewrite($data = false) |
124
|
|
|
{ |
125
|
|
|
return $this->write($data, self::MODE_WRITE); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $mode |
130
|
|
|
* |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
|
|
public function open($mode = self::MODE_READ) |
134
|
|
|
{ |
135
|
|
|
$this->handle = fopen($this->path, $mode); |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function close() |
144
|
|
|
{ |
145
|
|
|
if ($this->handle) { |
146
|
|
|
fclose($this->handle); |
147
|
|
|
$this->handle = null; |
148
|
|
|
} else { |
149
|
|
|
trigger_error('Attempting to close an unopened file', E_USER_WARNING); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return null|string |
157
|
|
|
*/ |
158
|
|
|
public function getUrl() |
159
|
|
|
{ |
160
|
|
|
if ($this->url === null) { |
161
|
|
|
$this->initUrl(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this->url; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function initUrl() |
168
|
|
|
{ |
169
|
|
|
$this->url = ''; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return null|string |
174
|
|
|
*/ |
175
|
|
|
public function getPath() |
176
|
|
|
{ |
177
|
|
|
if ($this->path === null) { |
178
|
|
|
$this->initPath(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->path; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function initPath() |
185
|
|
|
{ |
186
|
|
|
$this->path = ''; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|