Issues (2407)

administration/model/localisation/currency.php (15 issues)

1
<?php
2
3
/* 	Divine CMS - Open source CMS for widespread use.
4
    Copyright (c) 2019 Mykola Burakov ([email protected])
5
6
    See SOURCE.txt for other and additional information.
7
8
    This file is part of Divine CMS.
9
10
    This program is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
15
    This program is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
    GNU General Public License for more details.
19
20
    You should have received a copy of the GNU General Public License
21
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
class ModelLocalisationCurrency extends \Divine\Engine\Core\Model
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    public function addCurrency($data)
0 ignored issues
show
Expected 2 blank lines before function; 0 found
Loading history...
26
    {
27
        $this->db->query("
28
			INSERT INTO currency 
29
			SET title = '" . $this->db->escape($data['title']) . "', 
30
				code = '" . $this->db->escape($data['code']) . "', 
31
				symbol_left = '" . $this->db->escape($data['symbol_left']) . "', 
32
				symbol_right = '" . $this->db->escape($data['symbol_right']) . "', 
33
				decimal_place = '" . $this->db->escape($data['decimal_place']) . "', 
34
				value = '" . $this->db->escape($data['value']) . "', 
35
				status = '" . (int)$data['status'] . "', 
36
				date_modified = NOW()
37
		");
38
39
        $currency_id = $this->db->getLastId();
40
41
        if ($this->config->get('config_currency_auto')) {
42
            $this->refresh(true);
0 ignored issues
show
The method refresh() does not exist on ModelLocalisationCurrency. ( Ignorable by Annotation )

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

42
            $this->/** @scrutinizer ignore-call */ 
43
                   refresh(true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
        }
44
45
        $this->cache->delete('currency');
46
47
        return $currency_id;
48
    }
49
50
    public function editCurrency($currency_id, $data)
51
    {
52
        $this->db->query("
53
			UPDATE currency 
54
			SET title = '" . $this->db->escape($data['title']) . "', 
55
				code = '" . $this->db->escape($data['code']) . "', 
56
				symbol_left = '" . $this->db->escape($data['symbol_left']) . "', 
57
				symbol_right = '" . $this->db->escape($data['symbol_right']) . "', 
58
				decimal_place = '" . $this->db->escape($data['decimal_place']) . "', 
59
				value = '" . $this->db->escape($data['value']) . "', 
60
				status = '" . (int)$data['status'] . "', 
61
				date_modified = NOW() 
62
			WHERE currency_id = '" . (int)$currency_id . "'
63
		");
64
65
        $this->cache->delete('currency');
66
    }
67
68
    public function deleteCurrency($currency_id)
69
    {
70
        $this->db->query("
71
			DELETE 
72
			FROM currency 
73
			WHERE currency_id = '" . (int)$currency_id . "'
74
		");
75
76
        $this->cache->delete('currency');
77
    }
78
79
    public function getCurrency($currency_id)
80
    {
81
        $query = $this->db->query("
82
			SELECT DISTINCT * 
83
			FROM currency 
84
			WHERE currency_id = '" . (int)$currency_id . "'
85
		");
86
87
        return $query->row;
88
    }
89
90
    public function getCurrencyByCode($currency)
91
    {
92
        $query = $this->db->query("
93
			SELECT DISTINCT * 
94
			FROM currency 
95
			WHERE code = '" . $this->db->escape($currency) . "'
96
		");
97
98
        return $query->row;
99
    }
100
101
    public function getCurrencies($data = array())
102
    {
103
        if ($data) {
104
            $sql = "
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal \n SELECT... currency\n does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
105
				SELECT * 
106
				FROM currency
107
			";
108
109
            $sort_data = array(
110
                'title',
111
                'code',
112
                'value',
113
                'date_modified'
114
            );
115
116
            if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
117
                $sql .= " ORDER BY " . $data['sort'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ORDER BY does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
118
            } else {
119
                $sql .= " ORDER BY title";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ORDER BY title does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
120
            }
121
122
            if (isset($data['order']) && ($data['order'] == 'DESC')) {
123
                $sql .= " DESC";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal DESC does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
124
            } else {
125
                $sql .= " ASC";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ASC does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
126
            }
127
128
            if (isset($data['start']) || isset($data['limit'])) {
129
                if ($data['start'] < 0) {
130
                    $data['start'] = 0;
131
                }
132
133
                if ($data['limit'] < 1) {
134
                    $data['limit'] = 20;
135
                }
136
137
                $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal LIMIT does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal , does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
138
            }
139
140
            $query = $this->db->query($sql);
141
142
            return $query->rows;
143
        } else {
144
            $currency_data = $this->cache->get('currency');
145
146
            if (!$currency_data) {
147
                $currency_data = array();
148
149
                $query = $this->db->query("
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal \n SE...e ASC\n does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
150
					SELECT * 
151
					FROM currency ORDER BY title ASC
152
				");
153
154
                foreach ($query->rows as $result) {
155
                    $currency_data[$result['code']] = array(
156
                        'currency_id'   => $result['currency_id'],
157
                        'title'         => $result['title'],
158
                        'code'          => $result['code'],
159
                        'symbol_left'   => $result['symbol_left'],
160
                        'symbol_right'  => $result['symbol_right'],
161
                        'decimal_place' => $result['decimal_place'],
162
                        'value'         => $result['value'],
163
                        'status'        => $result['status'],
164
                        'date_modified' => $result['date_modified']
165
                    );
166
                }
167
168
                $this->cache->set(
169
                    'currency',
170
                    $currency_data
171
                );
172
            }
173
174
            return $currency_data;
175
        }
176
    }
177
178
    public function refresh($force = false)
179
    {
180
        $data = array();
181
182
        if ($force) {
183
            $query = $this->db->query("
184
				SELECT * 
185
				FROM currency 
186
				WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "'
187
			");
188
        } else {
189
            $query = $this->db->query("
190
				SELECT * 
191
				FROM currency 
192
				WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "' 
193
					AND date_modified < '" .  $this->db->escape(date('Y-m-d H:i:s', strtotime('-1 day'))) . "'
194
			");
195
        }
196
197
        foreach ($query->rows as $result) {
198
            $data[] = $this->config->get('config_currency') . $result['code'] . '=X';
199
        }
200
201
        $curl = curl_init();
202
203
        curl_setopt($curl, CURLOPT_URL, 'http://download.finance.yahoo.com/d/quotes.csv?s=' . implode(',', $data) . '&f=sl1&e=.csv');
0 ignored issues
show
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

203
        curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_URL, 'http://download.finance.yahoo.com/d/quotes.csv?s=' . implode(',', $data) . '&f=sl1&e=.csv');
Loading history...
204
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
205
        curl_setopt($curl, CURLOPT_HEADER, false);
206
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
207
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
208
209
        $content = curl_exec($curl);
0 ignored issues
show
It seems like $curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

209
        $content = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
210
211
        curl_close($curl);
0 ignored issues
show
It seems like $curl can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

211
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
212
213
        $lines = explode("\n", trim($content));
214
215
        foreach ($lines as $line) {
216
            $currency = \voku\helper\UTF8::substr($line, 4, 3);
217
            $value = \voku\helper\UTF8::substr($line, 11, 6);
218
219
            if ((float)$value) {
220
                $this->db->query("
221
					UPDATE currency 
222
					SET value = '" . (float)$value . "', 
223
						date_modified = '" .  $this->db->escape(date('Y-m-d H:i:s')) . "' 
224
					WHERE code = '" . $this->db->escape($currency) . "'
225
				");
226
            }
227
        }
228
229
        $this->db->query("
230
			UPDATE currency 
231
			SET value = '1.00000', 
232
				date_modified = '" .  $this->db->escape(date('Y-m-d H:i:s')) . "' 
233
			WHERE code = '" . $this->db->escape($this->config->get('config_currency')) . "'
234
		");
235
236
        $this->cache->delete('currency');
237
    }
238
239
    public function getTotalCurrencies()
240
    {
241
        $query = $this->db->query("
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal \n SELECT COU...FROM currency\n does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
242
			SELECT COUNT(*) AS total 
243
			FROM currency
244
		");
245
246
        return $query->row['total'];
247
    }
248
}
249