source: tags/release-2_0_0RC4/plugins/LocalFilesEditor/admin.php @ 3631

Last change on this file since 3631 was 2713, checked in by patdenice, 16 years ago

Merge from trunk 2712:

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