more comment fixes
[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 +--------------------------------------------------------------------+
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
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 50 *
6a488035 51 */
00be9182 52 public static function invoke($args) {
6a488035
TO
53 try {
54 return self::_invoke($args);
dcc4f6a7 55 }
dcc4f6a7 56 catch (Exception $e) {
be7dea3f 57 CRM_Core_Error::handleUnhandledException($e);
6a488035
TO
58 }
59 }
60
a0ee3941 61 /**
be7dea3f
TO
62 * This is the same as invoke(), but it does *not* include exception
63 * handling.
64 *
65 * @param array $args
66 * The parts of the URL which identify the intended CiviCRM page
67 * (e.g. array('civicrm', 'event', 'register')).
68 * @return string
69 * HTML. For non-HTML content, invoke() may call print() and exit().
a0ee3941 70 */
be7dea3f 71 public static function _invoke($args) {
6a488035 72 if ($args[0] !== 'civicrm') {
408b79bf 73 return NULL;
6a488035
TO
74 }
75
76 if (!defined('CIVICRM_SYMFONY_PATH')) {
be7dea3f
TO
77 // Traditional Civi invocation path
78 self::hackMenuRebuild($args); // may exit
79 self::init($args);
80 self::hackStandalone($args);
81 $item = self::getItem($args);
82 return self::runItem($item);
0db6c3e1
TO
83 }
84 else {
6a488035
TO
85 // Symfony-based invocation path
86 require_once CIVICRM_SYMFONY_PATH . '/app/bootstrap.php.cache';
87 require_once CIVICRM_SYMFONY_PATH . '/app/AppKernel.php';
4eeb9a5b 88 $kernel = new AppKernel('dev', TRUE);
6a488035
TO
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();
0db6c3e1
TO
94 }
95 else {
c24c4679
TO
96 $response->send();
97 exit();
98 }
6a488035
TO
99 }
100 }
353ffa53 101
6a488035
TO
102 /**
103 * Hackish support /civicrm/menu/rebuild
104 *
6a0b768e
TO
105 * @param array $args
106 * List of path parts.
6a488035
TO
107 * @void
108 */
109 static public function hackMenuRebuild($args) {
d3e86119 110 if (array('civicrm', 'menu', 'rebuild') == $args || array('civicrm', 'clearcache') == $args) {
6a488035
TO
111 // ensure that the user has a good privilege level
112 if (CRM_Core_Permission::check('administer CiviCRM')) {
113 self::rebuildMenuAndCaches();
114 CRM_Core_Session::setStatus(ts('Cleared all CiviCRM caches (database, menu, templates)'), ts('Complete'), 'success');
115 return CRM_Utils_System::redirect(); // exits
116 }
117 else {
118 CRM_Core_Error::fatal('You do not have permission to execute this url');
119 }
120 }
121 }
122
123 /**
124 * Perform general setup
125 *
6a0b768e
TO
126 * @param array $args
127 * List of path parts.
6a488035
TO
128 * @void
129 */
130 static public function init($args) {
131 // first fire up IDS and check for bad stuff
132 $config = CRM_Core_Config::singleton();
634e1a1a 133 if (!CRM_Core_Permission::check('skip IDS check')) {
6a488035
TO
134 $ids = new CRM_Core_IDS();
135 $ids->check($args);
136 }
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 */
150 static public function hackStandalone($args) {
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 */
170 static public function getItem($args) {
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 */
197 static public function runItem($item) {
198 $config = CRM_Core_Config::singleton();
199 if ($config->userFramework == 'Joomla' && $item) {
200 $config->userFrameworkURLVar = 'task';
201
202 // joomla 1.5RC1 seems to push this in the POST variable, which messes
203 // QF and checkboxes
204 unset($_POST['option']);
205 CRM_Core_Joomla::sidebarLeft();
206 }
207
208 // set active Component
209 $template = CRM_Core_Smarty::singleton();
210 $template->assign('activeComponent', 'CiviCRM');
211 $template->assign('formTpl', 'default');
212
213 if ($item) {
214 // CRM-7656 - make sure we send a clean sanitized path to create printer friendly url
215 $printerFriendly = CRM_Utils_System::makeURL(
353ffa53
TO
216 'snippet', FALSE, FALSE,
217 CRM_Utils_Array::value('path', $item)
218 ) . '2';
6a488035
TO
219 $template->assign('printerFriendly', $printerFriendly);
220
221 if (!array_key_exists('page_callback', $item)) {
222 CRM_Core_Error::debug('Bad item', $item);
223 CRM_Core_Error::fatal(ts('Bad menu record in database'));
224 }
225
226 // check that we are permissioned to access this page
227 if (!CRM_Core_Permission::checkMenuItem($item)) {
228 CRM_Utils_System::permissionDenied();
408b79bf 229 return NULL;
6a488035
TO
230 }
231
232 // check if ssl is set
a7488080 233 if (!empty($item['is_ssl'])) {
6a488035
TO
234 CRM_Utils_System::redirectToSSL();
235 }
236
237 if (isset($item['title'])) {
238 CRM_Utils_System::setTitle($item['title']);
239 }
240
241 if (isset($item['breadcrumb']) && !isset($item['is_public'])) {
242 CRM_Utils_System::appendBreadCrumb($item['breadcrumb']);
243 }
244
245 $pageArgs = NULL;
a7488080 246 if (!empty($item['page_arguments'])) {
6a488035
TO
247 $pageArgs = CRM_Core_Menu::getArrayForPathArgs($item['page_arguments']);
248 }
249
250 $template = CRM_Core_Smarty::singleton();
251 if (!empty($item['is_public'])) {
252 $template->assign('urlIsPublic', TRUE);
253 }
254 else {
255 $template->assign('urlIsPublic', FALSE);
256 self::versionCheck($template);
257 }
258
259 if (isset($item['return_url'])) {
260 $session = CRM_Core_Session::singleton();
261 $args = CRM_Utils_Array::value(
262 'return_url_args',
263 $item,
264 'reset=1'
265 );
266 $session->pushUserContext(CRM_Utils_System::url($item['return_url'], $args));
267 }
268
269 $result = NULL;
270 if (is_array($item['page_callback'])) {
99218b4b
TO
271 if ($item['page_callback']{0} !== '\\') {
272 // Legacy class-loading for PHP 5.2 namespaces; not sure it's needed, but counter-productive for PHP 5.3 namespaces
273 require_once str_replace('_', DIRECTORY_SEPARATOR, $item['page_callback'][0]) . '.php';
274 }
a1f0ed4e 275 $result = call_user_func($item['page_callback']);
6a488035
TO
276 }
277 elseif (strstr($item['page_callback'], '_Form')) {
278 $wrapper = new CRM_Utils_Wrapper();
279 $result = $wrapper->run(
280 CRM_Utils_Array::value('page_callback', $item),
281 CRM_Utils_Array::value('title', $item),
282 isset($pageArgs) ? $pageArgs : NULL
283 );
284 }
285 else {
286 $newArgs = explode('/', $_GET[$config->userFrameworkURLVar]);
99218b4b
TO
287 if ($item['page_callback']{0} !== '\\') {
288 // Legacy class-loading for PHP 5.2 namespaces; not sure it's needed, but counter-productive for PHP 5.3 namespaces
289 require_once str_replace('_', DIRECTORY_SEPARATOR, $item['page_callback']) . '.php';
290 }
6a488035
TO
291 $mode = 'null';
292 if (isset($pageArgs['mode'])) {
293 $mode = $pageArgs['mode'];
294 unset($pageArgs['mode']);
295 }
296 $title = CRM_Utils_Array::value('title', $item);
99218b4b 297 if (strstr($item['page_callback'], '_Page') || strstr($item['page_callback'], '\\Page\\')) {
408b79bf 298 $object = new $item['page_callback']($title, $mode);
6c2473d5 299 $object->urlPath = explode('/', $_GET[$config->userFrameworkURLVar]);
6a488035 300 }
99218b4b 301 elseif (strstr($item['page_callback'], '_Controller') || strstr($item['page_callback'], '\\Controller\\')) {
6a488035
TO
302 $addSequence = 'false';
303 if (isset($pageArgs['addSequence'])) {
304 $addSequence = $pageArgs['addSequence'];
305 $addSequence = $addSequence ? 'true' : 'false';
306 unset($pageArgs['addSequence']);
307 }
408b79bf 308 $object = new $item['page_callback']($title, TRUE, $mode, NULL, $addSequence);
6a488035
TO
309 }
310 else {
311 CRM_Core_Error::fatal();
312 }
313 $result = $object->run($newArgs, $pageArgs);
314 }
315
316 CRM_Core_Session::storeSessionObjects();
317 return $result;
318 }
319
320 CRM_Core_Menu::store();
321 CRM_Core_Session::setStatus(ts('Menu has been rebuilt'), ts('Complete'), 'success');
322 return CRM_Utils_System::redirect();
323 }
324
325 /**
326 * This function contains the default action
327 *
328 * @param $action
329 *
77b97be7
EM
330 * @param $contact_type
331 * @param $contact_sub_type
332 *
6a488035 333 */
00be9182 334 public static function form($action, $contact_type, $contact_sub_type) {
6a488035
TO
335 CRM_Utils_System::setUserContext(array('civicrm/contact/search/basic', 'civicrm/contact/view'));
336 $wrapper = new CRM_Utils_Wrapper();
337
338 $properties = CRM_Core_Component::contactSubTypeProperties($contact_sub_type, 'Edit');
339 if ($properties) {
340 $wrapper->run($properties['class'], ts('New %1', array(1 => $contact_sub_type)), $action, TRUE);
341 }
342 else {
343 $wrapper->run('CRM_Contact_Form_Contact', ts('New Contact'), $action, TRUE);
344 }
345 }
346
6a488035
TO
347 /**
348 * Show the message about CiviCRM versions
349 *
fa8dc18c 350 * @param CRM_Core_Smarty $template
6a488035 351 */
00be9182 352 public static function versionCheck($template) {
6a488035
TO
353 if (CRM_Core_Config::isUpgradeMode()) {
354 return;
355 }
fa8dc18c
CW
356 $newerVersion = $securityUpdate = NULL;
357 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'versionAlert', NULL, 1) & 1) {
358 $newerVersion = CRM_Utils_VersionCheck::singleton()->isNewerVersionAvailable();
359 }
360 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'securityUpdateAlert', NULL, 3) & 1) {
361 $securityUpdate = CRM_Utils_VersionCheck::singleton()->isSecurityUpdateAvailable();
362 }
6a488035 363 $template->assign('newer_civicrm_version', $newerVersion);
fa8dc18c 364 $template->assign('security_update', $securityUpdate);
6a488035
TO
365 }
366
a0ee3941
EM
367 /**
368 * @param bool $triggerRebuild
369 * @param bool $sessionReset
370 *
371 * @throws Exception
372 */
00be9182 373 public static function rebuildMenuAndCaches($triggerRebuild = FALSE, $sessionReset = FALSE) {
6a488035
TO
374 $config = CRM_Core_Config::singleton();
375 $config->clearModuleList();
376
ae2cab23
TO
377 // also cleanup all caches
378 $config->cleanupCaches($sessionReset || CRM_Utils_Request::retrieve('sessionReset', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET'));
379
6a488035
TO
380 CRM_Core_Menu::store();
381
382 // also reset navigation
383 CRM_Core_BAO_Navigation::resetNavigation();
384
6a488035
TO
385 // also cleanup module permissions
386 $config->cleanupPermissions();
387
9762f6ff
CW
388 // rebuild word replacement cache - pass false to prevent operations redundant with this fn
389 CRM_Core_BAO_WordReplacement::rebuild(FALSE);
76dca235 390
9f83bb22 391 CRM_Core_BAO_Setting::updateSettingsFromMetaData();
9762f6ff 392 // Clear js caches
4cc9b813 393 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
ab89fdde 394 CRM_Case_XMLRepository::singleton(TRUE);
1fcf16cc 395
6a488035
TO
396 // also rebuild triggers if requested explicitly
397 if (
398 $triggerRebuild ||
399 CRM_Utils_Request::retrieve('triggerRebuild', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET')
400 ) {
401 CRM_Core_DAO::triggerRebuild();
402 }
95a90cba 403 CRM_Core_DAO_AllCoreTables::reinitializeCache(TRUE);
6a488035
TO
404 CRM_Core_ManagedEntities::singleton(TRUE)->reconcile();
405 }
96025800 406
6a488035 407}