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