Browse Source

Import filename and upload improvements

Tobias Reich 10 years ago
parent
commit
38ae4662ff
4 changed files with 44 additions and 28 deletions
  1. 7 1
      docs/md/Settings.md
  2. 5 4
      php/modules/db.php
  3. 9 6
      php/modules/misc.php
  4. 23 17
      php/modules/upload.php

+ 7 - 1
docs/md/Settings.md

@@ -38,4 +38,10 @@ If `1`, Lychee will check if you are using the latest version. The notice will b
 
 	sorting = ORDER BY [row] [ASC|DESC]
 
-A typical part of an MySQL statement. This string will be appended to mostly every MySQL query.
+A typical part of an MySQL statement. This string will be appended to mostly every MySQL query.
+
+#### Import Filename
+
+	importFilename = [0|1]
+
+If `1`, Lychee will import the filename of the upload file.

+ 5 - 4
php/modules/db.php

@@ -93,7 +93,7 @@ function createDatabase($dbName, $database) {
 
 function createTables($database) {
 
-	if (!$database->query("SELECT * FROM lychee_settings;")) {
+	if (!$database->query("SELECT * FROM lychee_settings LIMIT 1;")) {
 
 		$query = "
 
@@ -114,7 +114,8 @@ function createTables($database) {
 			('password',''),
 			('thumbQuality','90'),
 			('checkForUpdates','1'),
-			('sorting','ORDER BY id DESC');
+			('sorting','ORDER BY id DESC'),
+			('importFilename','1');
 
 		";
 
@@ -122,7 +123,7 @@ function createTables($database) {
 
 	}
 
-	if (!$database->query("SELECT * FROM lychee_albums;")) {
+	if (!$database->query("SELECT * FROM lychee_albums LIMIT 1;")) {
 
 		$query = "
 
@@ -142,7 +143,7 @@ function createTables($database) {
 
     }
 
-    if (!$database->query("SELECT * FROM lychee_photos;")) {
+    if (!$database->query("SELECT * FROM lychee_photos LIMIT 1;")) {
 
 		$query = "
 

+ 9 - 6
php/modules/misc.php

@@ -79,14 +79,17 @@ function update() {
 
 	global $database;
 
-	if(!$database->query("SELECT `public` FROM `lychee_albums`;")) $database->query("ALTER TABLE  `lychee_albums` ADD  `public` TINYINT( 1 ) NOT NULL DEFAULT  '0'");
-	if(!$database->query("SELECT `password` FROM `lychee_albums`;")) $database->query("ALTER TABLE  `lychee_albums` ADD  `password` VARCHAR( 100 ) NULL DEFAULT ''");
-	if(!$database->query("SELECT `description` FROM `lychee_albums`;")) $database->query("ALTER TABLE  `lychee_albums` ADD  `description` VARCHAR( 1000 ) NULL DEFAULT ''");
-	if($database->query("SELECT `password` FROM `lychee_albums`;")) $database->query("ALTER TABLE  `lychee_albums` CHANGE  `password` `password` VARCHAR( 100 ) NULL DEFAULT ''");
+	if(!$database->query("SELECT `public` FROM `lychee_albums` LIMIT 1;")) $database->query("ALTER TABLE  `lychee_albums` ADD  `public` TINYINT( 1 ) NOT NULL DEFAULT  '0'");
+	if(!$database->query("SELECT `password` FROM `lychee_albums` LIMIT 1;")) $database->query("ALTER TABLE  `lychee_albums` ADD  `password` VARCHAR( 100 ) NULL DEFAULT ''");
+	if(!$database->query("SELECT `description` FROM `lychee_albums` LIMIT 1;")) $database->query("ALTER TABLE  `lychee_albums` ADD  `description` VARCHAR( 1000 ) NULL DEFAULT ''");
+	if($database->query("SELECT `password` FROM `lychee_albums` LIMIT 1;")) $database->query("ALTER TABLE  `lychee_albums` CHANGE  `password` `password` VARCHAR( 100 ) NULL DEFAULT ''");
 
-	if($database->query("SELECT `description` FROM `lychee_photos`;")) $database->query("ALTER TABLE  `lychee_photos` CHANGE  `description` `description` VARCHAR( 1000 ) NULL DEFAULT ''");
-	if($database->query("SELECT `shortlink` FROM `lychee_photos`;")) $database->query("ALTER TABLE  `lychee_photos` DROP  `shortlink`");
+	if($database->query("SELECT `description` FROM `lychee_photos` LIMIT 1;")) $database->query("ALTER TABLE  `lychee_photos` CHANGE  `description` `description` VARCHAR( 1000 ) NULL DEFAULT ''");
+	if($database->query("SELECT `shortlink` FROM `lychee_photos` LIMIT 1;")) $database->query("ALTER TABLE  `lychee_photos` DROP  `shortlink`");
 	$database->query("UPDATE `lychee_photos` SET url = replace(url, 'uploads/big/', ''), thumbUrl = replace(thumbUrl, 'uploads/thumb/', '')");
+	
+	$result = $database->query("SELECT `value` FROM `lychee_settings` WHERE `key` = 'importFilename' LIMIT 1;");
+	if ($result->fetch_object()!==true) $database->query("INSERT INTO `lychee_settings` (`key`, `value`) VALUES ('importFilename', '1')");
 
 	return true;
 

+ 23 - 17
php/modules/upload.php

@@ -11,7 +11,7 @@ if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
 
 function upload($files, $albumID) {
 
-	global $database;
+	global $database, $settings;
 
 	switch($albumID) {
 		// s for public (share)
@@ -32,35 +32,41 @@ function upload($files, $albumID) {
 	}
 
 	foreach ($files as $file) {
-
+	    	    
+	    if ($file['type']!=='image/jpeg'&&
+	    	$file['type']!=='image/png'&&
+	    	$file['type']!=='image/gif')
+	    		return false;
+	    		
 	    $id = str_replace('.', '', microtime(true));
 	    while(strlen($id)<14) $id .= 0;
-
-	    $tmp_name = $file["tmp_name"];
-
-	    $type = getimagesize($tmp_name);
-	    if (($type[2]!=1)&&($type[2]!=2)&&($type[2]!=3)) return false;
-	    $data = array_reverse(explode('.', $file["name"]));
-	    $data = $data[0];
-
-	    $photo_name = md5($id) . ".$data";
+	    
+	    $tmp_name = $file['tmp_name'];
+	    $extension = array_reverse(explode('.', $file['name']));
+	    $extension = $extension[0];
+	    $photo_name = md5($id) . ".$extension";
 
 	    // Import if not uploaded via web
 	    if (!is_uploaded_file($tmp_name)) {
-	    	if (copy($tmp_name, "../uploads/big/" . $photo_name)) {
+	    	if (copy($tmp_name, '../uploads/big/' . $photo_name)) {
 				unlink($tmp_name);
 				$import_name = $tmp_name;
 			}
 	    } else {
-		    move_uploaded_file($tmp_name, "../uploads/big/" . $photo_name);
-		    $import_name = "";
+		    move_uploaded_file($tmp_name, '../uploads/big/' . $photo_name);
+		    $import_name = '';
 	    }
 
 	    // Read infos
 	    $info = getInfo($photo_name);
 
+	    // Use title of file if IPTC title missing
+	    if ($info['title']===''&&
+	    	$settings['importFilename']==='1')
+	    		$info['title'] = mysqli_real_escape_string($database, substr(str_replace(".$extension", '', $file['name']), 0, 30));
+
 	    // Set orientation based on EXIF data
-	    if (isset($info['orientation'])&&isset($info['width'])&&isset($info['height'])) {
+	    if ($file['type']==='image/jpeg'&&isset($info['orientation'])&&isset($info['width'])&&isset($info['height'])) {
 
 	    	if ($info['orientation']==3||$info['orientation']==6||$info['orientation']==8) {
 
@@ -259,8 +265,8 @@ function createThumb($filename, $width = 200, $height = 200) {
     $info = getimagesize($url);
 
     $photoName = explode(".", $filename);
-    $newUrl = "../uploads/thumb/".$photoName[0].".jpeg";
-    $newUrl2x = "../uploads/thumb/".$photoName[0]."@2x.jpeg";
+    $newUrl = "../uploads/thumb/$photoName[0].jpeg";
+    $newUrl2x = "../uploads/thumb/$photoName[0]@2x.jpeg";
 
     // Set position and size
     $thumb = imagecreatetruecolor($width, $height);