1
0

BxBaseServiceContent.php 18 KB

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