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