Parcourir la source

#978 correct image upload handling, get rid of side effects

Alex Trofimov il y a 9 ans
Parent
commit
5f5efb34fc

+ 7 - 34
modules/base/general/classes/BxBaseModGeneralFormEntry.php

@@ -105,11 +105,7 @@ class BxBaseModGeneralFormEntry extends BxTemplFormView
                 $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
         }
         
-        $iContentId = parent::insert ($aValsToAdd, $isIgnore);
-        if(!empty($iContentId) && isset($CNF['FIELD_PHOTO']))
-            $this->processFiles ($CNF['FIELD_PHOTO'], $iContentId, true);
-
-        return $iContentId;
+        return parent::insert ($aValsToAdd, $isIgnore);
     }
 
     function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
@@ -119,24 +115,19 @@ class BxBaseModGeneralFormEntry extends BxTemplFormView
         if (isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
             $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
         
-        if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb() && isset($CNF['FIELD_THUMB'])) {
+        if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($iContentId) && isset($CNF['FIELD_THUMB'])) {
             $aThumb = bx_process_input (bx_get($CNF['FIELD_THUMB']), BX_DATA_INT);
             $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
             if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
                 $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
         }
         
-        $iRet = parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
-
-        if (isset($CNF['FIELD_PHOTO']))
-            $this->processFiles ($CNF['FIELD_PHOTO'], $iContentId, false);
-
-        return $iRet;
+        return parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
     }
 
     protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
     {
-        $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_oModule->_isModerator());
+        $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
     }
 
     protected function _getPhotoGhostTmplVars($aContentInfo = array())
@@ -149,7 +140,7 @@ class BxBaseModGeneralFormEntry extends BxTemplFormView
 			'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
 			'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
 			'bx_if:set_thumb' => array (
-				'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb(),
+				'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($this->aInputs[$CNF['FIELD_PHOTO']]['content_id']),
 				'content' => array (
 					'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
     				'txt_pict_use_as_thumb' => _t(!empty($CNF['T']['txt_pict_use_as_thumb']) ? $CNF['T']['txt_pict_use_as_thumb'] : '_sys_txt_form_entry_input_picture_use_as_thumb')
@@ -229,7 +220,7 @@ class BxBaseModGeneralFormEntry extends BxTemplFormView
 
             if ($aFile['private'])
                 $oStorage->setFilePrivate ($aFile['id'], 1);
-            
+
             if ($iContentId)
                 $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
         }
@@ -287,26 +278,8 @@ class BxBaseModGeneralFormEntry extends BxTemplFormView
 
     function getContentOwnerProfileId ($iContentId)
     {
-        $CNF = &$this->_oModule->_oConfig->CNF;
-
-        // file owner must be author of the content or profile itself in case of profile based module
-        if ($iContentId) {
-            if ($this->_oModule instanceof iBxDolProfileService) {
-                $oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->MODULE);
-            }
-            else {
-                $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
-                $oProfile = $aContentInfo ? BxDolProfile::getInstance($aContentInfo[$CNF['FIELD_AUTHOR']]) : null;
-            }
-            $iProfileId = $oProfile ? $oProfile->id() : bx_get_logged_profile_id();
-        }
-        else {
-            $iProfileId = bx_get_logged_profile_id();
-        }
-
-        return $iProfileId;
+        return $this->_oModule->serviceGetContentOwnerProfileId($iContentId);
     }
-
 }
 
 /** @} */

+ 14 - 2
modules/base/general/classes/BxBaseModGeneralFormsEntryHelper.php

@@ -106,6 +106,10 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
         if($sResult)
             return array('code' => 4, 'message' => $sResult);
 
+        // process uploaded files
+        if (isset($CNF['FIELD_PHOTO']))
+            $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, true);
+        
         list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
         return array('code' => 0, 'message' => '', 'content' => $aContentInfo);
     }
@@ -150,6 +154,10 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
         if ($sResult)
             return $sResult;
 
+        // process uploaded files
+        if (isset($CNF['FIELD_PHOTO']))
+            $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, true);
+        
         // perform action
         $this->_oModule->checkAllowedAdd(true);
 
@@ -212,9 +220,13 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
         if ($sResult)
             return $sResult;
 
+        // process uploaded files
+        if (isset($CNF['FIELD_PHOTO']))
+            $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, false);
+
         // perform action
         $this->_oModule->checkAllowedEdit($aContentInfo, true);
-
+        
         // redirect
         $this->redirectAfterEdit($aContentInfo);
     }
@@ -364,7 +376,7 @@ class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
             $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
             if ($oMetatags->keywordsIsEnabled())
                 $oMetatags->keywordsAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT']);
-            if ($oMetatags->locationsIsEnabled())
+            if (isset($CNF['FIELD_LOCATION_PREFIX']) && isset($oForm->aInputs[$CNF['FIELD_LOCATION_PREFIX']]) && $oMetatags->locationsIsEnabled())
                 $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
         }
 

+ 24 - 1
modules/base/general/classes/BxBaseModGeneralModule.php

@@ -722,9 +722,32 @@ class BxBaseModGeneralModule extends BxDolModule
     {
         return $this->serviceCheckAllowedWithContent('comments_post', $iContentId);
     }
+
+    public function serviceGetContentOwnerProfileId ($iContentId)
+    {
+        $CNF = &$this->_oConfig->CNF;
+
+        // file owner must be author of the content or profile itself in case of profile based module
+        if ($iContentId) {
+            if ($this instanceof iBxDolProfileService) {
+                $oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName());
+            }
+            else {
+                $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
+                $oProfile = $aContentInfo ? BxDolProfile::getInstance($aContentInfo[$CNF['FIELD_AUTHOR']]) : null;
+            }
+            $iProfileId = $oProfile ? $oProfile->id() : bx_get_logged_profile_id();
+        }
+        else {
+            $iProfileId = bx_get_logged_profile_id();
+        }
+
+        return $iProfileId;
+    }
+    
     // ====== PERMISSION METHODS
 
-    public function checkAllowedSetThumb ()
+    public function checkAllowedSetThumb ($iContentId = 0)
     {
         return CHECK_ACTION_RESULT_ALLOWED;
     }

+ 3 - 2
modules/base/general/classes/BxBaseModGeneralTemplate.php

@@ -204,6 +204,7 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
 
         $oStorage = BxDolStorage::getObjectInstance($sStorage);
         $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
+        $oTranscoderPreview = isset($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) && $CNF['OBJECT_IMAGES_TRANSCODER_PICTURE'] ? BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) : null;
         $aTranscodersVideo = false;
 
         if (isset($CNF['OBJECT_VIDEOS_TRANSCODERS']) && $CNF['OBJECT_VIDEOS_TRANSCODERS'])
@@ -213,7 +214,7 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
                 'webm' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['webm']),
             );
 
-        $aGhostFiles = $oStorage->getGhosts ($aData[$CNF['FIELD_AUTHOR']], $aData[$CNF['FIELD_ID']]);
+        $aGhostFiles = $oStorage->getGhosts ($this->getModule()->serviceGetContentOwnerProfileId($aData[$CNF['FIELD_ID']]), $aData[$CNF['FIELD_ID']]);
         if (!$aGhostFiles)
             return false;
 
@@ -240,7 +241,7 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
             $a['bx_if:image'] = array (
                 'condition' => $isImage,
                 'content' => array (
-                    'url_original' => $sUrlOriginal,
+                    'url_original' => $isImage && $oTranscoderPreview ? $oTranscoderPreview->getFileUrl($a['id']) : $sUrlOriginal,
                     'attr_file_name' => bx_html_attribute($a['file_name']),
                     'popup_id' => $sImgPopupId,
                     'url_preview' => $isImage ? $oTranscoder->getFileUrl($a['id']) : '',

+ 0 - 9
modules/base/groups/classes/BxBaseModGroupsFormEntry.php

@@ -30,15 +30,6 @@ class BxBaseModGroupsFormEntry extends BxBaseModProfileFormEntry
         $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . "modules/?r=" . $this->_oModule->_oConfig->getUri() . "/ajax_get_initial_members";
         return $this->genCustomInputUsernamesSuggestions($aInput);
     }
-
-    protected function _isAdmin ($iContentId = 0)
-    {
-        if (parent::_isAdmin ($iContentId))
-            return true;
-        if (!$iContentId || !($aDataEntry = $this->_oModule->_oDb->getContentInfoById((int)$iContentId)))
-            return false;
-        return CHECK_ACTION_RESULT_ALLOWED == $this->_oModule->checkAllowedEdit ($aDataEntry);        
-    }
 }
 
 /** @} */

+ 10 - 4
modules/base/profile/classes/BxBaseModProfileFormEntry.php

@@ -164,10 +164,7 @@ class BxBaseModProfileFormEntry extends BxBaseModGeneralFormEntry
 
     protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
     {
-        $isAdmin = $this->_oModule->_isModerator();        
-        if (!$isAdmin && ($aDataEntry = $this->_oModule->_oDb->getContentInfoById((int)$iContentId)))
-            $isAdmin = CHECK_ACTION_RESULT_ALLOWED == $this->_oModule->checkAllowedEdit ($aDataEntry);
-        $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $isAdmin);
+        $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
         $this->_oModule->_oDb->updateContentPictureById($iContentId, 0/*$iProfileId*/, $iFileId, $sPictureField);
     }
 
@@ -184,6 +181,15 @@ class BxBaseModProfileFormEntry extends BxBaseModGeneralFormEntry
 			),
 		);
     }
+
+    protected function _isAdmin ($iContentId = 0)
+    {
+        if (parent::_isAdmin ($iContentId))
+            return true;
+        if (!$iContentId || !($aDataEntry = $this->_oModule->_oDb->getContentInfoById((int)$iContentId)))
+            return false;
+        return CHECK_ACTION_RESULT_ALLOWED == $this->_oModule->checkAllowedEdit ($aDataEntry);        
+    }
 }
 
 /** @} */

+ 1 - 1
modules/base/text/classes/BxBaseModTextModule.php

@@ -158,7 +158,7 @@ class BxBaseModTextModule extends BxBaseModGeneralModule implements iBxDolConten
     /**
      * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
      */
-    public function checkAllowedSetThumb ()
+    public function checkAllowedSetThumb ($iContentId = 0)
     {
         // check ACL
         $aCheck = checkActionModule($this->_iProfileId, 'set thumb', $this->getName(), false);

+ 1 - 1
modules/boonex/albums/classes/BxAlbumsModule.php

@@ -161,7 +161,7 @@ class BxAlbumsModule extends BxBaseModTextModule
         return $this->_oTemplate->entryMediaView ($iMediaId, $mixedContext);
     }
 
-    public function checkAllowedSetThumb ()
+    public function checkAllowedSetThumb ($iContentId = 0)
     {
         return CHECK_ACTION_RESULT_NOT_ALLOWED;
     }

+ 1 - 1
modules/boonex/convos/classes/BxCnvModule.php

@@ -333,7 +333,7 @@ class BxCnvModule extends BxBaseModTextModule
     /**
      * No thumbs for convos
      */
-    public function checkAllowedSetThumb ()
+    public function checkAllowedSetThumb ($iContentId = 0)
     {
         return _t('_sys_txt_access_denied');
     }

+ 1 - 1
modules/boonex/files/classes/BxFilesModule.php

@@ -67,7 +67,7 @@ class BxFilesModule extends BxBaseModTextModule
         return $oMenu ? $oMenu->getCode() : false;
     }
 
-    public function checkAllowedSetThumb ()
+    public function checkAllowedSetThumb ($iContentId = 0)
     {
         return _t('_sys_txt_access_denied');
     }

+ 1 - 1
modules/boonex/shopify/classes/BxShopifyModule.php

@@ -234,7 +234,7 @@ class BxShopifyModule extends BxBaseModTextModule
         return true;
     }
 
-    public function checkAllowedSetThumb()
+    public function checkAllowedSetThumb($iContentId = 0)
     {
         return _t('_sys_txt_access_denied');
     }