PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/web/concrete/core/libraries/view.php

https://github.com/glockops/concrete5
PHP | 995 lines | 550 code | 135 blank | 310 comment | 171 complexity | d18ab6d4db487b6cb7db49480de94212 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, BSD-3-Clause
  1. <?
  2. defined('C5_EXECUTE') or die("Access Denied.");
  3. /**
  4. * @package Core
  5. * @category Concrete
  6. * @author Andrew Embler <andrew@concrete5.org>
  7. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  8. * @license http://www.concrete5.org/license/ MIT License
  9. *
  10. */
  11. /**
  12. * A generic object that every front-end template (view) or page extends.
  13. * @package Core
  14. * @author Andrew Embler <andrew@concrete5.org>
  15. * @category Concrete
  16. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  17. * @license http://www.concrete5.org/license/ MIT License
  18. *
  19. */
  20. class Concrete5_Library_View extends Object {
  21. /**
  22. * @var string
  23. */
  24. private $viewPath;
  25. /**
  26. * @var string
  27. */
  28. protected $pkgHandle;
  29. /**
  30. * @var bool
  31. */
  32. protected $disableContentInclude = false;
  33. /**
  34. * controller used by this particular view
  35. * @access public
  36. * @var object
  37. */
  38. public $controller;
  39. /**
  40. * An array of items that get loaded into a page's header
  41. * @var array
  42. */
  43. private $headerItems = array();
  44. /**
  45. * An array of items that get loaded into just before body close
  46. * @var array
  47. */
  48. private $footerItems = array();
  49. /**
  50. * themePaths holds the various hard coded paths to themes
  51. * @access private
  52. * @var array
  53. */
  54. private $themePaths = array();
  55. /**
  56. * @var bool
  57. */
  58. private $areLinksDisabled = false;
  59. /**
  60. * editing mode is enabled or not
  61. * @access private
  62. * @var boolean
  63. */
  64. private $isEditingEnabled = true;
  65. /**
  66. * getInstance() grabs one instance of the view w/the singleton pattern
  67. * @return View
  68. */
  69. public function getInstance() {
  70. static $instance;
  71. if (!isset($instance)) {
  72. $instance = new View();
  73. }
  74. return $instance;
  75. }
  76. /**
  77. * This grabs the theme for a particular path, if one exists in the themePaths array
  78. * @access private
  79. * @param string $path
  80. * @return string $theme
  81. */
  82. private function getThemeFromPath($path) {
  83. // there's probably a more efficient way to do this
  84. $theme = false;
  85. $txt = Loader::helper('text');
  86. foreach($this->themePaths as $lp => $layout) {
  87. if ($txt->fnmatch($lp, $path)) {
  88. $theme = $layout;
  89. break;
  90. }
  91. }
  92. return $theme;
  93. }
  94. /**
  95. * Returns a stylesheet found in a themes directory - but FIRST passes it through the tools CSS handler
  96. * in order to make certain style attributes found inside editable
  97. * @param string $stylesheet
  98. */
  99. public function getStyleSheet($stylesheet) {
  100. if ($this->isPreview()) {
  101. return REL_DIR_FILES_TOOLS . '/css/' . DIRNAME_THEMES . '/' . $this->getThemeHandle() . '/' . $stylesheet . '?mode=preview&time=' . time();
  102. }
  103. $pt = PageTheme::getByHandle($this->getThemeHandle());
  104. $file = $this->getThemePath() . '/' . $stylesheet;
  105. $cacheFile = DIR_FILES_CACHE . '/' . DIRNAME_CSS . '/' . $this->getThemeHandle() . '/' . $stylesheet;
  106. $env = Environment::get();
  107. $themeRec = $env->getUncachedRecord(DIRNAME_THEMES . '/' . $this->getThemeHandle() . '/' . $stylesheet, $pt->getPackageHandle());
  108. if (file_exists($cacheFile) && $themeRec->exists()) {
  109. if (filemtime($cacheFile) > filemtime($themeRec->file)) {
  110. return REL_DIR_FILES_CACHE . '/' . DIRNAME_CSS . '/' . $this->getThemeHandle() . '/' . $stylesheet;
  111. }
  112. }
  113. if ($themeRec->exists()) {
  114. $themeFile = $themeRec->file;
  115. if (!file_exists(DIR_FILES_CACHE . '/' . DIRNAME_CSS)) {
  116. @mkdir(DIR_FILES_CACHE . '/' . DIRNAME_CSS);
  117. }
  118. if (!file_exists(DIR_FILES_CACHE . '/' . DIRNAME_CSS . '/' . $this->getThemeHandle())) {
  119. @mkdir(DIR_FILES_CACHE . '/' . DIRNAME_CSS . '/' . $this->getThemeHandle());
  120. }
  121. $fh = Loader::helper('file');
  122. $stat = filemtime($themeFile);
  123. if (!file_exists(dirname($cacheFile))) {
  124. @mkdir(dirname($cacheFile), DIRECTORY_PERMISSIONS_MODE, true);
  125. }
  126. $style = $pt->parseStyleSheet($stylesheet);
  127. $r = @file_put_contents($cacheFile, $style);
  128. if ($r) {
  129. return REL_DIR_FILES_CACHE . '/' . DIRNAME_CSS . '/' . $this->getThemeHandle() . '/' . $stylesheet;
  130. } else {
  131. return $this->getThemePath() . '/' . $stylesheet;
  132. }
  133. }
  134. }
  135. /**
  136. * Function responsible for adding header items within the context of a view.
  137. * @access private
  138. */
  139. public function addHeaderItem($item, $namespace = 'VIEW') {
  140. if ($this->resolveItemConflicts($item)) {
  141. $this->headerItems[$namespace][] = $item;
  142. }
  143. }
  144. /**
  145. * Function responsible for adding footer items within the context of a view.
  146. * @access private
  147. */
  148. public function addFooterItem($item, $namespace = 'VIEW') {
  149. if ($this->resolveItemConflicts($item)) {
  150. $this->footerItems[$namespace][] = $item;
  151. }
  152. }
  153. /**
  154. * Internal helper function for addHeaderItem() and addFooterItem().
  155. * Looks through header and footer items for anything of the same type
  156. * and having the same "unique handle" as the given item.
  157. *
  158. * HOW TO USE THIS FUNCTION:
  159. * When calling this function, just pass the first $item argument
  160. * (the second optional argument is only for our own recursive use).
  161. * If we return FALSE, that means the given item should NOT be added to headerItems/footerItems.
  162. * If we return TRUE, then go ahead and add the item to headerItems/footerItems.
  163. *
  164. * NOTE: THIS FUNCTION HAS POTENTIAL SIDE-EFFECTS (IN ADDITION TO RETURN VALUE)...
  165. * ~If no duplicate is found, we return TRUE (with no side-effects).
  166. * ~If a duplicate is found and the given item has a HIGHER version than the found item,
  167. * we return TRUE **AND** we remove the found duplicate from headerItems or footerItems!!
  168. * ~If a duplicate is found and the given item does NOT have a higher version than
  169. * the found item, we return FALSE (with no side-effects).
  170. */
  171. private function resolveItemConflicts($checkItem, &$againstItems = null) {
  172. //Only check items that have "unique handles"
  173. if (empty($checkItem->handle)) {
  174. return true;
  175. }
  176. //Recursively check header items AND footer items
  177. if (is_null($againstItems)) {
  178. return ($this->resolveItemConflicts($checkItem, $this->headerItems) && $this->resolveItemConflicts($checkItem, $this->footerItems));
  179. }
  180. //Loop through all items and check for duplicates
  181. foreach ($againstItems as $itemNamespace => $namespaceItems) {
  182. foreach ($namespaceItems as $itemKey => $againstItem) {
  183. //Check the "unique handles"
  184. if (!empty($againstItem->handle) && (strtolower($checkItem->handle['handle']) == strtolower($againstItem->handle['handle']))) {
  185. //Check the item types (so js and css items can have the same handle without conflicting)
  186. //Note that we consider both the JavaScript and InlineScript items to be the same "type".
  187. $checkClass = get_class($checkItem);
  188. $againstClass = get_class($againstItem);
  189. if (($checkClass == $againstClass) || (!array_diff(array($checkClass, $againstClass), array('JavaScriptOutputObject', 'InlineScriptOutputObject')))) {
  190. //Does the given item have a higher version than the existing found item?
  191. if (version_compare($checkItem->handle['version'], $againstItem->handle['version'], '>')) {
  192. //Yes (new item is higher) so remove old item
  193. // and return true to indicate that the new item should be added.
  194. unset($againstItems[$itemNamespace][$itemKey]); // bug note: if we didn't return in the next line, this would cause problems the next time the loop iterated!
  195. return true;
  196. } else {
  197. //No (new item is not higher) so leave old item where it is
  198. // and return false to indicate that the new item should *not* be added.
  199. return false;
  200. }
  201. }
  202. }
  203. }
  204. }
  205. //No duplicates found, so return true to indicate that it's okay to add the item.
  206. return true;
  207. }
  208. /**
  209. * returns an array of string header items, typically inserted into the html <head> of a page through the header_required element
  210. * @return array
  211. */
  212. public function getHeaderItems() {
  213. //Combine items from all namespaces into one list
  214. $a1 = (is_array($this->headerItems['CORE'])) ? $this->headerItems['CORE'] : array();
  215. $a2 = (is_array($this->headerItems['VIEW'])) ? $this->headerItems['VIEW'] : array();
  216. $a3 = (is_array($this->headerItems['CONTROLLER'])) ? $this->headerItems['CONTROLLER'] : array();
  217. $items = array_merge($a1, $a2, $a3);
  218. //Remove exact string duplicates (items whose string representations are equal)
  219. if (version_compare(PHP_VERSION, '5.2.9', '<')) {
  220. $items = array_unique($items);
  221. } else {
  222. // stupid PHP (see http://php.net/array_unique#refsect1-function.array-unique-changelog )
  223. $items = array_unique($items, SORT_STRING);
  224. }
  225. return $items;
  226. }
  227. /**
  228. * returns an array of string footer items, typically inserted into the html before the close of the </body> tag of a page through the footer_required element
  229. * @return array
  230. */
  231. public function getFooterItems() {
  232. //Combine items from all namespaces into one list
  233. $a1 = (is_array($this->footerItems['CORE'])) ? $this->footerItems['CORE'] : array();
  234. $a2 = (is_array($this->footerItems['VIEW'])) ? $this->footerItems['VIEW'] : array();
  235. $a3 = (is_array($this->footerItems['CONTROLLER'])) ? $this->footerItems['CONTROLLER'] : array();
  236. $a4 = (is_array($this->footerItems['SCRIPT'])) ? $this->footerItems['SCRIPT'] : array();
  237. $items = array_merge($a1, $a2, $a3, $a4);
  238. //Remove exact string duplicates (items whose string representations are equal)
  239. if (version_compare(PHP_VERSION, '5.2.9', '<')) {
  240. $items = array_unique($items);
  241. } else {
  242. // stupid PHP (see http://php.net/array_unique#refsect1-function.array-unique-changelog )
  243. $items = array_unique($items, SORT_STRING);
  244. }
  245. //Also remove items having exact string duplicates in the header
  246. $headerItems = $this->getHeaderItems();
  247. $retItems = array();
  248. foreach($items as $it) {
  249. if (!in_array($it, $headerItems)) {
  250. $retItems[] = $it;
  251. }
  252. }
  253. return $retItems;
  254. }
  255. /**
  256. * Function responsible for outputting header items
  257. * @access private
  258. */
  259. public function outputHeaderItems() {
  260. $items = $this->getHeaderItems();
  261. foreach($items as $hi) {
  262. print $hi; // caled on two seperate lines because of pre php 5.2 __toString issues
  263. print "\n";
  264. }
  265. }
  266. /**
  267. * Function responsible for outputting footer items
  268. * @access private
  269. */
  270. public function outputFooterItems() {
  271. $items = $this->getFooterItems();
  272. foreach($items as $hi) {
  273. print $hi; // caled on two seperate lines because of pre php 5.2 __toString issues
  274. print "\n";
  275. }
  276. }
  277. /**
  278. * @param string
  279. * @return mixed
  280. */
  281. public function field($fieldName) {
  282. return $this->controller->field($fieldName);
  283. }
  284. /**
  285. * @access private
  286. * @return void
  287. */
  288. public function enablePreview() {
  289. $this->isPreview = true;
  290. }
  291. /**
  292. * @access private
  293. * @return bool
  294. */
  295. public function isPreview() {
  296. return $this->isPreview;
  297. }
  298. /**
  299. * @access private
  300. * @return void
  301. */
  302. public function disableLinks() {
  303. $this->areLinksDisabled = true;
  304. }
  305. /**
  306. * @access private
  307. * @return void
  308. */
  309. public function enableLinks() {
  310. $this->areLinksDisabled = false;
  311. }
  312. /**
  313. * @access private
  314. * @return bool
  315. */
  316. public function areLinksDisabled() {
  317. return $this->areLinksDisabled;
  318. }
  319. /**
  320. * Returns the path used to access this view
  321. * @return string
  322. */
  323. private function getViewPath() {
  324. return $this->viewPath;
  325. }
  326. /**
  327. * Returns the handle of the currently active theme
  328. * @return string
  329. */
  330. public function getThemeHandle() { return $this->ptHandle;}
  331. /**
  332. * gets the theme include file for this particular view
  333. * @access public
  334. * @return string $theme
  335. */
  336. public function getTheme() { return $this->theme;}
  337. /**
  338. * gets the relative theme path for use in templates
  339. * @access public
  340. * @return string $themePath
  341. */
  342. public function getThemePath() { return $this->themePath; }
  343. /**
  344. * set directory of current theme for use when loading an element
  345. * @access public
  346. * @param string $path
  347. */
  348. public function setThemeDirectory($path) { $this->themeDir=$path; }
  349. /**
  350. * get directory of current theme for use when loading an element
  351. * @access public
  352. * @return string $themeDir
  353. */
  354. public function getThemeDirectory() {return $this->themeDir;}
  355. /**
  356. * used by the theme_paths and site_theme_paths files in config/ to hard coded certain paths to various themes
  357. * @access public
  358. * @param $path string
  359. * @param $theme object, if null site theme is default
  360. * @return void
  361. */
  362. public function setThemeByPath($path, $theme = NULL) {
  363. if ($theme != VIEW_CORE_THEME && $theme != 'dashboard') { // this is a hack until we figure this code out.
  364. if (is_string($theme)) {
  365. $pageTheme = PageTheme::getByHandle($theme);
  366. if(is_object($pageTheme) && $pageTheme->getThemeHandle() == $theme) { // is it the theme that's been requested?
  367. $theme = $pageTheme;
  368. }
  369. }
  370. }
  371. $this->themePaths[$path] = $theme;
  372. }
  373. /**
  374. * Returns the value of the item in the POST array.
  375. * @access public
  376. * @param $key
  377. * @return void
  378. */
  379. public function post($key) {
  380. return $this->controller->post($key);
  381. }
  382. /**
  383. * gets the collection object for the current view
  384. * @access public
  385. * @return Collection Object $c
  386. */
  387. public function getCollectionObject() {
  388. return $this->c;
  389. }
  390. /**
  391. * sets the collection object for the current view
  392. * @access public
  393. * @return void
  394. */
  395. public function setCollectionObject($c) {
  396. $this->c = $c;
  397. }
  398. /**
  399. * Includes file from the current theme path. Similar to php's include().
  400. * Files included with this function will have all variables set using $this->controller->set() in their local scope,
  401. * As well as access to all that controller's helper objects.
  402. * @access public
  403. * @param string $file
  404. * @param array $args
  405. * @return void
  406. */
  407. public function inc($file, $args = array()) {
  408. extract($args);
  409. if (isset($this->c)) {
  410. $c = $this->c;
  411. }
  412. extract($this->controller->getSets());
  413. extract($this->controller->getHelperObjects());
  414. $env = Environment::get();
  415. include($env->getPath(DIRNAME_THEMES . '/' . $this->getThemeHandle() . '/' . $file, $this->pkgHandle));
  416. }
  417. /**
  418. * editing is enabled true | false
  419. * @access private
  420. * @return boolean
  421. */
  422. public function editingEnabled() {
  423. return $this->isEditingEnabled;
  424. }
  425. /**
  426. * set's editing to disabled
  427. * @access private
  428. * @return void
  429. */
  430. public function disableEditing() {
  431. $this->isEditingEnabled = false;
  432. }
  433. /**
  434. * sets editing to enabled
  435. * @access private
  436. * @return void
  437. */
  438. public function enableEditing() {
  439. $this->isEditingEnabled = true;
  440. }
  441. /**
  442. * This is rarely used. We want to render another view
  443. * but keep the current controller. Views should probably not
  444. * auto-grab the controller anyway but whatever
  445. * @access private
  446. * @param object $cnt
  447. * @return void
  448. */
  449. public function setController($cnt) {
  450. $this->controller = $cnt;
  451. }
  452. /**
  453. * checks the current view to see if you're in that page's "section" (top level)
  454. * (with one exception: passing in the home page url ('' or '/') will always return false)
  455. * @access public
  456. * @param string $url
  457. * @return boolean | void
  458. */
  459. public function section($url) {
  460. $cPath = Page::getCurrentPage()->getCollectionPath();
  461. if (!empty($cPath)) {
  462. $url = '/' . trim($url, '/');
  463. if (strpos($cPath, $url) !== false && strpos($cPath, $url) == 0) {
  464. return true;
  465. }
  466. }
  467. }
  468. /**
  469. * url is a utility function that is used inside a view to setup urls w/tasks and parameters
  470. * @access public
  471. * @param string $action
  472. * @param string $task
  473. * @return string $url
  474. */
  475. public static function url($action, $task = null) {
  476. $dispatcher = '';
  477. if ((!defined('URL_REWRITING_ALL')) || (!URL_REWRITING_ALL)) {
  478. $dispatcher = '/' . DISPATCHER_FILENAME;
  479. }
  480. $action = trim($action, '/');
  481. if ($action == '') {
  482. return DIR_REL . '/';
  483. }
  484. // if a query string appears in this variable, then we just pass it through as is
  485. if (strpos($action, '?') > -1) {
  486. return DIR_REL . $dispatcher. '/' . $action;
  487. } else {
  488. $_action = DIR_REL . $dispatcher. '/' . $action . '/';
  489. }
  490. if ($task != null) {
  491. if (ENABLE_LEGACY_CONTROLLER_URLS) {
  492. $_action .= '-/' . $task;
  493. } else {
  494. $_action .= $task;
  495. }
  496. $args = func_get_args();
  497. if (count($args) > 2) {
  498. for ($i = 2; $i < count($args); $i++){
  499. $_action .= '/' . $args[$i];
  500. }
  501. }
  502. if (strpos($_action, '?') === false) {
  503. $_action .= '/';
  504. }
  505. }
  506. return $_action;
  507. }
  508. public function checkMobileView() {
  509. if(isset($_COOKIE['ccmDisableMobileView']) && $_COOKIE['ccmDisableMobileView'] == true) {
  510. define('MOBILE_THEME_IS_ACTIVE', false);
  511. return false; // break out if we've said we don't want the mobile theme
  512. }
  513. $page = Page::getCurrentPage();
  514. if($page instanceof Page && $page->isAdminArea()) {
  515. define('MOBILE_THEME_IS_ACTIVE', false);
  516. return false; // no mobile theme for the dashboard
  517. }
  518. Loader::library('3rdparty/mobile_detect');
  519. $md = new Mobile_Detect();
  520. if ($md->isMobile()) {
  521. $themeId = Config::get('MOBILE_THEME_ID');
  522. if ($themeId > 0) {
  523. $mobileTheme = PageTheme::getByID($themeId);
  524. if($mobileTheme instanceof PageTheme) {
  525. define('MOBILE_THEME_IS_ACTIVE',true);
  526. // we have to grab the instance of the view
  527. // since on_page_view doesn't give it to us
  528. $this->setTheme($mobileTheme);
  529. }
  530. }
  531. }
  532. if (!defined('MOBILE_THEME_IS_ACTIVE')) {
  533. define('MOBILE_THEME_IS_ACTIVE', false);
  534. }
  535. }
  536. /**
  537. * A shortcut to posting back to the current page with a task and optional parameters. Only works in the context of
  538. * @param string $action
  539. * @param string $task
  540. * @return string $url
  541. */
  542. public function action($action, $task = null) {
  543. $a = func_get_args();
  544. array_unshift($a, $this->viewPath);
  545. $ret = call_user_func_array(array($this, 'url'), $a);
  546. return $ret;
  547. }
  548. /**
  549. * render's a fata error using the built-in view. This is currently only
  550. * used when the database connection fails
  551. * @access public
  552. * @param string $title
  553. * @param string $error
  554. * @return void
  555. */
  556. public function renderError($title, $error, $errorObj = null) {
  557. $innerContent = $error;
  558. $titleContent = $title;
  559. header('HTTP/1.1 500 Internal Server Error');
  560. if (!isset($this) || (!$this)) {
  561. $v = new View();
  562. $v->setThemeForView(DIRNAME_THEMES_CORE, FILENAME_THEMES_ERROR . '.php', true);
  563. include($v->getTheme());
  564. exit;
  565. }
  566. if (!isset($this->theme) || (!$this->theme) || (!file_exists($this->theme))) {
  567. $this->setThemeForView(DIRNAME_THEMES_CORE, FILENAME_THEMES_ERROR . '.php', true);
  568. include($this->theme);
  569. exit;
  570. } else {
  571. Loader::element('error_fatal', array('innerContent' => $innerContent,
  572. 'titleContent' => $titleContent));
  573. }
  574. }
  575. /**
  576. * sets the current theme
  577. * @access public
  578. * @param string $theme
  579. * @return void
  580. */
  581. public function setTheme($theme) {
  582. $this->themeOverride = $theme;
  583. }
  584. /**
  585. * set theme takes either a text-based theme ("concrete" or "dashboard" or something)
  586. * or a PageTheme object and sets information in the view about that theme. This is called internally
  587. * and is always passed the correct item based on context
  588. *
  589. * @access protected
  590. * @param PageTheme object $pl
  591. * @param string $filename
  592. * @param boolean $wrapTemplateInTheme
  593. * @return void
  594. */
  595. protected function setThemeForView($pl, $filename, $wrapTemplateInTheme = false) {
  596. // wrapTemplateInTheme gets set to true if we're passing the filename of a single page or page type file through
  597. $pkgID = 0;
  598. $env = Environment::get();
  599. if ($pl instanceof PageTheme) {
  600. $this->ptHandle = $pl->getThemeHandle();
  601. if ($pl->getPackageID() > 0) {
  602. $pkgID = $pl->getPackageID();
  603. $this->pkgHandle = $pl->getPackageHandle();
  604. }
  605. $rec = $env->getRecord(DIRNAME_THEMES . '/' . $pl->getThemeHandle() . '/' . $filename, $this->pkgHandle);
  606. if (!$rec->exists()) {
  607. if ($wrapTemplateInTheme) {
  608. $theme = $env->getPath(DIRNAME_THEMES . '/' . $pl->getThemeHandle() . '/' . FILENAME_THEMES_VIEW, $this->pkgHandle);
  609. } else {
  610. $theme = $env->getPath(DIRNAME_THEMES . '/' . $pl->getThemeHandle() . '/' . FILENAME_THEMES_DEFAULT, $this->pkgHandle);
  611. }
  612. } else {
  613. $theme = $rec->file;
  614. $this->disableContentInclude = true;
  615. }
  616. $themeDir = str_replace('/' . FILENAME_THEMES_DEFAULT, '', $env->getPath(DIRNAME_THEMES . '/' . $pl->getThemeHandle() . '/' . FILENAME_THEMES_DEFAULT, $this->pkgHandle));
  617. $themePath = str_replace('/' . FILENAME_THEMES_DEFAULT, '', $env->getURL(DIRNAME_THEMES . '/' . $pl->getThemeHandle() . '/' . FILENAME_THEMES_DEFAULT, $this->pkgHandle));
  618. } else {
  619. $this->ptHandle = $pl;
  620. if (file_exists(DIR_FILES_THEMES . '/' . $pl . '/' . $filename)) {
  621. $themePath = DIR_REL . '/' . DIRNAME_THEMES . '/' . $pl;
  622. $theme = DIR_FILES_THEMES . "/" . $pl . '/' . $filename;
  623. $themeDir = DIR_FILES_THEMES . "/" . $pl;
  624. } else if (file_exists(DIR_FILES_THEMES . '/' . $pl . '/' . FILENAME_THEMES_VIEW)) {
  625. $themePath = DIR_REL . '/' . DIRNAME_THEMES . '/' . $pl;
  626. $theme = DIR_FILES_THEMES . "/" . $pl . '/' . FILENAME_THEMES_VIEW;
  627. $themeDir = DIR_FILES_THEMES . "/" . $pl;
  628. } else if (file_exists(DIR_FILES_THEMES . '/' . DIRNAME_THEMES_CORE . '/' . $pl . '.php')) {
  629. $theme = DIR_FILES_THEMES . '/' . DIRNAME_THEMES_CORE . "/" . $pl . '.php';
  630. $themeDir = DIR_FILES_THEMES . '/' . DIRNAME_THEMES_CORE;
  631. } else if (file_exists(DIR_FILES_THEMES_CORE . "/" . $pl . '/' . $filename)) {
  632. $themePath = ASSETS_URL . '/' . DIRNAME_THEMES . '/' . DIRNAME_THEMES_CORE . '/' . $pl;
  633. $theme = DIR_FILES_THEMES_CORE . "/" . $pl . '/' . $filename;
  634. $themeDir = DIR_FILES_THEMES_CORE . "/" . $pl;
  635. } else if (file_exists(DIR_FILES_THEMES_CORE . "/" . $pl . '/' . FILENAME_THEMES_VIEW)) {
  636. $themePath = ASSETS_URL . '/' . DIRNAME_THEMES . '/' . DIRNAME_THEMES_CORE . '/' . $pl;
  637. $theme = DIR_FILES_THEMES_CORE . "/" . $pl . '/' . FILENAME_THEMES_VIEW;
  638. $themeDir = DIR_FILES_THEMES_CORE . "/" . $pl;
  639. } else if (file_exists(DIR_FILES_THEMES_CORE_ADMIN . "/" . $pl . '.php')) {
  640. $theme = DIR_FILES_THEMES_CORE_ADMIN . "/" . $pl . '.php';
  641. $themeDir = DIR_FILES_THEMES_CORE_ADMIN;
  642. }
  643. }
  644. $this->theme = $theme;
  645. $this->themePath = $themePath;
  646. $this->themeDir = $themeDir;
  647. $this->themePkgID = $pkgID;
  648. }
  649. public function escape($text){
  650. Loader::helper('text');
  651. return TextHelper::sanitize($text);
  652. }
  653. /**
  654. * render takes one argument - the item being rendered - and it can either be a path or a page object
  655. * @access public
  656. * @param string $view
  657. * @param array $args
  658. * @return void
  659. */
  660. public function render($view, $args = null) {
  661. if (is_array($args)) {
  662. extract($args);
  663. }
  664. // strip off a slash if there is one at the end
  665. if (is_string($view)) {
  666. if (substr($view, strlen($view) - 1) == '/') {
  667. $view = substr($view, 0, strlen($view) - 1);
  668. }
  669. }
  670. $dsh = Loader::helper('concrete/dashboard');
  671. $wrapTemplateInTheme = false;
  672. $this->checkMobileView();
  673. if (defined('DB_DATABASE') && ($view !== '/upgrade')) {
  674. Events::fire('on_start', $this);
  675. }
  676. // Extract controller information from the view, and put it in the current context
  677. if (!isset($this->controller)) {
  678. $this->controller = Loader::controller($view);
  679. $this->controller->setupAndRun();
  680. }
  681. if ($this->controller->getRenderOverride() != '') {
  682. $view = $this->controller->getRenderOverride();
  683. }
  684. // Determine which inner item to load, load it, and stick it in $innerContent
  685. $content = false;
  686. ob_start();
  687. if ($view instanceof Page) {
  688. $_pageBlocks = $view->getBlocks();
  689. if (!$dsh->inDashboard()) {
  690. $_pageBlocksGlobal = $view->getGlobalBlocks();
  691. $_pageBlocks = array_merge($_pageBlocks, $_pageBlocksGlobal);
  692. }
  693. // do we have any custom menu plugins?
  694. $cp = new Permissions($view);
  695. if ($cp->canViewToolbar()) {
  696. $ih = Loader::helper('concrete/interface/menu');
  697. $_interfaceItems = $ih->getPageHeaderMenuItems();
  698. foreach($_interfaceItems as $_im) {
  699. $_controller = $_im->getController();
  700. $_controller->outputAutoHeaderItems();
  701. }
  702. unset($_interfaceItems);
  703. unset($_im);
  704. unset($_controller);
  705. }
  706. unset($_interfaceItems);
  707. unset($_im);
  708. unset($_controller);
  709. // now, we output all the custom style records for the design tab in blocks/areas on the page
  710. $c = $this->getCollectionObject();
  711. $view->outputCustomStyleHeaderItems();
  712. $viewPath = $view->getCollectionPath();
  713. $this->viewPath = $viewPath;
  714. $cFilename = $view->getCollectionFilename();
  715. $ctHandle = $view->getCollectionTypeHandle();
  716. $editMode = $view->isEditMode();
  717. $c = $view;
  718. $this->c = $c;
  719. $env = Environment::get();
  720. // $view is a page. It can either be a SinglePage or just a Page, but we're not sure at this point, unfortunately
  721. if ($view->getCollectionTypeID() == 0 && $cFilename) {
  722. $wrapTemplateInTheme = true;
  723. $cFilename = trim($cFilename, '/');
  724. $content = $env->getPath(DIRNAME_PAGES . '/' . $cFilename, $view->getPackageHandle());
  725. $themeFilename = $c->getCollectionHandle() . '.php';
  726. } else {
  727. $rec = $env->getRecord(DIRNAME_PAGE_TYPES . '/' . $ctHandle . '.php', $view->getPackageHandle());
  728. if ($rec->exists()) {
  729. $wrapTemplateInTheme = true;
  730. $content = $rec->file;
  731. }
  732. $themeFilename = $ctHandle . '.php';
  733. }
  734. } else if (is_string($view)) {
  735. // if we're passing a view but our render override is not null, that means that we're passing
  736. // a new view from within a controller. If that's the case, then we DON'T override the viewPath, we want to keep it
  737. // In order to enable editable 404 pages, other editable pages that we render without actually visiting
  738. if (defined('DB_DATABASE') && $view == '/page_not_found') {
  739. $pp = Page::getByPath($view);
  740. if (!$pp->isError()) {
  741. $this->c = $pp;
  742. }
  743. }
  744. $viewPath = $view;
  745. if ($this->controller->getRenderOverride() != '' && $this->getCollectionObject() != null) {
  746. // we are INSIDE a collection renderring a view. Which means we want to keep the viewPath that of the collection
  747. $this->viewPath = $this->getCollectionObject()->getCollectionPath();
  748. }
  749. // we're just passing something like "/login" or whatever. This will typically just be
  750. // internal Concrete stuff, but we also prepare for potentially having something in DIR_FILES_CONTENT (ie: the webroot)
  751. if (file_exists(DIR_FILES_CONTENT . "/{$view}/" . FILENAME_COLLECTION_VIEW)) {
  752. $content = DIR_FILES_CONTENT . "/{$view}/" . FILENAME_COLLECTION_VIEW;
  753. } else if (file_exists(DIR_FILES_CONTENT . "/{$view}.php")) {
  754. $content = DIR_FILES_CONTENT . "/{$view}.php";
  755. } else if (file_exists(DIR_FILES_CONTENT_REQUIRED . "/{$view}/" . FILENAME_COLLECTION_VIEW)) {
  756. $content = DIR_FILES_CONTENT_REQUIRED . "/{$view}/" . FILENAME_COLLECTION_VIEW;
  757. } else if (file_exists(DIR_FILES_CONTENT_REQUIRED . "/{$view}.php")) {
  758. $content = DIR_FILES_CONTENT_REQUIRED . "/{$view}.php";
  759. } else if ($this->getCollectionObject() != null && $this->getCollectionObject()->isGeneratedCollection() && $this->getCollectionObject()->getPackageID() > 0) {
  760. //This is a single_page associated with a package, so check the package views as well
  761. $pagePkgPath = Package::getByID($this->getCollectionObject()->getPackageID())->getPackagePath();
  762. if (file_exists($pagePkgPath . "/single_pages/{$view}/" . FILENAME_COLLECTION_VIEW)) {
  763. $content = $pagePkgPath . "/single_pages/{$view}/" . FILENAME_COLLECTION_VIEW;
  764. } else if (file_exists($pagePkgPath . "/single_pages/{$view}.php")) {
  765. $content = $pagePkgPath . "/single_pages/{$view}.php";
  766. }
  767. }
  768. $wrapTemplateInTheme = true;
  769. $themeFilename = $view . '.php';
  770. }
  771. if (is_object($this->c)) {
  772. $c = $this->c;
  773. if (defined('DB_DATABASE') && ($view == '/page_not_found' || $view == '/login')) {
  774. $view = $c;
  775. $req = Request::get();
  776. $req->setCurrentPage($c);
  777. $_pageBlocks = $view->getBlocks();
  778. $_pageBlocksGlobal = $view->getGlobalBlocks();
  779. $_pageBlocks = array_merge($_pageBlocks, $_pageBlocksGlobal);
  780. }
  781. }
  782. if (is_array($_pageBlocks)) {
  783. foreach($_pageBlocks as $b1) {
  784. $b1p = new Permissions($b1);
  785. if ($b1p->canRead()) {
  786. $btc = $b1->getInstance();
  787. // now we inject any custom template CSS and JavaScript into the header
  788. if('Controller' != get_class($btc)){
  789. $btc->outputAutoHeaderItems();
  790. }
  791. $btc->runTask('on_page_view', array($view));
  792. }
  793. }
  794. }
  795. // Determine which outer item/theme to load
  796. // obtain theme information for this collection
  797. if (isset($this->themeOverride)) {
  798. $theme = $this->themeOverride;
  799. } else if ($this->controller->theme != false) {
  800. $theme = $this->controller->theme;
  801. } else if (($tmpTheme = $this->getThemeFromPath($viewPath)) != false) {
  802. $theme = $tmpTheme;
  803. } else if (is_object($this->c) && ($tmpTheme = $this->c->getCollectionThemeObject()) != false) {
  804. $theme = $tmpTheme;
  805. } else {
  806. $theme = FILENAME_COLLECTION_DEFAULT_THEME;
  807. }
  808. $this->setThemeForView($theme, $themeFilename, $wrapTemplateInTheme);
  809. // finally, we include the theme (which was set by setTheme and will automatically include innerContent)
  810. // disconnect from our db and exit
  811. $this->controller->on_before_render();
  812. extract($this->controller->getSets());
  813. extract($this->controller->getHelperObjects());
  814. if ($content != false && (!$this->disableContentInclude)) {
  815. include($content);
  816. }
  817. $innerContent = ob_get_contents();
  818. if (ob_get_level() > OB_INITIAL_LEVEL) {
  819. ob_end_clean();
  820. }
  821. if (defined('DB_DATABASE') && ($view !== '/upgrade')) {
  822. Events::fire('on_before_render', $this);
  823. }
  824. if (defined('APP_CHARSET')) {
  825. header("Content-Type: text/html; charset=" . APP_CHARSET);
  826. }
  827. if (file_exists($this->theme)) {
  828. $cache = PageCache::getLibrary();
  829. $shouldAddToCache = $cache->shouldAddToCache($this);
  830. if ($shouldAddToCache) {
  831. $cache->outputCacheHeaders($c);
  832. }
  833. ob_start();
  834. include($this->theme);
  835. $pageContent = ob_get_contents();
  836. ob_end_clean();
  837. $ret = Events::fire('on_page_output', $pageContent);
  838. if($ret != '') {
  839. print $ret;
  840. $pageContent = $ret;
  841. } else {
  842. print $pageContent;
  843. }
  844. $cache = PageCache::getLibrary();
  845. if ($shouldAddToCache) {
  846. $cache->set($c, $pageContent);
  847. }
  848. } else {
  849. throw new Exception(t('File %s not found. All themes need default.php and view.php files in them. Consult concrete5 documentation on how to create these files.', $this->theme));
  850. }
  851. if (defined('DB_DATABASE') && ($view !== '/upgrade')) {
  852. Events::fire('on_render_complete', $this);
  853. }
  854. if (ob_get_level() == OB_INITIAL_LEVEL) {
  855. require(DIR_BASE_CORE . '/startup/jobs.php');
  856. require(DIR_BASE_CORE . '/startup/shutdown.php');
  857. exit;
  858. }
  859. }
  860. }