PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/modules/pages/gv_send/header_php.php

https://bitbucket.org/archee/hejnarphotostore
PHP | 214 lines | 137 code | 42 blank | 35 comment | 29 complexity | aba5bdb4e6d2eb96cd2da48f5276b425 MD5 | raw file
  1. <?php
  2. /**
  3. * GV Send
  4. *
  5. * Used to allow customer to send GV to their friends/family by way of email.
  6. * They can send up to the amount of GV accumlated in their account by way of purchased GV's or GV's sent to them.
  7. *
  8. * @package page
  9. * @copyright Copyright 2003-2010 Zen Cart Development Team
  10. * @copyright Portions Copyright 2003 osCommerce
  11. * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
  12. * @version $Id: header_php.php 15880 2010-04-11 16:24:30Z wilt $
  13. */
  14. // This should be first line of the script:
  15. $zco_notifier->notify('NOTIFY_HEADER_START_GV_SEND');
  16. require_once('includes/classes/http_client.php');
  17. // verify no timeout has occurred on the send or process
  18. if (!$_SESSION['customer_id'] and ($_GET['action'] == 'send' or $_GET['action'] == 'process')) {
  19. zen_redirect(zen_href_link(FILENAME_TIME_OUT));
  20. }
  21. // if the customer is not logged on, redirect them to the login page
  22. if (!$_SESSION['customer_id']) {
  23. $_SESSION['navigation']->set_snapshot();
  24. zen_redirect(zen_href_link(FILENAME_LOGIN, '', 'SSL'));
  25. }
  26. require(DIR_WS_MODULES . zen_get_module_directory('require_languages.php'));
  27. if (isset($_POST['edit_x']) || isset($_POST['edit_y'])) {
  28. $_GET['action'] = 'send';
  29. }
  30. // extract sender's name+email from database, since logged-in customer is the one who is sending this GV email
  31. $account_query = "SELECT customers_firstname, customers_lastname, customers_email_address
  32. FROM " . TABLE_CUSTOMERS . "
  33. WHERE customers_id = :customersID";
  34. $account_query = $db->bindVars($account_query, ':customersID', $_SESSION['customer_id'], 'integer');
  35. $account = $db->Execute($account_query);
  36. $send_name = $account->fields['customers_firstname'] . ' ' . $account->fields['customers_lastname'];
  37. $send_firstname = $account->fields['customers_firstname'];
  38. $gv_query = "SELECT amount
  39. FROM " . TABLE_COUPON_GV_CUSTOMER . "
  40. WHERE customer_id = :customersID";
  41. $gv_query = $db->bindVars($gv_query, ':customersID', $_SESSION['customer_id'], 'integer');
  42. $gv_result = $db->Execute($gv_query);
  43. // Sanity Check
  44. // Some stuff for debugging
  45. // First let's get the local and base for how much the customer has in his GV account
  46. // The customer_gv account is always stored in the store's base currency
  47. // $local_customer_gv = $currencies->value($gv_result->fields['amount']);
  48. // $base_customer_gv = $gv_result->fields['amount'];
  49. // Now let's get the amount that the customer wants to send.
  50. // $local_customer_send = $_POST['amount'];
  51. // $base_customer_send = $currencies->value($_POST['amount'], true, DEFAULT_CURRENCY);
  52. if ($_GET['action'] == 'send') {
  53. $_SESSION['complete'] = '';
  54. $error = false;
  55. if (isset($_POST['edit_x']) || isset($_POST['edit_y'])) {
  56. $error = true;
  57. }
  58. if (!isset($_POST['to_name']) || trim($_POST['to_name']=='')) {
  59. $error = true;
  60. $messageStack->add('gv_send', ERROR_ENTRY_TO_NAME_CHECK, 'error');
  61. }
  62. if (!zen_validate_email(trim($_POST['email']))) {
  63. $error = true;
  64. $messageStack->add('gv_send', ERROR_ENTRY_EMAIL_ADDRESS_CHECK, 'error');
  65. }
  66. $customer_amount = $gv_result->fields['amount'];
  67. $_POST['amount'] = str_replace('$', '', $_POST['amount']);
  68. $gv_amount = trim($_POST['amount']);
  69. if (preg_match('/[^0-9\.]/', $gv_amount)) {
  70. $error = true;
  71. $messageStack->add('gv_send', ERROR_ENTRY_AMOUNT_CHECK, 'error');
  72. }
  73. if ( $currencies->value($gv_amount, true,DEFAULT_CURRENCY) > $customer_amount || $gv_amount == 0) {
  74. //echo $currencies->value($customer_amount, true,DEFAULT_CURRENCY);
  75. $error = true;
  76. $messageStack->add('gv_send', ERROR_ENTRY_AMOUNT_CHECK, 'error');
  77. }
  78. }
  79. if ($_GET['action'] == 'process') {
  80. if (!isset($_POST['back'])) { // customer didn't click the back button
  81. $id1 = zen_create_coupon_code($mail['customers_email_address']);
  82. // sanitize and remove non-numeric characters
  83. $_POST['amount'] = preg_replace('/[^0-9.%]/', '', $_POST['amount']);
  84. $new_amount = $gv_result->fields['amount'] - $currencies->value($_POST['amount'], true, DEFAULT_CURRENCY);
  85. //die($currencies->value($_POST['amount'], true, $_SESSION['currency']));
  86. $new_db_amount = $gv_result->fields['amount'] - $currencies->value($_POST['amount'], true, DEFAULT_CURRENCY);
  87. if ($new_amount < 0) {
  88. $error= true;
  89. $messageStack->add('gv_send', ERROR_ENTRY_AMOUNT_CHECK, 'error');
  90. $_GET['action'] = 'send';
  91. } else {
  92. $_GET['action'] = 'complete';
  93. $gv_query="UPDATE " . TABLE_COUPON_GV_CUSTOMER . "
  94. SET amount = '" . $new_amount . "'
  95. WHERE customer_id = :customersID";
  96. $gv_query = $db->bindVars($gv_query, ':customersID', $_SESSION['customer_id'], 'integer');
  97. $db->Execute($gv_query);
  98. $gv_query="INSERT INTO " . TABLE_COUPONS . " (coupon_type, coupon_code, date_created, coupon_amount)
  99. VALUES ('G', :couponCode, NOW(), :amount)";
  100. $gv_query = $db->bindVars($gv_query, ':couponCode', $id1, 'string');
  101. $gv_query = $db->bindVars($gv_query, ':amount', $currencies->value($_POST['amount'], true, DEFAULT_CURRENCY), 'currency');
  102. $gv = $db->Execute($gv_query);
  103. $insert_id = $db->Insert_ID();
  104. $gv_query="INSERT INTO " . TABLE_COUPON_EMAIL_TRACK . "(coupon_id, customer_id_sent, sent_firstname, sent_lastname, emailed_to, date_sent)
  105. VALUES (:insertID, :customersID, :firstname, :lastname, :email, now())";
  106. $gv_query = $db->bindVars($gv_query, ':insertID', $insert_id, 'integer');
  107. $gv_query = $db->bindVars($gv_query, ':customersID', $_SESSION['customer_id'], 'integer');
  108. $gv_query = $db->bindVars($gv_query, ':firstname', $account->fields['customers_firstname'], 'string');
  109. $gv_query = $db->bindVars($gv_query, ':lastname', $account->fields['customers_lastname'], 'string');
  110. $gv_query = $db->bindVars($gv_query, ':email', $_POST['email'], 'string');
  111. $db->Execute($gv_query);
  112. // build email content:
  113. $gv_email = STORE_NAME . "\n" .
  114. EMAIL_SEPARATOR . "\n" .
  115. sprintf(EMAIL_GV_TEXT_HEADER, $currencies->format($_POST['amount'], false)) . "\n" .
  116. EMAIL_SEPARATOR . "\n\n" .
  117. sprintf(EMAIL_GV_FROM, $send_name) . "\n";
  118. $html_msg['EMAIL_GV_TEXT_HEADER'] = sprintf(EMAIL_GV_TEXT_HEADER, '');
  119. $html_msg['EMAIL_GV_AMOUNT'] = $currencies->format($_POST['amount'], false);
  120. $html_msg['EMAIL_GV_FROM'] = sprintf(EMAIL_GV_FROM, $send_name) ;
  121. if (isset($_POST['message'])) {
  122. $gv_email .= EMAIL_GV_MESSAGE . "\n\n";
  123. $html_msg['EMAIL_GV_MESSAGE'] = EMAIL_GV_MESSAGE . '<br />';
  124. if (isset($_POST['to_name'])) {
  125. $gv_email .= sprintf(EMAIL_GV_SEND_TO, $_POST['to_name']) . "\n\n";
  126. $html_msg['EMAIL_GV_SEND_TO'] = '<tt>'.sprintf(EMAIL_GV_SEND_TO, $_POST['to_name']). '</tt><br />';
  127. }
  128. $gv_email .= stripslashes($_POST['message']) . "\n\n";
  129. $gv_email .= EMAIL_SEPARATOR . "\n\n";
  130. $html_msg['EMAIL_MESSAGE_HTML'] = stripslashes($_POST['message']);
  131. }
  132. $html_msg['GV_REDEEM_HOW'] = sprintf(EMAIL_GV_REDEEM, '<strong>' . $id1 . '</strong>');
  133. $html_msg['GV_REDEEM_URL'] = '<a href="'.zen_href_link(FILENAME_GV_REDEEM, 'gv_no=' . $id1, 'NONSSL', false).'">'.EMAIL_GV_LINK.'</a>';
  134. $html_msg['GV_REDEEM_CODE'] = $id1;
  135. $gv_email .= sprintf(EMAIL_GV_REDEEM, $id1) . "\n\n";
  136. $gv_email .= EMAIL_GV_LINK . ' ' . zen_href_link(FILENAME_GV_REDEEM, 'gv_no=' . $id1, 'NONSSL', false);
  137. $gv_email .= "\n\n";
  138. $gv_email .= EMAIL_GV_FIXED_FOOTER . "\n\n";
  139. $gv_email .= EMAIL_GV_SHOP_FOOTER;
  140. $gv_email_subject = sprintf(EMAIL_GV_TEXT_SUBJECT, $send_name);
  141. // include disclaimer
  142. $gv_email .= "\n\n" . EMAIL_ADVISORY . "\n\n";
  143. $html_msg['EMAIL_GV_FIXED_FOOTER'] = str_replace(array("\r\n", "\n", "\r", "-----"), '', EMAIL_GV_FIXED_FOOTER);
  144. $html_msg['EMAIL_GV_SHOP_FOOTER'] = EMAIL_GV_SHOP_FOOTER;
  145. // send the email
  146. zen_mail($_POST['to_name'], $_POST['email'], $gv_email_subject, nl2br($gv_email), STORE_NAME, EMAIL_FROM, $html_msg, 'gv_send');
  147. // send additional emails
  148. if (SEND_EXTRA_GV_CUSTOMER_EMAILS_TO_STATUS == '1' and SEND_EXTRA_GV_CUSTOMER_EMAILS_TO !='') {
  149. $extra_info = email_collect_extra_info(ENTRY_NAME . $_POST['to_name'], ENTRY_EMAIL . $_POST['email'], $send_name , $account->fields['customers_email_address']);
  150. $html_msg['EXTRA_INFO'] = $extra_info['HTML'];
  151. zen_mail('', SEND_EXTRA_GV_CUSTOMER_EMAILS_TO, SEND_EXTRA_GV_CUSTOMER_EMAILS_TO_SUBJECT . ' ' . $gv_email_subject,
  152. $gv_email . $extra_info['TEXT'], STORE_NAME, EMAIL_FROM, $html_msg,'gv_send_extra');
  153. }
  154. // do a fresh calculation after sending an email
  155. $gv_query = "SELECT amount
  156. FROM " . TABLE_COUPON_GV_CUSTOMER . "
  157. WHERE customer_id = :customersID";
  158. $gv_query = $db->bindVars($gv_query, ':customersID', $_SESSION['customer_id'], 'integer');
  159. $gv_result = $db->Execute($gv_query);
  160. }
  161. } else { // customer DID click the back button
  162. $_GET['action'] = '';
  163. }
  164. }
  165. $gv_current_balance = $currencies->format($gv_result->fields['amount']);
  166. if ($_GET['action'] == 'complete') zen_redirect(zen_href_link(FILENAME_GV_SEND, 'action=doneprocess'));
  167. $breadcrumb->add(NAVBAR_TITLE);
  168. // validate entries
  169. $gv_amount = (float)$gv_amount;
  170. // This should be last line of the script:
  171. $zco_notifier->notify('NOTIFY_HEADER_END_GV_SEND');
  172. ?>