concrete5 /web/concrete/core/models/file_list.php

Language PHP Lines 449
MD5 Hash 3d932c84da566ee3351f437d91357efa
Repository https://github.com/glockops/concrete5.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php defined('C5_EXECUTE') or die("Access Denied.");
/**
*
* An object that allows a filtered list of files to be returned.
* @package Files
*
*/
class Concrete5_Model_FileList extends DatabaseItemList { 

	protected $attributeFilters = array();
	protected $autoSortColumns = array('fvFilename', 'fvAuthorName','fvTitle', 'fDateAdded', 'fvDateAdded', 'fvSize');
	protected $itemsPerPage = 10;
	protected $attributeClass = 'FileAttributeKey';
	protected $permissionLevel = 'search_file_set';
	protected $filteredFileSetIDs = array();
	
	/* magic method for filtering by attributes. */
	public function __call($nm, $a) {
		if (substr($nm, 0, 8) == 'filterBy') {
			$txt = Loader::helper('text');
			$attrib = $txt->uncamelcase(substr($nm, 8));
			if (count($a) == 2) {
				$this->filterByAttribute($attrib, $a[0], $a[1]);
			} else {
				$this->filterByAttribute($attrib, $a[0]);
			}
		}			
	}

	/** 
	 * Filters by file extension
	 * @param mixed $extension
	 */
	public function filterByExtension($ext) {
		$this->filter('fv.fvExtension', $ext, '=');
	}

	/** 
	 * Filters by type of file
	 * @param mixed $type
	 */
	public function filterByType($type) {
		$this->filter('fv.fvType', $type, '=');
	}
	
	/** 
	 * Filters by "keywords" (which searches everything including filenames, title, tags, users who uploaded the file, tags)
	 */
	public function filterByKeywords($keywords) {
		$db = Loader::db();
		$keywordsExact = $db->quote($keywords);
		$qkeywords = $db->quote('%' . $keywords . '%');
		$keys = FileAttributeKey::getSearchableIndexedList();
		$attribsStr = '';
		foreach ($keys as $ak) {
			$cnt = $ak->getController();			
			$attribsStr.=' OR ' . $cnt->searchKeywords($keywords);
		}
		$this->filter(false, '(fvFilename like ' . $qkeywords . ' or fvDescription like ' . $qkeywords . ' or fvTitle like ' . $qkeywords . ' or fvTags like ' . $qkeywords . ' or u.uName = ' . $keywordsExact . $attribsStr . ')');
	}
	

	public function filterBySet($fs) {
		if ($fs != false) {
			$this->filteredFileSetIDs[] = intval($fs->getFileSetID());
		} else {
			// this is what we do when we are filtering by files in NO sets.
			$s1 = FileSet::getMySets();
			$sets = array();
			foreach($s1 as $fs) {
				$sets[] = $fs->getFileSetID();
			}
			if (count($sets) == 0) {
				return false;
			}
			$db = Loader::db();
			$setStr = implode(',', $sets);
			$this->addToQuery("left join FileSetFiles fsfex on fsfex.fID = f.fID");
			$this->filter(false, '(fsfex.fID is null or (select count(fID) from FileSetFiles where fID = fsfex.fID and fsID in (' . $setStr . ')) = 0)');
		}
	}

	public static function export($xml) {
		$fl = new FileList();
		$files = $fl->get();
		if (count($files) > 0) {
			$pkgs = $xml->addChild("files");
			foreach($files as $f) {
				$node = $pkgs->addChild('file');
				$node->addAttribute('filename', $f->getFileName());
			}
		}
	}

	public static function exportArchive($archive) {
		$fl = new FileList();
		$files = $fl->get();
		$filestring = '';
		$fh = Loader::helper('file');
		$filenames = array();
		$filename = $fh->getTemporaryDirectory() . '/' . $archive . '.zip';
		if (count($files) > 0) {
			try {
				if (class_exists('ZipArchive', false)) {
					$zip = new ZipArchive;
					$res = $zip->open($filename, ZipArchive::CREATE);
					if ($res === TRUE) {
						foreach($files as $f) {
							$file = $f->getPath();
							if (!in_array(basename($file), $filenames)) {
								$filenames[] = basename($file);
								$zip->addFile(addslashes($file), basename($file));
							}
						}
						$zip->close();
					} else {
						throw new Exception(t('Could not open with ZipArchive::CREATE'));
					}
				} else {
					$filestring = "'" . addslashes($filename) . "' ";
					foreach($files as $f) {
						$file = $f->getPath();
						if (!in_array(basename($file), $filenames)) {
							$filenames[] = basename($file);
							$filestring .= "'" . addslashes($file) . "' ";
						}
					}
					exec(DIR_FILES_BIN_ZIP . ' -j ' . $filestring);
				}
			} catch(Exception $e) {
				throw new Exception(t('Failed to create zip file as "%s": %s', $filename, $e->getMessage()));
			}
		}
	}
	
	protected function setupFileSetFilters() {	
		$fsIDs = array_unique($this->filteredFileSetIDs);
		$fsIDs = array_filter($fsIDs,'is_numeric');
		
		$db = Loader::db();
		$i = 0;
		$_fsIDs = array();
		if(is_array($fsIDs) && count($fsIDs)) {
			foreach($fsIDs as $fsID) {
				if($fsID > 0) {
					$_fsIDs[] = $fsID;
				}
			}
		}
		
		if (count($_fsIDs) > 1) {
			foreach($_fsIDs as $fsID) {
				if($fsID > 0) {
					if ($i == 0) {
						$this->addToQuery("left join FileSetFiles fsfl on fsfl.fID = f.fID");
					}
					$this->filter(false,'f.fID IN (SELECT DISTINCT fID FROM FileSetFiles WHERE fsID = '.$db->quote($fsID).')');
					$i++;
				}
			}
		} else if (count($_fsIDs) > 0) {
			$this->addToQuery("inner join FileSetFiles fsfl on fsfl.fID = f.fID");
			$this->filter('fsfl.fsID', $fsID);
			$i++;
		}
		
		// add FileSetFiles if we had a file set filter but
		// couldn't add it because it has been removed
		if ($i == 0 && count($this->filteredFileSetIDs)>0) {
			$this->addToQuery("inner join FileSetFiles fsfl on 1=2");
		}		
	}
	
	
	/** 
	 * Filters the file list by file size (in kilobytes)
	 */
	public function filterBySize($from, $to) {
		$this->filter('fv.fvSize', $from * 1024, '>=');
		$this->filter('fv.fvSize', $to * 1024, '<=');
	}
	
	/** 
	 * Filters by public date
	 * @param string $date
	 */
	public function filterByDateAdded($date, $comparison = '=') {
		$this->filter('f.fDateAdded', $date, $comparison);
	}
	
	public function filterByOriginalPageID($ocID) {
		$this->filter('f.ocID', $ocID);
	}
	
	/**
	 * filters a FileList by the uID of the approving User
	 * @param int $uID
	 * @return void
	 * @since 5.4.1.1+
	 */
	public function filterByApproverUID($uID) {
		$this->filter('fv.fvApproverUID', $uID);	
	}
	
	/**
	 * filters a FileList by the uID of the owning User
	 * @param int $uID
	 * @return void
	 * @since 5.4.1.1+
	*/
	public function filterByAuthorUID($uID) {
		$this->filter('fv.fvAuthorUID', $uID);	
	}
	
	public function setPermissionLevel($plevel) {
		$this->permissionLevel = $plevel;
	}
	
	/** 
	 * Filters by tag
	 * @param string $tag
	 */
	public function filterByTag($tag='') { 
		$db=Loader::db();  
		$this->filter(false, "( fv.fvTags like ".$db->qstr("%\n".$tag."\n%")."  )");
	}	
	
	protected function setBaseQuery() {
		$this->setQuery('SELECT DISTINCT f.fID, u.uName as fvAuthorName
		FROM Files f INNER JOIN FileVersions fv ON f.fID = fv.fID 
		LEFT JOIN Users u on u.uID = fv.fvAuthorUID
		');
	}

	protected function setupFilePermissions() {
		$u = new User();
		if ($this->permissionLevel == false || $u->isSuperUser()) {
			return false;
		}

		$accessEntities = $u->getUserAccessEntityObjects();
		foreach($accessEntities as $pae) {
			$peIDs[] = $pae->getAccessEntityID();
		}
		$db = Loader::db();
		// figure out which sets can read files in, not read files in, and read only my files in.
		$fsIDs = $db->GetCol('select fsID from FileSets where fsOverrideGlobalPermissions = 1');
		$viewableSets = array(-1);
		$nonviewableSets = array(-1);
		$myviewableSets = array(-1);

		$owpae = FileUploaderPermissionAccessEntity::getOrCreate();
		
		if (count($fsIDs) > 0) { 
			$pk = PermissionKey::getByHandle($this->permissionLevel);
			foreach($fsIDs as $fsID) {
				$fs = FileSet::getByID($fsID);
				$pk->setPermissionObject($fs);
				$list = $pk->getAccessListItems(PermissionKey::ACCESS_TYPE_ALL, $accessEntities);
				$list = PermissionDuration::filterByActive($list);
				if (count($list) > 0) { 
					foreach($list as $l) {
						$pae = $l->getAccessEntityObject();
						if ($pae->getAccessEntityID() == $owpae->getAccessEntityID()) {
							$myviewableSets[] = $fs->getFileSetID();
						} else {
							if ($l->getAccessType() == PermissionKey::ACCESS_TYPE_INCLUDE) {
								$viewableSets[] = $fs->getFileSetID();
							}
							if ($l->getAccessType() == PermissionKey::ACCESS_TYPE_EXCLUDE) {
								$nonviewableSets[] = $fs->getFileSetID();
							}
						}
					}
				} else {
					$nonviewableSets[] = $fs->getFileSetID();
				}
			}
		}

		$fs = FileSet::getGlobal();
		$fk = PermissionKey::getByHandle('search_file_set');
		$fk->setPermissionObject($fs);
		$accessEntities[] = $owpae;
		$list = $fk->getAccessListItems(PermissionKey::ACCESS_TYPE_ALL, $accessEntities);
		$list = PermissionDuration::filterByActive($list);
		foreach($list as $l) {
			$pae = $l->getAccessEntityObject();
			if ($pae->getAccessEntityID() == $owpae->getAccessEntityID()) {
				$valid = 'mine';
			} else {
				if ($l->getAccessType() == PermissionKey::ACCESS_TYPE_INCLUDE) {
					$valid = PermissionKey::ACCESS_TYPE_INCLUDE;
				}
				if ($l->getAccessType() == PermissionKey::ACCESS_TYPE_EXCLUDE) {
					$valid = PermissionKey::ACCESS_TYPE_EXCLUDE;
				}
			}
		}
		
		$uID = ($u->isRegistered()) ? $u->getUserID() : 0;
		// This excludes all files found in sets where I may only read mine, and I did not upload the file
		$this->filter(false, '(f.uID = ' . $uID . ' or (select count(fID) from FileSetFiles where FileSetFiles.fID = f.fID and fsID in (' . implode(',',$myviewableSets) . ')) = 0)');		
		
		if ($valid == 'mine') {
			// this means that we're only allowed to read files we've uploaded (unless, of course, those files are in previously covered sets)
			$this->filter(false, '(f.uID = ' . $uID . ' or (select count(fID) from FileSetFiles where FileSetFiles.fID = f.fID and fsID in (' . implode(',',$viewableSets) . ')) > 0)');		
		}
		
		// this excludes all file that are found in sets that I can't find
		$this->filter(false, '((select count(fID) from FileSetFiles where FileSetFiles.fID = f.fID and fsID in (' . implode(',', $nonviewableSets) . ')) = 0)');		
		
		$uID = ($u->isRegistered()) ? $u->getUserID() : 0;
		// This excludes all files found in sets where I may only read mine, and I did not upload the file
		$this->filter(false, '(f.uID = ' . $uID . ' or (select count(fID) from FileSetFiles where FileSetFiles.fID = f.fID and fsID in (' . implode(',',$myviewableSets) . ')) = 0)');		

		$db = Loader::db();
		$vpvPKID = $db->GetOne('select pkID from PermissionKeys where pkHandle = \'view_file\'');
		if ($this->permissionLevel == 'search_file_set') { 
			$vpPKID = $db->GetOne('select pkID from PermissionKeys where pkHandle = \'view_file_in_file_manager\'');
		} else {
			$vpPKID = $vpvPKID;
		}
		$pdIDs = $db->GetCol("select distinct pdID from FilePermissionAssignments fpa inner join PermissionAccessList pal on fpa.paID = pal.paID where pkID in (?, ?) and pdID > 0", array($vpPKID, $vpvPKID));
		$activePDIDs = array();
		if (count($pdIDs) > 0) {
			// then we iterate through all of them and find any that are active RIGHT NOW
			foreach($pdIDs as $pdID) {
				$pd = PermissionDuration::getByID($pdID);
				if ($pd->isActive()) {
					$activePDIDs[] = $pd->getPermissionDurationID();	
				}
			}
		}
		$activePDIDs[] = 0;
		
		// exclude files where its overridden but I don't have the ability to read		
		$this->filter(false, "(f.fOverrideSetPermissions = 0 or (select count(fID) from FilePermissionAssignments fpa inner join PermissionAccessList fpal on fpa.paID = fpal.paID where fpa.fID = f.fID and fpal.accessType = " . PermissionKey::ACCESS_TYPE_INCLUDE . " and fpal.pdID in (" . implode(',', $activePDIDs) . ") and fpal.peID in (" . implode(',', $peIDs) . ") and (if(fpal.peID = " . $owpae->getAccessEntityID() . " and f.uID <> " . $uID . ", false, true)) and (fpa.pkID = " . $vpPKID . ")) > 0)");

		
		// exclude detail files where read is excluded
		$this->filter(false, "f.fID not in (select ff.fID from Files ff inner join FilePermissionAssignments fpaExclude on ff.fID = fpaExclude.fID inner join PermissionAccessList palExclude on fpaExclude.paID = palExclude.paID where fOverrideSetPermissions = 1 and palExclude.accessType = " . PermissionKey::ACCESS_TYPE_EXCLUDE . " and palExclude.pdID in (" . implode(',', $activePDIDs) . ")
			and palExclude.peID in (" . implode(',', $peIDs) . ") and fpaExclude.pkID in (" . $vpPKID . "," . $vpvPKID . "))");		
	}
	
	
	/** 
	 * Returns an array of file objects based on current settings
	 */
	public function get($itemsToGet = 0, $offset = 0) {
		$files = array();
		Loader::model('file');
		$this->createQuery();
		$r = parent::get($itemsToGet, $offset);
		foreach($r as $row) {
			$f = File::getByID($row['fID']);			
			$files[] = $f;
		}
		return $files;
	}
	
	public function getTotal(){
		$files = array();
		Loader::model('file');
		$this->createQuery();
		return parent::getTotal();
	}
	
	//this was added because calling both getTotal() and get() was duplicating some of the query components
	protected function createQuery(){
		if(!$this->queryCreated){
			$this->setBaseQuery();
			$this->filter('fvIsApproved', 1);
			$this->setupAttributeFilters("left join FileSearchIndexAttributes on (fv.fID = FileSearchIndexAttributes.fID)");
			$this->setupFilePermissions();
			$this->setupFileSetFilters();
			$this->queryCreated=1;
		}
	}
	
	//$key can be handle or fak id
	public function sortByAttributeKey($key,$order='asc') {
		$this->sortBy($key, $order); // this is handled natively now
	}
	
	public function sortByFileSetDisplayOrder() {
		$this->sortByMultiple('fsDisplayOrder asc', 'fID asc');
	}
	
	public static function getExtensionList() {
		$db = Loader::db();
		$col = $db->GetCol('select distinct(trim(fvExtension)) as extension from FileVersions where fvIsApproved = 1 and fvExtension <> ""');
		return $col;
	}

	public static function getTypeList() {
		$db = Loader::db();
		$col = $db->GetCol('select distinct(trim(fvType)) as type from FileVersions where fvIsApproved = 1 and fvType <> 0');
		return $col;
	}

}

class Concrete5_Model_FileManagerDefaultColumnSet extends DatabaseItemListColumnSet {
	protected $attributeClass = 'FileAttributeKey';	
	
	public static function getFileDateAdded($f) {
		return date(DATE_APP_DASHBOARD_SEARCH_RESULTS_FILES, strtotime($f->getDateAdded()));
	}

	public static function getFileDateActivated($f) {
		$fv = $f->getVersion();
		return date(DATE_APP_DASHBOARD_SEARCH_RESULTS_FILES, strtotime($fv->getDateAdded()));
	}
	
	public function __construct() {
		$this->addColumn(new DatabaseItemListColumn('fvType', t('Type'), 'getType', false));
		$this->addColumn(new DatabaseItemListColumn('fvTitle', t('Title'), 'getTitle'));
		$this->addColumn(new DatabaseItemListColumn('fDateAdded', t('Added'), array('FileManagerDefaultColumnSet', 'getFileDateAdded')));
		$this->addColumn(new DatabaseItemListColumn('fvDateAdded', t('Active'), array('FileManagerDefaultColumnSet', 'getFileDateActivated')));
		$this->addColumn(new DatabaseItemListColumn('fvSize', t('Size'), 'getSize'));
		$title = $this->getColumnByKey('fDateAdded');
		$this->setDefaultSortColumn($title, 'desc');
	}
}

class Concrete5_Model_FileManagerAvailableColumnSet extends Concrete5_Model_FileManagerDefaultColumnSet {
	protected $attributeClass = 'FileAttributeKey';
	public function __construct() {
		parent::__construct();
		$this->addColumn(new DatabaseItemListColumn('fvAuthorName', t('Author'), 'getAuthorName'));
	}
}

class Concrete5_Model_FileManagerColumnSet extends DatabaseItemListColumnSet {
	protected $attributeClass = 'FileAttributeKey';
	public function getCurrent() {
		$u = new User();
		$fldc = $u->config('FILE_LIST_DEFAULT_COLUMNS');
		if ($fldc != '') {
			$fldc = @unserialize($fldc);
		}
		if (!($fldc instanceof DatabaseItemListColumnSet)) {
			$fldc = new FileManagerDefaultColumnSet();
		}
		return $fldc;
	}
}
Back to Top