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