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