Issues (112)

Controller/Payment/LogV2.php (1 issue)

1
<?php
2
namespace Pagantis\Pagantis\Controller\Payment;
3
4
use Magento\Framework\App\Action\Action;
5
use Magento\Framework\App\Action\Context;
6
use Magento\Framework\App\ResourceConnection;
7
use Magento\Framework\DB\Ddl\Table;
8
use Magento\Framework\App\RequestInterface;
9
use Magento\Framework\App\Request\InvalidRequestException;
10
use \Pagantis\Pagantis\Helper\Config;
11
12
class LogV2 extends Action
13
{
14
    /** Concurrency tablename */
15
    const LOGS_TABLE = 'Pagantis_logs';
16
17
    /** @var mixed $config */
18
    protected $config;
19
20
    /** @var ResourceConnection $dbObject */
21
    protected $dbObject;
22
23
    /**
24
     * LogV2 constructor.
25
     *
26
     * @param Context            $context
27
     * @param Config             $pagantisConfig
28
     * @param ResourceConnection $dbObject
29
     * @param RequestInterface   $request
30
     */
31
    public function __construct(
32
        Context $context,
33
        Config $pagantisConfig,
34
        ResourceConnection $dbObject,
35
        RequestInterface $request
36
    ) {
37
        $this->config = $pagantisConfig->getConfig();
38
        $this->dbObject = $dbObject;
39
40
        // CsrfAwareAction Magento2.3 compatibility
41
        if (interface_exists("\Magento\Framework\App\CsrfAwareActionInterface")) {
42
            if (isset($request) && $request->isPost() && empty($request->getParam('form_key'))) {
43
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
44
                $formKey = $objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
45
                $request->setParam('form_key', $formKey->getFormKey());
46
            }
47
        }
48
49
        return parent::__construct($context);
50
    }
51
52
    /**
53
     * Main function
54
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
55
     */
56
    public function execute()
57
    {
58
        try {
59
            $response = array();
60
            $secretKey = $this->getRequest()->getParam('secret');
61
            $privateKey = isset($this->config['pagantis_private_key']) ? $this->config['pagantis_private_key'] : null;
62
63
            if ($secretKey!='' && $privateKey!='') {
64
                $this->checkDbLogTable();
65
                /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
66
                $dbConnection = $this->dbObject->getConnection();
67
                $tableName    = $this->dbObject->getTableName(self::LOGS_TABLE);
68
                $sql          = $dbConnection
69
                    ->select()
70
                    ->from($tableName, array('log', 'createdAt'));
71
72
                if ($dateFrom = $this->getRequest()->getParam('from')) {
73
                    $sql->where('createdAt > ?', $dateFrom);
74
                }
75
76
                if ($dateTo = $this->getRequest()->getParam('to')) {
77
                    $sql->where('createdAt < ?', $dateTo);
78
                }
79
80
                $limit = ($this->getRequest()->getParam('limit')) ? $this->getRequest()->getParam('limit') : 50;
81
                $sql->limit($limit);
82
                $sql->order('createdAt', 'desc');
83
84
                $results = $dbConnection->fetchAll($sql);
85
                if (isset($results) && $privateKey == $secretKey) {
86
                    foreach ($results as $key => $result) {
87
                        $response[$key]['timestamp'] = $result['createdAt'];
88
                        $response[$key]['log']       = json_decode($result['log']);
89
                    }
90
                } else {
91
                    $response['result'] = 'Error';
92
                }
93
94
                $response = json_encode($response);
95
                header("HTTP/1.1 200", true, 200);
96
                header('Content-Type: application/json', true);
97
                header('Content-Length: '.strlen($response));
98
                echo($response);
99
                exit();
100
            }
101
        } catch (\Exception $e) {
102
            die($e->getMessage());
103
        }
104
    }
105
106
    /**
107
     * @return void|\Zend_Db_Statement_Interface
108
     * @throws \Zend_Db_Exception
109
     */
110
    private function checkDbLogTable()
111
    {
112
        /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
113
        $dbConnection = $this->dbObject->getConnection();
114
        $tableName = $this->dbObject->getTableName(self::LOGS_TABLE);
115
        if (!$dbConnection->isTableExists($tableName)) {
116
            $table = $dbConnection
117
                ->newTable($tableName)
118
                ->addColumn(
119
                    'id',
120
                    Table::TYPE_SMALLINT,
121
                    null,
122
                    array('nullable'=>false, 'auto_increment'=>true, 'primary'=>true)
123
                )
124
                ->addColumn('log', Table::TYPE_TEXT, null, array('nullable'=>false))
125
                ->addColumn(
126
                    'createdAt',
127
                    Table::TYPE_TIMESTAMP,
128
                    null,
129
                    array('nullable'=>false,
130
                          'default'=>Table::TIMESTAMP_INIT)
131
                );
132
            return $dbConnection->createTable($table);
133
        }
134
        return;
135
    }
136
137
    /**
138
     * @param RequestInterface $request
139
     *
140
     * @return InvalidRequestException|null
141
     */
142
    public function createCsrfValidationException(RequestInterface $request)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

142
    public function createCsrfValidationException(/** @scrutinizer ignore-unused */ RequestInterface $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
    {
144
        return null;
145
    }
146
147
    /**
148
     * @param RequestInterface $request
149
     *
150
     * @return bool|null
151
     */
152
    public function validateForCsrf(RequestInterface $request)
153
    {
154
        return true;
155
    }
156
}
157