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