1<?php
2/**
3 *
4 * Zenphoto site cloner
5 *
6 * @package admin
7 */
8define ('OFFSET_PATH', 4);
9require_once(dirname(dirname(dirname(__FILE__))).'/admin-globals.php');
10require_once(SERVERPATH.'/'.ZENFOLDER.'/reconfigure.php');
11
12admin_securityChecks(NULL, currentRelativeURL());
13XSRFdefender('cloneZenphoto');
14
15$msg = array();
16$folder = sanitize($_GET['clonePath']);
17$path = str_replace(WEBPATH,'/',SERVERPATH);
18$newinstall = trim(str_replace($path, '', $folder),'/').'/';
19
20if (trim($folder,'/') == SERVERPATH) {
21	$msg[] = gettext('You attempted to clone to the master install.');
22	$success = false;
23} else {
24	$success = true;
25	$targets = array(ZENFOLDER=>'dir', USER_PLUGIN_FOLDER=>'dir', 'index.php'=>'file');
26	$zplist = $_zp_gallery->getThemes();
27	foreach ($zplist as $theme=>$data) {
28		$targets[THEMEFOLDER.'/'.$theme] = 'dir';
29	}
30	foreach (array(internalToFilesystem('charset_tést'),internalToFilesystem('charset.tést')) as $charset) {
31		if (file_exists(SERVERPATH.'/'.DATA_FOLDER.'/'.$charset)) {
32			$targets[DATA_FOLDER.'/'.$charset] = 'file';
33		}
34	}
35
36	if (!is_dir($folder.DATA_FOLDER)) {
37		@mkdir($folder.DATA_FOLDER);
38	}
39	if (!is_dir($folder.THEMEFOLDER)) {
40		@mkdir($folder.THEMEFOLDER);
41	}
42
43	foreach ($targets as $target=>$type) {
44		if (file_exists($folder.$target)) {
45			$link = str_replace('\\', '/', @readlink($folder.$target));
46			switch ($type) {
47				case 'dir':
48					if (empty($link) || $link == $folder.$target) {
49						// an actual folder
50						if (removeDir($folder.$target)) {
51							if (SYMLINK && @symlink(SERVERPATH.'/'.$target, $folder.$target)) {
52								$msg[] = sprintf(gettext('The existing folder <code>%s</code> was replaced.'), $folder.filesystemToInternal($target))."<br />\n";
53							} else {
54								$msg[] = sprintf(gettext('The existing folder <code>%1$s</code> was removed but Link creation failed.'),$target)."<br />\n";
55								$success = false;
56							}
57						} else {
58							$msg[] = sprintf(gettext('The existing folder <code>%s</code> could not be removed.'), $folder.filesystemToInternal($target))."<br />\n";
59							$success = false;
60						}
61					} else {
62						// is a symlink
63						@chmod($folder.$target, 0777);
64						$success = @rmdir($folder.$target);
65						if (!$success) {	// some systems treat it as a dir, others as a file!
66							$success = @unlink($folder.$target);
67						}
68						if ($success) {
69							if (SYMLINK && @symlink(SERVERPATH.'/'.$target, $folder.$target)) {
70								$msg[] = sprintf(gettext('The existing symlink <code>%s</code> was replaced.'), $folder.filesystemToInternal($target))."<br />\n";
71							} else {
72								$msg[] = sprintf(gettext('The existing symlink <code>%s</code> was removed but Link creation failed.'),$target)."<br />\n";
73								$success = false;
74							}
75						} else {
76							$msg[] = sprintf(gettext('The existing symlink <code>%s</code> could not be removed.'), $folder.filesystemToInternal($target))."<br />\n";
77							$success = false;
78						}
79					}
80					break;
81				case 'file':
82					@chmod($folder.$target, 0777);
83					if (@unlink($folder.$target)) {
84						if (SYMLINK && @symlink(SERVERPATH.'/'.$target, $folder.$target)) {
85							if ($folder.$target == $link) {
86								$msg[] = sprintf(gettext('The existing file <code>%s</code> was replaced.'), $folder.filesystemToInternal($target))."<br />\n";
87							} else {
88								$msg[] = sprintf(gettext('The existing symlink <code>%s</code> was replaced.'), $folder.filesystemToInternal($target))."<br />\n";
89							}
90						} else {
91							$msg[] = sprintf(gettext('The existing file <code>%s</code> was removed but Link creation failed.'),$target)."<br />\n";
92							$success = false;
93						}
94					} else {
95						if ($folder.$target == $link) {
96							$msg[] = sprintf(gettext('The existing file <code>%s</code> could not be removed.'), $folder.filesystemToInternal($target))."<br />\n";
97						} else {
98							$msg[] = sprintf(gettext('The existing symlink <code>%s</code> could not be removed.'), $folder.filesystemToInternal($target))."<br />\n";
99						}
100						$success = false;
101					}
102					break;
103			}
104		} else {
105			if (SYMLINK && @symlink(SERVERPATH.'/'.$target, $folder.$target)) {
106				$msg[] = sprintf(gettext('<code>%s</code> Link created.'),$target)."<br />\n";
107			} else {
108				$msg[] = sprintf(gettext('<code>%s</code> Link creation failed.'),$target)."<br />\n";
109				$success = false;
110			}
111		}
112	}
113}
114if ($success) {
115	array_unshift($msg, '<h2>'.sprintf(gettext('Successful clone to %s'),$folder).'</h2>'."\n");
116	list($diff, $needs) = checkSignature(false);
117	if (empty($needs)) {
118		if (WEBPATH) {
119			$rootpath = str_replace(WEBPATH,'/',SERVERPATH);
120			$urlpath = str_replace(WEBPATH,'/',FULLWEBPATH);
121		} else {
122			$rootpath = SERVERPATH.'/';
123			$urlpath = FULLWEBPATH.'/';
124		}
125
126		if (substr($folder,0,strlen($rootpath)) == $rootpath) {
127			$msg[] = '<p><span class="buttons"><a href="'.$urlpath.$newinstall.ZENFOLDER.'/setup/index.php?autorun">'.gettext('setup the new install').'</a></span><br class="clearall" /></p>'."\n";
128		}
129	} else {
130		$reinstall = '<p>'.sprintf(gettext('Before running setup for <code>%1$s</code> please reinstall the following setup files from the %2$s to this installation:'),$newinstall,ZENPHOTO_VERSION).
131								"\n".'<ul>'."\n";
132		if (!empty($needs)) {
133				foreach ($needs as $script) {
134					$reinstall .= '<li>'.ZENFOLDER.'/setup/'.$script.'</li>'."\n";
135			}
136		}
137		$reinstall .=	'</ul></p>'."\n";
138		$msg[] = $reinstall;
139	}
140} else {
141	array_unshift($msg, '<h2>'.sprintf(gettext('Clone to <code>%s</code> failed'),$folder).'</h2>');
142}
143require_once(SERVERPATH.'/'.ZENFOLDER.'/'.PLUGIN_FOLDER.'/cloneZenphoto/cloneTab.php');
144
145?>