Link.php 23.7 KB
Newer Older
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
<?PHP
/***************************************************************
 *  Copyright notice
 *
 *  (c) 2010 Glowbase GmbH <support@glowbase.com>
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

namespace qfq;

//use qfq;

require_once(__DIR__ . '/Define.php');
require_once(__DIR__ . '/Utils.php');
31
32
require_once(__DIR__ . '/../store/Store.php');
require_once(__DIR__ . '/../store/Sip.php');
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

/*
 * u:url
 * m:mailto
 * p:page
 * 
 * t:text
 * a:AltText
 * o:ToolTip
 * 
 * r:render   
 * g:target
 * c:class  [n|i|e]:<class>
 * q:question  <text>
 * e:encryption 0|1
 * 
 * E:edit
 * N:new
 * D:delete
 * H:Help
 * I:information
 * M:Magnifier
 * B:bullet
 * P:picture       [file]
 * C:checkbox    [name]
 * R:right 
 * h:hash  
60
 *
61
62
63
64
65
66
 * A:    A:[u:p:m]
 * G:    G:[N|..]
 */

class Link {

67
68
69
    //TODO: delete muss noch implementiert werden
    private $delete = '';

Carsten  Rose's avatar
Carsten Rose committed
70
    /**
71
     * @var Sip
Carsten  Rose's avatar
Carsten Rose committed
72
73
74
     */
    private $sip = null;

75
76
77
78
79
    /**
     * @var Store
     */
    private $store = null;

80
81
82
83
84
85
86
87
    // Simulate global variable: will be set much earlier in other functions. Will be shown in error messages.
    private $fr_error;

    /**
     * @var Utils
     */
    private $utils;
    /**
88
     * @var array
89
     */
90
91
92
93
    private $renderControl = array();
    private $linkClassSelector = array("i" => "internal", "e" => "external");
    private $cssLinkClassInternal = '';
    private $cssLinkClassExternal = '';
94

95
96
97
    /**
     * __construct
     *
98
99
     * @param $fr_error
     * @param Sip $sip
100
     */
Carsten  Rose's avatar
Carsten Rose committed
101
    public function __construct($fr_error, Sip $sip, $phpUnit = false) {
102
        $this->fr_error = $fr_error;
Carsten  Rose's avatar
Carsten Rose committed
103
        $this->sip = $sip;
Carsten  Rose's avatar
Carsten Rose committed
104
        $this->store = Store::getInstance('', $phpUnit);
105
106
        $this->cssLinkClassInternal = $this->store->getVar(SYSTEM_CSS_LINK_CLASS_INTERNAL, STORE_SYSTEM);
        $this->cssLinkClassExternal = $this->store->getVar(SYSTEM_CSS_LINK_CLASS_EXTERNAL, STORE_SYSTEM);
107
108
        $this->initRenderControl();

109
    }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

    /**
     * Initializes RenderControl Array
     *
     * @param    void
     * @return    void
     */
    private function initRenderControl() {

        $this->utils = new Utils();

        /*
         * mode:
         * 0: no output
         * 1: text
         * 2: url
         * 3: <a href=url>url</a>
         * 4: <a href=url>Text</a>
         *
Carsten  Rose's avatar
Carsten Rose committed
129
         *  r=render mode, u=url, t:text and/or image.
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
         *
         *                  [r][u][t] = mode
         */

        $this->renderControl[0][0][0] = 0;
        $this->renderControl[0][0][1] = 0;
        $this->renderControl[0][1][0] = 3;
        $this->renderControl[0][1][1] = 4;

        $this->renderControl[1][0][0] = 0;
        $this->renderControl[1][0][1] = 1;
        $this->renderControl[1][1][0] = 3;
        $this->renderControl[1][1][1] = 4;

        $this->renderControl[2][0][0] = 0;
        $this->renderControl[2][0][1] = 0;
        $this->renderControl[2][1][0] = 0;
        $this->renderControl[2][1][1] = 4;

        $this->renderControl[3][0][0] = 0;
        $this->renderControl[3][0][1] = 1;
        $this->renderControl[3][1][0] = 2;
        $this->renderControl[3][1][1] = 1;

        $this->renderControl[4][0][0] = 0;
        $this->renderControl[4][0][1] = 1;
        $this->renderControl[4][1][0] = 2;
        $this->renderControl[4][1][1] = 2;

        $this->renderControl[5][0][0] = 0;
        $this->renderControl[5][0][1] = 0;
        $this->renderControl[5][1][0] = 0;
        $this->renderControl[5][1][1] = 0;
163
    }
164
165
166
167
168
169
170
171

    /**
     * Build the whole link
     *
     * @param    string $str :  Qualifier with params. 'report'-syntax. F.e.:  A:u:www.example.com|G:P:home.gif|t:Home"
     *
     * @return    string      The complete Link
     */
172
    public function renderLink($str) {
173
174
175
176
        $link = '';

        $vars = array();
        $this->clearVars($vars);
177
178

        // str="u:http://www.example.com|c:i|t:delete"
179
        $param = explode("|", $str);
180
181

        // Parse all parameter, fill variables
182
183
184
        foreach ($param as $item) {

            // set class defaults
185
            $this->parseItem($vars, $item);
186
187
        }

188
        // if there is no url or mailto definition: {{global.pageId}}
189
190
191
        if ($vars['url'] == '' && $vars['mailto'] == '') {
            $vars['url'] = "?" . $this->store->getVar(TYPO3_PAGE_ID, STORE_TYPO3);
        }
192

193
        $this->doCssClass($vars);
194

195
196
        $htmlUrl = $this->doUrl($vars);
        $htmlImage = $this->doImage($vars);
197

198
// Compose Image & Text
199
        $distance = ' ';
200
        $vars['text'] = ($vars['picturePosition'] == "l") ? $htmlImage . $distance . $vars['text'] : $vars['text'] . $distance . $htmlImage;
201

202
203
204
205
// ToolTip
        $extraSpan = ['', ''];
        if ($vars['toolTip']) {
            $extraSpan[0] = "<span " . $vars['toolTip'][0] . ">" . $vars['toolTip'][1];
206
207
208
            $extraSpan[1] = "</span>";
        }

209
// Create 'fake' modes for encrypted 'mailto'
210
        $prefix = "";
211
        if ($vars['mailto'] != '') {
212
            $prefix = "1";
213
            $vars['url'] = "dummy";
214
        }
215
216
// Create 'fake' mode for ajax delete
        if ($vars['delete']) {
217
218
            $prefix = "2";
        }
219
220
221
222
223
// Compose URL
// get Render Mode via Array renderControl
        $mode = $prefix . $this->renderControl[$vars['modeRender']][$vars['url'] ? 1 : 0][$vars['text'] ? 1 : 0];
// 0-4 URL, plain email
// 10-14 encrypted email
224
225
226
227
228
        switch ($mode) {
            // 0: No Output
            case '0':
            case '10':
            case '20':
229
            $link = "";
230
231
232
233
                break;

            // 1: 'text'
            case '1':
234
                $link = $extraSpan[0] . $vars['text'] . $extraSpan[1];
235
236
                break;
            case '11':
237
                $link = $extraSpan[0] . $this->encryptMailtoJS($vars, false) . $extraSpan[1];
238
239
240
241
                break;

            // 2: 'url'
            case '2':
242
                $link = $extraSpan[0] . $vars['url'] . $extraSpan[1];
243
244
                break;
            case '12':
245
                $link = $extraSpan[0] . $this->encryptMailtoJS($vars, false) . $extraSpan[1];
246
247
248
249
                break;

            // 3: <a href=url>url</a>
            case '3':
250
                $link = $htmlUrl . $vars['url'] . '</a>' . $vars['toolTip'][1];
251
252
                break;
            case '13':
253
254
                $vars['text'] = $vars['mailto'];
                $link = $this->encryptMailtoJS($vars, TRUE);
255
256
257
258
                break;

            // 4: <a href=url>Text</a>
            case '4':
259
                $link = $htmlUrl . $vars['text'] . '</a>' . $vars['toolTip'][1];
260
261
                break;
            case '14':
262
                $link = $this->encryptMailtoJS($vars, TRUE);
263
264
265
266
267
                break;
            case '21':
            case '22':
            case '23':
            case '24':
268
            //TODO: Alter Code, umstellen auf JS Client von RO. Vorlage koennte 'Delete' in Subrecord sein.
269
270
            $link = "<a href=\"javascript: void(0);\" onClick=\"var del = new FR.Delete({recordId:'',hash:'',forward:'" .
                $vars['pageAlias'] . "'});\" " . $vars['linkClass'] . ">" . $vars['text'] . "</a>";
271
272
        }

273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
        return $link;

    }

    private function clearVars(&$vars) {
        $vars['mailto'] = "";
        $vars['url'] = "";
        $vars['pageAlias'] = '';

        $vars['image'] = "";
        $vars['text'] = "";
        $vars['altText'] = "";
        $vars['imageTitle'] = "";
        $vars['question'] = "";
        $vars['target'] = "";
        $vars['toolTip'] = ['', ''];
        $vars['additionalUrlParams'] = "";

        $vars['modeRender'] = 0;
        $vars['picturePosition'] = "l";
        $vars['modeHash'] = false;
        $vars['encryption'] = 1;
        $vars['delete'] = '';

        $vars['linkClass'] = ""; // class name
        $vars['defaultLinkClass'] = ''; // Depending of 'as page' or 'as url'. Only used if class is not explizit set.
299

300
    }
301
302
303
304
305
306
307
308

    /**
     * Parse Item of link string, fill class global variables.
     *
     * @param    string $key :    F.e.: 'u'
     * @param    string $value :  F.e.: 'http://www.nzz.ch'
     * @throws SyntaxReportException
     */
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
    private
    function parseItem(&$vars, $item) {
        $key = '';
        $value = '';

        $arr = explode(":", $item, 2);
        switch (count($arr)) {
            case 0:
                return;
                break;

            case 1:
                $key = $arr[0];
                break;

            case 2:
                $key = $arr[0];
                $value = $arr[1];
                break;

            default:
                throw new CodeException("This statement should never be reached", ERROR_CODE_SHOULD_NOT_HAPPEN);

        }
333
334
335
336

        switch ($key) {
            // URL
            case "u":
337
338
339
340
                if ($vars['url'] || $vars['mailto'])
                    throw new SyntaxReportException ("Multiple URL, PAGE or MAILTO defined : '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['url'] = $value;
                $vars['defaultLinkClass'] = $this->cssLinkClassExternal;
341
342
                break;
            case "m":
343
344
345
346
                if ($vars['url'] || $vars['mailto'])
                    throw new SyntaxReportException ("Multiple URL, PAGE or MAILTO defined : '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['mailto'] = $value;
                $vars['defaultLinkClass'] = $this->cssLinkClassExternal;
347
348
                break;
            case "p":
349
350
351
                if ($vars['url'] || $vars['mailto'])
                    throw new SyntaxReportException ("Multiple URL, PAGE or MAILTO defined : '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['pageAlias'] = $value ? $value : $this->store->getVar(TYPO3_PAGE_ID, STORE_TYPO3); // If no pageid|pagealias is defined, take current page
Carsten  Rose's avatar
Carsten Rose committed
352
//                $this->url = "?" . $this->pageAlias;
353
                $vars['defaultLinkClass'] = $this->cssLinkClassInternal;
354
                break;
355

356
357
            // Text
            case "t":
358
                $vars['text'] = $value;
359
360
                break;
            case "a":
361
                $vars['altText'] = $value;
362
363
                break;
            case "o":
364
                $vars['toolTip'] = $this->utils->createToolTip($value);
365
366
367
368
                break;

            // Image
            case "P":
369
370
371
372
373
                if ($vars['image'])
                    throw new SyntaxReportException ("Multiple images defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['image'] = $value;
                $vars['AltText'] = "Grafic: " . $value;
                $vars['imageTitle'] = $value;
374
375
                break;
            case "B":
376
377
378
379
380
                if ($vars['image'])
                    throw new SyntaxReportException ("Multiple images defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['image'] = PATH_ICONS . "bullet-" . ($value ? $value : "green") . '.gif';
                $vars['defaultAltText'] = "Bullet: " . $value;
                $vars['imageTitle'] = $value;
381
382
                break;
            case "C":
383
384
385
386
387
                if ($vars['image'])
                    throw new SyntaxReportException ("Multiple images defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['image'] = PATH_ICONS . "checked-" . ($value ? $value : "green") . '.gif';
                $vars['defaultAltText'] = "Checked: " . $value;
                $vars['imageTitle'] = $value;
388
389
                break;
            case "D":
390
391
392
393
394
395
                if ($vars['image'])
                    throw new SyntaxReportException ("Multiple images defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['image'] = PATH_ICONS . 'delete.gif';
                $vars['defaultAltText'] = "Delete";
                $vars['imageTitle'] = "Delete";
                $vars['delete'] = true;
396
397
398
399
                // Include Extjs library
                $this->utils->loadJSlib($this->fr_error);
                break;
            case "E":
400
401
402
403
404
                if ($vars['image'])
                    throw new SyntaxReportException ("Multiple images defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['image'] = PATH_ICONS . 'edit.gif';
                $vars['defaultAltText'] = "Edit";
                $vars['imageTitle'] = "Edit";
405
406
                break;
            case "H":
407
408
409
410
411
                if ($vars['image'])
                    throw new SyntaxReportException ("Multiple images defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['image'] = PATH_ICONS . 'help.gif';
                $vars['defaultAltText'] = "Help";
                $vars['imageTitle'] = "Help";
412
413
                break;
            case "I":
414
415
416
417
418
                if ($vars['image'])
                    throw new SyntaxReportException ("Multiple images defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['image'] = PATH_ICONS . 'info.gif';
                $vars['defaultAltText'] = "Information";
                $vars['imageTitle'] = "Information";
419
420
                break;
            case "N":
421
422
423
424
425
                if ($vars['image'])
                    throw new SyntaxReportException ("Multiple images defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['image'] = PATH_ICONS . 'new.gif';
                $vars['defaultAltText'] = "New";
                $vars['imageTitle'] = "New";
426
427
                break;
            case "S":
428
429
430
431
432
                if ($vars['image'])
                    throw new SyntaxReportException ("Multiple images defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['image'] = PATH_ICONS . 'show.gif';
                $vars['defaultAltText'] = "Details";
                $vars['imageTitle'] = "Details";
433
434
435
436
                break;

            // Misc
            case "r":
437
438
439
                if ($vars['modeRender'])
                    throw new SyntaxReportException ("Multiple render modes defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['modeRender'] = $value;
440
441
                break;
            case "g":
442
443
444
                if ($vars['target'])
                    throw new SyntaxReportException ("Multiple 'target' defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['target'] = $value;
445
446
                break;
            case "c":
447
448
449
                if ($vars['linkClass'])
                    throw new SyntaxReportException ("Multiple linkClass defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['linkClass'] = $value;
450
451
                break;
            case "q":
452
453
454
                if ($vars['question'])
                    throw new SyntaxReportException ("Multiple question defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['question'] = $value ? $value : "Please confirm";
455
456
                break;
            case "e":
457
458
459
                if ($vars['encryption'] != 1)
                    throw new SyntaxReportException ("Multiple encryption defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['encryption'] = $value ? $value : "1";
460
461
                break;
            case "h":
462
463
464
                if ($vars['modeHash'])
                    throw new SyntaxReportException ("Multiple modeHash defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['modeHash'] = TRUE;
465
466
                break;
            case "U":    //if($this->additionalUrlParams) throw new syntaxException ( "Multiple additionalUrlParams defined: '$key'","",__FILE__,__LINE__, "Column: " . $fthis->r_error["columnIndex"], $this->fr_error);
467
                $vars['additionalUrlParams'] = $value;
468
469
                break;
            case "R":
470
471
472
                if ($vars['picturePosition'] != "l")
                    throw new SyntaxReportException ("Multiple picturePosition defined: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $vars['picturePosition'] = "r";
473
474
475
476
477
                break;

            // A,G
            case "G":
            case "A":
478
            // set class defaults
479
            $this->parseItem($vars, $value);
480
481
482
483
                break;
            case "" :
                break;
            default:
484
                throw new SyntaxReportException ("Unknown link qualifier: '$key'", "", null, __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
485
486
487

        }

488
    }
489
490
491
492
493
494
495
496

    /**
     * Parse CSS Class Settings
     *
     * @param    void
     *
     * @return    void
     */
497
498
    private
    function doCssClass(&$vars) {
499
        $class = '';
500

501
        switch ($vars['linkClass']) {
502
            case 'n':
503
                $vars['linkClass'] = '';
504
505
506
                break;
            case 'i':
            case 'e':
507
            $vars['linkClass'] = $this->linkClassSelector[$vars['linkClass']];
508
509
                break;
            default:
510
                $vars['linkClass'] = ($vars['linkClass'] == '') ? $vars['defaultLinkClass'] : $vars['linkClass'];
511
                break;
512
        }
513
    }
514
515

    /**
516
517
518
519
     * Builds complete HTML anchor.
     * - <a href="mailto:info@example.com:>info@example.com</a>
     * - <a href="http://example.com:>http://example.com</a>
     * - If $this->modeHash is set, create a
520
521
     *
     * @return string
522
     * @throws SyntaxReportException
523
     */
524
525
    private
    function doUrl(&$vars) {
Carsten  Rose's avatar
Carsten Rose committed
526
        $tags = '';
527
528
529
        // build URL
        $htmlUrl = "";

530
        if ($vars['url'] != '') {
531

532
            if ($vars['modeHash']) {
533

534
                $vars['url'] = $this->sip->queryStringToSip('id=' . $vars['pageAlias'] . '&' . $vars['additionalUrlParams']);
535

536
            } else {
537

538
                if ($vars['additionalUrlParams'] != '') {
539
                    // Append '&' or '?' depending if there is already a '?'
540
                    $vars['url'] .= (strpos($vars['url'], '?') >= 0) ? '&' : '?';
541
                    // Append all additional params.
542
                    $vars['url'] .= $vars['additionalUrlParams'];
543
544
                }
            }
545
546
        }

547
        // If there is no encryption: handle the mailto as an ordinary URL
548
        if ($vars['mailto'] != '') {
549

550
551
552
            if ($vars['encryption'] == 0) {
                $vars['url'] = "mailto:" . $vars['mailto'];
                $vars['mailto'] = "";
553
            } else {
554
                throw new SyntaxReportException ("Oops, sorry: encrypted mail not implemented ...", "", null, __FILE__, __LINE__,
555
                    "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
556
557
558
            }
        }

559
560
561
562
563
        $tags .= Support::doAttribute('href', $vars['url']);
        $tags .= Support::doAttribute('class', $vars['linkClass']);
        $tags .= Support::doAttribute('target', $vars['target']);
        if ($vars['question']) {
            $tags .= Support::doAttribute('onclick', 'confirm(\'' . $vars['question'] . '\')');
Carsten  Rose's avatar
Carsten Rose committed
564
        }
565

566
        $htmlUrl = '<a ' . $tags . $vars['toolTip'][0] . '>';
567
568
569

        return ($htmlUrl);
    }
570
571
572
573
574
575
576

    /**
     * Create Image HTML Tag
     * @return string $htmlImage
     * @internal param $void
     *
     */
577
578
579
    private
    function doImage($vars) {
        $tags = '';
580
581

        // Build Image
582
583
584
585
        if ($vars['image'] != '') {
            $tags .= Support::doAttribute('alttext', $vars['altText']);
            $tags .= Support::doAttribute('src', $vars['image']);
            $tags .= Support::doAttribute('title', $vars['title']);
586
587
        }

588
        return $tags == '' ? '' : '<img ' . $tags . '>';
589
    }
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606

    /**
     *  Encrypt the mailto address via JS.
     *  Email address protected against email crawler (as long as they don't interpret JS).
     *
     *    <script language=javascript><!--
     *    var mm1 = name
     *    var mm2 = @domain.
     *    var ... = tld
     *    document.write("<a href=" + "mail" + "to:" + mm1 + mm2 + ... + ">" + name + "</a>")
     *    document.write("<a href=" + "mail" + "to:" + mm1 + mm2 + ... + ">" + @domain. + "</a>")
     *    document.write("<a href=" + "mail" + "to:" + mm1 + mm2 + ... + ">" + tld + "</a>")
     *    //--></script>';
     *
     * @param    string $mailto Email address to encrypt.
     * @param    string $text Text to wrap <a href..>, </a> around.
     * @param    string $class Optional class definition
607
     * @param    bool $href TRUE: create a '<a>',   false: just encrypt or show the email, no link.
608
609
610
     *
     * @return    string The complete Link
     */
611
612
    private
    function encryptMailtoJS($vars, $href = TRUE) {
613
614

        // Split $mailto
615
        $tmp = $this->splitAndAddDelimter($vars['mailto'], "@");
616
617
618
619
620
621
622
623
624
625
626
627
        $arr = array_merge($this->splitAndAddDelimter($tmp[0], "."), $this->splitAndAddDelimter($tmp[1], "."));

        $tt = "<script language=javascript><!--" . chr(10);
        $ii = 0;
        if ($href) {
            $dw = 'document.write("<a href=" + "mail" + "to:"';
            foreach ($arr as $value) {
                // Setup JS Variables
                $tt .= 'var mm' . $ii . '= "' . $value . '"' . chr(10);
                // Compose $dw (documentwrite statement)
                $dw .= ' + mm' . $ii++;
            }
628
            $dw .= ' + "' . $vars['class'] . str_replace('"', '\\"', $vars['toolTip'][0]) . '>"';
629
630
631
632
633
634
635
            $closeDw = '"</a>")';
        } else {
            $dw = 'document.write(';
            $closeDw = ')';
        }

        // Wrap mailto around text
636
        if ($vars['mailto'] == $vars['text']) {
637
638
639
640
641
642
643
            // Text is an email: wrap every single part of mailto.
            $ii = 0;
            foreach ($arr as $value) {
                $tt .= $dw . " + mm" . $ii++ . ' + ' . $closeDw . chr(10);
            }
        } else {
            // Single wrap text
644
            $tt .= $dw . ' + "' . $vars['text'] . '" + ' . $closeDw . chr(10);
645
646
647
        }

        $tt .= '//--></script>';
648
        if ($href) $tt .= $vars['toolTip'][1];
649
650

        return ($tt);
651
    }
652
653
654
655
656
657
658
659
660
661

    /**
     * Split a string around the $delimiter.
     *
     * Append the delimiter to each part except the last one.
     *
     * @param $mailto
     * @param $delimiter
     * @return array
     */
662
663
    private
    function splitAndAddDelimter($mailto, $delimiter) {
664
665
666
667
668
669
670
671
672
673
674
675
        $value = '';

        $arr = explode($delimiter, $mailto);            // split string

        foreach ($arr as $key => $value) {
            $arr[$key] = $value . $delimiter;
        }

        if (isset($key))
            $arr[$key] = $value;

        return ($arr);
676
    }
677
}