commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Utils / System.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 * System wide utilities.
38 *
39 */
40 class CRM_Utils_System {
41
42 static $_callbacks = NULL;
43
44 /**
45 * @var string Page title
46 */
47 static $title = '';
48
49 /**
50 * Compose a new URL string from the current URL string.
51 *
52 * Used by all the framework components, specifically,
53 * pager, sort and qfc
54 *
55 * @param string $urlVar
56 * The url variable being considered (i.e. crmPageID, crmSortID etc).
57 * @param bool $includeReset
58 * (optional) Whether to include the reset GET string (if present).
59 * @param bool $includeForce
60 * (optional) Whether to include the force GET string (if present).
61 * @param string $path
62 * (optional) The path to use for the new url.
63 * @param bool|string $absolute
64 * (optional) Whether to return an absolute URL.
65 *
66 * @return string
67 * The URL fragment.
68 */
69 public static function makeURL($urlVar, $includeReset = FALSE, $includeForce = TRUE, $path = NULL, $absolute = FALSE) {
70 if (empty($path)) {
71 $config = CRM_Core_Config::singleton();
72 $path = CRM_Utils_Array::value($config->userFrameworkURLVar, $_GET);
73 if (empty($path)) {
74 return '';
75 }
76 }
77
78 return
79 self::url(
80 $path,
81 CRM_Utils_System::getLinksUrl($urlVar, $includeReset, $includeForce),
82 $absolute
83 );
84 }
85
86 /**
87 * Get the query string and clean it up.
88 *
89 * Strips some variables that should not be propagated, specifically variables
90 * like 'reset'. Also strips any side-affect actions (e.g. export).
91 *
92 * This function is copied mostly verbatim from Pager.php (_getLinksUrl)
93 *
94 * @param string $urlVar
95 * The URL variable being considered (e.g. crmPageID, crmSortID etc).
96 * @param bool $includeReset
97 * (optional) By default this is FALSE, meaning that the reset parameter
98 * is skipped. Set to TRUE to leave the reset parameter as-is.
99 * @param bool $includeForce
100 * (optional)
101 * @param bool $skipUFVar
102 * (optional)
103 *
104 * @return string
105 */
106 public static function getLinksUrl($urlVar, $includeReset = FALSE, $includeForce = TRUE, $skipUFVar = TRUE) {
107 // Sort out query string to prevent messy urls
108 $querystring = array();
109 $qs = array();
110 $arrays = array();
111
112 if (!empty($_SERVER['QUERY_STRING'])) {
113 $qs = explode('&', str_replace('&amp;', '&', $_SERVER['QUERY_STRING']));
114 for ($i = 0, $cnt = count($qs); $i < $cnt; $i++) {
115 // check first if exist a pair
116 if (strstr($qs[$i], '=') !== FALSE) {
117 list($name, $value) = explode('=', $qs[$i]);
118 if ($name != $urlVar) {
119 $name = rawurldecode($name);
120 //check for arrays in parameters: site.php?foo[]=1&foo[]=2&foo[]=3
121 if ((strpos($name, '[') !== FALSE) &&
122 (strpos($name, ']') !== FALSE)
123 ) {
124 $arrays[] = $qs[$i];
125 }
126 else {
127 $qs[$name] = $value;
128 }
129 }
130 }
131 else {
132 $qs[$qs[$i]] = '';
133 }
134 unset($qs[$i]);
135 }
136 }
137
138 if ($includeForce) {
139 $qs['force'] = 1;
140 }
141
142 // Ok this is a big assumption but usually works
143 // If we are in snippet mode, retain the 'section' param, if not, get rid
144 // of it.
145 if (!empty($qs['snippet'])) {
146 unset($qs['snippet']);
147 }
148 else {
149 unset($qs['section']);
150 }
151
152 if ($skipUFVar) {
153 $config = CRM_Core_Config::singleton();
154 unset($qs[$config->userFrameworkURLVar]);
155 }
156
157 foreach ($qs as $name => $value) {
158 if ($name != 'reset' || $includeReset) {
159 $querystring[] = $name . '=' . $value;
160 }
161 }
162
163 $querystring = array_merge($querystring, array_unique($arrays));
164
165 $url = implode('&', $querystring);
166 if ($urlVar) {
167 $url .= (!empty($querystring) ? '&' : '') . $urlVar . '=';
168 }
169
170 return $url;
171 }
172
173 /**
174 * If we are using a theming system, invoke theme, else just print the
175 * content.
176 *
177 * @param string $content
178 * The content that will be themed.
179 * @param bool $print
180 * (optional) Are we displaying to the screen or bypassing theming?
181 * @param bool $maintenance
182 * (optional) For maintenance mode.
183 *
184 * @return string
185 */
186 public static function theme(
187 &$content,
188 $print = FALSE,
189 $maintenance = FALSE
190 ) {
191 $config = &CRM_Core_Config::singleton();
192 return $config->userSystem->theme($content, $print, $maintenance);
193 }
194
195 /**
196 * Generate a query string if input is an array.
197 *
198 * @param array|string $query
199 * @return string
200 */
201 public static function makeQueryString($query) {
202 if (is_array($query)) {
203 $buf = '';
204 foreach ($query as $key => $value) {
205 $buf .= ($buf ? '&' : '') . urlencode($key) . '=' . urlencode($value);
206 }
207 $query = $buf;
208 }
209 return $query;
210 }
211
212 /**
213 * Generate an internal CiviCRM URL.
214 *
215 * @param string $path
216 * The path being linked to, such as "civicrm/add".
217 * @param array|string $query
218 * A query string to append to the link, or an array of key-value pairs.
219 * @param bool $absolute
220 * Whether to force the output to be an absolute link (beginning with a
221 * URI-scheme such as 'http:'). Useful for links that will be displayed
222 * outside the site, such as in an RSS feed.
223 * @param string $fragment
224 * A fragment identifier (named anchor) to append to the link.
225 *
226 * @param bool $htmlize
227 * @param bool $frontend
228 * @param bool $forceBackend
229 * @return string
230 * An HTML string containing a link to the given path.
231 */
232 public static function url(
233 $path = NULL,
234 $query = NULL,
235 $absolute = FALSE,
236 $fragment = NULL,
237 $htmlize = TRUE,
238 $frontend = FALSE,
239 $forceBackend = FALSE
240 ) {
241 $query = self::makeQueryString($query);
242
243 // we have a valid query and it has not yet been transformed
244 if ($htmlize && !empty($query) && strpos($query, '&amp;') === FALSE) {
245 $query = htmlentities($query);
246 }
247
248 $config = CRM_Core_Config::singleton();
249 return $config->userSystem->url($path, $query, $absolute, $fragment, $htmlize, $frontend, $forceBackend);
250 }
251
252 /**
253 * @param $text
254 * @param null $path
255 * @param null $query
256 * @param bool $absolute
257 * @param null $fragment
258 * @param bool $htmlize
259 * @param bool $frontend
260 * @param bool $forceBackend
261 *
262 * @return string
263 */
264 public static function href(
265 $text, $path = NULL, $query = NULL, $absolute = TRUE,
266 $fragment = NULL, $htmlize = TRUE, $frontend = FALSE, $forceBackend = FALSE
267 ) {
268 $url = self::url($path, $query, $absolute, $fragment, $htmlize, $frontend, $forceBackend);
269 return "<a href=\"$url\">$text</a>";
270 }
271
272 /**
273 * @return mixed
274 */
275 public static function permissionDenied() {
276 $config = CRM_Core_Config::singleton();
277 return $config->userSystem->permissionDenied();
278 }
279
280 /**
281 * @return mixed
282 */
283 public static function logout() {
284 $config = CRM_Core_Config::singleton();
285 return $config->userSystem->logout();
286 }
287
288 /**
289 * this is a very drupal specific function for now.
290 */
291 public static function updateCategories() {
292 $config = CRM_Core_Config::singleton();
293 if ($config->userSystem->is_drupal) {
294 $config->userSystem->updateCategories();
295 }
296 }
297
298 /**
299 * What menu path are we currently on. Called for the primary tpl
300 *
301 * @return string
302 * the current menu path
303 */
304 public static function currentPath() {
305 $config = CRM_Core_Config::singleton();
306 return trim(CRM_Utils_Array::value($config->userFrameworkURLVar, $_GET), '/');
307 }
308
309 /**
310 * called from a template to compose a url.
311 *
312 * @param array $params
313 * List of parameters.
314 *
315 * @return string
316 * url
317 */
318 public static function crmURL($params) {
319 $p = CRM_Utils_Array::value('p', $params);
320 if (!isset($p)) {
321 $p = self::currentPath();
322 }
323
324 return self::url(
325 $p,
326 CRM_Utils_Array::value('q', $params),
327 CRM_Utils_Array::value('a', $params, FALSE),
328 CRM_Utils_Array::value('f', $params),
329 CRM_Utils_Array::value('h', $params, TRUE),
330 CRM_Utils_Array::value('fe', $params, FALSE),
331 CRM_Utils_Array::value('fb', $params, FALSE)
332 );
333 }
334
335 /**
336 * Sets the title of the page.
337 *
338 * @param string $title
339 * @param string $pageTitle
340 */
341 public static function setTitle($title, $pageTitle = NULL) {
342 self::$title = $title;
343 $config = CRM_Core_Config::singleton();
344 return $config->userSystem->setTitle($title, $pageTitle);
345 }
346
347 /**
348 * Figures and sets the userContext.
349 *
350 * Uses the referer if valid else uses the default.
351 *
352 * @param array $names
353 * Refererer should match any str in this array.
354 * @param string $default
355 * (optional) The default userContext if no match found.
356 */
357 public static function setUserContext($names, $default = NULL) {
358 $url = $default;
359
360 $session = CRM_Core_Session::singleton();
361 $referer = CRM_Utils_Array::value('HTTP_REFERER', $_SERVER);
362
363 if ($referer && !empty($names)) {
364 foreach ($names as $name) {
365 if (strstr($referer, $name)) {
366 $url = $referer;
367 break;
368 }
369 }
370 }
371
372 if ($url) {
373 $session->pushUserContext($url);
374 }
375 }
376
377 /**
378 * Gets a class name for an object.
379 *
380 * @param object $object
381 * Object whose class name is needed.
382 *
383 * @return string
384 * The class name of the object.
385 */
386 public static function getClassName($object) {
387 return get_class($object);
388 }
389
390 /**
391 * Redirect to another URL.
392 *
393 * @param string $url
394 * The URL to provide to the browser via the Location header.
395 */
396 public static function redirect($url = NULL) {
397 if (!$url) {
398 $url = self::url('civicrm/dashboard', 'reset=1');
399 }
400
401 // replace the &amp; characters with &
402 // this is kinda hackish but not sure how to do it right
403 $url = str_replace('&amp;', '&', $url);
404
405 // If we are in a json context, respond appropriately
406 if (CRM_Utils_Array::value('snippet', $_GET) === 'json') {
407 CRM_Core_Page_AJAX::returnJsonResponse(array(
408 'status' => 'redirect',
409 'userContext' => $url,
410 ));
411 }
412
413 header('Location: ' . $url);
414 self::civiExit();
415 }
416
417 /**
418 * Redirect to another URL using JavaScript.
419 *
420 * Use an html based file with javascript embedded to redirect to another url
421 * This prevent the too many redirect errors emitted by various browsers
422 *
423 * @param string $url
424 * (optional) The destination URL.
425 * @param string $title
426 * (optional) The page title to use for the redirect page.
427 * @param string $message
428 * (optional) The message to provide in the body of the redirect page.
429 */
430 public static function jsRedirect(
431 $url = NULL,
432 $title = NULL,
433 $message = NULL
434 ) {
435 if (!$url) {
436 $url = self::url('civicrm/dashboard', 'reset=1');
437 }
438
439 if (!$title) {
440 $title = ts('CiviCRM task in progress');
441 }
442
443 if (!$message) {
444 $message = ts('A long running CiviCRM task is currently in progress. This message will be refreshed till the task is completed');
445 }
446
447 // replace the &amp; characters with &
448 // this is kinda hackish but not sure how to do it right
449 $url = str_replace('&amp;', '&', $url);
450
451 $template = CRM_Core_Smarty::singleton();
452 $template->assign('redirectURL', $url);
453 $template->assign('title', $title);
454 $template->assign('message', $message);
455
456 $html = $template->fetch('CRM/common/redirectJS.tpl');
457
458 echo $html;
459
460 self::civiExit();
461 }
462
463 /**
464 * Append an additional breadcrumb tag to the existing breadcrumbs.
465 *
466 * @param $breadCrumbs
467 */
468 public static function appendBreadCrumb($breadCrumbs) {
469 $config = CRM_Core_Config::singleton();
470 return $config->userSystem->appendBreadCrumb($breadCrumbs);
471 }
472
473 /**
474 * Reset an additional breadcrumb tag to the existing breadcrumb.
475 */
476 public static function resetBreadCrumb() {
477 $config = CRM_Core_Config::singleton();
478 return $config->userSystem->resetBreadCrumb();
479 }
480
481 /**
482 * Append a string to the head of the HTML file.
483 *
484 * @param string $bc
485 */
486 public static function addHTMLHead($bc) {
487 $config = CRM_Core_Config::singleton();
488 return $config->userSystem->addHTMLHead($bc);
489 }
490
491 /**
492 * Determine the post URL for a form.
493 *
494 * @param $action
495 * The default action if one is pre-specified.
496 *
497 * @return string
498 * The URL to post the form.
499 */
500 public static function postURL($action) {
501 $config = CRM_Core_Config::singleton();
502 return $config->userSystem->postURL($action);
503 }
504
505 /**
506 * Rewrite various system URLs to https.
507 */
508 public static function mapConfigToSSL() {
509 $config = CRM_Core_Config::singleton();
510 $config->userFrameworkResourceURL = str_replace('http://', 'https://', $config->userFrameworkResourceURL);
511 $config->resourceBase = $config->userFrameworkResourceURL;
512
513 if (!empty($config->extensionsURL)) {
514 $config->extensionsURL = str_replace('http://', 'https://', $config->extensionsURL);
515 }
516
517 return $config->userSystem->mapConfigToSSL();
518 }
519
520 /**
521 * Get the base URL of the system.
522 *
523 * @return string
524 */
525 public static function baseURL() {
526 $config = CRM_Core_Config::singleton();
527 return $config->userFrameworkBaseURL;
528 }
529
530 /**
531 */
532 public static function authenticateAbort($message, $abort) {
533 if ($abort) {
534 echo $message;
535 self::civiExit(0);
536 }
537 else {
538 return FALSE;
539 }
540 }
541
542 /**
543 * @param bool $abort
544 * (optional) Whether to exit; defaults to true.
545 *
546 * @return bool
547 */
548 public static function authenticateKey($abort = TRUE) {
549 // also make sure the key is sent and is valid
550 $key = trim(CRM_Utils_Array::value('key', $_REQUEST));
551
552 $docAdd = "More info at:" . CRM_Utils_System::docURL2("Managing Scheduled Jobs", TRUE, NULL, NULL, NULL, "wiki");
553
554 if (!$key) {
555 return self::authenticateAbort(
556 "ERROR: You need to send a valid key to execute this file. " . $docAdd . "\n",
557 $abort
558 );
559 }
560
561 $siteKey = defined('CIVICRM_SITE_KEY') ? CIVICRM_SITE_KEY : NULL;
562
563 if (!$siteKey || empty($siteKey)) {
564 return self::authenticateAbort(
565 "ERROR: You need to set a valid site key in civicrm.settings.php. " . $docAdd . "\n",
566 $abort
567 );
568 }
569
570 if (strlen($siteKey) < 8) {
571 return self::authenticateAbort(
572 "ERROR: Site key needs to be greater than 7 characters in civicrm.settings.php. " . $docAdd . "\n",
573 $abort
574 );
575 }
576
577 if ($key !== $siteKey) {
578 return self::authenticateAbort(
579 "ERROR: Invalid key value sent. " . $docAdd . "\n",
580 $abort
581 );
582 }
583
584 return TRUE;
585 }
586
587 /**
588 * @param bool $abort
589 * @param null $name
590 * @param null $pass
591 * @param bool $storeInSession
592 * @param bool $loadCMSBootstrap
593 * @param bool $requireKey
594 *
595 * @return bool
596 */
597 public static function authenticateScript($abort = TRUE, $name = NULL, $pass = NULL, $storeInSession = TRUE, $loadCMSBootstrap = TRUE, $requireKey = TRUE) {
598 // auth to make sure the user has a login/password to do a shell operation
599 // later on we'll link this to acl's
600 if (!$name) {
601 $name = trim(CRM_Utils_Array::value('name', $_REQUEST));
602 $pass = trim(CRM_Utils_Array::value('pass', $_REQUEST));
603 }
604
605 // its ok to have an empty password
606 if (!$name) {
607 return self::authenticateAbort(
608 "ERROR: You need to send a valid user name and password to execute this file\n",
609 $abort
610 );
611 }
612
613 if ($requireKey && !self::authenticateKey($abort)) {
614 return FALSE;
615 }
616
617 $result = CRM_Utils_System::authenticate($name, $pass, $loadCMSBootstrap);
618 if (!$result) {
619 return self::authenticateAbort(
620 "ERROR: Invalid username and/or password\n",
621 $abort
622 );
623 }
624 elseif ($storeInSession) {
625 // lets store contact id and user id in session
626 list($userID, $ufID, $randomNumber) = $result;
627 if ($userID && $ufID) {
628 $config = CRM_Core_Config::singleton();
629 $config->userSystem->setUserSession(array($userID, $ufID));
630 }
631 else {
632 return self::authenticateAbort(
633 "ERROR: Unexpected error, could not match userID and contactID",
634 $abort
635 );
636 }
637 }
638
639 return $result;
640 }
641
642 /**
643 * Authenticate the user against the uf db.
644 *
645 * In case of succesful authentication, returns an array consisting of
646 * (contactID, ufID, unique string). Returns FALSE if authentication is
647 * unsuccessful.
648 *
649 * @param string $name
650 * The username.
651 * @param string $password
652 * The password.
653 * @param bool $loadCMSBootstrap
654 * @param $realPath
655 *
656 * @return false|array
657 */
658 public static function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
659 $config = CRM_Core_Config::singleton();
660
661 /* Before we do any loading, let's start the session and write to it.
662 * We typically call authenticate only when we need to bootstrap the CMS
663 * directly via Civi and hence bypass the normal CMS auth and bootstrap
664 * process typically done in CLI and cron scripts. See: CRM-12648
665 *
666 * Q: Can we move this to the userSystem class so that it can be tuned
667 * per-CMS? For example, when dealing with UnitTests UF, there's no
668 * userFrameworkDSN.
669 */
670 $session = CRM_Core_Session::singleton();
671 $session->set('civicrmInitSession', TRUE);
672
673 if ($config->userFrameworkDSN) {
674 $dbDrupal = DB::connect($config->userFrameworkDSN);
675 }
676 return $config->userSystem->authenticate($name, $password, $loadCMSBootstrap, $realPath);
677 }
678
679 /**
680 * Set a message in the UF to display to a user.
681 *
682 * @param string $message
683 * The message to set.
684 */
685 public static function setUFMessage($message) {
686 $config = CRM_Core_Config::singleton();
687 return $config->userSystem->setMessage($message);
688 }
689
690
691 /**
692 * Determine whether a value is null-ish.
693 *
694 * @param $value
695 * The value to check for null.
696 * @return bool
697 */
698 public static function isNull($value) {
699 // FIXME: remove $value = 'null' string test when we upgrade our DAO code to handle passing null in a better way.
700 if (!isset($value) || $value === NULL || $value === '' || $value === 'null') {
701 return TRUE;
702 }
703 if (is_array($value)) {
704 // @todo Reuse of the $value variable = asking for trouble.
705 foreach ($value as $key => $value) {
706 if (!self::isNull($value)) {
707 return FALSE;
708 }
709 }
710 return TRUE;
711 }
712 return FALSE;
713 }
714
715 /**
716 * Obscure all but the last few digits of a credit card number.
717 *
718 * @param string $number
719 * The credit card number to obscure.
720 * @param int $keep
721 * (optional) The number of digits to preserve unmodified.
722 * @return string
723 * The obscured credit card number.
724 */
725 public static function mungeCreditCard($number, $keep = 4) {
726 $number = trim($number);
727 if (empty($number)) {
728 return NULL;
729 }
730 $replace = str_repeat('*', strlen($number) - $keep);
731 return substr_replace($number, $replace, 0, -$keep);
732 }
733
734 /**
735 * Determine which PHP modules are loaded.
736 *
737 * @return array
738 */
739 public static function parsePHPModules() {
740 ob_start();
741 phpinfo(INFO_MODULES);
742 $s = ob_get_contents();
743 ob_end_clean();
744
745 $s = strip_tags($s, '<h2><th><td>');
746 $s = preg_replace('/<th[^>]*>([^<]+)<\/th>/', "<info>\\1</info>", $s);
747 $s = preg_replace('/<td[^>]*>([^<]+)<\/td>/', "<info>\\1</info>", $s);
748 $vTmp = preg_split('/(<h2>[^<]+<\/h2>)/', $s, -1, PREG_SPLIT_DELIM_CAPTURE);
749 $vModules = array();
750 for ($i = 1; $i < count($vTmp); $i++) {
751 if (preg_match('/<h2>([^<]+)<\/h2>/', $vTmp[$i], $vMat)) {
752 $vName = trim($vMat[1]);
753 $vTmp2 = explode("\n", $vTmp[$i + 1]);
754 foreach ($vTmp2 as $vOne) {
755 $vPat = '<info>([^<]+)<\/info>';
756 $vPat3 = "/$vPat\s*$vPat\s*$vPat/";
757 $vPat2 = "/$vPat\s*$vPat/";
758 // 3cols
759 if (preg_match($vPat3, $vOne, $vMat)) {
760 $vModules[$vName][trim($vMat[1])] = array(trim($vMat[2]), trim($vMat[3]));
761 // 2cols
762 }
763 elseif (preg_match($vPat2, $vOne, $vMat)) {
764 $vModules[$vName][trim($vMat[1])] = trim($vMat[2]);
765 }
766 }
767 }
768 }
769 return $vModules;
770 }
771
772 /**
773 * Get a setting from a loaded PHP module.
774 */
775 public static function getModuleSetting($pModuleName, $pSetting) {
776 $vModules = self::parsePHPModules();
777 return $vModules[$pModuleName][$pSetting];
778 }
779
780 /**
781 * @param $title
782 * (optional)
783 *
784 * @return mixed|string
785 */
786 public static function memory($title = NULL) {
787 static $pid = NULL;
788 if (!$pid) {
789 $pid = posix_getpid();
790 }
791
792 $memory = str_replace("\n", '', shell_exec("ps -p" . $pid . " -o rss="));
793 $memory .= ", " . time();
794 if ($title) {
795 CRM_Core_Error::debug_var($title, $memory);
796 }
797 return $memory;
798 }
799
800 /**
801 * @param string $name
802 * @param string $mimeType
803 * @param $buffer
804 * @param string $ext
805 * @param bool $output
806 * @param string $disposition
807 */
808 public static function download(
809 $name, $mimeType, &$buffer,
810 $ext = NULL,
811 $output = TRUE,
812 $disposition = 'attachment'
813 ) {
814 $now = gmdate('D, d M Y H:i:s') . ' GMT';
815
816 header('Content-Type: ' . $mimeType);
817 header('Expires: ' . $now);
818
819 // lem9 & loic1: IE need specific headers
820 $isIE = strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE');
821 if ($ext) {
822 $fileString = "filename=\"{$name}.{$ext}\"";
823 }
824 else {
825 $fileString = "filename=\"{$name}\"";
826 }
827 if ($isIE) {
828 header("Content-Disposition: inline; $fileString");
829 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
830 header('Pragma: public');
831 }
832 else {
833 header("Content-Disposition: $disposition; $fileString");
834 header('Pragma: no-cache');
835 }
836
837 if ($output) {
838 print $buffer;
839 self::civiExit();
840 }
841 }
842
843 /**
844 * Gather and print (and possibly log) amount of used memory.
845 *
846 * @param string $title
847 * @param bool $log
848 * (optional) Whether to log the memory usage information.
849 */
850 public static function xMemory($title = NULL, $log = FALSE) {
851 $mem = (float ) xdebug_memory_usage() / (float ) (1024);
852 $mem = number_format($mem, 5) . ", " . time();
853 if ($log) {
854 echo "<p>$title: $mem<p>";
855 flush();
856 CRM_Core_Error::debug_var($title, $mem);
857 }
858 else {
859 echo "<p>$title: $mem<p>";
860 flush();
861 }
862 }
863
864 /**
865 * Take a URL (or partial URL) and make it better.
866 *
867 * Currently, URLs pass straight through unchanged unless they are "seriously
868 * malformed" (see http://us2.php.net/parse_url).
869 *
870 * @param string $url
871 * The URL to operate on.
872 * @return string
873 * The fixed URL.
874 */
875 public static function fixURL($url) {
876 $components = parse_url($url);
877
878 if (!$components) {
879 return NULL;
880 }
881
882 // at some point we'll add code here to make sure the url is not
883 // something that will mess up up, so we need to clean it up here
884 return $url;
885 }
886
887 /**
888 * Make sure a callback is valid in the current context.
889 *
890 * @param string $callback
891 * Name of the function to check.
892 *
893 * @return bool
894 */
895 public static function validCallback($callback) {
896 if (self::$_callbacks === NULL) {
897 self::$_callbacks = array();
898 }
899
900 if (!array_key_exists($callback, self::$_callbacks)) {
901 if (strpos($callback, '::') !== FALSE) {
902 list($className, $methodName) = explode('::', $callback);
903 $fileName = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
904 // ignore errors if any
905 @include_once $fileName;
906 if (!class_exists($className)) {
907 self::$_callbacks[$callback] = FALSE;
908 }
909 else {
910 // instantiate the class
911 $object = new $className();
912 if (!method_exists($object, $methodName)) {
913 self::$_callbacks[$callback] = FALSE;
914 }
915 else {
916 self::$_callbacks[$callback] = TRUE;
917 }
918 }
919 }
920 else {
921 self::$_callbacks[$callback] = function_exists($callback);
922 }
923 }
924 return self::$_callbacks[$callback];
925 }
926
927 /**
928 * Like PHP's built-in explode(), but always return an array of $limit items.
929 *
930 * This serves as a wrapper to the PHP explode() function. In the event that
931 * PHP's explode() returns an array with fewer than $limit elements, pad
932 * the end of the array with NULLs.
933 *
934 * @param string $separator
935 * @param string $string
936 * @param int $limit
937 * @return string[]
938 */
939 public static function explode($separator, $string, $limit) {
940 $result = explode($separator, $string, $limit);
941 for ($i = count($result); $i < $limit; $i++) {
942 $result[$i] = NULL;
943 }
944 return $result;
945 }
946
947 /**
948 * @param string $url
949 * The URL to check.
950 * @param bool $addCookie
951 * (optional)
952 *
953 * @return mixed
954 */
955 public static function checkURL($url, $addCookie = FALSE) {
956 // make a GET request to $url
957 $ch = curl_init($url);
958 if ($addCookie) {
959 curl_setopt($ch, CURLOPT_COOKIE, http_build_query($_COOKIE));
960 }
961 // it's quite alright to use a self-signed cert
962 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
963
964 // lets capture the return stuff rather than echo
965 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
966
967 // CRM-13227, CRM-14744: only return the SSL error status
968 return (curl_exec($ch) !== FALSE);
969 }
970
971 /**
972 * Assert that we are running on a particular PHP version.
973 *
974 * @param int $ver
975 * The major version of PHP that is required.
976 * @param bool $abort
977 * (optional) Whether to fatally abort if the version requirement is not
978 * met. Defaults to TRUE.
979 * @return bool
980 * Returns TRUE if the requirement is met, FALSE if the requirement is not
981 * met and we're not aborting due to the failed requirement. If $abort is
982 * TRUE and the requirement fails, this function does not return.
983 */
984 public static function checkPHPVersion($ver = 5, $abort = TRUE) {
985 $phpVersion = substr(PHP_VERSION, 0, 1);
986 if ($phpVersion >= $ver) {
987 return TRUE;
988 }
989
990 if ($abort) {
991 CRM_Core_Error::fatal(ts('This feature requires PHP Version %1 or greater',
992 array(1 => $ver)
993 ));
994 }
995 return FALSE;
996 }
997
998 /**
999 * @param $string
1000 * @param bool $encode
1001 *
1002 * @return string
1003 */
1004 public static function formatWikiURL($string, $encode = FALSE) {
1005 $items = explode(' ', trim($string), 2);
1006 if (count($items) == 2) {
1007 $title = $items[1];
1008 }
1009 else {
1010 $title = $items[0];
1011 }
1012
1013 // fix for CRM-4044
1014 $url = $encode ? self::urlEncode($items[0]) : $items[0];
1015 return "<a href=\"$url\">$title</a>";
1016 }
1017
1018 /**
1019 * @param string $url
1020 *
1021 * @return null|string
1022 */
1023 public static function urlEncode($url) {
1024 $items = parse_url($url);
1025 if ($items === FALSE) {
1026 return NULL;
1027 }
1028
1029 if (empty($items['query'])) {
1030 return $url;
1031 }
1032
1033 $items['query'] = urlencode($items['query']);
1034
1035 $url = $items['scheme'] . '://';
1036 if (!empty($items['user'])) {
1037 $url .= "{$items['user']}:{$items['pass']}@";
1038 }
1039
1040 $url .= $items['host'];
1041 if (!empty($items['port'])) {
1042 $url .= ":{$items['port']}";
1043 }
1044
1045 $url .= "{$items['path']}?{$items['query']}";
1046 if (!empty($items['fragment'])) {
1047 $url .= "#{$items['fragment']}";
1048 }
1049
1050 return $url;
1051 }
1052
1053 /**
1054 * Return the running civicrm version.
1055 *
1056 * @return string
1057 * civicrm version
1058 */
1059 public static function version() {
1060 static $version;
1061
1062 if (!$version) {
1063 $verFile = implode(DIRECTORY_SEPARATOR,
1064 array(dirname(__FILE__), '..', '..', 'civicrm-version.php')
1065 );
1066 if (file_exists($verFile)) {
1067 require_once $verFile;
1068 if (function_exists('civicrmVersion')) {
1069 $info = civicrmVersion();
1070 $version = $info['version'];
1071 }
1072 }
1073 else {
1074 // svn installs don't have version.txt by default. In that case version.xml should help -
1075 $verFile = implode(DIRECTORY_SEPARATOR,
1076 array(dirname(__FILE__), '..', '..', 'xml', 'version.xml')
1077 );
1078 if (file_exists($verFile)) {
1079 $str = file_get_contents($verFile);
1080 $xmlObj = simplexml_load_string($str);
1081 $version = (string) $xmlObj->version_no;
1082 }
1083 }
1084
1085 // pattern check
1086 if (!CRM_Utils_System::isVersionFormatValid($version)) {
1087 CRM_Core_Error::fatal('Unknown codebase version.');
1088 }
1089 }
1090
1091 return $version;
1092 }
1093
1094 /**
1095 * Determines whether a string is a valid CiviCRM version string.
1096 *
1097 * @param string $version
1098 * Version string to be checked.
1099 * @return bool
1100 */
1101 public static function isVersionFormatValid($version) {
1102 return preg_match("/^(\d{1,2}\.){2,3}(\d{1,2}|(alpha|beta)\d{1,2})(\.upgrade)?$/", $version);
1103 }
1104
1105 /**
1106 * Wraps or emulates PHP's getallheaders() function.
1107 */
1108 public static function getAllHeaders() {
1109 if (function_exists('getallheaders')) {
1110 return getallheaders();
1111 }
1112
1113 // emulate get all headers
1114 // http://www.php.net/manual/en/function.getallheaders.php#66335
1115 $headers = array();
1116 foreach ($_SERVER as $name => $value) {
1117 if (substr($name, 0, 5) == 'HTTP_') {
1118 $headers[str_replace(' ',
1119 '-',
1120 ucwords(strtolower(str_replace('_',
1121 ' ',
1122 substr($name, 5)
1123 )
1124 ))
1125 )] = $value;
1126 }
1127 }
1128 return $headers;
1129 }
1130
1131 /**
1132 */
1133 public static function getRequestHeaders() {
1134 if (function_exists('apache_request_headers')) {
1135 return apache_request_headers();
1136 }
1137 else {
1138 return $_SERVER;
1139 }
1140 }
1141
1142 /**
1143 * Determine whether this is an SSL request.
1144 *
1145 * Note that we inline this function in install/civicrm.php, so if you change
1146 * this function, please go and change the code in the install script as well.
1147 */
1148 public static function isSSL() {
1149 return
1150 (isset($_SERVER['HTTPS']) &&
1151 !empty($_SERVER['HTTPS']) &&
1152 strtolower($_SERVER['HTTPS']) != 'off') ? TRUE : FALSE;
1153 }
1154
1155 /**
1156 */
1157 public static function redirectToSSL($abort = FALSE) {
1158 $config = CRM_Core_Config::singleton();
1159 $req_headers = self::getRequestHeaders();
1160 // FIXME: Shouldn't the X-Forwarded-Proto check be part of CRM_Utils_System::isSSL()?
1161 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enableSSL') &&
1162 !self::isSSL() &&
1163 strtolower(CRM_Utils_Array::value('X_FORWARDED_PROTO', $req_headers)) != 'https'
1164 ) {
1165 // ensure that SSL is enabled on a civicrm url (for cookie reasons etc)
1166 $url = "https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
1167 if (!self::checkURL($url, TRUE)) {
1168 if ($abort) {
1169 CRM_Core_Error::fatal('HTTPS is not set up on this machine');
1170 }
1171 else {
1172 CRM_Core_Session::setStatus(ts('HTTPS is not set up on this machine'), ts('Warning'), 'alert');
1173 // admin should be the only one following this
1174 // since we dont want the user stuck in a bad place
1175 return;
1176 }
1177 }
1178 CRM_Utils_System::redirect($url);
1179 }
1180 }
1181
1182 /**
1183 * Get logged in user's IP address.
1184 *
1185 * Get IP address from HTTP REMOTE_ADDR header. If the CMS is Drupal then use
1186 * the Drupal function as this also handles reverse proxies (based on proper
1187 * configuration in settings.php)
1188 *
1189 * @param bool $strictIPV4
1190 * (optional) Whether to return only IPv4 addresses.
1191 *
1192 * @return string
1193 * IP address of logged in user.
1194 */
1195 public static function ipAddress($strictIPV4 = TRUE) {
1196 $address = CRM_Utils_Array::value('REMOTE_ADDR', $_SERVER);
1197
1198 $config = CRM_Core_Config::singleton();
1199 if ($config->userSystem->is_drupal && function_exists('ip_address')) {
1200 //drupal function handles the server being behind a proxy securely. We still have legacy ipn methods
1201 // that reach this point without bootstrapping hence the check that the fn exists
1202 $address = ip_address();
1203 }
1204
1205 // hack for safari
1206 if ($address == '::1') {
1207 $address = '127.0.0.1';
1208 }
1209
1210 // when we need to have strictly IPV4 ip address
1211 // convert ipV6 to ipV4
1212 if ($strictIPV4) {
1213 // this converts 'IPV4 mapped IPV6 address' to IPV4
1214 if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && strstr($address, '::ffff:')) {
1215 $address = ltrim($address, '::ffff:');
1216 }
1217 }
1218
1219 return $address;
1220 }
1221
1222 /**
1223 * Get the referring / previous page URL.
1224 *
1225 * @return string
1226 * The previous page URL
1227 */
1228 public static function refererPath() {
1229 return CRM_Utils_Array::value('HTTP_REFERER', $_SERVER);
1230 }
1231
1232 /**
1233 * Get the documentation base URL.
1234 *
1235 * @return string
1236 * Base URL of the CRM documentation.
1237 */
1238 public static function getDocBaseURL() {
1239 // FIXME: move this to configuration at some stage
1240 return 'http://book.civicrm.org/';
1241 }
1242
1243 /**
1244 * Returns wiki (alternate) documentation URL base.
1245 *
1246 * @return string
1247 * documentation url
1248 */
1249 public static function getWikiBaseURL() {
1250 // FIXME: move this to configuration at some stage
1251 return 'http://wiki.civicrm.org/confluence/display/CRMDOC/';
1252 }
1253
1254 /**
1255 * Returns URL or link to documentation page, based on provided parameters.
1256 *
1257 * For use in PHP code.
1258 * WARNING: Always returns URL, if ts function is not defined ($URLonly has
1259 * no effect).
1260 *
1261 * @param string $page
1262 * Title of documentation wiki page.
1263 * @param bool $URLonly
1264 * (optional) Whether to return URL only or full HTML link (default).
1265 * @param string $text
1266 * (optional) Text of HTML link (no effect if $URLonly = false).
1267 * @param string $title
1268 * (optional) Tooltip text for HTML link (no effect if $URLonly = false)
1269 * @param string $style
1270 * (optional) Style attribute value for HTML link (no effect if $URLonly = false)
1271 *
1272 * @param null $resource
1273 *
1274 * @return string
1275 * URL or link to documentation page, based on provided parameters.
1276 */
1277 public static function docURL2($page, $URLonly = FALSE, $text = NULL, $title = NULL, $style = NULL, $resource = NULL) {
1278 // if ts function doesn't exist, it means that CiviCRM hasn't been fully initialised yet -
1279 // return just the URL, no matter what other parameters are defined
1280 if (!function_exists('ts')) {
1281 if ($resource == 'wiki') {
1282 $docBaseURL = self::getWikiBaseURL();
1283 }
1284 else {
1285 $docBaseURL = self::getDocBaseURL();
1286 }
1287 return $docBaseURL . str_replace(' ', '+', $page);
1288 }
1289 else {
1290 $params = array(
1291 'page' => $page,
1292 'URLonly' => $URLonly,
1293 'text' => $text,
1294 'title' => $title,
1295 'style' => $style,
1296 'resource' => $resource,
1297 );
1298 return self::docURL($params);
1299 }
1300 }
1301
1302 /**
1303 * Returns URL or link to documentation page, based on provided parameters.
1304 *
1305 * For use in templates code.
1306 *
1307 * @param array $params
1308 * An array of parameters (see CRM_Utils_System::docURL2 method for names)
1309 *
1310 * @return void|string
1311 * URL or link to documentation page, based on provided parameters.
1312 */
1313 public static function docURL($params) {
1314
1315 if (!isset($params['page'])) {
1316 return NULL;
1317 }
1318
1319 if (CRM_Utils_Array::value('resource', $params) == 'wiki') {
1320 $docBaseURL = self::getWikiBaseURL();
1321 }
1322 else {
1323 $docBaseURL = self::getDocBaseURL();
1324 }
1325
1326 if (!isset($params['title']) or $params['title'] === NULL) {
1327 $params['title'] = ts('Opens documentation in a new window.');
1328 }
1329
1330 if (!isset($params['text']) or $params['text'] === NULL) {
1331 $params['text'] = ts('(learn more...)');
1332 }
1333
1334 if (!isset($params['style']) || $params['style'] === NULL) {
1335 $style = '';
1336 }
1337 else {
1338 $style = "style=\"{$params['style']}\"";
1339 }
1340
1341 $link = $docBaseURL . str_replace(' ', '+', $params['page']);
1342
1343 if (isset($params['URLonly']) && $params['URLonly'] == TRUE) {
1344 return $link;
1345 }
1346 else {
1347 return "<a href=\"{$link}\" $style target=\"_blank\" class=\"crm-doc-link no-popup\" title=\"{$params['title']}\">{$params['text']}</a>";
1348 }
1349 }
1350
1351 /**
1352 * Get the locale set in the hosting CMS.
1353 *
1354 * @return string
1355 * The used locale or null for none.
1356 */
1357 public static function getUFLocale() {
1358 $config = CRM_Core_Config::singleton();
1359 return $config->userSystem->getUFLocale();
1360 }
1361
1362 /**
1363 * Execute external or internal URLs and return server response.
1364 *
1365 * @param string $url
1366 * Request URL.
1367 * @param bool $addCookie
1368 * Whether to provide a cookie. Should be true to access internal URLs.
1369 *
1370 * @return string
1371 * Response from URL.
1372 */
1373 public static function getServerResponse($url, $addCookie = TRUE) {
1374 CRM_Core_TemporaryErrorScope::ignoreException();
1375 require_once 'HTTP/Request.php';
1376 $request = new HTTP_Request($url);
1377
1378 if ($addCookie) {
1379 foreach ($_COOKIE as $name => $value) {
1380 $request->addCookie($name, $value);
1381 }
1382 }
1383
1384 if (isset($_SERVER['AUTH_TYPE'])) {
1385 $request->setBasicAuth($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
1386 }
1387
1388 $config = CRM_Core_Config::singleton();
1389 if ($config->userFramework == 'WordPress') {
1390 session_write_close();
1391 }
1392
1393 $request->sendRequest();
1394 $response = $request->getResponseBody();
1395
1396 return $response;
1397 }
1398
1399 /**
1400 */
1401 public static function isDBVersionValid(&$errorMessage) {
1402 $dbVersion = CRM_Core_BAO_Domain::version();
1403
1404 if (!$dbVersion) {
1405 // if db.ver missing
1406 $errorMessage = ts('Version information found to be missing in database. You will need to determine the correct version corresponding to your current database state.');
1407 return FALSE;
1408 }
1409 elseif (!CRM_Utils_System::isVersionFormatValid($dbVersion)) {
1410 $errorMessage = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
1411 return FALSE;
1412 }
1413 elseif (stripos($dbVersion, 'upgrade')) {
1414 // if db.ver indicates a partially upgraded db
1415 $upgradeUrl = CRM_Utils_System::url("civicrm/upgrade", "reset=1");
1416 $errorMessage = ts('Database check failed - the database looks to have been partially upgraded. You may want to reload the database with the backup and try the <a href=\'%1\'>upgrade process</a> again.', array(1 => $upgradeUrl));
1417 return FALSE;
1418 }
1419 else {
1420 $codeVersion = CRM_Utils_System::version();
1421
1422 // if db.ver < code.ver, time to upgrade
1423 if (version_compare($dbVersion, $codeVersion) < 0) {
1424 $upgradeUrl = CRM_Utils_System::url("civicrm/upgrade", "reset=1");
1425 $errorMessage = ts('New codebase version detected. You might want to visit <a href=\'%1\'>upgrade screen</a> to upgrade the database.', array(1 => $upgradeUrl));
1426 return FALSE;
1427 }
1428
1429 // if db.ver > code.ver, sth really wrong
1430 if (version_compare($dbVersion, $codeVersion) > 0) {
1431 $errorMessage = '<p>' . ts('Your database is marked with an unexpected version number: %1. The v%2 codebase may not be compatible with your database state. You will need to determine the correct version corresponding to your current database state. You may want to revert to the codebase you were using until you resolve this problem.',
1432 array(1 => $dbVersion, 2 => $codeVersion)
1433 ) . '</p>';
1434 $errorMessage .= "<p>" . ts('OR if this is a manual install from git, you might want to fix civicrm-version.php file.') . "</p>";
1435 return FALSE;
1436 }
1437 }
1438 // FIXME: there should be another check to make sure version is in valid format - X.Y.alpha_num
1439
1440 return TRUE;
1441 }
1442
1443 /**
1444 * Exit with provided exit code.
1445 *
1446 * @param int $status
1447 * (optional) Code with which to exit.
1448 */
1449 public static function civiExit($status = 0) {
1450 // move things to CiviCRM cache as needed
1451 CRM_Core_Session::storeSessionObjects();
1452
1453 exit($status);
1454 }
1455
1456 /**
1457 * Reset the various system caches and some important static variables.
1458 */
1459 public static function flushCache() {
1460 // flush out all cache entries so we can reload new data
1461 // a bit aggressive, but livable for now
1462 $cache = CRM_Utils_Cache::singleton();
1463 $cache->flush();
1464
1465 // also reset the various static memory caches
1466
1467 // reset the memory or array cache
1468 CRM_Core_BAO_Cache::deleteGroup('contact fields', NULL, FALSE);
1469
1470 // reset ACL cache
1471 CRM_ACL_BAO_Cache::resetCache();
1472
1473 // reset various static arrays used here
1474 CRM_Contact_BAO_Contact::$_importableFields = CRM_Contact_BAO_Contact::$_exportableFields
1475 = CRM_Contribute_BAO_Contribution::$_importableFields
1476 = CRM_Contribute_BAO_Contribution::$_exportableFields
1477 = CRM_Pledge_BAO_Pledge::$_exportableFields = CRM_Contribute_BAO_Query::$_contributionFields
1478 = CRM_Core_BAO_CustomField::$_importFields
1479 = CRM_Core_BAO_Cache::$_cache = CRM_Core_DAO::$_dbColumnValueCache = NULL;
1480
1481 CRM_Core_OptionGroup::flushAll();
1482 CRM_Utils_PseudoConstant::flushAll();
1483 }
1484
1485 /**
1486 * Load CMS bootstrap.
1487 *
1488 * @param array $params
1489 * Array with uid name and pass
1490 * @param bool $loadUser
1491 * Boolean load user or not.
1492 * @param bool $throwError
1493 * @param $realPath
1494 */
1495 public static function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) {
1496 if (!is_array($params)) {
1497 $params = array();
1498 }
1499 $config = CRM_Core_Config::singleton();
1500 return $config->userSystem->loadBootStrap($params, $loadUser, $throwError, $realPath);
1501 }
1502
1503 /**
1504 * Check if user is logged in.
1505 *
1506 * @return bool
1507 */
1508 public static function isUserLoggedIn() {
1509 $config = CRM_Core_Config::singleton();
1510 return $config->userSystem->isUserLoggedIn();
1511 }
1512
1513 /**
1514 * Get current logged in user id.
1515 *
1516 * @return int
1517 * ufId, currently logged in user uf id.
1518 */
1519 public static function getLoggedInUfID() {
1520 $config = CRM_Core_Config::singleton();
1521 return $config->userSystem->getLoggedInUfID();
1522 }
1523
1524 /**
1525 */
1526 public static function baseCMSURL() {
1527 static $_baseURL = NULL;
1528 if (!$_baseURL) {
1529 $config = CRM_Core_Config::singleton();
1530 $_baseURL = $userFrameworkBaseURL = $config->userFrameworkBaseURL;
1531
1532 if ($config->userFramework == 'Joomla') {
1533 // gross hack
1534 // we need to remove the administrator/ from the end
1535 $_baseURL = str_replace("/administrator/", "/", $userFrameworkBaseURL);
1536 }
1537 else {
1538 // Drupal setting
1539 global $civicrm_root;
1540 if (strpos($civicrm_root,
1541 DIRECTORY_SEPARATOR . 'sites' .
1542 DIRECTORY_SEPARATOR . 'all' .
1543 DIRECTORY_SEPARATOR . 'modules'
1544 ) === FALSE
1545 ) {
1546 $startPos = strpos($civicrm_root,
1547 DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR
1548 );
1549 $endPos = strpos($civicrm_root,
1550 DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR
1551 );
1552 if ($startPos && $endPos) {
1553 // if component is in sites/SITENAME/modules
1554 $siteName = substr($civicrm_root,
1555 $startPos + 7,
1556 $endPos - $startPos - 7
1557 );
1558
1559 $_baseURL = $userFrameworkBaseURL . "sites/$siteName/";
1560 }
1561 }
1562 }
1563 }
1564 return $_baseURL;
1565 }
1566
1567 /**
1568 * Given a URL, return a relative URL if possible.
1569 *
1570 * @param string $url
1571 * @return string
1572 */
1573 public static function relativeURL($url) {
1574 // check if url is relative, if so return immediately
1575 if (substr($url, 0, 4) != 'http') {
1576 return $url;
1577 }
1578
1579 // make everything relative from the baseFilePath
1580 $baseURL = self::baseCMSURL();
1581
1582 // check if baseURL is a substr of $url, if so
1583 // return rest of string
1584 if (substr($url, 0, strlen($baseURL)) == $baseURL) {
1585 return substr($url, strlen($baseURL));
1586 }
1587
1588 // return the original value
1589 return $url;
1590 }
1591
1592 /**
1593 * Produce an absolute URL from a possibly-relative URL.
1594 *
1595 * @param string $url
1596 * @param bool $removeLanguagePart
1597 *
1598 * @return string
1599 */
1600 public static function absoluteURL($url, $removeLanguagePart = FALSE) {
1601 // check if url is already absolute, if so return immediately
1602 if (substr($url, 0, 4) == 'http') {
1603 return $url;
1604 }
1605
1606 // make everything absolute from the baseFileURL
1607 $baseURL = self::baseCMSURL();
1608
1609 //CRM-7622: drop the language from the URL if requested (and it’s there)
1610 $config = CRM_Core_Config::singleton();
1611 if ($removeLanguagePart) {
1612 $baseURL = self::languageNegotiationURL($baseURL, FALSE, TRUE);
1613 }
1614
1615 return $baseURL . $url;
1616 }
1617
1618 /**
1619 * Clean url, replaces first '&' with '?'
1620 *
1621 * @param string $url
1622 *
1623 * @return string
1624 * , clean url
1625 */
1626 public static function cleanUrl($url) {
1627 if (!$url) {
1628 return NULL;
1629 }
1630
1631 if ($pos = strpos($url, '&')) {
1632 $url = substr_replace($url, '?', $pos, 1);
1633 }
1634
1635 return $url;
1636 }
1637
1638 /**
1639 * Format the url as per language Negotiation.
1640 *
1641 * @param string $url
1642 *
1643 * @param bool $addLanguagePart
1644 * @param bool $removeLanguagePart
1645 *
1646 * @return string
1647 * , formatted url.
1648 */
1649 public static function languageNegotiationURL(
1650 $url,
1651 $addLanguagePart = TRUE,
1652 $removeLanguagePart = FALSE
1653 ) {
1654 $config = &CRM_Core_Config::singleton();
1655 return $config->userSystem->languageNegotiationURL($url, $addLanguagePart, $removeLanguagePart);
1656 }
1657
1658 /**
1659 * Append the contents of an 'extra' smarty template file if it is present in
1660 * the custom template directory. This does not work if there are
1661 * multiple custom template directories
1662 *
1663 * @param string $fileName
1664 * The name of the tpl file that we are processing.
1665 * @param string $content
1666 * The current content string. May be modified by this function.
1667 * @param string $overideFileName
1668 * (optional) Sent by contribution/event reg/profile pages which uses a id
1669 * specific extra file name if present.
1670 */
1671 public static function appendTPLFile(
1672 $fileName,
1673 &$content,
1674 $overideFileName = NULL
1675 ) {
1676 $template = CRM_Core_Smarty::singleton();
1677 if ($overideFileName) {
1678 $additionalTPLFile = $overideFileName;
1679 }
1680 else {
1681 $additionalTPLFile = str_replace('.tpl', '.extra.tpl', $fileName);
1682 }
1683
1684 if ($template->template_exists($additionalTPLFile)) {
1685 $content .= $template->fetch($additionalTPLFile);
1686 }
1687 }
1688
1689 /**
1690 * Get a list of all files that are found within the directories
1691 * that are the result of appending the provided relative path to
1692 * each component of the PHP include path.
1693 *
1694 * @author Ken Zalewski
1695 *
1696 * @param string $relpath
1697 * A relative path, typically pointing to a directory with multiple class
1698 * files.
1699 *
1700 * @return array
1701 * An array of files that exist in one or more of the directories that are
1702 * referenced by the relative path when appended to each element of the PHP
1703 * include path.
1704 */
1705 public static function listIncludeFiles($relpath) {
1706 $file_list = array();
1707 $inc_dirs = explode(PATH_SEPARATOR, get_include_path());
1708 foreach ($inc_dirs as $inc_dir) {
1709 $target_dir = $inc_dir . DIRECTORY_SEPARATOR . $relpath;
1710 if (is_dir($target_dir)) {
1711 $cur_list = scandir($target_dir);
1712 foreach ($cur_list as $fname) {
1713 if ($fname != '.' && $fname != '..') {
1714 $file_list[$fname] = $fname;
1715 }
1716 }
1717 }
1718 }
1719 return $file_list;
1720 }
1721 // listIncludeFiles()
1722
1723 /**
1724 * Get a list of all "plugins" (PHP classes that implement a piece of
1725 * functionality using a well-defined interface) that are found in a
1726 * particular CiviCRM directory (both custom and core are searched).
1727 *
1728 * @author Ken Zalewski
1729 *
1730 * @param string $relpath
1731 * A relative path referencing a directory that contains one or more
1732 * plugins.
1733 * @param string $fext
1734 * (optional) Only files with this extension will be considered to be
1735 * plugins.
1736 * @param array $skipList
1737 * (optional) List of files to skip.
1738 *
1739 * @return array
1740 * List of plugins, where the plugin name is both the key and the value of
1741 * each element.
1742 */
1743 public static function getPluginList($relpath, $fext = '.php', $skipList = array()) {
1744 $fext_len = strlen($fext);
1745 $plugins = array();
1746 $inc_files = CRM_Utils_System::listIncludeFiles($relpath);
1747 foreach ($inc_files as $inc_file) {
1748 if (substr($inc_file, 0 - $fext_len) == $fext) {
1749 $plugin_name = substr($inc_file, 0, 0 - $fext_len);
1750 if (!in_array($plugin_name, $skipList)) {
1751 $plugins[$plugin_name] = $plugin_name;
1752 }
1753 }
1754 }
1755 return $plugins;
1756 }
1757 // getPluginList()
1758
1759 /**
1760 */
1761 public static function executeScheduledJobs() {
1762 $facility = new CRM_Core_JobManager();
1763 $facility->execute(FALSE);
1764
1765 $redirectUrl = self::url('civicrm/admin/job', 'reset=1');
1766
1767 CRM_Core_Session::setStatus(
1768 ts('Scheduled jobs have been executed according to individual timing settings. Please check log for messages.'),
1769 ts('Complete'), 'success');
1770
1771 CRM_Utils_System::redirect($redirectUrl);
1772 }
1773
1774 /**
1775 * Evaluate any tokens in a URL.
1776 *
1777 * @param string|FALSE $url
1778 * @return string|FALSE
1779 */
1780 public static function evalUrl($url) {
1781 if (!$url || strpos($url, '{') === FALSE) {
1782 return $url;
1783 }
1784 else {
1785 $config = CRM_Core_Config::singleton();
1786 $vars = array(
1787 '{ver}' => CRM_Utils_System::version(),
1788 '{uf}' => $config->userFramework,
1789 '{php}' => phpversion(),
1790 '{sid}' => self::getSiteID(),
1791 '{baseUrl}' => $config->userFrameworkBaseURL,
1792 '{lang}' => $config->lcMessages,
1793 '{co}' => $config->defaultContactCountry,
1794 );
1795 return strtr($url, array_map('urlencode', $vars));
1796 }
1797 }
1798
1799 /**
1800 * Returns the unique identifier for this site, as used by community messages.
1801 *
1802 * SiteID will be generated if it is not already stored in the settings table.
1803 *
1804 * @return string
1805 */
1806 public static function getSiteID() {
1807 $sid = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'site_id');
1808 if (!$sid) {
1809 $config = CRM_Core_Config::singleton();
1810 $sid = md5('sid_' . (defined('CIVICRM_SITE_KEY') ? CIVICRM_SITE_KEY : '') . '_' . $config->userFrameworkBaseURL);
1811 civicrm_api3('Setting', 'create', array('domain_id' => 'all', 'site_id' => $sid));
1812 }
1813 return $sid;
1814 }
1815
1816 /**
1817 * Determine whether this is a developmental system.
1818 *
1819 * @return bool
1820 */
1821 public static function isDevelopment() {
1822 static $cache = NULL;
1823 if ($cache === NULL) {
1824 global $civicrm_root;
1825 $cache = file_exists("{$civicrm_root}/.svn") || file_exists("{$civicrm_root}/.git");
1826 }
1827 return $cache;
1828 }
1829
1830 /**
1831 * @return bool
1832 */
1833 public static function isInUpgradeMode() {
1834 $args = explode('/', $_GET['q']);
1835 $upgradeInProcess = CRM_Core_Session::singleton()->get('isUpgradePending');
1836 if ((isset($args[1]) && $args[1] == 'upgrade') || $upgradeInProcess) {
1837 return TRUE;
1838 }
1839 else {
1840 return FALSE;
1841 }
1842 }
1843
1844 /**
1845 * Determine the standard URL for viewing or editing the specified link.
1846 *
1847 * This function delegates the decision-making to (a) the hook system and
1848 * (b) the BAO system.
1849 *
1850 * @param array $crudLinkSpec
1851 * With keys:.
1852 * - action: int, CRM_Core_Action::UPDATE or CRM_Core_Action::VIEW [default: VIEW]
1853 * - entity_table: string, eg "civicrm_contact"
1854 * - entity_id: int
1855 * @return array|NULL
1856 * NULL if unavailable, or an array. array has keys:
1857 * - path: string
1858 * - query: array
1859 * - title: string
1860 * - url: string
1861 */
1862 public static function createDefaultCrudLink($crudLinkSpec) {
1863 $crudLinkSpec['action'] = CRM_Utils_Array::value('action', $crudLinkSpec, CRM_Core_Action::VIEW);
1864 $daoClass = CRM_Core_DAO_AllCoreTables::getClassForTable($crudLinkSpec['entity_table']);
1865 if (!$daoClass) {
1866 return NULL;
1867 }
1868
1869 $baoClass = str_replace('_DAO_', '_BAO_', $daoClass);
1870 if (!class_exists($baoClass)) {
1871 return NULL;
1872 }
1873
1874 $bao = new $baoClass();
1875 $bao->id = $crudLinkSpec['entity_id'];
1876 if (!$bao->find(TRUE)) {
1877 return NULL;
1878 }
1879
1880 $link = array();
1881 CRM_Utils_Hook::crudLink($crudLinkSpec, $bao, $link);
1882 if (empty($link) && is_callable(array($bao, 'createDefaultCrudLink'))) {
1883 $link = $bao->createDefaultCrudLink($crudLinkSpec);
1884 }
1885
1886 if (!empty($link)) {
1887 if (!isset($link['url'])) {
1888 $link['url'] = self::url($link['path'], $link['query'], TRUE, NULL, FALSE);
1889 }
1890 return $link;
1891 }
1892
1893 return NULL;
1894 }
1895
1896 }