PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/system/jsntplframework/libraries/joomlashine/helper.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 367 lines | 195 code | 58 blank | 114 comment | 28 complexity | a8e62621bae850c3774f8530a569b10a MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package JSNExtension
  5. * @subpackage JSNTplFramework
  6. * @author JoomlaShine Team <support@joomlashine.com>
  7. * @copyright Copyright (C) 2012 JoomlaShine.com. All Rights Reserved.
  8. * @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
  9. *
  10. * Websites: http://www.joomlashine.com
  11. * Technical Support: Feedback - http://www.joomlashine.com/contact-us/get-support.html
  12. */
  13. // No direct access to this file
  14. defined('_JEXEC') or die('Restricted access');
  15. jimport('joomla.registry.registry');
  16. /**
  17. * This class contains common method will be used in
  18. * the template framework
  19. *
  20. * @package JSNTplFramework
  21. * @since 1.0.0
  22. */
  23. abstract class JSNTplHelper
  24. {
  25. /**
  26. * JVersion instance
  27. *
  28. * @var JVersion
  29. */
  30. private static $_version;
  31. private static $_versionData;
  32. private static $_disabledFunctions;
  33. /**
  34. * Load templateDetails.xml using simplexml and return it
  35. *
  36. * @param string $template The template name
  37. *
  38. * @return object
  39. */
  40. public static function getManifest ($template)
  41. {
  42. $registry = JRegistry::getInstance('JSNTplFramework');
  43. if ($registry->exists('template.manifest')) {
  44. return $registry->get('template.manifest');
  45. }
  46. $xmlDocument = simplexml_load_file(JPATH_SITE . "/templates/{$template}/templateDetails.xml");
  47. $registry->set('template.manifest', $xmlDocument);
  48. return $xmlDocument;
  49. }
  50. /**
  51. *
  52. * @return boolean [description]
  53. */
  54. public static function isDisabledFunction ($name)
  55. {
  56. if (empty(self::$_disabledFunctions)) {
  57. $disabledFunctions = explode(',', ini_get('disable_functions'));
  58. $disabledFunctions = array_map('trim', $disabledFunctions);
  59. $disabledFunctions = array_map('strtolower', $disabledFunctions);
  60. self::$_disabledFunctions = $disabledFunctions;
  61. }
  62. return in_array(strtolower($name), self::$_disabledFunctions);
  63. }
  64. /**
  65. * Retrieve cached manifest from the database
  66. *
  67. * @param string $template Template name
  68. * @return array
  69. */
  70. public static function getManifestCache ($template)
  71. {
  72. $registry = JRegistry::getInstance('JSNTplFramework');
  73. $dbo = JFactory::getDBO();
  74. $query = $dbo->getQuery(true);
  75. $query->select('manifest_cache')
  76. ->from('#__extensions')
  77. ->where('element LIKE \'' . $template . '\'');
  78. $dbo->setQuery($query);
  79. return json_decode($dbo->loadResult());
  80. }
  81. /**
  82. * Retrieve current version of Joomla
  83. *
  84. * @return string
  85. */
  86. public static function getJoomlaVersion ($size = null, $includeDot = true)
  87. {
  88. $joomlaVersion = new JVersion();
  89. $versionPieces = explode('.', $joomlaVersion->getShortVersion());
  90. if (is_numeric($size) && $size > 0 && $size < count($versionPieces)) {
  91. $versionPieces = array_slice($versionPieces, 0, $size);
  92. }
  93. return implode($includeDot === true ? '.' : '', $versionPieces);
  94. }
  95. /**
  96. * Return the template ID
  97. *
  98. * @param string $name The template name
  99. *
  100. * @return string
  101. */
  102. public static function getTemplateId ($name)
  103. {
  104. $manifest = self::getManifest($name);
  105. if (isset($manifest->identifiedName)) {
  106. return (string) $manifest->identifiedName;
  107. }
  108. if (preg_match('/^jsn_(.*)_(free|pro)$/i', $name, $matched)) {
  109. return sprintf('tpl_%s', $matched[1]);
  110. }
  111. }
  112. /**
  113. * Retrieve version of the template that determined by name
  114. *
  115. * @param string $name The template name to retrieve version
  116. * @return string
  117. */
  118. public static function getTemplateVersion ($name)
  119. {
  120. $registry = JRegistry::getInstance('JSNTplFramework');
  121. if ($registry->exists('template.version')) {
  122. return $registry->get('template.version');
  123. }
  124. $manifest = JSNTplHelper::getManifestCache($name);
  125. $version = $manifest->version;
  126. $registry->set('template.version', $version);
  127. return $version;
  128. }
  129. /**
  130. * Retrieve edition of the template that determined by name
  131. *
  132. * @param string $name The template name to retrieve edition
  133. * @return string
  134. */
  135. public static function getTemplateEdition ($name)
  136. {
  137. $registry = JRegistry::getInstance('JSNTplFramework');
  138. if ($registry->exists('template.edition')) {
  139. return $registry->get('template.edition');
  140. }
  141. $manifest = JSNTplHelper::getManifest($name);
  142. $edition = isset($manifest->edition) ? (string) $manifest->edition : 'FREE';
  143. $registry->set('template.edition', $edition);
  144. return $edition;
  145. }
  146. /**
  147. * Fetch all installed extensions from the database
  148. *
  149. * @return array
  150. */
  151. public static function findInstalledExtensions ()
  152. {
  153. $registry = JRegistry::getInstance('JSNTplFramework');
  154. $installedExtensions = $registry->get('extension.installed', array());
  155. if (empty($installedExtensions)) {
  156. $dbo = JFactory::getDBO();
  157. $dbo->setQuery('SELECT element, manifest_cache FROM #__extensions WHERE type IN ("component", "plugin", "module")');
  158. foreach ($dbo->loadObjectList() as $extension) {
  159. $installedExtensions[$extension->element] = json_decode($extension->manifest_cache);
  160. }
  161. $registry->set('extension.installed', $installedExtensions);
  162. }
  163. return $installedExtensions;
  164. }
  165. /**
  166. * Return TRUE when PRO Edition of the template is installed
  167. *
  168. * @param string $template The template name
  169. *
  170. * @return boolean
  171. */
  172. public static function isInstalledProEdition ($template)
  173. {
  174. if (preg_match('/^jsn_(.*)_(free|pro)$/i', $template, $matched)) {
  175. $nameOfProEdition = sprintf('jsn_%s_pro', $matched[1]);
  176. $dbo = JFactory::getDBO();
  177. $dbo->setQuery("SELECT count(*) FROM #__extensions WHERE type LIKE 'template' AND element LIKE '{$nameOfProEdition}'");
  178. return intval($dbo->loadResult()) > 0;
  179. }
  180. return false;
  181. }
  182. /**
  183. * Return TRUE when extension with name=$name is installed
  184. *
  185. * @param string $name The name of extension
  186. *
  187. * @return boolean
  188. */
  189. public static function isInstalledExtension ($name)
  190. {
  191. $installedExtensions = self::findInstalledExtensions();
  192. return isset($installedExtensions[$name]);
  193. }
  194. /**
  195. * List all modified files of the template
  196. *
  197. * @param string $template The template name
  198. *
  199. * @return mixed
  200. */
  201. public static function getModifiedFiles ($template)
  202. {
  203. jimport('joomla.filesystem.folder');
  204. $templatePath = JPATH_SITE . "/templates/{$template}";
  205. $checksumFile = $templatePath . '/template.checksum';
  206. if (!is_file($checksumFile)) {
  207. return false;
  208. }
  209. $fileHandle = fopen($checksumFile, 'r');
  210. $hashTable = array();
  211. // Parse all hash data from checksum file
  212. while (!feof($fileHandle)) {
  213. $line = trim(fgets($fileHandle, 4096));
  214. if (!empty($line) && strpos($line, "\t") !== false) {
  215. list($path, $hash) = explode("\t", $line);
  216. $hashTable[$path] = $hash;
  217. }
  218. }
  219. // Find all files in template folder and check it state
  220. $files = JFolder::files($templatePath, '.', true, true, array('template.checksum', 'editions.json', '.svn', 'CVS', 'language'));
  221. $addedFiles = array();
  222. $changedFiles = array();
  223. $deletedFiles = array();
  224. $originalFiles = array();
  225. foreach ($files as $file) {
  226. $fileMd5 = md5_file($file);
  227. $file = str_replace(DIRECTORY_SEPARATOR, '/', $file);
  228. $file = ltrim(substr($file, strlen($templatePath)), '/');
  229. // Checking file is added
  230. if (!isset($hashTable[$file])) {
  231. $addedFiles[] = $file;
  232. }
  233. // Checking file is changed
  234. elseif (isset($hashTable[$file]) && $fileMd5 != $hashTable[$file]) {
  235. $changedFiles[] = $file;
  236. }
  237. // Checking file is original
  238. elseif (isset($hashTable[$file]) && $fileMd5 == $hashTable[$file]) {
  239. $originalFiles[] = $file;
  240. }
  241. }
  242. $templateFiles = array_merge($addedFiles, $changedFiles, $originalFiles);
  243. $templateFiles = array_unique($templateFiles);
  244. // Find all deleted files
  245. foreach (array_keys($hashTable) as $item) {
  246. if (!in_array($item, $templateFiles)) {
  247. $deletedFiles[] = $item;
  248. }
  249. }
  250. return array(
  251. 'add' => $addedFiles,
  252. 'delete' => $deletedFiles,
  253. 'edit' => $changedFiles
  254. );
  255. }
  256. /**
  257. * Download templates information data from JoomlaShine server
  258. *
  259. * @return void
  260. */
  261. public static function getVersionData ()
  262. {
  263. if (empty(self::$_versionData))
  264. {
  265. $httpClient = JSNTplHttpClient::createRequest();
  266. $response = $httpClient->get('http://www.joomlashine.com/versioning/product_version.php?category=cat_template');
  267. // Check http request result
  268. if ($response->status != 'OK' && intval($response->code) != 200) {
  269. throw new Exception('JSN_TPLFW_ERROR_CONTACTING_TO_JOOMLASHINE');
  270. }
  271. self::$_versionData = json_decode($response->content, true);
  272. }
  273. // Return result
  274. return self::$_versionData;
  275. }
  276. /**
  277. * Make a nested path , creating directories down the path
  278. * recursion !!
  279. *
  280. * @param string $path Path to create directories
  281. *
  282. * @return void
  283. */
  284. public static function makePath ($path)
  285. {
  286. // Check if directory already exists
  287. if (is_dir($path) || empty($path)) {
  288. return true;
  289. }
  290. // Ensure a file does not already exist with the same name
  291. $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
  292. if (is_file($path)) {
  293. trigger_error('mkdirr() File exists', E_USER_WARNING);
  294. return false;
  295. }
  296. // Crawl up the directory tree
  297. $nextPath = substr($path, 0, strrpos($path, DIRECTORY_SEPARATOR));
  298. if (self::makePath($nextPath)) {
  299. if (!file_exists($path)) {
  300. return mkdir($path);
  301. }
  302. }
  303. return false;
  304. }
  305. }