Fixes for db/cache counter
[mailer.git] / inc / modules / admin / overview-inc.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 07/13/2004 *
4  * ===============                              Last change: 08/02/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : overview-inc.php                                 *
8  * -------------------------------------------------------------------- *
9  * Short description : Output standard task management                  *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Standart-Aufgaben-Management ausgeben            *
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'])) || (!IS_ADMIN()))
36 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40
41 function OUTPUT_STANDARD_OVERVIEW(&$result_tasks)
42 {
43         global $cacheArray, $cacheArray;
44
45         // First check for solved and not assigned tasks and assign them to current admin
46         $result_task = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_task_system SET assigned_admin='%s' WHERE assigned_admin < 1 AND status != 'NEW'",
47          array(GET_ADMIN_ID(get_session('admin_login'))), __FILE__, __LINE__);
48
49         // We currently don't want to install an extension so let's find out if we need...
50         $EXT_LOAD_MODE = "register";
51         $JOBS_DONE = true;
52
53         // Open the extension directory
54         $handle = opendir(PATH."inc/extensions/") or mxchange_die("Cannot read extension directory!");
55         while ($file = readdir($handle)) {
56                 // Is this file an extension?
57                 if ((substr($file, 0, 4) == "ext-") && (substr($file, -4) == ".php")) {
58                         //* DEBUG: */ echo $file."<br />\n";
59                         // Possible newly installed extension found so we extract extension's name
60                         $ext = strtolower(substr($file, 4, -4)); // Keep always extension names on lower case!!!
61
62                         // Init variables
63                         $ext_ver = "";
64                         $result = false;
65
66                         // Check if extension is installed or not
67                         if ((is_array($cacheArray['extensions']['ext_version'])) && (isset($cacheArray['extensions']['ext_version'][$ext]))) {
68                                 // Load version from cache
69                                 if (!empty($cacheArray['extensions']['ext_version'][$ext])) {
70                                         // Extension is installed so we can get it's version number
71                                         $ext_ver = $cacheArray['extensions']['ext_version'][$ext];
72                                 } else {
73                                         // Extension is not installed so no version number was found
74                                         $ext_ver = "";
75                                 }
76
77                                 // Disable load from database
78                                 $result = false;
79                         } elseif (!EXT_IS_ACTIVE("cache")) {
80                                 // Load data from database
81                                 $result = SQL_QUERY_ESC("SELECT id, ext_version FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
82                                  array($ext), __FILE__, __LINE__);
83                         }
84
85                         // Is the extension not yet installed?
86                         if ((SQL_NUMROWS($result) == 0) && (empty($ext_ver))) {
87                                 // Generate subject line
88                                 $ext_subj = sprintf("[%s:]", $ext);
89
90                                 // Create a task for newly installed extension
91                                 CREATE_NEW_EXTENSION_TASK(GET_ADMIN_ID(get_session('admin_login')), $ext_subj, $ext);
92
93                                 // We maybe want to install an extension so let's test-drive it...
94                                 include(PATH."inc/extensions/".$file);
95                         } else {
96                                 // Maybe we want to update?
97                                 if ((empty($cacheArray['extensions']['ext_version'][$ext])) && (SQL_NUMROWS($result) == 1)) {
98                                         list($dummy, $ext_ver) = SQL_FETCHROW($result);
99                                         SQL_FREERESULT($result);
100                                 }
101
102                                 // Update extension
103                                 if (!empty($ext_ver)) EXTENSION_UPDATE($file, $ext, $ext_ver);
104
105                                 if (!empty($cacheArray['active_extensions'][$ext])) {
106                                         // Maybe we want to keept the current extension active?
107                                         if (($cacheArray['active_extensions'][$ext] == "Y") && (!EXT_IS_ACTIVE($ext))) {
108                                                 // Reactivate this extension!
109                                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='Y' WHERE ext_name='%s' LIMIT 1",
110                                                  array($ext), __FILE__, __LINE__);
111
112                                                 // Extension has been activated?
113                                                 if (SQL_AFFECTEDROWS() == 1) {
114                                                         // Then run all queries
115                                                         EXTENSION_RUN_SQLS(GET_EXT_ID($ext), "activate");
116                                                 } // END - if
117                                         } // END - if
118                                 }
119                         }
120                 }
121         }
122
123         // Close directory handle
124         closedir($handle);
125
126         // At last - but not least - check for own and new unassigned tasks...
127         $result_tasks = SQL_QUERY_ESC("SELECT id, assigned_admin, userid, task_type, subject, text, task_created
128 FROM "._MYSQL_PREFIX."_task_system
129 WHERE assigned_admin='%s' OR (assigned_admin='0' AND status='NEW')
130 ORDER BY userid DESC, task_type DESC, subject, task_created DESC",
131          array(GET_ADMIN_ID(get_session('admin_login'))), __FILE__, __LINE__);
132
133         if (SQL_NUMROWS($result_tasks) > 0) {
134                 // New jobs found!
135                 $JOBS_DONE = false;
136         }
137
138         // Free the result
139         SQL_FREERESULT($result);
140
141         // Return status
142         return $JOBS_DONE;
143 }
144
145 //
146 function OUTPUT_SELECTED_TASKS($_POST, $result_tasks) {
147         global $_CONFIG, $NOTES;
148         if ((isset($_POST['assign'])) && (count($_POST['task']) > 0)) {
149                 // Assign / do tasks
150                 $OUT = ""; $SW = 2;
151                 foreach ($_POST['task'] as $id=>$sel) {
152                         $result_task = SQL_QUERY_ESC("SELECT id, userid, task_type, subject, text, task_created, status, assigned_admin FROM "._MYSQL_PREFIX."_task_system WHERE id=%s AND (assigned_admin='%s' OR (assigned_admin='0' AND status='NEW')) LIMIT 1",
153                          array(bigintval($id), GET_ADMIN_ID(get_session('admin_login'))), __FILE__, __LINE__);
154                         if (SQL_NUMROWS($result_task) == 1) {
155                                 // Task is valid...
156                                 list($tid, $uid, $type, $subj, $text, $created, $status, $aid) = SQL_FETCHROW($result_task);
157                                 SQL_FREERESULT($result_task);
158
159                                 if ($aid == "0") {
160                                         // Assgin current admin to unassgigned task
161                                         $result_assign = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_task_system SET assigned_admin='%s' WHERE id=%s LIMIT 1",
162                                          array(GET_ADMIN_ID(get_session('admin_login')), bigintval($tid)), __FILE__, __LINE__);
163                                 }
164
165                                 $ADD = "";
166                                 if ($type == "SUPPORT_MEMBER") {
167                                         $mode = substr($text, 0, strpos($text, ":"));
168                                         $text = substr($text, strpos($text, ":") + 1);
169                                         $ADD = "<LI>".ADMIN_TASK_SUPPORT_MODE.": <STRONG>".$mode."</STRONG></LI>";
170                                 }
171
172                                 if ($uid > 0) {
173                                         $result_user = SQL_QUERY_ESC("SELECT sex, surname, family, email FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
174                                          array(bigintval($uid)), __FILE__, __LINE__);
175                                         if (SQL_NUMROWS($result_user) == 1)
176                                         {
177                                                 list($sex, $sname, $fname, $email) = SQL_FETCHROW($result_user);
178                                                 SQL_FREERESULT($result_user);
179                                                 $ADD = "<LI>".ADMIN_MEMBER_UID.": <STRONG>".ADMIN_USER_PROFILE_LINK($uid)." (<A href=\"".CREATE_EMAIL_LINK($email, "user_data")."\">".TRANSLATE_SEX($sex)." ".$sname." ".$fname."</A>)</STRONG></LI>";
180                                         }
181                                 }
182
183                                 // Compile and insert text from task into table template
184                                 $text = LOAD_TEMPLATE("admin_extensions_text", true, COMPILE_CODE($text));
185
186                                 // Initialize variables (no title for SQL commands by default)
187                                 $ext_name = "";
188                                 $title = TASK_NO_TITLE;
189
190                                 // Shall I list SQL commands assigned to an extension installation or update task?
191                                 if (((GET_EXT_VERSION("sql_patches") != '') && ($_CONFIG['verbose_sql'] == "Y")) || (!EXT_IS_ACTIVE("sql_patches"))) {
192                                         $ext_name = substr($subj, 1, strpos($subj, ":") - 1);
193                                         if ($type == "EXTENSION") {
194                                                 // Load SQL commands for registering
195                                                 $SQLs = EXTENSION_REGISTER($ext_name, $id, true);
196
197                                                 // Add notes to text
198                                                 $text .= $NOTES;
199
200                                                 // Set title
201                                                 $title = ADMIN_SQLS_EXECUTED_ON_REGISTER;
202                                         } elseif ($type == "EXTENSION_UPDATE") {
203                                                 // Load SQL commands for update (already done!)
204                                                 $ext_name = substr($ext_name, 7);
205                                                 $ext_name = substr($ext_name, 0, strpos($ext_name, "-"));
206                                                 $test = "[UPDATE-".$ext_name."-";
207                                                 $ext_ver = substr($subj, strlen($test));
208                                                 $ext_ver = substr($ext_ver, 0, strpos($ext_ver, ":"));
209
210                                                 // Load SQLs from file
211                                                 $SQLs = EXTENSION_UPDATE("ext-".$ext_name.".php", $ext_name, $ext_ver, true);
212
213                                                 // Add notes to text
214                                                 $text .= $NOTES;
215
216                                                 // Set title
217                                                 $title = ADMIN_SQLS_EXECUTED_ON_UPDATE;
218                                         } else {
219                                                 // Remove extension's name
220                                                 $ext_name = "";
221                                         }
222
223                                         // Add SQLs to a table
224                                         if (empty($SQLs)) $SQLs = array();
225                                         if (empty($title)) $title = "";
226                                         if ((!empty($ext_name)) && (GET_EXT_VERSION("sql_patches")) && ($_CONFIG['verbose_sql'] == "Y")) {
227                                                 // Add verbose SQL table
228                                                 $text .= EXTENSION_VERBOSE_TABLE($SQLs, $title, " class=\"admin_table top2 left2 right2\"", true, "100%")."<br />\n";
229                                         }
230                                 } else {
231                                         // Run SQL commands in dry mode but only return the notes
232                                         $SQLs = EXTENSION_UPDATE("ext-".$ext_name.".php", $ext_name, $ext_ver, true);
233                                         $text .= $NOTES;
234                                 }
235
236                                 // Prepare array for the template
237                                 $content = array(
238                                         'sw'        => $SW,
239                                         'subj'      => $subj,
240                                         'add'       => $ADD,
241                                         'text'      => $text,
242                                         'created'   => MAKE_DATETIME($created, "1"),
243                                         'extension' => $ext_name
244                                 );
245
246                                 // Load template
247                                 $OUT .= LOAD_TEMPLATE("admin_overview_row", true, $content);
248
249                                 // Which task do we actually have here?
250                                 switch ($type)
251                                 {
252                                 case "EXTENSION": // Install new extensions
253                                         $ext_name = substr($subj, 1, strpos($subj, ":") - 1);
254                                         $result_lines = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
255                                          array($ext_name), __FILE__, __LINE__);
256                                         $lines = SQL_NUMROWS($result_lines);
257                                         SQL_FREERESULT($result_lines);
258                                         if ($lines == "0") {
259                                                 // New extension found
260                                                 $OUT .= LOAD_TEMPLATE("admin_ext_reg_form", true, array(
261                                                         'id'       => bigintval($id),
262                                                         'ext_name' => $ext_name
263                                                 ));
264                                         } else {
265                                                 // Task is closed so nothing is todo
266                                                 $OUT .= "<FONT class=\"admin_failed\">".ADMIN_EXT_ALREADY_REGISTERED."</FONT>\n";
267
268                                                 // Close task but not already closes or deleted or update tasks
269                                                 if (($status != "CLOSED") && ($status != "DELETED") && ($type != "EXTENSION_UPDATE")) {
270                                                         ADMIN_SOLVE_TASK($tid);
271                                                 }
272                                         }
273                                         break;
274
275                                 case "EXTENSION_UPDATE":
276                                         // Extension updates are installed automatically
277                                         $OUT .= "<FONT class=\"admin_failed medium\">".ADMIN_EXTENSION_UPDATED."</FONT>\n";
278
279                                         // Close task
280                                         if (($status != "CLOSED") && ($status != "DELETED")) {
281                                                 // Solve the task
282                                                 ADMIN_SOLVE_TASK($tid);
283                                         }
284                                         break;
285
286                                 case "SUPPORT_MEMBER": // Assign on member's support request
287                                         switch ($mode)
288                                         {
289                                         default: // Unknown support mode
290                                                 $OUT .= "<FONT class=\"admin_failed medium\">".ADMIN_UNKNOWN_SUPPORT_MODE_1.$mode.ADMIN_UNKNOWN_SUPPORT_MODE_2."</FONT>\n";
291                                                 break;
292                                         }
293                                         break;
294
295                                 case "PAYOUT_REQUEST": // Payout requests
296                                         if (EXT_IS_ACTIVE("payout"))
297                                         {
298                                                 // Extension is installed so let him send a notification to the user
299                                                 $result_pay = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_user_payouts WHERE userid=%s AND payout_timestamp=%s LIMIT 1",
300                                                  array(bigintval($uid), bigintval($created)), __FILE__, __LINE__);
301                                                 list($pid) = SQL_FETCHROW($result_pay);
302                                                 SQL_FREERESULT($result_pay);
303
304                                                 if ((!empty($pid)) && ($pid > 0))
305                                                 {
306                                                         // Payout ID can be obtained
307                                                         $content = array(
308                                                                 'pid' => $pid,
309                                                                 'tid' => $tid,
310                                                         );
311                                                         $OUT .= LOAD_TEMPLATE("admin_payout_overview_form", true, $content);
312                                                 }
313                                                  else
314                                                 {
315                                                         // Problem obtaining payout ID
316                                                         $OUT .= "<FONT class=\"admin_failed medium\">".PAYOUT_OBTAIN_ID_FAILED."</FONT>\n";
317                                                 }
318                                         }
319                                          else
320                                         {
321                                                 // Extension is not installed
322                                                 $OUT .= "<FONT class=\"admin_failed medium\">".ADMIN_PAYOUT_NOT_INSTALLED."</FONT>\n";
323                                         }
324                                         break;
325
326                                 case "WERNIS_REQUEST": // Wernis requests
327                                         if (EXT_IS_ACTIVE("wernis"))
328                                         {
329                                                 // Extension is installed so let him send a notification to the user
330                                                 $result_pay = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_user_wernis WHERE userid=%s AND wernis_timestamp=%s LIMIT 1",
331                                                  array(bigintval($uid), bigintval($created)), __FILE__, __LINE__);
332                                                 list($pid) = SQL_FETCHROW($result_pay);
333                                                 SQL_FREERESULT($result_pay);
334
335                                                 if ((!empty($pid)) && ($pid > 0))
336                                                 {
337                                                         // Payout ID can be obtained
338                                                         $content = array(
339                                                                 'pid' => $pid,
340                                                                 'tid' => $tid,
341                                                         );
342                                                         $OUT .= LOAD_TEMPLATE("admin_wernis_overview_form", true, $content);
343                                                 }
344                                                  else
345                                                 {
346                                                         // Problem obtaining wernis ID
347                                                         $OUT .= "<FONT class=\"admin_failed medium\">".WERNIS_OBTAIN_ID_FAILED."</FONT>\n";
348                                                 }
349                                         }
350                                          else
351                                         {
352                                                 // Extension is not installed
353                                                 $OUT .= "<FONT class=\"admin_failed medium\">".ADMIN_WERNIS_NOT_INSTALLED."</FONT>\n";
354                                         }
355                                         break;
356
357                                 case "HOLIDAY_REQUEST": // Holiday requests
358                                         $OUT .= LOAD_TEMPLATE("admin_task_holiday", true, $uid);
359                                         break;
360
361                                 case "NL_UNSUBSCRIBE": // Newsletter unsubscriptions
362                                         $result = SQL_QUERY_ESC("SELECT nl_timespan FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
363                                          array(bigintval($uid)), __FILE__, __LINE__);
364                                         list($span) = SQL_FETCHROW($result);
365                                         SQL_FREERESULT($result);
366
367                                         if ($span > 0)
368                                         {
369                                                 // Undone unscubscribe request
370                                                 $content = array(
371                                                         'uid' => $uid,
372                                                         'id'  => $tid
373                                                 );
374                                                 $OUT .= LOAD_TEMPLATE("admin_newsletter_tsk", true, $content);
375                                         }
376                                          else
377                                         {
378                                                 // Already unsubscribed
379                                                 $OUT .= "<FONT class=\"admin_failed medium\">".ADMIN_NL_UNSUBSCRIBE_ALREADY."</FONT>\n";
380                                         }
381                                         break;
382
383                                 default: // Unknown task type
384                                         $OUT .= "<FONT class=\"admin_failed medium\">".ADMIN_UNKNOWN_TASK_TYPE_1.$type.ADMIN_UNKNOWN_TASK_TYPE_2.$id.ADMIN_UNKNOWN_TASK_TYPE_3."</FONT>\n";
385                                         break;
386                                 }
387                                 $OUT .= "  </TD>
388   <TD width=\"1%\" class=\"switch_sw".$SW." bottom2 right2\">&nbsp;</TD>
389 </TR>\n";
390                         }
391                         $SW = 3 - $SW;
392                 }
393                 define('__TASK_ROWS', $OUT);
394
395                 // Load final template
396                 LOAD_TEMPLATE("admin_overview_list");
397         }
398          else
399         {
400                 if ((isset($_POST['task'])) && ((sizeof($_POST['task']) > 0) || ($_POST['task'][0] == "1")))
401                 {
402                         // Only unassign / delete tasks when there are selected tasks posted
403                         if (!empty($_POST['unassign']))
404                         {
405                                 // Unassign from tasks
406                                 foreach ($_POST['task'] as $id=>$sel)
407                                 {
408                                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_task_system SET assigned_admin=0 WHERE id=%s AND assigned_admin=%s LIMIT 1",
409                                          array(bigintval($id), GET_ADMIN_ID(get_session('admin_login'))), __FILE__, __LINE__);
410                                 }
411                         }
412                          elseif (isset($_POST['del']))
413                         {
414                                 // Delete tasks
415                                 foreach ($_POST['task'] as $id=>$sel)
416                                 {
417                                         $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE id=%s AND assigned_admin IN (%s,0) LIMIT 1",
418                                          array(bigintval($id), GET_ADMIN_ID(get_session('admin_login'))), __FILE__, __LINE__);
419                                 }
420                         }
421
422                         // Update query
423                         $result_tasks = SQL_QUERY_ESC("SELECT id, assigned_admin, userid, task_type, subject, text, task_created FROM "._MYSQL_PREFIX."_task_system WHERE assigned_admin=%s OR (assigned_admin=0 AND status='NEW') ORDER BY task_created DESC",
424                          array(GET_ADMIN_ID(get_session('admin_login'))), __FILE__, __LINE__);
425                 }
426
427                 // There are uncompleted jobs!
428                 LOAD_TEMPLATE("admin_overview_header");
429                 $SW = 2;
430                 while (list($id, $admin, $uid, $type, $subj, $text, $created) = SQL_FETCHROW($result_tasks))
431                 {
432                         $infos = "---";
433                         if ($admin == "0")
434                         {
435                                 // No admin currently is assigned
436                                 $admin = "<FONT class=\"admin_note\">".ADMIN_NO_ADMIN_ASSIGNED."</FONT>";
437                         }
438                          else
439                         {
440                                 // Load admin's data
441                                 $login = GET_ADMIN_LOGIN($admin);
442                                 if ($login != "***")
443                                 {
444                                         // Admin found
445                                         $admin = "<A href=\"".URL."/modules.php?module=admin&amp;what=admins_contct&amp;admin=".$admin."\">".$login."</A>";
446                                 }
447                                  else
448                                 {
449                                         // Maybe deleted?
450                                         $admin = "<FONT class=\"admin_note\">".ADMIN_ID_404_1.$admin.ADMIN_ID_404_2."</FONT>";
451                                 }
452                         }
453                         $evl = "\$type_out = ADMIN_TASK_IS_".strtoupper($type).";";
454                         eval($evl);
455                         $type2 = substr($text, 0, strpos($text, ":"));
456                         // Generate infos
457                         switch ($type)
458                         {
459                         case "EXTENSION":
460                         case "EXTENSION_UPDATE":
461                                 $infos = substr($subj, 1, strpos($subj, ":") - 1);
462                                 break;
463                         }
464                         if ($uid > 0)
465                         {
466                                 // Member found otherwise it's a system task
467                                 $uid = ADMIN_USER_PROFILE_LINK($uid);
468                         }
469                          else
470                         {
471                                 $uid = "<I>".ADMIN_IS_SYSTEM_TASK."</I>";
472                         }
473
474                         $content = array(
475                                 'sw'      => $SW,
476                                 'id'      => $id,
477                                 'admin'   => $admin,
478                                 'infos'   => $infos,
479                                 'uid'     => $uid,
480                                 'type'    => $type_out,
481                                 'created' => MAKE_DATETIME($created, "2")
482                         );
483
484                         LOAD_TEMPLATE("admin_overview_list_rows", false, $content);
485                         $SW = 3 - $SW;
486                 }
487                 // Free memory
488                 SQL_FREERESULT($result_tasks);
489
490                 // Load footer template
491                 LOAD_TEMPLATE("admin_overview_footer");
492         }
493 }
494 //
495 ?>