source: trunk/admin/site_manager.php @ 4325

Last change on this file since 4325 was 4325, checked in by nikrou, 14 years ago

Feature 1244 resolved
Replace all mysql functions in core code by ones independant of database engine

Fix small php code synxtax : hash must be accessed with [ ] and not { }.

  • Property svn:eol-style set to LF
File size: 9.3 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'))
25{
26  die ("Hacking attempt!");
27}
28
29include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
30
31// +-----------------------------------------------------------------------+
32// | Check Access and exit when user status is not ok                      |
33// +-----------------------------------------------------------------------+
34check_status(ACCESS_ADMINISTRATOR);
35
36/**
37 * requests the given $url (a remote create_listing_file.php) and fills a
38 * list of lines corresponding to request output
39 *
40 * @param string $url
41 * @return void
42 */
43function remote_output($url)
44{
45  global $template, $page;
46
47  if (fetchRemote($url, $result))
48  {
49    $lines = explode("\r\n", $result);
50    // cleaning lines from HTML tags
51    foreach ($lines as $line)
52    {
53      $line = trim(strip_tags($line));
54      if (preg_match('/^PWG-([A-Z]+)-/', $line, $matches))
55      {
56        $template->append(
57          'remote_output',
58          array(
59            'CLASS' => 'remote'.ucfirst(strtolower($matches[1])),
60            'CONTENT' => $line
61           )
62         );
63      }
64    }
65  }
66  else
67  {
68    array_push($page['errors'], l10n('site_err_remote_file_not_found'));
69  }
70}
71
72
73// +-----------------------------------------------------------------------+
74// |                             template init                             |
75// +-----------------------------------------------------------------------+
76$template->set_filenames(array('site_manager'=>'site_manager.tpl'));
77
78// +-----------------------------------------------------------------------+
79// |                        new site creation form                         |
80// +-----------------------------------------------------------------------+
81if (isset($_POST['submit']) and !empty($_POST['galleries_url'])
82    and !is_adviser() )
83{
84  $is_remote = url_is_remote( $_POST['galleries_url'] );
85  $url = preg_replace('/[\/]*$/', '', $_POST['galleries_url']);
86  $url.= '/';
87  if (! $is_remote)
88  {
89    if ( ! (strpos($url, '.') === 0 ) )
90    {
91      $url = './' . $url;
92    }
93  }
94
95  // site must not exists
96  $query = '
97SELECT COUNT(id) AS count
98  FROM '.SITES_TABLE.'
99  WHERE galleries_url = \''.$url.'\'
100;';
101  $row = pwg_db_fetch_assoc(pwg_query($query));
102  if ($row['count'] > 0)
103  {
104    array_push($page['errors'],
105      l10n('site_already_exists').' ['.$url.']');
106  }
107  if (count($page['errors']) == 0)
108  {
109    if ($is_remote)
110    {
111      if ( ! isset($_POST['no_check']) )
112      {
113        $clf_url = $url.'create_listing_file.php';
114        $clf_url.= '?action=test';
115        $clf_url.= '&version='.PHPWG_VERSION;
116        if (fetchRemote($clf_url, $result))
117        {
118          $lines = explode("\r\n", $result);
119          $first_line = strip_tags($lines[0]);
120          if (!preg_match('/^PWG-INFO-2:/', $first_line))
121          {
122            array_push($page['errors'],
123                       l10n('site_err').' : '.$first_line);
124          }
125        }
126        else
127        {
128          array_push($page['errors'], l10n('site_err_remote_file_not_found') );
129        }
130      }
131    }
132    else
133    { // local directory
134      if ( ! file_exists($url) )
135      {
136        array_push($page['errors'],
137          l10n('Directory does not exist').' ['.$url.']');
138      }
139    }
140  }
141
142  if (count($page['errors']) == 0)
143  {
144    $query = '
145INSERT INTO '.SITES_TABLE.'
146  (galleries_url)
147  VALUES
148  (\''.$url.'\')
149;';
150    pwg_query($query);
151    array_push($page['infos'],
152               $url.' '.l10n('site_created'));
153  }
154}
155
156// +-----------------------------------------------------------------------+
157// |                            actions on site                            |
158// +-----------------------------------------------------------------------+
159if (isset($_GET['site']) and is_numeric($_GET['site']))
160{
161  $page['site'] = $_GET['site'];
162}
163if (isset($_GET['action']) and isset($page['site']) and !is_adviser())
164{
165  $query = '
166SELECT galleries_url
167  FROM '.SITES_TABLE.'
168  WHERE id = '.$page['site'].'
169;';
170  list($galleries_url) = pwg_db_fetch_row(pwg_query($query));
171  switch($_GET['action'])
172  {
173    case 'generate' :
174    {
175      $title = $galleries_url.' : '.l10n('remote_site_generate');
176      remote_output($galleries_url.'create_listing_file.php?action=generate');
177      break;
178    }
179    case 'test' :
180    {
181      $title = $galleries_url.' : '.l10n('remote_site_test');
182      remote_output($galleries_url.'create_listing_file.php?action=test&version='.PHPWG_VERSION);
183      break;
184    }
185    case 'clean' :
186    {
187      $title = $galleries_url.' : '.l10n('remote_site_clean');
188      remote_output($galleries_url.'create_listing_file.php?action=clean');
189      break;
190    }
191    case 'delete' :
192    {
193      delete_site($page['site']);
194      array_push($page['infos'],
195                 $galleries_url.' '.l10n('site_deleted'));
196      break;
197    }
198  }
199}
200
201$template->assign( array(
202  'U_HELP'    => get_root_url().'popuphelp.php?page=site_manager',
203  'F_ACTION'  => get_root_url().'admin.php'
204                .get_query_string_diff( array('action','site') )
205  ) );
206
207// +-----------------------------------------------------------------------+
208// |                           remote sites list                           |
209// +-----------------------------------------------------------------------+
210
211if ( is_file(PHPWG_ROOT_PATH.'listing.xml') )
212{
213  $xml_content = getXmlCode(PHPWG_ROOT_PATH.'listing.xml');
214  $local_listing_site_url = getAttribute(
215          getChild($xml_content, 'informations'),
216          'url'
217        );
218  if ( !url_is_remote($local_listing_site_url) )
219  {
220    $local_listing_site_url = null;
221  }
222}
223
224$query = '
225SELECT c.site_id, COUNT(DISTINCT c.id) AS nb_categories, COUNT(i.id) AS nb_images
226  FROM '.CATEGORIES_TABLE.' AS c LEFT JOIN '.IMAGES_TABLE.' AS i
227  ON c.id=i.storage_category_id
228  WHERE c.site_id IS NOT NULL
229  GROUP BY c.site_id
230;';
231$sites_detail = hash_from_query($query, 'site_id'); 
232
233$query = '
234SELECT *
235  FROM '.SITES_TABLE.'
236;';
237$result = pwg_query($query);
238
239while ($row = pwg_db_fetch_assoc($result))
240{
241  $is_remote = url_is_remote($row['galleries_url']);
242  $base_url = PHPWG_ROOT_PATH.'admin.php';
243  $base_url.= '?page=site_manager';
244  $base_url.= '&amp;site='.$row['id'];
245  $base_url.= '&amp;action=';
246
247  $update_url = PHPWG_ROOT_PATH.'admin.php';
248  $update_url.= '?page=site_update';
249  $update_url.= '&amp;site='.$row['id'];
250 
251  $tpl_var =
252    array(
253      'NAME' => $row['galleries_url'],
254      'TYPE' => l10n( $is_remote ? 'site_remote' : 'site_local' ),
255      'CATEGORIES' => (int)@$sites_detail[$row['id']]['nb_categories'],
256      'IMAGES' => (int)@$sites_detail[$row['id']]['nb_images'],
257      'U_SYNCHRONIZE' => $update_url
258     );
259     
260   if ($is_remote)
261   {
262     $tpl_var['remote'] =
263       array(
264         'U_TEST' => $base_url.'test',
265         'U_GENERATE' => $row['galleries_url'].'create_listing_file.php?action=generate',
266         'U_CLEAN' => $base_url.'clean',
267         );
268   }
269
270  if ($row['id'] != 1)
271  {
272    $tpl_var['U_DELETE'] = $base_url.'delete';
273  }
274
275  $plugin_links = array();
276  //$plugin_links is array of array composed of U_HREF, U_HINT & U_CAPTION
277  $plugin_links =
278    trigger_event('get_admins_site_links',
279      $plugin_links, $row['id'], $is_remote);
280  $tpl_var['plugin_links'] = $plugin_links;
281
282  $template->append('sites', $tpl_var);
283
284  if ( isset($local_listing_site_url) and
285       $row['galleries_url']==$local_listing_site_url )
286  {
287    $local_listing_site_id = $row['id'];
288    $template->assign( 'local_listing',
289        array(
290          'URL' =>  $local_listing_site_url,
291          'U_SYNCHRONIZE' => $update_url.'&amp;local_listing=1'
292        )
293      );
294  }
295}
296
297if ( isset($local_listing_site_url) and !isset($local_listing_site_id) )
298{
299  $template->assign( 'local_listing',
300      array(
301        'URL' =>  $local_listing_site_url,
302        'CREATE' => true
303      )
304    );
305}
306
307
308$template->assign_var_from_handle('ADMIN_CONTENT', 'site_manager');
309?>
Note: See TracBrowser for help on using the repository browser.