conn = @mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE); if (!$this->conn) { echo 'Database connection failed: ' . mysqli_connect_error(); exit; } } public function getConnection() { return $this->conn; } public function getAll() { $shops = array(); $sql = "SELECT * FROM " . self::DB_TABLE. " WHERE market_code LIKE '%" . SHOPIFY_DOMAIN . "%'"; $result = mysqli_query($this->conn, $sql); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { $shops[] = $row; } } return $shops; } public function delete($shop) { $shopName = mysqli_real_escape_string($this->conn, $shop); $sqls = "SELECT * FROM " . self::DB_TABLE. " WHERE market_code = '" . $shopName . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'"; $result = mysqli_query($this->conn, $sqls); if (mysqli_num_rows($result) == 1) { $sqlu = "UPDATE " . self::DB_TABLE. " SET market_apikey = '', market_conf_json = '' WHERE market_code = '" . $shopName . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'"; if (!mysqli_query($this->conn, $sqlu)) { throw new \Exception("Error deleting shop: " . mysqli_error($this->conn)); } else { echo "Shop deleted successfully"; } } } protected function merge($currentShopData, $newShopData) { if(empty($currentShopData)) { return $newShopData; } else { return array_merge($currentShopData, $newShopData); } } protected function update($data) { if(isset($_REQUEST['shop']) && ($_REQUEST['shop'] != '')) { $shop = mysqli_real_escape_string($this->conn, $_REQUEST['shop']); $sql = "UPDATE " . self::DB_TABLE. " SET market_conf_json = '" . json_encode($data) . "' WHERE market_code = '" . $shop . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'"; if (!mysqli_query($this->conn, $sql)) { throw new \Exception($this->printException($sql . ": " . mysqli_error($this->conn))); } } else { throw new \Exception($this->printException(SHOP_REQUIRED)); } } private function printException($message) { return 'Err Facturis Db: "' . $message . '". '; } public function getSettings($shop = '') { $shopName = ''; if(!empty($shop)) { $shopName = mysqli_real_escape_string($this->conn, $shop); } else { if(isset($_REQUEST['shop']) && ($_REQUEST['shop'] != '')) { $shopName = mysqli_real_escape_string($this->conn, $_REQUEST['shop']); } } if(empty($shopName)) { return array(); } $sql = "SELECT market_conf_json FROM " . self::DB_TABLE. " WHERE market_code = '" . $shopName . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'"; $result = mysqli_query($this->conn, $sql); if($result) { if (mysqli_num_rows($result)> 0) { while($row = mysqli_fetch_assoc($result)) { return json_decode($row['market_conf_json'], true); } } else { return array(); } } else { throw new \Exception($this->printException($sql . ": " . mysqli_error($this->conn))); } return array(); } public function saveOptions() { $new = array(); if(isset($_POST['fsync_auth_apikey'])) $new['fsync_auth_apikey'] = $_POST['fsync_auth_apikey']; if(isset($_POST['fsync_auth_username'])) $new['fsync_auth_username'] = $_POST['fsync_auth_username']; if(isset($_POST['fsync_auth_password'])) $new['fsync_auth_password'] = $_POST['fsync_auth_password']; if(isset($_POST['fsync_auth_fiscalcode'])) $new['fsync_auth_fiscalcode'] = $_POST['fsync_auth_fiscalcode']; if(isset($_POST['fsync_option_autosyncstock'])) $new['fsync_option_autosyncstock'] = $_POST['fsync_option_autosyncstock']; if(isset($_POST['fsync_option_autosyncorder'])) $new['fsync_option_autosyncorder'] = $_POST['fsync_option_autosyncorder']; if(isset($_POST['fsync_option_filterstock'])) $new['fsync_option_filterstock'] = $_POST['fsync_option_filterstock']; if(isset($_POST['fsync_option_syncordersas'])) $new['fsync_option_syncordersas'] = $_POST['fsync_option_syncordersas']; if(isset($_POST['fsync_option_proformaserie'])) $new['fsync_option_proformaserie'] = $_POST['fsync_option_proformaserie']; if(isset($_POST['fsync_option_avizeserie'])) $new['fsync_option_avizeserie'] = $_POST['fsync_option_avizeserie']; if(isset($_POST['fsync_option_expfacturaserie'])) $new['fsync_option_expfacturaserie'] = $_POST['fsync_option_expfacturaserie']; if(isset($_POST['fsync_option_pdls'])) $new['fsync_option_pdls'] = $_POST['fsync_option_pdls']; if(isset($_POST['fsync_option_daysago'])) $new['fsync_option_daysago'] = $_POST['fsync_option_daysago']; if(isset($_POST['fsync_option_autosyncstock']) && !isset($_POST['fsync_option_daysago'])) $new['fsync_option_locations'] = ''; if(isset($_POST['fsync_option_locations'])) $new['fsync_option_locations'] = implode(", ", $_POST['fsync_option_locations']); if(isset($_POST['fsync_option_withdiscount'])) $new['fsync_option_withdiscount'] = $_POST['fsync_option_withdiscount']; $current = $this->getSettings(); $merged = $this->merge($current, $new); $this->update($merged); } public function getToken($shop = '') { $shopName = ''; if(!empty($shop)) { $shopName = mysqli_real_escape_string($this->conn, $shop); } else { if(isset($_REQUEST['shop']) && ($_REQUEST['shop'] != '')) { $shopName = mysqli_real_escape_string($this->conn, $_REQUEST['shop']); } } if(empty($shopName)) { return ''; } $sql = "SELECT market_apikey FROM " . self::DB_TABLE. " WHERE market_code = '" . $shopName . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'"; $result = mysqli_query($this->conn, $sql); if($result) { if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { return $row['market_apikey']; } } else { throw new \Exception($this->printException('A token is required for the script to run')); } } else { throw new \Exception($this->printException("Error: " . $sql . ": " . mysqli_error($this->conn))); } return ''; } public function generateToken() { $query = array( "client_id" => API_KEY, "client_secret" => SHARED_SECRET, "code" => isset($_GET['code']) ? $_GET['code'] : '' ); $access_token_url = "https://" . (isset($_REQUEST['shop']) ? $_REQUEST['shop'] : '') . "/admin/oauth/access_token"; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $access_token_url); curl_setopt($ch, CURLOPT_POST, count($query)); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query)); $result = curl_exec($ch); $curl_error = curl_error($ch); curl_close($ch); if($curl_error) { throw new \Exception('Error installation: ' . json_encode($curl_error)); } $result = json_decode($result, true); if(!isset($result['access_token'])) { throw new \Exception('Error installation: No token found'); } return $result['access_token']; } public function add($access_token) { if(isset($_REQUEST['shop']) && ($_REQUEST['shop'] != '')) { $shop = mysqli_real_escape_string($this->conn, $_REQUEST['shop']); $token = mysqli_real_escape_string($this->conn, $access_token); $sql = "SELECT * FROM " . self::DB_TABLE. " WHERE market_code = '" . $shop . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'"; $result = mysqli_query($this->conn, $sql); if($result) { if (mysqli_num_rows($result) > 0) { $sql = "UPDATE " . self::DB_TABLE. " SET market_apikey = '" . $token . "', market_conf_json = '' WHERE market_code = '" . $shop . "' AND market_code LIKE '%" . SHOPIFY_DOMAIN . "%'"; } else { $sql = 'INSERT INTO ' . self::DB_TABLE. ' (market_apikey, market_code) VALUE ("' . $token . '", "' . $shop . '")'; } if (!mysqli_query($this->conn, $sql)) { throw new \Exception($this->printException($sql . ": " . mysqli_error($this->conn))); } } else { throw new \Exception($this->printException($sql . ": " . mysqli_error($this->conn))); } } } }