123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- class Database
- {
- public $conn;
- const DB_TABLE = 'fact_conf_marketplace';
- public function __construct()
- {
- $this->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_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)));
- }
- }
-
-
- }
- }
|