Search code examples
phpmybb

add function in php file get Call to undefined function exception


I'm new on php, I'm triyng to backup a post before deleting on a mybb forum.

I edited inc/class_moderation.php adding the following function backup_post, but when the call to the function backup_post throws

Call to undefined function backup_post() in /membri/myforum/inc/class_moderation.php on line 485

function delete_post($pid)
{
    global $mybb, $db, $cache, $plugins;

    $pid = $plugins->run_hooks("class_moderation_delete_post_start", $pid);
    // Get pid, uid, fid, tid, visibility, forum post count status of post
    $pid = intval($pid);
    $query = $db->query("
        SELECT p.pid, p.uid, p.fid, p.tid, p.visible, f.usepostcounts, t.visible as threadvisible, message, p.username as p_username, p.subject, p.dateline
        FROM ".TABLE_PREFIX."posts p
        LEFT JOIN ".TABLE_PREFIX."threads t ON (t.tid=p.tid)
        LEFT JOIN ".TABLE_PREFIX."forums f ON (f.fid=p.fid)
        WHERE p.pid='$pid'
    ");
    $post = $db->fetch_array($query);
    // If post counts enabled in this forum and it hasn't already been unapproved, remove 1
    if($post['usepostcounts'] != 0 && $post['visible'] != 0 && $post['threadvisible'] != 0)
    {
        $db->update_query("users", array("postnum" => "postnum-1"), "uid='{$post['uid']}'", 1, true);
    }

    if(!function_exists("remove_attachments"))
    {
        require MYBB_ROOT."inc/functions_upload.php";
    }

    // Remove attachments
    remove_attachments($pid);

    //Backup the post
    backup_post($pid);
    // Delete the post
    $db->delete_query("posts", "pid='$pid'");

    // Remove any reports attached to this post
    $db->delete_query("reportedposts", "pid='$pid'");

    $num_unapproved_posts = $num_approved_posts = 0;
    // Update unapproved post count
    if($post['visible'] == 0 || $post['threadvisible'] == 0)
    {
        ++$num_unapproved_posts;
    }
    else
    {
        ++$num_approved_posts;
    }
    $plugins->run_hooks("class_moderation_delete_post", $post['pid']);

    // Update stats
    $update_array = array(
        "replies" => "-{$num_approved_posts}",
        "unapprovedposts" => "-{$num_unapproved_posts}"
    );
    update_thread_counters($post['tid'], $update_array);

    // Update stats
    $update_array = array(
        "posts" => "-{$num_approved_posts}",
        "unapprovedposts" => "-{$num_unapproved_posts}"
    );

    update_forum_counters($post['fid'], $update_array);

    return true;
}

function backup_post($pid)
{
    global $mybb, $db, $cache, $plugins;

    // Get pid, uid, fid, tid, visibility, forum post count status of post
    $pid = intval($pid);
    $query = $db->query("
        SELECT p.pid, p.uid, p.fid, p.tid, p.visible, f.usepostcounts, t.visible as threadvisible, message, p.username as p_username, p.subject, p.dateline
        FROM ".TABLE_PREFIX."posts p
        LEFT JOIN ".TABLE_PREFIX."threads t ON (t.tid=p.tid)
        LEFT JOIN ".TABLE_PREFIX."forums f ON (f.fid=p.fid)
        WHERE p.pid='$pid'
    ");
    $post = $db->fetch_array($query);

    $deletedpost = array(
            "username_delete" => $mybb->user['username'],
            "username" => $post['p_username'],
            "subject" => $post['subject'],
            "message" => $post['message'],
            "dateline" => TIME_NOW,
            "post_dateline" => $post['dateline'],
        );
    $db->insert_query("posts_deleted", $deletedpost);

    //14-06-20016
    //ob_start();
    //var_dump($mybb);
    //$data = ob_get_clean();
    //$fp = fopen(MYBB_ROOT."myText.txt","wb");
    //fwrite($fp,$data);
    //fwrite($fp, "cancellato da ".$mybb->user['username']);
    //fwrite($fp, "autore ".$post['p_username']);
    //fwrite($fp, "messaggio ".$post['message']);


    /*
    foreach ($post as $key => $value) {
        fwrite($fp, $key );
    }
    */
    //fclose($fp);



    return true;
}

Solution

  • You are adding functions to a class (then we tend to call them "methods" instead of "functions", but that doesn't really matter).

    When you want to call a class method, you need to call it like $class_instance->method_name() instead of the usual method_name(), which works for "plain" functions.

    When you are calling a method from another method in the same class, you use $this to get the same class instance.

    So if you change this line:

    backup_post($pid);
    

    to:

    $this->backup_post($pid);
    

    it should work.