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