Caching of themes (fully supported) and admin menu (experimental!) now configurable
[mailer.git] / inc / modules / admin / admin-inc.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 08/31/2003 *
4  * ===============                              Last change: 11/23/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : admin-inc.php                                    *
8  * -------------------------------------------------------------------- *
9  * Short description : Administrative related functions                 *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Fuer die Administration benoetigte Funktionen    *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 //
41 function REGISTER_ADMIN ($user, $md5, $email=WEBMASTER)
42 {
43         $ret = "failed";
44         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
45          array($user), __FILE__, __LINE__);
46         if (SQL_NUMROWS($result) == 0) {
47                 // Ok, let's create the admin login
48                 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_admins (login, password, email) VALUES('%s', '%s', '%s')",
49                  array($user, $md5, $email), __FILE__, __LINE__);
50                 $ret = "done";
51         } else {
52                 // Free memory
53                 SQL_FREERESULT($result);
54
55                 // Login does already exist
56                 $ret = "already";
57         }
58         return $ret;
59 }
60 // Only be executed on login procedure!
61 function CHECK_ADMIN_LOGIN ($admin_login, $password)
62 {
63         global $cacheArray, $_CONFIG, $cacheInstance;
64
65         // Init variables
66         $ret = "404";
67         $data = array();
68
69         // Is the cache valid?
70         if (!empty($cacheArray['admins']['aid'][$admin_login])) {
71                 // Get password from cache
72                 $data['password'] = $cacheArray['admins']['password'][$admin_login];
73                 $ret = "pass";
74                 $_CONFIG['cache_hits']++;
75
76                 // Include more admins data?
77                 if (GET_EXT_VERSION("admins") >= "0.7.0") {
78                         // Load them here
79                         $data['login_failtures'] = $cacheArray['admins']['login_failtures'][$admin_login];
80                         $data['last_failture']   = $cacheArray['admins']['last_failture'][$admin_login];
81                 } // END - if
82         } elseif (GET_EXT_VERSION("cache") == "") {
83                 $ADD = "";
84                 if (GET_EXT_VERSION("admins") >= "0.7.0") {
85                         // Load them here
86                         $ADD = ", login_failtures, UNIX_TIMESTAMP(last_failture) AS last_failture";
87                 } // END - if
88
89                 // Get password from DB
90                 $result = SQL_QUERY_ESC("SELECT password".$ADD." FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
91                  array($admin_login), __FILE__, __LINE__);
92                 if (SQL_NUMROWS($result) == 1) {
93                         // Login password found
94                         $ret = "pass";
95
96                         // Fetch data
97                         $data = SQL_FETCHARRAY($result);
98                 } // END - if
99
100                 // Free result
101                 SQL_FREERESULT($result);
102         }
103
104         //* DEBUG: */ echo "*".$data['password']."/".md5($password)."/".$ret."<br />";
105         if ((isset($data['password'])) && (strlen($data['password']) == 32) && ($data['password'] == md5($password))) {
106                 // Generate new hash
107                 $data['password'] = generateHash($password);
108
109                 // Is the sql_patches not installed, than we cannot have a valid hashed password here!
110                 if (($ret == "pass") && ((GET_EXT_VERSION("sql_patches") < "0.3.6") || (GET_EXT_VERSION("sql_patches") == ""))) $ret = "done";
111         } elseif ((GET_EXT_VERSION("sql_patches") < "0.3.6") || (GET_EXT_VERSION("sql_patches") == "")) {
112                 // Old hashing way
113                 return $ret;
114         } elseif (!isset($data['password'])) {
115                 // Password not found, so no valid login!
116                 return $ret;
117         }
118
119         // Generate salt of password
120         define('__SALT', substr($data['password'], 0, -40));
121         $salt = __SALT;
122
123         // Check if password is same
124         //* DEBUG: */ echo "*".$ret.",".$data['password'].",".$password.",".$salt."*<br >\n";
125         if (($ret == "pass") && ($data['password'] == generateHash($password, $salt)) && (!empty($salt))) {
126                 // Re-hash the plain passord with new random salt
127                 $data['password'] = generateHash($password);
128
129                 // Do we have 0.7.0 of admins or later?
130                 // Remmeber login failtures if available
131                 if (GET_EXT_VERSION("admins") >= "0.7.0") {
132                         // Store it in session
133                         set_session('mxchange_admin_failtures', $data['login_failtures']);
134                         set_session('mxchange_admin_last_fail', $data['last_failture']);
135                 } // END - if
136
137                 // Update password
138                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET password='%s',login_failtures=0,last_failture='0000-00-00 00:00:00' WHERE login='%s' LIMIT 1",
139                  array($data['password'], $admin_login), __FILE__, __LINE__);
140
141                 // Rebuild cache
142                 REBUILD_CACHE("admins", "admin");
143
144                 // Login has failed by default... ;-)
145                 $ret = "failed";
146
147                 // Password matches so login here
148                 if (LOGIN_ADMIN($admin_login, $data['password'])) {
149                         // All done now
150                         $ret = "done";
151                 } // END - if
152         } elseif ((empty($salt)) && ($ret == "pass")) {
153                 // Something bad went wrong
154                 $ret = "failed";
155         } elseif ($ret == "done") {
156                 // Try to login here if we have the old hashing way (sql_patches not installed?)
157                 if (!LOGIN_ADMIN($admin_login, $data['password'])) {
158                         // Something went wrong
159                         $ret = "failed";
160                 } // END - if
161         }
162
163         // Count login failture if admins extension version is 0.7.0+
164         if (($ret == "pass") && (GET_EXT_VERSION("admins") >= "0.7.0")) {
165                 // Update counter
166                 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET login_failtures=login_failtures+1,last_failture=NOW() WHERE login='%s' LIMIT 1",
167                         array($admin_login), __FILE__, __LINE__);
168
169                 // Rebuild cache
170                 REBUILD_CACHE("admins", "admin");
171         } // END - if
172
173         // Return the result
174         //* DEBUG: */ die("RETURN=".$ret);
175         return $ret;
176 }
177
178 // Try to login the admin by setting some session/cookie variables
179 function LOGIN_ADMIN ($adminLogin, $passHash) {
180         global $cacheInstance;
181
182         // Reset failture counter on matching admins version
183         if ((GET_EXT_VERSION("admins") >= "0.7.0") && ((GET_EXT_VERSION("sql_patches") < "0.3.6") || (GET_EXT_VERSION("sql_patches") == ""))) {
184                 // Reset counter on out-dated sql_patches version
185                 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET login_failtures=0,last_failture='0000-00-00 00:00:00' WHERE login='%s' LIMIT 1",
186                         array($adminLogin), __FILE__, __LINE__);
187
188                 // Rebuild cache
189                 REBUILD_CACHE("admins", "admin");
190         } // END - if
191
192         // Now set all session variables and return the result
193         return (
194                 (
195                         set_session("admin_md5", generatePassString($passHash))
196                 ) && (
197                         set_session("admin_login", $adminLogin)
198                 ) && (
199                         set_session("admin_last", time())
200                 ) && (
201                         set_session("admin_to", bigintval($_POST['timeout']))
202                 )
203         );
204 }
205
206 // Only be executed on cookie checking
207 function CHECK_ADMIN_COOKIES ($admin_login, $password) {
208         global $cacheArray, $_CONFIG;
209         $ret = "404"; $pass = "";
210         if (!empty($cacheArray['admins']['aid'][$admin_login])) {
211                 // Get password from cache
212                 $pass = $cacheArray['admins']['password'][$admin_login];
213                 $ret = "pass";
214                 $_CONFIG['cache_hits']++;
215         } elseif (GET_EXT_VERSION("cache") == "") {
216                 // Get password from DB
217                 $result = SQL_QUERY_ESC("SELECT password FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
218                  array($admin_login), __FILE__, __LINE__);
219                 if (SQL_NUMROWS($result) == 1) {
220                         // Entry found
221                         $ret = "pass";
222
223                         // Fetch password
224                         list($pass) = SQL_FETCHROW($result);
225                 }
226
227                 // Free result
228                 SQL_FREERESULT($result);
229         }
230
231         //* DEBUG: */ echo __FUNCTION__.":".$pass."(".strlen($pass).")/".$password."(".strlen($password).")<br />\n";
232
233         // Check if password matches
234         if (($ret == "pass") && ((generatePassString($pass) == $password) || ($pass == $password) || ((strlen($pass) == 32) && (md5($password) == $pass)))) {
235                 // Passwords matches!
236                 $ret = "done";
237         }
238
239         // Return result
240         return $ret;
241 }
242 //
243 function admin_WriteData ($file, $comment, $prefix, $suffix, $DATA, $seek=0) {
244         // Initialize some variables
245         $done = false;
246         $seek++;
247         $found = false;
248
249         // Is the file there and read-/write-able?
250         if ((FILE_READABLE($file)) && (is_writeable($file))) {
251                 $search = "CFG: ".$comment;
252                 $tmp = $file.".tmp";
253
254                 // Open the source file
255                 $fp = @fopen($file, 'r') or OUTPUT_HTML("<STRONG>READ:</STRONG> ".$file."<br />");
256
257                 // Is the resource valid?
258                 if (is_resource($fp)) {
259                         // Open temporary file
260                         $fp_tmp = @fopen($tmp, 'w') or OUTPUT_HTML("<STRONG>WRITE:</STRONG> ".$tmp."<br />");
261
262                         // Is the resource again valid?
263                         if (is_resource($fp_tmp)) {
264                                 while (!feof($fp)) {
265                                         // Read from source file
266                                         $line = fgets ($fp, 1024);
267
268                                         if (strpos($line, $search) > -1) { $next = 0; $found = true; }
269
270                                         if ($next > -1) {
271                                                 if ($next == $seek) {
272                                                         $next = -1;
273                                                         $line = $prefix . $DATA . $suffix."\n";
274                                                 } else {
275                                                         $next++;
276                                                 }
277                                         }
278
279                                         // Write to temp file
280                                         fputs($fp_tmp, $line);
281                                 }
282
283                                 // Close temp file
284                                 fclose($fp_tmp);
285
286                                 // Finished writing tmp file
287                                 $done = true;
288                         }
289
290                         // Close source file
291                         fclose($fp);
292
293                         if (($done) && ($found)) {
294                                 // Copy back tmp file and delete tmp :-)
295                                 @copy($tmp, $file);
296                                 @unlink($tmp);
297                                 define('_FATAL', false);
298                         } elseif (!$found) {
299                                 OUTPUT_HTML("<STRONG>CHANGE:</STRONG> 404!");
300                                 define('_FATAL', true);
301                         } else {
302                                 OUTPUT_HTML("<STRONG>TMP:</STRONG> UNDONE!");
303                                 define('_FATAL', true);
304                         }
305                 }
306         } else {
307                 // File not found, not readable or writeable
308                 OUTPUT_HTML("<STRONG>404:</STRONG> ".$file."<br />");
309         }
310 }
311
312 //
313 function ADMIN_DO_ACTION($wht)
314 {
315         global $menuDesription, $menuTitle, $_CONFIG, $cacheArray, $DATA, $DEPTH;
316         //* DEBUG: */ echo __LINE__."*".$wht."/".$GLOBALS['module']."/".$GLOBALS['action']."/".$GLOBALS['what']."*<br />\n";
317         if (EXT_IS_ACTIVE("cache"))
318         {
319                 // Include cache instance
320                 global $cacheInstance;
321         }
322
323         // Remove any spaces from variable
324         if (empty($wht))
325         {
326                 // Default admin action is the overview page
327                 $wht = "overview";
328         }
329          else
330         {
331                 // Compile out some chars
332                 $wht = COMPILE_CODE($wht, false, false, false);
333         }
334
335         // Get action value
336         $act = GET_ACTION($GLOBALS['module'], $wht);
337
338         // Define admin login name and ID number
339         define('__ADMIN_LOGIN', get_session('admin_login'));
340         define('__ADMIN_ID'   , GET_ADMIN_ID(get_session('admin_login')));
341
342         // Preload templates
343         if (EXT_IS_ACTIVE("admins")) {
344                 define('__ADMIN_WELCOME', LOAD_TEMPLATE("admin_welcome_admins", true));
345         } else {
346                 define('__ADMIN_WELCOME', LOAD_TEMPLATE("admin_welcome", true));
347         }
348         define('__ADMIN_FOOTER' , LOAD_TEMPLATE("admin_footer" , true));
349         define('__ADMIN_MENU'   , ADD_ADMIN_MENU($act, $wht, true));
350
351         // Tableset header
352         LOAD_TEMPLATE("admin_main_header");
353
354         // Check if action/what pair is valid
355         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_admin_menu
356 WHERE action='%s' AND ((what='%s' AND what != 'overview') OR ((what='' OR what IS NULL) AND '%s'='overview'))
357 LIMIT 1", array($act, $wht, $wht), __FILE__, __LINE__);
358         if (SQL_NUMROWS($result) == 1)
359         {
360                 // Free memory
361                 SQL_FREERESULT($result);
362
363                 // Is valid but does the inlcude file exists?
364                 $INC = sprintf("%sinc/modules/admin/action-%s.php", PATH, $act);
365                 if ((FILE_READABLE($INC)) && (VALIDATE_MENU_ACTION("admin", $act, $wht)) && (__ACL_ALLOW == true))
366                 {
367                         // Ok, we finally load the admin action module
368                         include($INC);
369                 }
370                  elseif (__ACL_ALLOW == false)
371                 {
372                         // Access denied
373                         LOAD_TEMPLATE("admin_menu_failed", false, ADMINS_ACCESS_DENIED);
374                         ADD_FATAL(ADMINS_ACCESS_DENIED);
375                 }
376                  else
377                 {
378                         // Include file not found! :-(
379                         LOAD_TEMPLATE("admin_menu_failed", false, ADMIN_404_ACTION);
380                         ADD_FATAL(ADMIN_404_ACTION_1.$act.ADMIN_404_ACTION_2);
381                 }
382         } else {
383                 // Invalid action/what pair found!
384                 LOAD_TEMPLATE("admin_menu_failed", false, ADMIN_INVALID_ACTION);
385                 ADD_FATAL(ADMIN_INVALID_ACTION_1.$act."/".$wht.ADMIN_INVALID_ACTION_2);
386         }
387
388         // Tableset footer
389         LOAD_TEMPLATE("admin_main_footer");
390 }
391 //
392 function ADD_ADMIN_MENU($act, $wht, $return=false) {
393         global $menuDesription, $menuTitle, $cacheInstance, $_CONFIG;
394
395         // Init variables
396         $SUB = false;
397         $OUT = "";
398
399         // Menu descriptions
400         $menuDesription = array();
401         $menuTitle = array();
402
403         // Is there a cache instance?
404         if ((is_object($cacheInstance)) && (isset($_CONFIG['cache_admin_menu'])) && ($_CONFIG['cache_admin_menu'] == "Y")) {
405                 // Create cache name
406                 $cacheName = "admin_".$act."_".$wht."_".GET_LANGUAGE()."_".strtolower(get_session('admin_login'));
407
408                 // Is that cache there?
409                 if ($cacheInstance->cache_file($cacheName, true)) {
410                         // Then load it
411                         $data = $cacheInstance->cache_load();
412
413                         // Extract all parts
414                         $OUT = base64_decode($data['output'][0]);
415                         $menuTitle = unserialize(base64_decode($data['title'][0]));
416                         $menuDescription = unserialize(base64_decode($data['descr'][0]));
417
418                         // Return or output content?
419                         if ($return) {
420                                 return $OUT;
421                         } else {
422                                 OUTPUT_HTML($OUT);
423                         }
424                 } // END - if
425         } // END - if
426
427         // Build main menu
428         $result_main = SQL_QUERY("SELECT action, title, descr FROM "._MYSQL_PREFIX."_admin_menu WHERE (what='' OR what IS NULL) ORDER BY sort, id DESC", __FILE__, __LINE__);
429         if (SQL_NUMROWS($result_main) > 0)
430         {
431                 $OUT = "<TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"admin_menu_main\">
432 <TR><TD colspan=\"2\" height=\"7\" class=\"seperator\">&nbsp;</TD></TR>\n";
433                 while (list($menu, $title, $descr) = SQL_FETCHROW($result_main))
434                 {
435                         if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2"))
436                         {
437                                 $ACL = ADMINS_CHECK_ACL($menu, "");
438                         }
439                          else
440                         {
441                                 // ACL is "allow"... hmmm
442                                 $ACL = true;
443                         }
444                         if ($ACL)
445                         {
446                                 if (!$SUB)
447                                 {
448                                         // Insert compiled menu title and description
449                                         $menuTitle[$menu]      = $title;
450                                         $menuDesription[$menu] = $descr;
451                                 }
452                                 $OUT .= "<TR>
453         <TD class=\"admin_menu\" colspan=\"2\">
454                 <NOBR>&nbsp;<STRONG>&middot;</STRONG>&nbsp;";
455                                 if (($menu == $act) && (empty($wht)))
456                                 {
457                                         $OUT .= "<STRONG>";
458                                 }
459                                  else
460                                 {
461                                         $OUT .= "[<A href=\"".URL."/modules.php?module=admin&amp;action=".$menu."\">";
462                                 }
463                                 $OUT .= $title;
464                                 if (($menu == $act) && (empty($wht)))
465                                 {
466                                         $OUT .= "</STRONG>";
467                                 }
468                                  else
469                                 {
470                                         $OUT .= "</A>]";
471                                 }
472                                 $OUT .= "</NOBR></TD>
473 </TR>\n";
474                                 $result_what = SQL_QUERY_ESC("SELECT what, title, descr FROM "._MYSQL_PREFIX."_admin_menu WHERE action='%s' AND what != '' AND what IS NOT NULL ORDER BY sort, id DESC",
475                                  array($menu), __FILE__, __LINE__);
476                                 if ((SQL_NUMROWS($result_what) > 0) && ($act == $menu))
477                                 {
478                                         $menuDesription = array();
479                                         $menuTitle = array(); $SUB = true;
480                                         $OUT .= "<TR>
481         <TD width=\"10\" class=\"seperator\">&nbsp;</TD>
482         <TD class=\"admin_menu\">
483                 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"admin_menu_sub\">\n";
484                                         while (list($wht_sub, $title_what, $desc_what) = SQL_FETCHROW($result_what)) {
485                                                 // Filename
486                                                 $INC = sprintf("%sinc/modules/admin/what-%s.php", PATH, $wht_sub);
487                                                 if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2")) {
488                                                         $ACL = ADMINS_CHECK_ACL("", $wht_sub);
489                                                 } else {
490                                                         // ACL is "allow"... hmmm
491                                                         $ACL = true;
492                                                 }
493                                                 $readable = FILE_READABLE($INC);
494                                                 if ($ACL) {
495                                                         // Insert compiled title and description
496                                                         $menuTitle[$wht_sub]      = $title_what;
497                                                         $menuDesription[$wht_sub] = $desc_what;
498                                                         $OUT .= "<TR>
499         <TD class=\"admin_menu\" colspan=\"2\">
500                 <NOBR>&nbsp;<STRONG>--&gt;</STRONG>&nbsp;";
501                                                         if ($readable)
502                                                         {
503                                                                 if ($wht == $wht_sub)
504                                                                 {
505                                                                         $OUT .= "<STRONG>";
506                                                                 }
507                                                                  else
508                                                                 {
509                                                                         $OUT .= "[<A href=\"".URL."/modules.php?module=admin&amp;what=".$wht_sub."\">";
510                                                                 }
511                                                         }
512                                                          else
513                                                         {
514                                                                 $OUT .= "<I class=\"admin_note\">";
515                                                         }
516                                                         $OUT .= $title_what;
517                                                         if ($readable)
518                                                         {
519                                                                 if ($wht == $wht_sub)
520                                                                 {
521                                                                         $OUT .= "</STRONG>";
522                                                                 }
523                                                                  else
524                                                                 {
525                                                                         $OUT .= "</A>]";
526                                                                 }
527                                                         }
528                                                          else
529                                                         {
530                                                                 $OUT .= "</I>";
531                                                         }
532                                                         $OUT .= "</NOBR></TD>
533 </TR>\n";
534                                                 }
535                                         }
536
537                                         // Free memory
538                                         SQL_FREERESULT($result_what);
539                                         $OUT .= "    </TABLE>
540         </TD>
541 </TR>\n";
542                                 }
543                                 $OUT .= "<TR><TD height=\"7\" colspan=\"2\"></TD></TR>\n";
544                         }
545                 }
546
547                 // Free memory
548                 SQL_FREERESULT($result_main);
549                 $OUT .= "</TABLE>\n";
550         }
551
552         // Compile and run the code here. This inserts all constants into the
553         // HTML output. Costs me some time to figure this out... *sigh* Quix0r
554         $eval = "\$OUT = \"".COMPILE_CODE(addslashes($OUT))."\";";
555         eval($eval);
556
557         // Is there a cache instance again?
558         if ((is_object($cacheInstance)) && (isset($_CONFIG['cache_admin_menu'])) && ($_CONFIG['cache_admin_menu'] == "Y")) {
559                 // Init cache
560                 $cacheInstance->cache_init($cacheName);
561
562                 // Prepare cache data
563                 $data = array(
564                         'output' => base64_encode($OUT),
565                         'title'  => $menuTitle,
566                         'descr'  => $menuDesription
567                 );
568
569                 // Write the data away
570                 $cacheInstance->add_row($data);
571
572                 // Close cache
573                 $cacheInstance->cache_close();
574         } // END - if
575
576         // Return or output content?
577         if ($return) {
578                 return $OUT;
579         } else {
580                 OUTPUT_HTML($OUT);
581         }
582 }
583 //
584 function ADD_MEMBER_SELECTION_BOX ($def="0", $add_all=false, $return=false, $none=false)
585 {
586         // Output selection form with all confirmed user accounts listed
587         $result = SQL_QUERY("SELECT userid, surname, family FROM "._MYSQL_PREFIX."_user_data ORDER BY userid", __FILE__, __LINE__);
588         $OUT = "";
589
590         // USe this only for adding points (e.g. adding refs really makes no sence ;-) )
591         if ($add_all) $OUT = "      <OPTION value=\"all\">".ALL_MEMBERS."</OPTION>\n";
592          elseif ($none) $OUT = "      <OPTION value=\"0\">".SELECT_NONE."</OPTION>\n";
593         while (list($id, $sname, $fname) = SQL_FETCHROW($result))
594         {
595                 $OUT .= "      <OPTION value=\"".bigintval($id)."\"";
596                 if ($def == $id) $OUT .= " selected=\"selected\"";
597                 $OUT .= ">".$sname." ".$fname." (".bigintval($id).")</OPTION>\n";
598         }
599
600         // Free memory
601         SQL_FREERESULT($result);
602
603         if (!$return) {
604                 // Remeber options in constant
605                 define('_MEMBER_SELECTION', $OUT);
606
607                 // Display selection box
608                 define('__LANG_VALUE', GET_LANGUAGE());
609
610                 // Load template
611                 LOAD_TEMPLATE("admin_member_selection_box", false, $GLOBALS['what']);
612         } else {
613                 // Return content
614                 return "<select name=\"userid\" size=\"1\">\n".$OUT."</select>\n";
615         }
616 }
617 //
618 function ADMIN_MENU_SELECTION($MODE, $default="", $defid="") {
619         $wht = "what != ''";
620         if ($MODE == "action") $wht = "(what='' OR what IS NULL) AND action !='login'";
621         $result = SQL_QUERY_ESC("SELECT %s, title FROM "._MYSQL_PREFIX."_admin_menu WHERE ".$wht." ORDER BY sort",
622          array($MODE), __FILE__, __LINE__);
623         if (SQL_NUMROWS($result) > 0) {
624                 // Load menu as selection
625                 $OUT = "<SELECT name=\"".$MODE."_menu";
626                 if ((!empty($defid)) || ($defid == "0")) $OUT .= "[".$defid."]";
627                 $OUT .= "\" size=\"1\" class=\"admin_select\">
628         <OPTION value=\"\">".SELECT_NONE."</OPTION>\n";
629                 while (list($menu, $title) = SQL_FETCHROW($result)) {
630                         $OUT .= "  <OPTION value=\"".$menu."\"";
631                         if ((!empty($default)) && ($default == $menu)) $OUT .= " selected=\"selected\"";
632                         $OUT .= ">".$title."</OPTION>\n";
633                 } // END - while
634
635                 // Free memory
636                 SQL_FREERESULT($result);
637                 $OUT .= "</SELECT>\n";
638         } else {
639                 // No menus???
640                 $OUT = ADMIN_PROBLEM_NO_MENU;
641         }
642
643         // Return output
644         return $OUT;
645 }
646 // Save settings to the database
647 function ADMIN_SAVE_SETTINGS (&$POST, $tableName="_config", $whereStatement="config=0", $translateComma=array(), $alwaysAdd=false) {
648         global $_CONFIG, $cacheArray, $cacheInstance;
649
650         // Prepare all arrays, variables
651         $DATA = array();
652         $skip = false;
653
654         // Now, walk through all entries and prepare them for saving
655         foreach ($POST as $id => $val) {
656                 // Process only formular field but not submit buttons ;)
657                 if ($id != "ok") {
658                         // Do not save the ok value
659                         CONVERT_SELECTIONS_TO_TIMESTAMP($POST, $DATA, $id, $skip);
660
661                         // Shall we process this ID? It muss not be empty, of course
662                         if ((!$skip) && (!empty($id))) {
663                                 // Save this entry
664                                 $val = COMPILE_CODE($val);
665
666                                 // Translate the value? (comma to dot!)
667                                 if ((is_array($translateComma)) && (in_array($id, $translateComma))) {
668                                         // Then do it here... :)
669                                         $val = str_replace(",", ".", $val);
670                                 } // END - if
671
672                                 // Shall we add numbers or strings?
673                                 $test = (float)$val;
674                                 if ("".$val."" == "".$test."") {
675                                         // Add numbers
676                                         $DATA[] = $id."=".$val."";
677                                 } else {
678                                         // Add strings
679                                         $DATA[] = $id."='".trim($val)."'";
680                                 }
681
682                                 // Update current configuration
683                                 $_CONFIG[$id] = $val;
684                         } // END - if
685                 } // END - if
686         } // END - foreach
687
688         // Check if entry does exist
689         $result = false;
690         if (!$alwaysAdd) {
691                 if (!empty($whereStatement)) {
692                         $result = SQL_QUERY("SELECT * FROM "._MYSQL_PREFIX.$tableName." WHERE ".$whereStatement." LIMIT 1", __FILE__, __LINE__);
693                 } else {
694                         $result = SQL_QUERY("SELECT * FROM "._MYSQL_PREFIX.$tableName." LIMIT 1", __FILE__, __LINE__);
695                 }
696         } // END - if
697
698         if (SQL_NUMROWS($result) == 1) {
699                 // "Implode" all data to single string
700                 $DATA_UPDATE = implode(", ", $DATA);
701
702                 // Generate SQL string
703                 $SQL = "UPDATE "._MYSQL_PREFIX.$tableName." SET ".$DATA_UPDATE." WHERE ".$whereStatement." LIMIT 1";
704         } else {
705                 // Add Line (does only work with auto_increment!
706                 $KEYs = array(); $VALUEs = array();
707                 foreach ($DATA as $entry) {
708                         // Split up
709                         $line = explode("=", $entry);
710                         $KEYs[] = $line[0]; $VALUEs[] = $line[1];
711                 } // END - foreach
712
713                 // Add both in one line
714                 $KEYs = implode(", ", $KEYs);
715                 $VALUEs = implode(", ", $VALUEs);
716
717                 // Generate SQL string
718                 $SQL = "INSERT INTO "._MYSQL_PREFIX.$tableName." (".$KEYs.") VALUES(".$VALUEs.")";
719         }
720
721         // Free memory
722         SQL_FREERESULT($result);
723
724         // Simply run generated SQL string
725         $result = SQL_QUERY($SQL, __FILE__, __LINE__);
726
727         // Rebuild cache
728         REBUILD_CACHE("config", "config");
729
730         // Settings saved
731         LOAD_TEMPLATE("admin_settings_saved", false, "<STRONG class=\"admin_done\">".SETTINGS_SAVED."</STRONG>");
732 }
733 //
734 function ADMIN_MAKE_MENU_SELECTION($menu, $type, $name, $default="") {
735         // Open the requested menu directory
736         $handle = opendir(sprintf("%sinc/modules/%s/", PATH, $menu)) or mxchange_die("Cannot load menu ".$menu."!");
737
738         // Init the selection box
739         $OUT = "<SELECT name=\"".$name."\" class=\"admin_select\" size=\"1\">\n <OPTION value=\"\">".IS_TOP_MENU."</OPTION>\n";
740
741         // Walk through all files
742         while ($file = readdir($handle)) {
743                 // Is this a PHP script?
744                 if (($file != ".") && ($file != "..") && ($file != "lost+found") && (strpos($file, "".$type."-") > -1) && (strpos($file, ".php") > 0)) {
745                         // Then test if the file is readable
746                         $test = sprintf("%sinc/modules/%s/%s", PATH, $menu, $file);
747                         if ((is_file($test)) && (is_readable($test))) {
748                                 // Extract the value for what=xxx
749                                 $part = substr($file, (strlen($type) + 1));
750                                 $part = substr($part, 0, -4);
751
752                                 // Is that part different from the overview?
753                                 if ($part != "overview") {
754                                         $OUT .= "       <OPTION value=\"".$part."\"";
755                                         if ($part == $default) $OUT .= "selected";
756                                         $OUT .= ">".$part."</OPTION>\n";
757                                 } // END - if
758                         } // END - if
759                 } // END - if
760         } // END - while
761
762         // Close dir and selection box
763         closedir($handle);
764         $OUT .= "</SELECT>\n";
765         return $OUT;
766 }
767 //
768 function ADMIN_USER_PROFILE_LINK($uid, $title="", $wht="list_user") {
769         if (($title == "") && ($title != "0")) {
770                 // Set userid as title
771                 $title = $uid;
772         } // END - if
773
774         if (($title == "0") && ($wht == "list_refs")) {
775                 // Return title again
776                 return $title;
777         } // END - if
778
779         //* DEBUG: */ echo "A:".$title."<br />";
780         // Return link
781         return "<A href=\"".URL."/modules.php?module=admin&amp;what=".$wht."&amp;u_id=".$uid."\" title=\"".ADMIN_USER_PROFILE_TITLE."\">".$title."</A>";
782 }
783 //
784 function ADMIN_CHECK_MENU_MODE() {
785         global $_CONFIG, $cacheArray;
786
787         // Set the global mode as the mode for all admins
788         $MODE = $_CONFIG['admin_menu']; $ADMIN = $MODE;
789
790         // Check individual settings of current admin
791         if (isset($cacheArray['admins']['la_mode'][get_session('admin_login')])) {
792                 // Load from cache
793                 $ADMIN = $cacheArray['admins']['la_mode'][get_session('admin_login')];
794                 $_CONFIG['cache_hits']++;
795         } elseif (GET_EXT_VERSION("admins") >= "0.6.7") {
796                 // Load from database when version of "admins" is enough
797                 $result = SQL_QUERY_ESC("SELECT la_mode FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
798                  array(get_session('admin_login')), __FILE__, __LINE__);
799                 if (SQL_NUMROWS($result) == 1) {
800                         // Load data
801                         list($ADMIN) = SQL_FETCHROW($result);
802                 }
803
804                 // Free memory
805                 SQL_FREERESULT($result);
806         }
807
808         // Check what the admin wants and set it when it's not the global mode
809         if ($ADMIN != "global") $MODE = $ADMIN;
810
811         // Return admin-menu's mode
812         return $MODE;
813 }
814 // Change activation status
815 function ADMIN_CHANGE_ACTIVATION_STATUS ($IDs, $table, $row, $idRow = "id") {
816         global $_CONFIG;
817         $cnt = 0; $newStatus = "Y";
818         if ((is_array($IDs)) && (count($IDs) > 0)) {
819                 // "Walk" all through and count them
820                 foreach ($IDs as $id => $selected) {
821                         // Secure the ID number
822                         $id = bigintval($id);
823
824                         // Should always be 1 ;-)
825                         if ($selected == 1) {
826                                 // Determine new status
827                                 $result = SQL_QUERY_ESC("SELECT %s FROM "._MYSQL_PREFIX."_%s WHERE %s=%s LIMIT 1",
828                                         array($row, $table, $idRow, $id), __FILE__, __LINE__);
829
830                                 // Row found?
831                                 if (SQL_NUMROWS($result) == 1) {
832                                         // Load the status
833                                         list($currStatus) = SQL_FETCHROW($result);
834                                         if ($currStatus == "Y") $newStatus='N'; else $newStatus = "Y";
835
836                                         // Change this status
837                                         SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_%s SET %s='%s' WHERE %s=%s LIMIT 1",
838                                                 array($table, $row, $newStatus, $idRow, $id), __FILE__, __LINE__);
839
840                                         // Count up affected rows
841                                         $cnt += SQL_AFFECTEDROWS();
842                                 }
843
844                                 // Free the result
845                                 SQL_FREERESULT($result);
846                         }
847                 }
848
849                 // Output status
850                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_STATUS_CHANGED_1.$cnt.ADMIN_STATUS_CHANGED_2.count($IDs).ADMIN_STATUS_CHANGED_3);
851         } else {
852                 // Nothing selected!
853                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_NOTHING_SELECTED_CHANGE);
854         }
855 }
856 // Send mails for del/edit/lock build modes
857 function ADMIN_SEND_BUILD_MAILS ($mode, $table, $content, $id, $subjectPart="") {
858         // Default subject is the subject part
859         $subject = $subjectPart;
860
861         // Is the subject part not set?
862         if (empty($subjectPart)) {
863                 // Then use it from the mode
864                 $subject = strtoupper($mode);
865         } // END - if
866
867         // Is the raw userid set?
868         if ($_POST['uid_raw'][$id] > 0) {
869                 // Generate subject
870                 $eval = "\$subjectLine = MEMBER_".$subject."_".strtoupper($table)."_SUBJECT;";
871                 eval($eval);
872
873                 // Load email template
874                 if (!empty($subjectPart)) {
875                         $mail = LOAD_EMAIL_TEMPLATE("member_".$mode."_".strtolower($subjectPart)."_".$table, $content);
876                 } else {
877                         $mail = LOAD_EMAIL_TEMPLATE("member_".$mode."_".$table, $content);
878                 }
879
880                 // Send email out
881                 SEND_EMAIL($_POST['uid_raw'][$id], $subjectLine, $mail);
882         } // END - if
883
884         // Generate subject
885         $eval = "\$subjectLine = ADMIN_".$subject."_".strtoupper($table)."_SUBJECT;";
886         eval($eval);
887
888         // Send admin notification out
889         if (!empty($subjectPart)) {
890                 SEND_ADMIN_NOTIFICATION($subjectLine, "admin_".$mode."_".strtolower($subjectPart)."_".$table, $content, $_POST['uid_raw'][$id]);
891         } else {
892                 SEND_ADMIN_NOTIFICATION($subjectLine, "admin_".$mode."_".$table, $content, $_POST['uid_raw'][$id]);
893         }
894 }
895 // Build a special template list
896 function ADMIN_BUILD_LIST ($listType, $IDs, $table, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn) {
897         global $_CONFIG;
898         $OUT = ""; $SW = 2;
899
900         // "Walk" through all entries
901         foreach ($IDs as $id => $selected) {
902                 // Secure ID number
903                 $id = bigintval($id);
904
905                 // Get result from a given column array and table name
906                 $result = SQL_RESULT_FROM_ARRAY($table, $columns, $idColumn, $id);
907
908                 // Is there one entry?
909                 if (SQL_NUMROWS($result) == 1) {
910                         // Load all data
911                         $content = SQL_FETCHARRAY($result);
912
913                         // Filter all data
914                         foreach ($content as $key => $value) {
915                                 // Search index
916                                 $idx = array_search($key, $columns, true);
917
918                                 // Do we have a userid?
919                                 if ($key == "userid") {
920                                         // Add it again as raw id
921                                         $content['uid'] = bigintval($value);
922                                 } // END - if
923
924                                 // Handle the call in external function
925                                 $content[$key] = HANDLE_EXTRA_VALUES($filterFunctions[$idx], $value, $extraValues[$idx]);
926                         } // END - foreach
927
928                         // Add color switching
929                         $content['sw'] = $SW;
930
931                         // Then list it
932                         $OUT .= LOAD_TEMPLATE("admin_".$listType."_".$table."_row", true, $content);
933
934                         // Switch color
935                         $SW = 3 - $SW;
936                 } // END - if
937
938                 // Free the result
939                 SQL_FREERESULT($result);
940         } // END - foreach
941
942         // Load master template
943         LOAD_TEMPLATE("admin_".$listType."_".$table."", false, $OUT);
944 }
945 // Delete rows by given ID numbers
946 function ADMIN_DELETE_ENTRIES_CONFIRM ($IDs, $table, $columns=array(), $filterFunctions=array(), $extraValues=array(), $deleteNow=false, $idColumn="id", $userIdColumn="userid") {
947         // All valid entries? (We hope so here!)
948         if ((is_array($IDs)) && (count($IDs) > 0) && (count($columns) == count($filterFunctions)) && (count($columns) == count($extraValues))) {
949                 // Shall we delete here or list for deletion?
950                 if ($deleteNow) {
951                         // Delete them
952                         $SQL = "DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_%s WHERE %s IN (%s)";
953                         $idList = "";
954                         foreach ($IDs as $id => $sel) {
955                                 // Is there a userid?
956                                 if (isset($_POST['uid_raw'][$id])) {
957                                         // Load all data from that id
958                                         $result = SQL_QUERY_ESC("SELECT * FROM "._MYSQL_PREFIX."_%s WHERE %s=%s LIMIT 1",
959                                                 array($table, $idColumn, $id), __FILE__, __LINE__);
960
961                                         // Fetch the data
962                                         $content = SQL_FETCHARRAY($result);
963
964                                         // Free the result
965                                         SQL_FREERESULT($result);
966
967                                         // Send "build mails" out
968                                         ADMIN_SEND_BUILD_MAILS("del", $table, $content, $id);
969                                 } // END - if
970
971                                 // Add id number
972                                 $idList .= $id.",";
973                         } // END - if
974
975                         // Run the query
976                         SQL_QUERY($SQL, array($table, $idColumn, substr($idList, 0, -1)), __FILE__, __LINE__);
977
978                         // Was this fine?
979                         if (SQL_AFFECTEDROWS() == count($IDs)) {
980                                 // All deleted
981                                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_ALL_ENTRIES_REMOVED);
982                         } else {
983                                 // Some are still there :(
984                                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(ADMIN_SOME_ENTRIES_NOT_DELETED, SQL_AFFECTEDROWS(), count($IDs)));
985                         }
986                 } else {
987                         // List for deletion confirmation
988                         ADMIN_BUILD_LIST("del", $IDs, $table, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn);
989                 }
990         } // END - if
991 }
992 // Edit rows by given ID numbers
993 function ADMIN_EDIT_ENTRIES_CONFIRM ($IDs, $table, $columns=array(), $filterFunctions=array(), $extraValues=array(), $editNow=false, $idColumn="id", $userIdColumn="userid") {
994         // All valid entries? (We hope so here!)
995         if ((is_array($IDs)) && (count($IDs) > 0) && (count($columns) == count($filterFunctions)) && (count($columns) == count($extraValues))) {
996                 // Shall we change here or list for editing?
997                 if ($editNow) {
998                         // Change them all
999                         $affected = 0;
1000                         foreach ($IDs as $id => $sel) {
1001                                 // Prepare content array (new values)
1002                                 $content = array();
1003
1004                                 // Prepare SQL for this row
1005                                 $SQL = "UPDATE "._MYSQL_PREFIX."_".$table." SET";
1006                                 foreach ($_POST as $key => $entries) {
1007                                         // Skip raw userid which is always invalid
1008                                         if ($key == "uid_raw") {
1009                                                 // Continue with next field
1010                                                 continue;
1011                                         } // END - if
1012
1013                                         // Is entries an array?
1014                                         if (($key != $idColumn) && (is_array($entries)) && (isset($entries[$id]))) {
1015                                                 // Add this entry to content
1016                                                 $content[$key] = $entries[$id];
1017
1018                                                 // Send data through the filter function if found
1019                                                 if ((isset($filterFunctions[$key])) && (isset($extraValues[$key]))) {
1020                                                         // Filter function set!
1021                                                         $entries[$id] = HANDLE_EXTRA_VALUES($filterFunctions[$key], $entries[$id], $extraValues[$key]);
1022                                                 } // END - if
1023
1024                                                 // Then add this value
1025                                                 $SQL .= sprintf(" %s='%s',",
1026                                                         SQL_ESCAPE($key),
1027                                                         SQL_ESCAPE($entries[$id])
1028                                                 );
1029                                         } elseif (($key != $idColumn) && (!is_array($entries))) {
1030                                                 // Add normal entries as well!
1031                                                 $content[$key] =  $entries;
1032                                         }
1033
1034                                         // Do we have an URL?
1035                                         if ($key == "url") {
1036                                                 // Then add a framekiller test as well
1037                                                 $content['frametester'] = FRAMETESTER($content[$key]);
1038                                         } // END - if
1039                                 }
1040                                 $SQL = substr($SQL, 0, -1) . " WHERE ".$idColumn."=".bigintval($id)." LIMIT 1";
1041
1042                                 // Run this query
1043                                 SQL_QUERY($SQL, __FILE__, __LINE__);
1044
1045                                 // Add affected rows
1046                                 $affected += SQL_AFFECTEDROWS();
1047
1048                                 // Load all data from that id
1049                                 $result = SQL_QUERY_ESC("SELECT * FROM "._MYSQL_PREFIX."_%s WHERE %s=%s LIMIT 1",
1050                                         array($table, $idColumn, $id), __FILE__, __LINE__);
1051
1052                                 // Fetch the data
1053                                 global $DATA;
1054                                 $DATA = SQL_FETCHARRAY($result);
1055
1056                                 // Free the result
1057                                 SQL_FREERESULT($result);
1058
1059                                 // Send "build mails" out
1060                                 ADMIN_SEND_BUILD_MAILS("edit", $table, $content, $id);
1061                         } // END - foreach
1062
1063                         // Was this fine?
1064                         if ($affected == count($IDs)) {
1065                                 // All deleted
1066                                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_ALL_ENTRIES_EDITED);
1067                         } else {
1068                                 // Some are still there :(
1069                                 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(ADMIN_SOME_ENTRIES_NOT_EDITED, $affected, count($IDs)));
1070                         }
1071                 } else {
1072                         // List for editing
1073                         ADMIN_BUILD_LIST("edit", $IDs, $table, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn);
1074                 }
1075         } // END - if
1076 }
1077 // Un-/lock rows by given ID numbers
1078 function ADMIN_LOCK_ENTRIES_CONFIRM ($IDs, $table, $columns=array(), $filterFunctions=array(), $extraValues=array(), $statusArray=array(), $lockNow=false, $idColumn="id", $userIdColumn="userid") {
1079         // All valid entries? (We hope so here!)
1080         if ((is_array($IDs)) && (count($IDs) > 0) && (count($columns) == count($filterFunctions)) && (count($columns) == count($extraValues)) && ((!$lockNow) || (count($statusArray) == 1))) {
1081                 // Shall we un-/lock here or list for locking?
1082                 if ($lockNow) {
1083                         // Un-/lock entries
1084                         foreach ($IDs as $id => $sel) {
1085                                 // Construct SQL query
1086                                 $SQL = "UPDATE "._MYSQL_PREFIX."_".$table." SET";
1087
1088                                 // Load data of entry
1089                                 $result = SQL_QUERY_ESC("SELECT * FROM "._MYSQL_PREFIX."_%s WHERE %s=%s LIMIT 1",
1090                                         array($table, $idColumn, $id), __FILE__, __LINE__);
1091
1092                                 // Fetch the data
1093                                 $content = SQL_FETCHARRAY($result);
1094
1095                                 // Free the result
1096                                 SQL_FREERESULT($result);
1097
1098                                 // Add all status entries (e.g. status column last_updated or so)
1099                                 $newStatus = "UNKNOWN";
1100                                 $oldStatus = "UNKNOWN";
1101                                 $statusColumn = "unknown";
1102                                 foreach ($statusArray as $column => $statusInfo) {
1103                                         // Does the entry exist?
1104                                         if ((isset($content[$column])) && (isset($statusInfo[$content[$column]]))) {
1105                                                 // Add these entries for update
1106                                                 $SQL .= sprintf(" %s='%s',", SQL_ESCAPE($column), SQL_ESCAPE($statusInfo[$content[$column]]));
1107
1108                                                 // Remember status
1109                                                 if ($statusColumn == "unknown") {
1110                                                         // Always (!!!) change status column first!
1111                                                         $oldStatus = $content[$column];
1112                                                         $newStatus = $statusInfo[$oldStatus];
1113                                                         $statusColumn = $column;
1114                                                 } // END - if
1115                                         } elseif (isset($content[$column])) {
1116                                                 // Unfinished!
1117                                                 die("UNFINISHED: {$column}[".gettype($statusInfo)."] = {$content[$column]}");
1118                                         }
1119                                 } // END - foreach
1120
1121                                 // Add other columns as well
1122                                 foreach ($_POST as $key => $entries) {
1123                                         // Skip id, raw userid and 'do_lock'
1124                                         if (!in_array($key, array($idColumn, 'uid_raw', 'do_lock'))) {
1125                                                 // Are there brackets () at the end?
1126                                                 if (substr($entries[$id], -2, 2) == "()") {
1127                                                         // Direct SQL command found
1128                                                         $SQL .= sprintf(" %s=%s,", SQL_ESCAPE($key), SQL_ESCAPE($entries[$id]));
1129                                                 } else {
1130                                                         // Add regular entry
1131                                                         $SQL .= sprintf(" %s='%s',", SQL_ESCAPE($key), SQL_ESCAPE($entries[$id]));
1132                                                 }
1133                                         } // END - if
1134                                 } // END - if
1135
1136                                 // Finish SQL statement
1137                                 $SQL = substr($SQL, 0, -1) . " WHERE ".$idColumn."=".bigintval($id)." AND ".$statusColumn."='".$oldStatus."' LIMIT 1";
1138
1139                                 // Run the SQL
1140                                 SQL_QUERY($SQL, __FILE__, __LINE__);
1141
1142                                 // Do we have an URL?
1143                                 if (isset($content['url'])) {
1144                                         // Then add a framekiller test as well
1145                                         $content['frametester'] = FRAMETESTER($content['url']);
1146                                 } // END - if
1147
1148                                 // Send "build mails" out
1149                                 ADMIN_SEND_BUILD_MAILS("lock", $table, $content, $id, $statusInfo[$content[$column]]);
1150                         } // END - if
1151                 } else {
1152                         // List for editing
1153                         ADMIN_BUILD_LIST("lock", $IDs, $table, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn);
1154                 }
1155         } // END - if
1156 }
1157 // Checks proxy settins by fetching check-updates3.php from www.mxchange.org
1158 function ADMIN_TEST_PROXY_SETTINGS ($settingsArray) {
1159         global $_CONFIG;
1160
1161         // Set temporary the new settings
1162         $_CONFIG = array_merge($_CONFIG, $settingsArray);
1163
1164         // Now get the test URL
1165         $content = GET_URL("check-updates3.php");
1166
1167         // Is the first line with "200 OK"?
1168         $valid = eregi("200 OK", $content[0]);
1169
1170         // Return result
1171         return $valid;
1172 }
1173 // Sends out a link to the given email adress so the admin can reset his/her password
1174 function ADMIN_SEND_PASSWORD_RESET_LINK ($email) {
1175         global $_CONFIG;
1176         // Init output
1177         $OUT = "";
1178
1179         // Compile out security characters (must be for looking up!)
1180         $email = COMPILE_CODE($email);
1181
1182         // Look up administator login
1183         $result = SQL_QUERY_ESC("SELECT id, login, password FROM "._MYSQL_PREFIX."_admins WHERE email='%s' LIMIT 1",
1184                 array($email), __FILE__, __LINE__);
1185
1186         // Is there an account?
1187         if (SQL_NUMROWS($result) == 0) {
1188                 // No account found!
1189                 return ADMIN_NO_LOGIN_WITH_EMAIL;
1190         } // END - if
1191
1192         // Load all data
1193         $content = SQL_FETCHARRAY($result);
1194
1195         // Free result
1196         SQL_FREERESULT($result);
1197
1198         // Generate hash for reset link
1199         $content['hash'] = generateHash(URL.":".$content['id'].":".$content['login'].":".$content['password'], substr($content['password'], 10));
1200
1201         // Remove some data
1202         unset($content['id']);
1203         unset($content['password']);
1204
1205         // Prepare email
1206         $mailText = LOAD_EMAIL_TEMPLATE("admin_reset_password", $content);
1207
1208         // Send it out
1209         SEND_EMAIL($email, ADMIN_RESET_PASS_LINK_SUBJ, $mailText);
1210
1211         // Prepare output
1212         return ADMIN_RESET_LINK_SENT;
1213 }
1214 // Validate hash and login for password reset
1215 function ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN ($hash, $login) {
1216         // By default nothing validates... ;)
1217         $valid = false;
1218
1219         // Compile the login for lookup
1220         $login = COMPILE_CODE($login);
1221
1222         // Then try to find that user
1223         $result = SQL_QUERY_ESC("SELECT id, password, email FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
1224                 array($login), __FILE__, __LINE__);
1225
1226         // Is an account here?
1227         if (SQL_NUMROWS($result) == 1) {
1228                 // Load all data
1229                 $content = SQL_FETCHARRAY($result);
1230
1231                 // Generate hash again
1232                 $hashFromData = generateHash(URL.":".$content['id'].":".$login.":".$content['password'], substr($content['password'], 10));
1233
1234                 // Does both match?
1235                 $valid = ($hash == $hashFromData);
1236         } // END - if
1237
1238         // Free result
1239         SQL_FREERESULT($result);
1240
1241         // Return result
1242         return $valid;
1243 }
1244 // Reset the password for the login. Do NOT call this function without calling above function first!
1245 function ADMIN_RESET_PASSWORD ($login, $password) {
1246         // Init hash
1247         $passHash = "";
1248
1249         // Now check if we have sql_patches installed
1250         if (GET_EXT_VERSION("sql_patches") >= "0.3.6") {
1251                 // Use new way of hashing
1252                 $passHash = generateHash($password);
1253         } else {
1254                 // Old MD5 method
1255                 $passHash = md5($password);
1256         }
1257
1258         // Update database
1259         SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET password='%s' WHERE login='%s' LIMIT 1",
1260                 array($passHash, $login), __FILE__, __LINE__);
1261
1262         // Return output
1263         return ADMIN_PASSWORD_RESET_DONE;
1264 }
1265 // Solves a task by given id number
1266 function ADMIN_SOLVE_TASK ($id) {
1267         // Update the task data
1268         ADMIN_UPDATE_TASK_DATA($id, "status", "SOLVED");
1269 }
1270 // Marks a given task as deleted
1271 function ADMIN_DELETE_TASK ($id) {
1272         // Update the task data
1273         ADMIN_UPDATE_TASK_DATA($id, "status", "DELETED");
1274 }
1275 // Function to update task data
1276 function ADMIN_UPDATE_TASK_DATA ($id, $row, $data) {
1277         // Update the task
1278         SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_task_system SET %s='%s' WHERE id=%s LIMIT 1",
1279                 array($row, $data, bigintval($id)), __FILE__, __LINE__);
1280 }
1281 //
1282 ?>