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:
53: if ($client <= 0) {
54: global $client;
55: }
56:
57:
58: $escClient = cSecurity::toInteger($client);
59: $escDirname = $this->escape($sDirname);
60: $escFilename = $this->escape($sFilename);
61:
62:
63:
64:
65:
66: $os = strtolower(getenv('OS'));
67: $isWindows = (false !== strpos($os, 'windows'));
68: $binary = $isWindows ? '' : 'BINARY';
69:
70: $this->select("idclient = $escClient AND dirname = $binary '$escDirname' AND filename = $binary '$escFilename'");
71:
72: if (false !== $oItem = $this->next()) {
73: $oItem->update();
74: } else {
75: $sFiletype = cFileHandler::getExtension($sDirname . $sFilename);
76: $iFilesize = cApiUpload::getFileSize($sDirname, $sFilename);
77: $oItem = $this->create($sDirname, $sFilename, $sFiletype, $iFilesize, '');
78: }
79:
80: return $oItem;
81: }
82:
83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96:
97: public function create($sDirname, $sFilename, $sFiletype = '', $iFileSize = 0,
98: $sDescription = '', $iStatus = 0) {
99: global $client, $cfg, $auth;
100:
101: $oItem = $this->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:
211: public function __construct($mId = false) {
212: global $cfg;
213: parent::__construct($cfg['tab']['upl'], 'idupl');
214: if ($mId !== false) {
215: $this->loadByPrimaryKey($mId);
216: }
217: }
218:
219: 220: 221:
222: public function update() {
223: $sDirname = $this->get('dirname');
224: $sFilename = $this->get('filename');
225: $sExtension = cFileHandler::getExtension($sDirname . $sFilename);
226: $iFileSize = self::getFileSize($sDirname, $sFilename);
227:
228: $bTouched = false;
229:
230: if ($this->get('filetype') != $sExtension) {
231: $this->set('filetype', $sExtension);
232: $bTouched = true;
233: }
234:
235: if ($this->get('size') != $iFileSize) {
236: $this->set('size', $iFileSize);
237: $bTouched = true;
238: }
239:
240: if ($bTouched == true) {
241: $this->store();
242: }
243: }
244:
245: 246: 247: 248: 249: 250: 251:
252: public function store() {
253: global $auth, $_cecRegistry;
254:
255: $this->set('modifiedby', $auth->auth['uid']);
256: $this->set('lastmodified', date('Y-m-d H:i:s'), false);
257:
258:
259: $_cecIterator = $_cecRegistry->getIterator('Contenido.Upl_edit.SaveRows');
260: if ($_cecIterator->count() > 0) {
261: while (($chainEntry = $_cecIterator->next()) !== false) {
262: $chainEntry->execute($this->get('idupl'), $this->get('dirname'), $this->get('filename'));
263: }
264: }
265:
266: return parent::store();
267: }
268:
269: 270: 271: 272: 273:
274: public function deletePropertiesByItemid($sItemid) {
275: $oPropertiesColl = $this->_getPropertiesCollectionInstance();
276: $oPropertiesColl->deleteProperties('upload', $sItemid);
277: }
278:
279: 280: 281: 282: 283: 284: 285:
286: public static function getFileSize($sDirname, $sFilename) {
287: global $client, $cfgClient;
288:
289: $bIsDbfs = cApiDbfs::isDbfs($sDirname);
290: if (!$bIsDbfs) {
291: $sDirname = $cfgClient[$client]['upl']['path'] . $sDirname;
292: }
293:
294: $sFilePathName = $sDirname . $sFilename;
295:
296: $iFileSize = 0;
297: if ($bIsDbfs) {
298: $oDbfsCol = new cApiDbfsCollection();
299: $iFileSize = $oDbfsCol->getSize($sFilePathName);
300: } elseif (cFileHandler::exists($sFilePathName)) {
301: $iFileSize = filesize($sFilePathName);
302: }
303:
304: return $iFileSize;
305: }
306:
307: 308: 309: 310: 311: 312:
313: protected function _getPropertiesCollectionInstanceX() {
314: global $client;
315:
316:
317: if (!is_object($this->_oPropertyCollection)) {
318: $this->_oPropertyCollection = new cApiPropertyCollection();
319: $this->_oPropertyCollection->changeClient($client);
320: }
321: return $this->_oPropertyCollection;
322: }
323: }
324: