forked from LiveCarta/LiveCartaWP
Changed source root directory
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
class DUPX_REST_AUTH implements Requests_Auth
|
||||
{
|
||||
protected $nonce = null;
|
||||
protected $basicAuthUser = "";
|
||||
protected $basicAuthPassword = "";
|
||||
|
||||
public function __construct($nonce, $basicAuthUser = "", $basicAuthPassword = "")
|
||||
{
|
||||
$this->nonce = $nonce;
|
||||
$this->basicAuthUser = $basicAuthUser;
|
||||
$this->basicAuthPassword = $basicAuthPassword;
|
||||
}
|
||||
|
||||
public function register(Requests_Hooks &$hooks)
|
||||
{
|
||||
if (strlen($this->basicAuthUser) > 0) {
|
||||
$basicAuth = new Requests_Auth_Basic(array(
|
||||
$this->basicAuthUser,
|
||||
$this->basicAuthPassword
|
||||
));
|
||||
$basicAuth->register($hooks);
|
||||
}
|
||||
|
||||
$hooks->register('requests.before_request', array($this, 'before_request'));
|
||||
}
|
||||
|
||||
public function before_request(&$url, &$headers, &$data, &$type, &$options)
|
||||
{
|
||||
$data['_wpnonce'] = $this->nonce;
|
||||
foreach ($_COOKIE as $key => $val) {
|
||||
$options['cookies'][$key] = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
defined('ABSPATH') || defined('DUPXABSPATH') || exit;
|
||||
|
||||
use Duplicator\Libs\Snap\SnapIO;
|
||||
use Duplicator\Installer\Utils\Log\Log;
|
||||
use Duplicator\Installer\Core\Params\PrmMng;
|
||||
|
||||
|
||||
class DUPX_REST
|
||||
{
|
||||
const DUPLICATOR_NAMESPACE = 'duplicator/v1/';
|
||||
|
||||
/**
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $nonce = false;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $basicAuthUser = "";
|
||||
|
||||
/**
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $basicAuthPassword = "";
|
||||
|
||||
/**
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $url = false;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var self
|
||||
*/
|
||||
private static $instance = null;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (is_null(self::$instance)) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
$paramsManager = PrmMng::getInstance();
|
||||
$overwriteData = $paramsManager->getValue(PrmMng::PARAM_OVERWRITE_SITE_DATA);
|
||||
|
||||
if (is_array($overwriteData)) {
|
||||
if (
|
||||
isset($overwriteData['restUrl']) &&
|
||||
strlen($overwriteData['restUrl']) > 0 &&
|
||||
isset($overwriteData['restNonce']) &&
|
||||
strlen($overwriteData['restNonce']) > 0
|
||||
) {
|
||||
$this->url = SnapIO::untrailingslashit($overwriteData['restUrl']);
|
||||
$this->nonce = $overwriteData['restNonce'];
|
||||
}
|
||||
|
||||
if (strlen($overwriteData['restAuthUser']) > 0) {
|
||||
$this->basicAuthUser = $overwriteData['restAuthUser'];
|
||||
$this->basicAuthPassword = $overwriteData['restAuthPassword'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function checkRest($reset = false, &$errorMessage = "")
|
||||
{
|
||||
static $success = null;
|
||||
if (is_null($success) || $reset) {
|
||||
try {
|
||||
$success = true;
|
||||
if ($this->nonce === false) {
|
||||
throw new Exception("Nonce is not set.");
|
||||
}
|
||||
|
||||
if (strlen($testUrl = $this->getRestUrl('versions')) === 0) {
|
||||
throw new Exception("Couldn't get REST API backed URL to do tests. REST API URL was empty.");
|
||||
}
|
||||
|
||||
$response = Requests::get($testUrl, array(), $this->getRequestAuthOptions());
|
||||
if ($response->success == false) {
|
||||
Log::info(Log::v2str($response));
|
||||
throw new Exception("REST API request on $testUrl failed");
|
||||
}
|
||||
|
||||
if (($result = json_decode($response->body, true)) === null) {
|
||||
throw new Exception("Can't decode json.");
|
||||
}
|
||||
|
||||
if (!isset($result["dup"])) {
|
||||
throw new Exception("Did not receive the expected result.");
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
$success = false;
|
||||
$errorMessage = $ex->getMessage();
|
||||
Log::info("FAILED REST API CHECK. MESSAGE: " . $ex->getMessage());
|
||||
}
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
public function getVersions()
|
||||
{
|
||||
$response = Requests::get($this->getRestUrl('versions'), array(), $this->getRequestAuthOptions());
|
||||
if (!$response->success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (($result = json_decode($response->body)) === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return request auth options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getRequestAuthOptions()
|
||||
{
|
||||
return array(
|
||||
'auth' => new DUPX_REST_AUTH($this->nonce, $this->basicAuthUser, $this->basicAuthPassword),
|
||||
'verify' => false,
|
||||
'verifyname' => false
|
||||
);
|
||||
}
|
||||
|
||||
private function getRestUrl($subPath = '')
|
||||
{
|
||||
return $this->url ? $this->url . '/' . self::DUPLICATOR_NAMESPACE . $subPath : '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user