Aleksander Machniak
2013-03-06 d7e129e07df65b0603418b4ece926100397a8f66
program/steps/addressbook/func.inc
@@ -5,8 +5,11 @@
 | program/steps/addressbook/func.inc                                    |
 |                                                                       |
 | This file is part of the Roundcube Webmail client                     |
 | Copyright (C) 2005-2007, The Roundcube Dev Team                       |
 | Licensed under the GNU GPL                                            |
 | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
 |                                                                       |
 | Licensed under the GNU General Public License version 3 or            |
 | any later version with exceptions for skins & plugins.                |
 | See the README file for a full license statement.                     |
 |                                                                       |
 | PURPOSE:                                                              |
 |   Provide addressbook functionality and GUI objects                   |
@@ -14,9 +17,6 @@
 +-----------------------------------------------------------------------+
 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
 +-----------------------------------------------------------------------+
 $Id$
*/
$SEARCH_MODS_DEFAULT = array('name'=>1, 'firstname'=>1, 'surname'=>1, 'email'=>1, '*'=>1);
@@ -26,7 +26,7 @@
  'name'         => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('name'), 'category' => 'main'),
  'firstname'    => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('firstname'), 'category' => 'main'),
  'surname'      => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('surname'), 'category' => 'main'),
  'email'        => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => rcube_label('email'), 'subtypes' => array('home','work','other'), 'category' => 'main'),
  'email'        => array('type' => 'text', 'size' => 40, 'maxlength' => 254, 'label' => rcube_label('email'), 'subtypes' => array('home','work','other'), 'category' => 'main'),
  'middlename'   => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => rcube_label('middlename'), 'category' => 'main'),
  'prefix'       => array('type' => 'text', 'size' => 8,  'maxlength' => 20, 'limit' => 1, 'label' => rcube_label('nameprefix'), 'category' => 'main'),
  'suffix'       => array('type' => 'text', 'size' => 8,  'maxlength' => 20, 'limit' => 1, 'label' => rcube_label('namesuffix'), 'category' => 'main'),
@@ -56,17 +56,12 @@
  // TODO: define fields for vcards like GEO, KEY
);
$PAGE_SIZE = $RCMAIL->config->get('addressbook_pagesize', $RCMAIL->config->get('pagesize', 50));
// Addressbook UI
if (!$RCMAIL->action && !$OUTPUT->ajax_call) {
    // add list of address sources to client env
    $js_list = $RCMAIL->get_address_sources();
    $source = get_input_value('_source', RCUBE_INPUT_GPC);
    // use first directory by default
    if (!strlen($source) || !isset($js_list[$source]))
        $source = $js_list[key($js_list)]['id'];
    // count all/writeable sources
    $writeable = 0;
@@ -86,13 +81,22 @@
    $OUTPUT->set_env('search_mods', $search_mods);
    $OUTPUT->set_env('address_sources', $js_list);
    $OUTPUT->set_env('writable_source', $writeable);
    $OUTPUT->set_env('compose_extwin', $RCMAIL->config->get('compose_extwin',false));
    $OUTPUT->set_pagetitle(rcube_label('addressbook'));
    $_SESSION['addressbooks_count'] = $count;
    $_SESSION['addressbooks_count_writeable'] = $writeable;
    if (!strlen($source))
      $source = strval(key($js_list));
    // select address book
    $source = get_input_value('_source', RCUBE_INPUT_GPC);
    // use first directory by default
    if (!strlen($source) || !isset($js_list[$source])) {
        $source = $RCMAIL->config->get('default_addressbook');
        if (!strlen($source) || !isset($js_list[$source])) {
            $source = strval(key($js_list));
        }
    }
    $CONTACTS = rcmail_contact_source($source, true);
}
@@ -108,7 +112,7 @@
// instantiate a contacts object according to the given source
function rcmail_contact_source($source=null, $init_env=false, $writable=false)
{
    global $RCMAIL, $OUTPUT, $CONFIG, $CONTACT_COLTYPES;
    global $RCMAIL, $OUTPUT, $CONTACT_COLTYPES, $PAGE_SIZE;
    if (!strlen($source)) {
        $source = get_input_value('_source', RCUBE_INPUT_GPC);
@@ -116,7 +120,7 @@
    // Get object
    $CONTACTS = $RCMAIL->get_address_book($source, $writable);
    $CONTACTS->set_pagesize($CONFIG['pagesize']);
    $CONTACTS->set_pagesize($PAGE_SIZE);
    // set list properties and session vars
    if (!empty($_GET['_page']))
@@ -140,8 +144,13 @@
        $CONTACT_COLTYPES = array_intersect_key($CONTACT_COLTYPES, $contact_cols);
        // add associative coltypes definition
        if (!$CONTACTS->coltypes[0]) {
            foreach ($CONTACTS->coltypes as $col => $colprop)
            foreach ($CONTACTS->coltypes as $col => $colprop) {
                if (is_array($colprop['childs'])) {
                    foreach ($colprop['childs'] as $childcol => $childprop)
                        $colprop['childs'][$childcol] = array_merge((array)$CONTACT_COLTYPES[$col]['childs'][$childcol], $childprop);
                }
                $CONTACT_COLTYPES[$col] = $CONTACT_COLTYPES[$col] ? array_merge($CONTACT_COLTYPES[$col], $colprop) : $colprop;
            }
        }
    }
@@ -150,17 +159,6 @@
    return $CONTACTS;
}
function rcmail_default_source($writable=false)
{
    global $RCMAIL;
    // get list of address sources
    $first = reset($RCMAIL->get_address_sources($writable));
    // use first directory by default
    return $first['id'];
}
function rcmail_set_sourcename($abook)
{
@@ -172,7 +170,7 @@
        if (!$name && $source == 0) {
            $name = rcube_label('personaladrbook');
        }
        $OUTPUT->set_env('sourcename', $name);
        $OUTPUT->set_env('sourcename', html_entity_decode($name, ENT_COMPAT, 'UTF-8'));
    }
}
@@ -189,12 +187,12 @@
    $jsdata = array();
    $line_templ = html::tag('li', array(
        'id' => 'rcmli%s', 'class' => '%s'),
        'id' => 'rcmli%s', 'class' => '%s', 'noclose' => true),
        html::a(array('href' => '%s',
            'rel' => '%s',
            'onclick' => "return ".JS_OBJECT_NAME.".command('list','%s',this)"), '%s'));
    $sources = (array) $OUTPUT->env['address_sources'];
    $sources = (array) $OUTPUT->get_env('address_sources');
    reset($sources);
    // currently selected source
@@ -213,22 +211,24 @@
        if ($source['class_name'])
            $class_name .= ' ' . $source['class_name'];
        $name = !empty($source['name']) ? $source['name'] : $id;
        $out .= sprintf($line_templ,
            html_identifier($id),
            rcube_utils::html_identifier($id, true),
            $class_name,
            Q(rcmail_url(null, array('_source' => $id))),
            $source['id'],
            $js_id, (!empty($source['name']) ? Q($source['name']) : Q($id)));
            $js_id, $name);
        $groupdata = array('out' => $out, 'jsdata' => $jsdata, 'source' => $id);
        if ($source['groups'])
            $groupdata = rcmail_contact_groups($groupdata);
        $jsdata = $groupdata['jsdata'];
        $out = $groupdata['out'];
        $out .= '</li>';
    }
    $line_templ = html::tag('li', array(
        'id' => 'rcmliS%s', 'class' => '%s'),
        'id' => 'rcmli%s', 'class' => '%s'),
        html::a(array('href' => '#', 'rel' => 'S%s',
            'onclick' => "return ".JS_OBJECT_NAME.".command('listsearch', '%s', this)"), '%s'));
@@ -246,14 +246,17 @@
            $class_name .= ' ' . $source['class_name'];
        $out .= sprintf($line_templ,
            html_identifier($id),
            rcube_utils::html_identifier('S'.$id, true),
            $class_name,
            $id,
            $js_id, (!empty($source['name']) ? Q($source['name']) : Q($id)));
    }
    $OUTPUT->set_env('contactgroups', $jsdata);
    $OUTPUT->set_env('collapsed_abooks', (string)$RCMAIL->config->get('collapsed_abooks',''));
    $OUTPUT->add_gui_object('folderlist', $attrib['id']);
    $OUTPUT->include_script('treelist.js');
    // add some labels to client
    $OUTPUT->add_label('deletegroupconfirm', 'groupdeleting', 'addingmember', 'removingmember');
@@ -265,19 +268,25 @@
{
    global $RCMAIL;
    $groups_html = '';
    $groups = $RCMAIL->get_address_book($args['source'])->list_groups();
    $js_id = $RCMAIL->JQ($args['source']);
    if (!empty($groups)) {
        $line_templ = html::tag('li', array(
            'id' => 'rcmliG%s', 'class' => 'contactgroup'),
            'id' => 'rcmli%s', 'class' => 'contactgroup'),
            html::a(array('href' => '#',
                'rel' => '%s:%s',
                'onclick' => "return ".JS_OBJECT_NAME.".command('listgroup',{'source':'%s','id':'%s'},this)"), '%s'));
        // append collapse/expand toggle and open a new <ul>
        $is_collapsed = strpos($RCMAIL->config->get('collapsed_abooks',''), '&'.rawurlencode($args['source']).'&') !== false;
        $args['out'] .= html::div('treetoggle ' . ($is_collapsed ? 'collapsed' : 'expanded'), '&nbsp;');
        $jsdata = array();
        foreach ($groups as $group) {
            $args['out'] .= sprintf($line_templ,
                html_identifier($args['source'] . $group['ID']),
            $groups_html .= sprintf($line_templ,
                rcube_utils::html_identifier('G' . $args['source'] . $group['ID'], true),
                $args['source'], $group['ID'],
                $args['source'], $group['ID'], Q($group['name'])
            );
@@ -286,6 +295,10 @@
                'name' => $group['name'], 'type' => 'group');
        }
    }
    $args['out'] .= html::tag('ul',
      array('class' => 'groups', 'style' => ($is_collapsed ? "display:none;" : null)),
      $groups_html);
    return $args;
}
@@ -330,6 +343,7 @@
    while ($row = $result->next()) {
        $a_row_cols = array();
        $classes = array('person');  // org records will follow some day
        // build contact ID with source ID
        if (isset($row['sourceid'])) {
@@ -338,15 +352,14 @@
        // format each col
        foreach ($a_show_cols as $col) {
            $val = $row[$col];
            if ($val == '' && $col == 'name') {
                $val = rcube_addressbook::compose_display_name($row, true);
            }
            $val = $col == 'name' ? rcube_addressbook::compose_list_name($row) : $row[$col];
            $a_row_cols[$col] = Q($val);
   }
        }
        $OUTPUT->command($prefix.'add_contact_row', $row['ID'], $a_row_cols);
        if ($row['readonly'])
            $classes[] = 'readonly';
        $OUTPUT->command($prefix.'add_contact_row', $row['ID'], $a_row_cols, join(' ', $classes));
    }
}
@@ -359,12 +372,7 @@
    if (!$attrib['id'])
        $attrib['id'] = 'rcmcontactframe';
    $attrib['name'] = $attrib['id'];
    $OUTPUT->set_env('contentframe', $attrib['name']);
    $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
    return html::iframe($attrib);
    return $OUTPUT->frame($attrib, true);
}
@@ -377,13 +385,16 @@
    $OUTPUT->add_gui_object('countdisplay', $attrib['id']);
    if ($attrib['label'])
      $_SESSION['contactcountdisplay'] = $attrib['label'];
    return html::span($attrib, rcube_label('loading'));
}
function rcmail_get_rowcount_text($result=null)
{
    global $CONTACTS, $CONFIG;
    global $CONTACTS, $PAGE_SIZE;
    // read nr of contacts
    if (!$result) {
@@ -394,10 +405,10 @@
        $out = rcube_label('nocontactsfound');
    else
        $out = rcube_label(array(
            'name'  => 'contactsfromto',
            'name'  => $_SESSION['contactcountdisplay'] ? $_SESSION['contactcountdisplay'] : 'contactsfromto',
            'vars'  => array(
            'from'  => $result->first + 1,
            'to'    => min($result->count, $result->first + $CONFIG['pagesize']),
            'to'    => min($result->count, $result->first + $PAGE_SIZE),
            'count' => $result->count)
        ));
@@ -422,7 +433,6 @@
function rcmail_contact_form($form, $record, $attrib = null)
{
    global $RCMAIL, $CONFIG;
    static $jqueryui_loaded = 0;
    // Allow plugins to modify contact form content
    $plugin = $RCMAIL->plugins->exec_hook('contact_form', array(
@@ -572,8 +582,13 @@
                if (empty($values) && $colprop['visible'])
                    $values[] = '';
                if (!is_array($values)) {
                    // $values can be an object, don't use (array)$values syntax
                    $values = !empty($values) ? array($values) : array();
                }
                $rows = '';
                foreach ((array)$values as $i => $val) {
                foreach ($values as $i => $val) {
                    if ($subtypes[$i])
                        $subtype = $subtypes[$i];
@@ -582,7 +597,12 @@
                        $composite = array(); $j = 0;
                        $template = $RCMAIL->config->get($col . '_template', '{'.join('} {', array_keys($colprop['childs'])).'}');
                        foreach ($colprop['childs'] as $childcol => $cp) {
                            $childvalue = $val[$childcol] ? $val[$childcol] : $val[$j];
                            if (!empty($val) && is_array($val)) {
                                $childvalue = $val[$childcol] ? $val[$childcol] : $val[$j];
                            }
                            else {
                                $childvalue = '';
                            }
                            if ($edit_mode) {
                                if ($colprop['subtypes'] || $colprop['limit'] != 1) $cp['array'] = true;
@@ -597,7 +617,7 @@
                        $coltypes[$field] += (array)$colprop;
                        $coltypes[$field]['count']++;
                        $val = strtr($template, $composite);
                        $val = preg_replace('/\{\w+\}/', '', strtr($template, $composite));
                    }
                    else if ($edit_mode) {
                        // call callback to render/format value
@@ -609,17 +629,11 @@
                        if ($colprop['subtypes'] || $colprop['limit'] != 1)
                            $colprop['array'] = true;
                        // load jquery UI datepickert for date fields
                        // load jquery UI datepicker for date fields
                        if ($colprop['type'] == 'date') {
                            if (!$jqueryui_loaded++) {
                                $RCMAIL->plugins->load_plugin('jqueryui');
                                $RCMAIL->output->set_env('date_format', strtr($RCMAIL->config->get('date_format', 'Y-m-d'), array('y'=>'y', 'Y'=>'yy', 'm'=>'mm', 'n'=>'m', 'd'=>'dd', 'j'=>'d')));
                                foreach (array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec') as $month)
                                    $month_names[] = rcube_label($month);
                                $RCMAIL->output->set_env('month_names', $month_names);
                            }
                            $colprop['class'] .= ($colprop['class'] ? ' ' : '') . 'datepicker';
                            $val = format_date($val, $RCMAIL->config->get('date_format', 'Y-m-d'));
                            if (!$colprop['render_func'])
                                $val = rcmail_format_date_col($val);
                        }
                        $val = rcmail_get_edit_field($col, $val, $colprop, $colprop['type']);
@@ -657,12 +671,14 @@
                }
                // wrap rows in fieldgroup container
                $content .= html::tag('fieldset', array('class' => 'contactfieldgroup ' . ($colprop['subtypes'] ? 'contactfieldgroupmulti ' : '') . 'contactcontroller' . $col, 'style' => ($rows ? null : 'display:none')),
                  ($colprop['subtypes'] ? html::tag('legend', null, Q($colprop['label'])) : ' ') .
                  $rows);
                if ($rows) {
                    $content .= html::tag('fieldset', array('class' => 'contactfieldgroup ' . ($colprop['subtypes'] ? 'contactfieldgroupmulti ' : '') . 'contactcontroller' . $col, 'style' => ($rows ? null : 'display:none')),
                      ($colprop['subtypes'] ? html::tag('legend', null, Q($colprop['label'])) : ' ') .
                      $rows);
                }
            }
            if (!$content)
            if (!$content && (!$edit_mode || !$select_add->_count))
                continue;
            // also render add-field selector
@@ -693,17 +709,18 @@
{
    global $SOURCE_ID, $CONTACTS, $CONTACT_COLTYPES, $RCMAIL, $CONFIG;
    if (!$CONTACT_COLTYPES['photo'])
        return '';
    if ($result = $CONTACTS->get_result())
        $record = $result->first();
    $photo_img = $attrib['placeholder'] ? $CONFIG['skin_path'] . $attrib['placeholder'] : 'program/blank.gif';
    $photo_img = $attrib['placeholder'] ? $CONFIG['skin_path'] . $attrib['placeholder'] : 'program/resources/blank.gif';
    $RCMAIL->output->set_env('photo_placeholder', $photo_img);
    unset($attrib['placeholder']);
    if (strpos($record['photo'], 'http:') === 0)
    $plugin = $RCMAIL->plugins->exec_hook('contact_photo', array('record' => $record, 'data' => $record['photo']));
    if ($plugin['url'])
        $photo_img = $plugin['url'];
    else if (preg_match('!^https?://!i', $record['photo']))
        $photo_img = $record['photo'];
    else if ($record['photo'])
        $photo_img = $RCMAIL->url(array('_action' => 'photo', '_cid' => $record['ID'], '_source' => $SOURCE_ID));
@@ -713,7 +730,7 @@
    $img = html::img(array('src' => $photo_img, 'border' => 1, 'alt' => ''));
    $content = html::div($attrib, $img);
    if ($RCMAIL->action == 'edit' || $RCMAIL->action == 'add') {
    if ($CONTACT_COLTYPES['photo'] && ($RCMAIL->action == 'edit' || $RCMAIL->action == 'add')) {
        $RCMAIL->output->add_gui_object('contactphoto', $attrib['id']);
        $hidden = new html_hiddenfield(array('name' => '_photo', 'id' => 'ff_photo', 'value' => $ff_value));
        $content .= $hidden->show();
@@ -726,7 +743,7 @@
function rcmail_format_date_col($val)
{
    global $RCMAIL;
    return format_date($val, $RCMAIL->config->get('date_format', 'Y-m-d'));
    return format_date($val, $RCMAIL->config->get('date_format', 'Y-m-d'), false);
}
@@ -735,7 +752,7 @@
 *
 * @return array List of contact IDs per-source
 */
function rcmail_get_cids()
function rcmail_get_cids($filter = null)
{
    // contact ID (or comma-separated list of IDs) is provided in two
    // forms. If _source is an empty string then the ID is a string
@@ -743,6 +760,10 @@
    $cid    = get_input_value('_cid', RCUBE_INPUT_GPC);
    $source = (string) get_input_value('_source', RCUBE_INPUT_GPC);
    if (is_array($cid)) {
        return $cid;
    }
    if (!preg_match('/^[a-zA-Z0-9\+\/=_-]+(,[a-zA-Z0-9\+\/=_-]+)*$/', $cid)) {
        return array();
@@ -754,21 +775,29 @@
    // create per-source contact IDs array
    foreach ($cid as $id) {
        // if _source is not specified we'll find it from decoded ID
        // extract source ID from contact ID (it's there in search mode)
        // see #1488959 and #1488862 for reference
        if (!$got_source) {
            list ($c, $s) = explode('-', $id, 2);
            if (strlen($s)) {
                $result[(string)$s][] = $c;
            if ($sep = strrpos($id, '-')) {
                $contact_id = substr($id, 0, $sep);
                $source_id  = (string) substr($id, $sep+1);
                if (strlen($source_id)) {
                    $result[$source_id][] = $contact_id;
                }
            }
        }
        else {
            if (substr($id, -($got_source+1)) == "-$source") {
                $id = substr($id, 0, -($got_source+1));
            }
            $result[$source][] = $id;
        }
    }
    return $result;
    return $filter !== null ? $result[$filter] : $result;
}
// register UI objects
$OUTPUT->add_handlers(array(
    'directorylist' => 'rcmail_directory_list',