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