| 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 |
* Formulates basename dirs from a full directory path. |
| 32 |
* |
| 33 |
* This takes Windows® `\app_data\` sub-folders into consideration. |
| 34 |
* |
| 35 |
* @package s2Member\Utilities |
| 36 |
* @since 110815 |
| 37 |
* |
| 38 |
* @param str $dir_path Directory path. |
| 39 |
* @return str Basename directory path; including a possible `\app_data\` directory. |
| 40 |
*/ |
| 41 |
public static function basename_dir_app_data ($dir_path = FALSE) |
| 42 |
{ |
| 43 |
$dir_path = rtrim ($dir_path, DIRECTORY_SEPARATOR . "/"); |
| 44 |
/**/ |
| 45 |
$dir_path = preg_replace ("/(" . preg_quote (DIRECTORY_SEPARATOR, "/") . "|\/)app_data$/i", "", $dir_path, 1, $app_data); |
| 46 |
/**/ |
| 47 |
return basename ($dir_path) . (($app_data) ? "/app_data" : ""); |
| 48 |
} |
| 49 |
/** |
| 50 |
* Strips a trailing `\app_data\` sub-directory from the full path. |
| 51 |
* |
| 52 |
* @package s2Member\Utilities |
| 53 |
* @since 3.5 |
| 54 |
* |
| 55 |
* @param str $dir_path Directory path. |
| 56 |
* @return str Directory path without `\app_data\`. |
| 57 |
*/ |
| 58 |
public static function strip_dir_app_data ($dir_path = FALSE) |
| 59 |
{ |
| 60 |
$dir_path = rtrim ($dir_path, DIRECTORY_SEPARATOR . "/"); |
| 61 |
/**/ |
| 62 |
return preg_replace ("/(" . preg_quote (DIRECTORY_SEPARATOR, "/") . "|\/)app_data$/i", "", $dir_path, 1); |
| 63 |
} |
| 64 |
/** |
| 65 |
* Finds the relative path, from one location to another. |
| 66 |
* |
| 67 |
* @package s2Member\Utilities |
| 68 |
* @since 110815 |
| 69 |
* |
| 70 |
* @param str $from The directory to calculate a relative path from. |
| 71 |
* @param str $to The directory or file to build a relative path to. |
| 72 |
* @return str String with the relative path to ``$to``. |
| 73 |
*/ |
| 74 |
public static function rel_path ($from = FALSE, $to = FALSE) |
| 75 |
{ |
| 76 |
if (!($rel_path = array ()) && is_string ($from) && strlen ($from) && is_string ($to) && strlen ($to)) |
| 77 |
{ |
| 78 |
$to = str_replace (DIRECTORY_SEPARATOR, "/", $to); |
| 79 |
$to = (strpos (basename ($to), ".") === false) ? rtrim ($to, "/") . "/" : $to; |
| 80 |
$to = $rel_path = preg_split ("/\//", $to); |
| 81 |
/**/ |
| 82 |
$from = str_replace (DIRECTORY_SEPARATOR, "/", $from); |
| 83 |
$from = (strpos (basename ($from), ".") !== false) ? dirname ($from) : $from; |
| 84 |
$from = preg_split ("/\//", rtrim ($from, "/") . "/"); |
| 85 |
/**/ |
| 86 |
foreach ($from as $depth => $dir) /* Each ``$from`` directory. */ |
| 87 |
{ |
| 88 |
if (isset ($to[$depth]) && $dir === $to[$depth]) |
| 89 |
array_shift($rel_path); |
| 90 |
/**/ |
| 91 |
else if (($remaining = count ($from) - $depth) > 1) |
| 92 |
{ |
| 93 |
$rel_path = array_pad ($rel_path, ((count ($rel_path) + $remaining - 1) * -1), ".."); |
| 94 |
break; /* Stop now, no need to go any further. */ |
| 95 |
} |
| 96 |
else |
| 97 |
$rel_path[0] = "./" . $rel_path[0]; |
| 98 |
} |
| 99 |
} |
| 100 |
/**/ |
| 101 |
return implode ("/", $rel_path); |
| 102 |
} |
| 103 |
/** |
| 104 |
* Shortens to a path from document root. |
| 105 |
* |
| 106 |
* @package s2Member\Utilities |
| 107 |
* @since 110815 |
| 108 |
* |
| 109 |
* @param str $path Directory or file path. |
| 110 |
* @return str Shorther path, from document root. |
| 111 |
*/ |
| 112 |
public static function doc_root_path ($path = FALSE) |
| 113 |
{ |
| 114 |
return preg_replace ("/^" . preg_quote (rtrim ($_SERVER["DOCUMENT_ROOT"], DIRECTORY_SEPARATOR . "/"), "/") . "/", "", (string)$path); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
?> |