]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - themes/Hawaiian/lib/random.php
moved Hawaiian/images/pictures/random.php to Hawaiian/lib/random.php
[SourceForge/phpwiki.git] / themes / Hawaiian / lib / random.php
1 <?php
2
3 rcs_id('$Id: random.php,v 1.1 2002-01-18 20:08:30 carstenklapp Exp $');
4
5 class RandomImage {
6
7     /**
8      * Constructor
9      */
10     function RandomImage ($dirname) {
11         if (empty($dirname)) {
12             trigger_error(sprintf(_("%s is empty."), 'dirname'),
13                           E_USER_NOTICE);
14             return ""; // early return
15         }
16
17         $this->readAvailableImages($dirname);
18         //echo "count is " . count($this->imageList) ."<br>\n";//tempdebugcode
19
20         if (empty($this->imageList)) {
21             trigger_error(sprintf(_("%s is empty."), 'imageList'),
22                           E_USER_NOTICE);
23             return ""; // early return
24         }
25         $this->_srand(); // Start with a good seed.
26
27         $random_num = mt_rand(0,count($this->imageList)-1);
28         //echo "random_num is " . $random_num;
29
30 //FIXME: Help! This is where it all craps out.
31 trigger_error("The random image class doesn't quite work yet. Help!", E_USER_NOTICE);
32
33         $imgname = $this->imageList[$random_num];
34
35         return "$dirname/" . $imgname;
36     }
37
38
39     /**
40      * Prepare a random seed.
41      * 
42      * How random do you want it? See
43      * http://download.php.net/manual/en/function.srand.php
44      * mt_srand ((double) microtime() * 1000000 / pi())
45      */
46     function _srand($seed = '') {
47         static $wascalled = FALSE;
48         if (!$wascalled) {
49             $seed = $seed === '' ? (double) microtime() * 1000000 : $seed;
50             srand($seed);
51             $wascalled = TRUE;
52         }
53     }
54
55
56     /**
57      * Build an array in $this->imageList of image files from
58      * $dirname. Files are considered images when it's suffix matches
59      * one from $InlineImages.
60      *
61      * (This is a variation of function LoadDir in lib/loadsave.php)
62      * See also http://www.php.net/manual/en/function.readdir.php
63      */
64     function readAvailableImages($dirname) {
65         @ $handle = opendir($dir = $dirname);
66         if (empty($handle)) {
67             trigger_error(sprintf(_("Unable to open directory '%s' for reading"),
68                                   $dir), E_USER_NOTICE);
69             return; // early return
70         }
71         $this->imageList = array();
72         while ($fn = readdir($handle)) {
73
74             if ($fn[0] == '.' || filetype("$dir/$fn") != 'file')
75                 continue;
76             global $InlineImages;
77             if (preg_match("/($InlineImages)$/i", $fn)) {
78                 array_push($this->imageList, "$fn");
79             //echo $fn."<br>\n"; //debug
80             }
81         }
82         closedir($handle);
83     }
84
85 };
86
87 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
88 // (c-file-style: "gnu")
89 // Local Variables:
90 // mode: php
91 // tab-width: 8
92 // c-basic-offset: 4
93 // c-hanging-comment-ender-p: nil
94 // indent-tabs-mode: nil
95 // End:   
96 ?>