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