1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class NFtp extends NObject
29: {
30:
31: const ASCII = FTP_ASCII;
32: const TEXT = FTP_TEXT;
33: const BINARY = FTP_BINARY;
34: const IMAGE = FTP_IMAGE;
35: const TIMEOUT_SEC = FTP_TIMEOUT_SEC;
36: const AUTOSEEK = FTP_AUTOSEEK;
37: const AUTORESUME = FTP_AUTORESUME;
38: const FAILED = FTP_FAILED;
39: const FINISHED = FTP_FINISHED;
40: const MOREDATA = FTP_MOREDATA;
41:
42:
43:
44: private $resource;
45:
46:
47: private $state;
48:
49:
50:
51: 52:
53: public function __construct()
54: {
55: if (!extension_loaded('ftp')) {
56: throw new Exception("PHP extension FTP is not loaded.");
57: }
58: }
59:
60:
61:
62: 63: 64: 65: 66: 67: 68: 69:
70: public function __call($name, $args)
71: {
72: $name = strtolower($name);
73: $silent = strncmp($name, 'try', 3) === 0;
74: $func = $silent ? substr($name, 3) : $name;
75: static $aliases = array(
76: 'sslconnect' => 'ssl_connect',
77: 'getoption' => 'get_option',
78: 'setoption' => 'set_option',
79: 'nbcontinue' => 'nb_continue',
80: 'nbfget' => 'nb_fget',
81: 'nbfput' => 'nb_fput',
82: 'nbget' => 'nb_get',
83: 'nbput' => 'nb_put',
84: );
85: $func = 'ftp_' . (isset($aliases[$func]) ? $aliases[$func] : $func);
86:
87: if (!function_exists($func)) {
88: return parent::__call($name, $args);
89: }
90:
91:
92: NTools::tryError();
93:
94: if ($func === 'ftp_connect' || $func === 'ftp_ssl_connect') {
95: $this->state = array($name => $args);
96: $this->resource = call_user_func_array($func, $args);
97: $res = NULL;
98:
99: } elseif (!is_resource($this->resource)) {
100: NTools::catchError($msg);
101: throw new NFtpException("Not connected to FTP server. Call connect() or ssl_connect() first.");
102:
103: } else {
104: if ($func === 'ftp_login' || $func === 'ftp_pasv') {
105: $this->state[$name] = $args;
106: }
107:
108: array_unshift($args, $this->resource);
109: $res = call_user_func_array($func, $args);
110:
111: if ($func === 'ftp_chdir' || $func === 'ftp_cdup') {
112: $this->state['chdir'] = array(ftp_pwd($this->resource));
113: }
114: }
115:
116: if (NTools::catchError($msg) && !$silent) {
117: throw new NFtpException($msg);
118: }
119:
120: return $res;
121: }
122:
123:
124:
125: 126: 127: 128:
129: public function reconnect()
130: {
131: @ftp_close($this->resource);
132: foreach ($this->state as $name => $args) {
133: call_user_func_array(array($this, $name), $args);
134: }
135: }
136:
137:
138:
139: 140: 141: 142: 143:
144: public function fileExists($file)
145: {
146: return is_array($this->nlist($file));
147: }
148:
149:
150:
151: 152: 153: 154: 155:
156: public function isDir($dir)
157: {
158: $current = $this->pwd();
159: try {
160: $this->chdir($dir);
161: } catch (NFtpException $e) {
162: }
163: $this->chdir($current);
164: return empty($e);
165: }
166:
167:
168:
169: 170: 171: 172: 173:
174: public function mkDirRecursive($dir)
175: {
176: $parts = explode('/', $dir);
177: $path = '';
178: while (!empty($parts)) {
179: $path .= array_shift($parts);
180: try {
181: if ($path !== '') $this->mkdir($path);
182: } catch (NFtpException $e) {
183: if (!$this->isDir($path)) {
184: throw new NFtpException("Cannot create directory '$path'.");
185: }
186: }
187: $path .= '/';
188: }
189: }
190:
191: }
192:
193:
194:
195: 196: 197: 198: 199:
200: class NFtpException extends Exception
201: {
202: }
203: