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

/joomla/libraries/gantry/gantry.php

https://gitlab.com/ricardosanchez/prueba
PHP | 453 lines | 282 code | 53 blank | 118 comment | 72 complexity | 7013fc79e9c5dfba85c89495575e39be MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: gantry.php 12533 2013-08-08 17:30:05Z btowles $
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2016 RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. *
  8. * Gantry uses the Joomla Framework (http://www.joomla.org), a GNU/GPLv2 content management system
  9. *
  10. */
  11. defined('JPATH_BASE') or die();
  12. /** @var $gantry Gantry */
  13. global $gantry;
  14. $app = JFactory::getApplication();
  15. if (!defined('GANTRY_VERSION')) {
  16. /**
  17. * @name GANTRY_VERSION
  18. */
  19. define('GANTRY_VERSION', '4.1.31');
  20. if (!defined('DS')) {
  21. define('DS', DIRECTORY_SEPARATOR);
  22. }
  23. require_once (realpath(dirname(__FILE__)) . '/core/gantryloader.class.php');
  24. /**
  25. * @param string $path the gantry path to the class to import
  26. *
  27. * @return void
  28. */
  29. function gantry_import($path)
  30. {
  31. return GantryLoader::import($path);
  32. }
  33. /**
  34. * Adds a script file to the document with platform based checks
  35. *
  36. * @param $file
  37. *
  38. * @return void
  39. */
  40. function gantry_addScript($file)
  41. {
  42. gantry_import('core.gantryplatform');
  43. $platform = new GantryPlatform();
  44. $document = JFactory::getDocument();
  45. $filename = basename($file);
  46. $relative_path = dirname($file);
  47. // For local url path get the local path based on checks
  48. $file_path = gantry_getFilePath($file);
  49. $url_file_checks = $platform->getJSChecks($file_path, true);
  50. foreach ($url_file_checks as $url_file) {
  51. $full_path = realpath($url_file);
  52. if ($full_path !== false && file_exists($full_path)) {
  53. $document->addScript($relative_path . '/' . basename($full_path) . '?ver=4.1.31');
  54. break;
  55. }
  56. }
  57. }
  58. /**
  59. * Add inline script to the document
  60. *
  61. * @param $script
  62. *
  63. * @return void
  64. */
  65. function gantry_addInlineScript($script)
  66. {
  67. $document = JFactory::getDocument();
  68. $document->addScriptDeclaration($script);
  69. }
  70. /**
  71. * Add a css style file to the document with browser based checks
  72. *
  73. * @param $file
  74. *
  75. * @return void
  76. */
  77. function gantry_addStyle($file)
  78. {
  79. gantry_import('core.gantrybrowser');
  80. $browser = new GantryBrowser();
  81. $document = JFactory::getDocument();
  82. $filename = basename($file);
  83. $relative_path = dirname($file);
  84. // For local url path get the local path based on checks
  85. $file_path = gantry_getFilePath($file);
  86. $url_file_checks = $browser->getChecks($file_path, true);
  87. foreach ($url_file_checks as $url_file) {
  88. $full_path = realpath($url_file);
  89. if ($full_path !== false && file_exists($full_path)) {
  90. $document->addStyleSheet($relative_path . '/' . basename($full_path) . '?ver=4.1.31');
  91. }
  92. }
  93. }
  94. /**
  95. * Add inline css to the document
  96. *
  97. * @param $css
  98. *
  99. * @return void
  100. */
  101. function gantry_addInlineStyle($css)
  102. {
  103. $document = JFactory::getDocument();
  104. $document->addStyleDeclaration($css);
  105. }
  106. /**
  107. * Get the current template name either from the front end or the template being edited on the backend
  108. * @return null|string
  109. */
  110. function gantry_getTemplateById($id = 0)
  111. {
  112. $templates = gantry_getAllTemplates();
  113. $template = $templates[$id];
  114. return $template;
  115. }
  116. /**
  117. * @return array|mixed
  118. */
  119. function gantry_getAllTemplates()
  120. {
  121. $cache = JFactory::getCache('com_templates', '');
  122. $tag = JFactory::getLanguage()->getTag();
  123. $templates = $cache->get('templates0' . $tag);
  124. if ($templates === false) {
  125. // Load styles
  126. $db = JFactory::getDbo();
  127. $query = $db->getQuery(true);
  128. $query->select('id, home, template, params');
  129. $query->from('#__template_styles');
  130. $query->where('client_id = 0');
  131. $db->setQuery($query);
  132. $templates = $db->loadObjectList('id');
  133. foreach ($templates as &$template) {
  134. $registry = new JRegistry;
  135. $registry->loadString($template->params);
  136. $template->params = $registry;
  137. // Create home element
  138. if ($template->home == '1' && !isset($templates[0]) && $template->home == $tag) {
  139. $templates[0] = clone $template;
  140. }
  141. }
  142. $cache->store($templates, 'templates0' . $tag);
  143. }
  144. return $templates;
  145. }
  146. /**
  147. * @param $template_name
  148. *
  149. * @return bool
  150. */
  151. function gantry_getMasterTemplateStyleByName($template_name)
  152. {
  153. $templates = gantry_getAllTemplates();
  154. foreach ($templates as $template) {
  155. if ($template->template == $template_name && $template->params->get('master') == 'true') {
  156. return $template;
  157. }
  158. }
  159. return false;
  160. }
  161. /**
  162. * @return bool|string
  163. */
  164. function gantry_getTemplate()
  165. {
  166. $app = JFactory::getApplication();
  167. // if its an ajax call then return the requested template master
  168. if ($app->input->getWord('option') == 'com_gantry' && $app->input->getWord('task') == 'ajax') {
  169. $template_name = $app->input->getString('template');
  170. $template = gantry_getMasterTemplateStyleByName($template_name);
  171. } else {
  172. $template = $app->getTemplate(true);
  173. }
  174. return $template;
  175. }
  176. /**
  177. * @param int $id
  178. *
  179. * @return mixed
  180. */
  181. function gantry_getTemplateParams($id = 0)
  182. {
  183. $template = gantry_getTemplateById($id);
  184. return $template->params;
  185. }
  186. /**
  187. * @param $params
  188. * @param null $parent_name
  189. *
  190. * @return array
  191. */
  192. function gantry_flattenParams($params, $parent_name = null)
  193. {
  194. $values = array();
  195. foreach ($params as $param_name => $param_value) {
  196. $pname = (null != $parent_name) ? $parent_name . '-' : '';
  197. if (is_array($param_value)) {
  198. $sub_values = gantry_flattenParams($param_value, $param_name);
  199. foreach ($sub_values as $sub_value_name => $sub_value) {
  200. $values[$pname . $sub_value_name] = $sub_value;
  201. }
  202. } else {
  203. $values[$pname . $param_name] = $param_value;
  204. }
  205. }
  206. return $values;
  207. }
  208. /**
  209. * @param $url
  210. *
  211. * @return string
  212. */
  213. function gantry_getFilePath($url)
  214. {
  215. $uri = JURI::getInstance();
  216. $base = $uri->toString(array('scheme', 'host', 'port'));
  217. $path = JURI::Root(true);
  218. if ($url && $base && strpos($url, $base) !== false) $url = preg_replace('|^' . $base . '|', "", $url);
  219. if ($url && $path && strpos($url, $path) !== false) $url = preg_replace('|^' . $path . '|', '', $url);
  220. if (substr($url, 0, 1) != '/') $url = '/' . $url;
  221. $filepath = JPATH_SITE . $url;
  222. return $filepath;
  223. }
  224. /**
  225. *
  226. */
  227. function gantry_setup()
  228. {
  229. gantry_import('core.gantry');
  230. gantry_import('core.utilities.gantrycache');
  231. jimport('joomla.html.parameter');
  232. /** @var $gantry Gantry */
  233. global $gantry;
  234. $template = gantry_getTemplate();
  235. $template_name = $template->template;
  236. if ($template->params->get('master') != 'true') $template->params = gantry_getTemplateParams($template->params->get('master'));
  237. $conf = JFactory :: getConfig();
  238. $app = JFactory::getApplication();
  239. if ($template->params->get("cache.enabled", 1) == 1) {
  240. $cache = GantryCache::getCache(GantryCache::GROUP_NAME);
  241. $cache->addWatchFile(JPATH_SITE . '/templates/' . $template_name . '/templateDetails.xml');
  242. $cache->addWatchFile(JPATH_SITE . '/templates/' . $template_name . '/template-options.xml');
  243. $gantry = $cache->call('Gantry-' . $template_name, array('Gantry', 'getInstance'), array($template_name));
  244. } else {
  245. $gantry = Gantry::getInstance($template_name);
  246. }
  247. $gantry->init();
  248. }
  249. /**
  250. *
  251. */
  252. function gantry_template_initialize()
  253. {
  254. if (defined('GANTRY_INITTEMPLATE')) {
  255. return;
  256. }
  257. define('GANTRY_INITTEMPLATE', "GANTRY_INITTEMPLATE");
  258. /** @var $gantry Gantry */
  259. global $gantry;
  260. $gantry->initTemplate();
  261. }
  262. /**
  263. *
  264. */
  265. function gantry_admin_setup()
  266. {
  267. gantry_import('core.gantry');
  268. gantry_import('core.utilities.gantrycache');
  269. /** @var $gantry Gantry */
  270. global $gantry;
  271. $template_id = gantry_admin_getCurrentTemplateId();
  272. $template = gantry_getTemplateById($template_id);
  273. $cache = GantryCache::getCache(GantryCache::ADMIN_GROUP_NAME, null, true);
  274. $cache->addWatchFile(JPATH_SITE . '/templates/' . $template->template . '/templateDetails.xml');
  275. $cache->addWatchFile(JPATH_SITE . '/templates/' . $template->template . '/template-options.xml');
  276. $gantry = $cache->call('Gantry-' . $template->template, array(
  277. 'Gantry', 'getInstance'
  278. ), array($template->template));
  279. $gantry->adminInit();
  280. }
  281. /**
  282. * @return bool
  283. */
  284. function gantry_getCurrentTemplateId()
  285. {
  286. $id = false;
  287. $app = JFactory::getApplication();
  288. if (!$app->isAdmin()) {
  289. // get from ajax passed in
  290. if ($app->input->getString('option') == 'com_gantry' && $app->input->getString('task') == 'ajax') {
  291. $template = $app->input->getString('template');
  292. } else {
  293. $template = $app->getTemplate(true);
  294. }
  295. }
  296. return $id;
  297. }
  298. /**
  299. * Get the template style id that is being worked with on the admin side
  300. *
  301. * @return bool|int
  302. */
  303. function gantry_admin_getCurrentTemplateId()
  304. {
  305. $id = false;
  306. $app = JFactory::getApplication();
  307. if ($app->isAdmin()) {
  308. $session = JFactory::getSession();
  309. $session_registry = $session->get('registry');
  310. if ($app->input->getInt('id', 0) > 0 && $app->input->getString('option') == 'com_gantry' && $app->input->getString('layout') == 'edit'
  311. ) {
  312. $id = $app->input->getInt('id', 0);
  313. } else if ($app->input->getString('option') == 'com_gantry' &&$app->input->getString('task') == 'ajax') {
  314. $name = $app->input->getString('template');
  315. $id = gantry_getMasterTemplateStyleByName($name)->id;
  316. } else if ($session_registry->exists('com_gantry.edit.template.id')) {
  317. $session_ids = $session_registry->get('com_gantry.edit.template.id');
  318. $id = (int)array_shift($session_ids);
  319. }
  320. }
  321. return $id;
  322. }
  323. /**
  324. * @param $filename
  325. */
  326. function gantry_run_alternate_template($filename)
  327. {
  328. /** @var $gantry Gantry */
  329. global $gantry;
  330. // $filename comes from included scope
  331. $ext = substr($filename, strrpos($filename, '.'));
  332. $file = basename($filename, $ext);
  333. $checks = $gantry->browser->getChecks($filename);
  334. $platform = $gantry->browser->platform;
  335. $enabled = $gantry->get($platform . '-enabled', 0);
  336. $view = 'viewswitcher-' . $gantry->get('template_prefix') . $platform . '-switcher';
  337. // flip to get most specific first
  338. $checks = array_reverse($checks);
  339. // remove the default index.php page
  340. array_pop($checks);
  341. $template_paths = array(
  342. $gantry->templatePath, $gantry->gantryPath . '/' . 'tmpl'
  343. );
  344. foreach ($template_paths as $template_path) {
  345. if (file_exists($template_path) && is_dir($template_path)) {
  346. foreach ($checks as $check) {
  347. $check_path = preg_replace("/\?(.*)/", '', $template_path . '/' . $check);
  348. if (file_exists($check_path) && is_readable($check_path) && $enabled && JFactory::getApplication()->input->cookie->get($view, true, 'string') != '0') {
  349. // include the wanted index page
  350. ob_start();
  351. include_once($check_path);
  352. $contents = ob_get_contents();
  353. ob_end_clean();
  354. $gantry->altindex = $contents;
  355. break;
  356. }
  357. }
  358. if ($gantry->altindex !== false) break;
  359. }
  360. }
  361. }
  362. /**
  363. * @return bool
  364. */
  365. function gantry_is_template_include()
  366. {
  367. /** @var $gantry Gantry */
  368. global $gantry;
  369. $stack = debug_backtrace();
  370. if ($stack[1]['file'] == realpath($gantry->templatePath . '/lib/gantry/gantry.php')) {
  371. return true;
  372. }
  373. return false;
  374. }
  375. // Run the appropriate init
  376. $app = JFactory::getApplication();
  377. if ($app->isAdmin()) {
  378. gantry_admin_setup();
  379. } else {
  380. gantry_setup();
  381. if (!gantry_is_template_include()) {
  382. // setup for post
  383. $dispatcher = JDispatcher::getInstance();
  384. $dispatcher->register('onAfterDispatch', 'gantry_template_initialize');
  385. $dispatcher->register('onGantryTemplateInit', 'gantry_run_alternate_template');
  386. } else {
  387. gantry_template_initialize();
  388. }
  389. if (!isset($gantry_calling_file))
  390. {
  391. $backtrace = debug_backtrace();
  392. $gantry_calling_file = $backtrace[0]['file'];
  393. }
  394. $app->triggerEvent('onGantryTemplateInit', array($gantry_calling_file));
  395. }
  396. }
  397. // let run on the main template page
  398. if (gantry_is_template_include()) {
  399. gantry_run_alternate_template($filename);
  400. }