Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Additional functionality for Vanilla API?

edited October 2011 in Vanilla 2.0 - 2.8
Hello,

I'm very new to Vanilla but was attracted to it due to the possibility of a baked-in web services API (http://vanillaforums.org/docs/api). However, this API seems to be for read-only access to the data. Are there any plans to extend the API to allow modification of discussions, user data, etc.? Even if the API will not go all the way to being fully RESTful, just a basic CRUD interface for the main elements (users, categories, discussions, posts) would be great.

EDIT: If there are no current plans for this, can you point me in the right direction to put that functionality in myself?

Thanks,

z.

Best Answer

  • x00x00 MVP
    edited October 2011 Answer ✓
    This a basic idea just messing around as a plugin:
    $PluginInfo['ExtendAPI'] = array(
    'Name' => 'ExtendAPI',
    'Description' => "ExtendAPI",
    'Version' => '0.1',
    'Author' => 'me'
    );

    if (!function_exists('unserialize_xml')) {
    function unserialize_xml($input, $callback = null, $recurse = false){
    // Get input, loading an xml string with simplexml if its the top level of recursion
    $data = ((!$recurse) && is_string($input))? simplexml_load_string($input): $input;
    // Convert SimpleXMLElements to array
    if ($data instanceof SimpleXMLElement) $data = (array) $data;
    // Recurse into arrays
    if (is_array($data)) foreach ($data as &$item) $item = unserialize_xml($item, $callback, true);
    // Run callback and return
    return (!is_array($data) && is_callable($callback))? call_user_func($callback, $data): $data;
    }
    }

    class ExtendAPI extends Gdn_Plugin {

    public function PostController_Render_Before(&$Sender){
    if(in_array($Sender->DeliveryMethod(),array(DELIVERY_METHOD_JSON,DELIVERY_METHOD_XML))){
    if ($Sender->Request->RequestMethod()== 'POST') {
    $RawContent = file_get_contents("php://input");
    if($Sender->DeliveryMethod()==DELIVERY_METHOD_JSON){
    $Data = json_decode($RawContent);
    }elseif($Sender->DeliveryMethod()==DELIVERY_METHOD_XML){
    //dependent on some basic xml parser in this case using SimpleXML.
    $Data = unserialize_xml($RawContent);

    }

    foreach($Data As $NodeName => $NodeValue ){
    SetValue( $Sender->Form->InputPrefix.'/'.($NodeName=='TransKey'?'TransientKey':$NodeName),$_POST,$NodeValue);
    }

    if ($Sender->Form->AuthenticatedPostBack() === TRUE) {
    var_dump($Sender->Form);
    //TO DO validation, save, draft, etc.
    //TO DO output succes/fail, etc
    }
    }else if ($Sender->Request->RequestMethod() == 'GET') {
    $Sender->SetData(array('DiscussionID'=>'','Name'=>'','Body'=>'','CategoryID'=>'','Close'=>'','Save_Draft'=>''));
    $Sender->SetData('TransKey',Gdn::Session()->TransientKey());//'TransientKey' is strippped out so use 'TransKey'
    $Sender->SetData('RequiredFields',array('Name','Body','CategoryID'));

    }
    }

    }

    }

    grep is your friend.

Answers

  • ToddTodd Vanilla Staff
    We have tentative, but not firm plans to add POST access to the API. The two things that I want to do to get that in is:

    a) Default the Gdn_Form->InputPrefix = '' so that API calls can pass parameters in a straightforward manner. This has been done on a branch, but needs testing with javascript and css and whatnot to make sure it works.

    b) Provide an oauth 2.0 endpoint so that users can sign in to the forum through the API. If you want to hack an access_token query string I'd tie into Gdn_Dispatcher_BeforeDispatch_Handler and start a session based on what is passed into that query string parameter.
  • x00x00 MVP
    edited October 2011 Answer ✓
    This a basic idea just messing around as a plugin:
    $PluginInfo['ExtendAPI'] = array(
    'Name' => 'ExtendAPI',
    'Description' => "ExtendAPI",
    'Version' => '0.1',
    'Author' => 'me'
    );

    if (!function_exists('unserialize_xml')) {
    function unserialize_xml($input, $callback = null, $recurse = false){
    // Get input, loading an xml string with simplexml if its the top level of recursion
    $data = ((!$recurse) && is_string($input))? simplexml_load_string($input): $input;
    // Convert SimpleXMLElements to array
    if ($data instanceof SimpleXMLElement) $data = (array) $data;
    // Recurse into arrays
    if (is_array($data)) foreach ($data as &$item) $item = unserialize_xml($item, $callback, true);
    // Run callback and return
    return (!is_array($data) && is_callable($callback))? call_user_func($callback, $data): $data;
    }
    }

    class ExtendAPI extends Gdn_Plugin {

    public function PostController_Render_Before(&$Sender){
    if(in_array($Sender->DeliveryMethod(),array(DELIVERY_METHOD_JSON,DELIVERY_METHOD_XML))){
    if ($Sender->Request->RequestMethod()== 'POST') {
    $RawContent = file_get_contents("php://input");
    if($Sender->DeliveryMethod()==DELIVERY_METHOD_JSON){
    $Data = json_decode($RawContent);
    }elseif($Sender->DeliveryMethod()==DELIVERY_METHOD_XML){
    //dependent on some basic xml parser in this case using SimpleXML.
    $Data = unserialize_xml($RawContent);

    }

    foreach($Data As $NodeName => $NodeValue ){
    SetValue( $Sender->Form->InputPrefix.'/'.($NodeName=='TransKey'?'TransientKey':$NodeName),$_POST,$NodeValue);
    }

    if ($Sender->Form->AuthenticatedPostBack() === TRUE) {
    var_dump($Sender->Form);
    //TO DO validation, save, draft, etc.
    //TO DO output succes/fail, etc
    }
    }else if ($Sender->Request->RequestMethod() == 'GET') {
    $Sender->SetData(array('DiscussionID'=>'','Name'=>'','Body'=>'','CategoryID'=>'','Close'=>'','Save_Draft'=>''));
    $Sender->SetData('TransKey',Gdn::Session()->TransientKey());//'TransientKey' is strippped out so use 'TransKey'
    $Sender->SetData('RequiredFields',array('Name','Body','CategoryID'));

    }
    }

    }

    }

    grep is your friend.

  • It is not the best or complete anything, just shows the basic principles. When you move it in the post controller, you are going to have to do it in the discussion and comment methods, look in them for help, and probably a lot of the functionality will be shared, so you can move it into methods.

    I don't have time to continue right, but take the baton.

    grep is your friend.

  • ToddTodd Vanilla Staff
    Nice start @x00. As an fyi: If you have a block of code you want to display then use pre tags instead of code tags.
  • x00x00 MVP
    edited October 2011
    @Todd I gather. incidentally why is <code> styled as if it is inline. <code> is the correct tag for pre-formated code (splitting hairs)

    grep is your friend.

  • I should add a requirement is some basic XML parser like SimpleXML, and perhaps some out of the box alternative could be a stand in.

    grep is your friend.

  • ToddTodd Vanilla Staff
    In standard html code is an inline tag while pre is a block tag. This is standard html. We find it useful to have both be valid and like the border and different background styling.

    I suppose we can add a rule for code cascading inside a pre tag for those that are being truly semantically correct.
  • my bad it is a phrase element according to web standards.

    grep is your friend.

  • Todd, x00,

    Thank you both very much for your help. I've taken the basic plugin you wrote and am working with it. Since I barely know any PHP, it's going to be very interesting. I'll post back once I have better results.

    Regards,

    z.
Sign In or Register to comment.