PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_jfbconnect/assets/sourcecoast.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 305 lines | 238 code | 44 blank | 23 comment | 54 complexity | d7119e02565ca762b3070334b01a5067 MD5 | raw file
  1. <?php
  2. /**
  3. * @package SourceCoast Extension Version Tool
  4. * @copyright (C) 2010-2013 by SourceCoast - All rights reserved
  5. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
  6. */
  7. defined('_JEXEC') or die('Restricted access');
  8. jimport('joomla.filesystem.file');
  9. jimport('joomla.filesystem.folder');
  10. jimport('joomla.html.parameter.element');
  11. class sourceCoastConnect
  12. {
  13. var $xmlString;
  14. var $imagePath;
  15. function __construct($checkName, $imagePathDir)
  16. {
  17. $this->xmlString = $this->_getRemoteXML2($checkName);
  18. $this->imagePath = $imagePathDir;
  19. }
  20. function display($extensionName, $installedVersion = null)
  21. {
  22. $this->_getExtensionData('joomla', $extensionName);
  23. if ($installedVersion != null)
  24. $this->installedVersion = $installedVersion;
  25. else
  26. $this->installedVersion = $this->_getJoomlaInstalledVersion($extensionName);
  27. include_once(dirname(__FILE__) . '/template.php');
  28. }
  29. private function _getRemoteXML2($checkName)
  30. {
  31. if (!function_exists('curl_init')) {
  32. throw new Exception('SourceCoast extension check needs the CURL PHP extension.');
  33. }
  34. $site = 'www.sourcecoast.com';
  35. $xml = '/versions/' . $checkName . ".xml";
  36. $ch = curl_init($site . $xml);
  37. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  38. curl_setopt($ch, CURLOPT_HEADER, 0);
  39. $contents = curl_exec($ch);
  40. curl_close($ch);
  41. return $contents;
  42. }
  43. private function _getRemoteXML($checkName)
  44. {
  45. // Get the xml file
  46. $site = 'www.sourcecoast.com';
  47. $xml = '/versions/' . $checkName . ".xml";
  48. $contents = '';
  49. $handle = fsockopen($site, 80, $errno, $errstr, 30);
  50. if ($handle)
  51. {
  52. $out = "GET /$xml HTTP/1.0\r\n";
  53. $out .= "Host: $site\r\n";
  54. $out .= "Connection: Close\r\n\r\n";
  55. fwrite($handle, $out);
  56. $body = false;
  57. while (!feof($handle))
  58. {
  59. $return = fgets($handle, 1024);
  60. if ($body)
  61. $contents .= $return;
  62. if ($return == "\r\n")
  63. $body = true;
  64. }
  65. fclose($handle);
  66. }
  67. return $contents;
  68. }
  69. function getExtensionContainer($extensionType)
  70. {
  71. if($extensionType == 'library')
  72. return 'libraries';
  73. else
  74. return $extensionType . "s";
  75. }
  76. /**
  77. * Returns current version number, support link, and reviews for the passed in extension name. Input 1=cms name, input 2=extension name.
  78. * @return string (or an xmlrpcresp obj instance if call fails)
  79. */
  80. function getExtensionData($extensionName, $extensionType)
  81. {
  82. if ($this->xmlString)
  83. {
  84. $xml = new SimpleXMLElement($this->xmlString);
  85. $data = new stdClass();
  86. $extensionContainer = $this->getExtensionContainer($extensionType);
  87. $element = $xml->xpath('/sourcecoast/' . $extensionContainer . "/" . $extensionType . '[@system="' . $extensionName . '"]');
  88. if (count($element) > 0)
  89. {
  90. $data->currentVersion = $element[0]->version;
  91. $data->localVersion = $this->getInstalledVersion($extensionName, $extensionType);
  92. $data->name = $element[0]->name;
  93. return $data;
  94. }
  95. }
  96. $data->currentVersion = 'unknown';
  97. $data->localVersion = $this->getInstalledVersion($extensionName, $extensionType);
  98. $data->name = $extensionName;
  99. /* $dependencies = $element[0]->dependencies;
  100. if (count($dependencies) > 0)
  101. {
  102. foreach ($dependencies->dependency as $dependency)
  103. {
  104. $dep = new stdClass();
  105. $dep->name = $dependency->name;
  106. $dep->version = $dependency->version;
  107. $data->dependencies[] = $dep;
  108. }
  109. } */
  110. return $data;
  111. }
  112. function getInstalledVersion($extensionName, $extensionType)
  113. {
  114. $version = "Not Installed";
  115. $extensionType = $this->getExtensionContainer($extensionType);
  116. $xmlDir = "";
  117. $xmlFile = "";
  118. if ($extensionType == "components" || $extensionType == "modules")
  119. {
  120. if (JFolder::exists(JPATH_ADMINISTRATOR . '/' . $extensionType . '/' . $extensionName))
  121. {
  122. $xmlDir = JPATH_ADMINISTRATOR . '/' . $extensionType . '/' . $extensionName;
  123. $xmlFile = str_replace("com_", "", $extensionName) . ".xml";
  124. }
  125. else if (JFolder::exists(JPATH_SITE . '/' . $extensionType . '/' . $extensionName))
  126. {
  127. $xmlDir = JPATH_SITE . '/' . $extensionType . '/' . $extensionName;
  128. $xmlFile = $extensionName . ".xml";
  129. }
  130. }
  131. else if ($extensionType == "plugins")
  132. {
  133. $pluginData = explode(".", $extensionName);
  134. if (JFile::exists(JPATH_SITE . '/plugins/' . $pluginData[0] . '/' . $pluginData[1] . '/' . $pluginData[1] . '.xml'))
  135. $xmlDir = JPATH_SITE . '/plugins/' . $pluginData[0] . '/' . $pluginData[1];
  136. $xmlFile = $pluginData[1] . ".xml";
  137. }
  138. else if($extensionType == "libraries")
  139. {
  140. if (JFolder::exists(JPATH_SITE . '/libraries/' . $extensionName))
  141. {
  142. $xmlDir = JPATH_SITE . '/libraries/' . $extensionName;
  143. $xmlFile = $extensionName . '.xml';
  144. }
  145. }
  146. if ($xmlFile == "" || $xmlDir == "")
  147. return $version;
  148. if (JFile::exists($xmlDir . '/' . $xmlFile))
  149. {
  150. $xmlElement = simplexml_load_file($xmlDir . '/' . $xmlFile);
  151. if($xmlElement && isset($xmlElement->version))
  152. {
  153. $version = (string)$xmlElement->version;
  154. }
  155. }
  156. return $version;
  157. }
  158. function _showVersionInfoRow($extName, $extType)
  159. {
  160. $extData = $this->getExtensionData($extName, $extType);
  161. echo "<tr>";
  162. echo "<td>" . $extData->name . "</td>";
  163. if ($extData->localVersion == "Not Installed")
  164. {
  165. echo '<td><span style="color:#999999">' . $extData->localVersion . "</span></td>";
  166. echo '<td>' . $extData->currentVersion . "</span></td>";
  167. }
  168. else if ($extData->localVersion != $extData->currentVersion)
  169. {
  170. echo '<td><span style="color:#FF0000"><b>' . $extData->localVersion . "</b></span></td>";
  171. echo '<td>' . $extData->currentVersion . "</span></td>";
  172. }
  173. else
  174. {
  175. echo '<td><span style="color:#009900">' . $extData->localVersion . "</span></td>";
  176. echo "<td>" . $extData->currentVersion . "</span></td>";
  177. }
  178. if ($extType == "component")
  179. echo "<td>" . SourceCoastExtensionHelper::checkComponent($extName, $this->imagePath) . "</td>";
  180. else if ($extType == "library")
  181. echo "<td>" . SourceCoastExtensionHelper::checkLibrary($extName, $this->imagePath) . "</td>";
  182. else if ($extType == "module")
  183. echo "<td>" . SourceCoastExtensionHelper::checkModule($extName, $this->imagePath) . "</td>";
  184. else if ($extType == "plugin")
  185. echo "<td>" . SourceCoastExtensionHelper::checkPlugin($extName, $this->imagePath) . "</td>";
  186. echo "</tr>";
  187. }
  188. }
  189. class SourceCoastExtensionHelper
  190. {
  191. static function checkExtension($path, $query, $imagePath)
  192. {
  193. if (!is_dir($path) && !is_file($path))
  194. {
  195. $alt = "This extension does not appear to be installed.";
  196. return '<img title="' . $alt . '" alt="' . $alt . '" src="'.$imagePath.'icon-16-deny.png" width="10" height="10" />';
  197. }
  198. else
  199. {
  200. $dbo = JFactory::getDBO();
  201. $dbo->setQuery($query);
  202. $instance = $dbo->loadObject();
  203. if ($instance == null)
  204. {
  205. $alt = "This extension is installed but not published.";
  206. return '<img title="' . $alt . '" alt="' . $alt . '" src="'.$imagePath.'icon-16-notice-note.png" width="10" height="10" />';
  207. }
  208. else
  209. {
  210. $alt = "This extension is installed and published.";
  211. return '<img title="' . $alt . '" alt="' . $alt . '" src="'.$imagePath.'icon-16-allow.png" width="10" height="10" />';
  212. }
  213. }
  214. }
  215. static function checkComponent($name, $imagePath)
  216. {
  217. //Don't need to perform check since this is called from component code
  218. $alt = "This extension is installed and published.";
  219. return '<img title="' . $alt . '" alt="' . $alt . '" src="'.$imagePath.'icon-16-allow.png" width="10" height="10" />';
  220. }
  221. static function checkLibrary($name, $imagePath)
  222. {
  223. $dbo = JFactory::getDBO();
  224. return SourceCoastExtensionHelper::checkExtension(
  225. JPATH_ROOT . "/libraries/" . $name,
  226. "SELECT extension_id " .
  227. "FROM #__extensions " .
  228. "WHERE element = " . $dbo->quote($name) .
  229. " AND type = 'library'" .
  230. " AND enabled = 1",
  231. $imagePath
  232. );
  233. }
  234. static function checkModule($name, $imagePath)
  235. {
  236. $dbo = JFactory::getDBO();
  237. return SourceCoastExtensionHelper::checkExtension(
  238. JPATH_ROOT . "/modules/" . $name,
  239. "SELECT id " .
  240. "FROM #__modules " .
  241. "WHERE module = " . $dbo->quote($name) . " " .
  242. " AND published = 1",
  243. $imagePath
  244. );
  245. }
  246. static function checkPlugin($name, $imagePath)
  247. {
  248. $pluginParts = explode(".", $name);
  249. $dbo = JFactory::getDBO();
  250. return SourceCoastExtensionHelper::checkExtension(
  251. JPATH_ROOT . "/plugins/" . $pluginParts[0] . '/' . $pluginParts[1] . '/' . $pluginParts[1] . ".php",
  252. "SELECT extension_id " .
  253. "FROM #__extensions " .
  254. "WHERE folder = " . $dbo->quote($pluginParts[0]) . " " .
  255. "AND element = " . $dbo->quote($pluginParts[1]) . " " .
  256. "AND enabled = 1 AND access = 1",
  257. $imagePath
  258. );
  259. }
  260. }