summaryrefslogtreecommitdiff
path: root/utils/album.php
blob: 1b6b662aa50b076bd368ede1ed89c7137fb42702 (plain)
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
<?php
include_once("thumbnail.php");

class Photo {
  public $file;
  public $text;

  function Photo($file, $text) {
    $this->file = $file;
    $this->text = $text;
  }
}

class Album {
	// Album directory (and identifier)
  public $album;

	// Photo array
  public $photos;

	// Album data
  public $title;
  public $icon;
  public $copyright;
	
	public function add($photo) {
		// First added image is automatically made album icon.
		if($this->icon == "") $this->icon = $photo->file;

		$key = $photo->file;
		$this->photos[$key] = $photo;
	}
	
	public function write()
	{
		$fp = fopen($this->file, "w");
		fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

		fwrite($fp, "<album title=\"". $this->title . "\" icon=\"".$this->icon."\" copyright=\"" . $this->copyright . "\">\n");
		foreach($this->photos as $photo) {
			fwrite($fp, "  <photo file=\"" . $photo->file . "\"\n");
			fwrite($fp, "         text=\"" . $photo->text . "\">\n");
			fwrite($fp, "  </photo>\n");
		}
		fwrite($fp, "</album>\n");

		fclose($fp);
	}
	
	private function read()
	{

		$dom = new DomDocument;
		$dom->preserveWhiteSpace = FALSE;
		$dom->load($this->file);

		$params = $dom->getElementsByTagName('album');
		foreach ($params as $param) {
			$this->title = $param->getAttribute('title');
			$this->icon = $param->getAttribute('icon');
			$this->copyright = $param->getAttribute('copyright');
		}

		$params = $dom->getElementsByTagName('photo');
		foreach ($params as $param) {
			$photo = new Photo($param->getAttribute('file'), $param->getAttribute('text'));
			$this->add($photo);
		}

		// Key sort
		if(sizeof($this->photos) > 0) ksort($this->photos);
	}

	public function Album($album)
	{
		global $ALBUMS_DIR;
		$this->album = $album;
		$this->file =  $ALBUMS_DIR ."/". $album . "/album.xml";
		$this->read();
	}

}


function getAllAlbums()
{
	global $ALBUMS_DIR;
	$albums = array();

	$handle = opendir($ALBUMS_DIR . "/");
	$albumdirs = array();
	while($albumdir = readdir($handle)) {
		array_push($albumdirs, $albumdir);
	}

	rsort($albumdirs);

	foreach($albumdirs as $albumdir) {
		if(!strstr($albumdir, ".") && !strstr($albumdir, "..")) {
			$album = new Album($albumdir);
			array_push($albums, $album);
		}
	}

	return $albums;
}

function getRandomPhoto()
{
	$album;
	$photo;

	$albums = getAllAlbums();

	$numalbums = sizeof($albums);
	$ralbum = rand(0, sizeof($albums)-1);
	foreach($albums as $a) {
		$album = $a;
		$ralbum--;
		if(!$ralbum) break;
	}

	$numphotos = sizeof($album->photos);
	$rphoto = rand(0, $numphotos-1);
	foreach($album->photos as $p) {
		$photo = $p;
		$rphoto--;
		if(!$rphoto) break;
	}
	
	//	echo "<p>".$numalbums . " " .$ralbum . " ".$numphotos . " ".$rphoto . "</p>";

	return array($album, $photo);
}


?>