Browse Source

Code and speed improvements

Tobias Reich 11 years ago
parent
commit
6eb46da1a1
1 changed files with 54 additions and 53 deletions
  1. 54 53
      php/modules/album.php

+ 54 - 53
php/modules/album.php

@@ -15,7 +15,7 @@ function addAlbum($title = '', $public = 0, $visible = 1) {
 
 	if (strlen($title)>50) $title = substr($title, 0, 50);
 
-	$sysdate	= date("d.m.Y");
+	$sysdate	= date('d.m.Y');
 	$result		= $database->query("INSERT INTO lychee_albums (title, sysdate, public, visible) VALUES ('$title', '$sysdate', '$public', '$visible');");
 
 	if (!$result) return false;
@@ -28,49 +28,42 @@ function getAlbums($public) {
 	global $database, $settings;
 
 	// Smart Albums
-	if (!$public) $return = getSmartInfo();
+	if ($public===false) $return = getSmartInfo();
 
 	// Albums
-	if ($public) $query = "SELECT id, title, public, sysdate, password FROM lychee_albums WHERE public = 1 AND visible <> 0";
-	else $query = "SELECT id, title, public, sysdate, password FROM lychee_albums";
+	$query = "SELECT id, title, public, sysdate, password FROM lychee_albums WHERE public = 1 AND visible <> 0";
+	if ($public===false) $query = "SELECT id, title, public, sysdate, password FROM lychee_albums";
 
-	$result	= $database->query($query) OR exit("Error: $result <br>".$database->error);
-	$i		= 0;
+	$result = $database->query($query) OR exit('Error: ' . $database->error);
 
-	while($row = $result->fetch_object()) {
+	while ($row = $result->fetch_object()) {
 
 		// Info
-		$return["content"][$row->id]['id']		= $row->id;
-		$return["content"][$row->id]['title']	= $row->title;
-		$return["content"][$row->id]['public']	= $row->public;
-		$return["content"][$row->id]['sysdate']	= date('F Y', strtotime($row->sysdate));
+		$return['content'][$row->id]['id']		= $row->id;
+		$return['content'][$row->id]['title']	= $row->title;
+		$return['content'][$row->id]['public']	= $row->public;
+		$return['content'][$row->id]['sysdate']	= date('F Y', strtotime($row->sysdate));
 
 		// Password
-		if ($row->password=="") $return["content"][$row->id]['password'] = false;
-		else $return["content"][$row->id]['password'] = true;
+		$return['content'][$row->id]['password'] = false;
+		if ($row->password!='') $return['content'][$row->id]['password'] = true;
 
 		// Thumbs
-		if (($public&&$row->password=="")||(!$public)) {
+		if (($public&&$row->password=='')||(!$public)) {
 
 			$albumID = $row->id;
-			$result2 = $database->query("SELECT thumbUrl FROM lychee_photos WHERE album = '$albumID' ORDER BY star DESC, " . substr($settings['sorting'], 9)	. " LIMIT 0, 3");
+			$result2 = $database->query("SELECT thumbUrl FROM lychee_photos WHERE album = '$albumID' ORDER BY star DESC, " . substr($settings['sorting'], 9) . " LIMIT 0, 3");
 			$k = 0;
-			while($row2 = $result2->fetch_object()){
-				$return["content"][$row->id]["thumb$k"] = $row2->thumbUrl;
+			while ($row2 = $result2->fetch_object()) {
+				$return['content'][$row->id]["thumb$k"] = $row2->thumbUrl;
 				$k++;
 			}
-			if (!isset($return["content"][$row->id]["thumb0"])) $return["content"][$row->id]["thumb0"] = "";
-			if (!isset($return["content"][$row->id]["thumb1"])) $return["content"][$row->id]["thumb1"] = "";
-			if (!isset($return["content"][$row->id]["thumb2"])) $return["content"][$row->id]["thumb2"] = "";
 
 		}
 
-		// Album count
-		$i++;
-
 	}
 
-	$return["num"] = $i;
+	$return["num"] = $result->num_rows;
 
 	return $return;
 
@@ -84,28 +77,34 @@ function getSmartInfo() {
 	$result	= $database->query("SELECT thumbUrl FROM lychee_photos WHERE album = 0 " . $settings['sorting']);
 	$i		= 0;
 	while($row = $result->fetch_object()) {
-		if ($i<3) $return["unsortedThumb$i"] = $row->thumbUrl;
-		$i++;
+		if ($i<3) {
+			$return["unsortedThumb$i"] = $row->thumbUrl;
+			$i++;
+		} else break;
 	}
-	$return['unsortedNum'] = $i;
+	$return['unsortedNum'] = $result->num_rows;
 
 	// Public
 	$result2	= $database->query("SELECT thumbUrl FROM lychee_photos WHERE public = 1 " . $settings['sorting']);
 	$i			= 0;
 	while($row2 = $result2->fetch_object()) {
-		if ($i<3) $return["publicThumb$i"] = $row2->thumbUrl;
-		$i++;
+		if ($i<3) {
+			$return["publicThumb$i"] = $row2->thumbUrl;
+			$i++;
+		} else break;
 	}
-	$return['publicNum'] = $i;
+	$return['publicNum'] = $result2->num_rows;
 
 	// Starred
 	$result3	= $database->query("SELECT thumbUrl FROM lychee_photos WHERE star = 1 " . $settings['sorting']);
 	$i			= 0;
 	while($row3 = $result3->fetch_object()) {
-		if ($i<3) $return["starredThumb$i"] = $row3->thumbUrl;
-		$i++;
+		if ($i<3) {
+			$return["starredThumb$i"] = $row3->thumbUrl;
+			$i++;
+		} else break;
 	}
-	$return['starredNum'] = $i;
+	$return['starredNum'] = $result3->num_rows;
 
 	return $return;
 
@@ -118,15 +117,15 @@ function getAlbum($albumID) {
 	// Get album information
 	switch($albumID) {
 
-		case "f":	$return['public'] = false;
+		case 'f':	$return['public'] = false;
 					$query = "SELECT id, title, tags, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE star = 1 " . $settings['sorting'];
 					break;
 
-		case "s":	$return['public'] = false;
+		case 's':	$return['public'] = false;
 					$query = "SELECT id, title, tags, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE public = 1 " . $settings['sorting'];
 					break;
 
-		case "0":	$return['public'] = false;
+		case '0':	$return['public'] = false;
 					$query = "SELECT id, title, tags, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE album = 0 " . $settings['sorting'];
 					break;
 
@@ -136,7 +135,7 @@ function getAlbum($albumID) {
 					$return['description']	= $row->description;
 					$return['sysdate']		= date('d M. Y', strtotime($row->sysdate));
 					$return['public']		= $row->public;
-					$return['password']		= ($row->password=="" ? false : true);
+					$return['password']		= ($row->password=='' ? false : true);
 					$query = "SELECT id, title, tags, sysdate, public, star, album, thumbUrl FROM lychee_photos WHERE album = '$albumID' " . $settings['sorting'];
 					break;
 
@@ -144,8 +143,7 @@ function getAlbum($albumID) {
 
 	// Get photos
 	$result				= $database->query($query);
-	$previousPhotoID	= "";
-	$i					= 0;
+	$previousPhotoID	= '';
 	while($row = $result->fetch_assoc()) {
 
 		$return['content'][$row['id']]['id']		= $row['id'];
@@ -158,15 +156,14 @@ function getAlbum($albumID) {
 		$return['content'][$row['id']]['thumbUrl']	= $row['thumbUrl'];
 
 		$return['content'][$row['id']]['previousPhoto']	= $previousPhotoID;
-		$return['content'][$row['id']]['nextPhoto']		= "";
+		$return['content'][$row['id']]['nextPhoto']		= '';
 		if ($previousPhotoID!="") $return['content'][$previousPhotoID]['nextPhoto'] = $row['id'];
 
 		$previousPhotoID = $row['id'];
-		$i++;
 
 	}
 
-	if ($i==0) {
+	if ($result->num_rows===0) {
 
 		// Empty album
 		$return['content'] = false;
@@ -187,7 +184,7 @@ function getAlbum($albumID) {
 	}
 
 	$return['id']	= $albumID;
-	$return['num']	= $i;
+	$return['num']	= $result->num_rows;
 
 	return $return;
 
@@ -197,7 +194,8 @@ function setAlbumTitle($albumIDs, $title) {
 
 	global $database;
 
-	if (strlen($title)<1||strlen($title)>50) return false;
+	if (strlen($title)>50) $title = substr($title, 0, 50);
+
 	$result = $database->query("UPDATE lychee_albums SET title = '$title' WHERE id IN ($albumIDs);");
 
 	if (!$result) return false;
@@ -211,6 +209,7 @@ function setAlbumDescription($albumID, $description) {
 
 	$description = htmlentities($description);
 	if (strlen($description)>1000) return false;
+
 	$result = $database->query("UPDATE lychee_albums SET description = '$description' WHERE id = '$albumID';");
 
 	if (!$result) return false;
@@ -244,15 +243,15 @@ function getAlbumArchive($albumID) {
 	switch($albumID) {
 		case 's':
 			$query = "SELECT url FROM lychee_photos WHERE public = '1';";
-			$zipTitle = "Public";
+			$zipTitle = 'Public';
 			break;
 		case 'f':
 			$query = "SELECT url FROM lychee_photos WHERE star = '1';";
-			$zipTitle = "Starred";
+			$zipTitle = 'Starred';
 			break;
 		default:
 			$query = "SELECT url FROM lychee_photos WHERE album = '$albumID';";
-			$zipTitle = "Unsorted";
+			$zipTitle = 'Unsorted';
 	}
 
 	$zip	= new ZipArchive();
@@ -261,12 +260,13 @@ function getAlbumArchive($albumID) {
 	$i		= 0;
 
 	while($row = $result->fetch_object()) {
-		$files[$i] = "../uploads/big/".$row->url;
+		$files[$i] = '../uploads/big/' . $row->url;
 		$i++;
 	}
 
 	$result = $database->query("SELECT title FROM lychee_albums WHERE id = '$albumID' LIMIT 1;");
 	$row = $result->fetch_object();
+
 	if ($albumID!=0&&is_numeric($albumID)) $zipTitle = $row->title;
 	$filename = "../data/$zipTitle.zip";
 
@@ -275,9 +275,9 @@ function getAlbumArchive($albumID) {
 	}
 
 	foreach($files AS $zipFile) {
-		$newFile = explode("/",$zipFile);
+		$newFile = explode('/',$zipFile);
 		$newFile = array_reverse($newFile);
-		$zip->addFile($zipFile, $zipTitle."/".$newFile[0]);
+		$zip->addFile($zipFile, $zipTitle . '/' . $newFile[0]);
 	}
 
 	$zip->close();
@@ -296,7 +296,7 @@ function setAlbumPublic($albumID, $password) {
 
 	global $database;
 
-	$result	= $database->query("SELECT public FROM lychee_albums WHERE id = '$albumID';");
+	$result	= $database->query("SELECT public FROM lychee_albums WHERE id = '$albumID' LIMIT 1;");
 	$row	= $result->fetch_object();
 	$public	= ($row->public=='0' ? 1 : 0);
 
@@ -331,7 +331,7 @@ function checkAlbumPassword($albumID, $password) {
 	$result	= $database->query("SELECT password FROM lychee_albums WHERE id = '$albumID';");
 	$row	= $result->fetch_object();
 
-	if ($row->password=="") return true;
+	if ($row->password=='') return true;
 	else if ($row->password==$password) return true;
 	return false;
 
@@ -341,10 +341,11 @@ function isAlbumPublic($albumID) {
 
 	global $database;
 
+	if ($albumID==='0'||$albumID==='s'||$albumID==='f') return false;
+
 	$result	= $database->query("SELECT public FROM lychee_albums WHERE id = '$albumID';");
 	$row	= $result->fetch_object();
 
-	if ($albumID==='0'||$albumID==='s'||$albumID==='f') return false;
 	if ($row->public==1) return true;
 	return false;