2018-07-29 10:54:12 +00:00
|
|
|
package panel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2018-07-29 10:54:12 +00:00
|
|
|
)
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
func Plugins(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError {
|
2018-07-29 10:54:12 +00:00
|
|
|
basePage, ferr := buildBasePage(w, r, &user, "plugins", "plugins")
|
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !user.Perms.ManagePlugins {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.NoPermissions(w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var pluginList []interface{}
|
2019-04-19 08:20:10 +00:00
|
|
|
for _, plugin := range c.Plugins {
|
2018-07-29 10:54:12 +00:00
|
|
|
pluginList = append(pluginList, plugin)
|
|
|
|
}
|
|
|
|
|
2019-11-10 02:37:53 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "", "", "panel_plugins", c.PanelPage{basePage, pluginList, nil}})
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Abstract more of the plugin activation / installation / deactivation logic, so we can test all that more reliably and easily
|
2019-04-19 08:20:10 +00:00
|
|
|
func PluginsActivate(w http.ResponseWriter, r *http.Request, user c.User, uname string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, &user)
|
2018-07-29 10:54:12 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !user.Perms.ManagePlugins {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.NoPermissions(w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
plugin, ok := c.Plugins[uname]
|
2018-07-29 10:54:12 +00:00
|
|
|
if !ok {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("The plugin isn't registered in the system", w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
if plugin.Installable && !plugin.Installed {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("You can't activate this plugin without installing it first", w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
active, err := plugin.BypassActive()
|
|
|
|
hasPlugin, err2 := plugin.InDatabase()
|
|
|
|
if err != nil || err2 != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if plugin.Activate != nil {
|
2019-02-10 05:52:26 +00:00
|
|
|
err = plugin.Activate(plugin)
|
2018-07-29 10:54:12 +00:00
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError(err.Error(), w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasPlugin {
|
|
|
|
if active {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("The plugin is already active", w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
err = plugin.SetActive(true)
|
|
|
|
} else {
|
|
|
|
err = plugin.AddToDatabase(true, false)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Activating plugin '%s'", plugin.Name)
|
2019-02-10 05:52:26 +00:00
|
|
|
err = plugin.Init(plugin)
|
2018-07-29 10:54:12 +00:00
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError(err.Error(), w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
2019-11-10 02:37:53 +00:00
|
|
|
err = c.AdminLogs.CreateExtra("activate", 0, "plugin", user.LastIP, user.ID, c.SanitiseSingleLine(plugin.Name))
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2018-07-29 10:54:12 +00:00
|
|
|
|
|
|
|
http.Redirect(w, r, "/panel/plugins/", http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
func PluginsDeactivate(w http.ResponseWriter, r *http.Request, user c.User, uname string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, &user)
|
2018-07-29 10:54:12 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !user.Perms.ManagePlugins {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.NoPermissions(w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
plugin, ok := c.Plugins[uname]
|
2018-07-29 10:54:12 +00:00
|
|
|
if !ok {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("The plugin isn't registered in the system", w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
2018-07-29 13:02:48 +00:00
|
|
|
log.Printf("plugin: %+v\n", plugin)
|
2018-07-29 10:54:12 +00:00
|
|
|
|
|
|
|
active, err := plugin.BypassActive()
|
2018-07-29 13:02:48 +00:00
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-07-29 13:02:48 +00:00
|
|
|
} else if !active {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("The plugin you're trying to deactivate isn't active", w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.SetActive(false)
|
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
2018-07-29 13:02:48 +00:00
|
|
|
if plugin.Deactivate != nil {
|
2019-02-10 05:52:26 +00:00
|
|
|
plugin.Deactivate(plugin)
|
2018-07-29 13:02:48 +00:00
|
|
|
}
|
2019-11-10 02:37:53 +00:00
|
|
|
err = c.AdminLogs.CreateExtra("deactivate", 0, "plugin", user.LastIP, user.ID, c.SanitiseSingleLine(plugin.Name))
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2018-07-29 10:54:12 +00:00
|
|
|
|
|
|
|
http.Redirect(w, r, "/panel/plugins/", http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
func PluginsInstall(w http.ResponseWriter, r *http.Request, user c.User, uname string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, &user)
|
2018-07-29 10:54:12 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !user.Perms.ManagePlugins {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.NoPermissions(w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 08:20:10 +00:00
|
|
|
plugin, ok := c.Plugins[uname]
|
2018-07-29 10:54:12 +00:00
|
|
|
if !ok {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("The plugin isn't registered in the system", w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
if !plugin.Installable {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("This plugin is not installable", w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
if plugin.Installed {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError("This plugin has already been installed", w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
active, err := plugin.BypassActive()
|
|
|
|
hasPlugin, err2 := plugin.InDatabase()
|
|
|
|
if err != nil || err2 != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
if active {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(errors.New("An uninstalled plugin is still active"), w, r)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if plugin.Install != nil {
|
2019-02-10 05:52:26 +00:00
|
|
|
err = plugin.Install(plugin)
|
2018-07-29 10:54:12 +00:00
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError(err.Error(), w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if plugin.Activate != nil {
|
2019-02-10 05:52:26 +00:00
|
|
|
err = plugin.Activate(plugin)
|
2018-07-29 10:54:12 +00:00
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError(err.Error(), w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasPlugin {
|
|
|
|
err = plugin.SetInstalled(true)
|
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
err = plugin.SetActive(true)
|
|
|
|
} else {
|
|
|
|
err = plugin.AddToDatabase(true, true)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Installing plugin '%s'", plugin.Name)
|
2019-02-10 05:52:26 +00:00
|
|
|
err = plugin.Init(plugin)
|
2018-07-29 10:54:12 +00:00
|
|
|
if err != nil {
|
2019-04-19 08:20:10 +00:00
|
|
|
return c.LocalError(err.Error(), w, r, user)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
2019-11-10 02:37:53 +00:00
|
|
|
err = c.AdminLogs.CreateExtra("install", 0, "plugin", user.LastIP, user.ID, c.SanitiseSingleLine(plugin.Name))
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2018-07-29 10:54:12 +00:00
|
|
|
|
|
|
|
http.Redirect(w, r, "/panel/plugins/", http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|