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'])){
298 $handle = NULL;
300 if((!isset($_FILES['userfile']['name']))||(!isset($_POST['fileup'])))
301 {
302 print_red(_("There is no file uploaded."));
303 $smarty->assign("LDIFError",TRUE);
304 }
305 elseif(!$_FILES['userfile']['size'] > 0 )
306 {
307 print_red(_("The specified file is empty."));
308 $smarty->assign("LDIFError",TRUE);
309 }
310 /* Is there a tmp file, which we can use ? */
311 elseif(!file_exists($_FILES['userfile']['tmp_name']))
312 {
313 print_red(_("There is no file uploaded."));
314 $smarty->assign("LDIFError",TRUE);
315 }
316 elseif(!$handle = @fopen($_FILES['userfile']['tmp_name'],"r"))
317 {
318 print_red(_("There is no file uploaded."));
319 $smarty->assign("LDIFError",TRUE);
320 }
321 else
322 {
323 $smarty->assign("fileup",TRUE);
324 $str = "";
327 /* Reading content */
328 while(!feof($handle)) {
329 $str .= fread($handle,1024);
330 }
332 $lines = split("\n",$str);
333 $anz = 0;
334 $rest = 0;
335 $data = array();
337 /* check colum count */
338 foreach($lines as $line) {
339 /* continue if theres a comment */
340 if(substr(trim($line),0,1)=="#"){
341 continue;
342 }
344 $line= str_replace ("\t","",$line);
345 $cells = split(",",$line ) ;
347 if(count($cells)> $anz ){
348 $anz = count($cells);
349 }
350 }
352 /* At least one entry */
353 if($anz >1) {
355 /* Generate array with outpu info */
356 foreach($lines as $line) {
357 $rest = 0;
358 $cnt = 0;
360 /* dont use comments or empty lines */
361 if((substr(trim($line),0,1)=="#")||(empty($line))){
362 continue;
363 }
365 /* replace \t to , */
366 $line= str_replace ("\t" ,"" ,$line);
368 /* get all elements */
369 $cells = split(",",$line ) ;
371 /* attach all elements to data array */
372 foreach($cells as $cell) {
373 if(!empty($cell)) {
374 $cnt++;
375 $data[]=trim($cell);
376 }
377 }
379 /* cell count less than anz, attach some empty fields */
380 if(($cnt != $anz)&&(!empty($cnt))) {
381 $rest = $anz - $cnt;
382 for($i = 0 ; $i < $rest ; $i ++){
383 $data[] = " ";
384 }
385 }
386 }
388 unset($this->csvinfo['sorteddata']);
389 unset($this->csvinfo['colsstr']);
390 unset($this->csvinfo['sorteddata']);
392 $this->csvinfo['cols'] = $anz;
393 $this->csvinfo['data'] = array();
394 $this->csvinfo['data'] = $data;
395 $this->csvinfo['count'] = count($this->csvinfo['data']);
397 if($this->csvinfo['count']> (6* $this->csvinfo['cols'])) {
398 /* only show 6 rows in ouptuttablei */
399 $datouttemp = array_chunk($this->csvinfo['data'],(6* $this->csvinfo['cols']));
400 $this->csvinfo['dataout']=$datouttemp[0];
401 $this->csvinfo['shownrowscount'] = 6;
402 } else {
403 $this->csvinfo['shownrowscount'] = (count($this->csvinfo['data']))/$this->csvinfo['cols'];
404 $this->csvinfo['dataout']= $this->csvinfo['data'];
405 }
407 $this->csvinfo['tplid'] = $_POST['template'];
408 $this->csvinfo['templatestr'] = $this->csvinfo['arrtemplates' ][$this->csvinfo['tplid']];
409 $this->csvinfo['count'] = count($this->csvinfo['data']);
410 $this->csvinfo['rows'] = (count($this->csvinfo['data'])/$anz);
412 /* Set Templateid */
413 $smarty->assign("tplid",$this->csvinfo['tplid']);
415 /* Set Template */
416 $smarty->assign("tpl",$this->csvinfo['templatestr']);
418 /* Temp var 1 */
419 $smarty->assign("ia",1);
421 /* Temp var 2 */
422 $smarty->assign("i",0);
424 /* Num rows */
425 $smarty->assign("rows",$this->csvinfo['shownrowscount']);
428 for($i = 0 ; $i < $anz; $i ++)
429 $this->csvinfo['arr_selected'][]="0";
432 $smarty->assign("selectedattrs",$this->csvinfo['arr_selected']);
435 /* Entrie count5 */
436 $smarty->assign("anz",$this->csvinfo['cols']);
438 /* Array with data */
439 $smarty->assign("data",$this->csvinfo['dataout']);
441 @fclose($handle);
442 } else {
443 $smarty->assign("tpl",$this->csvinfo['attr'][$_POST['template']]);
444 $smarty->assign("LDIFError",TRUE);
445 $smarty->assign("fileup",FALSE);
446 print_red(_("The selected file does not contain any CSV Data..."));
447 }
448 }
449 }
450 }
452 /* Show main page */
453 return ($smarty->fetch (get_template_path('contentcsv.tpl', TRUE)));
454 }
455 }
457 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
458 ?>