function smd_random_banner($atts) {
global $img_dir, $file_base_path, $smd_rbanLang;
require_plugin('smd_lib');
// MLP support
$smd_rban_str = array(
'width' => '300',
'height' => '75',
'no_plug_text' => '',
);
$smd_rbanLang = new smd_MLP('smd_random_banner', 'smd_rban', $smd_rban_str);
if (!defined('SMD_DLM_TR')) {
define('SMD_DLM_TR', '@@');
}
if (!defined('SMD_DLM_REC')) {
define('SMD_DLM_REC', '|');
}
// allowable options (default listed first)
$allMode = array("image","file");
$allCompare = array("image:name","image:id","image:alt","image:caption","image:category","file:filename","file:id","file:category","file:description","file:status","file:size");
$allLinkIn = array("","image:caption","image:alt","image:name","file:description");
// lookup tables
$htmlObjFixed = array("id","data","height","type","width","playerversion","backcolor","elementid","uid");
$htmlObjParam = array("class","title","style","dir","lang","xml:lang","align","archive","border","classid","codebase","codetype","declare","hspace","name","standby","usemap","vspace");
$objMimeType = array("avi" => array("video/avi", "Video"),
"fla|flac" => array("audio/flac", "Audio"),
"flv|swf" => array("application/x-shockwave-flash", "Flash"),
"gif" => array("image/gif", "Image"),
"java" => array("application/x-java-applet", "App"),
"jpg" => array("image/jpeg", "Image"),
"mid" => array("audio/x-midi", "Audio"),
"mov" => array("video/quicktime", "Video"),
"mpg|mpeg|mp4" => array("video/mpeg", "Video"),
"mp2|mp3|m4a" => array("audio/mpeg", "Audio"),
"ogv|oga|ogx|ogg" => array("application/ogg", "Audio"),
"ofr" => array("application/x-ofr", "Audio"),
"png" => array("image/png", "Image"),
"ra|rm" => array("audio/x-pn-realvideo", "Video"),
"swo" => array("text/javascript", "SWFObject"),
"tif|tiff" => array("image/tiff", "Image"),
"wav" => array("audio/x-wav", "Audio"),
"wmv|wma|asf" => array("video/x-ms-wmv", "Audio"),
);
$objTemplateFixed = 'type="@@type" data="@@data" width="@@width" height="@@height" @@others ';
$objTemplate = array(
"Flash" => array(''),
"SWFObject" => array(''.n),
"Audio" => array(''),
"Video" => array(''),
"Image" => array('', ''),
);
// Log whether any attempt has been made to use category/id BEFORE lAtts(), which assigns them regardless
$attemptMatch = (isset($atts['category']) || isset($atts['id'])) ? true : false;
// Plugin options
extract(lAtts(array(
'mode' => 'image',
'category' => '',
'id' => '',
'subcats' => '0',
'match_text' => '',
'avoid_text' => '',
'compare_to' => 'name',
'limit' => '1',
'always_display' => '0',
'thumb' => '0',
'link_in' => '',
'link_prefix' => '',
'display_url' => '1',
'referer' => 'bid',
'raw_url' => '0',
'target' => '',
'count_downloads' => '1',
'files_dir' => 'files',
'delim' => ',',
'nv_delim' => '=',
'no_plug_text' => '',
'global_params' => '',
'imgclass' => '', // DEPRECATED
'class' => '',
'debug' => '0',
), $atts));
// Validate mode / link_in / target / class
$mode = (in_array($mode,$allMode)) ? $mode : $allMode[0];
$link_in = ($link_in && in_array($mode.':'.$link_in,$allLinkIn)) ? $link_in : $allLinkIn[0];
$target = ($target) ? ' target="' .$target. '"' : '';
$class = (!$class && $imgclass) ? $imgclass : $class;
// Validate compare_to
$compareToOpts = array();
if (isset($compare_to)) {
$ctList = do_list($compare_to);
$ctCount = count($ctList);
for ($idx = 0; $idx < $ctCount; $idx++) {
if(in_array($mode.':'.$ctList[$idx],$allCompare)) {
$compareToOpts[] = $ctList[$idx];
}
}
}
if(count($compareToOpts) <= 0) {
// Find the first entry matching $mode and use that
foreach ($allCompare as $cmp) {
if (strpos($cmp, $mode.':') === 0) {
$compareToOpts[] = substr($cmp,strlen($mode)+1);
break;
}
}
}
// If no_plug_text is a form, handle accordingly
$noplug = do_list($no_plug_text, ':');
if (count($noplug) > 0 && $noplug[0] == 'txpform') {
$no_plug_text = parse_form($noplug[1]);
}
// init variables
$clause = '1=1';
$clauseCat = $clause;
$clauseOR = array();
$fields = ($mode=='image') ? 'id, name, ext, caption, alt' : 'id, filename, description, status, size';
$dbTable = ($mode=='image') ? 'txp_image' : 'txp_file';
$subinfo = ($subcats) ? $mode.':'.$subcats : '';
// Extract all the fixed, ? and ! strings from category, ID, match and avoid texts
list($idinc, $idexc) = smd_doList($id, true, '', true, $delim);
list($catinc, $catexc) = smd_doList($category, false, $subinfo, true, $delim);
list($matchArray, $matchexc) = smd_doList($match_text, false, '', false, $delim);
list($avoidArray, $avoidexc) = smd_doList($avoid_text, false, '', false, $delim);
// Combine lists if applicable (since avoid_text="!squid" is a double negative and means match_text="squid")
$matchArray = array_merge($matchArray, $avoidexc);
$avoidArray = array_merge($avoidArray, $matchexc);
// Surround each entry in the ID/cat lists with quotes and make an SQL "in" list
if ($idinc) {
$clauseOR[] = $dbTable.'.id IN ('.implode(",", $idinc).')';
}
if ($catinc) {
$clauseOR[] = $dbTable.'.category IN ('.implode(",", $catinc).')';
}
if (!empty($clauseOR)) {
$clauseCat .= ' AND ('.implode(" OR ", $clauseOR).') ';
}
$clauseCat .= ($catexc) ? ' AND '.$dbTable.'.category NOT IN ('.implode(",", $catexc).')' : '';
$clauseCat .= ($idexc) ? ' AND '.$dbTable.'.id NOT IN ('.implode(",", $idexc).')' : '';
// If a category/id option was chosen and they matched nothing, use a bogus category that should not exist.
// This ensures that the plugin does the expected thing of matching nothing in the event it finds nothing.
if ($clauseCat == '1=1' && $attemptMatch) {
$clause = $dbTable.'.category = "smd_' .mt_rand(). '"';
} else {
$clause = $clauseCat;
}
// Now factor in any match/avoid text, comparing everything with whatever is in compare_to
$clauseAND = array();
$clauseMATCH = array();
$clauseAVOID = array();
foreach ($compareToOpts as $comp) {
for ($idx = 0; $idx < count($matchArray); $idx++) {
$clauseMATCH[] = $dbTable.'.'.$comp . " LIKE '%" .$matchArray[$idx]. "%'";
}
for ($idx = 0; $idx < count($avoidArray); $idx++) {
$clauseAVOID[] = $dbTable.'.'.$comp . " LIKE '%" .$avoidArray[$idx]. "%'";
}
}
if (count($clauseMATCH) > 0) {
$clauseAND[] = '(' .implode(' OR ',$clauseMATCH). ')';
}
if (count($clauseAVOID) > 0) {
$clauseAND[] = 'NOT (' .implode(' OR ',$clauseAVOID). ')';
}
if (count($clauseAND) > 0) {
$clause .= ' AND ' . implode(' AND ', $clauseAND);
}
// Need to keep clause, clauseCat and clauseSort separate for always_display
$clauseSort = " ORDER BY RAND() LIMIT $limit";
$clause .= $clauseSort;
if ($debug > 1) {
echo "++ INCLUDED/EXCLUDED IDs ++";
dmp($idinc);
dmp($idexc);
echo "++ INCLUDED/EXCLUDED Categories ++";
dmp($catinc);
dmp($catexc);
echo "++ INCLUDED/EXCLUDED Matches ++";
dmp($matchArray);
dmp($avoidArray);
dmp($clauseAND);
}
if ($debug > 0) {
echo "++ WHERE CLAUSES ++";
dmp($clauseCat);
dmp($clause);
}
// Pull out the requested objects, honouring the always_display level if nothing's returned
$rs = safe_rows($fields, $dbTable, $clause);
if (!$rs) {
if ($always_display > 0) {
$rs = safe_rows($fields, $dbTable, $clauseCat . $clauseSort);
if (!$rs) {
if ($always_display > 1) {
$rs = safe_rows($fields, $dbTable, "1=1".$clauseSort);
}
}
}
}
if ($debug > 2) {
echo "++ RECORD SET ++";
dmp($rs);
}
// Display them
$outStr = '';
if($rs) {
foreach($rs as $row) {
$objOut = '';
$linkurl = '';
$linkName = '';
$setMimeType = '';
$trimOpts = " \x22\t\n\r\0\x0B";
$objParams = array();
$theParams = array();
$flashVars = array();
$templateOptions = array();
// Extract parameters
switch ($mode) {
case "image":
$linkName = ($link_in) ? trim($row[$link_in]) : '';
break;
case "file":
$theDesc = do_list($global_params.$row[$link_in], ";");
if ($debug > 2) {
echo "++ DESCRIPTION FIELD CONTENTS ++";
dmp($theDesc);
}
foreach ($theDesc as $params) {
$params = trim($params);
if ($params != "") {
$theseParams = do_list($params, $nv_delim);
$paramName = strtolower($theseParams[0]);
$paramVal = trim($theseParams[1],$trimOpts);
if ($paramName == "link_url" && $link_in) {
$linkName = $paramVal;
} else if ($paramName == "mime_type") {
$setMimeType = $paramVal;
} else if ($paramName == "flashvars") {
$theParams[$paramName] = $paramVal;
$varstr = do_list($paramVal, "&");
foreach ($varstr as $var) {
$nvPair = do_list($var, "=");
$flashVars[$nvPair[0]] = $nvPair[1];
}
} else if (in_array($paramName,$htmlObjFixed)) {
$objParams[SMD_DLM_TR.$paramName] = $paramVal;
} else if (in_array($paramName,$htmlObjParam)) {
$objParams[SMD_DLM_TR.$paramName] = $paramVal;
$templateOptions[$paramName] = $paramName. '="' .SMD_DLM_TR.$paramName. '"';
} else {
$theParams[$paramName] = $paramVal;
}
}
}
break;
}
// Handle links
$urlpfx = "http://";
if (strlen($linkName) > 0) {
if (strncmp($linkName, $urlpfx, strlen($urlpfx)) == 0) {
$linkurl = $linkName;
} else if (substr($linkName, 0, 1) == '/' && $link_prefix == '') {
$linkurl = site_url(array()) . substr($linkName,1);
} else if (substr($link_prefix,0,1) == '/') {
$linkurl = site_url(array()) . substr($link_prefix,1) . $linkName;
} else {
$linkurl = $link_prefix . $linkName;
}
}
if ($debug > 0) {
echo "++ DESTINATION LINK ++";
dmp($linkurl);
}
// Throw object out to the page
if ($raw_url) {
switch ($mode) {
case "image":
$outStr = hu.$img_dir . DS .$row['id'] . (($thumb) ? 't' : '') . $row['ext'];
break;
case "file":
$outStr = ($count_downloads) ? hu.gTxt('file_download'). DS .$row['id'] : hu.$files_dir. DS .$row['filename'];
break;
}
} else {
switch($mode) {
case "image":
$imgOptions = array("id" => $row['id']);
if ($class != '') {
$imgOptions["class"] = $class;
}
$objOut = (($thumb) ? thumbnail($imgOptions) : image($imgOptions));
// Since the alt/caption may have been hijacked for link_in, use the other value if instructed
if (!$display_url) {
switch ($link_in) {
case "alt":
$objOut = str_replace('alt="'.$row['alt'].'"', 'alt="'.$row['caption'].'"', $objOut);
break;
case "caption":
$objOut = str_replace('title="'.$row['caption'].'"', 'title="'.$row['alt'].'"', $objOut);
break;
}
}
break;
case "file":
$templateStart = '';
$templateEnd = '';
$filePath = build_file_path($file_base_path, $row['filename']);
$fileDL = ($count_downloads) ? hu.gTxt('file_download') .DS. $row['id'] : hu.$files_dir .DS. $row['filename'];
if (file_exists($filePath)) {
// Get the file extension and look it up in the table
$pathBit = pathinfo($filePath);
$pathExt = ($setMimeType) ? $setMimeType : $pathBit['extension'];
foreach ($objMimeType as $exts => $types ) {
$extList = explode(SMD_DLM_REC,$exts);
// Set some sensible defaults if the mandatory options are not supplied
if (in_array($pathExt, $extList)) {
if (!array_key_exists(SMD_DLM_TR."type", $objParams)) {
$objParams[SMD_DLM_TR."type"] = $types[0];
}
if (!array_key_exists(SMD_DLM_TR."data", $objParams)) {
$objParams[SMD_DLM_TR."data"] = $fileDL;
}
if (!array_key_exists(SMD_DLM_TR."id", $objParams)) {
$objParams[SMD_DLM_TR."id"] = "id".mt_rand();
}
if (!array_key_exists(SMD_DLM_TR."width", $objParams)) {
$objParams[SMD_DLM_TR."width"] = $smd_rbanLang->gTxt('width');
}
if (!array_key_exists(SMD_DLM_TR."height", $objParams)) {
$objParams[SMD_DLM_TR."height"] = $smd_rbanLang->gTxt('height');
}
if (!array_key_exists(SMD_DLM_TR."playerversion", $objParams)) {
$objParams[SMD_DLM_TR."playerversion"] = "8";
}
if (!array_key_exists(SMD_DLM_TR."backcolor", $objParams)) {
$objParams[SMD_DLM_TR."backcolor"] = '#ffffff';
}
if (!array_key_exists(SMD_DLM_TR."elementid", $objParams)) {
$objParams[SMD_DLM_TR."elementid"] = "flashcontent_".mt_rand();
}
if (!array_key_exists(SMD_DLM_TR."uid", $objParams)) {
$objParams[SMD_DLM_TR."uid"] = "smd_".mt_rand();
}
$uid = $objParams[SMD_DLM_TR."uid"];
if ($class != '' && !array_key_exists(SMD_DLM_TR."class", $objParams)) {
$objParams[SMD_DLM_TR."class"] = $class;
$templateOptions[] = 'class="' .SMD_DLM_TR. 'class"';
}
$templateStart = $objTemplate[$types[1]][0];
$templateEnd = $objTemplate[$types[1]][1];
$templateStart = str_replace(SMD_DLM_TR."others", implode(" ", $templateOptions), $templateStart);
$templateEnd = str_replace(SMD_DLM_TR."others", implode(" ", $templateOptions), $templateEnd);
}
}
$objOut .= strtr($templateStart, $objParams);
if ($debug > 1) {
echo "++ MIME INFO ++";
dmp($pathExt);
echo "++ OBJECT INFO ++";
dmp($objParams);
dmp($theParams);
dmp($flashVars);
}
if ($setMimeType == "swo") {
foreach ($theParams as $key => $value) {
if ($key != "flashvars") {
$objOut .= $uid.'.addParam("'.$key.'", "'.$value.'");'.n;
}
}
foreach ($flashVars as $name => $val) {
$objOut .= $uid.'.addVariable("'.$name.'", "'.$val.'");'.n;
}
} else {
foreach ($theParams as $key => $value) {
$objOut .= '';
}
}
$objOut .= (($setMimeType=="swo") ? '' : $no_plug_text) . strtr($templateEnd, $objParams).n;
}
break;
}
// Surround with link / referer if necessary
if (($link_in != "") && (strlen($linkurl) > 0)) {
if ($referer != "") {
$refertxt = smd_addQSVar($linkurl, $referer, $row['id']);
} else {
$refertxt = $linkurl;
}
$outStr .= '' . $objOut. '';
} else {
$outStr .= $objOut;
}
}
}
}
return $outStr;
}