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