Code

Removed firstCall check from update function. This is problematic with several sub...
[gosa.git] / gosa-core / include / class_SnapshotHandler.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
24 class SnapshotHandler {
26   var $config;
27   var $isEnabled= false;
28   var $snapshotBases= array();
31   /* Create handler  */
32   function SnapshotHandler(&$config)
33   {
34     $this->config = &$config;   
35     $config = $this->config;
37     if($config->get_cfg_value("enableSnapshots") == "true"){
39       /* Check if the snapshot_base is defined */
40       if ($config->get_cfg_value("snapshotBase") == ""){
42         /* Send message if not done already */
43         if(!session::is_set("snapshotFailMessageSend")){
44           session::set("snapshotFailMessageSend",TRUE);
45           msg_dialog::display(_("Configuration error"),
46               sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not set."),
47                 "snapshotBase"), ERROR_DIALOG);
48         }
49         return;
50       }
52       /* Check if the snapshot_base is defined */
53       if (!is_callable("gzcompress")){
55         /* Send message if not done already */
56         if(!session::is_set("snapshotFailMessageSend")){
57           session::set("snapshotFailMessageSend",TRUE);
58           msg_dialog::display(_("Configuration error"),
59               sprintf(_("The snapshot functionality is enabled, but the required compression module is missing. Please install '%s'."),"php5-zip / php5-gzip"), ERROR_DIALOG);
60         }
61         return;
62       }
64       /* check if there are special server configurations for snapshots */
65       if ($config->get_cfg_value("snapshotURI") != ""){
67         /* check if all required vars are available to create a new ldap connection */
68         $missing = "";
69         foreach(array("snapshotURI","snapshotAdminDn","snapshotAdminPassword","snapshotBase") as $var){
70           if($config->get_cfg_value($var) == ""){
71             $missing .= $var." ";
73             /* Send message if not done already */
74             if(!session::is_set("snapshotFailMessageSend")){
75               session::set("snapshotFailMessageSend",TRUE);
76               msg_dialog::display(_("Configuration error"),
77                   sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not set."),
78                     $missing), ERROR_DIALOG);
79             }
80             return;
81           }
82         }
83       }
84       $this->isEnabled= true;
85       return;
86     }
87   }
90   function enabled()
91   {
92     return $this->isEnabled;
93   }   
96   function setSnapshotBases($bases)
97   {
98     $this->snapshotBases= $bases;
99   }
102   function getSnapshotBases()
103   {
104     return $this->snapshotBases;
105   }
108   function get_snapshot_link()
109   {
110     $snapshotLdap= null;
112     /* check if there are special server configurations for snapshots */
113     if($this->config->get_cfg_value("snapshotURI") != ""){
114       $server= $this->config->get_cfg_value("snapshotURI");
115       $user= $this->config->get_cfg_value("snapshotAdminDn");
116       $password= $this->config->get_credentials($this->config->get_cfg_value("snapshotAdminPassword"));
117       $snapshotLdap= new ldapMultiplexer(new LDAP($user,$password, $server));
118     }
120     /* Prepare bases */
121     $this->snapshotLdapBase= $this->config->get_cfg_value("snapshotBase");
122     $snapshotLdap->cd($this->snapshotLdapBase);
123     if (!$snapshotLdap->success()){
124       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($snapshotLdap->get_error(), $this->snapshotLdapBase, "", get_class()));
125     }
127     return $snapshotLdap;
128   }
131   function getDeletedSnapshots($objectBase, $raw= false)
132   {
133     // Skip if not enabled
134     if(!$this->enabled()){
135       return(array());
136     }
138     // Load user info
139     $ui= get_userinfo();
141     /* Create an additional ldap object which
142        points to our ldap snapshot server */
143     $ldap= $this->config->get_ldap_link();
144     $ldap->cd($this->config->current['BASE']);
145     $snapshotLdap= $this->get_snapshot_link();
146     if (!$snapshotLdap) {
147       $snapshotLdap= $ldap;
148     }
150     // Initialize base
151     $base= preg_replace("/".preg_quote($this->config->current['BASE'], '/')."$/",
152                         "", $objectBase).$this->snapshotLdapBase;
154     /* Fetch all objects and check if they do not exist anymore */
155     $objects= array();
156     $snapshotLdap->cd($base);
157     $snapshotLdap->ls("(objectClass=gosaSnapshotObject)", $base,
158                       array("gosaSnapshotType", "gosaSnapshotTimestamp", "gosaSnapshotDN", "description"));
159     while($entry = $snapshotLdap->fetch()){
161       $chk =  str_replace($base,"",$entry['dn']);
162       if(preg_match("/,ou=/",$chk)) continue;
164       if(!isset($entry['description'][0])){
165         $entry['description'][0]  = "";
166       }
167       $objects[] = $entry;
168     }
170     /* Check if entry still exists */
171     foreach($objects as $key => $entry){
172       $ldap->cat($entry['gosaSnapshotDN'][0]);
173       if($ldap->count()){
174         unset($objects[$key]);
175       }
176     }
178     /* Format result as requested */
179     if($raw) {
180       return($objects);
181     }else{
182       $tmp = array();
183       foreach($objects as $key => $entry){
184         $tmp[base64_encode($entry['dn'])] = $entry['description'][0];
185       }
186     }
187     return($tmp);
188   }
191   function hasSnapshots($dn)
192   {
193     return (count($this->getSnapshots($dn)) > 0);
194   }
197   function getSnapshots($dn, $raw= false)
198   {
199     // Empty if disabled
200     if(!$this->enabled()){
201       return(array());
202     }
204     /* Create an additional ldap object which
205        points to our ldap snapshot server */
206     $ldap= $this->config->get_ldap_link();
207     $ldap->cd($this->config->current['BASE']);
209     // Load snapshot LDAP connection
210     $snapshotLdap= $this->get_snapshot_link();
211     if (!$snapshotLdap) {
212       $snapshotLdap= $ldap;
213     }
215     // Initialize base
216     $base= preg_replace("/".preg_quote($this->config->current['BASE'], '/')."$/",
217                         "", $objectBase).$this->snapshotLdapBase;
219     /* Fetch all objects with  gosaSnapshotDN=$dn */
220     $snapshotLdap->cd($base);
221     $snapshotLdap->ls("(&(objectClass=gosaSnapshotObject)(gosaSnapshotDN=".$dn."))",$base,
222         array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description"));
224     /* Put results into a list and add description if missing */
225     $objects= array();
226     while($entry = $snapshotLdap->fetch()){
227       if(!isset($entry['description'][0])){
228         $entry['description'][0]  = "";
229       }
230       $objects[] = $entry;
231     }
233     /* Return the raw array, or format the result */
234     if($raw){
235       return($objects);
236     }else{
237       $tmp = array();
238       foreach($objects as $entry){
239         $tmp[base64_encode($entry['dn'])] = $entry['description'][0];
240       }
241     }
242     return($tmp);
243   }
246 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
247 ?>