Location: PHPKode > projects > Charray's CMS > ccms/module/random.php
<?php

// Charray's CMS (CCMS) Version 0.9.5
//
// Copyright (c) 2007, Kinson Chan
// charray[at]gmail.com / kchan[at]cs.hku.hk
// All rights reserved.
//
// Please refer to LICENSE.txt coming with this package for
// terms and conditions for redistribution and reuse.

if (!defined("CCMS_IN")) {
	die();
}

global $ccms_decoder_path;
require_once ($ccms_decoder_path . '/gallery.php');

function ccms_module_random($file) {
	global $ccms_data_path;
	global $ccms_controller_url;

	// Pick a picture. Proceed only when something is found.
	if ($picture = ccms_module_random_pick($ccms_data_path)) {
		$thumbnail = ccms_decoder_gallery_thumb($picture);
		$thumbnail_path = ccms_obtain_path($thumbnail, true);
		$picture_path = ccms_obtain_path($picture, true);
		$picture_alt = ccms_path_title($picture_path);
		$picture_alt = substr($picture_alt, 0, strrpos($picture_alt, '.'));
		$picture_alt = htmlentities($picture_alt, ENT_COMPAT, 'UTF-8');

		$result['title'] = 'Random Image';
		$result['content'] = '<p style="text-align: center;">' .
		                     '<a href="' . $ccms_controller_url .
				     $picture_path .
		                     '"><img src="' . $ccms_controller_url .
				     $thumbnail_path .
				     '" alt="' . $picture_alt .
				     '" style="width: 90%;" /></a></p>';
		return $result;
	} else {
		return false;
	}
}

function ccms_module_random_pick($dir) {
	$possible_results = array();
	$sub_files = ccms_list_directory($dir);

	// This is a recursive function.
	// Files and subdirectories are located. The function is
	// recursively called again for directories.
	foreach ($sub_files as $sub_file) {
		if (is_dir($sub_file)) {
			if ($temp = ccms_module_random_pick($sub_file)) {
				$possible_results[] = $temp;
			}
		} else if (ccms_decoder_gallery_displayable($sub_file, false,
		                                            false, $dummy))
		{
			$possible_results[] = $sub_file;
		}
	}

	// After collecting results from previous calls, randomly
	// pick any one of it.
	if (count($possible_results)) {
		return $possible_results[rand(0, count($possible_results) - 1)];
		die();
	} else {
		return false;
	}
}

?>
Return current item: Charray's CMS