Completed
Push — master ( a50592...10bab4 )
by Michael
01:56
created

trackback.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
//
3
// ------------------------------------------------------------------------ //
4
// This program is free software; you can redistribute it and/or modify     //
5
// it under the terms of the GNU General Public License as published by     //
6
// the Free Software Foundation; either version 2 of the License, or        //
7
// (at your option) any later version.                                      //
8
//                                                                          //
9
// You may not change or alter any portion of this comment or credits       //
10
// of supporting developers from this source code or any supporting         //
11
// source code which is considered copyrighted (c) material of the          //
12
// original comment or credit authors.                                      //
13
//                                                                          //
14
// This program is distributed in the hope that it will be useful,          //
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of           //
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
17
// GNU General Public License for more details.                             //
18
//                                                                          //
19
// You should have received a copy of the GNU General Public License        //
20
// along with this program; if not, write to the Free Software              //
21
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
22
// ------------------------------------------------------------------------ //
23
// Author: phppp (D.J., [email protected])                                  //
24
// URL: https://xoops.org                         //
25
// Project: Article Project                                                 //
26
// ------------------------------------------------------------------------ //
27
use Xmf\Request;
28
29
include __DIR__ . '/header.php';
30
31
// trackback is done by a POST
32
$art_id     = explode('/', Request::getString('REQUEST_URI', '', 'SERVER'));
33
$article_id = (int)$art_id[count($art_id) - 1];
34
$url        = Request::getString('url', '', 'POST');//$_POST['url'];
35
$title      = Request::getString('title', '', 'POST');//$_POST['title'];
36
$excerpt    = Request::getString('excerpt', '', 'POST');//$_POST['excerpt'];
37
$blog_name  = Request::getString('blog_name', '', 'POST');//$_POST['blog_name'];
38
$charset    = trim(Request::getString('charset', '', 'POST'));//trim($_POST['charset']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
88% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
40
if (empty($xoopsModuleConfig['trackback_option'])) {
41
    PlanetUtility::planetRespondToTrackback(1, 'Trackback is closed');
42
}
43
if (!strlen($title . $url . $blog_name)) {
44
    PlanetUtility::planetRespondToTrackback(1, planet_constant('MD_INVALID'));
45
}
46
47
if (!empty($article_id) && !empty($url)) {
48
    $trackbackHandler = xoops_getModuleHandler('trackback', $GLOBALS['moddirname']);
49
    $criteria         = new CriteriaCompo(new Criteria('art_id', $article_id));
50
    $criteria->add(new Criteria('tb_url', $url));
51
    if ($trackbackHandler->getCount($criteria) > 0) {
52
        PlanetUtility::planetRespondToTrackback(1, 'We already have a ping from that URI for this article.');
53
    }
54
55
    $charset   = empty($charset) ? 'utf-8' : $charset;
56
    $title     = XoopsLocal::convert_encoding($title, _CHARSET, $charset);
57
    $excerpt   = XoopsLocal::convert_encoding($excerpt, _CHARSET, $charset);
58
    $blog_name = XoopsLocal::convert_encoding($blog_name, _CHARSET, $charset);
59
    $tb_status = (int)$xoopsModuleConfig['trackback_option'];
60
61
    $com_pid    = 0;
62
    $com_itemid = $article_id;
63
    $com_rootid = 0;
64
    $com_title  = $title;
65
    $com_text   = $excerpt;
66
    $com_text   .= "\n\n[TRACKBACK]" . _POSTEDBY . ': ';
67
    if (!empty($url)) {
68
        $com_text .= '[url=' . $url . ']' . $blog_name . '[/url]';
69
    } else {
70
        $com_text .= $blog_name;
71
    }
72
    $com_modid = $xoopsModule->getVar('mid');
73
74
    $commentHandler = xoops_getHandler('comment');
75
    $comment        = $commentHandler->create();
76
    $comment->setVar('com_created', time());
77
    $comment->setVar('com_pid', $com_pid);
78
    $comment->setVar('com_itemid', $com_itemid);
79
    $comment->setVar('com_rootid', $com_rootid);
80
    $comment->setVar('com_ip', xoops_getenv('REMOTE_ADDR'));
81
    switch ($tb_status) {
82
        case 2:
83
            $comment->setVar('com_status', 2);
84
            $call_approvefunc = true;
85
            $call_updatefunc  = true;
86
            $notify_event     = 'comment';
87
            break;
88
        case 1:
89
        default:
90
            $comment->setVar('com_status', 1);
91
            $notify_event = 'comment_submit';
92
            break;
93
    }
94
    $comment->setVar('com_uid', 0);
95
    $com_title = xoops_trim($com_title);
96
    $com_title = empty($com_title) ? _NOTITLE : $com_title;
97
    $comment->setVar('com_title', $com_title);
98
    $comment->setVar('com_text', $com_text);
99
    $comment->setVar('dohtml', 0);
100
    $comment->setVar('dosmiley', 0);
101
    $comment->setVar('doxcode', 1);
102
    $comment->setVar('doimage', 0);
103
    $comment->setVar('dobr', 1);
104
    $comment->setVar('com_icon', '');
105
    $comment->setVar('com_modified', time());
106
    $comment->setVar('com_modid', $com_modid);
107
    if (false != $commentHandler->insert($comment)) {
108
        $newcid = $comment->getVar('com_id');
109
110
        // set own id as root id
111
        $com_rootid = $newcid;
112
        if (!$commentHandler->updateByField($comment, 'com_rootid', $com_rootid)) {
113
            $commentHandler->delete($comment);
114
            PlanetUtility::planetRespondToTrackback(1, xoops_error());
115
        }
116
117
        // call custom approve function if any
118
        if (false != $call_approvefunc && isset($comment_config['callback']['approve'])
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
119
            && '' != trim($comment_config['callback']['approve'])) {
120
            $skip = false;
121 View Code Duplication
            if (!function_exists($comment_config['callback']['approve'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
                if (isset($comment_config['callbackFile'])) {
123
                    $callbackfile = trim($comment_config['callbackFile']);
124
                    if ('' != $callbackfile
125
                        && file_exists(XOOPS_ROOT_PATH . '/modules/' . $moddir . '/' . $callbackfile)) {
126
                        require_once XOOPS_ROOT_PATH . '/modules/' . $moddir . '/' . $callbackfile;
127
                    }
128
                    if (!function_exists($comment_config['callback']['approve'])) {
129
                        $skip = true;
130
                    }
131
                } else {
132
                    $skip = true;
133
                }
134
            }
135
            if (!$skip) {
136
                $comment_config['callback']['approve']($comment);
137
            }
138
        }
139
140
        // call custom update function if any
141
        if (false != $call_updatefunc && isset($comment_config['callback']['update'])
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
142
            && '' != trim($comment_config['callback']['update'])) {
143
            $skip = false;
144 View Code Duplication
            if (!function_exists($comment_config['callback']['update'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
                if (isset($comment_config['callbackFile'])) {
146
                    $callbackfile = trim($comment_config['callbackFile']);
147
                    if ('' != $callbackfile
148
                        && file_exists(XOOPS_ROOT_PATH . '/modules/' . $moddir . '/' . $callbackfile)) {
149
                        require_once XOOPS_ROOT_PATH . '/modules/' . $moddir . '/' . $callbackfile;
150
                    }
151
                    if (!function_exists($comment_config['callback']['update'])) {
152
                        $skip = true;
153
                    }
154
                } else {
155
                    $skip = true;
156
                }
157
            }
158
            if (!$skip) {
159
                $criteria = new CriteriaCompo(new Criteria('com_modid', $com_modid));
160
                $criteria->add(new Criteria('com_itemid', $com_itemid));
161
                $criteria->add(new Criteria('com_status', XOOPS_COMMENT_ACTIVE));
162
                $comment_count = $commentHandler->getCount($criteria);
163
                $func          = $comment_config['callback']['update'];
164
                call_user_func_array($func, [$com_itemid, $comment_count, $comment->getVar('com_id')]);
165
            }
166
        }
167
168
        // RMV-NOTIFY
169
        // trigger notification event if necessary
170
        if ($notify_event) {
171
            $not_modid = $com_modid;
172
            //            require_once XOOPS_ROOT_PATH . '/include/notification_functions.php';
173
            $not_catinfo  = notificationCommentCategoryInfo($not_modid);
174
            $not_category = $not_catinfo['name'];
175
            $not_itemid   = $com_itemid;
176
            $not_event    = $notify_event;
177
            // Build an ABSOLUTE URL to view the comment.  Make sure we
178
            // point to a viewable page (i.e. not the system administration
179
            // module).
180
            $comment_tags = [];
181
            $not_module   = $xoopsModule;
182
            if (!isset($comment_url)) {
183
                $com_config  = $not_module->getInfo('comments');
184
                $comment_url = $com_config['pageName'] . '?';
185
                $comment_url .= $com_config['itemName'];
186
            }
187
            $comment_tags['X_COMMENT_URL'] = XOOPS_URL . '/modules/' . $not_module->getVar('dirname') . '/' . $comment_url . '=' . $com_itemid . '&amp;com_id=' . $newcid . '&amp;com_rootid=' . $com_rootid . '&amp;com_mode=' . $com_mode . '&amp;com_order=' . $com_order . '#comment' . $newcid;
188
            $notificationHandler           = xoops_getHandler('notification');
189
            $notificationHandler->triggerEvent($not_category, $not_itemid, $not_event, $comment_tags, false, $not_modid);
190
        }
191
192
        PlanetUtility::planetRespondToTrackback(0);
193
    } else {
194
        PlanetUtility::planetRespondToTrackback(1, xoops_error($comment->getHtmlErrors()));
195
    }
196
}
197