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

Last change on this file since 26461 was 26461, checked in by mistic100, 10 years ago

Update headers to 2014. Happy new year!!

  • Property svn:eol-style set to LF
File size: 12.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 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->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          $errors[] = 'CANNOT ACTIVATE - LANGUAGE IS ALREADY ACTIVATED';
61          break;
62        }
63
64        $query = '
65INSERT INTO '.LANGUAGES_TABLE.'
66  (id, version, name)
67  VALUES(\''.$language_id.'\',
68         \''.$this->fs_languages[$language_id]['version'].'\',
69         \''.$this->fs_languages[$language_id]['name'].'\')
70;';
71        pwg_query($query);
72        break;
73
74      case 'deactivate':
75        if (!isset($crt_db_language))
76        {
77          $errors[] = 'CANNOT DEACTIVATE - LANGUAGE IS ALREADY DEACTIVATED';
78          break;
79        }
80
81        if ($language_id == get_default_language())
82        {
83          $errors[] = 'CANNOT DEACTIVATE - LANGUAGE IS DEFAULT LANGUAGE';
84          break;
85        }
86
87        $query = '
88DELETE
89  FROM '.LANGUAGES_TABLE.'
90  WHERE id= \''.$language_id.'\'
91;';
92        pwg_query($query);
93        break;
94
95      case 'delete':
96        if (!empty($crt_db_language))
97        {
98          $errors[] = 'CANNOT DELETE - LANGUAGE IS ACTIVATED';
99          break;
100        }
101        if (!isset($this->fs_languages[$language_id]))
102        {
103          $errors[] = 'CANNOT DELETE - LANGUAGE DOES NOT EXIST';
104          break;
105        }
106
107        // Set default language to user who are using this language
108        $query = '
109UPDATE '.USER_INFOS_TABLE.'
110  SET language = \''.get_default_language().'\'
111  WHERE language = \''.$language_id.'\'
112;';
113        pwg_query($query);
114
115        deltree(PHPWG_ROOT_PATH.'language/'.$language_id, PHPWG_ROOT_PATH.'language/trash');
116        break;
117
118      case 'set_default':
119        $query = '
120UPDATE '.USER_INFOS_TABLE.'
121  SET language = \''.$language_id.'\'
122  WHERE user_id IN ('.$conf['default_user_id'].', '.$conf['guest_id'].')
123;';
124        pwg_query($query);
125        break;
126    }
127    return $errors;
128  }
129
130  /**
131  *  Get languages defined in the language directory
132  */
133  function get_fs_languages($target_charset = null)
134  {
135    if ( empty($target_charset) )
136    {
137      $target_charset = get_pwg_charset();
138    }
139    $target_charset = strtolower($target_charset);
140
141    $dir = opendir(PHPWG_ROOT_PATH.'language');
142    while ($file = readdir($dir))
143    {
144      if ($file!='.' and $file!='..')
145      {
146        $path = PHPWG_ROOT_PATH.'language/'.$file;
147        if (is_dir($path) and !is_link($path)
148            and preg_match('/^[a-zA-Z0-9-_]+$/', $file )
149            and file_exists($path.'/common.lang.php')
150            )
151        {
152          $language = array(
153              'name'=>$file,
154              'code'=>$file,
155              'version'=>'0',
156              'uri'=>'',
157              'author'=>'',
158            );
159          $plg_data = implode( '', file($path.'/common.lang.php') );
160
161          if ( preg_match("|Language Name: (.*)|", $plg_data, $val) )
162          {
163            $language['name'] = trim( $val[1] );
164            $language['name'] = convert_charset($language['name'], 'utf-8', $target_charset);
165          }
166          if (preg_match("|Version: (.*)|", $plg_data, $val))
167          {
168            $language['version'] = trim($val[1]);
169          }
170          if ( preg_match("|Language URI: (.*)|", $plg_data, $val) )
171          {
172            $language['uri'] = trim($val[1]);
173          }
174          if ( preg_match("|Author: (.*)|", $plg_data, $val) )
175          {
176            $language['author'] = trim($val[1]);
177          }
178          if ( preg_match("|Author URI: (.*)|", $plg_data, $val) )
179          {
180            $language['author uri'] = trim($val[1]);
181          }
182          if (!empty($language['uri']) and strpos($language['uri'] , 'extension_view.php?eid='))
183          {
184            list( , $extension) = explode('extension_view.php?eid=', $language['uri']);
185            if (is_numeric($extension)) $language['extension'] = $extension;
186          }
187          // IMPORTANT SECURITY !
188          $language = array_map('htmlspecialchars', $language);
189          $this->fs_languages[$file] = $language;
190        }
191      }
192    }
193    closedir($dir);
194    @uasort($this->fs_languages, 'name_compare');
195  }
196
197  function get_db_languages()
198  {
199    $query = '
200  SELECT id, name
201    FROM '.LANGUAGES_TABLE.'
202    ORDER BY name ASC
203  ;';
204    $result = pwg_query($query);
205
206    while ($row = pwg_db_fetch_assoc($result))
207    {
208      $this->db_languages[ $row['id'] ] = $row['name'];
209    }
210  }
211
212  /**
213   * Retrieve PEM server datas to $server_languages
214   */
215  function get_server_languages($new=false)
216  {
217    global $user, $conf;
218
219    $get_data = array(
220      'category_id' => $conf['pem_languages_category'],
221      'format' => 'php',
222    );
223
224    // Retrieve PEM versions
225    $version = PHPWG_VERSION;
226    $versions_to_check = array();
227    $url = PEM_URL . '/api/get_version_list.php';
228    if (fetchRemote($url, $result, $get_data) and $pem_versions = @unserialize($result))
229    {
230      if (!preg_match('/^\d+\.\d+\.\d+$/', $version))
231      {
232        $version = $pem_versions[0]['name'];
233      }
234      $branch = get_branch_from_version($version);
235      foreach ($pem_versions as $pem_version)
236      {
237        if (strpos($pem_version['name'], $branch) === 0)
238        {
239          $versions_to_check[] = $pem_version['id'];
240        }
241      }
242    }
243    if (empty($versions_to_check))
244    {
245      return false;
246    }
247
248    // Languages to check
249    $languages_to_check = array();
250    foreach($this->fs_languages as $fs_language)
251    {
252      if (isset($fs_language['extension']))
253      {
254        $languages_to_check[] = $fs_language['extension'];
255      }
256    }
257
258    // Retrieve PEM languages infos
259    $url = PEM_URL . '/api/get_revision_list.php';
260    $get_data = array_merge($get_data, array(
261      'last_revision_only' => 'true',
262      'version' => implode(',', $versions_to_check),
263      'lang' => $user['language'],
264      'get_nb_downloads' => 'true',
265      )
266    );
267    if (!empty($languages_to_check))
268    {
269      if ($new)
270      {
271        $get_data['extension_exclude'] = implode(',', $languages_to_check);
272      }
273      else
274      {
275        $get_data['extension_include'] = implode(',', $languages_to_check);
276      }
277    }
278
279    if (fetchRemote($url, $result, $get_data))
280    {
281      $pem_languages = @unserialize($result);
282      if (!is_array($pem_languages))
283      {
284        return false;
285      }
286      foreach ($pem_languages as $language)
287      {
288        if (preg_match('/^.*? \[[A-Z]{2}\]$/', $language['extension_name']))
289        {
290          $this->server_languages[$language['extension_id']] = $language;
291        }
292      }
293      @uasort($this->server_languages, array($this, 'extension_name_compare'));
294      return true;
295    }
296    return false;
297  }
298
299  /**
300   * Extract language files from archive
301   *
302   * @param string - install or upgrade
303   * @param string - remote revision identifier (numeric)
304   * @param string - language id or extension id
305   */
306  function extract_language_files($action, $revision, $dest='')
307  {
308    if ($archive = tempnam( PHPWG_ROOT_PATH.'language', 'zip'))
309    {
310      $url = PEM_URL . '/download.php';
311      $get_data = array(
312        'rid' => $revision,
313        'origin' => 'piwigo_'.$action,
314      );
315
316      if ($handle = @fopen($archive, 'wb') and fetchRemote($url, $handle, $get_data))
317      {
318        fclose($handle);
319        include_once(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php');
320        $zip = new PclZip($archive);
321        if ($list = $zip->listContent())
322        {
323          foreach ($list as $file)
324          {
325            // we search common.lang.php in archive
326            if (basename($file['filename']) == 'common.lang.php'
327              and (!isset($main_filepath)
328              or strlen($file['filename']) < strlen($main_filepath)))
329            {
330              $main_filepath = $file['filename'];
331            }
332          }
333          if (isset($main_filepath))
334          {
335            $root = basename(dirname($main_filepath)); // common.lang.php path in archive
336            if (preg_match('/^[a-z]{2}_[A-Z]{2}$/', $root))
337            {
338              if ($action == 'install')
339              {
340                $dest = $root;
341              }
342              $extract_path = PHPWG_ROOT_PATH.'language/'.$dest;
343              if (
344                $result = $zip->extract(
345                  PCLZIP_OPT_PATH, $extract_path,
346                  PCLZIP_OPT_REMOVE_PATH, $root,
347                  PCLZIP_OPT_REPLACE_NEWER
348                  )
349                )
350              {
351                foreach ($result as $file)
352                {
353                  if ($file['stored_filename'] == $main_filepath)
354                  {
355                    $status = $file['status'];
356                    break;
357                  }
358                }
359                if ($status == 'ok')
360                {
361                  $this->get_fs_languages();
362                  if ($action == 'install')
363                  {
364                    $this->perform_action('activate', $dest);
365                  }
366                }
367                if (file_exists($extract_path.'/obsolete.list')
368                  and $old_files = file($extract_path.'/obsolete.list', FILE_IGNORE_NEW_LINES)
369                  and !empty($old_files))
370                {
371                  $old_files[] = 'obsolete.list';
372                  foreach($old_files as $old_file)
373                  {
374                    $path = $extract_path.'/'.$old_file;
375                    if (is_file($path))
376                    {
377                      @unlink($path);
378                    }
379                    elseif (is_dir($path))
380                    {
381                      deltree($path, PHPWG_ROOT_PATH.'language/trash');
382                    }
383                  }
384                }
385              }
386              else $status = 'extract_error';
387            }
388            else $status = 'archive_error';
389          }
390          else $status = 'archive_error';
391        }
392        else $status = 'archive_error';
393      }
394      else $status = 'dl_archive_error';
395    }
396    else $status = 'temp_path_error';
397
398    @unlink($archive);
399    return $status;
400  }
401
402  /**
403   * Sort functions
404   */
405  function language_version_compare($a, $b)
406  {
407    $pattern = array('/([a-z])/ei', '/\.+/', '/\.\Z|\A\./');
408    $replacement = array( "'.'.intval('\\1', 36).'.'", '.', '');
409
410    $array = preg_replace($pattern, $replacement, array($a, $b));
411
412    return version_compare($array[0], $array[1], '>=');
413  }
414
415  function extension_name_compare($a, $b)
416  {
417    return strcmp(strtolower($a['extension_name']), strtolower($b['extension_name']));
418  }
419}
420?>
Note: See TracBrowser for help on using the repository browser.