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:  29:  30:  31:  32:  33:  34:  35:  36:  37: 
 38: namespace MOC\Module\Network\Ftp;
 39: use MOC\Api;
 40: use MOC\Generic\Device\Module;
 41: 
 42:  43:  44: 
 45: class File extends File\Read implements Module {
 46:      47:  48:  49:  50:  51: 
 52:     public static function InterfaceInstance() {
 53:         return new File();
 54:     }
 55: 
 56:      57:  58:  59:  60:  61: 
 62:     public static function InterfaceDepending() {
 63:         return Api::Core()->Depending();
 64:     }
 65: 
 66:      67:  68:  69:  70:  71: 
 72:     public static function InterfaceChangelog() {
 73:         return Api::Core()->Changelog();
 74:     }
 75: 
 76:      77:  78:  79:  80:  81:  82: 
 83:     public function Handle( $Location ) {
 84:         $this->Location( $Location );
 85:         $this->UpdateProperties();
 86:         return $this;
 87:     }
 88: 
 89:      90:  91:  92:  93: 
 94:     public function Exists() {
 95:         if( ftp_size( $this->Connection()->Handler(), $this->Location() ) > -1 ) {
 96:             return true;
 97:         } else {
 98:             return false;
 99:         }
100:     }
101: 
102:     103: 104: 105: 106: 107: 108: 
109:     public function Content( $Content = null ) {
110:         if( $Content !== null ) {
111:             if( $this->Content !== null ) { $this->Changed( true ); }
112:             $this->Content = $Content;
113:             $this->Changed( true );
114:             return $this;
115:         } elseif( $this->Content === null ) {
116:             $this->ReadAsString();
117:             $this->Changed( false );
118:             return $this->Content;
119:         } else {
120:             return $this->Content;
121:         }
122:     }
123: 
124:     125: 126: 127: 128: 
129:     public function Hash() {
130:         return ( file_exists( $this->Location() ) ? sha1_file( $this->Location() ) : sha1( $this->Location() ) );
131:     }
132: 
133:     134: 135: 
136:     public function SetFileNameEncoding() {
137:         $FileName = self::MixedToUtf8( $this->Name() );
138: 
139:         $FileName = str_replace(
140:             array('ä', 'ö', 'ü', 'ß', 'ó', 'è', 'é'),
141:             array('ae', 'oe', 'ue', 'ss', 'o', 'e', 'e'),
142:             $FileName );
143: 
144:         $FileName = preg_replace( '/\s/s', '-', $FileName );
145: 
146:         $FileName = preg_replace('/[^a-z0-9_-]/isU', '', $FileName);
147: 
148:         $FileName = trim($FileName);
149: 
150:         $this->Name( $FileName );
151: 
152:         $this->Location( dirname( $this->Location() ).DIRECTORY_SEPARATOR.$FileName.(strlen($this->Extension())?'.'.$this->Extension():'') );
153: 
154:         return $this;
155:     }
156: 
157:     private static $DictionaryLatin1ToUtf8 = null;
158:     private static $DictionaryUtf8ToLatin1 = null;
159: 
160:     private static function BuildDictionary() {
161:         if( self::$DictionaryUtf8ToLatin1 === null || self::$DictionaryLatin1ToUtf8 === null ) {
162:             for ($Run = 32; $Run <= 255; $Run++) {
163:                 self::$DictionaryLatin1ToUtf8[chr($Run)] = utf8_encode(chr($Run));
164:                 self::$DictionaryUtf8ToLatin1[utf8_encode(chr($Run))] = chr($Run);
165:             }
166:         }
167:     }
168: 
169:     170: 171: 172: 173: 
174:     public static function MixedToLatin1( $Text ) {
175:         self::BuildDictionary();
176:         foreach ( self::$DictionaryUtf8ToLatin1 as $Key => $Val) {
177:             $Text = str_replace( $Key, $Val, $Text );
178:         }
179:         return $Text;
180:     }
181: 
182:     183: 184: 185: 186: 
187:     public static function MixedToUtf8( $Text ) {
188:         self::BuildDictionary();
189:         return utf8_encode( self::MixedToLatin1( $Text ) );
190:     }
191: }
192: