summaryrefslogtreecommitdiff
path: root/email.php
blob: 9f7404a453650b270209dee64dc37b503edbf0c0 (plain)
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
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
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
250
251
252
253
254
255
256
257
258
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
303
304
305
306
307
308
309
310
311
312
313
314
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
<?php
    set_include_path( get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/lib');
    session_start();

    require_once 'Zend/Filter.php';
    require_once 'Zend/Filter/StripTags.php';
    require_once 'Zend/Filter/StringTrim.php';

    $filters = new Zend_Filter();
    $filters->addFilter(new Zend_Filter_StripTags())
            ->addFilter(new Zend_Filter_StringTrim());

    $pageTitle  = array_key_exists('pageTitle', $_GET) ? $filters->filter($_GET['pageTitle']) : '';
    $pageUrl    = array_key_exists('pageUrl', $_GET) ? $filters->filter($_GET['pageUrl']) : '';
    $page       = 'form';
    $errors     = array();

    if('post' == strtolower($_SERVER['REQUEST_METHOD']))
    {
        // someone sent over an invalid
        if(!Readability::hasValidParams())
        {
            Readability::logMessage("ERROR:Someone tried to send a request with an invalid set of parameters.");
            die();
        }

        require_once 'Zend/Validate/EmailAddress.php';

        //FILTER DATA

        $from       = $filters->filter($_POST['from']);
        $fromName   = $filters->filter($_POST['name']);
        $fromName   = !empty($fromName) ? $fromName : $from;
        $to         = $filters->filter($_POST['to']);
        $to         = array_map('trim', split(',', $to));
        $note       = $filters->filter($_POST['note']);
        $key        = $filters->filter($_POST['key']);
        $pageUrl    = $filters->filter($_POST['pageUrl']);
        $pageTitle  = $filters->filter($_POST['pageTitle']);

        if(!Readability::validateSecureKey($key))
        {
            $errors[] = 'key';
            Readability::logMessage("ERROR:Someone tried to send an email with an invalid key.");
        }

        // VALIDATE DATA

        $emailValidator = new Zend_Validate_EmailAddress();

        if(!$emailValidator->isValid($_POST['from']))
        {
            $errors[] = 'from';
        }

        if(count($to) == 0)
        {
            $errors[] = 'to';
        }
        else
        {
            foreach($to as $toAddress)
            {
                if(!$emailValidator->isValid($toAddress))
                {
                    $errors[] = 'to';
                    break;
                }
            }
        }

        // NO ERRORS SEND EMAIL
        if(count($errors) == 0)
        {
            // store the from address so it's saved for future use
            setcookie("from", $from, time()+3600*24*7*4, "/");
            setcookie("name", $fromName, time()+3600*24*7*4, "/");

            require_once 'Zend/Mail.php';
            require_once 'Zend/Mail/Transport/Smtp.php';

            $mailer = new Zend_Mail_Transport_Smtp('smtp.googlemail.com', Array(
                'auth'      => 'login',
                'username'  => 'readability@arc90.com',
                'password'  => '********',
                'ssl'       => 'ssl',
                'port'      => 465,
            ));
            $mailer->EOL = "\r\n";    // gmail is fussy about this
            Zend_Mail::setDefaultTransport($mailer);

            $body = '<html><head>';
            $body = '</head>';
            $body = '<body>';
            $body .= '<div style="font-size: 15px;">';
            $body .= '<p>This page was sent to you by: '.$from.'</p>';
            if(!empty($note))
            {
                $body .= '<p>Message from sender: </p><p><blockquote>'.stripslashes($note).'</blockquote></p>';
            }
            $body .= '<p>Just click this link: <a href="'.$pageUrl.'">'.$pageTitle.'</a></p>';
            $body .= '<hr />';
            $body .= '<p style="font-size: 90%;">Sent from <a href="http://lab.arc90.com/experiments/readability/">Readability</a> | An <a href="http://www.arc90.com">Arc90</a> lab experiment<p>';
            $body .= '</div>';
            $body .= '</body></html>';

            $mail = new Zend_Mail();
            $mail->setBodyHtml($body);
            $mail->setFrom($from, $fromName);
            $mail->addHeader('Reply-To', $from);

            foreach($to as $toAddress)
            {
                $mail->addTo($toAddress);
            }

            $mail->setSubject("Sent via Readability: {$pageTitle}");

            try
            {
                if(!$mail->send())
                {
                    Readability::logMessage("ERROR:There was an error sending the email. [to:".implode(', ', $to).", from:{$from}, notes:{$note}, pageUrl: {$pageUrl}, pageTitle: {$pageTitle}]");
                }
                else
                {
                    $page = 'complete';
                }
            }
            catch(Exception $e)
            {
                Readability::logMessage("ERROR:There was an exception sending the email. [to:".implode(', ', $to).", from:{$from}, notes:{$note}, pageUrl: {$pageUrl}, pageTitle: {$pageTitle}]");
                Readability::logMessage("ERROR:".$e->getMessage());
            }

            //header('location: close.html');
        }
    } // end of: if method == POST

    elseif('get' == strtolower($_SERVER['REQUEST_METHOD']))
    {
        $_SESSION['secureKey'] = Readability::generateSecureKey();
    }

    class Readability
    {
        public static function isError($field, $errors)
        {
            if(in_array($field, $errors))
            {
                return TRUE;
            }
            return FALSE;
        }

        public static function getErrorClass($field, $errors)
        {
            if(in_array($field, $errors))
            {
                return 'class = "error"';
            }
            return '';
        }

        public static function getParam($param)
        {
            if(isset($_POST) && array_key_exists($param, $_POST))
            {
                return $_POST[$param];
            }
            elseif(isset($_COOKIE) && array_key_exists($param, $_COOKIE))
            {
                return $_COOKIE[$param];
            }
            return '';
        }

        public static function logMessage($message)
        {
            $logFile = dirname(__FILE__) . '/log.txt';

            $handle = @fopen($logFile, 'a');
            if(is_resource($handle))
            {
                $message = date('Y-m-d G:i:s') . ' :: ' . $message . "\n";
                fwrite($handle, $message);
                fclose($handle);
            }
        }

        public static function generateSecureKey($length = 8)
        {
            $sucureKey = "";
            $possible = "012*3456)789b(cdfg#hjkmn@pqrs!tvwx[yz";

            for($x=0; $x < $length; $x++)
            {
                $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);

                if (!strstr($sucureKey, $char))
                {
                    $sucureKey .= $char;
                }
            }
            return $sucureKey;
        }

        /**
         * this adds a small (very small) level of security
         *
         * @param string $testKey 
         * @return void
         * @author David Hauenstein
         */
        public static function validateSecureKey($testKey)
        {
            if(!array_key_exists('secureKey', $_SESSION))
            {
                $_SESSION['secureKey'] = self::generateSecureKey();
                return false;
            }
            else
            {
                if($testKey != $_SESSION['secureKey'])
                {
                    return false;
                }
            }
            return true;
        }

        public static function emailAsLinks($addresses)
        {
            $toReturn = '';
            foreach($addresses as $address)
            {
                $toReturn .= '<a href="mailto:'.$address.'">' . $address . '</a>, ';
            }
            return substr($toReturn, 0, strlen($toReturn)-2);
        }

        public static function hasValidParams()
        {
            $requiredParams = array('from', 'name', 'to', 'note', 'key', 'pageTitle', 'pageUrl');
            $sentParams = array_keys($_POST);
            foreach($requiredParams as $required)
            {
                if(!in_array($required, $sentParams))
                {
                    return false;
                }
            }
            return true;
        }
    }
?>
<?= '<?' ?>xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Readability</title>
        <script type="text/javascript" charset="utf-8">
            window.onload = function(){
                document.getElementById('cancel-email').onclick = function(){
                    window.location = 'http://davehauenstein.com/readability/close.html';
                    return false;
                };
                document.getElementById('send-email').onclick = function(){
                    document.getElementById('send-email-form').submit();
                    return false;
                };
            };
            <?php if($page == "complete"){ ?>
            timer = setTimeout(function(){
                window.location = 'close.html';
            }, 3000);
            <?php } ?>
        </script>
        <style type="text/css" media="screen">
            *{
                margin: 0;
            }
            #email-container{
                font-size: 14px;
                margin: 0;
                padding: 0;
                width: 500px;
                height: 490px;
                font-family: times, serif;
                background-color: #fff;
            }
            h2{
                margin: 0 0 10px;
                background: url(http://lab.arc90.com/experiments/readability/images/email-head.gif) #e2e3e4 no-repeat 15px center;
                text-indent: -99999px;
                height: 40px;
            }
            form{
                padding-right: 20px;
            }
            label{
                font-size: 20px;
                padding-right: 10px;
                display: block;
                float: left;
                width: 130px;
                text-align: right;
            }
            input,
            textarea{
                padding: 5px;
                width: 320px;
                font-family: times, serif;
                font-size: 14px;
                border: solid 1px #999;
            }
            input.error{
                border: solid 2px #c33;
            }
            p.error{
                color: #c33;
                font-size: 14px;
            }
            .helper{
                font-size: 12px;
                margin-top: 3px;
                color: #666;
            }
            .details{
                font-style: italic;
                font-size: 15px;
            }
            .helper,
            .details{
                margin-left: 120px; /* add label width + label padding-right */
            }
            .section{
                margin-top: 15px;
                clear: both;
            }
            #note{
                <?php if(count($errors) > 0){ ?>
                height: 100px;
                <?php } else { ?>
                height: 140px;
                <?php } ?>
            }
            #send-email,
            #cancel-email{
                padding: 2px 2px;
                font-family: times, serif;
                background-color: #e7e8e9;
                font-size: 17px;
                border: solid 2px #666;
                cursor: pointer;
            }
            #send-email{
                margin-left: 180px;
                font-weight: bold;
            }
            #cancel-email{
                margin-left: 10px;
            }
            .logo{
                position: absolute;
                left: 10px;
                bottom: 10px;
            }
            #complete{
                margin-top: 120px;
                text-align: center;
                padding: 20px;
            }
            #complete p{
                margin: 0 0 10px;
                font-size: 16px;
            }
            #complete img{
                margin-top: 100px;
            }
        </style>
    </head>
    <body>
        <div id="email-container">
            <h2>Email Page</h2>

            <?php if($page == 'form'){ ?>
            <form action="./email.php" method="post" accept-charset="utf-8" id="send-email-form">
                <div class="section">
                    <label for="name">Your Name :</label>
                    <input type="text" name="name" id="name" value="<?php echo Readability::getParam('name') ?>" />
                </div>
                <div class="section">
                    <label for="from">Your Email :</label>
                    <input type="text" name="from" id="from" value="<?php echo Readability::getParam('from') ?>" <?php echo Readability::getErrorClass('from', $errors); ?> />
                    <?php if(Readability::isError('from', $errors)){ ?>
                    <p class="helper error">
                        This field should be a valid email address.
                    </p>
                    <?php } ?>
                </div>
                <div class="section">
                    <label for="to">Recipients :</label>
                    <input type="text" name="to" id="to" value="<?php echo Readability::getParam('to') ?>" <?php echo Readability::getErrorClass('to', $errors); ?> />
                    <?php if(Readability::isError('to', $errors)){ ?>
                    <p class="helper error">
                        Please ensure that all addresses are valid email adderesses.
                    </p>
                    <?php } ?>
                    <p class="helper">
                        Seperate multiple <em>email addresses</em> with commas.
                    </p>
                </div>
                <div class="section">
                    <label>Sending :</label>
                    <p class="details">
                        <?= $pageTitle ?>
                    </p>
                </div>
                <div class="section">
                    <label for="note">Note :</label>
                    <textarea name="note" id="note" rows="8" cols="40"><?php echo Readability::getParam('note') ?></textarea>
                </div>
                <div class="section">
                    <button id="send-email">Email Page</button>
                    <button id="cancel-email">Cancel</button>
                </div>
                <img src="http://lab.arc90.com/experiments/readability/images/email-readability.gif" alt="Readability" class="logo" />
                <input type="hidden" name="pageUrl" value="<?= $pageUrl; ?>" id="pageUrl" />
                <input type="hidden" name="pageTitle" value="<?= $pageTitle; ?>" id="pageTitle" />
                <input type="hidden" name="key" value="<?= $_SESSION['secureKey']; ?>" id="key" />
            </form>
            <?php }else if($page == "complete"){ ?>
            <div id="complete">
                <p>
                    A link to this page has been sent to <?php echo Readability::emailAsLinks($to) ?>
                </p>
                <p>
                    Thanks for using Readability.
                </p>
                <div>
                    <img src="http://davehauenstein.com/readabilityTest/images/footer-thanks.png" alt="Readability" />
                </div>
            </div>
            <?php } ?>
        </div>
    </body>
</html>