BxBaseServiceContent.php 17 KB

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