PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 111105
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v111105
260917 260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 All 189 releases
s2member / includes / classes / utils-dirs.inc.php

utils-dirs.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 111105, at includes/classes/utils-dirs.inc.php

207 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Directory utilities.
4 *
5 * Copyright: © 2009-2011
6 * {@link http://www.websharks-inc.com/ WebSharks, Inc.}
7 * ( coded in the USA )
8 *
9 * Released under the terms of the GNU General Public License.
10 * You should have received a copy of the GNU General Public License,
11 * along with this software. In the main directory, see: /licensing/
12 * If not, see: {@link http://www.gnu.org/licenses/}.
13 *
14 * @package s2Member\Utilities
15 * @since 3.5
16 */
17 if (realpath (__FILE__) === realpath ($_SERVER["SCRIPT_FILENAME"]))
18 exit("Do not access this file directly.");
19 /**/
20 if (!class_exists ("c_ws_plugin__s2member_utils_dirs"))
21 {
22 /**
23 * Directory utilities.
24 *
25 * @package s2Member\Utilities
26 * @since 3.5
27 */
28 class c_ws_plugin__s2member_utils_dirs
29 {
30 /**
31 * Normalizes directory separators.
32 *
33 * @package s2Member\Utilities
34 * @since 111017
35 *
36 * @param str $path Directory or file path.
37 * @return str Directory or file path, after having been normalized by this routine.
38 */
39 public static function n_dir_seps ($path = FALSE)
40 {
41 return rtrim (preg_replace ("/\/+/", "/", str_replace (array (DIRECTORY_SEPARATOR, "\\", "/"), "/", (string)$path)), "/");
42 }
43 /**
44 * Strips a trailing `/app_data/` sub-directory.
45 *
46 * @package s2Member\Utilities
47 * @since 3.5
48 *
49 * @param str $path Directory or file path.
50 * @return str Directory or file path without `/app_data/`.
51 */
52 public static function strip_dir_app_data ($path = FALSE)
53 {
54 return preg_replace ("/\/app_data$/", "", c_ws_plugin__s2member_utils_dirs::n_dir_seps ((string)$path));
55 }
56 /**
57 * Basename from a full directory or file path.
58 *
59 * @package s2Member\Utilities
60 * @since 110815
61 *
62 * @param str $path Directory or file path.
63 * @return str Basename; including a possible `/app_data/` directory.
64 */
65 public static function basename_dir_app_data ($path = FALSE)
66 {
67 $path = preg_replace ("/\/app_data$/", "", c_ws_plugin__s2member_utils_dirs::n_dir_seps ((string)$path), 1, $app_data);
68 /**/
69 return basename ($path) . (($app_data) ? "/app_data" : "");
70 }
71 /**
72 * Shortens to a directory or file path, from document root.
73 *
74 * @package s2Member\Utilities
75 * @since 110815
76 *
77 * @param str $path Directory or file path.
78 * @return str Shorther path, from document root.
79 */
80 public static function doc_root_path ($path = FALSE)
81 {
82 $doc_root = c_ws_plugin__s2member_utils_dirs::n_dir_seps ($_SERVER["DOCUMENT_ROOT"]);
83 /**/
84 return preg_replace ("/^" . preg_quote ($doc_root, "/") . "/", "", c_ws_plugin__s2member_utils_dirs::n_dir_seps ((string)$path));
85 }
86 /**
87 * Finds the relative path, from one location to another.
88 *
89 * @package s2Member\Utilities
90 * @since 110815
91 *
92 * @param str $from The full directory path to calculate a relative path `from`.
93 * @param str $to The full directory or file path, which this routine will build a relative path `to`.
94 * @param bool $try_realpaths Defaults to true. When true, try to acquire ``realpath()``, thereby resolving all relative paths and/or symlinks in ``$from`` and ``$to`` args.
95 * @param bool $use_win_diff_drive_jctn Defaults to true. When true, we'll work around issues with different drives on Windows® by trying to create a directory junction.
96 * @return str String with the relative path to: ``$to``.
97 */
98 public static function rel_path ($from = FALSE, $to = FALSE, $try_realpaths = TRUE, $use_win_diff_drive_jctn = TRUE)
99 {
100 if ( /* Initialize/validate. */!($rel_path = array ()) && is_string ($from) && strlen ($from) && is_string ($to) && strlen ($to))
101 {
102 $from = ($try_realpaths && ($_real_from = realpath ($from))) ? $_real_from : $from; /* Try this? */
103 $to = ($try_realpaths && ($_real_to = realpath ($to))) ? $_real_to : $to; /* Try to find realpath? */
104 /**/
105 $from = (is_file ($from)) ? dirname ($from) . "/" : $from . "/"; /* A (directory) with trailing `/`. */
106 /**/
107 $from = c_ws_plugin__s2member_utils_dirs::n_dir_seps ($from); /* Normalize directory separators now. */
108 $to = c_ws_plugin__s2member_utils_dirs::n_dir_seps ($to); /* Normalize directory separators here too. */
109 /**/
110 $from = preg_split ("/\//", $from); /* Convert ``$from``, to an array. Split on each directory separator. */
111 $to = preg_split ("/\//", $to); /* Also convert ``$to``, to an array. Split this on each directory separator. */
112 /**/
113 if ($use_win_diff_drive_jctn && stripos (PHP_OS, "win") === 0 /* Test for different drives on Windows® servers? */)
114 /**/
115 if (/*Drive? */preg_match ("/^([A-Z])\:$/i", $from[0], $_m) && ($_from_drive = $_m[1]) && preg_match ("/^([A-Z])\:$/i", $to[0], $_m) && ($_to_drive = $_m[1]))
116 if ( /* Are these locations on completely different drives? */$_from_drive !== $_to_drive)
117 {
118 $_from_drive_jctn = $_from_drive . ":/s2-" . $_to_drive . "-jctn";
119 $_sys_temp_dir_jctn = c_ws_plugin__s2member_utils_dirs::get_temp_dir (false) . "/s2-" . $_to_drive . "-jctn";
120 /**/
121 $_jctn = ($_sys_temp_dir_jctn && strpos ($_sys_temp_dir_jctn, $_from_drive) === 0) ? $_sys_temp_dir_jctn : $_from_drive_jctn;
122 /**/
123 if (($_from_drive_jctn_exists = (is_dir ($_from_drive_jctn)) ? true : false) || c_ws_plugin__s2member_utils_dirs::create_win_jctn ($_jctn, $_to_drive . ":/"))
124 {
125 array_shift /* Shift drive off and use junction now. */($to);
126 foreach (array_reverse (preg_split ("/\//", (($_from_drive_jctn_exists) ? $_from_drive_jctn : $_jctn))) as $_jctn_dir)
127 array_unshift ($to, $_jctn_dir);
128 }
129 else /* Else, we should trigger an error in this case. It's NOT possible to generate this. */
130 {
131 trigger_error ("Unable to generate a relative path across different Windows® drives." .
132 " Please create a Directory Junction here: " . $_from_drive_jctn . ", pointing to: " . $_to_drive . ":/", E_USER_ERROR);
133 }
134 }
135 /**/
136 unset ($_real_from, $_real_to, $_from_drive, $_to_drive, $_from_drive_jctn, $_sys_temp_dir_jctn, $_jctn, $_from_drive_jctn_exists, $_jctn_dir, $_m);
137 /**/
138 $rel_path = $to; /* Re-initialize. Start ``$rel_path`` as the value of the ``$to`` array. */
139 /**/
140 foreach (array_keys ($from) as $_depth) /* Each ``$from`` directory ``$_depth``. */
141 {
142 if (isset ($from[$_depth], $to[$_depth]) && $from[$_depth] === $to[$_depth])
143 array_shift($rel_path);
144 /**/
145 else if (($_remaining = count ($from) - $_depth) > 1)
146 {
147 $_left_p = -1 * (count ($rel_path) + ($_remaining - 1));
148 $rel_path = array_pad ($rel_path, $_left_p, "..");
149 break; /* Stop now, no need to go any further. */
150 }
151 else /* Else, set as the same directory `./[0]`. */
152 {
153 $rel_path[0] = "./" . $rel_path[0];
154 break; /* Stop now. */
155 }
156 }
157 }
158 /**/
159 return implode ("/", $rel_path);
160 }
161 /**
162 * Creates a directory Junction in Windows®.
163 *
164 * @package s2Member\Utilities
165 * @since 111013
166 *
167 * @param str $jctn Directory location of the Junction ( i.e. the link ).
168 * @param str $target Target directory that this Junction will connect to.
169 * @return bool True if created successfully, or already exists, else false.
170 */
171 public static function create_win_jctn ($jctn = FALSE, $target = FALSE)
172 {
173 if ($jctn && is_string ($jctn) && $target && is_string ($target) && stripos (PHP_OS, "win") === 0)
174 {
175 if (is_dir ($jctn)) /* Does it already exist? If so return now. */
176 return true; /* Return now to save extra processing time below. */
177 /**/
178 else if ( /* Possible? */function_exists ("shell_exec") && ($esa = "escapeshellarg"))
179 {
180 @shell_exec("mklink /J " . $esa ($jctn) . " " . $esa ($target));
181 /**/
182 clearstatcache (); /* Clear ``stat()`` cache now. */
183 if (is_dir ($jctn)) /* Created successfully? */
184 return true;
185 }
186 }
187 return false; /* Else return false. */
188 }
189 /**
190 * Get the system's temporary directory.
191 *
192 * @package s2Member\Utilities
193 * @since 111017
194 *
195 * @param str $fallback Defaults to true. If true, fallback on WordPress® routine if not available, or if not writable.
196 * @return str|bool Full string path to a writable temp directory, else false on failure.
197 */
198 public static function get_temp_dir ($fallback = TRUE)
199 {
200 $temp_dir = (($temp_dir = realpath (sys_get_temp_dir ())) && is_writable ($temp_dir)) ? $temp_dir : false;
201 $temp_dir = (!$temp_dir && $fallback && ($wp_temp_dir = realpath (get_temp_dir ())) && is_writable ($wp_temp_dir)) ? $wp_temp_dir : $temp_dir;
202 /**/
203 return ($temp_dir) ? c_ws_plugin__s2member_utils_dirs::n_dir_seps ($temp_dir) : false;
204 }
205 }
206 }
207 ?>