/** compute URL for a background image based on current time and index
  *
  * this routine returns a URL to be used as a background image. The
  * URL is different depending on the time of day and the $index parameter.
  * The latter is thought to be the position of the current item in the
  * main navigation (starting at 1) or 0 in case no item in the main
  * navigation is current.
  *
  * Configuration is done via two parameters:
  * the path in $this->config['header_banners_directory'] and
  * the interval in $this->config['header_banners_interval'].
  *
  * The interval is expressed in minutes. It means that a particular page
  * has the same background image for the duration of the interval and
  * another one in the next interval. The banners directory is supposed
  * to contain banner files of the correct dimensions, e.g. 980x170.
  *
  * If the specified directory starts with a '/' it is assumed to start in
  * the data root, e.g. something like /areas/exemplum/banners. This will
  * eventually lead to a URL of the form
  * /file.php/areas/exemplum/banners/mentha-banner.jpg using
  * was_file_url()
  *
  * If the specified directory does NOT start with a '/' it is assumed to
  * be a directory relative to the directory where index.php and friends
  * live, unless it starts with program/ in which case it is a static dir
  * somewhere in the program hierarchy. This is done via was_url().
  *
  * @param int $index indicates current main navigation item or 0 for none
  * @param bool $fully_qualified if TRUE forces the URL to contain a scheme, authority etc.
  * @return string ready to use URL
  * @uses was_file_url()
  * @uses was_url()
  */
 function cornelia_get_background_url($index, $fully_qualified = FALSE)
 {
     global $CFG;
     //
     // 0 -- sanity checks
     //
     $path = trim($this->config['header_banners_directory']);
     $interval = intval($this->config['header_banners_interval']);
     if (empty($path) || $interval <= 0) {
         return FALSE;
     }
     if (!utf8_validate($path) || strpos('/' . $path . '/', '/../') !== FALSE) {
         logger(sprintf("%s.%s(): invalid path '%s'; bailing out", __CLASS__, __FUNCTION__, $path));
         return FALSE;
     }
     //
     // 1 -- where to find the files?
     //
     if (substr($path, 0, 1) == '/') {
         $full_path = $CFG->datadir . $path;
     } elseif (substr($path, 0, 8) == 'program/') {
         $full_path = $CFG->progdir . substr($path, 7);
     } else {
         $full_path = $CFG->dir . '/' . $path;
     }
     // get rid of trailing slash if any
     if (substr($full_path, -1) == '/') {
         $full_path = substr($full_path, 0, -1);
     }
     if (($handle = @opendir($full_path)) === FALSE) {
         logger(sprintf("%s.%s(): cannot open directory '%s'", __CLASS__, __FUNCTION__, $path));
         return FALSE;
     }
     //
     // 2 -- scan the directory for graphics files (skip the thumbnails)
     //
     $prefix_len = strlen(THUMBNAIL_PREFIX);
     $extensions = explode(',', str_replace(array('.', ' '), '', $CFG->filemanager_images));
     $files = array();
     while (($entryname = readdir($handle)) !== FALSE) {
         $full_entryname = $full_path . '/' . $entryname;
         if ($entryname == '.' || $entryname == '..' || $entryname == 'index.html' || is_link($full_entryname) || !is_file($full_entryname)) {
             continue;
         }
         // we are now fairly sure $entryname is a genuine file. check extension (if any)
         if (strpos($entryname, '.') === FALSE) {
             $ext = '';
         } else {
             $components = explode('.', $entryname);
             $ext = utf8_strtolower(array_pop($components));
             unset($components);
         }
         if (array_search($ext, $extensions) === FALSE) {
             // not an image file, next please
             continue;
         }
         if (substr($entryname, 0, $prefix_len) == THUMBNAIL_PREFIX) {
             // thumbnail, next please
             continue;
         }
         if (($image_info = @getimagesize($full_entryname)) === FALSE) {
             // not an image, next please
             continue;
         }
         $files[] = $entryname;
     }
     closedir($handle);
     //
     // 3 -- is there any image at all?
     //
     if (($n = sizeof($files)) <= 0) {
         return FALSE;
     }
     if ($n == 1) {
         $entryname = $files[0];
         // Looks like Ford T: choice of 1
     } else {
         // use a different image every $interval minutes
         $entryname = $files[($index + time() / (60 * $interval)) % $n];
     }
     if (substr($path, 0, 1) == '/') {
         return was_file_url($path . '/' . $entryname, $fully_qualified);
     } else {
         return was_url($path . '/' . $entryname, $fully_qualified);
     }
 }
 /** construct the necessary code to show N static images in case JavaScript is OFF
  *
  * this steps through the first N images of the snapshots list and prepares the
  * necessary code to create and populate N static non-rotating img-tags,
  * sandwiched ina noscript tag (gracefull degradation...)
  *
  * The following slideshow parameters are used:
  *  - the available width
  *  - the available height
  *  - the number N of visible images
  *
  * The following parameters of the first N images are used:
  *  - width of the image (in pixels)
  *  - height of the image (in pixels)
  *  - the url of the image file (src-attribute of the img tag)
  *  - title to add to the display (document title)
  * Notably absent:
  *  - the number of seconds to display this image ;-)
  *
  * @param string $m code readability
  * @return void
  */
 function noscript_add_inline_show($m = '      ')
 {
     // sanity check
     $w = max(16, intval($this->inline_show_width));
     $h = max(16, intval($this->inline_show_height));
     $n = max(1, intval($this->inline_show_visible_images));
     $vx = intval($w / $n);
     $vy = $h;
     $index = 0;
     $this->theme->add_content($m . '<noscript>');
     if (($num_snapshots = sizeof($this->snapshots)) == 0) {
         $this->theme->add_content($m . '  ' . t('js_no_images', $this->domain));
     } else {
         foreach ($this->snapshots as $i => $snapshot) {
             if (++$index > $n) {
                 break;
             }
             $px = $snapshot['width'];
             $py = $snapshot['height'];
             $r = min($px * $vy, $py * $vx);
             $key = $snapshot['key'];
             $matches = array();
             $showtime = $this->default_showtime;
             if (preg_match('/^[0-9]*_([0-9]*)_/', $key, $matches)) {
                 $showtime = intval($matches[1]);
                 if (0 < $showtime && $showtime < 3600) {
                     $key = substr($snapshot['key'], strlen($matches[0]));
                 }
             }
             $attributes = array('width' => intval($r / $py), 'height' => intval($r / $px), 'title' => sprintf('%d/%d - %s (%dx%d, %s)', $index, $num_snapshots, $key, $snapshot['width'], $snapshot['height'], $snapshot['human_size']));
             $src = was_file_url($snapshot['image']);
             $this->theme->add_content($m . '  ' . html_img($src, $attributes));
         }
     }
     $this->theme->add_content($m . '</noscript>');
     return;
 }