Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Brice Maron <brice@bmaron.net>
  7. * @author Christoph Wurst <christoph@owncloud.com>
  8. * @author Frank Karlitschek <frank@karlitschek.de>
  9. * @author Individual IT Services <info@individual-it.net>
  10. * @author Jakob Sack <mail@jakobsack.de>
  11. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  12. * @author Joas Schilling <coding@schilljs.com>
  13. * @author John Molakvoæ <skjnldsv@users.noreply.github.com>
  14. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  15. * @author Lukas Reschke <lukas@statuscode.ch>
  16. * @author Marin Treselj <marin@pixelipo.com>
  17. * @author Michael Letzgus <www@chronos.michael-letzgus.de>
  18. * @author Morris Jobke <hey@morrisjobke.de>
  19. * @author Robin Appelman <robin@icewind.nl>
  20. * @author Roeland Jago Douma <roeland@famdouma.nl>
  21. * @author Thomas Müller <thomas.mueller@tmit.eu>
  22. * @author Vincent Petry <pvince81@owncloud.com>
  23. *
  24. * @license AGPL-3.0
  25. *
  26. * This code is free software: you can redistribute it and/or modify
  27. * it under the terms of the GNU Affero General Public License, version 3,
  28. * as published by the Free Software Foundation.
  29. *
  30. * This program is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. * GNU Affero General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU Affero General Public License, version 3,
  36. * along with this program. If not, see <http://www.gnu.org/licenses/>
  37. *
  38. */
  39. use OC\TemplateLayout;
  40. require_once __DIR__.'/template/functions.php';
  41. /**
  42. * This class provides the templates for ownCloud.
  43. */
  44. class OC_Template extends \OC\Template\Base {
  45. /** @var string */
  46. private $renderAs; // Create a full page?
  47. /** @var string */
  48. private $path; // The path to the template
  49. /** @var array */
  50. private $headers = array(); //custom headers
  51. /** @var string */
  52. protected $app; // app id
  53. protected static $initTemplateEngineFirstRun = true;
  54. /**
  55. * Constructor
  56. *
  57. * @param string $app app providing the template
  58. * @param string $name of the template file (without suffix)
  59. * @param string $renderAs If $renderAs is set, OC_Template will try to
  60. * produce a full page in the according layout. For
  61. * now, $renderAs can be set to "guest", "user" or
  62. * "admin".
  63. * @param bool $registerCall = true
  64. */
  65. public function __construct( $app, $name, $renderAs = "", $registerCall = true ) {
  66. // Read the selected theme from the config file
  67. self::initTemplateEngine($renderAs);
  68. $theme = OC_Util::getTheme();
  69. $requestToken = (OC::$server->getSession() && $registerCall) ? \OCP\Util::callRegister() : '';
  70. $parts = explode('/', $app); // fix translation when app is something like core/lostpassword
  71. $l10n = \OC::$server->getL10N($parts[0]);
  72. /** @var \OCP\Defaults $themeDefaults */
  73. $themeDefaults = \OC::$server->query(\OCP\Defaults::class);
  74. list($path, $template) = $this->findTemplate($theme, $app, $name);
  75. // Set the private data
  76. $this->renderAs = $renderAs;
  77. $this->path = $path;
  78. $this->app = $app;
  79. parent::__construct($template, $requestToken, $l10n, $themeDefaults);
  80. }
  81. /**
  82. * @param string $renderAs
  83. */
  84. public static function initTemplateEngine($renderAs) {
  85. if (self::$initTemplateEngineFirstRun){
  86. //apps that started before the template initialization can load their own scripts/styles
  87. //so to make sure this scripts/styles here are loaded first we use OC_Util::addScript() with $prepend=true
  88. //meaning the last script/style in this list will be loaded first
  89. if (\OC::$server->getSystemConfig()->getValue ('installed', false) && $renderAs !== 'error' && !\OCP\Util::needUpgrade()) {
  90. if (\OC::$server->getConfig ()->getAppValue ( 'core', 'backgroundjobs_mode', 'ajax' ) == 'ajax') {
  91. OC_Util::addScript ( 'backgroundjobs', null, true );
  92. }
  93. }
  94. OC_Util::addStyle('css-variables', null, true);
  95. OC_Util::addStyle('server', null, true);
  96. OC_Util::addStyle('jquery-ui-fixes',null,true);
  97. OC_Util::addStyle('jquery.ocdialog');
  98. OC_Util::addTranslations("core", null, true);
  99. OC_Util::addStyle('search', 'results');
  100. OC_Util::addScript('search', 'search', true);
  101. OC_Util::addScript('search', 'searchprovider');
  102. OC_Util::addScript('merged-template-prepend', null, true);
  103. OC_Util::addScript('files/fileinfo');
  104. OC_Util::addScript('files/client');
  105. OC_Util::addScript('contactsmenu_templates');
  106. OC_Util::addScript('core', 'dist/main', true);
  107. if (\OC::$server->getRequest()->isUserAgent([\OC\AppFramework\Http\Request::USER_AGENT_IE])) {
  108. // shim for the davclient.js library
  109. \OCP\Util::addScript('files/iedavclient');
  110. }
  111. self::$initTemplateEngineFirstRun = false;
  112. }
  113. }
  114. /**
  115. * find the template with the given name
  116. * @param string $name of the template file (without suffix)
  117. *
  118. * Will select the template file for the selected theme.
  119. * Checking all the possible locations.
  120. * @param string $theme
  121. * @param string $app
  122. * @return string[]
  123. */
  124. protected function findTemplate($theme, $app, $name) {
  125. // Check if it is a app template or not.
  126. if( $app !== '' ) {
  127. $dirs = $this->getAppTemplateDirs($theme, $app, OC::$SERVERROOT, OC_App::getAppPath($app));
  128. } else {
  129. $dirs = $this->getCoreTemplateDirs($theme, OC::$SERVERROOT);
  130. }
  131. $locator = new \OC\Template\TemplateFileLocator( $dirs );
  132. $template = $locator->find($name);
  133. $path = $locator->getPath();
  134. return array($path, $template);
  135. }
  136. /**
  137. * Add a custom element to the header
  138. * @param string $tag tag name of the element
  139. * @param array $attributes array of attributes for the element
  140. * @param string $text the text content for the element. If $text is null then the
  141. * element will be written as empty element. So use "" to get a closing tag.
  142. */
  143. public function addHeader($tag, $attributes, $text=null) {
  144. $this->headers[]= array(
  145. 'tag' => $tag,
  146. 'attributes' => $attributes,
  147. 'text' => $text
  148. );
  149. }
  150. /**
  151. * Process the template
  152. * @return boolean|string
  153. *
  154. * This function process the template. If $this->renderAs is set, it
  155. * will produce a full page.
  156. */
  157. public function fetchPage($additionalParams = null) {
  158. $data = parent::fetchPage($additionalParams);
  159. if( $this->renderAs ) {
  160. $page = new TemplateLayout($this->renderAs, $this->app);
  161. if(is_array($additionalParams)) {
  162. foreach ($additionalParams as $key => $value) {
  163. $page->assign($key, $value);
  164. }
  165. }
  166. // Add custom headers
  167. $headers = '';
  168. foreach(OC_Util::$headers as $header) {
  169. $headers .= '<'.\OCP\Util::sanitizeHTML($header['tag']);
  170. if ( strcasecmp($header['tag'], 'script') === 0 && in_array('src', array_map('strtolower', array_keys($header['attributes']))) ) {
  171. $headers .= ' defer';
  172. }
  173. foreach($header['attributes'] as $name=>$value) {
  174. $headers .= ' '.\OCP\Util::sanitizeHTML($name).'="'.\OCP\Util::sanitizeHTML($value).'"';
  175. }
  176. if ($header['text'] !== null) {
  177. $headers .= '>'.\OCP\Util::sanitizeHTML($header['text']).'</'.\OCP\Util::sanitizeHTML($header['tag']).'>';
  178. } else {
  179. $headers .= '/>';
  180. }
  181. }
  182. $page->assign('headers', $headers);
  183. $page->assign('content', $data);
  184. return $page->fetchPage($additionalParams);
  185. }
  186. return $data;
  187. }
  188. /**
  189. * Include template
  190. *
  191. * @param string $file
  192. * @param array|null $additionalParams
  193. * @return string returns content of included template
  194. *
  195. * Includes another template. use <?php echo $this->inc('template'); ?> to
  196. * do this.
  197. */
  198. public function inc( $file, $additionalParams = null ) {
  199. return $this->load($this->path.$file.'.php', $additionalParams);
  200. }
  201. /**
  202. * Shortcut to print a simple page for users
  203. * @param string $application The application we render the template for
  204. * @param string $name Name of the template
  205. * @param array $parameters Parameters for the template
  206. * @return boolean|null
  207. */
  208. public static function printUserPage( $application, $name, $parameters = array() ) {
  209. $content = new OC_Template( $application, $name, "user" );
  210. foreach( $parameters as $key => $value ) {
  211. $content->assign( $key, $value );
  212. }
  213. print $content->printPage();
  214. }
  215. /**
  216. * Shortcut to print a simple page for admins
  217. * @param string $application The application we render the template for
  218. * @param string $name Name of the template
  219. * @param array $parameters Parameters for the template
  220. * @return bool
  221. */
  222. public static function printAdminPage( $application, $name, $parameters = array() ) {
  223. $content = new OC_Template( $application, $name, "admin" );
  224. foreach( $parameters as $key => $value ) {
  225. $content->assign( $key, $value );
  226. }
  227. return $content->printPage();
  228. }
  229. /**
  230. * Shortcut to print a simple page for guests
  231. * @param string $application The application we render the template for
  232. * @param string $name Name of the template
  233. * @param array|string $parameters Parameters for the template
  234. * @return bool
  235. */
  236. public static function printGuestPage( $application, $name, $parameters = array() ) {
  237. $content = new OC_Template( $application, $name, "guest" );
  238. foreach( $parameters as $key => $value ) {
  239. $content->assign( $key, $value );
  240. }
  241. return $content->printPage();
  242. }
  243. /**
  244. * Print a fatal error page and terminates the script
  245. * @param string $error_msg The error message to show
  246. * @param string $hint An optional hint message - needs to be properly escape
  247. * @param int $statusCode
  248. * @suppress PhanAccessMethodInternal
  249. */
  250. public static function printErrorPage( $error_msg, $hint = '', $statusCode = 500) {
  251. if (\OC::$server->getAppManager()->isEnabledForUser('theming') && !\OC_App::isAppLoaded('theming')) {
  252. \OC_App::loadApp('theming');
  253. }
  254. if ($error_msg === $hint) {
  255. // If the hint is the same as the message there is no need to display it twice.
  256. $hint = '';
  257. }
  258. http_response_code($statusCode);
  259. try {
  260. $content = new \OC_Template( '', 'error', 'error', false );
  261. $errors = array(array('error' => $error_msg, 'hint' => $hint));
  262. $content->assign( 'errors', $errors );
  263. $content->printPage();
  264. } catch (\Exception $e) {
  265. $logger = \OC::$server->getLogger();
  266. $logger->error("$error_msg $hint", ['app' => 'core']);
  267. $logger->logException($e, ['app' => 'core']);
  268. header('Content-Type: text/plain; charset=utf-8');
  269. print("$error_msg $hint");
  270. }
  271. die();
  272. }
  273. /**
  274. * print error page using Exception details
  275. * @param Exception|Throwable $exception
  276. * @param int $statusCode
  277. * @return bool|string
  278. * @suppress PhanAccessMethodInternal
  279. */
  280. public static function printExceptionErrorPage($exception, $statusCode = 503) {
  281. http_response_code($statusCode);
  282. try {
  283. $request = \OC::$server->getRequest();
  284. $content = new \OC_Template('', 'exception', 'error', false);
  285. $content->assign('errorClass', get_class($exception));
  286. $content->assign('errorMsg', $exception->getMessage());
  287. $content->assign('errorCode', $exception->getCode());
  288. $content->assign('file', $exception->getFile());
  289. $content->assign('line', $exception->getLine());
  290. $content->assign('trace', $exception->getTraceAsString());
  291. $content->assign('debugMode', \OC::$server->getSystemConfig()->getValue('debug', false));
  292. $content->assign('remoteAddr', $request->getRemoteAddress());
  293. $content->assign('requestID', $request->getId());
  294. $content->printPage();
  295. } catch (\Exception $e) {
  296. try {
  297. $logger = \OC::$server->getLogger();
  298. $logger->logException($exception, ['app' => 'core']);
  299. $logger->logException($e, ['app' => 'core']);
  300. } catch (Throwable $e) {
  301. // no way to log it properly - but to avoid a white page of death we send some output
  302. header('Content-Type: text/plain; charset=utf-8');
  303. print("Internal Server Error\n\n");
  304. print("The server encountered an internal error and was unable to complete your request.\n");
  305. print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n");
  306. print("More details can be found in the server log.\n");
  307. // and then throw it again to log it at least to the web server error log
  308. throw $e;
  309. }
  310. header('Content-Type: text/plain; charset=utf-8');
  311. print("Internal Server Error\n\n");
  312. print("The server encountered an internal error and was unable to complete your request.\n");
  313. print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n");
  314. print("More details can be found in the server log.\n");
  315. }
  316. die();
  317. }
  318. }