PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 260805
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v260805
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 / src / includes / classes / utils-dirs.inc.php

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

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