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