Associate a user to an avatar
Create site17 by copying site16.
- /cms
- ...
- site16
- site17
In this chapter, we are going to program the analysis of the parameters in a URL and illustrate it by creating a unique icon associated to a user name.
To test the result online, enter http://www.frasq.org/cms/site17 in the address bar of your navigator.
Identify as foobar
with the password f00bar
.
An icon, computed from the connection name, is displayed on the right in the home page.
Enter http://www.frasq.org/cms/site17/avatar/frasq in the address bar of your navigator. An icon with 128x128 pixels is returned. Try other names. Enter http://www.frasq.org/cms/site17/avatar/frasq/64 to generate an icon with 64x64 pixels. If you specify a dimension which is inferior to 16 or superior to 128, an HTTP error is returned. Enter http://www.frasq.org/cms/site17/avatar?name=frasq&size=64 to pass the connection name and the size of the icon as parameters in a URL. The 2 methods are interchangeable. Try the URL http://www.frasq.org/cms/site17/avatar/frasq?size=48.
Start by creating the action avatar
by adding the file avatar.php in the folder actions with the following content:
- /cms/site17
- actions
- avatar.php
- actions
- require_once 'identicon.php';
- function avatar($lang, $arglist=false) {
- $name=false;
- $size=128;
- if ($arglist) {
- if (isset($arglist[0])) {
- $name = $arglist[0];
- }
- else if (isset($arglist['name'])) {
- $name = $arglist['name'];
- }
- if (isset($arglist[1])) {
- $size = $arglist[1];
- }
- else if (isset($arglist['size'])) {
- $size = $arglist['size'];
- }
- }
- if (!$name or !$size or !is_numeric($size) or $size < 16 or $size > 128) {
- return run('error/badrequest', $lang);
- }
- $img = identicon($name, $size);
- header('Content-Type: image/png');
- header("Content-Disposition: inline; filename=$name");
- imagepng($img);
- imagedestroy($img);
- return false;
- }
avatar
returns a document containing the unique PNG image built by the identicon
fucntion for a given name and size.
avatar
takes 2 arguments, the language and an array of parameters.
Values passed in $arglist
are either indexed or named.
avatar
extracts $name
and $size
from $arglist
either at positions 0 and 1 or 'name'
and 'size'
.
If $name
or $size
has an invalid value, avatar
returns an HTTP error.
To give access to the avatar
action, add an alias for each language in the file includes/aliases.inc:
- 'avatar' => 'avatar',
Add the file identicon.php in the folder library with the following content:
- /cms/site17
- library
- identicon.php
- library
The identicon
function returns a square image generated from the MD5 of a character string. The code is published on the site sourceforge.net.
Edit the file engine.php in the folder library and modify how a URL is interpreted by the dispatch
function and how an action is called by the run
function:
- $action=$args=$params=false;
Initializes the name of the action, the list of arguments and the list of parameters.
- if ($query) {
- $params = array();
- foreach (explode('&', $query) as $q) {
- $p = explode('=', $q);
- if (count($p) == 2) {
- list($key, $value) = $p;
- if ($key) {
- $params[$key]=urldecode($value);
- }
- }
- }
- }
Builds the list of parameters read after a ? in a URL.
- $arglist = $args ? $params ? array_merge($args, $params) : $args : $params;
- run($action, $lang, $arglist);
Combines in $arglist
the parameters read in a URL after the action name in $args
and after the ? in $params
.
- function run($action, $lang=false, $arglist=false) {
run
has 3 arguments: the name of the action, the language and a list of parameters.
- $farg = array();
- if ($lang) {
- $farg[] = $lang;
- }
- if ($arglist) {
- $farg[] = $arglist;
- }
- $output = call_user_func_array($func, $farg);
Calls the function corresponding to an action with a variable number of arguments.
Correct the function serviceunavailable
in the file actions/error/serviceunavailable.php to take into account the modification to an action call:
- function serviceunavailable($lang, $arglist=false) {
- $closing_time=$arglist[0];
- $opening_time=$arglist[1];
Add showing an avatar in the home page:
- <?php if (!empty($username)): ?>
- <p class="right"><img src="<?php echo $base_path; ?>/avatars/<?php echo $username; ?>.png" title="<?php echo $username; ?>" /></p>
- <?php endif; ?>
- <?php if (!empty($username)): ?>
- <p class="right"><img src="<?php echo $base_path; ?>/avatars/<?php echo $username; ?>.png" title="<?php echo $username; ?>" /></p>
- <?php endif; ?>
Pass the variable $username
to the view in the home
action:
- $username = isset($_SESSION['user']) ? $_SESSION['user']['name'] : false;
- $content = view('home', $lang, compact('username'));
Create the folder avatars at the root of the site:
- /cms/site17
- avatars
IMPORTANT: Apache must be able to read and write files in this folder. Check the access rights on the directory.
$ chgrp www-data avatars
$ chmod 775 avatars
Enter http://localhost/cms/site17/avatar/foobar/40 in the address bar of your nagigator.
Save the image in the file foobar.png in the folder avatars.
Identify as foobar
with the password f00bar
.
Check that the avatar is displayed on the home page.
Comments