/** * Copy a theme directory to create a new custom theme * * @param $source source directory * @param $target target directory * @return bool|string either true or an error message * @author Ozh * @since 1.3 */ function copyThemeDirectory($source, $target, $newname) { global $_zp_current_admin_obj; $message = true; $source = SERVERPATH . '/themes/' . internalToFilesystem($source); $target = SERVERPATH . '/themes/' . internalToFilesystem($target); // If the target theme already exists, nothing to do. if (is_dir($target)) { return gettext('Cannot create new theme.') . ' ' . sprintf(gettext('Directory “%s” already exists!'), basename($target)); } // If source dir is missing, exit too if (!is_dir($source)) { return gettext('Cannot create new theme.') . ' ' . sprintf(gettext('Cannot find theme directory “%s” to copy!'), basename($source)); } // We must be able to write to the themes dir. if (!is_writable(dirname($target))) { return gettext('Cannot create new theme.') . ' ' . gettext('The <tt>/themes</tt> directory is not writable!'); } // We must be able to create the directory if (!mkdir($target, FOLDER_MOD)) { return gettext('Cannot create new theme.') . ' ' . gettext('Could not create directory for the new theme'); } @chmod($target, FOLDER_MOD); // Get a list of files to copy: get all files from the directory, remove those containing '/.svn/' $source_files = array_filter(listDirectoryFiles($source), create_function('$str', 'return strpos($str, "/.svn/") === false;')); // Determine nested (sub)directories structure to create: go through each file, explode path on "/" // and collect every unique directory $dirs_to_create = array(); foreach ($source_files as $path) { $path = explode('/', dirname(str_replace($source . '/', '', $path))); $dirs = ''; foreach ($path as $subdir) { if ($subdir == '.svn' or $subdir == '.') { continue 2; } $dirs = "{$dirs}/{$subdir}"; $dirs_to_create[$dirs] = $dirs; } } // Create new directory structure foreach ($dirs_to_create as $dir) { mkdir("{$target}/{$dir}", FOLDER_MOD); @chmod("{$target}/{$dir}", FOLDER_MOD); } // Now copy every file foreach ($source_files as $file) { $newfile = str_replace($source, $target, $file); if (!copy("{$file}", "{$newfile}")) { return sprintf(gettext("An error occurred while copying files. Please delete manually the new theme directory “%s” and retry or copy files manually."), basename($target)); } @chmod("{$newfile}", FOLDER_MOD); } // Rewrite the theme header. if (file_exists($target . '/theme_description.php')) { $theme_description = array(); require $target . '/theme_description.php'; $theme_description['desc'] = sprintf(gettext('Your theme, based on theme %s'), $theme_description['name']); } else { $theme_description['desc'] = gettext('Your theme'); } $theme_description['name'] = $newname; $theme_description['author'] = $_zp_current_admin_obj->getUser(); $theme_description['version'] = '1.0'; $theme_description['date'] = date('Y-m-d H:m:s', time()); $description = sprintf('<' . '?php // Theme definition file $theme_description["name"] = "%s"; $theme_description["author"] = "%s"; $theme_description["version"] = "%s"; $theme_description["date"] = "%s"; $theme_description["desc"] = "%s"; ?' . '>', html_encode($theme_description['name']), html_encode($theme_description['author']), html_encode($theme_description['version']), html_encode($theme_description['date']), html_encode($theme_description['desc'])); $f = fopen($target . '/theme_description.php', 'w'); if ($f !== FALSE) { @fwrite($f, $description); fclose($f); $message = gettext('New custom theme created successfully!'); } else { $message = gettext('New custom theme created, but its description could not be updated'); } // Make a slightly custom theme image if (file_exists("{$target}/theme.png")) { $themeimage = "{$target}/theme.png"; } else { if (file_exists("{$target}/theme.gif")) { $themeimage = "{$target}/theme.gif"; } else { if (file_exists("{$target}/theme.jpg")) { $themeimage = "{$target}/theme.jpg"; } else { $themeimage = false; } } } if ($themeimage) { if ($im = zp_imageGet($themeimage)) { $x = zp_imageWidth($im) / 2 - 45; $y = zp_imageHeight($im) / 2 - 10; $text = "CUSTOM COPY"; $font = zp_imageLoadFont(); $ink = zp_colorAllocate($im, 0xff, 0xff, 0xff); // create a blueish overlay $overlay = zp_createImage(zp_imageWidth($im), zp_imageHeight($im)); $back = zp_colorAllocate($overlay, 0x60, 0x60, 0x90); zp_imageFill($overlay, 0, 0, $back); // Merge theme image and overlay zp_imageMerge($im, $overlay, 0, 0, 0, 0, zp_imageWidth($im), zp_imageHeight($im), 45); // Add text zp_writeString($im, $font, $x - 1, $y - 1, $text, $ink); zp_writeString($im, $font, $x + 1, $y + 1, $text, $ink); zp_writeString($im, $font, $x, $y, $text, $ink); // Save new theme image zp_imageOutput($im, 'png', $themeimage); } } return $message; }
header("Location: " . FULLWEBPATH . "/" . ZENFOLDER . "/admin-themes.php"); exitZP(); } $ok_extensions = array('css', 'php', 'js', 'txt', 'inc'); function isTextFile($file) { global $ok_extensions; $ext = strtolower(getSuffix($file)); return in_array($ext, $ok_extensions); } $messages = $file_to_edit = $file_content = null; $what = 'edit'; $themes = $_zp_gallery->getThemes(); $theme = basename(sanitize($_GET['theme'])); $themedir = SERVERPATH . '/themes/' . internalToFilesystem($theme); $themefiles = listDirectoryFiles($themedir); $themefiles_to_ext = array(); if (themeIsEditable($theme)) { foreach ($themefiles as $file) { if (isTextFile($file)) { $themefiles_to_ext[getSuffix($file)][] = $file; // array(['php']=>array('file.php', 'image.php'),['css']=>array('style.css')) } else { unset($themefiles[$file]); // $themefile will eventually have all editable files and nothing else } } if (isset($_GET['file'])) { if (!in_array($themedir . '/' . $_GET['file'], $themefiles)) { $messages['errorbox'][] = gettext('Cannot edit this file!'); }
/** * Copy a theme directory to create a new custom theme * * @param $source source directory * @param $target target directory * @return bool|string either true or an error message * @author Ozh * @since 1.3 */ function copyThemeDirectory($source, $target, $newname) { global $_zp_current_admin; $message = true; $source = SERVERPATH . '/themes/' . UTF8ToFilesystem($source); $target = SERVERPATH . '/themes/' . UTF8ToFilesystem($target); // If the target theme already exists, nothing to do. if (is_dir($target)) { return gettext('Cannot create new theme.') . ' ' . sprintf(gettext('Directory "%s" already exists!'), basename($target)); } // If source dir is missing, exit too if (!is_dir($source)) { return gettext('Cannot create new theme.') . ' ' . sprintf(gettext('Cannot find theme directory "%s" to copy!'), basename($source)); } // We must be able to write to the themes dir. if (!is_writable(dirname($target))) { return gettext('Cannot create new theme.') . ' ' . gettext('The <tt>/themes</tt> directory is not writable!'); } // We must be able to create the directory if (!mkdir($target, CHMOD_VALUE)) { return gettext('Cannot create new theme.') . ' ' . gettext('Could not create directory for the new theme'); } chmod($target, CHMOD_VALUE); // Get a list of files to copy: get all files from the directory, remove those containing '/.svn/' $source_files = array_filter(listDirectoryFiles($source), create_function('$str', 'return strpos($str, "/.svn/") === false;')); // Determine nested (sub)directories structure to create: go through each file, explode path on "/" // and collect every unique directory $dirs_to_create = array(); foreach ($source_files as $path) { $path = dirname(str_replace($source . '/', '', $path)); $path = explode('/', $path); $dirs = ''; foreach ($path as $subdir) { if ($subdir == '.svn' or $subdir == '.') { continue 2; } $dirs = "{$dirs}/{$subdir}"; $dirs_to_create[$dirs] = $dirs; } } /* Example result for theme 'effervescence_plus': $dirs_to_create = array ( '/styles' => '/styles', '/scripts' => '/scripts', '/images' => '/images', '/images/smooth' => '/images/smooth', '/images/slimbox' => '/images/slimbox', ); */ // Create new directory structure foreach ($dirs_to_create as $dir) { mkdir("{$target}/{$dir}", CHMOD_VALUE); chmod("{$target}/{$dir}", CHMOD_VALUE); // Using chmod as PHP doc suggested: "Avoid using umask() in multithreaded webservers. It is better to change the file permissions with chmod() after creating the file." } // Now copy every file foreach ($source_files as $file) { $newfile = str_replace($source, $target, $file); if (!copy("{$file}", "{$newfile}")) { return sprintf(gettext("An error occured while copying files. Please delete manually the new theme directory '%s' and retry or copy files manually."), basename($target)); } chmod("{$newfile}", CHMOD_VALUE); } // Rewrite the theme header. if (file_exists($target . '/theme_description.php')) { $theme_description = array(); require $target . '/theme_description.php'; $theme_description['desc'] = sprintf(gettext('Your theme, based on theme %s'), $theme_description['name']); } else { $theme_description['desc'] = gettext('Your theme'); } $theme_description['name'] = $newname; $theme_description['author'] = $_zp_current_admin['user']; $theme_description['version'] = '1.0'; $theme_description['date'] = date('d/m/Y'); $description = sprintf('<' . '?php // Zenphoto theme definition file $theme_description["name"] = "%s"; $theme_description["author"] = "%s"; $theme_description["version"] = "%s"; $theme_description["date"] = "%s"; $theme_description["desc"] = "%s"; ?' . '>', htmlentities($theme_description['name'], ENT_COMPAT), htmlentities($theme_description['author'], ENT_COMPAT), htmlentities($theme_description['version'], ENT_COMPAT), htmlentities($theme_description['date'], ENT_COMPAT), htmlentities($theme_description['desc'], ENT_COMPAT)); $f = fopen($target . '/theme_description.php', 'w'); if ($f !== FALSE) { @fwrite($f, $description); fclose($f); $message = gettext('New custom theme created successfully!'); } else { $message = gettext('New custom theme created, but its description could not be updated'); } // Make a slightly custom theme image if (file_exists("{$target}/theme.png")) { $themeimage = "{$target}/theme.png"; } else { if (file_exists("{$target}/theme.gif")) { $themeimage = "{$target}/theme.gif"; } else { if (file_exists("{$target}/theme.jpg")) { $themeimage = "{$target}/theme.jpg"; } else { $themeimage = false; } } } if ($themeimage) { require_once dirname(__FILE__) . '/functions-image.php'; if ($im = get_image($themeimage)) { $x = imagesx($im) / 2 - 45; $y = imagesy($im) / 2 - 10; $text = "CUSTOM COPY"; // create a blueish overlay $overlay = imagecreatetruecolor(imagesx($im), imagesy($im)); imagefill($overlay, 0, 0, 0x606090); // Merge theme image and overlay imagecopymerge($im, $overlay, 0, 0, 0, 0, imagesx($im), imagesy($im), 45); // Add text imagestring($im, 5, $x - 1, $y - 1, $text, 0xffffff); imagestring($im, 5, $x + 1, $y + 1, $text, 0xffffff); imagestring($im, 5, $x, $y, $text, 0xff0000); // Save new theme image imagepng($im, $themeimage); } } return $message; }