Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
DownloadDigitalSignSyncApp
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 7
420
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 checkAPI
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getFileDetails
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 setXMLHeaders
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 sendFileDownload
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
30
 getXML
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 array2xml
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace BuyerKiosk\SyncApp;
4
5use KLogger;
6use BuyerKiosk\Core\Store;
7
8class DownloadDigitalSignSyncApp extends Store
9{
10
11    private $filename;
12    private $version;
13    public $apiKey;
14    private $location;
15    private $log;
16    public function __construct($typeNum, $apiKey = null)
17    {
18        if (!parent::createStore($typeNum)) {
19            return false;
20        }
21        if ($apiKey !== null) {
22            if (!$this->checkAPI($apiKey)) {
23                return false;
24            }
25            $this->apiKey = $apiKey;
26        }
27        $this->log = new KLogger($_ENV['LOG_DIR']."dev_log.txt", KLogger::DEBUG);
28        $this->log->LogDebug("SimpleSyncDownloadApp Logging Started");
29        $this->location = "../userfrosting/lib/DigitalSignSyncApp/";
30        $this->getFileDetails();
31        $this->log->LogDebug($this->getXML());
32        return true;
33
34    }
35
36    private function checkAPI($key)
37    {
38        //Remove any non alpha-numeric characters. This will cause the character count to fail
39        $key = preg_replace("/[^A-Za-z0-9]/", "", $key);
40        //If we do not have 60 characters we know this is a bad key.
41        if (strlen($key) !== 60) {
42            return null;
43        }
44        $storeAPI = $this->getApiKey();
45        //Check if the two strings are the same
46        if ($key !== $storeAPI) {
47            return false;
48        }
49        return true;
50    }
51
52    private function getFileDetails()
53    {
54        global $db_name;
55        $db = dbConnectByName($db_name);
56
57        $stmt = $db->query("SELECT * FROM DigitalSignSync ORDER BY date LIMIT 1");
58        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
59
60        $this->log->LogDebug("Filename: " . $row['filename']);
61        $this->log->LogDebug("Version: " . $row['version']);
62
63        $this->filename = $row['filename'];
64        $this->version = $row['version'];
65        $this->log->LogDebug("Location: " . $this->location . $this->filename);
66    }
67
68    public function setXMLHeaders()
69    {
70        header('Content-Type: application/xml; charset=utf-8');
71    }
72
73    public function sendFileDownload()
74    {
75        if (isset($this->apiKey)) {
76            $filename = $this->filename;
77            $filepath = $this->location . $this->filename;
78            $this->log->LogDebug("Size [" . filesize($filepath) . "]");
79            set_time_limit(0);
80            header("Pragma: public");
81            header("Expires: 0");
82            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
83            header("Cache-Control: public");
84            header("Content-Description: File Transfer");
85            header("Content-Type: application/octet-stream");
86            header("Content-Disposition: attachment; filename='" . $filename . "'");
87            header("Content-Transfer-Encoding: binary");
88            header("Content-Length: " . filesize($filepath));
89            $file = @fopen($filepath, "rb");
90            if ($file) {
91                while (!feof($file)) {
92                    print(fread($file, 1024 * 8));
93                    flush();
94                    if (connection_status() != 0) {
95                        @fclose($file);
96                        die();
97                    }
98                }
99                @fclose($file);
100            }
101            $this->log->LogInfo("[" . $_SERVER['REMOTE_ADDR'] . "] Downloaded: " . $filename);
102        }
103    }
104
105    public function getXML()
106    {
107        $array = array("version" => $this->version, "filename" => $this->filename);
108        return $this->array2xml($array, "root");
109    }
110
111    private function array2xml($array, $wrap = 'ROW0', $upper = true)
112    {
113        // set initial value for XML string
114        $xml = '';
115        // wrap XML with $wrap TAG
116        if ($wrap != null) {
117            $xml .= "<$wrap>\n";
118        }
119        // main loop
120        foreach ($array as $key => $value) {
121            // set tags in uppercase if needed
122            if ($upper == true) {
123                $key = strtoupper($key);
124            }
125            // append to XML string
126            $xml .= "<$key>" . htmlspecialchars(trim($value)) . "</$key>";
127        }
128        // close wrap TAG if needed
129        if ($wrap != null) {
130            $xml .= "\n</$wrap>\n";
131        }
132        // return prepared XML string
133        return $xml;
134    }
135
136
137}