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();
16 var $view_logged = FALSE;
18 function csvimport (&$config, $dn= NULL)
19 {
20 /* Include config object */
21 $this->config= &$config;
23 $this->ui = get_userinfo();
24 }
26 function execute()
27 {
28 /* Call parent execute */
29 plugin::execute();
31 /* Log view */
32 if(!$this->view_logged){
33 $this->view_logged = TRUE;
34 new log("view","all/".get_class($this),$this->dn);
35 }
37 /* initiate smarty */
38 $smarty= get_smarty();
39 $smarty->assign("selectedattrs",array(0=>""));
40 $smarty->assign("data",array(0=>""));
41 $smarty->assign("head",array(0=>""));
42 $smarty->assign("sorted",0);
43 $smarty->assign("fileup",0);
45 /* Get the LDAP link, to generate the Export */
46 $ldap = $this->config->get_ldap_link();
48 $arrtemplates = array();
49 $tempvar =0;
51 /* Array to fill in Formfields */
52 $arrtemplates[$tempvar] = "None";
53 if(!is_array($this->csvinfo)){
54 $this->csvinfo=array();
55 }
57 /* Set Usertemplate information and get all Attribute from userclass */
58 unset ($this->csvinfo['arrtemplates']);
59 unset ($this->csvinfo['arrtempls']);
61 /* Generate Template Array, Attribute Array */
62 if(!isset($this->csvinfo['arrtempls'])){
64 /* Generating attributes */
65 $attrs = @get_class_vars("user");
66 $this->csvinfo['attr'] = $attrs['attributes'];
68 /* Attach the choise none to discard a csv col */
69 $new = array();
70 $new[0]="none";
71 $new[1]="userPassword";
72 for($c = 0; $c < count($this->csvinfo['attr']);$c++){
73 $new[$c+2]=$this->csvinfo['attr'][$c];
74 }
76 $this->csvinfo['attr'] = $new;
78 /* Search all Templates */
79 $this->csvinfo['templates'] = $ldap->search("(objectClass=gosaUserTemplate)",array("*"));
81 /* Array to handle selected Form fiels */
82 $this->csvinfo['arrtempls'][$tempvar]['name'] = "";
83 $this->csvinfo['arrtempls'][$tempvar]['dn'] = "";
85 $this->csvinfo['arrtemplates'][]="None";
87 while($temp = $ldap->fetch($this->csvinfo['templates'])){
88 $tempvar ++;
89 $this->csvinfo['arrtemplates'][$tempvar] = $temp['sn'][0]." - ".$this->config->idepartments[preg_replace("/^[^,]+,".get_people_ou()."/i", "", $temp['dn'])];
90 $this->csvinfo['arrtempls'] [$tempvar]['name'] = $temp['sn'][0];
91 $this->csvinfo['arrtempls'] [$tempvar]['dn'] = $temp['dn'];
92 }
93 }
95 $arr_temp = array_flip($this->csvinfo['attr']);
96 $this->csvinfo['arr_selected']= array($arr_temp['uid'],$arr_temp['sn'],$arr_temp['givenName'],$arr_temp['userPassword']);
98 $smarty->assign("templates",$this->csvinfo['arrtemplates']);
99 $smarty->assign("attrs",$this->csvinfo['attr']);
101 /* Check permissions for import */
102 $acl = $this->ui->get_permissions($this->config->current['BASE'],"all/all");
103 if(!preg_match("/w/",$acl)){
104 if(isset($_POST['userfile']) || isset($_POST['sorted']) || isset($_POST['fileup'])){
105 msg_dialog::display(_("Permission error"), _("You've no permission to import CSV files."), ERROR_DIALOG);
106 }
107 return ($smarty->fetch (get_template_path('contentcsv.tpl', TRUE)));
108 }
110 /* If the given dat from the csv File are sorted by the attributes */
111 if(isset($_POST['sorted'])) {
113 /* Template Handling */
114 $smarty->assign("fileup",TRUE);
115 $smarty->assign("sorted",TRUE);
117 for($i = 0 ; $i < $this->csvinfo['rows'];$i++){
118 /* Preparing given Data */
119 $this->csvinfo['sorteddata'][$i]=array();
121 /* Go through every col */
122 for($a = 0; $a <= ($this->csvinfo['cols']); $a ++) {
123 /* Append a status col at last, to show possibly errors */
124 if($a==($this->csvinfo['cols'])){
125 $this->csvinfo['sorteddata'][$i]["status"]="-";
126 } else {
127 $cc = ($i * ($this->csvinfo['cols']))+$a;
129 /* If theres a Attribut given for this col */
130 if(!empty($_POST[('row'.($a))])) {
131 $this->csvinfo['sorteddata'][$i][$this->csvinfo['attr'][$_POST[('row'.($a))]]]=$this->csvinfo['data'][($cc)];
132 }
133 }
134 }
135 }
137 /* aleast one entry */
138 $rowcnt=0;
139 if(isset($this->csvinfo['sorteddata'])){
140 $rowcnt ++;
142 /* Set the col count */
143 $tblcolcount= count ($this->csvinfo['cols']);
145 /* entrie count */
146 $tbl_entries= count ($this->csvinfo['count']);
148 /* row count */
149 $tbl_rows = $tbl_entries / $tblcolcount;
151 /* No error occurred */
152 $failing = FALSE;
154 /* For every row */
155 for($i = 0; $i < $this->csvinfo['rows']; $i++){
157 /* Get elemenet */
158 $dat = $this->csvinfo['sorteddata'][$i];
160 /* If not all needed vars set .... */
161 if( ( empty($dat['sn']))||( empty($dat['givenName']))||( empty($dat['uid']))||
162 (!isset($dat['sn']))||(!isset($dat['givenName']))||(!isset($dat['uid']))){
164 /* Output Error */
165 msg_dialog::display(_("Error"), _("Need at least 'sn', 'givenName' and 'uid' to create users."), ERROR_DIALOG);
166 } else {
168 /* Register usertab to trigger edit dialog */
169 $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'], 'new');
170 $keys = array_keys($dat);
173 /* Set all submitted vars */
174 if(is_array($keys))
175 foreach($keys as $key) {
176 if($key != "status"){
177 $this->usertab->$key = $dat[$key];
178 }
179 }
181 /* Initialize template */
182 if($this->csvinfo['tplid']!=0){
183 $this->usertab->adapt_from_template($this->csvinfo['arrtempls'][$this->csvinfo['tplid']]['dn']);
184 $this->usertab->save_object();
185 } else {
187 /* Reset vars */
188 if(is_array($keys))
189 foreach($keys as $key) {
190 if($key != "status"){
191 $this->usertab->by_object['user']->$key = $dat[$key];
192 }
193 }
194 }
196 // Setting user Password
197 if((isset($dat['userPassword']))&&(!empty($dat['userPassword'])))
198 $password = $dat['userPassword'];
199 else
200 {
201 $rand_str="";
202 $feed = "0123456789abcdefghijklmnopqrstuvwxyz";
203 for ($i=0; $i < 8; $i++)
204 {
205 $rand_str .= substr($feed, rand(0, strlen($feed)-1), 1);
206 }
207 $password = $rand_str;
208 }
210 /* Insert in the LDAP tree */
211 if(count($this->usertab->check())) {
212 msg_dialog::displayChecks($this->usertab->check());
213 $this->csvinfo['sorteddata'][$i]['status']="<b>"._("failed")."</b>";
214 $failing = $i+1;
215 break;
216 } else {
217 $this->csvinfo['sorteddata'][$i]['status']=_("ok");
218 $this->usertab->save();
219 change_password($this->usertab->dn,$password);
220 }
221 }
222 }
224 $pointsbefore = FALSE;
225 $pointsafter = FALSE;
227 /* Get Attributs for Tableheadline */
228 $this->csvinfo['colsstr'] = array();
229 for($i =0; $i <= $this->csvinfo['cols']; $i++) {
230 if(!empty($_POST[('row'.$i)])){
231 $this->csvinfo['colsstr'][$i] = $this->csvinfo['attr'][$_POST[('row'.$i)]];
232 }
233 }
235 /*Create array with data for the template */
236 if($failing == FALSE){
238 $dataouts= array();
239 for($i =1; $i <= $this->csvinfo['shownrowscount']; $i++) {
240 if(is_array($this->csvinfo['sorteddata'][($i-1)])){
241 foreach($this->csvinfo['sorteddata'][($i-1)] as $dat){
242 $dataouts[]=$dat;
243 }
244 }
245 }
247 if($this->csvinfo['rows']> $this->csvinfo['shownrowscount']){
248 $pointsafter = TRUE;
249 }
250 } else {
251 $pointsbefore = TRUE;
252 $pointsafter = TRUE;
254 $begin = $failing -3;
256 if($begin <0) {
257 $begin =0;
258 $pointsbefore = FALSE;
259 }
261 $end = $failing + 2;
263 if($end > $this->csvinfo['rows']) {
264 $end = $this->csvinfo['rows']+1;
265 $pointsafter = FALSE;
266 }
267 $this->csvinfo['shownrowscount']=$end - $begin;
269 if($this->csvinfo['shownrowscount']> $this->csvinfo['rows'])$this->csvinfo['shownrowscount']=$this->csvinfo['rows'];
271 $dataouts = array();
272 for($i =$begin; $i <= $end; $i++) {
273 if(is_array($this->csvinfo['sorteddata'][($i-1)])){
274 foreach($this->csvinfo['sorteddata'][($i-1)] as $dat){
275 $dataouts[]=$dat;
276 }
277 }
278 }
279 }
280 $tmparr2 = $this->csvinfo['colsstr'];
281 $tmparr2[]=_("status");
283 /* Error? */
284 if ($failing){
285 msg_dialog::display(_("Error"), sprintf(_("Cannot insert entry '%s'!"), $failing), ERROR_DIALOG);
286 }
287 $smarty->assign("error",$failing);
289 /* Set vars to smarty */
290 $smarty->assign("cols",count($tmparr2));
291 $smarty->assign("anz" ,$this->csvinfo['count']);
292 $smarty->assign("rows",$this->csvinfo['shownrowscount']);
295 $cnt = 0 ;
296 $tmp2 = $tmp3 = array();
297 if(is_array($dataouts))
298 foreach($dataouts as $tmp){
299 $tmp2[] = $tmp;
300 $cnt ++ ;
301 if($cnt >= count($tmparr2)){
302 $tmp3[] = $tmp2;
303 $tmp2= array();
304 $cnt = 0;
305 }
306 }
307 $smarty->assign("head",$tmparr2);
308 $smarty->assign("data",$tmp3);
310 /* Set other vars */
311 $smarty->assign("i",1);
312 $smarty->assign("ie",0);
313 $smarty->assign("tplid",$this->csvinfo['tplid']);
315 $smarty->assign("pointsafter",$pointsafter);
316 $smarty->assign("pointsbefore",$pointsbefore);
317 } else {
318 /* Set Template ID */
319 $tplid = $_POST['tplid'];
322 $smarty->assign("tpl",$arrtemplates[$tplid]);
323 msg_dialog::display(_("Information"), _("Nothing to import!"), INFO_DIALOG);
324 }
326 /* If theres a File uploaded */
327 } else {
328 /* Check if theres a file uploaded */
329 if(!empty($_FILES['userfile']['name'])){
331 $handle = NULL;
333 if((!isset($_FILES['userfile']['name']))||(!isset($_POST['fileup'])))
334 {
335 msg_dialog::display(_("Error"), sprintf(_("Cannot read uploaded file: %s"), _("file not found")), ERROR_DIALOG);
336 $smarty->assign("LDIFError",TRUE);
337 }
338 elseif(!$_FILES['userfile']['size'] > 0 )
339 {
340 msg_dialog::display(_("Error"), sprintf(_("Cannot read uploaded file: %s"), _("file is empty")), ERROR_DIALOG);
341 $smarty->assign("LDIFError",TRUE);
342 }
343 /* Is there a tmp file, which we can use ? */
344 elseif(!file_exists($_FILES['userfile']['tmp_name']))
345 {
346 msg_dialog::display(_("Error"), sprintf(_("Cannot read uploaded file: %s"), _("file not found")), ERROR_DIALOG);
347 $smarty->assign("LDIFError",TRUE);
348 }
349 elseif(!$handle = @fopen($_FILES['userfile']['tmp_name'],"r"))
350 {
351 msg_dialog::display(_("Error"), sprintf(_("Cannot read uploaded file: %s"), _("file not readable")), ERROR_DIALOG);
352 $smarty->assign("LDIFError",TRUE);
353 }
354 else
355 {
356 $smarty->assign("fileup",TRUE);
357 $str = "";
360 /* Reading content */
361 while(!feof($handle)) {
362 $str .= fread($handle,1024);
363 }
365 $lines = split("\n",$str);
366 $anz = 0;
367 $rest = 0;
368 $data = array();
370 /* check colum count */
371 if(is_array($lines))
372 foreach($lines as $line) {
373 /* continue if theres a comment */
374 if(substr(trim($line),0,1)=="#"){
375 continue;
376 }
378 $line= str_replace ("\t","",$line);
379 $cells = split(",",$line ) ;
381 if(count($cells)> $anz ){
382 $anz = count($cells);
383 }
384 }
386 /* At least one entry */
387 if($anz >1) {
389 /* Generate array with outpu info */
390 if(is_array($lines))
391 foreach($lines as $line) {
392 $rest = 0;
393 $cnt = 0;
395 /* dont use comments or empty lines */
396 if((substr(trim($line),0,1)=="#")||(empty($line))){
397 continue;
398 }
400 /* replace \t to , */
401 $line= str_replace ("\t" ,"" ,$line);
403 /* get all elements */
404 $cells = split(",",$line ) ;
406 /* attach all elements to data array */
407 if(is_array($cells))
408 foreach($cells as $cell) {
409 if(!empty($cell)) {
410 $cnt++;
411 $data[]=trim($cell);
412 }
413 }
415 /* cell count less than anz, attach some empty fields */
416 if(($cnt != $anz)&&(!empty($cnt))) {
417 $rest = $anz - $cnt;
418 for($i = 0 ; $i < $rest ; $i ++){
419 $data[] = " ";
420 }
421 }
422 }
424 unset($this->csvinfo['sorteddata']);
425 unset($this->csvinfo['colsstr']);
426 unset($this->csvinfo['sorteddata']);
428 $this->csvinfo['cols'] = $anz;
429 $this->csvinfo['data'] = array();
430 $this->csvinfo['data'] = $data;
431 $this->csvinfo['count'] = count($this->csvinfo['data']);
433 if($this->csvinfo['count']> (6* $this->csvinfo['cols'])) {
434 /* only show 6 rows in ouptuttablei */
435 $datouttemp = array_chunk($this->csvinfo['data'],(6* $this->csvinfo['cols']));
436 $this->csvinfo['dataout']=$datouttemp[0];
437 $this->csvinfo['shownrowscount'] = 6;
438 } else {
439 $this->csvinfo['shownrowscount'] = (count($this->csvinfo['data']))/$this->csvinfo['cols'];
440 $this->csvinfo['dataout']= $this->csvinfo['data'];
441 }
443 $this->csvinfo['tplid'] = $_POST['template'];
444 $this->csvinfo['templatestr'] = $this->csvinfo['arrtemplates' ][$this->csvinfo['tplid']];
445 $this->csvinfo['count'] = count($this->csvinfo['data']);
446 $this->csvinfo['rows'] = (count($this->csvinfo['data'])/$anz);
448 $i = 0;
449 $tmp = $tmp2= array();
450 if(is_array($this->csvinfo['dataout']))
451 foreach($this->csvinfo['dataout'] as $dat){
452 $tmp[]= $dat;
453 $i++;
454 if($i >=$this->csvinfo['cols']){
455 $i = 0;
456 $tmp2[] = $tmp;
457 $tmp = array();
458 }
460 }
462 /* Set Templateid */
463 $smarty->assign("tplid",$this->csvinfo['tplid']);
465 /* Set Template */
466 $smarty->assign("tpl",$this->csvinfo['templatestr']);
468 /* Temp var 1 */
469 $smarty->assign("ia",1);
471 /* Temp var 2 */
472 $smarty->assign("i",0);
474 /* Num rows */
475 $smarty->assign("rows",$this->csvinfo['shownrowscount']);
478 for($i = 0 ; $i < $anz; $i ++)
479 $this->csvinfo['arr_selected'][]="0";
481 $smarty->assign("selectedattrs",$this->csvinfo['arr_selected']);
483 /* Entrie count5 */
484 $smarty->assign("anz",$this->csvinfo['cols']);
486 /* Array with data */
487 $smarty->assign("data",$tmp2);
489 @fclose($handle);
490 } else {
491 $smarty->assign("tpl",$this->csvinfo['attr'][$_POST['template']]);
492 $smarty->assign("LDIFError",TRUE);
493 $smarty->assign("fileup",FALSE);
494 msg_dialog::display(_("Error"), _("Cannot find CSV data in the selected file!"), ERROR_DIALOG);
495 }
496 }
497 }
498 }
500 /* Show main page */
501 return ($smarty->fetch (get_template_path('contentcsv.tpl', TRUE)));
502 }
504 }
506 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
507 ?>