Fixed a version problem which causes admin logins to fail if ext-admins is installed
[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         $handle = opendir(constant('PATH')."inc/extensions/") or mxchange_die("Cannot read extension directory!");
56         while ($file = readdir($handle)) {
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 - while
100
101         // Close directory handle
102         closedir($handle);
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, task_created DESC",
109                 array(GET_CURRENT_ADMIN_ID()), __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 FROM `{!_MYSQL_PREFIX!}_task_system` WHERE id=%s AND (assigned_admin='%s' OR (assigned_admin='0' AND `status`='NEW')) LIMIT 1",
130                                 array(bigintval($id), GET_CURRENT_ADMIN_ID()), __FILE__, __LINE__);
131                         if (SQL_NUMROWS($result_task) == 1) {
132                                 // Task is valid...
133                                 list($tid, $uid, $type, $subj, $text, $created, $status, $aid) = SQL_FETCHROW($result_task);
134                                 SQL_FREERESULT($result_task);
135
136                                 if ($aid == "0") {
137                                         // Assgin current admin to unassgigned task
138                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET assigned_admin='%s' WHERE id=%s LIMIT 1",
139                                                 array(GET_CURRENT_ADMIN_ID(), bigintval($tid)), __FILE__, __LINE__);
140                                 } // END - if
141
142                                 $ADD = "";
143                                 if ($type == "SUPPORT_MEMBER") {
144                                         $mode = substr($text, 0, strpos($text, ":"));
145                                         $text = substr($text, strpos($text, ":") + 1);
146                                         $ADD = "<li>{--ADMIN_TASK_SUPPORT_MODE--}: <strong>".$mode."</strong></li>";
147                                 } // END - if
148
149                                 if ($uid > 0) {
150                                         $result_user = SQL_QUERY_ESC("SELECT gender, surname, family, email FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
151                                          array(bigintval($uid)), __FILE__, __LINE__);
152                                         if (SQL_NUMROWS($result_user) == 1) {
153                                                 list($gender, $sname, $fname, $email) = SQL_FETCHROW($result_user);
154                                                 SQL_FREERESULT($result_user);
155                                                 $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>";
156                                         } // END - if
157                                 } // END - if
158
159                                 // Decode entities of the text
160                                 $text = decodeEntities($text);
161
162                                 // Compile and insert text from task into table template
163                                 $text = LOAD_TEMPLATE("admin_extensions_text", true, $text);
164
165                                 // Initialize variables (no title for SQL commands by default)
166                                 $ext_name = "";
167                                 $title = getMessage('TASK_NO_TITLE');
168
169                                 // Shall I list SQL commands assigned to an extension installation or update task?
170                                 if (((GET_EXT_VERSION("sql_patches") != '') && (getConfig('verbose_sql') == "Y")) || (!EXT_IS_ACTIVE("sql_patches"))) {
171                                         $ext_name = substr($subj, 1, strpos($subj, ":") - 1);
172                                         if ($type == "EXTENSION") {
173                                                 // Load SQL commands for registering
174                                                 SET_SQLS(EXTENSION_REGISTER($ext_name, $id, true));
175
176                                                 // Add notes to text
177                                                 $text .= EXT_GET_NOTES();
178
179                                                 // Set title
180                                                 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REGISTER');
181                                         } elseif ($type == "EXTENSION_UPDATE") {
182                                                 // Prepare extension name and version
183                                                 $ext_name = substr($ext_name, 7);
184                                                 $ext_name = substr($ext_name, 0, strpos($ext_name, "-"));
185                                                 $test = "[UPDATE-".$ext_name."-";
186                                                 $ext_ver = substr($subj, strlen($test));
187                                                 $ext_ver = substr($ext_ver, 0, strpos($ext_ver, ":"));
188
189                                                 // Load SQLs from file
190                                                 EXTENSION_UPDATE($ext_name, $ext_ver, true);
191
192                                                 // Add notes to text
193                                                 $text .= EXT_GET_NOTES();
194
195                                                 // Set title
196                                                 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_UPDATE');
197                                         } else {
198                                                 // Remove extension's name
199                                                 $ext_name = "";
200                                         }
201
202                                         // Add SQLs to a table
203                                         if (!IS_SQLS_VALID()) INIT_SQLS();
204                                         if (empty($title)) $title = "";
205                                         if ((!empty($ext_name)) && (GET_EXT_VERSION("sql_patches")) && (getConfig('verbose_sql') == "Y")) {
206                                                 // Add verbose SQL table
207                                                 $text .= EXTENSION_VERBOSE_TABLE($title, " class=\"admin_table top2 left2 right2\"", true, "100%")."<br />\n";
208                                         } // END - if
209                                 } else {
210                                         // Run SQL commands in dry mode but only return the notes
211                                         EXTENSION_UPDATE($ext_name, $ext_ver, true);
212                                         $text .= EXT_GET_NOTES();
213                                 }
214
215                                 // Prepare array for the template
216                                 $content = array(
217                                         'sw'        => $SW,
218                                         'subj'      => $subj,
219                                         'add'       => $ADD,
220                                         'text'      => $text,
221                                         'created'   => MAKE_DATETIME($created, "1"),
222                                         'extension' => $ext_name
223                                 );
224
225                                 // Load template
226                                 $OUT .= LOAD_TEMPLATE("admin_overview_row", true, $content);
227
228                                 // Which task do we actually have here?
229                                 // @TODO Rewrite this to something with include files
230                                 switch ($type)
231                                 {
232                                 case "EXTENSION": // Install new extensions
233                                         $ext_name = substr($subj, 1, strpos($subj, ":") - 1);
234                                         $result_lines = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
235                                                 array($ext_name), __FILE__, __LINE__);
236                                         $lines = SQL_NUMROWS($result_lines);
237                                         SQL_FREERESULT($result_lines);
238                                         if ($lines == "0") {
239                                                 // New extension found
240                                                 $OUT .= LOAD_TEMPLATE("admin_ext_reg_form", true, array(
241                                                         'id'       => bigintval($id),
242                                                         'ext_name' => $ext_name
243                                                 ));
244                                         } else {
245                                                 // Task is closed so nothing is todo
246                                                 $OUT .= "<div class=\"admin_failed\">{--ADMIN_EXT_ALREADY_REGISTERED--}</div>\n";
247
248                                                 // Close task but not already closes or deleted or update tasks
249                                                 if (($status != "CLOSED") && ($status != "DELETED") && ($type != "EXTENSION_UPDATE")) {
250                                                         // Solve the task
251                                                         runFilterChain('solve_task', $tid);
252                                                 } // END - if
253                                         }
254                                         break;
255
256                                 case "EXTENSION_UPDATE": // Extension update
257                                         // Extension updates are installed automatically
258                                         $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_EXTENSION_UPDATED--}</div>\n";
259
260                                         // Close task
261                                         if (($status != "CLOSED") && ($status != "DELETED")) {
262                                                 // Solve the task
263                                                 runFilterChain('solve_task', $tid);
264                                         } // END - if
265                                         break;
266
267                                 case "SUPPORT_MEMBER": // Assign on member's support request
268                                         // @TODO This may also be rewritten to include files
269                                         switch ($mode)
270                                         {
271                                         default: // @TODO Unknown support mode
272                                                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown support mode %s detected. This part is under construction!", $mode));
273                                                 $OUT .= "<div class=\"admin_failed medium\">".ADMIN_UNKNOWN_SUPPORT_MODE_1.$mode.ADMIN_UNKNOWN_SUPPORT_MODE_2."</div>\n";
274                                                 break;
275                                         }
276                                         break;
277
278                                 case "PAYOUT_REQUEST": // Payout requests
279                                         if (EXT_IS_ACTIVE("payout")) {
280                                                 // Extension is installed so let him send a notification to the user
281                                                 $result_pay = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_user_payouts` WHERE userid=%s AND payout_timestamp=%s LIMIT 1",
282                                                         array(bigintval($uid), bigintval($created)), __FILE__, __LINE__);
283                                                 list($pid) = SQL_FETCHROW($result_pay);
284                                                 SQL_FREERESULT($result_pay);
285
286                                                 if ((!empty($pid)) && ($pid > 0)) {
287                                                         // Payout ID can be obtained
288                                                         $content = array(
289                                                                 'pid' => $pid,
290                                                                 'tid' => $tid,
291                                                         );
292                                                         $OUT .= LOAD_TEMPLATE("admin_payout_overview_form", true, $content);
293                                                 } else {
294                                                         // Problem obtaining payout ID
295                                                         $OUT .= "<div class=\"admin_failed medium\">".PAYOUT_OBTAIN_ID_FAILED."</div>\n";
296                                                 }
297                                         } else {
298                                                 // Extension is not installed
299                                                 $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_PAYOUT_NOT_INSTALLED--}</div>\n";
300                                         }
301                                         break;
302
303                                 case "WERNIS_REQUEST": // Wernis requests
304                                         if (EXT_IS_ACTIVE("wernis")) {
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_wernis` WHERE userid=%s AND wernis_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_wernis_overview_form", true, $content);
318                                                 } else {
319                                                         // Problem obtaining wernis ID
320                                                         $OUT .= "<div class=\"admin_failed medium\">{--WERNIS_OBTAIN_ID_FAILED--}</div>\n";
321                                                 }
322                                         } else {
323                                                 // Extension is not installed
324                                                 $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_WERNIS_NOT_INSTALLED--}</div>\n";
325                                         }
326                                         break;
327
328                                 case "HOLIDAY_REQUEST": // Holiday requests
329                                         $OUT .= LOAD_TEMPLATE("admin_task_holiday", true, $uid);
330                                         break;
331
332                                 case "NL_UNSUBSCRIBE": // Newsletter unsubscriptions
333                                         $result = SQL_QUERY_ESC("SELECT nl_timespan FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
334                                                 array(bigintval($uid)), __FILE__, __LINE__);
335                                         list($span) = SQL_FETCHROW($result);
336                                         SQL_FREERESULT($result);
337
338                                         if ($span > 0) {
339                                                 // Undone unscubscribe request
340                                                 $content = array(
341                                                         'uid' => $uid,
342                                                         'id'  => $tid
343                                                 );
344                                                 $OUT .= LOAD_TEMPLATE("admin_newsletter_tsk", true, $content);
345                                         } else {
346                                                 // Already unsubscribed
347                                                 $OUT .= "<div class=\"admin_failed medium\">".ADMIN_NL_UNSUBSCRIBE_ALREADY."</div>\n";
348                                         }
349                                         break;
350
351                                 default: // Unknown task type
352                                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown task type %s detected.", $type));
353                                         $OUT .= "<div class=\"admin_failed medium\">".ADMIN_UNKNOWN_TASK_TYPE_1.$type.ADMIN_UNKNOWN_TASK_TYPE_2.$id.ADMIN_UNKNOWN_TASK_TYPE_3."</div>\n";
354                                         break;
355                                 }
356                                 $OUT .= "  </td>
357   <td width=\"1%\" class=\"switch_sw".$SW." bottom2 right2\">&nbsp;</td>
358 </tr>\n";
359                         } // END - if
360                         $SW = 3 - $SW;
361                 } // END - foreach
362                 define('__TASK_ROWS', $OUT);
363
364                 // Load final template
365                 LOAD_TEMPLATE("admin_overview_list");
366         } else {
367                 if ((isset($POST['task'])) && ((count($POST['task']) > 0) || ($POST['task'][0] == "1"))) {
368                         // Only unassign / delete tasks when there are selected tasks posted
369                         if (!empty($POST['unassign'])) {
370                                 // Unassign from tasks
371                                 foreach ($POST['task'] as $id => $sel) {
372                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET assigned_admin=0 WHERE id=%s AND assigned_admin=%s LIMIT 1",
373                                                 array(bigintval($id), GET_CURRENT_ADMIN_ID()), __FILE__, __LINE__);
374                                 }
375                         } elseif (isset($POST['del'])) {
376                                 // Delete tasks
377                                 foreach ($POST['task'] as $id => $sel) {
378                                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE id=%s AND assigned_admin IN (%s,0) LIMIT 1",
379                                                 array(bigintval($id), GET_CURRENT_ADMIN_ID()), __FILE__, __LINE__);
380                                 }
381                         }
382
383                         // Update query
384                         $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",
385                                 array(GET_CURRENT_ADMIN_ID()), __FILE__, __LINE__);
386                 } // END - if
387
388                 // There are uncompleted jobs!
389                 // @TODO Rewrite this to one template and use $OUT = ...
390                 LOAD_TEMPLATE("admin_overview_header");
391                 $OUT = ""; $SW = 2;
392                 while ($content = SQL_FETCHARRAY($result_tasks)) {
393                         // Init infos
394                         $content['infos'] = "---";
395
396                         // Generate assign link
397                         $content['assigned_admin'] = GENERATE_AID_LINK($content['assigned_admin']);
398
399                         // Generate infos
400                         switch ($content['task_type'])
401                         {
402                         case "EXTENSION":
403                         case "EXTENSION_UPDATE":
404                                 $content['infos'] = substr($content['subject'], 1, strpos($content['subject'], ":") - 1);
405                                 break;
406                         }
407
408                         // Get task type
409                         $content['task_type_msg'] = getMessage('ADMIN_TASK_IS_'.strtoupper($content['task_type']).'');
410
411                         if ($content['userid'] > 0) {
412                                 // Member found otherwise it's a system task
413                                 $content['userid'] = ADMIN_USER_PROFILE_LINK($content['userid']);
414                         } else {
415                                 $content['userid'] = "<em>{--ADMIN_IS_SYSTEM_TASK--}</em>";
416                         }
417
418                         // Prepare content
419                         // @TODO Rewritings: admin->assigned_admin,uid->userid,type->task_type_msg in template
420                         $content = merge_array($content, array(
421                                 'sw'      => $SW,
422                                 'admin'   => $content['assigned_admin'],
423                                 'uid'     => $content['userid'],
424                                 'type'    => $content['task_type_msg'],
425                                 'created' => MAKE_DATETIME($content['task_created'], "2")
426                         ));
427
428                         // Do we have extension task?
429                         if (($content['task_type'] == "EXTENSION") && (GET_EXT_VERSION($content['infos']) == "")) {
430                                 // Load extension row template
431                                 // @TODO Rewrite this to $OUT .= ..., true, ...
432                                 LOAD_TEMPLATE("admin_overview_list_ext_rows", false, $content);
433                         } else {
434                                 // Load default row template
435                                 // @TODO Rewrite this to $OUT .= ..., true, ...
436                                 LOAD_TEMPLATE("admin_overview_list_rows", false, $content);
437                         }
438
439                         // Switch color
440                         $SW = 3 - $SW;
441                 }
442
443                 // Free memory
444                 SQL_FREERESULT($result_tasks);
445
446                 // Load footer template
447                 LOAD_TEMPLATE("admin_overview_footer");
448         }
449 }
450 //
451 ?>