1 <?php
3 class ldifimport extends plugin
4 {
5 /* Definitions */
6 var $plHeadline= "LDIF export";
7 var $plDescription= "This does something";
8 var $access= "";
10 /* attribute list for save action */
11 var $attributes= array();
12 var $objectclasses= array();
13 var $ui;
14 var $view_logged = FALSE;
16 function ldifimport (&$config, $dn= NULL)
17 {
18 /* Include config object */
19 $this->initTime = microtime(TRUE);
20 $this->config= &$config;
22 $this->ui = get_userinfo();
24 stats::log('plugin', $class = get_class($this), $category = array($this->acl_category), $action = 'open',
25 $amount = 1, $duration = (microtime(TRUE) - $this->initTime));
27 }
29 function execute()
30 {
31 /* Call parent execute */
32 plugin::execute();
34 /* Log view */
35 if(!$this->view_logged){
36 $this->view_logged = TRUE;
37 new log("view","all/".get_class($this),$this->dn);
38 }
40 /* Set government mode */
41 $smarty= get_smarty();
43 // Get the LDAP link, to generate the Export
44 $ldap = $this->config->get_ldap_link();
46 $smarty->assign("LDIFError",FALSE);
47 $smarty->assign("type",FALSE);
49 /* Get acls */
50 $acl = $this->ui->get_permissions($this->config->current['BASE'],"all/all");
52 /* Import requested check file and acls */
53 if((isset($_FILES['userfile']['name']))&&(isset($_POST['fileup']))){
55 /* Check acls */
56 if(!preg_match("/w/",$acl)){
57 msg_dialog::display(_("Permission error"), _("You need full write access to the LDAP tree to import data!"), ERROR_DIALOG);
58 }else{
60 $smarty->assign("type","importfile");
61 $handle = NULL;
63 // Check if there is a file submitted
64 if(!$_FILES['userfile']['size'] > 0 )
65 {
66 msg_dialog::display(_("Error"), sprintf(_("Cannot read uploaded file: %s"), _("file is empty")), ERROR_DIALOG);
67 $smarty->assign("LDIFError",TRUE);
68 }
70 // Is there a tmp file, which we can use ?
71 elseif(!file_exists($_FILES['userfile']['tmp_name']))
72 {
73 msg_dialog::display(_("Error"), sprintf(_("Cannot read uploaded file: %s"), _("file not found")), ERROR_DIALOG);
74 $smarty->assign("LDIFError",TRUE);
76 }
78 // Can we open the tmp file, for reading
79 elseif(!$handle = @fopen($_FILES['userfile']['tmp_name'],"r"))
80 {
81 msg_dialog::display(_("Error"), sprintf(_("Cannot read uploaded file: %s"), _("file not readable")), ERROR_DIALOG);
82 $smarty->assign("LDIFError",TRUE);
83 }
84 else
85 {
86 // Everything just fine :)
87 $str = "";
89 // Reading content
90 while(!feof($handle))
91 {
92 $str .= fread($handle,1024);
93 }
94 @fclose($handle);
96 // Should we use Overwrite ?
97 if(!empty($_POST['overwrite'])) $overwrite = true; else $overwrite = false;;
98 if(!empty($_POST['cleanup'])) $cleanup = true; else $cleanup = false;
100 $ErrorStr="";
101 $check = $ldap->import_complete_ldif($str,$ErrorStr,$overwrite,$cleanup);
103 if($check == INSERT_OK ){
104 $smarty->assign("LDIFError",FALSE);
105 } else {
106 $smarty->assign("LDIFError",TRUE);
107 }
109 switch($check)
110 {
111 case INSERT_OK:
112 break;
114 case ALREADY_EXISTING_ENTRY:
115 msg_dialog::display(_("LDAP error"), $ErrorStr, ERROR_DIALOG);
116 break;
118 case UNKNOWN_TOKEN_IN_LDIF_FILE:
119 msg_dialog::display(_("LDAP error"), $ErrorStr, ERROR_DIALOG);
120 break;
122 default:
123 msg_dialog::display(_("Internal error"), sprintf(_("Undefined flag %s!"), bold($check)), ERROR_DIALOG);
124 break;
125 }
126 }
127 }
128 }
129 return ($smarty->fetch (get_template_path('contentimport.tpl', TRUE)));
130 }
131 }
134 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
135 ?>