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

Last change on this file since 2588 was 2588, checked in by patdenice, 16 years ago
  • Move get_extents function to include/functions.inc.php.
  • Change all plugins version to 2.0.
  • LocalFiles Editor can now edit and create template extension.
  • Editarea (for LocalFiles Editor) go to version 0.7.2.2 (chrome compatible)
  • Editarea activation is now saved in database with AJAX.
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 14.3 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          'header.tpl',
189          'footer.tpl',
190          'index.tpl',
191          'nbm.tpl',
192          'notification.tpl',
193          'picture_content.tpl',
194          'picture.tpl',
195          'popuphelp.tpl',
196          'profile.tpl',
197          'profile_content.tpl',
198          'register.tpl',
199          'search.tpl',
200          'search_rules.tpl',
201          'slideshow.tpl',
202          'tags.tpl',
203          'upload.tpl');
204
205      $options['model'][] = l10n('locfiledit_empty_page');
206      $options['model'][] = '----------------------';
207      foreach (get_extents() as $pwg_template)
208      {
209        $value = PHPWG_ROOT_PATH . 'template-extension/' . $pwg_template;
210        $options['model'][$value] =  'template-extension / ' . str_replace('/', ' / ', $pwg_template);
211      }
212      $template_dir = PHPWG_ROOT_PATH . 'template';
213      foreach (get_dirs($template_dir) as $pwg_template)
214      {
215        if (count($options['model']) > 2)
216        {
217          $options['model'][] = '----------------------';
218        }
219        $dir = $template_dir . '/' . $pwg_template . '/';
220        if (is_dir($dir) and $content = opendir($dir))
221        {
222          while ($node = readdir($content))
223          {
224            if (is_file($dir . $node) and in_array($node, $eligible_templates))
225            {
226              $value = $dir . $node;
227              $options['model'][$value] = $pwg_template . ' / ' . $node;
228            }
229          }
230        }
231      }
232      // Assign variables to template
233      $template->assign('create_tpl', array(
234        'NEW_FILE_NAME' => $filename,
235        'MODEL_OPTIONS' => $options['model'],
236        'MODEL_SELECTED' => $selected['model'],
237        'PARENT_OPTIONS' => $options['parent'],
238        'PARENT_SELECTED' => $selected['parent']));
239      break;
240    }
241    // List existing template extensions
242    $template_dir = PHPWG_ROOT_PATH . 'template-extension';
243    $selected = 0; 
244    $options[] = l10n('locfiledit_choose_file');
245    $options[] = '----------------------';
246    foreach (get_extents() as $pwg_template)
247    {
248      $value = './template-extension/' . $pwg_template;
249      $options[$value] =  str_replace('/', ' / ', $pwg_template);
250      if ($edited_file == $value) $selected = $value;
251    }
252    if ($selected == 0 and !empty($edited_file))
253    {
254      $options[$edited_file] =  str_replace(array('./template-extension/', '/'), array('', ' / '), $edited_file);
255      $selected = $edited_file;
256    }
257    $template->assign('css_lang_tpl', array(
258      'OPTIONS' => $options,
259      'SELECTED' => $selected,
260      'NEW_FILE_URL' => $my_base_url.'&amp;tab=tpl&amp;newfile',
261      'NEW_FILE_CLASS' => empty($edited_file) ? '' : 'top_right'));
262
263    $editarea_options['syntax'] = 'html';
264    break;
265
266  case 'lang':
267    $selected = 0; 
268    $options[] = l10n('locfiledit_choose_file');
269    $options[] = '----------------------';
270    foreach (get_languages() as $language_code => $language_name)
271    {
272      $value = PHPWG_ROOT_PATH.'language/'.$language_code.'/local.lang.php';
273      if ($edited_file == $value)
274      {
275        $selected = $value;
276        $template->assign('show_default', array(
277          array('SHOW_DEFAULT' => LOCALEDIT_PATH
278                  . 'show_default.php?file='
279                  . 'language/'.$language_code.'/common.lang.php',
280                'FILE' => 'common.lang.php'),
281          array('SHOW_DEFAULT' => LOCALEDIT_PATH
282                  . 'show_default.php?file='
283                  . 'language/'.$language_code.'/admin.lang.php',
284                'FILE' => 'admin.lang.php')));
285      }
286      $options[$value] = $language_name;
287    }
288    $template->assign('css_lang_tpl', array(
289        'OPTIONS' => $options,
290        'SELECTED' => $selected));
291    $editarea_options['syntax'] = 'php';
292    break;
293   
294  case 'plug':
295    $edited_file = PHPWG_PLUGINS_PATH . "PersonalPlugin/main.inc.php";
296    $content_file = file_exists($edited_file) ?
297      file_get_contents($edited_file) : $new_file['plug'];
298    $editarea_options['syntax'] = 'php';
299    break;
300}
301
302// +-----------------------------------------------------------------------+
303// |                           Load backup file
304// +-----------------------------------------------------------------------+
305if (isset($_POST['restore']) and !is_adviser())
306{
307  $edited_file = $_POST['edited_file'];
308  $content_file = file_get_contents(get_bak_file($edited_file));
309  array_push($page['infos'],
310    l10n('locfiledit_bak_loaded1'),
311    l10n('locfiledit_bak_loaded2'));
312}
313
314// +-----------------------------------------------------------------------+
315// |                            Save file
316// +-----------------------------------------------------------------------+
317if (isset($_POST['submit']) and !is_adviser())
318{
319  $edited_file = $_POST['edited_file'];
320  $content_file = stripslashes($_POST['text']);
321  if (get_extension($edited_file) == 'php')
322        {
323    $content_file = eval_syntax($content_file);
324  }
325  if ($content_file === false)
326        {
327    array_push($page['errors'], l10n('locfiledit_syntax_error'));
328  }
329        else
330        {
331    if ($page['tab'] == 'plug' and !is_dir(PHPWG_PLUGINS_PATH . 'PersonalPlugin'))
332    {
333      @mkdir(PHPWG_PLUGINS_PATH . "PersonalPlugin");
334    }
335    if (file_exists($edited_file))
336    {
337      @copy($edited_file, get_bak_file($edited_file));
338      array_push($page['infos'], sprintf(l10n('locfiledit_saved_bak'), substr(get_bak_file($edited_file), 2)));
339    }
340   
341    if ($file = @fopen($edited_file , "w"))
342                {
343      @fwrite($file , $content_file);
344      @fclose($file);
345      array_unshift($page['infos'], l10n('locfiledit_save_config'));
346    }
347                else
348    {
349      array_push($page['errors'], l10n('locfiledit_cant_save'));
350    }
351  }
352}
353
354// +-----------------------------------------------------------------------+
355// |                            template initialization
356// +-----------------------------------------------------------------------+
357$template->set_filenames(array(
358    'plugin_admin_content' => dirname(__FILE__) . '/admin.tpl'));
359
360if (!empty($edited_file))
361{
362  if (!empty($page['errors']))
363        {
364    $content_file = stripslashes($_POST['text']);
365  }
366  $template->assign('zone_edit',
367    array('EDITED_FILE' => $edited_file,
368          'CONTENT_FILE' => htmlspecialchars($content_file),
369          'FILE_NAME' => trim($edited_file, './\\')));
370  if (file_exists(get_bak_file($edited_file)))
371  {
372    $template->assign('restore', true);
373  }
374  if (file_exists($edited_file))
375  {
376    $template->assign('restore_infos', true);
377  }
378 
379}
380
381$template->assign(array(
382  'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=plugin&amp;section=LocalFilesEditor%2Fadmin.php&amp;tab=' . $page['tab'],
383  'LOCALEDIT_PATH' => LOCALEDIT_PATH,
384  'LOAD_EDITAREA' => isset($conf['LocalFilesEditor']) ? $conf['LocalFilesEditor'] : 'off',
385  'EDITAREA_OPTIONS' => $editarea_options));
386
387$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
388
389?>
Note: See TracBrowser for help on using the repository browser.