<?php

class adminUtility extends MasterClass {
	
####################################################################################################################################################	
####################################################################################################################################################	

//function for adding a new administrator
public function insertAdmin(){
    
    if($this->checkRef()){

        //first clean the data
        if($data = $this->cleanData()){

            //get the timestamp
            $timeStamp = time();

            //query the database and see if this admin has already been entered via the email address
            $this->query = "SELECT admin_email FROM Admin WHERE admin_email = '".$data['Email']."'";            

            if($this->dbConn()){

                $check = $this->db->query($this->query);

                if($check->num_rows == 0){

                    //data is clean. Next create the insert query;
                    $this->query = "INSERT INTO Admin VALUES (";
                    $this->query .= "'','".$data['First_Name']."','";
                    $this->query .= $data['Last_Name']."','";
                    $this->query .= $data['Title']."','";
                    $this->query .= $data['Permission']."','";
                    $this->query .= $data['Email']."','";
                    $this->query .= sha1($data['Password'])."','";
                    $this->query .= $timeStamp."','1')";

                }else{

                    //an admin already exists by that particular email address. stop insertion and display the error
                    $result['Msg'] = 'There is already an administrator using that particular email address.<br/>';
                    $result['Msg'] .= '<ol>';
                    $result['Msg'] .= '<li>You can use the "Delete Administrator" tool to delete the admin using this email address before inserting this new admin.</li>';
                    $result['Msg'] .= '<li>You can use the "Modify Administrator" tool to change the email address of the administrator using this email address before inserting the new admin</li>';
                    $result['Msg'] .= '</ol>';
                    $result['Msg'] .= '* You can not have multiple administrators with the same email address due to security issues. Using the password reset tool on the admin login screen resets the password for the admin based on the entered email address. Multiple admin using the same email address will cause the reset tool to reset the password for each of the administrators with the same email address.';
                    $result['Status'] = 0;
                    $result['Class'] = 'error';

                }       

                //insert the new admin
                if($this->db->query($this->query)){

                    $this->dbClose();

                    //insertion was successful.. now email the information                    
                    $timeSt = time();
                    $today = date("m/d/y",$timeSt).' at '.date("g:i a",($timeSt+3600));

                    //now send introduction email
                    $msgTXT = "\n\r\n\r\n\r\n\r";
                    $msgTXT .= 'Your were added as an administrator on  ';
                    $msgTXT .= $today;
                    $msgTXT .= '. You may now login into the '.COMPANY_NAME.' admin panel using your email address ( ';
                    $msgTXT .= stripslashes($data['Email']);
                    $msgTXT .= ' ) and the password ( "';
                    $msgTXT .= stripslashes($data['Password']);
                    $msgTXT .= '" ) .';
                    $msgTXT .= "\n\r";
                    $msgTXT .= 'Link - '.WEBSITE_ADMIN_PANEL."\r\n";
                    $msgTXT .= 'to log into the '.COMPANY_NAME.' admin panel.';
                    $msgTXT .= "\n\r\n\r\n\r\n\r";

                    $msgHTML = '<p>';
                    $msgHTML .= 'A new administrator was was added on ';
                    $msgHTML .= $today;
                    $msgHTML .= '</p><p>You may now login into the '.COMPANY_NAME.' admin panel using your email address ( ';
                    $msgHTML .= stripslashes($data['Email']);
                    $msgHTML .= ' ) and the password ( "';
                    $msgHTML .= stripslashes($data['Password']);
                    $msgHTML .= '" ) .';
                    $msgHTML .= '</p><p>';
                    $msgHTML .= 'Link - '.WEBSITE_ADMIN_PANEL;
                    $msgHTML .= 'to log into the '.COMPANY_NAME.' admin panel.';
                    $msgHTML .= '</p>';                     

                    $subject = 'New Administrator | '.COMPANY_NAME.' Admin Panel';

                    $emailResult = $this->sendEmail($msgHTML,$msgTXT,$subject,$data['Email'],$data['First_Name'].' '.$data['Last_Name']);

                    //admin notification email
                    $msgTXT = "\n\r\n\r\n\r\n\r";
                    $msgTXT .= 'A new administrator was was added on ';
                    $msgTXT .= $today;
                    $msgTXT .= ' by you or someone logged in as you on the '.COMPANY_NAME.' admin panel. The information for the new admin is as follows:';
                    $msgTXT .= "\n\r\n\r";

                    $msgHTML = '<p>';
                    $msgHTML .= 'A new administrator was was added on ';
                    $msgHTML .= $today;
                    $msgHTML .= ' by you or someone logged in as you on the '.COMPANY_NAME.' admin panel. The information for the new admin is as follows:';
                    $msgHTML .= '</p><p>';                     

                    //loop through the informaiton and append it to the message
                    foreach($data as $key => $value){

                        switch($key){

                            case "First_Name":		
                                $msgTXT .= 'First Name'.': '.stripslashes($value)."\n\r";
                                $msgHTML .= 'First Name'.': '.stripslashes($value).'<br/>';
                            break;				
                            case "Last_Name":
                                $msgTXT .= 'Last Name'.': '.stripslashes($value)."\n\r";
                                $msgHTML .= 'Last Name'.': '.stripslashes($value).'<br/>';
                            break;			
                            case "Title":
                                $msgTXT .= 'Title / Position'.': '.stripslashes($value)."\n\r";
                                $msgHTML .= 'Title / Position'.': '.stripslashes($value).'<br/>';
                            break;
                            case "Permission":
                                $msgTXT .= 'Permission Level'.': ';
                                $msgHTML .= 'Permission Level'.': ';
                            if($value == 1){
                                $msgTXT .= 'General Administrator'."\n\r";
                                $msgHTML .= 'General Administrator'.'<br/>';
                            }else if($value == 2){
                                $msgTXT .= 'Master Administrator'."\n\r";
                                $msgHTML .= 'Master Administrator'.'<br/>';
                            }
                            break;
                            case "Email":
                                $msgTXT .= 'Email Address'.': '.stripslashes($value)."\n\r";
                                $msgHTML .= 'Email Address'.': '.stripslashes($value).'<br/>';
                            break;
                            case "Password":
                                $msgTXT .= 'Password'.': '.stripslashes($value)."\n\r";
                                $msgHTML .= 'Password'.': '.stripslashes($value).'<br/>';
                            break;

                        }

                    }
                    $msgHTML .= '</p>';

                    $emailNotificationResult = $this->sendEmail($msgHTML,$msgTXT,$subject);

                    $result['Msg'] = 'Addition of '.stripslashes($data['First_Name']).' '.stripslashes($data['Last_Name']).' as a new administrator was successful. An email confirmation has been sent to ';
                    $result['Msg'] .= 'your email address as well as to ';
                    $result['Msg'] .= $data['First_Name'].' '.$data['Last_Name'].' at the following email address '.$data['Email'];
                    $result['Status'] = 1;
                    $result['Class'] = 'success';

                }else{

                    $result['Msg'] = 'Unfortunately there was a problem with connecting to the MySQL database. Please contact your hosting provider';
                    $result['Status'] = 0;
                    $result['Class'] = 'error';

                }   
                
               $this->dbClose();

            }else{

                $result['Msg'] = 'There was a problem connecting to the MySQL database. Please contact WebDuck Designs at 860-781-6028.';
                $result['Status'] = 0;
                $result['Class'] = 'error';

            }               
        
        }

    return $result;
    exit;

    }

}
	
####################################################################################################################################################	
####################################################################################################################################################	

//the following function is for modify your information
public function modifyYourAccount(){

    if($this->checkRef()){

        $nbr = $_SESSION['adm1n']['Nbr'];

        $data = $this->cleanData();

        if($nbr != ''){

            $this->dbConn();
            
             $this->query = "UPDATE Admin SET ";
             $this->query .= "admin_fname = '";
             $this->query .= $data['First_Name'];
             $this->query .= "', admin_lname = '";
             $this->query .= $data['Last_Name'];
             $this->query .= "', admin_title = '";
             $this->query .= $data['Title'];
             $this->query .= "', admin_email = '";
             $this->query .= $data['Email'];             
             $this->query .= "' WHERE admin_nbr = '";
             $this->query .= $this->db->real_escape_string($nbr);
             $this->query .= "'";

             $this->db->query($this->query);

             $result['Msg'] = 'Your information has been successfully updated.';
             $result['Class'] = 'success';
             $result['Status'] = 1;
             
             $this->dbClose();

        }else{

             $result['Msg'] = 'Nobody is logged into the system.';
             $result['Class'] = 'error';
             $result['Status'] = 0;                            

        }

        return $result;

    }

} 

####################################################################################################################################################	
####################################################################################################################################################	

//the following function is for modify the information of an administrator
public function modifyAdmin(){
    
    if($this->checkRef()){

        $data = $this->cleanData();

        if($data['Id'] != ''){

            $this->dbConn();
            
             $this->query = "UPDATE Admin SET ";
             $this->query .= "admin_fname = '";
             $this->query .= $data['First_Name'];
             $this->query .= "', admin_lname = '";
             $this->query .= $data['Last_Name'];
             $this->query .= "', admin_title = '";
             $this->query .= $data['Title'];
             $this->query .= "', admin_permission = '";
             $this->query .= $data['Permission'];             
             $this->query .= "', admin_email = '";
             $this->query .= $data['Email'];                
             $this->query .= "', admin_status = '";
             $this->query .= $data['Status'];                
             $this->query .= "' WHERE admin_nbr = '";
             $this->query .= $this->db->real_escape_string($data['Id']);
             $this->query .= "'";

             $this->db->query($this->query);

             $result['Msg'] = 'Your administrator\'s information has been successfully updated.';
             $result['Class'] = 'success';
             $result['Status'] = 1;
             
             $this->dbClose();

        }else{

             $result['Msg'] = 'You cannot update an administrator without first selecting an administrator to update.';
             $result['Class'] = 'error';
             $result['Status'] = 0;                            

        }

        return $result;

    }

} 

####################################################################################################################################################	
####################################################################################################################################################	

public function getAdminInfo($id){

    if($this->checkRef()){
        
        $info = array();

        //make sure a id for an administrator is set
        if(isset($id)){

               $this->dbConn();

               //create the USERial query for the information 
               $this->query = "SELECT * FROM Admin WHERE admin_nbr = '";
               $adminId = $this->db->real_escape_string($id);
               $this->query .= $adminId."'";
               
               $getInfo = $this->db->query($this->query);

               if($getInfo->num_rows > 0){

                      $row = $getInfo->fetch_assoc();
                          
                      $info['Nbr'] = stripslashes($row['admin_nbr']);
                      $info['First_Name'] = stripslashes($row['admin_fname']);
                      $info['Last_Name'] = stripslashes($row['admin_lname']);
                      $info['Title'] = stripslashes($row['admin_title']);
                      $info['Permission'] = stripslashes($row['admin_permission']);
                      $info['Email'] = stripslashes($row['admin_email']);
                      $info['Status'] = stripslashes($row['admin_status']);

               }
               
               $this->dbClose();

        }

        return $info;
        
    }

}

####################################################################################################################################################	
####################################################################################################################################################	

//main function to handle the admin session
public function loginAdmin(){

    $result = array();

     //admin is attempting to login
     //clean the data
     if($data = $this->cleanData()){           

         $this->query = "SELECT * FROM Admin WHERE admin_email = '";
         $this->query .= $data['Email'];
         $this->query .= "' AND admin_pass = '";
         $this->query .= sha1($data['Password']);
         $this->query .= "'";

         if($this->dbConn()){

             //database is connected.. check to see if the administrator exists
             $check = $this->db->query($this->query);
             $this->dbClose();

             if($check->num_rows < 1){

                 //administrator does not exist or something is wrong with the data entered
                 $result['Msg'] = 'Please check the password and email address you entered. The information you supplied is not on file. If you have forgotten your password, you can use the password reset tool by choosing the link at the bottom of this page. If do not remember your email address or do not have access to the email address you used for your admin account, then contact WebDuck Designs at 612.708.4135';
                 $result['Status'] = 0;
                 $result['Class'] = 'error';                                                                     

             }else{

                 //everything checks out... do it.. do it..
                 //start by making the db result into an array
                 $dataInfo = $check->fetch_assoc();

                 //make sure the admin is active
                 if($dataInfo['admin_status'] == 1){

                     $adminData = array();
                     $adminData['Name'] = stripslashes($dataInfo['admin_fname']).' '.stripslashes($dataInfo['admin_lname']);
                     $adminData['Title'] = stripslashes($dataInfo['admin_title']);
                     $adminData['Permission'] = stripslashes($dataInfo['admin_permission']);
                     $adminData['Status'] = stripslashes($dataInfo['admin_status']);
                     $adminData['Nbr'] = stripslashes($dataInfo['admin_nbr']);
                     $adminData['Email'] = stripslashes($dataInfo['admin_email']);

                     if(!isset($_SESSION)){

                         session_start();

                     }	

                     $_SESSION['adm1n'] = $adminData; 

                     $result['Status'] = 1;
                     $result['ContinuePage'] = 'index.php';  

                 }else{

                      $result['Msg'] = 'Sorry, but your status as an administrator is SUSPENDED. Please contact the main site administrator.';
                      $result['Status'] = 0;
                      $result['Class'] = 'error';     

                 }

             }

         }else{

             $result['Msg'] = 'Could not connect to the database. The database may be down. Please try again in a couple hours. If the problem persists, contact WebDuck Designs at 860.781.6028';
             $result['Status'] = 0;
             $result['Class'] = 'error';     		

         }

     }else{

         $result = 'Data could not be cleaned. Something is wrong';
         $result['Status'] = 0;
         $result['Class'] = 'error';     

     }

     return $result;


}

####################################################################################################################################################	
####################################################################################################################################################	

//function to handle the password reset
public function resetPass(){
    
    if($this->checkRef()){

    //first clean the data then create the query / database base connection
    $data = $this->cleanData();

    //create the query using the clean data
    $this->query = "SELECT admin_nbr, admin_email FROM Admin WHERE admin_email = '";
    $this->query .= $data['Email'];
    $this->query .= "'";

    //perform the query to check and see if the actual email address exists
    if($this->dbConn()){

        $check = $this->db->query($this->query);

        if($check->num_rows < 1){

            //no admin is using this email address... Return the error
            $result['Msg'] = 'There does not appear to be any administrators that use '.stripslashes($data['Email']).' as an email address. Please make sure the email address is correct and try again. ';
            $result['Msg'] .= 'If you are sure that this is the correct email address then contact WebDuck Designs';
            $result['Status'] = 0;
            $result['Class'] = 'error';

        }else{

            $newPass = $this->genPass();

            $this->query = "UPDATE Admin SET admin_pass = '";
            $this->query .= sha1($newPass);
            $this->query .= "' WHERE admin_email = '";
            $this->query .= $data['Email'];
            $this->query .= "'";

            if($this->db->query($this->query)){

                $timeSt = time();
                $today = date("m/d/y",$timeSt).' at '.date("g:i a",($timeSt+3600)).' Central Time ';

                $msgTXT = "\n\r\n\r\n\r\n\r";
                $msgTXT .= 'A request was made on ';
                $msgTXT .= $today;
                $msgTXT .= ' to reset your password for the '.COMPANY_NAME.' admin panel. If you did not authorize this change then notify the main website administrator. ';
                $msgTXT .= "\n\r";
                $msgTXT .= 'Your password for the '.COMPANY_NAME.' admin panel was successfully reset. To log into the '.COMPANY_NAME.' admin panel, go to the following link and enter your email address and your new password which is ';
                $msgTXT .= '"'.$newPass.'" ';
                $msgTXT .= "\n\r";
                $msgTXT .= 'Link - '.WEBSITE_ADMIN_PANEL.' ';
                $msgTXT .= ' to log into the '.COMPANY_NAME.' admin panel with your new password';
                $msgTXT .= "\n\r\n\r\n\r\n\r";

                $msgHTML = "<p>";
                $msgHTML .= 'A request was made on ';
                $msgHTML .= $today;
                $msgHTML .= ' to reset your password for the '.COMPANY_NAME.' admin panel. If you did not authorize this change then notify the main website administrator. ';
                $msgHTML .= "</p><p>";
                $msgHTML .= 'Your password for the '.COMPANY_NAME.' admin panel was successfully reset. To log into the '.COMPANY_NAME.' admin panel, go to the following link and enter your email address and your new password which is ';
                $msgHTML .= '"'.$newPass.'" ';
                $msgHTML .= "</p><p>";
                $msgHTML .= 'Link - '.WEBSITE_ADMIN_PANEL.' ';
                $msgHTML .= ' to log into the '.COMPANY_NAME.' admin panel with your new password';
                $msgHTML .= "</p>";                                   

                $subject = 'Password Reset | '.COMPANY_NAME.' Admin Panel';

                $this->sendEmail($msgHTML,$msgTXT,$subject,$data['Email'],COMPANY_NAME);

                $result['Msg'] = 'Your new password has been sent to your email address. If you do not receive your email within the next hour then check your spam folders. It is common for emails being sent via a ';
                $result['Msg'] .= 'web application be labeled as spam email. If you have not received the email with your new password and you can not find it in your spam folders than contact WebDuck Designs.';
                $result['Msg'] .= '<p>&nbsp;</p><p><a href="login.php">CLICK HERE</a> to return to the Login screen.</p>';
                //$result['Msg'] .= '<p>New Password: '.$newPass.'</p>';
                $result['Status'] = 1;
                $result['Class'] = 'success';

            }

        }
        $this->dbClose();

    }

    return $result;
    }
    
}

####################################################################################################################################################	
####################################################################################################################################################	

//function to change your password
public function changePassword(){

    if($this->checkRef()){

        $nbr = $_SESSION['adm1n']['Nbr'];

        if($nbr != ''){

            $data = $this->cleanData();

            if($data['New_Password'] != '' && $data['New_Password_Confirm'] != '' && ($data['New_Password'] == $data['New_Password_Confirm'])){

                $this->dbConn();

                $this->query = "SELECT * FROM Admin WHERE admin_nbr = '";
                $this->query .= $this->db->real_escape_string($nbr);
                $this->query .= "' AND admin_pass = '";
                $this->query .= sha1($data['Current_Password']);
                $this->query .= "'";

                $check = $this->db->query($this->query);

                if($check->num_rows > 0){

                    $this->query = "UPDATE Admin SET ";
                    $this->query .= "admin_pass = '";
                    $this->query .= sha1($data['New_Password']);
                    $this->query .= "' WHERE admin_nbr = '";
                    $this->query .= $this->db->real_escape_string($nbr);
                    $this->query .= "'";

                    $this->db->query($this->query);

                    $result['Msg'] = 'You have successfully changed your password. The new password is effective immediately';
                    $result['Class'] = 'success';
                    $result['Status'] = '1';                                           

                }else{

                    $result['Msg'] = 'The current password you entered is incorrect.';
                    $result['Class'] = 'error';
                    $result['Status'] = 0;                                    

                }

                $this->dbClose();

            }else{

                $result['Msg'] = 'The new password and new password confirm fields need to match before submitting your information.';
                $result['Class'] = 'error';
                $result['Status'] = 0;                                    

            }

        }else{

            $result['Msg'] = 'Nobody is logged into the system.';
            $result['Class'] = 'error';
            $result['Status'] = 0;                          

        }

        return $result;

    }

}

###################################################################################################################################################	
###################################################################################################################################################	

//function for getting list of admin options
public function getAdminOptions($all=false){

    if($this->checkRef()){    

        $list = array();

        $this->dbConn();

        $this->query = "SELECT * FROM Admin_Options LEFT JOIN Admin_Categories ON admin_option_category = admin_category_nbr WHERE admin_option_category = '2' ";
        if($all === false){

            $this->query .= "AND admin_option_visible = '1' ";

        }
        $this->query .= "ORDER BY admin_option_nbr ASC";                  

        $getInfo = $this->db->query($this->query);

        if($getInfo->num_rows > 0){

            $i=0;
            while($row = $getInfo->fetch_assoc()){

                $list[$i]['Nbr'] = stripslashes($row['admin_option_nbr']);
                $list[$i]['Category'] = stripslashes($row['admin_option_category']);
                $list[$i]['Category_Name'] = stripslashes($row['admin_category_name']);
                $list[$i]['Directory'] = stripslashes($row['admin_category_directory']);                   
                $list[$i]['Name'] = stripslashes($row['admin_option_name']);
                $list[$i]['Desc'] = stripslashes($row['admin_option_description']);
                $list[$i]['Page'] = stripslashes($row['admin_option_page']);
                $list[$i]['Visible'] = stripslashes($row['admin_option_visible']);
                $i++;

            }

        }

        $this->dbClose();

        return $list;

    }
 
} 

###################################################################################################################################################	
###################################################################################################################################################	

//function for getting list of administrators
public function getAdministrators(){
    
    $info = array();
    
    if($this->checkRef()){
    
        $this->dbConn();
        
        $this->query = "SELECT * FROM Admin WHERE admin_nbr != '";
        $this->query .= $_SESSION['adm1n']['Nbr'];
        $this->query .= "' ORDER BY admin_lname ASC, admin_fname ASC";
        
        $getInfo = $this->db->query($this->query);
        
        if($getInfo->num_rows > 0){
            
           $i=0;
           while($row = $getInfo->fetch_assoc()){
               
               $info[$i]['Nbr'] = stripslashes($row['admin_nbr']);
               $info[$i]['First_Name'] = stripslashes($row['admin_fname']);
               $info[$i]['Last_Name'] = stripslashes($row['admin_lname']);
               $info[$i]['Title'] = stripslashes($row['admin_title']);
               $info[$i]['Permission'] = stripslashes($row['admin_permission']);
               $info[$i]['Email'] = stripslashes($row['admin_email']);
               $info[$i]['Status'] = stripslashes($row['admin_status']);
               $i++;
               
           }
       
        }
            
        $this->dbClose();
        
    }
    
    return $info;
    
}

###################################################################################################################################################	
###################################################################################################################################################	

//function for deleting an administrator
public function deleteAdmin(){
            
    if($this->checkRef()){
        
        $data = $this->cleanData();
        
        if($data['Id'] != ''){
            
            if($data['Id'] != $_SESSION['adm1n']['Nbr']){
            
                $this->dbConn();

                $this->query = "DELETE FROM Admin WHERE admin_nbr = '";
                $this->query .= $data['Id'];
                $this->query .= "'";
                
                $this->db->query($this->query);

                $this->dbClose();

                $result['Status'] = 1;
                $result['Class'] = 'success';
                $result['Msg'] = 'The administrator has been successfully deleted.';  
                
            }else{
                
                $result['Status'] = 0;
                $result['Class'] = 'error';
                $result['Msg'] = 'You cannot delete yourself.';                
                
            }
            
        }else{
            
            $result['Status'] = 0;
            $result['Class'] = 'error';
            $result['Msg'] = 'No administrator was selected.';
            
        }
        
    }
    
    return $result;
    
}

####################################################################################################################################################	
####################################################################################################################################################	

//function to change administrators password
public function changeAdminPassword(){

    if($this->checkRef()){

        $data = $this->cleanData();
        
        if($data['Id'] != ''){            

            if($data['New_Password'] != '' && $data['New_Password_Confirm'] != '' && ($data['New_Password'] == $data['New_Password_Confirm'])){

                $this->dbConn();

                $this->query = "SELECT * FROM Admin WHERE admin_nbr = '";
                $this->query .= $this->db->real_escape_string($data['Id']);
                $this->query .= "' AND admin_pass = '";
                $this->query .= sha1($data['Current_Password']);
                $this->query .= "'";

                $check = $this->db->query($this->query);

                if($check->num_rows > 0){

                    $this->query = "UPDATE Admin SET ";
                    $this->query .= "admin_pass = '";
                    $this->query .= sha1($data['New_Password']);
                    $this->query .= "' WHERE admin_nbr = '";
                    $this->query .= $this->db->real_escape_string($data['Id']);
                    $this->query .= "'";

                    $this->db->query($this->query);

                    $result['Msg'] = 'You have successfully changed your password. The new password is effective immediately';
                    $result['Class'] = 'success';
                    $result['Status'] = '1';                                           

                }else{

                    $result['Msg'] = 'The current password you entered is incorrect.';
                    $result['Class'] = 'error';
                    $result['Status'] = 0;                                    

                }

                $this->dbClose();

            }else{

                $result['Msg'] = 'The new password and new password confirm fields need to match before submitting your information.';
                $result['Class'] = 'error';
                $result['Status'] = 0;                                    

            }

        }else{

            $result['Msg'] = 'Nobody is logged into the system.';
            $result['Class'] = 'error';
            $result['Status'] = 0;                          

        }

        return $result;

    }

}


}

?>