3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * System wide utilities.
40 class CRM_Utils_System
{
42 static $_callbacks = NULL;
45 * Compose a new url string from the current url string
46 * Used by all the framework components, specifically,
49 * @param string $urlVar the url variable being considered (i.e. crmPageID, crmSortID etc)
51 * @return string the url fragment
54 static function makeURL($urlVar, $includeReset = FALSE, $includeForce = TRUE, $path = NULL) {
56 $config = CRM_Core_Config
::singleton();
57 $path = CRM_Utils_Array
::value($config->userFrameworkURLVar
, $_GET);
63 return self
::url($path,
64 CRM_Utils_System
::getLinksUrl($urlVar, $includeReset, $includeForce)
69 * get the query string and clean it up. Strip some variables that should not
70 * be propagated, specically variable like 'reset'. Also strip any side-affect
71 * actions (i.e. export)
73 * This function is copied mostly verbatim from Pager.php (_getLinksUrl)
75 * @param string $urlVar the url variable being considered (i.e. crmPageID, crmSortID etc)
76 * @param boolean $includeReset should we include the reset var (generally this variable should be skipped)
81 static function getLinksUrl($urlVar, $includeReset = FALSE, $includeForce = TRUE, $skipUFVar = TRUE) {
82 // Sort out query string to prevent messy urls
83 $querystring = array();
87 if (!empty($_SERVER['QUERY_STRING'])) {
88 $qs = explode('&', str_replace('&', '&', $_SERVER['QUERY_STRING']));
89 for ($i = 0, $cnt = count($qs); $i < $cnt; $i++
) {
90 // check first if exist a pair
91 if (strstr($qs[$i], '=') !== FALSE) {
92 list($name, $value) = explode('=', $qs[$i]);
93 if ($name != $urlVar) {
94 $name = rawurldecode($name);
95 //check for arrays in parameters: site.php?foo[]=1&foo[]=2&foo[]=3
96 if ((strpos($name, '[') !== FALSE) &&
97 (strpos($name, ']') !== FALSE)
117 unset($qs['snippet']);
118 unset($qs['section']);
121 $config = CRM_Core_Config
::singleton();
122 unset($qs[$config->userFrameworkURLVar
]);
125 foreach ($qs as $name => $value) {
126 if ($name != 'reset' ||
$includeReset) {
127 $querystring[] = $name . '=' . $value;
131 $querystring = array_merge($querystring, array_unique($arrays));
132 $querystring = array_map('htmlentities', $querystring);
134 return implode('&', $querystring) . (!empty($querystring) ?
'&' : '') . $urlVar . '=';
138 * if we are using a theming system, invoke theme, else just print the
141 * @param string $content the content that will be themed
142 * @param boolean $print are we displaying to the screen or bypassing theming?
143 * @param boolean $maintenance for maintenance mode
145 * @return void prints content on stdout
149 static function theme(
154 $config = &CRM_Core_Config
::singleton();
155 return $config->userSystem
->theme($content, $print, $maintenance);
159 * Generate a query string if input is an array
161 * @param mixed $query: array or string
166 static function makeQueryString($query) {
167 if (is_array($query)) {
169 foreach ($query as $key => $value) {
170 $buf .= ($buf ?
'&' : '') . urlencode($key) . '=' . urlencode($value);
178 * Generate an internal CiviCRM URL
180 * @param $path string The path being linked to, such as "civicrm/add"
181 * @param $query mixed A query string to append to the link, or an array of key-value pairs
182 * @param $absolute boolean Whether to force the output to be an absolute link (beginning with http:).
183 * Useful for links that will be displayed outside the site, such as in an
185 * @param $fragment string A fragment identifier (named anchor) to append to the link.
187 * @return string an HTML string containing a link to the given path.
198 $forceBackend = FALSE
200 $query = self
::makeQueryString($query);
202 // we have a valid query and it has not yet been transformed
203 if ($htmlize && !empty($query) && strpos($query, '&') === FALSE) {
204 $query = htmlentities($query);
207 $config = CRM_Core_Config
::singleton();
208 return $config->userSystem
->url($path, $query, $absolute, $fragment, $htmlize, $frontend, $forceBackend);
211 function href($text, $path = NULL, $query = NULL, $absolute = TRUE,
212 $fragment = NULL, $htmlize = TRUE, $frontend = FALSE, $forceBackend = FALSE
214 $url = self
::url($path, $query, $absolute, $fragment, $htmlize, $frontend, $forceBackend);
215 return "<a href=\"$url\">$text</a>";
218 static function permissionDenied() {
219 $config = CRM_Core_Config
::singleton();
220 return $config->userSystem
->permissionDenied();
223 static function logout() {
224 $config = CRM_Core_Config
::singleton();
225 return $config->userSystem
->logout();
228 // this is a very drupal specific function for now
229 static function updateCategories() {
230 $config = CRM_Core_Config
::singleton();
231 if ($config->userSystem
->is_drupal
) {
232 $config->userSystem
->updateCategories();
237 * What menu path are we currently on. Called for the primary tpl
239 * @return string the current menu path
242 static function currentPath() {
243 $config = CRM_Core_Config
::singleton();
244 return trim(CRM_Utils_Array
::value($config->userFrameworkURLVar
, $_GET), '/');
248 * this function is called from a template to compose a url
250 * @param array $params list of parameters
256 static function crmURL($params) {
257 $p = CRM_Utils_Array
::value('p', $params);
259 $p = self
::currentPath();
263 CRM_Utils_Array
::value('q', $params),
264 CRM_Utils_Array
::value('a', $params, FALSE),
265 CRM_Utils_Array
::value('f', $params),
266 CRM_Utils_Array
::value('h', $params, TRUE),
267 CRM_Utils_Array
::value('fe', $params, FALSE),
268 CRM_Utils_Array
::value('fb', $params, FALSE)
273 * sets the title of the page
275 * @param string $title
276 * @param string $pageTitle
282 static function setTitle($title, $pageTitle = NULL) {
283 $config = CRM_Core_Config
::singleton();
284 return $config->userSystem
->setTitle($title, $pageTitle);
288 * figures and sets the userContext. Uses the referer if valid
289 * else uses the default
291 * @param array $names refererer should match any str in this array
292 * @param string $default the default userContext if no match found
297 static function setUserContext($names, $default = NULL) {
300 $session = CRM_Core_Session
::singleton();
301 $referer = CRM_Utils_Array
::value('HTTP_REFERER', $_SERVER);
303 if ($referer && !empty($names)) {
304 foreach ($names as $name) {
305 if (strstr($referer, $name)) {
313 $session->pushUserContext($url);
318 * gets a class name for an object
320 * @param object $object - object whose class name is needed
322 * @return string $className - class name
327 static function getClassName($object) {
328 return get_class($object);
332 * redirect to another url
334 * @param string $url the url to goto
340 static function redirect($url = NULL) {
342 $url = self
::url('civicrm/dashboard', 'reset=1');
345 // replace the & characters with &
346 // this is kinda hackish but not sure how to do it right
347 $url = str_replace('&', '&', $url);
348 header('Location: ' . $url);
353 * use a html based file with javascript embedded to redirect to another url
354 * This prevent the too many redirect errors emitted by various browsers
356 * @param string $url the url to goto
362 static function jsRedirect(
368 $url = self
::url('civicrm/dashboard', 'reset=1');
372 $title = ts('CiviCRM task in progress');
376 $message = ts('A long running CiviCRM task is currently in progress. This message will be refreshed till the task is completed');
379 // replace the & characters with &
380 // this is kinda hackish but not sure how to do it right
381 $url = str_replace('&', '&', $url);
383 $template = CRM_Core_Smarty
::singleton();
384 $template->assign('redirectURL', $url);
385 $template->assign('title', $title);
386 $template->assign('message', $message);
388 $html = $template->fetch('CRM/common/redirectJS.tpl');
396 * Append an additional breadcrumb tag to the existing breadcrumb
398 * @param string $title
405 static function appendBreadCrumb($breadCrumbs) {
406 $config = CRM_Core_Config
::singleton();
407 return $config->userSystem
->appendBreadCrumb($breadCrumbs);
411 * Reset an additional breadcrumb tag to the existing breadcrumb
417 static function resetBreadCrumb() {
418 $config = CRM_Core_Config
::singleton();
419 return $config->userSystem
->resetBreadCrumb();
423 * Append a string to the head of the html file
425 * @param string $head the new string to be appended
431 static function addHTMLHead($bc) {
432 $config = CRM_Core_Config
::singleton();
433 return $config->userSystem
->addHTMLHead($bc);
437 * figure out the post url for the form
439 * @param the default action if one is pre-specified
441 * @return string the url to post the form
445 static function postURL($action) {
446 $config = CRM_Core_Config
::singleton();
447 return $config->userSystem
->postURL($action);
451 * rewrite various system urls to https
457 static function mapConfigToSSL() {
458 $config = CRM_Core_Config
::singleton();
459 $config->userFrameworkResourceURL
= str_replace('http://', 'https://',
460 $config->userFrameworkResourceURL
462 $config->resourceBase
= $config->userFrameworkResourceURL
;
463 return $config->userSystem
->mapConfigToSSL();
467 * Get the base URL from the system
475 static function baseURL() {
476 $config = CRM_Core_Config
::singleton();
477 return $config->userFrameworkBaseURL
;
480 static function authenticateAbort($message, $abort) {
490 static function authenticateKey($abort = TRUE) {
491 // also make sure the key is sent and is valid
492 $key = trim(CRM_Utils_Array
::value('key', $_REQUEST));
494 $docAdd = "More info at:" . CRM_Utils_System
::docURL2("Managing Scheduled Jobs", TRUE, NULL, NULL, NULL, "wiki");
497 return self
::authenticateAbort("ERROR: You need to send a valid key to execute this file. " . $docAdd . "\n",
502 $siteKey = defined('CIVICRM_SITE_KEY') ? CIVICRM_SITE_KEY
: NULL;
507 return self
::authenticateAbort("ERROR: You need to set a valid site key in civicrm.settings.php. " . $docAdd . "\n",
512 if (strlen($siteKey) < 8) {
513 return self
::authenticateAbort("ERROR: Site key needs to be greater than 7 characters in civicrm.settings.php. " . $docAdd . "\n",
518 if ($key !== $siteKey) {
519 return self
::authenticateAbort("ERROR: Invalid key value sent. " . $docAdd . "\n",
527 static function authenticateScript($abort = TRUE, $name = NULL, $pass = NULL, $storeInSession = TRUE, $loadCMSBootstrap = TRUE, $requireKey = TRUE) {
528 // auth to make sure the user has a login/password to do a shell
530 // later on we'll link this to acl's
532 $name = trim(CRM_Utils_Array
::value('name', $_REQUEST));
533 $pass = trim(CRM_Utils_Array
::value('pass', $_REQUEST));
536 // its ok to have an empty password
538 return self
::authenticateAbort("ERROR: You need to send a valid user name and password to execute this file\n",
543 if ($requireKey && !self
::authenticateKey($abort)) {
547 $result = CRM_Utils_System
::authenticate($name, $pass, $loadCMSBootstrap);
549 return self
::authenticateAbort("ERROR: Invalid username and/or password\n",
553 elseif ($storeInSession) {
554 // lets store contact id and user id in session
555 list($userID, $ufID, $randomNumber) = $result;
556 if ($userID && $ufID) {
558 $config = CRM_Core_Config
::singleton();
559 $config->userSystem
->setUserSession( array($userID, $ufID) );
562 return self
::authenticateAbort("ERROR: Unexpected error, could not match userID and contactID",
572 * Authenticate the user against the uf db
574 * @param string $name the user name
575 * @param string $password the password for the above user name
577 * @return mixed false if no auth
579 contactID, ufID, unique string ) if success
583 static function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
584 $config = CRM_Core_Config
::singleton();
585 return $config->userSystem
->authenticate($name, $password, $loadCMSBootstrap, $realPath);
589 * Set a message in the UF to display to a user
591 * @param string $name the message to set
596 static function setUFMessage($message) {
597 $config = CRM_Core_Config
::singleton();
598 return $config->userSystem
->setMessage($message);
603 static function isNull($value) {
604 // FIXME: remove $value = 'null' string test when we upgrade our DAO code to handle passing null in a better way.
605 if (!isset($value) ||
$value === NULL ||
$value === '' ||
$value === 'null') {
608 if (is_array($value)) {
609 foreach ($value as $key => $value) {
610 if (!self
::isNull($value)) {
619 static function mungeCreditCard($number, $keep = 4) {
620 $number = trim($number);
621 if (empty($number)) {
624 $replace = str_repeat('*', strlen($number) - $keep);
625 return substr_replace($number, $replace, 0, -$keep);
628 /** parse php modules from phpinfo */
629 function parsePHPModules() {
631 phpinfo(INFO_MODULES
);
632 $s = ob_get_contents();
635 $s = strip_tags($s, '<h2><th><td>');
636 $s = preg_replace('/<th[^>]*>([^<]+)<\/th>/', "<info>\\1</info>", $s);
637 $s = preg_replace('/<td[^>]*>([^<]+)<\/td>/', "<info>\\1</info>", $s);
638 $vTmp = preg_split('/(<h2>[^<]+<\/h2>)/', $s, -1, PREG_SPLIT_DELIM_CAPTURE
);
640 for ($i = 1; $i < count($vTmp); $i++
) {
641 if (preg_match('/<h2>([^<]+)<\/h2>/', $vTmp[$i], $vMat)) {
642 $vName = trim($vMat[1]);
643 $vTmp2 = explode("\n", $vTmp[$i +
1]);
644 foreach ($vTmp2 AS $vOne) {
645 $vPat = '<info>([^<]+)<\/info>';
646 $vPat3 = "/$vPat\s*$vPat\s*$vPat/";
647 $vPat2 = "/$vPat\s*$vPat/";
649 if (preg_match($vPat3, $vOne, $vMat)) {
650 $vModules[$vName][trim($vMat[1])] = array(trim($vMat[2]), trim($vMat[3]));
653 elseif (preg_match($vPat2, $vOne, $vMat)) {
654 $vModules[$vName][trim($vMat[1])] = trim($vMat[2]);
662 /** get a module setting */
663 function getModuleSetting($pModuleName, $pSetting) {
664 $vModules = self
::parsePHPModules();
665 return $vModules[$pModuleName][$pSetting];
668 static function memory($title = NULL) {
671 $pid = posix_getpid();
674 $memory = str_replace("\n", '', shell_exec("ps -p" . $pid . " -o rss="));
675 $memory .= ", " . time();
677 CRM_Core_Error
::debug_var($title, $memory);
682 static function download($name, $mimeType, &$buffer,
686 $now = gmdate('D, d M Y H:i:s') . ' GMT';
688 header('Content-Type: ' . $mimeType);
689 header('Expires: ' . $now);
691 // lem9 & loic1: IE need specific headers
692 $isIE = strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE');
694 $fileString = "filename=\"{$name}.{$ext}\"";
697 $fileString = "filename=\"{$name}\"";
700 header("Content-Disposition: inline; $fileString");
701 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
702 header('Pragma: public');
705 header("Content-Disposition: attachment; $fileString");
706 header('Pragma: no-cache');
715 static function xMemory($title = NULL, $log = FALSE) {
716 $mem = (float ) xdebug_memory_usage() / (float )(1024);
717 $mem = number_format($mem, 5) . ", " . time();
719 echo "<p>$title: $mem<p>";
721 CRM_Core_Error
::debug_var($title, $mem);
724 echo "<p>$title: $mem<p>";
729 static function fixURL($url) {
730 $components = parse_url($url);
736 // at some point we'll add code here to make sure the url is not
737 // something that will mess up up, so we need to clean it up here
742 * make sure the callback is valid in the current context
744 * @param string $callback the name of the function
749 static function validCallback($callback) {
750 if (self
::$_callbacks === NULL) {
751 self
::$_callbacks = array();
754 if (!array_key_exists($callback, self
::$_callbacks)) {
755 if (strpos($callback, '::') !== FALSE) {
756 list($className, $methodName) = explode('::', $callback);
757 $fileName = str_replace('_', DIRECTORY_SEPARATOR
, $className) . '.php';
758 // ignore errors if any
759 @include_once
($fileName);
760 if (!class_exists($className)) {
761 self
::$_callbacks[$callback] = FALSE;
764 // instantiate the class
765 $object = new $className();
766 if (!method_exists($object, $methodName)) {
767 self
::$_callbacks[$callback] = FALSE;
770 self
::$_callbacks[$callback] = TRUE;
775 self
::$_callbacks[$callback] = function_exists($callback);
778 return self
::$_callbacks[$callback];
782 * This serves as a wrapper to the php explode function
783 * we expect exactly $limit arguments in return, and if we dont
784 * get them, we pad it with null
786 static function explode($separator, $string, $limit) {
787 $result = explode($separator, $string, $limit);
788 for ($i = count($result); $i < $limit; $i++
) {
794 static function checkURL($url, $addCookie = FALSE) {
795 // make a GET request to $url
796 $ch = curl_init($url);
798 curl_setopt($ch, CURLOPT_COOKIE
, http_build_query($_COOKIE));
800 // it's quite alright to use a self-signed cert
801 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, 0);
803 // lets capture the return stuff rather than echo
804 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true );
806 return curl_exec($ch);
809 static function checkPHPVersion($ver = 5, $abort = TRUE) {
810 $phpVersion = substr(PHP_VERSION
, 0, 1);
811 if ($phpVersion >= $ver) {
816 CRM_Core_Error
::fatal(ts('This feature requires PHP Version %1 or greater',
823 static function formatWikiURL($string, $encode = FALSE) {
824 $items = explode(' ', trim($string), 2);
825 if (count($items) == 2) {
833 $url = $encode ? self
::urlEncode($items[0]) : $items[0];
834 return "<a href=\"$url\">$title</a>";
837 static function urlEncode($url) {
838 $items = parse_url($url);
839 if ($items === FALSE) {
843 if (!CRM_Utils_Array
::value('query', $items)) {
847 $items['query'] = urlencode($items['query']);
849 $url = $items['scheme'] . '://';
850 if (CRM_Utils_Array
::value('user', $items)) {
851 $url .= "{$items['user']}:{$items['pass']}@";
854 $url .= $items['host'];
855 if (CRM_Utils_Array
::value('port', $items)) {
856 $url .= ":{$items['port']}";
859 $url .= "{$items['path']}?{$items['query']}";
860 if (CRM_Utils_Array
::value('fragment', $items)) {
861 $url .= "#{$items['fragment']}";
868 * Function to return the latest civicrm version.
870 * @return string civicrm version
873 static function version() {
877 $verFile = implode(DIRECTORY_SEPARATOR
,
878 array(dirname(__FILE__
), '..', '..', 'civicrm-version.php')
880 if (file_exists($verFile)) {
881 require_once ($verFile);
882 if (function_exists('civicrmVersion')) {
883 $info = civicrmVersion();
884 $version = $info['version'];
888 // svn installs don't have version.txt by default. In that case version.xml should help -
889 $verFile = implode(DIRECTORY_SEPARATOR
,
890 array(dirname(__FILE__
), '..', '..', 'xml', 'version.xml')
892 if (file_exists($verFile)) {
893 $str = file_get_contents($verFile);
894 $xmlObj = simplexml_load_string($str);
895 $version = (string) $xmlObj->version_no
;
900 if (!CRM_Utils_System
::isVersionFormatValid($version)) {
901 CRM_Core_Error
::fatal('Unknown codebase version.');
908 static function isVersionFormatValid($version) {
909 return preg_match("/^(\d{1,2}\.){2,3}(\d{1,2}|(alpha|beta)\d{1,2})(\.upgrade)?$/", $version);
912 static function getAllHeaders() {
913 if (function_exists('getallheaders')) {
914 return getallheaders();
917 // emulate get all headers
918 // http://www.php.net/manual/en/function.getallheaders.php#66335
920 foreach ($_SERVER as $name => $value) {
921 if (substr($name, 0, 5) == 'HTTP_') {
922 $headers[str_replace(' ',
924 ucwords(strtolower(str_replace('_',
935 static function getRequestHeaders() {
936 if (function_exists('apache_request_headers')) {
937 return apache_request_headers();
945 * Check and determine is this is an SSL request
946 * Note that we inline this function in install/civicrm.php, so if
947 * you change this function, please go and change the code in the install script
949 static function isSSL( ) {
951 (isset($_SERVER['HTTPS']) &&
952 !empty($_SERVER['HTTPS']) &&
953 strtolower($_SERVER['HTTPS']) != 'off') ?
true : false;
956 static function redirectToSSL($abort = FALSE) {
957 $config = CRM_Core_Config
::singleton();
958 $req_headers = self
::getRequestHeaders();
959 if (CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enableSSL') &&
961 strtolower(CRM_Utils_Array
::value('X_FORWARDED_PROTO', $req_headers)) != 'https'
963 // ensure that SSL is enabled on a civicrm url (for cookie reasons etc)
964 $url = "https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
965 if (!self
::checkURL($url, TRUE)) {
967 CRM_Core_Error
::fatal('HTTPS is not set up on this machine');
970 CRM_Core_Session
::setStatus(ts('HTTPS is not set up on this machine'), ts('Warning'), 'alert');
971 // admin should be the only one following this
972 // since we dont want the user stuck in a bad place
976 CRM_Utils_System
::redirect($url);
981 * Get logged in user's IP address.
983 * Get IP address from HTTP Header. If the CMS is Drupal then use the Drupal function
984 * as this also handles reverse proxies (based on proper configuration in settings.php)
986 * @return string ip address of logged in user
989 static function ipAddress() {
990 $address = CRM_Utils_Array
::value('REMOTE_ADDR', $_SERVER);
992 $config = CRM_Core_Config
::singleton();
993 if ($config->userSystem
->is_drupal
) {
994 //drupal function handles the server being behind a proxy securely
999 if ($address == '::1') {
1000 $address = '127.0.0.1';
1007 * Returns you the referring / previous page url
1009 * @return string the previous page url
1012 static function refererPath() {
1013 return CRM_Utils_Array
::value('HTTP_REFERER', $_SERVER);
1017 * Returns default documentation URL base
1019 * @return string documentation url
1022 static function getDocBaseURL() {
1023 // FIXME: move this to configuration at some stage
1024 return 'http://book.civicrm.org/';
1028 * Returns wiki (alternate) documentation URL base
1030 * @return string documentation url
1033 static function getWikiBaseURL() {
1034 // FIXME: move this to configuration at some stage
1035 return 'http://wiki.civicrm.org/confluence/display/CRMDOC/';
1039 * Returns URL or link to documentation page, based on provided parameters.
1040 * For use in PHP code.
1041 * WARNING: Always returns URL, if ts function is not defined ($URLonly has no effect).
1043 * @param string $page Title of documentation wiki page
1044 * @param boolean $URLonly Whether function should return URL only or whole link (default)
1045 * @param string $text Text of HTML link (no effect if $URLonly = false)
1046 * @param string $title Tooltip text for HTML link (no effect if $URLonly = false)
1047 * @param string $style Style attribute value for HTML link (no effect if $URLonly = false)
1049 * @return string URL or link to documentation page, based on provided parameters
1052 static function docURL2($page, $URLonly = FALSE, $text = NULL, $title = NULL, $style = NULL, $resource = NULL) {
1053 // if ts function doesn't exist, it means that CiviCRM hasn't been fully initialised yet -
1054 // return just the URL, no matter what other parameters are defined
1055 if (!function_exists('ts')) {
1056 if ($resource == 'wiki') {
1057 $docBaseURL = self
::getWikiBaseURL();
1059 $docBaseURL = self
::getDocBaseURL();
1061 return $docBaseURL . str_replace(' ', '+', $page);
1066 'URLonly' => $URLonly,
1070 'resource' => $resource,
1072 return self
::docURL($params);
1077 * Returns URL or link to documentation page, based on provided parameters.
1078 * For use in templates code.
1080 * @param array $params An array of parameters (see CRM_Utils_System::docURL2 method for names)
1082 * @return string URL or link to documentation page, based on provided parameters
1085 static function docURL($params) {
1087 if (!isset($params['page'])) {
1091 if (CRM_Utils_Array
::value('resource', $params) == 'wiki') {
1092 $docBaseURL = self
::getWikiBaseURL();
1094 $docBaseURL = self
::getDocBaseURL();
1097 if (!isset($params['title']) or $params['title'] === NULL) {
1098 $params['title'] = ts('Opens documentation in a new window.');
1101 if (!isset($params['text']) or $params['text'] === NULL) {
1102 $params['text'] = ts('(learn more...)');
1105 if (!isset($params['style']) ||
$params['style'] === NULL) {
1109 $style = "style=\"{$params['style']}\"";
1112 $link = $docBaseURL . str_replace(' ', '+', $params['page']);
1114 if (isset($params['URLonly']) && $params['URLonly'] == TRUE) {
1118 return "<a href=\"{$link}\" $style target=\"_blank\" title=\"{$params['title']}\">{$params['text']}</a>";
1123 * Get the locale set in the hosting CMS
1125 * @return string the used locale or null for none
1127 static function getUFLocale() {
1128 $config = CRM_Core_Config
::singleton();
1129 return $config->userSystem
->getUFLocale();
1133 * Execute external or internal urls and return server response
1135 * @param string $url request url
1136 * @param boolean $addCookie should be true to access internal urls
1138 * @return string $response response from url
1141 static function getServerResponse($url, $addCookie = TRUE) {
1142 CRM_Core_Error
::ignoreException();
1143 require_once 'HTTP/Request.php';
1144 $request = new HTTP_Request($url);
1147 foreach ($_COOKIE as $name => $value) {
1148 $request->addCookie($name, $value);
1152 if (isset($_SERVER['AUTH_TYPE'])) {
1153 $request->setBasicAuth($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
1156 $config = CRM_Core_Config
::singleton();
1157 if ($config->userFramework
== 'WordPress') {
1158 session_write_close();
1161 $request->sendRequest();
1162 $response = $request->getResponseBody();
1164 CRM_Core_Error
::setCallback();
1168 static function isDBVersionValid(&$errorMessage) {
1169 $dbVersion = CRM_Core_BAO_Domain
::version();
1172 // if db.ver missing
1173 $errorMessage = ts('Version information found to be missing in database. You will need to determine the correct version corresponding to your current database state.');
1176 elseif (!CRM_Utils_System
::isVersionFormatValid($dbVersion)) {
1177 $errorMessage = ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.');
1180 elseif (stripos($dbVersion, 'upgrade')) {
1181 // if db.ver indicates a partially upgraded db
1182 $upgradeUrl = CRM_Utils_System
::url("civicrm/upgrade", "reset=1");
1183 $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));
1187 $codeVersion = CRM_Utils_System
::version();
1189 // if db.ver < code.ver, time to upgrade
1190 if (version_compare($dbVersion, $codeVersion) < 0) {
1191 $upgradeUrl = CRM_Utils_System
::url("civicrm/upgrade", "reset=1");
1192 $errorMessage = ts('New codebase version detected. You might want to visit <a href=\'%1\'>upgrade screen</a> to upgrade the database.', array(1 => $upgradeUrl));
1196 // if db.ver > code.ver, sth really wrong
1197 if (version_compare($dbVersion, $codeVersion) > 0) {
1198 $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.',
1199 array(1 => $dbVersion, 2 => $codeVersion)
1201 $errorMessage .= "<p>" . ts('OR if this is a manual install from git, you might want to fix civicrm-version.php file.') . "</p>";
1205 // FIXME: there should be another check to make sure version is in valid format - X.Y.alpha_num
1210 static function civiExit($status = 0) {
1211 // move things to CiviCRM cache as needed
1212 CRM_Core_Session
::storeSessionObjects();
1218 * Reset the various system caches and some important
1221 static function flushCache( ) {
1222 // flush out all cache entries so we can reload new data
1223 // a bit aggressive, but livable for now
1224 $cache = CRM_Utils_Cache
::singleton();
1227 // also reset the various static memory caches
1229 // reset the memory or array cache
1230 CRM_Core_BAO_Cache
::deleteGroup('contact fields', NULL, FALSE);
1233 CRM_ACL_BAO_Cache
::resetCache();
1235 // reset various static arrays used here
1236 CRM_Contact_BAO_Contact
::$_importableFields =
1237 CRM_Contact_BAO_Contact
::$_exportableFields =
1238 CRM_Contribute_BAO_Contribution
::$_importableFields =
1239 CRM_Contribute_BAO_Contribution
::$_exportableFields =
1240 CRM_Pledge_BAO_Pledge
::$_exportableFields =
1241 CRM_Contribute_BAO_Query
::$_contributionFields =
1242 CRM_Core_BAO_CustomField
::$_importFields =
1243 CRM_Core_DAO
::$_dbColumnValueCache = NULL;
1245 CRM_Core_OptionGroup
::flushAll();
1246 CRM_Utils_PseudoConstant
::flushAll();
1250 * load cms bootstrap
1252 * @param $params array with uid name and pass
1253 * @param $loadUser boolean load user or not
1255 static function loadBootStrap($params = array(
1256 ), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) {
1257 if (!is_array($params)) {
1260 $config = CRM_Core_Config
::singleton();
1261 return $config->userSystem
->loadBootStrap($params, $loadUser, $throwError, $realPath);
1265 * check is user logged in.
1269 public static function isUserLoggedIn() {
1270 $config = CRM_Core_Config
::singleton();
1271 return $config->userSystem
->isUserLoggedIn();
1275 * Get current logged in user id.
1277 * @return int ufId, currently logged in user uf id.
1279 public static function getLoggedInUfID() {
1280 $config = CRM_Core_Config
::singleton();
1281 return $config->userSystem
->getLoggedInUfID();
1284 static function baseCMSURL() {
1285 static $_baseURL = NULL;
1287 $config = CRM_Core_Config
::singleton();
1288 $_baseURL = $userFrameworkBaseURL = $config->userFrameworkBaseURL
;
1290 if ($config->userFramework
== 'Joomla') {
1292 // we need to remove the administrator/ from the end
1293 $_baseURL = str_replace("/administrator/", "/", $userFrameworkBaseURL);
1297 global $civicrm_root;
1298 if (strpos($civicrm_root,
1299 DIRECTORY_SEPARATOR
. 'sites' .
1300 DIRECTORY_SEPARATOR
. 'all' .
1301 DIRECTORY_SEPARATOR
. 'modules'
1303 $startPos = strpos($civicrm_root,
1304 DIRECTORY_SEPARATOR
. 'sites' . DIRECTORY_SEPARATOR
1306 $endPos = strpos($civicrm_root,
1307 DIRECTORY_SEPARATOR
. 'modules' . DIRECTORY_SEPARATOR
1309 if ($startPos && $endPos) {
1310 // if component is in sites/SITENAME/modules
1311 $siteName = substr($civicrm_root,
1313 $endPos - $startPos - 7
1316 $_baseURL = $userFrameworkBaseURL . "sites/$siteName/";
1324 static function relativeURL($url) {
1325 // check if url is relative, if so return immediately
1326 if (substr($url, 0, 4) != 'http') {
1330 // make everything relative from the baseFilePath
1331 $baseURL = self
::baseCMSURL();
1333 // check if baseURL is a substr of $url, if so
1334 // return rest of string
1335 if (substr($url, 0, strlen($baseURL)) == $baseURL) {
1336 return substr($url, strlen($baseURL));
1339 // return the original value
1343 static function absoluteURL($url, $removeLanguagePart = FALSE) {
1344 // check if url is already absolute, if so return immediately
1345 if (substr($url, 0, 4) == 'http') {
1349 // make everything absolute from the baseFileURL
1350 $baseURL = self
::baseCMSURL();
1352 //CRM-7622: drop the language from the URL if requested (and it’s there)
1353 $config = CRM_Core_Config
::singleton();
1354 if ($removeLanguagePart) {
1355 $baseURL = self
::languageNegotiationURL($baseURL, FALSE, TRUE);
1358 return $baseURL . $url;
1362 * Function to clean url, replaces first '&' with '?'
1364 * @param string $url
1366 * @return string $url, clean url
1369 static function cleanUrl($url) {
1374 if ($pos = strpos($url, '&')) {
1375 $url = substr_replace($url, '?', $pos, 1);
1382 * Format the url as per language Negotiation.
1384 * @param string $url
1386 * @return string $url, formatted url.
1389 static function languageNegotiationURL($url,
1390 $addLanguagePart = TRUE,
1391 $removeLanguagePart = FALSE
1393 $config = &CRM_Core_Config
::singleton();
1394 return $config->userSystem
->languageNegotiationURL($url, $addLanguagePart, $removeLanguagePart);
1398 * Append the contents of an 'extra' smarty template file if it is present in
1399 * the custom template directory. This does not work if there are
1400 * multiple custom template directories
1402 * @param string $fileName - the name of the tpl file that we are processing
1403 * @param string $content (by reference) - the current content string
1404 * @param string $overideFileName - an optional parameter which is sent by contribution/event reg/profile pages
1405 * which uses a id specific extra file name if present
1407 * @return void - the content string is modified if needed
1410 static function appendTPLFile($fileName,
1412 $overideFileName = NULL
1414 $template = CRM_Core_Smarty
::singleton();
1415 if ($overideFileName) {
1416 $additionalTPLFile = $overideFileName;
1419 $additionalTPLFile = str_replace('.tpl', '.extra.tpl', $fileName);
1422 if ($template->template_exists($additionalTPLFile)) {
1423 $content .= $template->fetch($additionalTPLFile);
1428 * Get a list of all files that are found within the directories
1429 * that are the result of appending the provided relative path to
1430 * each component of the PHP include path.
1432 * @author Ken Zalewski
1434 * @param string $relpath a relative path, typically pointing to
1435 * a directory with multiple class files
1437 * @return array An array of files that exist in one or more of the
1438 * directories that are referenced by the relative path
1439 * when appended to each element of the PHP include path
1442 static function listIncludeFiles($relpath) {
1443 $file_list = array();
1444 $inc_dirs = explode(PATH_SEPARATOR
, get_include_path());
1445 foreach ($inc_dirs as $inc_dir) {
1446 $target_dir = $inc_dir . DIRECTORY_SEPARATOR
. $relpath;
1447 if (is_dir($target_dir)) {
1448 $cur_list = scandir($target_dir);
1449 foreach ($cur_list as $fname) {
1450 if ($fname != '.' && $fname != '..') {
1451 $file_list[$fname] = $fname;
1458 // listIncludeFiles()
1461 * Get a list of all "plugins" (PHP classes that implement a piece of
1462 * functionality using a well-defined interface) that are found in a
1463 * particular CiviCRM directory (both custom and core are searched).
1465 * @author Ken Zalewski
1467 * @param string $relpath a relative path referencing a directory that
1468 * contains one or more plugins
1469 * @param string $fext only files with this extension will be considered
1471 * @param array $skipList list of files to skip
1473 * @return array List of plugins, where the plugin name is both the
1474 * key and the value of each element.
1477 static function getPluginList($relpath, $fext = '.php', $skipList = array(
1479 $fext_len = strlen($fext);
1481 $inc_files = CRM_Utils_System
::listIncludeFiles($relpath);
1482 foreach ($inc_files as $inc_file) {
1483 if (substr($inc_file, 0 - $fext_len) == $fext) {
1484 $plugin_name = substr($inc_file, 0, 0 - $fext_len);
1485 if (!in_array($plugin_name, $skipList)) {
1486 $plugins[$plugin_name] = $plugin_name;
1496 * @param string $fileName - the name of the tpl file that we are processing
1497 * @param string $content (by reference) - the current content string
1499 * @return void - the content string is modified if needed
1502 static function executeScheduledJobs() {
1503 $facility = new CRM_Core_JobManager();
1504 $facility->execute(FALSE);
1506 $redirectUrl = self
::url('civicrm/admin/job', 'reset=1');
1508 CRM_Core_Session
::setStatus(
1509 ts('Scheduled jobs have been executed according to individual timing settings. Please check log for messages.'),
1510 ts('Complete'), 'success');
1512 CRM_Utils_System
::redirect($redirectUrl);
1516 * Evaluate any tokens in a URL
1518 * @param string|FALSE $url
1519 * @return string|FALSE
1521 public static function evalUrl($url) {
1522 if ($url === FALSE) {
1526 $config = CRM_Core_Config
::singleton();
1528 '{ver}' => CRM_Utils_System
::version(),
1529 '{uf}' => $config->userFramework
,
1530 '{php}' => phpversion(),
1531 '{sid}' => md5('sid_' . (defined('CIVICRM_SITE_KEY') ? CIVICRM_SITE_KEY
: '') . '_' . $config->userFrameworkBaseURL
),
1532 '{baseUrl}' => $config->userFrameworkBaseURL
,
1533 '{lang}' => $config->lcMessages
,
1534 '{co}' => $config->defaultContactCountry
,
1536 foreach (array_keys($vars) as $k) {
1537 $vars[$k] = urlencode($vars[$k]);
1539 return strtr($url, $vars);
1545 * Determine whether this is a developmental system.
1549 static function isDevelopment() {
1550 static $cache = NULL;
1551 if ($cache === NULL) {
1552 global $civicrm_root;
1553 $cache = file_exists("{$civicrm_root}/.svn") ||
file_exists("{$civicrm_root}/.git");