source: trunk/admin/include/languages.class.php @ 6355

Last change on this file since 6355 was 6355, checked in by rvelices, 14 years ago

fix bug 1663 : wrong decoding of non Ascii iptc/exif (charset issue)

  • Property svn:eol-style set to LF
File size: 11.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 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
24class languages
25{
26  var $fs_languages = array();
27  var $db_languages = array();
28  var $server_languages = array();
29
30  /**
31   * Initialize $fs_languages and $db_languages
32  */
33  function languages($target_charset = null)
34  {
35    $this->fs_languages = $this->get_fs_languages($target_charset);
36  }
37
38  /**
39   * Set tabsheet for languages pages.
40   * @param string selected page.
41   */
42  function set_tabsheet($selected)
43  {
44    include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
45
46    $link = get_root_url().'admin.php?page=';
47
48    $tabsheet = new tabsheet();
49    $tabsheet->add('languages_installed', l10n('Installed Languages'), $link.'languages_installed');
50    $tabsheet->add('languages_new', l10n('Add New Language'), $link.'languages_new');
51    $tabsheet->select($selected);
52    $tabsheet->assign();
53  }
54
55  /**
56   * Perform requested actions
57   * @param string - action
58   * @param string - language id
59   * @param array - errors
60   */
61  function perform_action($action, $language_id)
62  {
63    global $conf;
64
65    if (isset($this->db_languages[$language_id]))
66    {
67      $crt_db_language = $this->db_languages[$language_id];
68    }
69
70    $errors = array();
71
72    switch ($action)
73    {
74      case 'activate':
75        if (isset($crt_db_language))
76        {
77          array_push($errors, 'CANNOT ACTIVATE - LANGUAGE IS ALREADY ACTIVATED');
78          break;
79        }
80
81        $query = '
82INSERT INTO '.LANGUAGES_TABLE.'
83  (id, name)
84  VALUES(\''.$language_id.'\', \''.$this->fs_languages[$language_id].'\')
85;';
86        pwg_query($query);
87        break;
88
89      case 'deactivate':
90        if (!isset($crt_db_language))
91        {
92          array_push($errors, 'CANNOT DEACTIVATE - LANGUAGE IS ALREADY DEACTIVATED');
93          break;
94        }
95
96        if ($language_id == get_default_language())
97        {
98          array_push($errors, 'CANNOT DEACTIVATE - LANGUAGE IS DEFAULT LANGUAGE');
99          break;
100        }
101
102        $query = "
103DELETE
104  FROM ".LANGUAGES_TABLE."
105  WHERE id= '".$language_id."'
106;";
107        pwg_query($query);
108        break;
109
110      case 'delete':
111        if (!empty($crt_db_language))
112        {
113          array_push($errors, 'CANNOT DELETE - LANGUAGE IS ACTIVATED');
114          break;
115        }
116        if (!isset($this->fs_languages[$language_id]))
117        {
118          array_push($errors, 'CANNOT DELETE - LANGUAGE DOES NOT EXIST');
119          break;
120        }
121
122        // Set default language to user who are using this language
123        $query = '
124UPDATE '.USER_INFOS_TABLE.'
125  SET language = "'.get_default_language().'"
126  WHERE language = "'.$language_id.'"
127;';
128        pwg_query($query);
129
130        if (!$this->deltree(PHPWG_ROOT_PATH.'language/'.$language_id))
131        {
132          $this->send_to_trash(PHPWG_ROOT_PATH.'language/'.$language_id);
133        }
134        break;
135
136      case 'set_default':
137        $query = '
138UPDATE '.USER_INFOS_TABLE.'
139  SET language = "'.$language_id.'"
140  WHERE user_id = '.$conf['default_user_id'].'
141;';
142        pwg_query($query);
143        break;
144    }
145    return $errors;
146  }
147
148  /**
149  *  Get languages defined in the language directory
150  */
151  function get_fs_languages($target_charset = null)
152  {
153    if ( empty($target_charset) )
154    {
155      $target_charset = get_pwg_charset();
156    }
157    $target_charset = strtolower($target_charset);
158
159    $dir = opendir(PHPWG_ROOT_PATH.'language');
160
161    while ($file = readdir($dir))
162    {
163      $path = PHPWG_ROOT_PATH.'language/'.$file;
164      if (!is_link($path) and is_dir($path) and file_exists($path.'/iso.txt'))
165      {
166        list($language_name) = @file($path.'/iso.txt');
167
168        $languages[$file] = convert_charset($language_name, 'utf-8', $target_charset);
169      }
170    }
171    closedir($dir);
172    @asort($languages);
173
174    return $languages;
175  }
176
177  function get_db_languages()
178  {
179    $query = '
180  SELECT id, name
181    FROM '.LANGUAGES_TABLE.'
182    ORDER BY name ASC
183  ;';
184    $result = pwg_query($query);
185
186    while ($row = pwg_db_fetch_assoc($result))
187    {
188      $this->db_languages[ $row['id'] ] = $row['name'];
189    }
190  }
191
192  /**
193   * Retrieve PEM server datas to $server_languages
194   */
195  function get_server_languages()
196  {
197    global $user;
198
199    $pem_category_id = 8;
200
201    // Retrieve PEM versions
202    $version = PHPWG_VERSION;
203    $versions_to_check = array();
204    $url = PEM_URL . '/api/get_version_list.php?category_id='.$pem_category_id.'&format=php';
205    if (fetchRemote($url, $result) and $pem_versions = @unserialize($result))
206    {
207      if (!preg_match('/^\d+\.\d+\.\d+/', $version))
208      {
209        $version = $pem_versions[0]['name'];
210      }
211      $branch = substr($version, 0, strrpos($version, '.'));
212      foreach ($pem_versions as $pem_version)
213      {
214        if (strpos($pem_version['name'], $branch) === 0)
215        {
216          $versions_to_check[] = $pem_version['id'];
217        }
218      }
219    }
220    if (empty($versions_to_check))
221    {
222      return false;
223    }
224
225    // Retrieve PEM languages infos
226    $url = PEM_URL . '/api/get_revision_list.php?category_id='.$pem_category_id.'&format=php&last_revision_only=true';
227    $url .= '&version=' . implode(',', $versions_to_check);
228    $url .= '&lang='.$user['language'];
229
230    if (fetchRemote($url, $result))
231    {
232      $pem_languages = @unserialize($result);
233      if (!is_array($pem_languages))
234      {
235        return false;
236      }
237      foreach ($pem_languages as $language)
238      {
239        if (preg_match('/^.*? \[[A-Z]{2}\]$/', $language['extension_name'])
240          and !in_array($language['extension_name'], $this->fs_languages))
241        {
242          $this->server_languages[] = $language;
243        }
244      }
245      return true;
246    }
247    return false;
248  }
249
250  /**
251   * Extract language files from archive
252   *
253   * @param string - install or upgrade
254   * @param string - remote revision identifier (numeric)
255   * @param string - language id or extension id
256   */
257  function extract_language_files($action, $revision, $dest='')
258  {
259    if ($archive = tempnam( PHPWG_ROOT_PATH.'language', 'zip'))
260    {
261      $url = PEM_URL . '/download.php?rid=' . $revision;
262      $url .= '&origin=piwigo_' . $action;
263
264      if ($handle = @fopen($archive, 'wb') and fetchRemote($url, $handle))
265      {
266        fclose($handle);
267        include(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php');
268        $zip = new PclZip($archive);
269        if ($list = $zip->listContent())
270        {
271          foreach ($list as $file)
272          {
273            // we search iso.txt in archive
274            if (basename($file['filename']) == 'iso.txt'
275              and (!isset($main_filepath)
276              or strlen($file['filename']) < strlen($main_filepath)))
277            {
278              $main_filepath = $file['filename'];
279            }
280          }
281          if (isset($main_filepath))
282          {
283            $root = basename(dirname($main_filepath)); // iso.txt path in archive
284            if (preg_match('/^[a-z]{2}_[A-Z]{2}$/', $root))
285            {
286              if ($action == 'install')
287              {
288                $dest = $root;
289              }
290              $extract_path = PHPWG_ROOT_PATH.'language/'.$dest;
291              if (
292                $result = $zip->extract(
293                  PCLZIP_OPT_PATH, $extract_path,
294                  PCLZIP_OPT_REMOVE_PATH, $root,
295                  PCLZIP_OPT_REPLACE_NEWER
296                  )
297                )
298              {
299                foreach ($result as $file)
300                {
301                  if ($file['stored_filename'] == $main_filepath)
302                  {
303                    $status = $file['status'];
304                    break;
305                  }
306                }
307                if ($status == 'ok')
308                {
309                  $this->fs_languages = $this->get_fs_languages();
310                  $this->perform_action('activate', $dest);
311                }
312                if (file_exists($extract_path.'/obsolete.list')
313                  and $old_files = file($extract_path.'/obsolete.list', FILE_IGNORE_NEW_LINES)
314                  and !empty($old_files))
315                {
316                  array_push($old_files, 'obsolete.list');
317                  foreach($old_files as $old_file)
318                  {
319                    $path = $extract_path.'/'.$old_file;
320                    if (is_file($path))
321                    {
322                      @unlink($path);
323                    }
324                    elseif (is_dir($path))
325                    {
326                      if (!$this->deltree($path))
327                      {
328                        $this->send_to_trash($path);
329                      }
330                    }
331                  }
332                }
333              }
334              else $status = 'extract_error';
335            }
336            else $status = 'archive_error';
337          }
338          else $status = 'archive_error';
339        }
340        else $status = 'archive_error';
341      }
342      else $status = 'dl_archive_error';
343    }
344    else $status = 'temp_path_error';
345
346    @unlink($archive);
347    return $status;
348  }
349
350  /**
351   * delete $path directory
352   * @param string - path
353   */
354  function deltree($path)
355  {
356    if (is_dir($path))
357    {
358      $fh = opendir($path);
359      while ($file = readdir($fh))
360      {
361        if ($file != '.' and $file != '..')
362        {
363          $pathfile = $path . '/' . $file;
364          if (is_dir($pathfile))
365          {
366            $this->deltree($pathfile);
367          }
368          else
369          {
370            @unlink($pathfile);
371          }
372        }
373      }
374      closedir($fh);
375      return @rmdir($path);
376    }
377  }
378
379  /**
380   * send $path to trash directory
381   * @param string - path
382   */
383  function send_to_trash($path)
384  {
385    $trash_path = PHPWG_ROOT_PATH . 'language/trash';
386    if (!is_dir($trash_path))
387    {
388      @mkdir($trash_path);
389      $file = @fopen($trash_path . '/.htaccess', 'w');
390      @fwrite($file, 'deny from all');
391      @fclose($file);
392    }
393    while ($r = $trash_path . '/' . md5(uniqid(rand(), true)))
394    {
395      if (!is_dir($r))
396      {
397        @rename($path, $r);
398        break;
399      }
400    }
401  }
402}
403?>
Note: See TracBrowser for help on using the repository browser.