BxFilesFormUpload.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 Files Files
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Files upload form
  13. */
  14. class BxFilesFormUpload extends BxBaseModTextFormEntry
  15. {
  16. public function __construct($aInfo, $oTemplate = false)
  17. {
  18. $this->MODULE = 'bx_files';
  19. parent::__construct($aInfo, $oTemplate);
  20. $this->_sGhostTemplate = 'form_ghost_template_upload.html';
  21. if (isset($this->aInputs['profile_id']))
  22. $this->aInputs['profile_id']['value'] = bx_get('profile_id');
  23. $CNF = &$this->_oModule->_oConfig->CNF;
  24. if (isset($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']]) && $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) {
  25. $oProfile = bx_get('profile_id') ? BxDolProfile::getInstance(bx_get('profile_id')) : false;
  26. $oPrivacy->setCustomPrivacy(bx_get('profile_id') && $oProfile && BxDolService::call($oProfile->getModule(), 'is_group_profile') ? true : false);
  27. $aSave = array('db' => array('pass' => 'Xss'));
  28. array_walk($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']], function ($a, $k, $aSave) {
  29. if (in_array($k, array('info', 'caption', 'value')))
  30. $aSave[0][$k] = $a;
  31. }, array(&$aSave));
  32. $aGroupChooser = $oPrivacy->getGroupChooserCustom($CNF['OBJECT_PRIVACY_VIEW'], $oPrivacy->isCustomPrivacy());
  33. $this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']] = array_merge($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']], $aGroupChooser, $aSave);
  34. }
  35. }
  36. public function insert ($aValsToAdd = array(), $isIgnore = false)
  37. {
  38. $CNF = &$this->_oModule->_oConfig->CNF;
  39. return $this->processFiles ($CNF['FIELD_PHOTO'], 0, true);
  40. }
  41. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  42. {
  43. $CNF = &$this->_oModule->_oConfig->CNF;
  44. if (!isset($this->aInputs[$sFieldFile]))
  45. return true;
  46. $mixedFileIds = $this->getCleanValue($sFieldFile);
  47. if(!$mixedFileIds)
  48. return true;
  49. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  50. if (!$oStorage)
  51. return false;
  52. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  53. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId, true);
  54. if (!$aGhostFiles)
  55. return true;
  56. bx_import('BxDolPrivacy');
  57. // get values form main form to pass it to each file later
  58. $aFormValues = array();
  59. foreach ($this->aInputs as $aInput) {
  60. if ($aInput['name'] && isset($aInput['value']) && $aInput['value'] && !is_array($aInput['value']) && $aInput['name'] != 'do_submit' && $aInput['name'] != $CNF['FIELD_PHOTO'] && $aInput['name'] != 'profile_id')
  61. $aFormValues[$aInput['name']] = $aInput['value'];//$this->getCleanValue($aInput['name']);
  62. }
  63. $aContentIds = array();
  64. foreach ($aGhostFiles as $aFile) {
  65. if (is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  66. continue;
  67. $iContentId = 0;
  68. if ($isAssociateWithContent)
  69. $iContentId = BxBaseModGeneralFormEntry::insert (array_merge(array(
  70. $CNF['FIELD_FILE_ID'] => $aFile['id'],
  71. $CNF['FIELD_TITLE'] => $this->getCleanValue('title-' . $aFile['id']),
  72. $CNF['FIELD_AUTHOR'] => bx_get('profile_id') && $this->_oModule->serviceIsAllowedAddContentToProfile(bx_get('profile_id')) ? bx_get('profile_id') : '',
  73. ), $aFormValues));
  74. if (!$iContentId)
  75. continue;
  76. $aContentIds[] = $iContentId;
  77. if ($aFile['private'] || (isset($aFormValues[$CNF['FIELD_ALLOW_VIEW_TO']]) && BX_DOL_PG_ALL !== $aFormValues[$CNF['FIELD_ALLOW_VIEW_TO']]))
  78. $oStorage->setFilePrivate ($aFile['id'], true);
  79. if ($iContentId)
  80. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  81. }
  82. return $aContentIds;
  83. }
  84. }
  85. /** @} */