Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
PageHandler.inc.php
1 <?php
2 
16 import('classes.handler.Handler');
17 
18 class PageHandler extends Handler {
22  function PageHandler() {
23  parent::Handler();
24  }
25 
26 
27  //
28  // Implement template methods from PKPHandler
29  //
33  function authorize($request, &$args, $roleAssignments) {
34  import('lib.pkp.classes.security.authorization.PKPSiteAccessPolicy');
35  $this->addPolicy(new PKPSiteAccessPolicy(
36  $request,
37  array('header', 'sidebar', 'css'),
38  SITE_ACCESS_ALL_ROLES
39  ));
40  if (!Config::getVar('general', 'installed')) define('SESSION_DISABLE_INIT', true);
41  return parent::authorize($request, $args, $roleAssignments, false);
42  }
43 
44 
45  //
46  // Public operations
47  //
53  function header($args, $request) {
54  $this->setupTemplate($request);
55  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_MANAGER); // Management menu items
56  $templateMgr = TemplateManager::getManager($request);
57 
58  $workingContexts = $this->getWorkingContexts($request);
59 
60  $multipleContexts = false;
61  if ($workingContexts && $workingContexts->getCount() > 1) {
62  $templateMgr->assign('multipleContexts', true);
63  $multipleContexts = true;
64  } else {
65  if (!$workingContexts) {
66  $templateMgr->assign('noContextsConfigured', true);
67  $templateMgr->assign('notInstalled', true);
68  } elseif ($workingContexts->getCount() == 0) { // no contexts configured or installing
69  $templateMgr->assign('noContextsConfigured', true);
70  }
71  }
72 
73  if ($multipleContexts) {
74  $dispatcher = $request->getDispatcher();
75  $contextsNameAndUrl = array();
76  while ($workingContext = $workingContexts->next()) {
77  $contextUrl = $dispatcher->url($request, ROUTE_PAGE, $workingContext->getPath());
78  $contextsNameAndUrl[$contextUrl] = $workingContext->getLocalizedName();
79  }
80 
81  // Get the current context switcher value. We donĀ“t need to worry about the
82  // value when there is no current context, because then the switcher will not
83  // be visible.
84  $currentContextUrl = null;
85  if ($currentContext = $request->getContext()) {
86  $currentContextUrl = $dispatcher->url($request, ROUTE_PAGE, $currentContext->getPath());
87  } else {
88  $contextsNameAndUrl = array(__('context.select')) + $contextsNameAndUrl;
89  }
90 
91  $templateMgr->assign('currentContextUrl', $currentContextUrl);
92  $templateMgr->assign('contextsNameAndUrl', $contextsNameAndUrl);
93  }
94 
95  if ($context = $request->getContext()) {
96  import('pages.about.AboutContextHandler');
97  if (in_array('IAboutContextInfoProvider', class_implements('AboutContextHandler'))) {
98  $templateMgr->assign('contextInfo', AboutContextHandler::getAboutInfo($context));
99  } else {
100  $settingsDao = $context->getSettingsDAO();
101  $templateMgr->assign('contextSettings', $settingsDao->getSettings($context->getId()));
102  }
103  }
104 
105  return $templateMgr->fetchJson('controllers/page/header.tpl');
106  }
107 
113  function sidebar($args, $request) {
114  $this->setupTemplate($request);
115  $templateMgr = TemplateManager::getManager($request);
116  return $templateMgr->fetchJson('controllers/page/sidebar.tpl');
117  }
118 
124  function css($args, $request) {
125  header('Content-Type: text/css');
126 
127  $stylesheetName = $request->getUserVar('name');
128  switch ($stylesheetName) {
129  case '':
130  case null:
131  $cacheDirectory = CacheManager::getFileCachePath();
132  $compiledStylesheetFile = $cacheDirectory . '/compiled.css';
133  if (!file_exists($compiledStylesheetFile)) {
134  // Generate the stylesheet file
135  require_once('lib/pkp/lib/lessphp/lessc.inc.php');
136  $less = new lessc('styles/index.less');
137  $less->importDir = './';
138  $compiledStyles = $less->parse();
139 
140  $compiledStyles = str_replace('{$baseUrl}', $request->getBaseUrl(), $compiledStyles);
141 
142  // Allow plugins to intervene in stylesheet compilation
143  HookRegistry::call('PageHandler::compileCss', array($request, $less, &$compiledStylesheetFile, &$compiledStyles));
144 
145  if (file_put_contents($compiledStylesheetFile, $compiledStyles) === false) {
146  // If the stylesheet cache can't be written, log the error and
147  // output the compiled styles directly without caching.
148  error_log("Unable to write \"$compiledStylesheetFile\".");
149  echo $compiledStyles;
150  return;
151  }
152  }
153 
154  // Allow plugins to intervene in stylesheet display
155  HookRegistry::call('PageHandler::displayCoreCss', array($request, &$compiledStylesheetFile));
156 
157  // Display the styles
158  header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($compiledStylesheetFile)).' GMT');
159  header('Content-Length: ' . filesize($compiledStylesheetFile));
160  readfile($compiledStylesheetFile);
161  break;
162  default:
163  // Allow plugins to intervene
164  $result = null;
165  $lastModified = null;
166  TemplateManager::getManager($request); // Trigger loading of the themes plugins
167  if (!HookRegistry::call('PageHandler::displayCss', array($request, &$stylesheetName, &$result, &$lastModified))) {
168  if ($lastModified) header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
169  header('Content-Length: ' . strlen($result));
170  echo $result;
171  }
172  }
173  }
174 }
175 
176 ?>
addPolicy($authorizationPolicy, $addToTop=false)
authorize($request, &$args, $roleAssignments)
static requireComponents()
setupTemplate($request)
sidebar($args, $request)
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
getWorkingContexts($request)
Definition: Handler.inc.php:33
static call($hookName, $args=null)
header($args, $request)
Base request handler application class.
Definition: Handler.inc.php:20
Class to that makes sure that a user is logged in.
Handler for requests for page components such as the header, sidebar, and CSS.
css($args, $request)