diff --git a/Readme b/Readme index 34dd9c4..400f278 100644 --- a/Readme +++ b/Readme @@ -2,8 +2,15 @@ adds json APIs to the forum add this application to your forum, enable it. the following URLs/methods will now be accessible to you: -/api/category -/api/discussion (GET: id, limit, offset) -/api/discussion/add (POST:Discussion/CategoryID, Discussion/Body, Discussion/Name, Discussion/TransientKey) -/api/comment/add (POST: Comment/DiscussionID, Comment/CategoryID, Comment/Body, Comment/Name, Comment/TransientKey) -/api/session +/api/loginapi (GET: user, pass) + +/api/categoryapi + +/api/discussionapi (GET: id, limit, offset) + +/api/discussionapi/add (POST:CategoryID, Body, Name, TransientKey, UserID) +/api/discussionapi/remove (POST:CategoryID, DiscussionID, TransientKey, UserID) + +/api/commentapi/add (POST: DiscussionID, CategoryID, Body, Name, TransientKey) + +/api/sessionapi diff --git a/controllers/class.apicontroller.php b/controllers/class.apicontroller.php index 0ce8b44..f85adf6 100755 --- a/controllers/class.apicontroller.php +++ b/controllers/class.apicontroller.php @@ -13,12 +13,13 @@ class APIController extends Gdn_Controller { public function __construct() { parent::__construct(); + } public function Initialize() { parent::Initialize(); - + $this->_DeliveryMethod = DELIVERY_METHOD_JSON; //$this->SetHeader("Content-Type", "application/json; charset=utf-8"); $this->SetHeader("Content-Type", "text/plain; charset=utf-8"); diff --git a/controllers/class.categorycontroller.php b/controllers/class.categoryapicontroller.php similarity index 98% rename from controllers/class.categorycontroller.php rename to controllers/class.categoryapicontroller.php index e9ddf45..986eb6d 100644 --- a/controllers/class.categorycontroller.php +++ b/controllers/class.categoryapicontroller.php @@ -1,6 +1,6 @@ Start($_POST['UserID'], TRUE, TRUE); + } + } public function Index() { + $Limit = GetIncomingValue('limit', 5); $Offset = GetIncomingValue('offset', 0); $DiscussionID = GetIncomingValue('id', 0); @@ -24,6 +34,7 @@ class DiscussionController extends APIController } $this->Render(); + Gdn::Session()->End(); } /** @@ -33,7 +44,7 @@ class DiscussionController extends APIController public function Add() { $Session = Gdn::Session(); - $Errors = array(); + $Errors = array(); // Set the model on the form. $this->Form->SetModel($this->DiscussionModel); @@ -41,7 +52,6 @@ class DiscussionController extends APIController if($this->Form->AuthenticatedPostBack() === TRUE) { $FormValues = $this->Form->FormValues(); - // Check category permissions if(!$Session->CheckPermission('Vanilla.Discussions.Add', $FormValues['CategoryID'])) $Errors[] = 'You do not have permission to start discussions in this category'; @@ -57,6 +67,41 @@ class DiscussionController extends APIController $this->SetJSON("Errors", $Errors); $this->Render(); + Gdn::Session()->End(); + } + + /** + * Remove a discussion. + * @param int The category id to add the discussion to. + */ + public function Remove() + { + $Session = Gdn::Session(); + $Errors = array(); + + // Set the model on the form. + $this->Form->SetModel($this->DiscussionModel); + + if($this->Form->AuthenticatedPostBack() === TRUE) + { + $FormValues = $this->Form->FormValues(); + + // Check category permissions + if(!$Session->CheckPermission('Vanilla.Discussions.Add', $FormValues['CategoryID'])) + $Errors[] = 'You do not have permission to start discussions in this category'; + else + $DiscussionID = $this->DiscussionModel->Delete($FormValues['DiscussionID']); + $this->SetJSON("removed", $DiscussionID); + } + else + $Errors[] = 'You do not have credentials to post as this user'; + + // Return the form errors + if(count($Errors) > 0) + $this->SetJSON("Errors", $Errors); + + $this->Render(); + Gdn::Session()->End(); } } diff --git a/controllers/class.sessionapicontroller.php b/controllers/class.sessionapicontroller.php new file mode 100644 index 0000000..92d63a4 --- /dev/null +++ b/controllers/class.sessionapicontroller.php @@ -0,0 +1,61 @@ +User != False) + $this->SetJSON("user", array("TransientKey"=>$Session->TransientKey(), "UserID"=>$Session->UserID, "Name"=>$Session->User->Name, "User"=>True)); + else + $this->SetJSON("user", array("TransientKey"=>$Session->TransientKey(), "UserID"=>0, "User"=>False)); + + $this->Render(); + } + + + public function Login(){ + + $Username = GetIncomingValue('user', 'admin'); + $Password = GetIncomingValue('pass', 'pass'); + + $UserModel = new UserModel(); + $User = $UserModel->GetByEmail($Username); + + if (!$User) { + $User = $UserModel->GetByUsername($Username); + } + + $Result = FALSE; + if ($User) { + // Check the password. + $PasswordHash = new Gdn_PasswordHash(); + $Result = $PasswordHash->CheckPassword($Password, val('Password', $User), val('HashMethod', $User)); + //print_r($User);exit; + + if ($Result) { + $Session = Gdn::Session(); + Gdn::Session()->Start($User->UserID, TRUE, TRUE); + $this->SetJSON("user", array("TransientKey"=>$User->Attributes['TransientKey'], "UserID"=>$User->UserID, "Name"=>$User->Name, "User"=>$Result)); + } else { + $this->SetJSON("user", array("TransientKey"=>false, "UserID"=>0, "User"=>False)); + } + + } + + $this->Render(); + Gdn::Session()->End(); + + //echo ($Result) ? 'Success' : 'Failure'; + } + + +} + +?> diff --git a/controllers/class.sessioncontroller.php b/controllers/class.sessioncontroller.php deleted file mode 100644 index 71459d8..0000000 --- a/controllers/class.sessioncontroller.php +++ /dev/null @@ -1,21 +0,0 @@ -User != False) - $this->SetJSON("user", array("TransientKey"=>$Session->TransientKey(), "UserID"=>$Session->UserID, "Name"=>$Session->User->Name, "User"=>True)); - else - $this->SetJSON("user", array("TransientKey"=>$Session->TransientKey(), "UserID"=>0, "User"=>False)); - - $this->Render(); - } - -} - -?> diff --git a/views/category/index.php b/views/categoryapi/index.php similarity index 100% rename from views/category/index.php rename to views/categoryapi/index.php diff --git a/views/comment/index.php b/views/commentapi/index.php similarity index 100% rename from views/comment/index.php rename to views/commentapi/index.php diff --git a/views/discussion/index.php b/views/discussionapi/add.php similarity index 100% rename from views/discussion/index.php rename to views/discussionapi/add.php diff --git a/views/session/index.php b/views/discussionapi/index.php similarity index 100% rename from views/session/index.php rename to views/discussionapi/index.php diff --git a/views/discussionapi/remove.php b/views/discussionapi/remove.php new file mode 100644 index 0000000..e69de29 diff --git a/views/sessionapi/index.php b/views/sessionapi/index.php new file mode 100644 index 0000000..e69de29 diff --git a/views/sessionapi/login.php b/views/sessionapi/login.php new file mode 100644 index 0000000..e69de29