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