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

/modules/core/ssl.inc.php

https://github.com/axxtel/agilebill
PHP | 186 lines | 109 code | 25 blank | 52 comment | 33 complexity | cf2350dde5bec7c4a11a3c67486ee41e MD5 | raw file
  1. <?php
  2. /**
  3. * AgileBill - Open Billing Software
  4. *
  5. * This body of work is free software; you can redistribute it and/or
  6. * modify it under the terms of the Open AgileBill License
  7. * License as published at http://www.agileco.com/agilebill/license1-4.txt
  8. *
  9. * For questions, help, comments, discussion, etc., please join the
  10. * Agileco community forums at http://forum.agileco.com/
  11. *
  12. * @link http://www.agileco.com/
  13. * @copyright 2004-2008 Agileco, LLC.
  14. * @license http://www.agileco.com/agilebill/license1-4.txt
  15. * @author Tony Landis <tony@agileco.com>
  16. * @package AgileBill
  17. * @version 1.4.93
  18. */
  19. # Debug
  20. if(empty($VAR) && empty($VAR['do']) && !defined("PATH_AGILE"))
  21. {
  22. include_once('../../config.inc.php');
  23. $test = new CORE_ssl;
  24. $test->test();
  25. }
  26. class CORE_ssl
  27. {
  28. function CORE_ssl ($type=false)
  29. {
  30. $disabled_functions = ini_get('disable_functions');
  31. if ( defined("PATH_CURL") && is_file(PATH_CURL) )
  32. $this->connect_curl_binary = true;
  33. if ( function_exists('curl_init') && $curl_version = curl_version()) {
  34. if ( phpversion() >= 5 ) {
  35. if (preg_match('/openssl/i', @$curl_version['ssl_version'] ))
  36. $this->connect_curl_module = true;
  37. } else {
  38. if (preg_match('/openssl/i', curl_version()))
  39. $this->connect_curl_module = true;
  40. }
  41. }
  42. if (phpversion() >= '4.3.0')
  43. if (function_exists("fsockopen") )
  44. if (function_exists("openssl_public_decrypt"))
  45. $this->connect_fsockopen = true;
  46. }
  47. # debuging...
  48. function test() {
  49. echo '<textarea cols="50" rows="50">';
  50. if ( @$this->connect_curl_binary ) {
  51. echo 'Using Binary Curl: ';
  52. echo $this->connect('secure.authorize.net', '', '', true, 1);
  53. } elseif ( @$this->connect_curl_module ) {
  54. echo 'Using Curl PHP Module: ';
  55. echo $this->connect('www.amazon.com', '', '', true, 1);
  56. } elseif ( @$this->connect_fsockopen ) {
  57. echo 'Using PHP fsockopen() function + openssl_public_decrypt(): ';
  58. echo $this->connect('www.amazon.com', '', '', true, 1);
  59. } else {
  60. echo 'No SSL functionality!';
  61. }
  62. echo "</textarea>";
  63. }
  64. # type: 1=post 2=get
  65. function connect($host, $url, $vars, $ssl, $type) {
  66. if ( @$this->connect_curl_binary )
  67. return $this->connect_curl_binary ($host, $url, $vars, $ssl, $type);
  68. elseif ( @$this->connect_curl_module )
  69. return $this->connect_curl_module ($host, $url, $vars, $ssl, $type);
  70. elseif ( @$this->connect_fsockopen )
  71. return $this->connect_fsockopen ($host, $url, $vars, $ssl, $type);
  72. else
  73. return false;
  74. }
  75. # SSL connection with Curl Binary
  76. function connect_curl_binary($host, $url, $vars, $ssl, $type) {
  77. if($ssl) $urli = 'https://'.$host .''. $url;
  78. else $urli = 'http://'.$host .''. $url;
  79. $params = "";
  80. if(is_array($vars)) {
  81. for($i=0; $i<count($vars); $i++) {
  82. if ($i > 0) { $params .= '&'; }
  83. $params .= $vars[$i][0].'='.urlencode($vars[$i][1]);
  84. }
  85. } elseif (!empty($vars)) {
  86. $params = $vars;
  87. }
  88. $curl = PATH_CURL;
  89. # connect
  90. if(!empty($params)) {
  91. //echo $curl ."-d -k \"$params\"". $urli;
  92. //echo $urli.'?'.$params;
  93. //exit;
  94. return `$curl -k -d "$params" $urli`;
  95. } else {
  96. //echo "$curl -k $urli";
  97. return `$curl -k $urli`;
  98. }
  99. }
  100. # SSL connection with Curl Module
  101. function connect_curl_module($host, $url, $vars, $ssl, $type) {
  102. if($ssl) $url = 'https://'.$host .''. $url;
  103. else $url = 'http://'.$host .''. $url;
  104. $params = '';
  105. if(is_array($vars)) {
  106. for($i=0; $i<count($vars); $i++) {
  107. if ($i > 0) { $params .= '&'; }
  108. @$params .= $vars[$i][0].'='.urlencode($vars[$i][1]);
  109. }
  110. } elseif (!empty($vars)) {
  111. $params = $vars;
  112. }
  113. $ch = curl_init();
  114. curl_setopt($ch, CURLOPT_POST, 1);
  115. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  116. curl_setopt($ch, CURLOPT_URL, $url);
  117. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  118. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  119. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  120. $result = curl_exec ($ch);
  121. curl_close ($ch);
  122. return $result;
  123. }
  124. # SSL connection with fsockopen()
  125. function connect_fsockopen($host, $url, $vars, $ssl, $type) {
  126. if($ssl) {
  127. $host = 'ssl://'. $host;
  128. $port = 443;
  129. } else {
  130. $port = 80;
  131. }
  132. $fp = @fsockopen ($host, $port, $errno, $errstr, 120);
  133. $ret = "";
  134. @$req = substr ($url, $p);
  135. if ($fp) {
  136. fputs ($fp, "POST $req HTTP/1.1\n");
  137. fputs ($fp, "Accept: */*\n");
  138. fputs ($fp, "Accept-Language: en\n");
  139. fputs ($fp, "Connection: Keep-Alive\n");
  140. fputs ($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)\n");
  141. fputs ($fp, "Content-type: application/x-www-form-urlencoded\n");
  142. $out = "";
  143. for($i=0; $i<count($vars); $i++) {
  144. if($i>0 && !empty($vars[$i][0]))
  145. $out .= '&';
  146. $out .= rawurlencode($vars[$i][0]) .'='. $out .= rawurlencode($vars[$i][1]);
  147. }
  148. $out = trim ($out);
  149. fputs ($fp, "Content-length: ".strlen($out)."\n\n");
  150. fputs ($fp, "$out");
  151. fputs ($fp, "\n");
  152. while(!feof($fp))
  153. $ret .= fgets($fp,128);
  154. fclose ($fp);
  155. } else {
  156. return false;
  157. }
  158. return $ret;
  159. }
  160. }
  161. ?>