Merge pull request #2159 from lcdservices/CRM-13909
[civicrm-core.git] / CRM / Core / Invoke.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * Given an argument list, invoke the appropriate CRM function
31 * Serves as a wrapper between the UserFrameWork and Core CRM
32 *
33 * @package CRM
34 * @copyright CiviCRM LLC (c) 2004-2013
35 * $Id$
36 *
37 */
38 class CRM_Core_Invoke {
39
40 /**
41 * This is the main function that is called on every click action and based on the argument
42 * respective functions are called
43 *
44 * @param $args array this array contains the arguments of the url
45 * @return string, HTML
46 *
47 * @static
48 * @access public
49 */
50 static function invoke($args) {
51 try {
52 return self::_invoke($args);
53 }
54
55 catch (Exception $e) {
56 return CRM_Core_Error::handleUnhandledException($e);
57 }
58 }
59
60 protected static function _invoke($args) {
61 if ($args[0] !== 'civicrm') {
62 return;
63 }
64
65 if (!defined('CIVICRM_SYMFONY_PATH')) {
66 try {
67 // Traditional Civi invocation path
68 self::hackMenuRebuild($args); // may exit
69 self::init($args);
70 self::hackStandalone($args);
71 $item = self::getItem($args);
72 return self::runItem($item);
73 }
74 catch (CRM_Core_EXCEPTION $e) {
75 $params = $e->getErrorData();
76 $message = $e->getMessage();
77 if (isset($params['legacy_status_bounce'])) {
78 //@todo remove this- see comments on
79 //https://github.com/eileenmcnaughton/civicrm-core/commit/ae686b09e2c987091612bb25ba0a58e520a203e7
80 CRM_Core_Error::statusBounce($params['message']);
81 }
82 else {
83 $session = CRM_Core_Session::singleton();
84 $session->setStatus(
85 $message,
86 CRM_Utils_Array::value('message_title', $params),
87 CRM_Utils_Array::value('message_type', $params, 'error')
88 );
89
90 // @todo remove this code - legacy redirect path is an interim measure for moving redirects out of BAO
91 // to somewhere slightly more acceptable. they should not be part of the exception class & should
92 // be managed @ the form level - if you find a form that is triggering this piece of code
93 // you should log a ticket for it to be removed with details about the form you were on.
94 if(!empty($params['legacy_redirect_path'])) {
95 if(CRM_Utils_System::isDevelopment()) {
96 // here we could set a message telling devs to log it per above
97 }
98 CRM_Utils_System::redirect($params['legacy_redirect_path'], $params['legacy_redirect_query']);
99 }
100 }
101 }
102 catch (Exception $e) {
103 // Recall: CRM_Core_Config is initialized before calling CRM_Core_Invoke
104 $config = CRM_Core_Config::singleton();
105 return CRM_Core_Error::handleUnhandledException($e);
106 /*
107 if ($config->backtrace) {
108 return CRM_Core_Error::formatHtmlException($e);
109 } else {
110 // TODO
111 }*/
112 }
113 } else {
114 // Symfony-based invocation path
115 require_once CIVICRM_SYMFONY_PATH . '/app/bootstrap.php.cache';
116 require_once CIVICRM_SYMFONY_PATH . '/app/AppKernel.php';
117 $kernel = new AppKernel('dev', true);
118 $kernel->loadClassCache();
119 $response = $kernel->handle(Symfony\Component\HttpFoundation\Request::createFromGlobals());
120 // $response->send();
121 return $response->getContent();
122 }
123 }
124 /**
125 * Hackish support /civicrm/menu/rebuild
126 *
127 * @param array $args list of path parts
128 * @void
129 */
130 static public function hackMenuRebuild($args) {
131 if (array('civicrm','menu','rebuild') == $args || array('civicrm', 'clearcache') == $args) {
132 // ensure that the user has a good privilege level
133 if (CRM_Core_Permission::check('administer CiviCRM')) {
134 self::rebuildMenuAndCaches();
135 CRM_Core_Session::setStatus(ts('Cleared all CiviCRM caches (database, menu, templates)'), ts('Complete'), 'success');
136 return CRM_Utils_System::redirect(); // exits
137 }
138 else {
139 CRM_Core_Error::fatal('You do not have permission to execute this url');
140 }
141 }
142 }
143
144 /**
145 * Perform general setup
146 *
147 * @param array $args list of path parts
148 * @void
149 */
150 static public function init($args) {
151 // first fire up IDS and check for bad stuff
152 $config = CRM_Core_Config::singleton();
153 if (!CRM_Core_Permission::check('skip IDS check')) {
154 $ids = new CRM_Core_IDS();
155 $ids->check($args);
156 }
157
158 // also initialize the i18n framework
159 require_once 'CRM/Core/I18n.php';
160 $i18n = CRM_Core_I18n::singleton();
161 }
162
163 /**
164 * Hackish support for /standalone/*
165 *
166 * @param array $args list of path parts
167 * @void
168 */
169 static public function hackStandalone($args) {
170 $config = CRM_Core_Config::singleton();
171 if ($config->userFramework == 'Standalone') {
172 $session = CRM_Core_Session::singleton();
173 if ($session->get('new_install') !== TRUE) {
174 CRM_Core_Standalone::sidebarLeft();
175 }
176 elseif ($args[1] == 'standalone' && $args[2] == 'register') {
177 CRM_Core_Menu::store();
178 }
179 }
180 }
181
182 /**
183 * Determine which menu $item corresponds to $args
184 *
185 * @param array $args list of path parts
186 * @return array; see CRM_Core_Menu
187 */
188 static public function getItem($args) {
189 if (is_array($args)) {
190 // get the menu items
191 $path = implode('/', $args);
192 } else {
193 $path = $args;
194 }
195 $item = CRM_Core_Menu::get($path);
196
197 // we should try to compute menus, if item is empty and stay on the same page,
198 // rather than compute and redirect to dashboard.
199 if (!$item) {
200 CRM_Core_Menu::store(FALSE);
201 $item = CRM_Core_Menu::get($path);
202 }
203
204 return $item;
205 }
206
207 /**
208 * Given a menu item, call the appropriate controller and return the response
209 *
210 * @param array $item see CRM_Core_Menu
211 * @return string, HTML
212 */
213 static public function runItem($item) {
214 $config = CRM_Core_Config::singleton();
215 if ($config->userFramework == 'Joomla' && $item) {
216 $config->userFrameworkURLVar = 'task';
217
218 // joomla 1.5RC1 seems to push this in the POST variable, which messes
219 // QF and checkboxes
220 unset($_POST['option']);
221 CRM_Core_Joomla::sidebarLeft();
222 }
223
224 // set active Component
225 $template = CRM_Core_Smarty::singleton();
226 $template->assign('activeComponent', 'CiviCRM');
227 $template->assign('formTpl', 'default');
228
229 if ($item) {
230 // CRM-7656 - make sure we send a clean sanitized path to create printer friendly url
231 $printerFriendly = CRM_Utils_System::makeURL(
232 'snippet', FALSE, FALSE,
233 CRM_Utils_Array::value('path', $item)
234 ) . '2';
235 $template->assign('printerFriendly', $printerFriendly);
236
237 if (!array_key_exists('page_callback', $item)) {
238 CRM_Core_Error::debug('Bad item', $item);
239 CRM_Core_Error::fatal(ts('Bad menu record in database'));
240 }
241
242 // check that we are permissioned to access this page
243 if (!CRM_Core_Permission::checkMenuItem($item)) {
244 CRM_Utils_System::permissionDenied();
245 return;
246 }
247
248 // check if ssl is set
249 if (CRM_Utils_Array::value('is_ssl', $item)) {
250 CRM_Utils_System::redirectToSSL();
251 }
252
253 if (isset($item['title'])) {
254 CRM_Utils_System::setTitle($item['title']);
255 }
256
257 if (isset($item['breadcrumb']) && !isset($item['is_public'])) {
258 CRM_Utils_System::appendBreadCrumb($item['breadcrumb']);
259 }
260
261 $pageArgs = NULL;
262 if (CRM_Utils_Array::value('page_arguments', $item)) {
263 $pageArgs = CRM_Core_Menu::getArrayForPathArgs($item['page_arguments']);
264 }
265
266 $template = CRM_Core_Smarty::singleton();
267 if (!empty($item['is_public'])) {
268 $template->assign('urlIsPublic', TRUE);
269 }
270 else {
271 $template->assign('urlIsPublic', FALSE);
272 self::versionCheck($template);
273 }
274
275 if (isset($item['return_url'])) {
276 $session = CRM_Core_Session::singleton();
277 $args = CRM_Utils_Array::value(
278 'return_url_args',
279 $item,
280 'reset=1'
281 );
282 $session->pushUserContext(CRM_Utils_System::url($item['return_url'], $args));
283 }
284
285 $result = NULL;
286 if (is_array($item['page_callback'])) {
287 $newArgs = explode('/', $_GET[$config->userFrameworkURLVar]);
288 require_once (str_replace('_', DIRECTORY_SEPARATOR, $item['page_callback'][0]) . '.php');
289 $result = call_user_func($item['page_callback'], $newArgs);
290 }
291 elseif (strstr($item['page_callback'], '_Form')) {
292 $wrapper = new CRM_Utils_Wrapper();
293 $result = $wrapper->run(
294 CRM_Utils_Array::value('page_callback', $item),
295 CRM_Utils_Array::value('title', $item),
296 isset($pageArgs) ? $pageArgs : NULL
297 );
298 }
299 else {
300 $newArgs = explode('/', $_GET[$config->userFrameworkURLVar]);
301 require_once (str_replace('_', DIRECTORY_SEPARATOR, $item['page_callback']) . '.php');
302 $mode = 'null';
303 if (isset($pageArgs['mode'])) {
304 $mode = $pageArgs['mode'];
305 unset($pageArgs['mode']);
306 }
307 $title = CRM_Utils_Array::value('title', $item);
308 if (strstr($item['page_callback'], '_Page')) {
309 $object = new $item['page_callback'] ($title, $mode );
310 }
311 elseif (strstr($item['page_callback'], '_Controller')) {
312 $addSequence = 'false';
313 if (isset($pageArgs['addSequence'])) {
314 $addSequence = $pageArgs['addSequence'];
315 $addSequence = $addSequence ? 'true' : 'false';
316 unset($pageArgs['addSequence']);
317 }
318 $object = new $item['page_callback'] ($title, true, $mode, null, $addSequence );
319 }
320 else {
321 CRM_Core_Error::fatal();
322 }
323 $result = $object->run($newArgs, $pageArgs);
324 }
325
326 CRM_Core_Session::storeSessionObjects();
327 return $result;
328 }
329
330 CRM_Core_Menu::store();
331 CRM_Core_Session::setStatus(ts('Menu has been rebuilt'), ts('Complete'), 'success');
332 return CRM_Utils_System::redirect();
333 }
334
335 /**
336 * This function contains the default action
337 *
338 * @param $action
339 *
340 * @static
341 * @access public
342 */
343 static function form($action, $contact_type, $contact_sub_type) {
344 CRM_Utils_System::setUserContext(array('civicrm/contact/search/basic', 'civicrm/contact/view'));
345 $wrapper = new CRM_Utils_Wrapper();
346
347 $properties = CRM_Core_Component::contactSubTypeProperties($contact_sub_type, 'Edit');
348 if ($properties) {
349 $wrapper->run($properties['class'], ts('New %1', array(1 => $contact_sub_type)), $action, TRUE);
350 }
351 else {
352 $wrapper->run('CRM_Contact_Form_Contact', ts('New Contact'), $action, TRUE);
353 }
354 }
355
356 /**
357 * This function contains the actions for profile arguments
358 *
359 * @param $args array this array contains the arguments of the url
360 *
361 * @static
362 * @access public
363 */
364 static function profile($args) {
365 if ($args[1] !== 'profile') {
366 return;
367 }
368
369 $secondArg = CRM_Utils_Array::value(2, $args, '');
370
371 if ($secondArg == 'map') {
372 $controller = new CRM_Core_Controller_Simple(
373 'CRM_Contact_Form_Task_Map',
374 ts('Map Contact'),
375 NULL, FALSE, FALSE, TRUE
376 );
377
378 $gids = explode(',', CRM_Utils_Request::retrieve('gid', 'String', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET'));
379
380 if (count($gids) > 1) {
381 foreach ($gids as $pfId) {
382 $profileIds[] = CRM_Utils_Type::escape($pfId, 'Positive');
383 }
384 $controller->set('gid', $profileIds[0]);
385 $profileGID = $profileIds[0];
386 }
387 else {
388 $profileGID = CRM_Utils_Request::retrieve('gid', 'Integer', $controller, TRUE);
389 }
390
391
392 // make sure that this profile enables mapping
393 // CRM-8609
394 $isMap =
395 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileGID, 'is_map');
396 if (!$isMap) {
397 CRM_Core_Error::statusBounce(ts('This profile does not have the map feature turned on.'));
398 }
399
400 $profileView = CRM_Utils_Request::retrieve('pv', 'Integer', $controller, FALSE);
401
402 // set the userContext stack
403 $session = CRM_Core_Session::singleton();
404 if ($profileView) {
405 $session->pushUserContext(CRM_Utils_System::url('civicrm/profile/view'));
406 }
407 else {
408 $session->pushUserContext(CRM_Utils_System::url('civicrm/profile', 'force=1'));
409 }
410
411 $controller->set('profileGID', $profileGID);
412 $controller->process();
413 return $controller->run();
414 }
415
416 if ($secondArg == 'edit' || $secondArg == 'create') {
417 // set the userContext stack
418 $session = CRM_Core_Session::singleton();
419 $session->pushUserContext(CRM_Utils_System::url('civicrm/profile', 'reset=1'));
420
421 $buttonType = CRM_Utils_Array::value('_qf_Edit_cancel', $_POST);
422 // CRM-5849: we should actually check the button *type*, but we get the *value*, potentially translated;
423 // we should keep both English and translated checks just to make sure we also handle untranslated Cancels
424 if ($buttonType == 'Cancel' or $buttonType == ts('Cancel')) {
425 $cancelURL = CRM_Utils_Request::retrieve('cancelURL',
426 'String',
427 CRM_Core_DAO::$_nullObject,
428 FALSE,
429 NULL,
430 $_POST
431 );
432 if ($cancelURL) {
433 CRM_Utils_System::redirect($cancelURL);
434 }
435 }
436
437 if ($secondArg == 'edit') {
438 $controller = new CRM_Core_Controller_Simple('CRM_Profile_Form_Edit',
439 ts('Create Profile'),
440 CRM_Core_Action::UPDATE,
441 FALSE, FALSE, TRUE
442 );
443 $controller->set('edit', 1);
444 $controller->process();
445 return $controller->run();
446 }
447 else {
448 $wrapper = new CRM_Utils_Wrapper();
449 return $wrapper->run('CRM_Profile_Form_Edit',
450 ts('Create Profile'),
451 array(
452 'mode' => CRM_Core_Action::ADD,
453 'ignoreKey' => TRUE,
454 )
455 );
456 }
457 }
458
459 if ($secondArg == 'view' || empty($secondArg)) {
460 $page = new CRM_Profile_Page_Listings();
461 return $page->run();
462 }
463
464 CRM_Utils_System::permissionDenied();
465 return;
466 }
467
468 /**
469 * Show the message about CiviCRM versions
470 *
471 * @param obj: $template (reference)
472 */
473 static function versionCheck($template) {
474 if (CRM_Core_Config::isUpgradeMode()) {
475 return;
476 }
477 $versionCheck = CRM_Utils_VersionCheck::singleton();
478 $newerVersion = $versionCheck->newerVersion();
479 $template->assign('newer_civicrm_version', $newerVersion);
480 }
481
482 static function rebuildMenuAndCaches($triggerRebuild = FALSE, $sessionReset = FALSE) {
483 $config = CRM_Core_Config::singleton();
484 $config->clearModuleList();
485
486 CRM_Core_Menu::store();
487
488 // also reset navigation
489 CRM_Core_BAO_Navigation::resetNavigation();
490
491 // also cleanup all caches
492 $config->cleanupCaches($sessionReset || CRM_Utils_Request::retrieve('sessionReset', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET'));
493
494 // also cleanup module permissions
495 $config->cleanupPermissions();
496
497 // also rebuild word replacement cache
498 CRM_Core_BAO_WordReplacement::rebuild();
499
500 CRM_Core_BAO_Setting::updateSettingsFromMetaData();
501 CRM_Core_Resources::singleton()->resetCacheCode();
502
503 // also rebuild triggers if requested explicitly
504 if (
505 $triggerRebuild ||
506 CRM_Utils_Request::retrieve('triggerRebuild', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET')
507 ) {
508 CRM_Core_DAO::triggerRebuild();
509 }
510
511 CRM_Core_ManagedEntities::singleton(TRUE)->reconcile();
512 }
513 }
514