diff options
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/modules/gallery.php | 303 | 
1 files changed, 303 insertions, 0 deletions
| diff --git a/utils/modules/gallery.php b/utils/modules/gallery.php new file mode 100644 index 0000000..b3f410d --- /dev/null +++ b/utils/modules/gallery.php @@ -0,0 +1,303 @@ +<?php +/* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ + +include_once($UTIL_DIR . "/convert.php"); +include_once($UTIL_DIR . "/markdown.php"); +include_once($UTIL_DIR . "/modules.php"); + +function isJpeg($file) +{ +	$parts = explode(".", $file); +	$suffix = $parts[sizeof($parts) - 1]; +	$suffix = strtolower($suffix); +	return $suffix == "jpg" || $suffix == "jpeg"; +} + +function cleanUp($dir) +{ +	$d = opendir($dir); +	while(false !== ($f = readdir($d))) { +		if($f == '.' || $f == '..') continue; +		if(is_dir($f)) cleanUp($dir . "/" . $f); +		else unlink($dir . "/" . $f); +	} +	closedir($d); + +	rmdir($dir); +} + +class Photo { +	public $id; +	private $path; +  public $title; +	public $image; + +  public function write($fp) +  { +    fwrite($fp, "    <photo id=\"" . +	   htmlspecialchars($this->id, ENT_QUOTES, "UTF-8") . "\"\n"); +    fwrite($fp, "           title=\"" . +	   htmlspecialchars($this->title, ENT_QUOTES, "UTF-8") . "\"\n"); +    fwrite($fp, "           image=\"" . +	   htmlspecialchars($this->image, ENT_QUOTES, "UTF-8") . "\"/>\n"); +  } + +  public function show() +  { +		$str = "<p>\n"; +		$str .= "  <img src=\"" . $this->path . "/" . $this->image . "\" width=\"100\"/><br/>\n"; +		$str .= "  " . $this->title . "\n"; +		$str .= "</p>\n"; +		return $str; +  } + +  public function Photo($path, $id, $title, $image) +  { +		$this->path = $path; +		$this->id = $id; +    $this->title = $title; +		$this->image = $image; +  } +} + + +class Album { +	public $id; +  public $title; +	public $copyright; +	public $enabled; +	public $photos = array(); + +	public function add($photo) +	{ +		$this->photos[$photo->id] = $photo; +	} + +  public function write($fp) +  { +    fwrite($fp, "  <album id=\"" . +	   htmlspecialchars($this->id, ENT_QUOTES, "UTF-8") . "\"\n"); +    fwrite($fp, "         title=\"" . +	   htmlspecialchars($this->title, ENT_QUOTES, "UTF-8") . "\"\n"); +    fwrite($fp, "         copyright=\"" . +	   htmlspecialchars($this->copyright, ENT_QUOTES, "UTF-8") . "\"\n"); +    fwrite($fp, "         icon=\"" . +	   htmlspecialchars($this->icon, ENT_QUOTES, "UTF-8") . "\"\n"); +    fwrite($fp, "         enabled=\"" . +	   htmlspecialchars($this->enabled, ENT_QUOTES, "UTF-8") . "\">\n"); +		foreach($this->photos as $photo) { +			$photo->write($fp); +		} +    fwrite($fp, "  </album>\n"); +  } + +  public function show() +  { +		$str = "<p>\n"; +		$str .= "<img src=\"" . $this->getPath() . "/" . $this->photos[$this->icon]->image . "\" width=\"64\"/>\n"; +		$str .= "<strong>" . $this->title . "</strong>\n"; +		$str .= "</p>\n"; +		foreach($this->photos as $photo) { +			$str .= $photo->show(); +		} +		return $str; +  } + +	public function getPath() +	{ +		global $ALBUMS_DIR; +		return $ALBUMS_DIR . "/" . $this->id; +	} + +	public function getNextID() +	{ +		$maxid = 0; +    foreach($this->photos as $photo) { +      if($photo->id > $nextid) $maxid = $photo->id; +    } +		return $maxid + 1; +	} + +  public function Album($id, $title, $copyright, $icon, $enabled) +  { +		$this->id = $id; +    $this->title = $title; +		$this->copyright = $copyright; +		$this->icon = $icon; +		$this->enabled = $enabled; +  } +} + +function unpackImages($zipfile, $album) +{ +	if(is_uploaded_file($zipfile['tmp_name'])) { +		echo "File ". $zipfile['name'] ." uploaded successfully.\n"; + +		// +		// Unzip file +		// +		// http://dk.php.net/manual/en/book.zip.php +		$zip = new ZipArchive(); +		$zip->open($zipfile['tmp_name']); +		$folder = $zipfile['tmp_name'].'.unzip'; +		$zip->extractTo($folder); +		 +		// +		// Look at EXIF header (Image Description) and move images +		// +		// http://dk.php.net/manual/en/book.exif.php +		if(!file_exists($album->getPath())) mkdir($album->getPath()); + 		$d = opendir($folder); +		while(false !== ($f = readdir($d))) { +			if(!isJpeg($f)) continue; +			$tags = exif_read_data($folder . "/" . $f, "ANY_TAG"); +			rename($folder . "/" . $f, $album->getPath() . "/" . $f); +			$photo = new Photo($album->getPath(), $album->getNextID(), $tags['ImageDescription'], $f); +			$album->add($photo); +		} +		closedir($d); + +		// +		// Clean up (recursively) +		// +		cleanUp($folder); +	} +} + +class Gallery { + +  private $file; +  private $albums = array(); + +  // Admin config +  public $admin_title = "Gallery"; +  public $admin_submodules = array("New gallery" => "newgallery"); + +  public function admin_newgallery($action, $vars) +  { +		global $ALBUMS_DIR; +    switch($action) { +		case "create": +			$album = new Album($this->getNextID(), $vars['title'], $vars['copyright'], "1", $vars['enabled']); +			unpackImages($_FILES['images'], $album); +			$this->add($album); +			$this->write(); +			echo $album->show(); +			break;	 + +    case "select": +    default: +      beginform("create", true); +			lineedit("Album title:", "title"); +			lineedit("Album copyright:", "copyright"); +			lineedit("Album enabled:", "enabled", "true"); +      fileupload("Select image archive:", "images", "application/zip"); +      button("Create"); +      endform(); +      break; +    } +  } + +  public function admin($sub, $action, $vars) +  { +    switch($sub) { +    case "newgallery": +      $this->admin_newgallery($action, $vars); +      break; +    } +  } + +  public function run($params) +  { +		$str = ""; +    foreach($params as $param) { +      switch($param) { +			default: +				foreach($this->albums as $album) { +					$str .= $album->show(); +				} +				break; +      } +    } +		return $str; +  } + +  public function add($album) { +		$this->albums[$album->id] = $album; +  } +	 +	public function getNextID() +	{ +		$maxid = 0; +    foreach($this->albums as $album) { +      if($album->id > $nextid) $maxid = $album->id; +    } +		return $maxid + 1; +	} + +  public function write() +  { +    $fp = fopen($this->file, "w"); +    fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); + +    fwrite($fp, "<gallery>\n"); +    foreach($this->albums as $album) { +      $album->write($fp); +    } +    fwrite($fp, "</gallery>\n"); + +    fclose($fp); +  } +	 +	private function read() +	{ +		$dom = new DomDocument; +		$dom->resolveExternals = FALSE; +		$dom->substituteEntities = FALSE; +		$dom->preserveWhiteSpace = FALSE; +		$dom->load($this->file); +		 +		$gallery = $dom->documentElement; +		//		$this->width = $gallery->getAttribute('width'); + +		foreach($gallery->childNodes as $albumElem) { + +			if($albumElem->tagName != "album") continue; + +			$album = new Album($albumElem->getAttribute('id'), +												 $albumElem->getAttribute('title'), +												 $albumElem->getAttribute('copyright'), +												 $albumElem->getAttribute('icon'), +												 $albumElem->getAttribute('enabled')); +			 +			foreach($albumElem->childNodes as $photoElem) { +				if($photoElem->tagName != "photo") continue; + +				$photo = new Photo($album->getPath(), +													 $photoElem->getAttribute('id'), +													 $photoElem->getAttribute('title'), +													 $photoElem->getAttribute('image')); +				$album->add($photo); + +			} +			 +			$this->add($album); + +		} +	} + +  public function Gallery($file) +  { +    $this->file =  $file; +		if(file_exists($file)) $this->read(); +  } + +} + +function gallery_init() +{ +  global $DATA_DIR; +  return new Gallery($DATA_DIR . "/gallery.xml"); +} + +?> | 
