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