commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Utils / System / DrupalBase.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 * Does this CMS / UF support a CMS specific logging mechanism?
43 * @todo - we should think about offering up logging mechanisms in a way that is also extensible by extensions
44 * @var bool
45 */
46 var $supports_UF_Logging = TRUE;
47
48 /**
49 */
50 public function __construct() {
51 /**
52 * deprecated property to check if this is a drupal install. The correct method is to have functions on the UF classes for all UF specific
53 * functions and leave the codebase oblivious to the type of CMS
54 * @deprecated
55 * @var bool
56 */
57 $this->is_drupal = TRUE;
58 $this->supports_form_extensions = TRUE;
59 }
60
61 /**
62 * @inheritDoc
63 */
64 public function getDefaultSiteSettings($dir) {
65 $config = CRM_Core_Config::singleton();
66 $siteName = $siteRoot = NULL;
67 $matches = array();
68 if (preg_match(
69 '|/sites/([\w\.\-\_]+)/|',
70 $config->templateCompileDir,
71 $matches
72 )) {
73 $siteName = $matches[1];
74 if ($siteName) {
75 $siteName = "/sites/$siteName/";
76 $siteNamePos = strpos($dir, $siteName);
77 if ($siteNamePos !== FALSE) {
78 $siteRoot = substr($dir, 0, $siteNamePos);
79 }
80 }
81 }
82 $url = $config->userFrameworkBaseURL;
83 return array($url, $siteName, $siteRoot);
84 }
85
86 /**
87 * Check if a resource url is within the drupal directory and format appropriately.
88 *
89 * @param $url (reference)
90 *
91 * @return bool
92 * TRUE for internal paths, FALSE for external. The drupal_add_js fn is able to add js more
93 * efficiently if it is known to be in the drupal site
94 */
95 public function formatResourceUrl(&$url) {
96 $internal = FALSE;
97 $base = CRM_Core_Config::singleton()->resourceBase;
98 global $base_url;
99 // Handle absolute urls
100 // 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)
101 // to see if the url is within our drupal dir, if it is we are able to treated it as an internal url
102 if (strpos($url, $base_url) === 0) {
103 $file = trim(str_replace($base_url, '', $url), '/');
104 // CRM-18130: Custom CSS URL not working if aliased or rewritten
105 if (file_exists(DRUPAL_ROOT . $file)) {
106 $url = $file;
107 $internal = TRUE;
108 }
109 }
110 // Handle relative urls that are within the CiviCRM module directory
111 elseif (strpos($url, $base) === 0) {
112 $internal = TRUE;
113 $url = $this->appendCoreDirectoryToResourceBase(substr(drupal_get_path('module', 'civicrm'), 0, -6)) . trim(substr($url, strlen($base)), '/');
114 }
115 // Strip query string
116 $q = strpos($url, '?');
117 if ($q && $internal) {
118 $url = substr($url, 0, $q);
119 }
120 return $internal;
121 }
122
123 /**
124 * In instance where civicrm folder has a drupal folder & a civicrm core folder @ the same level append the
125 * civicrm folder name to the url
126 * See CRM-13737 for discussion of how this allows implementers to alter the folder structure
127 * @todo - this only provides a limited amount of flexiblity - it still expects a 'civicrm' folder with a 'drupal' folder
128 * and is only flexible as to the name of the civicrm folder.
129 *
130 * @param string $url
131 * Potential resource url based on standard folder assumptions.
132 * @return string
133 * with civicrm-core directory appended if not standard civi dir
134 */
135 public function appendCoreDirectoryToResourceBase($url) {
136 global $civicrm_root;
137 $lastDirectory = basename($civicrm_root);
138 if ($lastDirectory != 'civicrm') {
139 return $url .= $lastDirectory . '/';
140 }
141 return $url;
142 }
143
144 /**
145 * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url)
146 *
147 * @inheritDoc
148 */
149 public function url(
150 $path = NULL,
151 $query = NULL,
152 $absolute = FALSE,
153 $fragment = NULL,
154 $htmlize = TRUE,
155 $frontend = FALSE,
156 $forceBackend = FALSE
157 ) {
158 $config = CRM_Core_Config::singleton();
159 $script = 'index.php';
160
161 $path = CRM_Utils_String::stripPathChars($path);
162
163 if (isset($fragment)) {
164 $fragment = '#' . $fragment;
165 }
166
167 if (!isset($config->useFrameworkRelativeBase)) {
168 $base = parse_url($config->userFrameworkBaseURL);
169 $config->useFrameworkRelativeBase = $base['path'];
170 }
171 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
172
173 $separator = $htmlize ? '&amp;' : '&';
174
175 if (!$config->cleanURL) {
176 if (isset($path)) {
177 if (isset($query)) {
178 return $base . $script . '?q=' . $path . $separator . $query . $fragment;
179 }
180 else {
181 return $base . $script . '?q=' . $path . $fragment;
182 }
183 }
184 else {
185 if (isset($query)) {
186 return $base . $script . '?' . $query . $fragment;
187 }
188 else {
189 return $base . $fragment;
190 }
191 }
192 }
193 else {
194 if (isset($path)) {
195 if (isset($query)) {
196 return $base . $path . '?' . $query . $fragment;
197 }
198 else {
199 return $base . $path . $fragment;
200 }
201 }
202 else {
203 if (isset($query)) {
204 return $base . $script . '?' . $query . $fragment;
205 }
206 else {
207 return $base . $fragment;
208 }
209 }
210 }
211 }
212
213 /**
214 * @inheritDoc
215 */
216 public function getUserIDFromUserObject($user) {
217 return !empty($user->uid) ? $user->uid : NULL;
218 }
219
220 /**
221 * @inheritDoc
222 */
223 public function setMessage($message) {
224 drupal_set_message($message);
225 }
226
227 /**
228 * @inheritDoc
229 */
230 public function getUniqueIdentifierFromUserObject($user) {
231 return empty($user->mail) ? NULL : $user->mail;
232 }
233
234 /**
235 * @inheritDoc
236 */
237 public function getLoggedInUniqueIdentifier() {
238 global $user;
239 return $this->getUniqueIdentifierFromUserObject($user);
240 }
241
242 /**
243 * @inheritDoc
244 */
245 public function permissionDenied() {
246 drupal_access_denied();
247 }
248
249 /**
250 * @inheritDoc
251 */
252 public function getUserRecordUrl($contactID) {
253 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
254 if (CRM_Core_Session::singleton()
255 ->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(array(
256 'cms:administer users',
257 'cms:view user account',
258 ))
259 ) {
260 return CRM_Utils_System::url('user/' . $uid);
261 };
262 }
263
264 /**
265 * @inheritDoc
266 */
267 public function checkPermissionAddUser() {
268 return CRM_Core_Permission::check('administer users');
269 }
270
271 /**
272 * @inheritDoc
273 */
274 public function logger($message) {
275 if (CRM_Core_Config::singleton()->userFrameworkLogging && function_exists('watchdog')) {
276 watchdog('civicrm', '%message', array('%message' => $message), NULL, WATCHDOG_DEBUG);
277 }
278 }
279
280 /**
281 * @inheritDoc
282 */
283 public function clearResourceCache() {
284 _drupal_flush_css_js();
285 }
286
287 /**
288 * Append Drupal js to coreResourcesList.
289 */
290 public function appendCoreResources(&$list) {
291 $list[] = 'js/crm.drupal.js';
292 }
293
294 /**
295 * @inheritDoc
296 */
297 public function flush() {
298 drupal_flush_all_caches();
299 }
300
301 /**
302 * @inheritDoc
303 */
304 public function getModules() {
305 $result = array();
306 $q = db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND schema_version <> -1');
307 foreach ($q as $row) {
308 $result[] = new CRM_Core_Module('drupal.' . $row->name, ($row->status == 1) ? TRUE : FALSE);
309 }
310 return $result;
311 }
312
313 /**
314 * Find any users/roles/security-principals with the given permission
315 * and replace it with one or more permissions.
316 *
317 * @param string $oldPerm
318 * @param array $newPerms
319 * Array, strings.
320 *
321 * @return void
322 */
323 public function replacePermission($oldPerm, $newPerms) {
324 $roles = user_roles(FALSE, $oldPerm);
325 if (!empty($roles)) {
326 foreach (array_keys($roles) as $rid) {
327 user_role_revoke_permissions($rid, array($oldPerm));
328 user_role_grant_permissions($rid, $newPerms);
329 }
330 }
331 }
332
333 /**
334 * @inheritDoc
335 */
336 public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) {
337 if (empty($url)) {
338 return $url;
339 }
340
341 //CRM-7803 -from d7 onward.
342 $config = CRM_Core_Config::singleton();
343 if (function_exists('variable_get') &&
344 module_exists('locale') &&
345 function_exists('language_negotiation_get')
346 ) {
347 global $language;
348
349 //does user configuration allow language
350 //support from the URL (Path prefix or domain)
351 if (language_negotiation_get('language') == 'locale-url') {
352 $urlType = variable_get('locale_language_negotiation_url_part');
353
354 //url prefix
355 if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX) {
356 if (isset($language->prefix) && $language->prefix) {
357 if ($addLanguagePart) {
358 $url .= $language->prefix . '/';
359 }
360 if ($removeLanguagePart) {
361 $url = str_replace("/{$language->prefix}/", '/', $url);
362 }
363 }
364 }
365 //domain
366 if ($urlType == LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN) {
367 if (isset($language->domain) && $language->domain) {
368 if ($addLanguagePart) {
369 $url = (CRM_Utils_System::isSSL() ? 'https' : 'http') . '://' . $language->domain . base_path();
370 }
371 if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) {
372 $url = str_replace('\\', '/', $url);
373 $parseUrl = parse_url($url);
374
375 //kinda hackish but not sure how to do it right
376 //hope http_build_url() will help at some point.
377 if (is_array($parseUrl) && !empty($parseUrl)) {
378 $urlParts = explode('/', $url);
379 $hostKey = array_search($parseUrl['host'], $urlParts);
380 $ufUrlParts = parse_url(CIVICRM_UF_BASEURL);
381 $urlParts[$hostKey] = $ufUrlParts['host'];
382 $url = implode('/', $urlParts);
383 }
384 }
385 }
386 }
387 }
388 }
389 return $url;
390 }
391
392 /**
393 * @inheritDoc
394 */
395 public function getVersion() {
396 return defined('VERSION') ? VERSION : 'Unknown';
397 }
398
399 /**
400 * @inheritDoc
401 */
402 public function updateCategories() {
403 // copied this from profile.module. Seems a bit inefficient, but i don't know a better way
404 cache_clear_all();
405 menu_rebuild();
406 }
407
408 /**
409 * @inheritDoc
410 */
411 public function getUFLocale() {
412 // return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale
413 // (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx
414 // sometimes for CLI based on order called, this might not be set and/or empty
415 global $language;
416
417 if (empty($language)) {
418 return NULL;
419 }
420
421 if ($language->language == 'zh-hans') {
422 return 'zh_CN';
423 }
424
425 if ($language->language == 'zh-hant') {
426 return 'zh_TW';
427 }
428
429 if (preg_match('/^.._..$/', $language->language)) {
430 return $language->language;
431 }
432
433 return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->language, 0, 2));
434 }
435
436 /**
437 * Perform any post login activities required by the UF -
438 * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp,
439 * calls hook_user op 'login' and generates a new session.
440 *
441 * @param array $params
442 *
443 * FIXME: Document values accepted/required by $params
444 */
445 public function userLoginFinalize($params = array()) {
446 user_login_finalize($params);
447 }
448
449 /**
450 * @inheritDoc
451 */
452 public function getLoginDestination(&$form) {
453 $args = NULL;
454
455 $id = $form->get('id');
456 if ($id) {
457 $args .= "&id=$id";
458 }
459 else {
460 $gid = $form->get('gid');
461 if ($gid) {
462 $args .= "&gid=$gid";
463 }
464 else {
465 // Setup Personal Campaign Page link uses pageId
466 $pageId = $form->get('pageId');
467 if ($pageId) {
468 $component = $form->get('component');
469 $args .= "&pageId=$pageId&component=$component&action=add";
470 }
471 }
472 }
473
474 $destination = NULL;
475 if ($args) {
476 // append destination so user is returned to form they came from after login
477 $destination = CRM_Utils_System::currentPath() . '?reset=1' . $args;
478 }
479 return $destination;
480 }
481
482 /**
483 * Fixme: Why are we overriding the parent function? Seems inconsistent.
484 * This version supplies slightly different params to $this->url (not absolute and html encoded) but why?
485 */
486 public function postURL($action) {
487 if (!empty($action)) {
488 return $action;
489 }
490 return $this->url($_GET['q']);
491 }
492
493 /**
494 * Get an array of user details for a contact, containing at minimum the user ID & name.
495 *
496 * @param int $contactID
497 *
498 * @return array
499 * CMS user details including
500 * - id
501 * - name (ie the system user name.
502 */
503 public function getUser($contactID) {
504 $userDetails = parent::getUser($contactID);
505 $user = $this->getUserObject($userDetails['id']);
506 $userDetails['name'] = $user->name;
507 $userDetails['email'] = $user->mail;
508 return $userDetails;
509 }
510
511 /**
512 * Load the user object.
513 *
514 * Note this function still works in drupal 6, 7 & 8 but is deprecated in Drupal 8.
515 *
516 * @param $userID
517 *
518 * @return object
519 */
520 public function getUserObject($userID) {
521 return user_load($userID);
522 }
523
524 }