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