Make WordPress Core

Ticket #75: 0000075-templates.php

File 0000075-templates.php, 6.1 KB (added by Ihad, 21 years ago)
Line 
1<?php
2require_once('../wp-includes/wp-l10n.php');
3
4$title = __("Template &amp; file editing");
5
6function add_magic_quotes($array) {
7 foreach ($array as $k => $v) {
8 if (is_array($v)) {
9 $array[$k] = add_magic_quotes($v);
10 } else {
11 $array[$k] = addslashes($v);
12 }
13 }
14 return $array;
15}
16
17function validate_file($file) {
18 if ('..' == substr($file,0,2))
19 die (__('Sorry, can&#8217;t edit files with ".." in the name. If you are trying to edit a file in your WordPress home directory, you can just type the name of the file in.'));
20
21 if (':' == substr($file,1,1))
22 die (__('Sorry, can&#8217;t call files with their real path.'));
23
24 if ('/' == substr($file,0,1))
25 $file = '.' . $file;
26
27 if (stristr($file, 'config') && $user_level < 10)
28 die(__('<p>You do not have sufficient permissions to edit config files for this blog.</p>'));
29
30 $file = stripslashes($file);
31 $file = str_replace('../', '', $file);
32
33 return $file;
34}
35
36if (!get_magic_quotes_gpc()) {
37 $_GET = add_magic_quotes($_GET);
38 $_POST = add_magic_quotes($_POST);
39 $_COOKIE = add_magic_quotes($_COOKIE);
40}
41
42$wpvarstoreset = array('action','standalone','redirect','profile','error','warning','a','file');
43for ($i=0; $i<count($wpvarstoreset); $i += 1) {
44 $wpvar = $wpvarstoreset[$i];
45 if (!isset($$wpvar)) {
46 if (empty($_POST["$wpvar"])) {
47 if (empty($_GET["$wpvar"])) {
48 $$wpvar = '';
49 } else {
50 $$wpvar = $_GET["$wpvar"];
51 }
52 } else {
53 $$wpvar = $_POST["$wpvar"];
54 }
55 }
56}
57
58switch($action) {
59
60case 'update':
61
62 $standalone = 1;
63 require_once("admin-header.php");
64
65 if ($user_level < 5) {
66 die(__('<p>You have do not have sufficient permissions to edit templates for this blog.</p>'));
67 }
68
69 $newcontent = stripslashes($_POST['newcontent']);
70 $file = $_POST['file'];
71 $file = validate_file($file);
72 $real_file = '../' . $file;
73 if (is_writeable($real_file)) {
74 $f = fopen($real_file, 'w+');
75 fwrite($f, $newcontent);
76 fclose($f);
77 header("Location: templates.php?file=$file&a=te");
78 } else {
79 header("Location: templates.php?file=$file");
80 }
81
82 exit();
83
84break;
85
86default:
87
88 require_once('admin-header.php');
89
90 if ($user_level <= 5) {
91 die(__('<p>You have do not have sufficient permissions to edit templates for this blog.</p>'));
92 }
93
94 if ('' == $file) {
95 if ('' != get_settings('blogfilename')) {
96 $file = get_settings('blogfilename');
97 } else {
98 $file = 'index.php';
99 }
100 }
101
102 $home = get_settings('home');
103 if ($home != '' && ('index.php' == $file || get_settings('blogfilename') == $file)) {
104 $home_root = str_replace('http://', '', $home);
105 $home_root = preg_replace('|([^/]*)(.*)|i', '$2', $home_root);
106 $real_file = $_SERVER['DOCUMENT_ROOT'] . $home_root . '/' . $file;
107 } else {
108 $file = validate_file($file);
109 $real_file = '../' . $file;
110 }
111
112 if (!is_file($real_file))
113 $error = 1;
114
115 if ((substr($file,0,2) == 'wp') and (substr($file,-4,4) == '.php') and ($file != 'wp.php'))
116 $warning = __(' &#8212; this is a WordPress file, be careful when editing it!');
117
118 if (!$error) {
119 $f = fopen($real_file, 'r');
120 $content = fread($f, filesize($real_file));
121 $content = htmlspecialchars($content);
122// $content = str_replace("</textarea","&lt;/textarea",$content);
123 }
124
125 ?>
126<?php if (isset($_GET['a'])) : ?>
127 <div class="updated"><p><?php _e('File edited successfully.') ?></p></div>
128<?php endif; ?>
129 <div class="wrap">
130 <?php
131 echo "<p>" . sprintf(__('Editing <strong>%s</strong>'), $file) . " $warning</p>";
132
133 if (!$error) {
134 ?>
135 <form name="template" action="templates.php" method="post">
136 <textarea cols="80" rows="21" style="width:98%; font-family: 'Courier New', Courier, monopace; font-size:small;" name="newcontent" tabindex="1"><?php echo $content ?></textarea>
137 <input type="hidden" name="action" value="update" />
138 <input type="hidden" name="file" value="<?php echo $file ?>" />
139 <p class="submit">
140 <?php
141 if (is_writeable($real_file)) {
142 echo "<input type='submit' name='submit' value='Update File &raquo;' tabindex='2' />";
143 } else {
144 echo "<input type='button' name='oops' value='" . __('(You cannot update that file/template: must make it writable, e.g. CHMOD 666)') ."' tabindex='2' />";
145 }
146 ?>
147</p>
148 </form>
149 <?php
150 } else {
151 echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
152 }
153 ?>
154</div>
155<div class="wrap">
156 <p><?php _e('To edit a file, type its name here. You can edit any file <a href="http://wiki.wordpress.org/index.php/MakeWritable" title="Read more about making files writable">writable by the server</a>, e.g. CHMOD 666.') ?></p>
157 <form name="file" action="templates.php" method="get">
158 <input type="text" name="file" />
159 <input type="submit" name="submit" value="<?php _e('Edit file &raquo;') ?>" />
160 </form>
161 <p><?php _e('Common files: (click to edit)') ?></p>
162 <ul>
163 <li><a href="templates.php?file=index.php"><?php _e('Main Index') ?> </a></li>
164 <li><a href="templates.php?file=wp-comments.php">Comments</a></li>
165 <li><a href="templates.php?file=wp-comments-popup.php"><?php _e('Popup comments') ?> </a></li>
166 <li><a href="templates.php?file=.htaccess"><?php _e('.htaccess (for rewrite rules)') ?></a></li>
167 <li><a href="templates.php?file=my-hacks.php"><?php _e('my-hacks.php (legacy hacks support)') ?></a></li>
168 </ul>
169<?php
170$plugins_dir = @ dir(ABSPATH . 'wp-content/plugins');
171if ($plugins_dir) {
172 while(($file = $plugins_dir->read()) !== false) {
173 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) )
174 $plugin_files[] = $file;
175 }
176}
177if ($plugins_dir || $plugin_files) :
178?>
179 <p>Plugin files:</p>
180 <ul>
181<?php foreach($plugin_files as $plugin_file) : ?>
182 <li><a href="templates.php?file=wp-content/plugins/<?php echo $plugin_file; ?>"><?php echo $plugin_file; ?></a></li>
183<?php endforeach; ?>
184 </ul>
185<?php endif; ?>
186 <p><?php _e('Note: of course, you can also edit the files/templates in your text editor of choice and upload them. This online editor is only meant to be used when you don&#8217;t have access to a text editor or FTP client.') ?></p>
187</div>
188<?php
189
190break;
191}
192
193include("admin-footer.php") ?>