@Pappnase++:
would it be possible that yu chnage the script this way we can place it into the frontend_init folder? so there is no change of the source needed.
Sure - the most functions are duplicate then but easyer for update etc...
---
I will post the code here (asap)!
========
He we are...
Code: Select all
<?php
/** ---------------------------------------------------------------------------
*
* -=[ Extended {RANDOM} ]=-
*
* gently extented the CORE::{RANDOM}-RT to the possibility to
* create an RANDOM-Value only one times per interval -
* the random-image will now show the whole interval
*
* @4phpwcms 1.2.6DEV (and before?- not testet yet!)
* @see request www.phpwcms.de/forum/viewtopic.php?t=5605
* by cyrano (1st), pappnase (4frontend_render)
* @author Neelix
* @licence "THE BEER-WARE LICENSE" (Revision 42):
* Neelix wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return Neelix.
* @version 0.4-DEV
* @see CORE::front.func.inc.php for original {RANDOM}
*
* @example generic {XRANDOM:[path]:[interval]}
* - [path] = search here for images
* - [interval] - using the old value
* interval >0 => interval in minutes
* interval =0 => like the core RT - new image on every call
* @example live {XRANDOM:picture/my_random_images:1440}
* This shows your random images which you have placed in a subfolder
* called "my_random_images" within the folder "picture" with an
* intervall of 1440 min = 24 hours.
*
* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
*
* Install:
*
* -----[ COPY ]-----------------------------------------
* this file to your render folder 'phpwcms_template/inc_script/frontend_render/'
*
* -----[ SQL ]------------------------------------------
*
* -!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!
* if you use an DB-Tableprefix (DB_PREPEND)
* => insert this before phpwcms_random
* example: CREATE TABLE my_phpwcms_random (
* -!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!
*
* CREATE TABLE phpwcms_random (
* rid bigint(20) unsigned NOT NULL default '0',
* datum datetime NOT NULL default '0000-00-00 00:00:00',
* PRIMARY KEY (datum)
* ) TYPE=MyISAM COMMENT='save values for {XRANDOM}';
*
* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
*
* history: v0.1 - stand alone - without core connection
* v0.2 - using core {random} with fixed interval
* (1time/day)
* v0.3 - extends interval in minutes
* v0.4 - build 4 frontend_render, rename to XRANDOM
* ----------------------------------------------------------------------------
*/
class ClExtRandom
{
var $_imgArray; /** @private Array[String] */
var $_imgPath; /** @private String */
/**
* Konstruktor:
*/
function ClExtRandom()
{
$this->_imgArray = array();
$this->_imgPath = '';
} //fnc-end
/**
* returns an random image (html-tag) from the give path
* - it looks for image of following type:
* - gif, jpg, jpeg, png
*
* @param String $path look here for images
* @param Integer $rand interval in min
* @return String html-img-tag
* @access public
*/
function getRandomImageTag($path,$rand)
{
$this->_imgPath = str_replace('//', '/', PHPWCMS_ROOT.'/'.$path.'/');
$imageinfo = false;
if (is_dir($this->_imgPath))
{
$handle = opendir( $this->_imgPath );
while($file = readdir( $handle ))
{
if ( $file != "." && $file != ".." )
{
if ( preg_match('/(\.jpg|\.jpeg|\.gif|\.png)$/', strtolower($file)) )
$this->_imgArray[] = $file;
}
}
closedir( $handle );
}
if (count($this->_imgArray)) $imageinfo = $this->_isRandomImage(0, $rand);
return ($imageinfo ? '<img src="'.$path.'/'.$imageinfo["imagename"].'" '.$imageinfo[3].' border="0" alt="'.$imageinfo["imagename"].'" />' : '');
} //fnc-end
/**
* get the last id from DB
* @param Integer $iInterval interval in min, optional default=1440
* @return Integer get the last value .or. -1 if nothing found
* @access privat
*/
function _getRandomID($iInterval=1440)
{
$sQuery = 'SELECT rid FROM '.DB_PREPEND.'phpwcms_random WHERE (NOW()<DATE_ADD(datum, Interval '.$iInterval.' minute));';
$hResult = mysql_query($sQuery, $GLOBALS['db']);
$row = mysql_fetch_object($hResult);
return ($row?$row->rid:-1); //0 can be an valid value! => negativ's are save
} //fnc-end
/**
* save the new id in the DB
* - update is also possible, if you get an easy PK for the table; but now I get this way...
* @param Integer $iRID new ID
* @return Boolean sucessful or error
* @access privat
*/
function _saveRandomID($iRID)
{
$sQuery1 = 'DELETE FROM '.DB_PREPEND.'phpwcms_random WHERE DATUM>0';
$sQuery2 = 'INSERT INTO '.DB_PREPEND.'phpwcms_random(rid,datum) VALUES ('.$iRID.', NOW());';
$hResult = mysql_query($sQuery1, $GLOBALS['db']);
$hResult = mysql_query($sQuery2, $GLOBALS['db']);
return $hResult; //ok .or. err
} //fnc-end
/**
* check 4 valid image
* @param Integer $count Counter , optional default=0 (4error: call again max size of _imgArray)
* @param Integer $rand Last Random ID, optional default=0
* @return Array with image infos
* @see PHP::getimagesize()
* @access privat
*/
function _isRandomImage($count=0, $rand=0)
{
$count++;
$rid=-1; //init
$randval=0; //-=-
//$rand==1 => use the value in the DB
if ($rand)
{
$rid = $this->_getRandomID($rand);
$randval = $rid;
}
//if normal || new date: $rid is set to init
//if extended we need an new id ...
if ($rid<0)
{
mt_srand ((double) microtime() * 1000000); //this is the init/start-value for the mt_rand-algorithm: here "now"
$randval = mt_rand( 0, count($this->_imgArray) - 1 );
if ($rand) $this->_saveRandomID($randval); //... and save it
}
$file = $this->_imgPath . $this->_imgArray[ $randval ];
//gets -> better tests for image info
$imageinfo = @getimagesize($file);
//if $imageinfo is not true repeat function and count smaller count all images
if(!$imageinfo)
{
if($count < count($this->_imgArray)) $imageinfo = $this->_isRandomImage($count, $rand);
}
else
{
$imageinfo["imagename"] = $this->_imgArray[ $randval ];
}
return $imageinfo;
} //fnc-end
};//end-class
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//anybody out there? - the same old story, but here in CLASS-manner (I hope it's possible in older PHPs to...)
if (!strpos($content['all'],'{XRANDOM:')===false)
{
$myRand = new ClExtRandom();
$content['all'] = preg_replace('/\{XRANDOM:(.*?):(\d+)\}/e', '$myRand->getRandomImageTag("$1","$2")', $content['all']);
}
?>
tested only with 1.2.6DEV!!!! (yet)
Erfahrung ist das, was man besitzt, kurz nach dem es gebraucht wurde.
Warning: I have no foggiest idea of English/German, but I do
-ha{p}{p}y day/night/dia/noche-