Introduce the RosWeb class to cleanly connect a standalone website subsystem with our Drupal layout and user information.

This will allow us to easily integrate standalone subsystems into our website design and develop our own subsystems independent of Drupal.
Work in Progress, so not yet finished!

svn path=/trunk/; revision=890
This commit is contained in:
Colin Finck 2015-04-16 19:18:06 +00:00
parent 9bdf294b56
commit 919470a467
2 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,54 @@
<?php
/*
PROJECT: ReactOS RosWeb Component for sharing layout and user information between website subsystems
LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
PURPOSE: Counterpart to rosweb.php for providing the information by using Drupal-internal functions
COPYRIGHT: Copyright 2015 Colin Finck <colin@reactos.org>
*/
function get_page_theme($theme)
{
// Adapted from drupal_render_page: First populate $page using element_info, finally use drupal_render on the array.
// We modify the array here to render our theme only and remove the "html" wrapper template from processing.
$page = element_info("page");
$page["#theme"] = $theme;
$page["#theme_wrappers"] = array();
return drupal_render($page);
}
// This script may only be called from the server itself through "rosweb.php"
// Deny any other request. This is mandatory for security!
if($_SERVER["REMOTE_ADDR"] != "127.0.0.1")
exit;
// Clear the $_GET array for Drupal
$rosweb_query = array_key_exists("q", $_GET) ? $_GET["q"] : "";
$_GET = array();
// Bootstrap Drupal.
// This pollutes the global namespace with many Drupal-specific variables and functions and is the sole reason we have to use this provider approach.
define("DRUPAL_ROOT", "/srv/www/www.reactos.org");
chdir(DRUPAL_ROOT);
require_once(DRUPAL_ROOT . "/includes/bootstrap.inc");
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
switch($rosweb_query)
{
case "CurrentUser":
$rosweb_user_info["id"] = $user->uid;
$rosweb_user_info["mail"] = $user->mail;
$rosweb_user_info["name"] = $user->name;
die(json_encode($rosweb_user_info));
case "Footer":
die(get_page_theme("page__footerinc"));
case "Header":
die(get_page_theme("page__headerinc"));
case "Sidebar":
die(get_page_theme("page__sidebarinc"));
}

View File

@ -0,0 +1,67 @@
<?php
/*
PROJECT: ReactOS RosWeb Component for sharing layout and user information between website subsystems
LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
PURPOSE: Encapsulating the shared information inside a class that doesn't clash with the code of other subsystems
COPYRIGHT: Copyright 2015 Colin Finck <colin@reactos.org>
Functional programming in PHP is prone to naming collisions. As Drupal 7 is still using functional PHP throughout the
whole CMS, we have to use this clean but slow provider approach and do a single HTTP request for each query.
When you add queries here, return as much information as useful in one query to reduce the number of required requests.
*/
class RosWeb
{
public function __construct()
{
// Collect available cookies for the HTTP request
// This is mandatory for e.g. getting information about the current user
$cookies = "";
foreach($_COOKIE as $key => $value)
{
if($cookies == "")
$cookies = "$key=$value";
else
$cookies .= ";$key=$value";
}
$opts = array(
"http" => array(
"method" => "GET",
"header" => "Cookie: $cookies\r\n"
)
);
// Create the stream context to reuse for all HTTP requests
$this->context = stream_context_create($opts);
}
private function _queryProvider($q)
{
$fp = fopen("http://" . $_SERVER["HTTP_HOST"] . "/rosweb/rosweb-provider.php?q=" . $q, "r", false, $this->context);
$ret = stream_get_contents($fp);
fclose($fp);
return $ret;
}
public function getCurrentUser()
{
return json_decode($this->_queryProvider("CurrentUser"));
}
public function getFooter()
{
return $this->_queryProvider("Footer");
}
public function getHeader()
{
return $this->_queryProvider("Header");
}
public function getSidebar()
{
return this->_queryProvider("Sidebar");
}
}