[!!!][CLEANUP] Move t3-table and t3-button to bootstrap
[Packages/TYPO3.CMS.git] / typo3 / sysext / sys_action / Classes / ActionTask.php
1 <?php
2 namespace TYPO3\CMS\SysAction;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19
20 /**
21 * This class provides a task for the taskcenter
22 *
23 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
24 * @author Georg Ringer <typo3@ringerge.org>
25 */
26 class ActionTask implements \TYPO3\CMS\Taskcenter\TaskInterface {
27
28 /**
29 * @var \TYPO3\CMS\Taskcenter\Controller\TaskModuleController
30 */
31 protected $taskObject;
32
33 /**
34 * @var \TYPO3\CMS\Backend\Form\FormEngine
35 */
36 public $t3lib_TCEforms;
37
38 /**
39 * All hook objects get registered here for later use
40 *
41 * @var array
42 */
43 protected $hookObjects = array();
44
45 /**
46 * URL to task module
47 *
48 * @var string
49 */
50 protected $moduleUrl;
51
52 /**
53 * Constructor
54 */
55 public function __construct(\TYPO3\CMS\Taskcenter\Controller\TaskModuleController $taskObject) {
56 $this->moduleUrl = BackendUtility::getModuleUrl('user_task');
57 $this->taskObject = $taskObject;
58 $GLOBALS['LANG']->includeLLFile('EXT:sys_action/locallang.xlf');
59 if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['sys_action']['tx_sysaction_task'])) {
60 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['sys_action']['tx_sysaction_task'] as $classRef) {
61 $this->hookObjects[] = GeneralUtility::getUserObj($classRef);
62 }
63 }
64 }
65
66 /**
67 * This method renders the task
68 *
69 * @return string The task as HTML
70 */
71 public function getTask() {
72 $content = '';
73 $show = (int)GeneralUtility::_GP('show');
74 foreach ($this->hookObjects as $hookObject) {
75 if (method_exists($hookObject, 'getTask')) {
76 $show = $hookObject->getTask($show, $this);
77 }
78 }
79 // If no task selected, render the menu
80 if ($show == 0) {
81 $content .= $this->taskObject->description($GLOBALS['LANG']->getLL('sys_action'), $GLOBALS['LANG']->getLL('description'));
82 $content .= $this->renderActionList();
83 } else {
84 $record = BackendUtility::getRecord('sys_action', $show);
85 // If the action is not found
86 if (count($record) == 0) {
87 $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, $GLOBALS['LANG']->getLL('action_error-not-found', TRUE), $GLOBALS['LANG']->getLL('action_error'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
88 $content .= $flashMessage->render();
89 } else {
90 // Render the task
91 $content .= $this->taskObject->description($record['title'], $record['description']);
92 // Output depends on the type
93 switch ($record['type']) {
94 case 1:
95 $content .= $this->viewNewBackendUser($record);
96 break;
97 case 2:
98 $content .= $this->viewSqlQuery($record);
99 break;
100 case 3:
101 $content .= $this->viewRecordList($record);
102 break;
103 case 4:
104 $content .= $this->viewEditRecord($record);
105 break;
106 case 5:
107 $content .= $this->viewNewRecord($record);
108 break;
109 default:
110 $flashMessage = GeneralUtility::makeInstance(
111 \TYPO3\CMS\Core\Messaging\FlashMessage::class,
112 $GLOBALS['LANG']->getLL('action_noType', TRUE),
113 $GLOBALS['LANG']->getLL('action_error'),
114 \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
115 );
116 $content .= '<br />' . $flashMessage->render();
117 }
118 }
119 }
120 return $content;
121 }
122
123 /**
124 * General overview over the task in the taskcenter menu
125 *
126 * @return string Overview as HTML
127 */
128 public function getOverview() {
129 $content = '<p>' . $GLOBALS['LANG']->getLL('description') . '</p>';
130 // Get the actions
131 $actionList = $this->getActions();
132 if (count($actionList) > 0) {
133 $items = '';
134 // Render a single action menu item
135 foreach ($actionList as $action) {
136 $active = GeneralUtility::_GP('show') === $action['uid'] ? ' class="active" ' : '';
137 $items .= '<li' . $active . '>
138 <a href="' . $action['link'] . '" title="' . htmlspecialchars($action['description']) . '">' . htmlspecialchars($action['title']) . '</a>
139 </li>';
140 }
141 $content .= '<ul>' . $items . '</ul>';
142 }
143 return $content;
144 }
145
146 /**
147 * Get all actions of an user. Admins can see any action, all others only those
148 * which are allowed in sys_action record itself.
149 *
150 * @return array Array holding every needed information of a sys_action
151 */
152 protected function getActions() {
153 $actionList = array();
154 // admins can see any record
155 if ($GLOBALS['BE_USER']->isAdmin()) {
156 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_action', '', '', 'sys_action.sorting');
157 } else {
158 // Editors can only see the actions which are assigned to a usergroup they belong to
159 $additionalWhere = 'be_groups.uid IN (' . ($GLOBALS['BE_USER']->groupList ?: 0) . ')';
160 $res = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query('sys_action.*', 'sys_action', 'sys_action_asgr_mm', 'be_groups', ' AND sys_action.hidden=0 AND ' . $additionalWhere, 'sys_action.uid', 'sys_action.sorting');
161 }
162 while ($actionRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
163 $editActionLink = '';
164 // Admins are allowed to edit sys_action records
165 if ($GLOBALS['BE_USER']->isAdmin()) {
166 $returnUrl = rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI'));
167 $link = GeneralUtility::getIndpEnv('TYPO3_REQUEST_DIR') . $GLOBALS['BACK_PATH'] . 'alt_doc.php?returnUrl=' . $returnUrl . '&edit[sys_action][' . $actionRow['uid'] . ']=edit';
168 $editActionLink = '<a class="edit" href="' . $link . '">' . '<img class="icon"' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/edit2.gif') . ' title="' . $GLOBALS['LANG']->getLL('edit-sys_action') . '" alt="" />' . $GLOBALS['LANG']->getLL('edit-sys_action') . '</a>';
169 }
170 $actionList[] = array(
171 'uid' => $actionRow['uid'],
172 'title' => $actionRow['title'],
173 'description' => $actionRow['description'],
174 'descriptionHtml' => nl2br(htmlspecialchars($actionRow['description'])) . $editActionLink,
175 'link' => $this->moduleUrl . '&SET[function]=sys_action.tx_sysaction_task&show=' . $actionRow['uid'],
176 'icon' => 'EXT:sys_action/sys_action.gif'
177 );
178 }
179 $GLOBALS['TYPO3_DB']->sql_free_result($res);
180 return $actionList;
181 }
182
183 /**
184 * Render the menu of sys_actions
185 *
186 * @return string List of sys_actions as HTML
187 */
188 protected function renderActionList() {
189 $content = '';
190 // Get the sys_action records
191 $actionList = $this->getActions();
192 // If any actions are found for the current users
193 if (count($actionList) > 0) {
194 $content .= $this->taskObject->renderListMenu($actionList);
195 } else {
196 $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, $GLOBALS['LANG']->getLL('action_not-found-description', TRUE), $GLOBALS['LANG']->getLL('action_not-found'), \TYPO3\CMS\Core\Messaging\FlashMessage::INFO);
197 $content .= $flashMessage->render();
198 }
199 // Admin users can create a new action
200 if ($GLOBALS['BE_USER']->isAdmin()) {
201 $returnUrl = rawurlencode($this->moduleUrl);
202 $link = GeneralUtility::getIndpEnv('TYPO3_REQUEST_DIR') . $GLOBALS['BACK_PATH'] . 'alt_doc.php?returnUrl=' . $returnUrl . '&edit[sys_action][0]=new';
203 $content .= '<p>' .
204 '<a href="' . $link . '" title="' . $GLOBALS['LANG']->getLL('new-sys_action') . '">' .
205 \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-new', array('class' => 'icon', 'title' => $GLOBALS['LANG']->getLL('new-sys_action'))) .
206 $GLOBALS['LANG']->getLL('new-sys_action') .
207 '</a></p>';
208 }
209 return $content;
210 }
211
212 /**
213 * Action to create a new BE user
214 *
215 * @param array $record sys_action record
216 * @return string form to create a new user
217 */
218 protected function viewNewBackendUser($record) {
219 $content = '';
220 $beRec = BackendUtility::getRecord('be_users', (int)$record['t1_copy_of_user']);
221 // A record is need which is used as copy for the new user
222 if (!is_array($beRec)) {
223 $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, $GLOBALS['LANG']->getLL('action_notReady', TRUE), $GLOBALS['LANG']->getLL('action_error'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
224 $content .= $flashMessage->render();
225 return $content;
226 }
227 $vars = GeneralUtility::_POST('data');
228 $key = 'NEW';
229 if ($vars['sent'] == 1) {
230 $errors = array();
231 // Basic error checks
232 if (!empty($vars['email']) && !GeneralUtility::validEmail($vars['email'])) {
233 $errors[] = $GLOBALS['LANG']->getLL('error-wrong-email');
234 }
235 if (empty($vars['username'])) {
236 $errors[] = $GLOBALS['LANG']->getLL('error-username-empty');
237 }
238 if ($vars['key'] === 'NEW' && empty($vars['password'])) {
239 $errors[] = $GLOBALS['LANG']->getLL('error-password-empty');
240 }
241 if ($vars['key'] !== 'NEW' && !$this->isCreatedByUser($vars['key'], $record)) {
242 $errors[] = $GLOBALS['LANG']->getLL('error-wrong-user');
243 }
244 foreach ($this->hookObjects as $hookObject) {
245 if (method_exists($hookObject, 'viewNewBackendUser_Error')) {
246 $errors = $hookObject->viewNewBackendUser_Error($vars, $errors, $this);
247 }
248 }
249 // Show errors if there are any
250 if (count($errors) > 0) {
251 $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, implode('<br />', $errors), $GLOBALS['LANG']->getLL('action_error'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
252 $content .= $flashMessage->render() . '<br />';
253 } else {
254 // Save user
255 $key = $this->saveNewBackendUser($record, $vars);
256 // Success message
257 $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, $vars['key'] === 'NEW' ? $GLOBALS['LANG']->getLL('success-user-created') : $GLOBALS['LANG']->getLL('success-user-updated'), $GLOBALS['LANG']->getLL('success'), \TYPO3\CMS\Core\Messaging\FlashMessage::OK);
258 $content .= $flashMessage->render() . '<br />';
259 }
260 }
261 // Load BE user to edit
262 if ((int)GeneralUtility::_GP('be_users_uid') > 0) {
263 $tmpUserId = (int)GeneralUtility::_GP('be_users_uid');
264 // Check if the selected user is created by the current user
265 $rawRecord = $this->isCreatedByUser($tmpUserId, $record);
266 if ($rawRecord) {
267 // Delete user
268 if (GeneralUtility::_GP('delete') == 1) {
269 $this->deleteUser($tmpUserId, $record['uid']);
270 }
271 $key = $tmpUserId;
272 $vars = $rawRecord;
273 }
274 }
275 $this->JScode();
276 $loadDB = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\RelationHandler::class);
277 $loadDB->start($vars['db_mountpoints'], 'pages');
278 $content .= '<form action="" method="post" enctype="multipart/form-data">
279 <fieldset class="fields">
280 <legend>' . $GLOBALS['LANG']->getLL('action_t1_legend_generalFields') . '</legend>
281 <div class="row">
282 <label for="field_disable">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_general.xlf:LGL.disable') . '</label>
283 <input type="checkbox" id="field_disable" name="data[disable]" value="1" class="checkbox" ' . ($vars['disable'] == 1 ? ' checked="checked" ' : '') . ' />
284 </div>
285 <div class="row">
286 <label for="field_realname">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_general.xlf:LGL.name') . '</label>
287 <input type="text" id="field_realname" name="data[realName]" value="' . htmlspecialchars($vars['realName']) . '" />
288 </div>
289 <div class="row">
290 <label for="field_username">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_tca.xlf:be_users.username') . '</label>
291 <input type="text" id="field_username" name="data[username]" value="' . htmlspecialchars($vars['username']) . '" />
292 </div>
293 <div class="row">
294 <label for="field_password">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_tca.xlf:be_users.password') . '</label>
295 <input type="password" id="field_password" name="data[password]" value="" />
296 </div>
297 <div class="row">
298 <label for="field_email">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_general.xlf:LGL.email') . '</label>
299 <input type="text" id="field_email" name="data[email]" value="' . htmlspecialchars($vars['email']) . '" />
300 </div>
301 </fieldset>
302 <fieldset class="fields">
303 <legend>' . $GLOBALS['LANG']->getLL('action_t1_legend_configuration') . '</legend>
304
305 <div class="row">
306 <label for="field_usergroup">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_tca.xlf:be_users.usergroup') . '</label>
307 <select id="field_usergroup" name="data[usergroup][]" multiple="multiple">
308 ' . $this->getUsergroups($record, $vars) . '
309 </select>
310 </div>
311 <div class="row">
312 <label for="field_db_mountpoints">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_tca.xlf:be_users.options_db_mounts') . '</label>
313 ' . $this->t3lib_TCEforms->dbFileIcons('data[db_mountpoints]', 'db', 'pages', $loadDB->itemArray, '', array('size' => 3)) . '
314 </div>
315 <div class="row">
316 <input type="hidden" name="data[key]" value="' . $key . '" />
317 <input type="hidden" name="data[sent]" value="1" />
318 <input class="btn btn-default" type="submit" value="' . ($key === 'NEW' ? $GLOBALS['LANG']->getLL('action_Create') : $GLOBALS['LANG']->getLL('action_Update')) . '" />
319 </div>
320 </fieldset>
321 </form>';
322 $content .= $this->getCreatedUsers($record, $key);
323 return $content;
324 }
325
326 /**
327 * Delete a BE user and redirect to the action by its id
328 *
329 * @param int $userId Id of the BE user
330 * @param int $actionId Id of the action
331 * @return void
332 */
333 protected function deleteUser($userId, $actionId) {
334 $GLOBALS['TYPO3_DB']->exec_UPDATEquery('be_users', 'uid=' . $userId, array(
335 'deleted' => 1,
336 'tstamp' => $GLOBALS['ACCESS_TIME']
337 ));
338 // redirect to the original task
339 $redirectUrl = $this->moduleUrl . '&show=' . $actionId;
340 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($redirectUrl);
341 }
342
343 /**
344 * Check if a BE user is created by the current user
345 *
346 * @param int $id Id of the BE user
347 * @param array $action sys_action record.
348 * @return mixed The record of the BE user if found, otherwise FALSE
349 */
350 protected function isCreatedByUser($id, $action) {
351 $record = BackendUtility::getRecord('be_users', $id, '*', ' AND cruser_id=' . $GLOBALS['BE_USER']->user['uid'] . ' AND createdByAction=' . $action['uid']);
352 if (is_array($record)) {
353 return $record;
354 } else {
355 return FALSE;
356 }
357 }
358
359 /**
360 * Render all users who are created by the current BE user including a link to edit the record
361 *
362 * @param array $action sys_action record.
363 * @param int $selectedUser Id of a selected user
364 * @return string html list of users
365 */
366 protected function getCreatedUsers($action, $selectedUser) {
367 $content = '';
368 $userList = array();
369 // List of users
370 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'be_users', 'cruser_id=' . $GLOBALS['BE_USER']->user['uid'] . ' AND createdByAction=' . (int)$action['uid'] . BackendUtility::deleteClause('be_users'), '', 'username');
371 // Render the user records
372 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
373 $icon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('be_users', $row, array('title' => 'uid=' . $row['uid']));
374 $line = $icon . $this->action_linkUserName($row['username'], $row['realName'], $action['uid'], $row['uid']);
375 // Selected user
376 if ($row['uid'] == $selectedUser) {
377 $line = '<strong>' . $line . '</strong>';
378 }
379 $userList[] = $line;
380 }
381 $GLOBALS['TYPO3_DB']->sql_free_result($res);
382 // If any records found
383 if (count($userList)) {
384 $content .= '<br />' . $this->taskObject->doc->section($GLOBALS['LANG']->getLL('action_t1_listOfUsers'), implode('<br />', $userList));
385 }
386 return $content;
387 }
388
389 /**
390 * Create a link to edit a user
391 *
392 * @param string $username Username
393 * @param string $realName Real name of the user
394 * @param int $sysActionUid Id of the sys_action record
395 * @param int $userId Id of the user
396 * @return string html link
397 */
398 protected function action_linkUserName($username, $realName, $sysActionUid, $userId) {
399 if (!empty($realName)) {
400 $username .= ' (' . $realName . ')';
401 }
402 // Link to update the user record
403 $href = $this->moduleUrl . '&SET[function]=sys_action.tx_sysaction_task&show=' . (int)$sysActionUid . '&be_users_uid=' . (int)$userId;
404 $link = '<a href="' . htmlspecialchars($href) . '">' . htmlspecialchars($username) . '</a>';
405 // Link to delete the user record
406 $onClick = ' onClick="return confirm(' . GeneralUtility::quoteJSvalue($GLOBALS['LANG']->getLL('lDelete_warning')) . ');"';
407 $link .= '
408 <a href="' . htmlspecialchars(($href . '&delete=1')) . '" ' . $onClick . '>'
409 . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-edit-delete') .
410 '</a>';
411 return $link;
412 }
413
414 /**
415 * Save/Update a BE user
416 *
417 * @param array $record Current action record
418 * @param array $vars POST vars
419 * @return int Id of the new/updated user
420 */
421 protected function saveNewBackendUser($record, $vars) {
422 // Check if the db mount is a page the current user is allowed to.);
423 $vars['db_mountpoints'] = $this->fixDbMount($vars['db_mountpoints']);
424 // Check if the usergroup is allowed
425 $vars['usergroup'] = $this->fixUserGroup($vars['usergroup'], $record);
426 $key = $vars['key'];
427 $vars['password'] = trim($vars['password']);
428 // Check if md5 is used as password encryption
429 if ($vars['password'] !== '' && strpos($GLOBALS['TCA']['be_users']['columns']['password']['config']['eval'], 'md5') !== FALSE) {
430 $vars['password'] = md5($vars['password']);
431 }
432 $data = '';
433 $newUserId = 0;
434 if ($key === 'NEW') {
435 $beRec = BackendUtility::getRecord('be_users', (int)$record['t1_copy_of_user']);
436 if (is_array($beRec)) {
437 $data = array();
438 $data['be_users'][$key] = $beRec;
439 $data['be_users'][$key]['username'] = $this->fixUsername($vars['username'], $record['t1_userprefix']);
440 $data['be_users'][$key]['password'] = $vars['password'];
441 $data['be_users'][$key]['realName'] = $vars['realName'];
442 $data['be_users'][$key]['email'] = $vars['email'];
443 $data['be_users'][$key]['disable'] = (int)$vars['disable'];
444 $data['be_users'][$key]['admin'] = 0;
445 $data['be_users'][$key]['usergroup'] = $vars['usergroup'];
446 $data['be_users'][$key]['db_mountpoints'] = $vars['db_mountpoints'];
447 $data['be_users'][$key]['createdByAction'] = $record['uid'];
448 }
449 } else {
450 // Check ownership
451 $beRec = BackendUtility::getRecord('be_users', (int)$key);
452 if (is_array($beRec) && $beRec['cruser_id'] == $GLOBALS['BE_USER']->user['uid']) {
453 $data = array();
454 $data['be_users'][$key]['username'] = $this->fixUsername($vars['username'], $record['t1_userprefix']);
455 if ($vars['password'] !== '') {
456 $data['be_users'][$key]['password'] = $vars['password'];
457 }
458 $data['be_users'][$key]['realName'] = $vars['realName'];
459 $data['be_users'][$key]['email'] = $vars['email'];
460 $data['be_users'][$key]['disable'] = (int)$vars['disable'];
461 $data['be_users'][$key]['admin'] = 0;
462 $data['be_users'][$key]['usergroup'] = $vars['usergroup'];
463 $data['be_users'][$key]['db_mountpoints'] = $vars['db_mountpoints'];
464 $newUserId = $key;
465 }
466 }
467 // Save/update user by using TCEmain
468 if (is_array($data)) {
469 $tce = GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
470 $tce->stripslashes_values = 0;
471 $tce->start($data, array(), $GLOBALS['BE_USER']);
472 $tce->admin = 1;
473 $tce->process_datamap();
474 $newUserId = (int)$tce->substNEWwithIDs['NEW'];
475 if ($newUserId) {
476 // Create
477 $this->action_createDir($newUserId);
478 } else {
479 // Update
480 $newUserId = (int)$key;
481 }
482 unset($tce);
483 }
484 return $newUserId;
485 }
486
487 /**
488 * Create the username based on the given username and the prefix
489 *
490 * @param string $username Username
491 * @param string $prefix Prefix
492 * @return string Combined username
493 */
494 protected function fixUsername($username, $prefix) {
495 return trim($prefix) . trim($username);
496 }
497
498 /**
499 * Clean the to be applied usergroups from not allowed ones
500 *
501 * @param array $appliedUsergroups Array of to be applied user groups
502 * @param array $actionRecord The action record
503 * @return array Cleaned array
504 */
505 protected function fixUserGroup($appliedUsergroups, $actionRecord) {
506 if (is_array($appliedUsergroups)) {
507 $cleanGroupList = array();
508 // Create an array from the allowed usergroups using the uid as key
509 $allowedUsergroups = array_flip(explode(',', $actionRecord['t1_allowed_groups']));
510 // Walk through the array and check every uid if it is under the allowed ines
511 foreach ($appliedUsergroups as $group) {
512 if (isset($allowedUsergroups[$group])) {
513 $cleanGroupList[] = $group;
514 }
515 }
516 $appliedUsergroups = $cleanGroupList;
517 }
518 return $appliedUsergroups;
519 }
520
521 /**
522 * Clean the to be applied DB-Mounts from not allowed ones
523 *
524 * @param string $appliedDbMounts List of pages like pages_123,pages456
525 * @return string Cleaned list
526 */
527 protected function fixDbMount($appliedDbMounts) {
528 // Admins can see any page, no need to check there
529 if (!empty($appliedDbMounts) && !$GLOBALS['BE_USER']->isAdmin()) {
530 $cleanDbMountList = array();
531 $dbMounts = GeneralUtility::trimExplode(',', $appliedDbMounts, TRUE);
532 // Walk through every wanted DB-Mount and check if it allowed for the current user
533 foreach ($dbMounts as $dbMount) {
534 $uid = (int)substr($dbMount, strrpos($dbMount, '_') + 1);
535 $page = BackendUtility::getRecord('pages', $uid);
536 // Check rootline and access rights
537 if ($this->checkRootline($uid) && $GLOBALS['BE_USER']->calcPerms($page)) {
538 $cleanDbMountList[] = 'pages_' . $uid;
539 }
540 }
541 // Build the clean list
542 $appliedDbMounts = implode(',', $cleanDbMountList);
543 }
544 return $appliedDbMounts;
545 }
546
547 /**
548 * Check if a page is inside the rootline the current user can see
549 *
550 * @param int $pageId Id of the the page to be checked
551 * @return bool Access to the page
552 */
553 protected function checkRootline($pageId) {
554 $access = FALSE;
555 $dbMounts = array_flip(explode(',', trim($GLOBALS['BE_USER']->dataLists['webmount_list'], ',')));
556 $rootline = BackendUtility::BEgetRootLine($pageId);
557 foreach ($rootline as $page) {
558 if (isset($dbMounts[$page['uid']]) && !$access) {
559 $access = TRUE;
560 }
561 }
562 return $access;
563 }
564
565 /**
566 * Add additional JavaScript to use the tceform select box
567 *
568 * @return void
569 */
570 protected function JScode() {
571 $this->t3lib_TCEforms = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Form\FormEngine::class);
572 $this->t3lib_TCEforms->backPath = $GLOBALS['BACK_PATH'];
573 $this->t3lib_TCEforms->printNeededJSFunctions();
574 }
575
576 /**
577 * Create a user directory if defined
578 *
579 * @param int $uid Id of the user record
580 * @return void
581 */
582 protected function action_createDir($uid) {
583 $path = $this->action_getUserMainDir();
584 if ($path) {
585 GeneralUtility::mkdir($path . $uid);
586 GeneralUtility::mkdir($path . $uid . '/_temp_/');
587 }
588 }
589
590 /**
591 * Get the path to the user home directory which is set in the localconf.php
592 *
593 * @return string Path
594 */
595 protected function action_getUserMainDir() {
596 $path = $GLOBALS['TYPO3_CONF_VARS']['BE']['userHomePath'];
597 // If path is set and a valid directory
598 if ($path && @is_dir($path) && $GLOBALS['TYPO3_CONF_VARS']['BE']['lockRootPath'] && GeneralUtility::isFirstPartOfStr($path, $GLOBALS['TYPO3_CONF_VARS']['BE']['lockRootPath']) && substr($path, -1) == '/') {
599 return $path;
600 }
601 }
602
603 /**
604 * Get all allowed usergroups which can be applied to a user record
605 *
606 * @param array $record sys_action record
607 * @param array $vars Selected be_user record
608 * @return string Rendered user groups
609 */
610 protected function getUsergroups($record, $vars) {
611 $content = '';
612 // Do nothing if no groups are allowed
613 if (empty($record['t1_allowed_groups'])) {
614 return $content;
615 }
616 $content .= '<option value=""></option>';
617 $grList = GeneralUtility::trimExplode(',', $record['t1_allowed_groups'], TRUE);
618 foreach ($grList as $group) {
619 $checkGroup = BackendUtility::getRecord('be_groups', $group);
620 if (is_array($checkGroup)) {
621 $selected = GeneralUtility::inList($vars['usergroup'], $checkGroup['uid']) ? ' selected="selected" ' : '';
622 $content .= '<option ' . $selected . 'value="' . $checkGroup['uid'] . '">' . htmlspecialchars($checkGroup['title']) . '</option>';
623 }
624 }
625 return $content;
626 }
627
628 /**
629 * Action to create a new record
630 *
631 * @param array $record sys_action record
632 * @return void Redirect to form to create a record
633 */
634 protected function viewNewRecord($record) {
635 $returnUrl = rawurlencode($this->moduleUrl);
636 $link = GeneralUtility::getIndpEnv('TYPO3_REQUEST_DIR') . $GLOBALS['BACK_PATH'] . 'alt_doc.php?returnUrl=' . $returnUrl . '&edit[' . $record['t3_tables'] . '][' . (int)$record['t3_listPid'] . ']=new';
637 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($link);
638 }
639
640 /**
641 * Action to edit records
642 *
643 * @param array $record sys_action record
644 * @return string list of records
645 */
646 protected function viewEditRecord($record) {
647 $content = '';
648 $actionList = array();
649 $dbAnalysis = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\RelationHandler::class);
650 $dbAnalysis->setFetchAllFields(TRUE);
651 $dbAnalysis->start($record['t4_recordsToEdit'], '*');
652 $dbAnalysis->getFromDB();
653 // collect the records
654 foreach ($dbAnalysis->itemArray as $el) {
655 $path = BackendUtility::getRecordPath($el['id'], $this->taskObject->perms_clause, $GLOBALS['BE_USER']->uc['titleLen']);
656 $record = BackendUtility::getRecord($el['table'], $dbAnalysis->results[$el['table']][$el['id']]);
657 $title = BackendUtility::getRecordTitle($el['table'], $dbAnalysis->results[$el['table']][$el['id']]);
658 $description = $GLOBALS['LANG']->sL($GLOBALS['TCA'][$el['table']]['ctrl']['title'], TRUE);
659 // @todo: which information could be needful
660 if (isset($record['crdate'])) {
661 $description .= ' - ' . BackendUtility::dateTimeAge($record['crdate']);
662 }
663 $actionList[$el['id']] = array(
664 'title' => $title,
665 'description' => BackendUtility::getRecordTitle($el['table'], $dbAnalysis->results[$el['table']][$el['id']]),
666 'descriptionHtml' => $description,
667 'link' => $GLOBALS['BACK_PATH'] . 'alt_doc.php?returnUrl=' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI')) . '&edit[' . $el['table'] . '][' . $el['id'] . ']=edit',
668 'icon' => \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord($el['table'], $dbAnalysis->results[$el['table']][$el['id']], array('title' => htmlspecialchars($path)))
669 );
670 }
671 // Render the record list
672 $content .= $this->taskObject->renderListMenu($actionList);
673 return $content;
674 }
675
676 /**
677 * Action to view the result of a SQL query
678 *
679 * @param array $record sys_action record
680 * @return string Result of the query
681 */
682 protected function viewSqlQuery($record) {
683 $content = '';
684 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('lowlevel')) {
685 $sql_query = unserialize($record['t2_data']);
686 if (!is_array($sql_query) || is_array($sql_query) && strtoupper(substr(trim($sql_query['qSelect']), 0, 6)) === 'SELECT') {
687 $actionContent = '';
688 $fullsearch = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\QueryView::class);
689 $fullsearch->formW = 40;
690 $fullsearch->noDownloadB = 1;
691 $type = $sql_query['qC']['search_query_makeQuery'];
692 if ($sql_query['qC']['labels_noprefix'] === 'on') {
693 $GLOBALS['SOBE']->MOD_SETTINGS['labels_noprefix'] = 'on';
694 }
695 $sqlQuery = $sql_query['qSelect'];
696 $queryIsEmpty = FALSE;
697 if ($sqlQuery) {
698 $res = $GLOBALS['TYPO3_DB']->sql_query($sqlQuery);
699 if (!$GLOBALS['TYPO3_DB']->sql_error()) {
700 $fullsearch->formW = 48;
701 // Additional configuration
702 $GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels'] = 1;
703 $GLOBALS['SOBE']->MOD_SETTINGS['queryFields'] = $sql_query['qC']['queryFields'];
704 $cP = $fullsearch->getQueryResultCode($type, $res, $sql_query['qC']['queryTable']);
705 $actionContent = $cP['content'];
706 // If the result is rendered as csv or xml, show a download link
707 if ($type === 'csv' || $type === 'xml') {
708 $actionContent .= '<br /><br /><a href="' . GeneralUtility::getIndpEnv('REQUEST_URI') . '&download_file=1"><strong>' . $GLOBALS['LANG']->getLL('action_download_file') . '</strong></a>';
709 }
710 } else {
711 $actionContent .= $GLOBALS['TYPO3_DB']->sql_error();
712 }
713 } else {
714 // Query is empty (not built)
715 $queryIsEmpty = TRUE;
716 $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, $GLOBALS['LANG']->getLL('action_emptyQuery', TRUE), $GLOBALS['LANG']->getLL('action_error'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
717 $content .= '<br />' . $flashMessage->render();
718 }
719 // Admin users are allowed to see and edit the query
720 if ($GLOBALS['BE_USER']->isAdmin()) {
721 if (!$queryIsEmpty) {
722 $actionContent .= '<hr /> ' . $fullsearch->tableWrap($sql_query['qSelect']);
723 }
724 $actionContent .= '<br /><a title="' . $GLOBALS['LANG']->getLL('action_editQuery') . '" href="'
725 . htmlspecialchars(BackendUtility::getModuleUrl('system_dbint')
726 . '&id=' . '&SET[function]=search' . '&SET[search]=query'
727 . '&storeControl[STORE]=-' . $record['uid'] . '&storeControl[LOAD]=1')
728 . '">
729 <img class="icon"' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'],
730 'gfx/edit2.gif') . ' alt="" />' . $GLOBALS['LANG']->getLL(($queryIsEmpty ? 'action_createQuery'
731 : 'action_editQuery')) . '</a><br /><br />';
732 }
733 $content .= $this->taskObject->doc->section($GLOBALS['LANG']->getLL('action_t2_result'), $actionContent, 0, 1);
734 } else {
735 // Query is not configured
736 $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, $GLOBALS['LANG']->getLL('action_notReady', TRUE), $GLOBALS['LANG']->getLL('action_error'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
737 $content .= '<br />' . $flashMessage->render();
738 }
739 } else {
740 // Required sysext lowlevel is not installed
741 $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, $GLOBALS['LANG']->getLL('action_lowlevelMissing', TRUE), $GLOBALS['LANG']->getLL('action_error'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
742 $content .= '<br />' . $flashMessage->render();
743 }
744 return $content;
745 }
746
747 /**
748 * Action to create a list of records of a specific table and pid
749 *
750 * @param array $record sys_action record
751 * @return string list of records
752 */
753 protected function viewRecordList($record) {
754 $content = '';
755 $this->id = (int)$record['t3_listPid'];
756 $this->table = $record['t3_tables'];
757 if ($this->id == 0 || $this->table == '') {
758 $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, $GLOBALS['LANG']->getLL('action_notReady', TRUE), $GLOBALS['LANG']->getLL('action_error'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
759 $content .= '<br />' . $flashMessage->render();
760 return $content;
761 }
762 // Loading current page record and checking access:
763 $this->pageinfo = BackendUtility::readPageAccess($this->id, $this->taskObject->perms_clause);
764 $access = is_array($this->pageinfo) ? 1 : 0;
765 // If there is access to the page, then render the list contents and set up the document template object:
766 if ($access) {
767 // Initialize the dblist object:
768 $dblist = GeneralUtility::makeInstance(\TYPO3\CMS\SysAction\ActionList::class);
769 $dblist->script = GeneralUtility::getIndpEnv('REQUEST_URI');
770 $dblist->backPath = $GLOBALS['BACK_PATH'];
771 $dblist->calcPerms = $GLOBALS['BE_USER']->calcPerms($this->pageinfo);
772 $dblist->thumbs = $GLOBALS['BE_USER']->uc['thumbnailsByDefault'];
773 $dblist->returnUrl = $this->taskObject->returnUrl;
774 $dblist->allFields = 1;
775 $dblist->localizationView = 1;
776 $dblist->showClipboard = 0;
777 $dblist->disableSingleTableView = 1;
778 $dblist->pageRow = $this->pageinfo;
779 $dblist->counter++;
780 $dblist->MOD_MENU = array('bigControlPanel' => '', 'clipBoard' => '', 'localization' => '');
781 $dblist->modTSconfig = $this->taskObject->modTSconfig;
782 $dblist->dontShowClipControlPanels = (!$this->taskObject->MOD_SETTINGS['bigControlPanel'] && $dblist->clipObj->current == 'normal' && !$this->modTSconfig['properties']['showClipControlPanelsDespiteOfCMlayers']);
783 // Initialize the listing object, dblist, for rendering the list:
784 $this->pointer = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(GeneralUtility::_GP('pointer'), 0, 100000);
785 $dblist->start($this->id, $this->table, $this->pointer, $this->taskObject->search_field, $this->taskObject->search_levels, $this->taskObject->showLimit);
786 $dblist->setDispFields();
787 // Render the list of tables:
788 $dblist->generateList();
789 // Add JavaScript functions to the page:
790 $this->taskObject->doc->JScode = $this->taskObject->doc->wrapScriptTags('
791
792 function jumpExt(URL,anchor) {
793 var anc = anchor?anchor:"";
794 window.location.href = URL+(T3_THIS_LOCATION?"&returnUrl="+T3_THIS_LOCATION:"")+anc;
795 return false;
796 }
797 function jumpSelf(URL) {
798 window.location.href = URL+(T3_RETURN_URL?"&returnUrl="+T3_RETURN_URL:"");
799 return false;
800 }
801
802 function setHighlight(id) {
803 top.fsMod.recentIds["web"]=id;
804 top.fsMod.navFrameHighlightedID["web"]="pages"+id+"_"+top.fsMod.currentBank; // For highlighting
805
806 if (top.content && top.content.nav_frame && top.content.nav_frame.refresh_nav) {
807 top.content.nav_frame.refresh_nav();
808 }
809 }
810
811 ' . $dblist->CBfunctions() . '
812 function editRecords(table,idList,addParams,CBflag) {
813 window.location.href="' . $GLOBALS['BACK_PATH'] . 'alt_doc.php?returnUrl=' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI')) . '&edit["+table+"]["+idList+"]=edit"+addParams;
814 }
815 function editList(table,idList) {
816 var list="";
817
818 // Checking how many is checked, how many is not
819 var pointer=0;
820 var pos = idList.indexOf(",");
821 while (pos!=-1) {
822 if (cbValue(table+"|"+idList.substr(pointer,pos-pointer))) {
823 list+=idList.substr(pointer,pos-pointer)+",";
824 }
825 pointer=pos+1;
826 pos = idList.indexOf(",",pointer);
827 }
828 if (cbValue(table+"|"+idList.substr(pointer))) {
829 list+=idList.substr(pointer)+",";
830 }
831
832 return list ? list : idList;
833 }
834 T3_THIS_LOCATION = "' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI')) . '";
835
836 if (top.fsMod) top.fsMod.recentIds["web"] = ' . (int)$this->id . ';
837 ');
838 // Setting up the context sensitive menu:
839 $this->taskObject->doc->getContextMenuCode();
840 // Begin to compile the whole page
841 $content .= '<form action="' . htmlspecialchars($dblist->listURL()) . '" method="post" name="dblistForm">' . $dblist->HTMLcode . '<input type="hidden" name="cmd_table" /><input type="hidden" name="cmd" />
842 </form>';
843 // If a listing was produced, create the page footer with search form etc:
844 if ($dblist->HTMLcode) {
845 // Making field select box (when extended view for a single table is enabled):
846 if ($dblist->table) {
847 $tmpBackpath = $GLOBALS['BACK_PATH'];
848 $GLOBALS['BACK_PATH'] = '';
849 $content .= $dblist->fieldSelectBox($dblist->table);
850 $GLOBALS['BACK_PATH'] = $tmpBackpath;
851 }
852 }
853 } else {
854 // Not enough rights to access the list view or the page
855 $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, $GLOBALS['LANG']->getLL('action_error-access', TRUE), $GLOBALS['LANG']->getLL('action_error'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
856 $content .= $flashMessage->render();
857 }
858 return $content;
859 }
860
861 }