-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrency.php
More file actions
174 lines (145 loc) · 3.7 KB
/
Copy pathcurrency.php
File metadata and controls
174 lines (145 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
$opts = getopt("r:c:h");
foreach (array_keys($opts) as $opt) switch ($opt) {
case 'r':
import($opts['r']);
break;
case 'c':
convert($opts['c']);
break;
case 'h':
echo help_text();
break;
}
echo "Thanks...";
/**
* Implements XML import cli
*/
function import($provider) {
if (url_exists($provider)) {
$mysqli = db_access();
if ($mysqli) {
import_currency($provider, $mysqli);
}
}
}
/**
* Helper - Check if url exists XML
*/
function url_exists($url){
$headers=get_headers($url);
return stripos($headers[0],"200 OK")?true:false;
}
/**
* Implements import in the database
*/
function import_currency($provider, $mysqli) {
// log console
echo 'Import Currency Start ....' . "\n";
// TODO log to file $log
// read xml
$xml = simplexml_load_file($provider);
if ($xml === FALSE) {
echo 'Failed Import ....' . "\n";
exit();
}
else {
// save to DB
$date = date("Y-m-d H:i:s");
foreach ($xml->children() as $conversion) {
$currency = $conversion->currency;
$rate = $conversion->rate;
//mysqli_real_escape_string()
$insert = $mysqli->prepare("INSERT INTO rates (currency, rate, currentdate) VALUES (?,?,?)");
$insert->bind_param("sds", $currency , $rate, $date );
$insert->execute();
}
// keep a history of imported files
$day = date('Y-m-d');
// create folder
if(!is_dir('xml')){
mkdir('xml');
}
copy($provider, 'xml/currency-'. $day);
// log console
echo 'Import Currency End ....' . "\n";
// TODO log to file $log
}
}
/**
* Helper - Connect to database
*/
function db_access(){
/* Initial values */
$mysql_hostname = "localhost";
$mysql_user = "currency";
$mysql_password = "wehDdGLUL7UvH5dy";
$mysql_database = "currency";
// TODO import config form file
// new connection to the MySQL server
$mysqli = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
// log connection error
if ($mysqli->connect_error) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
exit();
// TODO log to file
}
// TODO check if table exist or create
return $mysqli;
}
/**
* Implements Currency Conversion cli
*/
function convert($args) {
$mysqli = db_access();
if ($mysqli) {
$values = explode(",", $args);
$output = convert_currency($values, $mysqli);
echo implode(",", $output) . "\n";
}
}
/**
* Implements Currency Conversion of an array
*/
function convert_currency($items, $mysqli) {
$output = array();
foreach ($items as $item) {
// TODO user regular expression (lowercase)
$value = explode(" ", $item);
$change = get_change($value[0], $value[1], $mysqli);
if ($change){
$output[] = "USD " . $change;
}
else {
$output[] = "Not found rate for " . $value[0];
}
}
return $output;
}
/**
* Returns change value in USD
*/
function get_change($currency, $amount, $mysqli) {
$change = 0;
if ($query = $mysqli->prepare("SELECT rate FROM rates WHERE currency = ? ORDER BY currentdate DESC LIMIT 1")) {
$query->bind_param("s", $currency);
$query->execute();
$query->bind_result($rate);
$query->fetch();
if ($rate){
// Calculate USD change
$change = $amount * $rate;
}
}
return $change;
}
function help_text(){
$output = '';
$output .= "--------- Usage ---------" . "\n";
$output .= " -r [url] : Run Importer " . "\n";
$output .= " -c [values]: Convert values enter to USD" . "\n";
$output .= " Ex: -c 'CZK 62.5'" . "\n";
$output .= " Ex: -c 'JPY 5000,CZK 62.5'" . "\n";
$output .= "------------------" . "\n";
return $output;
}