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