package panel import ( "context" "encoding/json" "errors" "fmt" "net/http" "net/url" "regexp" "strings" "sync" ) var phpSoftwarePattern = regexp.MustCompile(`^php-[5-9]\.[0-9]$`) var phpPatchNumberPattern = regexp.MustCompile(`^[0-9]{1,4}$`) var phpPatchVersionPattern = regexp.MustCompile(`^[5-9]\.[0-9]\.[0-9]+$`) func (c *Client) phpRuntimePatches(ctx context.Context, branches []string) []RuntimePatch { results := make([]*RuntimePatch, len(branches)) var wg sync.WaitGroup for index, branch := range branches { if !phpSoftwarePattern.MatchString("php-" + branch) { continue } wg.Go(func() { plugin, err := c.runtimePlugin(ctx, "php-"+branch, "PHP") if err != nil { return } for _, release := range plugin.Versions { latest := release.MVersion + "." + release.Version if release.MVersion != branch || release.Beta != 0 || !phpPatchVersionPattern.MatchString(latest) { continue } results[index] = &RuntimePatch{Branch: branch, Installed: plugin.MinVersion, Latest: latest, UpdateAvailable: plugin.MinVersion != "" && newerRuntimeVersion(latest, plugin.MinVersion)} break } }) } wg.Wait() items := make([]RuntimePatch, 0, len(branches)) for _, item := range results { if item != nil { items = append(items, *item) } } return items } func newerRuntimeVersion(candidate, installed string) bool { return candidate != installed && sortDottedVersionsDescending([]string{candidate, installed})[0] == candidate } func (c *Client) UpdatePHPRuntime(ctx context.Context, branch, target string) error { if !phpSoftwarePattern.MatchString("php-" + branch) { return errors.New("invalid PHP branch") } patches := c.phpRuntimePatches(ctx, []string{branch}) if len(patches) != 1 || !patches[0].UpdateAvailable || patches[0].Latest != target { return errors.New("PHP target is not the advertised stable patch") } _, err := c.call(ctx, http.MethodPost, "/plugin", url.Values{"action": {"install_plugin"}, "sName": {"php-" + branch}, "version": {branch}, "min_version": {strings.TrimPrefix(target, branch+".")}, "type": {"0"}, "upgrade": {"1"}}) return err } func (c *Client) PHPRuntimeUpdatePending(ctx context.Context, branch string) (bool, error) { if !phpSoftwarePattern.MatchString("php-" + branch) { return false, errors.New("invalid PHP branch") } name := "upgrade[php-" + branch + "]" // aaPanel searches tasks by status OR type, not by name. Fetch only // execution tasks, then match the exact managed PHP branch locally. body, err := c.request(ctx, http.MethodPost, "/data", url.Values{"action": {"getData"}, "table": {"tasks"}, "limit": {"1000"}, "p": {"1"}, "search": {"execshell"}}) if err != nil { return false, err } var response struct { Data []struct { Name string `json:"name"` Status any `json:"status"` } `json:"data"` } if json.Unmarshal(body, &response) != nil || response.Data == nil { return false, errors.New("cannot verify PHP update queue") } if len(response.Data) >= 1000 { return false, errors.New("PHP update queue exceeded verification limit") } for _, task := range response.Data { status := fmt.Sprint(task.Status) if task.Name == name && (status == "0" || status == "-1") { return true, nil } } return false, nil } func (c *Client) ManagedRuntime(context.Context, int64) (RuntimeSpec, error) { return RuntimeSpec{}, errors.New("managed runtime records require the panel broker") } func (c *Client) ReplaceManagedRuntime(context.Context, int64, RuntimeSpec) error { return errors.New("runtime replacement requires the panel broker") } func (c *BrokerClient) UpdatePHPRuntime(ctx context.Context, branch, target string) error { _, err := c.operation(ctx, brokerRequest{Operation: "update_php_runtime", Name: branch, Version: target}) return err } func (c *BrokerClient) PHPRuntimeUpdatePending(ctx context.Context, branch string) (bool, error) { r, err := c.operation(ctx, brokerRequest{Operation: "php_update_pending", Name: branch}) return r.Pending, err } func (c *BrokerClient) ManagedRuntime(ctx context.Context, id int64) (RuntimeSpec, error) { r, err := c.operation(ctx, brokerRequest{Operation: "managed_runtime", SiteID: id}) if err != nil { return RuntimeSpec{}, err } if r.Runtime == nil { return RuntimeSpec{}, errors.New("site has no deployed language project") } return *r.Runtime, nil } func (c *BrokerClient) ReplaceManagedRuntime(ctx context.Context, id int64, spec RuntimeSpec) error { _, err := c.operation(ctx, brokerRequest{Operation: "replace_runtime", SiteID: id, Runtime: &spec}) return err } func (c *BrokerClient) ResetManagedRuntime(ctx context.Context, id int64, name string) error { _, err := c.operation(ctx, brokerRequest{Operation: "reset_managed_runtime", SiteID: id, Name: name}) return err } func (c *Client) ResetManagedRuntime(context.Context, int64, string) error { return errors.New("runtime reset requires the panel broker") } func (c *Client) RemoveManagedProxy(ctx context.Context, domain string) error { exists := func() (bool, error) { raw, err := c.request(ctx, http.MethodPost, "/site", url.Values{"action": {"GetProxyList"}, "sitename": {domain}}) if err != nil { return false, err } var rows []struct { Site string `json:"sitename"` Name string `json:"proxyname"` } if json.Unmarshal(raw, &rows) != nil { return false, errors.New("cannot verify managed proxy records") } for _, row := range rows { if row.Site == domain && row.Name == "sea-vhost-runtime" { return true, nil } } return false, nil } found, err := exists() if err != nil || !found { return err } if _, err = c.call(ctx, http.MethodPost, "/site", url.Values{"action": {"RemoveProxy"}, "sitename": {domain}, "proxyname": {"sea-vhost-runtime"}}); err != nil { return err } found, err = exists() if err != nil { return err } if found { return errors.New("panel did not remove the managed proxy") } return nil }