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