1 <?php
3 class faiProfile extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary = "Manage server basic objects";
7 var $cli_description = "Some longer text\nfor help";
8 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account = TRUE;
13 /* Attributes for this Object */
14 var $attributes = array("cn","description","FAIclass");
16 /* ObjectClasses for this Object*/
17 var $objectclasses = array("top","FAIclass","FAIprofile");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "FAIscriptEntry";
21 var $subClasses = array("top","FAIclass","FAIscriptEntry");
23 /* Specific attributes */
24 var $old_cn = "";
25 var $cn = ""; // The class name for this object
26 var $description = ""; // The description for this set of partitions
27 var $is_dialog = false; // specifies which buttons will be shown to save or abort
28 var $dialog = NULL; // a dialog, e.g. new disk dialog
29 var $FAIclass = ""; // Classnames used for this profile
30 var $FAIclasses = array(); // Contains classname seperated in an array
31 var $FAIAllclasses = array(); // Contains all possible Classnames
33 function faiProfile($config, $dn= NULL)
34 {
35 /* Load Attributes */
36 plugin::plugin ($config, $dn);
37 $ldap=$this->config->get_ldap_link();
39 $tmp = split(" ",$this->FAIclass);
41 foreach($tmp as $class){
42 if(!empty($class)){
43 $this->FAIclasses[trim($class)] = trim($class);
44 }
45 }
46 natcasesort($this->FAIclasses);
47 reset($this->FAIclasses) ;
49 $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTAble","FAIpackage");
51 $sort = array();
53 $base = $_SESSION['faifilter']['base'];
54 $ldap->cd($base);
55 $ldap->search("(objectClass=*)",array("*"));
56 while($attrs = $ldap->fetch()){
57 foreach($categories as $cat){
58 if(in_array($cat,$attrs['objectClass'])){
59 $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
60 $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
61 }
62 }
63 }
64 ksort($sort);
65 foreach($sort as $name){
66 $tmp[$name] =$this->FAIAllclasses[$name];
67 }
68 $this->FAIAllclasses = array();
69 $this->FAIAllclasses = $tmp;
71 if($dn != "new"){
72 $this->dn =$dn;
73 }
74 $this->old_cn = $this->cn;
75 }
77 function execute()
78 {
79 /* Call parent execute */
80 plugin::execute();
82 /* Fill templating stuff */
83 $smarty= get_smarty();
84 $display= "";
86 $s_entry = "";
87 $s_action = "";
89 /* Remove class name From list */
90 foreach($_POST as $name => $post){
91 if(preg_match("/DEL_/i",$name)){
92 $s_action = "delete";
93 $s_entry = preg_replace("/DEL_/","",$name);
94 $s_entry = preg_replace("/_.*$/","",$s_entry);
95 }elseif(preg_match("/Add_class/i",$name)){
96 $s_action = "add";
97 }elseif(preg_match("/DelClass/i",$name)){
98 $s_action = "delete";
99 $s_entry = $_POST['FAIclass'];
100 }elseif(preg_match("/AddClass/i",$name)){
101 $s_action = "add";
102 }
103 }
105 if($s_action == "delete"){
106 unset($this->FAIclasses[$s_entry]);
107 }
109 if($s_action == "add"){
110 $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses);
111 $this->is_dialog =true;
112 }
114 /* Save Dialog */
115 if(isset($_POST['SaveSubObject'])){
116 $this->dialog->save_object();
117 $msgs= $this->dialog->check();
118 if(count($msgs)){
119 print_red($msgs);
120 }else{
121 $ret = $this->dialog->save();
122 foreach($ret as $class){
123 $this->FAIclasses[$class] =$class;
124 }
125 $this->is_dialog=false;
126 unset($this->dialog);
127 $this->dialog=NULL;
128 ksort($this->FAIclasses);
129 }
130 }
132 /* Cancel Dialog */
133 if(isset($_POST['CancelSubObject'])){
134 $this->is_dialog=false;
135 unset($this->dialog);
136 $this->dialog=NULL;
137 }
139 if(isset($this->dialog)){
140 $this->dialog->save_object();
141 return($this->dialog->execute());
142 }
144 $divlist =new divlist("Profile");
145 $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
146 $divlist->SetEntriesPerPage(0);
148 $divlist->SetHeader(array(array("string"=>"Class name"),
149 array("string"=>"Objects","attach"=>"style='width:120px;'"),
150 array("string"=>"Options","attach"=>"style='border-right:none;width:60px;'")));
151 $listhead =
152 "<div style='background:#F0F0F9;padding:5px;'>".
153 "<input type='image' align='middle' src='images/editpaste.png' title='"._("Append new class names")."' alt='"._("Add")."' name='Add_class'> ".
154 "</div>";
156 $objTypes['FAIhook'] = "<image src='images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
157 $objTypes['FAItemplate'] = "<image src='images/fai_template.png' title='"._("Template bundle")."' alt=''>";
158 $objTypes['FAIscript'] = "<image src='images/fai_script.png' title='"._("Script bundle")."' alt=''>";
159 $objTypes['FAIvariable'] = "<image src='images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
160 $objTypes['FAIpackages'] = "<image src='images/fai_packages.png' title='"._("Packages bundle")."' alt=''>";
161 $objTypes['FAIpartitionTable'] = "<image src='images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
163 $actions = "<input type='image' src='images/editdelete.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>";
165 foreach($this->FAIclasses as $usedClass){
166 $str = "";
167 if(isset($this->FAIAllclasses[$usedClass])){
168 foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
169 $str.= $objTypes[$class];
170 }
171 }
173 $field1 = array("string"=> $usedClass,"attach"=>"");
174 $field2 = array("string"=> $str,"attach"=>"");
175 $field3 = array("string"=> preg_replace("/%KEY%/",$usedClass,$actions),"attach"=>"style='border-right:none;'");
176 $divlist->AddEntry(array($field1,$field2,$field3));
177 }
179 $smarty->assign("FAIclasses" ,$this->FAIclasses);
180 $smarty->assign("divlisthead" ,$listhead);
181 $smarty->assign("divlist" ,$divlist->DrawList());
183 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
184 * If we post the escaped strings they will be escaped again
185 */
186 foreach($this->attributes as $attrs){
187 if(get_magic_quotes_gpc()){
188 $smarty->assign($attrs,stripslashes($this->$attrs));
189 }else{
190 $smarty->assign($attrs,($this->$attrs));
191 }
192 }
195 $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
196 return($display);
197 }
199 function remove_from_parent()
200 {
201 $ldap = $this->config->get_ldap_link();
202 $ldap->cd ($this->dn);
203 $ldap->rmdir_recursive($this->dn);
204 $this->handle_post_events("remove");
205 }
207 /* Save data to object
208 */
209 function save_object()
210 {
211 plugin::save_object();
212 foreach($this->attributes as $attrs){
213 if(isset($_POST[$attrs])){
214 $this->$attrs = $_POST[$attrs];
215 }
216 }
217 }
220 /* Check supplied data */
221 function check()
222 {
223 $message= array();
225 if(count($this->FAIclasses) == 0){
226 $message[]=_("Please assign at least one class to this profile.");
227 }
229 if(empty($this->cn)){
230 $message[]=_("Please enter a valid name.");
231 }
233 $ldap = $this->config->get_ldap_link();
234 $ldap->cd($_SESSION['faifilter']['base']);
235 $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
237 if($ldap->count()){
238 $message[]=_("There is already a profile with this class name defined.");
239 }
241 return ($message);
242 }
245 /* Save to LDAP */
246 function save()
247 {
248 plugin::save();
250 $ldap = $this->config->get_ldap_link();
252 $this->FAIclass = "";
253 foreach($this->FAIclasses as $class){
254 $this->FAIclass.=$class." ";
255 }
257 $this->attrs['FAIclass']=trim($this->FAIclass);
259 $ldap->cat($this->dn);
260 if($ldap->count()!=0){
261 /* Write FAIscript to ldap*/
262 $ldap->cd($this->dn);
263 $ldap->modify($this->attrs);
264 }else{
265 /* Write FAIscript to ldap*/
266 $ldap->cd($this->config->current['BASE']);
267 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
268 $ldap->cd($this->dn);
269 $ldap->add($this->attrs);
270 }
271 show_ldap_error($ldap->get_error());
272 }
273 }
275 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
276 ?>