Cheetah
ChFilesUploader.php
Go to the documentation of this file.
1 <?php
2 
8 ch_import('ChWsbFilesUploader');
9 ch_import('ChWsbCategories');
10 ch_import('ChWsbAlbums');
11 ch_import('ChWsbModule');
12 
14 {
15  // constructor
16  function __construct()
17  {
18  parent::__construct('File');
19 
20  $this->oModule = ChWsbModule::getInstance('ChFilesModule');
21  $this->sWorkingFile = CH_WSB_URL_ROOT . $this->oModule->_oConfig->getBaseUri() . 'albums/my/add_objects';
22 
23  $iMaxByAdmin = 1024*1024*(int)getParam($this->oModule->_oConfig->getMainPrefix() . '_max_file_size');
24  if ($iMaxByAdmin > 0 && $iMaxByAdmin < $this->iMaxFilesize)
25  $this->iMaxFilesize = $iMaxByAdmin;
26  }
27 
28  function GenSendFileInfoForm($iFileID, $aDefaultValues = array())
29  {
30  $sFileUrl = "";
31  if(isset($aDefaultValues['image']))
32  $sFileUrl = $aDefaultValues['image'];
33  else if(!empty($iFileID)) {
34  $aFileInfo = ChWsbService::call('files', 'get_file_array', array($iFileID), 'Search');
35  $sFileUrl = $aFileInfo['file'];
36  }
37  $sProtoEl = '<img src="' . $sFileUrl . '" />';
38 
39  $aPossibleImage = array();
40  $aPossibleImage['preview_image'] = array(
41  'type' => 'custom',
42  'content' => $sProtoEl,
43  'caption' => _t('_ch_files_preview'),
44  );
45 
46  return $this->_GenSendFileInfoForm($iFileID, $aDefaultValues, $aPossibleImage, array());
47  }
48 
50  {
51  $iAuthorId = $this->_getAuthorId();
52  $sJSFileId = (int)$_POST['file_id'];
53  switch($_POST['type']) {
54  case 'upload':
55  default:
56  $iFileID = $sJSFileId;
57  break;
58  }
59 
60  if ($iFileID && $iAuthorId) {
61  $sTitle = $_POST['title'];
62  $sTags = $_POST['tags'];
63  $sDescription = $_POST['description'];
64  $iAllowDownload = (int)$_POST['AllowDownload'];
65 
66  $aCategories = array();
67  foreach ($_POST['Categories'] as $sVal) {
68  if ($sVal != '')
69  $aCategories[] = $sVal;
70  }
71  $sCategories = implode(CATEGORIES_DIVIDER, $aCategories);
72 
73  if ($this->initFile($iFileID, $sTitle, $sCategories, $sTags, $sDescription, array('AllowDownload' => $iAllowDownload))) {
74  $this->alertAdd($iFileID);
75  return '<script type="text/javascript">parent.' . $this->_sJsPostObject . '.onSuccessSendingFileInfo("' . $sJSFileId . '");</script>';
76  }
77  }
78  return '<script type="text/javascript">parent.' . $this->_sJsPostObject . '.showErrorMsg("file_failed_message");</script>';
79  }
80 
81  function servicePerformFileUpload ($sTmpFilename, $aFileInfo, $isUpdateThumb = '')
82  {
83  return $this->performFileUpload($sTmpFilename, $aFileInfo, false, $sTmpFilename);
84  }
85 
91  function performUpload ($sFilePath, $sRealFilename = '', $aInfo = array(), $isMoveUploadedFile = true, $aExtraParams = array())
92  {
93  $iOwner = $this->_getAuthorId();
94  if ($this->_iOwnerId)
95  $iOwner = $this->oModule->_iProfileId = $this->_iOwnerId;
96 
97  if (!$sRealFilename)
98  $sRealFilename = pathinfo($sFilePath, PATHINFO_BASENAME);
99 
100  // basic checking before upload
101 
102  if ($this->checkAuthorBeforeAdd())
103  return array('error' => _t('_LOGIN_REQUIRED_AE1'));
104 
105  if (!$this->oModule->_oConfig->checkAllowedExtsByFilename($sRealFilename))
106  return array('error' => _t('_sys_txt_wrong_file_extension'));
107 
108  if (!$this->oModule->isAllowedAdd())
109  return array('error' => _t('_Access denied'));
110 
111  $iFileSize = filesize($sFilePath);
112  if (!$iFileSize || $iFileSize > $this->iMaxFilesize)
113  return array('error' => _t('_' . $this->oModule->_oConfig->getMainPrefix() . '_size_error', _t_format_size($this->iMaxFilesize)));
114 
115  $this->sTempFilename = pathinfo($sRealFilename, PATHINFO_FILENAME);
116  if (!($iMID = $this->performFileUpload($sFilePath, array(), $isMoveUploadedFile, $sRealFilename, isset($aExtraParams['file_type']) ? $aExtraParams['file_type'] : '')))
117  return array('error' => _t('_sys_txt_upload_failed'));
118 
119  return array('id' => $iMID);
120  }
121 
122  function initFile($iMedID, $sTitle, $sCategories = '', $sTags = '', $sDesc = '', $aCustom = array())
123  {
124  $aCustom['Approved'] = getParam('ch_files_activation') == 'on' ? 'approved' : 'pending';
125  return parent::initFile($iMedID, $sTitle, $sCategories, $sTags, $sDesc, $aCustom);
126  }
127 
128  function performFileUpload($sTmpFile, $aFileInfo, $isMoveUploadedFile = true, $sOriginalFilename = '', $sFileType = '')
129  {
130  $iLastID = -1;
131 
132  // checker for flash uploader
133  if (!$this->oModule->_iProfileId)
134  $this->oModule->_iProfileId = $this->_iOwnerId;
135  if (! $this->_iOwnerId || file_exists($sTmpFile) == false || !$this->oModule->isAllowedAdd())
136  return false;
137 
138  $sMediaDir = $this->oModule->_oConfig->getFilesPath();
139 
140  if (! $sMediaDir) {
141  @unlink($sTmpFile);
142  return false;
143  }
144 
145  $sTempFileName = $sMediaDir . $this->_iOwnerId . '_temp';
146  @unlink($sTempFileName);
147 
148  if (($isMoveUploadedFile && is_uploaded_file($sTmpFile)) || !$isMoveUploadedFile) {
149 
150  if ($isMoveUploadedFile) {
151  move_uploaded_file($sTmpFile, $sTempFileName);
152  @unlink($sTmpFile);
153  } else {
154  $sTempFileName = $sTmpFile;
155  }
156 
157  @chmod($sTempFileName, 0666);
158  if (file_exists($sTempFileName)) {
159  $sOriginalFilenameSafe = process_db_input($sOriginalFilename, CH_TAGS_STRIP);
160  $sExtension = strrchr($sOriginalFilename, '.');
161  $iFileSize = filesize($sTempFileName);
162  $sFileSize = sprintf("%u", $iFileSize / 1024);
163  $sCurTime = time();
164 
165  if (is_array($aFileInfo) && count($aFileInfo) > 0) {
166  $aFileInfo['medSize'] = $iFileSize;
167  $iLastID = $this->insertSharedMediaToDb($sExtension, $aFileInfo, $this->_iOwnerId, array(
168  'AllowDownload' => (int)$aFileInfo['AllowDownload'],
169  'Type' => $aFileInfo['Type'],
170  ));
171  } else {
172  $aPassArray = array(
173  'medProfId' => $this->_iOwnerId,
174  'medTitle' => $sOriginalFilenameSafe,
175  'medDesc' => $sOriginalFilenameSafe,
176  'medExt' => trim($sExtension, '.'),
177  'medDate' => $sCurTime,
178  'medUri' => genRndPwd(8, false) . $sCurTime,
179  'medSize' => $iFileSize
180  );
181 
182  if (getParam('ch_files_activation') == 'on') {
183  $bAutoActivate = true;
184  $aPassArray['Approved'] = 'approved';
185  } else {
186  $bAutoActivate = false;
187  $aPassArray['Approved'] = 'pending';
188  }
189 
190  if ($sFileType)
191  $aPassArray['Type'] = process_db_input($sFileType, CH_TAGS_STRIP);
192  $iLastID = $this->oModule->_oDb->insertData($aPassArray);
193  $this->addObjectToAlbum($this->oModule->oAlbums, $_POST['extra_param_album'], $iLastID, $bAutoActivate);
194  $this->oModule->isAllowedAdd(true, true);
195  }
196 
197  $sFunc = ($isMoveUploadedFile) ? 'rename' : 'copy';
198  $sFilePostfix = '_' . sha1($sCurTime);
199  if (! $sFunc($sTempFileName, $sMediaDir . $iLastID . $sFilePostfix)) {
200  @unlink($sTempFileName);
201  return false;
202  }
203 
204  $this->sSendFileInfoFormCaption = $iLastID . $sExtension . " ({$sFileSize}kb)";
205 
206  $sFile = $sMediaDir . $iLastID . $sExtension;
207  }
208  }
209 
210  return $iLastID;
211  }
212 
213  function getUploadFormArray (&$aForm, $aAddObjects = array())
214  {
215  $aForm = parent::getUploadFormArray($aForm, $aAddObjects);
216  $aForm['AllowView'] = $this->oModule->oPrivacy->getGroupChooser($this->_iOwnerId, $this->oModule->_oConfig->getUri(), 'download');
217  return $aForm;
218  }
219 }
process_db_input
process_db_input($sText, $iStripTags=0)
Definition: utils.inc.php:256
ChWsbFilesUploader
Definition: ChWsbFilesUploader.php:13
ChWsbFilesUploader\addObjectToAlbum
addObjectToAlbum(&$oAlbums, $sAlbumUri, $iObjId, $bUpdateCounter=true, $iAuthorId=0, $aAlbumParams=array())
Definition: ChWsbFilesUploader.php:833
ChFilesUploader\performUpload
performUpload($sFilePath, $sRealFilename='', $aInfo=array(), $isMoveUploadedFile=true, $aExtraParams=array())
Definition: ChFilesUploader.php:91
ChFilesUploader
Definition: ChFilesUploader.php:14
ChWsbModule\getInstance
static getInstance($sClassName)
Definition: ChWsbModule.php:89
ChWsbFilesUploader\insertSharedMediaToDb
insertSharedMediaToDb($sExt, $aFileInfo, $iAuthorId=0, $aExtraData=array())
Definition: ChWsbFilesUploader.php:1040
ChWsbFilesUploader\checkAuthorBeforeAdd
checkAuthorBeforeAdd()
Definition: ChWsbFilesUploader.php:395
ch_import
ch_import($sClassName, $aModule=array())
Definition: utils.inc.php:1218
php
initFile
initFile($sId, $sTitle, $sCategory, $sTags, $sDesc)
Definition: functions.inc.php:79
ChFilesUploader\servicePerformFileUpload
servicePerformFileUpload($sTmpFilename, $aFileInfo, $isUpdateThumb='')
Definition: ChFilesUploader.php:81
_t_format_size
_t_format_size($iBytes, $iPrecision=2)
Definition: languages.inc.php:545
CATEGORIES_DIVIDER
const CATEGORIES_DIVIDER
Definition: ChWsbCategories.php:10
ChFilesUploader\__construct
__construct()
Definition: ChFilesUploader.php:16
$aInfo
$aInfo
Definition: constants.inc.php:21
ChWsbFilesUploader\_GenSendFileInfoForm
_GenSendFileInfoForm( $iFileID, $aDefaultValues=array(), $aPossibleImage=array(), $aPossibleDuration=array())
Definition: ChWsbFilesUploader.php:669
$sFile
$sFile
Definition: index.php:20
ChWsbFilesUploader\$_iOwnerId
$_iOwnerId
Definition: ChWsbFilesUploader.php:14
getParam
getParam($sParamName, $bUseCache=true)
Definition: db.inc.php:130
$sTitle
$sTitle
Definition: actions.inc.php:13
ChFilesUploader\getUploadFormArray
getUploadFormArray(&$aForm, $aAddObjects=array())
Definition: ChFilesUploader.php:213
$sTags
$sTags
Definition: actions.inc.php:12
genRndPwd
genRndPwd($iLength=8, $bSpecialCharacters=true)
Definition: utils.inc.php:1618
ChWsbFilesUploader\alertAdd
alertAdd($iMedID, $bCheckPrivacy=false)
Definition: ChWsbFilesUploader.php:972
_t
_t($key, $arg0="", $arg1="", $arg2="")
Definition: languages.inc.php:509
time
that in the case of a Adaptation or at a minimum such credit will if a credit for all contributing authors of the Adaptation or Collection then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors For the avoidance of You may only use the credit required by this Section for the purpose of attribution in the manner set out above by exercising Your rights under this You may not implicitly or explicitly assert or imply any connection sponsorship or endorsement by the Original Licensor and or Attribution as of You or Your use of the without the express prior written permission of the Original Licensor and or Attribution Parties Except as otherwise agreed in writing by the Licensor or as may be otherwise permitted by applicable if You Distribute or Publicly Perform the Work either by itself or as part of any Adaptations or You must not modify or take other derogatory action in relation to the Work which would be prejudicial to the Original Author s honor or reputation Licensor agrees that in those in which any exercise of the right granted in modification or other derogatory action prejudicial to the Original Author s honor and the Licensor will waive or not as this to the fullest extent permitted by the applicable national to enable You to reasonably exercise Your right under Warranties and Disclaimer UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN LICENSOR OFFERS THE WORK AS IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE STATUTORY OR WITHOUT WARRANTIES OF FITNESS FOR A PARTICULAR OR THE ABSENCE OF LATENT OR OTHER OR THE PRESENCE OF ABSENCE OF WHETHER OR NOT DISCOVERABLE SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED SO SUCH EXCLUSION MAY NOT APPLY TO YOU Limitation on Liability EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES Termination This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License Individuals or entities who have received Adaptations or Collections from You under this will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses and will survive any termination of this License Subject to the above terms and the license granted here is Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time
Definition: license.txt:56
ChFilesUploader\performFileUpload
performFileUpload($sTmpFile, $aFileInfo, $isMoveUploadedFile=true, $sOriginalFilename='', $sFileType='')
Definition: ChFilesUploader.php:128
ChFilesUploader\initFile
initFile($iMedID, $sTitle, $sCategories='', $sTags='', $sDesc='', $aCustom=array())
Definition: ChFilesUploader.php:122
CH_TAGS_STRIP
const CH_TAGS_STRIP
Definition: utils.inc.php:22
$aForm
$aForm
Definition: forgot.php:43
ChWsbFilesUploader\_getAuthorId
_getAuthorId()
Definition: ChWsbFilesUploader.php:102
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
ChWsbService\call
static call($mixed, $sMethod, $aParams=array(), $sClass='Module')
Definition: ChWsbService.php:32
$sDesc
$sDesc
Definition: actions.inc.php:21
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChFilesUploader\serviceAcceptFileInfo
serviceAcceptFileInfo()
Definition: ChFilesUploader.php:49
ChFilesUploader\GenSendFileInfoForm
GenSendFileInfoForm($iFileID, $aDefaultValues=array())
Definition: ChFilesUploader.php:28