e6de1acf1aaa9fd1bc928391f2e32f11dffa4a7b
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 */
23 define("INFO_DIALOG" , 10001);
24 define("WARNING_DIALOG" , 10002);
25 define("ERROR_DIALOG" , 10003);
26 define("CONFIRM_DIALOG" , 10004);
27 define("OK_CANCEL_DIALOG" , 10005);
28 define("FATAL_ERROR_DIALOG" , 10006);
30 class msg_dialog
31 {
32 private $s_Title = "Undefined";
33 private $s_Message = "Undefined";
34 private $i_Type = INFO_DIALOG ;
35 private $i_ID = 0;
36 private $a_Trace = array();
38 public function __construct($s_title,$s_message,$i_type)
39 {
40 global $config;
42 if(empty($s_message)) return;
44 if(!in_array($i_type,array(INFO_DIALOG,WARNING_DIALOG,ERROR_DIALOG,CONFIRM_DIALOG,FATAL_ERROR_DIALOG,OK_CANCEL_DIALOG))){
45 trigger_error("Invalid msg_dialog type.");
46 $i_type = INFO_DIALOG;
47 }
49 if((!session::is_set('errorsAlreadyPosted')) || !is_array(session::get('errorsAlreadyPosted'))){
50 session::set('errorsAlreadyPosted',array());
51 }
53 $errorsAlreadyPosted = session::get('errorsAlreadyPosted');
54 if(!isset($errorsAlreadyPosted[$s_title.$s_message])){
55 $errorsAlreadyPosted[$s_title.$s_message] = 0;
56 }
57 $errorsAlreadyPosted[$s_title.$s_message]++;
59 if($errorsAlreadyPosted[$s_title.$s_message] <=1 ){
61 $this->i_ID = preg_replace("/[^0-9]*/","",microtime());
62 $this->s_Title = $s_title;
63 $this->s_Message = $s_message;
64 $this->i_Type = $i_type;
66 /* Append trace information, only if error messages are enabled */
67 if( isset($config) &&
68 is_object($config) &&
69 isset($config->data['MAIN']['DISPLAYERRORS']) &&
70 preg_match("/^true$/i",$config->data['MAIN']['DISPLAYERRORS'])){
71 $this->a_Trace = debug_backtrace();
72 }
73 if(!session::is_set('msg_dialogs')){
74 session::set('msg_dialogs',array());
75 }
76 if($this->i_Type == FATAL_ERROR_DIALOG){
77 restore_error_handler();
78 error_reporting(E_ALL);
79 echo $this->execute();
80 }else{
81 $msg_dialogs = session::get('msg_dialogs');
82 $msg_dialogs[] = $this;
83 session::set('msg_dialogs',$msg_dialogs);
84 }
85 }
86 session::set('errorsAlreadyPosted',$errorsAlreadyPosted);
87 }
90 public static function display($s_title,$s_message,$i_type = INFO_DIALOG)
91 {
92 new msg_dialog($s_title,$s_message,$i_type);
93 }
96 public static function displayChecks($messages)
97 {
98 /* Assemble the message array to a plain string */
99 foreach ($messages as $error){
100 msg_dialog::display(_("Error"), $error, ERROR_DIALOG);
101 }
102 }
105 public function get_ID()
106 {
107 return($this->i_ID);
108 }
110 public function execute()
111 {
112 if($this->i_Type == FATAL_ERROR_DIALOG) {
113 $display =
114 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
115 \"http://www.w3.org/TR/html4/transitional.dtd\">
116 <html><head>
117 <title>GOsa startup failed</title>
118 </head><body>
119 <table style='width:100%; border:2px solid red;' summary=''>
120 <tr><td style='vertical-align:top;padding:10px'>
121 <img src='images/error.png' alt='{t}Error{/t}'>
122 </td><td style='width:100%'>
123 <b>".$this->s_Title."</b><br>
124 ".$this->s_Message."<br><br>
125 "._("Please fix the above error and reload the page.")."
126 </td></tr>
127 </table></body></html>";
128 return($display);;
129 }else{
131 $smarty = get_smarty();
132 $smarty->assign("s_Trace" ,print_a($this->a_Trace,TRUE));
133 $smarty->assign("i_TraceCnt",count($this->a_Trace));
134 $smarty->assign("i_Type",$this->i_Type);
135 $smarty->assign("s_Message",$this->s_Message);
136 $smarty->assign("s_Title",$this->s_Title);
137 $smarty->assign("i_ID",$this->i_ID);
138 $smarty->assign("frame",false);
139 $smarty->assign("JS",session::get('js'));
140 $smarty->assign("IE",preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']));
141 return($smarty->fetch(get_template_path('msg_dialog.tpl')));
142 }
143 }
146 public function is_confirmed()
147 {
148 if(isset($_POST['MSG_OK'.$this->i_ID])){
149 return(TRUE);
150 }else{
151 return(FALSE);
152 }
153 }
156 public static function get_dialogs()
157 {
158 $return ="";
159 $dialog_ids= "";
161 $seen = "";
162 if(isset($_POST['closed_msg_dialogs'])){
163 # $seen = $_POST['closed_msg_dialogs'];
164 }
166 if(session::is_set('msg_dialogs') && is_array(session::get('msg_dialogs')) && count(session::get('msg_dialogs'))){
168 /* Get frame one */
169 $smarty = get_smarty();
170 $smarty->assign("frame", true);
171 $smarty->assign("IE",preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']));
172 $return = $smarty->fetch(get_template_path('msg_dialog.tpl'));
174 if(!session::get('js')){
175 $dialog = array_pop(session::get('msg_dialogs'));
176 $return.= $dialog->execute();
177 }else{
178 $msg_dialogs = session::get('msg_dialogs');
179 foreach($msg_dialogs as $key => $dialog){
181 if(preg_match("/".$dialog->get_ID()."/",$seen)){
182 unset($msg_dialogs[$key]);
183 }else{
184 $return.= $dialog->execute();
185 $dialog_ids= $dialog->get_ID().",".$dialog_ids;
186 }
187 unset($msg_dialogs[$key]);
188 }
189 session::set('msg_dialogs',$msg_dialogs);
190 }
191 $dialog_ids = preg_replace("/,$/","",$dialog_ids);
193 $return .= "</div>";
194 $return .="<input type='hidden' style='width:400px;' name='pending_msg_dialogs' id='pending_msg_dialogs' value='".$dialog_ids."'>";
195 $return .="<input type='hidden' style='width:400px;' name='closed_msg_dialogs' id='closed_msg_dialogs' value=''>";
196 $return .="<input type='hidden' style='width:400px;' name='current_msg_dialogs' id='current_msg_dialogs' value=''>";
197 $return .="<input type='hidden' style='width:700px;' name='js_debug' id='js_debug'>";
198 }
199 return($return);
200 }
202 }
203 ?>