File: class_import.inc

package info (click to toggle)
gosa-plugins-ldapmanager 2.8~git20211022.877a67e-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 396 kB
  • sloc: php: 1,055; makefile: 5
file content (137 lines) | stat: -rw-r--r-- 4,723 bytes parent folder | download | duplicates (4)
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
<?php

class ldifimport extends plugin
{
    /* Definitions */
    var $plHeadline= "LDIF export";
    var $plDescription= "This does something";
    var $access= "";

    /* attribute list for save action */
    var $attributes= array();
    var $objectclasses= array();
    var $ui;
    var $view_logged = FALSE;

    function __construct(&$config, $dn= NULL)
    {
        /* Include config object */
        $this->initTime = microtime(TRUE);
        $this->config= &$config;

        $this->ui = get_userinfo();

        stats::log('plugin', $class = get_class($this), $category = array($this->acl_category),  $action = 'open',
                $amount = 1, $duration = (microtime(TRUE) - $this->initTime));

    }

    function execute()
    {
        /* Call parent execute */
        plugin::execute();

        /* Log view */
        if(!$this->view_logged){
            $this->view_logged = TRUE;
            new log("view","all/".get_class($this),$this->dn);
        }

        /* Set government mode */
        $smarty= get_smarty();

        // Get the LDAP link, to generate the Export
        $ldap = $this->config->get_ldap_link();

        $smarty->assign("LDIFError",FALSE);
        $smarty->assign("type",FALSE);

        /* Get acls */
        $acl = $this->ui->get_permissions($this->config->current['BASE'],"all/all");

        /* Import requested check file and acls */
        if((isset($_FILES['userfile']['name']))&&(isset($_POST['fileup']))){

            /* Check acls */
            if(!preg_match("/w/",$acl)){
                msg_dialog::display(_("Permission error"), _("You need full write access to the LDAP tree to import data!"), ERROR_DIALOG);
            }else{

                $smarty->assign("type","importfile");
                $handle = NULL;

                $filename = gosa_file_name($_FILES['userfile']['tmp_name']);

                // Check if there is a file submitted
                if(!$_FILES['userfile']['size'] > 0 )
                {
                    msg_dialog::display(_("Error"), sprintf(_("Cannot read uploaded file: %s"), _("file is empty")), ERROR_DIALOG);
                    $smarty->assign("LDIFError",TRUE);  
                }

                // Is there a tmp file, which we can use ?
                elseif(!file_exists($filename))  
                {
                    msg_dialog::display(_("Error"), sprintf(_("Cannot read uploaded file: %s"), _("file not found")), ERROR_DIALOG);
                    $smarty->assign("LDIFError",TRUE);

                }

                // Can we open the tmp file, for reading
                elseif(!$handle = @fopen($filename,"r"))
                {
                    msg_dialog::display(_("Error"), sprintf(_("Cannot read uploaded file: %s"), _("file not readable")), ERROR_DIALOG);
                    $smarty->assign("LDIFError",TRUE);  
                }
                else
                {
                    // Everything just fine :)
                    $str = ""; 

                    // Reading content 
                    while(!feof($handle))
                    {
                        $str .= fread($handle,1024);
                    }
                    @fclose($handle);

                    // Should we use Overwrite ?
                    if(!empty($_POST['overwrite'])) $overwrite = true; else $overwrite = false;;
                    if(!empty($_POST['cleanup']))   $cleanup   = true; else $cleanup = false;

                    $ErrorStr="";
                    $check = $ldap->import_complete_ldif($str,$ErrorStr,$overwrite,$cleanup);

                    if($check == INSERT_OK  ){
                        $smarty->assign("LDIFError",FALSE);
                    } else {
                        $smarty->assign("LDIFError",TRUE);
                    }

                    switch($check)
                    {
                        case INSERT_OK:
                            break;

                        case ALREADY_EXISTING_ENTRY:
                            msg_dialog::display(_("LDAP error"), $ErrorStr, ERROR_DIALOG);
                            break;

                        case UNKNOWN_TOKEN_IN_LDIF_FILE:
                            msg_dialog::display(_("LDAP error"), $ErrorStr, ERROR_DIALOG);
                            break;

                        default:
                            msg_dialog::display(_("Internal error"), sprintf(_("Undefined flag %s!"), bold($check)), ERROR_DIALOG);
                            break;
                    }
                }
            }
        }
        return ($smarty->fetch (get_template_path('contentimport.tpl', TRUE)));
    }
}


// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
?>