Should not be loadInclude() here, we need to rewrite EXT_LOAD_MODE and EXT_VER here
[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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if ((!defined('__SECURITY')) || (!IS_ADMIN())) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
42         require($INC);
43 }
44
45 function OUTPUT_STANDARD_OVERVIEW (&$result_tasks) {
46         // First check for solved and not assigned tasks and assign them to current admin
47         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET `assigned_admin`=%s WHERE assigned_admin < 1 AND status != 'NEW'",
48                 array(getCurrentAdminId()), __FILE__, __LINE__);
49
50         // We currently don't want to install an extension so let's find out if we need...
51         $EXT_LOAD_MODE = 'register';
52         $jobsDone = true;
53
54         // Open the extension directory
55         $extensionList = getArrayFromDirectory('inc/extensions/', 'ext-', false, false);
56         foreach ($extensionList as $file) {
57                 // Only file name is required... :(
58                 $file = basename($file);
59
60                 // Is this file an extension?
61                 if ((substr($file, 0, 4) == 'ext-') && (substr($file, -4) == '.php')) {
62                         // Possible newly installed extension found so we extract extension's name
63                         $ext_name = strtolower(substr($file, 4, -4)); // Keep always extension names on lower case!!!
64
65                         // Init variables
66                         $result = false;
67
68                         // Check if extension is installed or not
69                         $ext_ver = GET_EXT_VERSION($ext_name);
70                         //* DEBUG: */ OUTPUT_HTML($ext_name."=".$ext_ver."<br />");
71
72                         // Is the extension not yet installed?
73                         if (empty($ext_ver)) {
74                                 // Generate subject line
75                                 $ext_subj = sprintf("[%s:]", $ext_name);
76
77                                 // We maybe want to install an extension so let's test-drive it...
78                                 if (LOAD_EXTENSION($ext_name, $EXT_LOAD_MODE)) {
79                                         // Create a task for newly installed extension
80                                         CREATE_NEW_EXTENSION_TASK(getCurrentAdminId(), $ext_subj, $ext_name);
81                                 } // END - if
82                         } else {
83                                 // Test-drive extension in update mode
84                                 require(sprintf("%sinc/extensions/ext-%s.php", getConfig('PATH'), $ext_name));
85
86                                 // Update extension if extension is installed and outdated
87                                 //* DEBUG: */ OUTPUT_HTML("ext={$ext_name},ver={EXT_GET_VERSION()}/".GET_EXT_VERSION($ext_name)."<br />");
88                                 if (EXT_GET_VERSION() > $ext_ver) {
89                                         // Update the extension
90                                         EXTENSION_UPDATE($ext_name, $ext_ver);
91                                 } // END - if
92
93                                 if (isset($GLOBALS['cache_array']['active_extensions'][$ext_name])) {
94                                         // Maybe we want to keept the current extension active?
95                                         if (($GLOBALS['cache_array']['active_extensions'][$ext_name] == 'Y') && (!EXT_IS_ACTIVE($ext_name))) {
96                                                 // Reactivate this extension!
97                                                 ACTIVATE_EXTENSION($ext_name);
98                                         } // END - if
99                                 } // END - if
100                         }
101                 } // END - if
102         } // END - foreach
103
104         // At last - but not least - check for own and new unassigned tasks...
105         $result_tasks = SQL_QUERY_ESC("SELECT `id`, `assigned_admin`, `userid`, `task_type`, `subject`, `text`, `task_created`
106 FROM `{!_MYSQL_PREFIX!}_task_system`
107 WHERE `assigned_admin`=%s OR (`assigned_admin`=0 AND `status`='NEW')
108 ORDER BY `userid` DESC, `task_type` DESC, `subject` ASC, `task_created` DESC",
109         array(getCurrentAdminId()), __FILE__, __LINE__);
110
111         if (SQL_NUMROWS($result_tasks) > 0) {
112                 // New jobs found!
113                 $jobsDone = false;
114         } // END - if
115
116         // Free the result
117         SQL_FREERESULT($result);
118
119         // Return status
120         return $jobsDone;
121 }
122
123 // Outputs selected tasks
124 function OUTPUT_SELECTED_TASKS ($POST, $result_tasks) {
125         if ((isset($POST['assign'])) && (count($POST['task']) > 0)) {
126                 // Assign / do tasks
127                 $OUT = ''; $SW = 2;
128                 foreach ($POST['task'] as $id => $sel) {
129                         $result_task = SQL_QUERY_ESC("SELECT `id`, `userid`, `task_type`, `subject`, `text`, `task_created`, `status`, `assigned_admin`
130 FROM `{!_MYSQL_PREFIX!}_task_system`
131 WHERE `id`=%s AND (`assigned_admin`=%s OR (`assigned_admin`=0 AND `status`='NEW'))
132 LIMIT 1",
133                                 array(bigintval($id), getCurrentAdminId()), __FILE__, __LINE__);
134
135                         // Task is found?
136                         if (SQL_NUMROWS($result_task) == 1) {
137                                 // Task is valid...
138                                 list($tid, $uid, $type, $subj, $text, $created, $status, $aid) = SQL_FETCHROW($result_task);
139
140                                 if ($aid == '0') {
141                                         // Assgin current admin to unassgigned task
142                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET `assigned_admin`=%s WHERE `id`=%s LIMIT 1",
143                                         array(getCurrentAdminId(), bigintval($tid)), __FILE__, __LINE__);
144                                 } // END - if
145
146                                 $add = '';
147                                 // @TODO Rewrite this to a filter
148                                 if ($type == 'SUPPORT_MEMBER') {
149                                         $mode = substr($text, 0, strpos($text, ':'));
150                                         $text = substr($text, strpos($text, ':') + 1);
151                                         $add = "<li>{--ADMIN_TASK_SUPPORT_MODE--}: <strong>".$mode."</strong></li>";
152                                 } // END - if
153
154                                 // Is a userid assign?
155                                 if ($uid > 0) {
156                                         // Then load his data!
157                                         // @TODO Can this SQL be encapsulated in a function, so all similar queries can be rewritten?
158                                         $result_user = SQL_QUERY_ESC("SELECT gender, surname, family, email FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
159                                                 array(bigintval($uid)), __FILE__, __LINE__);
160
161                                         // Entry found?
162                                         if (SQL_NUMROWS($result_user) == 1) {
163                                                 list($gender, $sname, $fname, $email) = SQL_FETCHROW($result_user);
164                                                 $add = "<li>{--ADMIN_MEMBER_UID--}: <strong>".generateUserProfileLink($uid, 'user_data')." (<a href=\"".generateEmailLink($email, 'user_data')."\">".translateGender($gender)." ".$sname." ".$fname."</a>)</strong></li>";
165                                         } else {
166                                                 // Invalid userid, so log and zero it
167                                                 DEBUG_LOG(__FUNCTION__, __LINE__, 'Invalid userid=' . $uid . '-> Not found!');
168                                                 $uid = 0;
169                                         }
170
171                                         // Free result
172                                         SQL_FREERESULT($result_user);
173                                 } // END - if
174
175                                 // Decode entities of the text
176                                 $text = decodeEntities($text);
177
178                                 // Compile and insert text from task into table template
179                                 $text = LOAD_TEMPLATE('admin_extensions_text', true, $text);
180
181                                 // Initialize variables (no title for SQL commands by default)
182                                 $ext_name = ''; $ext_ver = '';
183                                 $title = getMessage('TASK_NO_TITLE');
184
185                                 // Shall I list SQL commands assigned to an extension installation or update task?
186                                 // OLD WAY: if (((GET_EXT_VERSION('sql_patches') != '') && (getConfig('verbose_sql') == 'Y')) || (!EXT_IS_ACTIVE('sql_patches'))) {
187                                 if ((getConfig('verbose_sql') == 'Y')) {
188                                         // Extract extension name from subject
189                                         $ext_name = substr($subj, 1, strpos($subj, ':') - 1);
190
191                                         // Update task or extension task?
192                                         if ($type == 'EXTENSION') {
193                                                 // @TODO Should we init sql_notes here or inside REGISTER_EXTENSION()?
194                                                 EXT_INIT_NOTES();
195
196                                                 // Load SQL commands for registering
197                                                 REGISTER_EXTENSION($ext_name, $id, true);
198
199                                                 // Add notes to text
200                                                 $text .= EXT_GET_NOTES();
201
202                                                 // Set title
203                                                 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REGISTER');
204                                         } elseif ($type == 'EXTENSION_UPDATE') {
205                                                 // Prepare extension name and version
206                                                 $ext_name = substr($ext_name, 7);
207                                                 $ext_name = substr($ext_name, 0, strpos($ext_name, '-'));
208                                                 $test = '[UPDATE-' . $ext_name . '-';
209                                                 $ext_ver = substr($subj, strlen($test));
210                                                 $ext_ver = substr($ext_ver, 0, strpos($ext_ver, ':'));
211
212                                                 // Load SQLs from file
213                                                 EXTENSION_UPDATE($ext_name, $ext_ver, true);
214
215                                                 // Add notes to text
216                                                 $text .= EXT_GET_NOTES();
217
218                                                 // Set title
219                                                 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_UPDATE');
220                                         } else {
221                                                 // Remove extension's name
222                                                 $ext_name = '';
223                                         }
224
225                                         // Auto-init SQL handler
226                                         if (!IS_SQLS_VALID()) INIT_SQLS();
227
228                                         // Add SQLs to a table
229                                         if ((!empty($ext_name)) && (GET_EXT_VERSION('sql_patches')) && (getConfig('verbose_sql') == 'Y')) {
230                                                 // Add verbose SQL table
231                                                 $text .= EXTENSION_VERBOSE_TABLE($title, " class=\"admin_table top2 left2 right2\"", true, '100%')."<br />\n";
232                                         } // END - if
233                                 } elseif ((!empty($ext_name)) && (!empty($ext_ver))) {
234                                         // Run SQL commands in dry mode but only return the notes
235                                         EXTENSION_UPDATE($ext_name, $ext_ver, true);
236                                         $text .= EXT_GET_NOTES();
237                                 } else {
238                                         // This should not normally happen!
239                                         debug_report_bug('ext_name(' . $ext_name . ') or ext_ver(' . $ext_ver . ') is empty! sql_patches=' . GET_EXT_VERSION('sql_patches') . '/verbose_sql=' . getConfig('verbose_sql'));
240                                 }
241
242                                 // Prepare array for the template
243                                 $content = array(
244                                         'sw'        => $SW,
245                                         'subj'      => $subj,
246                                         'add'       => $add,
247                                         'text'      => $text,
248                                         'created'   => generateDateTime($created, '1'),
249                                         'extension' => $ext_name
250                                 );
251
252                                 // Load template
253                                 $OUT .= LOAD_TEMPLATE('admin_overview_row', true, $content);
254
255                                 // Which task do we actually have here?
256                                 // @TODO Rewrite this to something with include files
257                                 switch ($type) {
258                                         case 'EXTENSION': // Install new extensions
259                                                 $ext_name = substr($subj, 1, strpos($subj, ':') - 1);
260                                                 $result_lines = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
261                                                 array($ext_name), __FILE__, __LINE__);
262                                                 $lines = SQL_NUMROWS($result_lines);
263                                                 SQL_FREERESULT($result_lines);
264                                                 if ($lines == '0') {
265                                                         // New extension found
266                                                         $OUT .= LOAD_TEMPLATE('admin_ext_reg_form', true, array(
267                                                         'id'       => bigintval($id),
268                                                         'ext_name' => $ext_name
269                                                         ));
270                                                 } else {
271                                                         // Task is closed so nothing is todo
272                                                         $OUT .= "<div class=\"admin_failed\">{--ADMIN_EXT_ALREADY_REGISTERED--}</div>\n";
273
274                                                         // Close task but not already closes or deleted or update tasks
275                                                         if (($status != 'CLOSED') && ($status != 'DELETED') && ($type != 'EXTENSION_UPDATE')) {
276                                                                 // Solve the task
277                                                                 runFilterChain('solve_task', $tid);
278                                                         } // END - if
279                                                 }
280                                                 break;
281
282                                         case 'EXTENSION_UPDATE': // Extension update
283                                                 // Extension updates are installed automatically
284                                                 $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_EXTENSION_UPDATED--}</div>\n";
285
286                                                 // Close task
287                                                 if (($status != 'CLOSED') && ($status != 'DELETED')) {
288                                                         // Solve the task
289                                                         runFilterChain('solve_task', $tid);
290                                                 } // END - if
291                                                 break;
292
293                                         case 'SUPPORT_MEMBER': // Assign on member's support request
294                                                 // @TODO This may also be rewritten to include files
295                                                 switch ($mode) {
296                                                         default: // @TODO Unknown support mode
297                                                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown support mode %s detected. This part is under construction!", $mode));
298                                                         $OUT .= "<div class=\"admin_failed medium\">".sprintf(getMessage('ADMIN_UNKNOWN_SUPPORT_MODE'), $mode)."</div>\n";
299                                                         break;
300                                                 }
301                                                 break;
302
303                                                         case 'PAYOUT_REQUEST': // Payout requests
304                                                                 if (EXT_IS_ACTIVE('payout')) {
305                                                                         // Extension is installed so let him send a notification to the user
306                                                                         $result_pay = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_user_payouts` WHERE userid=%s AND payout_timestamp=%s LIMIT 1",
307                                                                         array(bigintval($uid), bigintval($created)), __FILE__, __LINE__);
308                                                                         list($pid) = SQL_FETCHROW($result_pay);
309                                                                         SQL_FREERESULT($result_pay);
310
311                                                                         if ((!empty($pid)) && ($pid > 0)) {
312                                                                                 // Payout ID can be obtained
313                                                                                 $content = array(
314                                                                 'pid' => $pid,
315                                                                 'tid' => $tid,
316                                                                                 );
317                                                                                 $OUT .= LOAD_TEMPLATE('admin_payout_overview_form', true, $content);
318                                                                         } else {
319                                                                                 // Problem obtaining payout ID
320                                                                                 $OUT .= "<div class=\"admin_failed medium\">{--PAYOUT_OBTAIN_ID_FAILED--}</div>\n";
321                                                                         }
322                                                                 } else {
323                                                                         // Extension is not installed
324                                                                         $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_PAYOUT_NOT_INSTALLED--}</div>\n";
325                                                                 }
326                                                                 break;
327
328                                                         case 'WERNIS_REQUEST': // Wernis requests
329                                                                 if (EXT_IS_ACTIVE('wernis')) {
330                                                                         // Extension is installed so let him send a notification to the user
331                                                                         $result_pay = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_user_wernis` WHERE userid=%s AND wernis_timestamp=%s LIMIT 1",
332                                                                         array(bigintval($uid), bigintval($created)), __FILE__, __LINE__);
333                                                                         list($pid) = SQL_FETCHROW($result_pay);
334                                                                         SQL_FREERESULT($result_pay);
335
336                                                                         if ((!empty($pid)) && ($pid > 0)) {
337                                                                                 // Payout ID can be obtained
338                                                                                 $content = array(
339                                                                 'pid' => $pid,
340                                                                 'tid' => $tid,
341                                                                                 );
342                                                                                 $OUT .= LOAD_TEMPLATE('admin_wernis_overview_form', true, $content);
343                                                                         } else {
344                                                                                 // Problem obtaining wernis ID
345                                                                                 $OUT .= "<div class=\"admin_failed medium\">{--WERNIS_OBTAIN_ID_FAILED--}</div>\n";
346                                                                         }
347                                                                 } else {
348                                                                         // Extension is not installed
349                                                                         $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_WERNIS_NOT_INSTALLED--}</div>\n";
350                                                                 }
351                                                                 break;
352
353                                                         case 'HOLIDAY_REQUEST': // Holiday requests
354                                                                 $OUT .= LOAD_TEMPLATE('admin_task_holiday', true, $uid);
355                                                                 break;
356
357                                                         case 'NL_UNSUBSCRIBE': // Newsletter unsubscriptions
358                                                                 $result = SQL_QUERY_ESC("SELECT nl_timespan FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
359                                                                         array(bigintval($uid)), __FILE__, __LINE__);
360                                                                 list($span) = SQL_FETCHROW($result);
361                                                                 SQL_FREERESULT($result);
362
363                                                                 if ($span > 0) {
364                                                                         // Undone unscubscribe request
365                                                                         $content = array(
366                                                                                 'uid' => $uid,
367                                                                                 'id'  => $tid
368                                                                         );
369                                                                         $OUT .= LOAD_TEMPLATE('admin_newsletter_tsk', true, $content);
370                                                                 } else {
371                                                                         // Already unsubscribed
372                                                                         $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_NL_UNSUBSCRIBE_ALREADY--}</div>\n";
373                                                                 }
374                                                                 break;
375
376                                                         default: // Unknown task type
377                                                                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown task type %s detected.", $type));
378                                                                 $OUT .= "<div class=\"admin_failed medium\">".sprintf(getMessage('ADMIN_UNKNOWN_TASK_TYPE'), $type, $id)."</div>\n";
379                                                                 break;
380                                 }
381                                 $OUT .= "  </td>
382   <td width=\"1%\" class=\"switch_sw".$SW." bottom2 right2\">&nbsp;</td>
383 </tr>\n";
384                         } // END - if
385
386                         // Free result
387                         SQL_FREERESULT($result_task);
388
389                         // Switch colors
390                         $SW = 3 - $SW;
391                 } // END - foreach
392                 define('__TASK_ROWS', $OUT);
393
394                 // Load final template
395                 LOAD_TEMPLATE('admin_overview_list');
396         } else {
397                 if ((isset($POST['task'])) && ((count($POST['task']) > 0) || ($POST['task'][0] == '1'))) {
398                         // Only unassign / delete tasks when there are selected tasks posted
399                         if (!empty($POST['unassign'])) {
400                                 // Unassign from tasks
401                                 foreach ($POST['task'] as $id => $sel) {
402                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET `assigned_admin`=0 WHERE `id`=%s AND `assigned_admin`=%s LIMIT 1",
403                                         array(bigintval($id), getCurrentAdminId()), __FILE__, __LINE__);
404                                 }
405                         } elseif (isset($POST['del'])) {
406                                 // Delete tasks
407                                 foreach ($POST['task'] as $id => $sel) {
408                                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE `id`=%s AND assigned_admin IN (%s,0) LIMIT 1",
409                                         array(bigintval($id), getCurrentAdminId()), __FILE__, __LINE__);
410                                 }
411                         }
412
413                         // Update query
414                         $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",
415                         array(getCurrentAdminId()), __FILE__, __LINE__);
416                 } // END - if
417
418                 // There are uncompleted jobs!
419                 $OUT = ''; $SW = 2;
420                 while ($content = SQL_FETCHARRAY($result_tasks)) {
421                         // Init infos
422                         $content['infos'] = '---';
423
424                         // Generate assign link
425                         $content['assigned_admin'] = generateAdminLink($content['assigned_admin']);
426
427                         // Generate infos
428                         // @TODO Try to move this in includes
429                         switch ($content['task_type']) {
430                                 case 'EXTENSION':
431                                 case 'EXTENSION_UPDATE':
432                                         $content['infos'] = substr($content['subject'], 1, strpos($content['subject'], ':') - 1);
433                                         break;
434                         }
435
436                         // Get task type
437                         $content['task_type_msg'] = getMessage('ADMIN_TASK_IS_'.strtoupper($content['task_type']).'');
438
439                         if ($content['userid'] > 0) {
440                                 // Member found otherwise it's a system task
441                                 $content['userid'] = generateUserProfileLink($content['userid']);
442                         } else {
443                                 $content['userid'] = "<em>{--ADMIN_IS_SYSTEM_TASK--}</em>";
444                         }
445
446                         // Prepare content
447                         // @TODO Rewritings: admin->assigned_admin,uid->userid,type->task_type_msg in template
448                         $content = merge_array($content, array(
449                                 'sw'      => $SW,
450                                 'admin'   => $content['assigned_admin'],
451                                 'uid'     => $content['userid'],
452                                 'type'    => $content['task_type_msg'],
453                                 'created' => generateDateTime($content['task_created'], '2')
454                         ));
455
456                         // Do we have extension task?
457                         if (($content['task_type'] == 'EXTENSION') && (GET_EXT_VERSION($content['infos']) == '')) {
458                                 // Load extension row template
459                                 $OUT .= LOAD_TEMPLATE("admin_overview_list_ext_rows", true, $content);
460                         } else {
461                                 // Load default row template
462                                 $OUT .= LOAD_TEMPLATE("admin_overview_list_rows", true, $content);
463                         }
464
465                         // Switch color
466                         $SW = 3 - $SW;
467                 }
468
469                 // Free memory
470                 SQL_FREERESULT($result_tasks);
471
472                 // Load footer template
473                 LOAD_TEMPLATE("admin_overview_table", false, $OUT);
474         }
475 }
476 //
477 ?>