This repository has been archived by the owner on Dec 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGFtpWidget.php
133 lines (109 loc) · 2.96 KB
/
GFtpWidget.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
function gftpfilesort($f1, $f2){
$fn1 = strtolower($f1->filename);
$fn2 = strtolower($f2->filename);
$t1 = substr($f1->rights, 0, 1) == "d" ? 0 : 1;
$t2 = substr($f2->rights, 0, 1) == "d" ? 0 : 1;
if ($t1 == $t2) {
if ($fn1 < $fn2) return -1;
else if ($fn1 > $fn2) return 1;
else return 0;
} else if ($t1 < $t2) {
return -1;
} else {
return 1;
}
}
/**
* Widget used to display FTP folder content under a Yii grid.
*
* @author Hervé Guenot
* @link http://www.guenot.info
* @copyright Copyright © 2012 Hervé Guenot
* @license GNU LESSER GPL 3
* @version 1.0
*/
class GFtpWidget extends CWidget {
/**
* @var GFtpComponent|GFtpApplicationComponent FTP connection.
*/
public $ftp = null;
/**
* @var string Folder content.
*/
public $baseFolder = null;
/**
* @var bool Flag indicating if link will be displayed on folder to allow navigation.
*/
public $allowNavigation = true;
/**
* @var string Navigation parameter name (passed as GET variable).
*/
public $navKey = null;
/**
* @var array Columns to display.
*/
public $columns = null;
public function init() {
if ($this->ftp == null) {
if (!isset(Yii::app()->ftp)) {
throw new CException('No ftp connection found. Please set an application component call "ftp" or set property ftp ');
}
$this->ftp = Yii::app()->ftp;
}
if ($this->baseFolder == null || in_array(trim($this->baseFolder), array("", ".", ".."))) {
$this->baseFolder = "/";
}
if ($this->navKey == null || in_array(trim($this->navKey), array("", ".", ".."))) {
$this->navKey = 'ftpNavFolder';
}
if ($this->columns == null) {
$this->columns = array('rights', 'user', 'group', 'size', 'mdTime', 'filename');
}
if ($this->columns != null && !is_array($this->columns)) {
$this->columns = array($this->columns);
}
}
public function run() {
$error = null;
$files = array();
if ($this->allowNavigation && isset($_GET[$this->navKey])) {
try {
$this->ftp->chdir($_GET[$this->navKey]);
$this->baseFolder = $_GET[$this->navKey];
} catch (GFtpException $e) {
$error = $e;
}
} else {
try {
$this->ftp->chdir($this->baseFolder);
} catch (GFtpException $e) {
$error = $e;
}
}
if ($error == null) {
try {
$files = $this->ftp->ls('.', true, false);
$current = null;
$parent = false;
foreach ($files as $idx=>$file) {
if ($file->filename == '.') {
$current = $idx;
} else if ($file->filename == '..') {
$parent = true;
}
}
if ($current !== null){
unset($files[$current]);
}
usort ($files, 'gftpfilesort');
if (!$parent && trim($this->baseFolder) != "/") {
array_unshift($files, Yii::createComponent(array('class' => 'ext.GFtp.GFtpFile', 'filename' => '..', 'rights' => 'drwxr-xr-x')));
}
} catch (GFtpException $e) {
$error = $e;
}
}
$this->render ('ftpWidget', array('files' => $files, 'error' => $error));
}
}