forked from hguenot/GFtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFtpParser.php
53 lines (44 loc) · 1.33 KB
/
GFtpParser.php
1
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/* Jison generated parser */
class GFtpParser
{
public function parse($str) {
if (!preg_match('/^ftp:\/\/[^\/]+$/', $str)){
throw new GFtpException("connection must start with ftp://");
}
$str = substr($str, 6);
// Split connect string using reverse string
$parts = explode('@', strrev($str), 2);
// array("<port>:<url>", "<pass>:<user>") or array("<port>:<url>")
$res = array();
if (count($parts) >= 1) {
$hosts = explode(":", $parts[0], 2);
// array("<port>", "<url>") or array("<url>")
if (count($hosts) == 1) {
$res['host'] = strrev($hosts[0]);
} else if(count($hosts) == 2) {
$res['port'] = strrev($hosts[0]);
$res['host'] = strrev($hosts[1]);
} else {
throw new GFtpException("Invalid URL / port");
}
}
if (count($parts) == 2) {
$hosts = explode(":", strrev($parts[1]), 2);
// array("<user>", "<pass>") or array("<user>")
if (count($hosts) == 1) {
$res['user'] = $hosts[0];
} else if(count($hosts) == 2) {
$res['user'] = $hosts[0];
$res['pass'] = $hosts[1];
}
}
if (!isset($res['host']))
throw new GFtpException("No host found");
if (isset($res['port']) && !preg_match('/^[0-9]+/', $res['port'])){
throw new GFtpException("Port is not a number");
}
Yii::trace(CVarDumper::dumpAsString($res), 'gftp');
return $res;
}
}