Merge pull request #4981 from totten/master-cbf2
[civicrm-core.git] / CRM / Core / Invoke.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 * $Id$
36 *
37 */
38 class CRM_Core_Invoke {
39
40 /**
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().
44 *
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().
50 *
51 */
52 public static function invoke($args) {
53 try {
54 return self::_invoke($args);
55 }
56 catch (Exception $e) {
57 CRM_Core_Error::handleUnhandledException($e);
58 }
59 }
60
61 /**
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().
70 */
71 public static function _invoke($args) {
72 if ($args[0] !== 'civicrm') {
73 return NULL;
74 }
75
76 if (!defined('CIVICRM_SYMFONY_PATH')) {
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);
83 }
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());
91 if (preg_match(':^text/html:', $response->headers->get('Content-Type'))) {
92 // let the CMS handle the trappings
93 return $response->getContent();
94 }
95 else {
96 $response->send();
97 exit();
98 }
99 }
100 }
101
102 /**
103 * Hackish support /civicrm/menu/rebuild
104 *
105 * @param array $args
106 * List of path parts.
107 * @void
108 */
109 static public function hackMenuRebuild($args) {
110 if (array('civicrm', 'menu', 'rebuild') == $args || array('civicrm', 'clearcache') == $args) {
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 *
126 * @param array $args
127 * List of path parts.
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();
133 if (!CRM_Core_Permission::check('skip IDS check')) {
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 *
146 * @param array $args
147 * List of path parts.
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 *
166 * @param array $args
167 * List of path parts.
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);
174 }
175 else {
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 *
193 * @param array $item
194 * See CRM_Core_Menu.
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(
216 'snippet', FALSE, FALSE,
217 CRM_Utils_Array::value('path', $item)
218 ) . '2';
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();
229 return NULL;
230 }
231
232 // check if ssl is set
233 if (!empty($item['is_ssl'])) {
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;
246 if (!empty($item['page_arguments'])) {
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'])) {
271 require_once str_replace('_', DIRECTORY_SEPARATOR, $item['page_callback'][0]) . '.php';
272 $result = call_user_func($item['page_callback']);
273 }
274 elseif (strstr($item['page_callback'], '_Form')) {
275 $wrapper = new CRM_Utils_Wrapper();
276 $result = $wrapper->run(
277 CRM_Utils_Array::value('page_callback', $item),
278 CRM_Utils_Array::value('title', $item),
279 isset($pageArgs) ? $pageArgs : NULL
280 );
281 }
282 else {
283 $newArgs = explode('/', $_GET[$config->userFrameworkURLVar]);
284 require_once str_replace('_', DIRECTORY_SEPARATOR, $item['page_callback']) . '.php';
285 $mode = 'null';
286 if (isset($pageArgs['mode'])) {
287 $mode = $pageArgs['mode'];
288 unset($pageArgs['mode']);
289 }
290 $title = CRM_Utils_Array::value('title', $item);
291 if (strstr($item['page_callback'], '_Page')) {
292 $object = new $item['page_callback']($title, $mode);
293 $object->urlPath = explode('/', $_GET[$config->userFrameworkURLVar]);
294 }
295 elseif (strstr($item['page_callback'], '_Controller')) {
296 $addSequence = 'false';
297 if (isset($pageArgs['addSequence'])) {
298 $addSequence = $pageArgs['addSequence'];
299 $addSequence = $addSequence ? 'true' : 'false';
300 unset($pageArgs['addSequence']);
301 }
302 $object = new $item['page_callback']($title, TRUE, $mode, NULL, $addSequence);
303 }
304 else {
305 CRM_Core_Error::fatal();
306 }
307 $result = $object->run($newArgs, $pageArgs);
308 }
309
310 CRM_Core_Session::storeSessionObjects();
311 return $result;
312 }
313
314 CRM_Core_Menu::store();
315 CRM_Core_Session::setStatus(ts('Menu has been rebuilt'), ts('Complete'), 'success');
316 return CRM_Utils_System::redirect();
317 }
318
319 /**
320 * This function contains the default action
321 *
322 * @param $action
323 *
324 * @param $contact_type
325 * @param $contact_sub_type
326 *
327 */
328 public static function form($action, $contact_type, $contact_sub_type) {
329 CRM_Utils_System::setUserContext(array('civicrm/contact/search/basic', 'civicrm/contact/view'));
330 $wrapper = new CRM_Utils_Wrapper();
331
332 $properties = CRM_Core_Component::contactSubTypeProperties($contact_sub_type, 'Edit');
333 if ($properties) {
334 $wrapper->run($properties['class'], ts('New %1', array(1 => $contact_sub_type)), $action, TRUE);
335 }
336 else {
337 $wrapper->run('CRM_Contact_Form_Contact', ts('New Contact'), $action, TRUE);
338 }
339 }
340
341 /**
342 * Show the message about CiviCRM versions
343 *
344 * @param CRM_Core_Smarty $template
345 */
346 public static function versionCheck($template) {
347 if (CRM_Core_Config::isUpgradeMode()) {
348 return;
349 }
350 $newerVersion = $securityUpdate = NULL;
351 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'versionAlert', NULL, 1) & 1) {
352 $newerVersion = CRM_Utils_VersionCheck::singleton()->isNewerVersionAvailable();
353 }
354 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'securityUpdateAlert', NULL, 3) & 1) {
355 $securityUpdate = CRM_Utils_VersionCheck::singleton()->isSecurityUpdateAvailable();
356 }
357 $template->assign('newer_civicrm_version', $newerVersion);
358 $template->assign('security_update', $securityUpdate);
359 }
360
361 /**
362 * @param bool $triggerRebuild
363 * @param bool $sessionReset
364 *
365 * @throws Exception
366 */
367 public static function rebuildMenuAndCaches($triggerRebuild = FALSE, $sessionReset = FALSE) {
368 $config = CRM_Core_Config::singleton();
369 $config->clearModuleList();
370
371 // also cleanup all caches
372 $config->cleanupCaches($sessionReset || CRM_Utils_Request::retrieve('sessionReset', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET'));
373
374 CRM_Core_Menu::store();
375
376 // also reset navigation
377 CRM_Core_BAO_Navigation::resetNavigation();
378
379 // also cleanup module permissions
380 $config->cleanupPermissions();
381
382 // rebuild word replacement cache - pass false to prevent operations redundant with this fn
383 CRM_Core_BAO_WordReplacement::rebuild(FALSE);
384
385 CRM_Core_BAO_Setting::updateSettingsFromMetaData();
386 // Clear js caches
387 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
388 CRM_Case_XMLRepository::singleton(TRUE);
389
390 // also rebuild triggers if requested explicitly
391 if (
392 $triggerRebuild ||
393 CRM_Utils_Request::retrieve('triggerRebuild', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET')
394 ) {
395 CRM_Core_DAO::triggerRebuild();
396 }
397 CRM_Core_DAO_AllCoreTables::reinitializeCache(TRUE);
398 CRM_Core_ManagedEntities::singleton(TRUE)->reconcile();
399 }
400
401 }