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