PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/zabbix-2.0.1/frontends/php/include/classes/class.curl.php

#
PHP | 312 lines | 237 code | 50 blank | 25 comment | 36 complexity | bcf1d78cb314ba96847189524b23ef8c MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. <?php
  2. /*
  3. ** Zabbix
  4. ** Copyright (C) 2000-2011 Zabbix SIA
  5. **
  6. ** This program is free software; you can redistribute it and/or modify
  7. ** it under the terms of the GNU General Public License as published by
  8. ** the Free Software Foundation; either version 2 of the License, or
  9. ** (at your option) any later version.
  10. **
  11. ** This program is distributed in the hope that it will be useful,
  12. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ** GNU General Public License for more details.
  15. **
  16. ** You should have received a copy of the GNU General Public License
  17. ** along with this program; if not, write to the Free Software
  18. ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. **/
  20. class Curl {
  21. private $url;
  22. protected $port;
  23. protected $host;
  24. protected $protocol;
  25. protected $username;
  26. protected $password;
  27. protected $file;
  28. protected $reference;
  29. protected $path;
  30. protected $query;
  31. protected $arguments;
  32. public function __construct($url = null) {
  33. $this->url = null;
  34. $this->port = null;
  35. $this->host = null;
  36. $this->protocol = null;
  37. $this->username = null;
  38. $this->password = null;
  39. $this->file = null;
  40. $this->reference = null;
  41. $this->path = null;
  42. $this->query = null;
  43. $this->arguments = array();
  44. if (empty($url)) {
  45. $this->formatGetArguments();
  46. $protocol = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || $_SERVER['SERVER_PORT'] == 443) ? 'https' : 'http';
  47. $this->url = $url = $protocol.'://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['SCRIPT_NAME'].'?'.$this->getQuery();
  48. }
  49. else {
  50. $this->url = $url;
  51. $tmp_pos = zbx_strpos($this->url, '?');
  52. $this->query = ($tmp_pos !== false) ? substr($this->url, $tmp_pos + 1) : '';
  53. $tmp_pos = zbx_strpos($this->query, '#');
  54. if ($tmp_pos !== false) {
  55. $this->query = zbx_substring($this->query, 0, $tmp_pos);
  56. }
  57. $this->formatArguments($this->query);
  58. }
  59. $protocolSepIndex=zbx_strpos($this->url, '://');
  60. if ($protocolSepIndex !== false) {
  61. $this->protocol = zbx_strtolower(zbx_substring($this->url, 0, $protocolSepIndex));
  62. $this->host = substr($this->url, $protocolSepIndex + 3);
  63. $tmp_pos = zbx_strpos($this->host, '/');
  64. if ($tmp_pos !== false) {
  65. $this->host = zbx_substring($this->host, 0, $tmp_pos);
  66. }
  67. $atIndex = zbx_strpos($this->host, '@');
  68. if ($atIndex !== false) {
  69. $credentials = zbx_substring($this->host, 0, $atIndex);
  70. $colonIndex = zbx_strpos(credentials, ':');
  71. if ($colonIndex !== false) {
  72. $this->username = zbx_substring($credentials, 0, $colonIndex);
  73. $this->password = substr($credentials, $colonIndex);
  74. }
  75. else {
  76. $this->username = $credentials;
  77. }
  78. $this->host = substr($this->host, $atIndex + 1);
  79. }
  80. $host_ipv6 = zbx_strpos($this->host, ']');
  81. if ($host_ipv6 !== false) {
  82. if ($host_ipv6 < (zbx_strlen($this->host) - 1)) {
  83. $host_ipv6++;
  84. $host_less = substr($this->host, $host_ipv6);
  85. $portColonIndex = zbx_strpos($host_less, ':');
  86. if ($portColonIndex !== false) {
  87. $this->host = zbx_substring($this->host, 0, $host_ipv6);
  88. $this->port = substr($host_less, $portColonIndex + 1);
  89. }
  90. }
  91. }
  92. else {
  93. $portColonIndex = zbx_strpos($this->host, ':');
  94. if ($portColonIndex !== false) {
  95. $this->port = substr($this->host, $portColonIndex+1);
  96. $this->host = zbx_substring($this->host, 0, $portColonIndex);
  97. }
  98. }
  99. $this->file = substr($this->url, $protocolSepIndex + 3);
  100. $this->file = substr($this->file, zbx_strpos($this->file, '/'));
  101. if ($this->file == $this->host) {
  102. $this->file = '';
  103. }
  104. }
  105. else {
  106. $this->file = $this->url;
  107. }
  108. $tmp_pos = zbx_strpos($this->file, '?');
  109. if ($tmp_pos !== false) {
  110. $this->file = zbx_substring($this->file, 0, $tmp_pos);
  111. }
  112. $refSepIndex = zbx_strpos($url, '#');
  113. if ($refSepIndex !== false) {
  114. $this->file = zbx_substring($this->file, 0, $refSepIndex);
  115. $this->reference = substr($url, zbx_strpos($url, '#') + 1);
  116. }
  117. $this->path = $this->file;
  118. if (zbx_strlen($this->query) > 0) {
  119. $this->file .= '?'.$this->query;
  120. }
  121. if (zbx_strlen($this->reference) > 0) {
  122. $this->file .= '#'.$this->reference;
  123. }
  124. if (isset($_COOKIE['zbx_sessionid'])) {
  125. $this->setArgument('sid', substr($_COOKIE['zbx_sessionid'], 16, 16));
  126. }
  127. }
  128. public function formatQuery() {
  129. $query = array();
  130. foreach ($this->arguments as $key => $value) {
  131. if (is_null($value)) {
  132. continue;
  133. }
  134. if (is_array($value)) {
  135. foreach ($value as $vkey => $vvalue) {
  136. if (is_array($vvalue)) {
  137. continue;
  138. }
  139. $query[] = $key.'['.$vkey.']='.rawurlencode($vvalue);
  140. }
  141. }
  142. else {
  143. $query[] = $key.'='.rawurlencode($value);
  144. }
  145. }
  146. $this->query = implode('&', $query);
  147. }
  148. public function formatGetArguments() {
  149. $this->arguments = $_GET;
  150. if (isset($_COOKIE['zbx_sessionid'])) {
  151. $this->setArgument('sid', substr($_COOKIE['zbx_sessionid'], 16, 16));
  152. }
  153. $this->formatQuery();
  154. }
  155. public function formatArguments($query = null) {
  156. if (is_null($query)) {
  157. $this->arguments = $_REQUEST;
  158. }
  159. else {
  160. $args = explode('&', $query);
  161. foreach ($args as $id => $arg) {
  162. if (empty($arg)) {
  163. continue;
  164. }
  165. if (strpos($arg, '=') !== false) {
  166. list($name, $value) = explode('=', $arg);
  167. $this->arguments[$name] = isset($value) ? urldecode($value) : '';
  168. }
  169. else {
  170. $this->arguments[$arg] = '';
  171. }
  172. }
  173. }
  174. $this->formatQuery();
  175. }
  176. public function getUrl() {
  177. $this->formatQuery();
  178. $url = $this->protocol ? $this->protocol.'://' : '';
  179. $url .= $this->username ? $this->username : '';
  180. $url .= $this->password ? ':'.$this->password : '';
  181. $url .= $this->username || $this->password ? '@' : '';
  182. $url .= $this->host ? $this->host : '';
  183. $url .= $this->port ? ':'.$this->port : '';
  184. $url .= $this->path ? $this->path : '';
  185. $url .= $this->query ? '?'.$this->query : '';
  186. $url .= $this->reference ? '#'.urlencode($this->reference) : '';
  187. return $url;
  188. }
  189. public function setPort($port) {
  190. $this->port = $port;
  191. }
  192. public function getPort() {
  193. return $this->port;
  194. }
  195. public function setArgument($key, $value='') {
  196. $this->arguments[$key] = $value;
  197. }
  198. public function getArgument($key) {
  199. return isset($this->arguments[$key]) ? $this->arguments[$key] : null;
  200. }
  201. public function setQuery($query) {
  202. $this->query = $query;
  203. $this->formatArguments();
  204. $this->formatQuery();
  205. }
  206. public function getQuery() {
  207. $this->formatQuery();
  208. return $this->query;
  209. }
  210. public function setProtocol($protocol) {
  211. $this->protocol = $protocol;
  212. }
  213. // returns the protocol of $this URL, i.e. 'http' in the url 'http://server/'
  214. public function getProtocol() {
  215. return $this->protocol;
  216. }
  217. public function setHost($host) {
  218. $this->host = $host;
  219. }
  220. // returns the host name of $this URL, i.e. 'server.com' in the url 'http://server.com/'
  221. public function getHost() {
  222. return $this->host;
  223. }
  224. public function setUserName($username) {
  225. $this->username = $username;
  226. }
  227. // returns the user name part of $this URL, i.e. 'joe' in the url 'http://joe@server.com/'
  228. public function getUserName() {
  229. return $this->username;
  230. }
  231. public function setPassword($password) {
  232. $this->password = $password;
  233. }
  234. // returns the password part of $this url, i.e. 'secret' in the url 'http://joe:secret@server.com/'
  235. public function getPassword() {
  236. return $this->password;
  237. }
  238. // returns the file part of $this url, i.e. everything after the host name.
  239. public function getFile() {
  240. $url .= $this->path ? $this->path : '';
  241. $url .= $this->query ? '?'.$this->query : '';
  242. $url .= $this->reference ? '#'.urlencode($this->reference) : '';
  243. return $url;
  244. }
  245. public function setReference($reference) {
  246. $this->reference = $reference;
  247. }
  248. // returns the reference of $this url, i.e. 'bookmark' in the url 'http://server/file.html#bookmark'
  249. public function getReference() {
  250. return $this->reference;
  251. }
  252. public function setPath($path) {
  253. $this->path = $path;
  254. }
  255. // returns the file path of $this url, i.e. '/dir/file.html' in the url 'http://server/dir/file.html'
  256. public function getPath() {
  257. return $this->path;
  258. }
  259. public function toString() {
  260. return $this->getUrl();
  261. }
  262. }