PageRenderTime 52ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/system/expressionengine/helpers/version_helper.php

https://bitbucket.org/mbaily/tremain
PHP | 250 lines | 135 code | 45 blank | 70 comment | 22 complexity | 51634b3d33043bd71ffcd93bd31607ca MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author EllisLab Dev Team
  7. * @copyright Copyright (c) 2003 - 2013, EllisLab, Inc.
  8. * @license http://ellislab.com/expressionengine/user-guide/license.html
  9. * @link http://ellislab.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Segment Helper
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Helpers
  19. * @category Helpers
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. // --------------------------------------------------------------------
  24. /**
  25. * EE Version Check function
  26. *
  27. * Requests a file from ExpressionEngine.com that informs us what the current available version
  28. * of ExpressionEngine.
  29. *
  30. * @access private
  31. * @return bool|string
  32. */
  33. function get_version_info()
  34. {
  35. $EE =& get_instance();
  36. // Attempt to grab the local cached file
  37. $cached = _check_version_cache();
  38. $data = '';
  39. if ( ! $cached)
  40. {
  41. $details['timestamp'] = time();
  42. $dl_page_url = 'http://versions.ellislab.com/versions_ee2.txt';
  43. $target = parse_url($dl_page_url);
  44. $fp = @fsockopen($target['host'], 80, $errno, $errstr, 3);
  45. if (is_resource($fp))
  46. {
  47. fputs($fp,"GET ".$dl_page_url." HTTP/1.0\r\n" );
  48. fputs($fp,"Host: ".$target['host'] . "\r\n" );
  49. fputs($fp,"User-Agent: EE/EllisLab PHP/\r\n");
  50. fputs($fp,"If-Modified-Since: Fri, 01 Jan 2004 12:24:04\r\n\r\n");
  51. $headers = TRUE;
  52. while ( ! feof($fp))
  53. {
  54. $line = fgets($fp, 4096);
  55. if ($headers === FALSE)
  56. {
  57. $data .= $line;
  58. }
  59. elseif (trim($line) == '')
  60. {
  61. $headers = FALSE;
  62. }
  63. }
  64. fclose($fp);
  65. if ($data !== '')
  66. {
  67. // We have a file, now parse & make an array of arrays.
  68. $data = explode("\n", trim($data));
  69. $version_file = array();
  70. foreach ($data as $d)
  71. {
  72. $version_file[] = explode('|', $d);
  73. }
  74. // 0 =>
  75. // array
  76. // 0 => string '2.1.0' (length=5)
  77. // 1 => string '20100805' (length=8)
  78. // 2 => string 'normal' (length=6)
  79. if ($data === NULL)
  80. {
  81. // something's not right...
  82. $version_file['error'] = TRUE;
  83. }
  84. }
  85. else
  86. {
  87. $version_file['error'] = TRUE;
  88. }
  89. }
  90. else
  91. {
  92. $version_file['error'] = TRUE;
  93. }
  94. _write_version_cache($version_file);
  95. }
  96. else
  97. {
  98. $version_file = $cached;
  99. }
  100. // one final check for good measure
  101. if ( ! _is_valid_version_file($version_file))
  102. {
  103. return FALSE;
  104. }
  105. if (isset($version_file['error']) && $version_file['error'] == TRUE)
  106. {
  107. return FALSE;
  108. }
  109. return $version_file;
  110. }
  111. // --------------------------------------------------------------------
  112. /**
  113. * Validate version file
  114. * Prototype:
  115. * 0 =>
  116. * array
  117. * 0 => string '2.1.0' (length=5)
  118. * 1 => string '20100805' (length=8)
  119. * 2 => string 'normal' (length=6)
  120. *
  121. * @access private
  122. * @return bool
  123. */
  124. function _is_valid_version_file($version_file)
  125. {
  126. if ( ! is_array($version_file))
  127. {
  128. return FALSE;
  129. }
  130. foreach ($version_file as $version)
  131. {
  132. if ( ! is_array($version) OR count($version) != 3)
  133. {
  134. return FALSE;
  135. }
  136. foreach ($version as $val)
  137. {
  138. if ( ! is_string($val))
  139. {
  140. return FALSE;
  141. }
  142. }
  143. }
  144. return TRUE;
  145. }
  146. // --------------------------------------------------------------------
  147. /**
  148. * Check EE Version Cache.
  149. *
  150. * @access private
  151. * @return bool|string
  152. */
  153. function _check_version_cache()
  154. {
  155. $EE =& get_instance();
  156. $EE->load->helper('file');
  157. // check cache first
  158. $cache_expire = 60 * 60 * 24; // only do this once per day
  159. $contents = read_file(APPPATH.'cache/ee_version/current_version');
  160. if ($contents !== FALSE)
  161. {
  162. $details = @unserialize($contents);
  163. if (isset($details['timestamp']) && ($details['timestamp'] + $cache_expire) > $EE->localize->now)
  164. {
  165. return $details['data'];
  166. }
  167. else
  168. {
  169. return FALSE;
  170. }
  171. }
  172. }
  173. // --------------------------------------------------------------------
  174. /**
  175. * Write EE Version Cache
  176. *
  177. * @param array - details of version needed to be cached.
  178. * @return void
  179. */
  180. function _write_version_cache($details)
  181. {
  182. $EE =& get_instance();
  183. $EE->load->helper('file');
  184. $cache_path = $EE->config->item('cache_path');
  185. if (empty($cache_path))
  186. {
  187. $cache_path = APPPATH.'cache/';
  188. }
  189. $cache_path .= 'ee_version/';
  190. if ( ! is_dir($cache_path))
  191. {
  192. mkdir($cache_path, DIR_WRITE_MODE);
  193. @chmod($cache_path, DIR_WRITE_MODE);
  194. }
  195. $data = array(
  196. 'timestamp' => $EE->localize->now,
  197. 'data' => $details
  198. );
  199. if (write_file($cache_path.'current_version', serialize($data)))
  200. {
  201. @chmod($cache_path.'current_version', FILE_WRITE_MODE);
  202. }
  203. }
  204. /* End of file version_helper.php */
  205. /* Location: ./system/expressionengine/helpers/version_helper.php */