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