ae5db92b5b7706f61a24a76be471bd80073c6475
[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 (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
36 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40
41 //
42 function REGISTER_ADMIN ($user, $md5)
43 {
44         $ret = "failed";
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         {
49                 // Ok, let's create the admin login
50                 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_admins (login, password, email) VALUES('%s', '%s', '".WEBMASTER."')",
51                  array($user, $md5), __FILE__, __LINE__);
52                 $ret = "done";
53         }
54          else
55         {
56                 // Free memory
57                 SQL_FREERESULT($result);
58
59                 // Login does already exist
60                 $ret = "already";
61         }
62         return $ret;
63 }
64 // Only be executed on login procedure!
65 function CHECK_ADMIN_LOGIN ($admin_login, $password)
66 {
67         global $cacheArray, $_CONFIG, $cacheInstance;
68         $ret = "404"; $pass = "";
69         if (!empty($cacheArray['admins']['aid'][$admin_login]))
70         {
71                 // Get password from cache
72                 $pass = $cacheArray['admins']['password'][$admin_login];
73                 $ret = "pass";
74                 $_CONFIG['cache_hits']++;
75         }
76          else
77         {
78                 // Get password from DB
79                 $result = SQL_QUERY_ESC("SELECT password FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
80                  array($admin_login), __FILE__, __LINE__);
81                 if (SQL_NUMROWS($result) == 1)
82                 {
83                         $ret = "pass";
84                         list($pass) = SQL_FETCHROW($result);
85                         SQL_FREERESULT($result);
86                 }
87         }
88
89         //* DEBUG: */ echo "*".$pass."/".$password."/".$ret."<br />";
90         if ((strlen($pass) == 32) && ($pass == md5($password)))
91         {
92                 // Generate new hash
93                 $pass = generateHash($password);
94                 if (($ret == "pass") && (GET_EXT_VERSION("sql_patches") < "0.3.6")) $ret = "done";
95         }
96          elseif ((GET_EXT_VERSION("sql_patches") < "0.3.6") || (GET_EXT_VERSION("sql_patches") == ""))
97         {
98                 // Old hashing way
99                 return $ret;
100         }
101
102         // Generate salt of password
103         define('__SALT', substr($pass, 0, -40));
104         $salt = __SALT;
105
106         // Check if password is same
107         if (($ret == "pass") && ($pass == generateHash($password, $salt)) && (!empty($salt)))
108         {
109                 // Update password
110                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_admins SET password='%s' WHERE login='%s' LIMIT 1",
111                  array($pass, $admin_login), __FILE__, __LINE__);
112
113                 // Shall I remove the cache file?
114                 if ((EXT_IS_ACTIVE("cache")) && ($cacheInstance != false))
115                 {
116                         if ($cacheInstance->cache_file("admins", true)) $cacheInstance->cache_destroy();
117                 }
118
119                 // Password matches!
120                 $ret = "done";
121         }
122          elseif ((empty($salt)) && ($ret == "pass"))
123         {
124                 // Something bad went wrong
125                 $ret = "failed";
126         }
127         return $ret;
128 }
129 // Only be executed on cookie checking
130 function CHECK_ADMIN_COOKIES ($admin_login, $password) {
131         global $cacheArray, $_CONFIG;
132
133         // Get login and password hash
134         $admin_login = SQL_ESCAPE(get_session($admin_login));
135         $password = SQL_ESCAPE(get_session($password));
136
137         $ret = "404"; $pass = "";
138         if (!empty($cacheArray['admins']['aid'][$admin_login])) {
139                 // Get password from cache
140                 $pass = $cacheArray['admins']['password'][$admin_login];
141                 $ret = "pass";
142                 $_CONFIG['cache_hits']++;
143         } else {
144                 // Get password from DB
145                 $result = SQL_QUERY_ESC("SELECT password FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
146                  array($admin_login), __FILE__, __LINE__);
147                 if (SQL_NUMROWS($result) == 1) {
148                         $ret = "pass";
149                         list($pass) = SQL_FETCHROW($result);
150                         SQL_FREERESULT($result);
151                 }
152         }
153
154         //* DEBUG: */ echo "*".$pass."/".$password."<br />";
155
156         // Check if password matches
157         if (($ret == "pass") && ((generatePassString($pass) == $password) || ($pass == $password))) {
158                 // Passwords matches!
159                 $ret = "done";
160         }
161
162         // Return result
163         return $ret;
164 }
165 //
166 function admin_WriteData ($file, $comment, $prefix, $suffix, $DATA, $seek=0)
167 {
168         $done = false;  $seek++; $found = false;
169         if (file_exists($file))
170         {
171                 $search = "CFG: ".$comment;
172                 $tmp = $file.".tmp";
173                 $fp = fopen($file, 'r') or OUTPUT_HTML("<STRONG>READ:</STRONG> ".$file."<br />");
174                 if ($fp)
175                 {
176                         $fp_tmp = fopen($tmp, 'w') or OUTPUT_HTML("<STRONG>WRITE:</STRONG> ".$tmp."<br />");
177                         if ($fp_tmp)
178                         {
179                                 while (! feof($fp))
180                                 {
181                                         $line = fgets ($fp, 1024);
182                                         if (strpos($line, $search) > -1) { $next = 0; $found = true; }
183                                         if ($next > -1)
184                                         {
185                                                 if ($next == $seek)
186                                                 {
187                                                         $next = -1;
188                                                         $line = $prefix . $DATA . $suffix."\n";
189                                                 }
190                                                  else
191                                                 {
192                                                         $next++;
193                                                 }
194                                         }
195                                         fputs($fp_tmp, $line);
196                                 }
197                                 fclose($fp_tmp);
198                                 // Finished writing tmp file
199                                 $done = true;
200                         }
201                         fclose($fp);
202                         if (($done) && ($found))
203                         {
204                                 // Copy back tmp file and delete tmp :-)
205                                 @copy($tmp, $file);
206                                 @unlink($tmp);
207                                 define ('_FATAL', false);
208                         }
209                          elseif (!$found)
210                         {
211                                 OUTPUT_HTML("<STRONG>CHANGE:</STRONG> 404!");
212                                 define ('_FATAL', true);
213                         }
214                          else
215                         {
216                                 OUTPUT_HTML("<STRONG>TMP:</STRONG> UNDONE!");
217                                 define ('_FATAL', true);
218                         }
219                 }
220         }
221          else
222         {
223                 OUTPUT_HTML("<STRONG>404:</STRONG> ".$file."<br />");
224         }
225 }
226 //
227 function ADMIN_DO_ACTION($wht)
228 {
229         global $menuDesription, $menuTitle, $_CONFIG, $cacheArray, $link, $DATA;
230         //* DEBUG: */ echo __LINE__."*".$wht."/".$GLOBALS['module']."/".$GLOBALS['action']."/".$GLOBALS['what']."*<br />\n";
231         if (EXT_IS_ACTIVE("cache"))
232         {
233                 // Include cache instance
234                 global $cacheInstance;
235         }
236
237         // Remove any spaces from variable
238         if (empty($wht))
239         {
240                 // Default admin action is the overview page
241                 $wht = "overview";
242         }
243          else
244         {
245                 // Compile out some chars
246                 $wht = COMPILE_CODE($wht, false, false, false);
247         }
248
249         // Get action value
250         $act = GET_ACTION($GLOBALS['module'], $wht);
251
252         // Define admin login name and ID number
253         define('__ADMIN_LOGIN', SQL_ESCAPE(get_session('admin_login')));
254         define('__ADMIN_ID'   , GET_ADMIN_ID(get_session('admin_login')));
255
256         // Preload templates
257         if (EXT_IS_ACTIVE("admins")) {
258                 define('__ADMIN_WELCOME', LOAD_TEMPLATE("admin_welcome_admins", true));
259         } else {
260                 define('__ADMIN_WELCOME', LOAD_TEMPLATE("admin_welcome", true));
261         }
262         define('__ADMIN_FOOTER' , LOAD_TEMPLATE("admin_footer" , true));
263         define('__ADMIN_MENU'   , ADD_ADMIN_MENU($act, $wht, true));
264
265         // Tableset header
266         LOAD_TEMPLATE("admin_main_header");
267
268         // Check if action/what pair is valid
269         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_admin_menu
270 WHERE action='%s' AND ((what='%s' AND what != 'overview') OR (what='' AND '%s'='overview'))
271 LIMIT 1", array($act, $wht, $wht), __FILE__, __LINE__);
272         if (SQL_NUMROWS($result) == 1)
273         {
274                 // Free memory
275                 SQL_FREERESULT($result);
276
277                 // Is valid but does the inlcude file exists?
278                 $INC = sprintf(PATH."inc/modules/admin/action-%s.php", $act);
279                 if ((file_exists($INC)) && (is_readable($INC)) && (VALIDATE_MENU_ACTION("admin", $act, $wht)) && (__ACL_ALLOW == true))
280                 {
281                         // Ok, we finally load the admin action module
282                         include($INC);
283                 }
284                  elseif (__ACL_ALLOW == false)
285                 {
286                         // Access denied
287                         LOAD_TEMPLATE("admin_menu_failed", false, ADMINS_ACCESS_DENIED);
288                         ADD_FATAL(ADMINS_ACCESS_DENIED);
289                 }
290                  else
291                 {
292                         // Include file not found! :-(
293                         LOAD_TEMPLATE("admin_menu_failed", false, ADMIN_404_ACTION);
294                         ADD_FATAL(ADMIN_404_ACTION_1.$act.ADMIN_404_ACTION_2);
295                 }
296         } else {
297                 // Invalid action/what pair found!
298                 LOAD_TEMPLATE("admin_menu_failed", false, ADMIN_INVALID_ACTION);
299                 ADD_FATAL(ADMIN_INVALID_ACTION_1.$act."/".$wht.ADMIN_INVALID_ACTION_2);
300         }
301
302         // Tableset footer
303         LOAD_TEMPLATE("admin_main_footer");
304 }
305 //
306 function ADD_ADMIN_MENU($act, $wht,$return=false)
307 {
308         global $menuDesription, $menuTitle, $link;
309         $SUB = false;
310
311         // Menu descriptions
312         $menuDesription = array();
313         $menuTitle = array();
314
315         // Build main menu
316         $result_main = SQL_QUERY("SELECT action, title, descr FROM "._MYSQL_PREFIX."_admin_menu WHERE what='' ORDER BY sort, id DESC", __FILE__, __LINE__);
317         $OUT = "";
318         if (SQL_NUMROWS($result_main) > 0)
319         {
320                 $OUT = "<TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"admin_menu_main\">
321 <TR><TD colspan=\"2\" height=\"7\" class=\"seperator\">&nbsp;</TD></TR>\n";
322                 while (list($menu, $title, $descr) = SQL_FETCHROW($result_main))
323                 {
324                         if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2"))
325                         {
326                                 $ACL = ADMINS_CHECK_ACL($menu, "");
327                         }
328                          else
329                         {
330                                 // ACL is "allow"... hmmm
331                                 $ACL = true;
332                         }
333                         if ($ACL)
334                         {
335                                 if (!$SUB)
336                                 {
337                                         // Insert compiled menu title and description
338                                         $menuTitle[$menu]        = $title;
339                                         $menuDesription[$menu] = $descr;
340                                 }
341                                 $OUT .= "<TR>
342         <TD class=\"admin_menu\" colspan=\"2\">
343                 <NOBR>&nbsp;<STRONG>&middot;</STRONG>&nbsp;";
344                                         if (($menu == $act) && (empty($wht)))
345                                 {
346                                         $OUT .= "<STRONG>";
347                                 }
348                                  else
349                                 {
350                                         $OUT .= "[&nbsp;<A href=\"".URL."/modules.php?module=admin&amp;action=".$menu."\">";
351                                 }
352                                 $OUT .= $title;
353                                         if (($menu == $act) && (empty($wht)))
354                                 {
355                                         $OUT .= "</STRONG>";
356                                 }
357                                  else
358                                 {
359                                         $OUT .= "</A>&nbsp;]";
360                                 }
361                                 $OUT .= "</NOBR></TD>
362 </TR>\n";
363                                 $result_what = SQL_QUERY_ESC("SELECT what, title, descr FROM "._MYSQL_PREFIX."_admin_menu WHERE action='%s' AND what != '' ORDER BY sort, id DESC",
364                                  array($menu), __FILE__, __LINE__);
365                                 if ((SQL_NUMROWS($result_what) > 0) && ($act == $menu))
366                                 {
367                                         $menuDesription = array();
368                                         $menuTitle = array(); $SUB = true;
369                                         $OUT .= "<TR>
370         <TD width=\"10\" class=\"seperator\">&nbsp;</TD>
371         <TD class=\"admin_menu\">
372                 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"admin_menu_sub\">\n";
373                                                 while (list($wht_sub, $title_what, $desc_what) = SQL_FETCHROW($result_what))
374                                         {
375                                                 // Filename
376                                                 $INC = sprintf(PATH."inc/modules/admin/what-%s.php", $wht_sub);
377                                                 if ((EXT_IS_ACTIVE("admins")) && (GET_EXT_VERSION("admins") > "0.2"))
378                                                 {
379                                                         $ACL = ADMINS_CHECK_ACL("", $wht_sub);
380                                                 }
381                                                  else
382                                                 {
383                                                         // ACL is "allow"... hmmm
384                                                         $ACL = true;
385                                                 }
386                                                 $readable = ((file_exists($INC)) && (is_readable($INC)));
387                                                 if ($ACL)
388                                                 {
389                                                         // Insert compiled title and description
390                                                         $menuTitle[$wht_sub]        = $title_what;
391                                                         $menuDesription[$wht_sub] = $desc_what;
392                                                         $OUT .= "<TR>
393         <TD class=\"admin_menu\" colspan=\"2\">
394                 <NOBR>&nbsp;<STRONG>--&gt;</STRONG>&nbsp;";
395                                                         if ($readable)
396                                                         {
397                                                                 if ($wht == $wht_sub)
398                                                                 {
399                                                                         $OUT .= "<STRONG>";
400                                                                 }
401                                                                  else
402                                                                 {
403                                                                         $OUT .= "[&nbsp;<A href=\"".URL."/modules.php?module=admin&amp;what=".$wht_sub."\">";
404                                                                 }
405                                                         }
406                                                          else
407                                                         {
408                                                                 $OUT .= "<I class=\"admin_note\">";
409                                                         }
410                                                         $OUT .= $title_what;
411                                                         if ($readable)
412                                                         {
413                                                                 if ($wht == $wht_sub)
414                                                                 {
415                                                                         $OUT .= "</STRONG>";
416                                                                 }
417                                                                  else
418                                                                 {
419                                                                         $OUT .= "</A>&nbsp;]";
420                                                                 }
421                                                         }
422                                                          else
423                                                         {
424                                                                 $OUT .= "</I>";
425                                                         }
426                                                         $OUT .= "</NOBR></TD>
427 </TR>\n";
428                                                 }
429                                         }
430
431                                         // Free memory
432                                         SQL_FREERESULT($result_what);
433                                         $OUT .= "    </TABLE>
434         </TD>
435 </TR>\n";
436                                 }
437                                 $OUT .= "<TR><TD height=\"7\" colspan=\"2\"></TD></TR>\n";
438                         }
439                 }
440
441                 // Free memory
442                 SQL_FREERESULT($result_main);
443                 $OUT .= "</TABLE>\n";
444         }
445
446         // Compile and run the code here. This inserts all constants into the
447         // HTML output. Costs me some time to figure this out... *sigh* Quix0r
448         $eval = "\$OUT = \"".COMPILE_CODE(addslashes($OUT))."\";";
449         eval($eval);
450
451         // Return or output content?
452         if ($return) {
453                 return $OUT;
454         } else {
455                 OUTPUT_HTML($OUT);
456         }
457 }
458 //
459 function ADD_MEMBER_SELECTION_BOX($add_all = false, $return = false, $none = false, $def = "0")
460 {
461         // Output selection form with all confirmed user accounts listed
462         $result = SQL_QUERY("SELECT userid, surname, family FROM "._MYSQL_PREFIX."_user_data ORDER BY userid", __FILE__, __LINE__);
463         $OUT = "";
464
465         // USe this only for adding points (e.g. adding refs really makes no sence ;-) )
466         if ($add_all) $OUT = "      <OPTION value=\"all\">".ALL_MEMBERS."</OPTION>\n";
467          elseif ($none) $OUT = "      <OPTION value=\"0\">".SELECT_NONE."</OPTION>\n";
468         while (list($id, $sname, $fname) = SQL_FETCHROW($result))
469         {
470                 $OUT .= "      <OPTION value=\"".$id."\"";
471                 if ($def == $id) $OUT .= " selected=\"selected\"";
472                 $OUT .= ">".$sname." ".$fname." (".$id.")</OPTION>\n";
473         }
474
475         // Free memory
476         SQL_FREERESULT($result);
477
478         // Remeber options in constant
479         define('_MEMBER_SELECTION', $OUT);
480
481         if (!$return) {
482                 // Display selection box
483                 define('__LANG_VALUE', GET_LANGUAGE());
484
485                 // Load template
486                 LOAD_TEMPLATE("admin_member_selection_box", false, $GLOBALS['what']);
487         }
488 }
489 //
490 function ADMIN_MENU_SELECTION($MODE, $default="", $defid="") {
491         $wht = "what != ''";
492         if ($MODE == "action") $wht = "what='' AND action !='login'";
493         $result = SQL_QUERY_ESC("SELECT %s, title FROM "._MYSQL_PREFIX."_admin_menu WHERE ".$wht." ORDER BY sort",
494          array($MODE), __FILE__, __LINE__);
495         if (SQL_NUMROWS($result) > 0)
496         {
497                 // Load menu as selection
498                 $OUT = "<SELECT name=\"".$MODE."_menu";
499                 if ((!empty($defid)) || ($defid == "0")) $OUT .= "[".$defid."]";
500                 $OUT .= "\" size=\"1\" class=\"admin_select\">
501         <OPTION value=\"\">".SELECT_NONE."</OPTION>\n";
502                 while (list($menu, $title) = SQL_FETCHROW($result))
503                 {
504                         $OUT .= "  <OPTION value=\"".$menu."\"";
505                         if ((!empty($default)) && ($default == $menu)) $OUT .= " selected=\"selected\"";
506                         $OUT .= ">".$title."</OPTION>\n";
507                 }
508
509                 // Free memory
510                 SQL_FREERESULT($result);
511                 $OUT .= "</SELECT>\n";
512         }
513          else
514         {
515                 // No menus???
516                 $OUT = ADMIN_PROBLEM_NO_MENU;
517         }
518
519         // Return output
520         return $OUT;
521 }
522 //
523 function ADMIN_SAVE_SETTINGS (&$POST, $tableName="_config", $whereStatement="config=0", $translateComma = array(), $alwaysAdd=false)
524 {
525         global $_CONFIG, $cacheArray, $cacheInstance;
526         $DATA = array();
527         $skip = false; $TEST2 = "";
528         foreach ($POST as $id=>$val) {
529                 // Process only formular field but not submit buttons ;)
530                 if ($id != "ok") {
531                         // Do not save the ok value
532                         $TEST = substr($id, -3);
533                         if ((($TEST == "_ye") || ($TEST == "_mo") || ($TEST == "_we") || ($TEST == "_da") || ($TEST == "_ho") || ($TEST == "_mi") || ($TEST == "_se")) && (isset($val))) {
534                                 // Found a multi-selection for timings?
535                                 $TEST = substr($id, 0, -3);
536                                 if ((isset($POST[$TEST."_ye"])) && (isset($POST[$TEST."_mo"])) && (isset($POST[$TEST."_we"])) && (isset($POST[$TEST."_da"])) && (isset($POST[$TEST."_ho"])) && (isset($POST[$TEST."_mi"])) && (isset($POST[$TEST."_se"])) && ($TEST != $TEST2)) {
537                                         // Generate timestamp
538                                         $POST[$TEST] = CREATE_TIMESTAMP_FROM_SELECTIONS($TEST, $POST);
539                                         $DATA[] = "$TEST='".$POST[$TEST]."'";
540
541                                         // Remove data from array
542                                         unset($POST[$TEST."_ye"]);
543                                         unset($POST[$TEST."_mo"]);
544                                         unset($POST[$TEST."_we"]);
545                                         unset($POST[$TEST."_da"]);
546                                         unset($POST[$TEST."_ho"]);
547                                         unset($POST[$TEST."_mi"]);
548                                         unset($POST[$TEST."_se"]);
549
550                                         // Skip adding
551                                         unset($id); $skip = true; $TEST2 = $TEST;
552                                 }
553                         } else {
554                                 // Process this entry
555                                 $skip = false; $TEST2 = "";
556                         }
557
558                         // Shall we process this ID? It muss not be empty, of course
559                         if ((!$skip) && (!empty($id))) {
560                                 // Save this entry
561                                 $val = COMPILE_CODE($val);
562
563                                 // Translate the value? (comma to dot!)
564                                 if ((is_array($translateComma)) && (in_array($id, $translateComma))) {
565                                         // Then do it here... :)
566                                         $val = str_replace(",", ".", $val);
567                                 }
568
569                                 // Shall we add numbers or strings?
570                                 $test = (float)$val;
571                                 if ("".$val."" == "".$test."") {
572                                         // Add numbers
573                                         $DATA[] = $id."=".$val."";
574                                 } else {
575                                         // Add strings
576                                         $DATA[] = $id."='".trim($val)."'";
577                                 }
578
579                                 // Update current configuration
580                                 $_CONFIG[$id] = $val;
581                         }
582                 }
583         }
584
585         // Check if entry does exist
586         $result = false;
587         if (!$alwaysAdd) {
588                 if (!empty($whereStatement)) {
589                         $result = SQL_QUERY("SELECT * FROM "._MYSQL_PREFIX.$tableName." WHERE ".$whereStatement." LIMIT 1", __FILE__, __LINE__);
590                 } else {
591                         $result = SQL_QUERY("SELECT * FROM "._MYSQL_PREFIX.$tableName." LIMIT 1", __FILE__, __LINE__);
592                 }
593         }
594
595         if (SQL_NUMROWS($result) == 1) {
596                 // "Implode" all data to single string
597                 $DATA_UPDATE = implode(", ", $DATA);
598
599                 // Generate SQL string
600                 $SQL = "UPDATE "._MYSQL_PREFIX.$tableName." SET ".$DATA_UPDATE." WHERE ".$whereStatement." LIMIT 1";
601         } else {
602                 // Add Line (does only work with auto_increment!
603                 $KEYs = array(); $VALUEs = array();
604                 foreach ($DATA as $entry) {
605                         // Split up
606                         $line = explode("=", $entry);
607                         $KEYs[] = $line[0]; $VALUEs[] = $line[1];
608                 }
609
610                 // Add both in one line
611                 $KEYs = implode(", ", $KEYs);
612                 $VALUEs = implode(", ", $VALUEs);
613
614                 // Generate SQL string
615                 $SQL = "INSERT INTO "._MYSQL_PREFIX.$tableName." (".$KEYs.") VALUES(".$VALUEs.")";
616         }
617
618         // Free memory
619         SQL_FREERESULT($result);
620
621         // Simply run generated SQL string
622         $result = SQL_QUERY($SQL, __FILE__, __LINE__);
623
624         // Is the config table updated and the cache extension installed?
625         if ((GET_EXT_VERSION("cache") >= "0.1.2") && ($tableName == "_config")) {
626                 // Remove it here...
627                 if ($cacheInstance->cache_file("config", true)) $cacheInstance->cache_destroy();
628                 unset($cacheArray);
629         }
630
631         // Settings saved
632         LOAD_TEMPLATE("admin_settings_saved", false, "<STRONG class=\"admin_done\">".SETTINGS_SAVED."</STRONG>");
633 }
634 //
635 function ADMIN_MAKE_MENU_SELECTION($menu, $type, $name, $default="") {
636         // Init the selection box
637         $OUT = "<SELECT name=\"".$name."\" class=\"admin_select\" size=\"1\">\n <OPTION value=\"\">".IS_TOP_MENU."</OPTION>\n";
638
639         // Open the requested menu directory
640         $handle = opendir(PATH."inc/modules/".$menu."/") or mxchange_die("Cannot load menu ".$menu."!");
641         while ($file = readdir($handle)) {
642                 // Is this a PHP script?
643                 if (($file != ".") && ($file != "..") && ($file != "lost+found") && (strpos($file, "".$type."-") > -1) && (strpos($file, ".php") > 0)) {
644                         // Then test if the file is readable
645                         $test = PATH."inc/modules/".$menu."/".$file;
646                         if (is_readable($test)) {
647                                 // Extract the value for what=xxx
648                                 $part = substr($file, (strlen($type) + 1)); $part = substr($part, 0, strpos($part, ".php"));
649
650                                 // Is that part different from the overview?
651                                 if ($part != "overview") {
652                                         $OUT .= "       <OPTION value=\"".$part."\"";
653                                         if ($part == $default) $OUT .= "selected";
654                                         $OUT .= ">".$part."</OPTION>\n";
655                                 }
656                         }
657                 }
658         }
659         closedir($handle);
660         $OUT .= "</SELECT>\n";
661         return $OUT;
662 }
663 //
664 function ADMIN_USER_PROFILE_LINK($uid, $title="", $wht="list_user")
665 {
666         if (($title == "") && ($title != "0")) { $title = $uid; }
667         if (($title == "0") && ($wht == "list_refs"))
668         {
669                 // Return title again
670                 return $title;
671         }
672
673         //* DEBUG: */ echo "A:".$title."<br />";
674         // Return link
675         return "<A href=\"".URL."/modules.php?module=admin&amp;what=".$wht."&amp;u_id=".$uid."\" title=\"".ADMIN_USER_PROFILE_TITLE."\">".$title."</A>";
676 }
677 //
678 function ADMIN_CHECK_MENU_MODE()
679 {
680         global $_CONFIG, $cacheArray;
681
682         // Set the global mode as the mode for all admins
683         $MODE = $_CONFIG['admin_menu']; $ADMIN = $MODE;
684
685         // Check individual settings of current admin
686         if (isset($cacheArray['admins']['la_mode'][get_session('admin_login')]))
687         {
688                 // Load from cache
689                 $ADMIN = $cacheArray['admins']['la_mode'][get_session('admin_login')];
690                 $_CONFIG['cache_hits']++;
691         }
692          elseif (GET_EXT_VERSION("admins") >= "0.6.7")
693         {
694                 // Load from database when version of "admins" is enough
695                 $result = SQL_QUERY_ESC("SELECT la_mode FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
696                  array(get_session('admin_login')), __FILE__, __LINE__);
697                 if (SQL_NUMROWS($result) == 1)
698                 {
699                         // Load data
700                         list($ADMIN) = SQL_FETCHROW($result);
701                 }
702
703                 // Free memory
704                 SQL_FREERESULT($result);
705         }
706
707         // Check what the admin wants and set it when it's not the global mode
708         if ($ADMIN != "global") $MODE = $ADMIN;
709
710         // Return admin-menu's mode
711         return $MODE;
712 }
713 // Change activation status
714 function ADMIN_CHANGE_ACTIVATION_STATUS ($IDs, $table, $row, $idRow = "id") {
715         global $_CONFIG;
716         $cnt = 0; $newStatus = 'Y';
717         if ((is_array($IDs)) && (count($IDs) > 0)) {
718                 // "Walk" all through and count them
719                 foreach ($IDs as $id=>$selected) {
720                         // Secure the ID number
721                         $id = bigintval($id);
722
723                         // Should always be 1 ;-)
724                         if ($selected == 1) {
725                                 // Determine new status
726                                 $result = SQL_QUERY_ESC("SELECT %s FROM "._MYSQL_PREFIX."_%s WHERE %s=%d LIMIT 1",
727                                         array($row, $table, $idRow, $id), __FILE__, __LINE__);
728
729                                 // Row found?
730                                 if (SQL_NUMROWS($result) == 1) {
731                                         // Load the status
732                                         list($currStatus) = SQL_FETCHROW($result);
733                                         if ($currStatus == 'Y') $newStatus='N'; else $newStatus = 'Y';
734
735                                         // Change this status
736                                         SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_%s SET %s='%s' WHERE %s=%d LIMIT 1",
737                                                 array($table, $row, $newStatus, $idRow, $id), __FILE__, __LINE__);
738
739                                         // Count up affected rows
740                                         $cnt += SQL_AFFECTEDROWS();
741                                 }
742
743                                 // Free the result
744                                 SQL_FREERESULT($result);
745                         }
746                 }
747
748                 // Output status
749                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_STATUS_CHANGED_1.$cnt.ADMIN_STATUS_CHANGED_2.count($IDs).ADMIN_STATUS_CHANGED_3);
750         } else {
751                 // Nothing selected!
752                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_NOTHING_SELECTED_CHANGE);
753         }
754 }
755 // Delete rows by given ID numbers
756 function ADMIN_DELETE_ENTRIES_CONFIRM ($IDs, $table, $row, $columns = array(), $filterFunctions = array(), $deleteNow=false, $idRow="id") {
757         global $_CONFIG;
758         $OUT = ""; $SW = 2;
759         if ((is_array($IDs)) && (count($IDs) > 0)) {
760                 // "Walk" through all entries and count them
761                 if ($deleteNow) {
762                         // Delete them
763                 } else {
764                         // List for confirmation
765                         foreach ($IDs as $id=>$selected) {
766                                 // Secure ID number
767                                 $id = bigintval($id);
768
769                                 // Will always be 1 ;-)
770                                 if ($selected == 1) {
771                                         // Get result from a given column array and table name
772                                         $result = SQL_RESULT_FROM_ARRAY($table, $columns, $idRow, $id);
773
774                                         // Is there one entry?
775                                         if (SQL_NUMROWS($result) == 1) {
776                                                 // Load all data
777                                                 $content = SQL_FETCHARRAY($result);
778
779                                                 // Filter all data
780                                                 foreach ($content as $key=>$value) {
781                                                         // Is a filter function set?
782                                                         $idx = array_search($key, $columns, true);
783                                                         if (!empty($filterFunctions[$idx])) {
784                                                                 // Then call it!
785                                                                 $content[$key] = call_user_func($filterFunctions[$idx], $value);
786                                                         }
787                                                 }
788
789                                                 // Add color switching
790                                                 $content['sw'] = $SW;
791
792                                                 // Then list it again...
793                                                 $OUT .= LOAD_TEMPLATE("admin_del_".$table."_row", true, $content);
794                                                 $SW = 3 - $SW;
795                                         }
796
797                                         // Free the result
798                                         SQL_FREERESULT($result);
799                                 }
800                         }
801
802                         // Load master template
803                         LOAD_TEMPLATE("admin_del_".$table."", false, $OUT);
804                 }
805         }
806 }
807 //
808 ?>