/** * Save an asset to the database * * @author Jonathan Davis * @since 1.1 * * @param Asset $asset Asset object the data is associated with * @param string $data Binary data or path to the file to be saved * @param string $type (optional) Type of data provided - binary (default) or file * @return string|boolean A URI for the resource or false if failed **/ public function save($asset, $data, $type = 'binary') { if (empty($data)) { return false; } if ('binary' != $type) { if (!is_readable($data)) { die("Could not read the file."); } // Die because we can't use ShoppError $data = file_get_contents($data); } $data = @mysqli_real_escape_string(sDB::get()->dbh, $data); if (!$asset->id) { $uri = sDB::query("INSERT {$this->_table} SET data='{$data}'"); } else { sDB::query("UPDATE {$this->_table} SET data='{$data}' WHERE {$this->_key}='{$asset->uri}'"); } if (isset($uri)) { return $uri; } return false; }
/** * Provides the number of records found in the last query * * @author Jonathan Davis * @since 1.0 * * @return int The number of records found **/ public static function found() { $db = sDB::get(); $found = $db->found; $db->found = false; return $found; }
/** * Shopp 1.1.0 upgrades * * @author Jonathan Davis * @since 1.1 * * @return void **/ public function upgrade_110() { // 1.1 schema changes $db_version = ShoppSettings::dbversion(); if ($db_version < 1100) { return; } // Skip db_version is not less than 1100 $this->upschema('schema-110.sql'); $meta_table = ShoppDatabaseObject::tablename('meta'); $setting_table = ShoppDatabaseObject::tablename('setting'); $product_table = ShoppDatabaseObject::tablename('product'); // Update product status from the 'published' column sDB::query("UPDATE {$product_table} SET status=CAST(published AS unsigned)"); // Set product publish date based on the 'created' date column sDB::query("UPDATE {$product_table} SET publish=created WHERE status='publish'"); // Update Catalog $catalog_table = ShoppDatabaseObject::tablename('catalog'); sDB::query("UPDATE {$catalog_table} set parent=IF(category!=0, category, tag), type=IF(category!=0, 'category', 'tag')"); // Update specs $meta_table = ShoppDatabaseObject::tablename('meta'); $spec_table = ShoppDatabaseObject::tablename('spec'); $now = current_time('mysql'); sDB::query("INSERT INTO {$meta_table} (parent, context, type, name, value, numeral, sortorder, created, modified)\n\t\t\t\t\tSELECT product, 'product', 'spec', name, content, numeral, sortorder, '{$now}', '{$now}' FROM {$spec_table}"); // Update purchase table $purchase_table = ShoppDatabaseObject::tablename('purchase'); sDB::query("UPDATE {$purchase_table} SET txnid=transactionid, txnstatus=transtatus"); // Update image assets $meta_table = ShoppDatabaseObject::tablename('meta'); $asset_table = ShoppDatabaseObject::tablename('asset'); sDB::query("INSERT INTO {$meta_table} (parent, context, type, name, value, numeral, sortorder, created, modified)\n\t\t\t\t\t\t\tSELECT parent, context, 'image', 'processing', CONCAT_WS('::', id, name, value, size, properties, LENGTH(data)), '0', sortorder, created, modified FROM {$asset_table} WHERE datatype='image'"); $records = sDB::query("SELECT id, value FROM {$meta_table} WHERE type='image' AND name='processing'", 'array'); foreach ($records as $r) { list($src, $name, $value, $size, $properties, $datasize) = explode("::", $r->value); $p = unserialize($properties); $value = new StdClass(); if (isset($p['width'])) { $value->width = $p['width']; } if (isset($p['height'])) { $value->height = $p['height']; } if (isset($p['alt'])) { $value->alt = $p['alt']; } if (isset($p['title'])) { $value->title = $p['title']; } $value->filename = $name; if (isset($p['mimetype'])) { $value->mime = $p['mimetype']; } $value->size = $size; if ($datasize > 0) { $value->storage = "DBStorage"; $value->uri = $src; } else { $value->storage = "FSStorage"; $value->uri = $name; } $value = mysqli_real_escape_string(sDB::get()->dbh, serialize($value)); sDB::query("UPDATE {$meta_table} set name='original', value='{$value}' WHERE id={$r->id}"); } // Update product downloads $meta_table = ShoppDatabaseObject::tablename('meta'); $asset_table = ShoppDatabaseObject::tablename('asset'); $query = "INSERT INTO {$meta_table} (parent, context, type, name, value, numeral, sortorder, created, modified)\n\t\t\t\t\tSELECT parent, context, 'download', 'processing', CONCAT_WS('::', id, name, value, size, properties, LENGTH(data)), '0', sortorder, created, modified FROM {$asset_table} WHERE datatype='download' AND parent != 0"; sDB::query($query); $records = sDB::query("SELECT id, value FROM {$meta_table} WHERE type='download' AND name='processing'", 'array'); foreach ($records as $r) { list($src, $name, $value, $size, $properties, $datasize) = explode("::", $r->value); $p = unserialize($properties); $value = new StdClass(); $value->filename = $name; $value->mime = $p['mimetype']; $value->size = $size; if ($datasize > 0) { $value->storage = "DBStorage"; $value->uri = $src; } else { $value->storage = "FSStorage"; $value->uri = $name; } $value = mysqli_real_escape_string(sDB::get()->dbh, serialize($value)); sDB::query("UPDATE {$meta_table} set name='{$name}', value='{$value}' WHERE id={$r->id}"); } // Update promotions $promo_table = ShoppDatabaseObject::tablename('promo'); $records = sDB::query("UPDATE {$promo_table} SET target='Cart' WHERE scope='Order'", 'array'); $FSStorage = array('path' => array()); // Migrate Asset storage settings $image_storage = shopp_setting('image_storage_pref'); if ($image_storage == "fs") { $image_storage = "FSStorage"; $FSStorage['path']['image'] = shopp_setting('image_path'); } else { $image_storage = "DBStorage"; } shopp_set_setting('image_storage', $image_storage); $product_storage = shopp_setting('product_storage_pref'); if ($product_storage == "fs") { $product_storage = "FSStorage"; $FSStorage['path']['download'] = shopp_setting('products_path'); } else { $product_storage = "DBStorage"; } shopp_set_setting('product_storage', $product_storage); if (!empty($FSStorage['path'])) { shopp_set_setting('FSStorage', $FSStorage); } // Preserve payment settings // Determine active gateways $active_gateways = array(shopp_setting('payment_gateway')); $xco_gateways = (array) shopp_setting('xco_gateways'); if (!empty($xco_gateways)) { $active_gateways = array_merge($active_gateways, $xco_gateways); } // Load 1.0 payment gateway settings for active gateways $gateways = array(); foreach ($active_gateways as $reference) { list($dir, $filename) = explode('/', $reference); $gateways[] = preg_replace('/[^\\w+]/', '', substr($filename, 0, strrpos($filename, '.'))); } $where = "name like '%" . join("%' OR name like '%", $gateways) . "%'"; $query = "SELECT name, value FROM {$setting_table} WHERE {$where}"; $result = sDB::query($query, 'array'); $paycards = Lookup::paycards(); // Convert settings to 1.1-compatible settings $active_gateways = array(); foreach ($result as $_) { $active_gateways[] = $_->name; // Add gateway to the active gateways list $setting = unserialize($_->value); // Parse the settings // Get rid of legacy settings unset($setting['enabled'], $setting['path'], $setting['billing-required']); // Convert accepted payment cards $accepted = array(); if (isset($setting['cards']) && is_array($setting['cards'])) { foreach ($setting['cards'] as $cardname) { // Normalize card names $cardname = str_replace(array("Discover", "Diner’s Club", "Diners"), array("Discover Card", "Diner's Club", "Diner's Club"), $cardname); foreach ($paycards as $card) { if ($cardname == $card->name) { $accepted[] = $card->symbol; } } } $setting['cards'] = $accepted; } shopp_set_setting($_->name, $setting); // Save the gateway settings } // Save the active gateways to populate the payment settings page shopp_set_setting('active_gateways', join(', ', $active_gateways)); // Preserve update key $oldkey = shopp_setting('updatekey'); if (!empty($oldkey)) { $newkey = array($oldkey['status'] == "activated" ? 1 : 0, $oldkey['key'], $oldkey['type']); shopp_set_setting('updatekey', $newkey); } $this->roles(); // Setup Roles and Capabilities }
/** * Updates the database schema * * @author Jonathan Davis * @since 1.1 * * @return void **/ public function upschema($filename = 'schema.sql') { $path = SHOPP_PATH . '/core/schema'; $schema = "{$path}/{$filename}"; // Check for the schema definition file if (!file_exists($schema)) { $this->error('nodbschema-upgrade'); } // Test to ensure Shopp can create/drop tables $testtable = 'shopp_db_permissions_test_' . time(); $tests = array("CREATE TABLE {$testtable} ( id INT )", "DROP TABLE {$testtable}"); foreach ($tests as $testquery) { $db = sDB::get(); sDB::query($testquery); $error = mysql_error($db->dbh); if (!empty($error)) { $this->error('dbprivileges'); } } // Make sure dbDelta() is available if (!function_exists('dbDelta')) { require ABSPATH . 'wp-admin/includes/upgrade.php'; } ob_start(); include $schema; $schema = ob_get_clean(); // Update the table schema // Strip SQL comments $schema = preg_replace('/--\\s?(.*?)\\n/', "\n", $schema); $tables = preg_replace('/;\\s+/', ';', $schema); ob_start(); // Suppress dbDelta errors $changes = dbDelta($tables); ob_end_clean(); shopp_set_setting('db_updates', $changes); }