Make WordPress Core

Ticket #75: 0000075-templates.2.php

File 0000075-templates.2.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 $file = stripslashes($file);
28 $file = str_replace('../', '', $file);
29
30 return $file;
31}
32
33if (!get_magic_quotes_gpc()) {
34 $_GET = add_magic_quotes($_GET);
35 $_POST = add_magic_quotes($_POST);
36 $_COOKIE = add_magic_quotes($_COOKIE);
37}
38
39$wpvarstoreset = array('action','standalone','redirect','profile','error','warning','a','file');
40for ($i=0; $i<count($wpvarstoreset); $i += 1) {
41 $wpvar = $wpvarstoreset[$i];
42 if (!isset($$wpvar)) {
43 if (empty($_POST["$wpvar"])) {
44 if (empty($_GET["$wpvar"])) {
45 $$wpvar = '';
46 } else {
47 $$wpvar = $_GET["$wpvar"];
48 }
49 } else {
50 $$wpvar = $_POST["$wpvar"];
51 }
52 }
53}
54
55switch($action) {
56
57case 'update':
58
59 $standalone = 1;
60 require_once("admin-header.php");
61
62 if ($user_level < 5) {
63 die(__('<p>You have do not have sufficient permissions to edit templates for this blog.</p>'));
64 }
65
66 $newcontent = stripslashes($_POST['newcontent']);
67 $file = $_POST['file'];
68 $file = validate_file($file);
69 $real_file = '../' . $file;
70 if (is_writeable($real_file)) {
71 $f = fopen($real_file, 'w+');
72 fwrite($f, $newcontent);
73 fclose($f);
74 header("Location: templates.php?file=$file&a=te");
75 } else {
76 header("Location: templates.php?file=$file");
77 }
78
79 exit();
80
81break;
82
83default:
84
85 require_once('admin-header.php');
86
87 if (stristr($file, 'config') && $user_level <= 9) {
88 die(__('<p>You do not have sufficient permissions to edit config files for this blog.</p>'));
89 }
90
91 if ($user_level <= 5) {
92 die(__('<p>You have do not have sufficient permissions to edit templates for this blog.</p>'));
93 }
94
95 if ('' == $file) {
96 if ('' != get_settings('blogfilename')) {
97 $file = get_settings('blogfilename');
98 } else {
99 $file = 'index.php';
100 }
101 }
102
103 $home = get_settings('home');
104 if ($home != '' && ('index.php' == $file || get_settings('blogfilename') == $file)) {
105 $home_root = str_replace('http://', '', $home);
106 $home_root = preg_replace('|([^/]*)(.*)|i', '$2', $home_root);
107 $real_file = $_SERVER['DOCUMENT_ROOT'] . $home_root . '/' . $file;
108 } else {
109 $file = validate_file($file);
110 $real_file = '../' . $file;
111 }
112
113 if (!is_file($real_file))
114 $error = 1;
115
116 if ((substr($file,0,2) == 'wp') and (substr($file,-4,4) == '.php') and ($file != 'wp.php'))
117 $warning = __(' &#8212; this is a WordPress file, be careful when editing it!');
118
119 if (!$error) {
120 $f = fopen($real_file, 'r');
121 $content = fread($f, filesize($real_file));
122 $content = htmlspecialchars($content);
123// $content = str_replace("</textarea","&lt;/textarea",$content);
124 }
125
126 ?>
127<?php if (isset($_GET['a'])) : ?>
128 <div class="updated"><p><?php _e('File edited successfully.') ?></p></div>
129<?php endif; ?>
130 <div class="wrap">
131 <?php
132 echo "<p>" . sprintf(__('Editing <strong>%s</strong>'), $file) . " $warning</p>";
133
134 if (!$error) {
135 ?>
136 <form name="template" action="templates.php" method="post">
137 <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>
138 <input type="hidden" name="action" value="update" />
139 <input type="hidden" name="file" value="<?php echo $file ?>" />
140 <p class="submit">
141 <?php
142 if (is_writeable($real_file)) {
143 echo "<input type='submit' name='submit' value='Update File &raquo;' tabindex='2' />";
144 } else {
145 echo "<input type='button' name='oops' value='" . __('(You cannot update that file/template: must make it writable, e.g. CHMOD 666)') ."' tabindex='2' />";
146 }
147 ?>
148</p>
149 </form>
150 <?php
151 } else {
152 echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
153 }
154 ?>
155</div>
156<div class="wrap">
157 <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>
158 <form name="file" action="templates.php" method="get">
159 <input type="text" name="file" />
160 <input type="submit" name="submit" value="<?php _e('Edit file &raquo;') ?>" />
161 </form>
162 <p><?php _e('Common files: (click to edit)') ?></p>
163 <ul>
164 <li><a href="templates.php?file=index.php"><?php _e('Main Index') ?> </a></li>
165 <li><a href="templates.php?file=wp-comments.php">Comments</a></li>
166 <li><a href="templates.php?file=wp-comments-popup.php"><?php _e('Popup comments') ?> </a></li>
167 <li><a href="templates.php?file=.htaccess"><?php _e('.htaccess (for rewrite rules)') ?></a></li>
168 <li><a href="templates.php?file=my-hacks.php"><?php _e('my-hacks.php (legacy hacks support)') ?></a></li>
169 </ul>
170<?php
171$plugins_dir = @ dir(ABSPATH . 'wp-content/plugins');
172if ($plugins_dir) {
173 while(($file = $plugins_dir->read()) !== false) {
174 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) )
175 $plugin_files[] = $file;
176 }
177}
178if ($plugins_dir || $plugin_files) :
179?>
180 <p>Plugin files:</p>
181 <ul>
182<?php foreach($plugin_files as $plugin_file) : ?>
183 <li><a href="templates.php?file=wp-content/plugins/<?php echo $plugin_file; ?>"><?php echo $plugin_file; ?></a></li>
184<?php endforeach; ?>
185 </ul>
186<?php endif; ?>
187 <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>
188</div>
189<?php
190
191break;
192}
193
194include("admin-footer.php") ?>