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

/plugins/system/t3/includes/core/t3.php

https://gitlab.com/lankerd/paGO---Testing-Site
PHP | 482 lines | 357 code | 48 blank | 77 comment | 41 complexity | fe1871b0676bcc542f7097732641387a MD5 | raw file
  1. <?php
  2. /**
  3. *------------------------------------------------------------------------------
  4. * @package T3 Framework for Joomla!
  5. *------------------------------------------------------------------------------
  6. * @copyright Copyright (C) 2004-2013 JoomlArt.com. All Rights Reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. * @authors JoomlArt, JoomlaBamboo, (contribute to this project at github
  9. * & Google group to become co-author)
  10. * @Google group: https://groups.google.com/forum/#!forum/t3fw
  11. * @Link: http://t3-framework.org
  12. *------------------------------------------------------------------------------
  13. */
  14. // no direct access
  15. defined('_JEXEC') or die('Restricted access');
  16. /**
  17. * T3 class
  18. * Singleton class for T3
  19. * @package T3
  20. */
  21. class T3 {
  22. protected static $t3app = null;
  23. protected static $tmpl = null;
  24. /**
  25. * Import T3 Library
  26. *
  27. * @param string $package Object path that seperate by backslash (/)
  28. *
  29. * @return void
  30. */
  31. public static function import($package){
  32. $path = T3_ADMIN_PATH . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . strtolower($package) . '.php';
  33. if (file_exists($path)) {
  34. include_once $path;
  35. } else {
  36. trigger_error('T3::import not found object: ' . $package, E_USER_ERROR);
  37. }
  38. }
  39. /**
  40. * Register class with Joomla Loader. Override joomla core if $import_key avaiable
  41. *
  42. * @return void
  43. */
  44. public static function register ($class, $path, $import_key = null) {
  45. if (!empty($import_key)) jimport($import_key);
  46. JLoader::register ($class, $path);
  47. }
  48. /**
  49. * @param object $tpl template object to initialize if needed
  50. * @return bool|null|T3Admin
  51. */
  52. public static function getApp($tpl = null){
  53. if(empty(self::$t3app)){
  54. $japp = JFactory::getApplication();
  55. self::$t3app = $japp->isAdmin() ? self::getAdmin() : self::getSite($tpl);
  56. }
  57. return self::$t3app;
  58. }
  59. /**
  60. * initialize T3
  61. */
  62. public static function init ($xml) {
  63. $app = JFactory::getApplication();
  64. $input = $app->input;
  65. $coretheme = isset($xml->t3) && isset($xml->t3->base) ? trim((string)$xml->t3->base) : 'base';
  66. // check coretheme in media/t3/themes folder
  67. // if not exists, use default base theme in T3
  68. if (!$coretheme){
  69. $coretheme = 'base';
  70. }
  71. foreach(array(T3_EX_BASE_PATH, T3_ADMIN_PATH) as $basedir){
  72. if(is_dir($basedir . '/' . $coretheme)){
  73. if(is_file($basedir . '/' . $coretheme . '/define.php')){
  74. include_once ($basedir . '/' . $coretheme . '/define.php');
  75. }
  76. break;
  77. }
  78. }
  79. if(!defined('T3')){
  80. // get ready for the t3 core base theme
  81. include_once (T3_CORE_BASE_PATH . '/define.php');
  82. }
  83. if(!defined('T3')){
  84. T3::error(JText::sprintf('T3_MSG_FAILED_INIT_BASE', $coretheme));
  85. exit;
  86. }
  87. define ('T3_TEMPLATE', (String)$xml->tplname);
  88. define ('T3_TEMPLATE_URL', JURI::root(true).'/templates/'.T3_TEMPLATE);
  89. define ('T3_TEMPLATE_PATH', str_replace ('\\', '/', JPATH_ROOT) . '/templates/' . T3_TEMPLATE);
  90. define ('T3_TEMPLATE_REL', 'templates/' . T3_TEMPLATE);
  91. define ('T3_LOCAL_URL', T3_TEMPLATE_URL . '/' . T3_LOCAL_DIR);
  92. define ('T3_LOCAL_PATH', T3_TEMPLATE_PATH . '/' . T3_LOCAL_DIR);
  93. define ('T3_LOCAL_REL', T3_TEMPLATE_REL . '/' . T3_LOCAL_DIR);
  94. if ($input->getCmd('themer', 0)){
  95. define ('T3_THEMER', 1);
  96. }
  97. if (!$app->isAdmin()) {
  98. $params = $app->getTemplate(true)->params;
  99. define ('T3_DEV_FOLDER', $params->get ('t3-assets', 't3-assets') . '/dev');
  100. define ('T3_DEV_MODE', $params->get ('devmode', 0));
  101. } else {
  102. $params = self::getTemplate()->params;
  103. define ('T3_DEV_FOLDER', $params->get ('t3-assets', 't3-assets') . '/dev');
  104. }
  105. if (!is_dir(JPATH_ROOT.'/'.T3_DEV_FOLDER)) {
  106. jimport('joomla.filesystem.folder');
  107. JFolder::create(JPATH_ROOT.'/'.T3_DEV_FOLDER);
  108. }
  109. if($input->getCmd('t3lock', '')){
  110. JFactory::getSession()->set('T3.t3lock', $input->getCmd('t3lock', ''));
  111. $input->set('t3lock', null);
  112. }
  113. // load core library
  114. T3::import ('core/path');
  115. T3::import ('core/t3j');
  116. if (!$app->isAdmin()) {
  117. if(version_compare(JVERSION, '3.0', 'ge')){
  118. // override core joomla class
  119. // JViewLegacy
  120. T3::register('JViewLegacy', T3_ADMIN_PATH . '/includes/joomla30/viewlegacy.php');
  121. T3::register('JViewHtml', T3_ADMIN_PATH . '/includes/joomla30/viewhtml.php');
  122. // JModuleHelper
  123. T3::register('JModuleHelper', T3_ADMIN_PATH . '/includes/joomla30/modulehelper.php');
  124. // JPagination
  125. T3::register('JPagination', T3_ADMIN_PATH . '/includes/joomla30/pagination.php');
  126. // Register T3 Layout File to put a t3 base layer for layout files
  127. T3::register('JLayoutFile', T3_ADMIN_PATH . '/includes/joomla25/layout/file.php');
  128. } else {
  129. // override core joomla class
  130. // JView
  131. T3::register('JView', T3_ADMIN_PATH . '/includes/joomla25/view.php', 'joomla.application.component.view');
  132. // JModuleHelper
  133. T3::register('JModuleHelper', T3_ADMIN_PATH . '/includes/joomla25/modulehelper.php', 'joomla.application.module.helper');
  134. // JPagination
  135. T3::register('JPagination', T3_ADMIN_PATH . '/includes/joomla25/pagination.php', 'joomla.html.pagination');
  136. //register layout
  137. T3::register('JLayout', T3_ADMIN_PATH . '/includes/joomla25/layout/layout.php');
  138. T3::register('JLayoutBase', T3_ADMIN_PATH . '/includes/joomla25/layout/base.php');
  139. T3::register('JLayoutFile', T3_ADMIN_PATH . '/includes/joomla25/layout/file.php');
  140. T3::register('JLayoutHelper', T3_ADMIN_PATH . '/includes/joomla25/layout/helper.php');
  141. T3::register('JHtmlBootstrap', T3_ADMIN_PATH . '/includes/joomla25/html/bootstrap.php');
  142. T3::register('JHtmlBehavior', T3_ADMIN_PATH . '/includes/joomla25/html/behavior.php');
  143. T3::register('JHtmlString', T3_ADMIN_PATH . '/includes/joomla25/html/string.php');
  144. T3::register('JHtmlJquery', T3_ADMIN_PATH . '/includes/joomla25/html/jquery.php');
  145. // load j25 compat language
  146. JFactory::getLanguage()->load('plg_system_t3.j25.compat', JPATH_ADMINISTRATOR);
  147. }
  148. // import renderer
  149. T3::import('renderer/pageclass');
  150. T3::import('renderer/megamenu');
  151. T3::import('renderer/t3bootstrap');
  152. } else {
  153. }
  154. // capture for tm=1 => show theme magic
  155. if ($input->getCmd('tm') == 1) {
  156. $input->set('t3action', 'theme');
  157. $input->set('t3task', 'thememagic');
  158. }
  159. }
  160. public static function checkAction () {
  161. // excute action by T3
  162. if ($action = JFactory::getApplication()->input->getCmd ('t3action')) {
  163. T3::import ('core/action');
  164. T3Action::run ($action);
  165. }
  166. }
  167. /**
  168. * check for t3ajax action
  169. */
  170. public static function checkAjax () {
  171. // excute action by T3
  172. $input = JFactory::getApplication()->input;
  173. if ($input->getCmd ('t3ajax')) {
  174. T3::import('core/ajax');
  175. T3::import('renderer/t3ajax');
  176. //T3Ajax::processAjaxRule();
  177. JFactory::getApplication()->getTemplate(true)->params->set('mainlayout', 'ajax.' . $input->getCmd('f', 'html'));
  178. }
  179. }
  180. /**
  181. * get T3Admin object
  182. * @return T3Admin
  183. */
  184. public static function getAdmin(){
  185. T3::import ('core/admin');
  186. return new T3Admin();
  187. }
  188. /**
  189. * get T3Template object for frontend
  190. * @param $tpl
  191. * @return bool
  192. */
  193. public static function getSite($tpl){
  194. //when on site, the JDocumentHTML parameter must be pass
  195. if(empty($tpl)){
  196. return false;
  197. }
  198. $type = 'Template'. JFactory::getApplication()->input->getCmd ('t3tp', '');
  199. T3::import ('core/' . $type);
  200. // create global t3 template object
  201. $class = 'T3' . $type;
  202. return new $class($tpl);
  203. }
  204. /**
  205. * @param $msg
  206. * @param int $code
  207. * @throws Exception
  208. */
  209. public static function error($msg, $code = 500){
  210. if (JError::$legacy) {
  211. JError::setErrorHandling(E_ERROR, 'die');
  212. JError::raiseError($code, $msg);
  213. exit;
  214. } else {
  215. throw new Exception($msg, $code);
  216. }
  217. }
  218. /**
  219. * detect function to check a current template is T3 template
  220. * @return bool|SimpleXMLElement
  221. */
  222. public static function detect(){
  223. static $t3;
  224. if (!isset($t3)) {
  225. $t3 = false; // set false
  226. $app = JFactory::getApplication();
  227. $input = $app->input;
  228. // get template name
  229. $tplname = '';
  230. if($input->getCmd ('t3action') && $input->getInt('styleid', '')) {
  231. $tplname = self::getTemplate(true);
  232. } elseif ($app->isAdmin()) {
  233. // if not login, do nothing
  234. $user = JFactory::getUser();
  235. if (!$user->id){
  236. return false;
  237. }
  238. if($input->getCmd('option') == 'com_templates' &&
  239. (preg_match('/style\./', $input->getCmd('task')) ||
  240. $input->getCmd('view') == 'style' ||
  241. $input->getCmd('view') == 'template')){
  242. $db = JFactory::getDBO();
  243. $query = $db->getQuery(true);
  244. $id = $input->getInt('id');
  245. //when in POST the view parameter does not set
  246. if ($input->getCmd('view') == 'template') {
  247. $query
  248. ->select('element')
  249. ->from('#__extensions')
  250. ->where('extension_id='.(int)$id . ' AND type=' . $db->quote('template'));
  251. } else {
  252. $query
  253. ->select('template')
  254. ->from('#__template_styles')
  255. ->where('id='.(int)$id);
  256. }
  257. $db->setQuery($query);
  258. $tplname = $db->loadResult();
  259. }
  260. } else {
  261. $tplname = $app->getTemplate(false);
  262. }
  263. if ($tplname) {
  264. // parse xml
  265. $filePath = JPath::clean(JPATH_ROOT.'/templates/'.$tplname.'/templateDetails.xml');
  266. if (is_file ($filePath)) {
  267. $xml = $xml = simplexml_load_file($filePath);
  268. // check t3 or group=t3 (compatible with previous definition)
  269. if (isset($xml->t3) || (isset($xml->group) && strtolower($xml->group) == 't3')) {
  270. $xml->tplname = $tplname;
  271. $t3 = $xml;
  272. }
  273. }
  274. }
  275. }
  276. return $t3;
  277. }
  278. /**
  279. * get default template style
  280. */
  281. public static function getDefaultTemplate($name = false){
  282. static $template;
  283. if (!isset($template)) {
  284. $db = JFactory::getDbo();
  285. $query = $db->getQuery(true);
  286. $query
  287. ->select('id, home, template, s.params')
  288. ->from('#__template_styles as s')
  289. ->where('s.client_id = 0')
  290. ->where('s.home = \'1\'')
  291. ->where('e.enabled = 1')
  292. ->leftJoin('#__extensions as e ON e.element=s.template AND e.type='.$db->quote('template').' AND e.client_id=s.client_id');
  293. $db->setQuery($query);
  294. $result = $db->loadObject();
  295. $template = !empty($result) ? $result : false;
  296. }
  297. if($name && $template){
  298. return $template->template;
  299. }
  300. return $template;
  301. }
  302. /**
  303. * get the template object or template name
  304. * @param bool $name
  305. * @return mixed template object or template name
  306. */
  307. public static function getTemplate($name = false)
  308. {
  309. if(!isset(self::$tmpl) || !self::$tmpl){
  310. $app = JFactory::getApplication();
  311. $input = $app->input;
  312. $id = $input->getInt('styleid', $input->getInt('id'));
  313. if($id){
  314. $db = JFactory::getDbo();
  315. $query = $db->getQuery(true);
  316. $query
  317. ->select('template, params')
  318. ->from('#__template_styles')
  319. ->where('client_id = 0');
  320. if($app->isAdmin() && $input->get('view') == 'template' && defined('T3_TEMPLATE')){
  321. $query->where('template='. $db->quote(T3_TEMPLATE));
  322. } else {
  323. $query->where('id='. $id);
  324. }
  325. $db->setQuery($query);
  326. $template = $db->loadObject();
  327. if ($template) {
  328. $registry = new JRegistry;
  329. $registry->loadString($template->params);
  330. $template->params = $registry;
  331. }
  332. self::$tmpl = $template;
  333. }
  334. }
  335. if($name && self::$tmpl){
  336. return self::$tmpl->template;
  337. }
  338. return self::$tmpl;
  339. }
  340. /**
  341. * set caching template and its parameters
  342. * @param string $name
  343. * @param string $params
  344. */
  345. public static function setTemplate($name = '', $params = ''){
  346. if(!self::$tmpl){
  347. self::$tmpl = new stdClass;
  348. }
  349. if($name && $params){
  350. self::$tmpl->template = $name;
  351. self::$tmpl->params = $params;
  352. }
  353. }
  354. /**
  355. * get template parameters
  356. * @return JRegistry
  357. */
  358. public static function getTplParams()
  359. {
  360. $tmpl = self::getTemplate();
  361. return $tmpl ? $tmpl->params : new JRegistry; //empty registry ? or throw error
  362. }
  363. /**
  364. * check if current page is homepage
  365. */
  366. public static function isHome(){
  367. $active = JFactory::getApplication()->getMenu()->getActive();
  368. return (!$active || $active->home);
  369. }
  370. /**
  371. * fix ja back link
  372. * @param $buffer
  373. * @return mixed
  374. */
  375. public static function fixJALink($buffer){
  376. if(!self::isHome()){
  377. $buffer = preg_replace_callback('@<a[^>]*>JoomlArt.com</a>@i', array('T3', 'removeBacklink'), $buffer);
  378. }
  379. return $buffer;
  380. }
  381. /**
  382. * fix t3-framework.org back link
  383. * @param $buffer
  384. * @return mixed
  385. */
  386. public static function fixT3Link($buffer){
  387. if(!self::isHome()){
  388. $buffer = preg_replace_callback('@<a[^>]*>([^>]*)>T3 Framework</strong></a>@mi', array('T3', 'removeBacklink'), $buffer);
  389. }
  390. return $buffer;
  391. }
  392. /**
  393. * check nofollow attribute
  394. * @param $match
  395. * @return mixed
  396. */
  397. public static function removeBacklink($match){
  398. if($match && isset($match[0]) && strpos($match[0], 'rel="nofollow"') === false){
  399. $match[0] = str_replace('<a ', '<a rel="nofollow" ', $match[0]);
  400. }
  401. return $match[0];
  402. }
  403. }