Link.php 24.9 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
60
61
62
63
64
65
66
67
68
69
70

/*
 * 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  
 * f:formname
 * i:recordId
 * T:tablename
 * 
 * A:    A:[u:p:m]
 * G:    G:[N|..]
 */

class Link {

    private $mailto = "", $url = "", $image = "", $text = "", $altText = "", $imageTitle = "", $question = "", $target = "", $formName = "", $additionalUrlParams = "";
71
    private $toolTip = array(), $modeRender = 0, $picturePosition = "l", $modeHash = false, $recordId = -1, $encryption = 1;
72
73
74
75
    private $linkClass = "", $defaultLinkClass = "";
    private $linkClassSelector = array("i" => "internal", "e" => "external");
    private $renderControl, $link = "", $tableName = "";

Carsten  Rose's avatar
Carsten Rose committed
76
    /**
77
     * @var Sip
Carsten  Rose's avatar
Carsten Rose committed
78
79
80
     */
    private $sip = null;

81
82
83
84
85
    /**
     * @var Store
     */
    private $store = null;

86
87
88
89
90
91
92
93
94
95
96
97
98
    // Simulate global variable: will be set much earlier in other functions. Will be shown in error messages.
    private $fr_error;

    private $exceptionBody = "";
    /**
     * @var Utils
     */
    private $utils;
    /**
     * @var string
     */
    private $pageAlias = '';

99
100
101
    private $cssLinkClassInternal = '';
    private $cssLinkClassExternal = '';

102
103
104
105
106
107
108
109
110
111
112
    /**
     * __construct
     *
     * @param string $fullLevel Recent processed level, f.e.: '10.20.sql'
     * @param string $sql Recent processed query, f.e.: SELECT p.name FROM person AS p
     * @param string $columnValue Definition of link
     * @param int $count
     * @param int $columnIndex
     * @param string $fr_error
     * @param string $dbAlias
     */
Carsten  Rose's avatar
Carsten Rose committed
113
    public function __construct($fullLevel, $sql, $columnValue, $count, $columnIndex, $fr_error, $dbAlias = '', $sip) {
114
115
116
        $this->exceptionBody = "Level: " . $fullLevel . ".sql = " . $sql . "\nRow: " . $count . " , Column: " . $columnIndex . " , Bad link: " . $columnValue . "\n";
        $this->fr_error = $fr_error;
        $this->dbAlias = $dbAlias;
Carsten  Rose's avatar
Carsten Rose committed
117
        $this->sip = $sip;
118
        $this->store = Store::getInstance();
Carsten  Rose's avatar
Carsten Rose committed
119

120
121
        $this->cssLinkClassInternal = $this->store->getVar(SYSTEM_CSS_LINK_CLASS_INTERNAL, STORE_SYSTEM);
        $this->cssLinkClassExternal = $this->store->getVar(SYSTEM_CSS_LINK_CLASS_EXTERNAL, STORE_SYSTEM);
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
        $this->initRenderControl();

    } // __construct

    /**
     * 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
144
         *  r=render mode, u=url, t:text and/or image.
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
         *
         *                  [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;
    } // initRenderControl()

    /**
     * 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"
     * @param    string $count
     * @param    string &$hash hash used by the link, and maybe created for the link if count=1
     *
     * @return    string      The complete Link
     */
    public function renderLink($str, $count, &$hash) {

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

        // Parse all parameter, fill variables
        foreach ($parm as $item) {
            $arr = explode(":", $item, 2);
            $this->parseItem($arr[0], $arr[1]);
        }

        // if there is no url or mailto definition: ~global.pageId
        if (!$this->url && !$this->mailto) $this->url = "?" . $GLOBALS["TSFE"]->id;

        $this->doCssClass();

        $htmlUrl = $this->doUrl($count, $hash);
        $htmlImage = $this->doImage();

        // One space distance
        if ($this->text) $distance = " ";

        // Compose Image & Text
        $distance = ' ';
        $this->text = ($this->picturePosition == "l") ? $htmlImage . $distance . $this->text : $this->text . $distance . $htmlImage;

        // ToolTip
        $extraSpan = array();
        if ($this->toolTip) {
            $extraSpan[0] = "<span " . $this->toolTip[0] . ">" . $this->toolTip[1];
            $extraSpan[1] = "</span>";
        }

        // Create 'fake' modes for encrypted 'mailto'
        $prefix = "";
        if ($this->mailto) {
            $prefix = "1";
            $this->url = "dummy";
        }
        // Create 'fake' mode for ajax delete
        if ($this->delete) {
            $prefix = "2";
        }
        // Compose URL
        // get Render Mode via Array renderControl
        $mode = $prefix . $this->renderControl[$this->modeRender][$this->url ? 1 : 0][$this->text ? 1 : 0];
        // 0-4 URL, plain email
        // 10-14 encrypted email
        switch ($mode) {
            // 0: No Output
            case '0':
            case '10':
            case '20':
                $this->link = "";
                break;

            // 1: 'text'
            case '1':
                $this->link = $extraSpan[0] . $this->text . $extraSpan[1];
                break;
            case '11':
250
                $this->link = $extraSpan[0] . $this->encryptMailtoJS($this->mailto, $this->text, $this->linkClass, false) . $extraSpan[1];
251
252
253
254
255
256
257
                break;

            // 2: 'url'
            case '2':
                $this->link = $extraSpan[0] . $this->url . $extraSpan[1];
                break;
            case '12':
258
                $this->link = $extraSpan[0] . $this->encryptMailtoJS($this->mailto, $this->text, $this->linkClass, false) . $extraSpan[1];
259
260
261
262
263
264
265
266
267
268
269
270
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
299
300
301
302
                break;

            // 3: <a href=url>url</a>
            case '3':
                $this->link = $htmlUrl . $this->url . '</a>' . $this->toolTip[1];
                break;
            case '13':
                $this->link = $this->encryptMailtoJS($this->mailto, $this->mailto, $this->linkClass, TRUE);
                break;

            // 4: <a href=url>Text</a>
            case '4':
                $this->link = $htmlUrl . $this->text . '</a>' . $this->toolTip[1];
                break;
            case '14':
                $this->link = $this->encryptMailtoJS($this->mailto, $this->text, $this->linkClass, TRUE);
                break;
            case '21':
            case '22':
            case '23':
            case '24':
                $this->link = "<a href=\"javascript: void(0);\" onClick=\"var del = new FR.Delete({recordId:'" . $count . "',hash:'" . $hash . "',forward:'" . $this->pageAlias . "'});\" " . $this->linkClass . ">" . $this->text . "</a>";
        }

        return $this->link;

    } // buildHash()

    /**
     * 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
     */
    private function parseItem($key, $value) {

        $_EXTKEY = FORMREPORT;

        switch ($key) {
            // URL
            case "u":
                if ($this->url || $this->mailto) throw new SyntaxReportException ("Multiple URL, PAGE or MAILTO defined : '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->url = $value;
303
                $this->defaultLinkClass = $this->cssLinkClassExternal;
304
305
306
307
                break;
            case "m":
                if ($this->url || $this->mailto) throw new SyntaxReportException ("Multiple URL, PAGE or MAILTO defined : '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->mailto = $value;
308
                $this->defaultLinkClass = $this->cssLinkClassExternal;
309
310
311
312
313
                break;
            case "p":
                if ($this->url || $this->mailto) throw new SyntaxReportException ("Multiple URL, PAGE or MAILTO defined : '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->pageAlias = $value ? $value : $GLOBALS["TSFE"]->id; // If no pageid|pagealias ist defined, take current page
                $this->url = "?" . $this->pageAlias;
314
                $this->defaultLinkClass = $this->cssLinkClassInternal;
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
                break;
            // Text
            case "t":
                $this->text = $value;
                break;
            case "a":
                $this->altText = $value;
                break;
            case "o":
                $this->toolTip = $this->utils->createToolTip($value);
                break;

            // Image
            case "P":
                if ($this->image) throw new SyntaxReportException ("Multiple images defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->image = $value;
                $this->AltText = "Grafic: " . $value;
                $this->imageTitle = $value;
                break;
            case "B":
                if ($this->image) throw new SyntaxReportException ("Multiple images defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->image = PATH_ICONS . "bullet-" . ($value ? $value : "green") . '.gif';
                $this->defaultAltText = "Bullet: " . $value;
                $this->imageTitle = $value;
                break;
            case "C":
                if ($this->image) throw new SyntaxReportException ("Multiple images defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->image = PATH_ICONS . "checked-" . ($value ? $value : "green") . '.gif';
                $this->defaultAltText = "Checked: " . $value;
                $this->imageTitle = $value;
                break;
            case "D":
                if ($this->image) throw new SyntaxReportException ("Multiple images defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->image = PATH_ICONS . 'delete.gif';
                $this->defaultAltText = "Delete";
                $this->imageTitle = "Delete";
                $this->delete = true;
                // Include Extjs library
                $this->utils->loadJSlib($this->fr_error);
                break;
            case "E":
                if ($this->image) throw new SyntaxReportException ("Multiple images defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->image = PATH_ICONS . 'edit.gif';
                $this->defaultAltText = "Edit";
                $this->imageTitle = "Edit";
                break;
            case "H":
                if ($this->image) throw new SyntaxReportException ("Multiple images defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->image = PATH_ICONS . 'help.gif';
                $this->defaultAltText = "Help";
                $this->imageTitle = "Help";
                break;
            case "I":
                if ($this->image) throw new SyntaxReportException ("Multiple images defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->image = PATH_ICONS . 'info.gif';
                $this->defaultAltText = "Information";
                $this->imageTitle = "Information";
                break;
            case "N":
                if ($this->image) throw new SyntaxReportException ("Multiple images defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->image = PATH_ICONS . 'new.gif';
                $this->defaultAltText = "New";
                $this->imageTitle = "New";
                break;
            case "S":
                if ($this->image) throw new SyntaxReportException ("Multiple images defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->image = PATH_ICONS . 'show.gif';
                $this->defaultAltText = "Details";
                $this->imageTitle = "Details";
                break;

            // Misc
            case "r":
                if ($this->modeRender) throw new SyntaxReportException ("Multiple render modes defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->modeRender = $value;
                break;
            case "g":
                if ($this->target) throw new SyntaxReportException ("Multiple 'target' defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->target = $value;
                break;
            case "c":
                if ($this->linkClass) throw new SyntaxReportException ("Multiple linkClass defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->linkClass = $value;
                break;
            case "q":
                if ($this->question) throw new SyntaxReportException ("Multiple question defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->question = $value ? $value : "Please confirm";
                break;
            case "e":
                if ($this->encryption != 1) throw new SyntaxReportException ("Multiple encryption defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->encryption = $value ? $value : "1";
                break;
            case "h":
                if ($this->modeHash) throw new SyntaxReportException ("Multiple modeHash defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->modeHash = TRUE;
                break;
            case "f":
                if ($this->formName) throw new SyntaxReportException ("Multiple formName defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->formName = $value;
                break;
            case "U":    //if($this->additionalUrlParams) throw new syntaxException ( "Multiple additionalUrlParams defined: '$key'","",__FILE__,__LINE__, "Column: " . $fthis->r_error["columnIndex"], $this->fr_error);
                //$this->additionalUrlParams = $value;

                // Save url params as array!
                $param = explode('=', $value);
                $this->additionalUrlParams[$param[0]] = $param[1];
                break;
            case "i":
                if ($this->recordId != -1) throw new SyntaxReportException ("Multiple recordId defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->recordId = $value;
                break;
            case "R":
                if ($this->picturePosition != "l") throw new SyntaxReportException ("Multiple picturePosition defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->picturePosition = "r";
                break;
            case "T":
                if ($this->tableName) throw new SyntaxReportException ("Multiple tableName defined: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);
                $this->tableName = $value;
                break;

            // A,G
            case "G":
            case "A":
                $arr = explode(":", $value, 2);
                $this->parseItem($arr[0], $arr[1]);
                break;
            case "" :
                break;
            default:
                throw new SyntaxReportException ("Unknown link qualifier: '$key'", "", __FILE__, __LINE__, "Column: " . $this->fr_error["columnIndex"], $this->fr_error);

        }

    } // splitAndAddDelimter()

    /**
     * Parse CSS Class Settings
     *
     * @param    void
     *
     * @return    void
     */
    private function doCssClass() {

        // class: i|e|n|<custom name>
        if ($this->linkClass != 'n') {
            if (!$this->linkClass) $this->linkClass = $this->defaultLinkClass;

            if ($this->linkClassSelector[$this->linkClass])
                $this->linkClass = $this->linkClassSelector[$this->linkClass];

            $this->linkClass = ' class="' . $this->linkClass . '"';
        } else {
            $this->linkClass = "";
        }
    } // encryptMailtoJS()

    /**
     * Build URL
     *
     * @param int $count Row count of current query. $count=1 means 'generate new hash values', everything else means 'take existing hash values'.
     * @param string $hash RC for hash if count=1, else 'hash' to use.
     * @return string
     */
    private function doUrl($count, &$hash) {
        // build URL
        $htmlUrl = "";

        // If there is no encryption: handle the mailto as an ordinary URL
        if ($this->mailto && $this->encryption == 0) {
            $this->url = "mailto:" . $this->mailto;
            $this->mailto = "";
        }

        if ($this->url) {
            $param = array();

            if ($this->modeHash) {
                $param[] = $this->buildHash($count, $hash);
            } else {
                if ($this->formName) $param[] = URL_FORMNAME . '=' . $this->formName;
                if ($this->recordId != -1) $param[] = URL_RECORD_ID . '=' . $this->recordId;
                if ($this->additionalUrlParam) $param[] = $this->additionalUrlParams;
            }

            if ($param) {
                // Append '&' or '?' depending if there is already a '?'
                $this->url .= (strpos($this->url, '?') >= 0) ? '&' : '?';
                // Append all additional params.
                $this->url .= implode('&', $param);
            }

            // target
            if ($this->target) $this->target = ' target="' . $this->target . '"';

            // question
            if ($this->question) $this->question = ' onclick="return confirm(\'' . $this->question . '\')"';

            $htmlUrl = '<a href="' . $this->url . '"' . $this->linkClass . $this->target . $this->question . $this->toolTip[0] . '>';
        }
        return ($htmlUrl);
    } // doCssClass()

    /**
     * Build the URL and/or Hash
     *
521
522
523
     * @param $count
     * @param $hash
     * @return string The complete Link
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
     */
    private function buildHash($count, &$hash) {

        if ($count == 1) {
            $hash = $this->utils->randomAlphaNumUnique();

            $_SESSION[FORMREPORT][$hash]['formName'][0] = $this->formName;
            $_SESSION[FORMREPORT][$hash]['referrer'] = $_SERVER['REQUEST_URI'];
            // Delete Links: tablename mandatory
            if ($this->tableName) {
                $_SESSION[FORMREPORT][$hash]['tableName'] = $this->tableName;
                $_SESSION[FORMREPORT][$hash]['dbAlias'] = $this->dbAlias;
            }
        }

        $_SESSION[FORMREPORT][$hash]['idMap'][$count]['recordId'] = $this->recordId;
        $_SESSION[FORMREPORT][$hash]['idMap'][$count]['param'] = $this->additionalUrlParams;

        return (URL_HASH . '=' . $hash . '&' . URL_RECORD_ID . '=' . $count);
    } // doUrl()

    /**
     * Create Image HTML Tag
     * @return string $htmlImage
     * @internal param $void
     *
     */
    private function doImage() {
        $htmlImage = "";

        // Build Image
        if ($this->image) {

            if ($this->altText) $this->altText = ' alttext="' . $this->altText . '"';

            if ($this->image) $this->image = ' src="' . $this->image . '"';

            if ($this->imageTitle) $this->imageTitle = ' title="' . $this->imageTitle . '"';

            $htmlImage = '<img' . $this->image . $this->altText . $this->imageTitle . ' />';
        }

        return ($htmlImage);
    } // doImage()

    /**
     *  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
585
     * @param    bool $href TRUE: create a '<a>',   false: just encrypt or show the email, no link.
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
     *
     * @return    string The complete Link
     */
    private function encryptMailtoJS($mailto, $text, $class = "", $href = TRUE) {

        // Prepare CSS Definition
        if ($class) $class = str_replace('"', "'", $class);

        // Split $mailto
        $tmp = $this->splitAndAddDelimter($mailto, "@");
        $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++;
            }
            $dw .= ' + "' . $class . str_replace('"', '\\"', $this->toolTip[0]) . '>"';
            $closeDw = '"</a>")';
        } else {
            $dw = 'document.write(';
            $closeDw = ')';
        }

        // Wrap mailto around text
        if ($mailto == $text) {
            // 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
            $tt .= $dw . ' + "' . $text . '" + ' . $closeDw . chr(10);
        }

        $tt .= '//--></script>';
        if ($href) $tt .= $this->toolTip[1];

        return ($tt);
    } //renderLink()

    /**
     * Split a string around the $delimiter.
     *
     * Append the delimiter to each part except the last one.
     *
     * @param $mailto
     * @param $delimiter
     * @return array
     */
    private function splitAndAddDelimter($mailto, $delimiter) {
        $value = '';

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

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

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

        return ($arr);
    } // parseItem
}