| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Drupal specific stuff goes here |
| 38 | */ |
| 39 | abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base { |
| 40 | /** |
| 41 | * |
| 42 | */ |
| 43 | function __construct() { |
| 44 | $this->is_drupal = TRUE; |
| 45 | $this->supports_form_extensions = TRUE; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param string dir base civicrm directory |
| 50 | * Return default Site Settings |
| 51 | * @return array array |
| 52 | * - $url, (Joomla - non admin url) |
| 53 | * - $siteName, |
| 54 | * - $siteRoot |
| 55 | */ |
| 56 | function getDefaultSiteSettings($dir){ |
| 57 | $config = CRM_Core_Config::singleton(); |
| 58 | $siteName = $siteRoot = NULL; |
| 59 | $matches = array(); |
| 60 | if (preg_match( |
| 61 | '|/sites/([\w\.\-\_]+)/|', |
| 62 | $config->templateCompileDir, |
| 63 | $matches |
| 64 | )) { |
| 65 | $siteName = $matches[1]; |
| 66 | if ($siteName) { |
| 67 | $siteName = "/sites/$siteName/"; |
| 68 | $siteNamePos = strpos($dir, $siteName); |
| 69 | if ($siteNamePos !== FALSE) { |
| 70 | $siteRoot = substr($dir, 0, $siteNamePos); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | $url = $config->userFrameworkBaseURL; |
| 75 | return array($url, $siteName, $siteRoot); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Check if a resource url is within the drupal directory and format appropriately |
| 80 | * |
| 81 | * @param url (reference) |
| 82 | * |
| 83 | * @return bool: TRUE for internal paths, FALSE for external. The drupal_add_js fn is able to add js more |
| 84 | * efficiently if it is known to be in the drupal site |
| 85 | */ |
| 86 | function formatResourceUrl(&$url) { |
| 87 | $internal = FALSE; |
| 88 | $base = CRM_Core_Config::singleton()->resourceBase; |
| 89 | global $base_url; |
| 90 | // Handle absolute urls |
| 91 | // compares $url (which is some unknown/untrusted value from a third-party dev) to the CMS's base url (which is independent of civi's url) |
| 92 | // to see if the url is within our drupal dir, if it is we are able to treated it as an internal url |
| 93 | if (strpos($url, $base_url) === 0) { |
| 94 | $internal = TRUE; |
| 95 | $url = trim(str_replace($base_url, '', $url), '/'); |
| 96 | } |
| 97 | // Handle relative urls that are within the CiviCRM module directory |
| 98 | elseif (strpos($url, $base) === 0) { |
| 99 | $internal = TRUE; |
| 100 | $url = $this->appendCoreDirectoryToResourceBase(substr(drupal_get_path('module', 'civicrm'), 0, -6)) . trim(substr($url, strlen($base)), '/'); |
| 101 | } |
| 102 | // Strip query string |
| 103 | $q = strpos($url, '?'); |
| 104 | if ($q && $internal) { |
| 105 | $url = substr($url, 0, $q); |
| 106 | } |
| 107 | return $internal; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * In instance where civicrm folder has a drupal folder & a civicrm core folder @ the same level append the |
| 112 | * civicrm folder name to the url |
| 113 | * See CRM-13737 for discussion of how this allows implementers to alter the folder structure |
| 114 | * @todo - this only provides a limited amount of flexiblity - it still expects a 'civicrm' folder with a 'drupal' folder |
| 115 | * and is only flexible as to the name of the civicrm folder. |
| 116 | * |
| 117 | * @param string $url potential resource url based on standard folder assumptions |
| 118 | * @return string $url with civicrm-core directory appended if not standard civi dir |
| 119 | */ |
| 120 | function appendCoreDirectoryToResourceBase($url) { |
| 121 | global $civicrm_root; |
| 122 | $lastDirectory = basename($civicrm_root); |
| 123 | if($lastDirectory != 'civicrm') { |
| 124 | return $url .= $lastDirectory . '/'; |
| 125 | } |
| 126 | return $url; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url) |
| 131 | * |
| 132 | * @param $path string The path being linked to, such as "civicrm/add" |
| 133 | * @param $query string A query string to append to the link. |
| 134 | * @param $absolute boolean Whether to force the output to be an absolute link (beginning with http:). |
| 135 | * Useful for links that will be displayed outside the site, such as in an |
| 136 | * RSS feed. |
| 137 | * @param $fragment string A fragment identifier (named anchor) to append to the link. |
| 138 | * @param $htmlize boolean whether to convert to html eqivalant |
| 139 | * @param $frontend boolean a gross joomla hack |
| 140 | * @param $forceBackend boolean a gross joomla hack |
| 141 | * |
| 142 | * @return string an HTML string containing a link to the given path. |
| 143 | * @access public |
| 144 | * |
| 145 | */ |
| 146 | function url($path = NULL, $query = NULL, $absolute = FALSE, |
| 147 | $fragment = NULL, $htmlize = TRUE, |
| 148 | $frontend = FALSE, $forceBackend = FALSE |
| 149 | ) { |
| 150 | $config = CRM_Core_Config::singleton(); |
| 151 | $script = 'index.php'; |
| 152 | |
| 153 | $path = CRM_Utils_String::stripPathChars($path); |
| 154 | |
| 155 | if (isset($fragment)) { |
| 156 | $fragment = '#' . $fragment; |
| 157 | } |
| 158 | |
| 159 | if (!isset($config->useFrameworkRelativeBase)) { |
| 160 | $base = parse_url($config->userFrameworkBaseURL); |
| 161 | $config->useFrameworkRelativeBase = $base['path']; |
| 162 | } |
| 163 | $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase; |
| 164 | |
| 165 | $separator = $htmlize ? '&' : '&'; |
| 166 | |
| 167 | if (!$config->cleanURL) { |
| 168 | if (isset($path)) { |
| 169 | if (isset($query)) { |
| 170 | return $base . $script . '?q=' . $path . $separator . $query . $fragment; |
| 171 | } |
| 172 | else { |
| 173 | return $base . $script . '?q=' . $path . $fragment; |
| 174 | } |
| 175 | } |
| 176 | else { |
| 177 | if (isset($query)) { |
| 178 | return $base . $script . '?' . $query . $fragment; |
| 179 | } |
| 180 | else { |
| 181 | return $base . $fragment; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | else { |
| 186 | if (isset($path)) { |
| 187 | if (isset($query)) { |
| 188 | return $base . $path . '?' . $query . $fragment; |
| 189 | } |
| 190 | else { |
| 191 | return $base . $path . $fragment; |
| 192 | } |
| 193 | } |
| 194 | else { |
| 195 | if (isset($query)) { |
| 196 | return $base . $script . '?' . $query . $fragment; |
| 197 | } |
| 198 | else { |
| 199 | return $base . $fragment; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Get User ID from UserFramework system (Drupal) |
| 207 | * @param object $user object as described by the CMS |
| 208 | * @return mixed <NULL, number> |
| 209 | */ |
| 210 | function getUserIDFromUserObject($user) { |
| 211 | return !empty($user->uid) ? $user->uid : NULL; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Get Unique Identifier from UserFramework system (CMS) |
| 216 | * @param object $user object as described by the User Framework |
| 217 | * @return mixed $uniqueIdentifer Unique identifier from the user Framework system |
| 218 | * |
| 219 | */ |
| 220 | function getUniqueIdentifierFromUserObject($user) { |
| 221 | return empty($user->mail) ? NULL : $user->mail; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Get currently logged in user unique identifier - this tends to be the email address or user name. |
| 226 | * |
| 227 | * @return string $userID logged in user unique identifier |
| 228 | */ |
| 229 | function getLoggedInUniqueIdentifier() { |
| 230 | global $user; |
| 231 | return $this->getUniqueIdentifierFromUserObject($user); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Action to take when access is not permitted |
| 236 | */ |
| 237 | function permissionDenied() { |
| 238 | drupal_access_denied(); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Get Url to view user record |
| 243 | * @param integer $contactID Contact ID |
| 244 | * |
| 245 | * @return string |
| 246 | */ |
| 247 | function getUserRecordUrl($contactID) { |
| 248 | $uid = CRM_Core_BAO_UFMatch::getUFId($contactID); |
| 249 | if (CRM_Core_Session::singleton()->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(array('cms:administer users', 'cms:view user account'))) { |
| 250 | return CRM_Utils_System::url('user/' . $uid); |
| 251 | }; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Is the current user permitted to add a user |
| 256 | * @return bool |
| 257 | */ |
| 258 | function checkPermissionAddUser() { |
| 259 | if (CRM_Core_Permission::check('administer users')) { |
| 260 | return TRUE; |
| 261 | } |
| 262 | } |
| 263 | } |