Code

Updated DAK class names.
[gosa.git] / gosa-plugins / dak / addons / dak / class_dakkeyring.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: class_gotomasses.inc 10967 2008-05-20 13:18:12Z hickert $$
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 /*! \brief  The DAK keyring class used to manage DAK keyrings.
25             It provides remove/add key mehtods and lists all keys for a selected server.
26  */
27 class dak_keyring extends plugin
28 {
29   private $Servers          = array();
30   private $selected_Server  = array();
31   private $list             = array();
32   public $attributes        = array("selected_Server");
35   /*! \brief   Creates a dak_keyring class.
36       @param   The GOsa configuration object. 
37    */
38   public function __construct($config)
39   {
40     plugin::plugin($config,NULL);
42     /* Collect release servers and store them
43      */
44     $this->Servers = DAK::get_repositories_by_server($this->config);
45     if(count($this->Servers)){
46       $this->selected_Server  = key($this->Servers);
47     }
48   }
51   /*! \brief  Create HTML output for this plugin.
52       @return String  HTML output.
53    */
54   public function execute()
55   {
56     if(isset($_POST['search'])){
57       $this->refresh_list();
58     }
60     $smarty= get_smarty();
61     $smarty->assign("Servers"         , $this->Servers);
62     $smarty->assign("selected_Server" , $this->selected_Server);
63     $smarty->assign("list",$this->CreateList());
64     return($smarty->fetch (get_template_path('dak_keyring.tpl', TRUE, dirname(__FILE__))));
65   }
68   /*! \brief  Refresh the list of keys for the currently selected server 
69               This function is automatically called when a key was added and removed.
70    */
71   private function refresh_list()
72   {
73     $this->list =  DAK::list_keys($this->Servers[$this->selected_Server]['MAC']);
74   } 
77   /*! \brief  Creates the HTML output representing the keylist 
78               for this currently selected server 
79       @return String HTML list containig all keys for the selected server.
80    */
81   private function CreateList()
82   {
83     $divlist = new divlist("DAK_keyring");
84     $divlist->SetWidth("100%");
85     $divlist->SetHeight("450px");
86     $divlist->SetEntriesPerPage(0);
88     /* Set default col styles 
89      */
90     $s0 = "style='width:10px;'";
91     $s1 = "style='width:100px;'";
92     $s2 = "style='width:100px;'";
93     $s3 = "style='width:100px;'";
94     $s4 = "";
95     $s5 = "style='width:100px; border-right:0px;'";
97     /* Add header 
98      */
99     $h0 = array("string" => "",            "attach" => $s0);
100     $h1 = array("string" => _("Key ID"),   "attach" => $s1);
101     $h2 = array("string" => _("Length"),   "attach" => $s2);
102     $h3 = array("string" => _("Validity"), "attach" => $s3);
103     $h4 = array("string" => _("UID"),      "attach" => $s4);
104     $h5 = array("string" => _("Action"),   "attach" => $s5);
105     $divlist->SetHeader(array($h0,$h1,$h2,$h3,$h4,$h5));
107     /* Add entries 
108      */
109     foreach($this->list as $key => $entry){
111       /* Check if all attributes are given
112        */
113       if(!is_array($entry['UID'])){
114         $entry['UID'] = array($entry['UID']);
115       }
116       $key_id = $length = $valid = "";
118       if(isset($entry['ATTRIBUTES']['UID'])){
119         $key_id = $entry['ATTRIBUTES']['UID'];
120       }
121       if(isset($entry['ATTRIBUTES']['LENGTH'])){
122         $length = $entry['ATTRIBUTES']['LENGTH'];
123       }
124       if(isset($entry['ATTRIBUTES']['VALID'])){
125         $valid  = $entry['ATTRIBUTES']['VALID'];
126       }
128       /* Create detail icon 
129        */  
130       $hide = "<input type='image' name='details_".$key."' src='images/forward-arrow.png' alt='-' class='center'>&nbsp;";
131       $down = "<input type='image' name='details_".$key."' src='images/down-arrow.png' alt='+' class='center'>&nbsp;";
133       $actions = "<input type='image' class='center' alt='".msgPool::delButton()."' 
134                     src='images/lists/trash.png' name='remove_key_".$key."'>";
136       /* Add detailed entry 
137           (All UIDs are listed, each in a single column)
138        */
139       if(isset($entry['DETAILS']) && $entry['DETAILS'] == TRUE){
140         $first = TRUE;
141         foreach($entry['UID'] as $val){
143           $f0 = array("string" => "","attach" => $s0);
144           $f1 = array("string" => "","attach" => $s1);
145           $f2 = array("string" => "","attach" => $s2);
146           $f3 = array("string" => "","attach" => $s3);
147           $f4 = array("string" => htmlentities($val) ,"attach" => $s4);
148           $f5 = array("string" => "","attach" => $s5);
150           if($first){
151             $first = FALSE;
152             $f0 = array("string" => $hide         ,"attach" => $s0);
153             $f1 = array("string" => $key_id       ,"attach" => $s1);
154             $f2 = array("string" => $length       ,"attach" => $s2);
155             $f3 = array("string" => $valid        ,"attach" => $s3);
156             $f4 = array("string" => htmlentities($val) ,"attach" => $s4);
157             $f5 = array("string" => $actions      ,"attach" => $s5);
158           }
159           $divlist->AddEntry(array($f0,$f1,$f2,$f3,$f4,$f5));
160         }
161       }else{
163         /* Add non detailed entries, just the first uid is displayed.
164          */
165         if(count($entry['UID']) > 1){
166           $f0 = array("string" => $down,         "attach" => $s0);
167         }else{
168           $f0 = array("string" => "",         "attach" => $s0);
169         }
170         $f1 = array("string" => $key_id,       "attach" => $s1);
171         $f2 = array("string" => $length,       "attach" => $s2);
172         $f3 = array("string" => $valid,        "attach" => $s3);
173         $f4 = array("string" => htmlentities($entry['UID'][0]), "attach" => $s4);
174         $f5 = array("string" => $actions,"attach" => $s5);
175         $divlist->AddEntry(array($f0,$f1,$f2,$f3,$f4,$f5));
177       }
178     }
179     return($divlist->DrawList());
180   }
183   /*! \brief  Act on the HTML posts for this plugin 
184    */
185   public function save_object()
186   {
187     plugin::save_object();
189     /* Save html posts, like the selected repository server 
190      */
191     foreach($this->attributes as $attr){
192       if(isset($_POST[$attr])){
193         $this->$attr = get_post($attr);
194       }
195       }
196   
197     /* Display details icon was clicked
198      */
199     foreach($_POST as $name => $value){
200       if(preg_match("/^remove_key_/",$name)){
201         $id = preg_replace("/^remove_key_([0-9]*)_.*$/","\\1",$name);
202         $this->remove_key($id);
203         break;
204       }
206       if(preg_match("/^details_/",$name)){
207         $id = preg_replace("/^details_([0-9]*)_.*$/","\\1",$name);
208         if(isset($this->list[$id])){
209           if(!isset($this->list[$id]['DETAILS']) || !$this->list[$id]['DETAILS']){
210             $this->list[$id]['DETAILS'] = TRUE;
211           }else{
212             $this->list[$id]['DETAILS'] = FALSE;
213           }
214           break;
215         }
216       }
217     }
219     /* Import new keys 
220      */
221     if(isset($_POST['import_key']) && isset($_FILES['import'])){
222       if($_FILES['import']['size'] != 0){
223         $key = file_get_contents($_FILES['import']['tmp_name']);
224         if($this->import_key($key)){
225           $this->refresh_list();
226         }
227       }
228     }
229   }
231    
232   /*! \brief   Removes the given key from the keyring server.
233       @return  Boolean TRUE in case of success else FALSE.
234    */ 
235   private function remove_key($key)
236   {
237     if($this->list[$key]){
238       $mac = $this->Servers[$this->selected_Server]['MAC'];
239       return(DAK::remove_key($mac,$this->list[$key]['ATTRIBUTES']['UID']));
240     }
241   }
242   
244   /*! \brief  Imports a new key into the keyring for the currently selected server 
245       @return  Boolean TRUE in case of success else FALSE.
246    */
247   private function import_key($key)
248   {
249     $mac = $this->Servers[$this->selected_Server]['MAC'];
250     return(DAK::import_key($mac,$key));
251   }
254   /*! \brief  ACL plugin information  
255    */
256   static function plInfo()
257   {
258     return (array(
259           "plShortName"   => _("DAK keyring")."&nbsp;("._("Addon").")",
260           "plDescription" => _("DAK keyring management")."&nbsp;("._("Addon").")",
261           "plSelfModify"  => FALSE,
262           "plDepends"     => array(),
263           "plPriority"    => 98,
264           "plSection"       => array("addon"),
265           "plCategory"      => array("server"),
266           "plProvidedAcls"  => 
267               array(
268                    ) 
269           ));
270   }
272 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
273 ?>