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