PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/baser/plugins/feed/models/web_model.php

https://github.com/hashing/basercms
PHP | 280 lines | 129 code | 53 blank | 98 comment | 19 complexity | 3e3d47cd1e4f4672898473b77800907c MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * A simple WebModel class that eases post & get requests to foreign pages
  4. * Thx to RosSoft (http://rossoft.wordpress.com/) for the initial curl code!
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * Author: Felix Geisendorfer (thinkingphp.org / fg-webdesign.de)
  10. *
  11. */
  12. /**
  13. * Include files
  14. */
  15. /**
  16. * web_model
  17. *
  18. * @package baser.plugins.feed.models
  19. */
  20. class WebModel extends AppModel
  21. {
  22. /**
  23. *
  24. * @var boolean
  25. * @access public
  26. */
  27. var $useTable = false;
  28. /**
  29. *
  30. * @var int
  31. * @access public
  32. */
  33. var $connection_timeout = '30';
  34. /**
  35. * Set this to active if security is very important for your ssl connection, however I had
  36. * (valid) certification that would not pass the curl libraries ssl checks.
  37. *
  38. * @var boolean
  39. * @access public
  40. */
  41. var $ssl_strict = false;
  42. /**
  43. * This Model actually doesn't do any caching itself, this is left to the Models
  44. * that extend this Model.
  45. *
  46. * @var string
  47. * @access public
  48. */
  49. var $cacheFolder = 'web';
  50. /**
  51. * construct
  52. *
  53. * @param boolean $id
  54. * @param string $table
  55. * @param string $ds
  56. * @return void
  57. * @access private
  58. */
  59. function __construct($id = false, $table = null, $ds = null)
  60. {
  61. parent::__construct($id, $table, $ds);
  62. $this->cacheFolder = str_replace('/', DS, $this->cacheFolder);
  63. if ($this->cacheFolder=='web' || empty($this->cacheFolder))
  64. $this->cacheFolder = 'web'.DS;
  65. if (substr($this->cacheFolder, -1, 1)!=DS)
  66. $this->cacheFolder = $this->cacheFolder.DS;
  67. }
  68. /**
  69. * httpPost
  70. *
  71. * @param string $url
  72. * @param string $vars
  73. * @param string $headers
  74. * @param string $cookie_file
  75. * @param string $timeout
  76. * @return mixed
  77. * @access public
  78. */
  79. function httpPost($url, $vars = null, $headers = null, $cookie_file = null, $timeout = null)
  80. {
  81. $vars = $this->__toUrlData($vars);
  82. $ch = curl_init();
  83. if (! $ch)
  84. {
  85. return false;
  86. }
  87. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  88. // Don't check certifications that closley if not required, fixed some issues for me before
  89. if ($this->ssl_strict==false)
  90. {
  91. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  92. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  93. }
  94. curl_setopt($ch, CURLOPT_POST, 1);
  95. curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
  96. if (empty($timeout))
  97. $timeout = $this->connection_timeout;
  98. curl_setopt($ch,CURLOPT_TIMEOUT, $timeout);
  99. curl_setopt($ch, CURLOPT_URL, $url);
  100. curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); // follow redirects recursively
  101. if (!empty($headers))
  102. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  103. if (!empty($cookie_file))
  104. {
  105. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  106. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  107. }
  108. $response = curl_exec($ch);
  109. curl_close($ch);
  110. return $response;
  111. }
  112. /**
  113. * httpGet
  114. *
  115. * @param string $url
  116. * @param string $vars
  117. * @param string $headers
  118. * @param string $cookie_file
  119. * @param string $timeout
  120. * @return string
  121. * @access string
  122. */
  123. function httpGet($url, $vars = null, $headers = null, $cookie_file = null, $timeout = null)
  124. {
  125. if (!empty($vars))
  126. $url = $url.'?'.$this->__toUrlData($vars);
  127. $ch = curl_init();
  128. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  129. // Don't check certifications that closley if not required, fixed some issues for me before
  130. if ($this->ssl_strict==false)
  131. {
  132. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  133. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  134. }
  135. curl_setopt($ch, CURLOPT_URL, $url);
  136. if (empty($timeout))
  137. $timeout = $this->connection_timeout;
  138. curl_setopt($ch,CURLOPT_TIMEOUT, $timeout);
  139. if (!empty($headers))
  140. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  141. if (!empty($cookie_file))
  142. {
  143. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  144. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  145. }
  146. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper('get'));
  147. curl_setopt($ch, CURLOPT_VERBOSE, 1); ########### debug
  148. curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); // follow redirects recursively
  149. $ret = curl_exec($ch);
  150. curl_close($ch);
  151. return $ret;
  152. }
  153. /**
  154. * cleanUpCacheFolder
  155. *
  156. * @param int $expires
  157. * @param string $pattern
  158. * @return array
  159. * @access public
  160. */
  161. function cleanUpCacheFolder($expires = '+2 hours', $pattern = '.*')
  162. {
  163. uses('Folder');
  164. $path = TMP.'cache'.DS.$this->cacheFolder;
  165. $folder =& new Folder($path);
  166. $cachedFiles = $folder->find();
  167. if (!is_numeric($expires)) {
  168. $expires = strtotime($expires);
  169. }
  170. $errors = array();
  171. foreach ($cachedFiles as $cacheFile)
  172. {
  173. if (preg_match('/'.$pattern.'/iUs', $cacheFile))
  174. {
  175. $age = time() - @filemtime($path.$cacheFile);
  176. $maxAge = $expires - time();
  177. if ($age >= $maxAge)
  178. {
  179. // Well R.I.P. dear cache file ; )
  180. if (!@unlink($path.$cacheFile))
  181. {
  182. $errors[] = $cacheFile;
  183. }
  184. }
  185. }
  186. }
  187. if (empty($errors))
  188. return true;
  189. else
  190. return $errors;
  191. }
  192. /**
  193. * toUrlData
  194. *
  195. * @param string $arrayData
  196. * @return array
  197. * @access private
  198. */
  199. function __toUrlData($arrayData)
  200. {
  201. $postData = array();
  202. foreach ($arrayData as $key => $val)
  203. {
  204. array_push($postData, $key.'='.urlencode($val));
  205. }
  206. return join('&', $postData);
  207. }
  208. /**
  209. * Creates a unique cache file path by combining all parameters given to a unique MD5 hash
  210. *
  211. * @param string $ext The extension for the cache file
  212. * @return string Returns a unique file path
  213. * @access private
  214. */
  215. function __createCacheHash($ext = '.txt')
  216. {
  217. $args = func_get_args();
  218. array_shift($args);
  219. $hashSource = null;
  220. foreach ($args as $arg)
  221. {
  222. $hashSource = $hashSource . serialize($arg);
  223. }
  224. return md5($hashSource).$ext;
  225. }
  226. }
  227. ?>