'.__('Upload failed!','nggallery').'';
}
if ($_POST['importfolder']){
$galleryfolder = $_POST['galleryfolder'];
if ((!empty($galleryfolder)) AND ($defaultpath != $galleryfolder))
$messagetext = ngg_import_gallery($galleryfolder);
}
if ($_POST['uploadimage']){
if ($_FILES['imagefiles']['error'] == 0)
$messagetext = ngg_upload_images($defaultpath);
else
$messagetext = ''.__('Upload failed!','nggallery').'';
}
// message windows
if(!empty($messagetext)) { echo '
'; }
?>
'.__('No valid gallery name!', 'nggallery'). '';
if ( substr(decoct(@fileperms($myabspath.$defaultpath)),1) != '0777' )
return ''.__('Directory', 'nggallery').' '.$defaultpath.' '.__('didn\'t have the permissions 777!', 'nggallery').'';
$nggpath = $defaultpath.$new_pathname;
if (is_dir($myabspath.$nggpath))
return ''.__('Directory', 'nggallery').' '.$nggpath.' '.__('already exists!', 'nggallery').'';
// create new directories
if (!SAFE_MODE) {
if (!@mkdir ($myabspath.$nggpath,0777)) return (''.__('Unable to create directory ', 'nggallery').$nggpath.'!');
if (!@chmod ($myabspath.$nggpath,0777)) return (''.__('Unable to set directory permissions ', 'nggallery').$nggpath.'!');
if (!@mkdir ($myabspath.$nggpath.'/thumbs',0777)) return (''.__('Unable to create directory ', 'nggallery').$nggpath.'/thumbs !');
if (!@chmod ($myabspath.$nggpath.'/thumbs',0777)) return (''.__('Unable to set directory permissions', 'nggallery').$nggpath.'/thumbs !');
} else {
$safemode = '
'.__('The server Safe-Mode is on !', 'nggallery');
$safemode .= '
'.__('Please create directory ', 'nggallery').''.$nggpath.' ';
$safemode .= __('and the thumbnails directory ', 'nggallery').''.$nggpath.'/thumbs '.__('with permission 777 manually !', 'nggallery').'';
}
$result=$wpdb->get_var("SELECT name FROM $wpdb->nggallery WHERE name = '$galleryname' ");
if ($result) {
return ''.__('Gallery', 'nggallery').' '.$newgallery.' '.__('already exists', 'nggallery').'';
} else {
$result = $wpdb->query("INSERT INTO $wpdb->nggallery (name, path) VALUES ('$galleryname', '$nggpath') ");
if ($result) return ''.__('Gallery successfully created!','nggallery').''.$safemode;
}
}
// **************************************************************
function ngg_import_gallery($galleryfolder) {
// ** $galleryfolder contains relative path
//TODO: Check permission of existing thumb folder & images
// import a existing folder
global $wpdb;
// remove trailing slash at the end, if somebody use it
if (substr($galleryfolder, -1) == '/') $galleryfolder = substr($galleryfolder, 0, -1);
$gallerypath = WINABSPATH.$galleryfolder;
if (!is_dir($gallerypath)) return ''.__('Directory', 'nggallery').' '.$gallerypath.' '.__('doesn`t exist', 'nggallery').'!';
// read list of images
$imageslist = ngg_scandir($gallerypath);
if (empty($imageslist)) return ''.__('Directory', 'nggallery').' '.$gallerypath.' '.__('contains no pictures', 'nggallery').'!';
// create thumbnail folder
$check_thumbnail_folder = ngg_get_thumbnail_folder($gallerypath);
if (!$check_thumbnail_folder) {
if (SAFE_MODE) return ''.__('Thumbnail Directory', 'nggallery').' '.$gallerypath.'/thumbs '.__('doesn`t exist', 'nggallery').'!
'.__('Please create the folder thumbs in your gallery folder.', 'nggallery').'';
else @mkdir ($gallerypath.'/thumbs',0777) or die (''.__('Unable to create directory ', 'nggallery').$gallerypath.'/thumbs !');
}
// take folder name as gallery name
$galleryname = basename($galleryfolder);
$result = $wpdb->query("INSERT INTO $wpdb->nggallery (name, path) VALUES ('$galleryname', '$galleryfolder') ");
if (!$result) return ''.__('Database error. Could not add gallery!','nggallery').'';
$gallery_id = $wpdb->insert_id; // get index_id
//create thumbnails
ngg_generatethumbnail($gallerypath,$imageslist);
// add images to database
if (is_array($imageslist)) {
foreach($imageslist as $picture) {
$result = $wpdb->query("INSERT INTO $wpdb->nggpictures (galleryid, filename, alttext) VALUES ('$gallery_id', '$picture', '$picture') ");
if ($result) $count_pic++;
}
}
return ''.__('Gallery','nggallery').' '.$galleryname.' '.__('successfully created!','nggallery').'
'.$count_pic.__(' pictures added.','nggallery').'';
}
// **************************************************************
function ngg_scandir($dirname=".") {
// thx to php.net :-)
$ext = array("jpg", "png", "gif");
$files = array();
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle)))
for($i=0;$ierrmsg;
// skip if file is not there
if (!$thumb->error) {
echo $gallery_absfolder."/".$picture;
$thumb->resize($ngg_options[imgWidth],$ngg_options[imgHeight],$ngg_options[imgResampleMode]);
$thumb->save($gallery_absfolder."/".$picture,$ngg_options[imgQuality]);
}
$thumb->destruct();
}
}
return;
}
// **************************************************************
function ngg_generateWatermark($gallery_absfolder, $pictures) {
// ** $gallery_absfolder must contain abspath !!
$ngg_options = get_option('ngg_options');
if (is_array($pictures)) {
foreach($pictures as $picture) {
//TODO: Check for file permission
$thumb = new Thumbnail($gallery_absfolder."/".$picture, TRUE);
// echo $thumb->errmsg;
// skip if file is not there
if (!$thumb->error) {
if ($ngg_options[wmType] == 'image') {
$thumb->watermarkImgPath = $ngg_options[wmPath];
$thumb->watermarkImage($ngg_options[wmPos], $ngg_options[wmXpos], $ngg_options[wmYpos]);
}
if ($ngg_options[wmType] == 'text') {
$thumb->watermarkText = $ngg_options[wmText];
$thumb->watermarkCreateText($ngg_options[wmColor], $ngg_options[wmFont], $ngg_options[wmSize], $ngg_options[wmOpaque]);
$thumb->watermarkImage($ngg_options[wmPos], $ngg_options[wmXpos], $ngg_options[wmYpos]);
}
$thumb->save($gallery_absfolder."/".$picture,$ngg_options[imgQuality]);
}
$thumb->destruct();
}
}
return;
}
// **************************************************************
function ngg_generatethumbnail($gallery_absfolder, $pictures) {
// ** $gallery_absfolder must contain abspath !!
$ngg_options = get_option('ngg_options');
$prefix = ngg_get_thumbnail_prefix($gallery_absfolder);
$thumbfolder = ngg_get_thumbnail_folder($gallery_absfolder);
if (is_array($pictures)) {
foreach($pictures as $picture) {
$thumb = new Thumbnail($gallery_absfolder."/".$picture, TRUE);
// echo $thumb->errmsg;
// skip if file is not there
if (!$thumb->error) {
if ($ngg_options[thumbcrop]) $thumb->cropFromCenter($ngg_options[thumbwidth],$ngg_options[thumbResampleMode]);
else $thumb->resize($ngg_options[thumbwidth],$ngg_options[thumbheight],$ngg_options[thumbResampleMode]);
$thumb->save($gallery_absfolder.$thumbfolder.$prefix.$picture,$ngg_options[thumbquality]);
}
$thumb->destruct();
}
}
return;
}
// **************************************************************
function ngg_unzip($dir, $file) {
// thx to Gregor at http://blog.scoutpress.de/forum/topic/45
require_once(NGGALLERY_ABSPATH.'/lib/pclzip.lib.php');
$archive = new PclZip($file);
if ($archive->extract(PCLZIP_OPT_PATH, $dir) == 0) {
die("Error : ".$archive->errorInfo(true));
}
return;
}
// **************************************************************
function ngg_import_zipfile($defaultpath) {
$temp_zipfile = $_FILES['zipfile']['tmp_name'];
$filename = $_FILES['zipfile']['name'];
// check if file is a zip file
if ( $_FILES['zipfile']['type'] != "application/x-zip-compressed" ) {
@unlink($temp_zipfile); // del temp file
return ''.__('Uploaded file was no or a faulty zip file ! The server recognize : ','nggallery').$_FILES['zipfile']['type'].'';
}
//cleanup and take the zipfile name as folder name
$foldername = preg_replace ("/(\s+)/", '-', strtolower(strtok ($filename,'.')));
$newfolder = WINABSPATH.$defaultpath.$foldername;
if (!is_dir($newfolder)) {
// create new directories
if (!@mkdir ($newfolder, 0777)) return (''.__('Unable to create directory ', 'nggallery').$newfolder.'!');
if (!@chmod ($newfolder, 0777)) return (''.__('Unable to set directory permissions ', 'nggallery').$newfolder.'!');
if (!@mkdir ($newfolder.'/thumbs', 0777)) return (''.__('Unable to create directory ', 'nggallery').$newfolder.'/thumbs !');
if (!@chmod ($newfolder.'/thumbs', 0777)) return (''.__('Unable to set directory permissions ', 'nggallery').$newfolder.'/thumbs !');
}
else {
return ''.__('Directory already exists, please rename zip file', 'nggallery').'!';
}
// unzip and del temp file
ngg_unzip($newfolder, $temp_zipfile);
@unlink($temp_zipfile) or die (''.__('Unable to unlink zip file ', 'nggallery').$temp_zipfile.'!
');
$messagetext = ''.__('Zip-File successfully unpacked','nggallery').'
';
// parse now the folder and add to database
$messagetext .= ngg_import_gallery($defaultpath.$foldername);
return $messagetext;
}
// **************************************************************
function ngg_upload_images($defaultpath) {
// upload of pictures
//TODO: Multiple upload form
global $wpdb;
$temp_file = $_FILES['imagefiles']['tmp_name'];
$filename = $_FILES['imagefiles']['name'];
$dest_gallery = $_POST['galleryselect'];
if ($dest_gallery == 0) {
@unlink($temp_file) or die (''.__('Unable to unlink file ', 'nggallery').$temp_zipfile.'!
');
return ''.__('No gallery selected !','nggallery').'';
}
// get the path to the gallery
$gallerypath = $wpdb->get_var("SELECT path FROM $wpdb->nggallery WHERE gid = '$dest_gallery' ");
if (!$gallerypath){
@unlink($temp_file) or die (''.__('Unable to unlink file ', 'nggallery').$temp_zipfile.'!
');
return ''.__('Failure in database, no gallery path set !','nggallery').'';
}
$dest_file = WINABSPATH.$gallerypath."/".$filename;
// save temp file to gallery
if (!@move_uploaded_file($_FILES['imagefiles']['tmp_name'], $dest_file)) return ''.__('Error, the file could not moved to : ','nggallery').$dest_file.'';
if (!@chmod ($dest_file, 0666)) return ''.__('Error, the file permissions could not set','nggallery').'';
// Images must be an array
$imageslist = array();
$imageslist[] = $filename;
//create thumbnails
ngg_generatethumbnail(WINABSPATH.$gallerypath,$imageslist);
// add images to database
if (is_array($imageslist)) {
foreach($imageslist as $picture) {
$result = $wpdb->query("INSERT INTO $wpdb->nggpictures (galleryid, filename, alttext) VALUES ('$dest_gallery', '$picture', '$picture') ");
if ($result) $count_pic++;
}
}
return ''.__('Images successfully added','nggallery').'';
} // end function
?>