/** * * */ function getUserResByTag(folksoQuery $q, folksoDBconnect $dbc, folksoSession $fks) { $r = new folksoResponse(); try { $u = $fks->userSession(null); if (!$u instanceof folksoUser && !$q->is_param('user')) { return $r->setError(404, 'No user'); } elseif ($q->is_param('user')) { $u = new folksoUser($dbc); // we create a user object anyway $u->setUid($q->get_param('user')); if (!$u->exists($q->get_param('user'))) { return $r->setError(404, 'Missing or invalid user'); } } $i = new folksoDBinteract($dbc); $uq = new folksoUserQuery(); $sql = $uq->resourcesByTag($q->tag, $u->userid); $i->query($sql); /* these are inside the try block because exists() hits the DB */ if ($i->rowCount == 0) { if (isset($u->nick) || $u->exists()) { return $r->setOk(204, 'User has no resources with this tag'); } else { // no longer necessary return $r->setError(404, 'Unknown user'); } } } catch (dbException $e) { return $r->handleDBexception($e); } catch (badUseridException $e) { return $r->handleDBexception($e); // TODO: update this with new class } $r->setOk(200, 'Found'); $df = new folksoDisplayFactory(); if ($q->content_type() == 'json') { $dd = new folksoDataJson('resid', 'url', 'title'); } else { $dd = $df->ResourceList('xml'); } $r->t($dd->startform()); while ($row = $i->result->fetch_object()) { $r->t($dd->line($row->id, htmlspecialchars($row->uri_raw), htmlspecialchars($row->title))); } $r->t($dd->endform()); return $r; }
function autocomplete(folksoQuery $q, folksoDBconnect $dbc, folksoSession $fks) { $i = new folksoDBinteract($dbc); $r = new folksoResponse(); if ($i->db_error()) { $r->dbConnectionError($i->error_info()); return $r; } $sql = "SELECT tagdisplay " . "FROM tag " . "WHERE tagnorm like '" . $i->dbescape(strtolower($q->get_param('q'))) . "%'"; $i->query($sql); switch ($i->result_status) { case 'DBERR': $r->dbQueryError($i->error_info()); return $r; break; case 'NOROWS': $r->setOk(204, 'No matching tags'); return $r; break; case 'OK': $r->setOk(200, 'OK I guess'); while ($row = $i->result->fetch_object()) { /** For entirely numeric tags, we enclose them in quotes so that they can be treated as text instead of as ids. **/ if (is_numeric($row->tagdisplay)) { $r->t('"' . $row->tagdisplay . '"' . "\n"); } else { $r->t($row->tagdisplay . "\n"); } } return $r; break; } }
function metacomplete(folksoQuery $q, folksoWsseCreds $cred, folksoDBconnect $dbc) { $i = new folksoDBinteract($dbc); if ($i->db_error()) { header('HTTP/1.1 501 Database error'); die($i->error_info()); } $sql = "select tagdisplay " . " from metatag " . " where " . " tagnorm like '" . $i->dbescape(strtolower($q->get_param('q'))) . "%'"; $i->query($sql); switch ($i->result_status) { case 'DBERR': header('HTTP/1.1 501 Database query error'); die($i->error_info()); break; case 'NOROWS': header('HTTP/1.1 204 No matching tags'); return; break; case 'OK': header('HTTP/1.1 200 OK I guess'); while ($row = $i->result->fetch_object()) { /** For entirely numeric tags, we enclose them in quotes so that they can be treated as text instead of as ids. **/ if (is_numeric($row->tagdisplay)) { print '"' . $row->tagdisplay . '"' . "\n"; } else { print $row->tagdisplay . "\n"; } } break; } }
/** * rename tag * * rename, newname * */ function renameTag(folksoQuery $q, folksoDBconnect $dbc, folksoSession $fks) { $r = new folksoResponse(); $u = $fks->userSession(null, 'folkso', 'admin'); if (!$u instanceof folksoUser || !$u->checkUserRight('folkso', 'admin')) { return $r->unAuthorized($u); } try { $i = new folksoDBinteract($dbc); if (!$i->tagp($q->tag)) { $r->setError(404, 'Tag not found', 'Nothing to rename. No such tag: ' . $q->tag); return $r; } $query = "UPDATE tag\n SET tagdisplay = '" . $i->dbescape($q->get_param('newname')) . "', " . "tagnorm = normalize_tag('" . $i->dbescape($q->get_param('newname')) . "') " . "where "; if (is_numeric($q->tag)) { $query .= " id = " . $q->tag; } else { $query .= " tagnorm = normalize_tag('" . $i->dbescape($q->tag) . "')"; } $i->query($query); } catch (dbException $e) { return $r->handleDBexception($e); } $r->setOk(204, 'Tag renamed'); return $r; }
/** * Add a note to a resource * * Web params: POST, note, res */ function addNote(folksoQuery $q, folksoDBconnect $dbc, folksoSession $fks) { $r = new folksoResponse(); $u = $fks->userSession(null, 'folkso', 'tag'); if (!$u instanceof folksoUser || !$u->checkUserRight('folkso', 'tag')) { return $r->unAuthorized($u); } try { $i = new folksoDBinteract($dbc); $sql = "INSERT INTO note " . "SET note = '" . $i->dbescape($q->get_param("note")) . "', " . "userid = '" . $u->userid . "', " . "resource_id = "; if (is_numeric($q->res)) { $sql .= $q->res; } else { $sql .= "(SELECT id FROM resource " . " WHERE uri_normal = url_whack('" . $q->res . "'))"; } $i->query($sql); } catch (dbException $e) { return $r->handleDBexception($e); } $r->setOk(202, 'Note accepted'); $r->t("This note will be added to the resource: " . $q->res); $r->t("\n\nText of the submitted note:\n" . $q->get_param('note')); return $r; }
/** * Based on the request received, checks each response object is * checked to see if it is equiped to handle the request. */ public function Respond() { if (!$this->valid_method()) { // some kind of error header('HTTP/1.0 405'); print "NOT OK. Illegal request method for this resource."; return; } if (!$this->validClientAddress($_SERVER['REMOTE_HOST'], $_SERVER['REMOTE_ADDR'])) { header('HTTP/1.0 403'); print "Sorry, this not available to you"; return; } $q = new folksoQuery($_SERVER, $_GET, $_POST); $realm = 'folkso'; $loc = new folksoFabula(); $dbc = $loc->locDBC(); $fks = new folksoSession($dbc); /** * $sid: session ID */ $sid = $_COOKIE['folksosess'] ? $_COOKIE['folksosess'] : $q->get_param('session'); try { $fks->setSid($sid); } catch (badSidException $e) { if ($q->is_write_method()) { header('HTTP/1.1 403 Login required'); // redirect instead header('Location: ' . $loc->loginPage()); exit; } } /* check each response object and run the response if activatep returns true*/ $repflag = false; if (count($this->responseObjects) === 0) { trigger_error("No responseObjects available", E_USER_ERROR); } /** Walking the response objects **/ foreach ($this->responseObjects as $resp) { if ($resp->activatep($q)) { $repflag = true; $resp->Respond($q, $dbc, $fks); break; } } /** check for no valid response **/ if (!$repflag) { header('HTTP/1.1 400'); print "Client did not make a valid query. (folksoServer)"; // default response or error page... } }