Ticket #238: inpatch.patch

File inpatch.patch, 12.2 kB (added by dmorton, 6 years ago)

first running draft

  • cache.php

     
    227227      $reported = 0; 
    228228      $deleted = 0; 
    229229      $rescued = 0; 
     230       
     231      $ham_list = array(); 
     232      $spam_list = array(); 
     233      $delete_list = array(); 
     234       
    230235      global $_POST, $lang, $logger; 
    231236      $items = $_POST['cache_item']; 
    232237      foreach ($items as $type => $mail_item) { 
     
    240245          if ($newtype == "spam") { 
    241246            switch ($this->type) { 
    242247              case 'ham': 
    243                 // Mark the item as Confirmed Spam 
    244                 $update = "UPDATE maia_mail_recipients SET type = 'C' " . 
    245                           "WHERE recipient_id = ? AND mail_id = ?"; 
    246                 $this->dbh->query($update, array($euid, $mail_id)); 
     248                // Mark the item as false negative.   It will also be marked as confirmed.                
    247249                record_mail_stats($euid, $mail_id, "fn"); 
    248250                $reported++; 
    249251                break; 
    250               case 'spam': 
    251                 confirm_spam($euid, $mail_id); 
     252              default: 
    252253                $confirmed++; 
    253               default: 
    254254            } 
     255            array_push($spam_list, $mail_id); 
     256             
    255257          //send item 
    256258          } elseif ($newtype == "ham") { 
    257259            switch ($this->type) { 
    258260              case 'ham': 
    259               $logger->debug('confirming ' . $mail_id . ' as ham'); 
    260                 confirm_ham($euid, $mail_id); 
     261                  array_push($ham_list, $mail_id); 
    261262                $confirmed++; 
    262263                break; 
    263264              default: 
    264                 $result = rescue_item($euid, $mail_id); 
     265                $result = rescue_item($euid, $mail_id); // done individually because of mail delivery 
    265266                if (strncmp($result, "2", 1) == 0) { 
    266267                  $rescued++; 
    267268                } else { 
     
    270271            }               
    271272          //delete item. 
    272273          } elseif ($newtype == "delete") { 
    273               $logger->debug('deleting ' . $mail_id); 
    274  
    275           delete_mail_reference($euid, $mail_id); 
     274              array_push($delete_list, $mail_id); 
    276275              $deleted++; 
    277276          } 
    278277        } 
    279278      } 
     279       
     280      if (count($ham_list) > 0)      { confirm_ham($euid, $ham_list );   } 
     281      if (count($spam_list) > 0)    { confirm_spam($euid, $spam_list);   } 
     282      if (count($delete_list) > 0 ) { delete_mail_reference($euid, $delete_list); } 
    280283 
    281284      update_mail_stats($euid, "suspected_ham"); 
    282285      if ($confirmed > 0) { 
  • maia_db.php

     
    16761676 
    16771677 
    16781678    /* 
    1679      * delete_mail(): Deletes a single mail item and all references 
    1680      *                to it. 
     1679     * delete_mail(): Deletes mail items and all references 
     1680     *                to them. 
    16811681     */ 
    1682     function delete_mail($mail_id) 
     1682    function delete_mail($mail_ids) 
    16831683    { 
    16841684        global $dbh; 
    16851685 
    16861686        // Delete any references to recipients 
    1687         $delete = "DELETE FROM maia_mail_recipients WHERE mail_id = ?"; 
    1688         $dbh->query($delete, array($mail_id)); 
     1687        $delete = "DELETE FROM maia_mail_recipients WHERE mail_id IN (?" . str_repeat(',?', count($mail_ids) - 1) . ")"; 
     1688        $dbh->query($delete, $mail_ids); 
    16891689 
    16901690        // Delete any references to SpamAssassin rules 
    1691         $delete = "DELETE FROM maia_sa_rules_triggered WHERE mail_id = ?"; 
    1692         $dbh->query($delete, array($mail_id)); 
     1691        $delete = "DELETE FROM maia_sa_rules_triggered WHERE mail_id IN (?" . str_repeat(',?', count($mail_ids) - 1) . ")"; 
     1692        $dbh->query($delete, $mail_ids); 
    16931693 
    16941694        // Delete any references to viruses 
    1695         $delete = "DELETE FROM maia_viruses_detected WHERE mail_id = ?"; 
    1696         $dbh->query($delete, array($mail_id)); 
     1695        $delete = "DELETE FROM maia_viruses_detected WHERE mail_id IN (?" . str_repeat(',?', count($mail_ids) - 1) . ")"; 
     1696        $dbh->query($delete, $mail_ids); 
    16971697 
    16981698        // Delete any references to banned file attachments 
    1699         $delete = "DELETE FROM maia_banned_attachments_found WHERE mail_id = ?"; 
    1700         $dbh->query($delete, array($mail_id)); 
     1699        $delete = "DELETE FROM maia_banned_attachments_found WHERE mail_id IN (?" . str_repeat(',?', count($mail_ids) - 1) . ")"; 
     1700        $dbh->query($delete, $mail_ids); 
    17011701 
    17021702        // Delete the mail item itself 
    1703         $delete = "DELETE FROM maia_mail WHERE id = ?"; 
    1704         $dbh->query($delete, array($mail_id)); 
     1703        $delete = "DELETE FROM maia_mail WHERE id IN (?" . str_repeat(',?', count($mail_ids) - 1) . ")"; 
     1704        $dbh->query($delete, $mail_ids); 
    17051705    } 
    17061706 
    17071707 
    17081708    /* 
    1709      * delete_mail_references(): Deletes a single mail item's recipient 
     1709     * delete_mail_references(): Deletes mail items' recipient 
    17101710     *                           references. 
    17111711     */ 
    1712     function delete_mail_reference($user_id, $mail_id) 
     1712    function delete_mail_reference($user_id, $mail_ids) 
    17131713    { 
    17141714        global $dbh; 
     1715        global $logger; 
    17151716 
    1716         // Delete a specific user's recipient reference to this mail. 
    1717         $delete = "DELETE FROM maia_mail_recipients WHERE recipient_id = ? AND mail_id = ?"; 
    1718         $dbh->query($delete, array($user_id, $mail_id)); 
     1717        // Delete a specific users' recipient reference to these mail messages. 
     1718        $delete = "DELETE FROM maia_mail_recipients WHERE recipient_id = ? AND mail_id IN ("; 
     1719        $delete .= '?' . str_repeat(',?', count($mail_ids) - 1);         
     1720        $delete .= ")"; 
    17191721 
    1720         // If there are no other recipients referenced by this mail item, 
     1722        $dbh->query($delete, array_merge($user_id, $mail_ids)); 
     1723 
     1724        // If there are no other recipients referenced by each mail item, 
    17211725        // delete it. 
    1722         $select = "SELECT recipient_id FROM maia_mail_recipients WHERE mail_id = ?"; 
    1723         $sth = $dbh->query($select, array($mail_id)); 
    1724         if (!$sth->fetchRow()) { 
    1725             delete_mail($mail_id); 
     1726 
     1727        $select = "SELECT recipient_id, mail_id, maia_mail.id ". 
     1728        "FROM      maia_mail " . 
     1729        "LEFT JOIN maia_mail_recipients " .  
     1730               "ON maia_mail.id = maia_mail_recipients.mail_id " . 
     1731        "WHERE maia_mail.id IN (?" . str_repeat(',?', count($mail_ids) - 1) .") " . 
     1732          "AND mail_id IS NULL"; 
     1733        $sth = $dbh->query($select, $mail_ids); 
     1734        if (DB::isError($sth)) { 
     1735 
     1736            $logger->err('Standard Message: ' . $sth->getMessage() . $select); 
     1737            exit; 
    17261738        } 
    1727         $sth->free(); 
     1739        $deletions = array(); 
     1740        foreach ($mail_ids as $mail_id) { 
     1741            while ($row = $sth->fetchrow() ) { 
     1742                array_push($deletions, $row['id']); 
     1743            } 
     1744            $sth->free(); 
     1745        } 
     1746        delete_mail($deletions); 
     1747 
    17281748    } 
    17291749 
    17301750 
     
    18051825        } 
    18061826        $sth->free(); 
    18071827    } 
     1828     
     1829    function get_database_type($dbh) 
     1830    { 
     1831        return $dbh->phptype; 
     1832    } 
    18081833 
    18091834 
     1835    /* update the confirmation settings on one or multiple items.   */ 
     1836    function set_item_confirmations($message_type, $user_id, $mail_id) { 
     1837        global $dbh; 
     1838        if (get_database_type($dbh) == "mysql") { 
     1839            $token_code = "CONCAT('expired', recipient_id, mail_id)"; 
     1840        } else { 
     1841            $token_code = '"expired" || recipient_id || mail_id'; 
     1842        } 
     1843        $update = "UPDATE maia_mail_recipients SET type = ?, token=" . $token_code  . 
     1844                " WHERE recipient_id = ? AND mail_id IN ("; 
     1845                 
     1846        $update .= '?' . str_repeat(',?', count($mail_id) - 1); 
     1847         
     1848        $update .= ")"; 
     1849        $dbh->query($update, array_merge($message_type, $user_id, $mail_id));        
     1850    } 
     1851 
    18101852     /* 
    1811      * report_spam(): Confirm a mail item as false negative spam for one recipient. 
    1812      */ 
     1853      * report_spam(): Confirm mail item(s) as false negative spam.    
     1854             mail_id can be a scalar or an array 
     1855      */ 
    18131856    function report_spam($user_id, $mail_id) 
    18141857    { 
    18151858        global $dbh; 
    18161859 
    1817         $expired_token = 'expired' . $user_id . $mail_id; 
    1818  
    1819         // Mark the item as Confirmed Spam 
    1820         $update = "UPDATE maia_mail_recipients SET type = 'C', token=? " . 
    1821                   "WHERE recipient_id = ? AND mail_id = ?"; 
    1822         $dbh->query($update, array($expired_token, $user_id, $mail_id)); 
    1823  
    1824         // Add the item to the Spam stats columns 
     1860        // Mark the items as Confirmed Spam 
     1861        confirm_spam($user_id, $mail_id); 
     1862             
     1863        // Also add the items to the Spam stats columns 
    18251864        record_mail_stats($user_id, $mail_id, "fn"); 
    18261865 
    18271866    } 
    18281867 
    18291868    /* 
    1830      * confirm_spam(): Confirm a mail item as spam for one recipient. 
     1869     * confirm_spam(): Confirm mail item(s) as spam. 
     1870                      mail_id can be a scalar or an array 
    18311871     */ 
    18321872    function confirm_spam($user_id, $mail_id) 
    18331873    { 
    18341874        global $dbh; 
    18351875 
    1836         $expired_token = 'expired' . $user_id . $mail_id; 
     1876        // Mark the items as Confirmed Spam 
     1877        set_item_confirmations('C', $user_id, $mail_id); 
    18371878 
    1838         // Mark the item as Confirmed Spam 
    1839         $update = "UPDATE maia_mail_recipients SET type = 'C', token=? " . 
    1840                   "WHERE recipient_id = ? AND mail_id = ?"; 
    1841         $dbh->query($update, array($expired_token, $user_id, $mail_id)); 
    1842  
    1843         // Add the item to the Spam stats columns 
     1879        // Add the items to the Spam stats columns 
    18441880        record_mail_stats($user_id, $mail_id, "spam"); 
    18451881    } 
    18461882 
    18471883 
    18481884    /* 
    1849      * confirm_ham(): Confirm a mail item as ham for one recipient. 
     1885     * confirm_ham(): Confirm mail item(s) as ham. 
     1886                      mail_id can be a scalar or an array 
    18501887     */ 
    1851     function confirm_ham($user_id, $mail_id) 
     1888    function confirm_ham($user_id, $mail_ids) 
    18521889    { 
    18531890        global $dbh; 
    18541891 
    1855         $expired_token = 'expired' . $user_id . $mail_id; 
     1892        // Mark the items as Confirmed Ham 
     1893        set_item_confirmations('G', $user_id, $mail_ids); 
    18561894 
    1857         // Mark the item as Confirmed Ham 
    1858         $update = "UPDATE maia_mail_recipients SET type = 'G', token=? " . 
    1859                   "WHERE recipient_id = ? AND mail_id = ?"; 
    1860         $dbh->query($update, array($expired_token, $user_id, $mail_id)); 
    1861  
    1862         // Add the item to the Ham stats columns 
    1863         record_mail_stats($user_id, $mail_id, "ham"); 
     1895        // Add the items to the Ham stats columns 
     1896        record_mail_stats($user_id, $mail_ids, "ham"); 
    18641897    } 
    18651898 
    18661899 
     
    18721905    { 
    18731906        global $dbh; 
    18741907 
    1875         $expired_token = 'expired' . $user_id . $mail_id; 
    1876  
    18771908        $select = "SELECT sender_email, contents, " . 
    18781909                         "envelope_to, maia_mail_recipients.type " . 
    18791910                  "FROM maia_mail, maia_mail_recipients " . 
     
    19221953                if ($succeeded = (strncmp($smtp_result, "2", 1) == 0)) { 
    19231954                    if ($type == "S") { 
    19241955                        record_mail_stats($user_id, $mail_id, "fp"); 
    1925                         $update = "UPDATE maia_mail_recipients SET type = 'G', token=? " . 
    1926                                   "WHERE recipient_id = ? AND mail_id = ?"; 
    1927                         $dbh->query($update, array($expired_token, $user_id, $mail_id)); 
     1956                        set_item_confirmations('G', $user_id, $mail_id); 
    19281957                        if (get_user_value($user_id, "auto_whitelist") == "Y") { 
    19291958                            add_address_to_wb_list($user_id, $sender_email, "W"); 
    19301959                        } 
     
    20362065 
    20372066 
    20382067    /* 
    2039      * record_mail_stats(): Recalculate the specified user's stats record 
     2068     * record_mail_stats(): Recalculate the specified users' stats records 
    20402069     *                      for items of the specified (static) type, after 
    20412070     *                      suspected [spam|ham] is confirmed. 
    20422071     */ 
    2043     function record_mail_stats($euid, $mail_id, $type) 
     2072    function record_mail_stats($euid, $mail_ids, $type) 
    20442073    { 
    2045         global $dbh; 
    2046  
     2074      global $dbh; 
     2075      foreach ($mail_ids as $mail_id) {   
    20472076        $select = "SELECT received_date, size, score " . 
    20482077                  "FROM maia_mail WHERE id = ?"; 
    20492078        $sth = $dbh->query($select, array($mail_id)); 
     
    21612190            $sth2->free(); 
    21622191        } 
    21632192        $sth->free(); 
     2193      } 
    21642194    } 
    21652195     
    21662196    /*