Cleanup: Set SVN end of line property
[Packages/TYPO3.CMS.git] / typo3 / sysext / sys_action / class.tx_sysaction.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2010 Kasper Skaarhoj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
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 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
29 */
30
31 class tx_sysaction extends mod_user_task {
32 var $todoTypesCache = array();
33 var $insCounter=0;
34 var $xCol;
35
36 /**
37 * TCEforms instance
38 *
39 * @var t3lib_TCEforms
40 */
41 var $t3lib_TCEforms;
42
43 function overview_main() {
44 $mC = $this->renderActionList();
45 if ($mC) {
46 $icon = '<img src="'.$this->backPath.t3lib_extMgm::extRelPath("sys_action").'ext_icon.gif" width=18 height=16 class="absmiddle">';
47 return $this->mkMenuConfig($icon.$this->headLink("tx_sysaction",1),'',$mC);
48 }
49 }
50 function main() {
51 global $SOBE,$BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;
52
53 return $this->renderActions();
54 }
55 function JScode() {
56 $this->t3lib_TCEforms = t3lib_div::makeInstance("t3lib_TCEforms");
57 $this->t3lib_TCEforms->backPath = $GLOBALS["BACK_PATH"];
58 return $this->t3lib_TCEforms->dbFileCon();
59 }
60
61 // ************************
62 // ACTIONS
63 // ***********************
64 function renderActions() {
65 global $LANG;
66 $uid = t3lib_div::intInRange(t3lib_div::_GP("sys_action_uid"),0);
67 $out="";
68 $header="";
69 if ($uid) {
70 $res = $this->getActionResPointer($uid);
71 if($actionRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
72
73 // Action header:
74 $header = t3lib_iconworks::getIconImage("sys_action",$actionRow,$this->backPath,'hspace="2" class="absmiddle"').'<strong>'.htmlspecialchars($actionRow["title"]).'</strong>';
75 $out.='<table border=0 cellpadding=0 cellspacing=1 width=100%>
76 <tr><td colspan=2 class="bgColor5">' . $header . '</td></tr>
77 <tr>
78 <td width=1% valign=top class="bgColor4">' . $LANG->sL(t3lib_BEfunc::getItemLabel('sys_action', 'type')). '&nbsp;' . '</td>
79 <td valign=top class="bgColor4">' . htmlspecialchars(t3lib_BEfunc::getProcessedValue('sys_action', 'type', $actionRow['type'])) . '</td>
80 </tr>
81 <tr>
82 <td width=1% valign=top class="bgColor4">' . $LANG->sL(t3lib_BEfunc::getItemLabel('sys_action', 'description')) . '&nbsp;' . '</td>
83 <td valign=top class="bgColor4">' . nl2br(htmlspecialchars($actionRow['description'])) . '</td>
84 </tr>';
85 $out.='</table>';
86 $theCode = $this->pObj->doc->section("",$out,0,1);
87
88
89
90 // Types of actions:
91 switch($actionRow["type"]) {
92 case 1: // new BE user
93 $actionContent="";
94 $beRec = t3lib_BEfunc::getRecord("be_users",intval($actionRow["t1_copy_of_user"]));
95 if (is_array($beRec)) {
96 // Create or update:
97 $inData = t3lib_div::_GP("data");
98 // debug($inData);
99 $userRecord="";
100 $newFlag=0;
101 if (is_array($inData["be_users"])) {
102 $nId = $this->action_t1_createUpdateBeUser($inData["be_users"],$actionRow);
103 $userRecord=t3lib_BEfunc::getRecord("be_users",$nId);
104 }
105 if (t3lib_div::_GP("be_users_uid")) {
106 $userRecord = t3lib_BEfunc::getRecord('be_users', t3lib_div::_GP('be_users_uid'), '*', ' AND cruser_id='.intval($this->BE_USER->user['uid']).' AND createdByAction='.intval($actionRow['uid']));
107 }
108 if (!is_array($userRecord)) {
109 $userRecord=array();
110 if (is_array($inData["be_users"]["NEW"])) { $userRecord=$inData["be_users"]["NEW"]; }
111 $userRecord["uid"]="NEW";
112 $newFlag=1;
113 }
114
115 // List of users...
116 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'be_users', 'pid=0 AND cruser_id='.intval($this->BE_USER->user['uid']).' AND createdByAction='.intval($actionRow['uid']).t3lib_BEfunc::deleteClause('be_users'), '', 'username');
117 $lines = array();
118 while($uRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
119 $lines[] = "<nobr>".($uRow["uid"]==$userRecord["uid"]?"<strong>":"").$this->action_linkUserName(t3lib_iconworks::getIconImage("be_users",$uRow,$this->backPath,'title="uid='.$uRow["uid"].'" hspace="2" align="top"').$uRow["username"]." (".$uRow["realName"].")".($uRow["uid"]==$userRecord["uid"]?"</strong>":"")."</nobr>",$actionRow["uid"],$uRow["uid"])."<br>";
120 }
121 if (count($lines)) {
122 $theCode.= $this->pObj->doc->section($LANG->getLL("action_t1_listOfUsers"),implode("",$lines),0,1);
123 }
124
125 $formA=Array();
126 $opt=array();
127
128 $grList = t3lib_div::trimExplode(",",$actionRow["t1_allowed_groups"],1);
129 $opt[]='<option value=""></option>';
130 foreach ($grlist as $gu) {
131 $checkGr = t3lib_BEfunc::getRecord("be_groups",$gu);
132 if (is_array($checkGr)) $opt[]='<option value="'.$checkGr["uid"].'"'.(t3lib_div::inList($userRecord["usergroup"],$checkGr["uid"])?" selected":"").'>'.htmlspecialchars($checkGr["title"]).'</option>';
133 }
134
135 $formA[]=array($LANG->getLL("action_BEu_hidden").":&nbsp;", '<input type="checkbox" name="data[be_users]['.$userRecord["uid"].'][disable]" value=1'.($userRecord["disable"]?" checked":"").'>');
136 $formA[]=array($LANG->getLL("action_BEu_username").":&nbsp;", '<input type="text" name="data[be_users]['.$userRecord["uid"].'][username]" max=15 value="'.htmlspecialchars($userRecord["username"]).'"'.$this->pObj->doc->formWidth(15).'>');
137 $formA[]=array($LANG->getLL("action_BEu_password").":&nbsp;", '<input type="password" name="data[be_users]['.$userRecord["uid"].'][password]" max=40'.$this->pObj->doc->formWidth(15).'>');
138 $formA[]=array($LANG->getLL("action_BEu_realName").":&nbsp;", '<input type="text" name="data[be_users]['.$userRecord["uid"].'][realName]" value="'.htmlspecialchars($userRecord["realName"]).'"'.$this->pObj->doc->formWidth(30).'>');
139 $formA[]=array($LANG->getLL("action_BEu_email").":&nbsp;", '<input type="text" name="data[be_users]['.$userRecord["uid"].'][email]" value="'.htmlspecialchars($userRecord["email"]).'"'.$this->pObj->doc->formWidth(30).'>');
140 if (count($grList)) {
141 $formA[]=array($LANG->getLL("action_BEu_usergroups").":&nbsp;", '<select size='.t3lib_div::intInRange(count($opt),2).' multiple name="data[be_users]['.$userRecord["uid"].'][usergroups][]">'.implode("",$opt).'</select>');
142 }
143 // DB mounts:
144 $loadDB = t3lib_div::makeInstance("t3lib_loadDBGroup");
145 $loadDB->start($userRecord["db_mountpoints"],"pages");
146 $params=array(
147 "size" => 3
148 );
149 $formA[]=array($LANG->getLL("action_BEu_db_mount").":&nbsp;", $this->t3lib_TCEforms->dbFileIcons('data[be_users]['.$userRecord["uid"].'][db_mountpoints]','db','pages',$loadDB->itemArray,"",$params));
150
151 $formA[]=array("&nbsp;","&nbsp;");
152 $formA[]=array("&nbsp;", '<input type=hidden value="'.$uid.'" name="sys_action_uid"><input type=hidden value="'.t3lib_div::_GP("be_users_uid").'" name="be_users_uid"><input type=hidden value="'.$uid.'" name="sys_action_uid"><input type="submit" name="submit" value="'.$LANG->getLL($newFlag?"lCreate":"lUpdate").'">'.(!$newFlag?' <input type="submit" name="_delete_" value="'.$LANG->getLL("lDelete").'" onClick="return confirm('.$GLOBALS['LANG']->JScharCode($LANG->getLL("lDelete_warning")).');">':''));
153
154 if (!$newFlag) {
155 $p = 'uid='.$userRecord["uid"].", ".$LANG->getLL("lHomedir").": ";
156 $hPath=$this->action_getUserMainDir();
157 if ($hPath && @is_dir($hPath.$userRecord["uid"]."/")) {
158 $p.= $hPath;
159 } else {
160 $p.= $LANG->getLL("lNone");
161 }
162 $actionContent.=t3lib_iconworks::getIconImage("be_users",$userRecord,$this->backPath,'title="'.htmlspecialchars($p).'" hspace=2 align=top').$userRecord["username"]." (".$userRecord["realName"].")";
163 }
164 $actionContent.=$this->pObj->doc->table($formA);
165 $theCode.= $this->pObj->doc->section($LANG->getLL($newFlag?"action_Create":"action_Update"),$actionContent,0,1);
166 } else {
167 $theCode.= $this->pObj->doc->section($LANG->getLL("action_error"),'<span class="typo3-red">'.$LANG->getLL("action_notReady").'</span>',0,1);
168 }
169 break;
170 case 2: //SQL query
171 if (t3lib_extMgm::isLoaded("lowlevel")) {
172 $sql_query = unserialize($actionRow["t2_data"]);
173 if (is_array($sql_query) && strtoupper(substr(trim($sql_query["qSelect"]),0,6))=="SELECT") {
174 $fullsearch = t3lib_div::makeInstance("t3lib_fullsearch");
175 $fullsearch->formW = 40;
176 $fullsearch->noDownloadB=1;
177 $actionContent="";
178 // debug($sql_query);
179 $type = $sql_query["qC"]["search_query_makeQuery"];
180 $res = $GLOBALS['TYPO3_DB']->sql_query($sql_query["qSelect"]);
181 if (!$GLOBALS['TYPO3_DB']->sql_error()) {
182 $fullsearch->formW=48;
183 $cP=$fullsearch->getQueryResultCode($type,$res,$sql_query["qC"]["queryTable"]);
184 $actionContent=$cP["content"];
185 if ($type=="csv" || $type=="xml") {
186 $actionContent.='<BR><BR><a href="'.t3lib_div::getIndpEnv("REQUEST_URI").'&download_file=1"><strong>'.$LANG->getLL("action_download_file").'</strong></a>';
187 }
188 } else {
189 $actionContent.=$GLOBALS['TYPO3_DB']->sql_error();
190 }
191 if ($this->BE_USER->isAdmin()) {
192 $actionContent.="<HR>".$fullsearch->tableWrap($sql_query["qSelect"]);
193 $actionContent.='<BR><strong><a href="'.$this->backPath.t3lib_extMgm::extRelPath("lowlevel").'dbint/index.php?id='.
194 '&SET[function]=search'.
195 '&SET[search]=query'.
196 '&storeControl[STORE]=-'.$actionRow["uid"].
197 '&storeControl[LOAD]=1'.
198 '">' . $GLOBALS['LANG']->getLL('action_editQuery') . '</a></strong>';
199 }
200 $theCode.= $this->pObj->doc->section($LANG->getLL("action_t2_result"),$actionContent,0,1);
201 } else {
202 $theCode.= $this->pObj->doc->section($LANG->getLL("action_error"),'<span class="typo3-red">'.$LANG->getLL("action_notReady").'</span>',0,1);
203 }
204 } else {
205 $theCode.= $this->pObj->doc->section(
206 $GLOBALS['LANG']->getLL('action_error'),
207 '<span class="typo3-red">' . $GLOBALS['LANG']->getLL('action_lowlevelMissing', true) . '</span>',
208 0, 1
209 );
210 }
211 break;
212 case 3: //list records
213 return htmlspecialchars($this->headLInk('tx_taskcenterrootlist', 1)). '<br />'. $this->urlInIframe($this->backPath."db_list.php?id=".intval($actionRow["t3_listPid"])."&table=".$actionRow["t3_tables"],1);
214 break;
215 case 4: //edit records
216 $dbAnalysis = t3lib_div::makeInstance("t3lib_loadDBGroup");
217 $dbAnalysis->fromTC=0;
218 $dbAnalysis->start($actionRow["t4_recordsToEdit"],"*");
219 $dbAnalysis->getFromDB();
220
221 $lines=array();
222 foreach ($dbAnalysis->itemArray as $el) {
223 $path = t3lib_BEfunc::getRecordPath ($el["id"],$this->perms_clause,$this->BE_USER->uc["titleLen"]);
224 $lines[]='<tr>
225 <td nowrap class="bgColor4">'.
226 '<a href="'.$this->backPath.'alt_doc.php?returnUrl='.rawurlencode(t3lib_div::getIndpEnv("REQUEST_URI")).'&edit['.$el["table"].']['.$el["id"].']=edit">'.
227 t3lib_iconworks::getIconImage($el["table"],$dbAnalysis->results[$el["table"]][$el["id"]],$this->backPath,'hspace="2" align="top" title="'.htmlspecialchars($path).'"').
228 t3lib_BEfunc::getRecordTitle($el["table"],$dbAnalysis->results[$el["table"]][$el["id"]],TRUE).'</a></td>
229 </tr>';
230 }
231 $actionContent = '<table border=0 cellpadding=0 cellspacing=2>'.implode("",$lines).'</table>';
232 $theCode.= $this->pObj->doc->section($LANG->getLL("action_t4_edit"),$actionContent,0,1);
233 break;
234 case 5: //new record
235 return htmlspecialchars($this->headLInk('tx_taskcenterrootlist', 1)). '<br />'. $this->urlInIframe($this->backPath.'alt_doc.php?returnUrl='.rawurlencode('db_list.php?id='.intval($actionRow['t3_listPid']).'&table='.$actionRow['t3_tables']).'&edit['.$actionRow['t3_tables'].']['.intval($actionRow['t3_listPid']).']=new',1);
236 break;
237 default:
238 $theCode.= $this->pObj->doc->section($LANG->getLL("action_error"),'<span class="typo3-red">'.$LANG->getLL("action_noType").'</span>',0,1);
239 break;
240 }
241
242 }
243 }
244 return $theCode;
245 }
246 function getActionResPointer($uid=0) {
247 if ($this->BE_USER->isAdmin()) {
248 $wQ='';
249 if (intval($uid)>0) $wQ.=' AND sys_action.uid='.intval($uid);
250
251 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_action', 'sys_action.pid=0'.$wQ, '', 'sys_action.sorting');
252 } else {
253 $wQ = 'be_groups.uid IN ('.($this->BE_USER->groupList?$this->BE_USER->groupList:0).')';
254 $hQ = 'AND sys_action.hidden=0 ';
255 if (intval($uid)>0) $wQ.=' AND sys_action.uid='.intval($uid);
256
257 $res = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query(
258 'sys_action.*',
259 'sys_action',
260 'sys_action_asgr_mm',
261 'be_groups',
262 ' AND '.$wQ.' AND sys_action.pid=0 '.$hQ,
263 'sys_action.uid',
264 'sys_action.sorting'
265 );
266 }
267
268 return $res;
269 }
270 function renderActionList() {
271 global $LANG;
272
273 $res = $this->getActionResPointer();
274 $lines=array();
275 while($actionRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
276 $lines[]='<nobr>'.t3lib_iconworks::getIconImage("sys_action",$actionRow,$this->backPath,'hspace="2" align="top"').$this->action_link($this->fixed_lgd(htmlspecialchars($actionRow['title'])),$actionRow['uid'],htmlspecialchars($actionRow['description'])).'</nobr><br />';
277 }
278 $out = implode("",$lines);
279 return $out;
280 }
281 function action_link($str,$id,$title="") {
282 $str='<a href="index.php?SET[function]=tx_sysaction&sys_action_uid='.$id.'" onClick="this.blur();" title="'.htmlspecialchars($title).'">'.$str.'</a>';
283 return $str;
284 }
285 function action_linkUserName($str,$id,$bid) {
286 $str='<a href="index.php?sys_action_uid='.$id.'&be_users_uid='.$bid.'" onClick="this.blur();">'.$str.'</a>';
287 return $str;
288 }
289 function action_t1_createUpdateBeUser($arr,$actionRow=array()) {
290 reset($arr);
291 $key = key($arr);
292 $data="";
293 $nId=0;
294 $BEuid = $actionRow["t1_copy_of_user"];
295 if ($key=="NEW") {
296 $beRec = t3lib_BEfunc::getRecord("be_users",intval($BEuid));
297 if (is_array($beRec) && trim($arr[$key]["password"]) && $this->fixUsername($arr[$key]["username"],$actionRow["t1_userprefix"])) {
298 // debug($arr[$key]);
299 $data=array();
300 $data["be_users"][$key]=$beRec;
301 $data["be_users"][$key]["username"]=$this->fixUsername($arr[$key]["username"],$actionRow["t1_userprefix"]);
302 $data["be_users"][$key]["password"]=md5(trim($arr[$key]["password"]));
303 $data["be_users"][$key]["realName"]=$arr[$key]["realName"];
304 $data["be_users"][$key]["email"]=$arr[$key]["email"];
305 $data["be_users"][$key]["disable"]=intval($arr[$key]["disable"]);
306 $data["be_users"][$key]["admin"]=0;
307 $data["be_users"][$key]["usergroup"] = $this->fixUserGroup($data["be_users"][$key]["usergroup"],$actionRow["t1_allowed_groups"],$arr[$key]["usergroups"]);
308 $data["be_users"][$key]["db_mountpoints"]=$arr[$key]["db_mountpoints"];
309 $data["be_users"][$key]["createdByAction"]=$actionRow["uid"];
310 }
311 } else {
312 $beRec = t3lib_BEfunc::getRecord("be_users",intval($key));
313 if (is_array($beRec) && $beRec["cruser_id"]==$this->BE_USER->user["uid"]) {
314 if (t3lib_div::_GP("_delete_")) {
315 // delete... ?
316 $cmd=array();
317 $cmd["be_users"][$key]["delete"]=1;
318
319 $tce = t3lib_div::makeInstance("t3lib_TCEmain");
320 $tce->stripslashes_values=0;
321 $tce->start(Array(),$cmd,$this->BE_USER);
322 $tce->admin=1;
323 $tce->process_cmdmap();
324 // debug($cmd);
325 $nId = 0;
326 } elseif ($this->fixUsername($arr[$key]["username"],$actionRow["t1_userprefix"])) {
327 // check ownership...
328 $data=array();
329 $data["be_users"][$key]["username"]=$this->fixUsername($arr[$key]["username"],$actionRow["t1_userprefix"]);
330 if (trim($arr[$key]["password"])) {$data["be_users"][$key]["password"]=md5(trim($arr[$key]["password"]));}
331
332 $data["be_users"][$key]["realName"]=$arr[$key]["realName"];
333 $data["be_users"][$key]["email"]=$arr[$key]["email"];
334 $data["be_users"][$key]["disable"]=intval($arr[$key]["disable"]);
335 $data["be_users"][$key]["admin"]=0;
336 $data["be_users"][$key]["usergroup"] = $this->fixUserGroup($beRec["usergroup"],$actionRow["t1_allowed_groups"],$arr[$key]["usergroups"]);
337 $data["be_users"][$key]["db_mountpoints"]=$arr[$key]["db_mountpoints"];
338 $nId=$key;
339 }
340 }
341 }
342
343
344 if (is_array($data)) {
345 $tce = t3lib_div::makeInstance("t3lib_TCEmain");
346 $tce->stripslashes_values=0;
347 $tce->start($data,Array(),$this->BE_USER);
348 $tce->admin=1;
349 $tce->process_datamap();
350 $nId = intval($tce->substNEWwithIDs["NEW"]);
351 if ($nId) { // Create
352 $this->action_createDir($nId);
353 } else { // update
354 $nId=intval($key);
355 }
356 unset($tce);
357 }
358 return $nId;
359 }
360 function fixUsername($username,$prefix) {
361 $username=trim($username);
362 $prefix=trim($prefix);
363 $username=preg_replace('/^'.quotemeta($prefix).'/','',$username);
364
365 if ($username) {
366 return $prefix.$username;
367 } else return false;
368 }
369 function fixUserGroup($curUserGroup,$allowedGroups,$inGroups) {
370 // User group:
371 // All current groups:
372 $cGroups = array_flip(t3lib_div::trimExplode(",",$curUserGroup,1));
373 $grList = t3lib_div::intExplode(",",$allowedGroups);
374 foreach ($grList as $gu) {
375 unset($cGroups[$gu]); // Remove the group if it's in the array for some reason...
376 }
377 // reverse array again and set incoming groups:
378 $cGroups=array_keys($cGroups);
379 if (is_array($inGroups)) {
380 foreach ($inGroups as $gu) {
381 $checkGr = t3lib_BEfunc::getRecord("be_groups",$gu);
382 if (is_array($checkGr) && in_array($gu,$grList)) {
383 $cGroups[]=$gu;
384 }
385 }
386 }
387 return implode(",",$cGroups);
388 }
389 function action_createDir($uid) {
390 $path = $this->action_getUserMainDir();
391 if ($path) {
392 @mkdir($path.$uid,0755);
393 @mkdir($path.$uid."/_temp_",0755);
394 // debug($path);
395 }
396 }
397 function action_getUserMainDir() {
398 $path = $GLOBALS["TYPO3_CONF_VARS"]["BE"]["userHomePath"];
399 if ($path && @is_dir($path) &&
400 $GLOBALS["TYPO3_CONF_VARS"]["BE"]["lockRootPath"] &&
401 t3lib_div::isFirstPartOfStr($path,$GLOBALS["TYPO3_CONF_VARS"]["BE"]["lockRootPath"]) &&
402 substr($path,-1)=="/"
403 ) {
404 return $path;
405 }
406 }
407 }
408
409 if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/sys_action/class.tx_sysaction.php"]) {
410 include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/sys_action/class.tx_sysaction.php"]);
411 }
412
413 ?>