File: qrcode.php

package info (click to toggle)
roundcube 1.6.13%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,888 kB
  • sloc: javascript: 195,591; php: 76,917; sql: 3,150; sh: 2,882; pascal: 1,079; makefile: 234; xml: 93; perl: 73; ansic: 48; python: 21
file content (131 lines) | stat: -rw-r--r-- 4,426 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php

/**
 +-----------------------------------------------------------------------+
 | This file is part of the Roundcube Webmail client                     |
 |                                                                       |
 | Copyright (C) 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:                                                              |
 |   Show contact data as QR code                                        |
 +-----------------------------------------------------------------------+
 | Author: Aleksander Machniak <alec@alec.pl>                            |
 +-----------------------------------------------------------------------+
*/

if (stream_resolve_include_path('Bacon/BaconQrCode/autoload.php'))
    include_once('Bacon/BaconQrCode/autoload.php');

class rcmail_action_contacts_qrcode extends rcmail_action_contacts_index
{
    protected static $mode = self::MODE_HTTP;

    /**
     * Request handler.
     *
     * @param array $args Arguments from the previous step(s)
     */
    public function run($args = [])
    {
        // Get contact ID and source ID from request
        $cids   = self::get_cids();
        $source = key($cids);
        $cid    = $cids ? array_first($cids[$source]) : null;
        $rcmail = rcmail::get_instance();

        // read contact record
        $abook   = self::contact_source($source, true);
        $contact = $abook->get_record($cid, true);

        // generate QR code image
        if ($data = self::contact_qrcode($contact)) {
            $headers = [
                'Content-Type: ' . self::check_support(),
                'Content-Length: ' . strlen($data)
            ];

            $rcmail->output->sendExit($data, $headers);
        }

        $rcmail->output->sendExit('', ['HTTP/1.0 404 Contact not found']);
    }

    /**
     * Generate a QR-code image for a contact
     *
     * @param array $contact Contact record
     *
     * @return string|null Image content, Null on error or missing PHP extensions
     */
    public static function contact_qrcode($contact)
    {
        if (empty($contact)) {
            return null;
        }

        $type = self::check_support();

        if (empty($type)) {
            return null;
        }

        $vcard = new rcube_vcard();

        // QR code input is limited, use only common fields
        $fields = ['name', 'firstname', 'surname', 'middlename', 'nickname',
            'organization', 'phone', 'email', 'jobtitle', 'prefix', 'suffix'];

        foreach ($contact as $field => $value) {
            if (strpos($field, ':') !== false) {
                list($field, $section) = explode(':', $field, 2);
            }
            else {
                $section = null;
            }

            if (in_array($field, $fields)) {
                foreach ((array) $value as $v) {
                    $vcard->set($field, $v, $section);
                }
            }
        }

        $data = $vcard->export();

        if (empty($data)) {
            return null;
        }

        $renderer_style = new BaconQrCode\Renderer\RendererStyle\RendererStyle(300, 1);
        $renderer_image = $type == 'image/png'
            ? new BaconQrCode\Renderer\Image\ImagickImageBackEnd()
            : new BaconQrCode\Renderer\Image\SvgImageBackEnd();

        $renderer = new BaconQrCode\Renderer\ImageRenderer($renderer_style, $renderer_image);
        $writer   = new BaconQrCode\Writer($renderer);

        return $writer->writeString($data, RCUBE_CHARSET);
    }

    /**
     * Check required extensions and classes for QR code generation
     *
     * @return string|null Content-type of the image result
     */
    public static function check_support()
    {
        if (extension_loaded('iconv') && class_exists('BaconQrCode\Renderer\ImageRenderer')) {
            if (extension_loaded('xmlwriter')) {
                return 'image/svg+xml';
            }

            if (extension_loaded('imagick')) {
                return 'image/png';
            }
        }
    }
}