Advertisement
Guest User

Untitled

a guest
Jun 10th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. <?php
  2. // -- Configuration Section --
  3. // These variables must be set with the URL of Tradologic B2B API and the affiliate's credentials
  4. $b2bApiUrl = 'https://b2b-api.tradologic.net/';
  5. $accountId = 572;
  6. $affiliateUsername = 'Morgan';
  7. $authenticationKey = '123456789';
  8.  
  9. // These variables are used for configuration of the redirection to the brand's cashier page
  10. $redirectToCashier = true; // Indicates if a redirect to the brand's cashier page is performed after the new user is registered
  11. $brandUrl = 'http://www.binaryoptionsbrand.com'; // The brand URL is needed only when $redirectToCashier = true
  12. // -- End of Configuration Section --
  13.  
  14. $userRegistrationResult = sendUserRegistrationRequest($b2bApiUrl, $accountId, $affiliateUsername, $authenticationKey);
  15. $isUserRegistered = $userRegistrationResult['isUserRegistered'];
  16. $message = $userRegistrationResult['message'];
  17. $displayForm = $isUserRegistered ? 'none' : 'inline';
  18.  
  19. if ($redirectToCashier && $isUserRegistered && $userRegistrationResult['sessionId']) {
  20. $redirectUrl = trim($brandUrl, '/') . '/cashier/?hash=' . $userRegistrationResult['sessionId'];
  21. header('Location: ' . $redirectUrl);
  22. }
  23.  
  24. function sendUserRegistrationRequest($baseUrl, $accountId, $affiliateUsername, $authenticationKey)
  25. {
  26. $hasErrors = false;
  27. $message = '';
  28.  
  29. if ($_POST) {
  30. $email = trim($_POST['email']);
  31. $userPassword = trim($_POST['userPassword']);
  32. $userFirstName = trim($_POST['userFirstName']);
  33. $userLastName = trim($_POST['userLastName']);
  34. $phone = trim($_POST['phone']);
  35.  
  36. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  37. $hasErrors = true;
  38. $message = $message . 'The email is invalid.<BR/>';
  39. }
  40.  
  41. if (mb_strlen($userPassword, 'UTF-8') < 6) {
  42. $hasErrors = true;
  43. $message = $message . 'The password must be at least 6 symbols long.<BR/>';
  44. }
  45.  
  46. if (empty($userFirstName)) {
  47. $hasErrors = true;
  48. $message = $message . 'The first name is empty.<BR/>';
  49. }
  50.  
  51. if (empty($userLastName)) {
  52. $hasErrors = true;
  53. $message = $message . 'The last name is empty.<BR/>';
  54. }
  55.  
  56. if (empty($phone)) {
  57. $hasErrors = true;
  58. $message = $message . 'The phone is empty.<BR/>';
  59. }
  60.  
  61. if (!$hasErrors) {
  62. $dataParameters = [
  63. 'email' => $email,
  64. 'userPassword' => $userPassword,
  65. 'userFirstName' => $userFirstName,
  66. 'userLastName' => $userLastName,
  67. 'phone' => $phone,
  68. 'affiliateUsername' => $affiliateUsername,
  69. 'accountId' => $accountId
  70. ];
  71.  
  72. $ipAddress = getClientIp();
  73. if ($ipAddress != 'UNKNOWN') {
  74. $dataParameters['userIpAddress'] = $ipAddress;
  75. }
  76.  
  77. $json = sendPostRequest($baseUrl, "/v1/affiliate/users", $dataParameters, $authenticationKey);
  78. $hasErrors = $json -> {'messageType'} != "Registration_SuccessfulWithSession";
  79. $message = $json -> {'messageText'} . '<BR/>';
  80. }
  81. }
  82. else {
  83. $hasErrors = true;
  84. }
  85.  
  86. $result = [
  87. 'isUserRegistered' => !$hasErrors,
  88. 'message' => $message
  89. ];
  90.  
  91. if (!$hasErrors) {
  92. $result['sessionId'] = $json -> {'messageParameters'} -> {'sessionId'};
  93. }
  94.  
  95. return $result;
  96. }
  97.  
  98. function sendPostRequest($baseUrl, $resourceRoute, $dataParameters, $authenticationKey)
  99. {
  100. $checksum = getChecksum($dataParameters, $authenticationKey);
  101. $dataParameters['checksum'] = $checksum;
  102. $postData = json_encode($dataParameters);
  103.  
  104. $url = trim($baseUrl, '/') . '/' . trim($resourceRoute, '/');
  105.  
  106. $curl = curl_init();
  107. curl_setopt($curl, CURLOPT_URL, $url);
  108.  
  109. // Override the default headers
  110. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  111.  
  112. // 0 do not include header in output, 1 include header in output
  113. curl_setopt($curl, CURLOPT_HEADER, 0);
  114.  
  115. // Set the post variables
  116. curl_setopt($curl, CURLOPT_POST, 1);
  117. curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
  118.  
  119. // Set so curl_exec returns the result instead of outputting it.
  120. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  121.  
  122. // Get the response and close the channel.
  123. $response = curl_exec($curl);
  124. curl_close($curl);
  125.  
  126. $jsonResponse = json_decode($response);
  127. return $jsonResponse;
  128. }
  129.  
  130. function getChecksum($parameters, $authenticationKey)
  131. {
  132. ksort($parameters);
  133.  
  134. $dataString = '';
  135. foreach ($parameters as $value) {
  136. $dataString = $dataString . $value;
  137. }
  138.  
  139. $dataString = $dataString . $authenticationKey;
  140. $checksum = hash('sha256', $dataString);
  141. return $checksum;
  142. }
  143.  
  144. function getClientIp() {
  145. $ipAddress = '';
  146. if ($_SERVER['HTTP_CLIENT_IP']) {
  147. $ipAddress = $_SERVER['HTTP_CLIENT_IP'];
  148. }
  149. else if($_SERVER['HTTP_X_FORWARDED_FOR']) {
  150. $ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
  151. }
  152. else if($_SERVER['HTTP_X_FORWARDED']) {
  153. $ipAddress = $_SERVER['HTTP_X_FORWARDED'];
  154. }
  155. else if($_SERVER['HTTP_FORWARDED_FOR']) {
  156. $ipAddress = $_SERVER['HTTP_FORWARDED_FOR'];
  157. }
  158. else if($_SERVER['HTTP_FORWARDED']) {
  159. $ipAddress = $_SERVER['HTTP_FORWARDED'];
  160. }
  161. else if($_SERVER['REMOTE_ADDR']) {
  162. $ipAddress = $_SERVER['REMOTE_ADDR'];
  163. }
  164. else {
  165. $ipAddress = 'UNKNOWN';
  166. }
  167.  
  168. return $ipAddress;
  169. }
  170. ?>
  171.  
  172. <!DOCTYPE html>
  173. <html>
  174. <head>
  175. <title>User Registration</title>
  176. <meta charset="UTF-8">
  177. </head>
  178. <body>
  179. <div><?=$message?></div>
  180. <form method="POST" style="display:<?=$displayForm?>">
  181. <label for="email">Email:</label><input type="text" name="email" id="email" /><br/>
  182. <label for="userPassword">Password:</label><input type="password" name="userPassword" id="userPassword" /><br/>
  183. <label for="userFirstName">First name:</label><input type="text" name="userFirstName" id="userFirstName" /><br/>
  184. <label for="userLastName">Last name:</label><input type="text" name="userLastName" id="userLastName" /><br/>
  185. <label for="phone">Phone:</label><input type="text" name="phone" id="phone" /><br/>
  186. <input type="submit" value="Register" />
  187. </form>
  188. </body>
  189. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement