// 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!
register_callback('smd_rban_enum_str', 'l10n.enumerate_strings');
global $smd_rban_str;
$smd_rban_str = array(
'width' => '300',
'height' => '75',
);
function smd_random_banner($atts) {
require_plugin('smd_lib');
global $img_dir, $file_base_path, $smd_rban_str;
define('DLM_TR', '@@');
define('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");
$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"),
"java" => array("application/x-java-applet", "App"),
"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"),
"ra|rm" => array("audio/x-pn-realvideo", "Video"),
"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" => $objTemplateFixed. 'id="@@id">',
"Audio" => $objTemplateFixed. '>',
"Video" => $objTemplateFixed. '>',
);
// ** plugin options
if (is_array($atts)) extract($atts);
$category = isset($category) ? (($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];
$subcats = isset($subcats) ? true : false;
$thumb = isset($thumb) ? true : false;
$raw_url = isset($raw_url) ? true : false;
$always_display = isset($always_display) ? true : false;
$target = isset($target) ? ' target="' .$target. '"' : "";
$count_downloads = (isset($count_downloads) && $count_downloads=="1") ? true : false;
$files_dir = isset($files_dir) ? $files_dir : "files";
// Experimental: needs quote-ability fixing
if (isset($templates)) {
foreach (explode(",", $templates) as $tString) {
$tItems = explode("::",$tString);
$objTemplate[trim($tItems[0])] = $objTemplateFixed.trim($tItems[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_getAtts($match_text, $matchVars, $id_prefix);
$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_getAtts($avoid_text, $matchVars, $id_prefix);
$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_getAtts($category, $catVars, $id_prefix);
$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 (($category == "%") || (count($catsin) <= 0)) {
$whereCat = "category LIKE '%'";
} else {
$whereCat = implode(" AND ", $catsin);
}
// ** 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
$rs = safe_rows($fields, $dbTable, $where);
if (!$rs) {
if ($always_display) {
$rs = safe_rows($fields, $dbTable, $whereCat . $whereOB);
}
}
// ** display them
$staticprefix = "http://";
$outStr = '';
if($rs) {
foreach($rs as $row) {
$linkurl = '';
$linkName = '';
$trimOpts = " \x22\t\n\r\0\x0B";
$objParams = array();
$theParams = array();
$templateOptions = array();
switch ($mode) {
case "image":
$linkName=trim(($link_in=="alt") ? $row['alt'] : (($link_in=="caption") ? $row['caption'] : ""));
break;
case "file":
$theDesc = smd_splitRange($row['description'], ";\n");
foreach ($theDesc as $params) {
$params = trim($params);
if ($params != "") {
$theseParams = explode("=", $params);
if ($theseParams[0] == "link_url") {
if ($link_in == "description") {
$linkName = trim($theseParams[1], $trimOpts);
}
} else if (in_array($theseParams[0],$htmlObjFixed)) {
$objParams[DLM_TR.$theseParams[0]] = trim($theseParams[1],$trimOpts);
} else if (in_array($theseParams[0],$htmlObjParam)) {
$objParams[DLM_TR.$theseParams[0]] = trim($theseParams[1],$trimOpts);
$templateOptions[] = $theseParams[0]. '="' .DLM_TR.$theseParams[0]. '"';
} else {
$theParams[$theseParams[0]] = 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":
$template = '';
$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 = $pathBit['extension'];
foreach ($objMimeType as $exts => $types ) {
if (in_array($pathExt, explode(DLM_REC,$exts))) {
if (!array_key_exists(DLM_TR."type", $objParams)) {
$objParams[DLM_TR."type"] = $types[0];
}
if (!array_key_exists(DLM_TR."data", $objParams)) {
$objParams[DLM_TR."data"] = $fileDL;
}
if (!array_key_exists(DLM_TR."id", $objParams)) {
$objParams[DLM_TR."id"] = "id".mt_rand();
}
if (!array_key_exists(DLM_TR."width", $objParams)) {
$objParams[DLM_TR."width"] = smd_gTxt($smd_rban_str,'width');
}
if (!array_key_exists(DLM_TR."height", $objParams)) {
$objParams[DLM_TR."height"] = smd_gTxt($smd_rban_str,'height');
}
if ($imgclass != '' && !array_key_exists(DLM_TR."class", $objParams)) {
$objParams[DLM_TR."class"] = $imgclass;
$templateOptions[] = 'class="' .DLM_TR. 'class"';
}
$template = $objTemplate[$types[1]];
$template = str_replace(DLM_TR."others", implode(" ", $templateOptions), $template);
}
}
$objOut = "".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;
}
// Callback for MLP string replacement
function smd_rban_enum_str($event, $step='', $pre=0) {
global $smd_rban_str, $smdMLP;
$r = array (
'owner' => 'smd_random_banner',
'prefix' => $smdMLP['smd_random_banner'],
'lang' => 'en-gb', // Default language
'event' => 'public', // public/admin/common = which interface the strings will be loaded into
'strings' => $smd_rban_str,
);
return $r;
}