aboutsummaryrefslogtreecommitdiff
path: root/assets/css
AgeCommit message (Collapse)Author
2018-12-13templateoptionsvincent-peugnet
2018-12-10media-cleanvincent-peugnet
2018-12-10media-uploadvincent-peugnet
2018-12-09little-improvementsvincent-peugnet
2018-12-09auto-media-dir-listvincent-peugnet
2018-12-09media parservincent-peugnet
2018-12-05manualvincent-peugnet
2018-12-05mediastylevincent-peugnet
2018-12-04showeditmenuvincent-peugnet
2018-12-04editor right barvincent-peugnet
2018-12-04admin panelvincent-peugnet
2018-11-29fontsvincent-peugnet
2018-11-28force render buttonvincent-peugnet
2018-11-28minor changesvincent-peugnet
2018-11-14article-rendervincent-peugnet
2018-11-12corner-menuvincent-peugnet
2018-11-12url-cleaning-redirect-correct-idvincent-peugnet
2018-11-11cleanvincent-peugnet
2018-11-11cleaningvincent-peugnet
2018-11-11reboot foldervincent-peugnet
n210' href='#n210'>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
<?php

namespace Wcms;

use DateTime;
use DateTimeImmutable;
use DateTimeZone;

class Media extends Item
{
	Protected $id;
	Protected $path;
	Protected $extension;
	Protected $type;
	Protected $size;
	Protected $date;
	Protected $width;
	Protected $height;
	Protected $length;
	Protected $uid;
	Protected $permissions;

	const IMAGE = array('jpg', 'jpeg', 'gif', 'png');
	const SOUND = array('mp3', 'flac', 'wav', 'ogg');
	const VIDEO = array('mp4', 'mov', 'avi', 'mkv');
	const ARCHIVE = array('zip', 'rar');



// _____________________________________________________ F U N ____________________________________________________

	public function __construct(array $donnees)
	{
		$this->hydrate($donnees);
	}

	public function analyse()
	{
		$this->settype();

		$this->setdate();

		$filepath = $this->path . $this->id . '.' . $this->extension;

		if ($this->type == 'image') {
			list($width, $height, $type, $attr) = getimagesize($filepath);
			$this->width = $width;
			$this->height = $height;
		}

		$stat = stat($filepath);

		$permissions = decoct(fileperms($filepath) & 0777);

		$this->setpermissions($permissions);

		$this->hydrate($stat);


	}


	public function getfullpath()
	{
		if(!empty(Config::basepath())) {
			$base = '/' . Config::basepath();
		} else {
			$base = '';
		}
		$fullpath = $base . '/'. $this->path() . $this->id() . '.' . $this->extension();
		$fullpath = str_replace('\\', '/', $fullpath);
		return $fullpath;
	}

	public function getincludepath()
	{
		$includepath = $this->path() . $this->id() . '.' . $this->extension();
		$includepath = str_replace('\\', '/', $includepath);
		$includepath = substr($includepath, 6);
		return $includepath;
	}

	public function getfulldir()
	{
		return $this->path . $this->id . '.' . $this->extension;
	}

	/**
	 * Generate html code depending on media type
	 * 
	 * @return string html code
	 */
	public function getcode() : string
	{
		switch ($this->type) {
			case 'image':
				$code = '![' . $this->id . '](' . $this->getincludepath() . ')';
				break;
				
			case 'sound':
					$code = '&lt;audio controls src="' . $this->getincludepath() . '"&gt;&lt;/audio&gt;';
				break;
				
			case 'video':
					$code = '&lt;video controls=""&gt;&lt;source src="' . $this->getincludepath() . '" type="video/' . $this->extension . '"&gt;&lt;/video&gt;';
				break;

			default :
					$code = '[' . $this->id . '](' . $this->getincludepath() . ')';
				break;
			
		}
			
		return $code;

	}

	public function getsymbol()
	{
		switch ($this->type) {
			case 'image':
				$symbol = "🖼";
				break;
			
			case 'sound':
				$symbol = "🎵";
				break;
			
			case 'video':
				$symbol = "🎞";
				break;
				
			case 'document':
				$symbol = "📓";
				break;
			
			case 'archive':
				$symbol = "🗜";
				break;
					
			case 'code':
				$symbol = "📄";
				break;
							
			default :
				$symbol = "🎲";
				break;
		}
		return $symbol;
	}



// _________________________________________________ G E T ____________________________________________________

	public function id()
	{
		return $this->id;
	}

	public function path()
	{
		return $this->path;
	}

	public function extension()
	{
		return $this->extension;
	}

	public function type()
	{
		return $this->type;
	}

	public function size($display = 'binary')
	{
		if($display == 'hr') {
			return readablesize($this->size) . 'o';
		} else {
			return $this->size;
		}
	}

	public function date($option = 'date')
	{
		return $this->datetransform('date', $option);
	}

	public function width()
	{
		return $this->width;
	}

	public function height()
	{
		return $this->height;
	}

	public function length()
	{
		return $this->length;
	}

	public function surface()
	{
		$surface = $this->width * $this->height;
		return readablesize($surface, 1000) . 'px';
	}

	public function uid($option = 'id')
	{
		if($option === 'name') {
			$userinfo = posix_getpwuid($this->uid);
			return $userinfo['name'];
		} else {
			return $this->uid;
		}
	}

	public function permissions()
	{
		return $this->permissions;
	}

// ___________________________________________________ S E T __________________________________________________

	public function setid($id)
	{
		if (is_string($id)) {
			$this->id = $id;
		}
	}

	public function setpath($path)
	{
		if (strlen($path) < 40 and is_string($path)) {
			$this->path = strip_tags(strtolower($path));
		}
	}

	public function setextension($extension)
	{
		if (strlen($extension) < 7 and is_string($extension)) {
			$this->extension = strip_tags(strtolower($extension));
		}
	}

	public function settype()
	{
		if (!empty($this->extension) && isset(Model::MEDIA_EXT[$this->extension])) {
			$this->type = Model::MEDIA_EXT[$this->extension];
		} else {
			$this->type = 'other';
		}
	}

	public function setsize($size)
	{
		if (is_int($size)) {
			$this->size = $size;
		}
	}

	public function setdate()
	{
		$timestamp = filemtime($this->getfulldir());
		$this->date = new DateTimeImmutable("@$timestamp");
	}

	public function setwidth($width)
	{
		if (is_int($width)) {
			$this->width = strip_tags(strtolower($width));
		}
	}

	public function setheight($height)
	{
		if (is_int($height)) {
			$this->height = strip_tags(strtolower($height));
		}
	}

	public function setlength($length)
	{
		if ($this->type == 'sound') {
			$this->length = $length;
		}
	}

	public function setuid($uid)
	{
		$this->uid = $uid;
	}

	public function setpermissions($permissions)
	{
		$this->permissions = $permissions;
	}




}