2018-07-29 10:54:12 +00:00
|
|
|
package panel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
2022-02-21 03:53:13 +00:00
|
|
|
c "git.tuxpa.in/a/gosora/common"
|
2018-07-29 10:54:12 +00:00
|
|
|
)
|
|
|
|
|
2020-03-21 07:44:33 +00:00
|
|
|
func Plugins(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError {
|
2021-04-29 22:33:02 +00:00
|
|
|
bp, ferr := buildBasePage(w, r, u, "plugins", "plugins")
|
2018-07-29 10:54:12 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2020-03-21 07:44:33 +00:00
|
|
|
if !u.Perms.ManagePlugins {
|
|
|
|
return c.NoPermissions(w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 22:33:02 +00:00
|
|
|
plList, i := make([]interface{}, len(c.Plugins)), 0
|
2021-01-02 21:46:24 +00:00
|
|
|
for _, pl := range c.Plugins {
|
2021-04-29 22:33:02 +00:00
|
|
|
plList[i] = pl
|
|
|
|
i++
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 22:33:02 +00:00
|
|
|
return renderTemplate("panel", w, r, bp.Header, c.Panel{bp, "", "", "panel_plugins", c.PanelPage{bp, plList, 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
|
2020-07-14 21:50:29 +00:00
|
|
|
func PluginsActivate(w http.ResponseWriter, r *http.Request, u *c.User, uname string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManagePlugins {
|
|
|
|
return c.NoPermissions(w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
pl, ok := c.Plugins[uname]
|
2018-07-29 10:54:12 +00:00
|
|
|
if !ok {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError("The plugin isn't registered in the system", w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
if pl.Installable && !pl.Installed {
|
|
|
|
return c.LocalError("You can't activate this plugin without installing it first", w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
active, err := pl.BypassActive()
|
|
|
|
hasPlugin, err2 := pl.InDatabase()
|
2018-07-29 10:54:12 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
if pl.Activate != nil {
|
|
|
|
err = pl.Activate(pl)
|
2018-07-29 10:54:12 +00:00
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError(err.Error(), w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasPlugin {
|
|
|
|
if active {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError("The plugin is already active", w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
err = pl.SetActive(true)
|
2018-07-29 10:54:12 +00:00
|
|
|
} else {
|
2020-07-14 21:50:29 +00:00
|
|
|
err = pl.AddToDatabase(true, false)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
log.Printf("Activating plugin '%s'", pl.Name)
|
|
|
|
err = pl.Init(pl)
|
2018-07-29 10:54:12 +00:00
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError(err.Error(), w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
err = c.AdminLogs.CreateExtra("activate", 0, "plugin", u.GetIP(), u.ID, c.SanitiseSingleLine(pl.Name))
|
2019-11-10 02:37:53 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func PluginsDeactivate(w http.ResponseWriter, r *http.Request, u *c.User, uname string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManagePlugins {
|
|
|
|
return c.NoPermissions(w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
pl, ok := c.Plugins[uname]
|
2018-07-29 10:54:12 +00:00
|
|
|
if !ok {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError("The plugin isn't registered in the system", w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
log.Printf("plugin: %+v\n", pl)
|
2018-07-29 10:54:12 +00:00
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
active, err := pl.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 {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError("The plugin you're trying to deactivate isn't active", w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
err = pl.SetActive(false)
|
2018-07-29 10:54:12 +00:00
|
|
|
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
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
if pl.Deactivate != nil {
|
|
|
|
pl.Deactivate(pl)
|
2018-07-29 13:02:48 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
err = c.AdminLogs.CreateExtra("deactivate", 0, "plugin", u.GetIP(), u.ID, c.SanitiseSingleLine(pl.Name))
|
2019-11-10 02:37:53 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
func PluginsInstall(w http.ResponseWriter, r *http.Request, u *c.User, uname string) c.RouteError {
|
|
|
|
_, ferr := c.SimplePanelUserCheck(w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
if !u.Perms.ManagePlugins {
|
|
|
|
return c.NoPermissions(w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
pl, ok := c.Plugins[uname]
|
2018-07-29 10:54:12 +00:00
|
|
|
if !ok {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError("The plugin isn't registered in the system", w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
if !pl.Installable {
|
|
|
|
return c.LocalError("This plugin is not installable", w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
if pl.Installed {
|
|
|
|
return c.LocalError("This plugin has already been installed", w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
active, err := pl.BypassActive()
|
|
|
|
hasPlugin, err2 := pl.InDatabase()
|
2018-07-29 10:54:12 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
if pl.Install != nil {
|
|
|
|
err = pl.Install(pl)
|
2018-07-29 10:54:12 +00:00
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError(err.Error(), w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
if pl.Activate != nil {
|
|
|
|
err = pl.Activate(pl)
|
2018-07-29 10:54:12 +00:00
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError(err.Error(), w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasPlugin {
|
2020-07-14 21:50:29 +00:00
|
|
|
err = pl.SetInstalled(true)
|
2018-07-29 10:54:12 +00:00
|
|
|
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
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
err = pl.SetActive(true)
|
2018-07-29 10:54:12 +00:00
|
|
|
} else {
|
2020-07-14 21:50:29 +00:00
|
|
|
err = pl.AddToDatabase(true, true)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-14 21:50:29 +00:00
|
|
|
log.Printf("Installing plugin '%s'", pl.Name)
|
|
|
|
err = pl.Init(pl)
|
2018-07-29 10:54:12 +00:00
|
|
|
if err != nil {
|
2020-07-14 21:50:29 +00:00
|
|
|
return c.LocalError(err.Error(), w, r, u)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
2020-07-14 21:50:29 +00:00
|
|
|
err = c.AdminLogs.CreateExtra("install", 0, "plugin", u.GetIP(), u.ID, c.SanitiseSingleLine(pl.Name))
|
2019-11-10 02:37:53 +00:00
|
|
|
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
|
|
|
|
}
|