1 <?php
3 /*! \brief mail plugin
4 \author Guillame Delecourt <guillaume@opensides.be>
5 \author Benoit Mortier <benoit.mortier@opensides.be>
6 \author Vincent Seynhaeve <vincent@opensides.be>
7 \version 1.00
8 \date 25.09.2005
10 This class provides the functionality to read and write all attributes
11 relevant for nagiosAccount from/to the LDAP. It does syntax checking
12 and displays the formulars required.
13 */
15 class nagiosAccount extends plugin
16 {
17 /* Definitions */
18 var $plHeadline= "Nagios";
19 var $plDescription= "This does something";
21 /* plugin specific values */
22 var $NagiosAlias = "";
23 var $NagiosPager = "";
24 var $NagiosMail = "";
26 var $HostNotificationOptions = "";
27 var $HostNotificationPeriod = "";
28 var $ServiceNotificationPeriod = "";
29 var $ServiceNotitificationPeriod = "";
30 var $HostNotificationCommands = "";
31 var $ServiceNotificationCommands = "";
32 var $HostNotitificationOptions = "";
33 var $ServiceNotificationOptions = "";
35 var $AuthorizedAllHosts = "unchecked";
36 var $AuthorizedAllServices = "unchecked";
37 var $AuthorizedSystemCommands = "unchecked";
38 var $AuthorizedAllHostCommands = "unchecked";
39 var $AuthorizedAllServiceCommands = "unchecked";
40 var $AuthorizedConfigurationInformation = "unchecked";
41 var $AuthorizedSystemInformation = "unchecked";
43 /* attribute list for save action */
45 var $attributes= array("NagiosAlias", "NagiosPager", "NagiosMail", "HostNotificationPeriod",
46 "ServiceNotificationPeriod", "HostNotificationCommands", "ServiceNotificationCommands",
47 "HostNotificationOptions", "ServiceNotificationOptions", "AuthorizedAllHosts", "AuthorizedAllServices",
48 "AuthorizedSystemCommands", "AuthorizedAllHostCommands", "AuthorizedAllServiceCommands",
49 "AuthorizedConfigurationInformation", "AuthorizedSystemInformation");
51 var $is_chk_box = array("AuthorizedAllHosts","AuthorizedAllServices","AuthorizedSystemCommands",
52 "AuthorizedAllHostCommands","AuthorizedAllServiceCommands",
53 "AuthorizedConfigurationInformation","AuthorizedSystemInformation");
55 var $objectclasses= array("nagiosContact","nagiosAuth");
57 var $uid = "";
59 /* constructor, if 'dn' is set, the node loads the given
60 'dn' from LDAP */
61 function nagiosAccount ($config, $dn= NULL, $parent= NULL)
62 {
63 /* Configuration is fine, allways */
64 $this->config= $config;
66 plugin::plugin ($config, $dn, $parent);
68 /* Set uid */
69 if(isset($this->attrs['uid'])){
70 $this->uid = $this->attrs['uid'][0];
71 }
73 /* Save initial account state */
74 $this->initially_was_account= $this->is_account;
75 }
77 function execute()
78 {
79 /* Call parent execute */
80 plugin::execute();
82 /* Load templating engine */
83 $smarty= get_smarty();
84 $display= "";
86 /* Do we need to flip is_account state? */
87 if (isset($_POST['modify_state'])){
89 /* Onyl change account state if allowed */
90 if($this->is_account && $this->acl == "#all#"){
91 $this->is_account= !$this->is_account;
92 }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
93 $this->is_account= !$this->is_account;
94 }
95 }
97 /* Do we represent a valid account? */
98 if (!$this->is_account && $this->parent == NULL){
99 $display= "<img alt=\"\"src=\"images/stop.png\" align=\"middle\"> <b>".
100 _("This account has no nagios extensions.")."</b>";
102 $display.= back_to_main();
103 return ($display);
104 }
106 /* Show tab dialog headers */
107 if ($this->parent != NULL){
108 if ($this->is_account){
109 $display= $this->show_header(_("Remove nagios account"),
110 _("This account has nagios features enabled. You can disable them by clicking below."));
111 } else {
112 $display= $this->show_header(_("Create nagios account"), _("This account has nagios features disabled. You can enable them by clicking below."));
113 return($display);
114 }
115 }
117 /* Assign attributes an ACL to smarty */
118 foreach($this->attributes as $val) {
119 $smarty->assign("$val", $this->$val);
120 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
121 if(in_array($val,$this->is_chk_box)){
122 if($this->$val == "checked"){
123 $smarty->assign($val."CHK", " checked ");
124 }else{
125 $smarty->assign($val."CHK", "");
126 }
127 }
128 }
129 $smarty->assign('ServiceNotificationOptionsValues' ,array("d,u,r,v"=>"d,u,r,v" ,"d,u,r"=>"d,u,r" ,"d,u"=>"d,u"));
130 $smarty->assign('HostNotificationOptionsValues' ,array("w,u,c,r"=>"w,u,c,r" ,"w,u,c"=>"w,u,c" ,"c,w"=>"c,w"));
131 $smarty->assign('ServiceNotificationPeriodValues' ,array("24x7"=>"24x7" ,"24x5"=>"24x5" ,"8x5"=>"8x5"));
132 $smarty->assign('HostNotificationPeriodValues' ,array("24x7"=>"24x7" ,"24x5"=>"24x5" ,"8x5"=>"8x5"));
133 $display.= $smarty->fetch (get_template_path('nagios.tpl', TRUE, dirname(__FILE__)));
134 return ($display);
135 }
137 /* Save to LDAP */
138 function save()
139 {
140 $ldap= $this->config->get_ldap_link();
142 /* Call parents save to prepare $this->attrs */
143 plugin::save();
145 /* Adapt attributes if needed */
146 //$method= new $this->method($this->config);
147 //$id= $method->uattrib;
148 //$method->fixAttributesOnStore($this);
150 /* Write back to ldap */
151 $ldap->cd($this->dn);
152 $this->cleanup();
153 $ldap->modify ($this->attrs);
155 show_ldap_error($ldap->get_error(), _("Saving nagios account failed"));
157 /* Optionally execute a command after we're done */
158 if ($this->initially_was_account == $this->is_account){
159 if ($this->is_modified){
160 $this->handle_post_events("modify",array("uid" => $this->uid));
161 }
162 } else {
163 $this->handle_post_events("add", array("uid" => $this->uid));
164 }
165 }
167 function check()
168 {
169 /* Call common method to give check the hook */
170 $message= plugin::check();
172 $ldap= $this->config->get_ldap_link();
173 /* must: mail */
174 if ($this->NagiosAlias == ""){
175 $message[]= _("The required field 'NagiosAlias' is not set.");
176 }
177 if ($this->NagiosMail == ""){
178 $message[]= _("The required field 'NagiosMail' is not set.");
179 }
180 if ($this->is_template){
181 if (!is_email($this->NagiosMail, TRUE)){
182 $message[]= _("Please enter a valid email address in 'NagiosMail' field.");
183 }
184 } else {
185 if (!is_email($this->NagiosMail)){
186 $message[]= _("Please enter a valid email address in 'NagiosMail' field.");
187 }
188 }
189 return($message);
190 }
193 /* Use Save_object for every Post handling */
194 function save_object()
195 {
196 if (isset($_POST['nagiosTab'])){
197 /* Save ldap attributes */
198 plugin::save_object();
200 /* Specialhandling for checkboxes */
201 foreach($this->is_chk_box as $val){
202 if(isset($_POST[$val])){
203 $this->$val = "checked";
204 }else{
205 $this->$val = "unchecked";
206 }
207 }
208 }
209 }
211 function remove_from_parent()
212 {
213 /* Cancel if there's nothing to do here */
214 if (!$this->initially_was_account){
215 return;
216 }
218 /* include global link_info */
219 $ldap= $this->config->get_ldap_link();
221 /* Remove and write to LDAP */
222 plugin::remove_from_parent();
224 /* Adapt attributes if needed */
225 // $method= new $this->method($this->config);
226 // $method->fixAttributesOnRemove($this);
228 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
229 $this->attributes, "Save");
230 $ldap->cd($this->dn);
231 $this->cleanup();
232 $ldap->modify ($this->attrs);
234 show_ldap_error($ldap->get_error(), _("Removing nagios account failed"));
236 /* remove the entry from LDAP */
237 unset ($this->attrs['uid']);
239 /* Optionally execute a command after we're done */
240 $this->handle_post_events('remove',array("uid"=>$this->uid));
241 }
243 }
245 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
247 ?>