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 = "
\n";
$str .= " path . "/" . $this->image . "\" width=\"100\"/>
\n";
$str .= " " . $this->title . "\n";
$str .= "
\n"; $str .= "getPath() . "/" . $this->photos[$this->icon]->image . "\" width=\"64\"/>\n"; $str .= "" . $this->title . "\n"; $str .= "
\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 == 'on'; } } 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()); $imagefiles = array(); $d = opendir($folder); while(false !== ($f = readdir($d))) { if(!isJpeg($f)) continue; array_push($imagefiles, $f); } sort($imagefiles); foreach($imagefiles as $f) { $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 album" => "new", "Edit album" => "edit", "Delete album" => "delete"); public function admin_new($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: $form = new Form("create"); $form->addWidget(new LineEdit("Album title:", "title")); $form->addWidget(new LineEdit("Album copyright:", "copyright")); $form->addWidget(new CheckBox("Album enabled:", "enabled")); $form->addWidget(new FileUpload("Select image archive:", "images", "application/zip")); $form->addWidget(new Button("Create")); $form->render(); break; } } public function admin($sub, $action, $vars) { switch($sub) { case "new": $this->admin_new($action, $vars); break; case "edit": $this->admin_new($action, $vars); break; case "delete": $this->admin_new($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, "\n"); fwrite($fp, "