'Store', 'description' => 'Administer orders, products, customers, store settings, etc.', 'page callback' => 'uc_store_admin', 'access arguments' => array('access administration pages'), 'weight' => -12, 'file' => 'uc_store.admin.inc', ); $items['admin/store/customers'] = array( 'title' => 'Customers', 'description' => 'View and modify customer information and orders.', 'page callback' => 'uc_store_customers', 'access arguments' => array('view customers'), 'weight' => -6, 'file' => 'uc_store.admin.inc', ); $items['admin/store/customers/view'] = array( 'title' => 'View customers', 'description' => 'View and modify customer information and orders.', 'page arguments' => array(NULL, NULL, '25'), 'access arguments' => array('view customers'), 'weight' => -10, 'file' => 'uc_store.admin.inc', ); $items['admin/store/customers/search'] = array( 'title' => 'Search customers', 'description' => 'Search through your customer list.', 'page callback' => 'uc_store_customer_search', 'access arguments' => array('view customers'), 'weight' => -5, 'file' => 'uc_store.admin.inc', ); $items['admin/store/reports'] = array( 'title' => 'Reports', 'description' => 'Browse various store reports.', 'page callback' => 'uc_store_reports', 'access arguments' => array('view store reports'), 'weight' => 2, 'file' => 'uc_store.admin.inc', ); $items['admin/store/settings'] = array( 'title' => 'Configuration', 'description' => 'Adjust configuration settings for Ubercart.', 'page callback' => 'uc_store_configuration_page', 'access arguments' => array('administer store'), 'weight' => 6, 'file' => 'uc_store.admin.inc', ); $items['admin/store/help'] = array( 'title' => 'Help', 'description' => 'Links to get help!', 'page callback' => 'uc_store_ubercart_help', 'access arguments' => array('administer store'), 'weight' => 10, 'file' => 'uc_store.admin.inc', ); $items['admin/store/help/tokens'] = array( 'title' => 'Using tokens', 'description' => 'Understand what tokens are and how to use them.', 'page callback' => 'uc_store_ubercart_help_tokens', 'access arguments' => array('administer store'), 'file' => 'uc_store.admin.inc', ); $items['admin/store/settings/countries'] = array( 'title' => 'Country settings', 'description' => 'Configure countries and address formats.', 'page callback' => 'drupal_get_form', 'page arguments' => array('uc_country_import_form'), 'access arguments' => array('administer store'), 'file' => 'uc_store.admin.inc', ); $items['admin/store/settings/countries/import'] = array( 'title' => 'Countries', 'description' => 'Import and manage countries.', 'access arguments' => array('administer store'), 'weight' => -10, 'type' => MENU_DEFAULT_LOCAL_TASK, 'file' => 'uc_store.admin.inc', ); $items['admin/store/settings/countries/formats'] = array( 'title' => 'Address formats', 'description' => 'Edit country specific address format settings.', 'page callback' => 'drupal_get_form', 'page arguments' => array('uc_country_formats_form'), 'access arguments' => array('administer store'), 'weight' => -5, 'type' => MENU_LOCAL_TASK, 'file' => 'uc_store.admin.inc', ); $items['admin/store/settings/store'] = array( 'title' => 'Store settings', 'description' => 'Configure the main store settings.', 'page callback' => 'drupal_get_form', 'page arguments' => array('uc_store_settings_form'), 'access arguments' => array('administer store'), 'file' => 'uc_store.admin.inc', ); $items['admin/store/settings/store/initials'] = array( 'title' => 'User initials', 'description' => 'Assign initials to user accounts.', 'page callback' => 'drupal_get_form', 'page arguments' => array('uc_store_initials'), 'access arguments' => array('administer store'), 'file' => 'uc_store.admin.inc', ); $items['uc_js_util/%'] = array( 'title' => 'JS utilities', 'page callback' => 'uc_store_js_util', 'page arguments' => array(1), 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); $items['admin/store/customers/orders/%'] = array( 'title' => 'Customer orders', 'description' => 'View a list of orders placed by this customer.', 'page callback' => 'uc_store_customer_orders', 'page arguments' => array(4), 'access arguments' => array('view all orders'), 'weight' => -5, 'file' => 'uc_store.admin.inc', ); $items['admin/store/settings/countries/%/disable'] = array( 'title' => 'Disable a country', 'description' => 'Disable a country from use.', 'page callback' => 'uc_country_disable', 'page arguments' => array(4), 'access arguments' => array('administer store'), 'type' => MENU_CALLBACK, 'file' => 'uc_store.admin.inc', ); $items['admin/store/settings/countries/%/enable'] = array( 'title' => 'Enable a country', 'description' => 'Enable a disabled country.', 'page callback' => 'uc_country_enable', 'page arguments' => array(4), 'access arguments' => array('administer store'), 'type' => MENU_CALLBACK, 'file' => 'uc_store.admin.inc', ); $items['admin/store/settings/countries/%/remove'] = array( 'title' => 'Remove a country', 'description' => 'Remove an installed country.', 'page callback' => 'drupal_get_form', 'page arguments' => array('uc_country_remove_form', 4), 'access arguments' => array('administer store'), 'file' => 'uc_store.admin.inc', ); $items['admin/store/settings/countries/%/update/%'] = array( 'title' => 'Update a country', 'description' => 'Update an installed country.', 'page callback' => 'uc_country_update', 'page arguments' => array(4, 6), 'access arguments' => array('administer store'), 'type' => MENU_CALLBACK, 'file' => 'uc_store.admin.inc', ); return $items; } /** * Implements hook_init(). */ function uc_store_init() { module_load_include('inc', 'uc_store', 'includes/summaries'); module_load_include('inc', 'uc_store', 'includes/tapir'); drupal_add_css(drupal_get_path('module', 'uc_store') . '/uc_store.css', array('every_page' => TRUE)); global $conf; $conf['i18n_variables'][] = 'uc_store_name'; $conf['i18n_variables'][] = 'uc_field_first_name'; $conf['i18n_variables'][] = 'uc_field_last_name'; $conf['i18n_variables'][] = 'uc_field_email'; $conf['i18n_variables'][] = 'uc_field_phone'; $conf['i18n_variables'][] = 'uc_field_company'; $conf['i18n_variables'][] = 'uc_field_address'; $conf['i18n_variables'][] = 'uc_field_street'; $conf['i18n_variables'][] = 'uc_field_street1'; $conf['i18n_variables'][] = 'uc_field_street2'; $conf['i18n_variables'][] = 'uc_field_city'; $conf['i18n_variables'][] = 'uc_field_zone'; $conf['i18n_variables'][] = 'uc_field_postal_code'; $conf['i18n_variables'][] = 'uc_field_country'; } /** * Implements hook_element_info(). */ function uc_store_element_info() { $types = array(); $types['tapir_table'] = array( '#columns' => array(), '#rows' => array(), '#tree' => TRUE, '#value' => NULL, '#pre_render' => array('tapir_gather_rows'), '#theme' => 'tapir_table', '#process' => array('ajax_process_form'), ); return $types; } /** * Implements hook_theme(). */ function uc_store_theme() { return array( 'uc_store_footer' => array( 'variables' => array('message' => ''), ), 'uc_store_address_fields_form' => array( 'render element' => 'form', ), 'uc_pane_sort_table' => array( 'render element' => 'form', ), 'tapir_table' => array( 'render element' => 'element', ), 'uc_summary_overview' => array( 'variables' => array('summaries' => NULL, 'link' => TRUE), ), 'uc_price' => array( 'variables' => array('price' => 0), ), ); } /** * Implements hook_help(). */ function uc_store_help($path, $arg) { switch ($path) { case 'admin/store/customers': return t('This table lists out all users on your site who have placed orders.'); case 'admin/store/customers/search': return t('Use this page to search through users on your site who have placed orders.'); } } /** * Implements hook_permission(). */ function uc_store_permission() { return array( 'administer store' => array( 'title' => t('Administer store'), ), 'view customers' => array( 'title' => t('View customers'), ), 'view store reports' => array( 'title' => t('View store reports'), ), ); } /** * Implements hook_page_alter(). */ function uc_store_page_alter(&$page) { $id = variable_get('uc_footer_message', 0); // Exit if the store footer is turned off. if ($id === 'none') { return; } // Figure out what page is being viewed. $path = drupal_get_normal_path($_GET['q']); $parts = explode('/', $path); // Exit if the page isn't governed by Ubercart. switch ($parts[0]) { case 'admin': // No footer on /admin or /admin/*. // But add a footer on /admin/store and /admin/store/* if (!isset($parts[1]) || $parts[1] != 'store') { return; } break; case 'node': // No footer on /node or /node/[type]/add. // Only add a footer on /node/[nid] if that node is a product. if (count($parts) != 2 || intval($parts[1]) == 0) { return; } else { $node = node_load($parts[1]); if ($node == FALSE || !function_exists('uc_product_node_info') || !uc_product_is_product($node->type)) { return; } } break; case 'catalog': case 'cart': break; default: return; } $messages = _uc_store_footer_options(); if ($id == 0) { // Pseudorandom number based on the hash of the path and the site's private key, // so messages are consistent between pages on the same site, but different // on the same pages on different sites. $id = (hexdec(substr(md5($path . drupal_get_private_key()), 0, 2)) % count($messages)) + 1; } $page['page_bottom']['ubercart_footer'] = array( '#theme' => 'uc_store_footer', '#message' => $messages[$id], ); } /** * Implements hook_exit(). */ function uc_store_exit() { // Save the current request for tracking paths on subsequent page requests. // When HTTP_REFERER is set, the session version is not; and vice versa. if ($_SERVER['HTTP_REFERER'] == '') { $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https' : 'http'; $q = isset($_GET['q']) ? $_GET['q'] : ''; $_SESSION['uc_referer_uri'] = $protocol . '://' . $_SERVER['SERVER_NAME'] . $GLOBALS['base_path'] . $q; } else { if (isset($_SESSION['uc_referer_uri'])) { unset($_SESSION['uc_referer_uri']); } } // Save the timestamp of the last access. // $_SESSION['uc_last_access'] = REQUEST_TIME; } /** * Implements hook_reviews(). * * Provides code reviews for coder_review.module. */ function uc_store_reviews() { $coder_reviews = array(); $path = drupal_get_path('module', 'uc_store') . '/includes'; $files = drupal_system_listing('/coder_review_.*\.inc$/', $path, 'filepath', 0); foreach ($files as $file) { require_once DRUPAL_ROOT . '/' . $file->uri; $function = $file->name . '_reviews'; if (function_exists($function)) { if ($review = call_user_func($function)) { $coder_reviews = array_merge($coder_reviews, $review); } } } return $coder_reviews; } /****************************************************************************** * Callback Functions, Forms, and Tables ******************************************************************************/ /** * Displays a price in the standard format and with consistent markup. * * @ingroup themeable */ function theme_uc_price($variables) { return '' . uc_currency_format($variables['price']) . ''; } /** * Returns the default store footer options. */ function _uc_store_footer_options() { $url = array('!url' => 'http://www.ubercart.org/'); return array( 1 => t('Powered by Ubercart', $url), 2 => t('Drupal e-commerce provided by Ubercart.', $url), 3 => t('Supported by Ubercart, an open source e-commerce suite.', $url), 4 => t('Powered by Ubercart, the free shopping cart software.', $url), ); } /** * Wraps the footer in a div so it can be re-styled. * * @ingroup themeable */ function theme_uc_store_footer($variables) { return ''; } /** * Helper function for hook_entity_property_info() and hook_rules_data_info(). * * Should be used by implementations of those hooks that wish to wrap address * selectors. */ function uc_address_property_info() { return array( 'first_name' => array( 'type' => 'text', 'label' => t('First name'), 'description' => t('First name of the addressee.'), ), 'last_name' => array( 'type' => 'text', 'label' => t('Last name'), 'description' => t('Last name of the addressee.'), ), 'company' => array( 'type' => 'text', 'label' => t('Company'), 'description' => t('Name of the company at the address.'), ), 'street1' => array( 'type' => 'text', 'label' => t('Street line 1'), 'description' => t('First line of the street address.'), ), 'street2' => array( 'type' => 'text', 'label' => t('Street line 2'), 'description' => t('Second line of the street address.'), ), 'city' => array( 'type' => 'text', 'label' => t('City'), 'description' => t('Address city.'), ), 'zone' => array( 'type' => 'integer', 'label' => t('Zone'), 'description' => t('Address state/province/zone.'), 'options list' => 'uc_zone_option_list', ), 'postal_code' => array( 'type' => 'text', 'label' => t('Postal code'), 'description' => t('Address post code.'), ), 'country' => array( 'type' => 'integer', 'label' => t('Country'), 'description' => t('Address country.'), 'options list' => 'uc_country_option_list', ), 'phone' => array( 'type' => 'text', 'label' => t('Phone'), 'description' => t('Contact phone number.'), ), 'email' => array( 'type' => 'text', 'label' => t('Email'), 'description' => t('Contact email address.'), ), ); } /** * Form to configure address fields. * * @ingroup forms * @see uc_store_address_fields_form_submit() * @see uc_store_address_fields_form_reset() * @see theme_uc_store_address_fields_form() */ function uc_store_address_fields_form($form, &$form_state) { $form['fields'] = array( '#tree' => TRUE, '#summary callback' => 'uc_summarize_form', ); $fields = array( 'first_name' => array(t('First name'), TRUE), 'last_name' => array(t('Last name'), TRUE), 'phone' => array(t('Phone number'), TRUE), 'company' => array(t('Company'), TRUE), 'street1' => array(t('Street address 1'), TRUE), 'street2' => array(t('Street address 2'), TRUE), 'city' => array(t('City'), TRUE), 'zone' => array(t('State/Province'), TRUE), 'country' => array(t('Country'), TRUE), 'postal_code' => array(t('Postal code'), TRUE), 'address' => array(t('Address'), FALSE), 'street' => array(t('Street address'), FALSE), ); $current = variable_get('uc_address_fields', drupal_map_assoc(array('first_name', 'last_name', 'phone', 'company', 'street1', 'street2', 'city', 'zone', 'postal_code', 'country'))); $required = variable_get('uc_address_fields_required', drupal_map_assoc(array('first_name', 'last_name', 'street1', 'city', 'zone', 'postal_code', 'country'))); foreach ($fields as $field => $data) { if ($data[1]) { $form['fields'][$field]['#summary callback'] = 'uc_summarize_form'; $form['fields'][$field]['enabled'] = array( '#type' => 'checkbox', '#summary callback' => 'uc_summarize_checkbox', '#summary arguments' => array( t('@field is enabled.', array('@field' => uc_get_field_name($field))), t('@field is disabled.', array('@field' => uc_get_field_name($field))), ), '#default_value' => isset($current[$field]) ? TRUE : FALSE, ); $form['fields'][$field]['required'] = array( '#type' => 'checkbox', '#summary callback' => 'uc_summarize_null', '#default_value' => isset($required[$field]) ? TRUE : FALSE, ); } else { $form['fields'][$field]['enabled'] = array( '#markup' => '-', ); } $form['fields'][$field]['default'] = array( '#markup' => $data[0], ); $form['fields'][$field]['title'] = array( '#type' => 'textfield', '#summary callback' => 'uc_summarize_null', '#default_value' => uc_get_field_name($field), '#size' => 32, ); } $form['actions'] = array('#type' => 'actions'); $form['actions']['save'] = array( '#type' => 'submit', '#value' => t('Save configuration'), ); $form['actions']['reset'] = array( '#type' => 'submit', '#value' => t('Reset to defaults'), '#submit' => array('uc_store_address_fields_form_reset'), ); return $form; } /** * Returns HTML for uc_store_address_fields_form(). * * @see uc_store_address_fields_form() * @ingroup themeable */ function theme_uc_store_address_fields_form($variables) { $form = $variables['form']; $header = array(t('Enabled'), t('Field'), t('Title'), t('Required')); foreach (element_children($form['fields']) as $field) { $rows[] = array( array('data' => drupal_render($form['fields'][$field]['enabled']), 'align' => 'center'), drupal_render($form['fields'][$field]['default']), drupal_render($form['fields'][$field]['title']), drupal_render($form['fields'][$field]['required']), ); } $output = theme('table', array('header' => $header, 'rows' => $rows)) . '
' . drupal_render_children($form); return $output; } /** * Saves the address fields settings. * * @see uc_store_address_fields_form() */ function uc_store_address_fields_form_submit($form, &$form_state) { $fields = array(); $required = array(); foreach ($form_state['values']['fields'] as $field => $data) { variable_set('uc_field_' . $field, $data['title']); if (isset($data['enabled']) && $data['enabled']) { $fields[] = $field; } if (isset($data['required']) && $data['required']) { $required[] = $field; } } variable_set('uc_address_fields', drupal_map_assoc($fields)); variable_set('uc_address_fields_required', drupal_map_assoc($required)); drupal_set_message(t('The configuration options have been saved.')); } /** * Resets the address fields settings to their default values. * * @see uc_store_address_fields_form() */ function uc_store_address_fields_form_reset($form_id, &$form_state) { foreach ($form_state['values']['fields'] as $field => $data) { variable_del('uc_field_' . $field); } variable_del('uc_address_fields'); variable_del('uc_address_fields_required'); drupal_set_message(t('The configuration options have been reset to their default values.')); } /** * A handler for Javascript helper functions... */ function uc_store_js_util($func) { switch ($func) { case 'zone_select': $country_id = intval($_POST['country_id']) > 0 ? intval($_POST['country_id']) : uc_store_default_country(); $title = isset($_POST['title']) ? check_plain($_POST['title']) : NULL; $display = isset($_POST['display']) ? check_plain($_POST['display']) : 'name'; $select = uc_zone_select($title, NULL, NULL, $country_id, $display); $select['#parents'] = array(); $match = array('`<[/]*div[^>]*>`', '`<[/]*select[^>]*>`', '`\n|\r`'); $replace = array('', '', ''); $output = preg_replace($match, $replace, theme('select', array('element' => $select))); } print $output; exit(); } /******************************************************************************* * Module and Helper Functions ******************************************************************************/ /** * Returns the IMG tag for a store icon. * * @param $path * The Drupal path of the menu item. Atlernately may specify a filename by * passing this string as file:filename.png. * @param $small * Pass TRUE to get a link to the small version of the icon. If specifying a * filename, you should let this be FALSE. * * @return * HTML output for the image. */ function uc_store_get_icon($path, $small = FALSE, $class = 'uc-store-icon', $alt = NULL) { $file = FALSE; switch ($path) { case 'admin/store': $file = 'store_monitor'; break; case 'admin/store/orders': $file = 'menu_orders'; break; case 'admin/store/customers': $file = 'menu_customers'; break; case 'admin/store/products': $file = 'menu_products'; break; case 'admin/store/reports': $file = 'menu_reports'; break; case 'admin/store/settings': $file = 'menu_store_settings'; break; case 'admin/store/help': $file = 'menu_help'; break; } if (substr($path, 0, 5) == 'file:') { $file = substr($path, 5); } if (!$file) { // See if it's hooked in anywhere else... return ''; } if ($small) { $file .= '_small'; } $alt = ' alt="' . (string) $alt . '"'; $output = ''; return $output; } /** * Formats an amount for display with the store's currency settings. * * @param $value * The numeric value of the currency amount. * @param $sign * The currency symbol. If FALSE is given, no symbol is used. The default, * NULL, causes the variable 'uc_currency_sign' to be used, which defaults to * '$'. * @param $thou * The thousands separator character. If FALSE is given, no separator is used. * The default, NULL, causes the variable 'uc_currency_sign' to be used, which * defaults to ','. * @param $dec * The decimal separator character. If FALSE is given, confusion will abound, * because it will look 100 times bigger. The default, NULL, causes the * variable 'uc_currency_dec' to be used, which defaults to '.'. */ function uc_currency_format($value, $sign = NULL, $thou = NULL, $dec = NULL) { if ($value === NULL) { return NULL; } $output = ''; $sign_after = variable_get('uc_sign_after_amount', FALSE); $prec = variable_get('uc_currency_prec', 2); if (is_null($sign)) { $sign = variable_get('uc_currency_sign', '$'); } if (is_null($thou)) { $thou = variable_get('uc_currency_thou', ','); } if (is_null($dec)) { $dec = variable_get('uc_currency_dec', '.'); } // If the value is significantly less than the minimum precision, zero it. if ($prec > 0 && round(abs($value), $prec + 1) < pow(10, -$prec)) { $value = 0; } // Force the price to a positive value and add a negative sign if necessary. if ($value < 0) { $value = abs($value); $output .= '-'; } // Add the currency sign first if specified. if ($sign && !$sign_after) { $output .= $sign; } // Format the number, like 1234.567 => 1,234.57 $output .= number_format($value, $prec, $dec, $thou); // Add the currency sign last if specified. if ($sign && $sign_after) { $output .= $sign; } return $output; } /** * Formats a weight value for display. */ function uc_weight_format($value, $unit = NULL) { $vars = array('!value' => $value); if (is_null($unit)) { $unit = variable_get('uc_weight_unit', 'lb'); } $defaults = array( 'lb' => '!value lb.', 'oz' => '!value oz.', 'kg' => '!valuekg', 'g' => '!valueg', ); $pattern = variable_get('uc_weight_format_' . $unit, $defaults[$unit]); if (strpos($pattern, '!value') === FALSE) { $pattern = $defaults[$unit]; } $format = strtr($pattern, $vars); return $format; } /** * Gets the conversion ratio from one unit of weight to another. */ function uc_weight_conversion($from_units, $to_units = NULL) { if (is_null($to_units)) { $to_units = variable_get('uc_weight_unit', 'lb'); } $constant = strtoupper($from_units) . '_TO_' . strtoupper($to_units); if (defined($constant) && ($conversion = constant($constant)) > 0) { return $conversion; } else { return 1; } } /** * Formats a length value for display. */ function uc_length_format($value, $unit = NULL) { $vars = array('!value' => $value); if (is_null($unit)) { $unit = variable_get('uc_length_unit', 'in'); } $defaults = array( 'in' => '!valuein.', 'ft' => '!valueft.', 'cm' => '!valuecm', 'mm' => '!valuemm', ); $pattern = variable_get('uc_length_format_' . $unit, $defaults[$unit]); if (strpos($pattern, '!value') === FALSE) { $pattern = $defaults[$unit]; } $format = strtr($pattern, $vars); return $format; } /** * Gets the conversion ratio from one unit of length to another. */ function uc_length_conversion($from_units, $to_units = NULL) { if (is_null($to_units)) { $to_units = variable_get('uc_length_unit', 'in'); } $constant = strtoupper($from_units) . '_TO_' . strtoupper($to_units); if (defined($constant) && ($conversion = constant($constant)) > 0) { return $conversion; } else { return 1; } } /** * Formats a date value for display. */ function uc_date_format($month, $day, $year, $class = 'default') { $time = strtotime($month . '/' . $day . '/' . $year); $pattern = variable_get('uc_date_format_' . $class, 'm/d/Y'); if (strlen($pattern) < 3) { $pattern = 'm/d/Y'; } return format_date($time, 'custom', $pattern); } /** * Saves the address format for a country. */ function uc_set_address_format($country_id, $format) { variable_set('uc_address_format_' . intval($country_id), $format); } /** * Formats an address for display based on a country's address format. */ function uc_address_format($first_name, $last_name, $company, $street1, $street2, $city, $zone, $postal_code, $country) { $result = db_query("SELECT * FROM {uc_zones} WHERE zone_id = :id", array(':id' => $zone)); if (!($zone_data = $result->fetchAssoc())) { $zone_data = array('zone_code' => t('N/A'), 'zone_name' => t('Unknown')); } $result = db_query("SELECT * FROM {uc_countries} WHERE country_id = :id", array(':id' => $country)); if (!($country_data = $result->fetchAssoc())) { $country_data = array( 'country_name' => t('Unknown'), 'country_iso_code_2' => t('N/A'), 'country_iso_code_3' => t('N/A'), ); } $variables = array( "\r\n" => '
', '!company' => check_plain($company), '!first_name' => check_plain($first_name), '!last_name' => check_plain($last_name), '!street1' => check_plain($street1), '!street2' => check_plain($street2), '!city' => check_plain($city), '!zone_code' => $zone_data['zone_code'], '!zone_name' => $zone_data['zone_name'], '!postal_code' => check_plain($postal_code), '!country_name' => $country_data['country_name'], '!country_code2' => $country_data['country_iso_code_2'], '!country_code3' => $country_data['country_iso_code_3'], ); if (uc_store_default_country() != $country) { $variables['!country_name_if'] = $country_data['country_name']; $variables['!country_code2_if'] = $country_data['country_iso_code_2']; $variables['!country_code3_if'] = $country_data['country_iso_code_3']; } else { $variables['!country_name_if'] = ''; $variables['!country_code2_if'] = ''; $variables['!country_code3_if'] = ''; } $format = variable_get('uc_address_format_' . $country, ''); if (empty($format)) { $format = "!company\r\n!first_name !last_name\r\n!street1\r\n!street2\r\n!city, !zone_code !postal_code\r\n!country_name_if"; } $address = strtr($format, $variables); $address = strtr($address, array("\n" => '
')); $match = array('`^`', '`$`', '`(\s*|[\s*\s*]+)`', '``', '`, N/A`'); $replace = array('', '', '
', '
', '', ''); $address = preg_replace($match, $replace, $address); return $address; } /** * Returns the code abbreviation for a zone based on the zone ID or name. */ function uc_get_zone_code($zone = NULL) { if (empty($zone)) { return FALSE; } if (is_numeric($zone)) { $result = db_query("SELECT zone_code FROM {uc_zones} WHERE zone_id = :id", array(':id' => $zone)); } else { $result = db_query("SELECT zone_code FROM {uc_zones} WHERE zone_name = :name", array(':name' => $zone)); } if ($row = $result->fetchObject()) { return $row->zone_code; } return FALSE; } /** * Returns the rows of countries whose data matches the fields specified in the * $fields array. */ function uc_get_country_data($match = array(), $sort = 'country_name') { $valid_fields = array('country_id', 'country_name', 'country_iso_code_2', 'country_iso_code_3', 'version'); if (!is_array($match)) { $match = array(); } if (!in_array($sort, $valid_fields)) { $sort = 'country_name'; } $query = db_select('uc_countries') ->fields('uc_countries') ->orderBy($sort); if (count($match) > 0) { $where = ''; foreach ($match as $key => $value) { if (!in_array($key, $valid_fields)) { continue; } $query->condition($key, $value); } } $countries = $query->execute()->fetchAll(PDO::FETCH_ASSOC); return empty($countries) ? FALSE : $countries; } /** * Returns the name of an address field. */ function uc_get_field_name($field) { $fields = array( 'first_name' => t('First name'), 'last_name' => t('Last name'), 'email' => t('E-mail'), 'phone' => t('Phone number'), 'company' => t('Company'), 'address' => t('Address'), 'street' => t('Street address'), 'street1' => t('Street address 1'), 'street2' => t('Street address 2'), 'city' => t('City'), 'zone' => t('State/Province'), 'postal_code' => t('Postal code'), 'country' => t('Country'), ); $default = $fields[$field]; if (empty($default)) { drupal_set_message(t('The field title %field is being accessed incorrectly.', array('%field' => $field)), 'error'); return ''; } return variable_get('uc_field_' . $field, $default); } /** * Returns TRUE if the address field is enabled. */ function uc_address_field_enabled($field) { $fields = variable_get('uc_address_fields', drupal_map_assoc(array('first_name', 'last_name', 'phone', 'company', 'street1', 'street2', 'city', 'zone', 'postal_code', 'country'))); return isset($fields[$field]); } /** * Returns TRUE if the address field is required. */ function uc_address_field_required($field) { $fields = variable_get('uc_address_fields_required', drupal_map_assoc(array('first_name', 'last_name', 'street1', 'city', 'zone', 'postal_code', 'country'))); return isset($fields[$field]); } /** * A simple Forms API textfield generator... */ function uc_textfield($title, $default = NULL, $required = TRUE, $description = NULL, $maxlength = 32, $size = 32) { if (is_null($title) || empty($title)) return NULL; $textfield = array( '#type' => 'textfield', '#title' => $title, '#description' => $description, '#size' => $size, '#maxlength' => $maxlength, '#required' => $required, '#default_value' => $default, '#summary' => $default ? t('@title is %default.', array('@title' => $title, '%default' => $default)) : t('@title is not set.', array('@title' => $title)), ); return $textfield; } /** * Retrieves a zone's name from the database, using its ID. * * @param $id * The zone's ID. */ function uc_zone_get_by_id($id) { return db_query("SELECT zone_name FROM {uc_zones} WHERE zone_id = :id", array(':id' => $id))->fetchField(); } /** * Creates a zone select box for a form. * * @param $display * Can be 'code' or 'name'. */ function uc_zone_select($title, $default = NULL, $description = NULL, $country_id = NULL, $display = 'name', $required = FALSE) { if (empty($country_id)) { $country_id = uc_store_default_country(); } $order_by = ($display == 'code') ? 'zone_code' : 'zone_name'; $zones = db_query("SELECT * FROM {uc_zones} WHERE zone_country_id = :id ORDER BY $order_by", array(':id' => $country_id)); $options[''] = t('Please select'); foreach ($zones as $zone) { $options[$zone->zone_id] = ($display == 'code') ? $zone->zone_code : $zone->zone_name; } if (count($options) == 1) { $options = array(-1 => t('Not applicable')); } $select = array( '#type' => 'select', '#title' => $title, '#description' => $description, '#options' => $options, '#default_value' => $default, '#required' => $required, '#disabled' => isset($options[-1]) ? TRUE : FALSE, '#suffix' => '', ); return $select; } /** * Helper function to return zone options, grouped by country. */ function uc_zone_option_list() { $result = db_query("SELECT z.*, c.country_name FROM {uc_zones} AS z LEFT JOIN {uc_countries} AS c ON z.zone_country_id = c.country_id ORDER BY c.country_name, z.zone_name"); foreach ($result as $zone) { $options[$zone->country_name][$zone->zone_id] = $zone->zone_name; } return $options; } /** * Retrieves a country's name from the database, using its ID. * * @param $id * The country's ISO 3166-1 numeric identifier. */ function uc_country_get_by_id($id) { return db_query("SELECT country_name FROM {uc_countries} WHERE country_id = :id", array(':id' => $id))->fetchField(); } /** * Creates a country select box for a form. * * @param $display * Can be 'name', 'code2' for the 2-digit code, or 'code3' for the 3-digit code. */ function uc_country_select($title, $default = NULL, $description = NULL, $display = 'name', $required = FALSE) { if ($display == 'code2') { $order_by = 'country_iso_code_2'; } elseif ($display == 'code3') { $order_by = 'country_iso_code_3'; } else { $order_by = 'country_name'; } $result = db_query("SELECT * FROM {uc_countries} WHERE version > :version ORDER BY $order_by", array(':version' => 0)); $options = array(); while ($country = $result->fetchAssoc()) { $options[$country['country_id']] = $country[$order_by]; } if (count($options) == 0) { $options[] = t('No countries found.'); } $default = db_query("SELECT country_id FROM {uc_countries} WHERE country_id = :id AND version > :version", array(':id' => empty($default) ? 0 : intval($default), ':version' => 0))->fetchField(); $select = array( '#type' => 'select', '#title' => $title, '#description' => $description, '#options' => $options, '#default_value' => empty($default) ? uc_store_default_country() : $default, '#required' => $required, ); drupal_add_js(drupal_get_path('module', 'uc_store') . '/uc_country_select.js'); return $select; } /** * Returns a list of available countries. */ function uc_country_option_list() { $result = db_query("SELECT * FROM {uc_countries} WHERE version > :version ORDER BY country_name", array(':version' => 0)); $options = array(); while ($country = $result->fetchAssoc()) { $options[$country['country_id']] = $country['country_name']; } if (count($options) == 0) { $options[] = t('No countries found.'); } return $options; } /** * Creates a day select box for a form. */ function uc_select_day($title = NULL, $default = NULL, $allow_empty = FALSE) { $options = $allow_empty ? array('' => '') : array(); $select = array( '#type' => 'select', '#title' => (is_null($title) ? t('Day') : $title), '#options' => $options + drupal_map_assoc(range(1, 31)), '#default_value' => (is_null($default) ? 0 : $default), ); return $select; } /** * Creates a month select box for a form. */ function uc_select_month($title = NULL, $default = NULL, $allow_empty = FALSE) { $options = $allow_empty ? array('' => '') : array(); $select = array( '#type' => 'select', '#title' => (is_null($title) ? t('Month') : $title), '#options' => $options + array(1 => t('01 - January'), 2 => t('02 - February'), 3 => t('03 - March'), 4 => t('04 - April'), 5 => t('05 - May'), 6 => t('06 - June'), 7 => t('07 - July'), 8 => t('08 - August'), 9 => t('09 - September'), 10 => t('10 - October'), 11 => t('11 - November'), 12 => t('12 - December') ), '#default_value' => (is_null($default) ? 0 : $default), ); return $select; } /** * Creates a year select box for a form. */ function uc_select_year($title = NULL, $default = NULL, $min = NULL, $max = NULL, $allow_empty = FALSE) { $min = is_null($min) ? intval(date('Y')) : $min; $max = is_null($max) ? intval(date('Y')) + 20 : $max; $options = $allow_empty ? array('' => '') : array(); $select = array( '#type' => 'select', '#title' => (is_null($title) ? t('Year') : $title), '#options' => $options + drupal_map_assoc(range($min, $max)), '#default_value' => (is_null($default) ? 0 : $default), ); return $select; } /** * Creates an address select box based on a user's previous orders. * * @param $uid * The user's ID to search for in the orders table. * @param $type * Choose either 'shipping' or 'billing'. */ function uc_select_address($uid, $type = 'billing', $onchange = '', $title = NULL, $icon_suffix = FALSE) { $addresses = uc_get_addresses($uid, $type); if (!is_array($addresses) || count($addresses) == 0) { return NULL; } $options = array('0' => t('Select one...')); foreach ($addresses as $key => $address) { $option = $address['street1']; // Check if the address is a duplicate (i.e. same address, but sent to different person) if ((isset($addresses[$key - 1]) && $option == $addresses[$key - 1]['street1']) || (isset($addresses[$key + 1]) && $option == $addresses[$key + 1]['street1'])) { $option .= ' - ' . $address['first_name'] . ' ' . $address['last_name']; } $options[drupal_json_encode($address)] = check_plain($option); } $select = array( '#type' => 'select', '#title' => is_null($title) ? t('Address book') : $title, '#options' => $options, '#attributes' => array('onchange' => $onchange), '#suffix' => $icon_suffix ? uc_store_get_icon('file:address_book', FALSE, 'address-book-icon') : NULL, ); return $select; } /** * Loads a customer's previously given addresses. */ function uc_get_addresses($uid, $type = 'billing') { if ($uid == 0) { return NULL; } if ($type == 'delivery') { $type = 'delivery'; } else { $type = 'billing'; } $query = db_select('uc_orders', 'o')->distinct(); $alias = array(); $alias['first_name'] = $query->addField('o', $type . '_first_name', 'first_name'); $alias['last_name'] = $query->addField('o', $type . '_last_name', 'last_name'); $alias['phone'] = $query->addField('o', $type . '_phone', 'phone'); $alias['company'] = $query->addField('o', $type . '_company', 'company'); $alias['street1'] = $query->addField('o', $type . '_street1', 'street1'); $alias['street2'] = $query->addField('o', $type . '_street2', 'street2'); $alias['city'] = $query->addField('o', $type . '_city', 'city'); $alias['zone'] = $query->addField('o', $type . '_zone', 'zone'); $alias['postal_code'] = $query->addField('o', $type . '_postal_code', 'postal_code'); $alias['country'] = $query->addField('o', $type . '_country', 'country'); // In pgsql, ORDER BY requires the field being sorted by to be in the SELECT // list. But if we have the 'created' column in the SELECT list, the DISTINCT // is rather useless. So we will just sort addresses alphabetically. $query->condition('uid', $uid) ->condition('order_status', uc_order_status_list('general', TRUE), 'IN') ->orderBy($alias['street1']); $result = $query->execute(); $addresses = array(); while ($address = $result->fetchAssoc()) { if (!empty($address['street1']) || !empty($address['postal_code'])) { $addresses[] = $address; } } return $addresses; } /** * Strips
tags and form_token and form_id hidden fields from form HTML for * use in an AJAX populated div. (Enables these values to be access via $_POST.) */ function uc_strip_form($form) { unset($form['#type']); unset($form['form_id']); unset($form['form_token']); return $form; } /** * Returns the initials for a user account. * * @param $uid * The Drupal user id number. */ function uc_get_initials($uid) { if ($uid == 0 || $uid == NULL) { return '-'; } return check_plain(variable_get('user_initials_' . $uid, $uid)); } /** * Returns an array of country files in ubercart/uc_store/countries that can * be installed or updated. */ function _uc_country_import_list() { $dir = drupal_get_path('module', 'uc_store') . '/countries/'; $countries = array(); if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== FALSE) { switch (filetype($dir . $file)) { case 'file': if (substr($file, -4, 4) == '.cif') { $pieces = explode('_', substr($file, 0, strlen($file) - 4)); $country_id = intval($pieces[count($pieces) - 2]); $version = $pieces[count($pieces) - 1]; if (!isset($countries[$country_id])) { $countries[$country_id]['version'] = $version; $countries[$country_id]['file'] = $file; } else { if ($version > $countries[$country_id]['version']) { $countries[$country_id]['version'] = $version; $countries[$country_id]['file'] = $file; } } } break; } } closedir($dh); } } return $countries; } /** * Imports an Ubercart country file by filename. * * @param $file * The filename of the country to import. * * @return * TRUE or FALSE indicating whether or not the country was imported. */ function uc_country_import($file) { require_once(drupal_get_path('module', 'uc_store') . '/countries/' . $file); $pieces = explode('_', substr($file, 0, strlen($file) - 4)); $country_id = $pieces[count($pieces) - 2]; $version = $pieces[count($pieces) - 1]; $country = substr($file, 0, strlen($file) - strlen($country_id) - strlen($version) - 6); $func = $country . '_install'; if (function_exists($func)) { $func(); return TRUE; } return FALSE; } /** * Includes the appropriate country file and return the base for hooks. */ function _uc_country_import_include($country_id, $version) { $dir = drupal_get_path('module', 'uc_store') . '/countries/'; $match = '_' . $country_id . '_' . $version . '.cif'; $matchlen = strlen($match); $countries = array(); if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== FALSE) { switch (filetype($dir . $file)) { case 'file': if (substr($file, -$matchlen) == $match) { require_once($dir . $file); return substr($file, 0, strlen($file) - $matchlen); } break; } } closedir($dh); } } return FALSE; } /** * Sorts an array of arrays having a weight key to determine their order. */ function uc_weight_sort($a, $b) { if ($a['weight'] == $b['weight']) { return 0; } return ($a['weight'] > $b['weight']) ? 1 : -1; } /** * Returns the default message for a configurable message. */ function uc_get_message($message_id) { static $messages; if (empty($messages)) { $messages = module_invoke_all('uc_message'); drupal_alter('uc_get_message', $messages); } return $messages[$message_id]; } /** * Themes a pane sorting form into a table. * * @ingroup themeable */ function theme_uc_pane_sort_table($variables) { $form = $variables['form']; $prefix = $form['#pane_prefix']; if (isset($form['#table_attributes']) && is_array($form['#table_attributes'])) { $attributes = $form['#table_attributes']; } else { $attributes = array(); } $header = array(t('Enabled'), t('Title'), t('List position')); foreach (element_children($form) as $pane_id) { $rows[] = array( array( 'data' => drupal_render($form[$pane_id][$prefix . '_' . $pane_id . '_enabled']), 'align' => 'center' ), drupal_render($form[$pane_id]['title']), drupal_render($form[$pane_id][$prefix . '_' . $pane_id . '_weight']), ); } return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes)) . '
'; } /** * Returns the user-defined store address. */ function uc_store_address() { $store_address = uc_address_format(NULL, NULL, variable_get('uc_store_name', NULL), variable_get('uc_store_street1', NULL), variable_get('uc_store_street2', NULL), variable_get('uc_store_city', NULL), variable_get('uc_store_zone', NULL), variable_get('uc_store_postal_code', NULL), uc_store_default_country()); return $store_address; } /** * Returns store name and e-mail address in an RFC 2822 compliant string * for use as a "From" address when sending e-mail to customers. * The return string will look something like: Store Name * * @return An RFC 2822 compliant e-mail addres */ function uc_store_email_from() { // Use store e-mail address if set, else use site e-mail address $email_from = variable_get('uc_store_email', ''); if (empty($email_from)) { $email_from = variable_get('site_mail', ini_get('sendmail_from')); } // Add the store name to the e-mail "From" line. // Must be optional to prevent server conflicts. if (variable_get('uc_store_email_include_name', TRUE)) { $store = variable_get('uc_store_name', ''); if (!empty($store)) { // Handle non-ASCII characters in store name and wrap in double quotes // if it contains RFC 2822 'special' characters $email_from = uc_store_rfc2822_display_name($store) . ' <' . $email_from . '>'; } } return $email_from; } /** * Turns a text string into a valid RFC 2822 quoted string. * * Any text string not consisting of a limited set of valid characters * (notable printable non-valid characters include ',' and '.') needs * to be quoted in order to be used an an e-mail header such as the "From" * address. Double quotes in the original string are escaped (and nothing else). * * @param $name * The text string to convert to a RFC 2822 quoted string. */ function uc_store_rfc2822_display_name($name) { // Base64 encode $name string if it contains non-ASCII characters $name = mime_header_encode($name); // From RFC2822, section 3.4.2, define valid characters for an atom $valid_chars = "[a-zA-Z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]"; // Display name is composed of 0 or more atoms separated by white space if (!preg_match("/^(${valid_chars}*[ \t]*)*$/", $name)) { return '"' . addcslashes($name, '"') . '"'; } return $name; } /** * Derives a valid username from an e-mail address. * * @param $email * An e-mail address. * * @return * A username derived from the e-mail address, using the part of the address * up to the @ with integers appended to the end if needed to avoid a * duplicate username. */ function uc_store_email_to_username($email) { // Default to the first part of the e-mail address. $name = substr($email, 0, strpos($email, '@')); // Remove possible illegal characters. $name = preg_replace('/[^A-Za-z0-9_.-]/', '', $name); // Trim that value for spaces and length. $name = trim(substr($name, 0, USERNAME_MAX_LENGTH - 4)); // Make sure we don't hand out a duplicate username. while (db_query("SELECT COUNT(uid) FROM {users} WHERE name LIKE :name", array(':name' => $name))->fetchField() > 0) { // If the username got too long, trim it back down. if (strlen($name) == USERNAME_MAX_LENGTH) { $name = substr($name, 0, USERNAME_MAX_LENGTH - 4); } // Append a random integer to the name. $name .= rand(0, 9); } return $name; } /** * Handles credit-card encryption. * * Trimmed down version of GPL class by Tony Marston. Details available at * http://www.tonymarston.co.uk/php-mysql/encryption.html * * Usage: * 1) Create an encryption object. * ex: $crypt = new uc_encryption_class; * 2) To encrypt string data, use the encrypt method with the key. * ex: $encrypted = $crypt->encrypt($key, $string); * 3) To decrypt string data, use the decrypt method with the original key. * ex: $decrypted = $crypt->decrypt($key, $string); * 4) To check for errors, use the errors method to return an array of errors. * ex: $errors = $crypt->errors(); */ class uc_encryption_class { var $scramble1; var $scramble2; var $errors; var $adj; var $mod; function uc_encryption_class() { $this->errors = array(); $this->scramble1 = '! #$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`"abcdefghijklmnopqrstuvwxyz{|}~'; $this->scramble2 = 'f^jAE]okIOzU[2&q1{3`h5w_794p@6s8?BgP>dFV=m" Dadj = 1.75; $this->mod = 3; } function decrypt($key, $source) { $this->errors = array(); $fudgefactor = $this->_convertKey($key); if ($this->errors) { return; } if (empty($source)) { // Commented out to prevent errors getting logged for use cases that may // have variable encryption/decryption requirements. -RS // $this->errors[] = t('No value has been supplied for decryption'); return; } $target = NULL; $factor2 = 0; for ($i = 0; $i < strlen($source); $i++) { $char2 = substr($source, $i, 1); $num2 = strpos($this->scramble2, $char2); if ($num2 === FALSE) { $this->errors[] = t('Source string contains an invalid character (!char)', array('!char' => $char2)); return; } $adj = $this->_applyFudgeFactor($fudgefactor); $factor1 = $factor2 + $adj; $num1 = $num2 - round($factor1); $num1 = $this->_checkRange($num1); $factor2 = $factor1 + $num2; $char1 = substr($this->scramble1, $num1, 1); $target .= $char1; } return rtrim($target); } function encrypt($key, $source, $sourcelen = 0) { $this->errors = array(); $fudgefactor = $this->_convertKey($key); if ($this->errors) { return; } if (empty($source)) { // Commented out to prevent errors getting logged for use cases that may // have variable encryption/decryption requirements. -RS // $this->errors[] = t('No value has been supplied for encryption'); return; } while (strlen($source) < $sourcelen) { $source .= ' '; } $target = NULL; $factor2 = 0; for ($i = 0; $i < strlen($source); $i++) { $char1 = substr($source, $i, 1); $num1 = strpos($this->scramble1, $char1); if ($num1 === FALSE) { $this->errors[] = t('Source string contains an invalid character (!char)', array('!char' => $char1)); return; } $adj = $this->_applyFudgeFactor($fudgefactor); $factor1 = $factor2 + $adj; $num2 = round($factor1) + $num1; $num2 = $this->_checkRange($num2); $factor2 = $factor1 + $num2; $char2 = substr($this->scramble2, $num2, 1); $target .= $char2; } return $target; } function getAdjustment() { return $this->adj; } function getModulus() { return $this->mod; } function setAdjustment($adj) { $this->adj = (float) $adj; } function setModulus($mod) { $this->mod = (int)abs($mod); } function _applyFudgeFactor(&$fudgefactor) { static $alerted = FALSE; if (!is_array($fudgefactor)) { if (!$alerted) { // Throw an error that makes sense so this stops getting reported. $this->errors[] = t('No encryption key was found.'); drupal_set_message(t('Ubercart cannot find a necessary encryption key. Refer to the store admin dashboard to isolate which one.', array('!url' => url('admin/store'))), 'error'); $alerted = TRUE; } } else { $fudge = array_shift($fudgefactor); } $fudge = $fudge + $this->adj; $fudgefactor[] = $fudge; if (!empty($this->mod)) { if ($fudge % $this->mod == 0) { $fudge = $fudge * -1; } } return $fudge; } function _checkRange($num) { $num = round($num); $limit = strlen($this->scramble1); while ($num >= $limit) { $num = $num - $limit; } while ($num < 0) { $num = $num + $limit; } return $num; } function _convertKey($key) { if (empty($key)) { // Commented out to prevent errors getting logged for use cases that may // have variable encryption/decryption requirements. -RS // $this->errors[] = 'No value has been supplied for the encryption key'; return; } $array[] = strlen($key); $tot = 0; for ($i = 0; $i < strlen($key); $i++) { $char = substr($key, $i, 1); $num = strpos($this->scramble1, $char); if ($num === FALSE) { $this->errors[] = "Key contains an invalid character ($char)"; return; } $array[] = $num; $tot = $tot + $num; } $array[] = $tot; return $array; } } /** * Logs encryption errors to watchdog. * * @param $crypt * The object used to perform your encryption/decryption. * @param $module * The module name to specify in the watchdog notices. */ function uc_store_encryption_errors(&$crypt, $module) { if (!empty($crypt->errors)) { foreach ($crypt->errors as $message) { $items[] = $message; } watchdog('encryption', 'Encryption failed. !messages', array('!messages' => theme('item_list', array('items' => $items))), WATCHDOG_ERROR); } } /** * Returns a default store country value. */ function uc_store_default_country() { static $default; if (!empty($default)) { return $default; } $default = variable_get('uc_store_country', 840); $result = db_query("SELECT COUNT(*) FROM {uc_countries} WHERE country_id = :id AND version > :version", array(':id' => $default, ':version' => 0))->fetchField(); if ($result == 0) { $default = db_query_range("SELECT country_id FROM {uc_countries} WHERE version > :version ORDER BY country_name", 0, 1, array(':version' => 0))->fetchField(); } return $default; } /** * Wrapper for drupal_add_js() to cache .js files based on their timestamp. * * Deprecated! Use drupal_add_js() instead. */ // @todo PORT_7 make rule for coder module to report this and remove this function /* function uc_add_js($data = NULL, $type = 'file', $scope = 'header', $defer = FALSE, $cache = TRUE) { watchdog('uc_store', 'uc_add_js() has been deprecated. Use drupal_add_js() in your code.', array(), WATCHDOG_ERROR); drupal_add_js($data, array('type' => $type, 'scope' => $scope, 'defer' => $defer, 'cache'=>$cache)); } */ /** * Checks referers to see if they are in the allowed list. */ function uc_referer_check($urls) { $http_referer = uc_referer_uri(); // Always return true if we have no referer; covers the case of page refreshes // and switching from HTTP to HTTPS. This bypasses the two-time check below... // is it safe? if (empty($http_referer)) { return TRUE; } // Check the user didn't shamelessly two-time us with another site. $referer = parse_url($http_referer); if ($referer['host'] != $_SERVER['SERVER_NAME']) { return FALSE; } // The check itself. foreach ((array) $urls as $url) { if (substr($http_referer, -strlen($url)) == $url) { return TRUE; } } return FALSE; } /** * Provides a more reliable referrer for Ubercart. */ function uc_referer_uri() { if ($_SERVER['HTTP_REFERER'] == '') { return isset($_SESSION['uc_referer_uri']) ? $_SESSION['uc_referer_uri'] : ''; } else { return $_SERVER['HTTP_REFERER']; } } /** * Gets image widgets defined by various modules. */ function uc_store_get_image_widgets() { return module_invoke_all('uc_image_widget'); } /** * Implements hook_uc_image_widget(). * * Built-in support for Colorbox, Thickbox and Lightbox2. */ function uc_store_uc_image_widget() { $widgets = array(); if (module_exists('colorbox')) { $widgets['colorbox'] = array( 'name' => t('Colorbox'), 'callback' => 'uc_store_image_widget_colorbox', ); } if (module_exists('thickbox')) { $widgets['thickbox'] = array( 'name' => t('Thickbox'), 'callback' => 'uc_store_image_widget_thickbox', ); } if (module_exists('lightbox2')) { $widgets['lightbox2'] = array( 'name' => t('Lightbox2'), 'callback' => 'uc_store_image_widget_lightbox2', ); } return $widgets; } /** * Generates the Colorbox-specific HTML attributes. */ function uc_store_image_widget_colorbox($rel_count) { if (!is_null($rel_count)) { $img_index = 'uc_image_'. $rel_count; } else { $img_index = 'uc_image'; } return ' class="colorbox" rel="'. $img_index .'"'; } /** * Generates the Thickbox-specific HTML attributes. */ function uc_store_image_widget_thickbox($rel_count) { if (!is_null($rel_count)) { $img_index = 'uc_image_' . $rel_count; } else { $img_index = 'uc_image'; } return ' class="thickbox" rel="' . $img_index . '"'; } /** * Generates the Lightbox2-specific HTML attributes. */ function uc_store_image_widget_lightbox2($rel_count) { if (!is_null($rel_count)) { $img_index = 'lightbox[' . $rel_count . ']'; } else { $img_index = 'lightbox'; } return ' rel="' . $img_index . '"'; } /** * Gets the preferred language for a user's email address. * * @param $address * The email address to check. * * @return * The language object to be used in translation, localization, etc. If a * user account can not be found for $address, language_default() is * returned. * * @see user_preferred_language() * @see language_default() */ function uc_store_mail_recipient_language($address) { // See if any user exists for this address. $account = user_load_by_mail(trim($address)); if ($account) { $lang_object = user_preferred_language($account); } // If not, site-wide default. else { $lang_object = language_default(); } return $lang_object; } /** * Displays prices in forms with a minimum number of decimal places. * * @param $price * The price to display as the #default_value in a form field. */ function uc_store_format_price_field_value($price) { $exact = rtrim(number_format($price, 6, '.', ''), '0'); $round = number_format($price, variable_get('uc_currency_prec', 2), '.', ''); if ($exact == rtrim($round, '0')) { return $round; } else { return $exact; } } /** * Executes hook_uc_form_alter() implementations. * * API function to invoke hook_uc_form_alter() implementations allowing those * modules to alter the form before the Drupal layer hook_form_alter() is * invoked. * * @see hook_uc_form_alter(). */ function uc_form_alter(&$form, &$form_state, $form_id) { drupal_alter('uc_form', $form, $form_state, $form_id); }