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
|
|
|
)
|
|
|
|
|
2020-03-21 07:44:33 +00:00
|
|
|
func Plugins(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError {
|
|
|
|
basePage, 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-01-02 21:46:24 +00:00
|
|
|
var plList []interface{}
|
|
|
|
for _, pl := range c.Plugins {
|
|
|
|
plList = append(plList, pl)
|
2018-07-29 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 21:46:24 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage, "", "", "panel_plugins", c.PanelPage{basePage, 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
|
|
|
|
}
|