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