public function move_file($new_location) { $new_location = PerchUtil::file_path($new_location); $new_location = str_replace(PERCH_LOGINPATH, '/', $new_location); $new_location = str_replace('\\', '/', $new_location); $new_location = str_replace('..', '', $new_location); $new_location = str_replace('//', '/', $new_location); $old_path = PERCH_SITEPATH . $this->pagePath(); $new_path = PerchUtil::file_path(PERCH_SITEPATH . '/' . ltrim($new_location, '/')); if ($old_path != $new_path) { if (file_exists($old_path)) { if (!file_exists($new_path)) { $new_dir = PerchUtil::strip_file_name($new_path); if (!file_exists($new_dir)) { mkdir($new_dir, 0755, true); } if (is_writable($new_dir)) { if (rename($old_path, $new_path)) { // Is it a reference to a master page? If so, update the include $contents = file_get_contents($new_path); $pattern = '#' . preg_quote("<?php include(str_replace('/', DIRECTORY_SEPARATOR, 'XXX')); ?>") . '#'; $pattern = str_replace('XXX', '([a-zA-Z/\\.-]+)', $pattern); if (preg_match($pattern, $contents, $match)) { $current_path = $match[1]; $template_dir = PERCH_TEMPLATE_PATH . '/pages'; $template_path = str_replace(PERCH_SITEPATH . DIRECTORY_SEPARATOR, '', PERCH_TEMPLATE_PATH) . '/pages/'; // normalise $current_path = str_replace(DIRECTORY_SEPARATOR, '/', $current_path); $template_dir = str_replace(DIRECTORY_SEPARATOR, '/', $template_dir); $template_path = str_replace(DIRECTORY_SEPARATOR, '/', $template_path); $parts = explode($template_path, $current_path); if (PerchUtil::count($parts)) { $master_page_template = $parts[1]; $Pages = new PerchContent_Pages(); $a = PerchUtil::file_path($template_dir . '/' . $master_page_template); $b = PerchUtil::file_path(dirname($new_path)); $new_include_path = $Pages->get_relative_path($a, $b); $new_include = '<' . '?php include(str_replace(\'/\', DIRECTORY_SEPARATOR, \'' . $new_include_path . '\')); ?' . '>'; /* $new_include .= '<' . '?php /* '.PHP_EOL; $new_include .= 'Current path: '.$current_path.PHP_EOL; $new_include .= 'Template dir: '.$template_dir.PHP_EOL; $new_include .= 'Template path: '.$template_path.PHP_EOL; $new_include .= 'Master page template: '.$master_page_template.PHP_EOL; $new_include .= 'A: '.$a.PHP_EOL; $new_include .= 'B: '.$b.PHP_EOL; $new_include .= 'New include path: '.$new_include_path.PHP_EOL; $new_include .= 'Parts: '.print_r($parts, true).PHP_EOL; $new_include .= PHP_EOL.' *'.'/ ?' . '>'; */ file_put_contents($new_path, str_replace($match[0], $new_include, $contents)); } } else { // Else just update the Perch runtime. $pattern = '#' . preg_quote("include(__Y____X__" . trim(PERCH_LOGINPATH, '/') . "__DS__runtime.php__Y__);") . '#'; $pattern = str_replace('__X__', '([a-zA-Z/\\.-]*)', $pattern); $pattern = str_replace('__Y__', '[\'\\"]', $pattern); $pattern = str_replace('__DS__', '[\\\\/]', $pattern); if (preg_match($pattern, $contents, $match)) { PerchUtil::debug($match); $Pages = new PerchContent_Pages(); $a = PerchUtil::file_path(PERCH_PATH . '/runtime.php'); $b = PerchUtil::file_path(dirname($new_path)); $new_include_path = $Pages->get_relative_path($a, $b); PerchUtil::debug('New include path: ' . $new_include_path); $new_include = "include('{$new_include_path}');"; file_put_contents($new_path, str_replace($match[0], $new_include, $contents)); } } return array(true, false); } else { return array(false, 'The page could not be moved.'); } } else { return array(false, 'The destination folder could not be written to, so the page cannot be moved.'); } } else { return array(false, 'A page file already exists at the new location.'); } } else { return array(false, 'No page file exists at that location to move.'); } } else { // It's ok, as the file is already where they want it to be. return array(true, false); } }