<?php
#BEGIN_LICENSE
#-------------------------------------------------------------------------
# Module: random_image (c) 2008 by Robert Campbell 
#         (calguy1000@cmsmadesimple.org)
#  A small plugin module to display a random image from a directory.
# 
#-------------------------------------------------------------------------
# CMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
# This project's homepage is: http://www.cmsmadesimple.org
#
#-------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# However, as a special exception to the GPL, this software is distributed
# as an addon module to CMS Made Simple.  You may not use this software
# in any Non GPL version of CMS Made simple, or in any version of CMS
# Made simple that does not indicate clearly and obviously in its admin 
# section that the site was built with CMS Made simple.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------
#END_LICENSE

function smarty_cms_function_random_image($params, &$smarty) {
        if(empty($params['dir']))
                $dirn = "uploads/images";
        else
                $dirn = $params['dir'];

        $myDir = dir($dirn);
        if( ! $myDir ) 
        {
           return "<!-- Directory does not exist -->";
        }
		
        $count=1;
        while( $name = $myDir->read() ) {
          ++$count;
        }
        $myDir->close();

        $myDir = dir($dirn);
        $done = false;
		
        while( $done == false )
          {
			rewinddir();		  
            $randnum = rand(3,($count-2)+1);
            $entryName = '';

            for($i=0;$i<$randnum;$i++)
              {
                $entryName = $myDir->read();
              }
			  
            if( $entryName == "." || $entryName == "..") continue;
              
            $myimage = $entryName;
            $done = true;
              
          }
		  
        $myDir->close();
		
        return $dirn . "/" . $myimage;
}

function smarty_cms_help_function_random_image() {
        ?>
        <h3>What does this do?</h3>
        <p>Grabs a random image from the image directory specified</p>
        <h3>How do I use it?</h3>
        <p>Just insert the tag into your template/page like: <code>{random_image dir="images/albums"}</code></p>
        <h3>What parameters does it take?</h3>
        <ul>
                <li><em>(optional)</em>dir - directory containing the images.</li>
        </ul>
        </p>
        <?php
}

function smarty_cms_about_function_random_image() {
        ?>
        <p>Author: Robert Campbell&lt;rob@techcom.dyndns.org&gt;</p>
        <p>Version: 1.0.2</p>
        <p>Change History:</p>
        <ul>
        <li>v 1.0 - Initial revision</li>
        <li>v 1.0.1 - Fix simple issue where sometimes . or .. would be returned in the result</li>
        <li>v 1.0.2 - More fixes (stikki)</li>
        </ul>
        <?php
}
?>
?>
