Automad
 All Classes Functions Variables Pages
rename_file.php
1 <?php
2 /*
3  * ....
4  * .: '':.
5  * :::: ':..
6  * ::. ''..
7  * .:'.. ..':.:::' . :. '':.
8  * :. '' '' '. ::::.. ..:
9  * ::::. ..':.. .'''::::: .
10  * :::::::.. '..:::: :. :::: :
11  * ::'':::::::. ':::.'':.:::: :
12  * :.. ''::::::....': '':: :
13  * :::::. '::::: : .. '' .
14  * .''::::::::... ':::.'' ..'' :.''''.
15  * :..:::''::::: :::::...:'' :..:
16  * ::::::. ':::: :::::::: ..:: .
17  * ::::::::.:::: :::::::: :'':.:: .''
18  * ::: '::::::::.' ''::::: :.' '': :
19  * ::: :::::::::..' :::: ::...' .
20  * ::: .:::::::::: :::: :::: .:'
21  * '::' ''::::::: :::: : :: :
22  * ':::: :::: :'' .:
23  * :::: :::: ..''
24  * :::: ..:::: .:''
25  * '''' '''''
26  *
27  *
28  * AUTOMAD
29  *
30  * Copyright (c) 2014 by Marc Anton Dahmen
31  * http://marcdahmen.de
32  *
33  * Licensed under the MIT license.
34  * http://automad.org/license
35  */
36 
37 
38 namespace Automad\Core;
39 
40 
41 defined('AUTOMAD') or die('Direct access not permitted!');
42 
43 
44 /*
45  * Rename a file.
46  */
47 
48 
49 $output = array();
50 
51 
52 // Get correct path of file by the posted URL. For security reasons the file path gets build here and not on the client side.
53 if (isset($_POST['url']) && array_key_exists($_POST['url'], $this->collection)) {
54 
55  $url = $_POST['url'];
56  $Page = $this->collection[$url];
57  $path = AM_BASE_DIR . AM_DIR_PAGES . $Page->path;
58 
59 } else {
60 
61  $url = '';
62  $path = AM_BASE_DIR . AM_DIR_SHARED . '/';
63 
64 }
65 
66 
67 if (isset($_POST['old-name']) && isset($_POST['new-name'])) {
68 
69  if ($_POST['new-name']) {
70 
71  if ($_POST['new-name'] != $_POST['old-name']) {
72 
73  $oldFile = $path . basename($_POST['old-name']);
74  $newFile = $path . Parse::sanitize(basename($_POST['new-name']));
75 
76  if (is_writable($path) && is_writable($oldFile)) {
77 
78  if (!file_exists($newFile)) {
79  rename($oldFile, $newFile);
80  } else {
81  $output['error'] = '"' . $newFile . '" ' . $this->tb['error_existing'];
82  }
83 
84  } else {
85  $output['error'] = $this->tb['error_permission'];
86  }
87 
88  }
89 
90  } else {
91  $output['error'] = $this->tb['error_filename'];
92  }
93 
94 } else {
95  $output['error'] = $this->tb['error_form'];
96 }
97 
98 
99 // Echo JSON
100 echo json_encode($output);
101 
102 
103 ?>
static sanitize($str, $removeDots=false)
Definition: parse.php:413