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