/** * Get the current index of the pages. * * @return array */ function getIndex($filter_user = "", $excerpts = false, $links = false) { global $PIVOTX; // If we've already built the index, and we don't need to filter // on user or to create excerpts or links. if (is_array($this->index) && empty($filter_user) && $excerpts == false && $links == false) { return $this->index; } // Load the index, if it exists. if (file_exists($PIVOTX['paths']['db_path'] . "pages/pages.php")) { $this->index = loadSerialize($PIVOTX['paths']['db_path'] . "pages/pages.php"); } else { $this->index = getDefaultPages(); $this->saveIndex(false); } $currentuser = $PIVOTX['users']->getUser($PIVOTX['session']->currentUsername()); $currentuserlevel = !$currentuser ? 1 : $currentuser['userlevel']; // Spider the folder, read all pages.. $dir = dir($PIVOTX['paths']['db_path'] . "pages/"); while (false !== ($entry = $dir->read())) { if (preg_match('/^page_[0-9]+.php$/', $entry)) { $fullpage = loadSerialize($PIVOTX['paths']['db_path'] . "pages/" . $entry); // Skip corrupted files. if (!$fullpage) { debug("Couldn't load page from file {$entry}"); continue; } $page = array('uid' => $fullpage['code'], 'title' => $fullpage['title'], 'subtitle' => $fullpage['subtitle'], 'user' => $fullpage['user'], 'date' => $fullpage['date'], 'publish_date' => $fullpage['publish_date'], 'chapter' => $fullpage['chapter'], 'uri' => $fullpage['uri'], 'status' => $fullpage['status'], 'template' => $fullpage['template'], 'sortorder' => $fullpage['sortorder'], 'editable' => $PIVOTX['users']->allowEdit('page', $fullpage['user'])); // Make the 'excerpts', if we have to.. if ($excerpts) { $page['excerpt'] = makeExcerpt($fullpage['subtitle'] . $fullpage['introduction']); } // Make the links, if required.. if ($links) { $page['link'] = makePageLink($fullpage['uri']); } // Skip this page, if we're filtering for a user, and the user doesn't match. if (!empty($filter_user) && $filter_user != $pageuser['username']) { continue; } if (isset($this->index[$page['chapter']])) { $this->index[$page['chapter']]['pages'][] = $page; } else { $this->index['orphaned']['pages'][] = $page; $this->index['orphaned']['editable'] = $PIVOTX['users']->allowEdit('chapter'); } } } $dir->close(); foreach ($this->index as $key => $chapter) { // Sort the pages if there are any. if (isset($chapter['pages'])) { usort($chapter['pages'], array($this, 'sort')); } $chapter['editable'] = $PIVOTX['users']->allowEdit('chapter'); $this->index[$key] = $chapter; } return $this->index; }
/** * */ function smarty_searchresults($params, $format, &$smarty) { global $PIVOTX; $vars = $smarty->get_template_vars(); $results = $vars['searchresults']; $output = ""; $titlelength = getDefault($params['titletrimlength'], 60); $excerptlength = getDefault($params['excerptlength'], 100); if (isset($format)) { $smarty->assign('content', "<!-- Searchresults were already output -->"); if (count($results) > 0) { $output .= $params['prefix']; } foreach ($results as $result) { $temp_output = $format; $result['title'] = makeExcerpt($result['title'], $titlelength, true); $result['excerpt'] = makeExcerpt($result['introduction'] . $result['body'], $excerptlength, true); $temp_output = formatEntry($result, $temp_output); $output .= $temp_output; } if (count($results) > 0) { $output .= $params['postfix']; } return $output; } }
/** * Get Entries/Pages with a certain Tag * * @param string $tag * @param integer $skip * @return unknown */ function getEntriesWithTag($tag, $skip = 0) { global $PIVOTX; // How the entries are formated in the list $format_entry = "<li><a href='%link%'>%title%</a><br /><span>%excerpt%</span></li>\n"; $filename = urlencode($tag) . '.tag'; $tag = str_replace(" ", "+", $tag); if ($PIVOTX['config']->get('db_model') == "flat") { // Getting tags for flat files.. if (file_exists($PIVOTX['paths']['db_path'] . "tagdata/{$filename}")) { $sEntriesString = file_get_contents($PIVOTX['paths']['db_path'] . "tagdata/{$filename}"); } else { return ""; } $aEntries = explode(",", $sEntriesString); rsort($aEntries); $aLinks = array(); foreach ($aEntries as $nThisEntry) { $PIVOTX['db']->read_entry($nThisEntry); // Skip entries that aren't published - in case the tag index isn't up to date. if ($PIVOTX['db']->entry['status'] != 'publish') { continue; } $excerpt = makeExcerpt(parse_intro_or_body($PIVOTX['db']->entry['introduction'] . " " . $PIVOTX['db']->entry['body'], false, $PIVOTX['db']->entry['convert_lb']), 170); if ($PIVOTX['db']->entry["code"] != $skip) { $aLink = $format_entry; $aLink = str_replace("%link%", makeFileLink($PIVOTX['db']->entry["code"], '', ''), $aLink); $aLink = str_replace("%title%", $PIVOTX['db']->entry["title"], $aLink); $aLink = str_replace("%excerpt%", $excerpt, $aLink); $aLinks[] = $aLink; } } } else { // Getting tags for MySQL $tagtable = safeString($PIVOTX['config']->get('db_prefix') . "tags", true); $entriestable = safeString($PIVOTX['config']->get('db_prefix') . "entries", true); $pagestable = safeString($PIVOTX['config']->get('db_prefix') . "pages", true); // [JAN] // Set up DB factory $sqlFactory = new sqlFactory($PIVOTX['config']->get('db_model'), $PIVOTX['config']->get('db_databasename'), $PIVOTX['config']->get('db_hostname'), $PIVOTX['config']->get('db_username'), $PIVOTX['config']->get('db_password')); // Get a DB connection.. $sql = $sqlFactory->getSqlInstance(); // $sql = new sql('mysql', //$PIVOTX['config']->get('db_databasename'), // $PIVOTX['config']->get('db_hostname'), // $PIVOTX['config']->get('db_username'), // $PIVOTX['config']->get('db_password') // ); // [/JAN] $qry = array(); $qry['select'] = "t.*"; $qry['from'] = $tagtable . " AS t"; $qry['order'] = "target_uid DESC"; $qry['where'][] = "tag LIKE " . $sql->quote($tag); $qry['leftjoin'][$entriestable . " AS e"] = "t.target_uid = e.uid"; $qry['leftjoin'][$pagestable . " AS p"] = "t.target_uid = p.uid"; $qry['group'][] = "t.target_uid"; $qry['group'][] = "t.contenttype"; $sql->build_select($qry); $sql->query(); //echo nl2br(htmlentities($sql->get_last_query())); $rows = $sql->fetch_all_rows(); foreach ($rows as $row) { if ($row['contenttype'] == "entry") { $PIVOTX['db']->read_entry($row['target_uid']); // Skip entries that aren't published. if ($PIVOTX['db']->entry['status'] != "publish") { continue; } $excerpt = makeExcerpt(parse_intro_or_body($PIVOTX['db']->entry['introduction'] . " " . $PIVOTX['db']->entry['body'], false, $PIVOTX['db']->entry['convert_lb']), 170); if ($PIVOTX['db']->entry["code"] != $skip) { $aLink = $format_entry; $aLink = str_replace("%link%", makeFileLink($PIVOTX['db']->entry["code"], '', ''), $aLink); $aLink = str_replace("%title%", $PIVOTX['db']->entry["title"], $aLink); $aLink = str_replace("%excerpt%", $excerpt, $aLink); $aLinks[] = $aLink; } } else { if ($row['contenttype'] == "page") { $page = $PIVOTX['pages']->getPage($row['target_uid']); // Skip pages that aren't published. if ($page['status'] != "publish") { continue; } $title = $page['title']; $excerpt = makeExcerpt(parse_intro_or_body($page['introduction'] . " " . $page['body'], false, $page['convert_lb']), 170); $aLinks[] = "<li><a href=\"" . makePagelink($page['uri']) . "\">" . $title . "</a><br />\n{$excerpt}</li>\n"; } } } } if (count($aLinks) > 0) { $sLinkList = "<ul class='taglist'>\n"; $sLinkList .= implode("\n", $aLinks); $sLinkList .= "</ul>\n"; return $sLinkList; } else { return ""; } }
function update_index($update = TRUE) { global $PIVOTX; $this->check_current_index(); if (strlen($this->entry['title']) > 1) { $title = $this->entry['title']; $title = strip_tags($title); } else { $title = substr($this->entry['introduction'], 0, 300); $title = strip_tags($title); $title = str_replace("\n", "", $title); $title = str_replace("\r", "", $title); $title = substr($title, 0, 60); } // Make sure we have an URI. Old (converted from 1.x) entries don't have them, so we make them. if (empty($this->entry['uri'])) { $this->entry['uri'] = makeURI($this->entry['title']); } $size = strlen($this->entry['introduction']) + strlen($this->entry['body']); unset($commnames); if (isset($this->entry['comments'])) { // Initialise the IP blocklist. $blocklist = new IPBlock(); foreach ($this->entry['comments'] as $comment) { if (!$blocklist->isBlocked($comment['ip'])) { if ($comment['moderate'] != 1) { $commnames[] = stripslashes($comment['name']); } else { // if moderation is on, we add the name as '-'.. $commnames[] = '-'; } } } if (isset($commnames) && count($commnames) > 0) { $this->entry['commnames'] = implode(", ", array_unique($commnames)); $commcount = count($commnames); } else { $this->entry['commnames'] = ""; $commcount = 0; } } else { unset($this->entry['comments']); $commcount = 0; $this->entry['commnames'] = ""; } $this->entry['commcount'] = $commcount; if ($commcount == 0) { $commcount_str = __('No comments'); } else { if ($commcount == 1) { $commcount_str = __('%num% comment'); } else { $commcount_str = __('%num% comments'); } } $this->entry['commcount_str'] = str_replace("%num%", $PIVOTX['locale']->getNumber($commcount), $commcount_str); $this->entry['commcount_str'] = str_replace("%n%", $commcount, $commcount_str); unset($tracknames); if (isset($this->entry['trackbacks'])) { foreach ($this->entry['trackbacks'] as $trackback) { $tracknames[] = stripslashes($trackback['name']); } if (isset($tracknames) && count($tracknames) > 0) { $this->entry['tracknames'] = implode(", ", array_unique($tracknames)); $trackcount = count($tracknames); } else { $this->entry['tracknames'] = ""; $trackcount = 0; } } else { unset($this->entry['trackbacks']); $trackcount = 0; $this->entry['tracknames'] = ""; } $this->entry['trackcount'] = $trackcount; if ($trackcount == 0) { $trackcount_str = __('No trackbacks'); } else { if ($trackcount == 1) { $trackcount_str = __('%num% trackback'); } else { $trackcount_str = __('%num% trackbacks'); } } $this->entry['trackcount_str'] = str_replace("%num%", $PIVOTX['locale']->getNumber($trackcount), $trackcount_str); $this->entry['trackcount_str'] = str_replace("%n%", $trackcount, $trackcount_str); if (!isset($this->entry['status'])) { $this->entry['status'] = 'publish'; } $this->entry['excerpt'] = makeExcerpt($this->entry['introduction']); // Remove non-existing categories from entry before indexing if (count($this->all_cats) > 0) { $category = array_values(array_intersect($this->all_cats, $this->entry['category'])); } else { $category = $this->entry['category']; } if (is_array($this->entry['extrafields'])) { $extrafields = array_keys($this->entry['extrafields']); } else { $extrafields = array(); } $index_line = array('code' => $this->entry['code'], 'date' => addslashes($this->entry['date']), 'user' => $this->entry['user'], 'title' => addslashes($title), 'uri' => $this->entry['uri'], 'size' => $size, 'commcount' => $this->entry['commcount'], 'cnames' => $this->entry['commnames'], 'trackcount' => $this->entry['trackcount'], 'tnames' => $this->entry['tracknames'], 'category' => $category, 'extrafields' => $extrafields, 'status' => $this->entry['status'], 'excerpt' => $this->entry['excerpt']); if ($this->entry['code'] != "") { $this->entry_index[$this->entry['code']] = $index_line; $this->date_index[$this->entry['code']] = $this->entry['date']; $this->cat_index[$this->entry['code']] = $category; $this->uri_index[$this->entry['code']] = $this->entry['uri']; } if ($update) { $this->updated = TRUE; } }
/** * Gets a list of the $amount latest pages * * @param integer $amount */ function getLatestPages($amount, $filter_user = "") { $qry = array(); $qry['select'] = "p.uid, p.title, p.uri, p.subtitle, p.template, p.status, p.date, p.publish_date, p.edit_date, p.user, SUBSTRING(p.introduction, 1, 200) as introduction, c.chaptername"; $qry['from'] = $this->pagestable . " AS p"; $qry['limit'] = intval($amount); $qry['order'] = "date DESC"; $qry['leftjoin'][$this->chapterstable . " AS c"] = "p.chapter=c.uid"; if (!empty($filter_user)) { $qry['where'][] = "user=" . $this->sql->quote($filter_user); } $query = $this->sql->build_select($qry); $this->sql->query(); $pages = $this->sql->fetch_all_rows(); // Make the 'excerpts'.. foreach ($pages as $key => $page) { $pages[$key]['excerpt'] = makeExcerpt($page['subtitle'] . $page['introduction']); } return $pages; }
/** * Read a bunch of entries * * @param array $params * @return array */ function read_entries($params) { global $PIVOTX; $qry = array(); $qry['select'] = "e.*, e.uid AS code, e.comment_count AS commcount, e.comment_names AS commnames, e.trackback_count AS trackcount, e.trackback_names AS tracknames"; $qry['from'] = $this->entriestable . " AS e"; if (!empty($params['offset'])) { $params['date'] = ""; $qry['limit'] = intval($params['offset']) . ", " . $params['show']; } else { $qry['limit'] = $params['show']; } if (substr($params['orderby'], 0, 12) == "extrafields_") { if (empty($params['extrafields'])) { $qry['select'] .= ", ef.target_uid, ef.value"; $qry['leftjoin'][$this->extrafieldstable . " AS ef"] = "e.uid = ef.target_uid"; } $qry['where'][] = "ef.contenttype = 'entry'"; $qry['where'][] = "ef.fieldkey = '" . safeString(substr($params['orderby'], 12)) . "'"; if ($params['ordertype'] == "int") { $orderby = "CAST(ef.value as SIGNED)"; } else { $orderby = "ef.value"; } } elseif (!empty($params['orderby'])) { if ($params['ordertype'] == "int") { $orderby = "CAST(e." . safeString($params['orderby'], true) . " as SIGNED)"; } else { $orderby = "e." . safeString($params['orderby'], true); } } else { $orderby = "e.date"; } if ($params['order'] == "random") { $qry['order'] = "RAND()"; } elseif ($params['order'] == "desc") { $qry['order'] = $orderby . " DESC"; } else { $qry['order'] = $orderby . " ASC"; } if (!empty($params['uid'])) { if (is_array($params['uid'])) { $aUids = $params['uid']; } else { $aUids = explode(",", $params['uid']); } foreach ($aUids as $k => $uid) { if (!is_numeric($uid)) { unset($aUids[$k]); } } if (!empty($aUids)) { $uids = implode(', ', $aUids); $qry['where'][] = "e.uid in (" . $uids . ")"; } } else { if (!empty($params['start'])) { $params['date'] = ""; $params['start'] = explode("-", $params['start']); $start = sprintf("%s-%02s-%02s %02s:%02s:00", $params['start'][0], $params['start'][1], $params['start'][2], $params['start'][3], $params['start'][4]); $qry['where'][] = $orderby . " > " . $this->sql->quote($start); } if (!empty($params['end'])) { $params['date'] = ""; $params['end'] = explode("-", $params['end']); $end = sprintf("%s-%02s-%02s %02s:%02s:00", $params['end'][0], $params['end'][1], $params['end'][2], $params['end'][3], $params['end'][4]); $qry['where'][] = $orderby . " < " . $this->sql->quote($end); } if (!empty($params['date'])) { $params['date'] = explode("-", $params['date']); $year = (int) $params['date'][0]; if (count($params['date']) == 1) { $start = sprintf("%s-%02s-%02s 00:00:00", $year, 1, 1); $year++; $end = sprintf("%s-%02s-%02s 00:00:00", $year, 1, 1); } elseif (count($params['date']) == 2) { $month = (int) $params['date'][1]; $start = sprintf("%s-%02s-%02s 00:00:00", $year, $month, 1); $month++; if ($month > 12) { $month = 1; $year++; } $end = sprintf("%s-%02s-%02s 00:00:00", $year, $month, 1); } else { $month = (int) $params['date'][1]; $day = (int) $params['date'][2]; $start = sprintf("%s-%02s-%02s 00:00:00", $year, $month, $day); $end = sprintf("%s-%02s-%02s 23:59:00", $year, $month, $day); } $qry['where'][] = "{$orderby} > " . $this->sql->quote($start); $qry['where'][] = "{$orderby} < " . $this->sql->quote($end); } // Do not use a limit if a date range is given if (!empty($params['start']) && !empty($params['end']) || !empty($params['date'])) { unset($qry['limit']); } if (!empty($params['status'])) { $qry['where'][] = "e.status = " . $this->sql->quote($params['status']); } if (!empty($params['user'])) { $qry['where'][] = "e.user = "******"e.date DESC, e.uid DESC"; $qry['group'] = "e.date, e.uid"; //[/JAN] if (!empty($params['cats'])) { $qry['select'] .= ", c.category"; $qry['leftjoin'][$this->categoriestable . " AS c"] = "e.uid = c.target_uid"; if (is_array($params['cats'])) { $qry['where'][] = "c.category IN('" . implode("', '", $params['cats']) . "')"; } else { $qry['where'][] = "c.category= " . $this->sql->quote($params['cats']); } $qry['where'][] = "c.contenttype= 'entry'"; } if (!empty($params['tags'])) { $qry['select'] .= ", t.tag"; $qry['leftjoin'][$this->tagstable . " AS t"] = "e.uid = t.target_uid"; if (strpos($params['tags'], ",") !== false) { $aTags = explode(",", str_replace(" ", "", $params['tags'])); $tags = implode("', '", $aTags); $qry['where'][] = "t.tag IN ('" . $tags . "')"; } else { $qry['where'][] = "t.tag= " . $this->sql->quote($params['tags']); } $qry['where'][] = "t.contenttype= 'entry'"; } if (!empty($params['extrafields'])) { $qry['select'] .= ", ef.target_uid"; $qry['leftjoin'][$this->extrafieldstable . " AS ef"] = "e.uid = ef.target_uid"; foreach ($params['extrafields'] as $k => $v) { $qry['where_or'][] = "(ef.contenttype='entry' AND ef.fieldkey = '" . $k . "' AND ef.value = '" . $v . "')"; } } } if ($params['count_only'] === true) { // if we only want to count - override the select, group and order $qry['select'] = 'count(e.uid) as number'; unset($qry['order']); unset($qry['group']); //debug_printr($qry); $query = $this->sql->build_select($qry); //debug(nl2br($query)); $this->sql->query(); $result = $this->sql->fetch_row(); // return the result and skip the recht if read_entries return $result; } $query = $this->sql->build_select($qry); $this->sql->query(); // echo nl2br(htmlentities($query)); $rows = $this->sql->fetch_all_rows(); $entries = array(); if (!is_array($rows)) { $rows = array(); } foreach ($rows as $entry) { $entries[$entry['uid']] = $entry; // Make the 'excerpts'.. $entries[$entry['uid']]['excerpt'] = makeExcerpt($entry['introduction']); // Set the link.. $entries[$entry['uid']]['link'] = makeFileLink($entry, '', ''); } if (is_array($entries)) { $ids = makeValuepairs($entries, '', 'uid'); $ids = "'" . implode("', '", $ids) . "'"; // Ok, now we need to do a second query to get the correct arrays with all of the categories. $this->sql->query("SELECT * FROM " . $this->categoriestable . " AS c WHERE contenttype = 'entry' AND target_uid IN ({$ids})"); $tempcats = $this->sql->fetch_all_rows(); if ($tempcats) { // group them together by entry. foreach ($tempcats as $cat) { $cats[$cat['target_uid']][] = $cat['category']; } // Add them to our simple cache, for later retrieval.. $PIVOTX['cache']->setMultiple("categories", $cats); // Now, attach the categories to the entries.. foreach ($cats as $uid => $cat) { foreach ($entries as $key => $entry) { if ($entries[$key]['uid'] == $uid) { $entries[$key]['category'] = $cat; continue; } } } } // And a third query to get the correct records with all of the extra fields. $this->sql->query("SELECT * FROM " . $this->extrafieldstable . " AS e WHERE contenttype='entry' AND target_uid IN ({$ids})"); $tempfields = $this->sql->fetch_all_rows(); // Now, attach the tempfields to the entries.. if (!empty($tempfields)) { foreach ($tempfields as $tempfield) { foreach ($entries as $key => $entry) { if ($entries[$key]['uid'] == $tempfield['target_uid']) { if (!is_array($entries[$key]['extrafields'])) { $entries[$key]['extrafields'] = array(); } // Check if it's a serialised value.. if (is_array(unserialize($temp_field['value']))) { $temp_field['value'] = unserialize($temp_field['value']); } $entries[$key]['extrafields'][$tempfield['fieldkey']] = $tempfield['value']; } } } } } // Add them to our simple cache, for later retrieval.. $PIVOTX['cache']->setMultiple("entries", $entries); return $entries; }