global $smdMLP; $smdMLP = array( 'smd_slimbox' => 'smd_sbox', 'smd_slimbox_inc' => 'smd_sbox', 'smd_random_banner' => 'smd_rban', ); function smd_addQSVar($url, $key, $value) { $url = smd_removeQSVar($url, $key); if (strpos($url, '?') === false) { return ($url . '?' . $key . '=' . $value); } else { return ($url . '&' . $key . '=' . $value); } } function smd_removeQSVar($url, $key) { $url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&'); $url = substr($url, 0, -1); return ($url); } // return a list of categories under (and including) the given parent category function smd_getSubCats($parent,$cattype) { $parent = doSlash($parent); $lextrem = 1; $rextrem = 1; extract(safe_row("lft as lextrem, rgt as rextrem", "txp_category", "name='$parent' and type = '$cattype'")); $rs = safe_rows_start("id,name,title","txp_category", "lft between $lextrem and $rextrem and type = '$cattype' order by lft asc"); $cats = array(); while ($row = nextRow($rs)) { $cats[] = array( 'id' => $row['id'], 'name' => $row['name'], 'title' => $row['title'] ); } return $cats; } // Searches the passed $str for predetermined sequences of characters // and, if that sequence is in $allowed, replaces it as follows: // ?c = current global category (!c = not current category) // ?s = current section (!s = not current section) // ?t = current article title (!t = not current title) // ?id = current article ID, prepended with $idprefix (!id = not current ID) // ?field = contents of the current article's field (could be a comma-separated list) // !field = not the contents of the current article's field (could be a comma-separated list) // Ranges (e.g. 1-5 or a-z) will be expanded into their individual values (PHP>4.1.0 for chars). // Anything else is returned verbatim. // Outputs two arrays; the 1st containing items for inclusion, the 2nd for exclusion. function smd_getAtts($str, $allowed, $idprefix="", $splitat="/(,|,\s)+/", $pregopt = PREG_SPLIT_NO_EMPTY) { global $pretext, $thisarticle; $out = array(); $subout = array(); $notout = array(); $matches = smd_splitRange($str, $splitat, $pregopt); for ($idx = 0; $idx < count($matches); $idx++) { $thismatch = $matches[$idx]; if (($thismatch === "?c") && in_array("?c",$allowed)) { // Use global article category, if it exists if ($pretext['c'] != "") { if (!in_array($pretext['c'], $out)) { $out[] = $pretext['c']; } } } else if (($thismatch === "!c") && in_array("!c",$allowed)) { // Don't use global article category if ($pretext['c'] != "") { if (!in_array($pretext['c'], $notout)) { $notout[] = $pretext['c']; } } } else if (($thismatch === "?s") && in_array("?s",$allowed)) { // Use article section if ($pretext['s'] != "") { if (!in_array($pretext['s'], $out)) { $out[] = $pretext['s']; } } } else if (($thismatch === "!s") && in_array("!s",$allowed)) { // Don't use article section if ($pretext['s'] != "") { if (!in_array($pretext['s'], $notout)) { $notout[] = $pretext['s']; } } } else if (($thismatch === "?t") && in_array("?t",$allowed)) { // Use article URL title if this is an article. if ($thisarticle != NULL && $thisarticle['url_title'] != "") { if (!in_array($thisarticle['url_title'], $out)) { $out[] = $thisarticle['url_title']; } } } else if (($thismatch === "!t") && in_array("!t",$allowed)) { // Don't use article URL title if ($thisarticle != NULL && $thisarticle['url_title'] != "") { if (!in_array($thisarticle['url_title'], $notout)) { $notout[] = $thisarticle['url_title']; } } } else if (($thismatch === "?id") && in_array("?id",$allowed)) { // Use article ID, prepended with $idprefix if ($thisarticle != NULL) { if (!in_array($idprefix . $pretext['id'], $out)) { $out[] = $idprefix . $pretext['id']; } } } else if (($thismatch === "!id") && in_array("!id",$allowed)) { // Don't use article ID if ($thisarticle != NULL) { if (!in_array($idprefix . $pretext['id'], $notout)) { $notout[] = $idprefix . $pretext['id']; } } } else if ((substr($thismatch,0,1) === "?") && in_array("?field",$allowed)) { // Use the given field name; which may be a comma-separated sublist. // Split off the field name from the question mark $fieldname = substr($thismatch,1); if (($thisarticle != NULL) && (isset($thisarticle[strtolower($fieldname)]))) { $fieldContents = $thisarticle[strtolower($fieldname)]; } else { $fieldContents = $fieldname; } if (!empty($fieldContents)) { $subout = smd_splitRange(strip_tags($fieldContents), $splitat); foreach ($subout as $subname) { if (!in_array($subname, $out)) { $out[] = $subname; } } } } else if ((substr($thismatch,0,1) === "!") && in_array("!field",$allowed)) { // Negation. May either be a field name (and maybe another CSL) or a fixed term. // Split off the name from the exclamation mark $fieldname = substr($thismatch,1); if (($thisarticle != NULL) && (isset($thisarticle[strtolower($fieldname)]))) { $fieldContents = $thisarticle[strtolower($fieldname)]; } else { $fieldContents = $fieldname; } if (!empty($fieldContents)) { $subout = smd_splitRange(strip_tags($fieldContents), $splitat); foreach ($subout as $subname) { if (!in_array($subname, $notout)) { $notout[] = $subname; } } } } else { if (!in_array($thismatch, $out)) { $out[] = $thismatch; } } } return array($out,$notout); } // Return an array of items from a string of (usually) comma-separated values. // Some values may contain ranges of numbers like 1-5 that need 'expanding' first function smd_splitRange($str, $splitat="/(,|,\s)+/", $pregOpt = PREG_SPLIT_NO_EMPTY) { $retarr = array(); if ((substr($splitat,0,1) == "/") && (substr($splitat, strlen($splitat)-1, 1) == "/")) { $pat = $splitat; } else { $pat = '/['.$splitat.']+/'; } $elems = preg_split($pat, $str, -1, $pregOpt); foreach ($elems as $item) { $item = trim($item); $negate = false; // Does the item start with a negation character if (substr($item,0,1) === "!") { $negate = true; $item = substr($item,1); } // Is the item an integer list range if (preg_match('/^(\d+)\-(\d+)$/', $item)) { list($lo, $hi) = explode("-", $item, 2); $rng = range($lo, $hi); // Reapply the negation if necessary for($idx = 0; $idx < count($rng); $idx++) { $rng[$idx] = (($negate) ? "!" : "") . $rng[$idx]; } $retarr = array_merge($retarr, $rng); } else { $retarr[] = (($negate) ? "!" : "") . $item; } } return $retarr; } // Generic lookup function for interfacing with MLP to localise strings. // $stray = array of default strings // $what = key to look up // $args = any arguments the key is expecting for replacement function smd_gTxt($stray, $what, $args = array()) { global $textarray, $smdMLP; $fn = smd_getCaller(); // Drop out if the id isn't defined. // Would be nice to use reflection to find the name of the function and pass that, // but that's PHP5 territory only if (empty($fn)) { $str = $what; } else { // Prepare the prefixed key for use $key = $smdMLP[$fn] . '-' . $what; $key = strtolower($key); // Grab from the global textarray (possibly edited by MLP) if we can if(isset($textarray[$key])) { $str = $textarray[$key]; } else { // The string isn't in the localised $textarray so fallback to using // the (non prefixed) string array in the plugin $key = strtolower($what); $str = (isset($stray[$key])) ? $stray[$key] : $what; } } // Perform substitutions if(!empty($args)) { $str = strtr($str, $args); } return $str; } // Cheating way of finding a function's caller function smd_getCaller() { $backtrace = debug_backtrace(); return $backtrace[2]['function']; }