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

Last change on this file since 5357 was 5357, checked in by patdenice, 14 years ago

Feature 1535: Add language manager.

File size: 6.0 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   * Perform requested actions
40   * @param string - action
41   * @param string - language id
42   * @param array - errors
43   */
44  function perform_action($action, $language_id)
45  {
46    global $conf;
47
48    if (isset($this->db_languages[$language_id]))
49    {
50      $crt_db_language = $this->db_languages[$language_id];
51    }
52
53    $errors = array();
54
55    switch ($action)
56    {
57      case 'activate':
58        if (isset($crt_db_language))
59        {
60          array_push($errors, 'CANNOT ACTIVATE - LANGUAGE IS ALREADY ACTIVATED');
61          break;
62        }
63
64        $query = "
65INSERT INTO ".LANGUAGES_TABLE."
66  SET id = '".$language_id."',
67      name = '".$this->fs_languages[$language_id]."'
68;";
69        pwg_query($query);
70        break;
71
72      case 'deactivate':
73        if (!isset($crt_db_language))
74        {
75          array_push($errors, 'CANNOT DEACTIVATE - LANGUAGE IS ALREADY DEACTIVATED');
76          break;
77        }
78
79        if ($language_id == get_default_language())
80        {
81          array_push($errors, 'CANNOT DEACTIVATE - LANGUAGE IS DEFAULT LANGUAGE');
82          break;
83        }
84       
85        $query = "
86DELETE
87  FROM ".LANGUAGES_TABLE."
88  WHERE id= '".$language_id."'
89;";
90        pwg_query($query);
91        break;
92
93      case 'delete':
94        if (!empty($crt_db_language))
95        {
96          array_push($errors, 'CANNOT DELETE - LANGUAGE IS ACTIVATED');
97          break;
98        }
99        if (!isset($this->fs_languages[$language_id]))
100        {
101          array_push($errors, 'CANNOT DELETE - LANGUAGE DOES NOT EXIST');
102          break;
103        }
104
105        // Set default language to user who are using this language
106        $query = '
107UPDATE '.USER_INFOS_TABLE.'
108  SET language = "'.get_default_language().'"
109  WHERE language = "'.$language_id.'"
110;';
111        pwg_query($query);
112
113        if (!$this->deltree(PHPWG_ROOT_PATH.'language/'.$language_id))
114        {
115          $this->send_to_trash(PHPWG_ROOT_PATH.'language/'.$language_id);
116        }
117        break;
118
119      case 'set_default':
120        $query = '
121UPDATE '.USER_INFOS_TABLE.'
122  SET language = "'.$language_id.'"
123  WHERE user_id = '.$conf['default_user_id'].'
124;';
125        pwg_query($query);
126        break;
127    }
128    return $errors;
129  }
130
131  /**
132  *  Get languages defined in the language directory
133  */ 
134  function get_fs_languages($target_charset = null)
135  {
136    if ( empty($target_charset) )
137    {
138      $target_charset = get_pwg_charset();
139    }
140    $target_charset = strtolower($target_charset);
141
142    $dir = opendir(PHPWG_ROOT_PATH.'language');
143
144    while ($file = readdir($dir))
145    {
146      $path = PHPWG_ROOT_PATH.'language/'.$file;
147      if (!is_link($path) and is_dir($path) and file_exists($path.'/iso.txt'))
148      {
149        list($language_name) = @file($path.'/iso.txt');
150
151        $languages[$file] = convert_charset($language_name, $target_charset);
152      }
153    }
154    closedir($dir);
155    @asort($languages);
156
157    return $languages;
158  }
159
160  function get_db_languages()
161  {
162    $query = '
163  SELECT id, name
164    FROM '.LANGUAGES_TABLE.'
165    ORDER BY name ASC
166  ;';
167    $result = pwg_query($query);
168
169    while ($row = pwg_db_fetch_assoc($result))
170    {
171      $this->db_languages[ $row['id'] ] = $row['name'];
172    }
173  }
174
175  /**
176   * delete $path directory
177   * @param string - path
178   */
179  function deltree($path)
180  {
181    if (is_dir($path))
182    {
183      $fh = opendir($path);
184      while ($file = readdir($fh))
185      {
186        if ($file != '.' and $file != '..')
187        {
188          $pathfile = $path . '/' . $file;
189          if (is_dir($pathfile))
190          {
191            $this->deltree($pathfile);
192          }
193          else
194          {
195            @unlink($pathfile);
196          }
197        }
198      }
199      closedir($fh);
200      return @rmdir($path);
201    }
202  }
203
204  /**
205   * send $path to trash directory
206   * @param string - path
207   */
208  function send_to_trash($path)
209  {
210    $trash_path = PHPWG_ROOT_PATH . 'language/trash';
211    if (!is_dir($trash_path))
212    {
213      @mkdir($trash_path);
214      $file = @fopen($trash_path . '/.htaccess', 'w');
215      @fwrite($file, 'deny from all');
216      @fclose($file);
217    }
218    while ($r = $trash_path . '/' . md5(uniqid(rand(), true)))
219    {
220      if (!is_dir($r))
221      {
222        @rename($path, $r);
223        break;
224      }
225    }
226  }
227}
228?>
Note: See TracBrowser for help on using the repository browser.