/** * Returns criteria statement fragments for querying shares. * * @param string $userid The userid of the user to check access for. * @param integer $perm The level of permissions required. * * @return array An array with query and where string fragments. */ protected function _getUserAndGroupCriteria($userid, $perm = Horde_Perms::SHOW) { $query = $where = ''; if (empty($userid)) { $where = '(' . $this->_db->buildClause('s.perm_guest', '&', $perm) . ' > 0)'; } else { // (owner == $userid) $where .= 's.share_owner = ' . $this->_db->quote($userid); // (name == perm_creator and val & $perm) $where .= ' OR (' . $this->_db->buildClause('s.perm_creator', '&', $perm) . ' > 0)'; // (name == perm_creator and val & $perm) $where .= ' OR (' . $this->_db->buildClause('s.perm_default', '&', $perm) . ' > 0)'; // (name == perm_users and key == $userid and val & $perm) $query .= ' LEFT JOIN ' . $this->_table . '_users u ON u.share_id = s.share_id'; $where .= ' OR ( u.user_uid = ' . $this->_db->quote($userid) . ' AND (' . $this->_db->buildClause('u.perm', '&', $perm) . ' > 0))'; // If the user has any group memberships, check for those also. try { $groups = $this->_groups->listGroups($userid); if ($groups) { // (name == perm_groups and key in ($groups) and val & $perm) $ids = array_keys($groups); $group_ids = array(); foreach ($ids as $id) { $group_ids[] = $this->_db->quote((string) $id); } $query .= ' LEFT JOIN ' . $this->_table . '_groups g ON g.share_id = s.share_id'; $where .= ' OR (g.group_uid IN (' . implode(',', $group_ids) . ') AND (' . $this->_db->buildClause('g.perm', '&', $perm) . ' > 0))'; } } catch (Horde_Group_Exception $e) { $this->_logger->err($e); } } return array($query, $where); }
/** * Builds a piece of a search query. * * @param string $glue The glue to join the criteria (OR/AND). * @param array $criteria The array of criteria. * * @return array An SQL fragment and a list of values suitable for binding * as an array. */ protected function _buildSearchQuery($glue, array $criteria) { $clause = ''; $values = array(); foreach ($criteria as $key => $vals) { if (!empty($vals['OR']) || !empty($vals['AND'])) { if (!empty($clause)) { $clause .= ' ' . $glue . ' '; } $binds = $this->_buildSearchQuery(!empty($vals['OR']) ? 'OR' : 'AND', $vals); $clause .= '(' . $binds[0] . ')'; $values = array_merge($values, $binds[1]); } else { if (isset($vals['field'])) { if (!empty($clause)) { $clause .= ' ' . $glue . ' '; } $rhs = $this->_convertToDriver($vals['test']); $binds = $this->_db->buildClause($vals['field'], $vals['op'], $rhs, true, array('begin' => !empty($vals['begin']))); if (is_array($binds)) { $clause .= $binds[0]; $values = array_merge($values, $binds[1]); } else { $clause .= $binds; } } else { foreach ($vals as $test) { if (!empty($test['OR']) || !empty($test['AND'])) { if (!empty($clause)) { $clause .= ' ' . $glue . ' '; } $binds = $this->_buildSearchQuery(!empty($vals['OR']) ? 'OR' : 'AND', $test); $clause .= '(' . $binds[0] . ')'; $values = array_merge($values, $binds[1]); } else { if (!empty($clause)) { $clause .= ' ' . $key . ' '; } $rhs = $this->_convertToDriver($test['test']); if ($rhs == '' && $test['op'] == '=') { $clause .= '(' . $this->_db->buildClause($test['field'], '=', $rhs) . ' OR ' . $test['field'] . ' IS NULL)'; } else { $binds = $this->_db->buildClause($test['field'], $test['op'], $rhs, true, array('begin' => !empty($test['begin']))); if (is_array($binds)) { $clause .= $binds[0]; $values = array_merge($values, $binds[1]); } else { $clause .= $binds; } } } } } } } return array($clause, $values); }
/** * Searches a calendar. * * @param object $query An object with the criteria to search for. * @param boolean $json Store the results of the events' toJson() method? * * @return mixed An array of Kronolith_Events. * @throws Kronolith_Exception */ public function search($query, $json = false) { /* Build SQL conditions based on the query string. */ $cond = '(('; $values = array(); foreach (array('title', 'location', 'url', 'description') as $field) { if (!empty($query->{$field})) { $binds = $this->_db->buildClause('event_' . $field, 'LIKE', $this->convertToDriver($query->{$field}), true); if (is_array($binds)) { $cond .= $binds[0] . ' AND '; $values = array_merge($values, $binds[1]); } else { $cond .= $binds; } } } if (!empty($query->baseid)) { $binds = $this->_db->buildClause('event_baseid', '=', $query->baseid, true); if (is_array($binds)) { $cond .= $binds[0] . ' AND '; $values = array_merge($values, $binds[1]); } else { $cond .= $binds; } } if (isset($query->status)) { $binds = $this->_db->buildClause('event_status', '=', $query->status, true); if (is_array($binds)) { $cond .= $binds[0] . ' AND '; $values = array_merge($values, $binds[1]); } else { $cond .= $binds; } } if (!empty($query->creator)) { $binds = $this->_db->buildClause('event_creator_id', '=', $query->creator, true); if (is_array($binds)) { $cond .= $binds[0] . ' AND '; $values = array_merge($values, $binds[1]); } else { $cond .= $binds; } } if ($cond == '((') { $cond = ''; } else { $cond = substr($cond, 0, strlen($cond) - 5) . '))'; } $eventIds = $this->_listEventsConditional(empty($query->start) ? null : $query->start, empty($query->end) ? null : $query->end, $cond, $values); $events = array(); foreach ($eventIds as $eventId) { Kronolith::addSearchEvents($events, $this->getEvent($eventId), $query, $json); } return $events; }
public function getLikePages($pagename) { if (Horde_String::isUpper($pagename, 'UTF-8')) { $firstword = $pagename; $lastword = null; } else { /* Get the first and last word of the page name. */ $count = preg_match_all('/[A-Z][a-z0-9]*/', $pagename, $matches); if (!$count) { return array(); } $matches = $matches[0]; $firstword = $matches[0]; $lastword = $matches[$count - 1]; if (strlen($firstword) == 1 && strlen($matches[1]) == 1) { for ($i = 1; $i < $count; $i++) { $firstword .= $matches[$i]; if (isset($matches[$i + 1]) && strlen($matches[$i + 1]) > 1) { break; } } } if (strlen($lastword) == 1 && strlen($matches[$count - 2]) == 1) { for ($i = $count - 2; $i > 0; $i--) { $lastword = $matches[$i] . $lastword; if (isset($matches[$i - 1]) && strlen($matches[$i - 1]) > 1) { break; } } } } try { $where = $this->_db->buildClause('page_name', 'LIKE', $firstword); if (!empty($lastword) && $lastword != $firstword) { $where .= ' OR ' . $this->_db->buildClause('page_name', 'LIKE', $lastword); } } catch (Horde_Db_Exception $e) { throw new Wicked_Exception($e); } return $this->_retrieve($this->_params['table'], $where); }