Passed
Pull Request — master (#9)
by Michael
03:24
created

contact.php (1 issue)

Labels
Severity
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * Tell a friend form generator / send email
14
 *
15
 * @copyright::  {@link https://xoops.org/ XOOPS Project}
16
 * @license  ::    {@link https://www.gnu.org/licenses/gpl-2.0.html GNU Public License}
17
 * @package  ::    mylinks
18
 */
19
20
use XoopsModules\Mylinks;
21
22
require_once __DIR__ . '/header.php';
23
require_once $GLOBALS['xoops']->path('header.php');
24
require_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
25
26
xoops_loadLanguage('main', $GLOBALS['xoopsModule']->getVar('dirname'));
27
$myts = \MyTextSanitizer::getInstance();
28
29
if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) && !$GLOBALS['xoopsModuleConfig']['anontellafriend']) {
30
    redirect_header('index.php', 3, _NOPERM);
31
}
32
33
if (!isset($_POST['submit'])) {
34
    if (\Xmf\Request::hasVar('contents', 'GET')) {
35
        // coming back from failed attempt at filling out form
36
        $form_contents = unserialize($myts->stripSlashesGPC($_GET['contents']));
37
        $lid           = !empty($form_contents['lid']) ? (int)$form_contents['lid'] : 0;
38
        $comments      = !empty($form_contents['comments']) ? strip_tags(html_entity_decode($form_contents['comments'])) : '';
39
        $frname        = !empty($form_contents['frname']) ? $myts->htmlSpecialChars($form_contents['frname']) : '';
40
        $fremail       = !empty($form_contents['fremail']) ? $myts->htmlSpecialChars($form_contents['fremail']) : '';
41
        $sname         = !empty($form_contents['sname']) ? $myts->htmlSpecialChars($form_contents['sname']) : '';
42
        $semail        = !empty($form_contents['semail']) ? $myts->htmlSpecialChars($form_contents['semail']) : '';
43
    } else {
44
        $lid      = Mylinks\Utility::cleanVars($_GET, 'lid', 0, 'int', ['min' => 0]);
0 ignored issues
show
'lid' of type string is incompatible with the type XoopsModules\Mylinks\unknown_type expected by parameter $key of XoopsModules\Mylinks\Utility::cleanVars(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        $lid      = Mylinks\Utility::cleanVars($_GET, /** @scrutinizer ignore-type */ 'lid', 0, 'int', ['min' => 0]);
Loading history...
45
        $comments = $frname = $fremail = $sname = $semail = '';
46
    }
47
48
    // make sure that the link the user is sharing is valid & active
49
    $result = $GLOBALS['xoopsDB']->query("SELECT title FROM {$GLOBALS['xoopsDB']->prefix('mylinks_links')} WHERE `lid` = '{$lid}' AND status>0 LIMIT 0,1");
50
    if ($result) {
51
        list($linktitle) = $GLOBALS['xoopsDB']->fetchRow($result);
52
        $linktitle = $myts->stripSlashesGPC($linktitle);
53
    } else {
54
        // invalid (or inactive) link, can't send this to a friend
55
        redirect_header('index.php', 3, _MD_MYLINKS_INVALIDORINACTIVELNK);
56
    }
57
58
    $tfform = new \XoopsThemeForm(_MD_MYLINKS_TELLAFRIEND, 'tfform', $_SERVER['SCRIPT_NAME'], 'post', true);
59
    $tfform->addElement(new \XoopsFormText(_MD_MYLINKS_FRIEND . ' ' . _MD_MYLINKS_NAME, 'frname', 50, 50, trim($frname)), true);
60
    $tfform->addElement(new \XoopsFormText(_MD_MYLINKS_FRIEND . ' ' . _MD_MYLINKS_EMAIL, 'fremail', 50, 50, trim($fremail)), true);
61
    if (!$GLOBALS['xoopsUser'] instanceof \XoopsUser) {
62
        $tfform->addElement(new \XoopsFormText(_MD_MYLINKS_SENDER . ' ' . _MD_MYLINKS_NAME, 'sname', 50, 50, trim($sname)), true);
63
        $tfform->addElement(new \XoopsFormText(_MD_MYLINKS_SENDER . ' ' . _MD_MYLINKS_EMAIL, 'semail', 50, 50, trim($semail)), true);
64
    }
65
    $tfform->addElement(new \XoopsFormLabel(_MD_MYLINKS_TITLE, $linktitle));
66
    $tfform->addElement(new \XoopsFormTextArea(_COMMENTS, 'comments', trim($comments)));
67
    $tfform->addElement(new \XoopsFormHidden('lid', $lid));
68
    $tfform->addElement(new \XoopsFormCaptcha());
69
    //    $tfform->addElement(new \XoopsFormCaptcha(null, null, false, array('maxattempts'=>4)));
70
    $tfform->addElement(new \XoopsFormButtonTray('submit', _SUBMIT));
71
    $tfform->display();
72
    require_once $GLOBALS['xoops']->path('footer.php');
73
} else {
74
    if ($GLOBALS['xoopsSecurity'] instanceof XoopsSecurity) {
75
        if (!$GLOBALS['xoopsSecurity']->check()) {
76
            // failed xoops security check
77
            redirect_header('index.php', 3, $GLOBALS['xoopsSecurity']->getErrors(true));
78
        }
79
    } else {
80
        redirect_header('index.php', 3, _MD_MYLINKS_INVALID_SECURITY_TOKEN);
81
    }
82
83
    $lid      = Mylinks\Utility::cleanVars($_POST, 'lid', 0, 'int', ['min' => 0]);
84
    $comments = strip_tags(html_entity_decode($myts->stripSlashesGPC($_POST['comments'])));
85
    $frname   = Mylinks\Utility::cleanVars($_POST, 'frname', '', 'string');
86
    $fremail  = Mylinks\Utility::cleanVars($_POST, 'fremail', '', 'email');
87
    $sname    = Mylinks\Utility::cleanVars($_POST, 'sname', '', 'string');
88
    $semail   = Mylinks\Utility::cleanVars($_POST, 'semail', '', 'email');
89
90
    //Check captcha
91
    xoops_load('XoopsCaptcha');
92
    $xoopsCaptcha = XoopsCaptcha::getInstance();
93
    if (!$xoopsCaptcha->verify()) {
94
        if ($_SESSION['xoopscaptcha_attempt'] < $_SESSION['_maxattempts']) {
95
            $form_contents = [
96
                'lid'      => $lid,
97
                'frname'   => $frname,
98
                'fremail'  => $fremail,
99
                'sname'    => $sname,
100
                'semail'   => $semail,
101
                'comments' => $comments,
102
            ];
103
            $contents      = serialize($form_contents);
104
            redirect_header($_SERVER['SCRIPT_NAME'] . "?contents={$contents}", 2, $xoopsCaptcha->getMessage());
105
        } else {
106
            redirect_header('index.php', 2, $xoopsCaptcha->getMessage());
107
        }
108
    }
109
110
    $xadminmail = $GLOBALS['xoopsConfig']['adminmail'];    //setting from to server in case of SPF=>will admin config this as well
111
    $xsitename  = $GLOBALS['xoopsConfig']['sitename'];     //adding site title as sender (mod config this?)
112
113
    // set from name / email for registered user
114
    if ($GLOBALS['xoopsUser'] instanceof \XoopsUser) {
115
        $semail = $GLOBALS['xoopsUser']->getVar('email');
116
        $sname  = ucfirst($GLOBALS['xoopsUser']->getVar('uname'));
117
        $sname  = ('' == $sname) ? $GLOBALS['xoopsUser']->getVar('name') : $sname;
118
    }
119
    // check to see if email for recipient and sender are 'sane'
120
    if (!filter_var($fremail, FILTER_VALIDATE_EMAIL) || !filter_var($semail, FILTER_VALIDATE_EMAIL)) {
121
        redirect_header('index.php', 2, _MD_MYLINKS_INVALIDEMAIL);
122
    }
123
    // set the url to the link
124
    if ($lid > 0) {
125
        $linkurl = $GLOBALS['xoops']->url('modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . "/singlelink.php?lid={$lid}");
126
        // now check to make sure that the link the user is sharing is valid
127
        $result = $GLOBALS['xoopsDB']->query("SELECT title FROM {$GLOBALS['xoopsDB']->prefix('mylinks_links')} WHERE `lid` = '{$lid}' AND status>0 LIMIT 0,1");
128
        if ($result) {
129
            list($linktitle) = $GLOBALS['xoopsDB']->fetchRow($result);
130
            $linktitle = $myts->stripSlashesGPC($linktitle);
131
        } else {
132
            // invalid (or inactive) link, can't send this to a friend
133
            redirect_header('index.php', 3, _MD_MYLINKS_INVALIDORINACTIVELNK);
134
        }
135
    } else {
136
        redirect_header('index.php', 3, _MD_MYLINKS_INVALIDORINACTIVELNK);
137
    }
138
139
    $subject = sprintf(_MD_MYLINKS_EMAIL_SUBJECT, $xsitename, $sname);
140
141
    //now send mail to friend
142
    $xMailer = xoops_getMailer();
143
    $xMailer->useMail(); // Set it to use email (as opposed to PM)
144
    $xMailer->setTemplateDir($GLOBALS['xoops']->path('modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/mail_template/'));
145
    $xMailer->setTemplate('tellafriend_mail.tpl');
146
147
    // set common mail template variables
148
    $xMailer->assign(
149
        [
150
            'SNAME'        => $sname,
151
            'SEMAIL'       => $semail,
152
            'X_ADMINMAIL'  => $xadminmail,
153
            'X_SITENAME'   => $xsitename,
154
            'X_SITEURL'    => $GLOBALS['xoops']->url('/'),
155
            'X_LINK_TITLE' => strip_tags(html_entity_decode($linktitle)),
156
            'X_LINK'       => $linkurl,
157
            'FRNAME'       => $frname,
158
            'COMMENTS'     => strip_tags(html_entity_decode($comments)),
159
        ]
160
    );
161
162
    $xMailer->setToEmails($fremail);
163
    $xMailer->setFromEmail($xadminmail);
164
    $xMailer->setFromName($xsitename);
165
    $xMailer->setSubject($subject);
166
167
    if ($xMailer->send()) {
168
        //send was successful
169
        redirect_header('index.php', 2, _MD_MYLINKS_MESSEND);
170
    } else {
171
        redirect_header('index.php', 2, $xMailer->getErrors(true));
172
    }
173
}
174