示例#1
0
function submit_comment($comment_data)
{
    $output = false;
    $user_rg = '/^(user-)/';
    $comments_file = get_json_content(CONTENTPATH . 'comments.json');
    $comment = array();
    $comment['status'] = 'PENDING';
    if (isset($comment_data['usereply']) || isset($comment_data['adminreply']) || isset($comment_data['usersubmit'])) {
        if (isset($comment_data['usereply'])) {
            unset($comment_data['usereply']);
        } else {
            if (isset($comment_data['adminreply'])) {
                unset($comment_data['adminreply']);
                $comment['status'] = 'APPROVED';
            } else {
                if (isset($comment_data['usersubmit'])) {
                    unset($comment_data['usersubmit']);
                }
            }
        }
        foreach ($comment_data as $key => $value) {
            if (preg_match($user_rg, $key)) {
                switch ($key) {
                    case 'user-name':
                        $comment['user']['name'] = htmlspecialchars($value);
                        break;
                    case 'user-email':
                        $comment['user']['email'] = htmlspecialchars($value);
                        break;
                    case 'user-website':
                        $comment['user']['website'] = !empty($value) ? htmlspecialchars($value) : '';
                        break;
                    case 'postparent':
                        $comment['postparent'] = (int) $value;
                        break;
                    case 'commentparent':
                        $comment['commentparent'] = (int) $value;
                }
            } else {
                $comment[$key] = !empty($value) || (int) $value === 0 ? htmlspecialchars($value) : '';
            }
        }
    }
    $comment['id'] = (int) get_new_commentid();
    if (!comment_exist((int) $comment['id'])) {
        array_push($comments_file['comments'], $comment);
        if (write_to_json(CONTENTPATH . 'comments.json', $comments_file)) {
            $output = $comment;
        }
    }
    return $output;
}
/**
 * Change the status of a given comment from PENDING to APPROVED
 * @param int $comment_id 
 * @return bool Returns true on success and False on fail
 */
function approve_comment($comment_id)
{
    $output = false;
    $i = 0;
    if (comment_exist((int) $comment_id)) {
        $comment_file = get_json_content(CONTENTPATH . 'comments.json');
        $comments =& $comment_file['comments'];
        while ($i < count($comments)) {
            if ($comments[$i]['id'] === (int) $comment_id) {
                $comments[$i]['status'] = 'APPROVED';
                $approved = true;
                break;
            }
            $i++;
        }
        if ($approved === true) {
            if (write_to_json(CONTENTPATH . 'comments.json', $comment_file)) {
                $output = true;
            }
        }
    }
    return $output;
}