FusionDirectory
 All Data Structures Files Functions Variables
class_msg_dialog.inc
Go to the documentation of this file.
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2003-2010 Cajus Pollmeier
5  Copyright (C) 2011-2016 FusionDirectory
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
26 define('INFO_DIALOG', 10001);
27 define('WARNING_DIALOG', 10002);
28 define('ERROR_DIALOG', 10003);
29 // LDAP_ERROR is the same as ERROR_DIALOG
30 define('LDAP_ERROR', 10003);
31 define('CONFIRM_DIALOG', 10004);
32 define('FATAL_ERROR_DIALOG', 10006);
33 
39 {
40  private $s_Title = 'Undefined';
41  private $s_Message = 'Undefined';
42  private $i_Type = INFO_DIALOG;
43  private $i_ID = 0;
44  private $a_Trace = array();
45 
55  public function __construct($s_title, $s_message, $i_type)
56  {
57  if (!in_array($i_type, array(INFO_DIALOG,WARNING_DIALOG,ERROR_DIALOG,CONFIRM_DIALOG,FATAL_ERROR_DIALOG))) {
58  trigger_error('Invalid msg_dialog type.');
59  $i_type = INFO_DIALOG;
60  }
61 
62  $this->i_ID = preg_replace('/[^0-9]*/', '', microtime());
63  $this->s_Title = $s_title;
64  $this->s_Message = $s_message;
65  $this->i_Type = $i_type;
66  }
67 
68  protected function show()
69  {
70  global $config;
71 
72  if (empty($this->s_Message)) {
73  return;
74  }
75 
76  if ((!session::is_set('errorsAlreadyPosted')) || !is_array(session::get('errorsAlreadyPosted'))) {
77  session::set('errorsAlreadyPosted', array());
78  }
79 
80  $errorsAlreadyPosted = session::get('errorsAlreadyPosted');
81  if (!isset($errorsAlreadyPosted[$this->s_Title.$this->s_Message])) {
82  $errorsAlreadyPosted[$this->s_Title.$this->s_Message] = 0;
83  }
84  $errorsAlreadyPosted[$this->s_Title.$this->s_Message]++;
85 
86  session::set('errorsAlreadyPosted', $errorsAlreadyPosted);
87 
88  if ($errorsAlreadyPosted[$this->s_Title.$this->s_Message] > 1) {
89  /* Skip if the same message was already reported once */
90  return;
91  }
92 
93  /* Append trace information, only if error messages are enabled */
94  if (isset($config) && is_object($config) &&
95  $config->get_cfg_value('displayerrors') == 'TRUE') {
96  $this->a_Trace = debug_backtrace();
97  }
98  if ($this->i_Type == FATAL_ERROR_DIALOG) {
99  restore_error_handler();
100  error_reporting(E_ALL);
101  echo $this->execute();
102  } else {
103  if (session::is_set('msg_dialogs')) {
104  $msg_dialogs = session::get('msg_dialogs');
105  } else {
106  $msg_dialogs = array();
107  }
108  $msg_dialogs[] = $this;
109  session::set('msg_dialogs', $msg_dialogs);
110  }
111  }
112 
122  public static function display($s_title, $s_message, $i_type = INFO_DIALOG)
123  {
124  $dialog = new msg_dialog($s_title, $s_message, $i_type);
125  $dialog->show();
126  }
127 
128  /*
129  * \brief Display checks
130  *
131  * \param array $messages Contains messages
132  */
133  public static function displayChecks($messages)
134  {
135  foreach ($messages as $error) {
136  static::display(_('Error'), $error, ERROR_DIALOG);
137  }
138  }
139 
140  /*
141  * \brief Accessor of message dialog's identifier
142  *
143  * \return The identifier of the message dialog
144  */
145  public function get_ID()
146  {
147  return $this->i_ID;
148  }
149 
153  public function execute()
154  {
155  global $config;
156  if ($this->i_Type == FATAL_ERROR_DIALOG) {
157  $display =
158  '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
159  "http://www.w3.org/TR/html4/transitional.dtd">
160  <html><head>
161  <title>FusionDirectory startup failed</title>
162  </head><body>';
163  if (isset($config) && is_object($config) &&
164  $config->get_cfg_value('displayerrors') == 'TRUE') {
165  list($trace,) = html_trace();
166  $display .= $trace;
167  }
168  $display .=
169  '<table style="width:100%; border:2px solid red;">
170  <tr>
171  <td style="vertical-align:top;padding:10px">
172  <img src="geticon.php?context=status&amp;icon=dialog-error&amp;size=32" alt="'._('Error').'"/>
173  </td>
174  <td style="width:100%">
175  <b>'.$this->s_Title.'</b><br/>
176  '.$this->s_Message.'<br><br/>
177  '._('Please fix the above error and reload the page.').'
178  </td>
179  </tr>
180  </table></body></html>';
181  return $display;
182  } else {
183 
184  $smarty = get_smarty();
185  $smarty->assign('s_Trace', print_a($this->a_Trace, TRUE));
186  $smarty->assign('i_TraceCnt', count($this->a_Trace));
187  $smarty->assign('i_Type', $this->i_Type);
188  $smarty->assign('s_Message', $this->s_Message);
189  $smarty->assign('s_Title', $this->s_Title);
190  $smarty->assign('i_ID', $this->i_ID);
191  $smarty->assign('frame', FALSE);
192  $smarty->assign('JS', TRUE);
193  return $smarty->fetch(get_template_path('msg_dialog.tpl'));
194  }
195  }
196 
202  public function is_confirmed()
203  {
204  return isset($_POST['MSG_OK'.$this->i_ID]);
205  }
206 
210  public static function get_dialogs()
211  {
212  $return = "";
213  $dialog_ids = "";
214 
215  if (session::is_set('msg_dialogs') &&
216  is_array(session::get('msg_dialogs')) &&
217  count(session::get('msg_dialogs'))) {
218  /* Get frame one */
219  $smarty = get_smarty();
220  $smarty->assign('frame', TRUE);
221  $return = $smarty->fetch(get_template_path('msg_dialog.tpl'));
222 
223  $msg_dialogs = session::get('msg_dialogs');
224  foreach ($msg_dialogs as $key => $dialog) {
225  if (!preg_match('/'.$dialog->get_ID().'/', '')) {
226  $return .= $dialog->execute();
227  $dialog_ids = $dialog->get_ID().','.$dialog_ids;
228  }
229  unset($msg_dialogs[$key]);
230  }
231  session::set('msg_dialogs', $msg_dialogs);
232  $dialog_ids = preg_replace('/,$/', '', $dialog_ids);
233 
234  $return .= '</div>'."\n";
235  $return .= '<input type="hidden" style="width:400px;" name="pending_msg_dialogs" id="pending_msg_dialogs" value="'.$dialog_ids.'"/>'."\n";
236  $return .= '<input type="hidden" style="width:400px;" name="closed_msg_dialogs" id="closed_msg_dialogs" value=""/>'."\n";
237  $return .= '<input type="hidden" style="width:400px;" name="current_msg_dialogs" id="current_msg_dialogs" value=""/>'."\n";
238  $return .= '<input type="hidden" style="width:700px;" name="js_debug" id="js_debug">'."\n";
239  }
240  return $return;
241  }
242 }
243 ?>
get_template_path($filename= '', $plugin=FALSE, $path= '')
Return themed path for specified base file.
Definition: functions.inc:296
static get_dialogs()
Accessor of the message dialog.
is_confirmed()
Check if the message is confirmed by user.
static set($name, $value)
Set a value in a session.
static & get($name)
Accessor of a session.
& get_smarty()
Get global smarty object.
Definition: functions.inc:934
This class contains all the function needed to make messages dialogs.
execute()
Run the message dialog.
static display($s_title, $s_message, $i_type=INFO_DIALOG)
Display a message dialog.
static is_set($name)
Check if the name of the session is set.
__construct($s_title, $s_message, $i_type)
Message dialog constructor.