Should not be loadInclude() here, we need to rewrite EXT_LOAD_MODE and EXT_VER here
[mailer.git] / inc / modules / admin / overview-inc.php
index 36cf9c6f3fe6ffc09814ff0bbac6d2fb9946aa45..97b4ce5437e8d2326b4b585672bd9fe2e08268d2 100644 (file)
@@ -42,21 +42,23 @@ if ((!defined('__SECURITY')) || (!IS_ADMIN())) {
        require($INC);
 }
 
-function OUTPUT_STANDARD_OVERVIEW(&$result_tasks) {
+function OUTPUT_STANDARD_OVERVIEW (&$result_tasks) {
        // First check for solved and not assigned tasks and assign them to current admin
        SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_task_system` SET `assigned_admin`=%s WHERE assigned_admin < 1 AND status != 'NEW'",
-       array(getCurrentAdminId()), __FILE__, __LINE__);
+               array(getCurrentAdminId()), __FILE__, __LINE__);
 
        // We currently don't want to install an extension so let's find out if we need...
        $EXT_LOAD_MODE = 'register';
        $jobsDone = true;
 
        // Open the extension directory
-       $extensionList = getArrayFromDirectory("inc/extensions/", "ext-", false, false);
+       $extensionList = getArrayFromDirectory('inc/extensions/', 'ext-', false, false);
        foreach ($extensionList as $file) {
+               // Only file name is required... :(
+               $file = basename($file);
+
                // Is this file an extension?
-               if ((substr($file, 0, 4) == "ext-") && (substr($file, -4) == '.php')) {
-                       //* DEBUG: */ echo $file."<br />\n";
+               if ((substr($file, 0, 4) == 'ext-') && (substr($file, -4) == '.php')) {
                        // Possible newly installed extension found so we extract extension's name
                        $ext_name = strtolower(substr($file, 4, -4)); // Keep always extension names on lower case!!!
 
@@ -65,6 +67,7 @@ function OUTPUT_STANDARD_OVERVIEW(&$result_tasks) {
 
                        // Check if extension is installed or not
                        $ext_ver = GET_EXT_VERSION($ext_name);
+                       //* DEBUG: */ OUTPUT_HTML($ext_name."=".$ext_ver."<br />");
 
                        // Is the extension not yet installed?
                        if (empty($ext_ver)) {
@@ -78,10 +81,10 @@ function OUTPUT_STANDARD_OVERVIEW(&$result_tasks) {
                                } // END - if
                        } else {
                                // Test-drive extension in update mode
-                               require(sprintf("%sinc/extensions/ext-%s.php", constant('PATH'), $ext_name));
+                               require(sprintf("%sinc/extensions/ext-%s.php", getConfig('PATH'), $ext_name));
 
                                // Update extension if extension is installed and outdated
-                               //* DEBUG: */ print "ext={$ext_name},ver={EXT_GET_VERSION()}/".GET_EXT_VERSION($ext_name)."<br />\n";
+                               //* DEBUG: */ OUTPUT_HTML("ext={$ext_name},ver={EXT_GET_VERSION()}/".GET_EXT_VERSION($ext_name)."<br />");
                                if (EXT_GET_VERSION() > $ext_ver) {
                                        // Update the extension
                                        EXTENSION_UPDATE($ext_name, $ext_ver);
@@ -127,11 +130,12 @@ function OUTPUT_SELECTED_TASKS ($POST, $result_tasks) {
 FROM `{!_MYSQL_PREFIX!}_task_system`
 WHERE `id`=%s AND (`assigned_admin`=%s OR (`assigned_admin`=0 AND `status`='NEW'))
 LIMIT 1",
-                       array(bigintval($id), getCurrentAdminId()), __FILE__, __LINE__);
+                               array(bigintval($id), getCurrentAdminId()), __FILE__, __LINE__);
+
+                       // Task is found?
                        if (SQL_NUMROWS($result_task) == 1) {
                                // Task is valid...
                                list($tid, $uid, $type, $subj, $text, $created, $status, $aid) = SQL_FETCHROW($result_task);
-                               SQL_FREERESULT($result_task);
 
                                if ($aid == '0') {
                                        // Assgin current admin to unassgigned task
@@ -147,14 +151,25 @@ LIMIT 1",
                                        $add = "<li>{--ADMIN_TASK_SUPPORT_MODE--}: <strong>".$mode."</strong></li>";
                                } // END - if
 
+                               // Is a userid assign?
                                if ($uid > 0) {
+                                       // Then load his data!
+                                       // @TODO Can this SQL be encapsulated in a function, so all similar queries can be rewritten?
                                        $result_user = SQL_QUERY_ESC("SELECT gender, surname, family, email FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
-                                       array(bigintval($uid)), __FILE__, __LINE__);
+                                               array(bigintval($uid)), __FILE__, __LINE__);
+
+                                       // Entry found?
                                        if (SQL_NUMROWS($result_user) == 1) {
                                                list($gender, $sname, $fname, $email) = SQL_FETCHROW($result_user);
-                                               SQL_FREERESULT($result_user);
-                                               $add = "<li>{--ADMIN_MEMBER_UID--}: <strong>".generateUserProfileLink($uid)." (<a href=\"".generateMemberEmailLink($email, "user_data")."\">".translateGender($gender)." ".$sname." ".$fname."</a>)</strong></li>";
-                                       } // END - if
+                                               $add = "<li>{--ADMIN_MEMBER_UID--}: <strong>".generateUserProfileLink($uid, 'user_data')." (<a href=\"".generateEmailLink($email, 'user_data')."\">".translateGender($gender)." ".$sname." ".$fname."</a>)</strong></li>";
+                                       } else {
+                                               // Invalid userid, so log and zero it
+                                               DEBUG_LOG(__FUNCTION__, __LINE__, 'Invalid userid=' . $uid . '-> Not found!');
+                                               $uid = 0;
+                                       }
+
+                                       // Free result
+                                       SQL_FREERESULT($result_user);
                                } // END - if
 
                                // Decode entities of the text
@@ -164,13 +179,20 @@ LIMIT 1",
                                $text = LOAD_TEMPLATE('admin_extensions_text', true, $text);
 
                                // Initialize variables (no title for SQL commands by default)
-                               $ext_name = '';
+                               $ext_name = ''; $ext_ver = '';
                                $title = getMessage('TASK_NO_TITLE');
 
                                // Shall I list SQL commands assigned to an extension installation or update task?
-                               if (((GET_EXT_VERSION('sql_patches') != '') && (getConfig('verbose_sql') == 'Y')) || (!EXT_IS_ACTIVE('sql_patches'))) {
+                               // OLD WAY: if (((GET_EXT_VERSION('sql_patches') != '') && (getConfig('verbose_sql') == 'Y')) || (!EXT_IS_ACTIVE('sql_patches'))) {
+                               if ((getConfig('verbose_sql') == 'Y')) {
+                                       // Extract extension name from subject
                                        $ext_name = substr($subj, 1, strpos($subj, ':') - 1);
+
+                                       // Update task or extension task?
                                        if ($type == 'EXTENSION') {
+                                               // @TODO Should we init sql_notes here or inside REGISTER_EXTENSION()?
+                                               EXT_INIT_NOTES();
+
                                                // Load SQL commands for registering
                                                REGISTER_EXTENSION($ext_name, $id, true);
 
@@ -183,7 +205,7 @@ LIMIT 1",
                                                // Prepare extension name and version
                                                $ext_name = substr($ext_name, 7);
                                                $ext_name = substr($ext_name, 0, strpos($ext_name, '-'));
-                                               $test = '[UPDATE-'.$ext_name.'-';
+                                               $test = '[UPDATE-' . $ext_name . '-';
                                                $ext_ver = substr($subj, strlen($test));
                                                $ext_ver = substr($ext_ver, 0, strpos($ext_ver, ':'));
 
@@ -200,17 +222,21 @@ LIMIT 1",
                                                $ext_name = '';
                                        }
 
-                                       // Add SQLs to a table
+                                       // Auto-init SQL handler
                                        if (!IS_SQLS_VALID()) INIT_SQLS();
-                                       if (empty($title)) $title = '';
+
+                                       // Add SQLs to a table
                                        if ((!empty($ext_name)) && (GET_EXT_VERSION('sql_patches')) && (getConfig('verbose_sql') == 'Y')) {
                                                // Add verbose SQL table
-                                               $text .= EXTENSION_VERBOSE_TABLE($title, " class=\"admin_table top2 left2 right2\"", true, "100%")."<br />\n";
+                                               $text .= EXTENSION_VERBOSE_TABLE($title, " class=\"admin_table top2 left2 right2\"", true, '100%')."<br />\n";
                                        } // END - if
-                               } else {
+                               } elseif ((!empty($ext_name)) && (!empty($ext_ver))) {
                                        // Run SQL commands in dry mode but only return the notes
                                        EXTENSION_UPDATE($ext_name, $ext_ver, true);
                                        $text .= EXT_GET_NOTES();
+                               } else {
+                                       // This should not normally happen!
+                                       debug_report_bug('ext_name(' . $ext_name . ') or ext_ver(' . $ext_ver . ') is empty! sql_patches=' . GET_EXT_VERSION('sql_patches') . '/verbose_sql=' . getConfig('verbose_sql'));
                                }
 
                                // Prepare array for the template
@@ -228,8 +254,7 @@ LIMIT 1",
 
                                // Which task do we actually have here?
                                // @TODO Rewrite this to something with include files
-                               switch ($type)
-                               {
+                               switch ($type) {
                                        case 'EXTENSION': // Install new extensions
                                                $ext_name = substr($subj, 1, strpos($subj, ':') - 1);
                                                $result_lines = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
@@ -267,8 +292,7 @@ LIMIT 1",
 
                                        case 'SUPPORT_MEMBER': // Assign on member's support request
                                                // @TODO This may also be rewritten to include files
-                                               switch ($mode)
-                                               {
+                                               switch ($mode) {
                                                        default: // @TODO Unknown support mode
                                                        DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown support mode %s detected. This part is under construction!", $mode));
                                                        $OUT .= "<div class=\"admin_failed medium\">".sprintf(getMessage('ADMIN_UNKNOWN_SUPPORT_MODE'), $mode)."</div>\n";
@@ -332,20 +356,20 @@ LIMIT 1",
 
                                                        case 'NL_UNSUBSCRIBE': // Newsletter unsubscriptions
                                                                $result = SQL_QUERY_ESC("SELECT nl_timespan FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
-                                                               array(bigintval($uid)), __FILE__, __LINE__);
+                                                                       array(bigintval($uid)), __FILE__, __LINE__);
                                                                list($span) = SQL_FETCHROW($result);
                                                                SQL_FREERESULT($result);
 
                                                                if ($span > 0) {
                                                                        // Undone unscubscribe request
                                                                        $content = array(
-                                                       'uid' => $uid,
-                                                       'id'  => $tid
+                                                                               'uid' => $uid,
+                                                                               'id'  => $tid
                                                                        );
                                                                        $OUT .= LOAD_TEMPLATE('admin_newsletter_tsk', true, $content);
                                                                } else {
                                                                        // Already unsubscribed
-                                                                       $OUT .= "<div class=\"admin_failed medium\">".ADMIN_NL_UNSUBSCRIBE_ALREADY."</div>\n";
+                                                                       $OUT .= "<div class=\"admin_failed medium\">{--ADMIN_NL_UNSUBSCRIBE_ALREADY--}</div>\n";
                                                                }
                                                                break;
 
@@ -358,6 +382,11 @@ LIMIT 1",
   <td width=\"1%\" class=\"switch_sw".$SW." bottom2 right2\">&nbsp;</td>
 </tr>\n";
                        } // END - if
+
+                       // Free result
+                       SQL_FREERESULT($result_task);
+
+                       // Switch colors
                        $SW = 3 - $SW;
                } // END - foreach
                define('__TASK_ROWS', $OUT);
@@ -396,8 +425,8 @@ LIMIT 1",
                        $content['assigned_admin'] = generateAdminLink($content['assigned_admin']);
 
                        // Generate infos
-                       switch ($content['task_type'])
-                       {
+                       // @TODO Try to move this in includes
+                       switch ($content['task_type']) {
                                case 'EXTENSION':
                                case 'EXTENSION_UPDATE':
                                        $content['infos'] = substr($content['subject'], 1, strpos($content['subject'], ':') - 1);