Automad
 All Classes Functions Variables Pages
resolve.php
1 <?php
2 /*
3  * ....
4  * .: '':.
5  * :::: ':..
6  * ::. ''..
7  * .:'.. ..':.:::' . :. '':.
8  * :. '' '' '. ::::.. ..:
9  * ::::. ..':.. .'''::::: .
10  * :::::::.. '..:::: :. :::: :
11  * ::'':::::::. ':::.'':.:::: :
12  * :.. ''::::::....': '':: :
13  * :::::. '::::: : .. '' .
14  * .''::::::::... ':::.'' ..'' :.''''.
15  * :..:::''::::: :::::...:'' :..:
16  * ::::::. ':::: :::::::: ..:: .
17  * ::::::::.:::: :::::::: :'':.:: .''
18  * ::: '::::::::.' ''::::: :.' '': :
19  * ::: :::::::::..' :::: ::...' .
20  * ::: .:::::::::: :::: :::: .:'
21  * '::' ''::::::: :::: : :: :
22  * ':::: :::: :'' .:
23  * :::: :::: ..''
24  * :::: ..:::: .:''
25  * '''' '''''
26  *
27  *
28  * AUTOMAD
29  *
30  * Copyright (c) 2014 by Marc Anton Dahmen
31  * http://marcdahmen.de
32  *
33  * Licensed under the MIT license.
34  * http://automad.org/license
35  */
36 
37 
38 namespace Automad\Core;
39 
40 
41 defined('AUTOMAD') or die('Direct access not permitted!');
42 
43 
56 class Resolve {
57 
58 
72  public static function filePath($pagePath, $filePath) {
73 
74  if (strpos($filePath, '/') === 0) {
75 
76  // Relative to root
77  return AM_BASE_DIR . $filePath;
78 
79  } else {
80 
81  // Relative to page
82  return AM_BASE_DIR . AM_DIR_PAGES . $pagePath . $filePath;
83 
84  }
85 
86  }
87 
88 
101  public static function url($Page, $url) {
102 
103  if (strpos($url, '://') !== false || strpos($url, '//') === 0 || strpos($url, '?') === 0 || strpos($url, '#') === 0 || strpos($url, 'mailto:') === 0 || strpos($url, '&#') === 0) {
104 
105  // Absolute URL (contains '://' or starts with '//'), query string ('?'), anchor link ('#') or mailto link ('mailto:' and obfuscated '&#...').
106  return $url;
107 
108  } else if (strpos($url, '/') === 0) {
109 
110  // Relative to root
111  if (Parse::isFileName($url) || $url == '/' || strpos($url, '/?') === 0 || strpos($url, '/#') === 0) {
112  // Skip adding a possible '/index.php' when linking to files and to the homepage (possibly including a query string or anchor link),
113  // also if rewriting is disabled.
114  return AM_BASE_URL . $url;
115  } else {
116  return AM_BASE_URL . AM_INDEX . $url;
117  }
118 
119  } else {
120 
121  // Relative URL
122  if (Parse::isFileName($url)) {
123  $url = $Page->path . $url;
124  } else {
125  // Even though all trailing slashes get stripped out of beauty reasons, any page must still be understood as a directory instead of a file.
126  // Therefore it should be possible to link to a subpage with just href="subpage". Due to the missing trailing slash, that link would actually link to
127  // a page called subpage, but being a sibling of the current page instead of really being a child.
128  // Exampe:
129  // The current page is "http://domain.com/page" and has a link href="subpage".
130  // Just returning that link would reslove to "http://domain.com/subpage", which is wrong. It should be "http://domain.com/page/subpage".
131  // Therefore resolving that URL is also necessary.
132  $url = rtrim($Page->url, '/') . '/' . $url;
133  }
134 
135  // Resolve '../' and './'
136  $parts = explode('/', $url);
137  $resolvedParts = array();
138 
139  foreach ($parts as $part) {
140  if ($part == '..') {
141  array_pop($resolvedParts);
142  } else {
143  if ($part != '.') {
144  $resolvedParts[] = $part;
145  }
146  }
147  }
148 
149  $url = implode('/', $resolvedParts);
150 
151  // Remove slashes preceding query string or anchor links.
152  $url = str_replace(array('/?', '/#'), array('?', '#'), $url);
153 
154  // Trim trailing slashes, but always keep a leading one.
155  $url = '/' . trim($url, '/');
156 
157  // Prepend base.
158  if (Parse::isFileName($url)) {
159  return AM_BASE_URL . AM_DIR_PAGES . $url;
160  } else {
161  return AM_BASE_URL . AM_INDEX . $url;
162  }
163 
164  }
165 
166  }
167 
168 
169 }
170 
171 
172 ?>
static isFileName($str)
Definition: parse.php:154
static filePath($pagePath, $filePath)
Definition: resolve.php:72
static url($Page, $url)
Definition: resolve.php:101