/administrator/components/com_kunena/models/cpanel.php

https://github.com/zarkos/Kunena-2.0 · PHP · 141 lines · 85 code · 25 blank · 31 comment · 23 complexity · 743eb0349887a211644b048b49a29a42 MD5 · raw file

  1. <?php
  2. /**
  3. * Kunena Component
  4. * @package Kunena.Administrator
  5. * @subpackage Models
  6. *
  7. * @copyright (C) 2008 - 2011 Kunena Team. All rights reserved.
  8. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  9. * @link http://www.kunena.org
  10. **/
  11. defined ( '_JEXEC' ) or die ();
  12. jimport ( 'joomla.application.component.model' );
  13. kimport('kunena.model');
  14. /**
  15. * Cpanel Model for Kunena
  16. *
  17. * @since 2.0
  18. */
  19. class KunenaAdminModelCpanel extends KunenaModel {
  20. function getLatestVersion() {
  21. $latestVersion = $this->getLatestKunenaVersion();
  22. if ( $latestVersion['connect'] ) {
  23. if ( version_compare($latestVersion['latest_version'], KunenaForum::version(), '<=') ) {
  24. $needUpgrade = JText::sprintf('COM_KUNENA_COM_A_CHECK_VERSION_CORRECT', KunenaForum::version());
  25. } else {
  26. $needUpgrade = JText::sprintf('COM_KUNENA_COM_A_CHECK_VERSION_NEED_UPGRADE',$latestVersion['latest_version'],$latestVersion['released']);
  27. }
  28. } else {
  29. $needUpgrade = JText::_('COM_KUNENA_COM_A_CHECK_VERSION_CANNOT_CONNECT');
  30. }
  31. return $needUpgrade;
  32. }
  33. /* Get latest kunena version
  34. *
  35. * Code originally taken from AlphaUserPoints
  36. * copyright Copyright (C) 2008-2010 Bernard Gilly
  37. * license : GNU/GPL
  38. * Website : http://www.alphaplug.com
  39. */
  40. function getLatestKunenaVersion() {
  41. $app = JFactory::getApplication ();
  42. $url = 'http://update.kunena.org/kunena_update.xml';
  43. $data = '';
  44. $check = array();
  45. $check['connect'] = 0;
  46. $data = $app->getUserState('com_kunena.version_check', null);
  47. if ( empty($data) ) {
  48. //try to connect via cURL
  49. if(function_exists('curl_init') && function_exists('curl_exec')) {
  50. $ch = @curl_init();
  51. @curl_setopt($ch, CURLOPT_URL, $url);
  52. @curl_setopt($ch, CURLOPT_HEADER, 0);
  53. //http code is greater than or equal to 300 ->fail
  54. @curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  55. @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  56. //timeout of 5s just in case
  57. @curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  58. $data = @curl_exec($ch);
  59. @curl_close($ch);
  60. }
  61. //try to connect via fsockopen
  62. if(function_exists('fsockopen') && $data == '') {
  63. $errno = 0;
  64. $errstr = '';
  65. //timeout handling: 5s for the socket and 5s for the stream = 10s
  66. $fsock = @fsockopen("update.kunena.org", 80, $errno, $errstr, 5);
  67. if ($fsock) {
  68. @fputs($fsock, "GET /kunena_update.xml HTTP/1.1\r\n");
  69. @fputs($fsock, "HOST: update.kunena.org\r\n");
  70. @fputs($fsock, "Connection: close\r\n\r\n");
  71. //force stream timeout...
  72. @stream_set_blocking($fsock, 1);
  73. @stream_set_timeout($fsock, 5);
  74. $get_info = false;
  75. while (!@feof($fsock)) {
  76. if ($get_info) {
  77. $data .= @fread($fsock, 1024);
  78. } else {
  79. if (@fgets($fsock, 1024) == "\r\n") {
  80. $get_info = true;
  81. }
  82. }
  83. }
  84. @fclose($fsock);
  85. //need to check data cause http error codes aren't supported here
  86. if(!strstr($data, '<?xml version="1.0" encoding="utf-8"?><update>')) {
  87. $data = '';
  88. }
  89. }
  90. }
  91. //try to connect via fopen
  92. if (function_exists('fopen') && ini_get('allow_url_fopen') && $data == '') {
  93. //set socket timeout
  94. ini_set('default_socket_timeout', 5);
  95. $handle = @fopen ($url, 'r');
  96. //set stream timeout
  97. @stream_set_blocking($handle, 1);
  98. @stream_set_timeout($handle, 5);
  99. $data = @fread($handle, 1000);
  100. @fclose($handle);
  101. }
  102. return $data;
  103. }
  104. if( !empty($data) && strstr($data, '<?xml version="1.0" encoding="utf-8"?>') ) {
  105. $xml = JFactory::getXMLparser('Simple');
  106. $xml->loadString($data);
  107. $version = $xml->document->version[0];
  108. $check['latest_version'] = $version->data();
  109. $released = $xml->document->released[0];
  110. $check['released'] = $released->data();
  111. $check['connect'] = 1;
  112. $check['enabled'] = 1;
  113. }
  114. return $check;
  115. }
  116. }