Aleksander Machniak
2015-08-12 6b31846c438dc19cca0f2a01582bdf47dff61f81
commit | author | age
48e9c1 1 <?php
T 2
3 /**
4  * Present identities settings dialog to new users
5  *
6  * When a new user is created, this plugin checks the default identity
7  * and sets a session flag in case it is incomplete. An overlay box will appear
8  * on the screen until the user has reviewed/completed his identity.
9  *
10  * @version @package_version@
11  * @license GNU GPLv3+
12  * @author Thomas Bruederli
e58f3d 13  * @author Aleksander Machniak
48e9c1 14  */
T 15 class new_user_dialog extends rcube_plugin
16 {
d5038e 17     public $task = 'login|mail';
AM 18     public $noframe = true;
48e9c1 19
d5038e 20     function init()
AM 21     {
22         $this->add_hook('identity_create', array($this, 'create_identity'));
23         $this->register_action('plugin.newusersave', array($this, 'save_data'));
48e9c1 24
d5038e 25         // register additional hooks if session flag is set
AM 26         if ($_SESSION['plugin.newuserdialog']) {
27             $this->add_hook('render_page', array($this, 'render_page'));
28         }
2581c8 29     }
AM 30
d5038e 31     /**
AM 32      * Check newly created identity at first login
33      */
34     function create_identity($p)
35     {
36         // set session flag when a new user was created and the default identity seems to be incomplete
37         if ($p['login'] && !$p['complete']) {
38             $_SESSION['plugin.newuserdialog'] = true;
39         }
48e9c1 40     }
T 41
d5038e 42     /**
AM 43      * Callback function when HTML page is rendered
44      * We'll add an overlay box here.
45      */
46     function render_page($p)
47     {
48         if ($_SESSION['plugin.newuserdialog'] && $p['template'] == 'mail') {
49             $this->add_texts('localization');
50
51             $rcmail           = rcmail::get_instance();
52             $identity         = $rcmail->user->get_identity();
53             $identities_level = intval($rcmail->config->get('identities_level', 0));
54
55             // compose user-identity dialog
56             $table = new html_table(array('cols' => 2));
57
58             $table->add('title', $this->gettext('name'));
59             $table->add(null, html::tag('input', array(
60                     'type'     => 'text',
61                     'name'     => '_name',
62                     'value'    => $identity['name'],
63                     'disabled' => $identities_level == 4
64             )));
65
66             $table->add('title', $this->gettext('email'));
67             $table->add(null, html::tag('input', array(
68                     'type'     => 'text',
69                     'name'     => '_email',
70                     'value'    => rcube_utils::idn_to_utf8($identity['email']),
71                     'disabled' => in_array($identities_level, array(1, 3, 4))
72             )));
73
74             $table->add('title', $this->gettext('organization'));
75             $table->add(null, html::tag('input', array(
76                     'type'     => 'text',
77                     'name'     => '_organization',
78                     'value'    => $identity['organization'],
79                     'disabled' => $identities_level == 4
80             )));
81
82             $table->add('title', $this->gettext('signature'));
83             $table->add(null, html::tag('textarea', array(
84                     'name' => '_signature',
85                     'rows' => '3',
86                 ),
87                 $identity['signature']
88             ));
89
90             // add overlay input box to html page
91             $rcmail->output->add_footer(html::tag('form', array(
92                     'id' => 'newuserdialog',
93                     'action' => $rcmail->url('plugin.newusersave'),
94                     'method' => 'post'
95                 ),
96                 html::p('hint', rcube::Q($this->gettext('identitydialoghint'))) .
97                     $table->show() .
98                     html::p(array('class' => 'formbuttons'),
99                         html::tag('input', array('type' => 'submit',
100                             'class' => 'button mainaction', 'value' => $this->gettext('save'))))
101             ));
102
103             $title  = rcube::JQ($this->gettext('identitydialogtitle'));
104             $script = "
105 $('#newuserdialog').show()
106   .dialog({modal:true, resizable:false, closeOnEscape:false, width:450, title:'$title'})
107   .submit(function() {
108     var i, request = {}, form = $(this).serializeArray();
109     for (i in form)
110       request[form[i].name] = form[i].value;
111
112       rcmail.http_post('plugin.newusersave', request, true);
113       return false;
114   });
115
116 $('input[name=_name]').focus();
117 rcube_webmail.prototype.new_user_dialog_close = function() { $('#newuserdialog').dialog('close'); }
118 ";
119             // disable keyboard events for messages list (#1486726)
120             $rcmail->output->add_script($script, 'docready');
121
122             $this->include_stylesheet('newuserdialog.css');
123         }
e58f3d 124     }
AM 125
d5038e 126     /**
AM 127      * Handler for submitted form (ajax request)
128      *
129      * Check fields and save to default identity if valid.
130      * Afterwards the session flag is removed and we're done.
131      */
132     function save_data()
133     {
134         $rcmail      = rcmail::get_instance();
135         $identity    = $rcmail->user->get_identity();
136         $ident_level = intval($rcmail->config->get('identities_level', 0));
137         $disabled    = array();
48e9c1 138
d5038e 139         $save_data = array(
AM 140             'name'         => rcube_utils::get_input_value('_name', rcube_utils::INPUT_POST),
141             'email'        => rcube_utils::get_input_value('_email', rcube_utils::INPUT_POST),
142             'organization' => rcube_utils::get_input_value('_organization', rcube_utils::INPUT_POST),
143             'signature'    => rcube_utils::get_input_value('_signature', rcube_utils::INPUT_POST),
144         );
145
146         if ($ident_level == 4) {
147             $disabled = array('name', 'email', 'organization');
148         }
149         else if (in_array($ident_level, array(1, 3))) {
150             $disabled = array('email');
151         }
152
153         foreach ($disabled as $key) {
154             $save_data[$key] = $identity[$key];
155         }
156
157         if (empty($save_data['name']) || empty($save_data['email'])) {
158             $rcmail->output->show_message('formincomplete', 'error');
159         }
160         else if (!rcube_utils::check_email($save_data['email'] = rcube_utils::idn_to_ascii($save_data['email']))) {
161             $rcmail->output->show_message('emailformaterror', 'error', array('email' => $save_data['email']));
162         }
163         else {
164             // save data
165             $rcmail->user->update_identity($identity['identity_id'], $save_data);
166             $rcmail->session->remove('plugin.newuserdialog');
167             // hide dialog
168             $rcmail->output->command('new_user_dialog_close');
169             $rcmail->output->show_message('successfullysaved', 'confirmation');
170         }
171
172         $rcmail->output->send();
173     }
48e9c1 174 }