1
0

BxBaseServiceContent.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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($bAllSessions = false)
  72. {
  73. if (!isLogged())
  74. return false;
  75. if ($bAllSessions) {
  76. $o = new BxDolSessionQuery();
  77. $o->deleteByAccountId(getLoggedId());
  78. }
  79. else {
  80. bx_logout();
  81. }
  82. return true;
  83. }
  84. /**
  85. * @page service Service Calls
  86. * @section bx_system_general System Services
  87. * @subsection bx_system_general-content-objects Content Objects
  88. * @subsubsection bx_system_general_cnt-get_user_ids Get user's IDs
  89. *
  90. * Get user's account id, profile id and profile module content id by account id.
  91. *
  92. * Get user's IDs by user's account id(12):
  93. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/get_user_ids/TemplServiceContent&params[]=12" @endcode
  94. *
  95. * @param $iAccountId account id, or account email
  96. * @return array with user ids on success or false on error
  97. *
  98. * @see BxBaseServiceContent::serviceGetUserIds
  99. */
  100. /**
  101. * @ref bx_system_general_cnt-get_user_ids "Get user's IDs"
  102. */
  103. function serviceGetUserIds($iAccountId)
  104. {
  105. $oAccount = BxDolAccount::getInstance($iAccountId);
  106. if (!$oAccount)
  107. return false;
  108. return $this->getUserIds($oAccount);
  109. }
  110. /**
  111. * @page service Service Calls
  112. * @section bx_system_general System Services
  113. * @subsection bx_system_general-content-objects Content Objects
  114. * @subsubsection bx_system_general_cnt-get_info Get content info
  115. *
  116. * @code bx_srv('system', 'get_info', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  117. * @code {{~system:get_info:TemplServiceContent["bx_posts", 123]~}} @endcode
  118. *
  119. * Get post info by post id(123):
  120. * @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
  121. * Get account info by account id(12):
  122. * @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
  123. *
  124. * @param $sContentObject content object name
  125. * @param $iContentId content id
  126. * @param $bRawInfo if true - raw info return data directly from DB, if false - returns strutured array
  127. * @return content info array on success or empty array on error
  128. *
  129. * @see BxBaseServiceContent::serviceGetInfo
  130. */
  131. /**
  132. * @ref bx_system_general_cnt-get_info "Get content info"
  133. */
  134. public function serviceGetInfo ($sContentObject, $iContentId, $bRawInfo = false)
  135. {
  136. if ('sys_account' == $sContentObject) {
  137. $o = BxDolAccount::getInstance($iContentId);
  138. if (!$o)
  139. return false;
  140. return $o->getInfo();
  141. }
  142. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  143. if (!$o)
  144. return false;
  145. if ($bRawInfo)
  146. return $o->getContentInfo($iContentId, false);
  147. else
  148. return $o->getContentInfoAPI($iContentId, false);
  149. }
  150. /**
  151. * @page service Service Calls
  152. * @section bx_system_general System Services
  153. * @subsection bx_system_general-content-objects Content Objects
  154. * @subsubsection bx_system_general_cnt-get_link Get content link
  155. *
  156. * @code bx_srv('system', 'get_link', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  157. * @code {{~system:get_link:TemplServiceContent["bx_posts", 123]~}} @endcode
  158. *
  159. * Get post link by post id(123):
  160. * @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
  161. *
  162. * @param $sContentObject content object name
  163. * @param $iContentId content id
  164. * @return string with link on success or empty string on error
  165. *
  166. * @see BxBaseServiceContent::serviceGetLink
  167. */
  168. /**
  169. * @ref bx_system_general_cnt-get_link "Get content link"
  170. */
  171. public function serviceGetLink ($sContentObject, $iContentId)
  172. {
  173. if ('sys_account' == $sContentObject) {
  174. return false;
  175. }
  176. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  177. if (!$o)
  178. return false;
  179. return $o->getContentLink($iContentId);
  180. }
  181. /**
  182. * @page service Service Calls
  183. * @section bx_system_general System Services
  184. * @subsection bx_system_general-content-objects Content Objects
  185. * @subsubsection bx_system_general_cnt-delete Delete content
  186. *
  187. * @code bx_srv('system', 'delete', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  188. *
  189. * Delete post by post id(123):
  190. * @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
  191. * Delete account with all its content by account id(12):
  192. * @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
  193. *
  194. * @param $sContentObject content object name
  195. * @param $iContentId content id
  196. * @param $aParams array of params:
  197. * - `with_content` - true(default) | false: for `sys_account`, `bx_persons`, `bx_organizations`;
  198. * - `force` - true | false(default): for `bx_persons`, `bx_organizations`;
  199. * - `scheduled` - true | false(default): for `sys_account`;
  200. * @return array with code = 0 on success, or array with code != 0 and error message
  201. *
  202. * @see BxBaseServiceContent::serviceDelete
  203. */
  204. /**
  205. * @ref bx_system_general_cnt-delete "Delete content"
  206. */
  207. public function serviceDelete ($sContentObject, $iContentId, $aParams = [])
  208. {
  209. if ('sys_account' == $sContentObject) {
  210. $o = BxDolAccount::getInstance($iContentId);
  211. if (!$o)
  212. return false;
  213. return $o->delete(isset($aParams['with_content']) ? $aParams['with_content'] : true, isset($aParams['scheduled']) ? $aParams['scheduled'] : false);
  214. }
  215. if (in_array($sContentObject, ['bx_persons', 'bx_organizations'])) {
  216. $o = BxDolProfile::getInstanceByContentAndType($iContentId, $sContentObject);
  217. if (!$o)
  218. return false;
  219. return $o->delete(false, isset($aParams['with_content']) ? $aParams['with_content'] : true, isset($aParams['force']) ? $aParams['force'] : false);
  220. }
  221. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  222. if (!$o)
  223. return false;
  224. if ($sErrorMsg = $o->deleteContent($iContentId))
  225. return ['code' => 500, 'error' => $sErrorMsg];
  226. else
  227. return ['code' => 0];
  228. }
  229. /**
  230. * @page service Service Calls
  231. * @section bx_system_general System Services
  232. * @subsection bx_system_general-content-objects Content Objects
  233. * @subsubsection bx_system_general_cnt-update Update content
  234. *
  235. * @code bx_srv('system', 'update', ["bx_posts", 123, ["title" => "new title"]], 'TemplServiceContent'); @endcode
  236. *
  237. * Update post text by post id(123):
  238. * @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
  239. * Update account email by account id(4):
  240. * @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
  241. * To replace existing picture (for example profile avatar) delete previous picture first,
  242. * then set new one.
  243. *
  244. * @param $sContentObject content object name
  245. * @param $iContentId content id
  246. * @param $aValues key value pairs to update
  247. * @return array with code = 0 on success, or array with code != 0 and error message
  248. *
  249. * @see BxBaseServiceContent::serviceUpdate
  250. */
  251. /**
  252. * @ref bx_system_general_cnt-update "Update content"
  253. */
  254. public function serviceUpdate ($sContentObject, $iContentId, $aValues)
  255. {
  256. if ('sys_account' == $sContentObject) {
  257. $o = BxDolAccount::getInstance($iContentId);
  258. if (!$o)
  259. return false;
  260. $oQuery = BxDolAccountQuery::getInstance();
  261. foreach ($aValues as $k => $v) {
  262. if (!$oQuery->isFieldExists('sys_accounts', $k))
  263. return ['code' => 500, 'error' => _t('_sys_txt_forms_unknown_field_err', $k)];
  264. }
  265. foreach ($aValues as $k => $v) {
  266. if (!$oQuery->_updateField($o->id(), $k, $v))
  267. return ['code' => 500, 'error' => _t('_error occured')];
  268. }
  269. return true;
  270. }
  271. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  272. if (!$o)
  273. return false;
  274. if ($sErrorMsg = $o->updateContent($iContentId, $aValues))
  275. return ['code' => 500, 'error' => $sErrorMsg];
  276. else
  277. return ['code' => 0];
  278. }
  279. /**
  280. * @page service Service Calls
  281. * @section bx_system_general System Services
  282. * @subsection bx_system_general-content-objects Content Objects
  283. * @subsubsection bx_system_general_cnt-add Add content
  284. *
  285. * @code bx_srv('system', 'add', [$sContentObject, $aValues], 'TemplServiceContent'); @endcode
  286. *
  287. * Add new post with specified title, text and privacy, post authour is identified by memberSession cookie:
  288. * @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
  289. * Add new account with specified name, email and password, also mark email as confirmed:
  290. * @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
  291. *
  292. * @param $sContentObject content object name
  293. * @param $aValues key value pairs to add
  294. * @return array with code = 0 on success, or array with code != 0 and error message
  295. *
  296. * @see BxBaseServiceContent::serviceAdd
  297. */
  298. /**
  299. * @ref bx_system_general_cnt-add "Add content"
  300. */
  301. public function serviceAdd ($sContentObject, $aValues)
  302. {
  303. if ('sys_account' == $sContentObject) {
  304. $o = new BxTemplAccountForms();
  305. $a = $o->createAccount ($aValues);
  306. if (isset($a['account_id']) && isset($aValues['email_confirmed'])) {
  307. $o = BxDolAccount::getInstance($a['account_id']);
  308. if ($o) {
  309. $o->updateEmailConfirmed($aValues['email_confirmed'], isset($aValues['auto_send_confrmation_email']) ? $aValues['auto_send_confrmation_email'] : false);
  310. }
  311. }
  312. if (isset($a['account_id']) && isset($aValues['phone_confirmed'])) {
  313. $o = BxDolAccount::getInstance($a['account_id']);
  314. if ($o) {
  315. $o->updatePhoneConfirmed($aValues['phone_confirmed']);
  316. }
  317. }
  318. return $a;
  319. }
  320. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  321. if (!$o)
  322. return false;
  323. $a = $o->addContent($aValues);
  324. if (isset($a['code']) && !$a['code'] && isset($a['content'])) {
  325. return $a;
  326. } else {
  327. $a['code'] = 500 + $a['code'];
  328. $a['error'] = $a['message'];
  329. $a['data'] = $a['errors'];
  330. return $a;
  331. }
  332. }
  333. /**
  334. * @page service Service Calls
  335. * @section bx_system_general System Services
  336. * @subsection bx_system_general-content-objects Content Objects
  337. * @subsubsection bx_system_general_cnt-upload_from_url Upload file from URL
  338. *
  339. * Upload file and associate with content:
  340. * @code bx_srv('system', 'upload_from_url', [$sContentObject, $sFileUrl, ['content_id' => 123]], 'TemplServiceContent'); @endcode
  341. *
  342. * Upload photo from `http://example.com/a.jpg` URL to `bx_persons_pictures` storage and associate
  343. * uploaded file to a person with content id (123), returned value is newly uploaded file id,
  344. * it can be used in content update API/Service call to set profile picture.
  345. * @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
  346. *
  347. * @param $sStorageObject storage object name
  348. * @param $sFileUrl URL to file to store in the storage
  349. * @param $aParams array of params, possible array keys:
  350. * - `private` - true | false: set file as private or not, if omitted file is uploaded as public
  351. * - `profile_id` - int: set owner of file to this user, of omitted, then currently logged in user is becoming file owner
  352. * - `content_id` - int: associate file with this content
  353. * @return uploaded file Id on success, or array with code != 0 and error message
  354. *
  355. * @see BxBaseServiceContent::serviceUploadFromUrl
  356. */
  357. /**
  358. * @ref bx_system_general_cnt-upload_from_url "Upload file from URL"
  359. */
  360. public function serviceUploadFromUrl ($sStorageObject, $sFileUrl, $aParams = [])
  361. {
  362. $oStorage = BxDolStorage::getObjectInstance($sStorageObject);
  363. if (!$oStorage)
  364. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  365. $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);
  366. return $iFileId ? $iFileId : ['code' => $oStorage->getErrorCode(), 'error' => $oStorage->getErrorString()];
  367. }
  368. /**
  369. * @page service Service Calls
  370. * @section bx_system_general System Services
  371. * @subsection bx_system_general-content-objects Content Objects
  372. * @subsubsection bx_system_general_cnt-delete_file Delete file
  373. *
  374. * @code bx_srv('system', 'delete_file', ["bx_persons_pictures", 123], 'TemplServiceContent'); @endcode
  375. *
  376. * Delete file with id = 123 from `bx_persons_pictures` storage engine.
  377. * <b>Please note</b> that associated with this file id content need to be updated by setting
  378. * respective field to new file id or 0.
  379. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/delete_file/TemplServiceContent&params[]=bx_persons_pictures&params[]=123" @endcode
  380. *
  381. * @param $sStorageObject storage object name
  382. * @param $iFileId file id
  383. * @return true on success, or array with code != 0 and error message
  384. *
  385. * @see BxBaseServiceContent::serviceDeleteFile
  386. */
  387. /**
  388. * @ref bx_system_general_cnt-delete_file "Delete file"
  389. */
  390. public function serviceDeleteFile ($sStorageObject, $iFileId)
  391. {
  392. $oStorage = BxDolStorage::getObjectInstance($sStorageObject);
  393. if (!$oStorage)
  394. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  395. $b = $oStorage->deleteFile($iFileId);
  396. return $b ? $b : ['code' => $oStorage->getErrorCode(), 'error' => $oStorage->getErrorString()];
  397. }
  398. /**
  399. * @page service Service Calls
  400. * @section bx_system_general System Services
  401. * @subsection bx_system_general-content-objects Content Objects
  402. * @subsubsection bx_system_general_cnt-replace_file Replace file
  403. *
  404. * @code bx_srv('system', 'replace_file', ["bx_persons", 123, 'picture', 'http://a.me/i.png'], 'TemplServiceContent'); @endcode
  405. *
  406. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/replace_file/TemplServiceContent&params[]=bx_persons&params[]=123&params[]=picture&params[]=http://a.me/i.png" @endcode
  407. *
  408. * @param $sContentObject content object name
  409. * @param $iContentId content id
  410. * @param $sField field name
  411. * @param $sFileUrl file URL
  412. * @return true on success, or array with code != 0 and error message
  413. *
  414. * @see BxBaseServiceContent::serviceReplaceFile
  415. */
  416. /**
  417. * @ref bx_system_general_cnt-replace_file "Replace file"
  418. */
  419. public function serviceReplaceFile ($sContentObject, $iContentId, $sField, $sFileUrl)
  420. {
  421. // some checks
  422. if (!isLogged())
  423. return ['code' => 500, 'error' => _t('_sys_txt_not_found')];
  424. $a = $this->serviceGetInfo ($sContentObject, $iContentId, true);
  425. if (!$a)
  426. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  427. $oForm = bx_srv($sContentObject, 'get_object_form', ['edit']);
  428. if (!$oForm)
  429. return ['code' => 404, 'error' => _t('_sys_request_module_not_found_cpt')];
  430. if (!isset($oForm->aInputs[$sField]))
  431. return ['code' => 404, 'error' => 'Unknown field'];
  432. if (!isset($oForm->aInputs[$sField]['storage_object']))
  433. return ['code' => 404, 'error' => "'$sField' field isn't file field (storage object isn't found in the specified field)"];
  434. // delete previous file
  435. $sStorageObject = $oForm->aInputs[$sField]['storage_object'];
  436. if ($a[$sField]) {
  437. $a = $this->serviceDeleteFile($sStorageObject, $a[$sField]);
  438. if (is_array($a) && isset($a['code']) && $a['code'])
  439. return $a;
  440. }
  441. // upload new file
  442. $iFileId = $a = $this->serviceUploadFromUrl($sStorageObject, $sFileUrl, ['content_id' => $iContentId, 'profile_id' => bx_get_logged_profile_id()]);
  443. if (is_array($a) && isset($a['code']) && $a['code'])
  444. return $a;
  445. // assign uploaded file id to the field
  446. return $this->serviceUpdate($sContentObject, $iContentId, [$sField => $iFileId]);
  447. }
  448. protected function getUserIds($oAccount)
  449. {
  450. $oProfile = BxDolProfile::getInstanceByAccount($oAccount->id());
  451. if (!$oProfile)
  452. return false;
  453. return [
  454. 'account_id' => $oProfile->getAccountId(),
  455. 'profile_id' => $oProfile->id(),
  456. 'content_id' => $oProfile->getContentId(),
  457. 'content_module' => $oProfile->getModule(),
  458. ];
  459. }
  460. }
  461. /** @} */