PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/paypal/api/paypal_connect.php

https://gitlab.com/staging06/myproject
PHP | 140 lines | 89 code | 24 blank | 27 comment | 15 complexity | 90468597bd1fa938046fe488533abf75 MD5 | raw file
  1. <?php
  2. /**
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Academic Free License (AFL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/afl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class PayPalConnect
  27. {
  28. private $_logs = array();
  29. private $paypal = null;
  30. public function __construct()
  31. {
  32. $this->paypal = new PayPal();
  33. }
  34. public function makeConnection($host, $script, $body, $simple_mode = false, $http_header = false, $identify = false)
  35. {
  36. $this->_logs[] = $this->paypal->l('Making new connection to').' \''.$host.$script.'\'';
  37. if (function_exists('curl_exec'))
  38. $return = $this->_connectByCURL($host.$script, $body, $http_header, $identify);
  39. if (isset($return) && $return)
  40. return $return;
  41. $tmp = $this->_connectByFSOCK($host, $script, $body);
  42. if (!$simple_mode || !preg_match('/[A-Z]+=/', $tmp, $result))
  43. return $tmp;
  44. return Tools::substr($tmp, strpos($tmp, $result[0]));
  45. }
  46. public function getLogs()
  47. {
  48. return $this->_logs;
  49. }
  50. /************************************************************/
  51. /********************** CONNECT METHODS *********************/
  52. /************************************************************/
  53. private function _connectByCURL($url, $body, $http_header = false, $identify = false)
  54. {
  55. $ch = @curl_init();
  56. if (!$ch)
  57. $this->_logs[] = $this->paypal->l('Connect failed with CURL method');
  58. else
  59. {
  60. $this->_logs[] = $this->paypal->l('Connect with CURL method successful');
  61. $this->_logs[] = '<b>'.$this->paypal->l('Sending this params:').'</b>';
  62. $this->_logs[] = $body;
  63. @curl_setopt($ch, CURLOPT_URL, 'https://'.$url);
  64. if ($identify)
  65. @curl_setopt($ch, CURLOPT_USERPWD, Configuration::get('PAYPAL_LOGIN_CLIENT_ID').':'.Configuration::get('PAYPAL_LOGIN_SECRET'));
  66. @curl_setopt($ch, CURLOPT_POST, true);
  67. if ($body)
  68. @curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  69. @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  70. @curl_setopt($ch, CURLOPT_HEADER, false);
  71. @curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  72. @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  73. @curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  74. @curl_setopt($ch, CURLOPT_SSLVERSION, defined('CURL_SSLVERSION_TLSv1') ? CURL_SSLVERSION_TLSv1 : 1);
  75. @curl_setopt($ch, CURLOPT_VERBOSE, false);
  76. if ($http_header)
  77. @curl_setopt($ch, CURLOPT_HTTPHEADER, $http_header);
  78. $result = @curl_exec($ch);
  79. if (!$result)
  80. $this->_logs[] = $this->paypal->l('Send with CURL method failed ! Error:').' '.curl_error($ch);
  81. else
  82. $this->_logs[] = $this->paypal->l('Send with CURL method successful');
  83. @curl_close($ch);
  84. }
  85. return $result ? $result : false;
  86. }
  87. private function _connectByFSOCK($host, $script, $body)
  88. {
  89. $fp = @fsockopen('tls://'.$host, 443, $errno, $errstr, 4);
  90. if (!$fp)
  91. $this->_logs[] = $this->paypal->l('Connect failed with fsockopen method');
  92. else
  93. {
  94. $header = $this->_makeHeader($host, $script, Tools::strlen($body));
  95. $this->_logs[] = $this->paypal->l('Sending this params:').' '.$header.$body;
  96. @fputs($fp, $header.$body);
  97. $tmp = '';
  98. while (!feof($fp))
  99. $tmp .= trim(fgets($fp, 1024));
  100. fclose($fp);
  101. if (!isset($tmp) || $tmp == false)
  102. $this->_logs[] = $this->paypal->l('Send with fsockopen method failed !');
  103. else
  104. $this->_logs[] = $this->paypal->l('Send with fsockopen method successful');
  105. }
  106. return isset($tmp) ? $tmp : false;
  107. }
  108. private function _makeHeader($host, $script, $lenght)
  109. {
  110. return 'POST '.(string)$script.' HTTP/1.1'."\r\n".
  111. 'Host: '.(string)$host."\r\n".
  112. 'Content-Type: application/x-www-form-urlencoded'."\r\n".
  113. 'Content-Length: '.(int)$lenght."\r\n".
  114. 'Connection: close'."\r\n\r\n";
  115. }
  116. }