<?php

/**
 * Display the shopping cart and add products to cart.
 * Output with <txp:yab_shop_cart output="cart"/>
 *
 * @param array $atts
 * @return string
 */
function yab_shop_cart($atts)
{
  extract(lAtts
    (array(
      'output'  => 'cart'
    ),$atts)
  );

  global $thisarticle;

  $articleid = $thisarticle['thisid'];
  $section = $thisarticle['section'];

  yab_shop_start_session();

  $cart =& $_SESSION['wfcart'];
  if (!is_object($cart))
  {
    $cart = new wfCart();
  }

  // get the product info from database
  if ($section == yab_shop_config('shop_section_name'))
  {
    $products = array();
    $custom_field_price = yab_shop_config('custom_field_price');
    $products[$articleid] = safe_row("ID as id, Title as name, $custom_field_price as price", "textpattern", "ID = $articleid");
  }

  // add a product to cart
  if (ps('add') != '')
  {
    $pqty = preg_replace("/[^0-9]/", '', ps('qty'));
    $product = $products[$articleid];
    $product_price = yab_shop_replace_commas($product['price']);
    $product_property_1 = ps(strtolower(yab_shop_config('custom_field_property_1_name')));
    $product_property_2 = ps(strtolower(yab_shop_config('custom_field_property_2_name')));
    $product_property_3 = ps(strtolower(yab_shop_config('custom_field_property_3_name')));
    $product_id = $product['id'].'-'.yab_shop_check_item($cart, $product['id'], $product_property_1, $product_property_2, $product_property_3);

    $cart->add_item($product_id, $pqty, $product_price, $product['name'], $product_property_1, $product_property_2, $product_property_3);
  }

  $message = tag(yab_shop_lang('message_added'), 'span', ' id="yab-shop-added-message"');

  // test for given tag attribute; output message, cart or nothing
  switch ($output)
  {
    case 'none':
      return '';
      break;
    case 'cart':
      return yab_shop_build_cart($cart);
      break;
    case 'message':
      if (ps('add') != '')
      {
        return $message;
      }
      else
      {
        return '';
      }
      break;
    default:
      break;
  }
}

/**
 * Display the checkout table and the checkout form.
 * Edit the quantity of items and proceed the order with a mail.
 * Output with <txp:yab_shop_checkout />
 *
 * @return string
 */
function yab_shop_checkout()
{
  yab_shop_start_session();

  $cart =& $_SESSION['wfcart'];
  if (!is_object($cart))
  {
    $cart = new wfCart();
  }

  // edit the qty of products
  if (ps('edit') != '')
  {
    $qty = preg_replace("/[^0-9]/", '', ps('editqty'));
    $id = preg_replace("/[^{\\-}0-9]/", '', ps('editid'));

    if ($qty != '' and $id != '')
    {
      $cart->edit_item($id, $qty);
    }
    else
    {
    $cart->del_item($id);
    }
  }

  // delete a product from cart
  if (ps('del') != '')
  {
    $id = preg_replace("/[^{\\-}0-9]/", '', ps('editid')); // if someone will bad, strip strictly
    $cart->del_item($id);
  }

  // if something in cart show table an form, check POSTs and send order mail or not
  if ($cart->itemcount > 0)
  {
    $affirmation = yab_shop_config('order_affirmation_mail');
    $to_shop = graf(tag(yab_shop_lang('checkout_history_back'), 'a', ' href="'.pagelinkurl(array('s' => yab_shop_config('shop_section_name'))).'"'), ' class="history-back"');
    $checkout_display = yab_shop_build_checkout_table($cart);
    $checkout_message = graf(yab_shop_lang('checkout_required_field_notice'), ' class="yab-shop-notice"');
    $checkout_form = yab_shop_build_checkout_form();

    // if someone POSTs the form, check the fields and mail
    if (ps('order') != '')
    {
      $ps_order = array();
      $ps_order = yab_shop_clean_input($_POST);
      $checkout_message = graf(yab_shop_lang('checkout_mail_field_error'), ' class="yab-shop-required-notice"');

      // this one returns an empty string if required fields correctly filled
      $notice = yab_shop_check_required_fields($ps_order, $affirmation);

      // if required fields not filled or email is invalid
      if ($notice != '')
      {
        $checkout_message .= tag($notice, 'ul', ' class="yab-shop-notice"');
        $checkout_form = yab_shop_build_checkout_form();
      }
      // is checkout form correctly filled so try to send order mail
      else
      {
        $checkout_display = '';
        $checkout_form = '';

        // check is customer affirmation mail is set or not and send order and/or affirmation mail
        if ($affirmation != '1')
        {
          // check for paypal payment method
          if (ps('payment') != yab_shop_lang('checkout_payment_paypal'))
          {
            $checkout_message = graf(yab_shop_lang('checkout_mail_error'), ' class="yab-shop-message"');
            if (yab_shop_shop_mail(yab_shop_config('admin_mail'), yab_shop_mail_lang('admin_mail_subject'), yab_shop_build_mail_body($cart, $ps_order)))
            {
            $cart->empty_cart();
            $checkout_message = graf(yab_shop_lang('checkout_mail_success').$to_shop, ' class="yab-shop-message"');
            }
          }
          else
          {
            $checkout_display = yab_shop_build_checkout_table($cart, $no_change = '1');
            $checkout_message = '';
            if (yab_shop_paypal_config('use_encrypted_paypal_button') != '1')
            {
              $checkout_form = yab_shop_build_paypal_form($cart);
            }
            else
            {
              $checkout_form = yab_shop_build_paypal_encrypted_form($cart);
            }
            $cart->empty_cart();
          }
        }
        else
        {
          // check for paypal payment method
          if (ps('payment') != yab_shop_lang('checkout_payment_paypal'))
          {
            $checkout_message = graf(yab_shop_lang('checkout_mail_error'), ' class="yab-shop-message"');
            if (yab_shop_shop_mail(yab_shop_config('admin_mail'), yab_shop_mail_lang('admin_mail_subject'), yab_shop_build_mail_body($cart, $ps_order)))
            {
              $checkout_message = graf(yab_shop_lang('checkout_mail_affirmation_error'), ' class="yab-shop-message"');
              if (yab_shop_shop_mail($ps_order['email'], yab_shop_mail_lang('affirmation_mail_subject'), yab_shop_build_mail_body($cart, $ps_order, '1')))
              {
                $checkout_message = graf(yab_shop_lang('checkout_mail_affirmation_success').$to_shop, ' class="yab-shop-message"');
              }
              $cart->empty_cart();
            }
          }
          else
          {
            $checkout_display = yab_shop_build_checkout_table($cart, $no_change = '1');
            $checkout_message = '';
            if (yab_shop_paypal_config('use_encrypted_paypal_button') != '1')
            {
              $checkout_form = yab_shop_build_paypal_form($cart);
            }
            else
            {
              $checkout_form = yab_shop_build_paypal_encrypted_form($cart);
            }
            $cart->empty_cart();
          }
        }
      }
    }
    // no order POST reveived, so display table and form
    return $checkout_display.$checkout_message.$checkout_form;
  }
  // output if nothing in cart
  else
  {
    $checkout_display = graf(yab_shop_lang('empty_cart'), ' class="yab-empty"');

    // message when returned form paypal to shop
    if (gps('merchant_return_link') != '')
    {
      $checkout_display = graf(yab_shop_lang('paypal_return_message'), ' class="yab-shop-message"').graf(tag(yab_shop_lang('checkout_history_back'), 'a', ' href="'.pagelinkurl(array('s' => yab_shop_config('shop_section_name'))).'"'), ' class="history-back"');
    }

    return $checkout_display;
  }
}

/**
 * Display the add form in article/product views with assigned
 * properties as a select tag and qty as text input.
 * Output with <txp:yab_shop_add />
 *
 * @return string
 */
function yab_shop_add()
{
  global $thisarticle;

  $url_title = $thisarticle['url_title'];
  $section = $thisarticle['section'];  
  $posted = $thisarticle['posted'];
  $id = $thisarticle['thisid'];
  $property_1_name = yab_shop_config('custom_field_property_1_name');
  $property_2_name = yab_shop_config('custom_field_property_2_name');
  $property_3_name = yab_shop_config('custom_field_property_3_name');

  $add_form = tag(
    yab_shop_build_custom_select_tag($property_1_name, yab_shop_lang('custom_field_property_1')).
    yab_shop_build_custom_select_tag($property_2_name, yab_shop_lang('custom_field_property_2')).
    yab_shop_build_custom_select_tag($property_3_name, yab_shop_lang('custom_field_property_3')).
    graf(
      fInput('text','qty','1','','','','1').
      fInput('submit','add',yab_shop_lang('add_to_cart'),'submit'),
      ' class="yab-add"'
    ),
  'form', ' method="post" action="'.permlinkurl(array(
    'title'     => '',
    'url_title' => $url_title,
    'section'   => $section,
    'posted'    => $posted,
    'thisid'    => $id
  )).'"');

  return $add_form;
}

/**
 * Shows an additional message if a product is added to cart.
 * Output f.i. with <txp:yab_shop_add_message message="your message here" />
 * 
 * @param array $atts Custom message
 * @return string
 */
function yab_shop_add_message($atts)
{
  extract(lAtts
    (array(
      'message'  => yab_shop_lang('message_added')
    ),$atts)
  );

  $out = '';
  if (ps('add') != '')
  {
    $out = tag($message, 'span', ' id="yab-shop-add-message"');
  }
  return $out;
}

/**
 * It's a modified custom_field function from txp.
 * It displays the custom field price and let convert the currency formatting
 * or makes an array from custom fields.
 * Output f.i. with <txp:yab_shop_custom_field name="price" />
 * 
 * @param array $atts Used custom field
 * @return string As currency or select tag
 */
function yab_shop_custom_field($atts)
{
  global $thisarticle, $prefs;
  assert_article();

  extract(lAtts
    (array(
      'name'      => @$prefs['custom_1_set'],
      'default'   => '',
    ),$atts)
  );

  $currency = yab_shop_config('currency');
  $name = strtolower($name);
  $custom_field_price_name = strtolower(yab_shop_config('custom_field_price_name'));

  if (!empty($thisarticle[$name]))
  {

    // if given $name the name from the price custom field, then do currency formatting
    if ($name == $custom_field_price_name)
    {
      $out = $thisarticle[$name];
      $out = yab_shop_replace_commas($out);
      $out = yab_shop_currency_out($currency, 'cur').yab_shop_currency_out($currency, 'toform', $out);
    }
    // if another custom field, make an array and a select tag
    else
    {
      $out = $thisarticle[$name];
      $out = explode(';', $out);
      $out = yab_shop_type_select_custom($out, $name);
    }
  }
  else
  {
    $out = $default;
  }
  return $out;
}

/*
* yab_shop helper
*/

/**
 * Check required form fields for input.
 * 
 * @param array $ps_order Sanitized POSTed data
 * @param strip $affirmation From config
 * @return string For output
 */
function yab_shop_check_required_fields($ps_order, $affirmation)
{
  $notice = '';

  // dirty hack, 'cause empty checkboxes are not POSTed
  if (!isset($_POST['tou']))
  {
    $ps_order['tou|r'] = '';
  }

  // loop through POSTed and check for required "|r"
  foreach ($ps_order as $key => $ps)
  {
    if (preg_match('/\|r$/', $key) and $ps == '')
    {
      $notice .= tag(yab_shop_lang('checkout_'.preg_replace('/\|r$/', '', $key).''), 'li');
    }
  }

  // check POSTed email if customer affirmation is set to '1'
  if ($affirmation == '1' and !is_valid_email($ps_order['email']))
  {
    $notice .= tag(yab_shop_lang('checkout_mail_email_error'), 'li');
  }
  // if customer affirmation is set to '0' check POSTed email only for validity
  elseif ($affirmation == '0' and $ps_order['email'] != '')
  {
    if (!is_valid_email($ps_order['email']))
    {
      $notice .= tag(yab_shop_lang('checkout_mail_email_error'), 'li');
    }
  }
  return $notice;
}

/**
 * Match added products with product to add
 * depend on their properties to extend the id for saving.
 * 
 * @param array $cart Instance from class wfcart
 * @param string $productid Id from added product
 * @param string $product_property_1 Property from added product
 * @param string $product_property_2 Property from added product
 * @param string $product_property_3 Property from added product
 * @return string To extend the id
 */
function yab_shop_check_item($cart, $productid, $product_property_1, $product_property_2, $product_property_3)
{
  $i = 0;
  foreach ($cart->get_contents() as $item)
  {
    // if id match, look for given properties
    if (preg_match('/'.$productid.'-/', $item['itemid']))
    {
      $i++;
      if ($item['property_1'] == $product_property_1 and $item['property_2'] == $product_property_2 and $item['property_3'] == $product_property_3)
      {
        // if properties match, set $i to given $productid
        $i = str_replace($productid.'-', '', $item['itemid']);
        break;
      }
    }
  }
  return $i;
}

/**
 * Return POSTed data for repeated form views when required fields not filled.
 * 
 * @param string $input
 * @return string $output
 */
function yab_shop_return_input($input)
{
  $output = '';
  if (ps('order') != '')
  {
    $output = yab_shop_clean_input($_POST[$input]);
  }
  return $output;
}

/**
 * Build the form for unencrypted paypal submit.
 * 
 * @param array $cart Instance from class wfcart
 * @return string $form Formatted for output
 */
function yab_shop_build_paypal_form($cart)
{
  // check for live or testing
  $subdomain = '';
  if (yab_shop_paypal_config('paypal_live_or_sandbox') == 'sandbox')
  {
    $subdomain = '.sandbox';
  }

  // check for tax; if inclusive tax output should be '0.00' for paypal
  $tax = '0.00';
  if (yab_shop_config('tax_inclusive') == '0')
  {
    $tax = number_format(yab_shop_calculate_sum('tax'),2);
  }

  // if no email is set, paypal encounters an error
  $email = '';
  if (ps('email'))
  {
    $email = hInput('email', yab_shop_return_input('email')).n;
  }

  // some variables
  $action = 'https://www'.$subdomain.'.paypal.com/cgi-bin/webscr';
  $message = yab_shop_lang('checkout_paypal_no_forward');
  $message2 = yab_shop_lang('checkout_paypal_forward');
  $business_email = yab_shop_paypal_config('paypal_business_mail');
  $country = yab_shop_paypal_config('paypal_prefilled_country');
  $lc = yab_shop_paypal_config('paypal_interface_language');
  $section = pagelinkurl(array('s' => yab_shop_config('checkout_section_name')));
  $currency = yab_shop_config('currency');
  $shipping = yab_shop_shipping_costs();

  // get products and properties and build hidden inputs
  $i = 0;
  $products = '';
  foreach ($cart->get_contents() as $item)
  {
    $i++;
    $products .=  hInput('item_name_'.$i, $item['name']).n.
                  hInput('amount_'.$i, $item['price']).n.
                  hInput('quantity_'.$i, $item['qty']).n;
    $b = 0;
    $properties = '';
    if (!empty($item['property_1']))
    {
      $properties .=  hInput('on'.$b.'_'.$i, yab_shop_lang('custom_field_property_1')).n.
                      hInput('os'.$b.'_'.$i, $item['property_1']).n;
                      $b++;
    }
    if (!empty($item['property_2']))
    {
      $properties .=  hInput('on'.$b.'_'.$i, yab_shop_lang('custom_field_property_2')).n.
                      hInput('os'.$b.'_'.$i, $item['property_2']).n;
                      $b++;
    }
    if (!empty($item['property_3']))
    {
      $properties .=  hInput('on'.$b.'_'.$i, yab_shop_lang('custom_field_property_3')).n.
                      hInput('os'.$b.'_'.$i, $item['property_3']).n;
    }
    $products .= $properties;
  }

  $form = '';
  // do javascript and send the form automatically
  $form ='<script type="text/javascript">function doPaypal(){var New="'.$message2.'";document.getElementById("yabshoppaypalforward").innerHTML=New;document.getElementById("yab-paypal-form").submit();document.getElementById("yappaypalbusmit").style.display="none"}window.onload=doPaypal;</script>';
  // build form
  $form .= graf($message, ' class="yab-shop-message" id="yabshoppaypalforward"');
  $form .= tag(
    hInput('cmd', '_ext-enter').n.
    hInput('redirect_cmd', '_cart').n.
    hInput('upload', '1').n.
    hInput('business', $business_email).n.
    hInput('return', $section).n.
    hInput('country', $country).n.
    hInput('lc', $lc).n.
    hInput('currency_code', $currency).n.
    hInput('tax_cart', $tax).n.
    hInput('shipping_1', $shipping).n.
    hInput('first_name', yab_shop_return_input('firstname|r')).n.
    hInput('last_name', yab_shop_return_input('surname|r')).n.
    $email.
    hInput('address1', yab_shop_return_input('street|r')).n.
    hInput('city', yab_shop_return_input('city|r')).n.
    hInput('zip', yab_shop_return_input('postal|r')).n.
    $products.
    fInput('submit','paypal', yab_shop_lang('checkout_paypal_button'), 'submit', '', '', '', '', 'yappaypalbusmit').n,'form', ' method="post" action="'.$action.'" id="yab-paypal-form"'
  ); 
  return $form;
}

/**
 * Build the form for encrypted paypal submit.
 * 
 * @param array $cart Instance from class wfcart
 * @return string $form Formatted for output
 */
function yab_shop_build_paypal_encrypted_form($cart)
{
  global $tempdir;

  // check for live or testing
  $subdomain = '';
  if (yab_shop_paypal_config('paypal_live_or_sandbox') == 'sandbox')
  {
    $subdomain = '.sandbox';
  }

  // check for tax; if inclusive tax output should be '0.00' for paypal
  $tax = '0.00';
  if (yab_shop_config('tax_inclusive') == '0')
  {
    $tax = number_format(yab_shop_calculate_sum('tax'),2);
  }

  // some variables
  $action = 'https://www'.$subdomain.'.paypal.com/cgi-bin/webscr';
  $message = yab_shop_lang('checkout_paypal_no_forward');
  $message2 = yab_shop_lang('checkout_paypal_forward');
  $business_email = yab_shop_paypal_config('paypal_business_mail');
  $country = yab_shop_paypal_config('paypal_prefilled_country');
  $lc = yab_shop_paypal_config('paypal_interface_language');
  $section = pagelinkurl(array('s' => yab_shop_config('checkout_section_name')));
  $currency = yab_shop_config('currency');
  $shipping = yab_shop_shipping_costs();

  // variables for encrypted inputs
  $TempFileDirectory = $tempdir;
  $myPublicCertificate = yab_shop_paypal_config('paypal_certificates_path').'/'.yab_shop_paypal_config('paypal_my_public_certificate_name');
  $myPrivateKey = yab_shop_paypal_config('paypal_certificates_path').'/'.yab_shop_paypal_config('paypal_my_private_key_name');
  $CertificateID = yab_shop_paypal_config('paypal_certificate_id');
  $PayPalPublicCertificate = yab_shop_paypal_config('paypal_certificates_path').'/'.yab_shop_paypal_config('paypal_public_certificate_name');

  $paypal = &new PayPalEWP();
  $paypal->setCertificate($myPublicCertificate, $myPrivateKey);
  $paypal->setCertificateID($CertificateID);
  $paypal->setPayPalCertificate($PayPalPublicCertificate);

  $parameters = array(
    'cmd'           => '_ext-enter',
    'redirect_cmd'  => '_cart',
    'upload'        => '1',
    'business'      => $business_email,
    'cert_id'       => $CertificateID,
    'return'        => $section,
    'country'       => $country,
    'lc'            => $lc,
    'currency_code' => $currency,
    'tax_cart'      => $tax,
    'shipping_1'    => $shipping,
    'first_name'    => yab_shop_return_input('firstname|r'),
    'last_name'     => yab_shop_return_input('surname|r'),
    'address1'      => yab_shop_return_input('street|r'),
    'city'          => yab_shop_return_input('city|r'),
    'zip'           => yab_shop_return_input('postal|r')
  );

  // get products and properties and add to $parameters array
  $i = 0;
  foreach ($cart->get_contents() as $item)
  {
    $i++;
    $parameters['item_name_'.$i] = $item['name'];
    $parameters['amount_'.$i]    = $item['price'];
    $parameters['quantity_'.$i]  = $item['qty'];

    $b = 0;
    if (!empty($item['property_1']))
    {
      $parameters['on'.$b.'_'.$i] = yab_shop_lang('custom_field_property_1');
      $parameters['os'.$b.'_'.$i] = $item['property_1'];
      $b++;
    }
    if (!empty($item['property_2']))
    {
      $parameters['on'.$b.'_'.$i] = yab_shop_lang('custom_field_property_2');
      $parameters['os'.$b.'_'.$i] = $item['property_2'];
                      $b++;
    }
    if (!empty($item['property_3']))
    {
      $parameters['on'.$b.'_'.$i] = yab_shop_lang('custom_field_property_3');
      $parameters['os'.$b.'_'.$i] = $item['property_3'];
    }
  }

  // if no email is set, paypal encounters an error
  if (ps('email'))
  {
    $parameters['email'] = yab_shop_return_input('email');
  }

  $encryptedButton = $paypal->encryptButton($parameters);

  // do javascript and send the form automatically
  $form ='<script type="text/javascript">function doPaypal(){var New="'.$message2.'";document.getElementById("yabshoppaypalforward").innerHTML=New;document.getElementById("yab-paypal-form").submit();document.getElementById("yappaypalbusmit").style.display="none"}window.onload=doPaypal;</script>';
  // build form
  $form .= graf($message, ' class="yab-shop-message" id="yabshoppaypalforward"');
  $form .= tag(
    hInput('cmd', '_s-xclick').n.
    hInput('encrypted', $encryptedButton).n.
    fInput('submit','paypal', yab_shop_lang('checkout_paypal_button'), 'submit', '', '', '', '', 'yappaypalbusmit').n,'form', ' method="post" action="'.$action.'" id="yab-paypal-form"'
  );

  // check for errors
  switch ($paypal->error)
  {
    case 0:
      $out = $form;
      break;
    case 1:
      $out = 'Paypal certificate id is not set!';
      break;
    case 2:
      $out = 'Your public and/or private certificate is not readable! Please check permissions, names and paths!';
      break;
    case 3:
      $out = 'Paypal public certificate is not readable! Please check permissions, names and paths!';
      break;
    case 4:
      $out = 'Seems to be openssl is not supported!';
      break;
    default:
      $out =  'Unkown error occured!';
  }
  return $out;
}

/**
 * Build the checkout form
 * 
 * @return string Formatted for output
 */
function yab_shop_build_checkout_form()
{
  $req = '';
  // check for customer affirmation so it can set email as required
  if (yab_shop_config('order_affirmation_mail') == 1)
  {
    $req = ' class="yab-shop-required"';
  }

  $form = tag(
    fieldset(
      graf(
        tag(yab_shop_lang('checkout_firstname'), 'label', ' for="firstname"').
        fInput('text', 'firstname|r', yab_shop_return_input('firstname|r'), '', '', '', '', '', 'firstname'), ' class="yab-shop-required"'
      ).
      graf(
        tag(yab_shop_lang('checkout_surname'), 'label', ' for="surname"').
        fInput('text', 'surname|r', yab_shop_return_input('surname|r'), '', '', '', '', '', 'surname'), ' class="yab-shop-required"'
      ).
      graf(
        tag(yab_shop_lang('checkout_street'), 'label', ' for="street"').
        fInput('text', 'street|r', yab_shop_return_input('street|r'), '', '', '', '', '', 'street'), ' class="yab-shop-required"'
      ).
      graf(
        tag(yab_shop_lang('checkout_postal'), 'label', ' for="postal"').
        fInput('text', 'postal|r', yab_shop_return_input('postal|r'), '', '', '', '', '', 'postal').
        tag(yab_shop_lang('checkout_city'), 'label', ' for="city" class="city"').
        fInput('text', 'city|r', yab_shop_return_input('city|r'), '', '', '', '', '', 'city'),
      ' class="zip-city yab-shop-required"'
      ).
      graf(
        tag(yab_shop_lang('checkout_phone'), 'label', ' for="phone"').
        fInput('text', 'phone', yab_shop_return_input('phone'), '', '', '', '', '', 'phone')
      ).
      graf(
        tag(yab_shop_lang('checkout_email'), 'label', ' for="email"').
        fInput('text', 'email', yab_shop_return_input('email'), '', '', '', '', '', 'email'), $req
      ).
      yab_shop_checkout_payment_methods().
      graf(
        tag(yab_shop_lang('checkout_message'), 'label', ' for="message"').
        '<textarea cols="40" rows="5" name="message" id="message">'.yab_shop_return_input('message').'</textarea>'
      ).
      graf(
        checkbox('tou', '1', '0', '', 'tou').
        tag(yab_shop_lang('checkout_terms_of_use'), 'label', ' for="tou"'),
      ' class="tou yab-shop-required"'
      ).
      graf(
      fInput('submit', 'order', yab_shop_lang('checkout_order'), 'submit'), 
      ' class="submit"'
      )
    ),'form', ' method="post" action="'.pagelinkurl(array('s' => yab_shop_config('checkout_section_name'))).'" id="yab-checkout-form"'
  );
  return $form;
}

/**
 * Build the checkout table
 * 
 * @param array $cart Instance from wfcart
 * @return string Formatted for output
 */
function yab_shop_build_checkout_table($cart, $no_change = false)
{
  $tax_inclusive = yab_shop_config('tax_inclusive');

  $checkout_display = tr(
    tag(yab_shop_lang('table_caption_content'), 'th').
    tag(yab_shop_lang('table_caption_change'), 'th', ' class="yab-checkout-change"').
    tag(yab_shop_lang('table_caption_price'), 'th', ' class="yab-checkout-price"')
  ).n;

  // set a class when no_change for separate styling
  $class = '';
  if ($no_change != false)
  {
    $class = ' class="yab-shop-nochange"';
  }

  // get cart items, loop through and put it out 
  foreach ($cart->get_contents() as $item)
  {
    $item_price = yab_shop_currency_out(yab_shop_config('currency'), 'cur').yab_shop_currency_out(yab_shop_config('currency'), 'toform', $item['price']);
    $item_price_sum = yab_shop_currency_out(yab_shop_config('currency'), 'cur').yab_shop_currency_out(yab_shop_config('currency'), 'toform', $item['price'] * $item['qty']);

    // output with no availabilty to change qty
    $out_qty = yab_shop_checkout_qty_edit($item['itemid'], $item['qty']);
    if ($no_change != false)
    {
      $out_qty = $item['qty'];
    }

    $checkout_display .= tr(
      td(
        $item['name'].
        tag(
        yab_shop_build_checkout_customs($item['property_1'], yab_shop_lang('custom_field_property_1'), yab_shop_config('custom_field_property_1_name')).
        yab_shop_build_checkout_customs($item['property_2'], yab_shop_lang('custom_field_property_2'), yab_shop_config('custom_field_property_2_name')).
        yab_shop_build_checkout_customs($item['property_3'], yab_shop_lang('custom_field_property_3'), yab_shop_config('custom_field_property_3_name')).
        yab_shop_build_checkout_customs($item_price, yab_shop_lang('price'), yab_shop_config('custom_field_price_name'))
        , 'ul')
      ).
      td($out_qty, '', 'yab-checkout-change').
      td($item_price_sum, '', 'yab-checkout-price')
    ).n;
  }

  // check value of tax_inclusive setting and do calculate and output
  if ($tax_inclusive == '0')
  {
    $checkout_display .= tr(
      tda(yab_shop_lang('sub_total'), ' colspan="2"').
      tda(yab_shop_currency_out(yab_shop_config('currency'), 'cur').yab_shop_currency_out(yab_shop_config('currency'), 'toform', $cart->total), ' class="yab-checkout-sum"'),
      ' class="yab-checkout-subtotal"'
    ).n;
    $checkout_display .= tr(
      tda(yab_shop_lang('checkout_tax_exclusive'), ' colspan="2"').
      tda(yab_shop_currency_out(yab_shop_config('currency'), 'cur').yab_shop_currency_out(yab_shop_config('currency'), 'toform', yab_shop_calculate_sum('tax')), ' class="yab-checkout-sum"'),
      ' class="yab-checkout-tax"'
    ).n;
    $checkout_display .= tr(
      tda(yab_shop_lang('shipping_costs'), ' colspan="2"').
      tda(yab_shop_currency_out(yab_shop_config('currency'), 'cur').yab_shop_currency_out(yab_shop_config('currency'), 'toform', yab_shop_shipping_costs()), ' class="yab-checkout-sum"'),
      ' class="yab-checkout-shipping"'
    ).n;
    $checkout_display .= tr(
      tda(yab_shop_lang('grand_total'), ' colspan="2"').
      tda(yab_shop_currency_out(yab_shop_config('currency'), 'cur').yab_shop_currency_out(yab_shop_config('currency'), 'toform', yab_shop_calculate_sum('brutto') + yab_shop_shipping_costs()), ' class="yab-checkout-sum"'),
      ' class="yab-checkout-total"'
    );
  }
  else
  {
    $checkout_display .= tr(
      tda(yab_shop_lang('sub_total'), ' colspan="2"').
      tda(yab_shop_currency_out(yab_shop_config('currency'), 'cur').yab_shop_currency_out(yab_shop_config('currency'), 'toform', $cart->total), ' class="yab-checkout-sum"'),
      ' class="yab-checkout-subtotal"'
    ).n;
    $checkout_display .= tr(
      tda(yab_shop_lang('shipping_costs'), ' colspan="2"').
      tda(yab_shop_currency_out(yab_shop_config('currency'), 'cur').yab_shop_currency_out(yab_shop_config('currency'), 'toform', yab_shop_shipping_costs()), ' class="yab-checkout-sum"'),
      ' class="yab-checkout-shipping"'
    ).n;
    $checkout_display .= tr(
      tda(yab_shop_lang('grand_total'), ' colspan="2"').
      tda(yab_shop_currency_out(yab_shop_config('currency'), 'cur').yab_shop_currency_out(yab_shop_config('currency'), 'toform', $cart->total + yab_shop_shipping_costs()), ' class="yab-checkout-sum"'),
      ' class="yab-checkout-total"'
    ).n;
    $checkout_display .= tr(
      tda(yab_shop_lang('checkout_tax_inclusive'), ' colspan="2"').
      tda(yab_shop_currency_out(yab_shop_config('currency'), 'cur').yab_shop_currency_out(yab_shop_config('currency'), 'toform', yab_shop_calculate_sum('tax')), ' class="yab-checkout-sum"'),
      ' class="yab-checkout-tax"'
    );
  }
  $checkout_display = tag($checkout_display, 'table', ' id="yab-checkout-table"'.$class);
  return $checkout_display;
}

/**
 * Build the shopping cart.
 *
 * @param array $cart Instance of wfcart
 * @return string Formatted for output
 */
function yab_shop_build_cart($cart)
{
  $cart_display = '';

  // display the cart when products are added
  if ($cart->itemcount > 0)
  {
    foreach ($cart->get_contents() as $item)
    {
      $cart_display .= tag(
        $item['name'].tag(
          tag(yab_shop_lang('price').':&nbsp;'.yab_shop_currency_out(yab_shop_config('currency'), 'cur').yab_shop_currency_out(yab_shop_config('currency'), 'toform', $item['price']), 'li', ' class="yab-price"').
            tag(yab_shop_lang('quantity').':&nbsp;'.$item['qty'], 'li', ' class="yab-qty"'),
        'ul'),
      'li', ' class="yab-item"');
    }
    $cart_display = tag($cart_display, 'ul', ' class="yab-cart"');
    $cart_display .= tag(yab_shop_lang('sub_total').':&nbsp;'.yab_shop_currency_out(yab_shop_config('currency'), 'cur').yab_shop_currency_out(yab_shop_config('currency'), 'toform', $cart->total), 'span', ' class="yab-subtotal"');
    $cart_display .= tag(yab_shop_lang('to_checkout'), 'a', ' href="'.pagelinkurl(array('s' => yab_shop_config('checkout_section_name'))).'" title="'.yab_shop_lang('to_checkout').'" class="yab-to-checkout"');
  }
  // if nothing in cart show a notice
  else
  {
    $cart_display = tag(yab_shop_lang('empty_cart'), 'span', ' class="yab-empty"');
  }
  return $cart_display;
}

/**
 * Build the mail body
 * 
 * @param array $cart Instance from wfcart
 * @param array $ps_order Sanitized POSTed data
 * @param string $affirmation Customer affirmation value from config
 * @return string Formatted for output in mailbody
 */
function yab_shop_build_mail_body($cart, $ps_order, $affirmation = '0')
{
  $line_1 = '----------------------------------------------------------------------';
  $line_2 = '======================================================================';
  $line_3 = '______________________________________________________________________';

  // set CRLFs
  $eol = "\r\n";
  if (!is_windows())
  {
    $eol = "\n";
  }

  // set content for admin mail
  if ($affirmation != '1')
  {
    $body = yab_shop_mail_lang('admin_mail_pre_products').$eol;
  }
  // set content for customer mail
  else
  {
    $body = yab_shop_mail_lang('affirmation_mail_pre_products').$eol;
  }

  $body .= 
    $eol.yab_shop_lang('checkout_firstname').': '.$ps_order['firstname|r'].
    $eol.yab_shop_lang('checkout_surname').': '.$ps_order['surname|r'].
    $eol.yab_shop_lang('checkout_street').': '.$ps_order['street|r'].
    $eol.yab_shop_lang('checkout_postal').'/'.yab_shop_lang('checkout_city').': '.$ps_order['postal|r'].' '.$ps_order['city|r'].
    $eol.yab_shop_lang('checkout_phone').': '.$ps_order['phone'].
    $eol.yab_shop_lang('checkout_email').': '.$ps_order['email'].
    $eol.yab_shop_lang('checkout_payment').': '.$ps_order['payment'].
    $eol.yab_shop_lang('checkout_message').': '.$ps_order['message'].$eol;
  $body .= $eol.$line_1.$eol;

  // get cart content and put it out
  foreach ($cart->get_contents() as $item)
  {
    $item_price = yab_shop_config('currency').' '.yab_shop_currency_out(yab_shop_config('currency'), 'toform', $item['price']);
    $item_price_sum = yab_shop_config('currency').' '.yab_shop_currency_out(yab_shop_config('currency'), 'toform', $item['price'] * $item['qty']);

    $body .= $eol.$item['name'].
      $eol.$item['qty'].' x '.$item_price.' = '.$item_price_sum.$eol;
    $body .= yab_shop_build_mail_customs($item['property_1'], yab_shop_lang('custom_field_property_1'), $eol);
    $body .= yab_shop_build_mail_customs($item['property_2'], yab_shop_lang('custom_field_property_2'), $eol);
      $body .= yab_shop_build_mail_customs($item['property_3'], yab_shop_lang('custom_field_property_3'), $eol);
  }

  // different outputs for different values of tax inclusive value from config
  if (yab_shop_config('tax_inclusive') == '0')
  {
    $body .= $eol.$line_1.
      $eol.$eol.yab_shop_lang('sub_total').': '.yab_shop_config('currency').' '.yab_shop_currency_out(yab_shop_config('currency'), 'toform', $cart->total).
      $eol.yab_shop_lang('checkout_tax_exclusive').': '.yab_shop_config('currency').' '.yab_shop_currency_out(yab_shop_config('currency'), 'toform', yab_shop_calculate_sum('tax')).
      $eol.yab_shop_lang('shipping_costs').': '.yab_shop_config('currency').' '.yab_shop_currency_out(yab_shop_config('currency'), 'toform', yab_shop_shipping_costs()).
      $eol.$eol.$line_2.
      $eol.$eol.yab_shop_lang('grand_total').': '.yab_shop_config('currency').' '.yab_shop_currency_out(yab_shop_config('currency'), 'toform', yab_shop_calculate_sum('brutto') + yab_shop_shipping_costs()).
      $eol.$line_3.$eol.$line_2;
  }
  else
  {
    $body .= $eol.$line_1.
      $eol.$eol.yab_shop_lang('sub_total').': '.yab_shop_config('currency').' '.yab_shop_currency_out(yab_shop_config('currency'), 'toform', $cart->total).
      $eol.yab_shop_lang('shipping_costs').': '.yab_shop_config('currency').' '.yab_shop_currency_out(yab_shop_config('currency'), 'toform', yab_shop_shipping_costs()).
      $eol.$eol.$line_2.
      $eol.$eol.yab_shop_lang('grand_total').': '.yab_shop_config('currency').' '.yab_shop_currency_out(yab_shop_config('currency'), 'toform', $cart->total + yab_shop_shipping_costs()).
      $eol.$line_3.$eol.$line_2.
      $eol.$eol.yab_shop_lang('checkout_tax_inclusive').': '.yab_shop_config('currency').' '.yab_shop_currency_out(yab_shop_config('currency'), 'toform', yab_shop_calculate_sum('tax')).$eol;
  }

  // set content for admin mail
  if ($affirmation != '1')
  {
    $body .= $eol.yab_shop_mail_lang('admin_mail_after_products');
  }
  // set content for customer mail
  else
  {
    $body .= $eol.yab_shop_mail_lang('affirmation_mail_after_products');
  }

  return $body;
}

/**
 * Build the property for mail body output
 * 
 * @param string $item Product property from cart
 * @param string $lang Localization from config
 * @param string $eol CRLF
 * @return string Formatted for output in mailbody
 */
function yab_shop_build_mail_customs($item, $lang, $eol)
{
  $out = '';
  if (!empty($item))
  {
    $out = $lang.': '.$item.$eol;
  }
  return $out;
}

/**
 * Send mail.
 * Modified txpMail().
 * 
 * @param string $to
 * @param string $subject
 * @param string $body
 * @return boolean
 */
function yab_shop_shop_mail($to, $subject, $body)
{
  global $prefs;

  // decide which charset is beeing used
  if ($prefs['override_emailcharset'] and is_callable('utf8_decode'))
  {
    $charset = 'ISO-8859-1';
    $subject = utf8_decode($subject);
    $body = utf8_decode($body);
  }
  else
  {
  $charset = 'UTF-8';
  }

  // is mail()
  if (!is_callable('mail'))
  {
    return false;
  }
  else
  {
    // set CRLFs
    $eol = "\r\n";
    if (!is_windows())
    {
      $eol = "\n";
    }
    // mime-encode
    $sitename = yab_shop_mailheader($prefs['sitename'], 'text');
    $subject = yab_shop_mailheader($subject, 'text');

    // send mail and return boolean
    return mail($to, $subject, $body,
      'From: '.$sitename.' <'.yab_shop_config('admin_mail').'>'.''.
      $eol.'Reply-To: '.$sitename.' <'.yab_shop_config('admin_mail').'>'.''.
      $eol.'X-Mailer: Textpattern (yab_shop)'.
      $eol.'Content-Transfer-Encoding: 8-bit'.
      $eol.'Content-Type: text/plain; charset="'.$charset.'"'.$eol
    );
  }
}

/**
 * Mime-encode headers for mail. Borrowed from zem_contact_reborn.
 * See <http://vanmelick.com/txp/zem_contact_reborn.php?code>.
 *
 * @param string $sring 
 * @param sting $type
 * @return string
 */
function yab_shop_mailheader($string, $type)
{
  global $prefs;

  if (!strstr($string,'=?') and !preg_match('/[\x00-\x1F\x7F-\xFF]/', $string))
  {
    if ("phrase" == $type)
    {
      if (preg_match('/[][()<>@,;:".\x5C]/', $string))
      {
        $string = '"'.strtr($string, array("\\" => "\\\\", '"' => '\"')).'"';
      }
    }
    elseif ("text" != $type)
    {
      trigger_error('Unknown encode_mailheader type', E_USER_WARNING);
    }
    return $string;
  }

  if ($prefs['override_emailcharset'])
  {
    $start = '=?ISO-8859-1?B?';
    $pcre  = '/.{1,42}/s';
  }
  else
  {
    $start = '=?UTF-8?B?';
    $pcre  = '/.{1,45}(?=[\x00-\x7F\xC0-\xFF]|$)/s';
  }

  $end = '?=';
  $sep = "\r\n";

  if (!is_windows())
  {
    $sep = "\n";
  }

  preg_match_all($pcre, $string, $matches);

  return $start.join($end.$sep.' '.$start, array_map('base64_encode',$matches[0])).$end;
}

/**
 * Clean inputs from unwanted.
 * 
 * @param mixed $input for sanitize
 * @param string $modus for html output or db insert
 * @return mixed
 */
function yab_shop_clean_input($input, $modus = 'output')
{
  // is empty?
  if (empty($input))
  {
    $cleaned = $input;
  }

  // is $input an array do this function for each value
  if (is_array($input))
  {
    foreach ($input as $key => $val)
    {
      $cleaned[$key] = yab_shop_clean_input($val);
    }
  }
  else
  {
    // delete unwanted chars
    $cleaned = str_replace(array('=', '&', '"', '\'', '<', '>', ';', '\\'), '', $input);
    // if some chars forgotten so add or strip slashes, depends on modus
    if ($modus != 'output')
    {
      $cleaned = doSlash($cleaned);
    }
    else
    {
      $cleaned = doStrip($cleaned);
    }
  }
  return $cleaned;
}

/**
 * Check available payment methods and
 * build a select tag for checkout form.
 * 
 * @return string Formatted for output
 */
function yab_shop_checkout_payment_methods()
{
  $option = '';
  $attr = '';
  $select = '';
  $content = '';
  $hidden_value = '';
  $label = tag(yab_shop_lang('checkout_payment'), 'label', ' for="payment"');
  $b = 0;

  // check available payment methods and build option tags
  if (yab_shop_config('payment_method_acc') == '1')
  {
    $b++;
    $hidden_value .= yab_shop_lang('checkout_payment_acc');
    $option .= tag(yab_shop_lang('checkout_payment_acc'), 'option', ' value="'.yab_shop_lang('checkout_payment_acc').'"');
  }
  if (yab_shop_config('payment_method_pod') == '1')
  {
    $b++;
    $hidden_value .= yab_shop_lang('checkout_payment_pod');
    $option .= tag(yab_shop_lang('checkout_payment_pod'), 'option', ' value="'.yab_shop_lang('checkout_payment_pod').'"');
  }
  if (yab_shop_config('payment_method_pre') == '1')
  {
    $b++;
    $hidden_value .= yab_shop_lang('checkout_payment_pre');
    $option .= tag(yab_shop_lang('checkout_payment_pre'), 'option', ' value="'.yab_shop_lang('checkout_payment_pre').'"');
  }
  if (yab_shop_config('payment_method_paypal') == '1')
  {
    $b++;
    $hidden_value .= yab_shop_lang('checkout_payment_paypal');
    $option .= tag(yab_shop_lang('checkout_payment_paypal'), 'option', ' value="'.yab_shop_lang('checkout_payment_paypal').'"');
  }

  // if none available in config return an error message
  if ($b == 0)
  {
    $content .= 'No payment method available!';
    $attr .= ' style="font-weight: bold; color: #9E0000"';
  }
  // if one available payment method return hidden input and span
  elseif ($b == 1)
  {
    $select .=  fInput('hidden', 'payment', $hidden_value, '', '', '', '', '', 'payment').
                tag($hidden_value, 'span', ' id="yab-shop-one-payment"');
    $content .= $label.$select;
  }
  //if more then one payment method available, return select tag
  else
  {
    $select .= tag($option, 'select', ' name="payment" id="payment"');
    $content .= $label.$select;
  }

  $payment = graf($content, $attr);
  return $payment;
}

/**
 * Build edit and delete form for checkout table.
 * 
 * @param string $itemid
 * @param string $qty
 * @return string Formatted for output
 */
function yab_shop_checkout_qty_edit($itemid, $qty)
{
  $edit_form = tag(
    hInput('editid', $itemid).
    fInput('text','editqty',$qty,'','','','1').
    fInput('submit','edit',yab_shop_lang('checkout_edit'), 'submit-edit').
    fInput('submit','del',yab_shop_lang('checkout_delete'), 'submit-del'),
  'form', ' method="post" action="'.pagelinkurl(array('s' => yab_shop_config('checkout_section_name'))).'"'
  );
  return $edit_form;
}

/**
 * Build product property list for checkout table.
 * 
 * @param string $item Productid from cart
 * @param string $lang Localization form config
 * @param string $conf Name of property custom field from config
 * @return string Formatted for output
 */
function yab_shop_build_checkout_customs($item, $lang, $conf)
{
  $conf = strtolower($conf);
  $out = '';
  if (!empty($item))
  {
    $out = tag($lang.' '.$item, 'li', ' class="yab-checkout-item-'.$conf.'"');
  }
  return $out;
}

/**
 * Build paragraph with property label and
 * properties select tag prepared for output.
 * 
 * @param string $custom_field Name of property custom field from config
 * @param string $label_name Localization form config
 * @return string Formatted for output
 */
function yab_shop_build_custom_select_tag($custom_field, $label_name)
{
  global $thisarticle;
  $custom_field_low = strtolower($custom_field);
  $out = '';

  if (!empty($thisarticle[$custom_field_low]))
  {
    $out =  graf(
      tag($label_name.': ', 'label', ' for="select-'.$custom_field_low.'"').
      yab_shop_custom_field(array('name' => $custom_field)),' class="yab-add-select-'.$custom_field_low.'"'
    );
  }
  return $out;
}

/**
 * Set shipping cost depends on limit in config and subtotal value.
 * 
 * @return float
 */
function yab_shop_shipping_costs()
{
  $cart =& $_SESSION['wfcart'];
  $sub_total = $cart->total;
  $shipping_costs = yab_shop_replace_commas(yab_shop_config('shipping_costs'));
  $free_shipping = yab_shop_replace_commas(yab_shop_config('free_shipping'));

  if ($sub_total >= $free_shipping)
  {
    $shipping_costs = 0.00;
  }
  return $shipping_costs;
}

/**
 * Calculate order brutto, netto and tax values depends
 * on tax rate and tax rate inclusive or exclusive.
 * 
 * @param string $what 
 * @return float
 */
function yab_shop_calculate_sum($what)
{
  $cart =& $_SESSION['wfcart'];
  $tax_rate = yab_shop_replace_commas(yab_shop_config('tax_rate'));
  $tax_inclusive = yab_shop_config('tax_inclusive');
  $sub_total = $cart->total;
  $calculated = array();

  if ($tax_inclusive == '0')
  {
    $calculated['netto'] = $sub_total;
    $calculated['brutto'] = yab_shop_rounding($sub_total * ($tax_rate / 100 + 1));
    $calculated['tax'] = yab_shop_rounding($sub_total * ($tax_rate / 100));
    
  }
  else
  {
    $calculated['brutto'] = $sub_total;
    $calculated['netto'] = yab_shop_rounding($sub_total / ($tax_rate / 100 + 1), 'down');
    $calculated['tax'] = yab_shop_rounding($calculated['netto'] * ($tax_rate / 100));
  }
  return $calculated[$what];
}

/**
 * Round up or down the netto or tax value,
 * 'cause the revenue office want all the micro cents.
 * 
 * @param float $value
 * @param string $modus 
 * @return float
 */
function yab_shop_rounding($value, $modus = 'up')
{
  $decimal = 2;

  $rounded = ceil($value * pow(10, $decimal)) / pow(10, $decimal);
  if ($modus != 'up')
  {
    $rounded = floor($value * pow(10, $decimal)) / pow(10, $decimal);
  }
  return $rounded;
}

/**
 * Replace commas with points from input to be prepared for calculate.
 * 
 * @param string $input
 * @return sting
 */
function yab_shop_replace_commas($input)
{
  $replaced = str_replace(',', '.', $input);
  return $replaced;
}

/**
 * Output assigned currency symbol or corresponding number formatting.
 * 
 * @param string $currency From config in ISO 4217 format
 * @param string $what
 * @param float $toform Number that shall be formatted
 * @return float or string depends on $what
 */
function yab_shop_currency_out($currency, $what, $toform = '')
{
  switch ($currency)
  {
    case 'USD':
      $out = array(
        'cur'       => '&#36;',
        'toform'  => number_format($toform, 2)
      );
      break;
    case 'GBP':
      $out = array(
        'cur'       => '&#163;',
        'toform'  => number_format($toform, 2)
      );
      break;
    case 'CAD':
      $out = array(
        'cur'       => '&#36;',
        'toform'  => number_format($toform, 2)
      );
      break;
    case 'JPY':
      $out = array(
        'cur'       => '&#65509;',
        'toform'  => number_format($toform)
      );
      break;
    case 'AUD':
      $out = array(
        'cur'       => '&#36;',
        'toform'  => number_format($toform, 2)
      );
      break;
    case 'NZD':
      $out = array(
        'cur'       => '&#36;',
        'toform'  => number_format($toform, 2)
      );
      break;
    case 'CHF':
      $out = array(
        'cur'       => 'SFr.',
        'toform'  => number_format($toform, 2, ',', '.')
      );
      break;
    case 'HKD':
      $out = array(
        'cur'       => '&#36;',
        'toform'  => number_format($toform, 2)
      );
      break;
    case 'SGD':
      $out = array(
        'cur'       => '&#36;',
        'toform'  => number_format($toform, 2)
      );
      break;
    case 'SEK':
      $out = array(
        'cur'       => 'kr',
        'toform'  => number_format($toform, 2, ',', '.')
      );
      break;
    case 'DKK':
      $out = array(
        'cur'       => 'kr',
        'toform'  => number_format($toform, 2, ',', '.')
      );
      break;
    case 'PLN':
      $out = array(
        'cur'       => 'z&#322;',
        'toform'  => number_format($toform, 2, ',', '.')
      );
      break;
    case 'NOK':
      $out = array(
        'cur'       => 'kr',
        'toform'  => number_format($toform, 2, ',', '.')
      );
      break;
    case 'HUF':
      $out = array(
        'cur'       => 'Ft',
        'toform'  => number_format($toform)
      );
      break;
    case 'CZK':
      $out = array(
        'cur'       => 'K&#269;',
        'toform'  => number_format($toform, 2, ',', '.')
      );
      break;
    case 'EEK':
      $out = array(
        'cur'       => 'kr',
        'toform'  => number_format($toform, 2)
      );
      break;
    case 'RSD':
      $out = array(
        'cur'       => 'RSD&nbsp;',
        'toform'  => number_format($toform, 2, ',', '.')
      );
      break;
    default:
      $out = array(
        'cur'       => '&#8364;',
        'toform'  => number_format($toform, 2, ',', '.')
      );
      break;
  }
  return $out[$what];
}

/**
 * Build select tag.
 * 
 * @param array $array Taken from exploded property custom field
 * @param string $name Str_lower name of property custom field
 * @return string Formatted for output
 */
function yab_shop_type_select_custom($array, $name = 'type')
{
  $out = '<select name="'.$name.'" id="select-'.$name.'">'.n;
  foreach ($array as $option)
  {
    $option = trim(htmlspecialchars($option));
    $out .= t.'<option value="'.$option.'">'.$option.'</option>'.n;
  }
  $out .= '</select>'.n;
  return $out;
}

/**
 * Start the session.
 * 
 * @return boolean
 */
function yab_shop_start_session()
{
  if (headers_sent())
  {
    if (!isset($_SESSION))
    {
      $_SESSION = array();
    }
    return false;
  }
  elseif (!isset($_SESSION))
  {
    session_cache_limiter("must-revalidate");
    session_start();
    return true;
  }
  else
  {
  return true;
  }
}

if (!class_exists('wfCart'))
{

/**
 * Modified Webforce Cart v.1.5
 * 
 * Webforce Cart v.1.5
 * A Session based, Object Oriented Shopping Cart Component for PHP.
 *
 * (c) 2004-2005 Webforce Ltd, NZ
 * http://www.webforcecart.com/
 * all rights reserved 
 *
 * Webforce cart is free software. Licence LGPL. � 2004-2005 Webforce Ltd, New Zealand.
 * Licence: LGPL - http://www.gnu.org/copyleft/lesser.txt
 */

  class wfCart
  {
    var $total = 0;
    var $itemcount = 0;
    var $items = array();
    var $itemprices = array();
    var $itemnames = array();         // added
    var $itemproperties_1 = array();  // added
    var $itemproperties_2 = array();  // added
    var $itemproperties_3 = array();  // added
    var $itemqtys = array();

    function get_contents()
    { 
      $items = array();
      foreach ($this->items as $tmp_item)
      {
        $item = false;
        $item['itemid'] = $tmp_item;
        $item['qty'] = $this->itemqtys[$tmp_item];
        $item['price'] = $this->itemprices[$tmp_item];
        $item['name'] = $this->itemnames[$tmp_item];              // added
        $item['property_1'] = $this->itemproperties_1[$tmp_item]; // added
        $item['property_2'] = $this->itemproperties_2[$tmp_item]; // added
        $item['property_3'] = $this->itemproperties_3[$tmp_item]; // added
        $item['subtotal'] = $item['qty'] * $item['price'];
        $items[] = $item;
      }
      return $items;
    }

    function add_item($itemid, $qty = 1, $price = false, $name = false, $property_1 = false, $property_2 = false, $property_3 = false) // modified
    {
//       if(!$price)
//       {
//         $price = wf_get_price($itemid,$qty);
//       }
//       if(!$info)
//       {
//         $info = wf_get_info($itemid);
//       }

      if ($qty > 0) // added (undefinied index)
      {
        if (isset($this->itemqtys[$itemid]) and $this->itemqtys[$itemid] > 0) // modified
        {
          $this->itemqtys[$itemid] += $qty;
          $this->_update_total();
        }
        else
        {
          $this->items[]= $itemid;
          $this->itemqtys[$itemid] = $qty;
          $this->itemprices[$itemid] = $price;
          $this->itemnames[$itemid] = $name;              // added
          $this->itemproperties_1[$itemid] = $property_1; // added
          $this->itemproperties_2[$itemid] = $property_2; // added
          $this->itemproperties_3[$itemid] = $property_3; // added
        }
        $this->_update_total();
      }
    }

    function edit_item($itemid, $qty)
    {
      if ($qty < 1)
      {
        $this->del_item($itemid);
      }
      else
      {
        $this->itemqtys[$itemid] = $qty;
      // uncomment this line if using 
      // the wf_get_price function
      // $this->itemprices[$itemid] = wf_get_price($itemid,$qty);
      }
      $this->_update_total();
    }

    function del_item($itemid)
    {
      $ti = array();
      $this->itemqtys[$itemid] = 0;
      foreach ($this->items as $item)
      {
        if ($item != $itemid)
        {
          $ti[] = $item;
        }
      }
      $this->items = $ti;
      unset($this->itemprices[$itemid]);        // added for clean up cart
      unset($this->itemnames[$itemid]);         // added for clean up cart
      unset($this->itemproperties_1[$itemid]);  // added for clean up cart
      unset($this->itemproperties_2[$itemid]);  // added for clean up cart
      unset($this->itemproperties_3[$itemid]);  // added for clean up cart
      unset($this->itemqtys[$itemid]);          // added for clean up cart
      $this->_update_total();
    }

    function empty_cart()
    {
      $this->total = 0;
      $this->itemcount = 0;
      $this->items = array();
      $this->itemprices = array();
      $this->itemproperties_1 = array();  // added
      $this->itemproperties_2 = array();  // added
      $this->itemproperties_3 = array();  // added
      $this->itemqtys = array();
    }

    function _update_total()
    {
      $this->itemcount = 0;
      $this->total = 0;
      if (sizeof($this->items > 0))
      {
        foreach ($this->items as $item)
        {
          $this->total += ($this->itemprices[$item] * $this->itemqtys[$item]);
          $this->itemcount++;
        }
      }
    }
  }
}

if (!class_exists('PayPalEWP'))
{

/**
 * The PayPal class implements the dynamic encryption of
 * PayPal "buy now" buttons using the PHP openssl functions. 
 *
 * Original Author: Ivor Durham (ivor.durham@ivor.cc)
 * Edited by PayPal_Ahmad  (Nov. 04, 2006)
 * Posted originally on PDNCommunity:
 * http://www.pdncommunity.com/pdn/board/message?board.id=ewp&message.id=87#M87
 *
 * Using the orginal code on PHP 4.4.4 on WinXP Pro
 * I was getting the following error:
 *
 * "The email address for the business is not present in the encrypted blob.
 * Please contact your merchant"
 *
 * I modified and cleaned up a few things to resolve the error - this was
 * tested on PHP 4.4.4 + OpenSSL on WinXP Pro
 *
 * Modified 2008 by tf@1agency.de for PHP5 and PHPDoc
 *
 * 2008-03-26 Modified for usage with PHP4, Textpattern and
 * a little extended error handling
 * 
 * @copyright Ivor Durham <ivor.durham@ivor.cc>
 * @copyright PayPal_Ahmad  (Nov. 04, 2006)
 * @copyright Unknown Modifier 
 * @copyright Thomas Foerster <tf@1agency.de>
 * @copyright Tommy Schmucker 
 * @package PayPal
 */

  class PayPalEWP
  {
    var $certificate;           // Certificate resource
    var $certificateFile;       // Path to the certificate file
    var $privateKey;            // Private key resource (matching certificate)
    var $privateKeyFile;        // Path to the private key file
    var $paypalCertificate;     // PayPal public certificate resource
    var $paypalCertificateFile; // Path to PayPal public certificate file
    var $certificateID;         // ID assigned by PayPal to the $certificate.
    var $tempFileDirectory;
    var $error;

    /**
     * Constructor
     *
     */
    function __PayPalEWP()
    {
        $this->error = 0;
    }

    /**
     * Sets the ID assigned by PayPal to the client certificate
     *
     * @param string $id The certificate ID assigned when the certificate
     * was uploaded to PayPal
     */
    function setCertificateID($id)
    {
      if ($id != '')
      {
        $this->certificateID = $id;
      }
      else
      {
        $this->error = 1;
      }
    }

    /**
     * Set the client certificate and private key pair.
     *
     * @param string $certificateFilename The path to the client
     * (public) certificate
     * @param string $privateKeyFilename The path to the private key
     * corresponding to the certificate
     * @return bool TRUE if the private key matches the certificate,
     * FALSE otherwise
     */
    function setCertificate($certificateFilename, $privateKeyFilename)
    {
     if (is_readable($certificateFilename) and is_readable($privateKeyFilename))
     {
        $handle = fopen($certificateFilename, "r");
        if ($handle === false)
        {
          return false;
        }

        $size = filesize($certificateFilename);
        $certificate = fread($handle, $size);
        @fclose($handle);

        unset($handle);

        $handle = fopen($privateKeyFilename, "r");
        if ($handle === false)
        {
          return false;
        }

        $size = filesize($privateKeyFilename);
        $privateKey = fread($handle, $size);
        @fclose($handle);

        if (($certificate !== false) and ($privateKey !== false) and openssl_x509_check_private_key($certificate, $privateKey))
        {
          $this->certificate     = $certificate;
          $this->certificateFile = $certificateFilename;
          $this->privateKey      = $privateKey;
          $this->privateKeyFile  = $privateKeyFilename;
          return true;
        }
      }
      else
      {
        $this->error = 2;
        return false;
      }
    }

    /**
     * Sets the PayPal certificate
     *
     * @param string $fileName The path to the PayPal certificate
     * @return bool TRUE if the certificate is read successfully,
     * FALSE otherwise.
     */
    function setPayPalCertificate($fileName)
    {
      if (is_readable($fileName))
      {
        $handle = fopen($fileName, "r");
        if ($handle === false)
        {
          return false;
        }

        $size = filesize($fileName);
        $certificate = fread($handle, $size);
        if ($certificate === false)
        {
          return false;
        }

        fclose($handle);

        $this->paypalCertificate     = $certificate;
        $this->paypalCertificateFile = $fileName;

        return true;
      }
      else
      {
        $this->error = 3;
        return false;
      }
    }

    /**
     * Using the previously set certificates and the tempFileDirectory to
     * encrypt the button information
     *
     * @param array $parameters Array with parameter names as keys
     * @return mixed The encrypted string OR false
     */
    function encryptButton($parameters)
    {
      // Check encryption data is available.
      if (($this->certificateID == '') or !isset($this->certificate) or !isset($this->paypalCertificate))
      {
        return false;
      }

      $clearText = '';
      $encryptedText = '';

      // initialize data.
      $data = "cert_id=" . $this->certificateID . "\n";

      foreach($parameters as $k => $v)
      {
        $d[] = "$k=$v";
      }

      $data .= join("\n", $d);

      $dataFile = tempnam($this->tempFileDirectory, 'data');

      $out = fopen("{$dataFile}_data.txt", 'wb');
      fwrite($out, $data);
      fclose($out);

      $out = fopen("{$dataFile}_signed.txt", "w+"); 

      if (!openssl_pkcs7_sign("{$dataFile}_data.txt", "{$dataFile}_signed.txt", $this->certificate, $this->privateKey, array(), PKCS7_BINARY))
      {
        $this->error = 4;
        return false;
      }

      fclose($out);

      $signedData = explode("\n\n", file_get_contents("{$dataFile}_signed.txt"));

      $out = fopen("{$dataFile}_signed.txt", 'wb');
      fwrite($out, base64_decode($signedData[1]));
      fclose($out);

      if (!openssl_pkcs7_encrypt("{$dataFile}_signed.txt", "{$dataFile}_encrypted.txt", $this->paypalCertificate, array(), PKCS7_BINARY))
      {
        $this->error = 4;
        return false;
      }

      $encryptedData = explode("\n\n", file_get_contents("{$dataFile}_encrypted.txt"));

      $encryptedText = $encryptedData[1];

      @unlink($dataFile);  
      @unlink("{$dataFile}_data.txt");
      @unlink("{$dataFile}_signed.txt");
      @unlink("{$dataFile}_encrypted.txt");

      return "-----BEGIN PKCS7-----\n".$encryptedText."\n-----END PKCS7-----";
    }
  }
}


?>