From c9e9bd0f6f698093b0efd07a9f6ef19f6957d783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20Fern=C3=A1ndez?= Date: Mon, 1 Dec 2014 10:34:44 +0100 Subject: [PATCH 1/3] Necesary files to make this application work --- views/category/index.php | 0 views/comment/index.php | 0 views/discussion/index.php | 0 views/session/index.php | 0 4 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 views/category/index.php create mode 100644 views/comment/index.php create mode 100644 views/discussion/index.php create mode 100644 views/session/index.php diff --git a/views/category/index.php b/views/category/index.php new file mode 100644 index 0000000..e69de29 diff --git a/views/comment/index.php b/views/comment/index.php new file mode 100644 index 0000000..e69de29 diff --git a/views/discussion/index.php b/views/discussion/index.php new file mode 100644 index 0000000..e69de29 diff --git a/views/session/index.php b/views/session/index.php new file mode 100644 index 0000000..e69de29 From 3eeaa208b424b595dcf0253a62e1c4f07d5ae223 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20Fern=C3=A1ndez?= Date: Tue, 2 Dec 2014 14:02:05 +0100 Subject: [PATCH 2/3] fix namespaces --- Readme | 17 ++++-- controllers/class.apicontroller.php | 3 +- ...er.php => class.categoryapicontroller.php} | 2 +- ...ler.php => class.commentapicontroller.php} | 2 +- ....php => class.discussionapicontroller.php} | 53 ++++++++++++++-- controllers/class.sessionapicontroller.php | 61 +++++++++++++++++++ controllers/class.sessioncontroller.php | 21 ------- views/{category => categoryapi}/index.php | 0 views/{comment => commentapi}/index.php | 0 .../index.php => discussionapi/add.php} | 0 views/{session => discussionapi}/index.php | 0 views/discussionapi/remove.php | 0 views/sessionapi/index.php | 0 views/sessionapi/login.php | 0 14 files changed, 126 insertions(+), 33 deletions(-) rename controllers/{class.categorycontroller.php => class.categoryapicontroller.php} (98%) rename controllers/{class.commentcontroller.php => class.commentapicontroller.php} (95%) rename controllers/{class.discussioncontroller.php => class.discussionapicontroller.php} (54%) create mode 100644 controllers/class.sessionapicontroller.php delete mode 100644 controllers/class.sessioncontroller.php rename views/{category => categoryapi}/index.php (100%) rename views/{comment => commentapi}/index.php (100%) rename views/{discussion/index.php => discussionapi/add.php} (100%) rename views/{session => discussionapi}/index.php (100%) create mode 100644 views/discussionapi/remove.php create mode 100644 views/sessionapi/index.php create mode 100644 views/sessionapi/login.php 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 From fdc79f27961af72c37444f015ec95787fdc6ed4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20Fern=C3=A1ndez?= Date: Tue, 2 Dec 2014 17:32:31 +0100 Subject: [PATCH 3/3] discussions and comments add and remove --- Readme | 7 +- controllers/class.commentapicontroller.php | 56 +++++++- externalclass/qalib.php | 160 +++++++++++++++++++++ externalclass/test.php | 53 +++++++ views/commentapi/add.php | 0 views/commentapi/remove.php | 0 6 files changed, 266 insertions(+), 10 deletions(-) create mode 100644 externalclass/qalib.php create mode 100644 externalclass/test.php create mode 100644 views/commentapi/add.php create mode 100644 views/commentapi/remove.php diff --git a/Readme b/Readme index 400f278..217fd64 100644 --- a/Readme +++ b/Readme @@ -8,9 +8,10 @@ add this application to your forum, enable it. the following URLs/methods will n /api/discussionapi (GET: id, limit, offset) -/api/discussionapi/add (POST:CategoryID, Body, Name, TransientKey, UserID) -/api/discussionapi/remove (POST:CategoryID, DiscussionID, TransientKey, UserID) +/api/discussionapi/add (POST: CategoryID, Body, Name, TransientKey, UserID) +/api/discussionapi/remove (POST: DiscussionID, CategoryID, TransientKey, UserID) -/api/commentapi/add (POST: DiscussionID, CategoryID, Body, Name, TransientKey) +/api/commentapi/add (POST: DiscussionID, CategoryID, Body, TransientKey, UserID) +/api/commentapi/remove (POST: CommentID, CategoryID, TransientKey, UserID) /api/sessionapi diff --git a/controllers/class.commentapicontroller.php b/controllers/class.commentapicontroller.php index b32ccc4..45a1eb5 100644 --- a/controllers/class.commentapicontroller.php +++ b/controllers/class.commentapicontroller.php @@ -2,22 +2,30 @@ class CommentAPIController extends APIController { - public $Uses = array('Form', 'Database', 'CategoryModel', 'DiscussionModel', 'CommentModel'); + public $Uses = array('Form', 'Database', 'CategoryModel', 'DiscussionModel', 'CommentModel'); + + public function __construct() + { + parent::__construct(); + if (isset($_POST['UserID'])){ + Gdn::Session()->Start($_POST['UserID'], TRUE, TRUE); + } + } public function Index() { $this->Render(); - } + } - public function Add() + public function Add() { - $Session = Gdn::Session(); + $Session = Gdn::Session(); $Errors = array(); - // Set the model on the form. - $this->Form->SetModel($this->CommentModel); + // Set the model on the form. + $this->Form->SetModel($this->CommentModel); - if($this->Form->AuthenticatedPostBack() === TRUE) + if($this->Form->AuthenticatedPostBack() === TRUE) { $FormValues = $this->Form->FormValues(); @@ -40,6 +48,40 @@ class CommentAPIController extends APIController $this->Render(); } + /** + * Remove a comment. + * @param int The category id to remove the comment to. + */ + public function Remove() + { + $Session = Gdn::Session(); + $Errors = array(); + + // Set the model on the form. + $this->Form->SetModel($this->CommentModel); + + 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 + $CommentID = $this->CommentModel->Delete($FormValues['CommentID']); + $this->SetJSON("removed", $CommentID); + } + 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/externalclass/qalib.php b/externalclass/qalib.php new file mode 100644 index 0000000..2934fcb --- /dev/null +++ b/externalclass/qalib.php @@ -0,0 +1,160 @@ +login($user,$pass); + } + return false; + + } + + public function getTransientKey() { + return $this->TransientKey; + } + + public function getUserID(){ + return $this->UserID; + } + + public function curl_post($fields = array(), $url =''){ + $fields_string = ''; + + //url-ify the data for the POST + foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } + rtrim($fields_string, '&'); + + $ch = curl_init(); + curl_setopt($ch,CURLOPT_URL, $url); + curl_setopt($ch,CURLOPT_POST, count($fields)); + curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $result = curl_exec($ch); + curl_close($ch); + + return $result; + } + + public function curl_get($url=''){ + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $output = curl_exec($ch); + curl_close($ch); + return $output; + } + + /** + * Login function that retrieves TransientKey + * + * @param (string) (user) username + * @param (string) (pass) password + * @return (TransientKey) + */ + public function login($user='',$pass='') { + + + $url = $this->URL."/sessionapi/login?user=".$user."&pass=".$pass; + + $json = json_decode($this->curl_get($url)); + + $this->TransientKey = $json->user->TransientKey; + $this->UserID = $json->user->UserID; + + $this->Key = $this->UserID."-".$this->TransientKey; + + return $json->user; + } + + + /** + DISCUSSIONS + */ + + public function addDiscussion($CategoryID = false,$Name = false,$Body = false){ + + + //set POST variables + $url = $this->URL."/discussionapi/add"; + + $fields = array( + 'TransientKey' => $this->TransientKey, + 'UserID' => $this->UserID, + 'CategoryID' => urlencode($CategoryID), + 'Body' => urlencode($Body), + 'Name' => urlencode($Name), + 'Type' => 'Question' + ); + + return $this->curl_post($fields,$url); + + } + + public function removeDiscussion($CategoryID = false,$DiscussionID = false){ + + //set POST variables + $url = $this->URL."/discussionapi/remove"; + + $fields = array( + 'TransientKey' => $this->TransientKey, + 'UserID' => $this->UserID, + 'CategoryID' => urlencode($CategoryID), + 'DiscussionID' => urlencode($DiscussionID) + ); + + return $this->curl_post($fields,$url); + + } + + /** + COMMENTS + */ + + public function addComment($DiscussionID = false,$CategoryID = false,$Body = false){ + + //set POST variables + $url = $this->URL."/commentapi/add"; + + $fields = array( + 'TransientKey' => $this->TransientKey, + 'UserID' => $this->UserID, + 'CategoryID' => urlencode($CategoryID), + 'DiscussionID' => urlencode($DiscussionID), + 'Body' => urlencode($Body) + ); + + return $this->curl_post($fields,$url); + + } + + public function removeComment($CategoryID = false,$CommentID = false){ + + //set POST variables + $url = $this->URL."/commentapi/remove"; + + $fields = array( + 'TransientKey' => $this->TransientKey, + 'UserID' => $this->UserID, + 'CategoryID' => urlencode($CategoryID), + 'CommentID' => urlencode($CommentID) + ); + + return $this->curl_post($fields,$url); + + } + +} + + +?> \ No newline at end of file diff --git a/externalclass/test.php b/externalclass/test.php new file mode 100644 index 0000000..ff2772b --- /dev/null +++ b/externalclass/test.php @@ -0,0 +1,53 @@ +"; + + + + +$api = new MyClass('admin','pass'); + +echo "

"; + +/* + + //example login + + //print_r($api->login('admin','pass')); + +*/ + +/* + +//example add and remove discussion + + $json = $api->addDiscussion(1,'esto es una prueba','aquĆ­ viene el texto de la pregunta'); + echo $json; + $array = json_decode($json); + + echo "
"; + $json = $api->removeDiscussion(1,$array->DiscussionID); + echo $json; + +*/ + +/* + +//example add and remove discussion + + $json = $api->addComment(54,1,'comentario de pruebas'); + echo $json; + $array = json_decode($json); + + echo "
"; + $json = $api->removeComment(1,$array->CommentID); + echo $json; + +*/ + + + + +?> \ No newline at end of file diff --git a/views/commentapi/add.php b/views/commentapi/add.php new file mode 100644 index 0000000..e69de29 diff --git a/views/commentapi/remove.php b/views/commentapi/remove.php new file mode 100644 index 0000000..e69de29