Author: jfthomps Date: Wed Jan 9 15:28:41 2013 New Revision: 1430888 URL: Log: CMS commit to vcl by jfthomps Modified: vcl/site/trunk/content/docs/themes.mdtext Modified: vcl/site/trunk/content/docs/themes.mdtext URL: ============================================================================== --- vcl/site/trunk/content/docs/themes.mdtext (original) +++ vcl/site/trunk/content/docs/themes.mdtext Wed Jan 9 15:28:41 2013 @@ -16,189 +16,268 @@ Notice: Licensed to the Apache Softwa specific language governing permissions and limitations under the License. -[TOC] +## General Overview -## Overview -Custom themes can be created in VCL for each affiliation. This allows a multi-site -installation of VCL to have themes that match each affiliation's primary web site -theme. When a user logs in, the site will be displayed using the theme specified -for that user's affiliation. Additionally, when using [LDAP -authentication](/docs/ldapauth.html), the login page for the affiliation will be -displayed with the theme matching that affiliation. - -## File system structure -The themes are in a **themes** directory in the web code. If you installed your web -frontend under a directory named **vcl**, you would look in vcl/themes. Each VCL -release comes with a **default** theme, which you should see at vcl/themes/default. -Typically, you would set up the following directory structure for a new theme: - -* vcl/themes/mynewtheme -* vcl/themes/mynewtheme/page.php -* vcl/themes/mynewtheme/images -* vcl/themes/mynewtheme/js -* vcl/themes/mynewtheme/css - -The page.php file and css directory are the only ones that are required. After creating -the directory structure, you will need to go to the themes directory and run the script, passing it the name of the directory containing your new theme as -an argument: - - cd vcl/themes - ./ mynewtheme - -## VCL HTML generation -There are a few parts to how VCL generates the HTML for any given page. There are some -important functions of which to be aware. The first two must be in the page.php file: - -* getHeader - generates all content up to and including the opening of a div element -that will contain the main page content (typically what is to the right of the -navigation menu); this div element must have an **id** of **content**; must have -$refresh passed to it as an argument, which is then passed to getDojoHTML -* getFooter - closes the div element containing the main page content and generates any -remaining content to close out the HTML for the page - -These are functions that exist in utils.php that are available to be called from -getHeader and getFooter: - -* getDojoHTML - returns a string of Dojo related style and javascript; needs to be -called in the <head> part of the page; needs to be passed $refresh as an argument -* getExtraCSS - returns an array of css files expected to exist in the vcl/css directory -that need to be included for the current value of $mode; a link tag needs to be created -for each of these files -* getSelectLanguagePulldown - returns the html for a form that has a select element for -changing the locale of the site -* getNavMenu - returns a string of list items that make up the navigation part of the -site; needs to be wrapped in <ul> tags; accepts 3 arguments: - * $inclogout - 0/1 - should a **Logout** item be at the bottom of the navigation list - * $inchome - 0/1 - should a **HOME** item be at the top of the navigation list - * $homeurl - (optional) - if $inchome is 1, use this as the href of the HOME link; -if $inchome is 1 and $homeurl is not specified, HOME from conf.php is used - -## Understanding page.php -As noted above, this file must contain getHeader and getFooter. **getHeader** must -contain these elements, all of which can be seen in the default theme's page.php file: - -* opening html and head tags -* stylesheet link to "css/vcl.css" -* script tag with src="js/code.js" -* script block setting the javascript variable *cookiedomain* to the php define -*COOKIEDOMAIN* and setting usenls = 0 -* a call to getDojoHTML($refresh) in the head section of the HTML -* a call to getExtraCSS() with a foreach to add a stylesheet link for each file in the -array returned by getExtraCSS() -* closing head tag -* body tag with its class set to the name of the theme -* an unordered list wrapping the string returned by a call to getNavMenu() - this should -be inside an if conditional checking for $authed to be true -* must end with an opening div tag with an id of *content* - -**getFooter** must contain these elements: - -* closing div tag for the content div element -* closing body and html tags - -## Path references -There are a few things to keep in mind with respect to path references. From a web -browser's perspective, all HTML generated by getHeader and getFooter will be part of the -vcl/index.php file. So, any paths referenced in getHeader and getFooter need to be -relative to that - example: <img src="themes/mynewtheme/images/someimage.png">. Any -files referenced in css files in the theme's css directory will be relative to that css -file. So, if you were to include a reference to an image in the theme's image directory -in a css file in the css directory, you would reference it relative to the css directory -- example: url(../images/someimage.png) - -## Example: bare minimal theme -This example shows the bare minimum that must be included. For a more detailed example, -look at the default theme include in each release. We'll call the theme "minimal". First, -we create a directory under the themes directory for it: - - :::BashLexer - mkdir vcl/themes/minimal - -Next, we create a css directory and run the script: - - :::BashLexer - mkdir vcl/themes/minimal/css - cd vcl/themes - ./ minimal - -Now, we create vcl/themes/minimal/page.php with this content: - - :::PhpLexer - function getHeader($refresh) { - global $user, $mode, $authed, $locale, $VCLversion; - $rt = "\n"; - $rt .= "\n"; - $rt .= "\n"; - $rt .= "VCL\n"; - $rt .= "\n"; - $rt .= "\n"; - $rt .= "\n"; - $rt .= "\n"; - $rt .= getDojoHTML($refresh); - $extracss = getExtraCSS(); - foreach($extracss as $file) - $rt .= "\n"; - $rt .= "\n\n"; - $rt .= "\n\n"; - $rt .= "\n"; - $rt .= "
\n"; - return $rt; - } - - function getFooter() { - $year = date("Y"); - $rt = "
\n"; - $rt .= "\n"; - $rt .= "\n"; - return $rt; - } - -Finally, we create a basic css file at vcl/themes/minimal/css/minimal.css: - - :::CssLexer - #menulist { - float: left; - } - - #content { - margin-left: 15em; - } - -## Using a new theme -Themes are assigned on a per-affiliation basis in the affiliation table. This must -be done directly in the database by setting the **affiliation.theme** field for an -affiliation to the name of a theme in the themes directory. For example, to use our new -*minimal* theme, you would set **affiliation.theme** to **minimal** for the desired -affiliation. - -## Tips and Tricks -The VCL web site has some administrative pages that can generate very large tables. If -you want them to be completely wrapped in your theme, you will probably need to use -tables as part of you main layout instead of just using div elements and css. (Hopefully, -all of those large tables will be converted to dojo grids like the Manage Groups page by -the 2.4 release.) - -Mobile themes - If you would like to use a modified theme for mobile devices, you can add -a section of code to the top of getHeader and getFooter that detects mobile devices -based on $_SERVER['HTTP_USER_AGENT']. If a mobile browser is detected, you can then -call a different function that generates mobile specific HTML and returns it to the -calling function. Here is an example that could be used in getHeader: - - :::PhpLexer - if(array_key_exists('HTTP_USER_AGENT', $_SERVER) && - (eregi('iphone', $_SERVER['HTTP_USER_AGENT']) || - eregi('android', $_SERVER['HTTP_USER_AGENT']))) - return mobileHeader($refresh); - -mobileHeader would still need to meet all of the requirements of getHeader, but it -would just generate mobile specific HTML. A similar block of code would then be -added to getFooter, calling a function named something like mobileFooter. \ No newline at end of file +The code is broken up into multiple files, based on sections of the site (as divided up by the navigation area). There are also several general purpose files: + +* index.php - This is the only file used in any URLs. It includes other files based on the passed in "mode". +* states.php - builds an array of modes, which function should be called for each mode, and the part of the site each mode belongs to +* utils.php - contains many functions that are common to several sections +* errors.php - contains an array of all errors that can be reported by the site. I'm not sure how useful it was do set it up this way, but it's where things currently are. errors.php also contains the debug function that is used if the current user has an adminlevel of ADMIN_DEVELOPER in the user table. +* secrets.php - contains all passwords/passphrases and secret and shared keys +* conf.php - contains all configuration variables that might need to be modified for each install base +* php5extras.php - anything that needs to be done differently when using php5 vs php4 + + + +## Site Security + +The initial handling of form data within the code was quite insecure, and several areas of the site are still this way. After learning more about web security, I developed a security model based on "continuations". All of the pages have been converted to using continuations. + +Deep linking into the site is only allowed for modes in the $actions['entry'] array in states.php. Anything else requires the submission of a continuation. For the most part, access to different parts of the site is controlled by what privileges you have in the Privileges section of the site. However, there are a few things controlled by a user's adminlevel field in the user table. The very earliest form of authorization was handled by the user's adminlevel field, and it has continued to be useful in a few situations. + +All form data passed in to the site should be verified very strictly. Unfortunately, that is not currently the case. All of the main pages available to the average user should have been updated to have strict validation, though other parts of the site have not made it yet. Most sections of the site have a single function (or a very small number of similar functions) that handle the processing of form data. This will make it easier to add better checks throughout the site as the number of locations needing to be modified is fairly small. + +## General Processing Flow + +Every time someone views the site, it is through index.php. This file defines several global variables and includes conf.php, states.php, errors.php, and utils.php. It then creates a database connection and calls initGlobals(), which among other things, determines $mode. Based on $mode, index.php determines which function needs to be called and assigns it to $actionFunction. Next, checkAccess() is called, followed by sendHeaders() and printHTMLHeader() (which doesn't actually print the header if $mode is in $noHTMLwrappers). set_error_handler is called if the current user has an adminlevel of ADMIN_DEVELOPER. Next, $actionFunction is finally called, followed by disconnecting from the database, a call to printHTMLFooter(), and a call to semUnlock() to make sure the semaphore was unlocked if it was locked somewhere in the process. + +One thing worth noting that initGlobals() does is to include other files based on which mode is being processed. This helps to prevent the php script engine from having to process unnecessary files. It also adds a small layer of security because a section of code cannot be attacked if it has not been included. + +## Continuations + +Continuations are how the site handles sequences of pages. It also helps keep people from getting to parts of the site they aren't allowed to access or shouldn't jump in to the middle of (i.e. by using the browser's back button). Continuations are created by calling addContinuationsEntry, which accepts up to 6 arguments: + +* $nextmode - the mode that should be used when the continuation is submitted +* $data - an array of any data that you would like to have available when the continuation is submitted +* $duration - how long from "now" that the continuation should be valid (in seconds) +* $deleteFromSelf - boolean - 1 if this is the start of a new sequence of pages, 0 if it will be a continuation of a sequence. If it is set to 0, then the "parent" continuation for this one is the continuation that the site is currently processing. +* $multicall - boolean - whether or not the continuation may be submitted more than once +* $repeatProtect - boolean - used in cases where a "sequence loop" can occur + + +addContinuationsEntry returns a long encrypted string to be used as an identifier to be submitted (either as a hidden form field or in the URL for a GET link). What gets encrypted is a salt value, the id of the continuation that was created (or updated), the user's numeric id, and a timestamp. If the string gets tampered with, it will not decrypt properly. If someone tries to submit a continuation given to another user, their user ids won't match; so, it will be considered invalid. + +When a continuation is submitted, some checks are run and, if everything passes, whatever was submitted as $nextmode is the mode for which the site functions. One of those checks is that $duration has not expired; if it has, the user is given a notice that he has submitted expired data. Any data submitted as $data can be accessed by calling getContinuationsData() with a single argument being the index of the array that was passed to addContinuationsEntry. Additionally, getContinuationsData can be called with no arguments to get all of $data as a single array. If $multicall was set to 0, then the continuation is deleted. If $deleteFromSelf was also set to 0, then this continuation's parent will also be deleted. If $deleteFromSelf was set to 0 for the parent, it's parent will be deleted, and so on until a continuation is reached that had $deleteFromSelf set to 1. + +## Javascript and AJAX + +Efforts have been made to ensure the pages required for making and connecting to a reservation work without requiring any javascript. However, enhancements have been made to enrich those parts of the site if javascript is enabled. For some of the administrative parts of the site, javascript and AJAX have been used heavily, particularly the Privileges page. The [Dojo Toolkit]( is what is being used for javascript widgets and to handle some of the AJAX. + +## A Few Examples + + +### Adding a link to the navigation area + +Here are the steps that would need to be done to add a new section of the site. + +First, modify states.php to add a new mode. + +1. create a new $actions['mode'] with the name of your mode set to the name of the function that should be called +1. create a new $actions['pages'] with the name of your mode set to the name of the section this mode belongs to. This is only an internal identifier used to associate modes together. + +So, if your mode is named "examplemode", you could end up with these two lines being added: + + :::PhpLexer + $actions['mode']['examplemode'] = "exampleFunc1"; + $actions['pages']['examplemode'] = "exampleSection"; + + +While we're editing states.php, lets jump to the top and add our new mode to $actions['entry'] so that it can be called directly without having to already be on the site. Just add 'examplemode' as a new item at the end of the array. + +The next thing to do is to actually add the functions. Lets place them in a new file called 'examples.php' in the .ht-inc directory. Our first function can be really simple and just print out some text. So, create examples.php with this in it: + + :::PhpLexer + "exampleFunc1 successfully called.
\n"; + } + ?> + +There's one last thing we need to do before linking this in on the site. As described in the "General Processing Flow" section, initGlobals includes the required files based on the current mode's section. So, edit utils.php and scroll toward the end of it where files are included (using require_once) within a switch statement. In the switch statement, before the "default" case, add + + :::PhpLexer + case 'exampleSection': + require_once('.ht-inc/examples.php'); + break; + +Now, we're ready to actually add a link for this example function to the navigation area (of course, not all modes are linked to from the navigation area, but it is an easy example). To do this, edit utils.php and find the getNavMenu function close to the bottom of the file. We'll add our new mode to the end; so, find the "logout" part which should look something like this: + + :::PhpLexer + if($inclogout) { + $rt .= menulistLI('authentication'); + $rt .= ""; + $rt .= "Logout\n"; + } + +We'll basically duplicate that (without the if conditional), changing a few things so that we add this right below it: + + :::PhpLexer + $rt .= menulistLI('exampleSection'); + $rt .= ""; + $rt .= "Example Section\n"; + +Viewing the site should now show "Example Section" right under "Logout" in the navigation area. Clicking "Example Section" should cause "exampleFunc1 successfully called." to be displayed in the main content area of the site. + +### Using continuations when submitting form data + +Let's modify examplefunc1 so that it prints some form data that gets submitted with a continuation. + +So, change the contents of examplefunc1 to be: + + :::PhpLexer + $options = array(0 => "option1", + 1 => "option2"); + print "
\n"; + print "Select one of these options:"; + printSelectInput("theoption", $options); + $cont = addContinuationsEntry("submitFunc1Form", $options); + print "\n"; + print "\n"; + print "
\n"; + + +Now, we have a form that gets displayed when "Example Section" is clicked. Now, we need to add a function to process that form. Add this function to examples.php: + + :::PhpLexer + function submitFunc1Form() { + $data = getContinuationVar(); + $theoption = processInputVar("theoption", ARG_NUMERIC); + if(! array_key_exists($theoption, $data)) { + print "invalid option submitted\n"; + return; + } + print "The option you selected was: "; + print "{$data\[$theoption\]}
\n"; + } + + +Next, we add this function to states.php: + + :::PhpLexer + $actions['mode']['submitFunc1Form'] = "submitFunc1Form"; + $actions['pages']['submitFunc1Form'] = "exampleSection"; + +Now, click the "Example Section" link, select one of the options, and click Submit to see if it works. + +### Using AJAX to dynamically update a page + +AJAX is very useful for dynamically updating page content. Let's add something to examplefunc1 that can be updated with an AJAX call. + +Add this to the end of examplefunc1: + + :::PhpLexer + print "

\n"; + print "
\n"; + print "This will get dynamically changed.
\n"; + print "
\n"; + $cont = addContinuationsEntry("AJexample"); + print "Click to "; + print "update
\n"; + +Next, we need to add the javascript function we just referenced to code.js (in .ht-inc's parent directory) as well as a callback function that will be called when the results of the AJAX call are returned: + + :::PhpLexer + function JS_AJexample(contid) { + dojo.xhrPost({ + url:'index.php', + load: JS_AJexampleCB, + error: errorHandler, + content: {continuation: contid}, + timeout: 15000 + }); + } + + function JS_AJexampleCB(data, ioArgs) { + eval(data); + } + +Then, we need to add a few things to states.php: + +to the $actions array: + + :::PhpLexer + $actions['mode']['AJexample'] = "exampleFunc2"; + $actions['pages']['AJexample'] = "exampleSection"; + +to the $noHTMLwrappers array: + + :::PhpLexer + 'AJexample' + +Now, we need to create exampleFunc2 (in examples.php): + + :::PhpLexer + function exampleFunc2() { + print "document.getElementById('examplediv').innerHTML = 'Dynamic update';"; + } + +Then, we do something we haven't seen before. getDojoHTML (in utils.php) must be modified so that the correct dojo requires get set when we are in mode examplefunc1. For a simple AJAX update, we only need the dojo.parser module to be loaded. Since this is already loaded for some of the Group modes, just add another case statement under submitDeleteGroup so we have: + + :::PhpLexer + case 'viewGroups': + case 'submitEditGroup': + case 'submitAddGroup': + case 'submitDeleteGroup': + case 'examplemode': + $dojoRequires = array('dojo.parser'); + break; + +We also have to add a case statement a little further down where the HTML is actually generated. Find "case 'submitDeleteGroup':" in the switch statement following the one we just modified, and add another case statement for examplemode so we have: + + :::PhpLexer + case 'viewGroups': + case 'submitEditGroup': + case 'submitAddGroup': + case 'submitDeleteGroup': + case 'examplemode': + $rt .= "