Vigiar
1
0
Derivar
Você já tinha feito uma derivação do repositório wordpress-account-plugin, anteriormente
0
wordpress-account-plugin/mozout-account/includes/class-actions.php
2026-12-01 14:09:10 +02:00

212 linhas
10 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit;
}
/**
* The simple actions of the account screen. Each one is a POST with a nonce, only for administrators,
* sent to the API with an Idempotency-Key where the API asks for it, and noted in the activity log.
*/
class MOZOUT_ACCOUNT_Actions
{
public static function init()
{
foreach (array('refresh', 'cpanel', 'power', 'autorenew', 'lock', 'pay', 'pdf', 'reply', 'ticket') as $a) {
add_action('admin_post_mozout_account_' . $a, array(__CLASS__, $a));
}
}
private static function back()
{
$back = isset($_POST['back']) ? esc_url_raw(wp_unslash($_POST['back'])) : '';
return (strpos($back, admin_url()) === 0) ? $back : MOZOUT_ACCOUNT_Admin::url();
}
private static function done($ok, $text, $area, $extra = array())
{
MOZOUT_ACCOUNT_Log::add($area, $ok === true, $text, $extra);
MOZOUT_ACCOUNT_Data::forget();
MOZOUT_ACCOUNT_Admin::redirect('', $ok, $text, array(), self::back());
}
private static function sim($res)
{
return is_array($res) && !empty($res['data']['simulated']);
}
private static function sim_text($text, $res)
{
return self::sim($res) ? $text . ' ' . __('(Sandbox: simulated, nothing changed.)', 'mozout-account') : $text;
}
public static function refresh()
{
MOZOUT_ACCOUNT_Admin::check('mozout_account_refresh');
MOZOUT_ACCOUNT_Data::forget();
wp_safe_redirect(self::back());
exit;
}
public static function cpanel()
{
MOZOUT_ACCOUNT_Admin::check('mozout_account_cpanel');
$id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
$res = MOZOUT_ACCOUNT_Api::request('POST', '/v1/services/' . $id . '/cpanel-login', null, array('timeout' => 30));
if (is_wp_error($res)) {
self::done(false, $res->get_error_message(), 'hosting', array('service' => $id));
}
$url = isset($res['data']['url']) ? (string) $res['data']['url'] : '';
MOZOUT_ACCOUNT_Log::add('hosting', true, __('Control panel link created.', 'mozout-account'), array('service' => $id));
if ($url === '' || strpos($url, 'https://') !== 0) {
MOZOUT_ACCOUNT_Admin::redirect('', 'warning', __('The API accepted the request but gave no link (Sandbox simulates it).', 'mozout-account'), array(), self::back());
}
// A link that works once: open it now, never store it.
wp_redirect($url);
exit;
}
public static function power()
{
MOZOUT_ACCOUNT_Admin::check('mozout_account_power');
$id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
$op = isset($_POST['op']) ? sanitize_key(wp_unslash($_POST['op'])) : '';
if (!in_array($op, array('start', 'stop', 'restart'), true)) {
wp_die('bad action', 400);
}
$res = MOZOUT_ACCOUNT_Api::request('POST', '/v1/servers/' . $id . '/' . $op, null, array('timeout' => 30, 'idempotency_key' => 'mozout-account-' . $op . '-' . $id . '-' . gmdate('YmdHi')));
if (is_wp_error($res)) {
self::done(false, $res->get_error_message(), 'servers', array('server' => $id, 'action' => $op));
}
$texts = array(
'start' => __('The server is starting.', 'mozout-account'),
'stop' => __('The server is stopping.', 'mozout-account'),
'restart' => __('The server is restarting.', 'mozout-account'),
);
self::done(true, self::sim_text($texts[$op], $res), 'servers', array('server' => $id, 'action' => $op));
}
private static function domain_param()
{
$d = isset($_POST['domain']) ? strtolower(preg_replace('/[^A-Za-z0-9.\-]/', '', (string) wp_unslash($_POST['domain']))) : '';
if ($d === '' || strpos($d, '.') === false) {
wp_die('bad domain', 400);
}
return $d;
}
public static function autorenew()
{
MOZOUT_ACCOUNT_Admin::check('mozout_account_autorenew');
$d = self::domain_param();
$on = !empty($_POST['on']);
$res = MOZOUT_ACCOUNT_Api::request('PUT', '/v1/domains/' . rawurlencode($d) . '/auto-renew', array('auto_renew' => $on), array('timeout' => 20));
if (is_wp_error($res)) {
self::done(false, $res->get_error_message(), 'domains', array('domain' => $d));
}
self::done(true, self::sim_text($on ? __('Auto-renewal turned on.', 'mozout-account') : __('Auto-renewal turned off.', 'mozout-account'), $res), 'domains', array('domain' => $d));
}
public static function lock()
{
MOZOUT_ACCOUNT_Admin::check('mozout_account_lock');
$d = self::domain_param();
$on = !empty($_POST['on']);
$res = MOZOUT_ACCOUNT_Api::request('PUT', '/v1/domains/' . rawurlencode($d) . '/lock', array('locked' => $on), array('timeout' => 30));
if (is_wp_error($res)) {
self::done(false, $res->get_error_message(), 'domains', array('domain' => $d));
}
self::done(true, self::sim_text($on ? __('Transfer lock turned on.', 'mozout-account') : __('Transfer lock removed.', 'mozout-account'), $res), 'domains', array('domain' => $d));
}
public static function pay()
{
MOZOUT_ACCOUNT_Admin::check('mozout_account_pay');
$id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
$res = MOZOUT_ACCOUNT_Api::request('POST', '/v1/invoices/' . $id . '/pay', null, array('timeout' => 30, 'idempotency_key' => 'mozout-account-pay-' . $id . '-' . substr(md5(home_url()), 0, 8)));
if (is_wp_error($res)) {
self::done(false, $res->get_error_message(), 'invoices', array('invoice' => $id));
}
$d = $res['data'];
self::done(true, self::sim_text(sprintf(
/* translators: 1: amount, 2: credit left */
__('Invoice paid with credit: %1$s. Credit left: %2$s.', 'mozout-account'),
MOZOUT_ACCOUNT_Admin::money(isset($d['amount_charged']) ? $d['amount_charged'] : 0, isset($d['currency']) ? $d['currency'] : ''),
MOZOUT_ACCOUNT_Admin::money(isset($d['credit_balance']) ? $d['credit_balance'] : 0, isset($d['currency']) ? $d['currency'] : '')
), $res), 'invoices', array('invoice' => $id));
}
public static function pdf()
{
MOZOUT_ACCOUNT_Admin::check('mozout_account_pdf');
$id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
$res = MOZOUT_ACCOUNT_Api::request('GET', '/v1/invoices/' . $id . '/pdf', null, array('timeout' => 40));
if (is_wp_error($res)) {
self::done(false, $res->get_error_message(), 'invoices', array('invoice' => $id));
}
$d = $res['data'];
if (empty($d['pdf_available']) || empty($d['content'])) {
if (!empty($d['url']) && strpos($d['url'], 'https://') === 0) {
wp_redirect($d['url']);
exit;
}
self::done(false, __('The PDF of this invoice is not available.', 'mozout-account'), 'invoices', array('invoice' => $id));
}
$bin = base64_decode((string) $d['content'], true);
if ($bin === false) {
self::done(false, __('The PDF of this invoice is not available.', 'mozout-account'), 'invoices', array('invoice' => $id));
}
$name = sanitize_file_name(!empty($d['filename']) ? $d['filename'] : 'invoice-' . $id . '.pdf');
nocache_headers();
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $name . '"');
header('Content-Length: ' . strlen($bin));
echo $bin; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
exit;
}
public static function reply()
{
MOZOUT_ACCOUNT_Admin::check('mozout_account_reply');
$id = isset($_POST['id']) ? preg_replace('/[^A-Za-z0-9\-]/', '', (string) wp_unslash($_POST['id'])) : '';
$msg = isset($_POST['message']) ? sanitize_textarea_field(wp_unslash($_POST['message'])) : '';
if ($id === '' || trim($msg) === '') {
self::done(false, __('Write the message first.', 'mozout-account'), 'tickets');
}
$res = MOZOUT_ACCOUNT_Api::request('POST', '/v1/tickets/' . rawurlencode($id) . '/replies', array('message' => $msg), array('timeout' => 30, 'idempotency_key' => 'mozout-account-reply-' . md5($id . '|' . $msg)));
if (is_wp_error($res)) {
self::done(false, $res->get_error_message(), 'tickets', array('ticket' => $id));
}
self::done(true, self::sim_text(__('Reply sent.', 'mozout-account'), $res), 'tickets', array('ticket' => $id));
}
public static function ticket()
{
MOZOUT_ACCOUNT_Admin::check('mozout_account_ticket');
$p = wp_unslash($_POST);
$body = array(
'department_id' => isset($p['department_id']) ? (int) $p['department_id'] : 0,
'subject' => sanitize_text_field(isset($p['subject']) ? $p['subject'] : ''),
'message' => sanitize_textarea_field(isset($p['message']) ? $p['message'] : ''),
'priority' => isset($p['priority']) && in_array($p['priority'], array('low', 'medium', 'high'), true) ? $p['priority'] : 'medium',
);
$about = isset($p['about']) ? (string) $p['about'] : '0';
if (preg_match('/^s(\d+)$/', $about, $m)) {
$body['service_id'] = (int) $m[1];
} elseif (preg_match('/^d(\d+)$/', $about, $m)) {
$body['domain_id'] = (int) $m[1];
}
if (!$body['department_id'] || $body['subject'] === '' || trim($body['message']) === '') {
self::done(false, __('Fill in the department, the subject and the message.', 'mozout-account'), 'tickets');
}
$res = MOZOUT_ACCOUNT_Api::request('POST', '/v1/tickets', $body, array('timeout' => 30, 'idempotency_key' => 'mozout-account-ticket-' . md5(wp_json_encode($body))));
if (is_wp_error($res)) {
self::done(false, $res->get_error_message(), 'tickets');
}
$tid = isset($res['data']['id']) ? (string) $res['data']['id'] : '';
self::done(true, self::sim_text($tid !== '' ? sprintf(
/* translators: %s: ticket number */
__('Ticket #%s opened.', 'mozout-account'),
$tid
) : __('Ticket opened.', 'mozout-account'), $res), 'tickets', array('ticket' => $tid));
}
}