Merge pull request #1 from civicrm/master
[civicrm-core.git] / CRM / Core / Invoke.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * Given an argument list, invoke the appropriate CRM function
31 * Serves as a wrapper between the UserFrameWork and Core CRM
32 *
33 * @package CRM
6b83d5bd 34 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
35 */
36class CRM_Core_Invoke {
37
38 /**
be7dea3f
TO
39 * This is the main front-controller that integrates with the CMS. Any
40 * page-request that is sent to the CMS and intended for CiviCRM should
41 * be processed by invoke().
6a488035 42 *
be7dea3f
TO
43 * @param array $args
44 * The parts of the URL which identify the intended CiviCRM page
45 * (e.g. array('civicrm', 'event', 'register')).
46 * @return string
47 * HTML. For non-HTML content, invoke() may call print() and exit().
6a488035 48 *
6a488035 49 */
00be9182 50 public static function invoke($args) {
6a488035
TO
51 try {
52 return self::_invoke($args);
dcc4f6a7 53 }
dcc4f6a7 54 catch (Exception $e) {
be7dea3f 55 CRM_Core_Error::handleUnhandledException($e);
6a488035
TO
56 }
57 }
58
a0ee3941 59 /**
be7dea3f
TO
60 * This is the same as invoke(), but it does *not* include exception
61 * handling.
62 *
63 * @param array $args
64 * The parts of the URL which identify the intended CiviCRM page
65 * (e.g. array('civicrm', 'event', 'register')).
66 * @return string
67 * HTML. For non-HTML content, invoke() may call print() and exit().
a0ee3941 68 */
be7dea3f 69 public static function _invoke($args) {
6a488035 70 if ($args[0] !== 'civicrm') {
408b79bf 71 return NULL;
6a488035 72 }
1ebbf8bf
NG
73 // CRM-15901: Turn off PHP errors display for all ajax calls
74 if (CRM_Utils_Array::value(1, $args) == 'ajax' || CRM_Utils_Array::value('snippet', $_REQUEST)) {
75 ini_set('display_errors', 0);
76 }
6a488035
TO
77
78 if (!defined('CIVICRM_SYMFONY_PATH')) {
be7dea3f 79 // Traditional Civi invocation path
518fa0ee
SL
80 // may exit
81 self::hackMenuRebuild($args);
be7dea3f
TO
82 self::init($args);
83 self::hackStandalone($args);
84 $item = self::getItem($args);
85 return self::runItem($item);
0db6c3e1
TO
86 }
87 else {
6a488035
TO
88 // Symfony-based invocation path
89 require_once CIVICRM_SYMFONY_PATH . '/app/bootstrap.php.cache';
90 require_once CIVICRM_SYMFONY_PATH . '/app/AppKernel.php';
4eeb9a5b 91 $kernel = new AppKernel('dev', TRUE);
6a488035
TO
92 $kernel->loadClassCache();
93 $response = $kernel->handle(Symfony\Component\HttpFoundation\Request::createFromGlobals());
c24c4679
TO
94 if (preg_match(':^text/html:', $response->headers->get('Content-Type'))) {
95 // let the CMS handle the trappings
96 return $response->getContent();
0db6c3e1
TO
97 }
98 else {
c24c4679
TO
99 $response->send();
100 exit();
101 }
6a488035
TO
102 }
103 }
353ffa53 104
6a488035
TO
105 /**
106 * Hackish support /civicrm/menu/rebuild
107 *
6a0b768e
TO
108 * @param array $args
109 * List of path parts.
6a488035
TO
110 * @void
111 */
518fa0ee 112 public static function hackMenuRebuild($args) {
be2fb01f 113 if (['civicrm', 'menu', 'rebuild'] == $args || ['civicrm', 'clearcache'] == $args) {
6a488035
TO
114 // ensure that the user has a good privilege level
115 if (CRM_Core_Permission::check('administer CiviCRM')) {
116 self::rebuildMenuAndCaches();
117 CRM_Core_Session::setStatus(ts('Cleared all CiviCRM caches (database, menu, templates)'), ts('Complete'), 'success');
518fa0ee
SL
118 // exits
119 return CRM_Utils_System::redirect();
6a488035
TO
120 }
121 else {
122 CRM_Core_Error::fatal('You do not have permission to execute this url');
123 }
124 }
125 }
126
127 /**
d09edf64 128 * Perform general setup.
6a488035 129 *
6a0b768e
TO
130 * @param array $args
131 * List of path parts.
6a488035
TO
132 * @void
133 */
518fa0ee 134 public static function init($args) {
6a488035
TO
135 // first fire up IDS and check for bad stuff
136 $config = CRM_Core_Config::singleton();
6a488035
TO
137
138 // also initialize the i18n framework
139 require_once 'CRM/Core/I18n.php';
140 $i18n = CRM_Core_I18n::singleton();
141 }
142
143 /**
144 * Hackish support for /standalone/*
145 *
6a0b768e
TO
146 * @param array $args
147 * List of path parts.
6a488035
TO
148 * @void
149 */
518fa0ee 150 public static function hackStandalone($args) {
6a488035
TO
151 $config = CRM_Core_Config::singleton();
152 if ($config->userFramework == 'Standalone') {
153 $session = CRM_Core_Session::singleton();
154 if ($session->get('new_install') !== TRUE) {
155 CRM_Core_Standalone::sidebarLeft();
156 }
157 elseif ($args[1] == 'standalone' && $args[2] == 'register') {
158 CRM_Core_Menu::store();
159 }
160 }
161 }
162
163 /**
164 * Determine which menu $item corresponds to $args
165 *
6a0b768e
TO
166 * @param array $args
167 * List of path parts.
6a488035
TO
168 * @return array; see CRM_Core_Menu
169 */
518fa0ee 170 public static function getItem($args) {
6a488035
TO
171 if (is_array($args)) {
172 // get the menu items
173 $path = implode('/', $args);
0db6c3e1
TO
174 }
175 else {
6a488035
TO
176 $path = $args;
177 }
178 $item = CRM_Core_Menu::get($path);
179
180 // we should try to compute menus, if item is empty and stay on the same page,
181 // rather than compute and redirect to dashboard.
182 if (!$item) {
183 CRM_Core_Menu::store(FALSE);
184 $item = CRM_Core_Menu::get($path);
185 }
186
187 return $item;
188 }
189
190 /**
191 * Given a menu item, call the appropriate controller and return the response
192 *
6a0b768e
TO
193 * @param array $item
194 * See CRM_Core_Menu.
6a488035
TO
195 * @return string, HTML
196 */
518fa0ee 197 public static function runItem($item) {
76adcecc
TO
198 $ids = new CRM_Core_IDS();
199 $ids->check($item);
200
6a488035
TO
201 $config = CRM_Core_Config::singleton();
202 if ($config->userFramework == 'Joomla' && $item) {
203 $config->userFrameworkURLVar = 'task';
204
205 // joomla 1.5RC1 seems to push this in the POST variable, which messes
206 // QF and checkboxes
207 unset($_POST['option']);
208 CRM_Core_Joomla::sidebarLeft();
209 }
210
211 // set active Component
212 $template = CRM_Core_Smarty::singleton();
213 $template->assign('activeComponent', 'CiviCRM');
214 $template->assign('formTpl', 'default');
215
216 if ($item) {
217 // CRM-7656 - make sure we send a clean sanitized path to create printer friendly url
218 $printerFriendly = CRM_Utils_System::makeURL(
353ffa53
TO
219 'snippet', FALSE, FALSE,
220 CRM_Utils_Array::value('path', $item)
221 ) . '2';
6a488035
TO
222 $template->assign('printerFriendly', $printerFriendly);
223
224 if (!array_key_exists('page_callback', $item)) {
225 CRM_Core_Error::debug('Bad item', $item);
226 CRM_Core_Error::fatal(ts('Bad menu record in database'));
227 }
228
229 // check that we are permissioned to access this page
230 if (!CRM_Core_Permission::checkMenuItem($item)) {
231 CRM_Utils_System::permissionDenied();
408b79bf 232 return NULL;
6a488035
TO
233 }
234
235 // check if ssl is set
a7488080 236 if (!empty($item['is_ssl'])) {
6a488035
TO
237 CRM_Utils_System::redirectToSSL();
238 }
239
240 if (isset($item['title'])) {
241 CRM_Utils_System::setTitle($item['title']);
242 }
243
244 if (isset($item['breadcrumb']) && !isset($item['is_public'])) {
245 CRM_Utils_System::appendBreadCrumb($item['breadcrumb']);
246 }
247
248 $pageArgs = NULL;
a7488080 249 if (!empty($item['page_arguments'])) {
6a488035
TO
250 $pageArgs = CRM_Core_Menu::getArrayForPathArgs($item['page_arguments']);
251 }
252
253 $template = CRM_Core_Smarty::singleton();
254 if (!empty($item['is_public'])) {
255 $template->assign('urlIsPublic', TRUE);
256 }
257 else {
258 $template->assign('urlIsPublic', FALSE);
06576a03 259 self::statusCheck($template);
6a488035
TO
260 }
261
262 if (isset($item['return_url'])) {
263 $session = CRM_Core_Session::singleton();
264 $args = CRM_Utils_Array::value(
265 'return_url_args',
266 $item,
267 'reset=1'
268 );
269 $session->pushUserContext(CRM_Utils_System::url($item['return_url'], $args));
270 }
271
272 $result = NULL;
c8074a93
TO
273 // WISHLIST: Refactor this. Instead of pattern-matching on page_callback, lookup
274 // page_callback via Civi\Core\Resolver and check the implemented interfaces. This
275 // would require rethinking the default constructor.
276 if (is_array($item['page_callback']) || strpos($item['page_callback'], ':')) {
277 $result = call_user_func(Civi\Core\Resolver::singleton()->get($item['page_callback']));
6a488035
TO
278 }
279 elseif (strstr($item['page_callback'], '_Form')) {
280 $wrapper = new CRM_Utils_Wrapper();
281 $result = $wrapper->run(
282 CRM_Utils_Array::value('page_callback', $item),
283 CRM_Utils_Array::value('title', $item),
284 isset($pageArgs) ? $pageArgs : NULL
285 );
286 }
287 else {
288 $newArgs = explode('/', $_GET[$config->userFrameworkURLVar]);
6a488035
TO
289 $mode = 'null';
290 if (isset($pageArgs['mode'])) {
291 $mode = $pageArgs['mode'];
292 unset($pageArgs['mode']);
293 }
294 $title = CRM_Utils_Array::value('title', $item);
99218b4b 295 if (strstr($item['page_callback'], '_Page') || strstr($item['page_callback'], '\\Page\\')) {
408b79bf 296 $object = new $item['page_callback']($title, $mode);
6c2473d5 297 $object->urlPath = explode('/', $_GET[$config->userFrameworkURLVar]);
6a488035 298 }
99218b4b 299 elseif (strstr($item['page_callback'], '_Controller') || strstr($item['page_callback'], '\\Controller\\')) {
6a488035
TO
300 $addSequence = 'false';
301 if (isset($pageArgs['addSequence'])) {
302 $addSequence = $pageArgs['addSequence'];
303 $addSequence = $addSequence ? 'true' : 'false';
304 unset($pageArgs['addSequence']);
305 }
408b79bf 306 $object = new $item['page_callback']($title, TRUE, $mode, NULL, $addSequence);
6a488035
TO
307 }
308 else {
309 CRM_Core_Error::fatal();
310 }
311 $result = $object->run($newArgs, $pageArgs);
312 }
313
314 CRM_Core_Session::storeSessionObjects();
315 return $result;
316 }
317
318 CRM_Core_Menu::store();
319 CRM_Core_Session::setStatus(ts('Menu has been rebuilt'), ts('Complete'), 'success');
320 return CRM_Utils_System::redirect();
321 }
322
323 /**
d09edf64 324 * This function contains the default action.
6a488035
TO
325 *
326 * @param $action
327 *
77b97be7
EM
328 * @param $contact_type
329 * @param $contact_sub_type
330 *
6a488035 331 */
00be9182 332 public static function form($action, $contact_type, $contact_sub_type) {
be2fb01f 333 CRM_Utils_System::setUserContext(['civicrm/contact/search/basic', 'civicrm/contact/view']);
6a488035
TO
334 $wrapper = new CRM_Utils_Wrapper();
335
336 $properties = CRM_Core_Component::contactSubTypeProperties($contact_sub_type, 'Edit');
337 if ($properties) {
be2fb01f 338 $wrapper->run($properties['class'], ts('New %1', [1 => $contact_sub_type]), $action, TRUE);
6a488035
TO
339 }
340 else {
341 $wrapper->run('CRM_Contact_Form_Contact', ts('New Contact'), $action, TRUE);
342 }
343 }
344
6a488035 345 /**
f55dd135 346 * Show status in the footer (admin only)
6a488035 347 *
fa8dc18c 348 * @param CRM_Core_Smarty $template
6a488035 349 */
097c681e 350 public static function statusCheck($template) {
f55dd135 351 if (CRM_Core_Config::isUpgradeMode() || !CRM_Core_Permission::check('administer CiviCRM')) {
097c681e
AH
352 return;
353 }
f55dd135 354 // always use cached results - they will be refreshed by the session timer
b1fc1ab0 355 $status = Civi::cache('checks')->get('systemStatusCheckResult');
f55dd135 356 $template->assign('footer_status_severity', $status);
f608a24a 357 $template->assign('footer_status_message', CRM_Utils_Check::toStatusLabel($status));
097c681e 358 }
6a488035 359
a0ee3941
EM
360 /**
361 * @param bool $triggerRebuild
362 * @param bool $sessionReset
363 *
364 * @throws Exception
365 */
00be9182 366 public static function rebuildMenuAndCaches($triggerRebuild = FALSE, $sessionReset = FALSE) {
6a488035
TO
367 $config = CRM_Core_Config::singleton();
368 $config->clearModuleList();
369
ae2cab23
TO
370 // also cleanup all caches
371 $config->cleanupCaches($sessionReset || CRM_Utils_Request::retrieve('sessionReset', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET'));
372
6a488035
TO
373 CRM_Core_Menu::store();
374
375 // also reset navigation
376 CRM_Core_BAO_Navigation::resetNavigation();
377
6a488035
TO
378 // also cleanup module permissions
379 $config->cleanupPermissions();
380
9762f6ff
CW
381 // rebuild word replacement cache - pass false to prevent operations redundant with this fn
382 CRM_Core_BAO_WordReplacement::rebuild(FALSE);
76dca235 383
76bd16ab 384 Civi::service('settings_manager')->flush();
9762f6ff 385 // Clear js caches
4cc9b813 386 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
ab89fdde 387 CRM_Case_XMLRepository::singleton(TRUE);
1fcf16cc 388
6a488035
TO
389 // also rebuild triggers if requested explicitly
390 if (
391 $triggerRebuild ||
392 CRM_Utils_Request::retrieve('triggerRebuild', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET')
393 ) {
394 CRM_Core_DAO::triggerRebuild();
395 }
95a90cba 396 CRM_Core_DAO_AllCoreTables::reinitializeCache(TRUE);
6a488035
TO
397 CRM_Core_ManagedEntities::singleton(TRUE)->reconcile();
398 }
96025800 399
6a488035 400}