Example #1
0
 public function post()
 {
     // Set variables to be used
     APIHelper::setSessionUser();
     // Include dependencies
     jimport('joomla.database.table');
     $language = JFactory::getLanguage();
     $language->load('joomla', JPATH_ADMINISTRATOR);
     $language->load('com_categories', JPATH_ADMINISTRATOR);
     require_once JPATH_ADMINISTRATOR . '/components/com_categories/models/category.php';
     // Fake parameters
     $_POST['task'] = 'apply';
     $_REQUEST['task'] = 'apply';
     $_REQUEST[JUtility::getToken()] = 1;
     $_POST[JUtility::getToken()] = 1;
     $data = JRequest::getVar('jform', array(), 'post', 'array');
     $context = 'category';
     // Clear userstate just in case
     $model = APIodel::getInstance('Category', 'CategoriesModel');
     $success = $model->save($data);
     if ($model->getError()) {
         $response = $this->getErrorResponse(400, $model->getError());
     } elseif (!$success) {
         $response = $this->getErrorResponse(400, JText::_('COM_API_ERROR_OCURRED'));
     } else {
         $response = $this->getSuccessResponse(201, JText::_('COM_CATEGORIES_SAVE_SUCCESS'));
         // Get the ID of the category that was modified or inserted
         $response->id = $model->get('state')->get($context . '.id');
         // Checkin category
         $model->checkin($response->id);
     }
     $this->plugin->setResponse($response);
 }
Example #2
0
 /**
  * This is not the best example to follow
  * Please see the category plugin for a better example
  */
 public function post()
 {
     // Set variables to be used
     APIHelper::setSessionUser();
     // Include dependencies
     jimport('joomla.database.table');
     $language = JFactory::getLanguage();
     $language->load('joomla', JPATH_ADMINISTRATOR);
     $language->load('com_k2', JPATH_ADMINISTRATOR);
     require_once JPATH_ADMINISTRATOR . '/components/com_k2/models/item.php';
     JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_k2/tables');
     // Fake parameters
     $_REQUEST[JUtility::getToken()] = 1;
     $_POST[JUtility::getToken()] = 1;
     // Clear userstate just in case
     $row = $this->save();
     if ($this->getError()) {
         $response = $this->getErrorResponse(400, $this->getError());
     } elseif (!$row->id) {
         $response = $this->getErrorResponse(400, JText::_('COM_API_ERROR_OCURRED'));
     } else {
         $response = $this->getSuccessResponse(201, JText::_('COM_API_SUCCESS'));
         // Get the ID of the category that was modified or inserted
         $response->id = $row->id;
     }
     $this->plugin->setResponse($response);
 }
Example #3
0
 public function post()
 {
     // Set variables to be used
     APIHelper::setSessionUser();
     JFactory::getLanguage()->load('com_users', JPATH_ADMINISTRATOR);
     // Include dependencies
     jimport('joomla.application.component.controller');
     jimport('joomla.form.form');
     jimport('joomla.database.table');
     APIodel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_users/models');
     JForm::addFormPath(JPATH_ADMINISTRATOR . '/components/com_users/models/forms');
     JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_users/tables');
     // Get user data
     $data = JRequest::getVar('jform', array(), 'post', 'array');
     if (!isset($data['groups'])) {
         $data['groups'] = array();
     }
     // Save user
     $model = APIodel::getInstance('User', 'UsersModel');
     $model->getState('user.id');
     // This is only here to trigger populateState()
     $success = $model->save($data);
     if ($model->getError()) {
         $response = $this->getErrorResponse(400, $model->getError());
     } elseif (!$success) {
         $response = $this->getErrorResponse(400, JText::_('COM_API_ERROR_OCURRED'));
     } else {
         $response = $this->getSuccessResponse(201, JText::_('COM_API_SUCCESS'));
         $response->id = $model->getState('user.id');
     }
     $this->plugin->setResponse($response);
 }
Example #4
0
 public function get()
 {
     APIHelper::setSessionUser();
     $sites = self::getSites();
     if (JRequest::getVar('default', false)) {
         $sites = array_merge(array((object) array('value' => '', 'text' => JText::_('JOPTION_USE_DEFAULT'))), $sites);
     }
     $this->plugin->setResponse($sites);
 }
Example #5
0
 public function get()
 {
     APIHelper::setSessionUser();
     $options = array();
     if (JRequest::getVar('default', false)) {
         $options = array((object) array('value' => '', 'text' => JText::_('JOPTION_USE_DEFAULT')));
     }
     $sites = APIHelper::getField('timezone', array('name' => JRequest::getVar('field_name', ''), 'id' => JRequest::getVar('field_id', ''), '_options' => $options));
     $this->plugin->setResponse(array('html' => $sites->input));
 }
Example #6
0
 public function get()
 {
     jimport('joomla.application.helper');
     // Set variables to be used
     APIHelper::setSessionUser();
     $user = JFactory::getUser();
     // Response is always successfull if we gotten this far! yippy!
     $response = $this->getSuccessResponse(200, JText::_('COM_API_SUCCESS'));
     $response->user = (object) array('id' => $user->id, 'name' => $user->name, 'username' => $user->username, 'email' => $user->email);
     $this->plugin->setResponse($response);
 }
Example #7
0
 public function get()
 {
     APIHelper::setSessionUser();
     require_once JPATH_ADMINISTRATOR . '/components/com_menus/models/items.php';
     require_once JPATH_PLUGINS . '/api/menus/resources/helper.php';
     $model = APIodel::getInstance('APIHelperModel', 'MenuItemsModel');
     $model->_setCache('getstart', $model->getState('list.start'));
     $menuitems = $model->getItems();
     if (false === $menuitems || empty($menuitems) && $model->getError()) {
         $response = $this->getErrorResponse(400, $model->getError());
     } else {
         $response = $menuitems;
     }
     $this->plugin->setResponse($response);
 }
 /**
  * 获取登录信息
  */
 protected function getLoginInfo()
 {
     $ptLoginInfo = APIHelper::CheckPtLogin();
     if (empty($ptLoginInfo)) {
         echo '{"status": -99, "msg":"未登录"}';
         exit;
     }
     $uin = isset($ptLoginInfo['Uin']) ? sprintf("%.0f", $ptLoginInfo['Uin']) : 0;
     $nickname = isset($ptLoginInfo['NickName']) ? sprintf("%s", $ptLoginInfo['NickName']) : "";
     if ($uin < 10000) {
         echo '{"status": -98, "msg":"QQ不合法"}';
         exit;
     }
     $this->uin = $uin;
     $this->nickname = $nickname;
 }
Example #9
0
 public function get()
 {
     // Set variables to be used
     APIHelper::setSessionUser();
     APIodel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_users/models');
     $model = APIodel::getInstance('User', 'UsersModel');
     $grouplist = $model->getGroups();
     $groups = $model->getAssignedGroups(JRequest::getInt('user_id'));
     if (!$grouplist) {
         $html = '';
     } else {
         JHtml::addIncludePath(JPATH_ROOT . '/components/com_users/helpers/html');
         $html = JHtml::_('access.usergroups', 'jform[groups]', $groups, true);
     }
     $this->plugin->setResponse(array('html' => $html));
 }
Example #10
0
 public function get()
 {
     $extension = JRequest::getWord('extension');
     APIHelper::setSessionUser();
     require_once JPATH_ADMINISTRATOR . '/components/com_categories/models/categories.php';
     require_once JPATH_PLUGINS . '/api/categories/resources/helper.php';
     $model = APIodel::getInstance('APIHelperModel', 'CategoriesModel');
     $model->_setCache('getstart', $model->getState('list.start'));
     $categories = $model->getItems();
     if (false === $categories) {
         $response = $this->getErrorResponse(400, $model->getError());
     } else {
         $response = $categories;
     }
     $this->plugin->setResponse($response);
 }
Example #11
0
 public function authenticateRequest()
 {
     $user_id = APIHelper::getAPIUserID();
     if ($user_id === false) {
         return false;
     } else {
         $user = JFactory::getUser($user_id);
         if (!$user->id) {
             $this->setError(JText::_('COM_API_USER_NOT_FOUND'));
             return false;
         }
         if ($user->block == 1) {
             $this->setError(JText::_('COM_API_BLOCKED_USER'));
             return false;
         }
         return $user;
     }
 }
Example #12
0
 /**
  * HTML list ready response
  */
 public function get()
 {
     $extension = JRequest::getWord('extension');
     $categories = JHtml::_('category.options', $extension);
     // Verify permissions.  If the action attribute is set, then we scan the options.
     $action = 'core.edit.own';
     // Get the current user object.
     $user = JFactory::getUser(APIHelper::getAPIUserId());
     foreach ($categories as $i => $cat) {
         // To take save or create in a category you need to have create rights for that category
         // unless the item is already in that category.
         // Unset the option if the user isn't authorised for it. In this field assets are always categories.
         if ($user->authorise('core.create', $extension . '.category.' . $cat->value) != true) {
             unset($categories[$i]);
         }
     }
     $this->plugin->setResponse($categories);
 }
Example #13
0
 function login()
 {
     $userName = \Yoda\Request::getString('username');
     $password = \Yoda\Request::getString('password');
     $testMode = \Yoda\Request::getBool('test_mode');
     try {
         APIHelper::setupClient($userName, $password, $testMode);
         $details = new ApiDetails();
     } catch (SoapFault $e) {
         $this->redirect('index.php?controller=authorize', 'Error: (' . $e->faultcode . ') ' . $e->faultstring, 'error');
     } catch (Exception $e) {
         $this->redirect('index.php?controller=authorize', $e->getMessage(), 'error');
     }
     $_SESSION['username'] = $userName;
     $_SESSION['resellerKey'] = $password;
     $_SESSION['isTestMode'] = $testMode;
     $_SESSION['reseller_id'] = $details->getResellerId();
     $this->redirect('index.php');
 }
Example #14
0
 public function post()
 {
     // Set variables to be used
     APIHelper::setSessionUser();
     $language = JFactory::getLanguage();
     $language->load('joomla', JPATH_ADMINISTRATOR);
     $language->load('com_menus', JPATH_ADMINISTRATOR);
     // Include dependencies
     jimport('joomla.application.component.controller');
     jimport('joomla.form.form');
     jimport('joomla.database.table');
     require_once JPATH_ADMINISTRATOR . '/components/com_menus/controllers/item.php';
     require_once JPATH_ADMINISTRATOR . '/components/com_menus/models/item.php';
     JForm::addFormPath(JPATH_ADMINISTRATOR . '/components/com_menus/models/forms/');
     // Fake parameters
     $_POST['task'] = 'apply';
     $_REQUEST['task'] = 'apply';
     $_REQUEST[JUtility::getToken()] = 1;
     $_POST[JUtility::getToken()] = 1;
     $app = JFactory::getApplication();
     $context = 'com_menus.edit.item';
     // Save menuitem
     $controller = new MenusControllerItem();
     $success = $controller->execute('apply');
     if ($controller->getError()) {
         $response = $this->getErrorResponse(400, $controller->getError());
     } elseif (false === $success) {
         $response = $this->getErrorResponse(400, JText::_('COM_API_ERROR_OCURRED'));
     } else {
         $response = $this->getSuccessResponse(201, $controller->get('message'));
         // Kind of a weird way of doing this, there has to be a better way?
         $values = (array) $app->getUserState($context . '.id');
         $response->id = array_pop($values);
         $app->setUserState($context . '.id', $values);
         // Checkin menuitem
         $controller->getModel()->checkin($response->id);
     }
     // Clear userstate for future requests
     $app->setUserState($context . '.id', array());
     $this->plugin->setResponse($response);
 }
 /**
  * A copy with modifications for the api from the categoryparent.php
  * field on the categories component
  */
 public function get()
 {
     $options = array();
     JFactory::getLanguage()->load('joomla', JPATH_ADMINISTRATOR);
     $db = JFactory::getDbo();
     $query = $db->getQuery(true);
     $query->select('a.id AS value, a.title AS text, a.level');
     $query->from('#__categories AS a');
     $query->join('LEFT', '`#__categories` AS b ON a.lft > b.lft AND a.rgt < b.rgt');
     // Filter by the type
     if ($extension = JRequest::getVar('extension')) {
         $query->where('(a.extension = ' . $db->quote($extension) . ' OR a.parent_id = 0)');
     }
     // Prevent parenting to children of this item.
     if ($id = JRequest::getInt('id')) {
         $query->join('LEFT', '`#__categories` AS p ON p.id = ' . (int) $id);
         $query->where('NOT(a.lft >= p.lft AND a.rgt <= p.rgt)');
         $rowQuery = $db->getQuery(true);
         $rowQuery->select('a.id AS value, a.title AS text, a.level, a.parent_id');
         $rowQuery->from('#__categories AS a');
         $rowQuery->where('a.id = ' . (int) $id);
         $db->setQuery($rowQuery);
         $row = $db->loadObject();
     }
     $query->where('a.published IN (0,1)');
     $query->group('a.id');
     $query->order('a.lft ASC');
     // Get the options.
     $db->setQuery($query);
     $options = $db->loadObjectList();
     // Check for a database error.
     if ($db->getErrorNum()) {
         JError::raiseWarning(500, $db->getErrorMsg());
     }
     // Pad the option text with spaces using depth level as a multiplier.
     for ($i = 0, $n = count($options); $i < $n; $i++) {
         // Translate ROOT
         if ($options[$i]->level == 0) {
             $options[$i]->text = JText::_('JGLOBAL_ROOT_PARENT');
         }
         $options[$i]->text = str_repeat('- ', $options[$i]->level) . $options[$i]->text;
     }
     // Initialise variables.
     $user = JFactory::getUser(APIHelper::getAPIUserId());
     if (empty($id)) {
         // New item, only have to check core.create.
         foreach ($options as $i => $option) {
             // Unset the option if the user isn't authorised for it.
             if (!$user->authorise('core.create', $extension . '.category.' . $option->value)) {
                 unset($options[$i]);
             }
         }
     } else {
         // Existing item is a bit more complex. Need to account for core.edit and core.edit.own.
         foreach ($options as $i => $option) {
             // Unset the option if the user isn't authorised for it.
             if (!$user->authorise('core.edit', $extension . '.category.' . $option->value)) {
                 // As a backup, check core.edit.own
                 if (!$user->authorise('core.edit.own', $extension . '.category.' . $option->value)) {
                     // No core.edit nor core.edit.own - bounce this one
                     unset($options[$i]);
                 } else {
                     // TODO I've got a funny feeling we need to check core.create here.
                     // Maybe you can only get the list of categories you are allowed to create in?
                     // Need to think about that. If so, this is the place to do the check.
                 }
             }
         }
     }
     if (isset($row) && !isset($options[0])) {
         if ($row->parent_id == '1') {
             $parent = new stdClass();
             $parent->text = JText::_('JGLOBAL_ROOT_PARENT');
             array_unshift($options, $parent);
         }
     }
     $this->plugin->setResponse($options);
 }
 /**
  * Delete one object
  * @param   string $pk    Required parameter: CustomerID
  * @return CustomersModel response from the API call*/
 public function DeleteOneCustomers($pk)
 {
     //prepare query string for API call
     $queryBuilder = Configuration::BASEURI . "/Customers/{pk}";
     //process optional query parameters
     APIHelper::appendUrlWithTemplateParameters($queryBuilder, array("pk" => $pk));
     //validate and preprocess url
     $queryUrl = APIHelper::cleanUrl($queryBuilder);
     //prepare headers
     $headers = array("User-Agent" => @"APIMATIC 2.0", "Accept" => "application/json");
     //prepare API request
     $request = Unirest::delete($queryUrl, $headers);
     //and invoke the API call request to fetch the response
     $response = $request->getResponse();
     //Error handling using HTTP status codes
     if ($response->code < 200 || $response->code > 206) {
         //[200,206] = HTTP OK
         throw new APIException("HTTP Response Not OK", $response->code);
     }
     return $response->body;
 }
Example #17
0
 function setSessionUser()
 {
     $session =& JFactory::getSession();
     $session->set('user', JUser::getInstance(APIHelper::getAPIUserID()));
 }
Example #18
0
 /**
  * The User Profile endpoint returns information about the Uber user that has authorized with the application.
  * @return UserProfileModel response from the API call*/
 public function getUserProfile()
 {
     //prepare query string for API call
     $queryBuilder = Configuration::BASEURI . "/v1/me";
     //validate and preprocess url
     $queryUrl = APIHelper::cleanUrl($queryBuilder);
     //prepare headers
     $headers = array("accept" => "application/json", "Authorization", sprintf("Bearer %1{$s}", Configuration::$oAuthAccessToken));
     //prepare API request
     $request = Unirest::get($queryUrl, $headers);
     //and invoke the API call request to fetch the response
     $response = $request->getResponse();
     //Error handling using HTTP status codes
     if ($response->code == 400) {
         throw new APIException("Malformed request.", 400);
     } else {
         if ($response->code == 401) {
             throw new APIException("Unauthorized the request requires user authentication (not logged in).", 401);
         } else {
             if ($response->code == 403) {
                 throw new APIException("Forbidden. Also used for unauthorized requests such as improper OAuth 2.0 scopes or permissions issues.", 403);
             } else {
                 if ($response->code == 404) {
                     throw new APIException("Not found.", 404);
                 } else {
                     if ($response->code == 406) {
                         throw new APIException("Unacceptable content type. Client sent an accepts header for a content type which does not exist on the server. Body includes a list of acceptable content types: “Unacceptable content type. Request resource as: application/json, etc.", 406);
                     } else {
                         if ($response->code == 422) {
                             throw new APIException("Invalid request. The request body is parse-able however with invalid content.", 422);
                         } else {
                             if ($response->code == 429) {
                                 throw new APIException("Too Many Requests. Rate limited.", 429);
                             } else {
                                 if ($response->code == 500) {
                                     throw new APIException("Internal Server Error.", 500);
                                 } else {
                                     if ($response->code != 200) {
                                         //200 = HTTP OK
                                         throw new APIException("HTTP Response Not OK", $response->code);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return $response->body;
 }
Example #19
0
 static function setupAPIClient()
 {
     return APIHelper::setupClient($_SESSION['username'], $_SESSION['resellerKey'], $_SESSION['isTestMode']);
 }