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) // Anything else is returned verbatim. // Outputs two arrays; the 1st containing items for inclusion, the 2nd for exclusion function smd_getAtts($str, $allowed, $idprefix="") { global $pretext, $thisarticle; $out = array(); $subout = array(); $notout = array(); $matches = preg_split('/[,\s]+/',$str,-1,PREG_SPLIT_NO_EMPTY); 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. // It's an article if the next_id attribute exists if (array_key_exists('next_id',$pretext)) { if ($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 (array_key_exists('next_id',$pretext)) { if ($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 (array_key_exists('next_id',$pretext)) { 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 (array_key_exists('next_id',$pretext)) { if (!in_array($idprefix . $pretext['id'], $notout)) { $notout[] = $idprefix . $pretext['id']; } } } else if (($thismatch[0] === "?") && 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 (array_key_exists('next_id',$pretext)) { // Check this is an article if (isset($thisarticle[$fieldname])) { $fieldContents = $thisarticle[$fieldname]; } else { $fieldContents = $fieldname; } } else { $fieldContents = $fieldname; } if (!empty($fieldContents)) { $subout = preg_split('/[,\s]+/', strip_tags($fieldContents), -1, PREG_SPLIT_NO_EMPTY); foreach ($subout as $subname) { if (!in_array($subname, $out)) { $out[] = $subname; } } } } else if (($thismatch[0] === "!") && 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 (array_key_exists('next_id',$pretext)) { // Check this is an article if (isset($thisarticle[$fieldname])) { $fieldContents = $thisarticle[$fieldname]; } else { $fieldContents = $fieldname; } } else { $fieldContents = $fieldname; } if (!empty($fieldContents)) { $subout = preg_split('/[,\s]+/', strip_tags($fieldContents), -1, PREG_SPLIT_NO_EMPTY); foreach ($subout as $subname) { if (!in_array($subname, $notout)) { $notout[] = $subname; } } } } else { if (!in_array($thismatch, $out)) { $out[] = $thismatch; } } } return array($out,$notout); }