BxBaseServiceContent.php 18 KB

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