1 <?php
3 class csvimport extends plugin
4 {
5 /* Definitions */
6 var $plHeadline= "LDIF export";
7 var $plDescription= "This does something";
8 var $access= "";
10 /* Array with csv informations*/
11 var $csvinfo=array();
13 /* attribute list for save action */
14 var $attributes= array();
15 var $objectclasses= array();
17 function csvimport ($config, $dn= NULL)
18 {
19 /* Include config object */
20 $this->config= $config;
22 $ui= get_userinfo();
23 $acl= get_permissions ($config->current['BASE'], $ui->subtreeACL);
24 $acl= get_module_permission($acl, "all", $config->current['BASE']);
25 $this->access= $acl;
26 }
28 function execute()
29 {
30 /* initiate smarty */
31 $smarty= get_smarty();
32 $smarty->assign("sorted",0);
33 $smarty->assign("fileup",0);
35 /* Get the LDAP link, to generate the Export */
36 $ldap = $this->config->get_ldap_link();
38 $arrtemplates = array();
39 $tempvar =0;
41 /* Array to fill in Formfields */
42 $arrtemplates[$tempvar] = "None";
43 if(!is_array($this->csvinfo)){
44 $this->csvinfo=array();
45 }
47 /* Set Usertemplate information and get all Attribute from userclass */
48 unset ($this->csvinfo['arrtemplates']);
49 unset ($this->csvinfo['arrtempls']);
51 /* Generate Template Array, Attribute Array */
52 if(!isset($this->csvinfo['arrtempls'])){
54 /* Generating attributes */
55 $attrs = @get_class_vars("user");
56 $this->csvinfo['attr'] = $attrs['attributes'];
58 /* Attach the choise none to discard a csv col */
59 $new = array();
60 $new[0]="none";
61 $new[1]="userPassword";
62 for($c = 0; $c < count($this->csvinfo['attr']);$c++){
63 $new[$c+2]=$this->csvinfo['attr'][$c];
64 }
66 $this->csvinfo['attr'] = $new;
68 /* Search all Templates */
69 $this->csvinfo['templates'] = $ldap->search("(objectClass=gosaUserTemplate)",array("*"));
71 /* Array to handle selected Form fiels */
72 $this->csvinfo['arrtempls'][$tempvar]['name'] = "";
73 $this->csvinfo['arrtempls'][$tempvar]['dn'] = "";
75 $this->csvinfo['arrtemplates'][]="None";
77 while($temp = $ldap->fetch($this->csvinfo['templates'])){
78 $tempvar ++;
79 $this->csvinfo['arrtemplates'][$tempvar] = $temp['sn'][0]." - ".$this->config->idepartments[preg_replace("/^[^,]+,".get_people_ou()."/", "", $temp['dn'])];
80 $this->csvinfo['arrtempls'] [$tempvar]['name'] = $temp['sn'][0];
81 $this->csvinfo['arrtempls'] [$tempvar]['dn'] = $temp['dn'];
82 }
83 }
85 $arr_temp = array_flip($this->csvinfo['attr']);
86 $this->csvinfo['arr_selected']= array(0,0,$arr_temp['uid'],$arr_temp['sn'],$arr_temp['givenName'],$arr_temp['userPassword']);
88 $smarty->assign("templates",$this->csvinfo['arrtemplates']);
89 $smarty->assign("attrs",$this->csvinfo['attr']);
91 /* Check permissions for import */
92 if ($this->access != '#all#'){
93 print_red(_("You've no permission to do CSV imports."));
94 return ($smarty->fetch (get_template_path('contentcsv.tpl', TRUE)));
95 }
97 /* If the given dat from the csv File are sorted by the attributes */
98 if(isset($_POST['sorted'])) {
100 /* Template Handling */
101 $smarty->assign("fileup",TRUE);
102 $smarty->assign("sorted",TRUE);
104 for($i = 0 ; $i < $this->csvinfo['rows'];$i++){
105 /* Preparing given Data */
106 $this->csvinfo['sorteddata'][$i]=array();
108 /* Go through every col */
109 for($a = 1; $a <= ($this->csvinfo['cols']+1); $a ++) {
110 /* Append a status col at last, to show possibly errors */
111 if($a==($this->csvinfo['cols']+1)){
112 $this->csvinfo['sorteddata'][$i]["status"]="-";
113 } else {
114 $cc = ($i * ($this->csvinfo['cols']))+$a;
116 /* If theres a Attribut given for this col */
117 if(!empty($_POST[('row'.$a)])) {
118 $this->csvinfo['sorteddata'][$i][$this->csvinfo['attr'][$_POST[('row'.$a)]]]=$this->csvinfo['data'][($cc-1)];
119 }
120 }
121 }
122 }
124 /* aleast one entry */
125 $rowcnt=0;
126 if(isset($this->csvinfo['sorteddata'])){
127 $rowcnt ++;
129 /* Set the col count */
130 $tblcolcount= count ($this->csvinfo['cols']);
132 /* entrie count */
133 $tbl_entries= count ($this->csvinfo['count']);
135 /* row count */
136 $tbl_rows = $tbl_entries / $tblcolcount;
138 /* No error occured */
139 $failing = FALSE;
141 /* For every row */
142 for($i = 0; $i < $this->csvinfo['rows']; $i++){
144 /* Get elemenet */
145 $dat = $this->csvinfo['sorteddata'][$i];
148 /* If not all needed vars set .... */
149 if( ( empty($dat['sn']))||( empty($dat['givenName']))||( empty($dat['uid']))||
150 (!isset($dat['sn']))||(!isset($dat['givenName']))||(!isset($dat['uid']))){
152 /* Output Error */
154 print_red (_("Need 'sn','givenName' and 'uid' to create user."));
155 } else {
157 /* Register usertab to trigger edit dialog */
158 $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'], 'new');
159 $keys = array_keys($dat);
162 /* Set all submitted vars */
163 foreach($keys as $key) {
164 if($key != "status"){
165 $this->usertab->$key = $dat[$key];
166 }
167 }
169 /* Initialize template */
170 if($this->csvinfo['tplid']!=0){
171 $this->usertab->adapt_from_template($this->csvinfo['arrtempls'][$this->csvinfo['tplid']]['dn']);
172 $this->usertab->save_object();
173 } else {
175 /* Reset vars */
176 foreach($keys as $key) {
177 if($key != "status"){
178 $this->usertab->by_object['user']->$key = $dat[$key];
179 }
180 }
181 }
183 // Setting user Password
184 if((isset($dat['userPassword']))&&(!empty($dat['userPassword'])))
185 $password = $dat['userPassword'];
186 else
187 {
188 $rand_str="";
189 $feed = "0123456789abcdefghijklmnopqrstuvwxyz";
190 for ($i=0; $i < 8; $i++)
191 {
192 $rand_str .= substr($feed, rand(0, strlen($feed)-1), 1);
193 }
194 $password = $rand_str;
195 }
197 /* Insert in the LDAP tree */
198 if(count($this->usertab->check())) {
199 show_errors($this->usertab->check());
200 $this->csvinfo['sorteddata'][$i]['status']="<b>"._("failed")."</b>";
201 $failing = $i+1;
202 break;
203 } else {
204 $this->csvinfo['sorteddata'][$i]['status']=_("ok");
205 $this->usertab->save();
206 change_password($this->usertab->dn,$password);
207 }
208 }
209 }
211 $pointsbefore = FALSE;
212 $pointsafter = FALSE;
214 /* Get Attributs for Tableheadline */
215 $this->csvinfo['colsstr'] = array();
216 for($i =1; $i <= $this->csvinfo['cols']; $i++) {
217 if(!empty($_POST[('row'.$i)])){
218 $this->csvinfo['colsstr'][$i] = $this->csvinfo['attr'][$_POST[('row'.$i)]];
219 }
220 }
222 /*Create array with data for the template */
223 if($failing == FALSE){
224 for($i =1; $i <= $this->csvinfo['shownrowscount']; $i++) {
225 foreach($this->csvinfo['sorteddata'][($i-1)] as $dat){
226 $dataouts[]=$dat;
227 }
228 }
230 if($this->csvinfo['rows']> $this->csvinfo['shownrowscount']){
231 $pointsafter = TRUE;
232 }
233 } else {
234 $pointsbefore = TRUE;
235 $pointsafter = TRUE;
237 $begin = $failing -3;
239 if($begin <0) {
240 $begin =0;
241 $pointsbefore = FALSE;
242 }
244 $end = $failing + 2;
246 if($end > $this->csvinfo['rows']) {
247 $end = $this->csvinfo['rows']+1;
248 $pointsafter = FALSE;
249 }
250 $this->csvinfo['shownrowscount']=$end - $begin;
252 if($this->csvinfo['shownrowscount']> $this->csvinfo['rows'])$this->csvinfo['shownrowscount']=$this->csvinfo['rows'];
254 for($i =$begin; $i <= $end; $i++) {
255 foreach($this->csvinfo['sorteddata'][($i-1)] as $dat){
256 $dataouts[]=$dat;
257 }
258 }
259 }
260 $tmparr2 = $this->csvinfo['colsstr'];
261 $tmparr2[]=_("status");
263 /* Error? */
264 if ($failing){
265 print_red(sprintf(_("An Error Occured while inserting entry %s - process aborted"), $failing));
266 }
267 $smarty->assign("error",$failing);
269 /* Set vars to smarty */
270 $smarty->assign("cols",count($tmparr2));
271 $smarty->assign("anz" ,$this->csvinfo['count']);
272 $smarty->assign("rows",$this->csvinfo['shownrowscount']);
274 $smarty->assign("head",$tmparr2);
275 $smarty->assign("data",$dataouts);
277 /* Set other vars */
278 $smarty->assign("i",1);
279 $smarty->assign("ie",0);
280 $smarty->assign("tplid",$this->csvinfo['tplid']);
282 $smarty->assign("pointsafter",$pointsafter);
283 $smarty->assign("pointsbefore",$pointsbefore);
284 } else {
285 /* Set Template ID */
286 $tplid = $_POST['tplid'];
289 $smarty->assign("tpl",$arrtemplates[$tplid]);
290 print_red (_("Nothing to import!"));
291 }
293 /* If theres a File uploaded */
294 } else {
295 /* Check if theres a file uploaded */
296 if(!empty($_FILES['userfile']['name'])){
297 if((!isset($_FILES['userfile']['name']))||(!isset($_POST['fileup'])))
298 {
299 print_red(_("There is no file uploaded."));
300 $smarty->assign("LDIFError",TRUE);
301 }
302 elseif(!$_FILES['userfile']['size'] > 0 )
303 {
304 print_red(_("The specified file is empty."));
305 $smarty->assign("LDIFError",TRUE);
306 }
307 /* Is there a tmp file, which we can use ? */
308 elseif(!file_exists($_FILES['userfile']['tmp_name']))
309 {
310 print_red(_("There is no file uploaded."));
311 $smarty->assign("LDIFError",TRUE);
312 }
313 elseif(!$handle = @fopen($_FILES['userfile']['tmp_name'],"r"))
314 {
315 print_red(_("There is no file uploaded."));
316 $smarty->assign("LDIFError",TRUE);
317 }
318 else
319 {
320 $smarty->assign("fileup",TRUE);
321 $str = "";
323 /* Reading content */
324 while(!feof($handle)) {
325 $str .= fread($handle,1024);
326 }
328 $lines = split("\n",$str);
329 $anz = 0;
330 $rest = 0;
331 $data = array();
333 /* check colum count */
334 foreach($lines as $line) {
335 /* continue if theres a comment */
336 if(substr(trim($line),0,1)=="#"){
337 continue;
338 }
340 $line= str_replace ("\t","",$line);
341 $cells = split(",",$line ) ;
343 if(count($cells)> $anz ){
344 $anz = count($cells);
345 }
346 }
348 /* At least one entry */
349 if($anz >1) {
351 /* Generate array with outpu info */
352 foreach($lines as $line) {
353 $rest = 0;
354 $cnt = 0;
356 /* dont use comments or empty lines */
357 if((substr(trim($line),0,1)=="#")||(empty($line))){
358 continue;
359 }
361 /* replace \t to , */
362 $line= str_replace ("\t" ,"" ,$line);
364 /* get all elements */
365 $cells = split(",",$line ) ;
367 /* attach all elements to data array */
368 foreach($cells as $cell) {
369 if(!empty($cell)) {
370 $cnt++;
371 $data[]=trim($cell);
372 }
373 }
375 /* cell count less than anz, attach some empty fields */
376 if(($cnt != $anz)&&(!empty($cnt))) {
377 $rest = $anz - $cnt;
378 for($i = 0 ; $i < $rest ; $i ++){
379 $data[] = " ";
380 }
381 }
382 }
384 unset($this->csvinfo['sorteddata']);
385 unset($this->csvinfo['colsstr']);
386 unset($this->csvinfo['sorteddata']);
388 $this->csvinfo['cols'] = $anz;
389 $this->csvinfo['data'] = array();
390 $this->csvinfo['data'] = $data;
391 $this->csvinfo['count'] = count($this->csvinfo['data']);
393 if($this->csvinfo['count']> (6* $this->csvinfo['cols'])) {
394 /* only show 6 rows in ouptuttablei */
395 $datouttemp = array_chunk($this->csvinfo['data'],(6* $this->csvinfo['cols']));
396 $this->csvinfo['dataout']=$datouttemp[0];
397 $this->csvinfo['shownrowscount'] = 6;
398 } else {
399 $this->csvinfo['shownrowscount'] = (count($this->csvinfo['data']))/$this->csvinfo['cols'];
400 $this->csvinfo['dataout']= $this->csvinfo['data'];
401 }
403 $this->csvinfo['tplid'] = $_POST['template'];
404 $this->csvinfo['templatestr'] = $this->csvinfo['arrtemplates' ][$this->csvinfo['tplid']];
405 $this->csvinfo['count'] = count($this->csvinfo['data']);
406 $this->csvinfo['rows'] = (count($this->csvinfo['data'])/$anz);
408 /* Set Templateid */
409 $smarty->assign("tplid",$this->csvinfo['tplid']);
411 /* Set Template */
412 $smarty->assign("tpl",$this->csvinfo['templatestr']);
414 /* Temp var 1 */
415 $smarty->assign("ia",1);
417 /* Temp var 2 */
418 $smarty->assign("i",0);
420 /* Num rows */
421 $smarty->assign("rows",$this->csvinfo['shownrowscount']);
424 for($i = 0 ; $i < $anz; $i ++)
425 $this->csvinfo['arr_selected'][]="0";
428 $smarty->assign("selectedattrs",$this->csvinfo['arr_selected']);
431 /* Entrie count5 */
432 $smarty->assign("anz",$this->csvinfo['cols']);
434 /* Array with data */
435 $smarty->assign("data",$this->csvinfo['dataout']);
437 @fclose($handle);
438 } else {
439 $smarty->assign("tpl",$this->csvinfo['attr'][$_POST['template']]);
440 $smarty->assign("LDIFError",TRUE);
441 $smarty->assign("fileup",FALSE);
442 print_red(_("The selected file does not contain any CSV Data..."));
443 }
444 }
445 }
446 }
448 /* Show main page */
449 return ($smarty->fetch (get_template_path('contentcsv.tpl', TRUE)));
450 }
451 }
453 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
454 ?>