1616
1717use phpFastCache \Exceptions \phpFastCacheCoreException ;
1818use phpFastCache \Exceptions \phpFastCacheDriverException ;
19+ use phpFastCache \Util \Directory ;
1920
2021trait PathSeekerTrait
2122{
@@ -25,85 +26,91 @@ trait PathSeekerTrait
2526 public $ tmp = [];
2627
2728 /**
28- * @param bool $skip_create_path
29- * @param $config
29+ * @param bool $readonly
3030 * @return string
31- * @throws \Exception
31+ * @throws phpFastCacheDriverException
3232 */
33- public function getPath ($ getBasePath = false )
33+ 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 ' ;
36-
37- if (!isset ($ this ->config [ 'path ' ]) || $ this ->config [ 'path ' ] == '' ) {
38- if (self ::isPHPModule ()) {
39- $ path = $ tmp_dir ;
40- } else {
41- $ document_root_path = rtrim ($ _SERVER [ 'DOCUMENT_ROOT ' ], '/ ' ) . '/../ ' ;
42- $ path = isset ($ _SERVER [ 'DOCUMENT_ROOT ' ]) && is_writable ($ document_root_path ) ? $ document_root_path : rtrim (__DIR__ , '/ ' ) . '/ ' ;
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+ * Calculate the security key
41+ */
42+ {
43+ $ securityKey = array_key_exists ('securityKey ' , $ this ->config ) ? $ this ->config [ 'securityKey ' ] : '' ;
44+ if (!$ securityKey || $ securityKey === 'auto ' ) {
45+ if (isset ($ _SERVER [ 'HTTP_HOST ' ])) {
46+ $ securityKey = preg_replace ('/^www./ ' , '' , strtolower (str_replace (': ' , '_ ' , $ _SERVER [ 'HTTP_HOST ' ])));
47+ } else {
48+ $ securityKey = ($ this ->isPHPModule () ? 'web ' : 'cli ' );
49+ }
4350 }
44-
45- if ($ this ->config [ 'path ' ] != '' ) {
46- $ path = $ this ->config [ 'path ' ];
51+ if ($ securityKey !== '' ) {
52+ $ securityKey .= '/ ' ;
4753 }
48-
49- } else {
50- $ path = $ this ->config [ 'path ' ];
54+ $ securityKey = static ::cleanFileName ($ securityKey );
5155 }
52-
53- if ($ getBasePath === true ) {
54- return $ path ;
56+ /**
57+ * Extends the temporary directory
58+ * with the security key and the driver name
59+ */
60+ $ tmp_dir = rtrim ($ tmp_dir , '/ ' ) . DIRECTORY_SEPARATOR ;
61+ if (empty ($ this ->config [ 'path ' ]) || !is_string ($ this ->config [ 'path ' ])) {
62+ $ path = $ tmp_dir ;
63+ } else {
64+ $ path = rtrim ($ this ->config [ 'path ' ], '/ ' ) . DIRECTORY_SEPARATOR ;
5565 }
56-
57- $ securityKey = array_key_exists ('securityKey ' , $ this ->config ) ? $ this ->config [ 'securityKey ' ] : '' ;
58- if (!$ securityKey || $ securityKey === 'auto ' ) {
59- if (isset ($ _SERVER [ 'HTTP_HOST ' ])) {
60- $ securityKey = preg_replace ('/^www./ ' , '' , strtolower (str_replace (': ' , '_ ' , $ _SERVER [ 'HTTP_HOST ' ])));
61- } else {
62- $ securityKey = ($ this ->isPHPModule () ? 'web ' : 'cli ' );
66+ $ path_suffix = $ securityKey . DIRECTORY_SEPARATOR . $ this ->getDriverName ();
67+ $ full_path = Directory::getAbsolutePath ($ path . $ path_suffix );
68+ $ full_path_tmp = Directory::getAbsolutePath ($ tmp_dir . $ path_suffix );
69+ $ full_path_hash = md5 ($ full_path );
70+ /**
71+ * In readonly mode we only attempt
72+ * to verify if the directory exists
73+ * or not, if it does not then we
74+ * return the temp dir
75+ */
76+ if ($ readonly === true ) {
77+ if (!@file_exists ($ full_path ) || !@is_writable ($ full_path )){
78+ return $ full_path_tmp ;
6379 }
64- }
65-
66- if ($ securityKey !== '' ) {
67- $ securityKey .= '/ ' ;
68- }
69-
70- $ securityKey = static ::cleanFileName ($ securityKey );
71-
72- $ full_path = rtrim ($ path , '/ ' ) . '/ ' . $ securityKey ;
73- $ full_pathx = md5 ($ full_path );
74-
75-
76- if (!isset ($ this ->tmp [ $ full_pathx ])) {
77-
78- if (!@file_exists ($ full_path ) || !@is_writable ($ full_path )) {
80+ return $ full_path ;
81+ }else {
82+ if (!isset ($ this ->tmp [ $ full_path_hash ]) || (!@file_exists ($ full_path ) || !@is_writable ($ full_path ))) {
7983 if (!@file_exists ($ full_path )) {
8084 @mkdir ($ full_path , $ this ->setChmodAuto (), true );
81- }
82- if (!@is_writable ($ full_path )) {
85+ }else if (!@is_writable ($ full_path )) {
8386 @chmod ($ full_path , $ this ->setChmodAuto ());
8487 }
8588 if (!@is_writable ($ full_path )) {
86- // switch back to tmp dir again if the path is not writeable
87- $ full_path = rtrim ($ tmp_dir , '/ ' ) . '/ ' . $ securityKey ;
89+ /**
90+ * Switch back to tmp dir
91+ * again if the path is not writable
92+ */
93+ $ full_path = $ full_path_tmp ;
8894 if (!@file_exists ($ full_path )) {
8995 @mkdir ($ full_path , $ this ->setChmodAuto (), true );
9096 }
91- if (!@is_writable ($ full_path )) {
92- @chmod ($ full_path , $ this ->setChmodAuto ());
93- }
9497 }
98+ /**
99+ * In case there is no directory
100+ * writable including tye temporary
101+ * one, we must throw an exception
102+ */
95103 if (!@file_exists ($ full_path ) || !@is_writable ($ full_path )) {
96- throw new phpFastCacheCoreException ('PLEASE CREATE OR CHMOD ' . $ full_path . ' - 0777 OR ANY WRITABLE PERMISSION! ' , 92 );
104+ throw new phpFastCacheDriverException ('PLEASE CREATE OR CHMOD ' . $ full_path . ' - 0777 OR ANY WRITABLE PERMISSION! ' );
97105 }
106+ $ this ->tmp [ $ full_path_hash ] = $ full_path ;
107+ $ this ->htaccessGen ($ full_path , array_key_exists ('htaccess ' , $ this ->config ) ? $ this ->config [ 'htaccess ' ] : false );
98108 }
99-
100- $ this ->tmp [ $ full_pathx ] = true ;
101- $ this ->htaccessGen ($ full_path , array_key_exists ('htaccess ' , $ this ->config ) ? $ this ->config [ 'htaccess ' ] : false );
102109 }
103-
104110 return realpath ($ full_path );
105111 }
106112
113+
107114 /**
108115 * @param $keyword
109116 * @return string
0 commit comments