/wp-content/plugins/zencache/src/includes/closures/Shared/TokenUtils.php

https://gitlab.com/iamgraeme/royalmile · PHP · 300 lines · 164 code · 22 blank · 114 comment · 59 complexity · 493e23e2faa6ff130eec972ddef23e92 MD5 · raw file

  1. <?php
  2. namespace WebSharks\ZenCache;
  3. /*
  4. * Current host.
  5. *
  6. * @since 150422 Rewrite.
  7. *
  8. * @param boolean $dashify Optional, defaults to a `FALSE` value.
  9. * If `TRUE`, the token is returned with dashes in place of `[^a-z0-9]`.
  10. *
  11. * @param boolean $consider_domain_mapping Consider?
  12. *
  13. * @param string $consider_domain_mapping_domain A specific domain?
  14. *
  15. * @return string Current host.
  16. *
  17. * @note The return value of this function is cached to reduce overhead on repeat calls.
  18. */
  19. $self->hostToken = function ($dashify = false, $consider_domain_mapping = false, $consider_domain_mapping_domain = '') use ($self) {
  20. if (!is_null($token = &$self->staticKey('hostToken', array($dashify, $consider_domain_mapping, $consider_domain_mapping_domain)))) {
  21. return $token; // Already cached this.
  22. }
  23. $token = ''; // Initialize token value.
  24. if (!is_multisite() || $self->isAdvancedCache()) {
  25. $token = (string) $_SERVER['HTTP_HOST'];
  26. } elseif ($consider_domain_mapping && $self->canConsiderDomainMapping()) {
  27. if (($consider_domain_mapping_domain = trim((string) $consider_domain_mapping_domain))) {
  28. $token = $consider_domain_mapping_domain;
  29. } elseif ($self->isDomainMapping()) {
  30. $token = (string) $_SERVER['HTTP_HOST'];
  31. } else { // For the current blog ID.
  32. $token = $self->domainMappingUrlFilter($self->currentUrl());
  33. $token = $self->parseUrl($token, PHP_URL_HOST);
  34. }
  35. }
  36. if (!$token) { // Use default?
  37. $token = (string) $_SERVER['HTTP_HOST'];
  38. }
  39. if ($token) { // Have token?
  40. $token = strtolower($token);
  41. if ($dashify) { // Dashify it?
  42. $token = preg_replace('/[^a-z0-9]/i', '-', $token);
  43. $token = trim($token, '-');
  44. }
  45. }
  46. return $token;
  47. };
  48. /*
  49. * Host for a specific blog.
  50. *
  51. * @since 150821 Improving multisite compat.
  52. *
  53. * @param boolean $dashify Optional, defaults to a `FALSE` value.
  54. * If `TRUE`, the token is returned with dashes in place of `[^a-z0-9]`.
  55. *
  56. * @param boolean $consider_domain_mapping Consider?
  57. *
  58. * @param string $consider_domain_mapping_domain A specific domain?
  59. *
  60. * @param boolean $fallback Fallback on blog's domain when mapping?
  61. *
  62. * @param integer $blog_id For which blog ID?
  63. *
  64. * @return string Host for a specific blog.
  65. *
  66. * @note The return value of this function is NOT cached in support of `switch_to_blog()`.
  67. */
  68. $self->hostTokenForBlog = function ($dashify = false, $consider_domain_mapping = false, $consider_domain_mapping_domain = '', $fallback = false, $blog_id = 0) use ($self) {
  69. if (!is_multisite() || $self->isAdvancedCache()) {
  70. return $self->hostToken($dashify, $consider_domain_mapping, $consider_domain_mapping_domain);
  71. }
  72. $token = ''; // Initialize token value.
  73. if ($consider_domain_mapping && $self->canConsiderDomainMapping()) {
  74. if (($consider_domain_mapping_domain = trim((string) $consider_domain_mapping_domain))) {
  75. $token = $consider_domain_mapping_domain; // Force this value.
  76. } else {
  77. $token = $self->domainMappingBlogDomain($blog_id, $fallback);
  78. }
  79. } elseif (($blog_details = $self->blogDetails($blog_id))) {
  80. $token = $blog_details->domain; // Unmapped domain.
  81. }
  82. if ($token) { // Have token?
  83. $token = strtolower($token);
  84. if ($dashify) { // Dashify it?
  85. $token = preg_replace('/[^a-z0-9]/i', '-', $token);
  86. $token = trim($token, '-');
  87. }
  88. }
  89. return $token;
  90. };
  91. /*
  92. * Current site's base directory.
  93. *
  94. * @since 150422 Rewrite.
  95. *
  96. * @param boolean $dashify Optional, defaults to a `FALSE` value.
  97. * If `TRUE`, the token is returned with dashes in place of `[^a-z0-9\/]`.
  98. *
  99. * @param boolean $consider_domain_mapping Consider?
  100. *
  101. * @return string Current site's base directory.
  102. *
  103. * @note The return value of this function is cached to reduce overhead on repeat calls.
  104. */
  105. $self->hostBaseToken = function ($dashify = false, $consider_domain_mapping = false) use ($self) {
  106. if (!is_null($token = &$self->staticKey('hostBaseToken', array($dashify, $consider_domain_mapping)))) {
  107. return $token; // Already cached this.
  108. }
  109. $token = '/'; // Assume NOT multisite; or own domain.
  110. if (!is_multisite()) {
  111. return $token; // Not applicable.
  112. }
  113. if (defined('SUBDOMAIN_INSTALL') && SUBDOMAIN_INSTALL) {
  114. return $token; // Not applicable.
  115. }
  116. if ($consider_domain_mapping && $self->canConsiderDomainMapping()) {
  117. return $token; // Not applicable.
  118. }
  119. if (defined('PATH_CURRENT_SITE')) {
  120. $token = (string) PATH_CURRENT_SITE;
  121. }
  122. $token = trim($token, '\\/'." \t\n\r\0\x0B");
  123. $token = isset($token[0]) ? '/'.$token.'/' : '/';
  124. if ($token !== '/' && $dashify) {
  125. $token = preg_replace('/[^a-z0-9\/]/i', '-', $token);
  126. $token = trim($token, '-');
  127. }
  128. return $token;
  129. };
  130. /*
  131. * Current blog's sub-directory.
  132. *
  133. * @since 150422 Rewrite.
  134. *
  135. * @param boolean $dashify Optional, defaults to a `FALSE` value.
  136. * If `TRUE`, the token is returned with dashes in place of `[^a-z0-9\/]`.
  137. *
  138. * @param boolean $consider_domain_mapping Consider?
  139. *
  140. * @param string $path Defaults to the current URI path.
  141. *
  142. * @return string Current blog's sub-directory.
  143. *
  144. * @note The return value of this function is cached to reduce overhead on repeat calls.
  145. */
  146. $self->hostDirToken = function ($dashify = false, $consider_domain_mapping = false, $path = null) use ($self) {
  147. if (!isset($path)) { // Use current/default path?
  148. $path = (string) $self->parseUrl($_SERVER['REQUEST_URI'], PHP_URL_PATH);
  149. }
  150. $path = '/'.ltrim((string) $path, '/'); // Force leading slash.
  151. if (!is_null($token = &$self->staticKey('hostDirToken', array($dashify, $consider_domain_mapping, $path)))) {
  152. return $token; // Already cached this.
  153. }
  154. $token = '/'; // Assume NOT multisite; or own domain.
  155. if (!is_multisite()) {
  156. return $token; // Not applicable.
  157. }
  158. if (defined('SUBDOMAIN_INSTALL') && SUBDOMAIN_INSTALL) {
  159. return $token; // Not applicable.
  160. }
  161. if ($consider_domain_mapping && $self->canConsiderDomainMapping()) {
  162. return $token; // Not applicable.
  163. }
  164. if ($path && $path !== '/' && ($host_base_token = trim($self->hostBaseToken(), '/'))) {
  165. $path_minus_base = preg_replace('/^\/'.preg_quote($host_base_token, '/').'(\/|$)/i', '${1}', $path);
  166. } else {
  167. $path_minus_base = $path; // Default value.
  168. }
  169. list($token) = explode('/', trim($path_minus_base, '/'));
  170. $token = trim($token, '\\/'." \t\n\r\0\x0B");
  171. $token = isset($token[0]) ? '/'.$token.'/' : '/';
  172. if ($token !== '/') { // Perhaps NOT the main site?
  173. $blog_paths_file = $self->cacheDir().'/'.strtolower(SHORT_NAME).'-blog-paths';
  174. if (!is_file($blog_paths_file) || !in_array($token, unserialize(file_get_contents($blog_paths_file)), true)) {
  175. $token = '/'; // NOT a real/valid child blog path.
  176. }
  177. }
  178. if ($token !== '/' && $dashify) {
  179. $token = preg_replace('/[^a-z0-9\/]/i', '-', $token);
  180. $token = trim($token, '-');
  181. }
  182. return $token;
  183. };
  184. /*
  185. * A blog's sub-directory.
  186. *
  187. * @since 150821 Improving multisite compat.
  188. *
  189. * @param boolean $dashify Optional, defaults to a `FALSE` value.
  190. * If `TRUE`, the token is returned with dashes in place of `[^a-z0-9]`.
  191. *
  192. * @param boolean $consider_domain_mapping Consider?
  193. *
  194. * @param integer $blog_id For which blog ID?
  195. *
  196. * @return string A blog's sub-directory.
  197. *
  198. * @note The return value of this function is NOT cached in support of `switch_to_blog()`.
  199. */
  200. $self->hostDirTokenForBlog = function ($dashify = false, $consider_domain_mapping = false, $blog_id = 0) use ($self) {
  201. if (!is_multisite() || $self->isAdvancedCache()) {
  202. return $self->hostDirToken($dashify, $consider_domain_mapping);
  203. }
  204. $token = '/'; // Initialize token value.
  205. if (defined('SUBDOMAIN_INSTALL') && SUBDOMAIN_INSTALL) {
  206. return $token; // Not applicable.
  207. }
  208. if ($consider_domain_mapping && $self->canConsiderDomainMapping()) {
  209. return $token; // Not applicable.
  210. }
  211. if (($blog_details = $self->blogDetails($blog_id))) {
  212. $path = $blog_details->path; // e.g., `[/base]/path/` (includes base).
  213. if ($path && $path !== '/' && ($host_base_token = trim($self->hostBaseToken(), '/'))) {
  214. $path_minus_base = preg_replace('/^\/'.preg_quote($host_base_token, '/').'(\/|$)/i', '${1}', $path);
  215. } else {
  216. $path_minus_base = $path; // Default value.
  217. }
  218. list($token) = explode('/', trim($path_minus_base, '/'));
  219. }
  220. $token = trim($token, '\\/'." \t\n\r\0\x0B");
  221. $token = isset($token[0]) ? '/'.$token.'/' : '/';
  222. if ($token !== '/') { // Perhaps NOT the main site?
  223. $blog_paths_file = $self->cacheDir().'/'.strtolower(SHORT_NAME).'-blog-paths';
  224. if (!is_file($blog_paths_file) || !in_array($token, unserialize(file_get_contents($blog_paths_file)), true)) {
  225. $token = '/'; // NOT a real/valid child blog path.
  226. }
  227. }
  228. if ($token !== '/' && $dashify) {
  229. $token = preg_replace('/[^a-z0-9\/]/i', '-', $token);
  230. $token = trim($token, '-');
  231. }
  232. return $token;
  233. };
  234. /*
  235. * Current site's base directory & current blog's sub-directory.
  236. *
  237. * @since 150422 Rewrite.
  238. *
  239. * @param boolean $dashify Optional, defaults to a `FALSE` value.
  240. * If `TRUE`, the tokens are returned with dashes in place of `[^a-z0-9\/]`.
  241. *
  242. * @param boolean $consider_domain_mapping Consider?
  243. *
  244. * @param string $path Defaults to the current URI path.
  245. *
  246. * @return string Current site's base directory & current blog's sub-directory.
  247. *
  248. * @note The return value of this function is cached to reduce overhead on repeat calls.
  249. */
  250. $self->hostBaseDirTokens = function ($dashify = false, $consider_domain_mapping = false, $path = null) use ($self) {
  251. if (!is_null($tokens = &$self->staticKey('hostBaseDirTokens', array($dashify, $consider_domain_mapping, $path)))) {
  252. return $tokens; // Already cached this.
  253. }
  254. $tokens = $self->hostBaseToken($dashify, $consider_domain_mapping);
  255. $tokens .= $self->hostDirToken($dashify, $consider_domain_mapping, $path);
  256. return ($tokens = preg_replace('/\/+/', '/', $tokens));
  257. };
  258. /*
  259. * A site's base directory & a blog's sub-directory.
  260. *
  261. * @since 150821 Improving multisite compat.
  262. *
  263. * @param boolean $dashify Optional, defaults to a `FALSE` value.
  264. * If `TRUE`, the tokens are returned with dashes in place of `[^a-z0-9\/]`.
  265. *
  266. * @param boolean $consider_domain_mapping Consider?
  267. *
  268. * @param integer $blog_id For which blog ID?
  269. *
  270. * @return string A site's base directory & a blog's sub-directory.
  271. *
  272. * @note The return value of this function is NOT cached in support of `switch_to_blog()`.
  273. */
  274. $self->hostBaseDirTokensForBlog = function ($dashify = false, $consider_domain_mapping = false, $blog_id = 0) use ($self) {
  275. $tokens = $self->hostBaseToken($dashify, $consider_domain_mapping);
  276. $tokens .= $self->hostDirTokenForBlog($dashify, $consider_domain_mapping, $blog_id);
  277. return ($tokens = preg_replace('/\/+/', '/', $tokens));
  278. };