File: import.php

package info (click to toggle)
lwat 0.17-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 860 kB
  • ctags: 945
  • sloc: php: 3,151; sh: 212; makefile: 148
file content (157 lines) | stat: -rw-r--r-- 4,835 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/*
    LWAT, LDAP Webbased Administration Tool
    Copyright (C) 2007 Finn-Arne Johansen <faj@bzz.no> BzzWare AS, Norway

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

    require_once ('functions.php') ; 
    require('/usr/share/php/smarty/libs/Smarty.class.php') ; 

    ob_start () ; 
    loadConfig () ; 
    loadLocale () ; 

    session_start() ;
    $smarty = new Smarty();
    $smarty->template_dir = $smarty_templ ; 
    $smarty->compile_dir = $smarty_compile ; 

    $key = lwat_key () ; 
    $ldap = lwat_ldap ($ldaphost); 
    $authenticated = lwat_login ($ldap, '', '', $key) ; 
    $maxcount = 0 ;

    if ($file = file($_FILES["userfile"]["tmp_name"])) {
	for ($i = 0 ; $i < count($file); $i++) {
	    $line=split (";", $file[$i]) ;
	    foreach ($line as $value) 
		$cells[$i][] = trim ($value) ; 
	    if (count($line) > $maxcount) 
		$maxcount = count ($line) ; 
	}
	$smarty->assign ('cells', $cells) ; 
	for ($i = 0 ; $i < $maxcount; $i++) 
	    $columns[] = "" ; 
        $smarty->assign ('columns', $columns) ; 
	$smarty->assign ('cmd', upload) ; 
    } else {
        foreach ($_POST as $key => $value) {
	    $key = split ("_", $key) ; 
	    switch ($key[0]) {
	        case "group": 
	            $adduser[$key[1]]["group"][] = $value ; 
	            break ;
	        case "password": 
	        case "fullName": 
	        case "template": 
	            $adduser[$key[1]][$key[0]] = $value ; 
	            break ; 
		case "column": 
		    $columns[] = $value ; 
		    break ; 
		case "cmd": 
		    $cmd = $key[1] ; 
		    break ; 
		case "row": 
		    if ($value == "on") {
			$row = $key[1] ; 
			if (isset($new))
			    $new++ ; 
			else
			    $new = 0 ; 

		    }
		    break ; 
		case "cell": 
		    if ($key[1] == $row) {
		        switch ($columns[$key[2]]) {
		            case "Group": 
				$cell[$new]["group"][] = $value ; 
				break ;
		            default: 
				$cell[$new][$columns[$key[2]]] = $value ; 
				break ;
			}
	            }
		    break ; 
		default: 
		    debug (array('key' => $key,
				 'value' => $value)) ; 
		    break ;
	    }
	}
	$smarty->assign ('cmd', $cmd) ; 
	switch ($cmd) {
	    case "add":
	        foreach ($adduser as $username => $info) {
	            ldapAddUser ($ldap, $info["fullName"], $info["template"], 
	                         $username,  $info["password"], &$info["group"]) ; 
		    foreach ($info["group"] as $key => $group)
		        $adduser[$username]["AddGroup"][] = $group ; 
		    set_time_limit (20) ; 
	        }
	        $smarty->assign ('addUsers', $adduser) ; 
	        break ;
	    case "check":
		$templ_ini_file = parse_ini_file ('/etc/lwat/admin.ini', true) ; 
		foreach ($templ_ini_file as $key => $value)
		    $templates[] = $key ; 
		$smarty->assign ('Templates', $templates) ; 
		if (! in_array ("Template", $columns)) 
		    array_push ($columns, "Template") ; 
	       for ($i = 0 ; $i < count($cell) ; $i++) {
		   if (!in_array ($cell[$i]["Template"], $templates))
		       $cell[$i]["Template"] = "" ; 
		}

		if (! in_array ("fullName", $columns)) 
		    array_push ($columns, "FullName") ; 
		for ($i = 0 ; $i < count($cell) ; $i++) {
		    if ($cell[$i]["fullName"] == "")
			$cell[$i]["fullName"] = $cell[$i]["firstName"] . " " . $cell[$i]["lastName"] ; 
		}

		if (!in_array ("userName", $columns)) 
		    array_push ($columns, "userName") ; 
		$excludes = array() ; 
		for ($i = 0 ; $i < count($cell) ; $i++) {
		    $cell[$i]["userName"] = chk_username ($ldap, $cell[$i]["userName"], $excludes); 
		    if ($cell[$i]["userName"] == "")
			$cell[$i]["userName"] = get_username ($ldap, $cell[$i]["fullName"], $excludes) ; 
	            $excludes[] = $cell[$i]["userName"];
		}

		if (!in_array ("password", $columns)) 
		    array_push ($columns, "password") ; 
		for ($i = 0 ; $i < count($cell) ; $i++) {
		    if ($cell[$i]["password"] == "")
			$cell[$i]["password"] = pwgen () ; 
		}

		$smarty->assign ('columns', $columns) ; 
		$smarty->assign ('cells', $cell) ; 
		break ;
            default: 
                debug ($_POST) ; 
                break ;
	    }
    }
    ldap_close ($ldap);

    $smarty->display('import.tpl');

?>