INFRA-132 - s/null/NULL/
[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 }
85 else {
86 // Symfony-based invocation path
87 require_once CIVICRM_SYMFONY_PATH . '/app/bootstrap.php.cache';
88 require_once CIVICRM_SYMFONY_PATH . '/app/AppKernel.php';
89 $kernel = new AppKernel('dev', true);
90 $kernel->loadClassCache();
91 $response = $kernel->handle(Symfony\Component\HttpFoundation\Request::createFromGlobals());
92 if (preg_match(':^text/html:', $response->headers->get('Content-Type'))) {
93 // let the CMS handle the trappings
94 return $response->getContent();
95 }
96 else {
97 $response->send();
98 exit();
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;
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 * @static
328 */
329 public static function form($action, $contact_type, $contact_sub_type) {
330 CRM_Utils_System::setUserContext(array('civicrm/contact/search/basic', 'civicrm/contact/view'));
331 $wrapper = new CRM_Utils_Wrapper();
332
333 $properties = CRM_Core_Component::contactSubTypeProperties($contact_sub_type, 'Edit');
334 if ($properties) {
335 $wrapper->run($properties['class'], ts('New %1', array(1 => $contact_sub_type)), $action, TRUE);
336 }
337 else {
338 $wrapper->run('CRM_Contact_Form_Contact', ts('New Contact'), $action, TRUE);
339 }
340 }
341
342 /**
343 * Show the message about CiviCRM versions
344 *
345 * @param CRM_Core_Smarty $template
346 */
347 public static function versionCheck($template) {
348 if (CRM_Core_Config::isUpgradeMode()) {
349 return;
350 }
351 $newerVersion = $securityUpdate = NULL;
352 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'versionAlert', NULL, 1) & 1) {
353 $newerVersion = CRM_Utils_VersionCheck::singleton()->isNewerVersionAvailable();
354 }
355 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'securityUpdateAlert', NULL, 3) & 1) {
356 $securityUpdate = CRM_Utils_VersionCheck::singleton()->isSecurityUpdateAvailable();
357 }
358 $template->assign('newer_civicrm_version', $newerVersion);
359 $template->assign('security_update', $securityUpdate);
360 }
361
362 /**
363 * @param bool $triggerRebuild
364 * @param bool $sessionReset
365 *
366 * @throws Exception
367 */
368 public static function rebuildMenuAndCaches($triggerRebuild = FALSE, $sessionReset = FALSE) {
369 $config = CRM_Core_Config::singleton();
370 $config->clearModuleList();
371
372 // also cleanup all caches
373 $config->cleanupCaches($sessionReset || CRM_Utils_Request::retrieve('sessionReset', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET'));
374
375 CRM_Core_Menu::store();
376
377 // also reset navigation
378 CRM_Core_BAO_Navigation::resetNavigation();
379
380 // also cleanup module permissions
381 $config->cleanupPermissions();
382
383 // rebuild word replacement cache - pass false to prevent operations redundant with this fn
384 CRM_Core_BAO_WordReplacement::rebuild(FALSE);
385
386 CRM_Core_BAO_Setting::updateSettingsFromMetaData();
387 // Clear js caches
388 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
389 CRM_Case_XMLRepository::singleton(TRUE);
390
391 // also rebuild triggers if requested explicitly
392 if (
393 $triggerRebuild ||
394 CRM_Utils_Request::retrieve('triggerRebuild', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET')
395 ) {
396 CRM_Core_DAO::triggerRebuild();
397 }
398 CRM_Core_DAO_AllCoreTables::reinitializeCache(TRUE);
399 CRM_Core_ManagedEntities::singleton(TRUE)->reconcile();
400 }
401 }