BxApiModule.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup API API to the UNA backend
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxApiModule extends BxDolModule
  12. {
  13. function __construct(&$aModule)
  14. {
  15. parent::__construct($aModule);
  16. }
  17. public function serviceGetSafeServices()
  18. {
  19. $a = parent::serviceGetSafeServices();
  20. return array_merge($a, array (
  21. 'DeletePage' => '',
  22. 'ChangeAccountPassword' => '',
  23. ));
  24. }
  25. public function serviceGetPublicServices()
  26. {
  27. $a = parent::serviceGetPublicServices();
  28. return array_merge($a, array (
  29. 'Test' => '',
  30. 'GetPage' => '',
  31. ));
  32. }
  33. /**
  34. * @page public_api API Public
  35. * @section public_api_api_test /m/oauth2/com/test
  36. *
  37. * Test method to check public API
  38. *
  39. * **HTTP Method:**
  40. * `GET`
  41. *
  42. * **Request params:**
  43. * n/a
  44. *
  45. * **Response (success):**
  46. * @code
  47. * {
  48. * "result": "Test passed."
  49. * }
  50. * @endcode
  51. */
  52. public function serviceTest()
  53. {
  54. return array('result' => 'Test passed.');
  55. }
  56. /**
  57. * @page public_api API Public
  58. * @section public_api_api_get_page /m/oauth2/com/get_page
  59. *
  60. * Get page with cells and blocks as array
  61. *
  62. * **HTTP Method:**
  63. * `GET`
  64. *
  65. * **Request params:**
  66. * - `uri` - page URI
  67. *
  68. * **Response (success):**
  69. * @code
  70. * {
  71. * "id": "123", // page ID
  72. * "layout": "5", // page layout, 5 is simplest page with one cell
  73. * "module": "system", // module which this page
  74. * "title": "Test page", // page title
  75. * "type": 1, // page type, 1 is for default page with header and footer
  76. * "uri": "test", // page URI, which is part of page URL
  77. * "elements": {
  78. * "cell_1": [
  79. * {
  80. * "content": "test content", // block content, it can be array as well
  81. * "designbox_id": "11", // block design, such as padding, border, title
  82. * "hidden_on": "", // not empty block need to be hidden on mobile, or desktop
  83. * "id": "321",
  84. * "module": "system", // module name this block is related to
  85. * "order": "1", // block order
  86. * "title": "Test block", // block title
  87. * "type": "raw" // block type
  88. * }
  89. * ]
  90. * }
  91. * @endcode
  92. *
  93. * **Response (error):**
  94. * @code
  95. * {
  96. * "error":"short error description here",
  97. * "error_description":"long error description here"
  98. * }
  99. * @endcode
  100. */
  101. public function serviceGetPage()
  102. {
  103. $sUri = bx_get('uri');
  104. $oPage = BxDolPage::getObjectInstanceByURI($sUri);
  105. if (!$oPage) {
  106. return array(
  107. 'code' => 404,
  108. 'error' => 'Not Found',
  109. 'desc' => 'This page doesn\'t exist',
  110. );
  111. }
  112. if (!$oPage->isVisiblePage()) {
  113. return array(
  114. 'code' => 403,
  115. 'error' => 'Forbidden',
  116. 'desc' => 'This page requires special right to be viewed',
  117. );
  118. }
  119. return $oPage->getPage ();
  120. }
  121. /**
  122. * @page private_api API Private
  123. * @section private_api_api_delete_page /m/oauth2/com/delete_page
  124. *
  125. * Delete page with all blocks
  126. *
  127. * **Scopes:**
  128. * `api`
  129. *
  130. * **HTTP Method:**
  131. * `POST`
  132. *
  133. * **Request params:**
  134. * - `uri` - page URI
  135. *
  136. * **Request header:**
  137. * @code
  138. * Authorization: Bearer 9802c4a34e1535d8c3b721604ee0e7fb04116c49
  139. * @endcode
  140. *
  141. * **Response (success):**
  142. * @code
  143. * {
  144. * "code": 200,
  145. * }
  146. * @endcode
  147. *
  148. * **Response (error):**
  149. * @code
  150. * {
  151. * "error":"short error description here",
  152. * "error_description":"long error description here"
  153. * }
  154. * @endcode
  155. */
  156. public function serviceDeletePage()
  157. {
  158. $sUri = bx_get('uri');
  159. $oPage = BxDolPage::getObjectInstanceByURI($sUri);
  160. if (!$oPage) {
  161. return array(
  162. 'code' => 404,
  163. 'error' => 'No Content',
  164. 'desc' => 'Such page doesn\'t exist',
  165. );
  166. }
  167. if (!$oPage->isDeleteAllowed()) {
  168. return array(
  169. 'code' => 403,
  170. 'error' => 'Forbidden',
  171. 'desc' => 'Not enough rights to delete this page',
  172. );
  173. }
  174. $oPageBuilder = new BxTemplStudioBuilderPage($oPage->getModule(), $oPage->getName());
  175. $mixed = $oPageBuilder->processAction('page_delete');
  176. if (is_array($mixed) && (isset($mixed['msg']) || isset($mixed['message']))) {
  177. return array(
  178. 'code' => 500,
  179. 'error' => 'Internal Error',
  180. 'desc' => isset($mixed['msg']) ? $mixed['msg'] : $mixed['message'],
  181. );
  182. }
  183. return array('code' => 200);
  184. }
  185. /**
  186. * @page private_api API Private
  187. * @section private_api_api_change_account_password /m/oauth2/com/change_account_password
  188. *
  189. * Change account password
  190. *
  191. * **Scopes:**
  192. * `api`
  193. *
  194. * **HTTP Method:**
  195. * `POST`
  196. *
  197. * **Request params:**
  198. * - `password` - new password
  199. *
  200. * **Request header:**
  201. * @code
  202. * Authorization: Bearer 9802c4a34e1535d8c3b721604ee0e7fb04116c49
  203. * @endcode
  204. *
  205. * **Response (success):**
  206. * @code
  207. * {
  208. * "code": 200,
  209. * }
  210. * @endcode
  211. *
  212. * **Response (error):**
  213. * @code
  214. * {
  215. * "error":"short error description here",
  216. * "error_description":"long error description here"
  217. * }
  218. * @endcode
  219. */
  220. public function serviceChangeAccountPassword()
  221. {
  222. $sPassword = bx_get('password');
  223. if ($sPassword == '') {
  224. return array(
  225. 'code' => 404,
  226. 'error' => 'No Content',
  227. 'desc' => 'Password can\'t be empty',
  228. );
  229. }
  230. $oAccount = BxDolAccount::getInstance();
  231. if (!$oAccount->updatePassword($sPassword)){
  232. return array(
  233. 'code' => 500,
  234. 'error' => 'Internal Error',
  235. );
  236. }
  237. return array('code' => 200);
  238. }
  239. }
  240. /** @} */