CLEANUP: used tidy to clean up the HTML
[e-DoKo.git] / include / preferences.php
1 <?php
2 /* make sure that we are not called from outside the scripts,
3  * use a variable defined in config.php to check this
4  */
5 if(!isset($HOST))
6   exit;
7
8 $name  = $_SESSION["name"];
9 $email = DB_get_email('name',$name);
10 $myid = DB_get_userid('email',$email);
11 if(!$myid)
12   return;
13
14 /* track what got changed */
15 $changed_notify    = 0;
16 $changed_password  = 0;
17 $changed_cards     = 0;
18 $changed_timezone  = 0;
19 $changed_autosetup = 0;
20
21 output_status();
22 display_user_menu();
23
24 /* get old infos */
25 $PREF = DB_get_PREF($myid);
26 $timezone =  DB_get_user_timezone($myid);
27
28 DB_update_user_timestamp($myid);
29
30 /* does the user want to change some preferences? */
31 if(myisset("timezone"))
32   {
33     $newtimezone=$_REQUEST['timezone'];
34     if($newtimezone != $timezone)
35       {
36         DB_query("UPDATE User SET timezone=".DB_quote_smart($newtimezone).
37                  " WHERE id=".DB_quote_smart($myid));
38         $changed_timezone = 1;
39       }
40   }
41
42 if(myisset("cards"))
43   {
44     $cards=$_REQUEST['cards'];
45     if($cards != $PREF['cardset'])
46       {
47         /* check if we already have an entry for the user, if so change it, if not create new one */
48         $result = DB_query("SELECT * from User_Prefs".
49                            " WHERE user_id='$myid' AND pref_key='cardset'" );
50         if( DB_fetch_array($result))
51           $result = DB_query("UPDATE User_Prefs SET value=".DB_quote_smart($cards).
52                              " WHERE user_id='$myid' AND pref_key='cardset'" );
53         else
54           $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','cardset',".
55                              DB_quote_smart($cards).")");
56         $changed_cards = 1;
57       }
58   }
59
60 if(myisset("notify"))
61   {
62     $notify=$_REQUEST['notify'];
63     if($notify != $PREF['email'])
64       {
65         /* check if we already have an entry for the user, if so change it, if not create new one */
66         $result = DB_query("SELECT * from User_Prefs".
67                            " WHERE user_id='$myid' AND pref_key='email'" );
68         if( DB_fetch_array($result))
69           $result = DB_query("UPDATE User_Prefs SET value=".DB_quote_smart($notify).
70                              " WHERE user_id='$myid' AND pref_key='email'" );
71         else
72           $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','email',".
73                              DB_quote_smart($notify).")");
74         $changed_notify=1;
75       }
76   }
77
78 if(myisset("autosetup"))
79   {
80     $autosetup = $_REQUEST['autosetup'];
81     if($autosetup != $PREF['autosetup'])
82       {
83         /* check if we already have an entry for the user, if so change it, if not create new one */
84         $result = DB_query("SELECT * from User_Prefs".
85                            " WHERE user_id='$myid' AND pref_key='autosetup'" );
86         if( DB_fetch_array($result))
87           $result = DB_query("UPDATE User_Prefs SET value=".DB_quote_smart($autosetup).
88                              " WHERE user_id='$myid' AND pref_key='autosetup'" );
89         else
90           $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','autosetup',".
91                              DB_quote_smart($autosetup).")");
92         $changed_autosetup=1;
93       }
94   }
95
96
97 if(myisset("password0") &&  $_REQUEST["password0"]!="" )
98   {
99     $changed_password = 1;
100
101     /* check if old password matches */
102     $oldpasswd = md5($_REQUEST["password0"]);
103     $password  = DB_get_passwd_by_userid($myid);
104     if(!( ($password == $oldpasswd) || DB_check_recovery_passwords($oldpasswd,$email) ))
105       $changed_password = -1;
106
107     /* check if new passwords are types the same twice */
108     if($_REQUEST["password1"] != $_REQUEST["password2"] )
109       $changed_password = -2;
110
111     if($changed_password==1)
112       {
113         DB_query("UPDATE User SET password='".md5($_REQUEST["password1"]).
114                  "' WHERE id=".DB_quote_smart($myid));
115       }
116     /* error output below */
117   }
118
119 /* get infos again in case they have changed */
120 $PREF     = DB_get_PREF($myid);
121 $timezone = DB_get_user_timezone($myid);
122
123 /* output settings */
124
125 echo "<div class=\"user\">\n";
126 echo "  <form action=\"index.php?action=prefs\" method=\"post\">\n";
127 echo "  <h2>Your settings are</h2>\n";
128 echo "    <table>\n";
129 echo "    <tr><td>Email:                 </td><td> $email    </td></tr>\n";
130 echo "    <tr><td>Timezone:              </td><td>";
131 output_select_timezone("timezone",$timezone);
132 if($changed_timezone) echo "changed";
133 echo "</td></tr>\n";
134 echo "    <tr><td>Notification:          </td><td>";
135
136 echo "  <select id=\"notify\" name=\"notify\" size=\"1\">\n";
137       if($PREF['email']=="emailaddict")
138         {
139           echo "   <option value=\"emailaddict\" selected=\"selected\">less emails</option>\n";
140           echo "   <option value=\"emailnonaddict\">lots of emails</option>\n";
141         }
142       else
143         {
144           echo "   <option value=\"emailaddict\">less email</option>\n";
145           echo "   <option value=\"emailnonaddict\" selected=\"selected\">lots of email</option>\n";
146         }
147   echo "  </select>\n";
148 if($changed_notify) echo "changed";
149 echo " </td></tr>\n";
150 echo "    <tr><td>Autosetup:          </td><td>";
151
152 echo "  <select id=\"autosetup\" name=\"autosetup\" size=\"1\">\n";
153       if($PREF['autosetup']=="yes")
154         {
155           echo "   <option value=\"yes\" selected=\"selected\">accept every game</option>\n";
156           echo "   <option value=\"no\">ask for games</option>\n";
157         }
158       else
159         {
160           echo "   <option value=\"yes\">accept every game</option>\n";
161           echo "   <option value=\"no\" selected=\"selected\">ask for games</option>\n";
162         }
163   echo "  </select>\n";
164 if($changed_autosetup) echo "changed";
165 echo " </td></tr>\n";
166 echo "    <tr><td>Card set:              </td><td>";
167
168 echo "  <select id=\"cards\" name=\"cards\" size=\"1\">\n";
169       if($PREF['cardset']=="altenburg")
170         {
171           echo "   <option value=\"altenburg\" selected=\"selected\">German cards</option>\n";
172           echo "   <option value=\"english\">English cards</option>\n";
173         }
174       else
175         {
176           echo "   <option value=\"altenburg\">German cards</option>\n";
177           echo "   <option value=\"english\" selected=\"selected\">English cards</option>\n";
178         }
179   echo "  </select>\n";
180 if($changed_cards) echo "changed";
181 echo " </td></tr>\n";
182 echo "    <tr><td>Password(old):         </td><td>",
183   "<input type=\"password\" id=\"password0\" name=\"password0\" size=\"20\" maxlength=\"30\" />";
184 switch($changed_password)
185   {
186   case '-2':
187     echo "The new passwords don't match.";
188     break;
189   case '-1':
190     echo "The old password is not correct.";
191     break;
192   case '1':
193     echo "changed";
194     break;
195   }
196 echo  " </td></tr>\n";
197 echo "    <tr><td>Password(new):         </td><td>",
198   "<input type=\"password\" id=\"password1\" name=\"password1\" size=\"20\" maxlength=\"30\" />",
199   " </td></tr>\n";
200 echo "    <tr><td>Password(new, retype): </td><td>",
201   "<input type=\"password\" id=\"password2\" name=\"password2\" size=\"20\" maxlength=\"30\" />",
202   " </td></tr>\n";
203 echo "    <tr><td><input type=\"submit\" class=\"submitbutton\" name=\"passwd\" value=\"set\" /></td>",
204   "<td></td></tr>\n";
205 echo "    </table>\n";
206 echo "  </form>\n";
207 echo "</div>\n";
208
209 output_footer();
210 DB_close();
211 exit();
212
213 ?>