YaWK  24.1
Yet another WebKit
loginbox.php
Go to the documentation of this file.
1 <?php
3 {
4  /**
5  * @details<b>Loginbox - draw a loginbox (or logout button)</b>
6  *
7  * <p>In case you need a user login form, use this widget. If the user is not logged in,
8  * a form will be displayed, requesting username and password where users can login. In
9  * case the user is already logged in, a hello {user}! message will be displayed, followed
10  * by a logout button </p>
11  *
12  *
13  * @author Daniel Retzl <[email protected]>
14  * @copyright 2018 Daniel Retzl
15  * @version 1.0.0
16  * @brief Loginbox Widget
17  */
18  class loginbox extends \YAWK\widget
19  {
20  /** @param object global widget object data */
21  public $widget = '';
22  /** @param string Title that will be shown above widget */
23  public $loginboxHeading = '';
24  /** @param string Subtext will be displayed beside title */
25  public $loginboxSubtext = '';
26  /** @param string Username */
27  public $currentUser = '';
28  /** @param string Login Button Text */
29  public $loginboxLoginBtnText = "Login";
30  /** @param string Logout Button Text */
31  public $loginboxLogoutBtnText = "Logout";
32  /** @param string Logout Button html markup */
33  public $loginboxLogoutBtnMarkup = "";
34  /** @param string Login Button CSS Class */
35  public $loginboxLoginBtnClass = "btn btn-success";
36  /** @param string Logout Button CSS Class */
37  public $loginboxLogoutBtnClass = "btn btn-danger";
38  /** @param string Login Button margin-top */
39  public $loginboxLoginBtnMarginTop = "5px";
40  /** @param string Login Button margin top css markup */
42  /** @param string form width */
43  public $loginboxWidth = '';
44  /** @param string form width css markup */
45  public $loginboxWidthMarkup = '';
46  /** @param string form css class */
47  public $loginboxFormClass = '';
48  /** @param string form css class markup */
49  public $loginboxFormClassMarkup = '';
50  /** @param string ajax|html form processing mode */
51  public $loginboxProcessingMode = 'html';
52  /** @param string markup for html/ajax processing mode */
54  /** @param string submit button type, depending on processing mode */
55  public $loginboxProcessingModeSubmitBtnType = 'submit';
56  /** @param bool true|false Turn greeting on or off */
57  public $loginboxGreeting = '';
58  /** @param string Greeting Text */
59  public $loginboxGreetingText = '';
60  /** @param string Greeting html markup */
61  public $loginboxGreetingMarkup = '';
62  /** @param string Greeting Subtext */
63  public $loginboxGreetingSubtext = '';
64  /** @param bool true|false Show name within greeting? */
65  public $loginboxGreetingShowName = 'true';
66  /** @param string Greeting text type */
67  public $loginboxGreetingTextType = 'h2';
68  /** @param string Greeting text class */
69  public $loginboxGreetingTextClass = '';
70  /** @param string Greeting text class html markup */
72  /** @param string redirect to this page after successful login */
73  public $loginboxRedirect = '';
74  /** @param int delay before redirecting */
75  public $loginboxRedirectTime = 0;
76 
77  /**
78  * @brief Load all widget settings from database and fill object
79  * @param object $db Database Object
80  * @brief Load all widget settings on object init.
81  */
82  public function __construct($db)
83  {
84  // load this widget settings from db
85  $this->widget = new \YAWK\widget();
87  foreach ($settings as $property => $value) {
88  $this->$property = $value;
89  }
90  }
91 
92  /**
93  * @brief Init loginbox widget and call a function for demo purpose
94  * @brief loginbox Widget Init
95  */
96  public function init($db)
97  {
98  // check if a user is there
99  if ($this->currentUser = \YAWK\user::isAnybodyThere($db))
100  { // check if currentUser is logged in
101  if (\YAWK\user::isLoggedIn($db, $this->currentUser))
102  {
103  // user is logged in...
104  $this->drawLogoutButton($db);
105  }
106  else
107  { // user is not logged in, draw loginbox
108  $this->drawLoginBox($db, $this->currentUser, "");
109  }
110  }
111  else
112  { // no user is there, draw loginbox
113  $this->drawLoginBox($db, "", "");
114  }
115  }
116 
117  /**
118  * @brief Prepare Loginbox form settings
119  * @brief Evaluate loginbox settings and prepare html markup
120  */
121  public function setLoginProperties()
122  {
123  /** MARGIN TOP */
124  // check if loginbox button margin top is set and not empty
125  if (isset($this->loginboxLoginBtnMarginTop) && (!empty($this->loginboxLoginBtnMarginTop)))
126  { // check last 2 chars of string to see if user missed px afterwards
127  if (substr($this->loginboxLoginBtnMarginTop, -2) != "px")
128  { // append px to value
129  $this->loginboxLoginBtnMarginTop = $this->loginboxLoginBtnMarginTop."px";
130  }
131  // generate css style markupd
132  $this->loginboxLoginBtnMarginMarkup = " style=\"margin-top:$this->loginboxLoginBtnMarginTop;\"";
133  }
134  else
135  { // no markup required
136  $this->loginboxLoginBtnMarginMarkup = '';
137  }
138 
139  /** FORM WIDTH */
140  // check if form width is set and not empty
141  if (isset($this->loginboxWidth) && (!empty($this->loginboxWidth)))
142  {
143  // check last char of string to see if user missed % sign
144  if (substr($this->loginboxWidth, -1) != "%")
145  { // append percent sign
146  $this->loginboxWidth = $this->loginboxWidth."%";
147  }
148  // generate css style markup
149  $this->loginboxWidthMarkup = " style=\"width:$this->loginboxWidth;\"";
150 
151  // check if width is set to 100%
152  if ($this->loginboxWidth === "100%" || $this->loginboxWidth === "100" || ($this->loginboxWidth === "0"))
153  { // default behaviour, no markup needed
154  $this->loginboxWidthMarkup = '';
155  }
156  }
157  else
158  { // no markup required
159  $this->loginboxWidthMarkup = '';
160  }
161 
162  /** FORM CLASS */
163  // check form class
164  if (isset($this->loginboxFormClass) && (!empty($this->loginboxFormClass)))
165  {
166  // generate form class markup
167  $this->loginboxFormClassMarkup = " class=\"$this->loginboxFormClass\"";
168  }
169  else
170  { // no markup required
171  $this->loginboxFormClassMarkup = '';
172  }
173 
174  /** FORM PROCESSING MODE */
175  // check form processing mode (ajax or html)
176  if (isset($this->loginboxProcessingMode) && (!empty($this->loginboxProcessingMode)))
177  { // if form processing is html
178  if ($this->loginboxProcessingMode === "html")
179  { // set form markup html action....
180  $this->loginboxProcessingModeFormMarkup = " action=\"index.html\" ";
181  $this->loginboxProcessingModeSubmitBtnType = "submit";
182  }
183  else
184  { // ajax request - load required js files
185  $this->includeJS();
186  // no additional form markup required
187  $this->loginboxProcessingModeFormMarkup = "";
188  // set submit btn type to prevent 'traditional' submit behavior
189  $this->loginboxProcessingModeSubmitBtnType = "submit";
190  }
191  }
192  }
193 
194  /**
195  * @brief Set properties of greeting and logout button
196  * @brief set properties of greeting and logout button
197  */
198  public function setLogoutProperties()
199  {
200  /** GREETING TEXT MARKUP */
201  // check text type (H1-H6 / globaltext)
202  if (isset($this->loginboxGreetingTextType) && ($this->loginboxGreetingTextType === "GLOBALTEXT"))
203  { // set paragraph as default text type
204  $this->loginboxGreetingTextType = "p";
205  }
206  // check greeting text css class
207  if (isset($this->loginboxGreetingTextClass) && (!empty($this->loginboxGreetingTextClass)))
208  { // only add class if it is not empty
209  $this->loginboxGreetingTextClassMarkup = ' class="'.$this->loginboxGreetingTextClass.'"';
210  }
211  else
212  { // no markup needed
213  $this->loginboxGreetingTextClassMarkup = '';
214  }
215 
216  /** LOGOUT BUTTON MARKUP */
217  if (isset($this->loginboxRedirect) && (!empty($this->loginboxRedirect)))
218  {
219  // prepare redirect url
220  $this->loginboxRedirect = rawurlencode($this->loginboxRedirect);
221  // logout button with redirect url
222  $this->loginboxLogoutBtnMarkup = '<a href="logout" id="logoutBtn" style="margin-top:'.$this->loginboxLoginBtnMarginTop.'" class="'.$this->loginboxLogoutBtnClass.'" target="_self">'.$this->loginboxLogoutBtnText.'</a>';
223  }
224  else
225  { // logout button without redirect url
226  $this->loginboxLogoutBtnMarkup = '<a href="logout" id="logoutBtn" style="margin-top:'.$this->loginboxLoginBtnMarginTop.'" class="'.$this->loginboxLogoutBtnClass.'" target="_self">'.$this->loginboxLogoutBtnText.'</a>';
227  }
228 
229  /** SET GREETING AND LOGOUT BUTTON */
230  // maximum greeting
231  if ($this->loginboxGreeting === "greeting-max")
232  { // do a personal greeting with username
233  $this->loginboxGreetingMarkup = '<'.$this->loginboxGreetingTextType.$this->loginboxGreetingTextClassMarkup.'>'.$this->loginboxGreetingText.' '.$this->currentUser.' <small>'.$this->loginboxGreetingSubtext.'</small></'.$this->loginboxGreetingTextType.'>'.$this->loginboxLogoutBtnMarkup.'';
234  }
235 
236  // minimal greeting
237  if ($this->loginboxGreeting === "greeting-min")
238  { // greeting without name
239  $this->loginboxGreetingMarkup = '<'.$this->loginboxGreetingTextType.$this->loginboxGreetingTextClassMarkup.'>'.$this->loginboxGreetingText.' <small>'.$this->loginboxGreetingSubtext.'</small></'.$this->loginboxGreetingTextType.'>'.$this->loginboxLogoutBtnMarkup.'';
240  }
241 
242  // no greeting, just a logout button
243  if ($this->loginboxGreeting === "greeting-button")
244  { // display logout button
245  $this->loginboxGreetingMarkup = $this->loginboxLogoutBtnMarkup;
246  }
247 
248  // no greeting, silent login mode
249  if ($this->loginboxGreeting === "greeting-none")
250  { // no welcome message - do nothing
251  $this->loginboxGreetingMarkup = '';
252  }
253  }
254 
255  /**
256  * @brief Load required javascript file
257  * @brief include required ajax js file
258  */
259  public function includeJS()
260  {
261  // load notify js
262  echo "<script type=\"text/javascript\" src=\"system/engines/jquery/notify/bootstrap-notify.min.js\"></script>";
263  echo "<link rel=\"stylesheet\" href=\"system/engines/jquery/notify/bootstrap-notify.min.css\">";
264  // load validate js
265  echo "<script type=\"text/javascript\" src=\"system/engines/jquery/jquery.validate.min.js\"></script>";
266  // if current language is german
267  if (\YAWK\language::getCurrentLanguageStatic() == "de-DE")
268  { // load german language file
269  echo "<script type=\"text/javascript\" src=\"system/engines/jquery/messages_de.min.js\"></script>";
270  }
271  // load ajax file
272  echo "<script type=\"text/javascript\" src=\"system/widgets/loginbox/js/loginbox.ajax.min.js\"></script>";
273  }
274 
275  /**
276  * @brief returns the login box html markup
277  * @param object $db database object
278  * @param string $username username, as option
279  * @param string $password password, as option
280  */
281  public function drawLoginBox($db, $username, $password)
282  {
283  // first of all: get settings for this loginbox
284  $this->setLoginProperties();
285 
286  // bootstrap layout markup
287  echo "<div class=\"col-md-12\"".$this->loginboxLoginBtnMarginMarkup.">";
288 
289  // draw heading above loginbox
290  echo "<div id=\"heading\">";
291  echo $this->getHeading($this->loginboxHeading, $this->loginboxSubtext);
292  echo "</div>";
293 
294  // draw loginbox
295  echo"<form name=\"login\" ".$this->loginboxProcessingModeFormMarkup.$this->loginboxFormClassMarkup." id=\"loginForm\" role=\"form\" method=\"POST\"".$this->loginboxWidthMarkup.">
296  <input type=\"text\" id=\"user\" name=\"user\" value=\"".$username."\" class=\"form-control\" placeholder=\"Benutzername\">
297  <input type=\"password\" id=\"password\" name=\"password\" value=\"".$password."\" class=\"form-control\" placeholder=\"Passwort\">
298  <input type=\"hidden\" name=\"login\" value=\"login\">
299  <input type=\"hidden\" id=\"loginboxRedirect\" name=\"loginboxRedirect\" value=\"".$this->loginboxRedirect."\">
300  <input type=\"hidden\" id=\"loginboxRedirectTime\" name=\"loginboxRedirectTime\" value=\"".$this->loginboxRedirectTime."\">
301  <input type=\"hidden\" id=\"loginboxLogoutBtnText\" name=\"logoutBtnText\" value=\"".$this->loginboxLogoutBtnText."\">
302  <input type=\"hidden\" id=\"loginboxLogoutBtnClass\" name=\"logoutBtnClass\" value=\"".$this->loginboxLogoutBtnClass."\">
303  <input type=\"hidden\" id=\"loginboxGreeting\" name=\"loginboxGreeting\" value=\"".$this->loginboxGreeting."\">
304  <input type=\"hidden\" id=\"loginboxGreetingText\" name=\"loginboxGreetingText\" value=\"".$this->loginboxGreetingText."\">
305  <input type=\"hidden\" id=\"loginboxGreetingTextType\" name=\"loginboxGreetingTextType\" value=\"".$this->loginboxGreetingTextType."\">
306  <input type=\"hidden\" id=\"loginboxGreetingTextClass\" name=\"loginboxGreetingTextClass\" value=\"".$this->loginboxGreetingTextClass."\">
307  <input type=\"hidden\" id=\"loginboxGreetingSubtext\" name=\"loginboxGreetingSubtext\" value=\"".$this->loginboxGreetingSubtext."\">
308  <input type=\"hidden\" id=\"loginboxGreetingShowName\" name=\"loginboxGreetingShowName\" value=\"".$this->loginboxGreetingShowName."\">
309  <input type=\"".$this->loginboxProcessingModeSubmitBtnType."\" name=\"submit\" id=\"submit\" class=\"$this->loginboxLoginBtnClass\" value=\"$this->loginboxLoginBtnText\"".$this->loginboxLoginBtnMarginMarkup.">
310  </form>
311  <div id=\"thankYouMessage\"></div>
312  </div>";
313  }
314 
315  /**
316  * @brief returns the logout btn html markup
317  */
318  public function drawLogoutButton($db)
319  {
320  // get logout button settings
321  $this->setLogoutProperties();
322 
323  echo "
324  <div class=\"col-md-12\">
325  ".$this->loginboxGreetingMarkup."
326  </div>";
327  }
328  }
329 }
330 ?>
__construct($db)
Load all widget settings from database and fill object.
Definition: loginbox.php:81
init($db)
Init loginbox widget and call a function for demo purpose.
Definition: loginbox.php:95
includeJS()
Load required javascript file.
Definition: loginbox.php:258
setLogoutProperties()
Set properties of greeting and logout button.
Definition: loginbox.php:197
drawLoginBox($db, $username, $password)
returns the login box html markup
Definition: loginbox.php:280
setLoginProperties()
Prepare Loginbox form settings.
Definition: loginbox.php:120
drawLogoutButton($db)
returns the logout btn html markup
Definition: loginbox.php:317
static getCurrentLanguageStatic()
returns the currently set backend language, but is static callable
Definition: language.php:146
static isAnybodyThere($db)
check, if a session username is set and if user is logged in
Definition: user.php:361
Widgets are small, useful tools that you can include everywhere in your website.
Definition: widget.php:22
getWidgetSettingsArray($db)
Get widget settings and return it as array.
Definition: widget.php:69
getHeading($heading, $subtext)
Get widget heading and subtext, return headline.
Definition: widget.php:669
This class serves methods to create backup from files.
Definition: AdminLTE.php:2
if(isset($_POST['save'])) $settings
$value