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