Automatic deactivation of deprecated extensions added
[mailer.git] / inc / extensions.php
index 0063ac099786760e5be62c34ecc80122314d61a3..68f89a1954b3e8689e59ace083a41de4d71f5117 100644 (file)
@@ -37,8 +37,8 @@ if (!defined('__SECURITY')) {
        require($INC);
 }
 
-// Load the extension and maybe found language and function files
-function LOAD_EXTENSION ($ext_name) {
+// Load the extension and maybe found language and function files.
+function LOAD_EXTENSION ($ext_name, $EXT_LOAD_MODE = "") {
        global $EXT_LOADED, $_CONFIG, $CSS, $cacheMode;
 
        // Is the extension already loaded?
@@ -81,13 +81,22 @@ function LOAD_EXTENSION ($ext_name) {
                require($funcsInclude);
        } // END - if
 
-       // Set extension load mode to nothing (default)
-       $EXT_LOAD_MODE = "";
+       // Extensions are not deprecated by default
+       $EXT_DEPRECATED = "N";
 
        // Include the extension file
        //* DEBUG: */ echo "Extension loaded.<br />\n";
        require($extInclude);
 
+       // Is this extension deprecated?
+       if ($EXT_DEPRECATED == "Y") {
+               // Deactivate the extension
+               DEACTIVATE_EXTENSION($ext_name);
+
+               // Abort here
+               return false;
+       } // END - if
+
        // Mark it as loaded
        $EXT_LOADED[$ext_name] = true;
 
@@ -267,7 +276,7 @@ function EXTENSION_REGISTER ($ext_name, $id, $dry_run=false) {
                                } // END - if
 
                                // Register extension
-                               $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_extensions (ext_name, ext_active, ext_version) VALUES ('%s','%s','%s','%s')",
+                               $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_extensions (ext_name, ext_active, ext_version) VALUES ('%s','%s','%s')",
                                 array($ext_name, $EXT_ALWAYS_ACTIVE, $EXT_VERSION), __FILE__, __LINE__);
 
                                // Update task management
@@ -478,8 +487,7 @@ function GET_EXT_VERSION ($ext_name) {
        return $ret;
 }
 //
-function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false)
-{
+function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false) {
        // This shall never do a non-admin user!
        global $cacheInstance, $_CONFIG, $NOTES;
 
@@ -501,15 +509,13 @@ function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false)
        }
 
        // Check if version is updated
-       if ((($EXT_VERSION != $EXT_VER) || ($dry_run)) && (is_array($EXT_VER_HISTORY)))
-       {
+       if ((($EXT_VERSION != $EXT_VER) || ($dry_run)) && (is_array($EXT_VER_HISTORY))) {
                // Search for starting point
                $start = array_search($EXT_VER, $EXT_VER_HISTORY);
                $NOTES = "";
 
                // And load SQL queries in order of version history
-               for ($idx = ($start + 1); $idx < sizeof($EXT_VER_HISTORY); $idx++)
-               {
+               for ($idx = ($start + 1); $idx < sizeof($EXT_VER_HISTORY); $idx++) {
                        // Remove old SQLs array to prevent possible bugs
                        if (!$dry_run) { unset($SQLs); $SQLs = array(); }
 
@@ -520,25 +526,19 @@ function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false)
                        include(PATH."inc/extensions/".$file);
 
                        // Add notes
-                       if ($_CONFIG['verbose_sql'] == "Y")
-                       {
+                       if ($_CONFIG['verbose_sql'] == "Y") {
                                $EXT_VER = $EXT_VER_HISTORY[$idx];
-                               if (!empty($UPDATE_NOTES))
-                               {
+                               if (!empty($UPDATE_NOTES)) {
                                        // Update notes found
                                        $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
                                        $UPDATE_NOTES = "";
-                               }
-                                elseif ($EXT_VER == "0.0")
-                               {
+                               } elseif ($EXT_VER == "0.0") {
                                        // Initial release
                                        $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
-                               }
-                                else
-                               {
+                               } else {
                                        $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
                                }
-                       }
+                       } // END - if
 
                        // In real-mode execute any existing includes
                        if ((!$dry_run) && (count($INC_POOL) > 0)) {
@@ -570,10 +570,9 @@ function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false)
                                // Remove SQLs if extension is not installed
                                $SQLs = array();
                        }
-               }
+               } // END - for
 
-               if (!$dry_run)
-               {
+               if (!$dry_run) {
                        // In normal mode insert task and update extension's version...
                        $ext_subj = "[UPDATE-".$ext."-".$EXT_VERSION.":] ".ADMIN_UPDATE_EXT_SUBJ;
 
@@ -682,7 +681,7 @@ function GET_EXT_ID($name) {
        } elseif (!EXT_IS_ACTIVE("cache")) {
                // Load from database
                $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
-                array($name), __FILE__, __LINE__);
+                       array($name), __FILE__, __LINE__);
                list($ret) = SQL_FETCHROW($result);
                SQL_FREERESULT($result);
        }
@@ -692,7 +691,7 @@ function GET_EXT_ID($name) {
 }
 
 // Activate given extension
-function ACTIVATE_EXTENSION($ext_name) {
+function ACTIVATE_EXTENSION ($ext_name) {
        // Activate the extension
        $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='Y' WHERE ext_name='%s' LIMIT 1",
                array($ext_name), __FILE__, __LINE__);
@@ -704,6 +703,25 @@ function ACTIVATE_EXTENSION($ext_name) {
        } // END - if
 }
 
+// Deactivate given extension
+function DEACTIVATE_EXTENSION($ext_name) {
+       // Activate the extension
+       $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='N' WHERE ext_name='%s' LIMIT 1",
+               array($ext_name), __FILE__, __LINE__);
+
+       // Extension has been activated?
+       if (SQL_AFFECTEDROWS() == 1) {
+               // Then run all queries
+               EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "deactivate");
+
+               // Create new task
+               CREATE_EXTENSION_DEACTIVATION_TASK($ext_name);
+
+               // Notify the admin
+               SEND_ADMIN_NOTIFICATION(ADMIN_SUBJECT_EXTENSION_DEACTIVATED, "admin_ext_deactivated", array('ext_name' => $ext_name));
+       } // END - if
+}
+
 // Checks wether the extension is older than given
 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
        // Get current extension version
@@ -716,5 +734,81 @@ function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
        // Now compare both and return the result
        return ($currVersion < $ext_ver);
 }
+
+// Creates a new task for updated extension
+function CREATE_EXTENSION_UPDATE_TASK ($admin_id, $subject, $notes) {
+       // Check if task is not there
+       $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE subject='%s' LIMIT 1",
+               array($subject), __FILE__, __LINE__);
+       if (SQL_NUMROWS($result) == 0) {
+               // Task not created so it's a brand-new extension which we need to register and create a task for!
+               $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, userid, status, task_type, subject, text, task_created) VALUES ('%s','0','NEW','EXTENSION_UPDATE','%s','%s', UNIX_TIMESTAMP())",
+                       array($admin_id, $subject, $notes), __FILE__, __LINE__);
+       } // END - if
+
+       // Free memory
+       SQL_FREERESULT($result);
+}
+
+// Creates a new task for newly installed extension
+function CREATE_NEW_EXTENSION_TASK ($admin_id, $subject, $ext) {
+       // Not installed and do we have created a task for the admin?
+       $result = SQL_QUERY_ESC("SELECT `id` FROM `"._MYSQL_PREFIX."_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
+               array($subject), __FILE__, __LINE__);
+       if ((SQL_NUMROWS($result) == 0) && (GET_EXT_VERSION($ext) == "")) {
+               // Template file
+               $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
+                       PATH,
+                       GET_LANGUAGE(),
+                       $ext
+               );
+
+               // Load text for task
+               if (FILE_READABLE($tpl)) {
+                       // Load extension's own text template (HTML!)
+                       $msg = LOAD_TEMPLATE("ext_".$ext, true);
+               } else {
+                       // Load default message
+                       $msg = LOAD_TEMPLATE("admin_new_ext", "", 0);
+               }
+
+               // Task not created so it's a brand-new extension which we need to register and create a task for!
+               $result_insert = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, userid, status, task_type, subject, text, task_created)
+VALUES (%s,0,'NEW','EXTENSION','%s','%s',UNIX_TIMESTAMP())",
+                       array(
+                               $admin_id,
+                               $subject,
+                               addslashes($msg),
+                       ),  __FILE__, __LINE__, true, false
+               );
+       } // END - if
+
+       // Free memory
+       SQL_FREERESULT($result);
+}
+
+// Creates a task for automatically deactivated (deprecated) extension
+function CREATE_EXTENSION_DEACTIVATION_TASK ($ext) {
+       // Create subject line
+       $subject = sprintf("[%s:] %s", $ext, TASK_SUBJ_EXTENSION_DEACTIVATED);
+
+       // Not installed and do we have created a task for the admin?
+       $result = SQL_QUERY_ESC("SELECT `id` FROM `"._MYSQL_PREFIX."_task_system` WHERE `subject` = '%s' LIMIT 1",
+               array($subject), __FILE__, __LINE__);
+       if ((SQL_NUMROWS($result) == 0) && (GET_EXT_VERSION($ext) != "")) {
+               // Task not created so add it
+               $result_insert = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, userid, status, task_type, subject, text, task_created)
+VALUES (0,0,'NEW','EXTENSION_DEACTIVATION','%s','%s',UNIX_TIMESTAMP())",
+                       array(
+                               $subject,
+                               addslashes(LOAD_TEMPLATE("task_ext_deactivated", true, $ext)),
+                       ),  __FILE__, __LINE__, true, false
+               );
+       } // END - if
+
+       // Free memory
+       SQL_FREERESULT($result);
+}
+
 //
 ?>