ebay.php (9068B)
1 <?php 2 class ControllerExtensionOpenbayEbay extends Controller { 3 public function inbound() { 4 $post_data = $this->request->post; 5 $secret = $this->config->get('ebay_secret'); 6 $active = $this->config->get('ebay_status'); 7 8 $this->load->model('extension/openbay/ebay_product'); 9 $this->load->model('extension/openbay/ebay_order'); 10 11 if(empty($post_data)) { 12 http_response_code(400); 13 $this->response->addHeader('Content-Type: application/json'); 14 $this->response->setOutput(json_encode(array('error' => 'Bad request'))); 15 $this->response->output(); 16 exit(); 17 } else { 18 $data = $this->openbay->decrypt($post_data['data'], $this->openbay->ebay->getEncryptionKey(), $this->openbay->ebay->getEncryptionIv()); 19 20 if(isset($data['secret']) && $secret == $data['secret'] && $active == 1) { 21 if($data['action'] == 'ItemUnsold') { 22 $this->openbay->ebay->log('Action: Unsold Item'); 23 $product_id = $this->openbay->ebay->getProductId($data['itemId']); 24 25 if($product_id != false) { 26 $this->openbay->ebay->log('eBay item link found with internal product'); 27 $rules = $this->model_extension_openbay_ebay_product->getRelistRule($data['itemId']); 28 29 if(!empty($rules)) { 30 $this->openbay->ebay->log('Item is due to be automatically relisted'); 31 $this->db->query("INSERT INTO `" . DB_PREFIX . "ebay_listing_pending` SET `ebay_item_id` = '" . $this->db->escape($data['itemId']) . "', `product_id` = '" . (int)$product_id . "', `key` = '" . $this->db->escape($data['key']) . "'"); 32 $this->openbay->ebay->removeItemByItemId($data['itemId']); 33 } else { 34 $this->openbay->ebay->log('No automation rule set'); 35 $this->openbay->ebay->removeItemByItemId($data['itemId']); 36 } 37 } 38 39 $this->response->addHeader('Content-Type: application/json'); 40 $this->response->setOutput(json_encode(array('msg' => 'ok'))); 41 } 42 43 if($data['action'] == 'ItemListed') { 44 $this->openbay->ebay->log('Action: Listed Item'); 45 46 $product_id = $this->openbay->ebay->getProductIdFromKey($data['key']); 47 48 if($product_id != false) { 49 $this->openbay->ebay->createLink($product_id, $data['itemId'], ''); 50 $this->db->query("DELETE FROM `" . DB_PREFIX . "ebay_listing_pending` WHERE `key` = '" . $this->db->escape($data['key']) . "' LIMIT 1"); 51 $this->openbay->ebay->log('A link was found with product id: ' . $product_id . ', item id: ' . $data['itemId'] . ' and key: ' . $data['key']); 52 } else { 53 $this->openbay->ebay->log('No link found to previous item'); 54 } 55 56 $this->response->addHeader('Content-Type: application/json'); 57 $this->response->setOutput(json_encode(array('msg' => 'ok'))); 58 } 59 60 if($data['action'] == 'newOrder') { 61 $this->openbay->ebay->log('Action: newOrder / Order data from polling'); 62 $this->model_extension_openbay_ebay_order->importOrders($data['data2']); 63 64 $this->response->addHeader('Content-Type: application/json'); 65 $this->response->setOutput(json_encode(array('msg' => 'ok'))); 66 } 67 68 if($data['action'] == 'notificationOrder') { 69 $this->openbay->ebay->log('Action: notificationOrder / Order data from notification'); 70 $this->model_extension_openbay_ebay_order->importOrders($data['data']); 71 72 $this->response->addHeader('Content-Type: application/json'); 73 $this->response->setOutput(json_encode(array('msg' => 'ok'))); 74 } 75 76 if($data['action'] == 'outputLog') { 77 if (file_exists(DIR_LOGS . "ebaylog.log")) { 78 header('Pragma: public'); 79 header('Expires: 0'); 80 header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 81 header('Cache-Control: private', false); 82 header('Content-Type: application/force-download'); 83 header('Content-Length: ' . filesize(DIR_LOGS . "ebaylog.log")); 84 header('Content-Disposition: attachment; filename="ebaylog.log"'); 85 header('Content-Transfer-Encoding: binary'); 86 header('Connection: close'); 87 readfile(DIR_LOGS . "ebaylog.log"); 88 exit(); 89 } else { 90 $this->openbay->ebay->log('Action: outputLog / No log file found'); 91 92 http_response_code(404); 93 $this->response->addHeader('Content-Type: application/json'); 94 $this->response->setOutput(json_encode(array('error' => 'Log file not found'))); 95 } 96 } 97 98 if($data['action'] == 'config') { 99 $this->openbay->ebay->log('Action: config / Check store php limits for import options'); 100 101 $post_size = ini_get('post_max_size'); 102 $post_size = (int)str_replace(array('M','m','Mb','MB'), '', $post_size); 103 $version = (int)$this->config->get('feed_openbaypro_version'); 104 105 $this->response->addHeader('Content-Type: application/json'); 106 $this->response->setOutput(json_encode(array('msg' => 'ok', 'max_post' => $post_size, 'version' => $version))); 107 } 108 } else { 109 $this->openbay->ebay->log('Secret incorrect or module not active.'); 110 111 http_response_code(401); 112 $this->response->addHeader('Content-Type: application/json'); 113 $this->response->setOutput(json_encode(array('error' => 'Authorisation failed or module inactive'))); 114 $this->response->output(); 115 exit(); 116 } 117 } 118 } 119 120 public function importItems() { 121 set_time_limit(0); 122 123 $data = $this->request->post; 124 $secret = $this->config->get('ebay_secret'); 125 $active = $this->config->get('ebay_status'); 126 127 $this->response->addHeader('Content-Type: application/json'); 128 129 if(isset($data['secret']) && $secret == $data['secret'] && $active == 1 && isset($data['data'])) { 130 $this->load->model('extension/openbay/ebay_order'); 131 $this->load->model('extension/openbay/ebay_product'); 132 $this->model_extension_openbay_ebay_product->importItems($data); 133 $this->response->setOutput(json_encode(array('msg' => 'ok', 'error' => false))); 134 } else { 135 $this->response->setOutput(json_encode(array('msg' => 'Auth failed', 'error' => true))); 136 } 137 } 138 139 public function setup() { 140 @set_time_limit(0); 141 142 $this->load->model('setting/setting'); 143 $settings = $this->model_setting_setting->getSetting('ebay'); 144 145 $this->response->addHeader('Cache-Control: no-cache, must-revalidate'); 146 $this->response->addHeader('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 147 $this->response->addHeader('Content-type: application/json; charset=utf-8'); 148 149 if( 150 (isset($settings['ebay_token']) && !empty($settings['ebay_token'])) || 151 (isset($settings['ebay_secret']) && !empty($settings['ebay_secret'])) || 152 (isset($settings['ebay_encryption_key']) && !empty($settings['ebay_encryption_key'])) || 153 (isset($settings['ebay_encryption_iv']) && !empty($settings['ebay_encryption_iv'])) || 154 !isset($this->request->post['token']) || 155 !isset($this->request->post['secret']) || 156 !isset($this->request->post['encryption_key']) || 157 !isset($this->request->post['encryption_iv']) 158 ) { 159 $this->response->setOutput(json_encode(array('msg' => 'fail', 'reason' => 'Tokens are already setup or data missing'))); 160 } else { 161 $settings['ebay_token'] = $this->request->post['token']; 162 $settings['ebay_secret'] = $this->request->post['secret']; 163 $settings['ebay_encryption_key'] = $this->request->post['encryption_key']; 164 $settings['ebay_encryption_iv'] = $this->request->post['encryption_iv']; 165 166 $this->openbay->ebay->editSetting('ebay', $settings); 167 168 $this->response->setOutput(json_encode(array('msg' => 'ok', 'reason' => 'Auto setup has completed','version' => (int)$this->config->get('feed_openbaypro_version')))); 169 } 170 } 171 172 public function sync() { 173 @set_time_limit(0); 174 175 $this->response->addHeader('Content-Type: application/json'); 176 177 if($this->request->post['process'] == 'categories') { 178 $this->response->setOutput(json_encode($this->openbay->ebay->updateCategories())); 179 }elseif($this->request->post['process'] == 'settings') { 180 $this->response->setOutput(json_encode($this->openbay->ebay->updateSettings())); 181 }elseif($this->request->post['process'] == 'store') { 182 $this->response->setOutput(json_encode($this->openbay->ebay->updateStore())); 183 } 184 } 185 186 public function eventAddOrder($route, $data) { 187 188 } 189 190 public function eventAddOrderHistory($route, $data) { 191 $this->openbay->ebay->log('eventAddOrderHistory Event fired: ' . $route); 192 193 if (isset($data[0]) && !empty($data[0])) { 194 $this->load->model('extension/openbay/ebay_order'); 195 196 $this->openbay->ebay->log('Order ID: ' . (int)$data[0]); 197 198 $this->model_extension_openbay_ebay_order->addOrderHistory((int)$data[0]); 199 } 200 } 201 }