@@ -32,14 +32,43 @@ trait IOHelperTrait
3232 */
3333 public function getPath ($ readonly = false )
3434 {
35- $ tmp_dir = rtrim (ini_get ('upload_tmp_dir ' ) ? ini_get ('upload_tmp_dir ' ) : sys_get_temp_dir (), '\\/ ' ) . DIRECTORY_SEPARATOR . 'phpfastcache ' ;
35+ /**
36+ * Get the base system temporary directory
37+ */
38+ $ tmp_dir = rtrim (ini_get ('upload_tmp_dir ' ) ?: sys_get_temp_dir (), '\\/ ' ) . DIRECTORY_SEPARATOR . 'phpfastcache ' ;
39+
40+ /**
41+ * Calculate the security key
42+ */
43+ {
44+ $ securityKey = array_key_exists ('securityKey ' , $ this ->config ) ? $ this ->config [ 'securityKey ' ] : '' ;
45+ if (!$ securityKey || $ securityKey === 'auto ' ) {
46+ if (isset ($ _SERVER [ 'HTTP_HOST ' ])) {
47+ $ securityKey = preg_replace ('/^www./ ' , '' , strtolower (str_replace (': ' , '_ ' , $ _SERVER [ 'HTTP_HOST ' ])));
48+ } else {
49+ $ securityKey = ($ this ->isPHPModule () ? 'web ' : 'cli ' );
50+ }
51+ }
52+
53+ if ($ securityKey !== '' ) {
54+ $ securityKey .= '/ ' ;
55+ }
56+
57+ $ securityKey = static ::cleanFileName ($ securityKey );
58+ }
3659
37- if (!isset ($ this ->config [ 'path ' ]) || $ this ->config [ 'path ' ] == '' ) {
60+ /**
61+ * Extends the temporary directory
62+ * with the security key and the driver name
63+ */
64+ $ tmp_dir = rtrim ($ tmp_dir , '/ ' ) . DIRECTORY_SEPARATOR ;
65+
66+ if (empty ($ this ->config [ 'path ' ]) || !is_string ($ this ->config [ 'path ' ])) {
3867 if (self ::isPHPModule ()) {
3968 $ path = $ tmp_dir ;
4069 } else {
4170 $ document_root_path = rtrim ($ _SERVER [ 'DOCUMENT_ROOT ' ], '/ ' ) . '/../ ' ;
42- $ path = isset ($ _SERVER [ 'DOCUMENT_ROOT ' ]) && is_writable ($ document_root_path ) ? $ document_root_path : rtrim (__DIR__ , '/ ' ) . ' PathSeekerTrait.php/ ' ;
71+ $ path = isset ($ _SERVER [ 'DOCUMENT_ROOT ' ]) && is_writable ($ document_root_path ) ? $ document_root_path : rtrim (__DIR__ , '/ ' ) . DIRECTORY_SEPARATOR ;
4372 }
4473
4574 if ($ this ->config [ 'path ' ] != '' ) {
@@ -50,84 +79,60 @@ public function getPath($readonly = false)
5079 $ path = $ this ->config [ 'path ' ];
5180 }
5281
53- $ securityKey = array_key_exists ('securityKey ' , $ this ->config ) ? $ this ->config [ 'securityKey ' ] : '' ;
54- if (!$ securityKey || $ securityKey === 'auto ' ) {
55- if (isset ($ _SERVER [ 'HTTP_HOST ' ])) {
56- $ securityKey = preg_replace ('/^www./ ' , '' , strtolower (str_replace (': ' , '_ ' , $ _SERVER [ 'HTTP_HOST ' ])));
57- } else {
58- $ securityKey = ($ this ->isPHPModule () ? 'web ' : 'cli ' );
59- }
60- }
61-
62- if ($ securityKey !== '' ) {
63- $ securityKey .= '/ ' ;
64- }
65-
66- $ securityKey = static ::cleanFileName ($ securityKey );
67-
68- $ full_path = rtrim ($ path , '/ ' ) . '/ ' . $ securityKey ;
69- $ full_pathx = $ full_path ;
82+ $ full_path = rtrim ($ path , '/ ' )
83+ . DIRECTORY_SEPARATOR
84+ . $ securityKey
85+ . DIRECTORY_SEPARATOR
86+ . $ this ->getDriverName ();
87+ $ full_path_hash = md5 ($ full_path );
7088
89+ /**
90+ * In readonly mode we only attempt
91+ * to verify if the directory exists
92+ * or not, if it does not then we
93+ * return the temp dir
94+ */
7195 if ($ readonly === true ) {
96+ if ($ this ->config [ 'autoTmpFallback ' ] && (@file_exists ($ full_path ) || !@is_writable ($ full_path ))){
97+ return $ tmp_dir ;
98+ }
7299 return $ full_path ;
73- }
100+ }else {
101+ if (!isset ($ this ->tmp [ $ full_path_hash ]) || (!@file_exists ($ full_path ) || !@is_writable ($ full_path ))) {
102+ if (!@file_exists ($ full_path )) {
103+ @mkdir ($ full_path , $ this ->setChmodAuto (), true );
104+ }else if (!@is_writable ($ full_path )) {
105+ @chmod ($ full_path , $ this ->setChmodAuto ());
106+ }
74107
75- if (!isset ($ this ->tmp [ $ full_pathx ]) || (!@file_exists ($ full_path ) || !@is_writable ($ full_path ))) {
76- if (!@file_exists ($ full_path )) {
77- @mkdir ($ full_path , $ this ->setChmodAuto (), true );
78- }else if (!@is_writable ($ full_path )) {
79- @chmod ($ full_path , $ this ->setChmodAuto ());
80- }
108+ if ($ this ->config [ 'autoTmpFallback ' ] && !@is_writable ($ full_path )) {
109+ /**
110+ * Switch back to tmp dir
111+ * again if the path is not writable
112+ */
113+ $ full_path = $ tmp_dir ;
114+ if (!@file_exists ($ full_path )) {
115+ @mkdir ($ full_path , $ this ->setChmodAuto (), true );
116+ }
117+ }
81118
82- if ($ this ->config [ 'autoTmpFallback ' ] && !@is_writable ($ full_path )) {
83119 /**
84- * Switch back to tmp dir
85- * again if the path is not writable
120+ * In case there is no directory
121+ * writable including tye temporary
122+ * one, we must throw an exception
86123 */
87- $ full_path = rtrim ($ tmp_dir , '/ ' ) . '/ ' . $ securityKey ;
88- if (!@file_exists ($ full_path )) {
89- @mkdir ($ full_path , $ this ->setChmodAuto (), true );
124+ if (!@file_exists ($ full_path ) || !@is_writable ($ full_path )) {
125+ throw new phpFastCacheIOException ('PLEASE CREATE OR CHMOD ' . $ full_path . ' - 0777 OR ANY WRITABLE PERMISSION! ' );
90126 }
91- }
92- if (!@file_exists ($ full_path ) || !@is_writable ($ full_path )) {
93- throw new phpFastCacheIOException ('PLEASE CREATE OR CHMOD ' . $ full_path . ' - 0777 OR ANY WRITABLE PERMISSION! ' );
94- }
95127
96- $ this ->tmp [ $ full_pathx ] = true ;
97- $ this ->htaccessGen ($ full_path , array_key_exists ('htaccess ' , $ this ->config ) ? $ this ->config [ 'htaccess ' ] : false );
128+ $ this ->tmp [ $ full_path_hash ] = $ full_path ;
129+ $ this ->htaccessGen ($ full_path , array_key_exists ('htaccess ' , $ this ->config ) ? $ this ->config [ 'htaccess ' ] : false );
130+ }
98131 }
99132
100133 return realpath ($ full_path );
101134 }
102135
103- /**
104- * @param $keyword
105- * @return string
106- */
107- protected function encodeFilename ($ keyword )
108- {
109- return md5 ($ keyword );
110- }
111-
112- /**
113- * @return bool
114- */
115- public function isExpired ()
116- {
117- trigger_error (__FUNCTION__ . '() is deprecated, use ExtendedCacheItemInterface::isExpired() instead. ' , E_USER_DEPRECATED );
118-
119- return true ;
120- }
121-
122-
123- /**
124- * @return string
125- * @throws \phpFastCache\Exceptions\phpFastCacheCoreException
126- */
127- public function getFileDir ()
128- {
129- return $ this ->getPath () . DIRECTORY_SEPARATOR . self ::FILE_DIR ;
130- }
131136
132137 /**
133138 * @param $keyword
@@ -137,22 +142,23 @@ public function getFileDir()
137142 */
138143 private function getFilePath ($ keyword , $ skip = false )
139144 {
140- $ path = $ this ->getFileDir ();
145+ $ path = $ this ->getPath ();
141146
142147 if ($ keyword === false ) {
143148 return $ path ;
144149 }
145150
146151 $ filename = $ this ->encodeFilename ($ keyword );
147- $ folder = substr ($ filename , 0 , 2 );
148- $ path = rtrim ($ path , '/ ' ) . '/ ' . $ folder ;
152+ $ folder = substr ($ filename , 0 , 2 ) . DIRECTORY_SEPARATOR . substr ($ filename , 2 , 2 );
153+ $ path = rtrim ($ path , '/ \\' ) . DIRECTORY_SEPARATOR . $ folder ;
154+
149155 /**
150156 * Skip Create Sub Folders;
151157 */
152158 if ($ skip == false ) {
153159 if (!file_exists ($ path )) {
154160 if (@!mkdir ($ path , $ this ->setChmodAuto (), true )) {
155- throw new phpFastCacheIOException ('PLEASE CHMOD ' . $ this -> getPath () . ' - ' . $ this ->setChmodAuto () . ' OR ANY WRITABLE PERMISSION! ' );
161+ throw new phpFastCacheIOException ('PLEASE CHMOD ' . $ path . ' - ' . $ this ->setChmodAuto () . ' OR ANY WRITABLE PERMISSION! ' );
156162 }
157163 }
158164 }
@@ -161,6 +167,26 @@ private function getFilePath($keyword, $skip = false)
161167 }
162168
163169
170+
171+ /**
172+ * @param $keyword
173+ * @return string
174+ */
175+ protected function encodeFilename ($ keyword )
176+ {
177+ return md5 ($ keyword );
178+ }
179+
180+ /**
181+ * @return bool
182+ */
183+ public function isExpired ()
184+ {
185+ trigger_error (__FUNCTION__ . '() is deprecated, use ExtendedCacheItemInterface::isExpired() instead. ' , E_USER_DEPRECATED );
186+
187+ return true ;
188+ }
189+
164190 /**
165191 * @param $this ->config
166192 * @return int
@@ -209,15 +235,18 @@ protected function htaccessGen($path, $create = true)
209235 }
210236
211237 if (!file_exists ($ path . "/.htaccess " )) {
212- $ html = "order deny, allow \r\n
213- deny from all \r\n
214- allow from 127.0.0.1 " ;
238+ $ content = <<<HTACCESS
239+ ### This .htaccess is auto-generated by PhpFastCache ###
240+ order deny, allow
241+ deny from all
242+ allow from 127.0.0.1
243+ HTACCESS ;
215244
216245 $ file = @fopen ($ path . '/.htaccess ' , 'w+ ' );
217246 if (!$ file ) {
218247 throw new phpFastCacheIOException ('PLEASE CHMOD ' . $ path . ' - 0777 OR ANY WRITABLE PERMISSION! ' );
219248 }
220- fwrite ($ file , $ html );
249+ fwrite ($ file , $ content );
221250 fclose ($ file );
222251 }
223252 }
0 commit comments