/** * Generic connection manager for sending data * * @author Jonathan Davis * @since 1.1 * * @param string $data The encoded data to send * @param string $url (optional) The API endpoint URL to connect to * @param array $options (optional) WP_Http options * @return string Raw response **/ public function send($data, $url = false) { // Adds optional support for options $parameters = func_num_args(); $args = func_get_args(); if ($parameters > 2) { $options = $args[$parameters - 1]; } else { $options = array(); } $defaults = array('method' => 'POST', 'timeout' => SHOPP_GATEWAY_TIMEOUT, 'redirection' => 7, 'httpversion' => '1.0', 'user-agent' => SHOPP_GATEWAY_USERAGENT . '; ' . get_bloginfo('url'), 'blocking' => true, 'headers' => array(), 'cookies' => array(), 'body' => $data, 'compress' => false, 'decompress' => true, 'sslverify' => true); $params = array_merge($defaults, $options); $connection = new WP_Http(); $result = $connection->request($url, $params); if (is_wp_error($result)) { $errors = array(); foreach ($result->errors as $errname => $msgs) { $errors[] = join(' ', $msgs); } $errors = join(' ', $errors); new ShoppError($this->name . ": " . Lookup::errors('gateway', 'fail') . " {$errors} " . Lookup::errors('contact', 'admin') . " (WP_HTTP)", 'gateway_comm_err', SHOPP_COMM_ERR); return false; } elseif (empty($result) || !isset($result['response'])) { new ShoppError($this->name . ": " . Lookup::errors('gateway', 'noresponse'), 'gateway_comm_err', SHOPP_COMM_ERR); return false; } else { extract($result); } if (!in_array($response['code'], $this->codes)) { $error = Lookup::errors('gateway', 'http-' . $response['code']); if (empty($error)) { $error = Lookup::errors('gateway', 'http-unknown'); } new ShoppError($this->name . ": {$error}", 'gateway_comm_err', SHOPP_COMM_ERR); return false; } return $body; }
/** * AJAX behavior to process uploaded images * * @author Jonathan Davis * @return string JSON encoded result with thumbnail id and src **/ public static function images() { $context = false; $error = false; $valid_contexts = array('product', 'category'); if (isset($_FILES['Filedata']['error'])) { $error = $_FILES['Filedata']['error']; } if ($error) { die(json_encode(array('error' => Lookup::errors('uploads', $error)))); } if (isset($_REQUEST['type']) && in_array(strtolower($_REQUEST['type']), $valid_contexts)) { $parent = $_REQUEST['parent']; $context = strtolower($_REQUEST['type']); } if (!$context) { die(json_encode(array('error' => Shopp::__('The file could not be saved because the server cannot tell whether to attach the asset to a product or a category.')))); } if (!@is_uploaded_file($_FILES['Filedata']['tmp_name'])) { die(json_encode(array('error' => Shopp::__('The file could not be saved because the upload was not found on the server.')))); } if (0 == $_FILES['Filedata']['size']) { die(json_encode(array('error' => Shopp::__('The file could not be saved because the uploaded file is empty.')))); } // Save the source image if ('category' == $context) { $Image = new CategoryImage(); } else { $Image = new ProductImage(); } $Image->parent = $parent; $Image->type = 'image'; $Image->name = 'original'; $Image->filename = $_FILES['Filedata']['name']; $context = 'upload'; $tempfile = $_FILES['Filedata']['tmp_name']; if (!@is_readable($tempfile)) { $context = 'file'; $tempfile = get_temp_dir() . $Image->filename; if (!@move_uploaded_file($_FILES['Filedata']['tmp_name'], $tempfile)) { die(json_encode(array('error' => Shopp::__('The file could not be saved because the web server does not have permission to read the upload.')))); } } list($Image->width, $Image->height, $Image->mime, $Image->attr) = getimagesize($tempfile); $Image->mime = image_type_to_mime_type($Image->mime); $Image->size = filesize($tempfile); if (!$Image->unique()) { die(json_encode(array('error' => Shopp::__('The image already exists, but a new filename could not be generated.')))); } $Image->store($tempfile, $context); if ('file' == $context) { unlink($tempfile); } $Error = ShoppErrors()->code('storage_engine_save'); if (!empty($Error)) { die(json_encode(array('error' => $Error->message(true)))); } $Image->save(); if (empty($Image->id)) { die(json_encode(array('error' => Shopp::__('The image reference was not saved to the database.')))); } echo json_encode(array('id' => $Image->id)); exit; }
/** * Validate order data before transaction processing * * @author Jonathan Davis * @since 1.1 * * @return boolean Validity of the order **/ public function isvalid($report = true) { $Customer = $this->Customer; $Shipping = $this->Shipping; $Shiprates = $this->Shiprates; $Payments = $this->Payments; $Cart = $this->Cart; $valid = true; $errlevel = $report ? SHOPP_TRXN_ERR : SHOPP_DEBUG_ERR; shopp_debug('Validating order data for processing'); if (0 == $Cart->count()) { $valid = apply_filters('shopp_ordering_empty_cart', false); shopp_add_error(__('There are no items in the cart.', 'Shopp'), $errlevel); } $stock = true; foreach ($Cart as $item) { if (!$item->instock()) { $valid = apply_filters('shopp_ordering_items_outofstock', false); shopp_add_error(sprintf(__('%s does not have sufficient stock to process order.', 'Shopp'), $item->name . (empty($item->option->label) ? '' : '(' . $item->option->label . ')')), $errlevel); $stock = false; } } $valid_customer = true; if (!$Customer) { $valid_customer = apply_filters('shopp_ordering_empty_customer', false); } // No Customer // Always require name and email if (empty($Customer->firstname)) { $valid_customer = apply_filters('shopp_ordering_empty_firstname', false); } if (empty($Customer->lastname)) { $valid_customer = apply_filters('shopp_ordering_empty_lastname', false); } if (empty($Customer->email)) { $valid_customer = apply_filters('shopp_ordering_empty_email', false); } if (!$valid_customer) { $valid = false; shopp_add_error(__('There is not enough customer information to process the order.', 'Shopp'), $errlevel); } // Check for shipped items but no Shipping information $valid_shipping = true; if ($Cart->shipped() && shopp_setting_enabled('shipping')) { if (empty($Shipping->address)) { $valid_shipping = apply_filters('shopp_ordering_empty_shipping_address', false); } if (empty($Shipping->country)) { $valid_shipping = apply_filters('shopp_ordering_empty_shipping_country', false); } if (empty($Shipping->postcode)) { $valid_shipping = apply_filters('shopp_ordering_empty_shipping_postcode', false); } if ($Shiprates->count() == 0 && !$Shiprates->free()) { $valid = apply_filters('shopp_ordering_no_shipping_costs', false); $message = __('The order cannot be processed. No shipping is available to the address you provided. Please return to %scheckout%s and try again.', 'Shopp'); if ($Shiprates->realtime()) { $message = __('The order cannot be processed. The shipping rate service did not provide rates because of a problem and no other shipping is available to the address you provided. Please return to %scheckout%s and try again or contact the store administrator.', 'Shopp'); } if (!$valid) { shopp_add_error(sprintf($message, '<a href="' . Shopp::url(false, 'checkout', $this->security()) . '">', '</a>'), $errlevel); } } } if (!$valid_shipping) { $valid = false; shopp_add_error(__('The shipping address information is incomplete. The order cannot be processed.', 'Shopp'), $errlevel); } // Alert when no gateway is configured (and the order is not free) if ($Payments->count() == 0 && $Cart->total() > 0) { $valid = false; shopp_add_error(Lookup::errors('gateway', 'nogateways'), $errlevel); } return $valid; }
public function taxrate_upload() { if (!isset($_FILES['ratefile'])) { return false; } $upload = $_FILES['ratefile']; $filename = $upload['tmp_name']; if (empty($filename) && empty($upload['name']) && !isset($_POST['upload'])) { return false; } $error = false; if ($upload['error'] != 0) { return array('error' => Lookup::errors('uploads', $upload['error'])); } if (!is_readable($filename)) { return array('error' => Lookup::errors('uploadsecurity', 'is_readable')); } if (empty($upload['size'])) { return array('error' => Lookup::errors('uploadsecurity', 'is_empty')); } if ($upload['size'] != filesize($filename)) { return array('error' => Lookup::errors('uploadsecurity', 'filesize_mismatch')); } if (!is_uploaded_file($filename)) { return array('error' => Lookup::errors('uploadsecurity', 'is_uploaded_file')); } $data = file_get_contents($upload['tmp_name']); $cr = array("\r\n", "\r"); $formats = array(0 => false, 3 => 'xml', 4 => 'tab', 5 => 'csv'); preg_match('/((<[^>]+>.+?<\\/[^>]+>)|(.+?\\t.+?[\\n|\\r])|(.+?,.+?[\\n|\\r]))/', $data, $_); $format = $formats[count($_)]; if (!$format) { return array('error' => __('The uploaded file is not properly formatted as an XML, CSV or tab-delimmited file.', 'Shopp')); } $_ = array(); switch ($format) { case 'xml': /* Example XML import file: <localtaxrates> <taxrate name="Kent">1</taxrate> <taxrate name="New Castle">0.25</taxrate> <taxrate name="Sussex">1.4</taxrate> </localtaxrates> Taxrate record format: <taxrate name="(Name of locality)">(Percentage of the supplemental tax)</taxrate> Tax rate percentages should be represented as percentage numbers, not decimal percentages: 1.25 = 1.25% (0.0125) 10 = 10% (0.1) */ if (!class_exists('xmlQuery')) { require SHOPP_MODEL_PATH . '/XML.php'; } $XML = new xmlQuery($data); $taxrates = $XML->tag('taxrate'); while ($rate = $taxrates->each()) { $name = $rate->attr(false, 'name'); $value = $rate->content(); $_[$name] = $value; } break; case 'csv': ini_set('auto_detect_line_endings', true); if (($csv = fopen($upload['tmp_name'], 'r')) === false) { return array('error' => Lookup::errors('uploadsecurity', 'is_readable')); } while (($data = fgetcsv($csv, 1000)) !== false) { $_[$data[0]] = !empty($data[1]) ? $data[1] : 0; } fclose($csv); ini_set('auto_detect_line_endings', false); break; case 'tab': default: $data = str_replace($cr, "\n", $data); $lines = explode("\n", $data); foreach ($lines as $line) { list($key, $value) = explode("\t", $line); $_[$key] = $value; } } if (empty($_)) { array('error' => __('No useable tax rates could be found. The uploaded file may not be properly formatted.', 'Shopp')); } return apply_filters('shopp_local_taxrates_upload', $_); }
/** * Communicates with the Shopp update service server * * @author Jonathan Davis * @since 1.1 * * @param array $request (optional) A list of request variables to send * @param array $data (optional) A list of data variables to send * @param array $options (optional) * @return string The response from the server **/ public static function callhome($request = array(), $data = array(), $options = array()) { $query = http_build_query(array_merge(array('ver' => '1.2'), $request), '', '&'); $data = http_build_query($data, '', '&'); $defaults = array('method' => 'POST', 'timeout' => 20, 'redirection' => 7, 'httpversion' => '1.0', 'user-agent' => SHOPP_GATEWAY_USERAGENT . '; ' . get_bloginfo('url'), 'blocking' => true, 'headers' => array(), 'cookies' => array(), 'body' => $data, 'compress' => false, 'decompress' => true, 'sslverify' => false); $params = array_merge($defaults, $options); $URL = ShoppSupport::HOMEPAGE . "?{$query}"; // error_log('CALLHOME REQUEST ------------------'); // error_log($URL); // error_log(json_encode($params)); $connection = new WP_Http(); $result = $connection->request($URL, $params); // error_log(json_encode($result)); // error_log('-------------- END CALLHOME REQUEST'); extract($result); if (isset($response['code']) && 200 != $response['code']) { // Fail, fallback to http instead $URL = str_replace('https://', 'http://', $URL); $connection = new WP_Http(); $result = $connection->request($URL, $params); extract($result); } if (is_wp_error($result)) { $errors = array(); foreach ($result->errors as $errname => $msgs) { $errors[] = join(' ', $msgs); } $errors = join(' ', $errors); shopp_add_error("Shopp: " . Lookup::errors('callhome', 'fail') . " {$errors} " . Lookup::errors('contact', 'admin') . " (WP_HTTP)", SHOPP_ADMIN_ERR); return false; } elseif (empty($result) || !isset($result['response'])) { shopp_add_error("Shopp: " . Lookup::errors('callhome', 'noresponse'), SHOPP_ADMIN_ERR); return false; } else { extract($result); } if (isset($response['code']) && 200 != $response['code']) { $error = Lookup::errors('callhome', 'http-' . $response['code']); if (empty($error)) { $error = Lookup::errors('callhome', 'http-unkonwn'); } shopp_add_error("Shopp: {$error}", 'callhome_comm_err', SHOPP_ADMIN_ERR); return $body; } return $body; }