/** * Constructor * * @param object Database reference * @return @e void */ public function __construct(&$obj) { $reg = ipsRegistry::instance(); $this->member = $reg->member(); $this->DB = $reg->DB(); $this->tbl = ips_DBRegistry::getPrefix(); }
/** * Search * * @param int Member ID who is searching * @param string Words to search (probably tainted at this point, so be careful!) * @param int Offset start * @param int Number of results to return * @param array Array of folders to search (send nothing to search all) * @return boolean */ public function execute($memberID, $words, $start = 0, $end = 50, $folders = array()) { //----------------------------------------- // INIT //----------------------------------------- $ids = array(); $words = addslashes($words); $start = intval($start); $end = intval($end); $memberID = intval($memberID); $results = array(); $dbpre = ips_DBRegistry::getPrefix(); /* Do it... */ if ($words) { $this->DB->allow_sub_select = 1; /* Type of search */ if ($this->settings['use_fulltext'] and strtolower($this->settings['mysql_tbl_type']) == 'myisam') { $whereOne = "MATCH( t.mt_title ) AGAINST( '{$words}' IN BOOLEAN MODE )"; $whereTwo = "MATCH( p.msg_post ) AGAINST( '{$words}' IN BOOLEAN MODE )"; } else { $whereOne = "t.mt_title LIKE '%{$words}%'"; $whereTwo = "p.msg_post LIKE '%{$words}%'"; } $this->DB->query("SELECT SQL_CALC_FOUND_ROWS mt_id, mt_first_msg_id FROM ( ( SELECT t.mt_id, t.mt_first_msg_id\r\r\n\t\t\t\t\t\t\t\t\tFROM {$dbpre}message_topics t, {$dbpre}message_topic_user_map m\r\r\n\t\t\t\t\t\t\t\t\tWHERE (t.mt_id=m.map_topic_id AND m.map_user_id=" . $memberID . " AND m.map_user_banned=0 AND m.map_user_active=1 AND t.mt_is_deleted=0) AND {$whereOne}\r\r\n\t\t\t\t\t\t\t\t\tORDER BY t.mt_last_post_time DESC )\r\r\n\t\t\t\t\t\t\t\tUNION\r\r\n\t\t\t\t\t\t\t\t( SELECT p.msg_topic_id, p.msg_id\r\r\n\t\t\t\t\t\t\t\t\tFROM {$dbpre}message_posts p, {$dbpre}message_topic_user_map m\r\r\n\t\t\t\t\t\t\t\t\tWHERE (p.msg_topic_id=m.map_topic_id AND m.map_user_id=" . $memberID . " AND m.map_user_banned=0 AND m.map_user_active=1) AND {$whereTwo}\r\r\n\t\t\t\t\t\t\t\t\tORDER BY p.msg_date DESC ) ) as tbl\r\r\n\t\t\t\t\t\t\t\tGROUP BY mt_id\r\r\n\t\t\t\t\t\t\t\tLIMIT {$start}, {$end}"); while ($row = $this->DB->fetch()) { $ids[] = $row['mt_id']; } $this->DB->query("SELECT FOUND_ROWS() as row_your_boat"); $row = $this->DB->fetch(); /* Set rows var */ $this->_rows = intval($row['row_your_boat']); // comic genius $this->DB->allow_sub_select = 0; /* Now fetch some actual data! */ if (count($ids)) { $this->DB->build(array('select' => 't.*', 'from' => array('message_topics' => 't'), 'where' => 'mt_id IN (' . implode(",", $ids) . ')', 'order' => 't.mt_last_post_time DESC', 'add_join' => array(array('select' => 'map.*', 'from' => array('message_topic_user_map' => 'map'), 'where' => 'map.map_topic_id=t.mt_id', 'type' => 'left'), array('select' => 'p.*', 'from' => array('message_posts' => 'p'), 'where' => 'p.msg_id=t.mt_first_msg_id', 'type' => 'left')))); $this->DB->execute(); while ($row = $this->DB->fetch()) { $results[$row['mt_id']] = $row; } $this->_results = $results; } } }
/** * Perform the search * @param array $tags * @param array $options */ public function run(array $tags, array $options) { $where = array(); $order = !empty($options['sortKey']) ? $options['sortKey'] : 'tg.tag_meta_id'; $dir = !empty($options['sortOrder']) ? $options['sortOrder'] : 'desc'; $return = array(); /* Format query */ if (isset($options['meta_parent_id']) && (is_numeric($options['meta_parent_id']) || count($options['meta_parent_id']))) { $where[] = is_array($options['meta_parent_id']) && count($options['meta_parent_id']) ? 'tg.tag_meta_parent_id IN (' . implode(',', IPSLib::cleanIntArray($options['meta_parent_id'])) . ')' : 'tg.tag_meta_parent_id=' . intval($options['meta_parent_id']); } if (isset($options['meta_id']) && (is_numeric($options['meta_id']) || count($options['meta_id']))) { $where[] = is_array($options['meta_id']) && count($options['meta_id']) ? 'tg.tag_meta_id IN (' . implode(',', IPSLib::cleanIntArray($options['meta_id'])) . ')' : 'tg.tag_meta_id=' . intval($options['meta_id']); } if (isset($options['meta_app'])) { $where[] = is_array($options['meta_app']) && count($options['meta_app']) ? 'tg.tag_meta_app IN (\'' . implode("','", $options['meta_app']) . '\')' : 'tg.tag_meta_app=\'' . $options['meta_app'] . '\''; } if (isset($options['meta_area'])) { $where[] = is_array($options['meta_area']) && count($options['meta_area']) ? 'tg.tag_meta_area IN (\'' . implode("','", $options['meta_area']) . '\')' : 'tg.tag_meta_area=\'' . $options['meta_area'] . '\''; } if (!empty($options['not_meta_id'])) { $where[] = is_array($options['not_meta_id']) && count($options['not_meta_id']) ? 'tg.tag_meta_id NOT IN (' . implode(",", $options['not_meta_id']) . ')' : 'tg.tag_meta_id !=' . intval($options['not_meta_id']); } if (isset($tags)) { if (isset($options['match']) and $options['match'] == 'loose') { $_tags = is_array($tags) ? $tags : array($tags); $_t = array(); foreach ($_tags as $text) { $_t[] = ' tg.tag_text LIKE \'%' . $this->DB->addSlashes($text) . '%\''; } if (count($_t)) { $where[] = implode(" OR ", $_t); } } else { if (is_array($tags)) { $_t = $tags; $tags = array(); foreach ($_t as $t) { $tags[] = $this->DB->addSlashes($t); } } $where[] = is_array($tags) ? 'tg.tag_text IN (\'' . implode("','", $tags) . '\')' : 'tg.tag_text=\'' . $this->DB->addSlashes($tags) . '\''; } } $prefix = ips_DBRegistry::getPrefix(); /* Did we add in perm check? */ if (!empty($options['isViewable'])) { if ($options['joins']) { $select = array(); $join = ''; foreach ($options['joins'] as $j) { foreach ($j['from'] as $name => $ref) { $select[] = $j['select']; $join .= ' LEFT JOIN ' . $prefix . $name . ' ' . $ref; if ($j['where']) { $join .= ' ON (' . $j['where'] . ')'; } } } } if (count($select)) { $_select = ',' . implode(',', $select); } $options['limit'] = $options['limit'] > 0 && $options['limit'] < 5000 ? $options['limit'] : 250; /* we need to make an exception if filtering by date */ if (class_exists('IPSSearchRegistry') && (IPSSearchRegistry::get('in.search_date_start') || IPSSearchRegistry::get('in.search_date_end'))) { $options['limit'] = 10000000000.0; } $this->DB->allow_sub_select = true; $this->DB->query('SELECT tg.* ' . $_select . ' FROM ' . $prefix . 'core_tags tg ' . $join . ' WHERE ' . implode(' AND ', $where) . ' AND tg.tag_aai_lookup IN (' . 'SELECT tag_perm_aai_lookup FROM ' . $prefix . 'core_tags_perms WHERE ' . $this->DB->buildWherePermission($this->member->perm_id_array, 'tag_perm_text') . ' AND tag_perm_visible=1 ' . ') ORDER BY ' . $order . ' ' . $dir . ' LIMIT 0,' . $options['limit']); $this->DB->execute(); } else { if (is_array($options['joins'])) { $db = array('select' => 'tg.*', 'from' => array('core_tags' => 'tg'), 'where' => implode(' AND ', $where), 'add_join' => array($options['joins']), 'order' => $order . ' ' . $dir); } else { $db = array('select' => 'tg.*', 'from' => 'core_tags tg', 'where' => implode(' AND ', $where), 'order' => $order . ' ' . $dir); } if (!empty($options['limit']) || !empty($options['offset'])) { $db['limit'] = array(intval($options['offset']), intval($options['limit'])); } /* Fetch */ $this->DB->build($db); $this->DB->execute(); } /* Fetch data */ while ($row = $this->DB->fetch()) { $return[$row['tag_id']] = $row; } return $return; }
public function __construct(&$obj) { $this->DB = ipsRegistry::DB(); $this->prefix = ips_DBRegistry::getPrefix(); }