yf_comments_manage user module

Comments management.

Fileplugins/comments/modules/comments/yf_comments_manage.class.php
Usemodule('comments_manage'), URL ?object=comments_manage

Public properties

PropertyDefaultDescription
None declared in this class.

Public methods 4

None without a leading underscore.

Methods starting with _ hooks and helpers; not routable as page actions

_add

public function _add($params = [])

Form to add comments.

    public function _add($params = [])
    {
        if (empty(main()->USER_ID) && MAIN_TYPE_USER && ! $params['allow_guests_posts']) {
            return '';
        }
        $_GET['id'] = (int) (($_GET['id'] ?? null));
        $OBJECT_NAME = ! empty($params['object_name']) ? $params['object_name'] : ($_GET['object'] ?? null);
        $OBJECT_ID = ! empty($params['object_id']) ? (int) ($params['object_id']) : (int) (($_GET['id'] ?? null));
        $FORM_ACTION = ! empty($params['add_form_action']) ? $params['add_form_action'] : './?object=' . ($_GET['object'] ?? null) . '&action=add_comment&id=' . $OBJECT_ID;
        $STPL_NAME_ADD = ! empty($params['stpl_add']) ? $params['stpl_add'] : 'comments/add_form';
        $RETURN_PATH = ($_SERVER['HTTP_REFERER'] ?? null);
        if ( ! empty($params['return_path'])) {
            $RETURN_PATH = process_url($params['return_path']);
        } elseif ( ! empty($params['return_action'])) {
            $RETURN_PATH = process_url('./?object=' . ($_GET['object'] ?? null) . '&action=' . $params['return_action'] . '&id=' . $OBJECT_ID);
        }
        if (empty($OBJECT_NAME) || empty($OBJECT_ID)) {
            return '';
        }
        $COMMENT_IS_ALLOWED = true;
        if (is_object(module(($_GET['object'] ?? null)))) {
            $m = module('comments')->_add_allowed_method;
            $COMMENT_IS_ALLOWED = module(($_GET['object'] ?? null))->$m($params);
        }
        if ( ! $COMMENT_IS_ALLOWED && common()->_error_exists()) {
            return _e();
        }
        if ( ! $COMMENT_IS_ALLOWED) {
            return false;
        }
        if (count((array) $_POST) > 0 && ! isset($_POST['_not_for_comments'])) {
            if (isset($_POST['text2']) && ! isset($_POST['text'])) {
                $_POST['text'] = ($_POST['text2'] ?? null);
                unset($_POST['text2']);
            }
            if ( ! common()->_error_exists()) {
                $_POST['text'] = substr(($_POST['text'] ?? ''), 0, module('comments')->MAX_POST_TEXT_LENGTH);
                if (empty($_POST['text'])) {
                    _re('Comment text required');
                }
            }
            if (module(($_GET['object'] ?? null))->USE_CAPTCHA) {
                if (module('comments')->USE_TREE_MODE && isset($_POST['parent_id'])) {
                    if (empty(main()->USER_ID)) {
                        module(($_GET['object'] ?? null))->_captcha_check();
                    }
                } else {
                    module(($_GET['object'] ?? null))->_captcha_check();
                }
            }
            if ( ! common()->_error_exists() && MAIN_TYPE_USER) {
                $info_for_check = [
                    'comment_text' => ($_POST['text'] ?? null),
                    'user_id' => main()->USER_ID,
                ];
                $USER_BANNED = _check_user_ban($info_for_check, module('comments')->_user_info);
                if ($USER_BANNED) {
                    module('comments')->_user_info = user(main()->USER_ID);
                }
                if (module('comments')->_user_info['ban_comments']) {
                    return _e(
                        'Sorry, you are not allowed to post comments!' . PHP_EOL . 'Perhaps, you broke some of our rules and moderator has banned you from using this feature. Please, enjoy our site in some other way!'
                        . 'For more details <a href=\'./?object=faq&action=view&id=16\'>click here</a>'
                    );
                }
            }
            if ( ! common()->_error_exists() && module('comments')->ANTI_FLOOD_TIME && MAIN_TYPE_USER) {
                $FLOOD_DETECTED = db()->query_fetch(
                    'SELECT id,add_date FROM ' . db('comments') . ' 
					WHERE ' . (main()->USER_ID ? 'user_id=' . (int) (main()->USER_ID) : 'ip="' . _es(common()->get_ip()) . '"')
                        . ' AND add_date > ' . (time() - module('comments')->ANTI_FLOOD_TIME) . ' 
					ORDER BY add_date DESC LIMIT 1'
                );
                if ( ! empty($FLOOD_DETECTED)) {
                    _re('Please wait %num seconds before post comment.', ['%num' => (int) (module('comments')->ANTI_FLOOD_TIME - (time() - $FLOOD_DETECTED['add_date']))]);
                }
            }

            if ( ! common()->_error_exists()) {
                if (module('comments')->ANTI_SPAM_DETECT) {
                    $this->_spam_check(($_POST['text'] ?? null));
                }
            }

            if ( ! empty($_POST['user_email'])) {
                if ( ! preg_match('#^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\.[a-z]{2,3}$#i', $_POST['user_email'])) {
                    _re('Invalid e-mail, please check your spelling');
                }
            }

            if ( ! common()->_error_exists()) {
                if (module('comments')->AUTO_FILTER_INPUT_TEXT) {
                    $_POST['text'] = _filter_text(($_POST['text'] ?? null));
                }
                // Do close BB Codes (if needed)
                if (module('comments')->USE_BB_CODES) {
                    $BB_CODES_OBJ = _class('bb_codes');
                    if (is_object($BB_CODES_OBJ)) {
                        $_POST['text'] = $BB_CODES_OBJ->_force_close_bb_codes(($_POST['text'] ?? null));
                    }
                }
                db()->INSERT('comments', [
                    'object_name' => _es($OBJECT_NAME),
                    'object_id' => (int) $OBJECT_ID,
                    'parent_id' => (int) (isset($_POST['parent_id']) ? ($_POST['parent_id'] ?? null) : 0),
                    'user_id' => (int) (main()->USER_ID),
                    'user_name' => ! main()->USER_ID ? _es(($_POST['user_name'] ?? null)) : '',
                    'user_email' => ! main()->USER_ID ? _es(($_POST['user_email'] ?? null)) : '',
                    'text' => _es(($_POST['text'] ?? null)),
                    'add_date' => time(),
                    'active' => 1,
                    'ip' => _es(common()->get_ip()),
                ]);
                $RECORD_ID = db()->INSERT_ID();

                $try_trigger_callback = [module(($_GET['object'] ?? null)), module('comments')->_on_update_trigger];
                if (is_callable($try_trigger_callback)) {
                    call_user_func($try_trigger_callback, $params);
                }
                common()->_add_activity_points(main()->USER_ID, $OBJECT_NAME . '_comment', strlen(($_POST['text'] ?? '')), $RECORD_ID);
                return js_redirect($RETURN_PATH, false);
            }
        }
        $body = null;
        $error_message = _e();
        if (empty($_POST['go']) || ! empty($error_message)) {
            if ((module('comments')->VIEW_EMAIL_FIELD == true) and (empty(main()->USER_ID))) {
                $view_user_email = '1';
            } else {
                $view_user_email = '0';
            }
            $replace = [
                'form_action' => $FORM_ACTION,
                'error_message' => $error_message,
                'user_name' => _escape_tpl_braces($_POST['user_name']),
                'view_user_email' => $view_user_email,
                'user_email' => _escape_tpl_braces($_POST['user_email']),
                'text' => _prepare_html($_POST['text']),
                'object_name' => _prepare_html($OBJECT_NAME),
                'object_id' => (int) $OBJECT_ID,
                'use_captcha' => (int) ((bool) module(($_GET['object'] ?? null))->USE_CAPTCHA),
                'captcha_block' => module(($_GET['object'] ?? null))->_captcha_block(),
                'bb_codes_block' => module('comments')->USE_BB_CODES ? _class('bb_codes')->_display_buttons(['unique_id' => 'text']) : '',
                'submit_buttons' => _class_safe('preview')->_display_buttons(),
                'js_check' => (int) ((bool) module('comments')->JS_TEXT_CHECKING),
                'parent_id' => (int) (($_POST['parent_id'] ?? null)),
            ];
            $body = tpl()->parse($STPL_NAME_ADD, $replace);
        }
        return $body;
    }

_delete

public function _delete($params = [])

Do delete comment.

    public function _delete($params = [])
    {
        if (empty(main()->USER_ID) && MAIN_TYPE_USER) {
            return _error_need_login();
        }
        $_GET['id'] = (int) (($_GET['id'] ?? null));
        $comment_info = db()->query_fetch('SELECT * FROM ' . db('comments') . ' WHERE id=' . (int) (($_GET['id'] ?? null)));
        if (empty($comment_info['id'])) {
            return _e('No such comment!');
        }
        $OBJECT_NAME = ! empty($params['object_name']) ? $params['object_name'] : ($_GET['object'] ?? null);
        $OBJECT_ID = ! empty($params['object_id']) ? (int) ($params['object_id']) : (int) (($_GET['id'] ?? null));
        $SILENT_MODE = ! empty($params['silent_mode']) ? 1 : 0;
        $RETURN_PATH = ($_SERVER['HTTP_REFERER'] ?? null);
        if ( ! empty($params['return_path'])) {
            $RETURN_PATH = process_url($params['return_path']);
        } elseif ( ! empty($params['return_action'])) {
            $RETURN_PATH = process_url('./?object=' . ($_GET['object'] ?? null) . '&action=' . $params['return_action'] . '&id=' . $comment_info['object_id']);
        }
        if (empty($OBJECT_NAME) || empty($OBJECT_ID)) {
            return '';
        }
        if (module('comments')->_user_info['ban_comments'] && MAIN_TYPE_USER) {
            return _e(
                'Sorry, you are not allowed to post comments!' . PHP_EOL . 'Perhaps, you broke some of our rules and moderator has banned you from using this feature. Please, enjoy our site in some other way!'
                . 'For more details <a href=\'./?object=faq&action=view&id=16\'>click here</a>'
            );
        }
        $module_obj = module(($_GET['object'] ?? null));
        // Check if user is allowed to perform this action
        $delete_allowed = false;
        $delete_allowed_check_method = is_object($module_obj) && method_exists($module_obj, module('comments')->_delete_allowed_method);
        if ($delete_allowed_check_method) {
            $m = module('comments')->_delete_allowed_method;
            $delete_allowed = (bool) module(($_GET['object'] ?? null))->$m([
                'user_id' => $comment_info['user_id'],
                'object_id' => $comment_info['object_id'],
            ]);
        } else {
            $delete_allowed = main()->USER_ID && $comment_info['user_id'] == main()->USER_ID;
        }
        if (MAIN_TYPE_ADMIN || $SILENT_MODE) {
            $delete_allowed = true;
        } else {
            // get elapse time
            if ( ! empty(module('comments')->EDIT_LIMIT_TIME)) {
                $elapse_time = time() - $comment_info['add_date'];
                if ($elapse_time > module('comments')->EDIT_LIMIT_TIME) {
                    return _e('allowed time to delete has expired');
                }
            }
        }

        if ( ! $delete_allowed) {
            return _e('You are not allowed to perform this action');
        }
        module('unread')->_set_read('comments', ($_GET['id'] ?? null));

        if (module('comments')->USE_TREE_MODE) {
            $have_children = db()->query_fetch(
                'SELECT id FROM ' . db('comments') . ' 
				WHERE object_name="' . $comment_info['object_name'] . '" AND object_id=' . $comment_info['object_id'] . ' AND parent_id=' . $comment_info['id'] . ' 
				LIMIT 1'
            );

            if ($have_children) {
                db()->UPDATE('comments', [
                    'text' => '__comment was deleted__',
                    'user_id' => 0,
                ], 'id=' . (int) (($_GET['id'] ?? null)));
            } else {
                db()->query('DELETE FROM ' . db('comments') . ' WHERE id=' . (int) (($_GET['id'] ?? null)) . ' LIMIT 1');
            }
        } else {
            db()->query('DELETE FROM ' . db('comments') . ' WHERE id=' . (int) (($_GET['id'] ?? null)) . ' LIMIT 1');
        }
        // Execute custom on_update trigger (if exists one)
        $try_trigger_callback = [module(($_GET['object'] ?? null)), module('comments')->_on_update_trigger];
        if (is_callable($try_trigger_callback)) {
            call_user_func($try_trigger_callback, $params);
        }
        return ! $SILENT_MODE ? js_redirect($RETURN_PATH, false) : '';
    }

_edit

public function _edit($params = [])

Do edit own comment.

    public function _edit($params = [])
    {
        if (empty(main()->USER_ID) && MAIN_TYPE_USER) {
            return _error_need_login();
        }
        $_GET['id'] = (int) (($_GET['id'] ?? null));
        $comment_info = db()->query_fetch('SELECT * FROM ' . db('comments') . ' WHERE id=' . (int) (($_GET['id'] ?? null)));
        if (empty($comment_info['id'])) {
            return _e('No such comment!');
        }
        $OBJECT_NAME = ! empty($params['object_name']) ? $params['object_name'] : ($_GET['object'] ?? null);
        $OBJECT_ID = ! empty($params['object_id']) ? (int) ($params['object_id']) : (int) (($_GET['id'] ?? null));
        $FORM_ACTION = ! empty($params['add_form_action']) ? $params['add_form_action'] : './?object=' . ($_GET['object'] ?? null) . '&action=' . ($_GET['action'] ?? null) . '&id=' . $OBJECT_ID;
        $STPL_NAME_EDIT = ! empty($params['stpl_edit']) ? $params['stpl_edit'] : 'comments/edit_form';
        $RETURN_PATH = ($_SERVER['HTTP_REFERER'] ?? null);
        if ( ! empty($params['return_path'])) {
            $RETURN_PATH = process_url($params['return_path']);
        } elseif ( ! empty($params['return_action'])) {
            $RETURN_PATH = process_url('./?object=' . ($_GET['object'] ?? null) . '&action=' . $params['return_action'] . '&id=' . $comment_info['object_id']);
        }
        if (empty($OBJECT_NAME) || empty($OBJECT_ID)) {
            return '';
        }
        $edit_allowed = false;
        $edit_allowed_check_method = is_object(module(($_GET['object'] ?? null))) && method_exists(module(($_GET['object'] ?? null)), module('comments')->_edit_allowed_method);
        if ($edit_allowed_check_method) {
            $m = module('comments')->_edit_allowed_method;
            $edit_allowed = (bool) module(($_GET['object'] ?? null))->$m([
                'user_id' => $comment_info['user_id'],
                'object_id' => $comment_info['object_id'],
            ]);
        } else {
            $edit_allowed = main()->USER_ID && $comment_info['user_id'] == main()->USER_ID;
        }
        if (MAIN_TYPE_ADMIN) {
            $edit_allowed = true;
        } else {
            if ( ! empty(module('comments')->EDIT_LIMIT_TIME)) {
                $elapse_time = time() - $comment_info['add_date'];
                if ($elapse_time > module('comments')->EDIT_LIMIT_TIME) {
                    return _e('allowed time to edit has expired');
                }
            }
        }
        if ( ! $edit_allowed) {
            return _e('You are not allowed to perform this action');
        }
        $user_info = user($comment_info['user_id'], ['id', 'name', module('comments')->_user_nick_field, 'photo_verified'], ['WHERE' => ['active' => 1]]);
        if (count((array) $_POST) > 0 && ! isset($_POST['_not_for_comments'])) {
            $_POST['text'] = substr(($_POST['text'] ?? ''), 0, module('comments')->MAX_POST_TEXT_LENGTH);
            if (empty($_POST['text'])) {
                _re('Comment text required');
            }
            if (module(($_GET['object'] ?? null))->USE_CAPTCHA) {
                module(($_GET['object'] ?? null))->_captcha_check();
            }
            if ( ! common()->_error_exists() && MAIN_TYPE_USER) {
                $info_for_check = [
                    'comment_text' => ($_POST['text'] ?? null),
                    'user_id' => main()->USER_ID,
                ];
                $USER_BANNED = _check_user_ban($info_for_check, module('comments')->_user_info);
                if ($USER_BANNED) {
                    module('comments')->_user_info = user(main()->USER_ID);
                }
                if (module('comments')->_user_info['ban_comments']) {
                    return _e(
                        'Sorry, you are not allowed to post comments!' . PHP_EOL . 'Perhaps, you broke some of our rules and moderator has banned you from using this feature. Please, enjoy our site in some other way!'
                        . 'For more details <a href=\'./?object=faq&action=view&id=16\'>click here</a>'
                    );
                }
            }
            // Anti-flood check
            if ( ! common()->_error_exists() && module('comments')->ANTI_FLOOD_TIME && MAIN_TYPE_USER) {
                $FLOOD_DETECTED = db()->query_fetch(
                    'SELECT id,add_date FROM ' . db('comments') . ' WHERE '
                        . (main()->USER_ID ? 'user_id=' . (int) (main()->USER_ID) : 'ip="' . _es(common()->get_ip()) . '"')
                        . ' AND add_date > ' . (time() - module('comments')->ANTI_FLOOD_TIME) . ' 
					ORDER BY add_date DESC 
					LIMIT 1'
                );
                if ( ! empty($FLOOD_DETECTED)) {
                    _re('Please wait %num seconds before post comment.', ['%num' => (int) (module('comments')->ANTI_FLOOD_TIME - (time() - $FLOOD_DETECTED['add_date']))]);
                }
            }

            // Anti-spam check
            if ( ! common()->_error_exists()) {
                if (module('comments')->ANTI_SPAM_DETECT) {
                    $this->_spam_check(($_POST['text'] ?? null));
                }
            }

            if ( ! common()->_error_exists()) {
                if (module('comments')->AUTO_FILTER_INPUT_TEXT) {
                    $_POST['text'] = _filter_text(($_POST['text'] ?? null));
                }
                if (module('comments')->USE_BB_CODES) {
                    $BB_CODES_OBJ = _class('bb_codes');
                    if (is_object($BB_CODES_OBJ)) {
                        $_POST['text'] = $BB_CODES_OBJ->_force_close_bb_codes(($_POST['text'] ?? null));
                    }
                }
                db()->UPDATE('comments', [
                    'text' => _es(($_POST['text'] ?? null)),
                ], 'id=' . (int) ($comment_info['id']));

                $try_trigger_callback = [module(($_GET['object'] ?? null)), module('comments')->_on_update_trigger];
                if (is_callable($try_trigger_callback)) {
                    call_user_func($try_trigger_callback, $params);
                }
                $RETURN_PATH = ! empty($params['return_path']) ? process_url($params['return_path']) : ( ! empty($params['return_action']) ? process_url('./?object=' . ($_GET['object'] ?? null) . '&action=' . $params['return_action'] . '&id=' . $comment_info['object_id']) : ($_SERVER['HTTP_REFERER'] ?? null));
                return js_redirect($RETURN_PATH, false);
            }
        } else {
            $_POST['text'] = $comment_info['text'];
        }
        $body = null;
        $error_message = _e();
        if (empty($_POST['go']) || ! empty($error_message)) {
            $replace = [
                'form_action' => $FORM_ACTION,
                'error_message' => $error_message,
                'user_id' => (int) (main()->USER_ID),
                'user_name' => _prepare_html(_display_name($user_info)),
                'user_avatar' => _show_avatar($comment_info['user_id'], $user_info, 1, 1),
                'user_profile_link' => _profile_link($comment_info['user_id']),
                'user_email_link' => _email_link($comment_info['user_id']),
                'text' => _prepare_html($_POST['text']),
                'back_url' => _escape_tpl_braces($_SERVER['HTTP_REFERER']),
                'object_name' => _prepare_html($OBJECT_NAME),
                'object_id' => (int) $OBJECT_ID,
                'use_captcha' => (int) ((bool) module(($_GET['object'] ?? null))->USE_CAPTCHA),
                'captcha_block' => module(($_GET['object'] ?? null))->_captcha_block(),
                'bb_codes_block' => module('comments')->USE_BB_CODES ? _class('bb_codes')->_display_buttons(['unique_id' => 'text']) : '',
                'js_check' => (int) ((bool) module('comments')->JS_TEXT_CHECKING),
            ];
            $body = tpl()->parse($STPL_NAME_EDIT, $replace);
        }
        return $body;
    }

_spam_check

public function _spam_check($text)

Check spam.

    public function _spam_check($text)
    {
        preg_match_all(module('comments')->HTML_LINK_REGEX, $text, $result);
        preg_match_all(module('comments')->BBCODE_LINK_REGEX, $text, $result2);
        $count_links = count((array) $result[1]) + count((array) $result2[1]);
        if (empty(main()->USER_ID)) {
            if ($count_links > 1) {
                _re('Too many links');
            }
        } else {
            if ($count_links > 3) {
                _re('Too many links');
            }
        }
    }