Passed
Push — dev ( 7e9308...6c8d74 )
by Greg
03:26
created

classes/DomainMOD/Conversion.php (1 issue)

1
<?php
2
/**
3
 * /classes/DomainMOD/Conversion.php
4
 *
5
 * This file is part of DomainMOD, an open source domain and internet asset manager.
6
 * Copyright (c) 2010-2018 Greg Chetcuti <[email protected]>
7
 *
8
 * Project: http://domainmod.org   Author: http://chetcuti.com
9
 *
10
 * DomainMOD is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
11
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
12
 * version.
13
 *
14
 * DomainMOD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
15
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along with DomainMOD. If not, see
18
 * http://www.gnu.org/licenses/.
19
 *
20
 */
21
//@formatter:off
22
namespace DomainMOD;
23
24
class Conversion
25
{
26
    public $deeb;
27
    public $log;
28
    public $time;
29
30
    public function __construct()
31
    {
32
        $this->deeb = Database::getInstance();
33
        $this->log = new Log('class.conversion');
34
        $this->time = new Time();
35
    }
36
37
    public function updateRates($default_currency, $user_id)
38
    {
39
        $pdo = $this->deeb->cnxx;
40
        $result = $this->getActiveCurrencies();
41
42
        $stmt = $pdo->prepare("
43
            SELECT id
44
            FROM currency_conversions
45
            WHERE currency_id = :currency_id
46
              AND user_id = :user_id");
47
        $stmt->bindParam('currency_id', $bind_currency_id, \PDO::PARAM_INT);
48
        $stmt->bindValue('user_id', $user_id, \PDO::PARAM_INT);
49
50
        foreach ($result as $row) {
51
52
            $conversion_rate = $row->currency == $default_currency ? 1 : $this->getConversionRate($row->currency, $default_currency);
53
54
            $bind_currency_id = $row->id;
55
            $stmt->execute();
56
57
            $result_conversion = $stmt->fetchColumn();
58
59
            if (!$result_conversion) {
60
61
                $is_existing = '0';
62
                $log_message = 'Unable to retrieve user currency';
63
                $log_extra = array('User ID' => $user_id, 'Currency ID' => $row->id, 'Default Currency' =>
64
                    $default_currency, 'Conversion Rate' => $conversion_rate);
65
                $this->log->critical($log_message, $log_extra);
66
67
            } else {
68
69
                $is_existing = '1';
70
71
            }
72
73
            $this->updateConversionRate($conversion_rate, $is_existing, $row->id, $user_id);
74
75
        }
76
77
        $result_message = 'Conversion Rates updated<BR>';
78
79
        return $result_message;
80
    }
81
82
    public function getActiveCurrencies()
83
    {
84
        $result = $this->deeb->cnxx->query("
85
            SELECT id, currency
86
            FROM
87
            (  SELECT c.id, c.currency
88
               FROM currencies AS c, fees AS f, domains AS d
89
               WHERE c.id = f.currency_id
90
                 AND f.id = d.fee_id
91
               GROUP BY c.currency
92
               UNION
93
               SELECT c.id, c.currency
94
               FROM currencies AS c, ssl_fees AS f, ssl_certs AS sslc
95
               WHERE c.id = f.currency_id
96
                 AND f.id = sslc.fee_id
97
               GROUP BY c.currency
98
            ) AS temp
99
            GROUP BY currency")->fetchAll();
100
101
        if (!$result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
102
103
            $log_message = 'Unable to retrieve active currencies';
104
            $this->log->critical($log_message);
105
            return $log_message;
106
107
        } else {
108
109
            return $result;
110
111
        }
112
    }
113
114
    public function getConversionRate($from_currency, $to_currency)
115
    {
116
        $currency_slug = $from_currency . '_' . $to_currency;
117
        $full_url = 'https://free.currencyconverterapi.com/api/v5/convert?q=' . $currency_slug . '&compact=y';
118
        $handle = curl_init($full_url);
119
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
120
        curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
121
        curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
122
        $result = curl_exec($handle);
123
        curl_close($handle);
124
        $json_result = json_decode($result);
125
        $conversion_rate = $json_result->{$currency_slug}->val;
126
127
        if ($conversion_rate != '' && $conversion_rate != 'N/A' && $conversion_rate != 'n/a') {
128
129
            return $conversion_rate;
130
131
        } else {
132
133
            $log_message = 'Unable to retrieve Currency Converter API (FREE) currency conversion';
134
            $log_extra = array('From Currency' => $from_currency, 'To Currency' => $to_currency,
135
                               'Conversion Rate Result' => $conversion_rate);
136
            $this->log->critical($log_message, $log_extra);
137
            return $log_message;
138
139
        }
140
    }
141
142
    public function updateConversionRate($conversion_rate, $is_existing, $currency_id, $user_id)
143
    {
144
        $pdo = $this->deeb->cnxx;
145
146
        if ($is_existing == '1') {
147
148
            $stmt = $pdo->prepare("
149
                UPDATE currency_conversions
150
                SET conversion = :conversion_rate,
151
                    update_time = :update_time
152
                WHERE currency_id = :currency_id
153
                  AND user_id = :user_id");
154
            $stmt->bindValue('conversion_rate', strval($conversion_rate), \PDO::PARAM_STR);
155
            $bind_timestamp = $this->time->stamp();
156
            $stmt->bindValue('update_time', $bind_timestamp, \PDO::PARAM_STR);
157
            $stmt->bindValue('currency_id', $currency_id, \PDO::PARAM_INT);
158
            $stmt->bindValue('user_id', $user_id, \PDO::PARAM_INT);
159
            $stmt->execute();
160
161
            $log_message = 'Conversion rate updated';
162
            $log_extra = array('User ID' => $user_id, 'Currency ID' => $currency_id, 'Conversion Rate' => $conversion_rate, 'Update Time' => $this->time->stamp());
163
            $this->log->info($log_message, $log_extra);
164
165
        } else {
166
167
            $stmt = $pdo->prepare("
168
                INSERT INTO currency_conversions
169
                (currency_id, user_id, conversion, insert_time)
170
                VALUES
171
                (:currency_id, :user_id, :conversion_rate, :update_time)");
172
            $stmt->bindValue('currency_id', $currency_id, \PDO::PARAM_INT);
173
            $stmt->bindValue('user_id', $user_id, \PDO::PARAM_INT);
174
            $stmt->bindValue('conversion_rate', strval($conversion_rate), \PDO::PARAM_STR);
175
            $bind_timestamp = $this->time->stamp();
176
            $stmt->bindValue('update_time', $bind_timestamp, \PDO::PARAM_STR);
177
            $stmt->execute();
178
179
            $log_message = 'Conversion rate inserted';
180
            $log_extra = array('User ID' => $user_id, 'Currency ID' => $currency_id, 'Conversion Rate' => $conversion_rate, 'Update Time' => $this->time->stamp());
181
            $this->log->info($log_message, $log_extra);
182
183
        }
184
    }
185
186
} //@formatter:on
187