/** * returns plugins xml version * * @param null|PluginTable|int $plugin The plugin id or object to check version for * @param bool $raw 1/True: version only (no farm), 0/False: Formatted version (green/red/shortened), 2: array of version information ( $version, $latestVersion, $isLatest, $latestURL ) * @param int $duration The duration to cache the plugin version xml file (null/0 for no limit) * @param int $length The maximum version length to display (null/0 for no limit) * @return null|string */ public function getPluginVersion($plugin, $raw = false, $duration = 24, $length = 0) { global $_CB_framework, $ueConfig; cbimport('cb.snoopy'); static $plgVersions = null; if ($plgVersions === null) { $cacheFile = $_CB_framework->getCfg('absolute_path') . '/cache/cbpluginsversions.xml'; $plgVersionsXML = null; if (file_exists($cacheFile)) { if (!$duration || intval(($_CB_framework->now() - filemtime($cacheFile)) / 3600) > $duration) { $request = true; } else { $plgVersionsXML = new SimpleXMLElement(trim(file_get_contents($cacheFile))); $request = false; } } else { $request = true; } if ($request) { $s = new CBSnoopy(); $s->read_timeout = 30; $s->referer = $_CB_framework->getCfg('live_site'); @$s->fetch('http://update.joomlapolis.net/cbpluginsversions20.xml'); if ((int) $s->status == 200) { try { $plgVersionsXML = new SimpleXMLElement($s->results); $plgVersionsXML->saveXML($cacheFile); } catch (Exception $e) { } } } if ($plgVersionsXML) { $plgVersions = $plgVersionsXML->getElementByPath('cb_plugins/' . (checkJversion() >= 2 ? 'j30' : 'j15')); } else { $plgVersions = false; } } $plugin = $this->getCachedPluginObject($plugin); if (!$plugin) { return $raw === 2 ? array(null, null, null, null) : null; } static $cache = array(); $pluginId = (int) $plugin->id; if (!isset($cache[$pluginId][$raw])) { $xmlFile = $this->getPluginXmlPath($plugin); $version = null; $latestVersion = null; $isLatest = null; $latestURL = null; if (file_exists($xmlFile)) { try { $xml = new SimpleXMLElement(trim(file_get_contents($xmlFile))); } catch (\Exception $e) { $xml = null; echo "{$xmlFile} not an XML file!!!"; } if ($xml !== null) { $ver = null; if (isset($xml->release)) { // New release XML variable used by incubator projects: $ver = $xml->release; } elseif (isset($xml->cbsubsversion)) { // CBSubs plugin versions are same as the CBSubs version; lets grab them: $cbsubsVer = $xml->cbsubsversion->attributes(); if (isset($cbsubsVer['version'])) { $ver = $cbsubsVer['version']; } } elseif (isset($xml->description)) { // Attempt to parse plugin description for a version using logical naming: if (preg_match('/(?:plugin|field|fieldtype|ver|version|' . preg_quote($plugin->name) . ') ((?:[0-9]+(?:\\.)?(?:(?: )?RC)?(?:(?: )?B)?(?:(?: )?BETA)?)+)/i', $xml->description, $matches)) { $ver = $matches[1]; } } // Check if version was found; if it was lets clean it up: if ($ver) { if (preg_match('/^\\d+(\\.\\d+)+(-[a-z]+\\.\\d+)?(\\+\\w)?$/', $ver)) { $version = $ver; } else { $version = preg_replace('/\\.*([a-zA-Z]+)\\.*/i', '.$1.', preg_replace('/^[a-zA-Z]+/i', '', str_replace(array('-', '_', '+'), '.', str_replace(' ', '', strtoupper($ver))))); } if (is_integer($version)) { $version = implode('.', str_split($version)); } elseif (preg_match('/^(\\d{2,})(\\.[a-zA-Z].+)/i', $version, $matches)) { $version = implode('.', str_split($matches[1])) . $matches[2]; } $version = trim(str_replace('..', '.', $version), '.'); // Encase the version is too long lets cut it short for readability and display full version as mouseover title: if ($version && $length && cbIsoUtf_strlen($version) > $length) { $versionName = rtrim(trim(cbIsoUtf_substr($version, 0, $length)), '.') . '…'; $versionShort = true; } else { $versionName = $version; $versionShort = false; } // Lets try and parse out latest version and latest url from versions xml data: if ($plgVersions) { foreach ($plgVersions as $plgVersion) { $plgName = (string) $plgVersion->name; $plgFile = (string) $plgVersion->file; if ($plgName == $plugin->name || strpos($plgName, $plugin->name) !== false || strpos($plgFile, $plugin->folder) !== false) { $latestVersion = (string) $plgVersion->version; $latestURL = (string) $plgVersion->url; } } } if ($latestVersion) { if (version_compare($version, $latestVersion) >= 0) { $isLatest = true; } else { $isLatest = false; } } // Format version display: if (!$raw) { if ($latestVersion) { if ($isLatest) { $version = '<span class="text-success"' . ($versionShort ? ' title="' . htmlspecialchars($version) . '"' : null) . '><strong>' . $versionName . '</strong></span>'; } else { $version = '<span class="text-danger" title="' . htmlspecialchars($latestVersion) . '"><strong>' . $versionName . '</strong></span>'; if ($latestURL) { $version = '<a href="' . htmlspecialchars($latestURL) . '" target="_blank">' . $version . '</a>'; } } } else { if ($versionShort) { $version = '<span title="' . htmlspecialchars($version) . '">' . $versionName . '</span>'; } else { $version = $versionName; } } } } } } if (!$version && !$raw) { if ($plugin->iscore) { // core plugins are same version as CB it self: if ($length && cbIsoUtf_strlen($ueConfig['version']) > $length) { $version = '<span title="' . htmlspecialchars($ueConfig['version']) . '">' . rtrim(trim(cbIsoUtf_substr($ueConfig['version'], 0, $length)), '.') . '…</span>'; } else { $version = $ueConfig['version']; } } else { $version = '-'; } } if ($raw === 2) { $version = array($version, $latestVersion, $isLatest, $latestURL); } $cache[$pluginId][$raw] = $version; } return $cache[$pluginId][$raw]; }
/** * @param string $url * @param string $file * @param int $duration * @return SimpleXMLElement|null */ public static function getFeedXML($url, $file, $duration = 12) { global $_CB_framework; cbimport('cb.snoopy'); $cache = $_CB_framework->getCfg('absolute_path') . '/cache/' . $file; $xml = null; if (file_exists($cache)) { if (!$duration || intval(($_CB_framework->now() - filemtime($cache)) / 3600) > $duration) { $request = true; } else { $xml = new SimpleXMLElement(trim(file_get_contents($cache))); $request = false; } } else { $request = true; } if ($request) { $s = new CBSnoopy(); $s->read_timeout = 30; $s->referer = $_CB_framework->getCfg('live_site'); @$s->fetch($url); if ((int) $s->status == 200) { try { $xml = new SimpleXMLElement($s->results); $xml->saveXML($cache); } catch (Exception $e) { } } } return $xml; }