source: branches/2.0/plugins/LocalFilesEditor/admin.php @ 3183

Last change on this file since 3183 was 3183, checked in by patdenice, 15 years ago

merge part of r3182 from trunk to branch 2.0.
Re-order templates extension alphabetically.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 14.6 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('css',
43               l10n('locfiledit_onglet_css'),
44               $my_base_url.'&amp;tab=css');
45$tabsheet->add('tpl',
46               l10n('locfiledit_onglet_tpl'),
47               $my_base_url.'&amp;tab=tpl');
48$tabsheet->add('lang',
49               l10n('locfiledit_onglet_lang'),
50               $my_base_url.'&amp;tab=lang');
51$tabsheet->add('plug',
52               l10n('locfiledit_onglet_plug'),
53               $my_base_url.'&amp;tab=plug');
54$tabsheet->select($page['tab']);
55$tabsheet->assign();
56
57
58// +-----------------------------------------------------------------------+
59// |                            Variables init
60// +-----------------------------------------------------------------------+
61$edited_file = isset($_POST['edited_file']) ? $_POST['edited_file'] : '';
62$content_file = '';
63$new_file['localconf'] = "<?php\n\n/* ".l10n('locfiledit_newfile')." */\n\n\n\n\n?>";
64$new_file['css'] = "/* " . l10n('locfiledit_newfile') . " */\n\n";
65$new_file['tpl'] = "{* " . l10n('locfiledit_newfile') . " *}\n\n";
66$new_file['lang'] = $new_file['localconf'];
67$new_file['plug'] = "<?php\n/*
68Plugin Name: " . l10n('locfiledit_onglet_plug') . "
69Version: 1.0
70Description: " . l10n('locfiledit_onglet_plug') . "
71Plugin URI: http://piwigo.org
72Author:
73Author URI:
74*/\n\n\n\n\n?>";
75$newfile_page = isset($_GET['newfile']) ? true : false;
76
77// Editarea options
78$editarea_options = array(
79  'language' => substr($user['language'], 0, 2),
80  'start_highlight' => true,
81  'allow_toggle' => false,
82  'toolbar' => 'search,fullscreen, |,select_font, |, undo, redo, change_smooth_selection, highlight, reset_highlight, |, help');
83
84// Edit selected file for CSS, template and language
85if ((isset($_POST['edit'])) and !is_numeric($_POST['file_to_edit']))
86{
87  $edited_file = $_POST['file_to_edit'];
88  $content_file = file_exists($edited_file) ? 
89    file_get_contents($edited_file) : $new_file[$page['tab']];
90}
91
92// Edit new tpl file
93if (isset($_POST['create_tpl']))
94{
95  $filename = $_POST['tpl_name'];
96  if (empty($filename))
97  {
98    array_push($page['errors'], l10n('locfiledit_empty_filename'));
99  }
100  if (get_extension($filename) != 'tpl')
101  {
102    $filename .= '.tpl';
103  }
104  if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename))
105  {
106    array_push($page['errors'], l10n('locfiledit_filename_error'));
107  }
108  if (is_numeric($_POST['tpl_model']) and $_POST['tpl_model'] != '0')
109  {
110    array_push($page['errors'], l10n('locfiledit_model_error'));
111  }
112  if (file_exists($_POST['tpl_parent'] . '/' . $filename))
113  {
114    array_push($page['errors'], l10n('locfiledit_file_already_exists'));
115  }
116  if (!empty($page['errors']))
117  {
118    $newfile_page = true;
119  }
120  else
121  {
122    $edited_file = $_POST['tpl_parent'] . '/' . $filename;
123    $content_file = ($_POST['tpl_model'] == '0') ? $new_file['tpl'] : file_get_contents($_POST['tpl_model']);
124  }
125}
126
127// +-----------------------------------------------------------------------+
128// |                            Process tabsheet
129// +-----------------------------------------------------------------------+
130switch ($page['tab'])
131{
132  case 'localconf':
133    $edited_file = PHPWG_ROOT_PATH . "include/config_local.inc.php";
134    $content_file = file_exists($edited_file) ?
135      file_get_contents($edited_file) : $new_file['localconf'];
136       
137    $template->assign('show_default' , array(
138        array('SHOW_DEFAULT' => LOCALEDIT_PATH
139                . 'show_default.php?file=include/config_default.inc.php',
140              'FILE' => 'config_default.inc.php')));
141    $editarea_options['syntax'] = 'php';
142    break;
143
144  case 'css':
145    $template_dir = PHPWG_ROOT_PATH . 'template';
146    $selected = 0; 
147    $options[] = l10n('locfiledit_choose_file');
148    $options[] = '----------------------';
149    $value = PHPWG_ROOT_PATH . "template-common/local-layout.css";
150    $options[$value] = 'template-common / local-layout.css';
151    if ($edited_file == $value) $selected = $value;
152       
153    foreach (get_dirs($template_dir) as $pwg_template)
154    {
155      $options[] = '----------------------';
156      $value = $template_dir . '/' . $pwg_template . '/local-layout.css';
157      $options[$value] = $pwg_template . ' / local-layout.css';
158      if ($edited_file == $value) $selected = $value;
159      $options[] = '----------------------';
160      foreach (get_dirs($template_dir.'/'.$pwg_template.'/theme') as $theme)
161      {
162        $value = $template_dir.'/'.$pwg_template.'/theme/'.$theme.'/theme.css';
163        $options[$value] = $pwg_template . ' / ' . $theme . ' / theme.css';
164        if ($edited_file == $value) $selected = $value;
165      }
166    }
167    $template->assign('css_lang_tpl', array(
168        'OPTIONS' => $options,
169        'SELECTED' => $selected));
170    $editarea_options['syntax'] = 'css';
171    break;
172 
173  case 'tpl':
174    // New file form creation
175    if ($newfile_page and !is_adviser())
176    {
177      $filename = isset($_POST['tpl_name']) ? $_POST['tpl_name'] : '';
178      $selected['model'] = isset($_POST['tpl_model']) ? $_POST['tpl_model'] : '0';
179      $selected['parent'] = isset($_POST['tpl_parent']) ? $_POST['tpl_parent'] : PHPWG_ROOT_PATH . 'template-extension';
180
181      // Parent directories list
182      $options['parent'] = array(PHPWG_ROOT_PATH . 'template-extension' => 'template-extension');
183      $options['parent'] = array_merge($options['parent'], get_rec_dirs(PHPWG_ROOT_PATH . 'template-extension'));
184
185      // Model list
186      $eligible_templates = array(
187          'about.tpl',
188          'footer.tpl',
189          'header.tpl',
190          'identification.tpl',
191          'index.tpl',
192          'mainpage_categories.tpl',
193          'menubar.tpl',
194          'menubar_categories.tpl',
195          'menubar_identification.tpl',
196          'menubar_links.tpl',
197          'menubar_menu.tpl',
198          'menubar_specials.tpl',
199          'menubar_tags.tpl',
200          'nbm.tpl',
201          'notification.tpl',
202          'picture.tpl',
203          'picture_content.tpl',
204          'popuphelp.tpl',
205          'profile.tpl',
206          'profile_content.tpl',
207          'redirect.tpl',
208          'register.tpl',
209          'search.tpl',
210          'search_rules.tpl',
211          'slideshow.tpl',
212          'tags.tpl',
213          'thumbnails.tpl',
214          'upload.tpl');
215
216      $options['model'][] = l10n('locfiledit_empty_page');
217      $options['model'][] = '----------------------';
218      foreach (get_extents() as $pwg_template)
219      {
220        $value = PHPWG_ROOT_PATH . 'template-extension/' . $pwg_template;
221        $options['model'][$value] =  'template-extension / ' . str_replace('/', ' / ', $pwg_template);
222      }
223      $template_dir = PHPWG_ROOT_PATH . 'template';
224      foreach (get_dirs($template_dir) as $pwg_template)
225      {
226        if (count($options['model']) > 2)
227        {
228          $options['model'][] = '----------------------';
229        }
230        $dir = $template_dir . '/' . $pwg_template . '/';
231        if (is_dir($dir) and $content = opendir($dir))
232        {
233          while ($node = readdir($content))
234          {
235            if (is_file($dir . $node) and in_array($node, $eligible_templates))
236            {
237              $value = $dir . $node;
238              $options['model'][$value] = $pwg_template . ' / ' . $node;
239            }
240          }
241        }
242      }
243      // Assign variables to template
244      $template->assign('create_tpl', array(
245        'NEW_FILE_NAME' => $filename,
246        'MODEL_OPTIONS' => $options['model'],
247        'MODEL_SELECTED' => $selected['model'],
248        'PARENT_OPTIONS' => $options['parent'],
249        'PARENT_SELECTED' => $selected['parent']));
250      break;
251    }
252    // List existing template extensions
253    $template_dir = PHPWG_ROOT_PATH . 'template-extension';
254    $selected = 0; 
255    $options[] = l10n('locfiledit_choose_file');
256    $options[] = '----------------------';
257    foreach (get_extents() as $pwg_template)
258    {
259      $value = './template-extension/' . $pwg_template;
260      $options[$value] =  str_replace('/', ' / ', $pwg_template);
261      if ($edited_file == $value) $selected = $value;
262    }
263    if ($selected == 0 and !empty($edited_file))
264    {
265      $options[$edited_file] =  str_replace(array('./template-extension/', '/'), array('', ' / '), $edited_file);
266      $selected = $edited_file;
267    }
268    $template->assign('css_lang_tpl', array(
269      'OPTIONS' => $options,
270      'SELECTED' => $selected,
271      'NEW_FILE_URL' => $my_base_url.'&amp;tab=tpl&amp;newfile',
272      'NEW_FILE_CLASS' => empty($edited_file) ? '' : 'top_right'));
273
274    $editarea_options['syntax'] = 'html';
275    break;
276
277  case 'lang':
278    $selected = 0; 
279    $options[] = l10n('locfiledit_choose_file');
280    $options[] = '----------------------';
281    foreach (get_languages() as $language_code => $language_name)
282    {
283      $value = PHPWG_ROOT_PATH.'language/'.$language_code.'/local.lang.php';
284      if ($edited_file == $value)
285      {
286        $selected = $value;
287        $template->assign('show_default', array(
288          array('SHOW_DEFAULT' => LOCALEDIT_PATH
289                  . 'show_default.php?file='
290                  . 'language/'.$language_code.'/common.lang.php',
291                'FILE' => 'common.lang.php'),
292          array('SHOW_DEFAULT' => LOCALEDIT_PATH
293                  . 'show_default.php?file='
294                  . 'language/'.$language_code.'/admin.lang.php',
295                'FILE' => 'admin.lang.php')));
296      }
297      $options[$value] = $language_name;
298    }
299    $template->assign('css_lang_tpl', array(
300        'OPTIONS' => $options,
301        'SELECTED' => $selected));
302    $editarea_options['syntax'] = 'php';
303    break;
304   
305  case 'plug':
306    $edited_file = PHPWG_PLUGINS_PATH . "PersonalPlugin/main.inc.php";
307    $content_file = file_exists($edited_file) ?
308      file_get_contents($edited_file) : $new_file['plug'];
309    $editarea_options['syntax'] = 'php';
310    break;
311}
312
313// +-----------------------------------------------------------------------+
314// |                           Load backup file
315// +-----------------------------------------------------------------------+
316if (isset($_POST['restore']) and !is_adviser())
317{
318  $edited_file = $_POST['edited_file'];
319  $content_file = file_get_contents(get_bak_file($edited_file));
320  array_push($page['infos'],
321    l10n('locfiledit_bak_loaded1'),
322    l10n('locfiledit_bak_loaded2'));
323}
324
325// +-----------------------------------------------------------------------+
326// |                            Save file
327// +-----------------------------------------------------------------------+
328if (isset($_POST['submit']) and !is_adviser())
329{
330  $edited_file = $_POST['edited_file'];
331  $content_file = stripslashes($_POST['text']);
332  if (get_extension($edited_file) == 'php')
333        {
334    $content_file = eval_syntax($content_file);
335  }
336  if ($content_file === false)
337        {
338    array_push($page['errors'], l10n('locfiledit_syntax_error'));
339  }
340        else
341        {
342    if ($page['tab'] == 'plug' and !is_dir(PHPWG_PLUGINS_PATH . 'PersonalPlugin'))
343    {
344      @mkdir(PHPWG_PLUGINS_PATH . "PersonalPlugin");
345    }
346    if (file_exists($edited_file))
347    {
348      @copy($edited_file, get_bak_file($edited_file));
349      array_push($page['infos'], sprintf(l10n('locfiledit_saved_bak'), substr(get_bak_file($edited_file), 2)));
350    }
351   
352    if ($file = @fopen($edited_file , "w"))
353                {
354      @fwrite($file , $content_file);
355      @fclose($file);
356      array_unshift($page['infos'], l10n('locfiledit_save_config'));
357    }
358                else
359    {
360      array_push($page['errors'], l10n('locfiledit_cant_save'));
361    }
362  }
363}
364
365// +-----------------------------------------------------------------------+
366// |                            template initialization
367// +-----------------------------------------------------------------------+
368$template->set_filenames(array(
369    'plugin_admin_content' => dirname(__FILE__) . '/admin.tpl'));
370
371if (!empty($edited_file))
372{
373  if (!empty($page['errors']))
374        {
375    $content_file = stripslashes($_POST['text']);
376  }
377  $template->assign('zone_edit',
378    array('EDITED_FILE' => $edited_file,
379          'CONTENT_FILE' => htmlspecialchars($content_file),
380          'FILE_NAME' => trim($edited_file, './\\')));
381  if (file_exists(get_bak_file($edited_file)))
382  {
383    $template->assign('restore', true);
384  }
385  if (file_exists($edited_file))
386  {
387    $template->assign('restore_infos', true);
388  }
389 
390}
391
392$template->assign(array(
393  'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=plugin&amp;section=LocalFilesEditor%2Fadmin.php&amp;tab=' . $page['tab'],
394  'LOCALEDIT_PATH' => LOCALEDIT_PATH,
395  'LOAD_EDITAREA' => isset($conf['LocalFilesEditor']) ? $conf['LocalFilesEditor'] : 'off',
396  'EDITAREA_OPTIONS' => $editarea_options));
397
398$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
399
400?>
Note: See TracBrowser for help on using the repository browser.