annotate program/include/rcmail_utils.php @ 0:4681f974d28b

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:52:31 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1 <?php
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4 +-----------------------------------------------------------------------+
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
5 | program/include/rcmail_utils.php |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
6 | |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
7 | This file is part of the Roundcube PHP suite |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
8 | Copyright (C) 2005-2015 The Roundcube Dev Team |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
9 | |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
10 | Licensed under the GNU General Public License version 3 or |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
11 | any later version with exceptions for skins & plugins. |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
12 | See the README file for a full license statement. |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
13 | |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
14 | CONTENTS: |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
15 | Roundcube utilities |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
16 | |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
17 +-----------------------------------------------------------------------+
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
18 | Author: Thomas Bruederli <roundcube@gmail.com> |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
19 | Author: Aleksander Machniak <alec@alec.pl> |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
20 +-----------------------------------------------------------------------+
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
21 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
22
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
23 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
24 * Roundcube utilities
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
25 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
26 * @package Webmail
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
27 * @subpackage Utils
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
28 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
29 class rcmail_utils
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
30 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
31 public static $db;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
32
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
33 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
34 * Initialize database object and connect
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
35 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
36 * @return rcube_db Database instance
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
37 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
38 public static function db()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
39 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
40 if (self::$db === null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
41 $rc = rcube::get_instance();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
42 $db = rcube_db::factory($rc->config->get('db_dsnw'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
43
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
44 $db->set_debug((bool)$rc->config->get('sql_debug'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
45
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
46 // Connect to database
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
47 $db->db_connect('w');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
48
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
49 if (!$db->is_connected()) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
50 rcube::raise_error("Error connecting to database: " . $db->is_error(), false, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
51 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
52
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
53 self::$db = $db;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
54 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
55
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
56 return self::$db;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
57 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
58
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
59 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
60 * Initialize database schema
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
61 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
62 * @param string $dir Directory with sql files
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
63 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
64 public static function db_init($dir)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
65 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
66 $db = self::db();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
67
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
68 $file = $dir . '/' . $db->db_provider . '.initial.sql';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
69 if (!file_exists($file)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
70 rcube::raise_error("DDL file $file not found", false, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
71 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
72
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
73 echo "Creating database schema... ";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
74
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
75 if ($sql = file_get_contents($file)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
76 if (!$db->exec_script($sql)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
77 $error = $db->is_error();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
78 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
79 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
80 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
81 $error = "Unable to read file $file or it is empty";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
82 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
83
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
84 if ($error) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
85 echo "[FAILED]\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
86 rcube::raise_error($error, false, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
87 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
88 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
89 echo "[OK]\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
90 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
91 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
92
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
93 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
94 * Update database schema
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
95 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
96 * @param string $dir Directory with sql files
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
97 * @param string $package Component name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
98 * @param string $ver Optional current version number
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
99 * @param array $opts Parameters (errors, quiet)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
100 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
101 * @return True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
102 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
103 public static function db_update($dir, $package, $ver = null, $opts = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
104 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
105 // Check if directory exists
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
106 if (!file_exists($dir)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
107 if ($opts['errors']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
108 rcube::raise_error("Specified database schema directory doesn't exist.", false, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
109 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
110 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
111 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
112
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
113 $db = self::db();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
114
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
115 // Read DB schema version from database (if 'system' table exists)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
116 if (in_array($db->table_name('system'), (array)$db->list_tables())) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
117 $db->query("SELECT `value`"
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
118 . " FROM " . $db->table_name('system', true)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
119 . " WHERE `name` = ?",
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
120 $package . '-version');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
121
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
122 $row = $db->fetch_array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
123 $version = preg_replace('/[^0-9]/', '', $row[0]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
124 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
125
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
126 // DB version not found, but release version is specified
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
127 if (!$version && $ver) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
128 // Map old release version string to DB schema version
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
129 // Note: This is for backward compat. only, do not need to be updated
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
130 $map = array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
131 '0.1-stable' => 1,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
132 '0.1.1' => 2008030300,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
133 '0.2-alpha' => 2008040500,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
134 '0.2-beta' => 2008060900,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
135 '0.2-stable' => 2008092100,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
136 '0.2.1' => 2008092100,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
137 '0.2.2' => 2008092100,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
138 '0.3-stable' => 2008092100,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
139 '0.3.1' => 2009090400,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
140 '0.4-beta' => 2009103100,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
141 '0.4' => 2010042300,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
142 '0.4.1' => 2010042300,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
143 '0.4.2' => 2010042300,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
144 '0.5-beta' => 2010100600,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
145 '0.5' => 2010100600,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
146 '0.5.1' => 2010100600,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
147 '0.5.2' => 2010100600,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
148 '0.5.3' => 2010100600,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
149 '0.5.4' => 2010100600,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
150 '0.6-beta' => 2011011200,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
151 '0.6' => 2011011200,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
152 '0.7-beta' => 2011092800,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
153 '0.7' => 2011111600,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
154 '0.7.1' => 2011111600,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
155 '0.7.2' => 2011111600,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
156 '0.7.3' => 2011111600,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
157 '0.7.4' => 2011111600,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
158 '0.8-beta' => 2011121400,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
159 '0.8-rc' => 2011121400,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
160 '0.8.0' => 2011121400,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
161 '0.8.1' => 2011121400,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
162 '0.8.2' => 2011121400,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
163 '0.8.3' => 2011121400,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
164 '0.8.4' => 2011121400,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
165 '0.8.5' => 2011121400,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
166 '0.8.6' => 2011121400,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
167 '0.9-beta' => 2012080700,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
168 );
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
169
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
170 $version = $map[$ver];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
171 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
172
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
173 // Assume last version before the 'system' table was added
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
174 if (empty($version)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
175 $version = 2012080700;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
176 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
177
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
178 $dir .= '/' . $db->db_provider;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
179 if (!file_exists($dir)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
180 if ($opts['errors']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
181 rcube::raise_error("DDL Upgrade files for " . $db->db_provider . " driver not found.", false, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
182 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
183 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
184 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
185
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
186 $dh = opendir($dir);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
187 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
188
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
189 while ($file = readdir($dh)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
190 if (preg_match('/^([0-9]+)\.sql$/', $file, $m) && $m[1] > $version) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
191 $result[] = $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
192 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
193 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
194 sort($result, SORT_NUMERIC);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
195
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
196 foreach ($result as $v) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
197 if (!$opts['quiet']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
198 echo "Updating database schema ($v)... ";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
199 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
200
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
201 // Ignore errors here to print the error only once
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
202 $db->set_option('ignore_errors', true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
203 $error = self::db_update_schema($package, $v, "$dir/$v.sql");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
204 $db->set_option('ignore_errors', false);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
205
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
206 if ($error) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
207 if (!$opts['quiet']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
208 echo "[FAILED]\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
209 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
210 if ($opts['errors']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
211 rcube::raise_error("Error in DDL upgrade $v: $error", false, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
212 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
213 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
214 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
215 else if (!$opts['quiet']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
216 echo "[OK]\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
217 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
218 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
219
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
220 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
221 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
222
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
223 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
224 * Run database update from a single sql file
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
225 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
226 protected static function db_update_schema($package, $version, $file)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
227 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
228 $db = self::db();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
229
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
230 // read DDL file
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
231 if ($sql = file_get_contents($file)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
232 if (!$db->exec_script($sql)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
233 return $db->is_error();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
234 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
235 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
236
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
237 // escape if 'system' table does not exist
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
238 if ($version < 2013011000) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
239 return;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
240 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
241
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
242 $system_table = $db->table_name('system', true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
243
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
244 $db->query("UPDATE " . $system_table
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
245 . " SET `value` = ?"
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
246 . " WHERE `name` = ?",
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
247 $version, $package . '-version');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
248
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
249 if (!$db->is_error() && !$db->affected_rows()) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
250 $db->query("INSERT INTO " . $system_table
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
251 ." (`name`, `value`) VALUES (?, ?)",
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
252 $package . '-version', $version);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
253 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
254
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
255 return $db->is_error();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
256 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
257
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
258 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
259 * Removes all deleted records older than X days
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
260 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
261 * @param int $days Number of days
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
262 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
263 public static function db_clean($days)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
264 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
265 // mapping for table name => primary key
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
266 $primary_keys = array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
267 'contacts' => 'contact_id',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
268 'contactgroups' => 'contactgroup_id',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
269 );
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
270
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
271 $db = self::db();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
272
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
273 $threshold = date('Y-m-d 00:00:00', time() - $days * 86400);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
274
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
275 foreach (array('contacts','contactgroups','identities') as $table) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
276 $sqltable = $db->table_name($table, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
277
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
278 // also delete linked records
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
279 // could be skipped for databases which respect foreign key constraints
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
280 if ($db->db_provider == 'sqlite' && ($table == 'contacts' || $table == 'contactgroups')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
281 $pk = $primary_keys[$table];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
282 $memberstable = $db->table_name('contactgroupmembers');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
283
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
284 $db->query(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
285 "DELETE FROM " . $db->quote_identifier($memberstable)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
286 . " WHERE `$pk` IN ("
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
287 . "SELECT `$pk` FROM $sqltable"
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
288 . " WHERE `del` = 1 AND `changed` < ?"
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
289 . ")",
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
290 $threshold);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
291
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
292 echo $db->affected_rows() . " records deleted from '$memberstable'\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
293 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
294
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
295 // delete outdated records
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
296 $db->query("DELETE FROM $sqltable WHERE `del` = 1 AND `changed` < ?", $threshold);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
297
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
298 echo $db->affected_rows() . " records deleted from '$table'\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
299 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
300 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
301
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
302 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
303 * Reindex contacts
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
304 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
305 public static function indexcontacts()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
306 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
307 $db = self::db();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
308
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
309 // iterate over all users
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
310 $sql_result = $db->query("SELECT `user_id` FROM " . $db->table_name('users', true) . " ORDER BY `user_id`");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
311 while ($sql_result && ($sql_arr = $db->fetch_assoc($sql_result))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
312 echo "Indexing contacts for user " . $sql_arr['user_id'] . "...\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
313
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
314 $contacts = new rcube_contacts($db, $sql_arr['user_id']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
315 $contacts->set_pagesize(9999);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
316
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
317 $result = $contacts->list_records();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
318 while ($result->count && ($row = $result->next())) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
319 unset($row['words']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
320 $contacts->update($row['ID'], $row);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
321 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
322 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
323
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
324 echo "done.\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
325 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
326
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
327 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
328 * Modify user preferences
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
329 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
330 * @param string $name Option name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
331 * @param string $value Option value
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
332 * @param int $userid Optional user identifier
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
333 * @param string $type Optional value type (bool, int, string)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
334 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
335 public static function mod_pref($name, $value, $userid = null, $type = 'string')
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
336 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
337 $db = self::db();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
338
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
339 if ($userid) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
340 $query = '`user_id` = ' . intval($userid);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
341 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
342 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
343 $query = '1=1';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
344 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
345
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
346 $type = strtolower($type);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
347
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
348 if ($type == 'bool' || $type == 'boolean') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
349 $value = rcube_utils::get_boolean($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
350 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
351 else if ($type == 'int' || $type == 'integer') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
352 $value = (int) $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
353 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
354
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
355 // iterate over all users
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
356 $sql_result = $db->query("SELECT * FROM " . $db->table_name('users', true) . " WHERE $query");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
357
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
358 while ($sql_result && ($sql_arr = $db->fetch_assoc($sql_result))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
359 echo "Updating prefs for user " . $sql_arr['user_id'] . "...";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
360
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
361 $user = new rcube_user($sql_arr['user_id'], $sql_arr);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
362 $prefs = $old_prefs = $user->get_prefs();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
363
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
364 $prefs[$name] = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
365
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
366 if ($prefs != $old_prefs) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
367 $user->save_prefs($prefs, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
368 echo "saved.\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
369 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
370 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
371 echo "nothing changed.\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
372 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
373 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
374 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
375 }