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 print_a($this->usertab);
199 /* Insert in the LDAP tree */
200 if(count($this->usertab->check())) {
201 show_errors($this->usertab->check());
202 $this->csvinfo['sorteddata'][$i]['status']="<b>"._("failed")."</b>";
203 $failing = $i+1;
204 break;
205 } else {
206 $this->csvinfo['sorteddata'][$i]['status']=_("ok");
207 $this->usertab->save();
208 change_password($this->usertab->dn,$password);
209 }
210 }
211 }
213 $pointsbefore = FALSE;
214 $pointsafter = FALSE;
216 /* Get Attributs for Tableheadline */
217 $this->csvinfo['colsstr'] = array();
218 for($i =1; $i <= $this->csvinfo['cols']; $i++) {
219 if(!empty($_POST[('row'.$i)])){
220 $this->csvinfo['colsstr'][$i] = $this->csvinfo['attr'][$_POST[('row'.$i)]];
221 }
222 }
224 /*Create array with data for the template */
225 if($failing == FALSE){
226 for($i =1; $i <= $this->csvinfo['shownrowscount']; $i++) {
227 foreach($this->csvinfo['sorteddata'][($i-1)] as $dat){
228 $dataouts[]=$dat;
229 }
230 }
232 if($this->csvinfo['rows']> $this->csvinfo['shownrowscount']){
233 $pointsafter = TRUE;
234 }
235 } else {
236 $pointsbefore = TRUE;
237 $pointsafter = TRUE;
239 $begin = $failing -3;
241 if($begin <0) {
242 $begin =0;
243 $pointsbefore = FALSE;
244 }
246 $end = $failing + 2;
248 if($end > $this->csvinfo['rows']) {
249 $end = $this->csvinfo['rows']+1;
250 $pointsafter = FALSE;
251 }
252 $this->csvinfo['shownrowscount']=$end - $begin;
254 if($this->csvinfo['shownrowscount']> $this->csvinfo['rows'])$this->csvinfo['shownrowscount']=$this->csvinfo['rows'];
256 for($i =$begin; $i <= $end; $i++) {
257 foreach($this->csvinfo['sorteddata'][($i-1)] as $dat){
258 $dataouts[]=$dat;
259 }
260 }
261 }
262 $tmparr2 = $this->csvinfo['colsstr'];
263 $tmparr2[]=_("status");
265 /* Error? */
266 if ($failing){
267 print_red(sprintf(_("An Error Occured while inserting entry %s - process aborted"), $failing));
268 }
269 $smarty->assign("error",$failing);
271 /* Set vars to smarty */
272 $smarty->assign("cols",count($tmparr2));
273 $smarty->assign("anz" ,$this->csvinfo['count']);
274 $smarty->assign("rows",$this->csvinfo['shownrowscount']);
276 $smarty->assign("head",$tmparr2);
277 $smarty->assign("data",$dataouts);
279 /* Set other vars */
280 $smarty->assign("i",1);
281 $smarty->assign("ie",0);
282 $smarty->assign("tplid",$this->csvinfo['tplid']);
284 $smarty->assign("pointsafter",$pointsafter);
285 $smarty->assign("pointsbefore",$pointsbefore);
286 } else {
287 /* Set Template ID */
288 $tplid = $_POST['tplid'];
291 $smarty->assign("tpl",$arrtemplates[$tplid]);
292 print_red (_("Nothing to import !."));
293 }
295 /* If theres a File uploaded */
296 } else {
297 /* Check if theres a file uploaded */
298 if(!empty($_FILES['userfile']['name'])){
299 if((!isset($_FILES['userfile']['name']))||(!isset($_POST['fileup'])))
300 {
301 print_red(_("There is no file uploaded."));
302 $smarty->assign("LDIFError",TRUE);
303 }
304 elseif(!$_FILES['userfile']['size'] > 0 )
305 {
306 print_red(_("The specified file is empty."));
307 $smarty->assign("LDIFError",TRUE);
308 }
309 /* Is there a tmp file, which we can use ? */
310 elseif(!file_exists($_FILES['userfile']['tmp_name']))
311 {
312 print_red(_("There is no file uploaded."));
313 $smarty->assign("LDIFError",TRUE);
314 }
315 elseif(!$handle = @fopen($_FILES['userfile']['tmp_name'],"r"))
316 {
317 print_red(_("There is no file uploaded."));
318 $smarty->assign("LDIFError",TRUE);
319 }
320 else
321 {
322 $smarty->assign("fileup",TRUE);
323 $str = "";
325 /* Reading content */
326 while(!feof($handle)) {
327 $str .= fread($handle,1024);
328 }
330 $lines = split("\n",$str);
331 $anz = 0;
332 $rest = 0;
333 $data = array();
335 /* check colum count */
336 foreach($lines as $line) {
337 /* continue if theres a comment */
338 if(substr(trim($line),0,1)=="#"){
339 continue;
340 }
342 $line= str_replace ("\t","",$line);
343 $cells = split(",",$line ) ;
345 if(count($cells)> $anz ){
346 $anz = count($cells);
347 }
348 }
350 /* At least one entry */
351 if($anz >1) {
353 /* Generate array with outpu info */
354 foreach($lines as $line) {
355 $rest = 0;
356 $cnt = 0;
358 /* dont use comments or empty lines */
359 if((substr(trim($line),0,1)=="#")||(empty($line))){
360 continue;
361 }
363 /* replace \t to , */
364 $line= str_replace ("\t" ,"" ,$line);
366 /* get all elements */
367 $cells = split(",",$line ) ;
369 /* attach all elements to data array */
370 foreach($cells as $cell) {
371 if(!empty($cell)) {
372 $cnt++;
373 $data[]=trim($cell);
374 }
375 }
377 /* cell count less than anz, attach some empty fields */
378 if(($cnt != $anz)&&(!empty($cnt))) {
379 $rest = $anz - $cnt;
380 for($i = 0 ; $i < $rest ; $i ++){
381 $data[] = " ";
382 }
383 }
384 }
386 unset($this->csvinfo['sorteddata']);
387 unset($this->csvinfo['colsstr']);
388 unset($this->csvinfo['sorteddata']);
390 $this->csvinfo['cols'] = $anz;
391 $this->csvinfo['data'] = array();
392 $this->csvinfo['data'] = $data;
393 $this->csvinfo['count'] = count($this->csvinfo['data']);
395 if($this->csvinfo['count']> (6* $this->csvinfo['cols'])) {
396 /* only show 6 rows in ouptuttablei */
397 $datouttemp = array_chunk($this->csvinfo['data'],(6* $this->csvinfo['cols']));
398 $this->csvinfo['dataout']=$datouttemp[0];
399 $this->csvinfo['shownrowscount'] = 6;
400 } else {
401 $this->csvinfo['shownrowscount'] = (count($this->csvinfo['data']))/$this->csvinfo['cols'];
402 $this->csvinfo['dataout']= $this->csvinfo['data'];
403 }
405 $this->csvinfo['tplid'] = $_POST['template'];
406 $this->csvinfo['templatestr'] = $this->csvinfo['arrtemplates' ][$this->csvinfo['tplid']];
407 $this->csvinfo['count'] = count($this->csvinfo['data']);
408 $this->csvinfo['rows'] = (count($this->csvinfo['data'])/$anz);
410 /* Set Templateid */
411 $smarty->assign("tplid",$this->csvinfo['tplid']);
413 /* Set Template */
414 $smarty->assign("tpl",$this->csvinfo['templatestr']);
416 /* Temp var 1 */
417 $smarty->assign("ia",1);
419 /* Temp var 2 */
420 $smarty->assign("i",0);
422 /* Num rows */
423 $smarty->assign("rows",$this->csvinfo['shownrowscount']);
426 for($i = 0 ; $i < $anz; $i ++)
427 $this->csvinfo['arr_selected'][]="0";
430 $smarty->assign("selectedattrs",$this->csvinfo['arr_selected']);
433 /* Entrie count5 */
434 $smarty->assign("anz",$this->csvinfo['cols']);
436 /* Array with data */
437 $smarty->assign("data",$this->csvinfo['dataout']);
439 @fclose($handle);
440 } else {
441 $smarty->assign("tpl",$this->csvinfo['attr'][$_POST['template']]);
442 $smarty->assign("LDIFError",TRUE);
443 $smarty->assign("fileup",FALSE);
444 print_red(_("The selected file does not contain any CSV Data..."));
445 }
446 }
447 }
448 }
450 /* Show main page */
451 return ($smarty->fetch (get_template_path('contentcsv.tpl', TRUE)));
452 }
453 }
455 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
456 ?>