function smd_random_text($atts, $thing=NULL) { global $txpcfg, $variable, $pretext, $thisarticle, $thisfile, $thislink, $thisimage; extract(lAtts(array( 'type' => 'string', 'source' => 'hello world', 'delim' => '|', 'file_delim' => '\n', 'file_delim_type' => 'char', // char or regex 'out_delim' => ',', 'item_num' => '', 'shuffle' => '0', 'trim' => '1', 'escape' => 'html', 'form' => '', 'limit' => '1', 'count' => 'up', 'wraptag' => '', 'break' => '', 'var_prefix' => 'smd_rnd_txt', 'class' => '', 'debug' => '0', ), $atts)); $srcpt = explode($delim, $source); $limit = ($limit < 1) ? 1 : $limit; $from = array(); $items = array(); $multirow = 1; switch ($type) { case "field": if (isset($thisfile[$srcpt[0]]) && $thisfile[$srcpt[0]] != "") { $from = $thisfile[$srcpt[0]]; } else if (isset($thislink[$srcpt[0]]) && $thislink[$srcpt[0]] != "") { $from = $thislink[$srcpt[0]]; } else if (isset($thisimage[$srcpt[0]]) && $thisimage[$srcpt[0]] != "") { $from = $thisimage[$srcpt[0]]; } else if (isset($pretext[$srcpt[0]]) && $pretext[$srcpt[0]] != "") { $from = $pretext[$srcpt[0]]; } else if (isset($thisarticle[$srcpt[0]]) && $thisarticle[$srcpt[0]] != "") { $from = $thisarticle[$srcpt[0]]; } if ($from) { $from = explode($delim, $from); $limit = (count($from) < $limit) ? count($from) : $limit; $items = ($item_num != '') ? ((count($from) >= $item_num) ? $item_num : 0) : array_rand($from, $limit); } break; case "txpvar": $from = explode($delim, $variable[$srcpt[0]]); $limit = (count($from) < $limit) ? count($from) : $limit; $items = ($item_num != '') ? ((count($from) >= $item_num) ? $item_num : 0) : array_rand($from, $limit); break; case "urlvar": $from = explode($delim, gps($srcpt[0])); $limit = (count($from) < $limit) ? count($from) : $limit; $items = ($item_num != '') ? ((count($from) >= $item_num) ? $item_num : 0) : array_rand($from, $limit); break; case "database": $where = "1=1 ORDER BY RAND() LIMIT ".$limit; $from = safe_rows($srcpt[1], $srcpt[0], $where, $debug); $multirow = count(explode(',', $srcpt[1])); $limit = count($from); $items = ($item_num != '') ? ((count($from) >= $item_num) ? $item_num : 0) : array_keys($from); break; case "file": $path = $txpcfg['doc_root'].DS.ltrim($srcpt[0], DS); if ($debug) { echo "++ FILE PATH ++"; dmp($path); } if (file_exists($path)) { if ($file_delim == '\n') { $from = doArray(file($path), 'rtrim'); } else if ($file_delim_type == 'char') { $from = explode($file_delim, file_get_contents($path)); } else if ($file_delim_type == 'regex') { // Cannot guarantee that a fixed delimiter won't break preg_match (and preg_quote doesn't help) so // dynamically assign the delimiter based on the first entry in $dlmPool that is NOT in the file_delim attribute. // This minimises (does not eliminate) the possibility of a TXP-initiated preg_match error, while still // preserving errors outside TXP's control (e.g. mangled user-submitted PCRE pattern) $dlmPool = array('/', '@', '#', '~', '`', '|', '!', '%'); $dlm = array_merge(array_diff($dlmPool, preg_split('//', $file_delim, -1))); $dlm = (count($dlm) > 0) ? $dlm[0].$file_delim.$dlm[0] : $file_delim; if ($debug) { echo "++ REGEX ++"; dmp($dlm); } $from = preg_split($dlm, file_get_contents($path)); } if ($debug > 3) { echo "++ SPLIT FILE CONTENTS ++"; dmp($from); } $limit = (count($from) < $limit) ? count($from) : $limit; $items = ($item_num != '') ? ((count($from) >= $item_num) ? $item_num : 0) : array_rand($from, $limit); } break; case "string": default: $from = $srcpt; $limit = (count($from) < $limit) ? count($from) : $limit; $items = ($item_num != '') ? ((count($from) >= $item_num) ? $item_num : 0) : array_rand($from, $limit); break; } // Force array if it isn't already (some PHP functions return single values) if (!is_array($items)) { $items = array($items); } if ($shuffle) { shuffle($items); } if ($multirow == 1) { $from = ($trim) ? doArray($from, 'trim') : $from; } if ($debug) { echo "++ ITEM POOL ++"; dmp($from); echo "++ CHOSEN ITEMS ++"; dmp($items); } if ($debug > 2) { echo "++ PRETEXT ++"; dmp($pretext); if (isset($thisfile)) { echo "++ THIS FILE ++"; dmp($thisfile); } if (isset($thislink)) { echo "++ THIS LINK ++"; dmp($thislink); } if (isset($thisimage)) { echo "++ THIS IMAGE ++"; dmp($thisimage); } if (isset($thisarticle)) { echo "++ THIS ARTICLE ++"; dmp($thisarticle); } } // Find out how to plonk the output $what = ($form) ? fetch_form($form) : (($thing) ? $thing : '{'.$var_prefix.'}'); // Pick the given $items keys out of the $from array and display them $out = array(); $ctr = ($count=="up") ? 0 : $limit-1; foreach ($items as $item) { $replacement = array(); if (is_array($from[$item])) { foreach ($from[$item] as $jdx => $val) { $val = ($trim) ? trim($val) : $val; $replacement['{'.$var_prefix.'_'.$jdx.'}'] = ($escape == 'html') ? htmlspecialchars($val) : $val; } $from[$item] = join($out_delim, $from[$item]); $from[$item] = ($trim) ? trim($from[$item]) : $from[$item]; } $replacement['{'.$var_prefix.'}'] = ($escape == 'html') ? htmlspecialchars($from[$item]) : $from[$item]; $replacement['{'.$var_prefix.'_rows}'] = $limit; $replacement['{'.$var_prefix.'_thisindex}'] = $ctr; $replacement['{'.$var_prefix.'_thisrow}'] = $ctr + 1; $replacement['{'.$var_prefix.'_chosen_item}'] = $item; if ($debug > 1) { echo "++ REPLACEMENTS ++"; dmp($replacement); } $out[] = parse(strtr($what, $replacement)); $ctr = ($count=="up") ? $ctr+1 : $ctr-1; } return doWrap($out, $wraptag, $break, $class); }