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