1: <?php
2: /**
3: * LICENSE (BSD)
4: *
5: * Copyright (c) 2012, Gerd Christian Kunze
6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions are
10: * met:
11: *
12: * * Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: *
15: * * Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * * Neither the name of Gerd Christian Kunze nor the names of the
20: * contributors may be used to endorse or promote products derived from
21: * this software without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34: *
35: * Read
36: * 16.10.2012 10:42
37: */
38: namespace MOC\Module\Network\Ftp\Directory;
39: use MOC\Api;
40: use MOC\Generic\Device\Module;
41: use MOC\Module\Network\Ftp\Directory;
42: use MOC\Module\Network\Ftp\File;
43: use MOC\Module\Network\Ftp\Transport\RawData;
44:
45: /**
46: * Directory-Read
47: */
48: class Read extends Write implements Module {
49:
50: /**
51: * Get Changelog
52: *
53: * @static
54: * @return \MOC\Core\Changelog
55: */
56: public static function InterfaceChangelog() {
57: return Api::Core()->Changelog();
58: }
59:
60: /**
61: * Get Dependencies
62: *
63: * @static
64: * @return \MOC\Core\Depending
65: */
66: public static function InterfaceDepending() {
67: return Api::Core()->Depending();
68: }
69:
70: /**
71: * Get Singleton/Instance
72: *
73: * @static
74: * @return Read
75: */
76: public static function InterfaceInstance() {
77: return new Read();
78: }
79:
80: /**
81: * @param $Name
82: *
83: * @return File
84: */
85: /* public function File( $Name ) {
86: return \MOC\Core\Drive::InterfaceInstance()->File()->Handle( $this->Location().DIRECTORY_SEPARATOR.$Name );
87: }
88: */
89: /**
90: * @return File[]
91: */
92: public function FileList() {
93: // Select Current-Directory as Work-Directory
94: $this->DirectoryWorking();
95: // Get Raw-File-List
96: if( false !== ( $FileList = ftp_rawlist( $this->Connection()->Handler(), $this->Location() ) ) ) {
97: $List = array();
98: $RawData = RawData::InterfaceInstance();
99: foreach( (array)$FileList as $File ) {
100: $RawData->Determine( $File );
101: var_dump( $File );
102: if( $RawData->RawDataType() == $RawData::RAW_DATA_TYPE_FILE ) {
103: array_push( $List, File::InterfaceInstance() );
104: /** @var File $Current */
105: $Current = end( $List );
106: $Current->Connection( $this->Connection() );
107: $Current->Handle( $this->Location().'/'.$RawData->RawDataName() );
108: $Current->Size( ftp_size( $this->Connection()->Handler(), $RawData->RawDataName() ) );
109: $Current->Time( ftp_mdtm( $this->Connection()->Handler(), $RawData->RawDataName() ) );
110: }
111: }
112: return $List;
113: }
114: return array();
115: }
116:
117: /**
118: * @return \MOC\Module\Drive\File[]
119: */
120: public function FileListRecursive() {
121: /* if( is_dir( $this->Location() ) ) {
122: $List = array();
123: if( $Directory = $this->DirectoryHandler() ) {
124: while ( false !== ( $Item = $Directory->read() ) ) {
125: if( $Item != '.' && $Item != '..' ) {
126: $Item = $this->UpdateSyntax($this->Location() . DIRECTORY_SEPARATOR . $Item );
127: if( is_dir( $Item ) ) {
128: $Recursive = \MOC\Core\Drive::InterfaceInstance()->Directory()->Handle( $Item );
129: $List = array_merge( $List, $Recursive->FileListRecursive() );
130: } else {
131: array_push( $List, \MOC\Core\Drive::InterfaceInstance()->File()->Handle( $Item ) );
132: }
133: }
134: }
135: }
136: $Directory->close();
137: return $List;
138: } else {
139: return array();
140: }
141: */ }
142:
143: /**
144: * @return \MOC\Core\Drive\Directory[]
145: */
146: public function DirectoryList() {
147: // Select Current-Directory as Work-Directory
148: $this->DirectoryWorking();
149: // Get Raw-Directory-List
150: if( false !== ( $DirectoryList = ftp_rawlist( $this->Connection()->Handler(), $this->Location() ) ) ) {
151: $List = array();
152: $RawData = RawData::InterfaceInstance();
153: foreach( (array)$DirectoryList as $Directory ) {
154: $RawData->Determine( $Directory );
155: if( $RawData->RawDataType() == $RawData::RAW_DATA_TYPE_DIRECTORY ) {
156: array_push( $List, Directory::InterfaceInstance() );
157: /** @var Directory $Current */
158: $Current = end( $List );
159: $Current->Connection( $this->Connection() );
160: $Current->Handle( $this->Location().'/'.$RawData->RawDataName() );
161: }
162: }
163: return $List;
164: }
165: return array();
166: }
167:
168: /**
169: * @return \MOC\Core\Drive\Directory[]
170: */
171: public function DirectoryListRecursive() {
172: /*
173: if( is_dir( $this->Location() ) ) {
174: $List = array();
175: if( $Directory = $this->DirectoryHandler() ) {
176: while ( false !== ( $Item = $Directory->read() ) ) {
177: if( $Item != '.' && $Item != '..' ) {
178: $Item = $this->UpdateSyntax( $this->Location() . DIRECTORY_SEPARATOR . $Item );
179: if( is_dir( $Item ) ) {
180: $Recursive = \MOC\Core\Drive::InterfaceInstance()->Directory()->Handle( $Item );
181: array_push( $List, \MOC\Core\Drive::InterfaceInstance()->Directory()->Handle( $Item ) );
182: $List = array_merge( $List, $Recursive->DirectoryListRecursive() );
183: }
184: }
185: }
186: }
187: $Directory->close();
188: return $List;
189: } else {
190: return array();
191: }
192: */
193: }
194:
195: /**
196: * @return string
197: */
198: public function DirectoryRoot() {
199: return '/';
200: }
201:
202: /**
203: * @return string
204: */
205: public function DirectoryCurrent() {
206: return ftp_pwd( $this->Connection()->Handler() );
207: }
208:
209: /**
210: * @return bool
211: */
212: public function DirectoryWorking() {
213: return ftp_chdir( $this->Connection()->Handler(), $this->Location() );
214: }
215: }
216: