2 /************************************************************************
3 * MXChange v0.2.1 Start: 08/31/2003 *
4 * =============== Last change: 11/23/2004 *
6 * -------------------------------------------------------------------- *
7 * File : admin-inc.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Administrative related functions *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Fuer die Administration benoetigte Funktionen *
12 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
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. *
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. *
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, *
32 ************************************************************************/
34 // Some security stuff...
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
37 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
42 function REGISTER_ADMIN ($user, $md5, $email=WEBMASTER)
45 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
46 array($user), __FILE__, __LINE__);
47 if (SQL_NUMROWS($result) == 0) {
48 // Ok, let's create the admin login
49 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_admins (login, password, email) VALUES('%s', '%s', '%s')",
50 array($user, $md5, $email), __FILE__, __LINE__);
54 SQL_FREERESULT($result);
56 // Login does already exist
61 // Only be executed on login procedure!
62 function CHECK_ADMIN_LOGIN ($admin_login, $password)
64 global $cacheArray, $_CONFIG, $cacheInstance;
65 $ret = "404"; $pass = "";
66 if (!empty($cacheArray['admins']['aid'][$admin_login])) {
67 // Get password from cache
68 $pass = $cacheArray['admins']['password'][$admin_login];
70 $_CONFIG['cache_hits']++;
72 // Get password from DB
73 $result = SQL_QUERY_ESC("SELECT password FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
74 array($admin_login), __FILE__, __LINE__);
75 if (SQL_NUMROWS($result) == 1) {
77 list($pass) = SQL_FETCHROW($result);
78 SQL_FREERESULT($result);
82 //* DEBUG: */ echo "*".$pass."/".md5($password)."/".$ret."<br />";
83 if ((strlen($pass) == 32) && ($pass == md5($password))) {
85 $pass = generateHash($password);
87 // Is the sql_patches not installed, than we cannot have a valid hashed password here!
88 if (($ret == "pass") && ((GET_EXT_VERSION("sql_patches") < "0.3.6") || (GET_EXT_VERSION("sql_patches") == ""))) $ret = "done";
89 } elseif ((GET_EXT_VERSION("sql_patches") < "0.3.6") || (GET_EXT_VERSION("sql_patches") == "")) {
94 // Generate salt of password
95 define('__SALT', substr($pass, 0, -40));
98 // Check if password is same
99 //* DEBUG: */ echo "*".$ret.",".$pass.",".$password.",".$salt."*<br >\n";
100 if (($ret == "pass") && ($pass == generateHash($password, $salt)) && (!empty($salt))) {
101 // Change the passord hash here
102 $pass = generateHash($password);
105 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET password='%s' WHERE login='%s' LIMIT 1",
106 array($pass, $admin_login), __FILE__, __LINE__);
108 // Shall I remove the cache file?
109 if ((EXT_IS_ACTIVE("cache")) && ($cacheInstance != false)) {
110 if ($cacheInstance->cache_file("admins", true)) $cacheInstance->cache_destroy();
113 // Login has failed by default... ;-)
116 // Password matches so login here
117 if (LOGIN_ADMIN($admin_login, $pass)) {
121 } elseif ((empty($salt)) && ($ret == "pass")) {
122 // Something bad went wrong
124 } elseif ($ret == "done") {
125 // Try to login here if we have the old hashing way (sql_patches not installed?)
126 if (!LOGIN_ADMIN($admin_login, $pass)) {
127 // Something went wrong
133 //* DEBUG: */ die("RETURN=".$ret);
137 // Try to login the admin by setting some session/cookie variables
138 function LOGIN_ADMIN ($adminLogin, $passHash) {
139 // Now set all session variables and return the result
142 set_session("admin_md5", generatePassString($passHash))
144 set_session("admin_login", $adminLogin)
146 set_session("admin_last", time())
148 set_session("admin_to", $_POST['timeout'])
153 // Only be executed on cookie checking
154 function CHECK_ADMIN_COOKIES ($admin_login, $password) {
155 global $cacheArray, $_CONFIG;
156 $ret = "404"; $pass = "";
157 if (!empty($cacheArray['admins']['aid'][$admin_login])) {
158 // Get password from cache
159 $pass = $cacheArray['admins']['password'][$admin_login];
161 $_CONFIG['cache_hits']++;
163 // Get password from DB
164 $result = SQL_QUERY_ESC("SELECT password FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
165 array($admin_login), __FILE__, __LINE__);
166 if (SQL_NUMROWS($result) == 1) {
171 list($pass) = SQL_FETCHROW($result);
175 SQL_FREERESULT($result);
178 //* DEBUG: */ echo __FUNCTION__.":".$pass."(".strlen($pass).")/".$password."(".strlen($password).")<br />\n";
180 // Check if password matches
181 if (($ret == "pass") && ((generatePassString($pass) == $password) || ($pass == $password) || ((strlen($pass) == 32) && (md5($password) == $pass)))) {
182 // Passwords matches!
190 function admin_WriteData ($file, $comment, $prefix, $suffix, $DATA, $seek=0) {
191 // Initialize some variables
196 // Is the file there and read-/write-able?
197 if ((FILE_READABLE($file)) && (is_writeable($file))) {
198 $search = "CFG: ".$comment;
201 // Open the source file
202 $fp = @fopen($file, 'r') or OUTPUT_HTML("<STRONG>READ:</STRONG> ".$file."<br />");
204 // Is the resource valid?
205 if (is_resource($fp)) {
206 // Open temporary file
207 $fp_tmp = @fopen($tmp, 'w') or OUTPUT_HTML("<STRONG>WRITE:</STRONG> ".$tmp."<br />");
209 // Is the resource again valid?
210 if (is_resource($fp_tmp)) {
212 // Read from source file
213 $line = fgets ($fp, 1024);
215 if (strpos($line, $search) > -1) { $next = 0; $found = true; }
218 if ($next == $seek) {
220 $line = $prefix . $DATA . $suffix."\n";
226 // Write to temp file
227 fputs($fp_tmp, $line);
233 // Finished writing tmp file
240 if (($done) && ($found)) {
241 // Copy back tmp file and delete tmp :-)
244 define('_FATAL', false);
246 OUTPUT_HTML("<STRONG>CHANGE:</STRONG> 404!");
247 define('_FATAL', true);
249 OUTPUT_HTML("<STRONG>TMP:</STRONG> UNDONE!");
250 define('_FATAL', true);
254 // File not found, not readable or writeable
255 OUTPUT_HTML("<STRONG>404:</STRONG> ".$file."<br />");
260 function ADMIN_DO_ACTION($wht)
262 global $menuDesription, $menuTitle, $_CONFIG, $cacheArray, $DATA, $DEPTH;
263 //* DEBUG: */ echo __LINE__."*".$wht."/".$GLOBALS['module']."/".$GLOBALS['action']."/".$GLOBALS['what']."*<br />\n";
264 if (EXT_IS_ACTIVE("cache"))
266 // Include cache instance
267 global $cacheInstance;
270 // Remove any spaces from variable
273 // Default admin action is the overview page
278 // Compile out some chars
279 $wht = COMPILE_CODE($wht, false, false, false);
283 $act = GET_ACTION($GLOBALS['module'], $wht);
285 // Define admin login name and ID number
286 define('__ADMIN_LOGIN', get_session('admin_login'));
287 define('__ADMIN_ID' , GET_ADMIN_ID(get_session('admin_login')));
290 if (EXT_IS_ACTIVE("admins")) {
291 define('__ADMIN_WELCOME', LOAD_TEMPLATE("admin_welcome_admins", true));
293 define('__ADMIN_WELCOME', LOAD_TEMPLATE("admin_welcome", true));
295 define('__ADMIN_FOOTER' , LOAD_TEMPLATE("admin_footer" , true));
296 define('__ADMIN_MENU' , ADD_ADMIN_MENU($act, $wht, true));
299 LOAD_TEMPLATE("admin_main_header");
301 // Check if action/what pair is valid
302 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_admin_menu
303 WHERE action='%s' AND ((what='%s' AND what != 'overview') OR ((what='' OR what IS NULL) AND '%s'='overview'))
304 LIMIT 1", array($act, $wht, $wht), __FILE__, __LINE__);
305 if (SQL_NUMROWS($result) == 1)
308 SQL_FREERESULT($result);
310 // Is valid but does the inlcude file exists?
311 $INC = sprintf("%sinc/modules/admin/action-%s.php", PATH, $act);
312 if ((FILE_READABLE($INC)) && (VALIDATE_MENU_ACTION("admin", $act, $wht)) && (__ACL_ALLOW == true))
314 // Ok, we finally load the admin action module
317 elseif (__ACL_ALLOW == false)
320 LOAD_TEMPLATE("admin_menu_failed", false, ADMINS_ACCESS_DENIED);
321 ADD_FATAL(ADMINS_ACCESS_DENIED);
325 // Include file not found! :-(
326 LOAD_TEMPLATE("admin_menu_failed", false, ADMIN_404_ACTION);
327 ADD_FATAL(ADMIN_404_ACTION_1.$act.ADMIN_404_ACTION_2);
330 // Invalid action/what pair found!
331 LOAD_TEMPLATE("admin_menu_failed", false, ADMIN_INVALID_ACTION);
332 ADD_FATAL(ADMIN_INVALID_ACTION_1.$act."/".$wht.ADMIN_INVALID_ACTION_2);
336 LOAD_TEMPLATE("admin_main_footer");
339 function ADD_ADMIN_MENU($act, $wht,$return=false)
341 global $menuDesription, $menuTitle;
345 $menuDesription = array();
346 $menuTitle = array();
349 $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__);
351 if (SQL_NUMROWS($result_main) > 0)
353 $OUT = "<TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"admin_menu_main\">
354 <TR><TD colspan=\"2\" height=\"7\" class=\"seperator\"> </TD></TR>\n";
355 while (list($menu, $title, $descr) = SQL_FETCHROW($result_main))
357 if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2"))
359 $ACL = ADMINS_CHECK_ACL($menu, "");
363 // ACL is "allow"... hmmm
370 // Insert compiled menu title and description
371 $menuTitle[$menu] = $title;
372 $menuDesription[$menu] = $descr;
375 <TD class=\"admin_menu\" colspan=\"2\">
376 <NOBR> <STRONG>·</STRONG> ";
377 if (($menu == $act) && (empty($wht)))
383 $OUT .= "[<A href=\"".URL."/modules.php?module=admin&action=".$menu."\">";
386 if (($menu == $act) && (empty($wht)))
394 $OUT .= "</NOBR></TD>
396 $result_what = SQL_QUERY_ESC("SELECT what, title, descr FROM "._MYSQL_PREFIX."_admin_menu WHERE action='%s' AND what != '' ORDER BY sort, id DESC",
397 array($menu), __FILE__, __LINE__);
398 if ((SQL_NUMROWS($result_what) > 0) && ($act == $menu))
400 $menuDesription = array();
401 $menuTitle = array(); $SUB = true;
403 <TD width=\"10\" class=\"seperator\"> </TD>
404 <TD class=\"admin_menu\">
405 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"admin_menu_sub\">\n";
406 while (list($wht_sub, $title_what, $desc_what) = SQL_FETCHROW($result_what))
409 $INC = sprintf("%sinc/modules/admin/what-%s.php", PATH, $wht_sub);
410 if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2"))
412 $ACL = ADMINS_CHECK_ACL("", $wht_sub);
416 // ACL is "allow"... hmmm
419 $readable = FILE_READABLE($INC);
422 // Insert compiled title and description
423 $menuTitle[$wht_sub] = $title_what;
424 $menuDesription[$wht_sub] = $desc_what;
426 <TD class=\"admin_menu\" colspan=\"2\">
427 <NOBR> <STRONG>--></STRONG> ";
430 if ($wht == $wht_sub)
436 $OUT .= "[<A href=\"".URL."/modules.php?module=admin&what=".$wht_sub."\">";
441 $OUT .= "<I class=\"admin_note\">";
446 if ($wht == $wht_sub)
459 $OUT .= "</NOBR></TD>
465 SQL_FREERESULT($result_what);
470 $OUT .= "<TR><TD height=\"7\" colspan=\"2\"></TD></TR>\n";
475 SQL_FREERESULT($result_main);
476 $OUT .= "</TABLE>\n";
479 // Compile and run the code here. This inserts all constants into the
480 // HTML output. Costs me some time to figure this out... *sigh* Quix0r
481 $eval = "\$OUT = \"".COMPILE_CODE(addslashes($OUT))."\";";
484 // Return or output content?
492 function ADD_MEMBER_SELECTION_BOX ($def="0", $add_all=false, $return=false, $none=false)
494 // Output selection form with all confirmed user accounts listed
495 $result = SQL_QUERY("SELECT userid, surname, family FROM "._MYSQL_PREFIX."_user_data ORDER BY userid", __FILE__, __LINE__);
498 // USe this only for adding points (e.g. adding refs really makes no sence ;-) )
499 if ($add_all) $OUT = " <OPTION value=\"all\">".ALL_MEMBERS."</OPTION>\n";
500 elseif ($none) $OUT = " <OPTION value=\"0\">".SELECT_NONE."</OPTION>\n";
501 while (list($id, $sname, $fname) = SQL_FETCHROW($result))
503 $OUT .= " <OPTION value=\"".$id."\"";
504 if ($def == $id) $OUT .= " selected=\"selected\"";
505 $OUT .= ">".$sname." ".$fname." (".$id.")</OPTION>\n";
509 SQL_FREERESULT($result);
512 // Remeber options in constant
513 define('_MEMBER_SELECTION', $OUT);
515 // Display selection box
516 define('__LANG_VALUE', GET_LANGUAGE());
519 LOAD_TEMPLATE("admin_member_selection_box", false, $GLOBALS['what']);
522 return "<select name=\"userid\" size=\"1\">\n".$OUT."</select>\n";
526 function ADMIN_MENU_SELECTION($MODE, $default="", $defid="") {
528 if ($MODE == "action") $wht = "(what='' OR what IS NULL) AND action !='login'";
529 $result = SQL_QUERY_ESC("SELECT %s, title FROM "._MYSQL_PREFIX."_admin_menu WHERE ".$wht." ORDER BY sort",
530 array($MODE), __FILE__, __LINE__);
531 if (SQL_NUMROWS($result) > 0) {
532 // Load menu as selection
533 $OUT = "<SELECT name=\"".$MODE."_menu";
534 if ((!empty($defid)) || ($defid == "0")) $OUT .= "[".$defid."]";
535 $OUT .= "\" size=\"1\" class=\"admin_select\">
536 <OPTION value=\"\">".SELECT_NONE."</OPTION>\n";
537 while (list($menu, $title) = SQL_FETCHROW($result)) {
538 $OUT .= " <OPTION value=\"".$menu."\"";
539 if ((!empty($default)) && ($default == $menu)) $OUT .= " selected=\"selected\"";
540 $OUT .= ">".$title."</OPTION>\n";
544 SQL_FREERESULT($result);
545 $OUT .= "</SELECT>\n";
548 $OUT = ADMIN_PROBLEM_NO_MENU;
554 // Save settings to the database
555 function ADMIN_SAVE_SETTINGS (&$POST, $tableName="_config", $whereStatement="config=0", $translateComma=array(), $alwaysAdd=false) {
556 global $_CONFIG, $cacheArray, $cacheInstance;
558 // Prepare all arrays, variables
562 // Now, walk through all entries and prepare them for saving
563 foreach ($POST as $id => $val) {
564 // Process only formular field but not submit buttons ;)
566 // Do not save the ok value
567 CONVERT_SELECTIONS_TO_TIMESTAMP($POST, $DATA, $id, $skip);
569 // Shall we process this ID? It muss not be empty, of course
570 if ((!$skip) && (!empty($id))) {
572 $val = COMPILE_CODE($val);
574 // Translate the value? (comma to dot!)
575 if ((is_array($translateComma)) && (in_array($id, $translateComma))) {
576 // Then do it here... :)
577 $val = str_replace(",", ".", $val);
580 // Shall we add numbers or strings?
582 if ("".$val."" == "".$test."") {
584 $DATA[] = $id."=".$val."";
587 $DATA[] = $id."='".trim($val)."'";
590 // Update current configuration
591 $_CONFIG[$id] = $val;
596 // Check if entry does exist
599 if (!empty($whereStatement)) {
600 $result = SQL_QUERY("SELECT * FROM "._MYSQL_PREFIX.$tableName." WHERE ".$whereStatement." LIMIT 1", __FILE__, __LINE__);
602 $result = SQL_QUERY("SELECT * FROM "._MYSQL_PREFIX.$tableName." LIMIT 1", __FILE__, __LINE__);
606 if (SQL_NUMROWS($result) == 1) {
607 // "Implode" all data to single string
608 $DATA_UPDATE = implode(", ", $DATA);
610 // Generate SQL string
611 $SQL = "UPDATE "._MYSQL_PREFIX.$tableName." SET ".$DATA_UPDATE." WHERE ".$whereStatement." LIMIT 1";
613 // Add Line (does only work with auto_increment!
614 $KEYs = array(); $VALUEs = array();
615 foreach ($DATA as $entry) {
617 $line = explode("=", $entry);
618 $KEYs[] = $line[0]; $VALUEs[] = $line[1];
621 // Add both in one line
622 $KEYs = implode(", ", $KEYs);
623 $VALUEs = implode(", ", $VALUEs);
625 // Generate SQL string
626 $SQL = "INSERT INTO "._MYSQL_PREFIX.$tableName." (".$KEYs.") VALUES(".$VALUEs.")";
630 SQL_FREERESULT($result);
632 // Simply run generated SQL string
633 $result = SQL_QUERY($SQL, __FILE__, __LINE__);
635 // Is the config table updated and the cache extension installed?
636 if ((GET_EXT_VERSION("cache") >= "0.1.2") && ($tableName == "_config")) {
638 if ($cacheInstance->cache_file("config", true)) $cacheInstance->cache_destroy();
643 LOAD_TEMPLATE("admin_settings_saved", false, "<STRONG class=\"admin_done\">".SETTINGS_SAVED."</STRONG>");
646 function ADMIN_MAKE_MENU_SELECTION($menu, $type, $name, $default="") {
647 // Open the requested menu directory
648 $handle = opendir(sprintf("%sinc/modules/%s/", PATH, $menu)) or mxchange_die("Cannot load menu ".$menu."!");
650 // Init the selection box
651 $OUT = "<SELECT name=\"".$name."\" class=\"admin_select\" size=\"1\">\n <OPTION value=\"\">".IS_TOP_MENU."</OPTION>\n";
653 // Walk through all files
654 while ($file = readdir($handle)) {
655 // Is this a PHP script?
656 if (($file != ".") && ($file != "..") && ($file != "lost+found") && (strpos($file, "".$type."-") > -1) && (strpos($file, ".php") > 0)) {
657 // Then test if the file is readable
658 $test = sprintf("%sinc/modules/%s/%s", PATH, $menu, $file);
659 if ((is_file($test)) && (is_readable($test))) {
660 // Extract the value for what=xxx
661 $part = substr($file, (strlen($type) + 1));
662 $part = substr($part, 0, -4);
664 // Is that part different from the overview?
665 if ($part != "overview") {
666 $OUT .= " <OPTION value=\"".$part."\"";
667 if ($part == $default) $OUT .= "selected";
668 $OUT .= ">".$part."</OPTION>\n";
674 // Close dir and selection box
676 $OUT .= "</SELECT>\n";
680 function ADMIN_USER_PROFILE_LINK($uid, $title="", $wht="list_user") {
681 if (($title == "") && ($title != "0")) {
682 // Set userid as title
686 if (($title == "0") && ($wht == "list_refs")) {
687 // Return title again
691 //* DEBUG: */ echo "A:".$title."<br />";
693 return "<A href=\"".URL."/modules.php?module=admin&what=".$wht."&u_id=".$uid."\" title=\"".ADMIN_USER_PROFILE_TITLE."\">".$title."</A>";
696 function ADMIN_CHECK_MENU_MODE() {
697 global $_CONFIG, $cacheArray;
699 // Set the global mode as the mode for all admins
700 $MODE = $_CONFIG['admin_menu']; $ADMIN = $MODE;
702 // Check individual settings of current admin
703 if (isset($cacheArray['admins']['la_mode'][get_session('admin_login')])) {
705 $ADMIN = $cacheArray['admins']['la_mode'][get_session('admin_login')];
706 $_CONFIG['cache_hits']++;
707 } elseif (GET_EXT_VERSION("admins") >= "0.6.7") {
708 // Load from database when version of "admins" is enough
709 $result = SQL_QUERY_ESC("SELECT la_mode FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
710 array(get_session('admin_login')), __FILE__, __LINE__);
711 if (SQL_NUMROWS($result) == 1) {
713 list($ADMIN) = SQL_FETCHROW($result);
717 SQL_FREERESULT($result);
720 // Check what the admin wants and set it when it's not the global mode
721 if ($ADMIN != "global") $MODE = $ADMIN;
723 // Return admin-menu's mode
726 // Change activation status
727 function ADMIN_CHANGE_ACTIVATION_STATUS ($IDs, $table, $row, $idRow = "id") {
729 $cnt = 0; $newStatus = "Y";
730 if ((is_array($IDs)) && (count($IDs) > 0)) {
731 // "Walk" all through and count them
732 foreach ($IDs as $id=>$selected) {
733 // Secure the ID number
734 $id = bigintval($id);
736 // Should always be 1 ;-)
737 if ($selected == 1) {
738 // Determine new status
739 $result = SQL_QUERY_ESC("SELECT %s FROM "._MYSQL_PREFIX."_%s WHERE %s=%s LIMIT 1",
740 array($row, $table, $idRow, $id), __FILE__, __LINE__);
743 if (SQL_NUMROWS($result) == 1) {
745 list($currStatus) = SQL_FETCHROW($result);
746 if ($currStatus == "Y") $newStatus='N'; else $newStatus = "Y";
748 // Change this status
749 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_%s SET %s='%s' WHERE %s=%s LIMIT 1",
750 array($table, $row, $newStatus, $idRow, $id), __FILE__, __LINE__);
752 // Count up affected rows
753 $cnt += SQL_AFFECTEDROWS();
757 SQL_FREERESULT($result);
762 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_STATUS_CHANGED_1.$cnt.ADMIN_STATUS_CHANGED_2.count($IDs).ADMIN_STATUS_CHANGED_3);
765 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_NOTHING_SELECTED_CHANGE);
768 // Build a special template list
769 function ADMIN_BUILD_LIST ($listType, $IDs, $table, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn) {
773 // "Walk" through all entries
774 foreach ($IDs as $id => $selected) {
776 $id = bigintval($id);
778 // Will always be 1 ;-)
779 if ($selected == 1) {
780 // Get result from a given column array and table name
781 $result = SQL_RESULT_FROM_ARRAY($table, $columns, $idColumn, $id);
783 // Is there one entry?
784 if (SQL_NUMROWS($result) == 1) {
786 $content = SQL_FETCHARRAY($result);
789 foreach ($content as $key => $value) {
791 $idx = array_search($key, $columns, true);
793 // Do we have a userid?
794 if ($key == "userid") {
795 // Add it again as raw id
796 $content['uid'] = bigintval($value);
799 // Handle the call in external function
800 $content[$key] = HANDLE_EXTRA_VALUES($filterFunctions[$idx], $value, $extraValues[$idx]);
803 // Add color switching
804 $content['sw'] = $SW;
807 $OUT .= LOAD_TEMPLATE("admin_".$listType."_".$table."_row", true, $content);
814 SQL_FREERESULT($result);
818 // Load master template
819 LOAD_TEMPLATE("admin_".$listType."_".$table."", false, $OUT);
821 // Delete rows by given ID numbers
822 function ADMIN_DELETE_ENTRIES_CONFIRM ($IDs, $table, $columns=array(), $filterFunctions=array(), $extraValues=array(), $deleteNow=false, $idColumn="id", $userIdColumn="userid") {
823 // All valid entries? (We hope so here!)
824 if ((is_array($IDs)) && (count($IDs) > 0) && (count($columns) == count($filterFunctions)) && (count($columns) == count($extraValues))) {
825 // Shall we delete here or list for deletion?
828 $SQL = "DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_%s WHERE %s IN (%s)";
830 foreach ($IDs as $id => $sel) {
831 // Is there a userid?
832 if (isset($_POST['uid'][$id])) {
833 // Load all data from that id
834 $result = SQL_QUERY_ESC("SELECT * FROM "._MYSQL_PREFIX."_%s WHERE %s=%s LIMIT 1",
835 array($table, $idColumn, $id), __FILE__, __LINE__);
838 $content = SQL_FETCHARRAY($result);
841 SQL_FREERESULT($result);
843 // Do we have a *real* userid?
844 if ($_POST['uid'][$id] > 0) {
846 $eval = "\$subject = MEMBER_DEL_".strtoupper($table)."_SUBJECT;";
849 // Load email template
850 $mail = LOAD_EMAIL_TEMPLATE("member_del_".$table, $content);
853 SEND_EMAIL($_POST['uid'][$id], $subject, $mail);
857 $eval = "\$subject = ADMIN_DEL_".strtoupper($table)."_SUBJECT;";
860 // Send admin notification out
861 SEND_ADMIN_NOTIFICATION($subject, "admin_del_".$table, $content, $_POST['uid'][$id]);
869 SQL_QUERY($SQL, array($table, $idColumn, substr($idList, 0, -1)), __FILE__, __LINE__);
872 if (SQL_AFFECTEDROWS() == count($IDs)) {
874 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_ALL_ENTRIES_REMOVED);
876 // Some are still there :(
877 LOAD_TEMPLATE("admin_settings_saved", false, sprintf(ADMIN_SOME_ENTRIES_NOT_DELETED, SQL_AFFECTEDROWS(), count($IDs)));
880 // List for deletion confirmation
881 ADMIN_BUILD_LIST("del", $IDs, $table, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn);
885 // Edit rows by given ID numbers
886 function ADMIN_EDIT_ENTRIES_CONFIRM ($IDs, $table, $columns=array(), $filterFunctions=array(), $extraValues=array(), $editNow=false, $idColumn="id", $userIdColumn="userid") {
887 // All valid entries? (We hope so here!)
888 if ((is_array($IDs)) && (count($IDs) > 0) && (count($columns) == count($filterFunctions)) && (count($columns) == count($extraValues))) {
889 // Shall we change here or list for editing?
895 ADMIN_BUILD_LIST("edit", $IDs, $table, $columns, $filterFunctions, $extraValues, $idColumn, $userIdColumn);
899 // Checks proxy settins by fetching check-updates3.php from www.mxchange.org
900 function ADMIN_TEST_PROXY_SETTINGS ($settingsArray) {
903 // By default they are invalid
906 // Set temporary the new settings
907 $_CONFIG = array_merge($_CONFIG, $settingsArray);
909 // Now get the test URL
910 $content = GET_URL("check-updates3.php");
912 // Is the first line with "200 OK"?
913 $valid = eregi("200 OK", $content[0]);
918 // Sends out a link to the given email adress so the admin can reset his/her password
919 function ADMIN_SEND_PASSWORD_RESET_LINK ($email) {
924 // Compile out security characters (must be for looking up!)
925 $email = COMPILE_CODE($email);
927 // Look up administator login
928 $result = SQL_QUERY_ESC("SELECT id, login, password FROM "._MYSQL_PREFIX."_admins WHERE email='%s' LIMIT 1",
929 array($email), __FILE__, __LINE__);
931 // Is there an account?
932 if (SQL_NUMROWS($result) == 0) {
934 return ADMIN_NO_LOGIN_WITH_EMAIL;
938 $content = SQL_FETCHARRAY($result);
941 SQL_FREERESULT($result);
943 // Generate hash for reset link
944 $content['hash'] = generateHash(URL.":".$content['id'].":".$content['login'].":".$content['password'], substr($content['password'], 10));
947 unset($content['id']);
948 unset($content['password']);
951 $mailText = LOAD_EMAIL_TEMPLATE("admin_reset_password", $content);
954 SEND_EMAIL($email, ADMIN_RESET_PASS_LINK_SUBJ, $mailText);
957 return ADMIN_RESET_LINK_SENT;
959 // Validate hash and login for password reset
960 function ADMIN_VALIDATE_RESET_LINK_HASH_LOGIN ($hash, $login) {
961 // By default nothing validates... ;)
964 // Compile the login for lookup
965 $login = COMPILE_CODE($login);
967 // Then try to find that user
968 $result = SQL_QUERY_ESC("SELECT id, password, email FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
969 array($login), __FILE__, __LINE__);
971 // Is an account here?
972 if (SQL_NUMROWS($result) == 1) {
974 $content = SQL_FETCHARRAY($result);
976 // Generate hash again
977 $hashFromData = generateHash(URL.":".$content['id'].":".$login.":".$content['password'], substr($content['password'], 10));
980 $valid = ($hash == $hashFromData);
984 SQL_FREERESULT($result);
989 // Reset the password for the login. Do NOT call this function without calling above function first!
990 function ADMIN_RESET_PASSWORD ($login, $password) {
994 // Now check if we have sql_patches installed
995 if (GET_EXT_VERSION("sql_patches") >= "0.3.6") {
996 // Use new way of hashing
997 $passHash = generateHash($password);
1000 $passHash = md5($password);
1004 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET password='%s' WHERE login='%s' LIMIT 1",
1005 array($passHash, $login), __FILE__, __LINE__);
1008 return ADMIN_PASSWORD_RESET_DONE;
1010 // Solves a task by given id number
1011 function ADMIN_SOLVE_TASK ($id) {
1012 // Update the task data
1013 ADMIN_UPDATE_TASK_DATA($id, "status", "SOLVED");
1015 // Marks a given task as deleted
1016 function ADMIN_DELETE_TASK ($id) {
1017 // Update the task data
1018 ADMIN_UPDATE_TASK_DATA($id, "status", "DELETED");
1020 // Function to update task data
1021 function ADMIN_UPDATE_TASK_DATA ($id, $row, $data) {
1023 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_task_system SET %s='%s' WHERE id=%s LIMIT 1",
1024 array($row, $data, bigintval($id)), __FILE__, __LINE__);