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