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

/admin/windwalker/helpers/system.php

https://bitbucket.org/asikart-extension/joomclouds-client
PHP | 370 lines | 172 code | 69 blank | 129 comment | 32 complexity | 3284797b7e4b46674bc092fbdd6312f7 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Windwalker.Framework
  4. * @subpackage Helpers
  5. *
  6. * @copyright Copyright (C) 2012 Asikart. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. * @author Generated by AKHelper - http://asikart.com
  9. */
  10. // No direct access
  11. defined('_JEXEC') or die;
  12. /**
  13. * Handle some component system information.
  14. *
  15. * @package Windwalker.Framework
  16. * @subpackage Helpers
  17. */
  18. class AKHelperSystem
  19. {
  20. /**
  21. * A cache to store component system config (Not Joomla! component params).
  22. *
  23. * @var array
  24. */
  25. static $config = array();
  26. /**
  27. * Version of component.
  28. *
  29. * @var array
  30. */
  31. static $version = array();
  32. /**
  33. * Profiler store.
  34. *
  35. * @var array
  36. */
  37. static $profiler = array() ;
  38. /**
  39. * Buffer to save last Profiler logs form UserState.
  40. *
  41. * @var array
  42. */
  43. static $state_buffer = array();
  44. /**
  45. * Generate UUID v4 or v5.
  46. *
  47. * Ref from: https://gist.github.com/dahnielson/508447
  48. *
  49. * @param mixed The condition to generate v3 MD5 UUID, may be string or array.
  50. * If this params is null, will generate v4 random UUID.
  51. * When condition exists, output will always the same.
  52. * @param mixed UUID version. May be integer 5 or string 'v5',
  53. * others will retuen v4 random uuid or v3 md5 uuid(if $condition exists).
  54. *
  55. * @return string 32bit UUID.
  56. */
  57. public static function uuid($condition = null, $version = 4)
  58. {
  59. $uuid = '' ;
  60. // Generate UUID v4 By Random
  61. if(!$condition) {
  62. $uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  63. // 32 bits for "time_low"
  64. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  65. // 16 bits for "time_mid"
  66. mt_rand(0, 0xffff),
  67. // 16 bits for "time_hi_and_version",
  68. // four most significant bits holds version number 4
  69. mt_rand(0, 0x0fff) | 0x4000,
  70. // 16 bits, 8 bits for "clk_seq_hi_res",
  71. // 8 bits for "clk_seq_low",
  72. // two most significant bits holds zero and one for variant DCE1.1
  73. mt_rand(0, 0x3fff) | 0x8000,
  74. // 48 bits for "node"
  75. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  76. );
  77. }else{
  78. // Genertae UUID v3 By Condition MD5
  79. $condition = (array) $condition ;
  80. $condition = implode( '-', $condition );
  81. $chars = md5( $condition );
  82. $uuid = substr($chars,0,8) . '-';
  83. $uuid .= substr($chars,8,4) . '-';
  84. $uuid .= substr($chars,12,4) . '-';
  85. $uuid .= substr($chars,16,4) . '-';
  86. $uuid .= substr($chars,20,12);
  87. }
  88. // Generate UUID v5
  89. if( $version == 5 || $version == 'v5' ) {
  90. if(preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) !== 1 ) {
  91. return $uuid ;
  92. }
  93. // Get hexadecimal components of namespace
  94. $nhex = str_replace(array('-','{','}'), '', $uuid);
  95. // Binary Value
  96. $nstr = '';
  97. // Convert Namespace UUID to bits
  98. for($i = 0; $i < strlen($nhex); $i+=2)
  99. {
  100. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  101. }
  102. // Calculate hash value
  103. if( $condition ) {
  104. $condition = is_array($condition) ? implode('-', $condition) : $condition ;
  105. }else{
  106. $condition = uniqid();
  107. }
  108. $hash = sha1($nstr . $condition);
  109. $uuid = sprintf('%08s-%04s-%04x-%04x-%12s',
  110. // 32 bits for "time_low"
  111. substr($hash, 0, 8),
  112. // 16 bits for "time_mid"
  113. substr($hash, 8, 4),
  114. // 16 bits for "time_hi_and_version",
  115. // four most significant bits holds version number 5
  116. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
  117. // 16 bits, 8 bits for "clk_seq_hi_res",
  118. // 8 bits for "clk_seq_low",
  119. // two most significant bits holds zero and one for variant DCE1.1
  120. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  121. // 48 bits for "node"
  122. substr($hash, 20, 12)
  123. );
  124. }
  125. return $uuid ;
  126. }
  127. /**
  128. * Get component Joomla! params, a proxy of JComponentHelper::getParams($option) ;
  129. *
  130. * @param string $option Component option name.
  131. *
  132. * @return JRegistry Component params object.
  133. */
  134. public static function getParams($option = null)
  135. {
  136. if(!$option) {
  137. $option = AKHelper::_('path.getOption') ;
  138. }
  139. if($option) {
  140. return JComponentHelper::getParams($option);
  141. }
  142. }
  143. /**
  144. * Get component system config, if first param not exists, will return all params object.
  145. *
  146. * @param string $key Param key.
  147. * @param string $default Default value if key not exists.
  148. * @param string $option Component option name.
  149. *
  150. * @return mixed Param value.
  151. */
  152. public static function getConfig($key = null, $default = null, $option = null)
  153. {
  154. if(!$option){
  155. $option = AKHelper::_('path.getOption') ;
  156. }
  157. // Singleton & Lazy loading
  158. if(isset(self::$config[$option])) {
  159. if(!$key){
  160. return self::$config[$option] ;
  161. }else{
  162. return self::$config[$option]->get($key, $default) ;
  163. }
  164. }
  165. // Init Config
  166. self::$config[$option] = new JRegistry();
  167. self::$config[$option]->loadFile( AKHelper::_('path.getAdmin', $option).'/includes/config.json' );
  168. if(!$key){
  169. return self::$config[$option] ;
  170. }else{
  171. return self::$config[$option]->get($key, $default) ;
  172. }
  173. }
  174. /**
  175. * Save component params to #__extension.
  176. *
  177. * @param mixed $params A params object, array or JRegistry object.
  178. * @param string $element Extension element name, eg: com_content, mod_modules.
  179. * @param string $client Client, 1 => 'site', 2 => 'administrator'.
  180. * @param string $group Group(folder) name for plugin.
  181. *
  182. * @return boolean Success or not.
  183. */
  184. public static function saveParams($params, $element, $client = null, $group = null)
  185. {
  186. if( $params instanceof JRegistry ) {
  187. $params = (string) $params ;
  188. }else{
  189. $params = json_decode($params) ;
  190. }
  191. $client = ($client == 'admin' || $client == 1) ? 1 : 0 ;
  192. $db = JFactory::getDbo();
  193. $q = $db->getQuery(true) ;
  194. if( $client ) {
  195. $q->where("client_id = '{$client}'") ;
  196. }
  197. if( $group ) {
  198. $q->where("folder = '{$group}'");
  199. }
  200. $q->update( '#__extensions' )
  201. ->set("params = '{$params}'")
  202. ->where("element = '{$element}'")
  203. ;
  204. $db->setQuery($q);
  205. return $db->execute();
  206. }
  207. /**
  208. * Save component config to "config.json" in includes dir.
  209. *
  210. * @param mixed $params A config object, array or JRegistry object.
  211. * @param string $option Component option name.
  212. *
  213. * @return boolean Success or not.
  214. */
  215. public static function saveConfig($params, $option = null)
  216. {
  217. if( $params instanceof JRegistry ) {
  218. $params = (string) $params ;
  219. }else{
  220. $params = json_decode($params) ;
  221. }
  222. $path = AKHelper::_('path.getAdmin', $option) . '/includes/config.json' ;
  223. return JFile::write($path, $params) ;
  224. }
  225. /**
  226. * Get component version form manifest XML file.
  227. *
  228. * @param string $option Component option name.
  229. *
  230. * @return string Component version.
  231. */
  232. public static function getVersion($option = null)
  233. {
  234. if(!$option){
  235. $option = AKHelper::_('path.getOption') ;
  236. }
  237. if(isset(self::$version[$option])) {
  238. return self::$version[$option] ;
  239. }
  240. $xml = AKHelper::_('path.getAdmin').'/'.substr(AKHelper::_('path.getOption'), 4).'.xml' ;
  241. $xml = JFactory::getXML($xml, true) ;
  242. return self::$version[$option] = $xml->version ;
  243. }
  244. /**
  245. * A helper to add JProfiler log mark. Need to trun on the debug mode.
  246. *
  247. * @param string $text Log text.
  248. * @param string $namespace The JProfiler instance ID. Default is the core profiler "Application".
  249. */
  250. public static function mark($text, $namespace = null)
  251. {
  252. $app = JFactory::getApplication() ;
  253. if(!$namespace) {
  254. $namespace = 'Application' ;
  255. }
  256. if( !(JDEBUG && $namespace == 'Application') && !AKDEBUG) {
  257. return ;
  258. }
  259. if(!isset(self::$profiler[$namespace])) {
  260. jimport('joomla.error.profiler');
  261. self::$profiler[$namespace] = JProfiler::getInstance($namespace);
  262. // Get last page logs.
  263. self::$state_buffer = $app->getUserState('windwalker.system.profiler.'.$namespace);
  264. }
  265. self::$profiler[$namespace]->mark($text) ;
  266. // Save in session
  267. $app->setUserState('windwalker.system.profiler.'.$namespace, self::$profiler[$namespace]->getBuffer());
  268. }
  269. /**
  270. * Render the profiler log data, and echo it..
  271. *
  272. * @param string $namespace The JProfiler instance ID. Default is the core profiler "Application".
  273. */
  274. public static function renderProfiler($namespace = null)
  275. {
  276. $app = JFactory::getApplication() ;
  277. if(!$namespace) {
  278. $namespace = 'Application' ;
  279. }
  280. $buffer = 'No Profiler data.';
  281. if(isset(self::$profiler[$namespace])) {
  282. $_PROFILER = self::$profiler[$namespace] ;
  283. $buffer = $_PROFILER->getBuffer();
  284. $buffer = implode("\n<br />\n", $buffer) ;
  285. }else{
  286. $buffer = $app->getUserState('windwalker.system.profiler.'.$namespace);
  287. $buffer = $buffer ? implode("\n<br />\n", $buffer) : '';
  288. }
  289. $buffer = $buffer ? $buffer : 'No Profiler data.';
  290. // Get last page logs
  291. $state_buffer = self::$state_buffer ;
  292. if($state_buffer) {
  293. $state_buffer = implode("\n<br />\n", $state_buffer) ;
  294. $buffer = $state_buffer . "\n<br />---------<br />\n" . $buffer ;
  295. }
  296. // Render
  297. $buffer = "<pre><h3>WindWalker Debug [namespace: {$namespace}]: </h3>".$buffer.'</pre>' ;
  298. $app->setUserState('windwalker.system.profiler.'.$namespace, '');
  299. echo $buffer ;
  300. }
  301. }