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

Last change on this file since 9359 was 9359, checked in by plg, 13 years ago

feature 2200: LocalFilesEditor plugin now uses the new cleaner URL alias for administration pages

  • Property svn:eol-style set to LF
File size: 13.6 KB
RevLine 
[2235]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[8728]5// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[2235]23
24if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
25include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
[2291]26include_once(LOCALEDIT_PATH.'functions.inc.php');
[2235]27load_language('plugin.lang', LOCALEDIT_PATH);
[9359]28$my_base_url = get_root_url().'admin.php?page=plugin-'.basename(dirname(__FILE__));
[2235]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'),
[9359]41               $my_base_url.'-localconf');
[5216]42$tabsheet->add('css',
43               l10n('locfiledit_onglet_css'),
[9359]44               $my_base_url.'-css');
[2235]45$tabsheet->add('tpl',
46               l10n('locfiledit_onglet_tpl'),
[9359]47               $my_base_url.'-tpl');
[2235]48$tabsheet->add('lang',
49               l10n('locfiledit_onglet_lang'),
[9359]50               $my_base_url.'-lang');
[2235]51$tabsheet->add('plug',
52               l10n('locfiledit_onglet_plug'),
[9359]53               $my_base_url.'-plug');
[2235]54$tabsheet->select($page['tab']);
55$tabsheet->assign();
56
57
58// +-----------------------------------------------------------------------+
59// |                            Variables init
60// +-----------------------------------------------------------------------+
[2588]61$edited_file = isset($_POST['edited_file']) ? $_POST['edited_file'] : '';
[2235]62$content_file = '';
[2588]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'];
[2235]67$new_file['plug'] = "<?php\n/*
68Plugin Name: " . l10n('locfiledit_onglet_plug') . "
69Version: 1.0
70Description: " . l10n('locfiledit_onglet_plug') . "
[3133]71Plugin URI: http://piwigo.org
[2235]72Author:
73Author URI:
74*/\n\n\n\n\n?>";
[2588]75$newfile_page = isset($_GET['newfile']) ? true : false;
[2235]76
[2291]77// Editarea options
[2588]78$editarea_options = array(
79  'language' => substr($user['language'], 0, 2),
[2291]80  'start_highlight' => true,
[2588]81  'allow_toggle' => false,
[2291]82  'toolbar' => 'search,fullscreen, |,select_font, |, undo, redo, change_smooth_selection, highlight, reset_highlight, |, help');
83
[2235]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
[2588]92// Edit new tpl file
93if (isset($_POST['create_tpl']))
94{
95  $filename = $_POST['tpl_name'];
[2906]96  if (empty($filename))
97  {
98    array_push($page['errors'], l10n('locfiledit_empty_filename'));
99  }
[2588]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'));
[2906]115  }
116  if (!empty($page['errors']))
117  {
[2588]118    $newfile_page = true;
119  }
[2906]120  else
[2588]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}
[2235]126
127// +-----------------------------------------------------------------------+
128// |                            Process tabsheet
129// +-----------------------------------------------------------------------+
130switch ($page['tab'])
131{
132  case 'localconf':
[8722]133    $edited_file = PHPWG_ROOT_PATH.PWG_LOCAL_DIR . "config/config.inc.php";
[2235]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')));
[2588]141    $editarea_options['syntax'] = 'php';
[2235]142    break;
143
[5216]144  case 'css':
145    $selected = 0; 
146    $options[] = l10n('locfiledit_choose_file');
147    $options[] = '----------------------';
[8722]148    $value = PHPWG_ROOT_PATH.PWG_LOCAL_DIR . "css/rules.css";
[5216]149    $options[$value] = 'local / css / rules.css';
150    if ($edited_file == $value) $selected = $value;
151    $options[] = '----------------------';
152       
153    foreach (get_dirs($conf['themes_dir']) as $theme_id)
154    {
[8722]155      $value = PHPWG_ROOT_PATH.PWG_LOCAL_DIR . 'css/'.$theme_id.'-rules.css';
[5216]156      $options[$value] = 'local / css / '.$theme_id.'-rules.css';
157      if ($edited_file == $value) $selected = $value;
158    }
159    $template->assign('css_lang_tpl', array(
160        'OPTIONS' => $options,
161        'SELECTED' => $selected));
162    $editarea_options['syntax'] = 'css';
163    break;
164
[2235]165  case 'tpl':
[2588]166    // New file form creation
[8126]167    if ($newfile_page)
[2235]168    {
[2588]169      $filename = isset($_POST['tpl_name']) ? $_POST['tpl_name'] : '';
170      $selected['model'] = isset($_POST['tpl_model']) ? $_POST['tpl_model'] : '0';
171      $selected['parent'] = isset($_POST['tpl_parent']) ? $_POST['tpl_parent'] : PHPWG_ROOT_PATH . 'template-extension';
172
173      // Parent directories list
174      $options['parent'] = array(PHPWG_ROOT_PATH . 'template-extension' => 'template-extension');
175      $options['parent'] = array_merge($options['parent'], get_rec_dirs(PHPWG_ROOT_PATH . 'template-extension'));
176
177      $options['model'][] = l10n('locfiledit_empty_page');
178      $options['model'][] = '----------------------';
[5216]179      $i = 0;
[2588]180      foreach (get_extents() as $pwg_template)
[2235]181      {
[2588]182        $value = PHPWG_ROOT_PATH . 'template-extension/' . $pwg_template;
183        $options['model'][$value] =  'template-extension / ' . str_replace('/', ' / ', $pwg_template);
[5216]184        $i++;
[2588]185      }
[5216]186      foreach (get_dirs($conf['themes_dir']) as $theme_id)
[2588]187      {
[5216]188        if ($i)
[2235]189        {
[2588]190          $options['model'][] = '----------------------';
[5216]191          $i = 0;
[2588]192        }
[5216]193        $dir = $conf['themes_dir'] . '/' . $theme_id . '/template/';
[2588]194        if (is_dir($dir) and $content = opendir($dir))
195        {
196          while ($node = readdir($content))
[2235]197          {
[5216]198            if (is_file($dir.$node) and get_extension($node) == 'tpl')
[2588]199            {
200              $value = $dir . $node;
[5216]201              $options['model'][$value] = $theme_id . ' / ' . $node;
202              $i++;
[2588]203            }
[2235]204          }
205        }
206      }
[5216]207      if (end($options['model']) == '----------------------')
208      {
209        array_pop($options['model']);
210      }
[2588]211      // Assign variables to template
212      $template->assign('create_tpl', array(
213        'NEW_FILE_NAME' => $filename,
214        'MODEL_OPTIONS' => $options['model'],
215        'MODEL_SELECTED' => $selected['model'],
216        'PARENT_OPTIONS' => $options['parent'],
217        'PARENT_SELECTED' => $selected['parent']));
218      break;
[2235]219    }
[2588]220    // List existing template extensions
221    $selected = 0; 
222    $options[] = l10n('locfiledit_choose_file');
223    $options[] = '----------------------';
224    foreach (get_extents() as $pwg_template)
225    {
226      $value = './template-extension/' . $pwg_template;
227      $options[$value] =  str_replace('/', ' / ', $pwg_template);
228      if ($edited_file == $value) $selected = $value;
229    }
230    if ($selected == 0 and !empty($edited_file))
231    {
232      $options[$edited_file] =  str_replace(array('./template-extension/', '/'), array('', ' / '), $edited_file);
233      $selected = $edited_file;
234    }
[2235]235    $template->assign('css_lang_tpl', array(
[2588]236      'OPTIONS' => $options,
237      'SELECTED' => $selected,
[9359]238      'NEW_FILE_URL' => $my_base_url.'-tpl&amp;newfile',
[2588]239      'NEW_FILE_CLASS' => empty($edited_file) ? '' : 'top_right'));
240
241    $editarea_options['syntax'] = 'html';
[2235]242    break;
243
244  case 'lang':
[2588]245    $selected = 0; 
246    $options[] = l10n('locfiledit_choose_file');
[2235]247    $options[] = '----------------------';
248    foreach (get_languages() as $language_code => $language_name)
249    {
[8722]250      $value = PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'language/'.$language_code.'.lang.php';
[2235]251      if ($edited_file == $value)
252      {
253        $selected = $value;
254        $template->assign('show_default', array(
255          array('SHOW_DEFAULT' => LOCALEDIT_PATH
256                  . 'show_default.php?file='
257                  . 'language/'.$language_code.'/common.lang.php',
258                'FILE' => 'common.lang.php'),
259          array('SHOW_DEFAULT' => LOCALEDIT_PATH
260                  . 'show_default.php?file='
261                  . 'language/'.$language_code.'/admin.lang.php',
262                'FILE' => 'admin.lang.php')));
263      }
264      $options[$value] = $language_name;
265    }
266    $template->assign('css_lang_tpl', array(
267        'OPTIONS' => $options,
268        'SELECTED' => $selected));
[2588]269    $editarea_options['syntax'] = 'php';
[2235]270    break;
271   
272  case 'plug':
273    $edited_file = PHPWG_PLUGINS_PATH . "PersonalPlugin/main.inc.php";
274    $content_file = file_exists($edited_file) ?
275      file_get_contents($edited_file) : $new_file['plug'];
[2588]276    $editarea_options['syntax'] = 'php';
[2235]277    break;
278}
279
280// +-----------------------------------------------------------------------+
281// |                           Load backup file
282// +-----------------------------------------------------------------------+
[8126]283if (isset($_POST['restore']))
[2235]284{
285  $edited_file = $_POST['edited_file'];
[2588]286  $content_file = file_get_contents(get_bak_file($edited_file));
[2235]287  array_push($page['infos'],
[2588]288    l10n('locfiledit_bak_loaded1'),
289    l10n('locfiledit_bak_loaded2'));
[2235]290}
291
292// +-----------------------------------------------------------------------+
293// |                            Save file
294// +-----------------------------------------------------------------------+
[8126]295if (isset($_POST['submit']))
[2235]296{
[5272]297  if (!is_webmaster())
[5256]298  {
299    array_push($page['errors'], l10n('locfiledit_webmaster_only'));
[2235]300  }
[5256]301  else
302  {
303    $edited_file = $_POST['edited_file'];
304    $content_file = stripslashes($_POST['text']);
305    if (get_extension($edited_file) == 'php')
[2235]306    {
[5256]307      $content_file = eval_syntax($content_file);
[2235]308    }
[5256]309    if ($content_file === false)
[2235]310    {
[5256]311      array_push($page['errors'], l10n('locfiledit_syntax_error'));
[2235]312    }
[5256]313    else
[2235]314    {
[5256]315      if ($page['tab'] == 'plug' and !is_dir(PHPWG_PLUGINS_PATH . 'PersonalPlugin'))
316      {
317        @mkdir(PHPWG_PLUGINS_PATH . "PersonalPlugin");
318      }
319      if (file_exists($edited_file))
320      {
321        @copy($edited_file, get_bak_file($edited_file));
322        array_push($page['infos'], sprintf(l10n('locfiledit_saved_bak'), substr(get_bak_file($edited_file), 2)));
323      }
324     
325      if ($file = @fopen($edited_file , "w"))
326      {
327        @fwrite($file , $content_file);
328        @fclose($file);
329        array_unshift($page['infos'], l10n('locfiledit_save_config'));
330        $template->delete_compiled_templates();
331      }
332      else
333      {
334        array_push($page['errors'], l10n('locfiledit_cant_save'));
335      }
[2235]336    }
337  }
338}
339
340// +-----------------------------------------------------------------------+
341// |                            template initialization
342// +-----------------------------------------------------------------------+
343$template->set_filenames(array(
344    'plugin_admin_content' => dirname(__FILE__) . '/admin.tpl'));
345
346if (!empty($edited_file))
347{
348  if (!empty($page['errors']))
349        {
350    $content_file = stripslashes($_POST['text']);
351  }
352  $template->assign('zone_edit',
353    array('EDITED_FILE' => $edited_file,
354          'CONTENT_FILE' => htmlspecialchars($content_file),
355          'FILE_NAME' => trim($edited_file, './\\')));
[2588]356  if (file_exists(get_bak_file($edited_file)))
[2235]357  {
358    $template->assign('restore', true);
359  }
[2588]360  if (file_exists($edited_file))
361  {
362    $template->assign('restore_infos', true);
363  }
364 
[2235]365}
366
[2588]367$template->assign(array(
[9359]368  'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=plugin-LocalFilesEditor-'.$page['tab'],
[2588]369  'LOCALEDIT_PATH' => LOCALEDIT_PATH,
370  'LOAD_EDITAREA' => isset($conf['LocalFilesEditor']) ? $conf['LocalFilesEditor'] : 'off',
371  'EDITAREA_OPTIONS' => $editarea_options));
[2291]372
[2235]373$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
374
375?>
Note: See TracBrowser for help on using the repository browser.