1 <?php
3 class faiProfile extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = TRUE;
8 /* Attributes for this Object */
9 var $attributes = array("cn","description","FAIclass");
11 /* ObjectClasses for this Object*/
12 var $objectclasses = array("top","FAIclass","FAIprofile");
14 /* Specific attributes */
15 var $old_cn = "";
16 var $cn = ""; // The class name for this object
17 var $description = ""; // The description for this set of partitions
18 var $is_dialog = false; // specifies which buttons will be shown to save or abort
19 var $FAIclass = ""; // Classnames used for this profile
20 var $FAIclasses = array(); // Contains classname seperated in an array
21 var $FAIAllclasses = array(); // Contains all possible Classnames
22 var $ui ;
23 var $FAIstate = "";
24 var $view_logged = FALSE;
26 var $classSelect;
28 function faiProfile(&$config, $dn= NULL)
29 {
30 /* Load Attributes */
31 plugin::plugin ($config, $dn);
32 $ldap=$this->config->get_ldap_link();
34 $this->ui = get_userinfo();
36 /* Parse ldap attribute to get all assigned classes */
37 $tmp = explode(" ",$this->FAIclass);
38 $tmp2 = array();
39 foreach($tmp as $class){
40 if(!empty($class)){
41 $tmp2[trim($class)] = trim($class);
42 }
43 }
45 /* Sort assigned classes */
46 if(is_array($tmp2)){
47 foreach($tmp2 as $class){
48 $this->FAIclasses[$class]=$class;
49 }
50 }
52 $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
54 /* Build filter */
55 $filter= "";
56 foreach ($categories as $cat){
57 $filter.= "(objectClass=$cat)";
58 }
60 /* Get ldap connection */
61 $base = session::get('CurrentMainBase');
62 $ldap->cd($base);
63 $sort = array();
65 /* search all FAI classes */
66 $ldap->search("(|$filter)",array("*"));
67 while($attrs = $ldap->fetch()){
69 /* Sort by categorie */
70 foreach($categories as $cat){
71 if(in_array_strict($cat,$attrs['objectClass'])){
73 /* Append entry */
74 $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
76 /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
77 $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
78 }
79 }
80 }
82 /* Sort the sort array */
83 //ksort($sort);
85 /* Reorder the FAIclasses array */
86 foreach($sort as $name){
87 $tmp[$name] =$this->FAIAllclasses[$name];
88 }
90 /* Assign sorted classes */
91 $this->FAIAllclasses = array();
92 $this->FAIAllclasses = $tmp;
94 if($dn != "new"){
95 $this->dn =$dn;
96 }
97 $this->old_cn = $this->cn;
99 // Prepare lists
100 $this->classList = new sortableListing(array(),array(), TRUE);
101 $this->classList->setDeleteable(true);
102 $this->classList->setInstantDelete(false);
103 $this->classList->setEditable(false);
104 $this->classList->setWidth("100%");
105 $this->classList->setHeight("200px");
106 $this->classList->setHeader(array(_("Name"),_("Types")));
107 $this->classList->setDefaultSortColumn(0);
108 }
111 function acl_base_for_current_object($dn)
112 {
113 if($dn == "new" || $dn == ""){
114 if($this->dn == "new"){
115 $dn= $this->parent->parent->acl_base;
116 }else{
117 $dn = $this->dn;
118 }
119 }
120 return($dn);
121 }
124 function execute()
125 {
126 /* Call parent execute */
127 plugin::execute();
129 if($this->is_account && !$this->view_logged){
130 $this->view_logged = TRUE;
131 new log("view","fai/".get_class($this),$this->dn);
132 }
134 /* Fill templating stuff */
135 $smarty= get_smarty();
136 $display= "";
138 $s_entry = "";
139 $s_action = "";
141 /* Remove class name From list */
142 $sort_once = false;
143 if(!preg_match("/freeze/",$this->FAIstate)){
145 if(isset($_POST['AddClass']) && $this->acl_is_writeable("FAIclass")){
146 $this->classSelect = new classSelect($this->config, get_userinfo());
147 $this->dialog =true;
148 }
151 /* Save Dialog */
152 if(isset($_POST['classSelect_save']) && $this->classSelect instanceOf classSelect){
153 $list = $this->classSelect->save();
154 foreach($list as $entry){
155 $class = $entry['cn'][0];
156 $this->FAIclasses[$class] =$class;
157 }
158 $this->dialog=false;
159 $this->classSelect=FALSE;
160 }
161 }
163 /* Cancel Dialog */
164 if(isset($_POST['classSelect_cancel']) && $this->classSelect instanceOf classSelect){
165 $this->dialog=false;
166 $this->classSelect=FALSE;
167 }
169 if($this->classSelect instanceOf classSelect){
170 session::set('filterFAIClass_base',$this->parent->parent->fai_release);
171 session::set('filterBlacklist', array('cn' => array_values($this->FAIclasses)));
172 return($this->classSelect->execute());
173 }
175 /* item images */
176 $objTypes['FAIhook'] = image('plugins/fai/images/fai_hook.png','',_("Hook bundle"));
177 $objTypes['FAItemplate'] = image('plugins/fai/images/fai_template.png','',_("Template bundle"));
178 $objTypes['FAIscript'] = image('plugins/fai/images/fai_script.png','',_("Script bundle"));
179 $objTypes['FAIvariable'] = image('plugins/fai/images/fai_variable.png','',_("Variable bundle"));
180 $objTypes['FAIpackageList'] = image('plugins/fai/images/fai_packages.png','',_("Package bundle"));
181 $objTypes['FAIpartitionTable'] = image('plugins/fai/images/fai_partitionTable.png','',_("Partition table"));
183 $emp = image('images/empty.png');
185 /* Delete button */
186 $actions = image('images/lists/trash.png','DEL_%KEY%',_("Remove class from profile"));
188 /* Up down buttons */
189 $linkupdown = image('images/lists/sort-up.png','sortup_%s',_("Up"), 'top');
190 $linkupdown.= image('images/lists/sort-down.png','sortdown_%s',_("Down"), 'bottom');
192 $this->classList->setAcl($this->getacl('FAIclass',preg_match('/^freeze/', $this->FAIstate)));
193 $data = $lData = array();
194 foreach($this->FAIclasses as $usedClass){
195 $str = "";
196 if(isset($this->FAIAllclasses[$usedClass])){
197 foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
198 $str.= $objTypes[$class];
199 }
200 }
201 $data[$usedClass] = $usedClass;
202 $lData[$usedClass] = array('data'=>array($usedClass,$str));
203 }
204 $this->classList->setListData($data,$lData);
205 $this->classList->update();
208 $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));
209 $smarty->assign("FAIclasses" ,$this->FAIclasses);
210 $smarty->assign("listing" ,$this->classList->render());
212 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
213 * If we post the escaped strings they will be escaped again
214 */
215 foreach($this->attributes as $attrs){
216 $smarty->assign($attrs,set_post($this->$attrs));
217 }
220 $dn = $this->acl_base_for_current_object($this->dn);
221 $smarty->assign("sub_object_is_addable",
222 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
223 !preg_match("/freeze/",$this->FAIstate));
225 $tmp = $this->plInfo();
226 foreach($tmp['plProvidedAcls'] as $name => $translated){
227 $smarty->assign($name."ACL",$this->getacl($name, preg_match("/freeze/",$this->FAIstate)));
228 }
230 $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
231 return($display);
232 }
234 function remove_from_parent()
235 {
236 $ldap = $this->config->get_ldap_link();
237 $ldap->cd ($this->dn);
238 $release = $this->parent->parent->fai_release;
239 $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
240 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
241 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
242 $this->handle_post_events("remove");
243 }
246 /* Save data to object
247 */
248 function save_object()
249 {
250 if(!preg_match("/freeze/",$this->FAIstate)){
251 plugin::save_object();
253 $this->classList->save_object();
254 $action = $this->classList->getAction();
255 if($action['action'] == 'reorder'){
256 $this->FAIclasses = $this->classList->getMaintainedData();
257 }
258 if($action['action'] == 'delete'){
259 $s_entry = $this->classList->getKey($action['targets'][0]);
260 unset($this->FAIclasses[$s_entry]);
261 }
262 }
263 }
266 /* Check supplied data */
267 function check()
268 {
269 /* Call common method to give check the hook */
270 $message= plugin::check();
272 if(count($this->FAIclasses) == 0){
273 $message[]=_("No class specified for this profile!");
274 }
276 if($this->cn == ""){
277 $message[]= msgPool::required(_("Name"));
278 }
280 /* Ensure that we do not overwrite an allready existing entry
281 */
282 if($this->dn == "new" || $this->cn != $this->old_cn){
283 $release = $this->parent->parent->fai_release;
284 $new_dn= 'cn='.$this->cn.",".get_ou("faiProfile", "faiProfileRDN").get_ou("faiManagement", "faiBaseRDN").$release;
285 $res = faiManagement::check_class_name("FAIprofile",$this->cn,$new_dn);
286 if(isset($res[$this->cn])){
287 $message[] = msgPool::duplicated(_("Name"));
288 }
289 }
291 return ($message);
292 }
295 /* Save to LDAP */
296 function save()
297 {
298 plugin::save();
300 $ldap = $this->config->get_ldap_link();
302 $this->FAIclass = "";
303 foreach($this->FAIclasses as $class){
304 $this->FAIclass.=$class." ";
305 }
307 $this->attrs['FAIclass']=trim($this->FAIclass);
309 /* Remove the old FAI profile, if the dn has changed.
310 */
311 if($this->cn != $this->old_cn && $this->old_cn != ""){
313 $old_dn = preg_replace("/^cn=[^,]+,/","cn=".$this->old_cn.",",$this->dn);
314 FAI::prepare_to_save_FAI_object($old_dn,array(),TRUE);
315 }
316 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
318 if($this->initially_was_account){
319 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
320 }else{
321 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
322 }
323 }
326 function PrepareForCopyPaste($source)
327 {
328 plugin::PrepareForCopyPaste($source);
330 /* Parse ldap attribute to get all assigned classes */
331 $tmp = explode(" ",$this->FAIclass);
332 $tmp2 = array();
333 foreach($tmp as $class){
334 if(!empty($class)){
335 $tmp2[trim($class)] = trim($class);
336 }
337 }
339 /* Sort assigned classes */
340 if(is_array($tmp2)){
341 foreach($tmp2 as $class){
342 $this->FAIclasses[$class]=$class;
343 }
344 }
345 }
348 /* Return plugin informations for acl handling */
349 static function plInfo()
350 {
351 return (array(
352 "plShortName" => _("Profile"),
353 "plDescription" => _("FAI profile"),
354 "plSelfModify" => FALSE,
355 "plDepends" => array(),
356 "plPriority" => 30,
357 "plSection" => array("administration"),
358 "plCategory" => array("fai"),
360 "plProperties" =>
361 array(
362 array(
363 "name" => "faiProfileRDN",
364 "type" => "rdn",
365 "default" => "ou=profiles,",
366 "description" => _("RDN for FAI profile storage."),
367 "check" => "gosaProperty::isRdn",
368 "migrate" => "migrate_faiProfileRDN",
369 "group" => "plugin",
370 "mandatory" => TRUE)),
373 "plProvidedAcls" => array(
374 "cn" => _("Name"),
375 "description" => _("Description"),
376 "FAIclass" => _("FAI classes"))
377 ));
378 }
381 /*! \brief Used for copy & paste.
382 Returns a HTML input mask, which allows to change the cn of this entry.
383 @param Array Array containing current status && a HTML template.
384 */
385 function getCopyDialog()
386 {
387 $vars = array("cn");
388 $smarty = get_smarty();
389 $smarty->assign("cn", set_post($this->cn));
390 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
391 $ret = array();
392 $ret['string'] = $str;
393 $ret['status'] = "";
394 return($ret);
395 }
398 /*! \brief Used for copy & paste.
399 Some entries must be renamed to avaoid duplicate entries.
400 */
401 function saveCopyDialog()
402 {
403 if(isset($_POST['cn'])){
404 $this->cn = get_post('cn');
405 }
406 }
407 }
409 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
410 ?>