Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php declare(strict_types=1); namespace App\Controllers\User; use App\Controllers\Base..

Decoded Output download

<?php

declare(strict_types=1);

namespace App\Controllers\User;

use App\Controllers\BaseController;
use App\Models\Order;
use App\Models\Product;
use App\Models\UserCoupon;
use Psr\Http\Message\ResponseInterface;
use Slim\Http\Response;
use Slim\Http\ServerRequest;
use function explode;
use function in_array;
use function json_decode;
use function time;

final class CouponController extends BaseController
{
    public function check(ServerRequest $request, Response $response, array $args): ResponseInterface
    {
        $coupon_raw = $this->antiXss->xss_clean($request->getParam('coupon'));
        $product_id = $this->antiXss->xss_clean($request->getParam('product_id'));
        $invalid_coupon_msg = '';

        if ($coupon_raw === '') {
            return $response->withJson([
                'ret' => 0,
                'msg' => $invalid_coupon_msg,
            ]);
        }

        $coupon = (new UserCoupon())->where('code', $coupon_raw)->first();

        if ($coupon === null || ($coupon->expire_time !== 0 && $coupon->expire_time < time())) {
            return $response->withJson([
                'ret' => 0,
                'msg' => $invalid_coupon_msg,
            ]);
        }

        $product = (new Product())->where('id', $product_id)->first();

        if ($product === null) {
            return $response->withJson([
                'ret' => 0,
                'msg' => $invalid_coupon_msg,
            ]);
        }

        $limit = json_decode($coupon->limit);

        if ($limit->disabled) {
            return $response->withJson([
                'ret' => 0,
                'msg' => $invalid_coupon_msg,
            ]);
        }

        if ($limit->product_id !== '' && ! in_array($product_id, explode(',', $limit->product_id))) {
            return $response->withJson([
                'ret' => 0,
                'msg' => $invalid_coupon_msg,
            ]);
        }

        $user = $this->user;
        $use_limit = $limit->use_time;

        if ($use_limit > 0) {
            $user_use_count = (new Order())->where('user_id', $user->id)->where('coupon', $coupon->code)->count();
            if ($user_use_count >= $use_limit) {
                return $response->withJson([
                    'ret' => 0,
                    'msg' => $invalid_coupon_msg,
                ]);
            }
        }

        $total_use_limit = $limit->total_use_time;

        if ($total_use_limit > 0 && $coupon->use_count >= $total_use_limit) {
            return $response->withJson([
                'ret' => 0,
                'msg' => $invalid_coupon_msg,
            ]);
        }

        $content = json_decode($coupon->content);

        if ($content->type === 'percentage') {
            $discount = $product->price * $content->value / 100;
        } else {
            $discount = $content->value;
        }

        $buy_price = $product->price - $discount;

        return $response->withJson([
            'ret' => 1,
            'msg' => '',
            'data' => [
                'coupon-code' => $coupon->code,
                'product-buy-discount' => $discount,
                'product-buy-total' => $buy_price,
            ],
        ]);
    }
}
 ?>

Did this file decode correctly?

Original Code

<?php

declare(strict_types=1);

namespace App\Controllers\User;

use App\Controllers\BaseController;
use App\Models\Order;
use App\Models\Product;
use App\Models\UserCoupon;
use Psr\Http\Message\ResponseInterface;
use Slim\Http\Response;
use Slim\Http\ServerRequest;
use function explode;
use function in_array;
use function json_decode;
use function time;

final class CouponController extends BaseController
{
    public function check(ServerRequest $request, Response $response, array $args): ResponseInterface
    {
        $coupon_raw = $this->antiXss->xss_clean($request->getParam('coupon'));
        $product_id = $this->antiXss->xss_clean($request->getParam('product_id'));
        $invalid_coupon_msg = '';

        if ($coupon_raw === '') {
            return $response->withJson([
                'ret' => 0,
                'msg' => $invalid_coupon_msg,
            ]);
        }

        $coupon = (new UserCoupon())->where('code', $coupon_raw)->first();

        if ($coupon === null || ($coupon->expire_time !== 0 && $coupon->expire_time < time())) {
            return $response->withJson([
                'ret' => 0,
                'msg' => $invalid_coupon_msg,
            ]);
        }

        $product = (new Product())->where('id', $product_id)->first();

        if ($product === null) {
            return $response->withJson([
                'ret' => 0,
                'msg' => $invalid_coupon_msg,
            ]);
        }

        $limit = json_decode($coupon->limit);

        if ($limit->disabled) {
            return $response->withJson([
                'ret' => 0,
                'msg' => $invalid_coupon_msg,
            ]);
        }

        if ($limit->product_id !== '' && ! in_array($product_id, explode(',', $limit->product_id))) {
            return $response->withJson([
                'ret' => 0,
                'msg' => $invalid_coupon_msg,
            ]);
        }

        $user = $this->user;
        $use_limit = $limit->use_time;

        if ($use_limit > 0) {
            $user_use_count = (new Order())->where('user_id', $user->id)->where('coupon', $coupon->code)->count();
            if ($user_use_count >= $use_limit) {
                return $response->withJson([
                    'ret' => 0,
                    'msg' => $invalid_coupon_msg,
                ]);
            }
        }

        $total_use_limit = $limit->total_use_time;

        if ($total_use_limit > 0 && $coupon->use_count >= $total_use_limit) {
            return $response->withJson([
                'ret' => 0,
                'msg' => $invalid_coupon_msg,
            ]);
        }

        $content = json_decode($coupon->content);

        if ($content->type === 'percentage') {
            $discount = $product->price * $content->value / 100;
        } else {
            $discount = $content->value;
        }

        $buy_price = $product->price - $discount;

        return $response->withJson([
            'ret' => 1,
            'msg' => '',
            'data' => [
                'coupon-code' => $coupon->code,
                'product-buy-discount' => $discount,
                'product-buy-total' => $buy_price,
            ],
        ]);
    }
}

Function Calls

None

Variables

None

Stats

MD5 42d2a7fdeba699cadca947462e5c6da0
Eval Count 0
Decode Time 107 ms