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