Jump to content

Recommended Posts

I'm working with a PHP script and I keep getting a Fatal error: Call to a member function on a non-object in /homepages/17/d160651986/htdocs/mail/mlm_fns.php on line 306

error.

 

Here is the mlm_fns.php script, what am I doing wrong?

<?php

// do we already have a record of this subscriber?
function subscriber_exists($email)
{
  if(!$email)
    return false;

  if(!($conn=db_connect()))
    return false;

  $query = "select count(*) from subscribers where email = '$email'";

  $result = $conn->query($query);
  if(!$result)
    return false;
  $row = $result->fetch_array();
  return ($row[0]>0);
}

// is this email address subscribed to this list?
function subscribed($email, $listid)
{
  if(!$email||!$listid)
    return false;

  if(!($conn=db_connect()))
    return false;

  $query = "select count(*) from sub_lists where email = '$email'
            and listid = $listid";

  $result = $conn->query($query);
  if(!$result)
    return false;
  $row=$result->fetch_array();
  return ($row[0]>0);
}

// is this listid the id of a list?
function list_exists($listid)
{
  if(!$listid)
    return false;

  if(!($conn=db_connect()))
    return false;

  $query = "select count(*) from lists where listid = '$listid'";

  $result = $conn->query($query);
  if(!$result)
    return false;
  $row=$result->fetch_array();
  return ($row[0]>0);
}

// get the name of the person with this email
function get_real_name($email)
{
  if(!$email)
    return false;

  if(!($conn=db_connect()))
    return false;

  $query = "select realname from subscribers where email = '$email'";

  $result = $conn->query($query);
  if(!$result)
    return false;
  $row=$result->fetch_array();
  return trim($row[0]);
}

// get the type (HTML or Text) that this person wants email in
function get_mimetype($email)
{
  if(!$email)
    return false;

  if(!($conn=db_connect()))
    return false;

  $query = "select mimetype from subscribers where email = '$email'";

  $result = $conn->query($query);
  if(!$result)
    return false;
  $row=$result->fetch_array();
  return trim($row[0]);
}

// subscribe this email address to this list
function subscribe($email, $listid)
{
  if(!$email||!$listid||!list_exists($listid)||!subscriber_exists($email))
    return false;

  //if already subscribed exit
  if(subscribed($email, $listid))
    return false;

  if(!($conn=db_connect()))
    return false;

  $query = "insert into sub_lists values ('$email', $listid)";

  $result = $conn->query($query);
  return $result;
}

// unsubscribe this email address from this list
function unsubscribe($email, $listid)
{
  if(!$email||!$listid)
    return false;

  if(!($conn=db_connect()))
    return false;

  $query = "delete from sub_lists where email = '$email' and listid = $listid";

  $result = $conn->query($query);
  return $result;
}

// load the data stored about this mail from the database
function load_mail_info($mailid)
{
  if(!$mailid)
    return false;

  if(!($conn=db_connect()))
    return false;

  $query = "select subject, listid, status, sent from mail
            where mailid = $mailid";

  $result = $conn->query($query);

  if(!$result)
  {
    echo "Cannot retrieve this mail";
    return false;
  }
  return $result->fetch_assoc();

}

// load the data stored about this list from the database
function load_list_info($listid)
{
  if(!$listid)
    return false;

  if(!($conn=db_connect()))
    return false;

  $query = "select listname, blurb from lists where listid = $listid";
  $result = $conn->query($query);
  if(!$result)
  {
    echo 'Cannot retrieve this list';
    return false;
  }
  $info =  $result->fetch_assoc();

  $query = "select count(*) from sub_lists where listid = $listid";
  $result = $conn->query($query);
  if($result)
  {
    $row = $result->fetch_array();
    $info['subscribers'] = $row[0];
  }
  $query = "select count(*) from mail where listid = $listid
            and status = 'SENT'";
  $result = $conn->query($query);
  if($result)
  {
    $row = $result->fetch_array();
    $info['archive'] = $row[0];
  }
  return $info;
}


// get the name that belongs to this list id
function get_list_name($listid)
{
  if(!$listid)
    return false;

  if(!($conn=db_connect()))
    return false;

  $query = "select listname from lists where listid = $listid";
  $result = $conn->query($query);
  if(!$result)
  {
    return false;
  }
  $row = $result->fetch_array();
  return $row[0];
}

// add a new list to the database
function store_list($admin_user, $details)
{
  if(!filled_out($details))
  {
    echo 'All fields must be filled in.  Try again.<br /><br />';
    return false;
  }
  else
  {
    if(!check_admin_user($admin_user))
      return false;  
      // how did this function get called by somebody not logged in as admin?
    
    if(!($conn=db_connect()))
    { 
      return false;
    }
    
    $query = "select count(*) from lists where listname = '".$details['name']."'";
    $result = $conn->query($query);
    $row = $result->fetch_array();
    if($row[0] > 0)
    {
      echo 'Sorry, there is already a list with this name.';
      return false;
    }
    
    $query = "insert into lists values (NULL, 
                                       '".$details['name']."',
                                       '".$details['blurb']."')";

    $result = $conn->query($query);
    return $result; 
  }
}

// get the lists that this user is subscribed to
function get_subscribed_lists($email)
{
  $list = array();  

  $query = "select lists.listid, listname from sub_lists, lists 
            where email='$email' and lists.listid = sub_lists.listid 
            order by listname";

  if($conn=db_connect())
  {
    $result = $conn->query($query);
    if(!$result)
    {
      echo '<p>Unable to get list from database.</p>';
      return false;
    }
    $num = $result->num_rows;
    for($i = 0; $i<$num; $i++)
    {
      $row = $result->fetch_array();
      array_push($list, array($row[0], $row[1]));
    }
  }
  return $list;
}

//get the lists that this user is *not* subscribed to 
function get_unsubscribed_lists($email)
{
  $list = array();  

  $query = "select lists.listid, listname, email from lists left join sub_lists 
                   on lists.listid = sub_lists.listid 
                   and email='$email' where email is NULL order by listname";
  if($conn=db_connect())
  {
    $result = $conn->query($query);
    if(!$result)
    {
      echo '<p>Unable to get list from database.</p>';
      return false;
    }
    $num = $result->num_rows;
    for($i = 0; $i<$num; $i++)
    {
      $row = $result->fetch_array();
      array_push($list, array($row[0], $row[1]));
    }
  }
  return $list;
}

// get all lists 
function get_all_lists()
{
  $list = array();  

  $query = 'select listid, listname from lists order by listname';

  if($conn=db_connect())
  {
    $result = $conn->query($query);
    if(!$result)
    {
      echo '<p>Unable to get list from database.</p>';
      return false;
    }
    $num = $result->num_rows;
    for($i = 0; $i<$num; $i++)
    {
      $row = $result->fetch_array();
      array_push($list, array($row[0], $row[1]));
    }
  }
  return $list;
} 

function get_archive($listid)
{
  //returns an array of the archived mail for this list
  //array has rows like (mailid, subject)

  $list = array();  
  $listname = get_list_name($listid);
  
  $query = "select mailid, subject, listid from mail 
            where listid = $listid and status = 'SENT' order by sent"; 

  if($conn=db_connect())
  {
    $result = $conn->query($query);
    if(!$result)
    {
      echo '<p>Unable to get list from database.</p>';
      return false;
    }
    $num = $result->num_rows;
  
    for($i = 0; $i<$num; $i++)
    {
      $row = $result->fetch_array();
      $arr_row = array($row[0], $row[1], 
                   $listname, $listid); 
      array_push($list, $arr_row);
    }
  }
  return $list;
} 

// get the list of mail created, but not yet sent
function get_unsent_mail($email)
{
  if(!check_admin_user($email))
  {
    return false;
  }
  
  $list = array();  

  $query = "select mailid, subject, listid from mail 
            where status = 'STORED' or status = 'TESTED' order by modified"; 

  if($conn=db_connect())
  {
    $result = $conn->query($query);
    if(!$result)
    {
      echo '<p>Unable to get list from database.</p>';
      return false;
    }
    $num = $result->num_rows;
    for($i = 0; $i<$num; $i++)
    {
      $row = $result->fetch_array();
      array_push($list, array($row[0], 
                              $row[1], 
                              get_list_name($row[2]),
                              $row[2]
                              )
                 );
    }
  }
  return $list;
} 

// add a new subscriber to the database, or let a user modify their data
function store_account($normal_user, $admin_user, $details)
{
  if(!filled_out($details))
  {
    echo 'All fields must be filled in.  Try again.<br /><br />';
    return false;
  }
  else
  {
    if(subscriber_exists($details['email']))
    {
      //check logged in as the user they are trying to change
      if(get_email()==$details['email'])
      {
        $query = "update subscribers set realname = '$details[realname]',
                                         mimetype = '$details[mimetype]'
                  where email = '" . $details[email] . "'";
        if($conn=db_connect())
        {
          if ($conn->query($query))
            return true;
          else
            return false;
        }
        else
        {
          echo 'Could not store changes.<br /><br /><br /><br /><br /><br />';
          return false;
        }
      }
      else
      {
        echo '<p>Sorry, that email address is already registered here.</p>';
        echo '<p>You will need to log in with that address to change '
             .' its settings.</p>';
        return false;                 
      }      
    }
    else // new account
    {
      $query = "insert into subscribers 
                        values ('$details[email]',
                        '$details[realname]',
                        '$details[mimetype]',
                         sha1('$details[new_password]'),
                                                0)";          
        if($conn=db_connect())
        {
          if ($conn->query($query))
            return true;
          else
            return false;
        }
        else
        {
          echo 'Could not store new account.<br /><br /><br /><br /><br /><br />';
          return false;
        }
    }
  }
}

// create the message from the stored DB entries and files
// send test messages to the administrator, or real messages to the whole list
function send($mailid, $admin_user)
{
  if(!check_admin_user($admin_user))
    return false;
  
  if(!($info = load_mail_info($mailid)))
  { 
    echo "Cannot load list information for message $mailid";
    return false;
  }
  $subject = $info['subject'];
  $listid = $info['listid'];
  $status = $info['status'];
  $sent = $info['sent'];
    
  $from_name = 'Pyramid MLM';
      
  $from_address = 'return@address';
  $query = "select email from sub_lists where listid = $listid";
  
  $conn = db_connect();
  $result = $conn->query($query);
  if (!$result)
  {
    echo $query;
    return false;  
  }
  else if ($result->num_rows==0)
  {
    echo "There is nobody subscribed to list number $listid";
    return false; 
  }
  
  // include PEAR mail classes
  include('Mail.php');
  include('Mail/Mime.php');

  // instantiate MIME class and pass it the carriage return/line feed 
  // character used on this system
  $message = new Mail_mime("\r\n");

  // read in the text version of the newsletter
  $textfilename = "archive/$listid/$mailid/text.txt";
  $tfp = fopen($textfilename, "r");
  $text = fread($tfp, filesize($textfilename));
  fclose($tfp);

  // read in the HTML version of the newsletter
  $htmlfilename = "archive/$listid/$mailid/index.html";
  $hfp = fopen($htmlfilename, "r");
  $html = fread($hfp, filesize($htmlfilename));
  fclose($hfp);

  // add HTML and text to the mimemail object
  $message->setTXTBody($text);
  $message->setHTMLBody($html);

  // get the list of images that relate to this message
  $query = "select path, mimetype from images where mailid = $mailid";
  $result = $conn->query($query);
  if(!$result)
  {
    echo '<p>Unable to get image list from database.</p>';
    return false;
  }
  $num = $result->num_rows;
  for($i = 0; $i<$num; $i++)
  {  
    //load each image from disk
    $row = $result->fetch_array();
    $imgfilename = "archive/$listid/$mailid/".$row[0];
    $imgtype = $row[1];
     // add each image to the object
    $message->addHTMLImage($imgfilename, $imgtype, $imgfilename, true);
  }
    
  // create message body
  $body = $message->get();  

  // create message headers
  $from = '"'.get_real_name($admin_user).'" <'.$admin_user.'>';
  $hdrarray = array(              
               'From'    => $from,
               'Subject' => $subject);

  $hdrs = $message->headers($hdrarray);

  // create the actual sending object
  $sender =& Mail::factory('mail');

  if($status == 'STORED')
  {
    
    // send the HTML message to the administrator
    $sender->send($admin_user, $hdrs, $body);
      
    // send the plain text version of the message to administrator
    mail($admin_user, $subject, $text, 'From: "'.get_real_name($admin_user).'" <'.$admin_user.">");

    echo "Mail sent to $admin_user"; 

    // mark newsletter as tested
    $query = "update mail set status = 'TESTED' where mailid = $mailid";
    $result = $conn->query($query);
    
    echo '<p>Press send again to send mail to whole list.<center>';
    display_button('send', "&id=$mailid");
    echo '</center></p>';
  }    
  else if($status == 'TESTED')
  {
    //send to whole list
    
    $query = "select subscribers.realname, sub_lists.email, 
                     subscribers.mimetype  
              from sub_lists, subscribers 
              where listid = $listid and 
                    sub_lists.email = subscribers.email";
                       
    $result = $conn->query($query);
    if(!$result)
      echo '<p>Error getting subscriber list</p>';
      
    $count = 0;      
    // for each subscriber
    while( $subscriber = $result->fetch_row() )
    {
      if($subscriber[2]=='H')
      {
        //send HTML version to people who want it
        $sender->send($subscriber[1], $hdrs, $body);
      }
      else
      {
        //send text version to people who don't want HTML mail
        mail($subscriber[1], $subject, $text, 
                       'From: "'.get_real_name($admin_user).'" <'.$admin_user.">");
      }
      $count++; 
    }
      
    $query = "update mail set status = 'SENT', sent = now() 
              where mailid = $mailid";
    $result = $conn->query($query);
    echo "<p>A total of $count messages were sent.</p>";    
  }
  else if($status == 'SENT')
  {
    echo '<p>This mail has already been sent.</p>';    
  }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/144011-php-error-help/
Share on other sites

Here is the actualy file, line 82, my bad:

 

<?php
function retrieve_message($auth_user, $accountid, $messageid, $fullheaders)
{
  $message = array();
  
  if(!($auth_user && $messageid && $accountid))
    return false;
  
  $imap = open_mailbox($auth_user, $accountid);
  if(!$imap) 
    return false;      
  
  $header = imap_header($imap, $messageid);
  
  if(!$header) 
    return false;
  
  $message['body'] = imap_body($imap, $messageid);
  if(!$message['body']) 
    $message['body'] = "[This message has no body]\n\n\n\n\n\n";
    
  if($fullheaders)
    $message['fullheaders'] = imap_fetchheader($imap, $messageid);
  else
    $message['fullheaders'] = '';
  
  $message['subject'] = $header->subject;
  $message['fromaddress'] =   $header->fromaddress;
  $message['toaddress'] =   $header->toaddress;
  $message['ccaddress'] =   $header->ccaddress;
  $message['date'] =   $header->date;
    
  // note we can get more detailed information by using from and to 
  // rather than fromaddress and toaddress, but these are easier
  
  imap_close($imap); 
  return $message;
}

function account_exists($auth_user, $accountid)
{
  // does this user have an account with this id?
  
  $query = "select count(*) from accounts where username = '$auth_user' and accountid = $accountid";
  if($conn=db_connect())
  {
    $result = $conn->query($query);
    $row = $result->fetch_array();
    return $row[0];
  }
  return false;
}

function get_account_list($auth_user)
{
  // get an array of the account ids that belong to this user

  $query = "select accountid from accounts where username = '$auth_user'";
  $list = array();
  
  if($conn=db_connect())
  {
    $result = $conn->query($query);
    $num = $result->num_rows;
    for($i = 0; $i<$num; $i++)
    {
      $row = $result->fetch_array();
      array_push($list, $row[0]);
    }
  }
  return $list;
}

function number_of_accounts($auth_user)
{
  // get the number of accounts that belong to this user
  
  $query = "select count(*) from accounts where username = '$auth_user'";

  if($conn=db_connect())
  {
    $result = $conn->query($query);
      if($result)
      {
        $row = $result->fetch_array();
        return $row[0];
      }
  }
  return 0;
}

function delete_account($auth_user, $accountid)
{
  //delete one of this user's accounts from the DB

  $query = "delete from accounts where accountid='$accountid' and username = '$auth_user'";
  if($conn=db_connect())
  {
    $result = $conn->query($query);
  }
  return $result;
}

function get_account_settings($auth_user, $accountid=0)
{
  //get an array containing the settings for this account
  
  $settings = array();
  if($conn=db_connect())
  {
    if($accountid>0 && $accountid!='')
    {
      $query = "select * from accounts where accountid=$accountid and username = '$auth_user'";
    }
    else if (number_of_accounts($auth_user)==1)
    {
      $query = "select * from accounts where username = '$auth_user'";
    }
    else
    {
      return false;
    }
    $result = $conn->query($query);
    if($result)
    {
      $settings = $result->fetch_assoc();
      return $settings;
    }
    else
      return false;
  }
  return false;
}

function get_accounts($auth_user)
{
  $list = array();
  if($conn=db_connect())
  {
    $query = "select * from accounts where username = '$auth_user'";
    $result = $conn->query($query);
    if($result)
    {
      while($settings = $result->fetch_assoc())
        array_push($list, $settings);
    }
    else
      return false;
  }
  return $list;
}


function store_account_settings($auth_user, $settings)
{
  if(!filled_out($settings))
  {
    echo 'All fields must be filled in.  Try again.<br /><br />';
    return false;
  }
  else
  {
    if($settings['account']>0)
      $query = "update accounts  set server = '$settings[server]',
                  port = $settings[port], type = '$settings[type]',
                  remoteuser = '$settings[remoteuser]', 
                  remotepassword = '$settings[remotepassword]' 
                where accountid = $settings[account] 
                  and username = '$auth_user'";
    else
      $query = "insert into accounts values ('$auth_user', 
                     '$settings[server]', $settings[port], 
                     '$settings[type]', '$settings[remoteuser]', 
                     '$settings[remotepassword]', NULL)";
    if($conn=db_connect())
    {
      $result=$conn->query($query);
      if ($result)
        return true;
      else
        return false;
    }
    else
    {
      echo 'could not store changes.<br /><br /><br /><br /><br /><br />';
      return false;
    }
  }
}

function delete_message($auth_user, $accountid, $message_id)
{
  // delete a single message from the server
  
  $imap = open_mailbox($auth_user, $accountid);
  if($imap)
  {
    imap_delete($imap, $message_id);
    imap_expunge($imap);
    imap_close($imap);
    return true;
  }
  return false;
}


function open_mailbox($auth_user, $accountid)
{

  // select mailbox if there is only one
  if(number_of_accounts($auth_user)==1)
  {
    $accounts = get_account_list($auth_user); 
    $_SESSION['selected_account'] = $accounts[0]; 
    $accountid = $accounts[0];
  }

  // connect to the POP3 or IMAP server the user has selected
  $settings = get_account_settings($auth_user, $accountid);
  if(!sizeof($settings)) return 0;
  $mailbox = '{'.$settings[server];
  if($settings[type]=='POP3')
    $mailbox .= '/pop3';
  
  $mailbox .= ':'.$settings[port].'}INBOX';

  // suppress warning, remember to check return value  
@ $imap = imap_open($mailbox, $settings['remoteuser'], $settings['remotepassword']);

  return  $imap;
}


function get_list($imap)
{
  // get the list of messages in this mailbox
  $headers = imap_headers($imap);
  $messages = sizeof($headers);
  for($i = 0; $i<$messages; $i++)
  {
    echo $headers[$i];
  }
  imap_close($imap); 
}

function send_message($to, $cc, $subject, $message)
{
  // send one email via PHP
  
  if (!$conn=db_connect())
  { 
    return false;
  }
  $query = 'select address from users where username=\''.$_SESSION['auth_user']."'";
  
  $result = $conn->query($query);
  if (!$result)
  {
    return false;  
  }
  else if ($result->num_rows==0)
  {
    return false; 
  }
  else
  {
    $row = $result->fetch_object();
    $other = 'From: '.$row->address;
    if (!empty($cc))
       $other.="\r\nCc: $cc";
    if (mail($to, $subject, $message, $other))
    {
      return true;
    }
    else
    {
      return false;
    }
  }
}

function add_quoting($string, $pattern = '> ')
{
  // add a quoting pattern to mark text quoted in your reply
  return $pattern.str_replace("\n", "\n$pattern", $string);
}


Link to comment
https://forums.phpfreaks.com/topic/144011-php-error-help/#findComment-755659
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.