1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
18:
19: 20: 21: 22: 23: 24:
25: class cApiUploadCollection extends ItemCollection {
26:
27: 28: 29: 30: 31:
32: public function __construct() {
33: global $cfg;
34: parent::__construct($cfg['tab']['upl'], 'idupl');
35: $this->_setItemClass('cApiUpload');
36:
37:
38: $this->_setJoinPartner('cApiClientCollection');
39: }
40:
41: 42: 43: 44: 45: 46: 47: 48: 49:
50: public function sync($sDirname, $sFilename, $client = 0) {
51: $client = cSecurity::toInteger($client);
52: if ($client <= 0) {
53: global $client;
54: }
55:
56: $sDirname = $this->escape($sDirname);
57: $sFilename = $this->escape($sFilename);
58: if (strstr(strtolower($_ENV['OS']), 'windows') === false) {
59:
60:
61: $this->select("dirname = BINARY '$sDirname' AND filename = BINARY '$sFilename' AND idclient = " . (int) $client);
62: } else {
63:
64:
65: $this->select("dirname = '$sDirname' AND filename = '$sFilename' AND idclient = " . (int) $client);
66: }
67:
68: if (($oItem = $this->next()) !== false) {
69: $oItem->update();
70: } else {
71: $sFiletype = (string) uplGetFileExtension($sFilename);
72: $iFilesize = cApiUpload::getFileSize($sDirname, $sFilename);
73: $oItem = $this->create($sDirname, $sFilename, $sFiletype, $iFilesize, '');
74: }
75:
76: return $oItem;
77: }
78:
79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92:
93: public function create($sDirname, $sFilename, $sFiletype = '', $iFileSize = 0, $sDescription = '', $iStatus = 0) {
94: global $client, $cfg, $auth;
95:
96: $oItem = parent::createNewItem();
97:
98: $oItem->set('idclient', $client);
99: $oItem->set('filename', $sFilename, false);
100: $oItem->set('filetype', $sFiletype, false);
101: $oItem->set('size', $iFileSize, false);
102: $oItem->set('dirname', $sDirname, false);
103:
104: $oItem->set('status', $iStatus, false);
105: $oItem->set('author', $auth->auth['uid']);
106: $oItem->set('created', date('Y-m-d H:i:s'), false);
107: $oItem->store();
108:
109: return $oItem;
110: }
111:
112: 113: 114: 115: 116: 117: 118: 119: 120: 121:
122: public function delete($id) {
123: global $cfgClient, $client;
124:
125: $oUpload = new cApiUpload();
126: $oUpload->loadByPrimaryKey($id);
127:
128: $sDirFileName = $oUpload->get('dirname') . $oUpload->get('filename');
129:
130:
131: $_cecIterator = cRegistry::getCecRegistry()->getIterator('Contenido.Upl_edit.Delete');
132: if ($_cecIterator->count() > 0) {
133: while (($chainEntry = $_cecIterator->next()) !== false) {
134: $chainEntry->execute($oUpload->get('idupl'), $oUpload->get('dirname'), $oUpload->get('filename'));
135: }
136: }
137:
138:
139: if (cApiDbfs::isDbfs($sDirFileName)) {
140: $oDbfs = new cApiDbfsCollection();
141: $oDbfs->remove($sDirFileName);
142: } elseif (cFileHandler::exists($cfgClient[$client]['upl']['path'] . $sDirFileName)) {
143: unlink($cfgClient[$client]['upl']['path'] . $sDirFileName);
144: }
145:
146:
147:
148:
149: $oUpload->deletePropertiesByItemid($sDirFileName);
150:
151: $this->deleteUploadMetaData($id);
152:
153:
154: return parent::delete($id);
155: }
156:
157: 158: 159: 160: 161: 162: 163: 164: 165:
166: protected function deleteUploadMetaData($idupl) {
167: global $client, $db, $cfg;
168: $sql = "DELETE FROM `%s` WHERE %s = '%s'";
169: return $db->query($sql, $cfg['tab']['upl_meta'], 'idupl', (int) $idupl);
170: }
171:
172: 173: 174: 175: 176: 177:
178: public function deleteByDirname($sDirname) {
179: global $client;
180:
181: $this->select("dirname = '" . $this->escape($sDirname) . "' AND idclient = " . (int) $client);
182: while (($oUpload = $this->next()) !== false) {
183: $this->delete($oUpload->get('idupl'));
184: }
185: }
186:
187: }
188:
189: 190: 191: 192: 193: 194:
195: class cApiUpload extends Item {
196:
197: 198: 199: 200: 201:
202: protected $_oPropertyCollection;
203:
204: 205: 206: 207: 208:
209: public function __construct($mId = false) {
210: global $cfg;
211: parent::__construct($cfg['tab']['upl'], 'idupl');
212: if ($mId !== false) {
213: $this->loadByPrimaryKey($mId);
214: }
215: }
216:
217: 218: 219:
220: public function update() {
221: $sDirname = $this->get('dirname');
222: $sFilename = $this->get('filename');
223: $sExtension = (string) uplGetFileExtension($sFilename);
224: $iFileSize = self::getFileSize($sDirname, $sFilename);
225:
226: $bTouched = false;
227:
228: if ($this->get('filetype') != $sExtension) {
229: $this->set('filetype', $sExtension);
230: $bTouched = true;
231: }
232:
233: if ($this->get('size') != $iFileSize) {
234: $this->set('size', $iFileSize);
235: $bTouched = true;
236: }
237:
238: if ($bTouched == true) {
239: $this->store();
240: }
241: }
242:
243: 244: 245: 246: 247: 248: 249:
250: public function store() {
251: global $auth, $_cecRegistry;
252:
253: $this->set('modifiedby', $auth->auth['uid']);
254: $this->set('lastmodified', date('Y-m-d H:i:s'), false);
255:
256:
257: $_cecIterator = $_cecRegistry->getIterator('Contenido.Upl_edit.SaveRows');
258: if ($_cecIterator->count() > 0) {
259: while (($chainEntry = $_cecIterator->next()) !== false) {
260: $chainEntry->execute($this->get('idupl'), $this->get('dirname'), $this->get('filename'));
261: }
262: }
263:
264: return parent::store();
265: }
266:
267: 268: 269: 270: 271:
272: public function deletePropertiesByItemid($sItemid) {
273: $oPropertiesColl = $this->_getPropertiesCollectionInstance();
274: $oPropertiesColl->deleteProperties('upload', $sItemid);
275: }
276:
277: 278: 279: 280: 281: 282: 283:
284: public static function getFileSize($sDirname, $sFilename) {
285: global $client, $cfgClient;
286:
287: $bIsDbfs = cApiDbfs::isDbfs($sDirname);
288: if (!$bIsDbfs) {
289: $sDirname = $cfgClient[$client]['upl']['path'] . $sDirname;
290: }
291:
292: $sFilePathName = $sDirname . $sFilename;
293:
294: $iFileSize = 0;
295: if ($bIsDbfs) {
296: $oDbfsCol = new cApiDbfsCollection();
297: $iFileSize = $oDbfsCol->getSize($sFilePathName);
298: } elseif (cFileHandler::exists($sFilePathName)) {
299: $iFileSize = filesize($sFilePathName);
300: }
301:
302: return $iFileSize;
303: }
304:
305: 306: 307: 308: 309: 310:
311: protected function _getPropertiesCollectionInstanceX() {
312: global $client;
313:
314:
315: if (!is_object($this->_oPropertyCollection)) {
316: $this->_oPropertyCollection = new cApiPropertyCollection();
317: $this->_oPropertyCollection->changeClient($client);
318: }
319: return $this->_oPropertyCollection;
320: }
321:
322: }
323: