Database.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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_avizeserie'])) $new['fsync_option_avizeserie'] = $_POST['fsync_option_avizeserie'];
  125. if(isset($_POST['fsync_option_expfacturaserie'])) $new['fsync_option_expfacturaserie'] = $_POST['fsync_option_expfacturaserie'];
  126. if(isset($_POST['fsync_option_pdls'])) $new['fsync_option_pdls'] = $_POST['fsync_option_pdls'];
  127. if(isset($_POST['fsync_option_daysago'])) $new['fsync_option_daysago'] = $_POST['fsync_option_daysago'];
  128. if(isset($_POST['fsync_option_autosyncstock']) && !isset($_POST['fsync_option_daysago'])) $new['fsync_option_locations'] = '';
  129. if(isset($_POST['fsync_option_locations'])) $new['fsync_option_locations'] = implode(", ", $_POST['fsync_option_locations']);
  130. if(isset($_POST['fsync_option_withdiscount'])) $new['fsync_option_withdiscount'] = $_POST['fsync_option_withdiscount'];
  131. $current = $this->getSettings();
  132. $merged = $this->merge($current, $new);
  133. $this->update($merged);
  134. }
  135. public function getToken($shop = '')
  136. {
  137. $shopName = '';
  138. if(!empty($shop))
  139. {
  140. $shopName = mysqli_real_escape_string($this->conn, $shop);
  141. }
  142. else
  143. {
  144. if(isset($_REQUEST['shop']) && ($_REQUEST['shop'] != ''))
  145. {
  146. $shopName = mysqli_real_escape_string($this->conn, $_REQUEST['shop']);
  147. }
  148. }
  149. if(empty($shopName))
  150. {
  151. return '';
  152. }
  153. $sql = "SELECT market_apikey FROM " . self::DB_TABLE. " WHERE market_code = '" . $shopName . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'";
  154. $result = mysqli_query($this->conn, $sql);
  155. if($result)
  156. {
  157. if (mysqli_num_rows($result) > 0) {
  158. while($row = mysqli_fetch_assoc($result)) {
  159. return $row['market_apikey'];
  160. }
  161. } else {
  162. throw new \Exception($this->printException('A token is required for the script to run'));
  163. }
  164. }
  165. else
  166. {
  167. throw new \Exception($this->printException("Error: " . $sql . ": " . mysqli_error($this->conn)));
  168. }
  169. return '';
  170. }
  171. public function generateToken()
  172. {
  173. $query = array(
  174. "client_id" => API_KEY,
  175. "client_secret" => SHARED_SECRET,
  176. "code" => isset($_GET['code']) ? $_GET['code'] : ''
  177. );
  178. $access_token_url = "https://" . (isset($_REQUEST['shop']) ? $_REQUEST['shop'] : '') . "/admin/oauth/access_token";
  179. $ch = curl_init();
  180. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  181. curl_setopt($ch, CURLOPT_URL, $access_token_url);
  182. curl_setopt($ch, CURLOPT_POST, count($query));
  183. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));
  184. $result = curl_exec($ch);
  185. $curl_error = curl_error($ch);
  186. curl_close($ch);
  187. if($curl_error)
  188. {
  189. throw new \Exception('Error installation: ' . json_encode($curl_error));
  190. }
  191. $result = json_decode($result, true);
  192. if(!isset($result['access_token']))
  193. {
  194. throw new \Exception('Error installation: No token found');
  195. }
  196. return $result['access_token'];
  197. }
  198. public function add($access_token)
  199. {
  200. if(isset($_REQUEST['shop']) && ($_REQUEST['shop'] != ''))
  201. {
  202. $shop = mysqli_real_escape_string($this->conn, $_REQUEST['shop']);
  203. $token = mysqli_real_escape_string($this->conn, $access_token);
  204. $sql = "SELECT * FROM " . self::DB_TABLE. " WHERE market_code = '" . $shop . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'";
  205. $result = mysqli_query($this->conn, $sql);
  206. if($result)
  207. {
  208. if (mysqli_num_rows($result) > 0)
  209. {
  210. $sql = "UPDATE " . self::DB_TABLE. " SET market_apikey = '" . $token . "', market_conf_json = '' WHERE market_code = '" . $shop . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'";
  211. }
  212. else
  213. {
  214. $sql = 'INSERT INTO ' . self::DB_TABLE. ' (market_apikey, market_code) VALUE ("' . $token . '", "' . $shop . '")';
  215. }
  216. if (!mysqli_query($this->conn, $sql)) {
  217. throw new \Exception($this->printException($sql . ": " . mysqli_error($this->conn)));
  218. }
  219. }
  220. else
  221. {
  222. throw new \Exception($this->printException($sql . ": " . mysqli_error($this->conn)));
  223. }
  224. }
  225. }
  226. }