/
toolbox.php
121 lines (111 loc) · 3.09 KB
/
toolbox.php
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
<?php
require '../Slim/Slim.php';
include "../inc/database.php";
$app = new Slim();
$app->get('/', 'index' );
$app->get('/cache/:file', 'backup' );
$app->get('/mxoptix/:area', 'query_to_mxoptix' );
$app->get('/pgt/:area', 'query_to_PGT' );
$app->get('/mxapps/:area', 'query_to_MxApps' );
function backup($file='')
{
$file = $file . '.csv';
if (file_exists($file)) {
echo file_get_contents($file);
}else{
echo "file not exists";
}
}
function index()
{
echo "Error: solo puedes accesar el servicio con una peticion. Revisa la API!";
}
function query_to_mxoptix($area){
if( $area != '' ){
queryAndReturnCSV_Ans('MxOptix', $area);
}
}
function query_to_PGT($area){
if( $area != '' ){
queryAndReturnCSV_Ans('PGT', $area);
}
}
function query_to_MxApps($area){
if( $area != '' ){
queryAndReturnCSV_Ans('MxApps', $area);
}
}
function queryAndReturnCSV_Ans($connection, $area)
{
try {
$DB = new $connection();
$query = file_get_contents('sql/' . $area .'.sql');
$DB->setQuery($query);
$results = null;
oci_execute($DB->statement);
oci_fetch_all($DB->statement, $results,0,-1,OCI_FETCHSTATEMENT_BY_ROW);
$ans = trim(array2csv($results));
file_put_contents($area . '.csv', $ans);
echo $ans;
$DB->close();
} catch (Exception $e) {
$DB->close();
echo ('Caught exception: '. $e->getMessage(). "\n");
}
}
function csvFromTypeOfMachine($machineName='')
{
try {
$DB = new PGT();
$query = file_get_contents('sql/deflector.sql');
$DB->setQuery($query);
$results = null;
// $DB->bind_vars(':system_id',$machineName);
oci_execute($DB->statement);
oci_fetch_all($DB->statement, $results,0,-1,OCI_FETCHSTATEMENT_BY_ROW);
$ans = trim(array2csv($results));
file_put_contents('deflector.csv', $ans);
echo $ans;
$DB->close();
} catch (Exception $e) {
$DB->close();
echo ('Caught exception: '. $e->getMessage(). "\n");
}
}
function example(){
try {
$DB = new MxOptix();
global $app;
$body = $app->request()->getBody();
$body = json_decode($body, true);
// print_r($body);
$DB->setQuery($body['query']);
$results = null;
oci_execute($DB->statement);
oci_fetch_all($DB->statement, $results,0,-1,OCI_FETCHSTATEMENT_BY_ROW);
// print_r($results);
// print_r($results);
echo array2csv($results);
$DB->close();
} catch (Exception $e) {
$DB->close();
echo ('Caught exception: '. $e->getMessage(). "\n");
}
}
function array2csv($array) {
$ans = '';
$start = true;
$head = array();
foreach($array as $key => $value) {
if ($start) {
foreach ($value as $key2 => $value2) {
array_push($head, $key2);
}
$ans .= implode(",", $head) . PHP_EOL;
$start = false;
}
$ans .= implode(',', $value) . PHP_EOL;
}
return $ans;
}
$app->run();