travispessetto / script-updater-for-php

A MIT Licensed script updater for projects
MIT License
31 stars 7 forks source link

Parse JSON Error Syntax #5

Open Inet-Developments opened 10 months ago

Inet-Developments commented 10 months ago

Hi there

i tried the test updater works fine initially but upon updating i get an error message saying

The update failed Status: parsererror Error: SyntaxError: Unexpected token '<', " "... is not valid JSON

i have attached a picture. Anyhelp would be a appreciated

updater

travispessetto commented 10 months ago

I believe this is caused by deprecated PHP code as when investigating the response in Chrome I get the following warning:

Deprecated: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior in update-test\controller.php on line 232

That said, I am getting the problem even on the first run. I will see if I can update the code.

Inet-Developments commented 10 months ago

much appreciated travis i originally thought it may of been a server configuration error my end but i look forward to updates.

Thanks

Chris.

Poltavtcev commented 9 months ago

I also encounter the same error when attempting to restore the backup. Additionally, if I add a simple authentication function to the plugins.

travispessetto commented 9 months ago

@Inet-Developments Please try the new release, v4.10, and see if the problems you had a resolved.

@Poltavtcev. Please try the new release, v4.10 and see if the problem is resolved. If it is not resolved, then please provide me with the following information:

Poltavtcev commented 9 months ago

@travispessetto Yes, everything remains unchanged for me...

Here is an example of an authentication function that works for me.

`class Authorize { private $authorized = false; private $username = 'user'; private $password = 'password';

public function ConstructorHook()
{
    $this->checkAuthorization();
    if (!$this->authorized) {
        header('WWW-Authenticate: Basic realm="Authorization Required"');
        header('HTTP/1.0 401 Unauthorized');
        echo 'Authorization Required';
        exit();
    }
}

private function checkAuthorization()
{

    if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
        $enteredUsername = $_SERVER['PHP_AUTH_USER'];
        $enteredPassword = $_SERVER['PHP_AUTH_PW'];

        if ($enteredUsername === $this->username && $enteredPassword === $this->password) {
            $this->authorized = true;
        }
    }
}

}

$auth = new Authorize(); $auth->ConstructorHook(); `

But when I try to extract data from the configuration file, it immediately gives an error.

`include '../config.php';

class Authorize { private $authorized = false; private $username = $config["auth_username"]; private $password = $config["auth_password"];

public function ConstructorHook()
{
    $this->checkAuthorization();
    if (!$this->authorized) {
        header('WWW-Authenticate: Basic realm="Authorization Required"');
        header('HTTP/1.0 401 Unauthorized');
        echo 'Authorization Required';
        exit();
    }
}

private function checkAuthorization()
{

    if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
        $enteredUsername = $_SERVER['PHP_AUTH_USER'];
        $enteredPassword = $_SERVER['PHP_AUTH_PW'];

        if ($enteredUsername === $this->username && $enteredPassword === $this->password) {
            $this->authorized = true;
        }
    }
}

}

$auth = new Authorize(); $auth->ConstructorHook(); `

travispessetto commented 9 months ago

I made some changes to master, so if you can pull that down from github and test it that would be great.

That said, when I ran your authorize code it said it had invalid syntax. Those lines are:

private $username = $config["auth_username"];
private $password = $config["auth_password"];

So I removed the $config from them and added them into the contructor:

    public function __construct()
    {
        $config = ConfigSingleton::Instance();
        $this->username = $config->auth_username;
        $this->password = $config->auth_password;
    }

You also do not need the following two lines at the bottom:

$auth = new Authorize();
$auth->ConstructorHook();

This is what I ended up with and appears to work:

<?php

class Authorize
{
    private $authorized = false;
    private $username;
    private $password;

    public function __construct()
    {
        $config = ConfigSingleton::Instance();
        $this->username = $config->auth_username;
        $this->password = $config->auth_password;
    }

    public function ConstructorHook()
    {
        $this->checkAuthorization();
        if (!$this->authorized) {
            header('WWW-Authenticate: Basic realm="Authorization Required"');
            header('HTTP/1.0 401 Unauthorized');
            echo 'Authorization Required';
            exit();
        }
    }

    private function checkAuthorization()
    {

        if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
            $enteredUsername = $_SERVER['PHP_AUTH_USER'];
            $enteredPassword = $_SERVER['PHP_AUTH_PW'];

            if ($enteredUsername === $this->username && $enteredPassword === $this->password) {
                $this->authorized = true;
            }
        }
    }
}
Poltavtcev commented 9 months ago

Yes, now everything is in order here. Thank you.