BxBaseServiceContent.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Services for content objects
  11. */
  12. class BxBaseServiceContent extends BxDol
  13. {
  14. function serviceCheckLogged()
  15. {
  16. $oAccount = BxDolAccount::getInstance();
  17. $aRet = $oAccount ? $this->getUserIds($oAccount) : null;
  18. if ($aRet)
  19. $aRet['session'] = BxDolSession::getInstance()->getId();
  20. else
  21. $aRet = ['session' => BxDolSession::getInstance()->getId()];
  22. return $aRet;
  23. }
  24. /**
  25. * @page service Service Calls
  26. * @section bx_system_general System Services
  27. * @subsection bx_system_general-content-objects Content Objects
  28. * @subsubsection bx_system_general_cnt-login Login user and get user session
  29. *
  30. * Login user with account id = 12 and get session id back, session id can be used
  31. * as cookie header to perform other API calls under logged user:
  32. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/login/TemplServiceContent&params[]=12" @endcode
  33. *
  34. * @param $iAccountId account id, or account email
  35. * @param $bRememberMe remeber session
  36. * @return array with session and user ids on success or false on error
  37. *
  38. * @see BxBaseServiceContent::serviceLogin
  39. */
  40. /**
  41. * @ref bx_system_general_cnt-login "Login user and get user's session"
  42. */
  43. function serviceLogin($iAccountId, $bRememberMe = false)
  44. {
  45. $oAccount = BxDolAccount::getInstance($iAccountId);
  46. if (!$oAccount)
  47. return false;
  48. bx_login($oAccount->id(), $bRememberMe);
  49. $aRet = $this->getUserIds($oAccount);
  50. if ($aRet)
  51. $aRet['session'] = BxDolSession::getInstance()->getId();
  52. else
  53. $aRet = ['session' => BxDolSession::getInstance()->getId()];
  54. return $aRet;
  55. }
  56. /**
  57. * @page service Service Calls
  58. * @section bx_system_general System Services
  59. * @subsection bx_system_general-content-objects Content Objects
  60. * @subsubsection bx_system_general_cnt-logout Logout current user
  61. *
  62. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/logout/TemplServiceContent" @endcode
  63. *
  64. * @return boolean
  65. *
  66. * @see BxBaseServiceContent::serviceLogout
  67. */
  68. /**
  69. * @ref bx_system_general_cnt-logout "Logout current user"
  70. */
  71. function serviceLogout()
  72. {
  73. if (!isLogged())
  74. return false;
  75. bx_logout();
  76. return true;
  77. }
  78. /**
  79. * @page service Service Calls
  80. * @section bx_system_general System Services
  81. * @subsection bx_system_general-content-objects Content Objects
  82. * @subsubsection bx_system_general_cnt-get_user_ids Get user's IDs
  83. *
  84. * Get user's account id, profile id and profile module content id by account id.
  85. *
  86. * Get user's IDs by user's account id(12):
  87. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/get_user_ids/TemplServiceContent&params[]=12" @endcode
  88. *
  89. * @param $iAccountId account id, or account email
  90. * @return array with user ids on success or false on error
  91. *
  92. * @see BxBaseServiceContent::serviceGetUserIds
  93. */
  94. /**
  95. * @ref bx_system_general_cnt-get_user_ids "Get user's IDs"
  96. */
  97. function serviceGetUserIds($iAccountId)
  98. {
  99. $oAccount = BxDolAccount::getInstance($iAccountId);
  100. if (!$oAccount)
  101. return false;
  102. return $this->getUserIds($oAccount);
  103. }
  104. /**
  105. * @page service Service Calls
  106. * @section bx_system_general System Services
  107. * @subsection bx_system_general-content-objects Content Objects
  108. * @subsubsection bx_system_general_cnt-get_info Get content info
  109. *
  110. * @code bx_srv('system', 'get_info', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  111. * @code {{~system:get_info:TemplServiceContent["bx_posts", 123]~}} @endcode
  112. *
  113. * Get post info by post id(123):
  114. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/get_info/TemplServiceContent&params[]=bx_posts&params[]=123" @endcode
  115. * Get account info by account id(12):
  116. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/get_info/TemplServiceContent&params[]=sys_account&params[]=12" @endcode
  117. *
  118. * @param $sContentObject content object name
  119. * @param $iContentId content id
  120. * @param $bRawInfo if true - raw info return data directly from DB, if false - returns strutured array
  121. * @return content info array on success or empty array on error
  122. *
  123. * @see BxBaseServiceContent::serviceGetInfo
  124. */
  125. /**
  126. * @ref bx_system_general_cnt-get_info "Get content info"
  127. */
  128. public function serviceGetInfo ($sContentObject, $iContentId, $bRawInfo = false)
  129. {
  130. if ('sys_account' == $sContentObject) {
  131. $o = BxDolAccount::getInstance($iContentId);
  132. if (!$o)
  133. return false;
  134. return $o->getInfo();
  135. }
  136. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  137. if (!$o)
  138. return false;
  139. if ($bRawInfo)
  140. return $o->getContentInfo($iContentId, false);
  141. else
  142. return $o->getContentInfoAPI($iContentId, false);
  143. }
  144. /**
  145. * @page service Service Calls
  146. * @section bx_system_general System Services
  147. * @subsection bx_system_general-content-objects Content Objects
  148. * @subsubsection bx_system_general_cnt-get_link Get content link
  149. *
  150. * @code bx_srv('system', 'get_link', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  151. * @code {{~system:get_link:TemplServiceContent["bx_posts", 123]~}} @endcode
  152. *
  153. * Get post link by post id(123):
  154. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/get_link/TemplServiceContent&params[]=bx_posts&params[]=123" @endcode
  155. *
  156. * @param $sContentObject content object name
  157. * @param $iContentId content id
  158. * @return string with link on success or empty string on error
  159. *
  160. * @see BxBaseServiceContent::serviceGetLink
  161. */
  162. /**
  163. * @ref bx_system_general_cnt-get_link "Get content link"
  164. */
  165. public function serviceGetLink ($sContentObject, $iContentId)
  166. {
  167. if ('sys_account' == $sContentObject) {
  168. return false;
  169. }
  170. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  171. if (!$o)
  172. return false;
  173. return $o->getContentLink($iContentId);
  174. }
  175. /**
  176. * @page service Service Calls
  177. * @section bx_system_general System Services
  178. * @subsection bx_system_general-content-objects Content Objects
  179. * @subsubsection bx_system_general_cnt-delete Delete content
  180. *
  181. * @code bx_srv('system', 'delete', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  182. *
  183. * Delete post by post id(123):
  184. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/delete/TemplServiceContent&params[]=bx_posts&params[]=123" @endcode
  185. * Delete account with all its content by account id(12):
  186. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/delete/TemplServiceContent&params[]=sys_account&params[]=12" @endcode
  187. *
  188. * @param $sContentObject content object name
  189. * @param $iContentId content id
  190. * @param $aParams array of params:
  191. * - `with_content` - true(default) | false: for `sys_account`, `bx_persons`, `bx_organizations`;
  192. * - `force` - true | false(default): for `bx_persons`, `bx_organizations`;
  193. * - `scheduled` - true | false(default): for `sys_account`;
  194. * @return array with code = 0 on success, or array with code != 0 and error message
  195. *
  196. * @see BxBaseServiceContent::serviceDelete
  197. */
  198. /**
  199. * @ref bx_system_general_cnt-delete "Delete content"
  200. */
  201. public function serviceDelete ($sContentObject, $iContentId, $aParams = [])
  202. {
  203. if ('sys_account' == $sContentObject) {
  204. $o = BxDolAccount::getInstance($iContentId);
  205. if (!$o)
  206. return false;
  207. return $o->delete(isset($aParams['with_content']) ? $aParams['with_content'] : true, isset($aParams['scheduled']) ? $aParams['scheduled'] : false);
  208. }
  209. if (in_array($sContentObject, ['bx_persons', 'bx_organizations'])) {
  210. $o = BxDolProfile::getInstanceByContentAndType($iContentId, $sContentObject);
  211. if (!$o)
  212. return false;
  213. return $o->delete(false, isset($aParams['with_content']) ? $aParams['with_content'] : true, isset($aParams['force']) ? $aParams['force'] : false);
  214. }
  215. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  216. if (!$o)
  217. return false;
  218. if ($sErrorMsg = $o->deleteContent($iContentId))
  219. return ['code' => 500, 'error' => $sErrorMsg];
  220. else
  221. return ['code' => 0];
  222. }
  223. /**
  224. * @page service Service Calls
  225. * @section bx_system_general System Services
  226. * @subsection bx_system_general-content-objects Content Objects
  227. * @subsubsection bx_system_general_cnt-update Update content
  228. *
  229. * @code bx_srv('system', 'update', ["bx_posts", 123, ["title" => "new title"]], 'TemplServiceContent'); @endcode
  230. *
  231. * Update post text by post id(123):
  232. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/update/TemplServiceContent&params=%5B%22bx_posts%22%2C123%2C%7B%22text%22%3A%22new%20text%22%7D%5D" @endcode
  233. * Update account email by account id(4):
  234. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/update/TemplServiceContent&params=%5B%22sys_account%22%2C4%2C%7B%22email%22%3A%22new%40email.com%22%7D%5D" @endcode
  235. * To replace existing picture (for example profile avatar) delete previous picture first,
  236. * then set new one.
  237. *
  238. * @param $sContentObject content object name
  239. * @param $iContentId content id
  240. * @param $aValues key value pairs to update
  241. * @return array with code = 0 on success, or array with code != 0 and error message
  242. *
  243. * @see BxBaseServiceContent::serviceUpdate
  244. */
  245. /**
  246. * @ref bx_system_general_cnt-update "Update content"
  247. */
  248. public function serviceUpdate ($sContentObject, $iContentId, $aValues)
  249. {
  250. if ('sys_account' == $sContentObject) {
  251. $o = BxDolAccount::getInstance($iContentId);
  252. if (!$o)
  253. return false;
  254. $oQuery = BxDolAccountQuery::getInstance();
  255. foreach ($aValues as $k => $v) {
  256. if (!$oQuery->isFieldExists('sys_accounts', $k))
  257. return ['code' => 500, 'error' => _t('_sys_txt_forms_unknown_field_err', $k)];
  258. }
  259. foreach ($aValues as $k => $v) {
  260. if (!$oQuery->_updateField($o->id(), $k, $v))
  261. return ['code' => 500, 'error' => _t('_error occured')];
  262. }
  263. return true;
  264. }
  265. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  266. if (!$o)
  267. return false;
  268. if ($sErrorMsg = $o->updateContent($iContentId, $aValues))
  269. return ['code' => 500, 'error' => $sErrorMsg];
  270. else
  271. return ['code' => 0];
  272. }
  273. /**
  274. * @page service Service Calls
  275. * @section bx_system_general System Services
  276. * @subsection bx_system_general-content-objects Content Objects
  277. * @subsubsection bx_system_general_cnt-add Add content
  278. *
  279. * @code bx_srv('system', 'add', [$sContentObject, $aValues], 'TemplServiceContent'); @endcode
  280. *
  281. * Add new post with specified title, text and privacy, post authour is identified by memberSession cookie:
  282. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/add/TemplServiceContent&params=%5B%22bx_posts%22%2C%7B%22title%22%3A%22Post%20title%22%2C%22text%22%3A%22Some%20text%22%2C%22cat%22%3A2%2C%22allow_view_to%22%3A3%7D%5D" @endcode
  283. * Add new account with specified name, email and password, also mark email as confirmed:
  284. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/add/TemplServiceContent&params=%5B%22sys_account%22%2C%20%7B%22name%22%3A%22Vasya%22%2C%22email%22%3A%22vasya%40vasya.com%22%2C%22email_confirmed%22%3A%221%22%2C%22password%22%3A%221234%22%7D%5D" @endcode
  285. *
  286. * @param $sContentObject content object name
  287. * @param $aValues key value pairs to add
  288. * @return array with code = 0 on success, or array with code != 0 and error message
  289. *
  290. * @see BxBaseServiceContent::serviceAdd
  291. */
  292. /**
  293. * @ref bx_system_general_cnt-add "Add content"
  294. */
  295. public function serviceAdd ($sContentObject, $aValues)
  296. {
  297. if ('sys_account' == $sContentObject) {
  298. $o = new BxTemplAccountForms();
  299. $a = $o->createAccount ($aValues);
  300. if (isset($a['account_id']) && isset($aValues['email_confirmed'])) {
  301. $o = BxDolAccount::getInstance($a['account_id']);
  302. if ($o) {
  303. $o->updateEmailConfirmed($aValues['email_confirmed'], isset($aValues['auto_send_confrmation_email']) ? $aValues['auto_send_confrmation_email'] : false);
  304. }
  305. }
  306. if (isset($a['account_id']) && isset($aValues['phone_confirmed'])) {
  307. $o = BxDolAccount::getInstance($a['account_id']);
  308. if ($o) {
  309. $o->updatePhoneConfirmed($aValues['phone_confirmed']);
  310. }
  311. }
  312. return $a;
  313. }
  314. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  315. if (!$o)
  316. return false;
  317. $a = $o->addContent($aValues);
  318. if (isset($a['code']) && !$a['code'] && isset($a['content'])) {
  319. return $a;
  320. } else {
  321. $a['code'] = 500 + $a['code'];
  322. $a['error'] = $a['message'];
  323. $a['data'] = $a['errors'];
  324. return $a;
  325. }
  326. }
  327. /**
  328. * @page service Service Calls
  329. * @section bx_system_general System Services
  330. * @subsection bx_system_general-content-objects Content Objects
  331. * @subsubsection bx_system_general_cnt-upload_from_url Upload file from URL
  332. *
  333. * Upload file and associate with content:
  334. * @code bx_srv('system', 'upload_from_url', [$sContentObject, $sFileUrl, ['content_id' => 123]], 'TemplServiceContent'); @endcode
  335. *
  336. * Upload photo from `http://example.com/a.jpg` URL to `bx_persons_pictures` storage and associate
  337. * uploaded file to a person with content id (123), returned value is newly uploaded file id,
  338. * it can be used in content update API/Service call to set profile picture.
  339. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/upload_from_url/TemplServiceContent&params=%5B%22bx_persons_pictures%22%2C%22http%3A%2F%2Fexample.com%2Fa.jpg%22%2C%20%7B%22content_id%22%3A123%7D%5D" @endcode
  340. *
  341. * @param $sStorageObject storage object name
  342. * @param $sFileUrl URL to file to store in the storage
  343. * @param $aParams array of params, possible array keys:
  344. * - `private` - true | false: set file as private or not, if omitted file is uploaded as public
  345. * - `profile_id` - int: set owner of file to this user, of omitted, then currently logged in user is becoming file owner
  346. * - `content_id` - int: associate file with this content
  347. * @return uploaded file Id on success, or array with code != 0 and error message
  348. *
  349. * @see BxBaseServiceContent::serviceUploadFromUrl
  350. */
  351. /**
  352. * @ref bx_system_general_cnt-upload_from_url "Upload file from URL"
  353. */
  354. public function serviceUploadFromUrl ($sStorageObject, $sFileUrl, $aParams = [])
  355. {
  356. $oStorage = BxDolStorage::getObjectInstance($sStorageObject);
  357. if (!$oStorage)
  358. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  359. $iFileId = $oStorage->storeFileFromUrl($sFileUrl, isset($aParams['private']) && $aParams['private'] ? true : false, isset($aParams['profile_id']) ? $aParams['profile_id'] : 0, isset($aParams['content_id']) ? $aParams['content_id'] : 0);
  360. return $iFileId ? $iFileId : ['code' => $oStorage->getErrorCode(), 'error' => $oStorage->getErrorString()];
  361. }
  362. /**
  363. * @page service Service Calls
  364. * @section bx_system_general System Services
  365. * @subsection bx_system_general-content-objects Content Objects
  366. * @subsubsection bx_system_general_cnt-delete_file Delete file
  367. *
  368. * @code bx_srv('system', 'delete_file', ["bx_persons", 123], 'TemplServiceContent'); @endcode
  369. *
  370. * Delete file with id = 123 from `bx_persons_pictures` storage engine.
  371. * <b>Please note</b> that associated with this file id content need to be updated by setting
  372. * respective field to new file id or 0.
  373. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/upload_from_url/TemplServiceContent&params[]=bx_persons_pictures&params[]=123" @endcode
  374. *
  375. * @param $sStorageObject storage object name
  376. * @param $iFileId file id
  377. * @return true on success, or array with code != 0 and error message
  378. *
  379. * @see BxBaseServiceContent::serviceDeleteFile
  380. */
  381. /**
  382. * @ref bx_system_general_cnt-delete_file "Delete file"
  383. */
  384. public function serviceDeleteFile ($sStorageObject, $iFileId)
  385. {
  386. $oStorage = BxDolStorage::getObjectInstance($sStorageObject);
  387. if (!$oStorage)
  388. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  389. $b = $oStorage->deleteFile($iFileId);
  390. return $b ? $b : ['code' => $oStorage->getErrorCode(), 'error' => $oStorage->getErrorString()];
  391. }
  392. protected function getUserIds($oAccount)
  393. {
  394. $oProfile = BxDolProfile::getInstanceByAccount($oAccount->id());
  395. if (!$oProfile)
  396. return false;
  397. return [
  398. 'account_id' => $oProfile->getAccountId(),
  399. 'profile_id' => $oProfile->id(),
  400. 'content_id' => $oProfile->getContentId(),
  401. 'content_module' => $oProfile->getModule(),
  402. ];
  403. }
  404. }
  405. /** @} */