1 | <?php |
---|
2 | /****************************************************************************** |
---|
3 | * |
---|
4 | * Filename: pjmt_utils.php |
---|
5 | * |
---|
6 | * Description: Provides various utility functions for the toolkit |
---|
7 | * |
---|
8 | * Author: Evan Hunter |
---|
9 | * |
---|
10 | * Date: 20/1/2005 |
---|
11 | * |
---|
12 | * Project: JPEG Metadata |
---|
13 | * |
---|
14 | * Revision: 1.11 |
---|
15 | * |
---|
16 | * NOTE: This file will change with every revision update, hence will not |
---|
17 | * be shown in the changes list |
---|
18 | * |
---|
19 | * URL: http://electronics.ozhiker.com |
---|
20 | * |
---|
21 | * Copyright: Copyright " . $auteur . " 2004 |
---|
22 | * |
---|
23 | * License: This file is part of the PHP JPEG Metadata Toolkit. |
---|
24 | * |
---|
25 | * The PHP JPEG Metadata Toolkit is free software; you can |
---|
26 | * redistribute it and/or modify it under the terms of the |
---|
27 | * GNU General Public License as published by the Free Software |
---|
28 | * Foundation; either version 2 of the License, or (at your |
---|
29 | * option) any later version. |
---|
30 | * |
---|
31 | * The PHP JPEG Metadata Toolkit is distributed in the hope |
---|
32 | * that it will be useful, but WITHOUT ANY WARRANTY; without |
---|
33 | * even the implied warranty of MERCHANTABILITY or FITNESS |
---|
34 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
35 | * for more details. |
---|
36 | * |
---|
37 | * You should have received a copy of the GNU General Public |
---|
38 | * License along with the PHP JPEG Metadata Toolkit; if not, |
---|
39 | * write to the Free Software Foundation, Inc., 59 Temple |
---|
40 | * Place, Suite 330, Boston, MA 02111-1307 USA |
---|
41 | * |
---|
42 | * If you require a different license for commercial or other |
---|
43 | * purposes, please contact the author: evan@ozhiker.com |
---|
44 | * |
---|
45 | ******************************************************************************/ |
---|
46 | |
---|
47 | |
---|
48 | /****************************************************************************** |
---|
49 | * |
---|
50 | * Function: get_relative_path |
---|
51 | * |
---|
52 | * Description: Creates a relative path version of a file or directiroy name, |
---|
53 | * given a directory that it will be relative to. |
---|
54 | * |
---|
55 | * Parameters: target - a file or directory name which will be made relative |
---|
56 | * fromdir - the directory which the returned path is relative to |
---|
57 | * |
---|
58 | * Returns: output - the relative path |
---|
59 | * |
---|
60 | ******************************************************************************/ |
---|
61 | |
---|
62 | function get_relative_path( $target, $fromdir ) |
---|
63 | { |
---|
64 | // Check that the fromdir has a trailing slash, otherwise realpath will |
---|
65 | // strip the last directory name off |
---|
66 | if ( ( $fromdir[ strlen( $fromdir ) - 1 ] != "\\" ) && |
---|
67 | ( $fromdir[ strlen( $fromdir ) - 1 ] != "/" ) ) |
---|
68 | { |
---|
69 | $fromdir .= "/"; |
---|
70 | } |
---|
71 | |
---|
72 | // get a real directory name for each of the target and from directory |
---|
73 | $from = realpath( $fromdir ); |
---|
74 | $target = realpath( $target ); |
---|
75 | $to = dirname( $target ); |
---|
76 | |
---|
77 | // Can't get relative path with drive in path - remove it |
---|
78 | if ( ( $colonpos = strpos( $target, ":" ) ) != FALSE ) |
---|
79 | { |
---|
80 | $target = substr( $target, $colonpos+1 ); |
---|
81 | } |
---|
82 | if ( ( $colonpos = strpos( $from, ":" ) ) != FALSE ) |
---|
83 | { |
---|
84 | $from = substr( $from, $colonpos+1 ); |
---|
85 | } |
---|
86 | if ( ( $colonpos = strpos( $to, ":" ) ) != FALSE ) |
---|
87 | { |
---|
88 | $to = substr( $to, $colonpos+1 ); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | $path = "../"; |
---|
93 | $posval = 0; |
---|
94 | // Step through the paths until a difference is found (ignore slash, backslash differences |
---|
95 | // or the end of one is found |
---|
96 | while ( ( ( $from[$posval] == $to[$posval] ) || |
---|
97 | ( ( $from[$posval] == "\\" ) && ( $to[$posval] == "/" ) ) || |
---|
98 | ( ( $from[$posval] == "/" ) && ( $to[$posval] == "\\" ) ) ) && |
---|
99 | ( $from[$posval] && $to[$posval] ) ) |
---|
100 | { |
---|
101 | $posval++; |
---|
102 | } |
---|
103 | // Save the position of the first difference |
---|
104 | $diffpos = $posval; |
---|
105 | |
---|
106 | // Check if the directories are the same or |
---|
107 | // the if target is in a subdirectory of the fromdir |
---|
108 | if ( ( ! $from[$posval] ) && |
---|
109 | ( $to[$posval] == "/" || $to[$posval] == "\\" || !$to[$posval] ) ) |
---|
110 | { |
---|
111 | // target is in fromdir or a subdirectory |
---|
112 | // Build relative path starting with a ./ |
---|
113 | return ( "./" . substr( $target, $posval+1, strlen( $target ) ) ); |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | // target is outside the fromdir branch |
---|
118 | // find out how many "../"'s are necessary |
---|
119 | // Step through the fromdir path, checking for slashes |
---|
120 | // each slash encountered requires a "../" |
---|
121 | |
---|
122 | while ( $from[++$posval] && $posval < strlen($from)-1 ) |
---|
123 | { |
---|
124 | // Check for slash |
---|
125 | if ( ( $from[$posval] == "/" ) || ( $from[$posval] == "\\" ) ) |
---|
126 | { |
---|
127 | // Found a slash, add a "../" |
---|
128 | $path .= "../"; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | // Search backwards to find where the first common directory |
---|
133 | // as some letters in the first different directory names |
---|
134 | // may have been the same |
---|
135 | $diffpos--; |
---|
136 | while ( ( $to[$diffpos] != "/" ) && ( $to[$diffpos] != "\\" ) && $to[$diffpos] ) |
---|
137 | { |
---|
138 | $diffpos--; |
---|
139 | } |
---|
140 | // Build relative path to return |
---|
141 | |
---|
142 | return ( $path . substr( $target, $diffpos+1, strlen( $target ) ) ); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | /****************************************************************************** |
---|
147 | * End of Function: get_relative_path |
---|
148 | ******************************************************************************/ |
---|
149 | |
---|
150 | |
---|
151 | ?> |
---|