PageRenderTime 64ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/xenforo/library/bdApi/ControllerApi/Tool.php

https://gitlab.com/billyprice1/bdApi
PHP | 305 lines | 232 code | 60 blank | 13 comment | 27 complexity | e1c02090ff393d68d041d6f44fcd7f62 MD5 | raw file
  1. <?php
  2. class bdApi_ControllerApi_Tool extends bdApi_ControllerApi_Abstract
  3. {
  4. public function actionGetLogin()
  5. {
  6. $redirectUri = $this->_input->filterSingle('redirect_uri', XenForo_Input::STRING);
  7. if (empty($redirectUri)) {
  8. return $this->responseError(new XenForo_Phrase('bdapi_slash_tools_login_requires_redirect_uri'), 400);
  9. }
  10. /* @var $session bdApi_Session */
  11. $session = XenForo_Application::getSession();
  12. $clientId = $session->getOAuthClientId();
  13. if (empty($clientId)) {
  14. $this->_response->setHeader('X-Api-Login-Error', 'client_id');
  15. return $this->responseNoPermission();
  16. }
  17. if (!$session->isValidRedirectUri($redirectUri)) {
  18. $this->_response->setHeader('X-Api-Login-Error', 'redirect_uri');
  19. return $this->responseNoPermission();
  20. }
  21. $userId = XenForo_Visitor::getUserId();
  22. if (empty($userId)) {
  23. $this->_response->setHeader('X-Api-Login-Error', 'oauth_token');
  24. return $this->responseNoPermission();
  25. }
  26. $loginLinkData = array(
  27. 'redirect' => $redirectUri,
  28. 'timestamp' => XenForo_Application::$time + 10,
  29. );
  30. $loginLinkData['user_id'] = bdApi_Crypt::encryptTypeOne($userId, $loginLinkData['timestamp']);
  31. $loginLink = XenForo_Link::buildPublicLink('login/api', '', $loginLinkData);
  32. return $this->responseRedirect(XenForo_ControllerResponse_Redirect::RESOURCE_CANONICAL_PERMANENT, $loginLink);
  33. }
  34. public function actionPostLoginSocial()
  35. {
  36. $social = array();
  37. $options = XenForo_Application::getOptions();
  38. if ($options->get('facebookAppId')) {
  39. $social[] = 'facebook';
  40. }
  41. if ($options->get('twitterAppKey')) {
  42. $social[] = 'twitter';
  43. }
  44. if ($options->get('googleClientId')) {
  45. $social[] = 'google';
  46. }
  47. return $this->responseData('bdApi_ViewApi_Tool_LoginSocial', array('social' => $social));
  48. }
  49. public function actionGetLogout()
  50. {
  51. $redirectUri = $this->_input->filterSingle('redirect_uri', XenForo_Input::STRING);
  52. if (empty($redirectUri)) {
  53. return $this->responseError(new XenForo_Phrase('bdapi_slash_tools_login_requires_redirect_uri'), 400);
  54. }
  55. /* @var $session bdApi_Session */
  56. $session = XenForo_Application::getSession();
  57. $clientId = $session->getOAuthClientId();
  58. if (empty($clientId)) {
  59. $this->_response->setHeader('X-Api-Logout-Error', 'client_id');
  60. return $this->responseNoPermission();
  61. }
  62. if (!$session->isValidRedirectUri($redirectUri)) {
  63. $this->_response->setHeader('X-Api-Logout-Error', 'redirect_uri');
  64. return $this->responseNoPermission();
  65. }
  66. $logoutLinkData = array(
  67. 'redirect' => $redirectUri,
  68. '_xfToken' => XenForo_Visitor::getInstance()->get('csrf_token_page'),
  69. 'timestamp' => XenForo_Application::$time + 10,
  70. );
  71. $logoutLinkData['md5'] = bdApi_Crypt::encryptTypeOne(md5($logoutLinkData['redirect']), $logoutLinkData['timestamp']);
  72. $logoutLink = XenForo_Link::buildPublicLink('logout', '', $logoutLinkData);
  73. return $this->responseRedirect(XenForo_ControllerResponse_Redirect::RESOURCE_CANONICAL_PERMANENT, $logoutLink);
  74. }
  75. public function actionPostPasswordTest()
  76. {
  77. $input = $this->_input->filter(array(
  78. 'password' => XenForo_Input::STRING,
  79. 'password_algo' => XenForo_Input::STRING,
  80. 'decrypt' => XenForo_Input::UINT,
  81. ));
  82. if (!XenForo_Application::debugMode()) {
  83. return $this->responseNoPermission();
  84. }
  85. if (empty($input['decrypt'])) {
  86. $result = bdApi_Crypt::encrypt($input['password'], $input['password_algo']);
  87. } else {
  88. $result = bdApi_Crypt::decrypt($input['password'], $input['password_algo']);
  89. }
  90. $data = array('result' => $result);
  91. return $this->responseData('bdApi_ViewApi_Tool_PasswordTest', $data);
  92. }
  93. public function actionPostPasswordResetRequest()
  94. {
  95. $this->_assertRegistrationRequired();
  96. $user = XenForo_Visitor::getInstance()->toArray();
  97. /* @var $userConfirmationModel XenForo_Model_UserConfirmation */
  98. $userConfirmationModel = $this->getModelFromCache('XenForo_Model_UserConfirmation');
  99. $userConfirmationModel->sendPasswordResetRequest($user);
  100. return $this->responseMessage(new XenForo_Phrase('password_reset_request_has_been_emailed_to_you'));
  101. }
  102. public function actionPostLink()
  103. {
  104. $type = $this->_input->filterSingle('type', XenForo_Input::STRING, array('default' => 'public'));
  105. $route = $this->_input->filterSingle('route', XenForo_Input::STRING, array('default' => 'index'));
  106. switch ($type) {
  107. case 'admin':
  108. $link = XenForo_Link::buildAdminLink($route);
  109. break;
  110. case 'public':
  111. default:
  112. $link = XenForo_Link::buildPublicLink($route);
  113. break;
  114. }
  115. $data = array(
  116. 'type' => $type,
  117. 'route' => $route,
  118. 'link' => $link,
  119. );
  120. return $this->responseData('bdApi_ViewApi_Tool_Link', $data);
  121. }
  122. public function actionPostPing()
  123. {
  124. $this->_assertAdminPermission('bdApi');
  125. $visitor = XenForo_Visitor::getInstance();
  126. $userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);
  127. $message = $this->_input->filterSingle('message', XenForo_Input::STRING);
  128. XenForo_Model_Alert::alert(
  129. $userId,
  130. $visitor['user_id'], $visitor['username'],
  131. 'api_ping', 0, 'message',
  132. array(
  133. 'message' => $message,
  134. )
  135. );
  136. return $this->responseMessage(new XenForo_Phrase('changes_saved'));
  137. }
  138. public function actionGetParseLink()
  139. {
  140. $link = $this->_input->filterSingle('link', XenForo_Input::STRING);
  141. $link = XenForo_Link::convertUriToAbsoluteUri($link, true);
  142. $fc = XenForo_Application::get('_bdApi_fc');
  143. /* @var $dependencies bdApi_Dependencies */
  144. $dependencies = $fc->getDependencies();
  145. $request = new bdApi_Zend_Controller_Request_Http($link);
  146. $request->setBaseUrl(parse_url(XenForo_Application::getOptions()->get('boardUrl'), PHP_URL_PATH));
  147. $routeMatch = $dependencies->routePublic($request);
  148. if (!$routeMatch OR !$routeMatch->getControllerName()) {
  149. // link cannot be route
  150. return $this->_actionGetParseLink_getControllerResponseNop($link, false);
  151. }
  152. $controllerResponse = $this->_actionGetParseLink_getControllerResponse($link, $request, $routeMatch);
  153. if (!empty($controllerResponse)) {
  154. return $controllerResponse;
  155. }
  156. // controller / action not recognized...
  157. return $this->_actionGetParseLink_getControllerResponseNop($link, true);
  158. }
  159. protected function _actionGetParseLink_getControllerResponseNop($link, $routed)
  160. {
  161. return $this->responseData('bdApi_ViewApi_Tool_ParseLink', array(
  162. 'link' => $link,
  163. 'routed' => $routed,
  164. ));
  165. }
  166. protected function _actionGetParseLink_getControllerResponse($link, Zend_Controller_Request_Http $request, XenForo_RouteMatch $routeMatch)
  167. {
  168. switch ($routeMatch->getControllerName()) {
  169. case 'XenForo_ControllerPublic_Forum':
  170. $nodeId = $request->getParam('node_id');
  171. if (empty($nodeId)) {
  172. $nodeName = $request->getParam('node_name');
  173. if (!empty($nodeName)) {
  174. /* @var $nodeModel XenForo_Model_Node */
  175. $nodeModel = $this->getModelFromCache('XenForo_Model_Node');
  176. $node = $nodeModel->getNodeByName($nodeName, 'Forum');
  177. if (!empty($node)) {
  178. $nodeId = $node['node_id'];
  179. }
  180. }
  181. }
  182. if (!empty($nodeId)) {
  183. $this->_request->setParam('forum_id', $nodeId);
  184. }
  185. return $this->responseReroute('bdApi_ControllerApi_Thread', 'get-index');
  186. case 'XenForo_ControllerPublic_Thread':
  187. $threadId = $request->getParam('thread_id');
  188. if (!empty($threadId)) {
  189. $this->_request->setParam('thread_id', $threadId);
  190. $linkFragment = parse_url($link, PHP_URL_FRAGMENT);
  191. if (!empty($linkFragment) AND preg_match('#^post-(?<post_id>\d+)$#', $linkFragment, $fragment)) {
  192. $this->_request->setParam('page_of_post_id', $fragment['post_id']);
  193. }
  194. return $this->responseReroute('bdApi_ControllerApi_Post', 'get-index');
  195. }
  196. break;
  197. case 'XenForo_ControllerPublic_Post':
  198. $postId = $request->getParam('post_id');
  199. if (!empty($postId)) {
  200. $this->_request->setParam('page_of_post_id', $postId);
  201. return $this->responseReroute('bdApi_ControllerApi_Post', 'get-index');
  202. }
  203. break;
  204. }
  205. return null;
  206. }
  207. protected function _preDispatchFirst($action)
  208. {
  209. switch ($action) {
  210. case 'GetLogin':
  211. case 'GetLogout':
  212. $this->_redirectAsNoPermission = true;
  213. break;
  214. }
  215. parent::_preDispatchFirst($action);
  216. }
  217. protected $_redirectAsNoPermission = false;
  218. public function responseNoPermission()
  219. {
  220. if ($this->_redirectAsNoPermission) {
  221. // this "hack" is required because other pre dispatch jobs may throw no permission response around
  222. // and we want to redirect them all, not just from our actions
  223. $redirectUri = $this->_input->filterSingle('redirect_uri', XenForo_Input::STRING);
  224. if (!empty($redirectUri)) {
  225. return $this->responseRedirect(
  226. XenForo_ControllerResponse_Redirect::RESOURCE_CANONICAL_PERMANENT,
  227. $redirectUri
  228. );
  229. }
  230. }
  231. return parent::responseNoPermission();
  232. }
  233. protected function _getScopeForAction($action)
  234. {
  235. return false;
  236. }
  237. /**
  238. *
  239. * @return XenForo_Model_Alert
  240. */
  241. protected function _getAlertModel()
  242. {
  243. return $this->getModelFromCache('XenForo_Model_Alert');
  244. }
  245. }