2006-01-24 08:18:26 +00:00
|
|
|
<?php
|
2006-01-23 07:54:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2012-11-21 15:29:04 +00:00
|
|
|
* Functions to handle paths in Drupal.
|
2006-01-23 07:54:08 +00:00
|
|
|
*/
|
|
|
|
|
2013-03-09 05:46:28 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
2006-04-24 19:25:37 +00:00
|
|
|
/**
|
|
|
|
* Check if the current page is the front page.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Boolean value: TRUE if the current page is the front page; FALSE if otherwise.
|
|
|
|
*/
|
|
|
|
function drupal_is_front_page() {
|
2009-11-20 06:12:45 +00:00
|
|
|
// Use the advanced drupal_static() pattern, since this is called very often.
|
2010-01-07 04:54:18 +00:00
|
|
|
static $drupal_static_fast;
|
|
|
|
if (!isset($drupal_static_fast)) {
|
|
|
|
$drupal_static_fast['is_front_page'] = &drupal_static(__FUNCTION__);
|
|
|
|
}
|
|
|
|
$is_front_page = &$drupal_static_fast['is_front_page'];
|
2008-12-09 07:16:10 +00:00
|
|
|
|
|
|
|
if (!isset($is_front_page)) {
|
2013-09-16 03:58:06 +00:00
|
|
|
$is_front_page = (current_path() == \Drupal::config('system.site')->get('page.front'));
|
2008-12-09 07:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $is_front_page;
|
2006-04-24 19:25:37 +00:00
|
|
|
}
|
2007-10-16 14:10:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a path matches any pattern in a set of patterns.
|
|
|
|
*
|
|
|
|
* @param $path
|
|
|
|
* The path to match.
|
|
|
|
* @param $patterns
|
|
|
|
* String containing a set of patterns separated by \n, \r or \r\n.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Boolean value: TRUE if the path matches a pattern, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
function drupal_match_path($path, $patterns) {
|
2009-04-03 17:41:32 +00:00
|
|
|
$regexps = &drupal_static(__FUNCTION__);
|
2007-10-17 12:34:16 +00:00
|
|
|
|
2007-10-16 14:10:33 +00:00
|
|
|
if (!isset($regexps[$patterns])) {
|
2010-04-24 15:10:34 +00:00
|
|
|
// Convert path settings to a regular expression.
|
|
|
|
// Therefore replace newlines with a logical or, /* with asterisks and the <front> with the frontpage.
|
|
|
|
$to_replace = array(
|
|
|
|
'/(\r\n?|\n)/', // newlines
|
|
|
|
'/\\\\\*/', // asterisks
|
|
|
|
'/(^|\|)\\\\<front\\\\>($|\|)/' // <front>
|
|
|
|
);
|
|
|
|
$replacements = array(
|
|
|
|
'|',
|
|
|
|
'.*',
|
2013-09-16 03:58:06 +00:00
|
|
|
'\1' . preg_quote(\Drupal::config('system.site')->get('page.front'), '/') . '\2'
|
2010-04-24 15:10:34 +00:00
|
|
|
);
|
|
|
|
$patterns_quoted = preg_quote($patterns, '/');
|
|
|
|
$regexps[$patterns] = '/^(' . preg_replace($to_replace, $replacements, $patterns_quoted) . ')$/';
|
2007-10-16 14:10:33 +00:00
|
|
|
}
|
2008-06-18 03:36:24 +00:00
|
|
|
return (bool)preg_match($regexps[$patterns], $path);
|
2007-10-16 14:10:33 +00:00
|
|
|
}
|
2009-05-06 15:51:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the current URL path of the page being viewed.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* - http://example.com/node/306 returns "node/306".
|
|
|
|
* - http://example.com/drupalfolder/node/306 returns "node/306" while
|
|
|
|
* base_path() returns "/drupalfolder/".
|
|
|
|
* - http://example.com/path/alias (which is a path alias for node/306) returns
|
|
|
|
* "node/306" as opposed to the path alias.
|
|
|
|
*
|
2013-02-06 11:50:46 +00:00
|
|
|
* This function is available only after DRUPAL_BOOTSTRAP_FULL.
|
2009-05-06 15:51:36 +00:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The current Drupal URL path.
|
2010-01-29 22:40:41 +00:00
|
|
|
*
|
|
|
|
* @see request_path()
|
2009-05-06 15:51:36 +00:00
|
|
|
*/
|
|
|
|
function current_path() {
|
2012-08-06 19:09:08 +00:00
|
|
|
// @todo Remove the check for whether the request service exists and the
|
|
|
|
// fallback code below, once the path alias logic has been figured out in
|
|
|
|
// http://drupal.org/node/1269742.
|
2013-12-22 21:03:21 +00:00
|
|
|
if (\Drupal::getContainer()->isScopeActive('request')) {
|
|
|
|
$path = \Drupal::request()->attributes->get('_system_path');
|
2013-02-06 12:47:22 +00:00
|
|
|
if ($path !== NULL) {
|
|
|
|
return $path;
|
|
|
|
}
|
2012-07-22 23:40:08 +00:00
|
|
|
}
|
2012-08-06 19:09:08 +00:00
|
|
|
// If we are outside the request scope, fall back to using the path stored in
|
|
|
|
// _current_path().
|
2012-04-29 15:16:27 +00:00
|
|
|
return _current_path();
|
2009-05-06 15:51:36 +00:00
|
|
|
}
|
2009-06-01 11:23:27 +00:00
|
|
|
|
2009-10-22 01:27:18 +00:00
|
|
|
/**
|
2013-01-10 23:50:55 +00:00
|
|
|
* Fetches a specific URL alias from the database.
|
2009-10-22 01:27:18 +00:00
|
|
|
*
|
|
|
|
* @param $conditions
|
|
|
|
* A string representing the source, a number representing the pid, or an
|
|
|
|
* array of query conditions.
|
|
|
|
*
|
2012-11-21 15:29:04 +00:00
|
|
|
* @see \Drupal\Core\Path\Path::load()
|
2009-10-22 01:27:18 +00:00
|
|
|
*/
|
|
|
|
function path_load($conditions) {
|
|
|
|
if (is_numeric($conditions)) {
|
|
|
|
$conditions = array('pid' => $conditions);
|
|
|
|
}
|
|
|
|
elseif (is_string($conditions)) {
|
|
|
|
$conditions = array('source' => $conditions);
|
|
|
|
}
|
|
|
|
elseif (!is_array($conditions)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-12-22 21:03:21 +00:00
|
|
|
return \Drupal::service('path.crud')->load($conditions);
|
2009-10-22 01:27:18 +00:00
|
|
|
}
|
|
|
|
|
#610234 by Gábor Hojtsy, ksenzee, cwgordon7, David_Rothstein, seutje, marcvangend, sun, JoshuaRogers, markus_petrux, Bojhan, Rob Loach, Everett Zufelt, drifter, markboulton, leisareichelt, et al: Added Overlay module to core, which shows administrative pages in a JS overlay, retaining context on the front-end site.
2009-12-02 07:28:22 +00:00
|
|
|
/**
|
2013-01-10 23:50:55 +00:00
|
|
|
* Determines whether a path is in the administrative section of the site.
|
#610234 by Gábor Hojtsy, ksenzee, cwgordon7, David_Rothstein, seutje, marcvangend, sun, JoshuaRogers, markus_petrux, Bojhan, Rob Loach, Everett Zufelt, drifter, markboulton, leisareichelt, et al: Added Overlay module to core, which shows administrative pages in a JS overlay, retaining context on the front-end site.
2009-12-02 07:28:22 +00:00
|
|
|
*
|
2013-01-10 23:50:55 +00:00
|
|
|
* By default, paths are considered to be non-administrative. If a path does
|
|
|
|
* not match any of the patterns in path_get_admin_paths(), or if it matches
|
|
|
|
* both administrative and non-administrative patterns, it is considered
|
#610234 by Gábor Hojtsy, ksenzee, cwgordon7, David_Rothstein, seutje, marcvangend, sun, JoshuaRogers, markus_petrux, Bojhan, Rob Loach, Everett Zufelt, drifter, markboulton, leisareichelt, et al: Added Overlay module to core, which shows administrative pages in a JS overlay, retaining context on the front-end site.
2009-12-02 07:28:22 +00:00
|
|
|
* non-administrative.
|
|
|
|
*
|
|
|
|
* @param $path
|
|
|
|
* A Drupal path.
|
2010-07-16 02:51:44 +00:00
|
|
|
*
|
#610234 by Gábor Hojtsy, ksenzee, cwgordon7, David_Rothstein, seutje, marcvangend, sun, JoshuaRogers, markus_petrux, Bojhan, Rob Loach, Everett Zufelt, drifter, markboulton, leisareichelt, et al: Added Overlay module to core, which shows administrative pages in a JS overlay, retaining context on the front-end site.
2009-12-02 07:28:22 +00:00
|
|
|
* @return
|
|
|
|
* TRUE if the path is administrative, FALSE otherwise.
|
|
|
|
*
|
|
|
|
* @see path_get_admin_paths()
|
|
|
|
* @see hook_admin_paths()
|
|
|
|
* @see hook_admin_paths_alter()
|
|
|
|
*/
|
|
|
|
function path_is_admin($path) {
|
|
|
|
$path_map = &drupal_static(__FUNCTION__);
|
|
|
|
if (!isset($path_map['admin'][$path])) {
|
|
|
|
$patterns = path_get_admin_paths();
|
|
|
|
$path_map['admin'][$path] = drupal_match_path($path, $patterns['admin']);
|
|
|
|
$path_map['non_admin'][$path] = drupal_match_path($path, $patterns['non_admin']);
|
|
|
|
}
|
|
|
|
return $path_map['admin'][$path] && !$path_map['non_admin'][$path];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-10 23:50:55 +00:00
|
|
|
* Gets a list of administrative and non-administrative paths.
|
#610234 by Gábor Hojtsy, ksenzee, cwgordon7, David_Rothstein, seutje, marcvangend, sun, JoshuaRogers, markus_petrux, Bojhan, Rob Loach, Everett Zufelt, drifter, markboulton, leisareichelt, et al: Added Overlay module to core, which shows administrative pages in a JS overlay, retaining context on the front-end site.
2009-12-02 07:28:22 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* An associative array containing the following keys:
|
|
|
|
* 'admin': An array of administrative paths and regular expressions
|
|
|
|
* in a format suitable for drupal_match_path().
|
|
|
|
* 'non_admin': An array of non-administrative paths and regular expressions.
|
|
|
|
*
|
|
|
|
* @see hook_admin_paths()
|
|
|
|
* @see hook_admin_paths_alter()
|
|
|
|
*/
|
|
|
|
function path_get_admin_paths() {
|
|
|
|
$patterns = &drupal_static(__FUNCTION__);
|
|
|
|
if (!isset($patterns)) {
|
2013-09-16 03:58:06 +00:00
|
|
|
$paths = \Drupal::moduleHandler()->invokeAll('admin_paths');
|
#610234 by Gábor Hojtsy, ksenzee, cwgordon7, David_Rothstein, seutje, marcvangend, sun, JoshuaRogers, markus_petrux, Bojhan, Rob Loach, Everett Zufelt, drifter, markboulton, leisareichelt, et al: Added Overlay module to core, which shows administrative pages in a JS overlay, retaining context on the front-end site.
2009-12-02 07:28:22 +00:00
|
|
|
drupal_alter('admin_paths', $paths);
|
|
|
|
// Combine all admin paths into one array, and likewise for non-admin paths,
|
|
|
|
// for easier handling.
|
|
|
|
$patterns = array();
|
|
|
|
$patterns['admin'] = array();
|
|
|
|
$patterns['non_admin'] = array();
|
|
|
|
foreach ($paths as $path => $enabled) {
|
|
|
|
if ($enabled) {
|
|
|
|
$patterns['admin'][] = $path;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$patterns['non_admin'][] = $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$patterns['admin'] = implode("\n", $patterns['admin']);
|
|
|
|
$patterns['non_admin'] = implode("\n", $patterns['non_admin']);
|
|
|
|
}
|
|
|
|
return $patterns;
|
|
|
|
}
|
2009-12-17 13:10:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks a path exists and the current user has access to it.
|
|
|
|
*
|
|
|
|
* @param $path
|
|
|
|
* The path to check.
|
|
|
|
* @param $dynamic_allowed
|
|
|
|
* Whether paths with menu wildcards (like user/%) should be allowed.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* TRUE if it is a valid path AND the current user has access permission,
|
|
|
|
* FALSE otherwise.
|
|
|
|
*/
|
|
|
|
function drupal_valid_path($path, $dynamic_allowed = FALSE) {
|
|
|
|
global $menu_admin;
|
|
|
|
// We indicate that a menu administrator is running the menu access check.
|
|
|
|
$menu_admin = TRUE;
|
|
|
|
if ($path == '<front>' || url_is_external($path)) {
|
|
|
|
$item = array('access' => TRUE);
|
|
|
|
}
|
|
|
|
elseif ($dynamic_allowed && preg_match('/\/\%/', $path)) {
|
|
|
|
// Path is dynamic (ie 'user/%'), so check directly against menu_router table.
|
|
|
|
if ($item = db_query("SELECT * FROM {menu_router} where path = :path", array(':path' => $path))->fetchAssoc()) {
|
2013-10-09 05:00:28 +00:00
|
|
|
$item['link_path'] = $item['path'];
|
|
|
|
$item['link_title'] = $item['title'];
|
2009-12-17 13:10:19 +00:00
|
|
|
$item['external'] = FALSE;
|
|
|
|
$item['options'] = '';
|
|
|
|
_menu_link_translate($item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$item = menu_get_item($path);
|
2013-07-24 20:54:37 +00:00
|
|
|
}
|
|
|
|
// Check the new routing system.
|
|
|
|
if (!empty($item['route_name'])) {
|
|
|
|
$map = array();
|
2013-09-16 03:58:06 +00:00
|
|
|
$route = \Drupal::service('router.route_provider')->getRouteByName($item['route_name']);
|
2013-07-24 20:54:37 +00:00
|
|
|
$item['access'] = menu_item_route_access($route, $path, $map);
|
2009-12-17 13:10:19 +00:00
|
|
|
}
|
|
|
|
$menu_admin = FALSE;
|
|
|
|
return $item && $item['access'];
|
|
|
|
}
|