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

Last change on this file since 5160 was 5160, checked in by patdenice, 14 years ago

Editor
Update editarea to 0.8.2.
Remove CSS tab.
Fix jQuery path.

  • Property svn:eol-style set to LF
File size: 13.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
25include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
26include_once(LOCALEDIT_PATH.'functions.inc.php');
27load_language('plugin.lang', LOCALEDIT_PATH);
28$my_base_url = get_admin_plugin_menu_link(__FILE__);
29
30// +-----------------------------------------------------------------------+
31// |                            Tabssheet
32// +-----------------------------------------------------------------------+
33if (!isset($_GET['tab']))
34    $page['tab'] = 'localconf';
35else
36    $page['tab'] = $_GET['tab'];
37
38$tabsheet = new tabsheet();
39$tabsheet->add('localconf',
40               l10n('locfiledit_onglet_localconf'),
41               $my_base_url.'&amp;tab=localconf');
42$tabsheet->add('tpl',
43               l10n('locfiledit_onglet_tpl'),
44               $my_base_url.'&amp;tab=tpl');
45$tabsheet->add('lang',
46               l10n('locfiledit_onglet_lang'),
47               $my_base_url.'&amp;tab=lang');
48$tabsheet->add('plug',
49               l10n('locfiledit_onglet_plug'),
50               $my_base_url.'&amp;tab=plug');
51$tabsheet->select($page['tab']);
52$tabsheet->assign();
53
54
55// +-----------------------------------------------------------------------+
56// |                            Variables init
57// +-----------------------------------------------------------------------+
58$edited_file = isset($_POST['edited_file']) ? $_POST['edited_file'] : '';
59$content_file = '';
60$new_file['localconf'] = "<?php\n\n/* ".l10n('locfiledit_newfile')." */\n\n\n\n\n?>";
61$new_file['css'] = "/* " . l10n('locfiledit_newfile') . " */\n\n";
62$new_file['tpl'] = "{* " . l10n('locfiledit_newfile') . " *}\n\n";
63$new_file['lang'] = $new_file['localconf'];
64$new_file['plug'] = "<?php\n/*
65Plugin Name: " . l10n('locfiledit_onglet_plug') . "
66Version: 1.0
67Description: " . l10n('locfiledit_onglet_plug') . "
68Plugin URI: http://piwigo.org
69Author:
70Author URI:
71*/\n\n\n\n\n?>";
72$newfile_page = isset($_GET['newfile']) ? true : false;
73
74// Editarea options
75$editarea_options = array(
76  'language' => substr($user['language'], 0, 2),
77  'start_highlight' => true,
78  'allow_toggle' => false,
79  'toolbar' => 'search,fullscreen, |,select_font, |, undo, redo, change_smooth_selection, highlight, reset_highlight, |, help');
80
81// Edit selected file for CSS, template and language
82if ((isset($_POST['edit'])) and !is_numeric($_POST['file_to_edit']))
83{
84  $edited_file = $_POST['file_to_edit'];
85  $content_file = file_exists($edited_file) ? 
86    file_get_contents($edited_file) : $new_file[$page['tab']];
87}
88
89// Edit new tpl file
90if (isset($_POST['create_tpl']))
91{
92  $filename = $_POST['tpl_name'];
93  if (empty($filename))
94  {
95    array_push($page['errors'], l10n('locfiledit_empty_filename'));
96  }
97  if (get_extension($filename) != 'tpl')
98  {
99    $filename .= '.tpl';
100  }
101  if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename))
102  {
103    array_push($page['errors'], l10n('locfiledit_filename_error'));
104  }
105  if (is_numeric($_POST['tpl_model']) and $_POST['tpl_model'] != '0')
106  {
107    array_push($page['errors'], l10n('locfiledit_model_error'));
108  }
109  if (file_exists($_POST['tpl_parent'] . '/' . $filename))
110  {
111    array_push($page['errors'], l10n('locfiledit_file_already_exists'));
112  }
113  if (!empty($page['errors']))
114  {
115    $newfile_page = true;
116  }
117  else
118  {
119    $edited_file = $_POST['tpl_parent'] . '/' . $filename;
120    $content_file = ($_POST['tpl_model'] == '0') ? $new_file['tpl'] : file_get_contents($_POST['tpl_model']);
121  }
122}
123
124// +-----------------------------------------------------------------------+
125// |                            Process tabsheet
126// +-----------------------------------------------------------------------+
127switch ($page['tab'])
128{
129  case 'localconf':
130    $edited_file = PHPWG_ROOT_PATH . "include/config_local.inc.php";
131    $content_file = file_exists($edited_file) ?
132      file_get_contents($edited_file) : $new_file['localconf'];
133       
134    $template->assign('show_default' , array(
135        array('SHOW_DEFAULT' => LOCALEDIT_PATH
136                . 'show_default.php?file=include/config_default.inc.php',
137              'FILE' => 'config_default.inc.php')));
138    $editarea_options['syntax'] = 'php';
139    break;
140
141  case 'tpl':
142    // New file form creation
143    if ($newfile_page and !is_adviser())
144    {
145      $filename = isset($_POST['tpl_name']) ? $_POST['tpl_name'] : '';
146      $selected['model'] = isset($_POST['tpl_model']) ? $_POST['tpl_model'] : '0';
147      $selected['parent'] = isset($_POST['tpl_parent']) ? $_POST['tpl_parent'] : PHPWG_ROOT_PATH . 'template-extension';
148
149      // Parent directories list
150      $options['parent'] = array(PHPWG_ROOT_PATH . 'template-extension' => 'template-extension');
151      $options['parent'] = array_merge($options['parent'], get_rec_dirs(PHPWG_ROOT_PATH . 'template-extension'));
152
153      // Model list
154      $eligible_templates = array(
155          'about.tpl',
156          'footer.tpl',
157          'header.tpl',
158          'identification.tpl',
159          'index.tpl',
160          'mainpage_categories.tpl',
161          'menubar.tpl',
162          'menubar_categories.tpl',
163          'menubar_identification.tpl',
164          'menubar_links.tpl',
165          'menubar_menu.tpl',
166          'menubar_specials.tpl',
167          'menubar_tags.tpl',
168          'navigation_bar.tpl',
169          'nbm.tpl',
170          'notification.tpl',
171          'picture.tpl',
172          'picture_content.tpl',
173          'picture_nav_buttons.tpl',
174          'popuphelp.tpl',
175          'profile.tpl',
176          'profile_content.tpl',
177          'redirect.tpl',
178          'register.tpl',
179          'search.tpl',
180          'search_rules.tpl',
181          'slideshow.tpl',
182          'tags.tpl',
183          'thumbnails.tpl',
184          'upload.tpl');
185
186      $options['model'][] = l10n('locfiledit_empty_page');
187      $options['model'][] = '----------------------';
188      foreach (get_extents() as $pwg_template)
189      {
190        $value = PHPWG_ROOT_PATH . 'template-extension/' . $pwg_template;
191        $options['model'][$value] =  'template-extension / ' . str_replace('/', ' / ', $pwg_template);
192      }
193      $template_dir = PHPWG_ROOT_PATH . 'template';
194      foreach (get_dirs($template_dir) as $pwg_template)
195      {
196        if (count($options['model']) > 2)
197        {
198          $options['model'][] = '----------------------';
199        }
200        $dir = $template_dir . '/' . $pwg_template . '/';
201        if (is_dir($dir) and $content = opendir($dir))
202        {
203          while ($node = readdir($content))
204          {
205            if (is_file($dir . $node) and in_array($node, $eligible_templates))
206            {
207              $value = $dir . $node;
208              $options['model'][$value] = $pwg_template . ' / ' . $node;
209            }
210          }
211        }
212      }
213      // Assign variables to template
214      $template->assign('create_tpl', array(
215        'NEW_FILE_NAME' => $filename,
216        'MODEL_OPTIONS' => $options['model'],
217        'MODEL_SELECTED' => $selected['model'],
218        'PARENT_OPTIONS' => $options['parent'],
219        'PARENT_SELECTED' => $selected['parent']));
220      break;
221    }
222    // List existing template extensions
223    $template_dir = PHPWG_ROOT_PATH . 'template-extension';
224    $selected = 0; 
225    $options[] = l10n('locfiledit_choose_file');
226    $options[] = '----------------------';
227    foreach (get_extents() as $pwg_template)
228    {
229      $value = './template-extension/' . $pwg_template;
230      $options[$value] =  str_replace('/', ' / ', $pwg_template);
231      if ($edited_file == $value) $selected = $value;
232    }
233    if ($selected == 0 and !empty($edited_file))
234    {
235      $options[$edited_file] =  str_replace(array('./template-extension/', '/'), array('', ' / '), $edited_file);
236      $selected = $edited_file;
237    }
238    $template->assign('css_lang_tpl', array(
239      'OPTIONS' => $options,
240      'SELECTED' => $selected,
241      'NEW_FILE_URL' => $my_base_url.'&amp;tab=tpl&amp;newfile',
242      'NEW_FILE_CLASS' => empty($edited_file) ? '' : 'top_right'));
243
244    $editarea_options['syntax'] = 'html';
245    break;
246
247  case 'lang':
248    $selected = 0; 
249    $options[] = l10n('locfiledit_choose_file');
250    $options[] = '----------------------';
251    foreach (get_languages() as $language_code => $language_name)
252    {
253      $value = PHPWG_ROOT_PATH.'language/'.$language_code.'/local.lang.php';
254      if ($edited_file == $value)
255      {
256        $selected = $value;
257        $template->assign('show_default', array(
258          array('SHOW_DEFAULT' => LOCALEDIT_PATH
259                  . 'show_default.php?file='
260                  . 'language/'.$language_code.'/common.lang.php',
261                'FILE' => 'common.lang.php'),
262          array('SHOW_DEFAULT' => LOCALEDIT_PATH
263                  . 'show_default.php?file='
264                  . 'language/'.$language_code.'/admin.lang.php',
265                'FILE' => 'admin.lang.php')));
266      }
267      $options[$value] = $language_name;
268    }
269    $template->assign('css_lang_tpl', array(
270        'OPTIONS' => $options,
271        'SELECTED' => $selected));
272    $editarea_options['syntax'] = 'php';
273    break;
274   
275  case 'plug':
276    $edited_file = PHPWG_PLUGINS_PATH . "PersonalPlugin/main.inc.php";
277    $content_file = file_exists($edited_file) ?
278      file_get_contents($edited_file) : $new_file['plug'];
279    $editarea_options['syntax'] = 'php';
280    break;
281}
282
283// +-----------------------------------------------------------------------+
284// |                           Load backup file
285// +-----------------------------------------------------------------------+
286if (isset($_POST['restore']) and !is_adviser())
287{
288  $edited_file = $_POST['edited_file'];
289  $content_file = file_get_contents(get_bak_file($edited_file));
290  array_push($page['infos'],
291    l10n('locfiledit_bak_loaded1'),
292    l10n('locfiledit_bak_loaded2'));
293}
294
295// +-----------------------------------------------------------------------+
296// |                            Save file
297// +-----------------------------------------------------------------------+
298if (isset($_POST['submit']) and !is_adviser())
299{
300  $edited_file = $_POST['edited_file'];
301  $content_file = stripslashes($_POST['text']);
302  if (get_extension($edited_file) == 'php')
303        {
304    $content_file = eval_syntax($content_file);
305  }
306  if ($content_file === false)
307        {
308    array_push($page['errors'], l10n('locfiledit_syntax_error'));
309  }
310        else
311        {
312    if ($page['tab'] == 'plug' and !is_dir(PHPWG_PLUGINS_PATH . 'PersonalPlugin'))
313    {
314      @mkdir(PHPWG_PLUGINS_PATH . "PersonalPlugin");
315    }
316    if (file_exists($edited_file))
317    {
318      @copy($edited_file, get_bak_file($edited_file));
319      array_push($page['infos'], sprintf(l10n('locfiledit_saved_bak'), substr(get_bak_file($edited_file), 2)));
320    }
321   
322    if ($file = @fopen($edited_file , "w"))
323                {
324      @fwrite($file , $content_file);
325      @fclose($file);
326      array_unshift($page['infos'], l10n('locfiledit_save_config'));
327    }
328                else
329    {
330      array_push($page['errors'], l10n('locfiledit_cant_save'));
331    }
332  }
333}
334
335// +-----------------------------------------------------------------------+
336// |                            template initialization
337// +-----------------------------------------------------------------------+
338$template->set_filenames(array(
339    'plugin_admin_content' => dirname(__FILE__) . '/admin.tpl'));
340
341if (!empty($edited_file))
342{
343  if (!empty($page['errors']))
344        {
345    $content_file = stripslashes($_POST['text']);
346  }
347  $template->assign('zone_edit',
348    array('EDITED_FILE' => $edited_file,
349          'CONTENT_FILE' => htmlspecialchars($content_file),
350          'FILE_NAME' => trim($edited_file, './\\')));
351  if (file_exists(get_bak_file($edited_file)))
352  {
353    $template->assign('restore', true);
354  }
355  if (file_exists($edited_file))
356  {
357    $template->assign('restore_infos', true);
358  }
359 
360}
361
362$template->assign(array(
363  'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=plugin&amp;section=LocalFilesEditor%2Fadmin.php&amp;tab=' . $page['tab'],
364  'LOCALEDIT_PATH' => LOCALEDIT_PATH,
365  'LOAD_EDITAREA' => isset($conf['LocalFilesEditor']) ? $conf['LocalFilesEditor'] : 'off',
366  'EDITAREA_OPTIONS' => $editarea_options));
367
368$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
369
370?>
Note: See TracBrowser for help on using the repository browser.