Umfragen
config.class.php
Go to the documentation of this file.
1 <?php
2 /*
3  * config.class.php
4  *
5  * Copyright 2013 Johannes ter Haak <jojo@jojo-42>
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 Street, Fifth Floor, Boston,
20  * MA 02110-1301, USA.
21  *
22  *
23  */
24 
29 require_once("db.class.php");
30 
34 class config{
35 
36  private $data = array();
37 
43  public function __construct($db){
44  if( ! $db instanceof db ){
45  throw new exception("No database object");
46  }else{
47  $this->db = $db;
48  }
49  }
50 
56  public function define_constants(){
57  foreach( array_keys($this->data) as $name ){
58  if( strtoupper($name) === $name ){
59  // GLOBALS
60  if( $name == "GROUPS_WHITELIST" ){
61  $GLOBALS["groups_whitelist"] = $this->data["GROUPS_WHITELIST"];
62  }else if( $name == "EMAIL_GROUPS" ){
63  $GLOBALS["email_groups"] = $this->data["EMAIL_GROUPS"];
64  // STRINGS AND INT FLOAT
65  }else if( (!defined($name)) AND ((is_string($this->data[$name])) OR (is_numeric($this->data[$name])) ) ){
66  define( $name , $this->data[$name] );
67  // ARRAYS
68  }else if( (!defined($name)) AND (is_array($this->data[$name])) ){
69  define( $name , implode(",",$this->data[$name]) );
70  // BOOLS
71  }else if( (!defined($name)) AND (is_bool($this->data[$name])) ){
72  if($this->data[$name] === true ){
73  define( $name , "true" );
74  }else{
75  define( $name , "false" );
76  }
77  }
78  } // end if constant
79  } // end foreach data
80  }
81 
86  public function load_defaults($override=false){
87  if( !isset($this->data["APP_ROOT_URL"]) ){
88  $this->data["APP_ROOT_URL"] = "";
89  }
90  }
91 
95  public function load(){
96  $data = $this->db->get_config_array();
97  if( isset($data) ){
98  foreach( $data as $row ){
99  if( (isset($row["name"])) AND (isset($row["value"])) ){
100  $this->data[$row["name"]] = unserialize($row["value"]);
101  } // end issset name and value
102  } // end foreach row
103  $this->load_defaults();
104  $this->define_constants();
105  }else{
106 
107  }
108  }
109 
114  public function save($name){
115  if( isset($this->data[$name]) ){
116  $data = serialize($this->data[$name]);
117  $this->db->update_config_field($name,$data);
118  }
119  }
120 
125  public function save_all(){
126  foreach( array_keys($this->data) as $name ){
127  $this->save($name);
128  }
129  return true;
130  }
131 
138  public function set($name,$value){
139  if( $name == "APP_ROOT_URL"){
140  $f = filter_var($value,FILTER_VALIDATE_URL);
141  if( $f !== false){
142  if( (substr($f,0,7) === "http://") OR (substr($f,0,8) === "https://") ){
143  $this->data["APP_ROOT_URL"] = trim($f,"/");
144  }else{
145  return "Bitte http:// oder https:// in dr URL des Feldes APP_ROOT_URL angeben.";
146  }
147  }else{
148  return "http-Adresse für APP_ROOT_URL ist ungültig!";
149  }
150 
151  }else if( $name == "ANALYTICS_HTML_INCLUDE_FILE"){
152  if( ($value == "") OR (is_readable($value)) ){
153  $this->data["ANALYTICS_HTML_INCLUDE_FILE"] = $value;
154  }else{
155  return "Datei aus ANALYTICS_HTML_INCLUDE_FILE nicht lesbar (oder existiert nicht)";
156  }
157 
158  }else if( $name == "TEACHER_GROUP"){
159  $new_gr = array();
160  if( is_array($value) ){
161  foreach( $value as $gr ){
162  if( is_numeric($gr) ){
163  $new_gr[] = intval($gr);
164  }
165  }
166  $this->data["TEACHER_GROUP"] = $new_gr;
167  }else{
168  return true;
169  }
170  }
171 
172  else if( $name == "GROUPS_WHITELIST"){
173  $new_gr = array();
174  if( is_array($value) ){
175  foreach( $value as $gr ){
176  if( is_string($gr) ){
177  $new_gr[] = $gr;
178  }
179  }
180  $this->data["GROUPS_WHITELIST"] = $new_gr;
181  }else{
182  return true;
183  }
184  }
185 
186  /*
187  * email
188  * */
189  else if( $name == "EMAIL_ENABLE"){
190  if( $value === true ){
191  $this->data["EMAIL_ENABLE"] = $value;
192  }else{
193  $this->data["EMAIL_ENABLE"] = false;
194  }
195 
196  }else if( $name == "EMAIL_METHOD"){
197  if( in_array($value,array("group","LDAP")) ){
198  $this->data[$name] = $value;
199  }else{
200  return "Wert für EMAIL_METHOD ist ungültig.";
201  }
202 
203  }else if( $name == "EMAIL_SEND_METHOD"){
204  if( in_array($value,array("smtp","mail","sendmail")) ){
205  $this->data[$name] = $value;
206  }else{
207  return "Wert für EMAIL_SEND_METHOD ist ungültig.";
208  }
209 
210  }else if( $name == "EMAIL_AUTH_ENCRYPTION"){
211  if( in_array($value,array("ssl")) ){
212  $this->data[$name] = $value;
213  }else{
214  return "Wert für EMAIL_AUTH_ENCRYPTION ist ungültig.";
215  }
216 
217  }else if( $name == "EMAIL_SMTP_SERVER"){
218  $this->data[$name] = $value;
219  }
220 
221  else if( $name == "EMAIL_SMTP_PORT"){
222  if( is_numeric($value) ){
223  $this->data[$name] = intval( $value );
224  }else{
225  return "EMAIL_SMTP_PORT muss eine Ganzzahl sein.";
226  }
227  }
228 
229  else if( $name == "EMAIL_SMTP_USERNAME"){
230  $this->data[$name] = $value;
231 
232  }else if( $name == "EMAIL_SMTP_PASSWORD"){
233  $this->data[$name] = $value;
234 
235  }else if( $name == "EMAIL_ADDRESS"){
236  $f = filter_var($value,FILTER_VALIDATE_EMAIL);
237  if( $f !== false ){
238  $this->data[$name] = $f;
239  }else{
240  return "E-mail Adresse aus EMAIL_ADDRESS ist keine gültige E-mail Adresse.";
241  }
242 
243  }else if( $name == "EMAIL_NAME"){
244  $this->data[$name] = $value;
245  }
246 
247  else if( $name == "EMAIL_GROUPS"){
248  if( is_array($value) ){
249  $ok = array();
250  foreach( array_keys($value) as $gr ){
251  if( (is_numeric($gr)) AND (filter_var($value[$gr],FILTER_VALIDATE_EMAIL)) ){
252  $ok["$gr"] = "{$value[$gr]}";
253  }
254  }
255  foreach( $this->get("TEACHER_GROUP") as $t ){
256  if( !isset($ok["{$t}"]) ){
257  $ok["{$t}"] = "";
258  }
259  }
260  $this->data[$name] = $ok;
261  } // end is array
262  }
263 
264  // if no error return true
265  return true;
266  }
267 
273  public function get($name){
274  if( $name == "APP_ROOT_URL"){
275  if( isset($this->data["APP_ROOT_URL"]) ){
276  return $this->data["APP_ROOT_URL"];
277  }else{
278  return "";
279  }
280 
281  }else if( $name == "ANALYTICS_HTML_INCLUDE_FILE"){
282  if( isset($this->data[$name]) ){
283  return $this->data[$name];
284  }else{
285  return "";
286  }
287 
288  }else if( $name == "TEACHER_GROUP"){
289  if( isset($this->data[$name]) ){
290  return $this->data[$name];
291  }else{
292  return array();
293  }
294  }
295 
296  else if( $name == "GROUPS_WHITELIST"){
297  if( isset($this->data[$name]) ){
298  return $this->data[$name];
299  }else{
300  return array();
301  }
302  }
303 
304  else if( $name == "EMAIL_ENABLE"){
305  if( isset($this->data[$name]) ){
306  return $this->data[$name];
307  }else{
308  return false;
309  }
310 
311  }else if( $name == "EMAIL_METHOD"){
312  if( isset($this->data[$name]) ){
313  return $this->data[$name];
314  }else{
315  return "group";
316  }
317 
318  }else if( $name == "EMAIL_SEND_METHOD"){
319  if( isset($this->data[$name]) ){
320  return $this->data[$name];
321  }else{
322  return "smtp";
323  }
324 
325  }else if( $name == "EMAIL_AUTH_ENCRYPTION"){
326  if( isset($this->data[$name]) ){
327  return $this->data[$name];
328  }else{
329  return "ssl";
330  }
331 
332  }else if( $name == "EMAIL_SMTP_SERVER"){
333  if( isset($this->data[$name]) ){
334  return $this->data[$name];
335  }else{
336  return "";
337  }
338 
339  }else if( $name == "EMAIL_SMTP_PORT"){
340  if( isset($this->data[$name]) ){
341  return $this->data[$name];
342  }else{
343  return 465;
344  }
345 
346  }else if( $name == "EMAIL_SMTP_USERNAME"){
347  if( isset($this->data[$name]) ){
348  return $this->data[$name];
349  }else{
350  return "";
351  }
352 
353  }else if( $name == "EMAIL_SMTP_PASSWORD"){
354  if( isset($this->data[$name]) ){
355  return $this->data[$name];
356  }else{
357  return "";
358  }
359 
360  }else if( $name == "EMAIL_ADDRESS"){
361  if( isset($this->data[$name]) ){
362  return $this->data[$name];
363  }else{
364  return "";
365  }
366 
367  }else if( $name == "EMAIL_NAME"){
368  if( isset($this->data[$name]) ){
369  return $this->data[$name];
370  }else{
371  return "";
372  }
373 
374  }else if( $name == "EMAIL_GROUPS"){
375  if( isset($this->data[$name]) ){
376  return $this->data[$name];
377  }else{
378  return array();
379  }
380  }
381 
382 
383  }
384 
388  public function display_app_root_url_edit(){
389  $value = htmlspecialchars($this->get("APP_ROOT_URL") , ENT_QUOTES , "UTF-8",true);
390  echo "<input type='text' name='config_app_root_url' value='$value' />";
391  }
392 
393  public function handle_app_root_url_edit(){
394  if( isset($_POST["config_app_root_url"]) ){
395  return $this->set("APP_ROOT_URL",$_POST["config_app_root_url"]);
396  }
397  }
398 
402  public function display_analytics_file_edit(){
403  $value = htmlspecialchars($this->get("ANALYTICS_HTML_INCLUDE_FILE") , ENT_QUOTES , "UTF-8",true);
404  echo "<input type='text' name='config_analytics_file' value='$value' />";
405 
406  }
407 
408  public function handle_analytics_file_edit(){
409  if( isset($_POST["config_analytics_file"]) ){
410  return $this->set("ANALYTICS_HTML_INCLUDE_FILE",$_POST["config_analytics_file"]);
411  }
412  }
413 
418  public function display_teacher_group_edit( array $list){
419  $grps = $this->get("TEACHER_GROUP");
420  echo "<select name='config_teacher_group[]' size='5' multiple='multiple'>";
421  foreach( $list as $gr){
422  if( is_array($gr) ){
423  if( substr($gr[0],0,2) != "p_" ){
424  if( in_array($gr[1],$grps) ){
425  echo "<option value='{$gr[1]}' selected='selected'>{$gr[0]}</option>";
426  }else{
427  echo "<option value='{$gr[1]}'>{$gr[0]}</option>";
428  }
429  } // end exclude projects
430  } // end is array
431  } // end foreach group
432  echo "</select>";
433  }
434 
435  public function handle_teacher_group_edit(array $list){
436  $new_grps = array();
437  if( (isset($_POST["config_teacher_group"])) AND (is_array($_POST["config_teacher_group"])) ){
438  foreach( $list as $gr ){
439  if( in_array($gr[1],$_POST["config_teacher_group"]) ){
440  $new_grps[] = $gr[1];
441  }
442  }
443  return $this->set("TEACHER_GROUP",$new_grps);
444  }
445  }
446 
451  public function display_groups_whitelist_edit( array $list){
452  $grps = $this->get("GROUPS_WHITELIST");
453  echo "<select name='config_groups_whitelist[]' size='5' multiple='multiple'>";
454  foreach( $list as $gr){
455  if( is_array($gr) ){
456  if( substr($gr[0],0,2) != "p_" ){
457  if( in_array($gr[0],$grps) ){
458  echo "<option value='{$gr[0]}' selected='selected'>{$gr[0]}</option>";
459  }else{
460  echo "<option value='{$gr[0]}'>{$gr[0]}</option>";
461  }
462  } // end no projects
463  } // end if array
464  } // end foreach group available
465  echo "</select>";
466  }
467 
468  public function handle_groups_whitelist_edit(array $list){
469  $new_grps = array();
470  if( (isset($_POST["config_groups_whitelist"])) AND (is_array($_POST["config_groups_whitelist"])) ){
471  foreach( $list as $gr ){
472  if( in_array($gr[0],$_POST["config_groups_whitelist"]) ){
473  $new_grps[] = $gr[0];
474  }
475  }
476  return $this->set("GROUPS_WHITELIST",$new_grps);
477  }
478  }
479 
483  public function display_enable_email_edit(){
484  $val = $this->get("EMAIL_ENABLE");
485  echo "<select name='config_email_enable'>";
486  if( $val === true ){
487  echo "<option value='false'>Aus</option>";
488  echo "<option value='true' selected='selected'>An</option>";
489  }else{
490  echo "<option value='false' selected='selected'>Aus</option>";
491  echo "<option value='true'>An</option>";
492  }
493  echo "</select>";
494  }
495 
496  public function handle_enable_email_edit(){
497  if( (isset($_POST["config_email_enable"])) AND ( in_array($_POST["config_email_enable"],array("true","false")) ) ){
498  if( $_POST["config_email_enable"] == "true"){
499  return $this->set("EMAIL_ENABLE", true );
500  } else{
501  return $this->set("EMAIL_ENABLE", false );
502  }
503  }
504  }
505 
509  public function display_email_method_edit(){
510  $val = $this->get("EMAIL_METHOD");
511  $methods = array("group","LDAP");
512  echo "<select name='config_email_method'>";
513  foreach( $methods as $m)
514  if( $val === $m ){
515  echo "<option value='$m' selected='selected'>$m</option>";
516  }else{
517  echo "<option value='$m'>$m</option>";
518  }
519  echo "</select>";
520  }
521 
522  public function handle_email_method_edit(){
523  $methods = array("group","LDAP");
524  if( (isset($_POST["config_email_method"])) ){
525  if( $_POST["config_email_method"] == "LDAP" ){
526  return "Das Auslesen von Email Adressen aus dem LDAP verzeichnis wird noch nicht unterstützt. (EMAIL_METHOD)";
527  }
528  return $this->set("EMAIL_METHOD", $_POST["config_email_method"] );
529  }
530  }
531 
536  $val = $this->get("EMAIL_SEND_METHOD");
537  $methods = array("smtp","mail","sendmail");
538  echo "<select name='config_email_send_method'>";
539  foreach( $methods as $m)
540  if( $val === $m ){
541  echo "<option value='$m' selected='selected'>$m</option>";
542  }else{
543  echo "<option value='$m'>$m</option>";
544  }
545  echo "</select>";
546  }
547 
548  public function handle_email_send_method_edit(){
549  $methods = array("smtp","mail","sendmail");
550  if( (isset($_POST["config_email_send_method"])) ){
551  if( $_POST["config_email_send_method"] != "smtp" ){
552  return "Momentan wird noch keine andere Methode als 'smtp' unterstützt (EMAIL_SEND_METHOD)";
553  }
554  return $this->set("EMAIL_SEND_METHOD", $_POST["config_email_send_method"] );
555  }
556  }
557 
562  $val = $this->get("EMAIL_AUTH_ENCRYPTION");
563  $methods = array("ssl");
564  echo "<select name='config_email_auth_encryption'>";
565  foreach( $methods as $m)
566  if( $val === $m ){
567  echo "<option value='$m' selected='selected'>$m</option>";
568  }else{
569  echo "<option value='$m'>$m</option>";
570  }
571  echo "</select>";
572  }
573 
575  $methods = array("ssl");
576  if( (isset($_POST["config_email_auth_encryption"])) ){
577  if( $_POST["config_email_auth_encryption"] != "ssl" ){
578  return "Momentan wird noch keine andere Methode als 'ssl' unterstützt (EMAIL_AUTH_ENCRYPTION)";
579  }
580  return $this->set("EMAIL_AUTH_ENCRYPTION", $_POST["config_email_auth_encryption"] );
581  }
582  }
583 
588  $value = htmlspecialchars($this->get("EMAIL_SMTP_SERVER") , ENT_QUOTES , "UTF-8",true);
589  echo "<input type='text' name='config_email_smtp_server' value='$value' />";
590 
591  }
592 
593  public function handle_email_smtp_server_edit(){
594  if( isset($_POST["config_email_smtp_server"]) ){
595  return $this->set("EMAIL_SMTP_SERVER",$_POST["config_email_smtp_server"]);
596  }
597  }
598 
602  public function display_email_smtp_port_edit(){
603  $value = htmlspecialchars($this->get("EMAIL_SMTP_PORT") , ENT_QUOTES , "UTF-8",true);
604  echo "<input type='text' name='config_email_smtp_port' value='$value' />";
605 
606  }
607 
608  public function handle_email_smtp_port_edit(){
609  if( isset($_POST["config_email_smtp_port"]) ){
610  return $this->set("EMAIL_SMTP_PORT",$_POST["config_email_smtp_port"]);
611  }
612  }
613 
618  $value = htmlspecialchars($this->get("EMAIL_SMTP_USERNAME") , ENT_QUOTES , "UTF-8",true);
619  echo "<input type='text' name='config_email_smtp_username' value='$value' />";
620 
621  }
622 
624  if( isset($_POST["config_email_smtp_username"]) ){
625  return $this->set("EMAIL_SMTP_USERNAME",$_POST["config_email_smtp_username"]);
626  }
627  }
628 
633  $value = htmlspecialchars($this->get("EMAIL_SMTP_PASSWORD") , ENT_QUOTES , "UTF-8",true);
634  if( $value != "" ){
635  $h = "<span style='font-size:80%'>(vorhanden)</span>";
636  }else{
637  $h = "";
638  }
639  echo "<input type='password' name='config_email_smtp_password' /> $h";
640 
641  }
642 
644  if( isset($_POST["config_email_smtp_password"]) ){
645  if( $_POST["config_email_smtp_password"] != "" ){
646  return $this->set("EMAIL_SMTP_PASSWORD",$_POST["config_email_smtp_password"]);
647  }else{
648  return true;
649  }
650  }
651  }
652 
656  public function display_email_address_edit(){
657  $value = htmlspecialchars($this->get("EMAIL_ADDRESS") , ENT_QUOTES , "UTF-8",true);
658  echo "<input type='text' name='config_email_address' value='$value' />";
659 
660  }
661 
662  public function handle_email_address_edit(){
663  if( isset($_POST["config_email_address"]) ){
664  return $this->set("EMAIL_ADDRESS",$_POST["config_email_address"]);
665  }
666  }
667 
671  public function display_email_name_edit(){
672  $value = htmlspecialchars($this->get("EMAIL_NAME") , ENT_QUOTES , "UTF-8",true);
673  echo "<input type='text' name='config_email_name' value='$value' />";
674 
675  }
676 
677  public function handle_email_name_edit(){
678  if( isset($_POST["config_email_name"]) ){
679  return $this->set("EMAIL_NAME",$_POST["config_email_name"]);
680  }
681  }
682 
687  public function display_email_groups_edit( array $grouplist){
688  $teachers = $this->get("TEACHER_GROUP");
689  $vals = $this->get("EMAIL_GROUPS");
690  $map = array();
691  foreach($grouplist as $g ){
692  $map[ $g[1] ] = $g[0];
693  }
694  foreach( $teachers as $t ){
695  if( isset($vals[$t]) ){
696  $val = $vals[$t];
697  }else{
698  $val = "";
699  }
700  echo "<label for='config_email_group_{$t}'>{$map[$t]}:</label><br/>";
701  echo "<input type='text' name='config_email_group_{$t}' id='config_email_group_{$t}' value='{$val}' style='margin-left:.8em'/><br/>";
702  }
703  }
704 
705  public function handle_email_groups_edit(array $grouplist){
706  $teachers = $this->get("TEACHER_GROUP");
707  $map = array();
708  foreach($grouplist as $g ){
709  $map[ $g[1] ] = $g[0];
710  }
711  $val = array();
712  foreach( $teachers as $t ){
713  if( isset($_POST["config_email_group_{$t}"]) AND ($_POST["config_email_group_{$t}"] != "") ){
714  $val[ "$t" ] = $_POST["config_email_group_{$t}"];
715  }
716  }
717 
718  return $this->set("EMAIL_GROUPS", $val);
719  }
720 
721 
722 }
723 // ! no ending newLine
724 ?>