Reply LinkBack Thread Tools Search this Thread
Old 23-04-2008, 12:03   #41 (permalink)
seen.to
unusual suspect ™
 
seen.to's Avatar
 
Join Date: Jul 2004
Location: DE, USA
Posts: 2,905
Not a function as such. Something I add to combat mail injection spammers. I've since added more but this is the version I have at hand:
PHP Code:
$email preg_replace("/\r/"""$email);
$email preg_replace("/\n/"""$email);
if (
eregi("cc"$email) || eregi("bcc"$email) || 
eregi("to"$email) || eregi("Content-Type"$email)) 
die(
"Sorry, there was a problem with your message being sent. 
Please click <a href=\"javascript:history.go(-1)\">here</a> to try again"
); 
__________________
  Reply With Quote
Old 19-05-2008, 05:13   #42 (permalink)
fiza
Registered User
 
Join Date: May 2008
Posts: 2
First of all thanks . because this thread is very much helpful to learn php functions.
some functions are:-
zip_entry_read() Reads from an open entry in the ZIP file
zip_open() Opens a ZIP file
zip_read() Reads the next entry in a ZIP file
  Reply With Quote
Old 11-07-2008, 01:49   #43 (permalink)
Aakriti
Registered User
 
Join Date: Jul 2008
Posts: 15
1)zip_close() to close zip file
2)zip_entry_read() Reads from an open entry in the ZIP file
3)zip_entry_filesize() Returns the actual file size of an entry in the ZIP file
  Reply With Quote
Old 11-07-2008, 09:33   #44 (permalink)
Cborrow
I like code.
 
Join Date: Dec 2004
Location: Chesapeake, VA
Posts: 229
Send a message via AIM to Cborrow
Simple class for sending email, with / without attachments.

PHP Code:
<?php
    
class SimpleMail {
        public 
$to;
        public 
$from;
        public 
$fromName;
        public 
$subject;
        public 
$cc;
        public 
$bcc;
        public 
$message;
        
        private 
$uid;
        private 
$mailer;
        private 
$attachments;
        
        public function 
__construct($to null$from null$subject null$message null) {
            if(!empty(
$to) && !empty($from)) {
                
$this->to $to;
                
$this->from $from;
                
                if(!empty(
$subject)) {
                    
$this->subject $subject;
                }
                if(!empty(
$message)) {
                    
$this->message $message;
                }
            }
            
            
$this->mailer "PHP " phpversion();
            
$this->attachments = array();
        }
        
        public function 
SetMailer($mailer) {
            if(!empty(
$mailer) && is_string($mailer)) {
                
$this->mailer $mailer;
            }
        }
        
        public function 
SetTo($to) {
            if(!empty(
$to) && is_string($to)) {
                
$this->to $to;
            }
        }
        
        public function 
SetFrom($from) {
            if(!empty(
$from) && is_string($from)) {
                
$this->from $from;
            }
        }
        
        public function 
SetSubject($subject) {
            if(!empty(
$subject) && is_string($subject)) {
                
$this->subject $subject;
            }
        }
        
        public function 
AddCC($email) {
            if(!empty(
$email) && is_string($email)) {
                if(
strlen($this->cc) > 0) {
                    
$this->cc .= ",".$email;
                }
                else {
                    
$this->cc $email;
                }
            }
        }
        
        public function 
AddBCC($email) {
            if(!empty(
$email) && is_string($email)) {
                if(
strlen($this->bcc) > 0) {
                    
$this->bcc .= ",".$email;
                }
                else {
                    
$this->bcc $email;
                }
            }
        }
        
        public function 
AddMessage($message) {
            if(!empty(
$message) && is_string($message)) {
                
$this->message $message;
            }
        }
        
        public function 
AddAttachment($attachment) {
            if(
file_exists($attachment)) {
                
$attachmentInfo = array();
                
                
$this->uid "==Multipart_Boundary_x{" md5(uniqid(time())) . "}x";
                
$attachmentInfo['Attachment'] = $attachment;
                
$attachmentInfo['Size'] = filesize($attachment);
                
                
$fp fopen($attachment"rb");
                
flock($fpLOCK_SH);
                
$data fread($fpfilesize($attachment) + 1);
                
flock($fpLOCK_UN);
                
fclose($fp);
                
                
$attachmentInfo['Content'] = chunk_split(base64_encode($data), 64);
                
                
$extStart strrpos($this->attachment".");
                
$attachmentInfo['Extension'] = substr($attachmentInfo['Attachment'], $extStart, (strlen($attachmentInfo['Attachment']) - $extStart));
                
$attachmentInfo['Name'] = self::GenerateRandomFilename($attachmentInfo['Extension']);
                
$attachmentInfo['Mime'] = "application/octet-stream; name=\"{$attachmentInfo['Name']}\"\r\n\r\n";
                
                
$this->attachments[] = $attachmentInfo;
            }
        }
        
        public function 
SendMessage($to null$from null$subject null) {
            if(empty(
$to) && !empty($this->to)) { $to $this->to; }
            if(empty(
$from) && !empty($this->from)) { $from $this->from; }
            if(empty(
$subject) && !empty($this->subject)) { $subject $this->subject; }
            if(empty(
$to) || empty($from) || empty($subject)) { return false; }
            
            
$headers "";
            
$headers.= "From: {$this->fromName} <{$from}>\r\n";
            
$headers.= "Reply-To: {$to}\r\n";
            
$headers.= "Subject: {$subject}\r\n";
            
$headers.= "X-Mailer: {$this->mailer}\r\n";
            
$headers.= "MIME-Version: 1.0\r\n";
            
$headers.= "Content-Type: multipart/mixed; boundary=\"{$this->uid}\"\r\n";
            
$headers.= "--{$this->uid}\r\n";
            
$headers.= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
            
$headers.= "Content-Trasfer-Encoding: 7bit\r\n\r\n";
            
$headers.= $this->message "\r\n\r\n";
            
            if(!empty(
$this->attachments) && is_array($this->attachments)) {
                foreach(
$this->attachments as $attactment) {
                    
$headers.= "--{$this->uid}\r\n";
                    
$headers.= "Content-Type: application/octet-stream; name=\"{$attactment['Name']}\"\r\n";
                    
$headers.= "Content-Transfer-Encoding: base64\r\n";
                    
$headers.= "Content-Disposition: attachment; filename=\"{$attactment['Name']}\"\r\n\r\n";
                    
$headers.= $attactment['Content'] . "\r\n\r\n";
                }
            }
            
            
$headers.= "--{$this->uid}--";
            
            if(
mail($to$from""$headers)) {
                return 
true;
            }
            return 
false;
        }
        
        private function 
GenerateRandomFilename($extension) {
            
$chars "abcdefghijklmnopqrstuvwxyz0123456789";
            
$salt "";
            
            for(
$i 0$i mt_rand(525); $i++) {
                
$salt.= $chars{$i};    
            }
            
            
$fname md5($salt time());
            return 
$fname $extension
        }
    }
?>
__________________
Code { for thought }
"My programs never have bugs, they just develop random features."

Last edited by Cborrow : 16-07-2008 at 08:14.
  Reply With Quote
Old 13-07-2008, 12:17   #45 (permalink)
boycoda
Registered User
 
Join Date: Jul 2008
Posts: 10
Damn, i've only recently gotten into functions / classes. So much to learn, already found one or two things in this thread that are helpful. Good stuff!
  Reply With Quote
Old 15-07-2008, 20:55   #46 (permalink)
SpooF
Registered User
 
SpooF's Avatar
 
Join Date: Feb 2007
Posts: 31
PHP Code:
function &load_class($class$instantiate TRUE) {
    static 
$objects = array();
    
    if(isset(
$objects[$class]))
    {
        return 
$objects[$class];
    }
    
    if (
file_exists(PATH.$class.EXT))
    {
        require(
PATH.$class.EXT);
    }
    
    if (
$instantiate == FALSE)
    {
        
$objects[$class] = TRUE;
        return 
$objects[$class];
    }
    
    
$name strtolower($class);
    
    
$objects[$class] =& new $name();
    return 
$objects[$class];



To just you just have the define a PATH and EXT variable.

PATH: path way to your classes.
EXT: extension for you classes (.php)

PHP Code:
class myClass() {

    function 
_load($load) {
        foreach(
$autoload as $key=>$class) {
            
$this->$key =& load_class($class);
        }    
    }

}

$load = array(
    
'someClass'=>'someClass'
);
$class = new myClass();
$class->_load($load);

$class->someClass->function(); 
  Reply With Quote
Old 15-07-2008, 22:23   #47 (permalink)
hobolooter
Registered User
 
hobolooter's Avatar
 
Join Date: Feb 2004
Location: USA
Posts: 72
Send a message via AIM to hobolooter
Quote:
Originally Posted by proc355
it's hashing not encryption; also, dont multi-hash things - it does nothing except chew resources - you want a salted hash

Most of your comments were useless unlike the code he wrote. Honestly, commenting on coding convention and variable naming is just obnoxious.

The comment I quoted, however, irked me the most, especially since it was never replied to. Multi-hashing is actually very useful especially if you know how hashes are reversed. If you hash a hash with a salt, chances are you will have a more secure result string than had you just used a salted hash. You are correct though it is not encryption.

Also wasting resources? C'mon man. Don't post if you are just going to be making stuff up. An MD5 hash calculation on a 16 byte string is ridiculous to even consider a resource hog.
  Reply With Quote
Old 16-07-2008, 00:47   #48 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,713
Kind of late for that - that post was over half a year old. I don't think proc has even been around for a few months. But that being said, I've read quite a bit on salting and multi-hashing, and there are some arguments that show multi-hashing to actually reduce security. I didn't really understand it myself (beyond me), but the people who were making those arguments made them well.
__________________
This space for rent.

Dads Japan
After Hours Japan
  Reply With Quote
Old 16-07-2008, 08:07   #49 (permalink)
RaelRode
Designers are strange :)
 
RaelRode's Avatar
 
Join Date: Jan 2007
Location: Shrewsbury, UK
Posts: 1,730
Send a message via ICQ to RaelRode Send a message via AIM to RaelRode Send a message via MSN to RaelRode Send a message via Yahoo to RaelRode Send a message via Skype™ to RaelRode
Quote:
Originally Posted by hobolooter
Most of your comments were useless unlike the code he wrote. Honestly, commenting on coding convention and variable naming is just obnoxious.

The comment I quoted, however, irked me the most, especially since it was never replied to. Multi-hashing is actually very useful especially if you know how hashes are reversed. If you hash a hash with a salt, chances are you will have a more secure result string than had you just used a salted hash. You are correct though it is not encryption.

Also wasting resources? C'mon man. Don't post if you are just going to be making stuff up. An MD5 hash calculation on a 16 byte string is ridiculous to even consider a resource hog.

Multi-hashing increases the chances of a collision. i.e. two words hashed over and voer could result in the same hash at the end.

Because passwords usually aren't the same, they're random, especially if you have to have a number in there. But if you hash it with md5, you get a 32 character string, then hash that another 32 character string, so they're always 32 characters long. hashing over and over is bad practice, and it's better to use a dynamic and static salt.
__________________
If it works, it's valid.
  Reply With Quote
Old 16-07-2008, 09:26   #50 (permalink)
hobolooter
Registered User
 
hobolooter's Avatar
 
Join Date: Feb 2004
Location: USA
Posts: 72
Send a message via AIM to hobolooter
Quote:
Originally Posted by RaelRode
Multi-hashing increases the chances of a collision. i.e. two words hashed over and voer could result in the same hash at the end.

Because passwords usually aren't the same, they're random, especially if you have to have a number in there. But if you hash it with md5, you get a 32 character string, then hash that another 32 character string, so they're always 32 characters long. hashing over and over is bad practice, and it's better to use a dynamic and static salt.

Since this is a thread about PHP code and not the benefits of hashing, I'll drop it. However, if anything gets taken away from my previous post is that it is not completely pointless to multi-hash and it is not a resource hog as previously stated.

Granted that post was a million years old, but this thread is the only stickied in this subforum. This thread is read, and I think it should at least be addressed.
  Reply With Quote
Old 18-07-2008, 09:22   #51 (permalink)
MikeMackay
Everything is fine.
 
MikeMackay's Avatar
 
Join Date: Feb 2005
Location: Witham & London
Posts: 845
Send a message via MSN to MikeMackay Send a message via Skype™ to MikeMackay
Quote:
Originally Posted by seen.to
Not a function as such. Something I add to combat mail injection spammers. I've since added more but this is the version I have at hand:
PHP Code:
$email preg_replace("/\r/"""$email);
$email preg_replace("/\n/"""$email);
if (
eregi("cc"$email) || eregi("bcc"$email) || 
eregi("to"$email) || eregi("Content-Type"$email)) 
die(
"Sorry, there was a problem with your message being sent. 
Please click <a href=\"javascript:history.go(-1)\">here</a> to try again"
); 
Unfortunately that code will reject the mail if the address was something like:

rotten.tomato@domain.com
rebecca.bloggs@domain.com

Reason being is that "eregi" is returning true on any occurrence of 'to' or 'cc' whether it's encapsulated in a legitimate word/address or not. Surely it would be better to do this instead:

PHP Code:
$email preg_replace("/\r/"""$email);
$email preg_replace("/\n/"""$email);
if (
eregi("cc:"$email) || eregi("bcc:"$email) || 
eregi("to:"$email) || eregi("Content-Type"$email)) 

By adding the colon to the expression match you are eliminating the chances of blocking potentially legitimate address through while still checking for any mail injection headers.
__________________
  Reply With Quote
Old 18-07-2008, 09:33   #52 (permalink)
freelancr
Senior Member
 
freelancr's Avatar
 
Join Date: Oct 2006
Posts: 2,203
Quote:
Originally Posted by hobolooter
Also wasting resources? C'mon man. Don't post if you are just going to be making stuff up. An MD5 hash calculation on a 16 byte string is ridiculous to even consider a resource hog.

Hashing is quite CPU intensive compared to other tasks. Obviously if you have a website that isn't busy, and is on a dedicated server, then you should be fine. But if you are designing the login for Google accounts, then you need something as efficient as you can get.

MD5 hashing isn't secure anymore, you should use SHA-512 instead, and salt the hash rather than hashing multiple times.
  Reply With Quote
Old 22-08-2008, 08:57   #53 (permalink)
DaveChild
Registered User
 
Join Date: Aug 2008
Posts: 8
Quote:
Originally Posted by freelancr
Validate E-Mail Address:

That email validation function was originally from my site, and is a bit out of date now (especially since the opening up of the TLD system). I rewrote the code and released it a little while ago under an open source license - see addedbytes.com/blog/email-address-validation-v2/

Here's a couple of functions I find useful. They add and remove variables from querystrings (great if you're dynamically building querystrings, especially on listing pages with listing options).

Add a Variable to a Querystring

PHP Code:
function add_querystring_var($url$key$value) {
    
$url preg_replace('/(.*)(\?|&)' $key '=[^&]+?(&)(.*)/i''$1$2$4'$url '&');
    
$url substr($url0, -1);
    if (
strpos($url'?') === false) {
        return (
$url '?' $key '=' $value);
    } else {
        return (
$url '&' $key '=' $value);
    }


Remove a Variable from a Querystring

PHP Code:
function remove_querystring_var($url$key) {
    
$url preg_replace('/(.*)(\?|&)' $key '=[^&]+?(&)(.*)/i''$1$2$4'$url '&');
    
$url substr($url0, -1);
    return (
$url);

  Reply With Quote
Old 25-08-2008, 22:46   #54 (permalink)
feha
Design Destroyer
 
feha's Avatar
 
Join Date: Aug 2008
Location: Somewhere in Universe
Posts: 200
Sorry, You don't deserve this.

Last edited by feha : 17-09-2008 at 06:24.
  Reply With Quote