/** * get a seat if possible * * @param string to designate the target anchor * @param int the initial number of available seats * @return boolean TRUE if you there is enough room, FALSE otherwise */ public static function get_seat($reference, $offer = 20) { global $context; // number of seats is not really managed if (!$offer || $offer < 3) { return TRUE; } // compute the number of confirmed attendees $query = "SELECT id FROM " . SQL::table_name('enrolments') . " WHERE (anchor LIKE '" . $reference . "') AND (approved LIKE 'Y')"; if (($approved = SQL::query_count($query)) && $approved >= $offer) { return FALSE; } // some seats are available return TRUE; }
/** * count users doing the same thing * * @param string reference of the handled object (e.g., 'article:123') * @param string description of the action (e.g., 'post' or 'like') * @return int total count of profiles for this anchor and action */ public static function count_users_at($anchor, $action = NULL) { global $context; // limit the query to one anchor $where = "(anchor LIKE '" . SQL::escape($anchor) . "')"; // for some actions only if (is_array($action)) { $where .= " AND (action IN ('" . implode("', '", $action) . "'))"; } elseif ($action) { $where .= " AND (action LIKE '" . SQL::escape($action) . "')"; } // the list of activities $query = "SELECT DISTINCT edit_id\tFROM " . SQL::table_name('activities') . " AS activities" . " WHERE " . $where . " AND (edit_id > 0)"; // count records return SQL::query_count($query); }
/** * get one section * * @param int or string the id or nick name of the section * @param boolean TRUE to always fetch a fresh instance, FALSE to enable cache * @return the resulting $item array, with at least keys: 'id', 'title', 'description', etc. */ public static function get($id, $mutable = FALSE) { global $context; $output = array(); // sanity check if (!$id) { $output = NULL; return $output; } // ensure proper unicode encoding $id = (string) $id; $id = utf8::encode($id); // filter id from reference if parameter given that way if (substr($id, 0, 8) === 'section:') { $id = substr($id, 8); } // cache previous answers static $cache; if (!is_array($cache)) { $cache = array(); } // cache hit, but only for immutable objects if (!$mutable && isset($cache[$id])) { return $cache[$id]; } // search by id if (is_numeric($id)) { $query = "SELECT * FROM " . SQL::table_name('sections') . " AS sections" . " WHERE (sections.id = " . SQL::escape((int) $id) . ")"; $output = SQL::query_first($query); // or look for given name of handle } else { $query = "SELECT * FROM " . SQL::table_name('sections') . " AS sections" . " WHERE (sections.nick_name LIKE '" . SQL::escape($id) . "') OR (handle LIKE '" . SQL::escape($id) . "')"; $count = SQL::query_count($query); if ($count == 1) { // do the job $output = SQL::query_first($query); } elseif ($count > 1) { // result depending language give by $context['page_language'] if (!isset($_SESSION['surfer_language']) || $_SESSION['surfer_language'] == 'none') { $language = $context['language']; } else { $language = $_SESSION['surfer_language']; } $result = SQL::query($query); while ($item = SQL::fetch($result)) { $output = $item; // return last by default if ($item['language'] == $language) { $output = $item; break; } } } } // save in cache if (is_array($output) && isset($output['id']) && count($cache) < 1000) { $cache[$id] = $output; } // return by reference return $output; }
/** * enroll one user * * This function ensure that an invited person has been enrolled to this event. * * @see articles/invite.php * * @param int id of the enrolled person */ function invite($id) { global $context; // force enrolment only if required if (!isset($_REQUEST['force_enrolment']) || $_REQUEST['force_enrolment'] != 'Y') { return; } // get matching profile if (($user = Users::get($id)) && is_callable(array($this->anchor, 'get_reference'))) { // if there is no enrolment record yet $query = "SELECT id FROM " . SQL::table_name('enrolments') . " WHERE (anchor LIKE '" . SQL::escape($this->anchor->get_reference()) . "') AND (user_id = " . SQL::escape($user['id']) . ")"; if (!SQL::query_count($query)) { // fields to save $query = array(); // reference to the meeting page $query[] = "anchor = '" . SQL::escape($this->anchor->get_reference()) . "'"; // direct enrolment $query[] = "approved = 'Y'"; // save user id $query[] = "user_id = " . SQL::escape($user['id']); // save user e-mail address $query[] = "user_email = '" . SQL::escape($user['email']) . "'"; // insert a new record $query = "INSERT INTO " . SQL::table_name('enrolments') . " SET " . implode(', ', $query); SQL::query($query); } } }
/** * count tables in database * * @param string database name * @param resource connection to the database server, if any * @return int number of tables, or FALSE on failure */ public static function count_tables($name = NULL, $connection = NULL) { global $context; // sanity check if (!$name) { $name = $context['database']; } // use the default connection if (!$connection) { // we do need a connection to the database if (!isset($context['connection']) || !$context['connection']) { return FALSE; } $connection = $context['connection']; } // the query to list tables $query = 'SHOW TABLES'; // count tables return SQL::query_count($query, TRUE, $connection); }
/** * toggle a membership * * The father parameter is used to specialize a membership to a sub-category. * * @param string the anchor id (e.g., 'category:123') * @param string the member id (e.g., 'article:456') * @param string the father id, if any (e.g., 'category:456') * @return string either a null string, or some text describing an error to be inserted into the html response * * @see categories/select.php * @see users/track.php **/ public static function toggle($anchor, $member, $father = NULL) { global $context; // anchor cannot be empty if (!$anchor) { return i18n::s('An anchor is required for this operation.'); } // member cannot be empty if (!$member) { return i18n::s('A member is required for this operation.'); } // clear the cache Cache::clear(array($anchor, $member, $father)); // check if the membership already exists $query = "SELECT id FROM " . SQL::table_name('members') . " WHERE (anchor LIKE '" . SQL::escape($anchor) . "') AND (member LIKE '" . SQL::escape($member) . "') LIMIT 0, 1"; // delete an existing membership if (SQL::query_count($query)) { $query = "DELETE FROM " . SQL::table_name('members') . " WHERE (anchor LIKE '" . SQL::escape($anchor) . "') AND (member LIKE '" . SQL::escape($member) . "')"; // insert one new record } else { // boost further queries list($member_type, $member_id) = explode(':', $member, 2); // insert one new record $query = "INSERT INTO " . SQL::table_name('members') . " SET" . " anchor='" . SQL::escape($anchor) . "'," . " member='" . SQL::escape($member) . "'," . " member_type='" . SQL::escape($member_type) . "'," . " member_id='" . SQL::escape($member_id) . "'," . " edit_date='" . SQL::escape(gmstrftime('%Y-%m-%d %H:%M:%S')) . "'"; } // update the database if (SQL::query($query) === FALSE) { return NULL; } // delete the father membership, if any if ($father) { $query = "DELETE FROM " . SQL::table_name('members') . " WHERE (anchor LIKE '" . SQL::escape($father) . "') AND (member LIKE '" . SQL::escape($member) . "')"; SQL::query($query); } // end of job return NULL; }
/** * visits prove the presence of one user * * @param int id of the visiting user * @param int maximum age of visit, in seconds * @return TRUE if the user is present, FALSE otherwise */ public static function prove_presence_of($user, $timeout = 3600) { global $context; // sanity check if (!$user) { return FALSE; } // only consider recent presence records $threshold = gmstrftime('%Y-%m-%d %H:%M:%S', time() - $timeout); // select matching links $query = "SELECT id FROM " . SQL::table_name('visits') . " AS visits" . " WHERE (visits.user_id = " . SQL::escape($user) . ")" . "\tAND (visits.edit_date >= '" . SQL::escape($threshold) . "')" . " LIMIT 1"; if (!($result = SQL::query_count($query))) { return FALSE; } // we have at least one recent record return TRUE; }
} // count links edited this year $query = "SELECT id FROM " . SQL::table_name('links') . " WHERE (anchor IN ('" . implode("', '", $anchors) . "')) AND (edit_date LIKE '" . $current_year . "%')"; if ($count = SQL::query_count($query)) { $fields_y[] = "`links`=" . $count; $total_y += $count; } // count links attached to private anchors $query = "SELECT id FROM " . SQL::table_name('links') . " WHERE (anchor IN ('" . implode("', '", $anchors_private) . "'))"; if ($count = SQL::query_count($query)) { $fields[] = "`links_private`=" . $count; $total_private += $count; } // count private links edited this year $query = "SELECT id FROM " . SQL::table_name('links') . " WHERE (anchor IN ('" . implode("', '", $anchors_private) . "')) AND (edit_date LIKE '" . $current_year . "%')"; if ($count = SQL::query_count($query)) { $fields_y[] = "`links_private`=" . $count; $total_private_y += $count; } // total number of items in this section $fields[] = "`total`=" . $total; $fields[] = "`total_private`=" . $total_private; // add a record for this section $query = "INSERT INTO " . SQL::table_name('stat_sections') . ' SET ' . implode(', ', $fields); $records += SQL::query($query); // total number of items in this section $fields_y[] = "`total`=" . $total_y; $fields_y[] = "`total_private`=" . $total_private_y; // add a record for things edited during current year $query = "INSERT INTO " . SQL::table_name('stat_sections_' . $current_year) . ' SET ' . implode(', ', $fields); SQL::query($query);