Database.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. class Database
  3. {
  4. public $conn;
  5. const DB_TABLE = 'fact_conf_marketplace';
  6. public function __construct()
  7. {
  8. $this->conn = @mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
  9. if (!$this->conn) {
  10. echo 'Database connection failed: ' . mysqli_connect_error();
  11. exit;
  12. }
  13. }
  14. public function getConnection()
  15. {
  16. return $this->conn;
  17. }
  18. public function getAll()
  19. {
  20. $shops = array();
  21. $sql = "SELECT * FROM " . self::DB_TABLE. " WHERE market_code LIKE '%" . SHOPIFY_DOMAIN . "%'";
  22. $result = mysqli_query($this->conn, $sql);
  23. if (mysqli_num_rows($result) > 0) {
  24. while($row = mysqli_fetch_assoc($result)) {
  25. $shops[] = $row;
  26. }
  27. }
  28. return $shops;
  29. }
  30. public function delete($shop)
  31. {
  32. $shopName = mysqli_real_escape_string($this->conn, $shop);
  33. $sqls = "SELECT * FROM " . self::DB_TABLE. " WHERE market_code = '" . $shopName . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'";
  34. $result = mysqli_query($this->conn, $sqls);
  35. if (mysqli_num_rows($result) == 1) {
  36. $sqlu = "UPDATE " . self::DB_TABLE. " SET market_apikey = '', market_conf_json = '' WHERE market_code = '" . $shopName . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'";
  37. if (!mysqli_query($this->conn, $sqlu)) {
  38. throw new \Exception("Error deleting shop: " . mysqli_error($this->conn));
  39. }
  40. else
  41. {
  42. echo "Shop deleted successfully";
  43. }
  44. }
  45. }
  46. protected function merge($currentShopData, $newShopData)
  47. {
  48. if(empty($currentShopData))
  49. {
  50. return $newShopData;
  51. }
  52. else
  53. {
  54. return array_merge($currentShopData, $newShopData);
  55. }
  56. }
  57. protected function update($data)
  58. {
  59. if(isset($_REQUEST['shop']) && ($_REQUEST['shop'] != ''))
  60. {
  61. $shop = mysqli_real_escape_string($this->conn, $_REQUEST['shop']);
  62. $sql = "UPDATE " . self::DB_TABLE. " SET market_conf_json = '" . json_encode($data) . "' WHERE market_code = '" . $shop . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'";
  63. if (!mysqli_query($this->conn, $sql)) {
  64. throw new \Exception($this->printException($sql . ": " . mysqli_error($this->conn)));
  65. }
  66. }
  67. else
  68. {
  69. throw new \Exception($this->printException(SHOP_REQUIRED));
  70. }
  71. }
  72. private function printException($message)
  73. {
  74. return 'Err Facturis Db: "' . $message . '". ';
  75. }
  76. public function getSettings($shop = '')
  77. {
  78. $shopName = '';
  79. if(!empty($shop))
  80. {
  81. $shopName = mysqli_real_escape_string($this->conn, $shop);
  82. }
  83. else
  84. {
  85. if(isset($_REQUEST['shop']) && ($_REQUEST['shop'] != ''))
  86. {
  87. $shopName = mysqli_real_escape_string($this->conn, $_REQUEST['shop']);
  88. }
  89. }
  90. if(empty($shopName))
  91. {
  92. return array();
  93. }
  94. $sql = "SELECT market_conf_json FROM " . self::DB_TABLE. " WHERE market_code = '" . $shopName . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'";
  95. $result = mysqli_query($this->conn, $sql);
  96. if($result)
  97. {
  98. if (mysqli_num_rows($result)> 0) {
  99. while($row = mysqli_fetch_assoc($result)) {
  100. return json_decode($row['market_conf_json'], true);
  101. }
  102. } else {
  103. return array();
  104. }
  105. }
  106. else
  107. {
  108. throw new \Exception($this->printException($sql . ": " . mysqli_error($this->conn)));
  109. }
  110. return array();
  111. }
  112. public function saveOptions()
  113. {
  114. $new = array();
  115. if(isset($_POST['fsync_auth_apikey'])) $new['fsync_auth_apikey'] = $_POST['fsync_auth_apikey'];
  116. if(isset($_POST['fsync_auth_username'])) $new['fsync_auth_username'] = $_POST['fsync_auth_username'];
  117. if(isset($_POST['fsync_auth_password'])) $new['fsync_auth_password'] = $_POST['fsync_auth_password'];
  118. if(isset($_POST['fsync_auth_fiscalcode'])) $new['fsync_auth_fiscalcode'] = $_POST['fsync_auth_fiscalcode'];
  119. if(isset($_POST['fsync_option_autosyncstock'])) $new['fsync_option_autosyncstock'] = $_POST['fsync_option_autosyncstock'];
  120. if(isset($_POST['fsync_option_autosyncorder'])) $new['fsync_option_autosyncorder'] = $_POST['fsync_option_autosyncorder'];
  121. if(isset($_POST['fsync_option_filterstock'])) $new['fsync_option_filterstock'] = $_POST['fsync_option_filterstock'];
  122. if(isset($_POST['fsync_option_syncordersas'])) $new['fsync_option_syncordersas'] = $_POST['fsync_option_syncordersas'];
  123. if(isset($_POST['fsync_option_proformaserie'])) $new['fsync_option_proformaserie'] = $_POST['fsync_option_proformaserie'];
  124. if(isset($_POST['fsync_option_daysago'])) $new['fsync_option_daysago'] = $_POST['fsync_option_daysago'];
  125. if(isset($_POST['fsync_option_autosyncstock']) && !isset($_POST['fsync_option_daysago'])) $new['fsync_option_locations'] = '';
  126. if(isset($_POST['fsync_option_locations'])) $new['fsync_option_locations'] = implode(", ", $_POST['fsync_option_locations']);
  127. if(isset($_POST['fsync_option_withdiscount'])) $new['fsync_option_withdiscount'] = $_POST['fsync_option_withdiscount'];
  128. $current = $this->getSettings();
  129. $merged = $this->merge($current, $new);
  130. $this->update($merged);
  131. }
  132. public function getToken($shop = '')
  133. {
  134. $shopName = '';
  135. if(!empty($shop))
  136. {
  137. $shopName = mysqli_real_escape_string($this->conn, $shop);
  138. }
  139. else
  140. {
  141. if(isset($_REQUEST['shop']) && ($_REQUEST['shop'] != ''))
  142. {
  143. $shopName = mysqli_real_escape_string($this->conn, $_REQUEST['shop']);
  144. }
  145. }
  146. if(empty($shopName))
  147. {
  148. return '';
  149. }
  150. $sql = "SELECT market_apikey FROM " . self::DB_TABLE. " WHERE market_code = '" . $shopName . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'";
  151. $result = mysqli_query($this->conn, $sql);
  152. if($result)
  153. {
  154. if (mysqli_num_rows($result) > 0) {
  155. while($row = mysqli_fetch_assoc($result)) {
  156. return $row['market_apikey'];
  157. }
  158. } else {
  159. throw new \Exception($this->printException('A token is required for the script to run'));
  160. }
  161. }
  162. else
  163. {
  164. throw new \Exception($this->printException("Error: " . $sql . ": " . mysqli_error($this->conn)));
  165. }
  166. return '';
  167. }
  168. public function generateToken()
  169. {
  170. $query = array(
  171. "client_id" => API_KEY,
  172. "client_secret" => SHARED_SECRET,
  173. "code" => isset($_GET['code']) ? $_GET['code'] : ''
  174. );
  175. $access_token_url = "https://" . (isset($_REQUEST['shop']) ? $_REQUEST['shop'] : '') . "/admin/oauth/access_token";
  176. $ch = curl_init();
  177. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  178. curl_setopt($ch, CURLOPT_URL, $access_token_url);
  179. curl_setopt($ch, CURLOPT_POST, count($query));
  180. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));
  181. $result = curl_exec($ch);
  182. $curl_error = curl_error($ch);
  183. curl_close($ch);
  184. if($curl_error)
  185. {
  186. throw new \Exception('Error installation: ' . json_encode($curl_error));
  187. }
  188. $result = json_decode($result, true);
  189. if(!isset($result['access_token']))
  190. {
  191. throw new \Exception('Error installation: No token found');
  192. }
  193. return $result['access_token'];
  194. }
  195. public function add($access_token)
  196. {
  197. if(isset($_REQUEST['shop']) && ($_REQUEST['shop'] != ''))
  198. {
  199. $shop = mysqli_real_escape_string($this->conn, $_REQUEST['shop']);
  200. $token = mysqli_real_escape_string($this->conn, $access_token);
  201. $sql = "SELECT * FROM " . self::DB_TABLE. " WHERE market_code = '" . $shop . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'";
  202. $result = mysqli_query($this->conn, $sql);
  203. if($result)
  204. {
  205. if (mysqli_num_rows($result) > 0)
  206. {
  207. $sql = "UPDATE " . self::DB_TABLE. " SET market_apikey = '" . $token . "', market_conf_json = '' WHERE market_code = '" . $shop . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'";
  208. }
  209. else
  210. {
  211. $sql = 'INSERT INTO ' . self::DB_TABLE. ' (market_apikey, market_code) VALUE ("' . $token . '", "' . $shop . '")';
  212. }
  213. if (!mysqli_query($this->conn, $sql)) {
  214. throw new \Exception($this->printException($sql . ": " . mysqli_error($this->conn)));
  215. }
  216. }
  217. else
  218. {
  219. throw new \Exception($this->printException($sql . ": " . mysqli_error($this->conn)));
  220. }
  221. }
  222. }
  223. }