// v0.1 Initial public release
// v0.11 Added PFX to query (thanks mercury)
// v0.12 Fixed help to include trailing slashes (thanks vittorio)
// v0.13 Unreleased. Added look_in=category and referer
// v0.14 Added match_text and use_alt (thanks lukepermsn)
// v0.15 Added always_display and limit (thanks lukepermsn)
// v0.15a Bugfix for linkcaption (thanks mercury)
// v0.16 Tightened image() params for txp v4.0.4 compliance
// v0.17 Added thumb, linkprefix and linkalt options
// v0.17a bid option now works with existing url params (thanks Papuass)
// v0.18 Added imgclass option (thanks DigitalRealm)
// v0.2 Major overhaul. Added subcategory support (thanks lukepermsn and santheo). Uses smd_lib. Enhanced match_text/avoid_text so look_in is no longer needed; dropped use_alt and added compare_to; replaced linkcaption/linkalt with link_in; added id_prefix; linkprefix is now link_prefix
// v0.21 Added MLP(Multi-Lingual Pack) support
// v0.22 Added raw_url option
// v0.23 Against better judgement, added target option (thanks jstubbs, cdtavijit)
// v0.3 Added support for files as well as images - finally, video/swf banners are a reality!
// v0.3a Fixed: if category="?field" is empty/returns no results, plugin will no longer display a random image from the full pool
// v0.31 Extended always_display. Added nv_delim to specify name=value delimiter in file mode params. Fixed: define() warning
// v0.32 Added no_plug_text and global_params
// v0.33 Uses smd_lib v0.3. Added swfObject support. Can now override mime type.
// v0.34 Added static image support to files mode(!)
function smd_random_banner($atts) {
require_plugin('smd_lib');
// MLP support
global $img_dir, $file_base_path, $smd_rbanLang;
$smd_rban_str = array(
'width' => '300',
'height' => '75',
);
$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', '|');
}
$count_downloads = true;
// ** allowable options (default listed first)
$compareTo = array("image:name","image:id","image:alt","image:caption","image:category","file:filename","file:category","file:description","file:status","file:size");
$linkIn = array("","image:caption","image:alt","file:description");
$modes = array("image","file");
// ** 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"),
"swo" => array("text/javascript", "SWFObject"),
"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"),
"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('', ''),
);
// ** plugin options
if (is_array($atts)) extract($atts);
$catidopts = isset($category) ? true : false;
$category = isset($category) ? doSlash($category) : "";
$match_text = isset($match_text) ? $match_text : "";
$avoid_text = isset($avoid_text) ? $avoid_text : "";
$mode = (isset($mode) && in_array($mode,$modes)) ? $mode : $modes[0];
$referer = isset($referer) ? $referer : "bid";
$limit = isset($limit) ? $limit: "1";
$imgclass = isset($imgclass) ? $imgclass : "";
$id_prefix = isset($id_prefix) ? $id_prefix : "article-";
$link_prefix = isset($link_prefix) ? $link_prefix : "";
$link_in = (isset($link_in) && in_array($mode.":".$link_in,$linkIn)) ? $link_in : $linkIn[0];
$nv_delim = isset($nv_delim) ? $nv_delim : "=";
$subcats = isset($subcats) ? true : false;
$thumb = isset($thumb) ? true : false;
$raw_url = isset($raw_url) ? true : false;
$always_display = isset($always_display) ? $always_display : "0";
$target = isset($target) ? ' target="' .$target. '"' : "";
$count_downloads = (isset($count_downloads) && $count_downloads=="1") ? true : false;
$files_dir = isset($files_dir) ? $files_dir : "files";
$no_plug_text = isset($no_plug_text) ? $no_plug_text : "";
$global_params = isset($global_params) ? $global_params : "";
// Experimental: needs quote-ability fixing
// if (isset($templates)) {
// foreach (explode(",", $templates) as $tString) {
// $tItems = explode("::",$tString);
// $objTemplate[trim($tItems[0])] = $objTemplateFixed.trim($tItems[1]);
// }
// }
// Find out if no_plug_text is a form and handle accordingly
$noplug = smd_split($no_plug_text, false, ":");
if (count($noplug) > 0 && $noplug[0] == "txpform") {
$no_plug_text = parse_form($noplug[1]);
}
$compareToOpts = array();
if (isset($compare_to)) {
$ctList = preg_split('/[,\s]+/',$compare_to,-1,PREG_SPLIT_NO_EMPTY);
for ($idx = 0; $idx < count($ctList); $idx++) {
if(in_array($mode.":".$ctList[$idx],$compareTo)) {
$compareToOpts[] = $ctList[$idx];
}
}
}
if(count($compareToOpts) <= 0) {
// Find the first entry matching $mode and use that
foreach ($compareTo as $cmp) {
if (strpos($cmp, $mode.":") === 0) {
$compareToOpts[] = substr($cmp,strlen($mode)+1);
break;
}
}
}
// ** init variables
$where = "";
$fields = ($mode=="image") ? "id, ext, caption, alt" : "id, filename, description, status, size";
$dbTable = ($mode=="image") ? "txp_image" : "txp_file";
$matchArray = array();
$avoidArray = array();
$matchVars = array("?c", "?s", "?t", "?id", "?field");
$whereAND = array();
// ** process match_text
if ($match_text != "" ) {
// Convert the string into an SQL-style 'in' list.
// Process the special ('?something') cases too.
$retArray = smd_getOpts($match_text, $matchVars, $id_prefix, false);
$matchArray = $retArray[0];
// Make up the WHERE statement from the combined list of matches.
// The categories/image IDs are ORed together in brackets first
// before the negation options are tacked on as ANDs afterwards
$whereTmp = "";
$whereOR = array();
if (count($matchArray) > 0) {
foreach ($compareToOpts as $comp) {
$hold = array();
for ($idx = count($matchArray); $idx >=0; $idx--) {
if (empty($matchArray[$idx])) {
unset($matchArray[$idx]);
} else {
$hold[] = $comp . " LIKE '%" .$matchArray[$idx]. "%'";
}
}
$whereTmp = implode(" OR ",$hold);
if (trim($whereTmp) != "") {
$whereOR[] = $whereTmp;
}
}
}
if (count($whereOR) > 0) {
$whereAND[] = '(' .implode(" OR ",$whereOR). ')';
}
}
// ** do the same for avoid_text
if ($avoid_text != "" ) {
// Convert the string into an SQL-style 'in' list.
// Process the special ('?something') cases too.
$retArray = smd_getOpts($avoid_text, $matchVars, $id_prefix, false);
$avoidArray = $retArray[0];
// Make up the WHERE statement from the combined list of NOT matches.
$whereTmp = "";
$whereOR = array();
if (count($avoidArray) > 0) {
foreach ($compareToOpts as $comp) {
$hold = array();
for ($idx = count($avoidArray); $idx >=0; $idx--) {
if (empty($avoidArray[$idx])) {
unset($avoidArray[$idx]);
} else {
$hold[] = $comp . " LIKE '%" .$avoidArray[$idx]. "%'";
}
}
$whereTmp = implode(" OR ",$hold);
if (trim($whereTmp) != "") {
$whereOR[] = $whereTmp;
}
}
}
if (count($whereOR) > 0) {
$whereAND[] = 'NOT (' .implode(" OR ",$whereOR). ')';
}
}
// Work out the categories to search
$catsin = array();
$fullCatList = array();
$notCatList = array();
$catVars = array("?c","?id","?s","?t","?field","!c","!id","!s","!t","!field");
$retArray = smd_getOpts($category, $catVars, $id_prefix, false);
$fullCatList = $retArray[0];
$notCatList = $retArray[1];
// included categories
$tmpa = array();
for ($idx = 0; $idx < count($fullCatList); $idx++) {
if ($subcats) {
$categs = smd_getSubCats($fullCatList[$idx],$mode);
for ($jdx = 0; $jdx < count($categs); $jdx++) {
if ($categs[$jdx]['name'] != "root") {
$tmpa[] = "'" .$categs[$jdx]['name']. "'";
}
}
} else {
$tmpa[] = "'" .$fullCatList[$idx]. "'";
}
}
if (count($tmpa) > 0) {
$catsin[] = "category IN (" .implode(",", $tmpa). ")";
}
// excluded categories
$tmpa = array();
for ($idx = 0; $idx < count($notCatList); $idx++) {
if ($subcats) {
$categs = smd_getSubCats($notCatList[$idx],$mode);
for ($jdx = 0; $jdx < count($categs); $jdx++) {
if ($categs[$jdx]['name'] != "root") {
$tmpa[] = "'" .$categs[$jdx]['name']. "'";
}
}
} else {
$tmpa[] = "'" .$notCatList[$idx]. "'";
}
}
if (count($tmpa) > 0) {
$catsin[] = "category NOT IN (" .implode(",", $tmpa). ")";
}
if (count($catsin) > 0) {
$whereCat = implode(" AND ", $catsin);
} else {
// Issue a non-existent category if all the supplied categories are bogus.
// This ensures no match: without it, the whole image pool would be used
$whereCat = "category LIKE '" . (($catidopts) ? "smd".mt_rand() : "%") . "'";
}
// ** make up the final WHERE statement
$whereOB = " ORDER BY RAND() LIMIT $limit";
$where = $whereCat . ((count($whereAND)>0) ? " AND " .implode(" AND ",$whereAND) : "") . $whereOB;
// ** pull out the requested objects, honouring the always_display level if nothing's returned
$rs = safe_rows($fields, $dbTable, $where);
if (!$rs) {
if ($always_display > 0) {
$rs = safe_rows($fields, $dbTable, $whereCat . $whereOB);
if (!$rs) {
if ($always_display > 1) {
$rs = safe_rows($fields, $dbTable, "1=1".$whereOB);
}
}
}
}
// ** display them
$staticprefix = "http://";
$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();
switch ($mode) {
case "image":
$linkName=trim(($link_in=="alt") ? $row['alt'] : (($link_in=="caption") ? $row['caption'] : ""));
break;
case "file":
$theDesc = smd_split($global_params.$row['description'], false, ";\n");
foreach ($theDesc as $params) {
$params = trim($params);
if ($params != "") {
$theseParams = explode($nv_delim, $params);
$paramName = strtolower($theseParams[0]);
if ($paramName == "link_url") {
if ($link_in == "description") {
$linkName = trim($theseParams[1], $trimOpts);
}
} else if ($paramName == "mime_type") {
$setMimeType = trim($theseParams[1], $trimOpts);
} else if ($paramName == "flashvars") {
$varstr = explode("&",trim($theseParams[1], $trimOpts));
foreach ($varstr as $var) {
$nvPair = explode("=",$var);
$flashVars[$nvPair[0]] = $nvPair[1];
}
} else if (in_array($paramName,$htmlObjFixed)) {
$objParams[SMD_DLM_TR.$paramName] = trim($theseParams[1],$trimOpts);
} else if (in_array($paramName,$htmlObjParam)) {
$objParams[SMD_DLM_TR.$paramName] = trim($theseParams[1],$trimOpts);
$templateOptions[$paramName] = $paramName. '="' .SMD_DLM_TR.$paramName. '"';
} else {
$theParams[$paramName] = trim($theseParams[1],$trimOpts);
}
}
}
break;
}
// ** handle links
if (strlen($linkName) > 0) {
if (substr($linkName, 0, strlen($staticprefix)) === $staticprefix) {
$linkType = "FULL";
} else if ($linkName[0] == '/') {
if ($link_prefix == '') {
$linkType = "ROOT_NO_PREFIX";
} else if (substr($link_prefix, 0, strlen($staticprefix)) === $staticprefix) {
$linkType = "FULL_PREFIX";
} else if ($link_prefix[0] == '/') {
$linkType = "ROOT";
} else {
$linkType = "RELATIVE";
}
} else if ($link_prefix == '') {
$linkType = "RELATIVE";
} else if ($link_prefix[0] == '/') {
$linkType = "ROOT";
} else if (substr($link_prefix, 0, strlen($staticprefix)) === $staticprefix) {
$linkType = "FULL_PREFIX";
} else {
$linkType = "RELATIVE";
}
switch ($linkType) {
case "FULL":
$linkurl = $linkName;
break;
case "ROOT_NO_PREFIX":
$linkurl = site_url($atts) . substr($linkName,1);
break;
case "ROOT":
$linkurl = site_url($atts) . substr($link_prefix,1) . $linkName;
break;
case "RELATIVE":
default:
$linkurl = $link_prefix . $linkName;
break;
}
}
// ** throw object out to the page
if ($raw_url) {
switch ($mode) {
case "image":
$outStr = hu.$img_dir . "/" .$row['id'] . (($thumb) ? 't' : '') . $row['ext'];
break;
case "file":
$outStr = ($count_downloads) ? hu.gTxt('file_download').'/'.$row['id'] : hu.$files_dir.'/'.$row['filename'];
break;
}
} else {
switch($mode) {
case "image":
$imgOptions = array("id" => $row['id']);
if ($imgclass != '') {
$imgOptions["class"] = $imgclass;
}
$objOut = (($thumb) ? thumbnail($imgOptions) : image($imgOptions));
break;
case "file":
$templateStart = '';
$templateEnd = '';
$filePath = build_file_path($file_base_path, $row['filename']);
$fileDL = ($count_downloads) ? hu.gTxt('file_download').'/'.$row['id'] : hu.$files_dir.'/'.$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 ($imgclass != '' && !array_key_exists(SMD_DLM_TR."class", $objParams)) {
$objParams[SMD_DLM_TR."class"] = $imgclass;
$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 ($setMimeType == "swo") {
foreach ($theParams as $key => $value) {
$objOut .= $uid.'.addParam("'.$key.'", "'.$value.'");'.n;
}
foreach ($flashVars as $name => $val) {
$objOut .= $uid.'.addVariable("'.$name.'", "'.$val.'");'.n;
}
} else {
foreach ($theParams+$flashVars as $key => $value) {
$objOut .= '';
}
}
$objOut .= (($setMimeType=="swo") ? '' : $no_plug_text) . strtr($templateEnd, $objParams).n;
}
break;
}
if (($link_in != "") && (strlen($linkurl) > 0)) {
if ($referer != "") {
$refertxt = smd_addQSVar($linkurl, $referer, $row['id']);
} else {
$refertxt = $linkurl;
}
$outStr .= '' . $objOut. '';
} else {
$outStr .= $objOut;
}
}
}
} else {
return;
}
return $outStr;
}