0bad37a6818bb30be509349109f0f5de33c85eb9
[civicrm-core.git] / CRM / Core / Invoke.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 */
49 public static function invoke($args) {
50 try {
51 return self::_invoke($args);
52 }
53
54 catch (Exception $e) {
55 return CRM_Core_Error::handleUnhandledException($e);
56 }
57 }
58
59 /**
60 * @param $args
61 */
62 protected static function _invoke($args) {
63 if ($args[0] !== 'civicrm') {
64 return;
65 }
66
67 if (!defined('CIVICRM_SYMFONY_PATH')) {
68 try {
69 // Traditional Civi invocation path
70 self::hackMenuRebuild($args); // may exit
71 self::init($args);
72 self::hackStandalone($args);
73 $item = self::getItem($args);
74 return self::runItem($item);
75 }
76 catch (CRM_Core_EXCEPTION $e) {
77 $params = $e->getErrorData();
78 $message = $e->getMessage();
79 if (isset($params['legacy_status_bounce'])) {
80 //@todo remove this- see comments on
81 //https://github.com/eileenmcnaughton/civicrm-core/commit/ae686b09e2c987091612bb25ba0a58e520a203e7
82 CRM_Core_Error::statusBounce($params['message']);
83 }
84 else {
85 $session = CRM_Core_Session::singleton();
86 $session->setStatus(
87 $message,
88 CRM_Utils_Array::value('message_title', $params),
89 CRM_Utils_Array::value('message_type', $params, 'error')
90 );
91
92 // @todo remove this code - legacy redirect path is an interim measure for moving redirects out of BAO
93 // to somewhere slightly more acceptable. they should not be part of the exception class & should
94 // be managed @ the form level - if you find a form that is triggering this piece of code
95 // you should log a ticket for it to be removed with details about the form you were on.
96 if(!empty($params['legacy_redirect_path'])) {
97 if(CRM_Utils_System::isDevelopment()) {
98 // here we could set a message telling devs to log it per above
99 }
100 CRM_Utils_System::redirect($params['legacy_redirect_path'], $params['legacy_redirect_query']);
101 }
102 }
103 }
104 catch (Exception $e) {
105 // Recall: CRM_Core_Config is initialized before calling CRM_Core_Invoke
106 $config = CRM_Core_Config::singleton();
107 return CRM_Core_Error::handleUnhandledException($e);
108 /*
109 if ($config->backtrace) {
110 return CRM_Core_Error::formatHtmlException($e);
111 } else {
112 // TODO
113 }*/
114 }
115 } else {
116 // Symfony-based invocation path
117 require_once CIVICRM_SYMFONY_PATH . '/app/bootstrap.php.cache';
118 require_once CIVICRM_SYMFONY_PATH . '/app/AppKernel.php';
119 $kernel = new AppKernel('dev', true);
120 $kernel->loadClassCache();
121 $response = $kernel->handle(Symfony\Component\HttpFoundation\Request::createFromGlobals());
122 if (preg_match(':^text/html:', $response->headers->get('Content-Type'))) {
123 // let the CMS handle the trappings
124 return $response->getContent();
125 } else {
126 $response->send();
127 exit();
128 }
129 }
130 }
131 /**
132 * Hackish support /civicrm/menu/rebuild
133 *
134 * @param array $args list of path parts
135 * @void
136 */
137 static public function hackMenuRebuild($args) {
138 if (array('civicrm','menu','rebuild') == $args || array('civicrm', 'clearcache') == $args) {
139 // ensure that the user has a good privilege level
140 if (CRM_Core_Permission::check('administer CiviCRM')) {
141 self::rebuildMenuAndCaches();
142 CRM_Core_Session::setStatus(ts('Cleared all CiviCRM caches (database, menu, templates)'), ts('Complete'), 'success');
143 return CRM_Utils_System::redirect(); // exits
144 }
145 else {
146 CRM_Core_Error::fatal('You do not have permission to execute this url');
147 }
148 }
149 }
150
151 /**
152 * Perform general setup
153 *
154 * @param array $args list of path parts
155 * @void
156 */
157 static public function init($args) {
158 // first fire up IDS and check for bad stuff
159 $config = CRM_Core_Config::singleton();
160 if (!CRM_Core_Permission::check('skip IDS check')) {
161 $ids = new CRM_Core_IDS();
162 $ids->check($args);
163 }
164
165 // also initialize the i18n framework
166 require_once 'CRM/Core/I18n.php';
167 $i18n = CRM_Core_I18n::singleton();
168 }
169
170 /**
171 * Hackish support for /standalone/*
172 *
173 * @param array $args list of path parts
174 * @void
175 */
176 static public function hackStandalone($args) {
177 $config = CRM_Core_Config::singleton();
178 if ($config->userFramework == 'Standalone') {
179 $session = CRM_Core_Session::singleton();
180 if ($session->get('new_install') !== TRUE) {
181 CRM_Core_Standalone::sidebarLeft();
182 }
183 elseif ($args[1] == 'standalone' && $args[2] == 'register') {
184 CRM_Core_Menu::store();
185 }
186 }
187 }
188
189 /**
190 * Determine which menu $item corresponds to $args
191 *
192 * @param array $args list of path parts
193 * @return array; see CRM_Core_Menu
194 */
195 static public function getItem($args) {
196 if (is_array($args)) {
197 // get the menu items
198 $path = implode('/', $args);
199 } else {
200 $path = $args;
201 }
202 $item = CRM_Core_Menu::get($path);
203
204 // we should try to compute menus, if item is empty and stay on the same page,
205 // rather than compute and redirect to dashboard.
206 if (!$item) {
207 CRM_Core_Menu::store(FALSE);
208 $item = CRM_Core_Menu::get($path);
209 }
210
211 return $item;
212 }
213
214 /**
215 * Given a menu item, call the appropriate controller and return the response
216 *
217 * @param array $item see CRM_Core_Menu
218 * @return string, HTML
219 */
220 static public function runItem($item) {
221 $config = CRM_Core_Config::singleton();
222 if ($config->userFramework == 'Joomla' && $item) {
223 $config->userFrameworkURLVar = 'task';
224
225 // joomla 1.5RC1 seems to push this in the POST variable, which messes
226 // QF and checkboxes
227 unset($_POST['option']);
228 CRM_Core_Joomla::sidebarLeft();
229 }
230
231 // set active Component
232 $template = CRM_Core_Smarty::singleton();
233 $template->assign('activeComponent', 'CiviCRM');
234 $template->assign('formTpl', 'default');
235
236 if ($item) {
237 // CRM-7656 - make sure we send a clean sanitized path to create printer friendly url
238 $printerFriendly = CRM_Utils_System::makeURL(
239 'snippet', FALSE, FALSE,
240 CRM_Utils_Array::value('path', $item)
241 ) . '2';
242 $template->assign('printerFriendly', $printerFriendly);
243
244 if (!array_key_exists('page_callback', $item)) {
245 CRM_Core_Error::debug('Bad item', $item);
246 CRM_Core_Error::fatal(ts('Bad menu record in database'));
247 }
248
249 // check that we are permissioned to access this page
250 if (!CRM_Core_Permission::checkMenuItem($item)) {
251 CRM_Utils_System::permissionDenied();
252 return;
253 }
254
255 // check if ssl is set
256 if (!empty($item['is_ssl'])) {
257 CRM_Utils_System::redirectToSSL();
258 }
259
260 if (isset($item['title'])) {
261 CRM_Utils_System::setTitle($item['title']);
262 }
263
264 if (isset($item['breadcrumb']) && !isset($item['is_public'])) {
265 CRM_Utils_System::appendBreadCrumb($item['breadcrumb']);
266 }
267
268 $pageArgs = NULL;
269 if (!empty($item['page_arguments'])) {
270 $pageArgs = CRM_Core_Menu::getArrayForPathArgs($item['page_arguments']);
271 }
272
273 $template = CRM_Core_Smarty::singleton();
274 if (!empty($item['is_public'])) {
275 $template->assign('urlIsPublic', TRUE);
276 }
277 else {
278 $template->assign('urlIsPublic', FALSE);
279 self::versionCheck($template);
280 }
281
282 if (isset($item['return_url'])) {
283 $session = CRM_Core_Session::singleton();
284 $args = CRM_Utils_Array::value(
285 'return_url_args',
286 $item,
287 'reset=1'
288 );
289 $session->pushUserContext(CRM_Utils_System::url($item['return_url'], $args));
290 }
291
292 $result = NULL;
293 if (is_array($item['page_callback'])) {
294 require_once (str_replace('_', DIRECTORY_SEPARATOR, $item['page_callback'][0]) . '.php');
295 $result = call_user_func($item['page_callback']);
296 }
297 elseif (strstr($item['page_callback'], '_Form')) {
298 $wrapper = new CRM_Utils_Wrapper();
299 $result = $wrapper->run(
300 CRM_Utils_Array::value('page_callback', $item),
301 CRM_Utils_Array::value('title', $item),
302 isset($pageArgs) ? $pageArgs : NULL
303 );
304 }
305 else {
306 $newArgs = explode('/', $_GET[$config->userFrameworkURLVar]);
307 require_once (str_replace('_', DIRECTORY_SEPARATOR, $item['page_callback']) . '.php');
308 $mode = 'null';
309 if (isset($pageArgs['mode'])) {
310 $mode = $pageArgs['mode'];
311 unset($pageArgs['mode']);
312 }
313 $title = CRM_Utils_Array::value('title', $item);
314 if (strstr($item['page_callback'], '_Page')) {
315 $object = new $item['page_callback'] ($title, $mode );
316 $object->urlPath = explode('/', $_GET[$config->userFrameworkURLVar]);
317 }
318 elseif (strstr($item['page_callback'], '_Controller')) {
319 $addSequence = 'false';
320 if (isset($pageArgs['addSequence'])) {
321 $addSequence = $pageArgs['addSequence'];
322 $addSequence = $addSequence ? 'true' : 'false';
323 unset($pageArgs['addSequence']);
324 }
325 $object = new $item['page_callback'] ($title, true, $mode, null, $addSequence );
326 }
327 else {
328 CRM_Core_Error::fatal();
329 }
330 $result = $object->run($newArgs, $pageArgs);
331 }
332
333 CRM_Core_Session::storeSessionObjects();
334 return $result;
335 }
336
337 CRM_Core_Menu::store();
338 CRM_Core_Session::setStatus(ts('Menu has been rebuilt'), ts('Complete'), 'success');
339 return CRM_Utils_System::redirect();
340 }
341
342 /**
343 * This function contains the default action
344 *
345 * @param $action
346 *
347 * @param $contact_type
348 * @param $contact_sub_type
349 *
350 * @static
351 */
352 public static function form($action, $contact_type, $contact_sub_type) {
353 CRM_Utils_System::setUserContext(array('civicrm/contact/search/basic', 'civicrm/contact/view'));
354 $wrapper = new CRM_Utils_Wrapper();
355
356 $properties = CRM_Core_Component::contactSubTypeProperties($contact_sub_type, 'Edit');
357 if ($properties) {
358 $wrapper->run($properties['class'], ts('New %1', array(1 => $contact_sub_type)), $action, TRUE);
359 }
360 else {
361 $wrapper->run('CRM_Contact_Form_Contact', ts('New Contact'), $action, TRUE);
362 }
363 }
364
365 /**
366 * Show the message about CiviCRM versions
367 *
368 * @param CRM_Core_Smarty $template
369 */
370 public static function versionCheck($template) {
371 if (CRM_Core_Config::isUpgradeMode()) {
372 return;
373 }
374 $newerVersion = $securityUpdate = NULL;
375 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'versionAlert', NULL, 1) & 1) {
376 $newerVersion = CRM_Utils_VersionCheck::singleton()->isNewerVersionAvailable();
377 }
378 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'securityUpdateAlert', NULL, 3) & 1) {
379 $securityUpdate = CRM_Utils_VersionCheck::singleton()->isSecurityUpdateAvailable();
380 }
381 $template->assign('newer_civicrm_version', $newerVersion);
382 $template->assign('security_update', $securityUpdate);
383 }
384
385 /**
386 * @param bool $triggerRebuild
387 * @param bool $sessionReset
388 *
389 * @throws Exception
390 */
391 public static function rebuildMenuAndCaches($triggerRebuild = FALSE, $sessionReset = FALSE) {
392 $config = CRM_Core_Config::singleton();
393 $config->clearModuleList();
394
395 // also cleanup all caches
396 $config->cleanupCaches($sessionReset || CRM_Utils_Request::retrieve('sessionReset', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET'));
397
398 CRM_Core_Menu::store();
399
400 // also reset navigation
401 CRM_Core_BAO_Navigation::resetNavigation();
402
403 // also cleanup module permissions
404 $config->cleanupPermissions();
405
406 // rebuild word replacement cache - pass false to prevent operations redundant with this fn
407 CRM_Core_BAO_WordReplacement::rebuild(FALSE);
408
409 CRM_Core_BAO_Setting::updateSettingsFromMetaData();
410 // Clear js caches
411 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
412 CRM_Case_XMLRepository::singleton(TRUE);
413
414 // also rebuild triggers if requested explicitly
415 if (
416 $triggerRebuild ||
417 CRM_Utils_Request::retrieve('triggerRebuild', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET')
418 ) {
419 CRM_Core_DAO::triggerRebuild();
420 }
421 CRM_Core_DAO_AllCoreTables::reinitializeCache(TRUE);
422 CRM_Core_ManagedEntities::singleton(TRUE)->reconcile();
423 }
424 }