Exemple #1
0
	private static function _Rotate(){
		$dir = new DirectoryLocal('logs/');
		$entries = $dir->ls('log');
		foreach($entries as $file){
			/** @var \Core\Filestore\Backends\FileLocal $file */

			$log = new LogFile($file->getBasename(true));
			try{
				$log->archive();
				echo 'Archived ' . $file->getBasename(true) . ' log.' . NL;
			}
			catch(\Exception $e){
				echo $e->getMessage();
				return false;
			}
		}

		if(\ConfigHandler::Get('/core/logs/rotate/keep')){
			$entries = $dir->ls();
			$types = [];
			foreach($entries as $file){
				/** @var \Core\Filestore\Backends\FileLocal $file */

				$base = $file->getBasename();
				// I want only the filename before the first '.'.
				$base = substr($base, 0, strpos($base, '.'));

				if(!isset($types[$base])){
					$types[$base] = [];
				}

				$types[$base][ $file->getMTime() ] = $file;
			}

			foreach($types as $base => $files){
				// This will sort the files newest-to-oldest.
				krsort($files);

				$limit = \ConfigHandler::Get('/core/logs/rotate/keep');
				$x = 0;
				$deleted = 0;
				foreach($files as $file){
					/** @var \Core\Filestore\Backends\FileLocal $file */
					++$x;
					if($x > $limit){
						if($file->exists()){
							$file->delete();
							++$deleted;
						}
					}
				}

				if($deleted > 0){
					echo 'Purged ' . $deleted . ' old ' . $base  . ' log file(s).' . NL;
				}
			}
		}

		return true;
	}
Exemple #2
0
/**
 * Resolve a name for an asset to an actual file.
 *
 * @param $filename
 *
 * @return \Core\Filestore\Directory
 *
 * @throws \Exception
 */
function resolve_asset_directory($filename){
	$resolved = get_asset_path();

	if (strpos($filename, 'assets/') === 0) {
		// Allow "assets/blah" to be passed in
		$filename = substr($filename, 7);
	}
	elseif(strpos($filename, 'asset/') === 0){
		// Allow "asset/blah" to be passed in.
		$filename = substr($filename, 6);
	}
	elseif(strpos($filename, $resolved) === 0){
		// Allow the fully resolved name to be passed in
		$filename = substr($filename, strlen($resolved));
	}

	//var_dump($filename);

	// I need to check the custom, current theme, and finally default locations for the file.
	$theme = \ConfigHandler::Get('/theme/selected');
	switch(CDN_TYPE){
		case 'local':
			if(\Core\ftp()){
				// FTP has its own sub-type.
				$custom  = new Backends\DirectoryFTP($resolved  . 'custom/' . $filename);
				$themed  = new Backends\DirectoryFTP($resolved  . $theme . '/' . $filename);
				$default = new Backends\DirectoryFTP($resolved  . 'default/' . $filename);
			}
			else{
				$custom  = new Backends\DirectoryLocal($resolved  . 'custom/' . $filename);
				$themed  = new Backends\DirectoryLocal($resolved  . $theme . '/' . $filename);
				$default = new Backends\DirectoryLocal($resolved  . 'default/' . $filename);
			}

			break;
		default:
			throw new \Exception('Unsupported CDN type: ' . CDN_TYPE);
			break;
	}

	if($custom->exists()){
		// If there is a custom asset installed, USE THAT FIRST!
		return $custom;
	}
	elseif($themed->exists()){
		// Otherwise, the themes can override component assets too.
		return $themed;
	}
	else{
		return $default;
	}
}