a644ff2531faf07ba525bcf8ead779d978d57042
[Packages/TYPO3.CMS.git] / typo3 / sysext / sys_action / Classes / ActionTask.php
1 <?php
2 namespace TYPO3\CMS\SysAction;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * (c) 2010-2013 Georg Ringer <typo3@ringerge.org>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29 use TYPO3\CMS\Backend\Utility\BackendUtility;
30
31 /**
32 * This class provides a task for the taskcenter
33 *
34 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
35 * @author Georg Ringer <typo3@ringerge.org>
36 */
37 class ActionTask implements \TYPO3\CMS\Taskcenter\TaskInterface {
38
39 /**
40 * @var \TYPO3\CMS\Taskcenter\Controller\TaskModuleController
41 */
42 protected $taskObject;
43
44 /**
45 * @var \TYPO3\CMS\Backend\Form\FormEngine
46 * @todo Define visibility
47 */
48 public $t3lib_TCEforms;
49
50 /**
51 * All hook objects get registered here for later use
52 *
53 * @var array
54 */
55 protected $hookObjects = array();
56
57 /**
58 * Constructor
59 */
60 public function __construct(\TYPO3\CMS\Taskcenter\Controller\TaskModuleController $taskObject) {
61 $this->taskObject = $taskObject;
62 $GLOBALS['LANG']->includeLLFile('EXT:sys_action/locallang.xlf');
63 if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['sys_action']['tx_sysaction_task'])) {
64 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['sys_action']['tx_sysaction_task'] as $classRef) {
65 $this->hookObjects[] = GeneralUtility::getUserObj($classRef);
66 }
67 }
68 }
69
70 /**
71 * This method renders the task
72 *
73 * @return string The task as HTML
74 */
75 public function getTask() {
76 $content = '';
77 $show = (int)GeneralUtility::_GP('show');
78 foreach ($this->hookObjects as $hookObject) {
79 if (method_exists($hookObject, 'getTask')) {
80 $show = $hookObject->getTask($show, $this);
81 }
82 }
83 // If no task selected, render the menu
84 if ($show == 0) {
85 $content .= $this->taskObject->description($GLOBALS['LANG']->getLL('sys_action'), $GLOBALS['LANG']->getLL('description'));
86 $content .= $this->renderActionList();
87 } else {
88 $record = BackendUtility::getRecord('sys_action', $show);
89 // If the action is not found
90 if (count($record) == 0) {
91 $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $GLOBALS['LANG']->getLL('action_error-not-found', TRUE), $GLOBALS['LANG']->getLL('action_error'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
92 $content .= $flashMessage->render();
93 } else {
94 // Render the task
95 $content .= $this->taskObject->description($record['title'], $record['description']);
96 // Output depends on the type
97 switch ($record['type']) {
98 case 1:
99 $content .= $this->viewNewBackendUser($record);
100 break;
101 case 2:
102 $content .= $this->viewSqlQuery($record);
103 break;
104 case 3:
105 $content .= $this->viewRecordList($record);
106 break;
107 case 4:
108 $content .= $this->viewEditRecord($record);
109 break;
110 case 5:
111 $content .= $this->viewNewRecord($record);
112 break;
113 default:
114 $flashMessage = GeneralUtility::makeInstance(
115 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
116 $GLOBALS['LANG']->getLL('action_noType', TRUE),
117 $GLOBALS['LANG']->getLL('action_error'),
118 \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
119 );
120 $content .= '<br />' . $flashMessage->render();
121 }
122 }
123 }
124 return $content;
125 }
126
127 /**
128 * General overview over the task in the taskcenter menu
129 *
130 * @return string Overview as HTML
131 */
132 public function getOverview() {
133 $content = '<p>' . $GLOBALS['LANG']->getLL('description') . '</p>';
134 // Get the actions
135 $actionList = $this->getActions();
136 if (count($actionList) > 0) {
137 $items = '';
138 // Render a single action menu item
139 foreach ($actionList as $action) {
140 $active = GeneralUtility::_GP('show') === $action['uid'] ? ' class="active" ' : '';
141 $items .= '<li' . $active . '>
142 <a href="' . $action['link'] . '" title="' . htmlspecialchars($action['description']) . '">' . htmlspecialchars($action['title']) . '</a>
143 </li>';
144 }
145 $content .= '<ul>' . $items . '</ul>';
146 }
147 return $content;
148 }
149
150 /**
151 * Get all actions of an user. Admins can see any action, all others only those
152 * which are allowed in sys_action record itself.
153 *
154 * @return array Array holding every needed information of a sys_action
155 */
156 protected function getActions() {
157 $actionList = array();
158 // admins can see any record
159 if ($GLOBALS['BE_USER']->isAdmin()) {
160 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_action', '', '', 'sys_action.sorting');
161 } else {
162 // Editors can only see the actions which are assigned to a usergroup they belong to
163 $additionalWhere = 'be_groups.uid IN (' . ($GLOBALS['BE_USER']->groupList ?: 0) . ')';
164 $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');
165 }
166 while ($actionRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
167 $editActionLink = '';
168 // Admins are allowed to edit sys_action records
169 if ($GLOBALS['BE_USER']->isAdmin()) {
170 $returnUrl = rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI'));
171 $link = GeneralUtility::getIndpEnv('TYPO3_REQUEST_DIR') . $GLOBALS['BACK_PATH'] . 'alt_doc.php?returnUrl=' . $returnUrl . '&edit[sys_action][' . $actionRow['uid'] . ']=edit';
172 $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>';
173 }
174 $actionList[] = array(
175 'uid' => $actionRow['uid'],
176 'title' => $actionRow['title'],
177 'description' => $actionRow['description'],
178 'descriptionHtml' => nl2br(htmlspecialchars($actionRow['description'])) . $editActionLink,
179 'link' => 'mod.php?M=user_task&SET[function]=sys_action.tx_sysaction_task&show=' . $actionRow['uid'],
180 'icon' => 'EXT:sys_action/sys_action.gif'
181 );
182 }
183 $GLOBALS['TYPO3_DB']->sql_free_result($res);
184 return $actionList;
185 }
186
187 /**
188 * Render the menu of sys_actions
189 *
190 * @return string List of sys_actions as HTML
191 */
192 protected function renderActionList() {
193 $content = '';
194 // Get the sys_action records
195 $actionList = $this->getActions();
196 // If any actions are found for the current users
197 if (count($actionList) > 0) {
198 $content .= $this->taskObject->renderListMenu($actionList);
199 } else {
200 $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $GLOBALS['LANG']->getLL('action_not-found-description', TRUE), $GLOBALS['LANG']->getLL('action_not-found'), \TYPO3\CMS\Core\Messaging\FlashMessage::INFO);
201 $content .= $flashMessage->render();
202 }
203 // Admin users can create a new action
204 if ($GLOBALS['BE_USER']->isAdmin()) {
205 $returnUrl = rawurlencode('mod.php?M=user_task');
206 $link = GeneralUtility::getIndpEnv('TYPO3_REQUEST_DIR') . $GLOBALS['BACK_PATH'] . 'alt_doc.php?returnUrl=' . $returnUrl . '&edit[sys_action][0]=new';
207 $content .= '<br />
208 <a href="' . $link . '" title="' . $GLOBALS['LANG']->getLL('new-sys_action') . '">' . '<img class="icon"' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/new_record.gif') . ' title="' . $GLOBALS['LANG']->getLL('new-sys_action') . '" alt="" /> ' . $GLOBALS['LANG']->getLL('new-sys_action') . '</a>';
209 }
210 return $content;
211 }
212
213 /**
214 * Action to create a new BE user
215 *
216 * @param array $record sys_action record
217 * @return string form to create a new user
218 */
219 protected function viewNewBackendUser($record) {
220 $content = '';
221 $beRec = BackendUtility::getRecord('be_users', (int)$record['t1_copy_of_user']);
222 // A record is need which is used as copy for the new user
223 if (!is_array($beRec)) {
224 $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $GLOBALS['LANG']->getLL('action_notReady', TRUE), $GLOBALS['LANG']->getLL('action_error'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
225 $content .= $flashMessage->render();
226 return $content;
227 }
228 $vars = GeneralUtility::_POST('data');
229 $key = 'NEW';
230 if ($vars['sent'] == 1) {
231 $errors = array();
232 // Basic error checks
233 if (!empty($vars['email']) && !GeneralUtility::validEmail($vars['email'])) {
234 $errors[] = $GLOBALS['LANG']->getLL('error-wrong-email');
235 }
236 if (empty($vars['username'])) {
237 $errors[] = $GLOBALS['LANG']->getLL('error-username-empty');
238 }
239 if (empty($vars['password'])) {
240 $errors[] = $GLOBALS['LANG']->getLL('error-password-empty');
241 }
242 if ($vars['key'] !== 'NEW' && !$this->isCreatedByUser($vars['key'], $record)) {
243 $errors[] = $GLOBALS['LANG']->getLL('error-wrong-user');
244 }
245 foreach ($this->hookObjects as $hookObject) {
246 if (method_exists($hookObject, 'viewNewBackendUser_Error')) {
247 $errors = $hookObject->viewNewBackendUser_Error($vars, $errors, $this);
248 }
249 }
250 // Show errors if there are any
251 if (count($errors) > 0) {
252 $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', implode('<br />', $errors), $GLOBALS['LANG']->getLL('action_error'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
253 $content .= $flashMessage->render() . '<br />';
254 } else {
255 // Save user
256 $key = $this->saveNewBackendUser($record, $vars);
257 // Success message
258 $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $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);
259 $content .= $flashMessage->render() . '<br />';
260 }
261 }
262 // Load BE user to edit
263 if ((int)GeneralUtility::_GP('be_users_uid') > 0) {
264 $tmpUserId = (int)GeneralUtility::_GP('be_users_uid');
265 // Check if the selected user is created by the current user
266 $rawRecord = $this->isCreatedByUser($tmpUserId, $record);
267 if ($rawRecord) {
268 // Delete user
269 if (GeneralUtility::_GP('delete') == 1) {
270 $this->deleteUser($tmpUserId, $record['uid']);
271 }
272 $key = $tmpUserId;
273 $vars = $rawRecord;
274 }
275 }
276 $this->JScode();
277 $loadDB = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
278 $loadDB->start($vars['db_mountpoints'], 'pages');
279 $content .= '<form action="" method="post" enctype="multipart/form-data">
280 <fieldset class="fields">
281 <legend>General fields</legend>
282 <div class="row">
283 <label for="field_disable">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_general.xlf:LGL.disable') . '</label>
284 <input type="checkbox" id="field_disable" name="data[disable]" value="1" class="checkbox" ' . ($vars['disable'] == 1 ? ' checked="checked" ' : '') . ' />
285 </div>
286 <div class="row">
287 <label for="field_realname">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_general.xlf:LGL.name') . '</label>
288 <input type="text" id="field_realname" name="data[realName]" value="' . htmlspecialchars($vars['realName']) . '" />
289 </div>
290 <div class="row">
291 <label for="field_username">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_tca.xlf:be_users.username') . '</label>
292 <input type="text" id="field_username" name="data[username]" value="' . htmlspecialchars($vars['username']) . '" />
293 </div>
294 <div class="row">
295 <label for="field_password">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_tca.xlf:be_users.password') . '</label>
296 <input type="password" id="field_password" name="data[password]" value="" />
297 </div>
298 <div class="row">
299 <label for="field_email">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_general.xlf:LGL.email') . '</label>
300 <input type="text" id="field_email" name="data[email]" value="' . htmlspecialchars($vars['email']) . '" />
301 </div>
302 </fieldset>
303 <fieldset class="fields">
304 <legend>Configuration</legend>
305
306 <div class="row">
307 <label for="field_usergroup">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_tca.xlf:be_users.usergroup') . '</label>
308 <select id="field_usergroup" name="data[usergroup][]" multiple="multiple">
309 ' . $this->getUsergroups($record, $vars) . '
310 </select>
311 </div>
312 <div class="row">
313 <label for="field_db_mountpoints">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_tca.xlf:be_users.options_db_mounts') . '</label>
314 ' . $this->t3lib_TCEforms->dbFileIcons('data[db_mountpoints]', 'db', 'pages', $loadDB->itemArray, '', array('size' => 3)) . '
315 </div>
316 <div class="row">
317 <input type="hidden" name="data[key]" value="' . $key . '" />
318 <input type="hidden" name="data[sent]" value="1" />
319 <input type="submit" value="' . ($key === 'NEW' ? $GLOBALS['LANG']->getLL('action_Create') : $GLOBALS['LANG']->getLL('action_Update')) . '" />
320 </div>
321 </fieldset>
322 </form>';
323 $content .= $this->getCreatedUsers($record, $key);
324 return $content;
325 }
326
327 /**
328 * Delete a BE user and redirect to the action by its id
329 *
330 * @param integer $userId Id of the BE user
331 * @param integer $actionId Id of the action
332 * @return void
333 */
334 protected function deleteUser($userId, $actionId) {
335 $GLOBALS['TYPO3_DB']->exec_UPDATEquery('be_users', 'uid=' . $userId, array(
336 'deleted' => 1,
337 'tstamp' => $GLOBALS['ACCESS_TIME']
338 ));
339 // redirect to the original task
340 $redirectUrl = 'mod.php?M=user_task&show=' . $actionId;
341 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($redirectUrl);
342 }
343
344 /**
345 * Check if a BE user is created by the current user
346 *
347 * @param integer $id Id of the BE user
348 * @param array $action sys_action record.
349 * @return mixed The record of the BE user if found, otherwise FALSE
350 */
351 protected function isCreatedByUser($id, $action) {
352 $record = BackendUtility::getRecord('be_users', $id, '*', ' AND cruser_id=' . $GLOBALS['BE_USER']->user['uid'] . ' AND createdByAction=' . $action['uid']);
353 if (is_array($record)) {
354 return $record;
355 } else {
356 return FALSE;
357 }
358 }
359
360 /**
361 * Render all users who are created by the current BE user including a link to edit the record
362 *
363 * @param array $action sys_action record.
364 * @param integer $selectedUser Id of a selected user
365 * @return string html list of users
366 */
367 protected function getCreatedUsers($action, $selectedUser) {
368 $content = '';
369 $userList = array();
370 // List of users
371 $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');
372 // Render the user records
373 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
374 $icon = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('be_users', $row, array('title' => 'uid=' . $row['uid']));
375 $line = $icon . $this->action_linkUserName($row['username'], $row['realName'], $action['uid'], $row['uid']);
376 // Selected user
377 if ($row['uid'] == $selectedUser) {
378 $line = '<strong>' . $line . '</strong>';
379 }
380 $userList[] = $line;
381 }
382 $GLOBALS['TYPO3_DB']->sql_free_result($res);
383 // If any records found
384 if (count($userList)) {
385 $content .= '<br />' . $this->taskObject->doc->section($GLOBALS['LANG']->getLL('action_t1_listOfUsers'), implode('<br />', $userList));
386 }
387 return $content;
388 }
389
390 /**
391 * Create a link to edit a user
392 *
393 * @param string $username Username
394 * @param string $realName Real name of the user
395 * @param integer $sysActionUid Id of the sys_action record
396 * @param integer $userId Id of the user
397 * @return string html link
398 */
399 protected function action_linkUserName($username, $realName, $sysActionUid, $userId) {
400 if (!empty($realName)) {
401 $username .= ' (' . $realName . ')';
402 }
403 // Link to update the user record
404 $href = 'mod.php?M=user_task&SET[function]=sys_action.tx_sysaction_task&show=' . (int)$sysActionUid . '&be_users_uid=' . (int)$userId;
405 $link = '<a href="' . htmlspecialchars($href) . '">' . htmlspecialchars($username) . '</a>';
406 // Link to delete the user record
407 $onClick = ' onClick="return confirm(' . GeneralUtility::quoteJSvalue($GLOBALS['LANG']->getLL('lDelete_warning')) . ');"';
408 $link .= '
409 <a href="' . htmlspecialchars(($href . '&delete=1')) . '" ' . $onClick . '>
410 <img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], 'gfx/delete_record.gif') . ' alt="" />
411 </a>';
412 return $link;
413 }
414
415 /**
416 * Save/Update a BE user
417 *
418 * @param array $record Current action record
419 * @param array $vars POST vars
420 * @return integer Id of the new/updated user
421 */
422 protected function saveNewBackendUser($record, $vars) {
423 // Check if the db mount is a page the current user is allowed to.);
424 $vars['db_mountpoints'] = $this->fixDbMount($vars['db_mountpoints']);
425 // Check if the usergroup is allowed
426 $vars['usergroup'] = $this->fixUserGroup($vars['usergroup'], $record);
427 // Check if md5 is used as password encryption
428 if (strpos($GLOBALS['TCA']['be_users']['columns']['password']['config']['eval'], 'md5') !== FALSE) {
429 $vars['password'] = md5($vars['password']);
430 }
431 $key = $vars['key'];
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'] = trim($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 (trim($vars['password'])) {
456 $data['be_users'][$key]['password'] = trim($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');
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 integer $pageId Id of the the page to be checked
551 * @return boolean 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');
572 $this->t3lib_TCEforms->backPath = $GLOBALS['BACK_PATH'];
573 $js = $this->t3lib_TCEforms->dbFileCon();
574 $this->taskObject->doc->JScodeArray[] = $js;
575 return $js;
576 }
577
578 /**
579 * Create a user directory if defined
580 *
581 * @param integer $uid Id of the user record
582 * @return void
583 */
584 protected function action_createDir($uid) {
585 $path = $this->action_getUserMainDir();
586 if ($path) {
587 GeneralUtility::mkdir($path . $uid);
588 GeneralUtility::mkdir($path . $uid . '/_temp_/');
589 }
590 }
591
592 /**
593 * Get the path to the user home directory which is set in the localconf.php
594 *
595 * @return string Path
596 */
597 protected function action_getUserMainDir() {
598 $path = $GLOBALS['TYPO3_CONF_VARS']['BE']['userHomePath'];
599 // If path is set and a valid directory
600 if ($path && @is_dir($path) && $GLOBALS['TYPO3_CONF_VARS']['BE']['lockRootPath'] && GeneralUtility::isFirstPartOfStr($path, $GLOBALS['TYPO3_CONF_VARS']['BE']['lockRootPath']) && substr($path, -1) == '/') {
601 return $path;
602 }
603 }
604
605 /**
606 * Get all allowed usergroups which can be applied to a user record
607 *
608 * @param array $record sys_action record
609 * @param array $vars Selected be_user record
610 * @return string Rendered user groups
611 */
612 protected function getUsergroups($record, $vars) {
613 $content = '';
614 // Do nothing if no groups are allowed
615 if (empty($record['t1_allowed_groups'])) {
616 return $content;
617 }
618 $content .= '<option value=""></option>';
619 $grList = GeneralUtility::trimExplode(',', $record['t1_allowed_groups'], TRUE);
620 foreach ($grList as $group) {
621 $checkGroup = BackendUtility::getRecord('be_groups', $group);
622 if (is_array($checkGroup)) {
623 $selected = GeneralUtility::inList($vars['usergroup'], $checkGroup['uid']) ? ' selected="selected" ' : '';
624 $content .= '<option ' . $selected . 'value="' . $checkGroup['uid'] . '">' . htmlspecialchars($checkGroup['title']) . '</option>';
625 }
626 }
627 return $content;
628 }
629
630 /**
631 * Action to create a new record
632 *
633 * @param array $record sys_action record
634 * @return void Redirect to form to create a record
635 */
636 protected function viewNewRecord($record) {
637 $returnUrl = rawurlencode('mod.php?M=user_task');
638 $link = GeneralUtility::getIndpEnv('TYPO3_REQUEST_DIR') . $GLOBALS['BACK_PATH'] . 'alt_doc.php?returnUrl=' . $returnUrl . '&edit[' . $record['t3_tables'] . '][' . (int)$record['t3_listPid'] . ']=new';
639 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($link);
640 }
641
642 /**
643 * Action to edit records
644 *
645 * @param array $record sys_action record
646 * @return string list of records
647 */
648 protected function viewEditRecord($record) {
649 $content = '';
650 $actionList = array();
651 $dbAnalysis = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
652 $dbAnalysis->setFetchAllFields(TRUE);
653 $dbAnalysis->start($record['t4_recordsToEdit'], '*');
654 $dbAnalysis->getFromDB();
655 // collect the records
656 foreach ($dbAnalysis->itemArray as $el) {
657 $path = BackendUtility::getRecordPath($el['id'], $this->taskObject->perms_clause, $GLOBALS['BE_USER']->uc['titleLen']);
658 $record = BackendUtility::getRecord($el['table'], $dbAnalysis->results[$el['table']][$el['id']]);
659 $title = BackendUtility::getRecordTitle($el['table'], $dbAnalysis->results[$el['table']][$el['id']]);
660 $description = $GLOBALS['LANG']->sL($GLOBALS['TCA'][$el['table']]['ctrl']['title'], TRUE);
661 // @todo: which information could be needful
662 if (isset($record['crdate'])) {
663 $description .= ' - ' . BackendUtility::dateTimeAge($record['crdate']);
664 }
665 $actionList[$el['id']] = array(
666 'title' => $title,
667 'description' => BackendUtility::getRecordTitle($el['table'], $dbAnalysis->results[$el['table']][$el['id']]),
668 'descriptionHtml' => $description,
669 'link' => $GLOBALS['BACK_PATH'] . 'alt_doc.php?returnUrl=' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI')) . '&edit[' . $el['table'] . '][' . $el['id'] . ']=edit',
670 'icon' => \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord($el['table'], $dbAnalysis->results[$el['table']][$el['id']], array('title' => htmlspecialchars($path)))
671 );
672 }
673 // Render the record list
674 $content .= $this->taskObject->renderListMenu($actionList);
675 return $content;
676 }
677
678 /**
679 * Action to view the result of a SQL query
680 *
681 * @param array $record sys_action record
682 * @return string Result of the query
683 */
684 protected function viewSqlQuery($record) {
685 $content = '';
686 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('lowlevel')) {
687 $sql_query = unserialize($record['t2_data']);
688 if (!is_array($sql_query) || is_array($sql_query) && strtoupper(substr(trim($sql_query['qSelect']), 0, 6)) === 'SELECT') {
689 $actionContent = '';
690 $fullsearch = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\QueryView');
691 $fullsearch->formW = 40;
692 $fullsearch->noDownloadB = 1;
693 $type = $sql_query['qC']['search_query_makeQuery'];
694 if ($sql_query['qC']['labels_noprefix'] === 'on') {
695 $GLOBALS['SOBE']->MOD_SETTINGS['labels_noprefix'] = 'on';
696 }
697 $sqlQuery = $sql_query['qSelect'];
698 $queryIsEmpty = FALSE;
699 if ($sqlQuery) {
700 $res = $GLOBALS['TYPO3_DB']->sql_query($sqlQuery);
701 if (!$GLOBALS['TYPO3_DB']->sql_error()) {
702 $fullsearch->formW = 48;
703 // Additional configuration
704 $GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels'] = 1;
705 $cP = $fullsearch->getQueryResultCode($type, $res, $sql_query['qC']['queryTable']);
706 $actionContent = $cP['content'];
707 // If the result is rendered as csv or xml, show a download link
708 if ($type === 'csv' || $type === 'xml') {
709 $actionContent .= '<br /><br /><a href="' . GeneralUtility::getIndpEnv('REQUEST_URI') . '&download_file=1"><strong>' . $GLOBALS['LANG']->getLL('action_download_file') . '</strong></a>';
710 }
711 } else {
712 $actionContent .= $GLOBALS['TYPO3_DB']->sql_error();
713 }
714 } else {
715 // Query is empty (not built)
716 $queryIsEmpty = TRUE;
717 $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $GLOBALS['LANG']->getLL('action_emptyQuery', TRUE), $GLOBALS['LANG']->getLL('action_error'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
718 $content .= '<br />' . $flashMessage->render();
719 }
720 // Admin users are allowed to see and edit the query
721 if ($GLOBALS['BE_USER']->isAdmin()) {
722 if (!$queryIsEmpty) {
723 $actionContent .= '<hr /> ' . $fullsearch->tableWrap($sql_query['qSelect']);
724 }
725 $actionContent .= '<br /><a title="' . $GLOBALS['LANG']->getLL('action_editQuery') . '" href="'
726 . BackendUtility::getModuleUrl('system_dbint')
727 . '&id=' . '&SET[function]=search' . '&SET[search]=query'
728 . '&storeControl[STORE]=-' . $record['uid'] . '&storeControl[LOAD]=1' . '">
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', $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', $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', $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');
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 jumpToUrl(URL) {
793 window.location.href = URL;
794 return false;
795 }
796 function jumpExt(URL,anchor) {
797 var anc = anchor?anchor:"";
798 window.location.href = URL+(T3_THIS_LOCATION?"&returnUrl="+T3_THIS_LOCATION:"")+anc;
799 return false;
800 }
801 function jumpSelf(URL) {
802 window.location.href = URL+(T3_RETURN_URL?"&returnUrl="+T3_RETURN_URL:"");
803 return false;
804 }
805
806 function setHighlight(id) {
807 top.fsMod.recentIds["web"]=id;
808 top.fsMod.navFrameHighlightedID["web"]="pages"+id+"_"+top.fsMod.currentBank; // For highlighting
809
810 if (top.content && top.content.nav_frame && top.content.nav_frame.refresh_nav) {
811 top.content.nav_frame.refresh_nav();
812 }
813 }
814
815 ' . $dblist->CBfunctions() . '
816 function editRecords(table,idList,addParams,CBflag) {
817 window.location.href="' . $GLOBALS['BACK_PATH'] . 'alt_doc.php?returnUrl=' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI')) . '&edit["+table+"]["+idList+"]=edit"+addParams;
818 }
819 function editList(table,idList) {
820 var list="";
821
822 // Checking how many is checked, how many is not
823 var pointer=0;
824 var pos = idList.indexOf(",");
825 while (pos!=-1) {
826 if (cbValue(table+"|"+idList.substr(pointer,pos-pointer))) {
827 list+=idList.substr(pointer,pos-pointer)+",";
828 }
829 pointer=pos+1;
830 pos = idList.indexOf(",",pointer);
831 }
832 if (cbValue(table+"|"+idList.substr(pointer))) {
833 list+=idList.substr(pointer)+",";
834 }
835
836 return list ? list : idList;
837 }
838 T3_THIS_LOCATION = "' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI')) . '";
839
840 if (top.fsMod) top.fsMod.recentIds["web"] = ' . (int)$this->id . ';
841 ');
842 // Setting up the context sensitive menu:
843 $this->taskObject->doc->getContextMenuCode();
844 // Begin to compile the whole page
845 $content .= '<form action="' . htmlspecialchars($dblist->listURL()) . '" method="post" name="dblistForm">' . $dblist->HTMLcode . '<input type="hidden" name="cmd_table" /><input type="hidden" name="cmd" />
846 </form>';
847 // If a listing was produced, create the page footer with search form etc:
848 if ($dblist->HTMLcode) {
849 // Making field select box (when extended view for a single table is enabled):
850 if ($dblist->table) {
851 $tmpBackpath = $GLOBALS['BACK_PATH'];
852 $GLOBALS['BACK_PATH'] = '';
853 $content .= $dblist->fieldSelectBox($dblist->table);
854 $GLOBALS['BACK_PATH'] = $tmpBackpath;
855 }
856 }
857 } else {
858 // Not enough rights to access the list view or the page
859 $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $GLOBALS['LANG']->getLL('action_error-access', TRUE), $GLOBALS['LANG']->getLL('action_error'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
860 $content .= $flashMessage->render();
861 }
862 return $content;
863 }
864
865 }