10

Associate a user to an avatar

Create site17 by copying site16.

  1. /cms
    1. ...
    2. site16
    3. 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:

  1. /cms/site17
    1. actions
      1. avatar.php
  1. require_once 'identicon.php';
  2.  
  3. function avatar($lang, $arglist=false) {
  4.     $name=false;
  5.     $size=128;
  6.  
  7.     if ($arglist) {
  8.         if (isset($arglist[0])) {
  9.             $name = $arglist[0];
  10.         }
  11.         else if (isset($arglist['name'])) {
  12.             $name = $arglist['name'];
  13.         }
  14.         if (isset($arglist[1])) {
  15.             $size = $arglist[1];
  16.         }
  17.         else if (isset($arglist['size'])) {
  18.             $size = $arglist['size'];
  19.         }
  20.     }
  21.  
  22.     if (!$name or !$size or !is_numeric($size) or $size < 16 or $size > 128) {
  23.         return run('error/badrequest', $lang);
  24.     }
  25.  
  26.     $img = identicon($name, $size);
  27.  
  28.     header('Content-Type: image/png');
  29.     header("Content-Disposition: inline; filename=$name");
  30.  
  31.     imagepng($img);
  32.     imagedestroy($img);
  33.  
  34.     return false;
  35. }

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:

  1.         'avatar'                => 'avatar',

Add the file identicon.php in the folder library with the following content:

  1. /cms/site17
    1. library
      1. identicon.php

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:

  1.     $action=$args=$params=false;

Initializes the name of the action, the list of arguments and the list of parameters.

  1.             if ($query) {
  2.                 $params = array();
  3.                 foreach (explode('&', $query) as $q) {
  4.                     $p = explode('=', $q);
  5.                     if (count($p) == 2) {
  6.                         list($key, $value) = $p;
  7.                         if ($key) {
  8.                             $params[$key]=urldecode($value);
  9.                         }
  10.                     }
  11.                 }
  12.             }

Builds the list of parameters read after a ? in a URL.

  1.     $arglist = $args ? $params ? array_merge($args, $params) : $args : $params;
  2.  
  3.     run($action, $lang, $arglist);

Combines in $arglist the parameters read in a URL after the action name in $args and after the ? in $params.

  1. function run($action, $lang=false, $arglist=false) {

run has 3 arguments: the name of the action, the language and a list of parameters.

  1.     $farg = array();
  2.     if ($lang) {
  3.         $farg[] = $lang;
  4.     }
  5.     if ($arglist) {
  6.         $farg[] = $arglist;
  7.     }
  8.  
  9.     $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:

  1. function serviceunavailable($lang, $arglist=false) {
  2.     $closing_time=$arglist[0];
  3.     $opening_time=$arglist[1];

Add showing an avatar in the home page:

  1. <?php if (!empty($username)): ?>
  2. <p class="right"><img src="<?php echo $base_path; ?>/avatars/<?php echo $username; ?>.png" title="<?php echo $username; ?>" /></p>
  3. <?php endif; ?>
  1. <?php if (!empty($username)): ?>
  2. <p class="right"><img src="<?php echo $base_path; ?>/avatars/<?php echo $username; ?>.png" title="<?php echo $username; ?>" /></p>
  3. <?php endif; ?>

Pass the variable $username to the view in the home action:

  1.     $username = isset($_SESSION['user']) ? $_SESSION['user']['name'] : false;
  2.  
  3.     $content = view('home', $lang, compact('username'));

Create the folder avatars at the root of the site:

  1. /cms/site17
    1. 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

To add a comment, click here.