Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Switch sessions code over to Wall object implementation

  • Loading branch information...
commit e4624c5d95ccc2d50126e00243048ed06fa0dafd 1 parent 469f89a
@birtles birtles authored
View
80 wall/lib/walls.inc
@@ -104,69 +104,6 @@ function getWallIdFromPath($path) {
return $res;
}
-function startNewSession($wallId, $sessionId, $datetime) {
- // Sanitize input
- $wallId = toIntOrNull($wallId);
- $sessionId = toIntOrNull($sessionId);
-
- // Work out the latest session and if it needs closing
- $latestSession = getLatestSession($wallId);
-
- // Is the passed-in session ID correct?
- if (@$latestSession['id'] !== $sessionId) {
- return false;
- }
-
- // Close the latest session if it's open
- if (@$latestSession['endDate'] === null) {
- closeLastSession($wallId, $sessionId, $datetime);
- }
-
- // Start a new session
- $conn =& getDbConnection();
- $qwallid = $conn->quote($wallId, 'integer');
- $qdatetime = $conn->quote($datetime, 'timestamp');
- $query = 'INSERT INTO sessions(wallId, beginDate)'
- . ' VALUES(' . $conn->quote($wallId, 'integer')
- . ',' . $conn->quote($datetime, 'timestamp')
- . ')';
- $res =& $conn->exec($query);
- if (PEAR::isError($res)) {
- error_log($res->getMessage() . ', ' . $res->getDebugInfo());
- throw new KeyedException('db-error');
- }
- $conn->disconnect();
-
- return true;
-}
-
-function closeLastSession($wallId, $sessionId, $datetime) {
- // Sanitize input
- $wallId = toIntOrNull($wallId);
- $sessionId = toIntOrNull($sessionId);
-
- // Check if the passed in sessionID actually matches the latest session (and
- // that session is not already closed)
- if ($sessionId !== getActiveSessionId($wallId)) {
- return false;
- }
-
- // Close session
- $conn =& getDbConnection();
- $query = 'UPDATE sessions'
- . ' SET endDate=' . $conn->quote($datetime, 'timestamp')
- . ' WHERE sessionId=' . $conn->quote($sessionId, 'integer');
- $res =& $conn->exec($query);
- if (PEAR::isError($res)) {
- error_log($res->getMessage() . ', ' . $res->getDebugInfo());
- throw new KeyedException('db-error');
- }
- $conn->disconnect();
-
- // Return true since we made a change
- return true;
-}
-
// Returns NULL if there is no active session for the wall
function getActiveSessionId($wallId) {
$conn =& getDbConnection();
@@ -353,7 +290,7 @@ class Wall {
$latestSession = $this->latestSession;
// Is the passed-in session ID correct?
- if (@$latestSession['id'] !== $sessionId) {
+ if (toIntOrNull($latestSession['id']) !== $sessionId) {
return false;
}
@@ -376,7 +313,7 @@ class Wall {
// Update latest session
$this->metadata['latestSession']['id']
- = $conn->lastInsertId('sessions', 'sessionId');
+ = intval($conn->lastInsertId('sessions', 'sessionId'));
$this->metadata['latestSession']['start'] = $datetime;
$this->metadata['latestSession']['end'] = null;
$this->metadata['status'] = 'running';
@@ -395,12 +332,15 @@ class Wall {
throw new KeyedException('no-auth');
}
- // Check if the passed in sessionID actually matches the latest session (and
- // that session is not already closed)
+ // Check that:
+ // (a) there is actually a session to close
+ // (b) the session is not already closed
+ // (c) the passed-in session ID matches the latest session
$latestSession = $this->latestSession;
- if (!$latestSession || $latestSession['sessionId'] !== $sessionId) {
+ if ($latestSession === null ||
+ $latestSession['end'] !== null ||
+ $latestSession['id'] !== $sessionId)
return false;
- }
// End session
$conn =& getDbConnection();
@@ -609,7 +549,7 @@ class Walls {
// Session information
if ($row['sessionid']) {
if ($hasAccess) {
- $session['id'] = $row['sessionid'];
+ $session['id'] = intval($row['sessionid']);
$session['start'] = $row['sessionstart'];
$session['end'] = $row['sessionend'];
$metadata['latestSession'] = $session;
View
15 wall/public/wall-maker/api/closeSession.php
@@ -23,21 +23,26 @@
fclose($handle);
// Prepare parameters
-$wallId = @$json['wallId'];
-$sessionId = @$json['sessionId'];
+$wallId = toIntOrNull($json['wallId']);
+$sessionId = toIntOrNull($json['sessionId']);
if (!isset($wallId) || !isset($sessionId)) {
bailWithError('bad-request');
}
-// Close session
+// Get wall
+$wall = Walls::getById($wallId, $_SESSION['email']);
+if ($wall === null)
+ bailWithError('not-found');
+
+// End session
$currentdatetime = gmdate("Y-m-d H:i:s");
-$madeChange = closeLastSession($wallId, $sessionId, $currentdatetime);
+$madeChange = $wall->endSession($sessionId, $currentdatetime);
// Return the result
// - If we made a change then return the latest session.
// - Otherwise return a parallel-change notification with the latest session in
// the detail.
-$latestSession = getLatestSession($wallId);
+$latestSession = $wall->latestSession;
if ($madeChange) {
print json_encode($latestSession);
} else {
View
16 wall/public/wall-maker/api/startSession.php
@@ -6,6 +6,7 @@
require_once('../../../lib/parapara.inc');
require_once('api.inc');
require_once('walls.inc');
+require_once('utils.inc');
header('Content-Type: text/plain; charset=UTF-8');
@@ -23,21 +24,26 @@
fclose($handle);
// Prepare parameters
-$wallId = @$json['wallId'];
-$sessionId = @$json['sessionId'];
+$wallId = toIntOrNull($json['wallId']);
+$sessionId = toIntOrNull($json['sessionId']);
if (!isset($wallId) || !isset($sessionId)) {
bailWithError('bad-request');
}
-// Start new session
+// Get wall
+$wall = Walls::getById($wallId, $_SESSION['email']);
+if ($wall === null)
+ bailWithError('not-found');
+
+// Start session
$currentdatetime = gmdate("Y-m-d H:i:s");
-$madeChange = startNewSession($wallId, $sessionId, $currentdatetime);
+$madeChange = $wall->startSession($sessionId, $currentdatetime);
// Return the result
// - If we made a change then return the latest session.
// - Otherwise return a parallel-change notification with the latest session in
// the detail.
-$latestSession = getLatestSession($wallId);
+$latestSession = $wall->latestSession;
if ($madeChange) {
print json_encode($latestSession);
} else {
Please sign in to comment.
Something went wrong with that request. Please try again.