<?php
/**
* VCF5 Project - Open Federated Microformat Project
*
* You may not change or alter any portion of this comment or credits
* of supporting developers from this source code or any supporting source code
* which is considered copyrighted (c) material of the original comment or credit authors.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Chronolabs Cooperative http://syd.au.snails.email
* @license ACADEMIC APL 2 (https://sourceforge.net/u/chronolabscoop/wiki/Academic%20Public%20License%2C%20version%202.0/)
* @license GNU GPL 3 (http://www.gnu.org/licenses/gpl.html)
* @package vcf5.core
* @since 1.0.1
* @author Dr. Simon Antony Roberts <wishcraft@users.sourceforge.net>
* @version 1.0.0
* @description This is the core for the VCF5 the Variant Caller File is appified in version 5 for federation
* @link http://blog.vcf.email
* @link https://github.com/VCF5
* @link https://sourceforge.net/projects/vcf5
* @link https://facebook.com/vcf5project
* @link https://twitter.com/vcf5project
*
*/
include __DIR__ . '/mainfile.php';
vcf5_loadLanguage('search');
/* @var $config_handler Vcf5ConfigHandler */
$config_handler = vcf5_getHandler('config');
$vcf5ConfigSearch = $config_handler->getConfigsByCat(VCF5_CONF_SEARCH);
if ($vcf5ConfigSearch['enable_search'] != 1) {
header('Location: ' . VCF5_URL . '/index.php');
exit();
}
$action = 'search';
if (!empty($_GET['action'])) {
$action = trim(strip_tags($_GET['action']));
} elseif (!empty($_POST['action'])) {
$action = trim(strip_tags($_POST['action']));
}
$query = '';
if (!empty($_GET['query'])) {
$query = trim(strip_tags($_GET['query']));
} elseif (!empty($_POST['query'])) {
$query = trim(strip_tags($_POST['query']));
}
$andor = 'AND';
if (!empty($_GET['andor'])) {
$andor = trim(strip_tags($_GET['andor']));
} elseif (!empty($_POST['andor'])) {
$andor = trim(strip_tags($_POST['andor']));
}
$mid = $uid = $start = 0;
if (!empty($_GET['mid'])) {
$mid = (int)$_GET['mid'];
} elseif (!empty($_POST['mid'])) {
$mid = (int)$_POST['mid'];
}
if (!empty($_GET['uid'])) {
$uid = (int)$_GET['uid'];
} elseif (!empty($_POST['uid'])) {
$uid = (int)$_POST['uid'];
}
if (!empty($_GET['start'])) {
$start = (int)$_GET['start'];
} elseif (!empty($_POST['start'])) {
$start = (int)$_POST['start'];
}
$queries = array();
if ($action === 'results') {
if ($query == '') {
redirect_header('search.php', 1, _SR_PLZENTER);
}
} elseif ($action === 'showall') {
if ($query == '' || empty($mid)) {
redirect_header('search.php', 1, _SR_PLZENTER);
}
} elseif ($action === 'showallbyuser') {
if (empty($mid) || empty($uid)) {
redirect_header('search.php', 1, _SR_PLZENTER);
}
}
$GLOBALS['vcf5Option']['template_main'] = 'system_search.html';
$groups = is_object($vcf5User) ? $vcf5User->getGroups() : VCF5_GROUP_ANONYMOUS;
/* @var $gperm_handler Vcf5GroupPermHandler */
$gperm_handler = vcf5_getHandler('groupperm');
$available_apps = $gperm_handler->getItemIds('app_read', $groups);
if ($action === 'search') {
include $GLOBALS['vcf5']->path('header.php');
include $GLOBALS['vcf5']->path('include/searchform.php');
$vcf5Tpl->assign('form', $search_form->render());
include $GLOBALS['vcf5']->path('footer.php');
exit();
}
if ($andor !== 'OR' && $andor !== 'exact' && $andor !== 'AND') {
$andor = 'AND';
}
$myts = MyTextSanitizer::getInstance();
if ($action !== 'showallbyuser') {
if ($andor !== 'exact') {
$ignored_queries = array(); // holds kewords that are shorter than allowed minmum length
$temp_queries = preg_split('/[\s,]+/', $query);
foreach ($temp_queries as $q) {
$q = trim($q);
if (strlen($q) >= $vcf5ConfigSearch['keyword_min']) {
$queries[] = $myts->addSlashes($q);
} else {
$ignored_queries[] = $myts->addSlashes($q);
}
}
if (count($queries) == 0) {
redirect_header('search.php', 2, sprintf(_SR_KEYTOOSHORT, $vcf5ConfigSearch['keyword_min']));
}
} else {
$query = trim($query);
if (strlen($query) < $vcf5ConfigSearch['keyword_min']) {
redirect_header('search.php', 2, sprintf(_SR_KEYTOOSHORT, $vcf5ConfigSearch['keyword_min']));
}
$queries = array($myts->addSlashes($query));
}
}
switch ($action) {
case 'results':
/* @var $app_handler Vcf5AppHandler */
$app_handler = vcf5_getHandler('app');
$criteria = new CriteriaCompo(new Criteria('hassearch', 1));
$criteria->add(new Criteria('isactive', 1));
$criteria->add(new Criteria('mid', '(' . implode(',', $available_apps) . ')', 'IN'));
$apps = $app_handler->getObjects($criteria, true);
$mids = isset($_REQUEST['mids']) ? $_REQUEST['mids'] : array();
if (empty($mids) || !is_array($mids)) {
unset($mids);
$mids = array_keys($apps);
}
$vcf5Option['vcf5_pagetitle'] = _SR_SEARCHRESULTS . ': ' . implode(' ', $queries);
include $GLOBALS['vcf5']->path('header.php');
$vcf5Tpl->assign('results', true);
$nomatch = true;
$keywords = '';
$error_length = '';
$error_keywords = '';
if ($andor !== 'exact') {
foreach ($queries as $q) {
$keywords .= htmlspecialchars(stripslashes($q)) . ' ';
}
if (!empty($ignored_queries)) {
$error_length = sprintf(_SR_IGNOREDWORDS, $vcf5ConfigSearch['keyword_min']);
foreach ($ignored_queries as $q) {
$error_keywords .= htmlspecialchars(stripslashes($q)) . ' ';
}
}
} else {
$keywords .= '"' . htmlspecialchars(stripslashes($queries[0])) . '"';
}
$vcf5Tpl->assign('keywords', $keywords);
$vcf5Tpl->assign('error_length', $error_length);
$vcf5Tpl->assign('error_keywords', $error_keywords);
$results_arr = array();
foreach ($mids as $mid) {
$mid = (int)$mid;
if (in_array($mid, $available_apps)) {
$app = $apps[$mid];
$results = $app->search($queries, $andor, 5, 0);
$count = count($results);
if (is_array($results) && $count > 0) {
$nomatch = false;
$app_name = $app->getVar('name');
for ($i = 0; $i < $count; ++$i) {
if (isset($results[$i]['image']) && $results[$i]['image'] != '') {
$results_arr[$i]['image_link'] = 'apps/' . $app->getVar('dirname') . '/' . $results[$i]['image'];
} else {
$results_arr[$i]['image_link'] = 'images/icons/posticon2.gif';
}
$results_arr[$i]['image_title'] = $app->getVar('name');
if (!preg_match("/^http[s]*:\/\//i", $results[$i]['link'])) {
$results[$i]['link'] = 'apps/' . $app->getVar('dirname') . '/' . $results[$i]['link'];
}
$results_arr[$i]['link'] = $results[$i]['link'];
$results_arr[$i]['link_title'] = $myts->htmlspecialchars($results[$i]['title']);
$results[$i]['uid'] = @(int)$results[$i]['uid'];
if (!empty($results[$i]['uid'])) {
$uname = Vcf5User::getUnameFromId($results[$i]['uid']);
$results_arr[$i]['uname'] = $uname;
$results_arr[$i]['uname_link'] = VCF5_URL . '/userinfo.php?uid=' . $results[$i]['uid'];
}
if (!empty($results[$i]['time'])){
$results_arr[$i]['time'] = formatTimestamp((int)$results[$i]['time']);
}
}
if ($count >= 5) {
$search_url = VCF5_URL . '/search.php?query=' . urlencode(stripslashes(implode(' ', $queries)));
$search_url .= "&mid={$mid}&action=showall&andor={$andor}";
$search_arr['app_show_all'] = htmlspecialchars($search_url);
}
$search_arr['app_name'] = $app_name;
$search_arr['app_data'] = $results_arr;
$vcf5Tpl->append_by_ref('search', $search_arr);
unset($results_arr, $search_arr);
}
}
unset($results, $app, $app_name);
}
if ($nomatch) {
$vcf5Tpl->assign('nomatch', _SR_NOMATCH);
}
include $GLOBALS['vcf5']->path('include/searchform.php');
$vcf5Tpl->assign('form', $search_form->render());
break;
case 'showall':
case 'showallbyuser':
include $GLOBALS['vcf5']->path('header.php');
$vcf5Tpl->assign('showallbyuser', true);
/* @var $app_handler Vcf5AppHandler */
$app_handler = vcf5_getHandler('app');
$app = $app_handler->get($mid);
$results = $app->search($queries, $andor, 20, $start, $uid);
$count = count($results);
if (is_array($results) && $count > 0) {
$next_results = $app->search($queries, $andor, 1, $start + 20, $uid);
$next_count = count($next_results);
$has_next = false;
if (is_array($next_results) && $next_count == 1) {
$has_next = true;
}
if ($action === 'showall') {
$vcf5Tpl->assign('showall', true);
$keywords = '';
if ($andor !== 'exact') {
foreach ($queries as $q) {
$keywords .= htmlspecialchars(stripslashes($q));
}
} else {
$keywords .= htmlspecialchars(stripslashes($queries[0]));
}
$vcf5Tpl->assign('keywords', $keywords);
}
$vcf5Tpl->assign('showing', sprintf(_SR_SHOWING, $start + 1, $start + $count));
$vcf5Tpl->assign('app_name', $app->getVar('name'));
$results_arr = array();
for ($i = 0; $i < $count; ++$i) {
if (isset($results[$i]['image']) && $results[$i]['image'] != '') {
$results_arr['image_link'] = 'apps/' . $app->getVar('dirname') . '/' . $results[$i]['image'];
} else {
$results_arr['image_link'] = 'images/icons/posticon2.gif';
}
$results_arr['image_title'] = $app->getVar('name');
if (!preg_match("/^http[s]*:\/\//i", $results[$i]['link'])) {
$results[$i]['link'] = 'apps/' . $app->getVar('dirname') . '/' . $results[$i]['link'];
}
$results_arr['link'] = $results[$i]['link'];
$results_arr['link_title'] = $myts->htmlspecialchars($results[$i]['title']);
$results['uid'] = @(int)$results[$i]['uid'];
if (!empty($results[$i]['uid'])) {
$uname = Vcf5User::getUnameFromId($results[$i]['uid']);
$results_arr['uname'] = $uname;
$results_arr['uname_link'] = VCF5_URL . '/userinfo.php?uid=' . $results[$i]['uid'];
}
if (!empty($results[$i]['time'])){
$results_arr['time'] = formatTimestamp((int)$results[$i]['time']);
}
$vcf5Tpl->append_by_ref('results_arr', $results_arr);
unset($results_arr);
}
$search_url = VCF5_URL . '/search.php?query=' . urlencode(stripslashes(implode(' ', $queries)));
$search_url .= "&mid={$mid}&action={$action}&andor={$andor}";
if ($action === 'showallbyuser') {
$search_url .= "&uid={$uid}";
}
if ($start > 0) {
$prev = $start - 20;
$search_url_prev = $search_url . "&start={$prev}";
$vcf5Tpl->assign('previous', htmlspecialchars($search_url_prev));
}
if (false !== $has_next) {
$next = $start + 20;
$search_url_next = $search_url . "&start={$next}";
$vcf5Tpl->assign('next', htmlspecialchars($search_url_next));
}
} else {
$vcf5Tpl->assign('nomatch', true);
}
include $GLOBALS['vcf5']->path('include/searchform.php');
$search_form->display();
break;
}
include $GLOBALS['vcf5']->path('footer.php');