Skip to content

Commit

Permalink
Merge branch 'master' of github.com:joindin/joindin-api
Browse files Browse the repository at this point in the history
  • Loading branch information
akrabat committed Sep 25, 2016
2 parents 3aa4eec + 5146bc1 commit 260edf2
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 276 deletions.
4 changes: 2 additions & 2 deletions src/controllers/EventImagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class EventImagesController extends ApiController
public function createImage($request, $db)
{
if (! isset($request->user_id)) {
throw new Exception("You must be logged in to create data", 400);
throw new Exception("You must be logged in to create data", 401);
}

$event_id = $this->getItemId($request);
Expand Down Expand Up @@ -96,7 +96,7 @@ public function createImage($request, $db)
public function deleteImage($request, $db)
{
if (! isset($request->user_id)) {
throw new Exception("You must be logged in to create data", 400);
throw new Exception("You must be logged in to create data", 401);
}

$event_id = $this->getItemId($request);
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/Event_commentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function reportComment($request, $db)
{
// must be logged in to report a comment
if (! isset($request->user_id) || empty($request->user_id)) {
throw new Exception('You must log in to report a comment');
throw new Exception('You must log in to report a comment', 401);
}

$comment_mapper = new EventCommentMapper($db, $request);
Expand Down Expand Up @@ -186,7 +186,7 @@ public function moderateReportedComment($request, $db)
{
// must be logged in
if (! isset($request->user_id) || empty($request->user_id)) {
throw new Exception('You must log in to moderate a comment');
throw new Exception('You must log in to moderate a comment', 401);
}

$comment_mapper = new EventCommentMapper($db, $request);
Expand Down
11 changes: 6 additions & 5 deletions src/controllers/EventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function getAction($request, $db)
public function postAction($request, $db)
{
if (! isset($request->user_id)) {
throw new Exception("You must be logged in to create data", 400);
throw new Exception("You must be logged in to create data", 401);
}
if (isset($request->url_elements[4])) {
switch ($request->url_elements[4]) {
Expand Down Expand Up @@ -359,7 +359,7 @@ function ($tag) {
public function deleteAction($request, $db)
{
if (! isset($request->user_id)) {
throw new Exception("You must be logged in to delete data", 400);
throw new Exception("You must be logged in to delete data", 401);
}
if (isset($request->url_elements[4])) {
switch ($request->url_elements[4]) {
Expand All @@ -382,7 +382,7 @@ public function deleteAction($request, $db)
public function putAction($request, $db)
{
if (! isset($request->user_id)) {
throw new Exception('You must be logged in to edit data', 400);
throw new Exception('You must be logged in to edit data', 401);
}

$event_id = $this->getItemId($request);
Expand Down Expand Up @@ -539,6 +539,7 @@ function ($tag) {
*/
public function createTrack($request, $db)
{
// Should we not check for a login?
$track = array();
$event_id = $this->getItemId($request);
$track['event_id']= $event_id;
Expand Down Expand Up @@ -602,7 +603,7 @@ public function createTrack($request, $db)
public function approveAction($request, $db)
{
if (! isset($request->user_id)) {
throw new Exception("You must be logged in to create data", 400);
throw new Exception("You must be logged in to create data", 401);
}

$event_id = $this->getItemId($request);
Expand Down Expand Up @@ -642,7 +643,7 @@ public function approveAction($request, $db)
public function rejectAction($request, $db)
{
if (! isset($request->user_id)) {
throw new Exception("You must be logged in to create data", 400);
throw new Exception("You must be logged in to create data", 401);
}

$event_id = $this->getItemId($request);
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/Talk_commentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function reportComment($request, $db)
{
// must be logged in to report a comment
if (! isset($request->user_id) || empty($request->user_id)) {
throw new Exception('You must log in to report a comment');
throw new Exception('You must log in to report a comment', 401);
}

$comment_mapper = new TalkCommentMapper($db, $request);
Expand Down Expand Up @@ -101,7 +101,7 @@ public function moderateReportedComment($request, $db)
{
// must be logged in
if (! isset($request->user_id) || empty($request->user_id)) {
throw new Exception('You must log in to moderate a comment');
throw new Exception('You must log in to moderate a comment', 401);
}

$comment_mapper = new TalkCommentMapper($db, $request);
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/TalksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getAction($request, $db)
public function postAction($request, $db)
{
if (! isset($request->user_id)) {
throw new Exception("You must be logged in to create data", 400);
throw new Exception("You must be logged in to create data", 401);
}
$talk_id = $this->getItemId($request);

Expand Down Expand Up @@ -150,7 +150,7 @@ public function postAction($request, $db)
public function deleteAction($request, $db)
{
if (! isset($request->user_id)) {
throw new Exception("You must be logged in to delete data", 400);
throw new Exception("You must be logged in to delete data", 401);
}
if (isset($request->url_elements[4])) {
switch ($request->url_elements[4]) {
Expand Down Expand Up @@ -323,7 +323,7 @@ protected function getTalkById($db, $request, $talk_id)
public function createTalkAction(Request $request, PDO $db)
{
if (!isset($request->user_id)) {
throw new Exception("You must be logged in to create data", 400);
throw new Exception("You must be logged in to create data", 401);
}

$event_id = $this->getItemId($request);
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/TracksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function getAction($request, $db)

public function editTrack($request, $db)
{
// Should we not check for login?
$track_id = $this->getItemId($request);

$track_mapper = new TrackMapper($db, $request);
Expand Down Expand Up @@ -74,6 +75,7 @@ public function editTrack($request, $db)

public function deleteTrack($request, $db)
{
// Should we not check for login?
$track_id = $this->getItemId($request);

$track_mapper = new TrackMapper($db, $request);
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ public function postAction($request, $db)
*/
public function updateUser(Request $request, $db)
{
if (false === ($request->getUserId())) {
throw new Exception("You must be logged in to change a user account", 400);
if (false == ($request->getUserId())) {
throw new Exception("You must be logged in to change a user account", 401);
}

$userId = $this->getItemId($request);
Expand Down
2 changes: 1 addition & 1 deletion src/inc/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function setClientInfo()
$header = new Header('X-Forwarded-For', $_SERVER['HTTP_X_FORWARDED_FOR'], ',');
$header->parseParams();
$elementArray = $header->buildEntityArray();
$ipAddress = count($elementArray) ? $elementArray[0] : null;
$ipAddress = count($elementArray) ? $elementArray[0][0] : null;
}
$this->clientIP = $ipAddress;
$this->clientUserAgent = $userAgent;
Expand Down
129 changes: 0 additions & 129 deletions src/inc/Timezone.php

This file was deleted.

6 changes: 3 additions & 3 deletions src/models/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ public function getOutputView(Request $request, $verbose = false)

// special handling for dates
if ($this->event_tz_place != '' && $this->event_tz_cont != '') {
$tz = $this->event_tz_cont . '/' . $this->event_tz_place;
$tz = new DateTimeZone($this->event_tz_cont . '/' . $this->event_tz_place);
} else {
$tz = 'UTC';
$tz = new DateTimeZone('UTC');
}

foreach ($fields as $output_name => $name) {
$value = $this->$name;

// override if it is a date
if (substr($output_name, - 5) == '_date' && ! empty($value)) {
$value = Timezone::formattedEventDatetimeFromUnixtime($value, $tz, 'c');
$value = (new DateTime('@' . $value))->setTimezone($tz)->format('c');
}

$item[$output_name] = $value;
Expand Down
11 changes: 3 additions & 8 deletions src/models/ApiMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,11 @@ public function transformResults($results, $verbose)
// special handling for dates
if (substr($key, - 5) == '_date' && ! empty($row[ $value ])) {
if ($row['event_tz_place'] != '' && $row['event_tz_cont'] != '') {
$tz = $row['event_tz_cont'] . '/' . $row['event_tz_place'];
$tz = new DateTimeZone($row['event_tz_cont'] . '/' . $row['event_tz_place']);
} else {
$tz = 'UTC';
$tz = new DateTimeZone('UTC');
}
$entry[ $key ] =
Timezone::formattedEventDatetimeFromUnixtime(
$row[ $value ],
$tz,
'c'
);
$entry[ $key ] = (new DateTime('@' . $row[$value]))->setTimezone($tz)->format('c');
} else {
if (array_key_exists($value, $row)) {
$entry[$key] = $row[$value];
Expand Down
8 changes: 4 additions & 4 deletions src/models/TalkMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getTalksByEventId($event_id, $resultsperpage, $start)
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total = $this->getTotalCount($sql, array(':event_id' => $event_id));
$results = $this->processResults($results);

return new TalkModelCollection($results, $total);
}

Expand Down Expand Up @@ -697,7 +697,7 @@ public function thisUserHasAdminOn($talk_id)
. ' from user_admin a '
. ' inner join user u on u.ID = a.uid '
. ' inner join talks t on t.event_id = rid '
. ' where rtype="event" and rcode!="pending"'
. ' where rtype="event" and (rcode!="pending" OR rcode is null)'
. ' AND u.ID = :user_id'
. ' AND t.ID = :talk_id';
$stmt = $this->_db->prepare($sql);
Expand Down Expand Up @@ -734,13 +734,13 @@ public function addTalkToTrack($talk_id, $track_id)
$stmt = $this->_db->prepare($sql);
$stmt->execute($params);
$talk_track_id = $stmt->fetchColumn();

if ($talk_track_id === false) {
// insert new row as not in database
$sql = 'insert into talk_track (track_id, talk_id) values (:track_id, :talk_id)';
$stmt = $this->_db->prepare($sql);
$stmt->execute($params);

$talk_track_id = $this->_db->lastInsertId();
}
return $talk_track_id;
Expand Down
5 changes: 4 additions & 1 deletion src/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// @codingStandardsIgnoreFile
include '../inc/Autoloader.php';
include '../inc/Request.php';
include '../inc/Timezone.php';
include '../inc/Header.php';
if (!function_exists('apache_request_headers')) {
include '../inc/nginx-helper.php';
Expand All @@ -17,6 +16,10 @@ function handle_exception($e)
$status_code = is_numeric($status_code) ? $status_code : 500;
header("Status: " . $status_code, false, $status_code);

if ($status_code === 401) {
header('WWW-Authenticate: Bearer realm="api.joind.in"');
}

$message = $e->getMessage();
if ($e instanceof PDOException && (!isset($config['mode']) || $config['mode'] !== "development")) {
$message = "Database error";
Expand Down
Loading

0 comments on commit 260edf2

Please sign in to comment.