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: * Property
36: * 31.07.2012 16:47
37: */
38: namespace MOC\Core\Drive\Directory;
39: use MOC\Api;
40: use MOC\Generic\Device\Core;
41:
42: /**
43: * Directory-Property
44: */
45: class Property implements Core {
46: /**
47: * Get Dependencies
48: *
49: * @static
50: * @return \MOC\Core\Depending
51: */
52: public static function InterfaceDepending() {
53: return Api::Core()->Depending();
54: }
55:
56: /**
57: * Get Singleton/Instance
58: *
59: * @static
60: * @return object
61: */
62: public static function InterfaceInstance() {
63: return new Property();
64: }
65:
66: /**
67: * Get Changelog
68: *
69: * @static
70: * @return \MOC\Core\Changelog
71: */
72: public static function InterfaceChangelog() {
73: return Api::Core()->Changelog()->Create( __CLASS__ );
74: }
75:
76:
77: /** @var null|string $Location */
78: private $Location = null;
79:
80: /** @var null|string $Name */
81: private $Name = null;
82: /** @var null|string $Path */
83: private $Path = null;
84: /** @var null|int $Time */
85: private $Time = null;
86:
87: /**
88: * Directory-Location
89: *
90: * @param null|string $Location
91: *
92: * @return null|string
93: */
94: public function Location( $Location = null ) {
95: if( $Location !== null ) {
96: $this->Location = $this->UpdateSyntax( $Location );
97: } return $this->Location;
98: }
99:
100: /**
101: * Directory-Name
102: *
103: * @param string|null $Name
104: *
105: * @return string|null
106: */
107: public function Name( $Name = null ) {
108: if( $Name !== null ) {
109: $this->Name = $Name;
110: } return $this->Name;
111: }
112:
113: /**
114: * Directory-Path
115: *
116: * @param string|null $Path
117: *
118: * @return string|null
119: */
120: public function Path( $Path = null ) {
121: if( $Path !== null ) {
122: $this->Path = $this->UpdateSyntax( $Path );
123: } return $this->Path;
124: }
125:
126: /**
127: * Directory-Timestamp
128: *
129: * @param int|null $Time
130: *
131: * @return int|null
132: */
133: public function Time( $Time = null ) {
134: if( $Time !== null ) {
135: $this->Time = $Time;
136: } return $this->Time;
137: }
138:
139: /**
140: * Read Directory-Properties
141: */
142: protected function UpdateProperties() {
143: $this->Name( pathinfo( $this->Location(), PATHINFO_FILENAME ) );
144: $this->Path( pathinfo( $this->Location(), PATHINFO_DIRNAME ) );
145: if( is_dir( $this->Location() ) ) {
146: $this->Time( filemtime( $this->Location() ) );
147: }
148: }
149:
150: /**
151: * Correct Path-Syntax
152: *
153: * @param string $Path
154: *
155: * @return string
156: */
157: protected function UpdateSyntax( $Path ) {
158: $Path = rtrim( preg_replace( '![\\\/]+!', DIRECTORY_SEPARATOR, $Path ), '\\/' );
159: while( preg_match( '!(\\\|/)[^\\\/]+?(\\\|/)\.\.!', $Path, $Match ) ) {
160: $Path = preg_replace( '!(\\\|/)[^\\\/]+?(\\\|/)\.\.!', '', $Path, 1 );
161: }
162:
163: if( !is_dir( $Path ) && !is_file( $Path ) && !empty( $Path ) ) {
164: mkdir( $Path, 0777, true );
165: }
166: return $Path;
167: }
168:
169: /**
170: * Problem to fix: The $_SERVER["DOCUMENT_ROOT"] is empty in IIS.
171: *
172: * Based on: http://fyneworks.blogspot.com/2007/08/php-documentroot-in-iis-windows-servers.html
173: * Added by Diego, 13-AUG-2007.
174: *
175: * @static
176: * @return void
177: */
178: protected function UpdateDocumentRootOnIIS() {
179: // let's make sure the $_SERVER['DOCUMENT_ROOT'] variable is set
180: if(!isset($_SERVER['DOCUMENT_ROOT'])){
181: if(isset($_SERVER['SCRIPT_FILENAME'])){
182: $_SERVER['DOCUMENT_ROOT'] = $this->UpdateSyntax( substr($_SERVER['SCRIPT_FILENAME'], 0, 0-strlen($_SERVER['PHP_SELF'])) );
183: };
184: };
185: if(!isset($_SERVER['DOCUMENT_ROOT'])){
186: if(isset($_SERVER['PATH_TRANSLATED'])){
187: $_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr( $this->UpdateSyntax( $_SERVER['PATH_TRANSLATED'] ), 0, 0-strlen($_SERVER['PHP_SELF'])));
188: };
189: };
190: // $_SERVER['DOCUMENT_ROOT'] is now set - you can use it as usual...
191: }
192:
193: }
194: