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