BxBaseServiceContent.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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-modules_list Get modules list
  115. *
  116. * @code bx_srv('system', 'modules_list', [], 'TemplServiceContent'); @endcode
  117. * @code {{~system:modules_list:TemplServiceContent~}} @endcode
  118. *
  119. * Get modules list with content objects, content objects fields
  120. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/modules_list/TemplServiceContent" @endcode
  121. *
  122. * @param $bContentModulesOnly if true - return only modules with content objects, if false - return all modules
  123. * @return content objects info array
  124. *
  125. * @see BxBaseServiceContent::serviceModulesList
  126. */
  127. /**
  128. * @ref bx_system_general_cnt-modules_list "Get modules list"
  129. */
  130. public function serviceModulesList ($bContentModulesOnly = false)
  131. {
  132. $aAllowedKeys = ['title', 'type', 'uri', 'db_prefix'];
  133. $a = BxDolModuleQuery::getInstance()->getModules();
  134. $aModules = [];
  135. foreach ($a as $r) {
  136. if ($r['enabled'] != 1)
  137. continue;
  138. $isContentModule = 'system' == $r['name'] ? true : (BxDolContentInfo::getObjectInstance($r['name']) ? true : false);
  139. if ($bContentModulesOnly && !$isContentModule)
  140. continue;
  141. $aModules[$r['name']]['content_module'] = $isContentModule;
  142. foreach ($aAllowedKeys as $sKey) {
  143. if (isset($r[$sKey])) {
  144. $aModules[$r['name']][$sKey] = $r[$sKey];
  145. }
  146. }
  147. }
  148. return $aModules;
  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-modules_fields Get modules fields
  155. *
  156. * @code bx_srv('system', 'content_modules_fields', [], 'TemplServiceContent'); @endcode
  157. * @code {{~system:content_modules_fields:TemplServiceContent~}} @endcode
  158. *
  159. * Get mcontent odules fields
  160. * @code curl -s --cookie "memberSession=SESSIONIDHERE" -H "Authorization: Bearer APIKEYHERE" "http://example.com/api.php?r=system/content_modules_fields/TemplServiceContent" @endcode
  161. *
  162. * @param $sModule module name or 'all' for all modules
  163. * @param $bVerbose if true - return all fields info, if false - return only short list of fields info
  164. * @return content objects info array
  165. *
  166. * @see BxBaseServiceContent::serviceContentModulesFields
  167. */
  168. /**
  169. * @ref bx_system_general_cnt-modules_fields "Get content fields"
  170. */
  171. public function serviceContentModulesFields ($sModule = 'all', $bVerbose = false)
  172. {
  173. $a = BxDolContentInfo::getSystems();
  174. $a['system'] = [
  175. 'db_table' => 'sys_accounts',
  176. 'db_table_fields' => BxDolDb::getInstance()->getFields('sys_accounts')['original'],
  177. ];
  178. $a['system']['db_table_fields'][] = 'auto_send_confrmation_email';
  179. if ($sModule != 'all') {
  180. if (isset($a[$sModule])) {
  181. $a = [$sModule => $a[$sModule]];
  182. }
  183. else {
  184. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  185. }
  186. }
  187. foreach ($a as $sSystem => $r) {
  188. if ($sSystem == 'system') {
  189. $aModules[$sSystem] = $r;
  190. continue;
  191. }
  192. if ($r['alert_action_add'] == 'commentPost') {
  193. continue;
  194. }
  195. $oModule = BxDolModule::getInstance($sSystem);
  196. if (!$oModule || !$oModule->isEnabled())
  197. continue;
  198. $oFormsHelper = $oModule->getFormsHelper();
  199. $sTableEntries = $oModule->_oConfig->CNF['TABLE_ENTRIES'];
  200. $aFields = BxDolDb::getInstance()->getFields($sTableEntries);
  201. $aModules[$sSystem] = [
  202. 'db_table' => $sTableEntries,
  203. 'db_table_fields' => $aFields['original'],
  204. 'fields_add' => $this->_prepareShortFields($oFormsHelper->getObjectFormAdd()->aInputs, $bVerbose),
  205. 'fields_edit' => $this->_prepareShortFields($oFormsHelper->getObjectFormEdit()->aInputs, $bVerbose),
  206. ];
  207. }
  208. return $aModules;
  209. }
  210. protected function _prepareShortFields($aFields, $bVerbose)
  211. {
  212. if ($bVerbose)
  213. return $aFields;
  214. $aShortList = ['type', 'name', 'caption', 'info', 'required', 'value', 'values'];
  215. $aResult = [];
  216. foreach ($aFields as $sKey => $aField) {
  217. if ($aField['type'] == 'submit')
  218. continue;
  219. $aResult[$sKey] = [];
  220. foreach ($aShortList as $sShortKey) {
  221. if (isset($aField[$sShortKey]) && !empty($aField[$sShortKey])) {
  222. $aResult[$sKey][$sShortKey] = $aField[$sShortKey];
  223. }
  224. }
  225. }
  226. return $aResult;
  227. }
  228. /**
  229. * @page service Service Calls
  230. * @section bx_system_general System Services
  231. * @subsection bx_system_general-content-objects Content Objects
  232. * @subsubsection bx_system_general_cnt-get_info Get content info
  233. *
  234. * @code bx_srv('system', 'get_info', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  235. * @code {{~system:get_info:TemplServiceContent["bx_posts", 123]~}} @endcode
  236. *
  237. * Get post info by post id(123):
  238. * @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
  239. * Get account info by account id(12):
  240. * @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
  241. *
  242. * @param $sContentObject content object name
  243. * @param $iContentId content id
  244. * @param $bRawInfo if true - raw info return data directly from DB, if false - returns strutured array
  245. * @return content info array on success or empty array on error
  246. *
  247. * @see BxBaseServiceContent::serviceGetInfo
  248. */
  249. /**
  250. * @ref bx_system_general_cnt-get_info "Get content info"
  251. */
  252. public function serviceGetInfo ($sContentObject, $iContentId, $bRawInfo = false)
  253. {
  254. if ('sys_account' == $sContentObject || 'system' == $sContentObject) {
  255. $o = BxDolAccount::getInstance($iContentId);
  256. if (!$o)
  257. return false;
  258. return $o->getInfo();
  259. }
  260. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  261. if (!$o)
  262. return false;
  263. if ($bRawInfo)
  264. return $o->getContentInfo($iContentId, false);
  265. else
  266. return $o->getContentInfoAPI($iContentId, false);
  267. }
  268. /**
  269. * @page service Service Calls
  270. * @section bx_system_general System Services
  271. * @subsection bx_system_general-content-objects Content Objects
  272. * @subsubsection bx_system_general_cnt-get_link Get content link
  273. *
  274. * @code bx_srv('system', 'get_link', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  275. * @code {{~system:get_link:TemplServiceContent["bx_posts", 123]~}} @endcode
  276. *
  277. * Get post link by post id(123):
  278. * @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
  279. *
  280. * @param $sContentObject content object name
  281. * @param $iContentId content id
  282. * @return string with link on success or empty string on error
  283. *
  284. * @see BxBaseServiceContent::serviceGetLink
  285. */
  286. /**
  287. * @ref bx_system_general_cnt-get_link "Get content link"
  288. */
  289. public function serviceGetLink ($sContentObject, $iContentId)
  290. {
  291. if ('sys_account' == $sContentObject) {
  292. return false;
  293. }
  294. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  295. if (!$o)
  296. return false;
  297. return $o->getContentLink($iContentId);
  298. }
  299. /**
  300. * @page service Service Calls
  301. * @section bx_system_general System Services
  302. * @subsection bx_system_general-content-objects Content Objects
  303. * @subsubsection bx_system_general_cnt-delete Delete content
  304. *
  305. * @code bx_srv('system', 'delete', ["bx_posts", 123], 'TemplServiceContent'); @endcode
  306. *
  307. * Delete post by post id(123):
  308. * @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
  309. * Delete account with all its content by account id(12):
  310. * @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
  311. *
  312. * @param $sContentObject content object name
  313. * @param $iContentId content id
  314. * @param $aParams array of params:
  315. * - `with_content` - true(default) | false: for `sys_account`, `bx_persons`, `bx_organizations`;
  316. * - `force` - true | false(default): for `bx_persons`, `bx_organizations`;
  317. * - `scheduled` - true | false(default): for `sys_account`;
  318. * @return array with code = 0 on success, or array with code != 0 and error message
  319. *
  320. * @see BxBaseServiceContent::serviceDelete
  321. */
  322. /**
  323. * @ref bx_system_general_cnt-delete "Delete content"
  324. */
  325. public function serviceDelete ($sContentObject, $iContentId, $aParams = [])
  326. {
  327. if ('sys_account' == $sContentObject) {
  328. $o = BxDolAccount::getInstance($iContentId);
  329. if (!$o)
  330. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  331. return $o->delete(isset($aParams['with_content']) ? $aParams['with_content'] : true, isset($aParams['scheduled']) ? $aParams['scheduled'] : false);
  332. }
  333. if (in_array($sContentObject, ['bx_persons', 'bx_organizations'])) {
  334. $o = BxDolProfile::getInstanceByContentAndType($iContentId, $sContentObject);
  335. if (!$o)
  336. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  337. return $o->delete(false, isset($aParams['with_content']) ? $aParams['with_content'] : true, isset($aParams['force']) ? $aParams['force'] : false);
  338. }
  339. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  340. if (!$o)
  341. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  342. if ($sErrorMsg = $o->deleteContent($iContentId))
  343. return ['code' => 500, 'error' => $sErrorMsg];
  344. else
  345. return ['code' => 0];
  346. }
  347. /**
  348. * @page service Service Calls
  349. * @section bx_system_general System Services
  350. * @subsection bx_system_general-content-objects Content Objects
  351. * @subsubsection bx_system_general_cnt-update Update content
  352. *
  353. * @code bx_srv('system', 'update', ["bx_posts", 123, ["title" => "new title"]], 'TemplServiceContent'); @endcode
  354. *
  355. * Update post text by post id(123):
  356. * @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
  357. * Update account email by account id(4):
  358. * @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
  359. * To replace existing picture (for example profile avatar) delete previous picture first,
  360. * then set new one.
  361. *
  362. * @param $sContentObject content object name
  363. * @param $iContentId content id
  364. * @param $aValues key value pairs to update
  365. * @param $sDisplay form display name to use
  366. * @param $iProfileId profile ID to perform action on behalf of
  367. * @return array with code = 0 on success, or array with code != 0 and error message
  368. *
  369. * @see BxBaseServiceContent::serviceUpdate
  370. */
  371. /**
  372. * @ref bx_system_general_cnt-update "Update content"
  373. */
  374. public function serviceUpdate ($sContentObject, $iContentId, $aValues, $sDisplay = false, $iProfileId = 0)
  375. {
  376. if ($iProfileId) {
  377. $GLOBALS['glForceCurrentProfileId'] = $iProfileId;
  378. }
  379. $a = null;
  380. if ('sys_account' == $sContentObject) {
  381. $o = BxDolAccount::getInstance($iContentId);
  382. if (!$o) {
  383. $a = ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  384. }
  385. else {
  386. $oQuery = BxDolAccountQuery::getInstance();
  387. foreach ($aValues as $k => $v) {
  388. if (!$oQuery->isFieldExists('sys_accounts', $k)) {
  389. $a = ['code' => 500, 'error' => _t('_sys_txt_forms_unknown_field_err', $k)];
  390. break;
  391. }
  392. }
  393. if (!$a) {
  394. foreach ($aValues as $k => $v) {
  395. if (!$oQuery->_updateField($o->id(), $k, $v)) {
  396. $a = ['code' => 500, 'error' => _t('_error occured')];
  397. break;
  398. }
  399. }
  400. }
  401. if (!$a) {
  402. $a = ['code' => 0]; // success
  403. }
  404. }
  405. }
  406. else {
  407. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  408. if (!$o) {
  409. $a = ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  410. }
  411. else {
  412. if ($sErrorMsg = $o->updateContent($iContentId, $aValues, $sDisplay))
  413. $a = ['code' => 500, 'error' => $sErrorMsg];
  414. else
  415. $a = ['code' => 0]; // success
  416. }
  417. }
  418. if ($iProfileId) {
  419. $GLOBALS['glForceCurrentProfileId'] = 0;
  420. }
  421. return $a;
  422. }
  423. /**
  424. * @page service Service Calls
  425. * @section bx_system_general System Services
  426. * @subsection bx_system_general-content-objects Content Objects
  427. * @subsubsection bx_system_general_cnt-add Add content
  428. *
  429. * @code bx_srv('system', 'add', [$sContentObject, $aValues], 'TemplServiceContent'); @endcode
  430. *
  431. * Add new post with specified title, text and privacy, post authour is identified by memberSession cookie:
  432. * @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
  433. * Add new account with specified name, email and password, also mark email as confirmed:
  434. * @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
  435. *
  436. * @param $sContentObject content object name
  437. * @param $aValues key value pairs to add
  438. * @param $iProfileId profile ID to perform action on behalf of
  439. * @return array with code = 0 on success, or array with code != 0 and error message
  440. *
  441. * @see BxBaseServiceContent::serviceAdd
  442. */
  443. /**
  444. * @ref bx_system_general_cnt-add "Add content"
  445. */
  446. public function serviceAdd ($sContentObject, $aValues, $iProfileId = 0)
  447. {
  448. if ($iProfileId) {
  449. $GLOBALS['glForceCurrentProfileId'] = $iProfileId;
  450. }
  451. $a = [];
  452. if (bx_get_logged_profile_id() == 0) {
  453. $a = ['code' => 403, 'error' => 'Permission denied. Please login first.'];
  454. }
  455. elseif ('sys_account' == $sContentObject || 'system' == $sContentObject) {
  456. $o = new BxTemplAccountForms();
  457. $a = $o->createAccount ($aValues, BX_PROFILE_ACTION_EXTERNAL, false);
  458. if (isset($a['account_id']) && isset($aValues['email_confirmed'])) {
  459. $o = BxDolAccount::getInstance($a['account_id']);
  460. if ($o) {
  461. $o->updateEmailConfirmed($aValues['email_confirmed'], isset($aValues['auto_send_confrmation_email']) ? $aValues['auto_send_confrmation_email'] : false);
  462. }
  463. }
  464. if (isset($a['account_id']) && isset($aValues['phone_confirmed'])) {
  465. $o = BxDolAccount::getInstance($a['account_id']);
  466. if ($o) {
  467. $o->updatePhoneConfirmed($aValues['phone_confirmed']);
  468. }
  469. }
  470. }
  471. else {
  472. $o = BxDolContentInfo::getObjectInstance($sContentObject);
  473. if (!$o) {
  474. $a = ['code' => 404, 'error' => 'Content object(module) not found'];
  475. }
  476. else {
  477. $a = $o->addContent($aValues);
  478. if (!(isset($a['code']) && !$a['code'] && isset($a['content']))) {
  479. $a['code'] = 500 + $a['code'];
  480. $a['error'] = $a['message'];
  481. $a['data'] = $a['errors'];
  482. }
  483. }
  484. }
  485. if ($iProfileId) {
  486. $GLOBALS['glForceCurrentProfileId'] = 0;
  487. }
  488. return $a;
  489. }
  490. /**
  491. * @page service Service Calls
  492. * @section bx_system_general System Services
  493. * @subsection bx_system_general-content-objects Content Objects
  494. * @subsubsection bx_system_general_cnt-upload_from_url Upload file from URL
  495. *
  496. * Upload file and associate with content:
  497. * @code bx_srv('system', 'upload_from_url', [$sContentObject, $sFileUrl, ['content_id' => 123]], 'TemplServiceContent'); @endcode
  498. *
  499. * Upload photo from `http://example.com/a.jpg` URL to `bx_persons_pictures` storage and associate
  500. * uploaded file to a person with content id (123), returned value is newly uploaded file id,
  501. * it can be used in content update API/Service call to set profile picture.
  502. * @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
  503. *
  504. * @param $sStorageObject storage object name
  505. * @param $sFileUrl URL to file to store in the storage
  506. * @param $aParams array of params, possible array keys:
  507. * - `private` - true | false: set file as private or not, if omitted file is uploaded as public
  508. * - `profile_id` - int: set owner of file to this user, of omitted, then currently logged in user is becoming file owner
  509. * - `content_id` - int: associate file with this content
  510. * @return uploaded file Id on success, or array with code != 0 and error message
  511. *
  512. * @see BxBaseServiceContent::serviceUploadFromUrl
  513. */
  514. /**
  515. * @ref bx_system_general_cnt-upload_from_url "Upload file from URL"
  516. */
  517. public function serviceUploadFromUrl ($sStorageObject, $sFileUrl, $aParams = [])
  518. {
  519. $oStorage = BxDolStorage::getObjectInstance($sStorageObject);
  520. if (!$oStorage)
  521. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  522. $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);
  523. return $iFileId ? $iFileId : ['code' => $oStorage->getErrorCode(), 'error' => $oStorage->getErrorString()];
  524. }
  525. /**
  526. * @page service Service Calls
  527. * @section bx_system_general System Services
  528. * @subsection bx_system_general-content-objects Content Objects
  529. * @subsubsection bx_system_general_cnt-delete_file Delete file
  530. *
  531. * @code bx_srv('system', 'delete_file', ["bx_persons_pictures", 123], 'TemplServiceContent'); @endcode
  532. *
  533. * Delete file with id = 123 from `bx_persons_pictures` storage engine.
  534. * <b>Please note</b> that associated with this file id content need to be updated by setting
  535. * respective field to new file id or 0.
  536. * @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
  537. *
  538. * @param $sStorageObject storage object name
  539. * @param $iFileId file id
  540. * @return true on success, or array with code != 0 and error message
  541. *
  542. * @see BxBaseServiceContent::serviceDeleteFile
  543. */
  544. /**
  545. * @ref bx_system_general_cnt-delete_file "Delete file"
  546. */
  547. public function serviceDeleteFile ($sStorageObject, $iFileId)
  548. {
  549. $oStorage = BxDolStorage::getObjectInstance($sStorageObject);
  550. if (!$oStorage)
  551. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  552. $b = $oStorage->deleteFile($iFileId);
  553. return $b ? $b : ['code' => $oStorage->getErrorCode(), 'error' => $oStorage->getErrorString()];
  554. }
  555. /**
  556. * @page service Service Calls
  557. * @section bx_system_general System Services
  558. * @subsection bx_system_general-content-objects Content Objects
  559. * @subsubsection bx_system_general_cnt-replace_file Replace file
  560. *
  561. * @code bx_srv('system', 'replace_file', ["bx_persons", 123, 'picture', 'http://a.me/i.png'], 'TemplServiceContent'); @endcode
  562. *
  563. * @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
  564. *
  565. * @param $sContentObject content object name
  566. * @param $iContentId content id
  567. * @param $sField field name
  568. * @param $sFileUrl file URL
  569. * @return true on success, or array with code != 0 and error message
  570. *
  571. * @see BxBaseServiceContent::serviceReplaceFile
  572. */
  573. /**
  574. * @ref bx_system_general_cnt-replace_file "Replace file"
  575. */
  576. public function serviceReplaceFile ($sContentObject, $iContentId, $sField, $sFileUrl, $sDisplay = false)
  577. {
  578. // some checks
  579. if (!isLogged())
  580. return ['code' => 500, 'error' => _t('_sys_txt_not_found')];
  581. $a = $this->serviceGetInfo ($sContentObject, $iContentId, true);
  582. if (!$a)
  583. return ['code' => 404, 'error' => _t('_sys_txt_not_found')];
  584. $aParams = [];
  585. if ($sDisplay !== false)
  586. $aParams['display'] = $sDisplay;
  587. $oForm = bx_srv($sContentObject, 'get_object_form', ['edit', $aParams]);
  588. if (!$oForm)
  589. return ['code' => 404, 'error' => _t('_sys_request_module_not_found_cpt')];
  590. if (!isset($oForm->aInputs[$sField]))
  591. return ['code' => 404, 'error' => 'Unknown field'];
  592. if (!isset($oForm->aInputs[$sField]['storage_object']))
  593. return ['code' => 404, 'error' => "'$sField' field isn't file field (storage object isn't found in the specified field)"];
  594. // delete previous file
  595. $sStorageObject = $oForm->aInputs[$sField]['storage_object'];
  596. if ($a[$sField]) {
  597. $a = $this->serviceDeleteFile($sStorageObject, $a[$sField]);
  598. if (is_array($a) && isset($a['code']) && $a['code'])
  599. return $a;
  600. }
  601. // upload new file
  602. $iFileId = $a = $this->serviceUploadFromUrl($sStorageObject, $sFileUrl, ['content_id' => $iContentId, 'profile_id' => bx_get_logged_profile_id()]);
  603. if (is_array($a) && isset($a['code']) && $a['code'])
  604. return $a;
  605. // assign uploaded file id to the field
  606. return $this->serviceUpdate($sContentObject, $iContentId, [$sField => $iFileId], $sDisplay);
  607. }
  608. protected function getUserIds($oAccount)
  609. {
  610. $oProfile = BxDolProfile::getInstanceByAccount($oAccount->id());
  611. if (!$oProfile)
  612. return false;
  613. return [
  614. 'account_id' => $oProfile->getAccountId(),
  615. 'profile_id' => $oProfile->id(),
  616. 'content_id' => $oProfile->getContentId(),
  617. 'content_module' => $oProfile->getModule(),
  618. ];
  619. }
  620. }
  621. /** @} */