212 lines
11 KiB
PHP
212 lines
11 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 INTERWEB_MZ_ACCOUNT_Actions
|
|
{
|
|
public static function init()
|
|
{
|
|
foreach (array('refresh', 'cpanel', 'power', 'autorenew', 'lock', 'pay', 'pdf', 'reply', 'ticket') as $a) {
|
|
add_action('admin_post_interweb_mz_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 : INTERWEB_MZ_ACCOUNT_Admin::url();
|
|
}
|
|
|
|
private static function done($ok, $text, $area, $extra = array())
|
|
{
|
|
INTERWEB_MZ_ACCOUNT_Log::add($area, $ok === true, $text, $extra);
|
|
INTERWEB_MZ_ACCOUNT_Data::forget();
|
|
INTERWEB_MZ_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.)', 'interweb-mz-account') : $text;
|
|
}
|
|
|
|
public static function refresh()
|
|
{
|
|
INTERWEB_MZ_ACCOUNT_Admin::check('interweb_mz_account_refresh');
|
|
INTERWEB_MZ_ACCOUNT_Data::forget();
|
|
wp_safe_redirect(self::back());
|
|
exit;
|
|
}
|
|
|
|
public static function cpanel()
|
|
{
|
|
INTERWEB_MZ_ACCOUNT_Admin::check('interweb_mz_account_cpanel');
|
|
$id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
|
|
$res = INTERWEB_MZ_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'] : '';
|
|
INTERWEB_MZ_ACCOUNT_Log::add('hosting', true, __('Control panel link created.', 'interweb-mz-account'), array('service' => $id));
|
|
if ($url === '' || strpos($url, 'https://') !== 0) {
|
|
INTERWEB_MZ_ACCOUNT_Admin::redirect('', 'warning', __('The API accepted the request but gave no link (Sandbox simulates it).', 'interweb-mz-account'), array(), self::back());
|
|
}
|
|
// A link that works once: open it now, never store it.
|
|
wp_redirect($url);
|
|
exit;
|
|
}
|
|
|
|
public static function power()
|
|
{
|
|
INTERWEB_MZ_ACCOUNT_Admin::check('interweb_mz_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 = INTERWEB_MZ_ACCOUNT_Api::request('POST', '/v1/servers/' . $id . '/' . $op, null, array('timeout' => 30, 'idempotency_key' => 'interweb-mz-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.', 'interweb-mz-account'),
|
|
'stop' => __('The server is stopping.', 'interweb-mz-account'),
|
|
'restart' => __('The server is restarting.', 'interweb-mz-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()
|
|
{
|
|
INTERWEB_MZ_ACCOUNT_Admin::check('interweb_mz_account_autorenew');
|
|
$d = self::domain_param();
|
|
$on = !empty($_POST['on']);
|
|
$res = INTERWEB_MZ_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.', 'interweb-mz-account') : __('Auto-renewal turned off.', 'interweb-mz-account'), $res), 'domains', array('domain' => $d));
|
|
}
|
|
|
|
public static function lock()
|
|
{
|
|
INTERWEB_MZ_ACCOUNT_Admin::check('interweb_mz_account_lock');
|
|
$d = self::domain_param();
|
|
$on = !empty($_POST['on']);
|
|
$res = INTERWEB_MZ_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.', 'interweb-mz-account') : __('Transfer lock removed.', 'interweb-mz-account'), $res), 'domains', array('domain' => $d));
|
|
}
|
|
|
|
public static function pay()
|
|
{
|
|
INTERWEB_MZ_ACCOUNT_Admin::check('interweb_mz_account_pay');
|
|
$id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
|
|
$res = INTERWEB_MZ_ACCOUNT_Api::request('POST', '/v1/invoices/' . $id . '/pay', null, array('timeout' => 30, 'idempotency_key' => 'interweb-mz-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.', 'interweb-mz-account'),
|
|
INTERWEB_MZ_ACCOUNT_Admin::money(isset($d['amount_charged']) ? $d['amount_charged'] : 0, isset($d['currency']) ? $d['currency'] : ''),
|
|
INTERWEB_MZ_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()
|
|
{
|
|
INTERWEB_MZ_ACCOUNT_Admin::check('interweb_mz_account_pdf');
|
|
$id = isset($_POST['id']) ? (int) $_POST['id'] : 0;
|
|
$res = INTERWEB_MZ_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.', 'interweb-mz-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.', 'interweb-mz-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()
|
|
{
|
|
INTERWEB_MZ_ACCOUNT_Admin::check('interweb_mz_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.', 'interweb-mz-account'), 'tickets');
|
|
}
|
|
$res = INTERWEB_MZ_ACCOUNT_Api::request('POST', '/v1/tickets/' . rawurlencode($id) . '/replies', array('message' => $msg), array('timeout' => 30, 'idempotency_key' => 'interweb-mz-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.', 'interweb-mz-account'), $res), 'tickets', array('ticket' => $id));
|
|
}
|
|
|
|
public static function ticket()
|
|
{
|
|
INTERWEB_MZ_ACCOUNT_Admin::check('interweb_mz_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.', 'interweb-mz-account'), 'tickets');
|
|
}
|
|
$res = INTERWEB_MZ_ACCOUNT_Api::request('POST', '/v1/tickets', $body, array('timeout' => 30, 'idempotency_key' => 'interweb-mz-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.', 'interweb-mz-account'),
|
|
$tid
|
|
) : __('Ticket opened.', 'interweb-mz-account'), $res), 'tickets', array('ticket' => $tid));
|
|
}
|
|
}
|