comparison bin/moduserprefs.sh @ 0:4681f974d28b

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:52:31 -0500
parents
children bf99236cc5cd
comparison
equal deleted inserted replaced
-1:000000000000 0:4681f974d28b
1 #!/usr/bin/env php
2 <?php
3 /*
4 +-----------------------------------------------------------------------+
5 | bin/moduserprefs.sh |
6 | |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2012, The Roundcube Dev Team |
9 | |
10 | Licensed under the GNU General Public License version 3 or |
11 | any later version with exceptions for skins & plugins. |
12 | See the README file for a full license statement. |
13 | |
14 | PURPOSE: |
15 | Bulk-change settings stored in user preferences |
16 +-----------------------------------------------------------------------+
17 | Author: Thomas Bruederli <roundcube@gmail.com> |
18 +-----------------------------------------------------------------------+
19 */
20
21 define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' );
22
23 require_once INSTALL_PATH.'program/include/clisetup.php';
24
25 function print_usage()
26 {
27 print "Usage: moduserprefs.sh [--user=user-id] pref-name [pref-value|--delete]\n";
28 print "--user User ID in local database\n";
29 print "--delete Unset the given preference\n";
30 }
31
32
33 // get arguments
34 $args = rcube_utils::get_opt(array('u' => 'user', 'd' => 'delete'));
35
36 if ($_SERVER['argv'][1] == 'help') {
37 print_usage();
38 exit;
39 }
40 else if (empty($args[0]) || (!isset($args[1]) && !$args['delete'])) {
41 print "Missing required parameters.\n";
42 print_usage();
43 exit;
44 }
45
46 $pref_name = trim($args[0]);
47 $pref_value = $args['delete'] ? null : trim($args[1]);
48
49 // connect to DB
50 $rcmail = rcube::get_instance();
51
52 $db = $rcmail->get_dbh();
53 $db->db_connect('w');
54
55 if (!$db->is_connected() || $db->is_error())
56 die("No DB connection\n" . $db->is_error());
57
58 $query = '1=1';
59
60 if ($args['user'])
61 $query = '`user_id` = ' . intval($args['user']);
62
63 // iterate over all users
64 $sql_result = $db->query("SELECT * FROM " . $db->table_name('users', true) . " WHERE $query");
65 while ($sql_result && ($sql_arr = $db->fetch_assoc($sql_result))) {
66 echo "Updating prefs for user " . $sql_arr['user_id'] . "...";
67
68 $user = new rcube_user($sql_arr['user_id'], $sql_arr);
69 $prefs = $old_prefs = $user->get_prefs();
70
71 $prefs[$pref_name] = $pref_value;
72
73 if ($prefs != $old_prefs) {
74 $user->save_prefs($prefs, true);
75 echo "saved.\n";
76 }
77 else {
78 echo "nothing changed.\n";
79 }
80 }
81
82 ?>