source: trunk/plugins/LocalFilesEditor/admin.php @ 2275

Last change on this file since 2275 was 2235, checked in by patdenice, 17 years ago

New: plugin LocalFiles Editor.
Corrected: $id in smarty comment style in intro.tpl

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: admin.php 2235 2008-03-01 14:49:11Z patdenice $
8// | last update   : $Date: 2008-03-01 14:49:11 +0000 (Sat, 01 Mar 2008) $
9// | last modifier : $Author: patdenice $
10// | revision      : $Revision: 2235 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
28include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
29load_language('plugin.lang', LOCALEDIT_PATH);
30$my_base_url = get_admin_plugin_menu_link(__FILE__);
31
32/**
33 * returns $code if php syntax is correct
34 * else return false
35 *
36 * @param string php code
37 */
38function eval_syntax($code)
39{
40    $code = str_replace(array('<?php', '?>'), '', $code);
41    $b = 0;
42    foreach (token_get_all($code) as $token)
43        {
44        if ('{' == $token) ++$b;
45        else if ('}' == $token) --$b;
46    }
47    if ($b) return false;
48    else
49        {
50        ob_start();
51        $eval = eval('if(0){' . $code . '}');
52        ob_end_clean();
53        if ($eval === false) return false;
54        else return '<?php' . $code . '?>';
55    }
56}
57
58
59// +-----------------------------------------------------------------------+
60// |                            Tabssheet
61// +-----------------------------------------------------------------------+
62if (!isset($_GET['tab']))
63    $page['tab'] = 'localconf';
64else
65    $page['tab'] = $_GET['tab'];
66
67$tabsheet = new tabsheet();
68$tabsheet->add('localconf',
69               l10n('locfiledit_onglet_localconf'),
70               $my_base_url.'&amp;tab=localconf');
71$tabsheet->add('css',
72               l10n('locfiledit_onglet_css'),
73               $my_base_url.'&amp;tab=css');
74$tabsheet->add('tpl',
75               l10n('locfiledit_onglet_tpl'),
76               $my_base_url.'&amp;tab=tpl');
77$tabsheet->add('lang',
78               l10n('locfiledit_onglet_lang'),
79               $my_base_url.'&amp;tab=lang');
80$tabsheet->add('plug',
81               l10n('locfiledit_onglet_plug'),
82               $my_base_url.'&amp;tab=plug');
83$tabsheet->select($page['tab']);
84$tabsheet->assign();
85
86
87// +-----------------------------------------------------------------------+
88// |                            Variables init
89// +-----------------------------------------------------------------------+
90$edited_file = '';
91$content_file = '';
92$new_file['localconf'] = "<?php\n\n".l10n('locfiledit_newfile')."\n\n\n\n\n?>";
93$new_file['css'] = l10n('locfiledit_newfile') . "\n\n";
94$new_file['lang'] = "<?php\n\n" . l10n('locfiledit_newfile') . "\n\n\n\n\n?>";
95$new_file['plug'] = "<?php\n/*
96Plugin Name: " . l10n('locfiledit_onglet_plug') . "
97Version: 1.0
98Description: " . l10n('locfiledit_onglet_plug') . "
99Plugin URI: http://www.phpwebgallery.net
100Author:
101Author URI:
102*/\n\n\n\n\n?>";
103
104// Edit selected file for CSS, template and language
105if ((isset($_POST['edit'])) and !is_numeric($_POST['file_to_edit']))
106{
107  $edited_file = $_POST['file_to_edit'];
108  $content_file = file_exists($edited_file) ? 
109    file_get_contents($edited_file) : $new_file[$page['tab']];
110}
111
112
113// +-----------------------------------------------------------------------+
114// |                            Process tabsheet
115// +-----------------------------------------------------------------------+
116$options[] = l10n('locfiledit_choose_file');
117$selected = 0; 
118
119switch ($page['tab'])
120{
121  case 'localconf':
122    $edited_file = PHPWG_ROOT_PATH . "include/config_local.inc.php";
123    $content_file = file_exists($edited_file) ?
124      file_get_contents($edited_file) : $new_file['localconf'];
125       
126    $template->assign('show_default' , array(
127        array('SHOW_DEFAULT' => LOCALEDIT_PATH
128                . 'show_default.php?file=include/config_default.inc.php',
129              'FILE' => 'config_default.inc.php')));
130    break;
131
132
133  case 'css':
134    $template_dir = PHPWG_ROOT_PATH . 'template';
135    $options[] = '----------------------';
136    $value = PHPWG_ROOT_PATH . "template-common/local-layout.css";
137    $options[$value] = 'template-common / local-layout.css';
138    if ($edited_file == $value) $selected = $value;
139       
140    foreach (get_dirs($template_dir) as $pwg_template)
141    {
142      $options[] = '----------------------';
143      $value = $template_dir . '/' . $pwg_template . '/local-layout.css';
144      $options[$value] = $pwg_template . ' / local-layout.css';
145      if ($edited_file == $value) $selected = $value;
146      $options[] = '----------------------';
147      foreach (get_dirs($template_dir.'/'.$pwg_template.'/theme') as $theme)
148      {
149        $value = $template_dir.'/'.$pwg_template.'/theme/'.$theme.'/theme.css';
150        $options[$value] = $pwg_template . ' / ' . $theme . ' / theme.css';
151        if ($edited_file == $value) $selected = $value;
152      }
153    }
154    $template->assign('css_lang_tpl', array(
155        'OPTIONS' => $options,
156        'SELECTED' => $selected));
157    break;
158
159 
160  case 'tpl':
161    $template_dir = PHPWG_ROOT_PATH . 'template';
162    foreach (get_dirs($template_dir) as $pwg_template)
163    {
164      $dir = $template_dir . '/' . $pwg_template . '/';
165      $options[] = '----------------------';
166      if (is_dir($dir) and $content = opendir($dir))
167      {
168        while ($node = readdir($content))
169        {
170          if (is_file($dir . $node)
171            and strtolower(get_extension($node)) == 'tpl'
172            and !strpos($node , '.bak.tpl'))
173          {
174            $value = $dir . $node;
175            $options[$value] = $pwg_template . ' / ' . $node;
176            if ($edited_file == $value) $selected = $value;
177          }
178        }
179      }
180    }
181    $template->assign('css_lang_tpl', array(
182        'OPTIONS' => $options,
183        'SELECTED' => $selected));
184    break;
185
186
187  case 'lang':
188    $options[] = '----------------------';
189    foreach (get_languages() as $language_code => $language_name)
190    {
191      $value = PHPWG_ROOT_PATH.'language/'.$language_code.'/local.lang.php';
192      if ($edited_file == $value)
193      {
194        $selected = $value;
195        $template->assign('show_default', array(
196          array('SHOW_DEFAULT' => LOCALEDIT_PATH
197                  . 'show_default.php?file='
198                  . 'language/'.$language_code.'/common.lang.php',
199                'FILE' => 'common.lang.php'),
200          array('SHOW_DEFAULT' => LOCALEDIT_PATH
201                  . 'show_default.php?file='
202                  . 'language/'.$language_code.'/admin.lang.php',
203                'FILE' => 'admin.lang.php')));
204      }
205      $options[$value] = $language_name;
206    }
207    $template->assign('css_lang_tpl', array(
208        'OPTIONS' => $options,
209        'SELECTED' => $selected));
210    break;
211   
212  case 'plug':
213    $edited_file = PHPWG_PLUGINS_PATH . "PersonalPlugin/main.inc.php";
214    $content_file = file_exists($edited_file) ?
215      file_get_contents($edited_file) : $new_file['plug'];
216    break;
217}
218
219
220// +-----------------------------------------------------------------------+
221// |                           Load backup file
222// +-----------------------------------------------------------------------+
223if (isset($_POST['restore']) and !is_adviser())
224{
225  $edited_file = $_POST['edited_file'];
226  $content_file = file_get_contents(
227      substr_replace($edited_file , '.bak' , strrpos($edited_file ,'.') , 0));
228
229  array_push($page['infos'],
230             l10n('locfiledit_bak_loaded1'),
231             l10n('locfiledit_bak_loaded2'));
232}
233
234
235// +-----------------------------------------------------------------------+
236// |                            Save file
237// +-----------------------------------------------------------------------+
238if (isset($_POST['submit']) and !is_adviser())
239{
240  $edited_file = $_POST['edited_file'];
241  $content_file = stripslashes($_POST['text']);
242  if (get_extension($edited_file) == 'php')
243        {
244    $content_file = eval_syntax($content_file);
245  }
246  if ($content_file === false)
247        {
248    array_push($page['errors'], l10n('locfiledit_syntax_error'));
249  }
250        else
251        {
252    if ($page['tab'] == 'plug'
253      and !is_dir(PHPWG_PLUGINS_PATH . 'PersonalPlugin'))
254    {
255      @mkdir(PHPWG_PLUGINS_PATH . "PersonalPlugin");
256    }
257    if (file_exists($edited_file))
258    {
259      @copy($edited_file,
260        substr_replace($edited_file,
261                       '.bak',
262                       strrpos($edited_file , '.'),
263                       0)
264      );
265    }
266   
267    if ($file = @fopen($edited_file , "w"))
268                {
269      @fwrite($file , $content_file);
270      @fclose($file);
271      array_push($page['infos'],
272        l10n('locfiledit_save_config'),
273        sprintf(l10n('locfiledit_saved_bak'),
274           substr(substr_replace($edited_file,
275                      '.bak',
276                      strrpos($edited_file , '.'),
277                      0),
278                  2)));
279    }
280                else
281    {
282      array_push($page['errors'], l10n('locfiledit_cant_save'));
283    }
284  }
285}
286
287
288// +-----------------------------------------------------------------------+
289// |                            template initialization
290// +-----------------------------------------------------------------------+
291$template->set_filenames(array(
292    'plugin_admin_content' => dirname(__FILE__) . '/admin.tpl'));
293
294if (!empty($edited_file))
295{
296  if (!empty($page['errors']))
297        {
298    $content_file = stripslashes($_POST['text']);
299  }
300  $template->assign('zone_edit',
301    array('EDITED_FILE' => $edited_file,
302          'CONTENT_FILE' => htmlspecialchars($content_file),
303          'FILE_NAME' => trim($edited_file, './\\')));
304  if (file_exists(
305        substr_replace($edited_file ,'.bak',strrpos($edited_file , '.'),0)))
306  {
307    $template->assign('restore', true);
308  }
309}
310
311$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
312
313?>
Note: See TracBrowser for help on using the repository browser.