Example #1
0
 static function server($albumID = 0, $path, $useTemp = false)
 {
     global $database, $plugins, $settings;
     # Parse path
     if (!isset($path)) {
         $path = LYCHEE_UPLOADS_IMPORT;
     }
     if (substr($path, -1) === '/') {
         $path = substr($path, 0, -1);
     }
     if (is_dir($path) === false) {
         Log::error($database, __METHOD__, __LINE__, 'Given path is not a directory (' . $path . ')');
         return 'Error: Given path is not a directory!';
     }
     # Skip folders of Lychee
     if ($path === LYCHEE_UPLOADS_BIG || $path . '/' === LYCHEE_UPLOADS_BIG || $path === LYCHEE_UPLOADS_THUMB || $path . '/' === LYCHEE_UPLOADS_THUMB) {
         Log::error($database, __METHOD__, __LINE__, 'Given path is a reserved path of Lychee (' . $path . ')');
         return 'Error: Given path is a reserved path of Lychee!';
     }
     /*if ($useTemp===true) {
     			$path = Import::move($database, $path);
     			if ($path===false) {
     				Log::error($database, __METHOD__, __LINE__, 'Failed to move import to temporary directory');
     				return false;
     			}
     		}*/
     $error = false;
     $contains['photos'] = false;
     $contains['albums'] = false;
     # Get all files
     $files = glob($path . '/*');
     foreach ($files as $file) {
         # It is possible to move a file because of directory permissions but
         # the file may still be unreadable by the user
         if (!is_readable($file)) {
             $error = true;
             Log::error($database, __METHOD__, __LINE__, 'Could not read file or directory: ' . $file);
             continue;
         }
         if (@exif_imagetype($file) !== false) {
             # Photo
             if (!Import::photo($database, $plugins, $settings, $file, $albumID)) {
                 $error = true;
                 Log::error($database, __METHOD__, __LINE__, 'Could not import file: ' . $file);
                 continue;
             }
             $contains['photos'] = true;
         } else {
             if (is_dir($file)) {
                 # Folder
                 $name = mysqli_real_escape_string($database, basename($file));
                 $album = new Album($database, null, null, null);
                 $newAlbumID = $album->add('[Import] ' . $name);
                 $contains['albums'] = true;
                 if ($newAlbumID === false) {
                     $error = true;
                     Log::error($database, __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')');
                     continue;
                 }
                 $import = Import::server($newAlbumID, $file . '/', false);
                 if ($import !== true && $import !== 'Notice: Import only contains albums!') {
                     $error = true;
                     Log::error($database, __METHOD__, __LINE__, 'Could not import folder. Function returned warning');
                     continue;
                 }
             }
         }
     }
     # Delete tmpdir if import was successful
     /*if ($error===false&&$useTemp===true&&file_exists(LYCHEE_DATA . $tmpdirname)) {
     			if (@rmdir(LYCHEE_DATA . $tmpdirname)===false) Log::error($database, __METHOD__, __LINE__, 'Could not delete temp-folder (' . LYCHEE_DATA . $tmpdirname . ') after successful import');
     		}*/
     if ($contains['photos'] === false && $contains['albums'] === false) {
         return 'Warning: Folder empty or no readable files to process!';
     }
     if ($contains['photos'] === false && $contains['albums'] === true) {
         return 'Notice: Import only contains albums!';
     }
     return true;
 }
Example #2
0
 static function server($albumID = 0, $path)
 {
     global $database, $plugins, $settings;
     # Parse path
     if (!isset($path)) {
         $path = LYCHEE_UPLOADS_IMPORT;
     }
     if (substr($path, -1) === '/') {
         $path = substr($path, 0, -1);
     }
     if (is_dir($path) === false) {
         Log::error($database, __METHOD__, __LINE__, 'Given path is not a directory (' . $path . ')');
         return 'Error: Given path is not a directory!';
     }
     # Skip folders of Lychee
     if ($path === LYCHEE_UPLOADS_BIG || $path . '/' === LYCHEE_UPLOADS_BIG || $path === LYCHEE_UPLOADS_MEDIUM || $path . '/' === LYCHEE_UPLOADS_MEDIUM || $path === LYCHEE_UPLOADS_THUMB || $path . '/' === LYCHEE_UPLOADS_THUMB) {
         Log::error($database, __METHOD__, __LINE__, 'The given path is a reserved path of Lychee (' . $path . ')');
         return 'Error: Given path is a reserved path of Lychee!';
     }
     $error = false;
     $contains['photos'] = false;
     $contains['albums'] = false;
     # Invoke plugins directly, as instance method not valid here
     # Note that updated albumId and path explicitly passed, rather
     # than using func_get_args() which will only return original ones
     $plugins->activate(__METHOD__ . ":before", array($albumID, $path));
     # Get all files
     $files = glob($path . '/*');
     foreach ($files as $file) {
         # It is possible to move a file because of directory permissions but
         # the file may still be unreadable by the user
         if (!is_readable($file)) {
             $error = true;
             Log::error($database, __METHOD__, __LINE__, 'Could not read file or directory: ' . $file);
             continue;
         }
         if (@exif_imagetype($file) !== false) {
             # Photo
             if (!Import::photo($database, $plugins, $settings, $file, $albumID)) {
                 $error = true;
                 Log::error($database, __METHOD__, __LINE__, 'Could not import file: ' . $file);
                 continue;
             }
             $contains['photos'] = true;
         } else {
             if (is_dir($file)) {
                 # Folder
                 $name = mysqli_real_escape_string($database, basename($file));
                 $album = new Album($database, null, null, null);
                 $newAlbumID = $album->add('[Import] ' . $name);
                 $contains['albums'] = true;
                 if ($newAlbumID === false) {
                     $error = true;
                     Log::error($database, __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')');
                     continue;
                 }
                 $import = Import::server($newAlbumID, $file . '/');
                 if ($import !== true && $import !== 'Notice: Import only contains albums!') {
                     $error = true;
                     Log::error($database, __METHOD__, __LINE__, 'Could not import folder. Function returned warning');
                     continue;
                 }
             }
         }
     }
     # Invoke plugins directly, as instance method not valid here
     # Note that updated albumId and path explicitly passed, rather
     # than using func_get_args() which will only return original ones
     $plugins->activate(__METHOD__ . ":after", array($albumID, $path));
     if ($contains['photos'] === false && $contains['albums'] === false) {
         return 'Warning: Folder empty or no readable files to process!';
     }
     if ($contains['photos'] === false && $contains['albums'] === true) {
         return 'Notice: Import only contains albums!';
     }
     return true;
 }
Example #3
0
 public function update(\SplSubject $subject)
 {
     if ($subject->action !== 'Photo::add:before') {
         return false;
     }
     if (!isset($subject->args[0][0]['tmp_name'])) {
         return false;
     }
     # Get watermark info
     $watermark = $this->get();
     if ($watermark === false) {
         Log::error($this->database, __METHOD__, __LINE__, 'Specified watermark not found in database');
         return false;
     }
     # Set watermark info
     $old_path = null;
     $new_path = LYCHEE_DATA . 'watermark_temp.jpeg';
     $type = $watermark->type;
     # Set text
     $text = $watermark->text;
     $font_path = __DIR__ . '/' . $watermark->font_path;
     $font_size = $watermark->font_size;
     $font_color = $watermark->font_color;
     $font_bgcolor = $watermark->font_bgcolor;
     # Set image
     $image_path = __DIR__ . '/' . $watermark->image_path;
     # Set position
     $position = array('align' => $watermark->position_align, 'x' => $watermark->position_x, 'y' => $watermark->position_y);
     # Set import info
     $albumID = @$subject->args[1];
     $description = @$subject->args[2];
     $tags = @$subject->args[3];
     # Add tag
     if (!isset($tags) || $tags === '') {
         $tags = 'watermarked';
     } else {
         $tags .= ',watermarked';
     }
     # For each file
     foreach ($subject->args[0] as $file) {
         # Set watermark info
         $old_path = $file['tmp_name'];
         # New photo class
         $photo = new Photo($this->database, null, null, null);
         # Read infos
         $info = $photo->getInfo($old_path);
         # Move to data-folder before adjusting and loosing the metadata of the photo
         if (!@copy($old_path, $new_path)) {
             Log::error($this->database, __METHOD__, __LINE__, 'Could not copy photo to data before watermarking');
             exit('Error: Could not copy photo to data before watermarking!');
         }
         # Set orientation based on EXIF data
         if (isset($info['orientation'], $info['width'], $info['height']) && $info['orientation'] !== '') {
             if (!$photo->adjustFile($new_path, $info)) {
                 Log::notice($this->database, __METHOD__, __LINE__, 'Could not adjust photo (' . $info['title'] . ')');
             }
         }
         if ($type === 'text') {
             # Watermark with text
             $return = $this->addText($new_path, $new_path, $text, $font_path, $font_size, $font_color, $font_bgcolor, $position);
             if ($return !== true) {
                 Log::error($this->database, __METHOD__, __LINE__, 'Failed to watermark photo with text. Function returned: ' . $return);
                 return false;
             }
         } else {
             # Watermark with photo
             $return = $this->addImage($new_path, $new_path, $image_path, $position);
             if ($return !== true) {
                 Log::error($this->database, __METHOD__, __LINE__, 'Failed to watermark photo with image. Function returned: ' . $return);
                 return false;
             }
         }
         # Import new image
         $return = Import::photo($this->database, null, $this->settings, $new_path, $albumID, $description, $tags);
         if ($return !== true) {
             Log::error($this->database, __METHOD__, __LINE__, 'Failed to import watermarked photo. Import returned: ' . $return);
             return false;
         }
     }
     return true;
 }