source: trunk/admin/include/functions_install.inc.php @ 6090

Last change on this file since 6090 was 6090, checked in by plg, 10 years ago

bug 1648 fixed: add the pwg_db_check_version functions for pdo-sqlite, sqlite
and pgsql, also make the $pwg_db_link global in the install_db_connect function
so that it can be used in SQLite functions.

bug fixed: with pdo-sqlite, pwg_db_num_rows always returns 0, so when
initializing user data, it tries to insert the same row twice.

File size: 4.9 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
24/**
25 * loads an sql file and executes all queries
26 *
27 * Before executing a query, $replaced is... replaced by $replacing. This is
28 * useful when the SQL file contains generic words. Drop table queries are
29 * not executed.
30 *
31 * @param string filepath
32 * @param string replaced
33 * @param string replacing
34 * @return void
35 */
36function execute_sqlfile($filepath, $replaced, $replacing, $dblayer)
37{
38  $sql_lines = file($filepath);
39  $query = '';
40  foreach ($sql_lines as $sql_line)
41  {
42    $sql_line = trim($sql_line);
43    if (preg_match('/(^--|^$)/', $sql_line))
44    {
45      continue;
46    }
47    $query.= ' '.$sql_line;
48    // if we reached the end of query, we execute it and reinitialize the
49    // variable "query"
50    if (preg_match('/;$/', $sql_line))
51    {
52      $query = trim($query);
53      $query = str_replace($replaced, $replacing, $query);
54      // we don't execute "DROP TABLE" queries
55      if (!preg_match('/^DROP TABLE/i', $query))
56      {
57        if ('mysql' == $dblayer)
58        {
59          if ( preg_match('/^(CREATE TABLE .*)[\s]*;[\s]*/im', $query, $matches) )
60          {
61            $query = $matches[1].' DEFAULT CHARACTER SET utf8'.';';
62          }
63        }
64        pwg_query($query);
65      }
66      $query = '';
67    }
68  }
69}
70
71/**
72 * Search for database engines available
73 *
74 * We search for functions_DATABASE_ENGINE.inc.php
75 * and we check if the connect function for that database exists
76 *
77 * @return array
78 */
79function available_engines()
80{
81  $engines = array();
82
83  $pattern = PHPWG_ROOT_PATH. 'include/dblayer/functions_%s.inc.php';
84  include_once PHPWG_ROOT_PATH. 'include/dblayer/dblayers.inc.php';
85
86  foreach ($dblayers as $engine_name => $engine)
87  {
88    if (file_exists(sprintf($pattern, $engine_name))) 
89    {
90      $engines[$engine_name]['label'] = $engine['engine'];
91      $engines[$engine_name]['available'] = false;
92
93      if (isset($engine['function_available'])
94          && function_exists($engine['function_available']))
95      {
96        $engines[$engine_name]['available'] = true;
97      }
98      elseif (isset($engine['class_available']) 
99              && class_exists($engine['class_available']))
100      {
101        $engines[$engine_name]['available'] = true;
102      } 
103    }
104  }
105
106  if ($engines['sqlite']['available'] and !$engines['pdo-sqlite']['available'])
107  {
108    unset($engines['pdo-sqlite']);
109  }
110  elseif ($engines['pdo-sqlite']['available'] and !$engines['sqlite']['available'])
111  {
112    unset($engines['sqlite']);
113  }
114  elseif (DEFAULT_DB_SQLITE=='native')
115  {
116    unset($engines['pdo-sqlite']);
117  }
118  else
119  {
120    unset($engines['sqlite']);
121  }
122
123  return $engines;
124}
125
126/**
127 * Automatically activate all core themes in the "themes" directory.
128 *
129 * @return void
130 */
131function activate_core_themes()
132{
133  include_once(PHPWG_ROOT_PATH.'admin/include/themes.class.php');
134  $themes = new themes();
135  foreach ($themes->fs_themes as $theme_id => $fs_theme)
136  {
137    if (in_array($theme_id, array('Sylvia', 'clear', 'dark')))
138    {
139      $themes->perform_action('activate', $theme_id);
140    }
141  }
142}
143
144function install_db_connect(&$infos, &$errors)
145{
146  global $pwg_db_link;
147 
148  try
149  {
150    $pwg_db_link = pwg_db_connect($_POST['dbhost'], $_POST['dbuser'], $_POST['dbpasswd'], $_POST['dbname']);
151    if ($pwg_db_link)
152    {
153      pwg_db_check_version();
154    }
155  }
156  catch (Exception $e)
157  {
158    array_push( $errors, l10n($e->getMessage()));
159  }
160}
161?>
Note: See TracBrowser for help on using the repository browser.