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

Last change on this file since 10307 was 10307, checked in by patdenice, 13 years ago

feature:2262
Replace editarea by Codemirror:
http://codemirror.net

  • Property svn:eol-style set to LF
File size: 13.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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_root_url().'admin.php?page=plugin-'.basename(dirname(__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.'-localconf');
42$tabsheet->add('css',
43               l10n('locfiledit_onglet_css'),
44               $my_base_url.'-css');
45$tabsheet->add('tpl',
46               l10n('locfiledit_onglet_tpl'),
47               $my_base_url.'-tpl');
48$tabsheet->add('lang',
49               l10n('locfiledit_onglet_lang'),
50               $my_base_url.'-lang');
51$tabsheet->add('plug',
52               l10n('locfiledit_onglet_plug'),
53               $my_base_url.'-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// Edit selected file for CSS, template and language
78if ((isset($_POST['edit'])) and !is_numeric($_POST['file_to_edit']))
79{
80  $edited_file = $_POST['file_to_edit'];
81  $content_file = file_exists($edited_file) ? 
82    file_get_contents($edited_file) : $new_file[$page['tab']];
83}
84
85// Edit new tpl file
86if (isset($_POST['create_tpl']))
87{
88  $filename = $_POST['tpl_name'];
89  if (empty($filename))
90  {
91    array_push($page['errors'], l10n('locfiledit_empty_filename'));
92  }
93  if (get_extension($filename) != 'tpl')
94  {
95    $filename .= '.tpl';
96  }
97  if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename))
98  {
99    array_push($page['errors'], l10n('locfiledit_filename_error'));
100  }
101  if (is_numeric($_POST['tpl_model']) and $_POST['tpl_model'] != '0')
102  {
103    array_push($page['errors'], l10n('locfiledit_model_error'));
104  }
105  if (file_exists($_POST['tpl_parent'] . '/' . $filename))
106  {
107    array_push($page['errors'], l10n('locfiledit_file_already_exists'));
108  }
109  if (!empty($page['errors']))
110  {
111    $newfile_page = true;
112  }
113  else
114  {
115    $edited_file = $_POST['tpl_parent'] . '/' . $filename;
116    $content_file = ($_POST['tpl_model'] == '0') ? $new_file['tpl'] : file_get_contents($_POST['tpl_model']);
117  }
118}
119
120// +-----------------------------------------------------------------------+
121// |                            Process tabsheet
122// +-----------------------------------------------------------------------+
123switch ($page['tab'])
124{
125  case 'localconf':
126    $edited_file = PHPWG_ROOT_PATH.PWG_LOCAL_DIR . "config/config.inc.php";
127    $content_file = file_exists($edited_file) ?
128      file_get_contents($edited_file) : $new_file['localconf'];
129       
130    $template->assign('show_default' , array(
131        array('SHOW_DEFAULT' => LOCALEDIT_PATH
132                . 'show_default.php?file=include/config_default.inc.php',
133              'FILE' => 'config_default.inc.php')));
134    $codemirror_mode = 'application/x-httpd-php';
135    break;
136
137  case 'css':
138    $selected = 0; 
139    $options[] = l10n('locfiledit_choose_file');
140    $options[] = '----------------------';
141    $value = PHPWG_ROOT_PATH.PWG_LOCAL_DIR . "css/rules.css";
142    $options[$value] = 'local / css / rules.css';
143    if ($edited_file == $value) $selected = $value;
144    $options[] = '----------------------';
145       
146    foreach (get_dirs($conf['themes_dir']) as $theme_id)
147    {
148      $value = PHPWG_ROOT_PATH.PWG_LOCAL_DIR . 'css/'.$theme_id.'-rules.css';
149      $options[$value] = 'local / css / '.$theme_id.'-rules.css';
150      if ($edited_file == $value) $selected = $value;
151    }
152    $template->assign('css_lang_tpl', array(
153        'OPTIONS' => $options,
154        'SELECTED' => $selected));
155    $codemirror_mode = 'text/css';
156    break;
157
158  case 'tpl':
159    // New file form creation
160    if ($newfile_page)
161    {
162      $filename = isset($_POST['tpl_name']) ? $_POST['tpl_name'] : '';
163      $selected['model'] = isset($_POST['tpl_model']) ? $_POST['tpl_model'] : '0';
164      $selected['parent'] = isset($_POST['tpl_parent']) ? $_POST['tpl_parent'] : PHPWG_ROOT_PATH . 'template-extension';
165
166      // Parent directories list
167      $options['parent'] = array(PHPWG_ROOT_PATH . 'template-extension' => 'template-extension');
168      $options['parent'] = array_merge($options['parent'], get_rec_dirs(PHPWG_ROOT_PATH . 'template-extension'));
169
170      $options['model'][] = l10n('locfiledit_empty_page');
171      $options['model'][] = '----------------------';
172      $i = 0;
173      foreach (get_extents() as $pwg_template)
174      {
175        $value = PHPWG_ROOT_PATH . 'template-extension/' . $pwg_template;
176        $options['model'][$value] =  'template-extension / ' . str_replace('/', ' / ', $pwg_template);
177        $i++;
178      }
179      foreach (get_dirs($conf['themes_dir']) as $theme_id)
180      {
181        if ($i)
182        {
183          $options['model'][] = '----------------------';
184          $i = 0;
185        }
186        $dir = $conf['themes_dir'] . '/' . $theme_id . '/template/';
187        if (is_dir($dir) and $content = opendir($dir))
188        {
189          while ($node = readdir($content))
190          {
191            if (is_file($dir.$node) and get_extension($node) == 'tpl')
192            {
193              $value = $dir . $node;
194              $options['model'][$value] = $theme_id . ' / ' . $node;
195              $i++;
196            }
197          }
198        }
199      }
200      if (end($options['model']) == '----------------------')
201      {
202        array_pop($options['model']);
203      }
204      // Assign variables to template
205      $template->assign('create_tpl', array(
206        'NEW_FILE_NAME' => $filename,
207        'MODEL_OPTIONS' => $options['model'],
208        'MODEL_SELECTED' => $selected['model'],
209        'PARENT_OPTIONS' => $options['parent'],
210        'PARENT_SELECTED' => $selected['parent']));
211      break;
212    }
213    // List existing template extensions
214    $selected = 0; 
215    $options[] = l10n('locfiledit_choose_file');
216    $options[] = '----------------------';
217    foreach (get_extents() as $pwg_template)
218    {
219      $value = './template-extension/' . $pwg_template;
220      $options[$value] =  str_replace('/', ' / ', $pwg_template);
221      if ($edited_file == $value) $selected = $value;
222    }
223    if ($selected == 0 and !empty($edited_file))
224    {
225      $options[$edited_file] =  str_replace(array('./template-extension/', '/'), array('', ' / '), $edited_file);
226      $selected = $edited_file;
227    }
228    $template->assign('css_lang_tpl', array(
229      'OPTIONS' => $options,
230      'SELECTED' => $selected,
231      'NEW_FILE_URL' => $my_base_url.'-tpl&amp;newfile',
232      'NEW_FILE_CLASS' => empty($edited_file) ? '' : 'top_right'));
233
234    $codemirror_mode = 'text/html';
235    break;
236
237  case 'lang':
238    $selected = 0; 
239    $options[] = l10n('locfiledit_choose_file');
240    $options[] = '----------------------';
241    foreach (get_languages() as $language_code => $language_name)
242    {
243      $value = PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'language/'.$language_code.'.lang.php';
244      if ($edited_file == $value)
245      {
246        $selected = $value;
247        $template->assign('show_default', array(
248          array('SHOW_DEFAULT' => LOCALEDIT_PATH
249                  . 'show_default.php?file='
250                  . 'language/'.$language_code.'/common.lang.php',
251                'FILE' => 'common.lang.php'),
252          array('SHOW_DEFAULT' => LOCALEDIT_PATH
253                  . 'show_default.php?file='
254                  . 'language/'.$language_code.'/admin.lang.php',
255                'FILE' => 'admin.lang.php')));
256      }
257      $options[$value] = $language_name;
258    }
259    $template->assign('css_lang_tpl', array(
260        'OPTIONS' => $options,
261        'SELECTED' => $selected));
262    $codemirror_mode = 'application/x-httpd-php';
263    break;
264   
265  case 'plug':
266    $edited_file = PHPWG_PLUGINS_PATH . "PersonalPlugin/main.inc.php";
267    $content_file = file_exists($edited_file) ?
268      file_get_contents($edited_file) : $new_file['plug'];
269    $codemirror_mode = 'application/x-httpd-php';
270    break;
271}
272
273// +-----------------------------------------------------------------------+
274// |                           Load backup file
275// +-----------------------------------------------------------------------+
276if (isset($_POST['restore']))
277{
278  $edited_file = $_POST['edited_file'];
279  $content_file = file_get_contents(get_bak_file($edited_file));
280  array_push($page['infos'],
281    l10n('locfiledit_bak_loaded1'),
282    l10n('locfiledit_bak_loaded2'));
283}
284
285// +-----------------------------------------------------------------------+
286// |                            Save file
287// +-----------------------------------------------------------------------+
288if (isset($_POST['submit']))
289{
290  if (!is_webmaster())
291  {
292    array_push($page['errors'], l10n('locfiledit_webmaster_only'));
293  }
294  else
295  {
296    $edited_file = $_POST['edited_file'];
297    $content_file = stripslashes($_POST['text']);
298    if (get_extension($edited_file) == 'php')
299    {
300      $content_file = eval_syntax($content_file);
301    }
302    if ($content_file === false)
303    {
304      array_push($page['errors'], l10n('locfiledit_syntax_error'));
305    }
306    else
307    {
308      if ($page['tab'] == 'plug' and !is_dir(PHPWG_PLUGINS_PATH . 'PersonalPlugin'))
309      {
310        @mkdir(PHPWG_PLUGINS_PATH . "PersonalPlugin");
311      }
312      if (file_exists($edited_file))
313      {
314        @copy($edited_file, get_bak_file($edited_file));
315        array_push($page['infos'], sprintf(l10n('locfiledit_saved_bak'), substr(get_bak_file($edited_file), 2)));
316      }
317     
318      if ($file = @fopen($edited_file , "w"))
319      {
320        @fwrite($file , $content_file);
321        @fclose($file);
322        array_unshift($page['infos'], l10n('locfiledit_save_config'));
323        $template->delete_compiled_templates();
324      }
325      else
326      {
327        array_push($page['errors'], l10n('locfiledit_cant_save'));
328      }
329    }
330  }
331}
332
333// +-----------------------------------------------------------------------+
334// |                            template initialization
335// +-----------------------------------------------------------------------+
336$template->set_filenames(array(
337    'plugin_admin_content' => dirname(__FILE__) . '/admin.tpl'));
338
339if (!empty($edited_file))
340{
341  if (!empty($page['errors']))
342        {
343    $content_file = stripslashes($_POST['text']);
344  }
345  $template->assign('zone_edit',
346    array('EDITED_FILE' => $edited_file,
347          'CONTENT_FILE' => htmlspecialchars($content_file),
348          'FILE_NAME' => trim($edited_file, './\\')));
349  if (file_exists(get_bak_file($edited_file)))
350  {
351    $template->assign('restore', true);
352  }
353  if (file_exists($edited_file))
354  {
355    $template->assign('restore_infos', true);
356  }
357 
358}
359
360$template->assign(array(
361  'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=plugin-LocalFilesEditor-'.$page['tab'],
362  'LOCALEDIT_PATH' => LOCALEDIT_PATH,
363  'LOAD_CODEMIRROR' => isset($conf['LocalFilesEditor']) ? $conf['LocalFilesEditor'] : 'off',
364  'CODEMIRROR_MODE' => @$codemirror_mode
365  )
366);
367
368$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
369
370?>
Note: See TracBrowser for help on using the repository browser.